diff --git a/media/livestream/src/main/java/com/example/livestream/CreateChannel.java b/media/livestream/src/main/java/com/example/livestream/CreateChannel.java new file mode 100644 index 00000000000..dd728eef960 --- /dev/null +++ b/media/livestream/src/main/java/com/example/livestream/CreateChannel.java @@ -0,0 +1,133 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.livestream; + +// [START livestream_create_channel] + +import com.google.cloud.video.livestream.v1.AudioStream; +import com.google.cloud.video.livestream.v1.Channel; +import com.google.cloud.video.livestream.v1.Channel.Output; +import com.google.cloud.video.livestream.v1.CreateChannelRequest; +import com.google.cloud.video.livestream.v1.ElementaryStream; +import com.google.cloud.video.livestream.v1.InputAttachment; +import com.google.cloud.video.livestream.v1.InputName; +import com.google.cloud.video.livestream.v1.LivestreamServiceClient; +import com.google.cloud.video.livestream.v1.LocationName; +import com.google.cloud.video.livestream.v1.Manifest; +import com.google.cloud.video.livestream.v1.Manifest.ManifestType; +import com.google.cloud.video.livestream.v1.MuxStream; +import com.google.cloud.video.livestream.v1.SegmentSettings; +import com.google.cloud.video.livestream.v1.VideoStream; +import com.google.cloud.video.livestream.v1.VideoStream.H264CodecSettings; +import com.google.protobuf.Duration; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class CreateChannel { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String location = "us-central1"; + String channelId = "my-channel-id"; + String inputId = "my-input-id"; + String outputUri = "gs://my-bucket/my-output-folder/"; + + createChannel(projectId, location, channelId, inputId, outputUri); + } + + public static void createChannel( + String projectId, String location, String channelId, String inputId, String outputUri) + throws InterruptedException, ExecutionException, TimeoutException, IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // 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-input") + .setInput(InputName.of(projectId, location, inputId).toString()) + .build()) + .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()); + } + } +} +// [END livestream_create_channel] diff --git a/media/livestream/src/main/java/com/example/livestream/CreateChannelWithBackupInput.java b/media/livestream/src/main/java/com/example/livestream/CreateChannelWithBackupInput.java new file mode 100644 index 00000000000..a069badc850 --- /dev/null +++ b/media/livestream/src/main/java/com/example/livestream/CreateChannelWithBackupInput.java @@ -0,0 +1,152 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.livestream; + +// [START livestream_create_channel_with_backup_input] + +import com.google.cloud.video.livestream.v1.AudioStream; +import com.google.cloud.video.livestream.v1.Channel; +import com.google.cloud.video.livestream.v1.Channel.Output; +import com.google.cloud.video.livestream.v1.CreateChannelRequest; +import com.google.cloud.video.livestream.v1.ElementaryStream; +import com.google.cloud.video.livestream.v1.InputAttachment; +import com.google.cloud.video.livestream.v1.InputAttachment.AutomaticFailover; +import com.google.cloud.video.livestream.v1.InputName; +import com.google.cloud.video.livestream.v1.LivestreamServiceClient; +import com.google.cloud.video.livestream.v1.LocationName; +import com.google.cloud.video.livestream.v1.Manifest; +import com.google.cloud.video.livestream.v1.Manifest.ManifestType; +import com.google.cloud.video.livestream.v1.MuxStream; +import com.google.cloud.video.livestream.v1.SegmentSettings; +import com.google.cloud.video.livestream.v1.VideoStream; +import com.google.cloud.video.livestream.v1.VideoStream.H264CodecSettings; +import com.google.protobuf.Duration; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class CreateChannelWithBackupInput { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String location = "us-central1"; + String channelId = "my-channel-id"; + String primaryInputId = "my-primary-input-id"; + String backupInputId = "my-backup-input-id"; + String outputUri = "gs://my-bucket/my-output-folder/"; + + createChannelWithBackupInput( + projectId, location, channelId, primaryInputId, backupInputId, outputUri); + } + + public static void createChannelWithBackupInput( + String projectId, + String location, + String channelId, + String primaryInputId, + String backupInputId, + String outputUri) + throws InterruptedException, ExecutionException, TimeoutException, IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // 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()); + } + } +} +// [END livestream_create_channel_with_backup_input] diff --git a/media/livestream/src/main/java/com/example/livestream/CreateInput.java b/media/livestream/src/main/java/com/example/livestream/CreateInput.java index e0c821cf9f8..c78fa3ddea6 100644 --- a/media/livestream/src/main/java/com/example/livestream/CreateInput.java +++ b/media/livestream/src/main/java/com/example/livestream/CreateInput.java @@ -18,12 +18,10 @@ // [START livestream_create_input] -import com.google.api.gax.longrunning.OperationFuture; import com.google.cloud.video.livestream.v1.CreateInputRequest; import com.google.cloud.video.livestream.v1.Input; import com.google.cloud.video.livestream.v1.LivestreamServiceClient; import com.google.cloud.video.livestream.v1.LocationName; -import com.google.cloud.video.livestream.v1.OperationMetadata; import java.io.IOException; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -53,9 +51,8 @@ public static void createInput(String projectId, String location, String inputId .setInput(Input.newBuilder().setType(Input.Type.RTMP_PUSH).build()) .build(); - OperationFuture call = - livestreamServiceClient.createInputAsync(createInputRequest); - Input result = call.get(1, TimeUnit.MINUTES); + Input result = + livestreamServiceClient.createInputAsync(createInputRequest).get(1, TimeUnit.MINUTES); System.out.println("Input: " + result.getName()); } } diff --git a/media/livestream/src/main/java/com/example/livestream/DeleteChannel.java b/media/livestream/src/main/java/com/example/livestream/DeleteChannel.java new file mode 100644 index 00000000000..ce91857f82c --- /dev/null +++ b/media/livestream/src/main/java/com/example/livestream/DeleteChannel.java @@ -0,0 +1,56 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.livestream; + +// [START livestream_delete_channel] + +import com.google.cloud.video.livestream.v1.ChannelName; +import com.google.cloud.video.livestream.v1.DeleteChannelRequest; +import com.google.cloud.video.livestream.v1.LivestreamServiceClient; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class DeleteChannel { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String location = "us-central1"; + String channelId = "my-channel-id"; + + deleteChannel(projectId, location, channelId); + } + + public static void deleteChannel(String projectId, String location, String channelId) + throws InterruptedException, ExecutionException, TimeoutException, IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // 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"); + } + } +} +// [END livestream_delete_channel] diff --git a/media/livestream/src/main/java/com/example/livestream/GetChannel.java b/media/livestream/src/main/java/com/example/livestream/GetChannel.java new file mode 100644 index 00000000000..da303c0f198 --- /dev/null +++ b/media/livestream/src/main/java/com/example/livestream/GetChannel.java @@ -0,0 +1,49 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.livestream; + +// [START livestream_get_channel] + +import com.google.cloud.video.livestream.v1.Channel; +import com.google.cloud.video.livestream.v1.ChannelName; +import com.google.cloud.video.livestream.v1.LivestreamServiceClient; +import java.io.IOException; + +public class GetChannel { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String location = "us-central1"; + String channelId = "my-channel-id"; + + getChannel(projectId, location, channelId); + } + + public static void getChannel(String projectId, String location, String channelId) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // 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); + Channel response = livestreamServiceClient.getChannel(name); + System.out.println("Channel: " + response.getName()); + } + } +} +// [END livestream_get_channel] diff --git a/media/livestream/src/main/java/com/example/livestream/ListChannels.java b/media/livestream/src/main/java/com/example/livestream/ListChannels.java new file mode 100644 index 00000000000..eb8a9c83d22 --- /dev/null +++ b/media/livestream/src/main/java/com/example/livestream/ListChannels.java @@ -0,0 +1,57 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.livestream; + +// [START livestream_list_channels] + +import com.google.cloud.video.livestream.v1.Channel; +import com.google.cloud.video.livestream.v1.ListChannelsRequest; +import com.google.cloud.video.livestream.v1.LivestreamServiceClient; +import com.google.cloud.video.livestream.v1.LocationName; +import java.io.IOException; + +public class ListChannels { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String location = "us-central1"; + + listChannels(projectId, location); + } + + public static void listChannels(String projectId, String location) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (LivestreamServiceClient livestreamServiceClient = LivestreamServiceClient.create()) { + var listChannelsRequest = + ListChannelsRequest.newBuilder() + .setParent(LocationName.of(projectId, location).toString()) + .build(); + + LivestreamServiceClient.ListChannelsPagedResponse response = + livestreamServiceClient.listChannels(listChannelsRequest); + System.out.println("Channels:"); + + for (Channel channel : response.iterateAll()) { + System.out.println(channel.getName()); + } + } + } +} +// [END livestream_list_channels] diff --git a/media/livestream/src/main/java/com/example/livestream/StartChannel.java b/media/livestream/src/main/java/com/example/livestream/StartChannel.java new file mode 100644 index 00000000000..79c8f1ba97f --- /dev/null +++ b/media/livestream/src/main/java/com/example/livestream/StartChannel.java @@ -0,0 +1,51 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.livestream; + +// [START livestream_start_channel] + +import com.google.cloud.video.livestream.v1.ChannelName; +import com.google.cloud.video.livestream.v1.LivestreamServiceClient; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class StartChannel { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String location = "us-central1"; + String channelId = "my-channel-id"; + + startChannel(projectId, location, channelId); + } + + public static void startChannel(String projectId, String location, String channelId) + throws InterruptedException, ExecutionException, TimeoutException, IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // 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.startChannelAsync(name).get(1, TimeUnit.MINUTES); + System.out.println("Started channel"); + } + } +} +// [END livestream_start_channel] diff --git a/media/livestream/src/main/java/com/example/livestream/StopChannel.java b/media/livestream/src/main/java/com/example/livestream/StopChannel.java new file mode 100644 index 00000000000..5e3add091fd --- /dev/null +++ b/media/livestream/src/main/java/com/example/livestream/StopChannel.java @@ -0,0 +1,51 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.livestream; + +// [START livestream_stop_channel] + +import com.google.cloud.video.livestream.v1.ChannelName; +import com.google.cloud.video.livestream.v1.LivestreamServiceClient; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class StopChannel { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String location = "us-central1"; + String channelId = "my-channel-id"; + + stopChannel(projectId, location, channelId); + } + + public static void stopChannel(String projectId, String location, String channelId) + throws InterruptedException, ExecutionException, TimeoutException, IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // 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"); + } + } +} +// [END livestream_stop_channel] diff --git a/media/livestream/src/main/java/com/example/livestream/UpdateChannel.java b/media/livestream/src/main/java/com/example/livestream/UpdateChannel.java new file mode 100644 index 00000000000..eb5912db5b7 --- /dev/null +++ b/media/livestream/src/main/java/com/example/livestream/UpdateChannel.java @@ -0,0 +1,72 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.livestream; + +// [START livestream_update_channel] + +import com.google.cloud.video.livestream.v1.Channel; +import com.google.cloud.video.livestream.v1.ChannelName; +import com.google.cloud.video.livestream.v1.InputAttachment; +import com.google.cloud.video.livestream.v1.InputName; +import com.google.cloud.video.livestream.v1.LivestreamServiceClient; +import com.google.cloud.video.livestream.v1.UpdateChannelRequest; +import com.google.protobuf.FieldMask; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class UpdateChannel { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String location = "us-central1"; + String channelId = "my-channel-id"; + String inputId = "my-input-id"; + + updateChannel(projectId, location, channelId, inputId); + } + + public static void updateChannel( + String projectId, String location, String channelId, String inputId) + throws InterruptedException, ExecutionException, TimeoutException, IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (LivestreamServiceClient livestreamServiceClient = LivestreamServiceClient.create()) { + var updateChannelRequest = + UpdateChannelRequest.newBuilder() + .setChannel( + Channel.newBuilder() + .setName(ChannelName.of(projectId, location, channelId).toString()) + .addInputAttachments( + 0, + InputAttachment.newBuilder() + .setKey("updated-input") + .setInput(InputName.of(projectId, location, inputId).toString()) + .build())) + .setUpdateMask(FieldMask.newBuilder().addPaths("input_attachments").build()) + .build(); + + Channel result = + livestreamServiceClient.updateChannelAsync(updateChannelRequest).get(1, TimeUnit.MINUTES); + System.out.println("Updated channel: " + result.getName()); + } + } +} +// [END livestream_update_channel] diff --git a/media/livestream/src/main/java/com/example/livestream/UpdateInput.java b/media/livestream/src/main/java/com/example/livestream/UpdateInput.java index a718c8a3595..633f4f87f56 100644 --- a/media/livestream/src/main/java/com/example/livestream/UpdateInput.java +++ b/media/livestream/src/main/java/com/example/livestream/UpdateInput.java @@ -18,11 +18,9 @@ // [START livestream_update_input] -import com.google.api.gax.longrunning.OperationFuture; import com.google.cloud.video.livestream.v1.Input; import com.google.cloud.video.livestream.v1.InputName; import com.google.cloud.video.livestream.v1.LivestreamServiceClient; -import com.google.cloud.video.livestream.v1.OperationMetadata; import com.google.cloud.video.livestream.v1.PreprocessingConfig; import com.google.cloud.video.livestream.v1.PreprocessingConfig.Crop; import com.google.cloud.video.livestream.v1.UpdateInputRequest; @@ -62,9 +60,8 @@ public static void updateInput(String projectId, String location, String inputId .setUpdateMask(FieldMask.newBuilder().addPaths("preprocessing_config").build()) .build(); - OperationFuture call = - livestreamServiceClient.updateInputAsync(updateInputRequest); - Input result = call.get(1, TimeUnit.MINUTES); + Input result = + livestreamServiceClient.updateInputAsync(updateInputRequest).get(1, TimeUnit.MINUTES); System.out.println("Updated input: " + result.getName()); } } diff --git a/media/livestream/src/test/java/com/example/livestream/CreateChannelTest.java b/media/livestream/src/test/java/com/example/livestream/CreateChannelTest.java new file mode 100644 index 00000000000..5b1bb25fe5c --- /dev/null +++ b/media/livestream/src/test/java/com/example/livestream/CreateChannelTest.java @@ -0,0 +1,118 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.livestream; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.api.gax.rpc.NotFoundException; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class CreateChannelTest { + + private static final String LOCATION = "us-central1"; + private static final String CHANNEL_ID = + "my-channel-" + UUID.randomUUID().toString().substring(0, 25); + private static final String INPUT_ID = + "my-input-" + UUID.randomUUID().toString().substring(0, 25); + private static final String OUTPUT_URI = "gs://my-bucket/my-output-folder/"; + + private static String PROJECT_ID; + private static String CHANNEL_NAME; + private static PrintStream originalOut; + private ByteArrayOutputStream bout; + + private static String requireEnvVar(String varName) { + String varValue = System.getenv(varName); + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName)); + return varValue; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void beforeTest() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + // Clean up old resources in the test project. + TestUtils.cleanAllStale(PROJECT_ID, LOCATION); + + originalOut = System.out; + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + CHANNEL_NAME = + String.format("projects/%s/locations/%s/channels/%s", PROJECT_ID, LOCATION, CHANNEL_ID); + try { + DeleteChannel.deleteChannel(PROJECT_ID, LOCATION, CHANNEL_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + // Don't worry if the channel doesn't already exist. + } + + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + // Don't worry if the input doesn't already exist. + } + CreateInput.createInput(PROJECT_ID, LOCATION, INPUT_ID); + bout.reset(); + } + + @Test + public void test_CreateChannel() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + CreateChannel.createChannel(PROJECT_ID, LOCATION, CHANNEL_ID, INPUT_ID, OUTPUT_URI); + String output = bout.toString(); + assertThat(output, containsString(CHANNEL_NAME)); + bout.reset(); + } + + @After + public void tearDown() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + try { + DeleteChannel.deleteChannel(PROJECT_ID, LOCATION, CHANNEL_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + System.out.printf(String.valueOf(e)); + } + + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + System.out.printf(String.valueOf(e)); + } + System.setOut(originalOut); + bout.reset(); + } +} diff --git a/media/livestream/src/test/java/com/example/livestream/CreateChannelWithBackupInputTest.java b/media/livestream/src/test/java/com/example/livestream/CreateChannelWithBackupInputTest.java new file mode 100644 index 00000000000..f7e7e6d2cd3 --- /dev/null +++ b/media/livestream/src/test/java/com/example/livestream/CreateChannelWithBackupInputTest.java @@ -0,0 +1,136 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.livestream; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.api.gax.rpc.NotFoundException; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class CreateChannelWithBackupInputTest { + + private static final String LOCATION = "us-central1"; + private static final String CHANNEL_ID = + "my-channel-" + UUID.randomUUID().toString().substring(0, 25); + private static final String PRIMARY_INPUT_ID = + "my-primary-input-" + UUID.randomUUID().toString().substring(0, 25); + private static final String BACKUP_INPUT_ID = + "my-backup-input-" + UUID.randomUUID().toString().substring(0, 25); + private static final String OUTPUT_URI = "gs://my-bucket/my-output-folder/"; + + private static String PROJECT_ID; + private static String CHANNEL_NAME; + private static PrintStream originalOut; + private ByteArrayOutputStream bout; + + private static String requireEnvVar(String varName) { + String varValue = System.getenv(varName); + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName)); + return varValue; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void beforeTest() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + // Clean up old resources in the test project. + TestUtils.cleanAllStale(PROJECT_ID, LOCATION); + + originalOut = System.out; + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + CHANNEL_NAME = + String.format("projects/%s/locations/%s/channels/%s", PROJECT_ID, LOCATION, CHANNEL_ID); + try { + DeleteChannel.deleteChannel(PROJECT_ID, LOCATION, CHANNEL_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + // Don't worry if the channel doesn't already exist. + } + + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, PRIMARY_INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + // Don't worry if the input doesn't already exist. + } + + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, BACKUP_INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + // Don't worry if the input doesn't already exist. + } + + CreateInput.createInput(PROJECT_ID, LOCATION, PRIMARY_INPUT_ID); + CreateInput.createInput(PROJECT_ID, LOCATION, BACKUP_INPUT_ID); + + bout.reset(); + } + + @Test + public void test_CreateChannelWithBackupInput() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + CreateChannelWithBackupInput.createChannelWithBackupInput( + PROJECT_ID, LOCATION, CHANNEL_ID, PRIMARY_INPUT_ID, BACKUP_INPUT_ID, OUTPUT_URI); + String output = bout.toString(); + assertThat(output, containsString(CHANNEL_NAME)); + bout.reset(); + } + + @After + public void tearDown() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + try { + DeleteChannel.deleteChannel(PROJECT_ID, LOCATION, CHANNEL_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + System.out.printf(String.valueOf(e)); + } + + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, PRIMARY_INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + System.out.printf(String.valueOf(e)); + } + + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, BACKUP_INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + System.out.printf(String.valueOf(e)); + } + System.setOut(originalOut); + bout.reset(); + } +} diff --git a/media/livestream/src/test/java/com/example/livestream/DeleteChannelTest.java b/media/livestream/src/test/java/com/example/livestream/DeleteChannelTest.java new file mode 100644 index 00000000000..1cfdc017afb --- /dev/null +++ b/media/livestream/src/test/java/com/example/livestream/DeleteChannelTest.java @@ -0,0 +1,110 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.livestream; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.api.gax.rpc.NotFoundException; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class DeleteChannelTest { + + private static final String LOCATION = "us-central1"; + private static String PROJECT_ID; + private static final String CHANNEL_ID = + "my-channel-" + UUID.randomUUID().toString().substring(0, 25); + private static final String INPUT_ID = + "my-input-" + UUID.randomUUID().toString().substring(0, 25); + private static final String OUTPUT_URI = "gs://my-bucket/my-output-folder/"; + + private static PrintStream originalOut; + private ByteArrayOutputStream bout; + + private static String requireEnvVar(String varName) { + String varValue = System.getenv(varName); + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName)); + return varValue; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void beforeTest() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + // Clean up old resources in the test project. + TestUtils.cleanAllStale(PROJECT_ID, LOCATION); + + originalOut = System.out; + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + try { + DeleteChannel.deleteChannel(PROJECT_ID, LOCATION, CHANNEL_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + // Don't worry if the channel doesn't already exist. + } + + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + // Don't worry if the input doesn't already exist. + } + + CreateInput.createInput(PROJECT_ID, LOCATION, INPUT_ID); + CreateChannel.createChannel(PROJECT_ID, LOCATION, CHANNEL_ID, INPUT_ID, OUTPUT_URI); + + bout.reset(); + } + + @Test + public void test_DeleteChannel() throws Exception { + DeleteChannel.deleteChannel(PROJECT_ID, LOCATION, CHANNEL_ID); + String output = bout.toString(); + assertThat(output, containsString("Deleted channel")); + bout.reset(); + } + + @After + public void tearDown() throws IOException { + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + System.out.printf(String.valueOf(e)); + } + System.setOut(originalOut); + bout.reset(); + } +} diff --git a/media/livestream/src/test/java/com/example/livestream/GetChannelTest.java b/media/livestream/src/test/java/com/example/livestream/GetChannelTest.java new file mode 100644 index 00000000000..d732a61540b --- /dev/null +++ b/media/livestream/src/test/java/com/example/livestream/GetChannelTest.java @@ -0,0 +1,120 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.livestream; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.api.gax.rpc.NotFoundException; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class GetChannelTest { + + private static final String LOCATION = "us-central1"; + private static String PROJECT_ID; + private static final String CHANNEL_ID = + "my-channel-" + UUID.randomUUID().toString().substring(0, 25); + private static final String INPUT_ID = + "my-input-" + UUID.randomUUID().toString().substring(0, 25); + private static String CHANNEL_NAME; + private static final String OUTPUT_URI = "gs://my-bucket/my-output-folder/"; + + private static PrintStream originalOut; + private ByteArrayOutputStream bout; + + private static String requireEnvVar(String varName) { + String varValue = System.getenv(varName); + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName)); + return varValue; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void beforeTest() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + // Clean up old resources in the test project. + TestUtils.cleanAllStale(PROJECT_ID, LOCATION); + + originalOut = System.out; + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + CHANNEL_NAME = + String.format("projects/%s/locations/%s/channels/%s", PROJECT_ID, LOCATION, CHANNEL_ID); + try { + DeleteChannel.deleteChannel(PROJECT_ID, LOCATION, CHANNEL_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + // Don't worry if the channel doesn't already exist. + } + + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + // Don't worry if the input doesn't already exist. + } + + CreateInput.createInput(PROJECT_ID, LOCATION, INPUT_ID); + CreateChannel.createChannel(PROJECT_ID, LOCATION, CHANNEL_ID, INPUT_ID, OUTPUT_URI); + + bout.reset(); + } + + @Test + public void test_GetChannel() throws Exception { + GetChannel.getChannel(PROJECT_ID, LOCATION, CHANNEL_ID); + String output = bout.toString(); + assertThat(output, containsString(CHANNEL_NAME)); + bout.reset(); + } + + @After + public void tearDown() throws IOException { + + try { + DeleteChannel.deleteChannel(PROJECT_ID, LOCATION, CHANNEL_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + System.out.printf(String.valueOf(e)); + } + + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + System.out.printf(String.valueOf(e)); + } + System.setOut(originalOut); + bout.reset(); + } +} diff --git a/media/livestream/src/test/java/com/example/livestream/ListChannelsTest.java b/media/livestream/src/test/java/com/example/livestream/ListChannelsTest.java new file mode 100644 index 00000000000..9a72d89df0b --- /dev/null +++ b/media/livestream/src/test/java/com/example/livestream/ListChannelsTest.java @@ -0,0 +1,120 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.livestream; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.api.gax.rpc.NotFoundException; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class ListChannelsTest { + + private static final String LOCATION = "us-central1"; + private static String PROJECT_ID; + private static final String CHANNEL_ID = + "my-channel-" + UUID.randomUUID().toString().substring(0, 25); + private static final String INPUT_ID = + "my-input-" + UUID.randomUUID().toString().substring(0, 25); + private static String CHANNEL_NAME; + private static final String OUTPUT_URI = "gs://my-bucket/my-output-folder/"; + + private static PrintStream originalOut; + private ByteArrayOutputStream bout; + + private static String requireEnvVar(String varName) { + String varValue = System.getenv(varName); + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName)); + return varValue; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void beforeTest() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + // Clean up old resources in the test project. + TestUtils.cleanAllStale(PROJECT_ID, LOCATION); + + originalOut = System.out; + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + CHANNEL_NAME = + String.format("projects/%s/locations/%s/channels/%s", PROJECT_ID, LOCATION, CHANNEL_ID); + try { + DeleteChannel.deleteChannel(PROJECT_ID, LOCATION, CHANNEL_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + // Don't worry if the channel doesn't already exist. + } + + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + // Don't worry if the input doesn't already exist. + } + + CreateInput.createInput(PROJECT_ID, LOCATION, INPUT_ID); + CreateChannel.createChannel(PROJECT_ID, LOCATION, CHANNEL_ID, INPUT_ID, OUTPUT_URI); + + bout.reset(); + } + + @Test + public void test_ListChannels() throws Exception { + ListChannels.listChannels(PROJECT_ID, LOCATION); + String output = bout.toString(); + assertThat(output, containsString(CHANNEL_NAME)); + bout.reset(); + } + + @After + public void tearDown() throws IOException { + + try { + DeleteChannel.deleteChannel(PROJECT_ID, LOCATION, CHANNEL_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + System.out.printf(String.valueOf(e)); + } + + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + System.out.printf(String.valueOf(e)); + } + System.setOut(originalOut); + bout.reset(); + } +} diff --git a/media/livestream/src/test/java/com/example/livestream/StartChannelTest.java b/media/livestream/src/test/java/com/example/livestream/StartChannelTest.java new file mode 100644 index 00000000000..060ad97f44f --- /dev/null +++ b/media/livestream/src/test/java/com/example/livestream/StartChannelTest.java @@ -0,0 +1,119 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.livestream; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.api.gax.rpc.NotFoundException; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class StartChannelTest { + + private static final String LOCATION = "us-central1"; + private static String PROJECT_ID; + private static final String CHANNEL_ID = + "my-channel-" + UUID.randomUUID().toString().substring(0, 25); + private static final String INPUT_ID = + "my-input-" + UUID.randomUUID().toString().substring(0, 25); + private static final String OUTPUT_URI = "gs://my-bucket/my-output-folder/"; + + private static PrintStream originalOut; + private ByteArrayOutputStream bout; + + private static String requireEnvVar(String varName) { + String varValue = System.getenv(varName); + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName)); + return varValue; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void beforeTest() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + // Clean up old resources in the test project. + TestUtils.cleanAllStale(PROJECT_ID, LOCATION); + + originalOut = System.out; + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + try { + DeleteChannel.deleteChannel(PROJECT_ID, LOCATION, CHANNEL_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + // Don't worry if the channel doesn't already exist. + } + + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + // Don't worry if the input doesn't already exist. + } + + CreateInput.createInput(PROJECT_ID, LOCATION, INPUT_ID); + CreateChannel.createChannel(PROJECT_ID, LOCATION, CHANNEL_ID, INPUT_ID, OUTPUT_URI); + + bout.reset(); + } + + @Test + public void test_StartChannel() throws Exception { + StartChannel.startChannel(PROJECT_ID, LOCATION, CHANNEL_ID); + String output = bout.toString(); + assertThat(output, containsString("Started channel")); + bout.reset(); + } + + @After + public void tearDown() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + + StopChannel.stopChannel(PROJECT_ID, LOCATION, CHANNEL_ID); + try { + DeleteChannel.deleteChannel(PROJECT_ID, LOCATION, CHANNEL_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + System.out.printf(String.valueOf(e)); + } + + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + System.out.printf(String.valueOf(e)); + } + System.setOut(originalOut); + bout.reset(); + } +} diff --git a/media/livestream/src/test/java/com/example/livestream/StopChannelTest.java b/media/livestream/src/test/java/com/example/livestream/StopChannelTest.java new file mode 100644 index 00000000000..c37ca3fb9ca --- /dev/null +++ b/media/livestream/src/test/java/com/example/livestream/StopChannelTest.java @@ -0,0 +1,119 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.livestream; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.api.gax.rpc.NotFoundException; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class StopChannelTest { + + private static final String LOCATION = "us-central1"; + private static String PROJECT_ID; + private static final String CHANNEL_ID = + "my-channel-" + UUID.randomUUID().toString().substring(0, 25); + private static final String INPUT_ID = + "my-input-" + UUID.randomUUID().toString().substring(0, 25); + private static final String OUTPUT_URI = "gs://my-bucket/my-output-folder/"; + + private static PrintStream originalOut; + private ByteArrayOutputStream bout; + + private static String requireEnvVar(String varName) { + String varValue = System.getenv(varName); + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName)); + return varValue; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void beforeTest() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + // Clean up old resources in the test project. + TestUtils.cleanAllStale(PROJECT_ID, LOCATION); + + originalOut = System.out; + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + try { + DeleteChannel.deleteChannel(PROJECT_ID, LOCATION, CHANNEL_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + // Don't worry if the channel doesn't already exist. + } + + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + // Don't worry if the input doesn't already exist. + } + + CreateInput.createInput(PROJECT_ID, LOCATION, INPUT_ID); + CreateChannel.createChannel(PROJECT_ID, LOCATION, CHANNEL_ID, INPUT_ID, OUTPUT_URI); + StartChannel.startChannel(PROJECT_ID, LOCATION, CHANNEL_ID); + + bout.reset(); + } + + @Test + public void test_StopChannel() throws Exception { + StopChannel.stopChannel(PROJECT_ID, LOCATION, CHANNEL_ID); + String output = bout.toString(); + assertThat(output, containsString("Stopped channel")); + bout.reset(); + } + + @After + public void tearDown() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + + try { + DeleteChannel.deleteChannel(PROJECT_ID, LOCATION, CHANNEL_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + System.out.printf(String.valueOf(e)); + } + + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + System.out.printf(String.valueOf(e)); + } + System.setOut(originalOut); + bout.reset(); + } +} diff --git a/media/livestream/src/test/java/com/example/livestream/TestUtils.java b/media/livestream/src/test/java/com/example/livestream/TestUtils.java index 5d01ac28df5..0f33fc9d414 100644 --- a/media/livestream/src/test/java/com/example/livestream/TestUtils.java +++ b/media/livestream/src/test/java/com/example/livestream/TestUtils.java @@ -17,8 +17,11 @@ package com.example.livestream; import com.google.api.gax.rpc.NotFoundException; +import com.google.cloud.video.livestream.v1.Channel; +import com.google.cloud.video.livestream.v1.DeleteChannelRequest; import com.google.cloud.video.livestream.v1.DeleteInputRequest; import com.google.cloud.video.livestream.v1.Input; +import com.google.cloud.video.livestream.v1.ListChannelsRequest; import com.google.cloud.video.livestream.v1.ListInputsRequest; import com.google.cloud.video.livestream.v1.LivestreamServiceClient; import com.google.cloud.video.livestream.v1.LocationName; @@ -32,6 +35,11 @@ public class TestUtils { private static final int DELETION_THRESHOLD_TIME_HOURS_IN_SECONDS = 10800; // 3 hours + public static void cleanAllStale(String projectId, String location) { + cleanStaleChannels(projectId, location); + cleanStaleInputs(projectId, location); + } + public static void cleanStaleInputs(String projectId, String location) { try (LivestreamServiceClient livestreamServiceClient = LivestreamServiceClient.create()) { var listInputsRequest = @@ -45,10 +53,7 @@ public static void cleanStaleInputs(String projectId, String location) { for (Input input : response.iterateAll()) { if (input.getCreateTime().getSeconds() < Instant.now().getEpochSecond() - DELETION_THRESHOLD_TIME_HOURS_IN_SECONDS) { - var deleteInputRequest = - DeleteInputRequest.newBuilder() - .setName(input.getName()) - .build(); + var deleteInputRequest = DeleteInputRequest.newBuilder().setName(input.getName()).build(); livestreamServiceClient.deleteInputAsync(deleteInputRequest).get(1, TimeUnit.MINUTES); } } @@ -58,4 +63,40 @@ public static void cleanStaleInputs(String projectId, String location) { e.printStackTrace(); } } -} \ No newline at end of file + + 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) { + + 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; + } + 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(); + } + } +} diff --git a/media/livestream/src/test/java/com/example/livestream/UpdateChannelTest.java b/media/livestream/src/test/java/com/example/livestream/UpdateChannelTest.java new file mode 100644 index 00000000000..ba93f2cb37a --- /dev/null +++ b/media/livestream/src/test/java/com/example/livestream/UpdateChannelTest.java @@ -0,0 +1,120 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.livestream; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.api.gax.rpc.NotFoundException; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class UpdateChannelTest { + + private static final String LOCATION = "us-central1"; + private static String PROJECT_ID; + private static final String CHANNEL_ID = + "my-channel-" + UUID.randomUUID().toString().substring(0, 25); + private static final String INPUT_ID = + "my-input-" + UUID.randomUUID().toString().substring(0, 25); + private static String CHANNEL_NAME; + private static final String OUTPUT_URI = "gs://my-bucket/my-output-folder/"; + + private static PrintStream originalOut; + private ByteArrayOutputStream bout; + + private static String requireEnvVar(String varName) { + String varValue = System.getenv(varName); + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName)); + return varValue; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void beforeTest() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + // Clean up old resources in the test project. + TestUtils.cleanAllStale(PROJECT_ID, LOCATION); + + originalOut = System.out; + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + CHANNEL_NAME = + String.format("projects/%s/locations/%s/channels/%s", PROJECT_ID, LOCATION, CHANNEL_ID); + try { + DeleteChannel.deleteChannel(PROJECT_ID, LOCATION, CHANNEL_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + // Don't worry if the channel doesn't already exist. + } + + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + // Don't worry if the input doesn't already exist. + } + + CreateInput.createInput(PROJECT_ID, LOCATION, INPUT_ID); + CreateChannel.createChannel(PROJECT_ID, LOCATION, CHANNEL_ID, INPUT_ID, OUTPUT_URI); + + bout.reset(); + } + + @Test + public void test_UpdateChannel() throws Exception { + UpdateChannel.updateChannel(PROJECT_ID, LOCATION, CHANNEL_ID, INPUT_ID); + String output = bout.toString(); + assertThat(output, containsString(CHANNEL_NAME)); + bout.reset(); + } + + @After + public void tearDown() throws IOException { + + try { + DeleteChannel.deleteChannel(PROJECT_ID, LOCATION, CHANNEL_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + System.out.printf(String.valueOf(e)); + } + + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + System.out.printf(String.valueOf(e)); + } + System.setOut(originalOut); + bout.reset(); + } +}