|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.livestream; |
| 18 | + |
| 19 | +// [START livestream_create_channel_with_backup_input] |
| 20 | + |
| 21 | +import com.google.cloud.video.livestream.v1.AudioStream; |
| 22 | +import com.google.cloud.video.livestream.v1.Channel; |
| 23 | +import com.google.cloud.video.livestream.v1.Channel.Output; |
| 24 | +import com.google.cloud.video.livestream.v1.CreateChannelRequest; |
| 25 | +import com.google.cloud.video.livestream.v1.ElementaryStream; |
| 26 | +import com.google.cloud.video.livestream.v1.InputAttachment; |
| 27 | +import com.google.cloud.video.livestream.v1.InputAttachment.AutomaticFailover; |
| 28 | +import com.google.cloud.video.livestream.v1.InputName; |
| 29 | +import com.google.cloud.video.livestream.v1.LivestreamServiceClient; |
| 30 | +import com.google.cloud.video.livestream.v1.LocationName; |
| 31 | +import com.google.cloud.video.livestream.v1.Manifest; |
| 32 | +import com.google.cloud.video.livestream.v1.Manifest.ManifestType; |
| 33 | +import com.google.cloud.video.livestream.v1.MuxStream; |
| 34 | +import com.google.cloud.video.livestream.v1.SegmentSettings; |
| 35 | +import com.google.cloud.video.livestream.v1.VideoStream; |
| 36 | +import com.google.cloud.video.livestream.v1.VideoStream.H264CodecSettings; |
| 37 | +import com.google.protobuf.Duration; |
| 38 | +import java.io.IOException; |
| 39 | +import java.util.concurrent.ExecutionException; |
| 40 | +import java.util.concurrent.TimeUnit; |
| 41 | +import java.util.concurrent.TimeoutException; |
| 42 | + |
| 43 | +public class CreateChannelWithBackupInput { |
| 44 | + |
| 45 | + public static void main(String[] args) throws Exception { |
| 46 | + // TODO(developer): Replace these variables before running the sample. |
| 47 | + String projectId = "my-project-id"; |
| 48 | + String location = "us-central1"; |
| 49 | + String channelId = "my-channel-id"; |
| 50 | + String primaryInputId = "my-primary-input-id"; |
| 51 | + String backupInputId = "my-backup-input-id"; |
| 52 | + String outputUri = "gs://my-bucket/my-output-folder/"; |
| 53 | + |
| 54 | + createChannelWithBackupInput( |
| 55 | + projectId, location, channelId, primaryInputId, backupInputId, outputUri); |
| 56 | + } |
| 57 | + |
| 58 | + public static void createChannelWithBackupInput( |
| 59 | + String projectId, |
| 60 | + String location, |
| 61 | + String channelId, |
| 62 | + String primaryInputId, |
| 63 | + String backupInputId, |
| 64 | + String outputUri) |
| 65 | + throws InterruptedException, ExecutionException, TimeoutException, IOException { |
| 66 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 67 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 68 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 69 | + try (LivestreamServiceClient livestreamServiceClient = LivestreamServiceClient.create()) { |
| 70 | + VideoStream videoStream = |
| 71 | + VideoStream.newBuilder() |
| 72 | + .setH264( |
| 73 | + H264CodecSettings.newBuilder() |
| 74 | + .setProfile("main") |
| 75 | + .setBitrateBps(1000000) |
| 76 | + .setFrameRate(30) |
| 77 | + .setHeightPixels(720) |
| 78 | + .setWidthPixels(1280)) |
| 79 | + .build(); |
| 80 | + |
| 81 | + AudioStream audioStream = |
| 82 | + AudioStream.newBuilder().setCodec("aac").setChannelCount(2).setBitrateBps(160000).build(); |
| 83 | + |
| 84 | + var createChannelRequest = |
| 85 | + CreateChannelRequest.newBuilder() |
| 86 | + .setParent(LocationName.of(projectId, location).toString()) |
| 87 | + .setChannelId(channelId) |
| 88 | + .setChannel( |
| 89 | + Channel.newBuilder() |
| 90 | + .addInputAttachments( |
| 91 | + 0, |
| 92 | + InputAttachment.newBuilder() |
| 93 | + .setKey("my-primary-input") |
| 94 | + .setInput( |
| 95 | + InputName.of(projectId, location, primaryInputId).toString()) |
| 96 | + .setAutomaticFailover( |
| 97 | + AutomaticFailover.newBuilder() |
| 98 | + .addInputKeys("my-backup-input") |
| 99 | + .build()) |
| 100 | + .build()) |
| 101 | + .addInputAttachments( |
| 102 | + 1, |
| 103 | + InputAttachment.newBuilder() |
| 104 | + .setKey("my-backup-input") |
| 105 | + .setInput( |
| 106 | + InputName.of(projectId, location, backupInputId).toString())) |
| 107 | + .setOutput(Output.newBuilder().setUri(outputUri).build()) |
| 108 | + .addElementaryStreams( |
| 109 | + ElementaryStream.newBuilder() |
| 110 | + .setKey("es_video") |
| 111 | + .setVideoStream(videoStream)) |
| 112 | + .addElementaryStreams( |
| 113 | + ElementaryStream.newBuilder() |
| 114 | + .setKey("es_audio") |
| 115 | + .setAudioStream(audioStream)) |
| 116 | + .addMuxStreams( |
| 117 | + MuxStream.newBuilder() |
| 118 | + .setKey("mux_video") |
| 119 | + .addElementaryStreams("es_video") |
| 120 | + .setSegmentSettings( |
| 121 | + SegmentSettings.newBuilder() |
| 122 | + .setSegmentDuration( |
| 123 | + Duration.newBuilder().setSeconds(2).build()) |
| 124 | + .build()) |
| 125 | + .build()) |
| 126 | + .addMuxStreams( |
| 127 | + MuxStream.newBuilder() |
| 128 | + .setKey("mux_audio") |
| 129 | + .addElementaryStreams("es_audio") |
| 130 | + .setSegmentSettings( |
| 131 | + SegmentSettings.newBuilder() |
| 132 | + .setSegmentDuration( |
| 133 | + Duration.newBuilder().setSeconds(2).build()) |
| 134 | + .build()) |
| 135 | + .build()) |
| 136 | + .addManifests( |
| 137 | + Manifest.newBuilder() |
| 138 | + .setFileName("manifest.m3u8") |
| 139 | + .setType(ManifestType.HLS) |
| 140 | + .addMuxStreams("mux_video") |
| 141 | + .addMuxStreams("mux_audio") |
| 142 | + .setMaxSegmentCount(5) |
| 143 | + .build())) |
| 144 | + .build(); |
| 145 | + |
| 146 | + Channel result = |
| 147 | + livestreamServiceClient.createChannelAsync(createChannelRequest).get(1, TimeUnit.MINUTES); |
| 148 | + System.out.println("Channel: " + result.getName()); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | +// [END livestream_create_channel_with_backup_input] |
0 commit comments