Search in sources :

Example 11 with LivestreamServiceClient

use of com.google.cloud.video.livestream.v1.LivestreamServiceClient in project java-docs-samples by GoogleCloudPlatform.

the class TestUtils method cleanStaleChannels.

public static void cleanStaleChannels(String projectId, String location) {
    try (LivestreamServiceClient livestreamServiceClient = LivestreamServiceClient.create()) {
        var listChannelsRequest = ListChannelsRequest.newBuilder().setParent(LocationName.of(projectId, location).toString()).build();
        LivestreamServiceClient.ListChannelsPagedResponse response = livestreamServiceClient.listChannels(listChannelsRequest);
        for (Channel channel : response.iterateAll()) {
            if (channel.getCreateTime().getSeconds() < Instant.now().getEpochSecond() - DELETION_THRESHOLD_TIME_HOURS_IN_SECONDS) {
                // Stop the channel
                try {
                    livestreamServiceClient.stopChannelAsync(channel.getName()).get(1, TimeUnit.MINUTES);
                } catch (ExecutionException e) {
                    // Ignore error if the channel isn't stopped or the stop operation times out.
                    e.printStackTrace();
                } catch (NotFoundException | InterruptedException | TimeoutException e) {
                    e.printStackTrace();
                    continue;
                }
                // Delete the channel events
                var listEventsRequest = ListEventsRequest.newBuilder().setParent(channel.getName()).build();
                LivestreamServiceClient.ListEventsPagedResponse eventsResponse = livestreamServiceClient.listEvents(listEventsRequest);
                for (Event event : eventsResponse.iterateAll()) {
                    var deleteEventRequest = DeleteEventRequest.newBuilder().setName(event.getName()).build();
                    livestreamServiceClient.deleteEvent(deleteEventRequest);
                }
                // Delete the channel
                var deleteChannelRequest = DeleteChannelRequest.newBuilder().setName(channel.getName()).build();
                livestreamServiceClient.deleteChannelAsync(deleteChannelRequest).get(1, TimeUnit.MINUTES);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) {
        e.printStackTrace();
    }
}
Also used : Channel(com.google.cloud.video.livestream.v1.Channel) NotFoundException(com.google.api.gax.rpc.NotFoundException) IOException(java.io.IOException) Event(com.google.cloud.video.livestream.v1.Event) ExecutionException(java.util.concurrent.ExecutionException) LivestreamServiceClient(com.google.cloud.video.livestream.v1.LivestreamServiceClient) TimeoutException(java.util.concurrent.TimeoutException)

Example 12 with LivestreamServiceClient

use of com.google.cloud.video.livestream.v1.LivestreamServiceClient in project java-docs-samples by GoogleCloudPlatform.

the class StopChannel method stopChannel.

public static void stopChannel(String projectId, String location, String channelId) throws InterruptedException, ExecutionException, TimeoutException, IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (LivestreamServiceClient livestreamServiceClient = LivestreamServiceClient.create()) {
        ChannelName name = ChannelName.of(projectId, location, channelId);
        livestreamServiceClient.stopChannelAsync(name).get(1, TimeUnit.MINUTES);
        System.out.println("Stopped channel");
    }
}
Also used : ChannelName(com.google.cloud.video.livestream.v1.ChannelName) LivestreamServiceClient(com.google.cloud.video.livestream.v1.LivestreamServiceClient)

Example 13 with LivestreamServiceClient

use of com.google.cloud.video.livestream.v1.LivestreamServiceClient in project java-docs-samples by GoogleCloudPlatform.

the class CreateChannelWithBackupInput method createChannelWithBackupInput.

public static void createChannelWithBackupInput(String projectId, String location, String channelId, String primaryInputId, String backupInputId, String outputUri) throws InterruptedException, ExecutionException, TimeoutException, IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (LivestreamServiceClient livestreamServiceClient = LivestreamServiceClient.create()) {
        VideoStream videoStream = VideoStream.newBuilder().setH264(H264CodecSettings.newBuilder().setProfile("main").setBitrateBps(1000000).setFrameRate(30).setHeightPixels(720).setWidthPixels(1280)).build();
        AudioStream audioStream = AudioStream.newBuilder().setCodec("aac").setChannelCount(2).setBitrateBps(160000).build();
        var createChannelRequest = CreateChannelRequest.newBuilder().setParent(LocationName.of(projectId, location).toString()).setChannelId(channelId).setChannel(Channel.newBuilder().addInputAttachments(0, InputAttachment.newBuilder().setKey("my-primary-input").setInput(InputName.of(projectId, location, primaryInputId).toString()).setAutomaticFailover(AutomaticFailover.newBuilder().addInputKeys("my-backup-input").build()).build()).addInputAttachments(1, InputAttachment.newBuilder().setKey("my-backup-input").setInput(InputName.of(projectId, location, backupInputId).toString())).setOutput(Output.newBuilder().setUri(outputUri).build()).addElementaryStreams(ElementaryStream.newBuilder().setKey("es_video").setVideoStream(videoStream)).addElementaryStreams(ElementaryStream.newBuilder().setKey("es_audio").setAudioStream(audioStream)).addMuxStreams(MuxStream.newBuilder().setKey("mux_video").addElementaryStreams("es_video").setSegmentSettings(SegmentSettings.newBuilder().setSegmentDuration(Duration.newBuilder().setSeconds(2).build()).build()).build()).addMuxStreams(MuxStream.newBuilder().setKey("mux_audio").addElementaryStreams("es_audio").setSegmentSettings(SegmentSettings.newBuilder().setSegmentDuration(Duration.newBuilder().setSeconds(2).build()).build()).build()).addManifests(Manifest.newBuilder().setFileName("manifest.m3u8").setType(ManifestType.HLS).addMuxStreams("mux_video").addMuxStreams("mux_audio").setMaxSegmentCount(5).build())).build();
        Channel result = livestreamServiceClient.createChannelAsync(createChannelRequest).get(1, TimeUnit.MINUTES);
        System.out.println("Channel: " + result.getName());
    }
}
Also used : AudioStream(com.google.cloud.video.livestream.v1.AudioStream) Channel(com.google.cloud.video.livestream.v1.Channel) VideoStream(com.google.cloud.video.livestream.v1.VideoStream) LivestreamServiceClient(com.google.cloud.video.livestream.v1.LivestreamServiceClient)

