Skip to content

Commit 1278c48

Browse files
authored
feat: add channel event resource code samples and tests (GoogleCloudPlatform#7163)
1 parent b4e0894 commit 1278c48

9 files changed

Lines changed: 756 additions & 1 deletion

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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_event]
20+
21+
import com.google.cloud.video.livestream.v1.ChannelName;
22+
import com.google.cloud.video.livestream.v1.CreateEventRequest;
23+
import com.google.cloud.video.livestream.v1.Event;
24+
import com.google.cloud.video.livestream.v1.Event.AdBreakTask;
25+
import com.google.cloud.video.livestream.v1.LivestreamServiceClient;
26+
import com.google.protobuf.Duration;
27+
import java.io.IOException;
28+
29+
public class CreateChannelEvent {
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+
String eventId = "my-channel-event-id";
37+
38+
createChannelEvent(projectId, location, channelId, eventId);
39+
}
40+
41+
public static void createChannelEvent(
42+
String projectId, String location, String channelId, String eventId) throws IOException {
43+
// Initialize client that will be used to send requests. This client only needs to be created
44+
// once, and can be reused for multiple requests. After completing all of your requests, call
45+
// the "close" method on the client to safely clean up any remaining background resources.
46+
try (LivestreamServiceClient livestreamServiceClient = LivestreamServiceClient.create()) {
47+
var createEventRequest =
48+
CreateEventRequest.newBuilder()
49+
.setParent(ChannelName.of(projectId, location, channelId).toString())
50+
.setEventId(eventId)
51+
.setEvent(
52+
Event.newBuilder()
53+
.setAdBreak(
54+
AdBreakTask.newBuilder()
55+
.setDuration(Duration.newBuilder().setSeconds(30).build())
56+
.build())
57+
.setExecuteNow(true)
58+
.build())
59+
.build();
60+
61+
Event response = livestreamServiceClient.createEvent(createEventRequest);
62+
System.out.println("Channel event: " + response.getName());
63+
}
64+
}
65+
}
66+
// [END livestream_create_channel_event]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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_event]
20+
21+
import com.google.cloud.video.livestream.v1.DeleteEventRequest;
22+
import com.google.cloud.video.livestream.v1.EventName;
23+
import com.google.cloud.video.livestream.v1.LivestreamServiceClient;
24+
import java.io.IOException;
25+
26+
public class DeleteChannelEvent {
27+
28+
public static void main(String[] args) throws Exception {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "my-project-id";
31+
String location = "us-central1";
32+
String channelId = "my-channel-id";
33+
String eventId = "my-channel-event-id";
34+
35+
deleteChannelEvent(projectId, location, channelId, eventId);
36+
}
37+
38+
public static void deleteChannelEvent(
39+
String projectId, String location, String channelId, String eventId) throws IOException {
40+
// Initialize client that will be used to send requests. This client only needs to be created
41+
// once, and can be reused for multiple requests. After completing all of your requests, call
42+
// the "close" method on the client to safely clean up any remaining background resources.
43+
try (LivestreamServiceClient livestreamServiceClient = LivestreamServiceClient.create()) {
44+
var deleteEventRequest =
45+
DeleteEventRequest.newBuilder()
46+
.setName(EventName.of(projectId, location, channelId, eventId).toString())
47+
.build();
48+
49+
livestreamServiceClient.deleteEvent(deleteEventRequest);
50+
System.out.println("Deleted channel event");
51+
}
52+
}
53+
}
54+
// [END livestream_delete_channel_event]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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_get_channel_event]
20+
21+
import com.google.cloud.video.livestream.v1.Event;
22+
import com.google.cloud.video.livestream.v1.EventName;
23+
import com.google.cloud.video.livestream.v1.LivestreamServiceClient;
24+
import java.io.IOException;
25+
26+
public class GetChannelEvent {
27+
28+
public static void main(String[] args) throws Exception {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "my-project-id";
31+
String location = "us-central1";
32+
String channelId = "my-channel-id";
33+
String eventId = "my-channel-event-id";
34+
35+
getChannelEvent(projectId, location, channelId, eventId);
36+
}
37+
38+
public static void getChannelEvent(
39+
String projectId, String location, String channelId, String eventId) throws IOException {
40+
// Initialize client that will be used to send requests. This client only needs to be created
41+
// once, and can be reused for multiple requests. After completing all of your requests, call
42+
// the "close" method on the client to safely clean up any remaining background resources.
43+
try (LivestreamServiceClient livestreamServiceClient = LivestreamServiceClient.create()) {
44+
EventName name = EventName.of(projectId, location, channelId, eventId);
45+
Event response = livestreamServiceClient.getEvent(name);
46+
System.out.println("Channel event: " + response.getName());
47+
}
48+
}
49+
}
50+
// [END livestream_get_channel_event]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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_list_channel_events]
20+
21+
import com.google.cloud.video.livestream.v1.ChannelName;
22+
import com.google.cloud.video.livestream.v1.Event;
23+
import com.google.cloud.video.livestream.v1.ListEventsRequest;
24+
import com.google.cloud.video.livestream.v1.LivestreamServiceClient;
25+
import java.io.IOException;
26+
27+
public class ListChannelEvents {
28+
29+
public static void main(String[] args) throws Exception {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "my-project-id";
32+
String location = "us-central1";
33+
String channelId = "my-channel-id";
34+
35+
listChannelEvents(projectId, location, channelId);
36+
}
37+
38+
public static void listChannelEvents(String projectId, String location, String channelId)
39+
throws IOException {
40+
// Initialize client that will be used to send requests. This client only needs to be created
41+
// once, and can be reused for multiple requests. After completing all of your requests, call
42+
// the "close" method on the client to safely clean up any remaining background resources.
43+
try (LivestreamServiceClient livestreamServiceClient = LivestreamServiceClient.create()) {
44+
var listEventsRequest =
45+
ListEventsRequest.newBuilder()
46+
.setParent(ChannelName.of(projectId, location, channelId).toString())
47+
.build();
48+
49+
LivestreamServiceClient.ListEventsPagedResponse response =
50+
livestreamServiceClient.listEvents(listEventsRequest);
51+
System.out.println("Channel events:");
52+
53+
for (Event event : response.iterateAll()) {
54+
System.out.println(event.getName());
55+
}
56+
}
57+
}
58+
}
59+
// [END livestream_list_channel_events]
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
import static org.hamcrest.CoreMatchers.containsString;
20+
import static org.hamcrest.MatcherAssert.assertThat;
21+
import static org.junit.Assert.assertNotNull;
22+
23+
import com.google.api.gax.rpc.NotFoundException;
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.IOException;
26+
import java.io.PrintStream;
27+
import java.util.UUID;
28+
import java.util.concurrent.ExecutionException;
29+
import java.util.concurrent.TimeoutException;
30+
import org.junit.After;
31+
import org.junit.Before;
32+
import org.junit.BeforeClass;
33+
import org.junit.Test;
34+
import org.junit.runner.RunWith;
35+
import org.junit.runners.JUnit4;
36+
37+
@RunWith(JUnit4.class)
38+
public class CreateChannelEventTest {
39+
40+
private static final String LOCATION = "us-central1";
41+
private static String PROJECT_ID;
42+
private static final String CHANNEL_ID =
43+
"my-channel-" + UUID.randomUUID().toString().substring(0, 25);
44+
private static final String INPUT_ID =
45+
"my-input-" + UUID.randomUUID().toString().substring(0, 25);
46+
private static final String EVENT_ID =
47+
"my-channel-event-" + UUID.randomUUID().toString().substring(0, 25);
48+
private static String EVENT_NAME;
49+
private static final String OUTPUT_URI = "gs://my-bucket/my-output-folder/";
50+
51+
private static PrintStream originalOut;
52+
private ByteArrayOutputStream bout;
53+
54+
private static String requireEnvVar(String varName) {
55+
String varValue = System.getenv(varName);
56+
assertNotNull(
57+
String.format("Environment variable '%s' is required to perform these tests.", varName));
58+
return varValue;
59+
}
60+
61+
@BeforeClass
62+
public static void checkRequirements() {
63+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
64+
PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT");
65+
}
66+
67+
@Before
68+
public void beforeTest()
69+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
70+
// Clean up old resources in the test project.
71+
TestUtils.cleanAllStale(PROJECT_ID, LOCATION);
72+
73+
originalOut = System.out;
74+
bout = new ByteArrayOutputStream();
75+
System.setOut(new PrintStream(bout));
76+
77+
EVENT_NAME =
78+
String.format(
79+
"projects/%s/locations/%s/channels/%s/events/%s",
80+
PROJECT_ID, LOCATION, CHANNEL_ID, EVENT_ID);
81+
try {
82+
DeleteChannel.deleteChannel(PROJECT_ID, LOCATION, CHANNEL_ID);
83+
} catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) {
84+
// Don't worry if the channel doesn't already exist.
85+
}
86+
87+
try {
88+
DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID);
89+
} catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) {
90+
// Don't worry if the input doesn't already exist.
91+
}
92+
93+
CreateInput.createInput(PROJECT_ID, LOCATION, INPUT_ID);
94+
CreateChannel.createChannel(PROJECT_ID, LOCATION, CHANNEL_ID, INPUT_ID, OUTPUT_URI);
95+
StartChannel.startChannel(PROJECT_ID, LOCATION, CHANNEL_ID);
96+
97+
bout.reset();
98+
}
99+
100+
@Test
101+
public void test_CreateChannelEvent() throws Exception {
102+
CreateChannelEvent.createChannelEvent(PROJECT_ID, LOCATION, CHANNEL_ID, EVENT_ID);
103+
String output = bout.toString();
104+
assertThat(output, containsString(EVENT_NAME));
105+
bout.reset();
106+
}
107+
108+
@After
109+
public void tearDown()
110+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
111+
112+
StopChannel.stopChannel(PROJECT_ID, LOCATION, CHANNEL_ID);
113+
DeleteChannelEvent.deleteChannelEvent(PROJECT_ID, LOCATION, CHANNEL_ID, EVENT_ID);
114+
try {
115+
DeleteChannel.deleteChannel(PROJECT_ID, LOCATION, CHANNEL_ID);
116+
} catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) {
117+
System.out.printf(String.valueOf(e));
118+
}
119+
120+
try {
121+
DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID);
122+
} catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) {
123+
System.out.printf(String.valueOf(e));
124+
}
125+
System.setOut(originalOut);
126+
bout.reset();
127+
}
128+
}

0 commit comments

Comments
 (0)