Skip to content

Commit b4e0894

Browse files
authored
feat: add channel resource code samples and tests (GoogleCloudPlatform#7162)
1 parent 3882bdf commit b4e0894

19 files changed

Lines changed: 1633 additions & 15 deletions
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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]
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.InputName;
28+
import com.google.cloud.video.livestream.v1.LivestreamServiceClient;
29+
import com.google.cloud.video.livestream.v1.LocationName;
30+
import com.google.cloud.video.livestream.v1.Manifest;
31+
import com.google.cloud.video.livestream.v1.Manifest.ManifestType;
32+
import com.google.cloud.video.livestream.v1.MuxStream;
33+
import com.google.cloud.video.livestream.v1.SegmentSettings;
34+
import com.google.cloud.video.livestream.v1.VideoStream;
35+
import com.google.cloud.video.livestream.v1.VideoStream.H264CodecSettings;
36+
import com.google.protobuf.Duration;
37+
import java.io.IOException;
38+
import java.util.concurrent.ExecutionException;
39+
import java.util.concurrent.TimeUnit;
40+
import java.util.concurrent.TimeoutException;
41+
42+
public class CreateChannel {
43+
44+
public static void main(String[] args) throws Exception {
45+
// TODO(developer): Replace these variables before running the sample.
46+
String projectId = "my-project-id";
47+
String location = "us-central1";
48+
String channelId = "my-channel-id";
49+
String inputId = "my-input-id";
50+
String outputUri = "gs://my-bucket/my-output-folder/";
51+
52+
createChannel(projectId, location, channelId, inputId, outputUri);
53+
}
54+
55+
public static void createChannel(
56+
String projectId, String location, String channelId, String inputId, String outputUri)
57+
throws InterruptedException, ExecutionException, TimeoutException, IOException {
58+
// Initialize client that will be used to send requests. This client only needs to be created
59+
// once, and can be reused for multiple requests. After completing all of your requests, call
60+
// the "close" method on the client to safely clean up any remaining background resources.
61+
try (LivestreamServiceClient livestreamServiceClient = LivestreamServiceClient.create()) {
62+
VideoStream videoStream =
63+
VideoStream.newBuilder()
64+
.setH264(
65+
H264CodecSettings.newBuilder()
66+
.setProfile("main")
67+
.setBitrateBps(1000000)
68+
.setFrameRate(30)
69+
.setHeightPixels(720)
70+
.setWidthPixels(1280))
71+
.build();
72+
73+
AudioStream audioStream =
74+
AudioStream.newBuilder().setCodec("aac").setChannelCount(2).setBitrateBps(160000).build();
75+
76+
var createChannelRequest =
77+
CreateChannelRequest.newBuilder()
78+
.setParent(LocationName.of(projectId, location).toString())
79+
.setChannelId(channelId)
80+
.setChannel(
81+
Channel.newBuilder()
82+
.addInputAttachments(
83+
0,
84+
InputAttachment.newBuilder()
85+
.setKey("my-input")
86+
.setInput(InputName.of(projectId, location, inputId).toString())
87+
.build())
88+
.setOutput(Output.newBuilder().setUri(outputUri).build())
89+
.addElementaryStreams(
90+
ElementaryStream.newBuilder()
91+
.setKey("es_video")
92+
.setVideoStream(videoStream))
93+
.addElementaryStreams(
94+
ElementaryStream.newBuilder()
95+
.setKey("es_audio")
96+
.setAudioStream(audioStream))
97+
.addMuxStreams(
98+
MuxStream.newBuilder()
99+
.setKey("mux_video")
100+
.addElementaryStreams("es_video")
101+
.setSegmentSettings(
102+
SegmentSettings.newBuilder()
103+
.setSegmentDuration(
104+
Duration.newBuilder().setSeconds(2).build())
105+
.build())
106+
.build())
107+
.addMuxStreams(
108+
MuxStream.newBuilder()
109+
.setKey("mux_audio")
110+
.addElementaryStreams("es_audio")
111+
.setSegmentSettings(
112+
SegmentSettings.newBuilder()
113+
.setSegmentDuration(
114+
Duration.newBuilder().setSeconds(2).build())
115+
.build())
116+
.build())
117+
.addManifests(
118+
Manifest.newBuilder()
119+
.setFileName("manifest.m3u8")
120+
.setType(ManifestType.HLS)
121+
.addMuxStreams("mux_video")
122+
.addMuxStreams("mux_audio")
123+
.setMaxSegmentCount(5)
124+
.build()))
125+
.build();
126+
127+
Channel result =
128+
livestreamServiceClient.createChannelAsync(createChannelRequest).get(1, TimeUnit.MINUTES);
129+
System.out.println("Channel: " + result.getName());
130+
}
131+
}
132+
}
133+
// [END livestream_create_channel]
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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]