Example 14 with LivestreamServiceClient

use of com.google.cloud.video.livestream.v1.LivestreamServiceClient in project java-docs-samples by GoogleCloudPlatform.

the class CreateInput method createInput.

public static void createInput(String projectId, String location, String inputId) throws InterruptedException, ExecutionException, TimeoutException, IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (LivestreamServiceClient livestreamServiceClient = LivestreamServiceClient.create()) {
        var createInputRequest = CreateInputRequest.newBuilder().setParent(LocationName.of(projectId, location).toString()).setInputId(inputId).setInput(Input.newBuilder().setType(Input.Type.RTMP_PUSH).build()).build();
        Input result = livestreamServiceClient.createInputAsync(createInputRequest).get(1, TimeUnit.MINUTES);
        System.out.println("Input: " + result.getName());
    }
}
Also used : Input(com.google.cloud.video.livestream.v1.Input) LivestreamServiceClient(com.google.cloud.video.livestream.v1.LivestreamServiceClient)

Example 15 with LivestreamServiceClient

use of com.google.cloud.video.livestream.v1.LivestreamServiceClient in project java-docs-samples by GoogleCloudPlatform.

the class DeleteChannel method deleteChannel.

public static void deleteChannel(String projectId, String location, String channelId) throws InterruptedException, ExecutionException, TimeoutException, IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (LivestreamServiceClient livestreamServiceClient = LivestreamServiceClient.create()) {
        var deleteChannelRequest = DeleteChannelRequest.newBuilder().setName(ChannelName.of(projectId, location, channelId).toString()).build();
        livestreamServiceClient.deleteChannelAsync(deleteChannelRequest).get(1, TimeUnit.MINUTES);
        System.out.println("Deleted channel");
    }
}
Also used : LivestreamServiceClient(com.google.cloud.video.livestream.v1.LivestreamServiceClient)

Aggregations

LivestreamServiceClient (com.google.cloud.video.livestream.v1.LivestreamServiceClient)19 Channel (com.google.cloud.video.livestream.v1.Channel)6 Input (com.google.cloud.video.livestream.v1.Input)5 Event (com.google.cloud.video.livestream.v1.Event)4 ChannelName (com.google.cloud.video.livestream.v1.ChannelName)3 NotFoundException (com.google.api.gax.rpc.NotFoundException)2 AudioStream (com.google.cloud.video.livestream.v1.AudioStream)2 VideoStream (com.google.cloud.video.livestream.v1.VideoStream)2 IOException (java.io.IOException)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 EventName (com.google.cloud.video.livestream.v1.EventName)1 InputName (com.google.cloud.video.livestream.v1.InputName)1