media/livestream/src/main/java/com/example/livestream/CreateInput.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818

1919
// [START livestream_create_input]
2020

21-
import com.google.api.gax.longrunning.OperationFuture;
2221
import com.google.cloud.video.livestream.v1.CreateInputRequest;
2322
import com.google.cloud.video.livestream.v1.Input;
2423
import com.google.cloud.video.livestream.v1.LivestreamServiceClient;
2524
import com.google.cloud.video.livestream.v1.LocationName;
26-
import com.google.cloud.video.livestream.v1.OperationMetadata;
2725
import java.io.IOException;
2826
import java.util.concurrent.ExecutionException;
2927
import java.util.concurrent.TimeUnit;
@@ -53,9 +51,8 @@ public static void createInput(String projectId, String location, String inputId
5351
.setInput(Input.newBuilder().setType(Input.Type.RTMP_PUSH).build())
5452
.build();
5553

56-
OperationFuture<Input, OperationMetadata> call =
57-
livestreamServiceClient.createInputAsync(createInputRequest);
58-
Input result = call.get(1, TimeUnit.MINUTES);
54+
Input result =
55+
livestreamServiceClient.createInputAsync(createInputRequest).get(1, TimeUnit.MINUTES);
5956
System.out.println("Input: " + result.getName());
6057
}
6158
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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_delete_channel]
20+
21+
import com.google.cloud.video.livestream.v1.ChannelName;
22+
import com.google.cloud.video.livestream.v1.DeleteChannelRequest;
23+
import com.google.cloud.video.livestream.v1.LivestreamServiceClient;
24+
import java.io.IOException;
25+
import java.util.concurrent.ExecutionException;
26+
import java.util.concurrent.TimeUnit;
27+
import java.util.concurrent.TimeoutException;
28+
29+
public class DeleteChannel {
30+
31+
public static void main(String[] args) throws Exception {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String projectId = "my-project-id";
34+
String location = "us-central1";
35+
String channelId = "my-channel-id";
36+
37+
deleteChannel(projectId, location, channelId);
38+
}
39+
40+
public static void deleteChannel(String projectId, String location, String channelId)
41+
throws InterruptedException, ExecutionException, TimeoutException, IOException {
42+
// Initialize client that will be used to send requests. This client only needs to be created
43+
// once, and can be reused for multiple requests. After completing all of your requests, call
44+
// the "close" method on the client to safely clean up any remaining background resources.
45+
try (LivestreamServiceClient livestreamServiceClient = LivestreamServiceClient.create()) {
46+
var deleteChannelRequest =
47+
DeleteChannelRequest.newBuilder()
48+
.setName(ChannelName.of(projectId, location, channelId).toString())
49+
.build();
50+
51+
livestreamServiceClient.deleteChannelAsync(deleteChannelRequest).get(1, TimeUnit.MINUTES);
52+
System.out.println("Deleted channel");
53+
}
54+
}
55+
}
56+
// [END livestream_delete_channel]

0 commit comments

Comments
 (0)