Skip to content

Commit 0594d10

Browse files
authored
feat: add Video Stitcher live session code samples and tests (GoogleCloudPlatform#7180)
1 parent eaef7c2 commit 0594d10

9 files changed

Lines changed: 800 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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.stitcher;
18+
19+
// [START video_stitcher_create_live_session]
20+
21+
import com.google.cloud.video.stitcher.v1.AdTag;
22+
import com.google.cloud.video.stitcher.v1.CreateLiveSessionRequest;
23+
import com.google.cloud.video.stitcher.v1.LiveSession;
24+
import com.google.cloud.video.stitcher.v1.LocationName;
25+
import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient;
26+
import java.io.IOException;
27+
28+
public class CreateLiveSession {
29+
30+
public static void main(String[] args) throws Exception {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "my-project-id";
33+
String location = "us-central1";
34+
// Uri of the live stream to stitch; this URI must reference either an MPEG-DASH
35+
// manifest (.mpd) file or an M3U playlist manifest (.m3u8) file.
36+
String sourceUri = "https://storage.googleapis.com/my-bucket/main.mpd";
37+
// See Single Inline Linear
38+
// (https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/tags)
39+
String adTagUri = "https://pubads.g.doubleclick.net/gampad/ads...";
40+
String slateId = "my-slate-id";
41+
42+
createLiveSession(projectId, location, sourceUri, adTagUri, slateId);
43+
}
44+
45+
public static void createLiveSession(
46+
String projectId, String location, String sourceUri, String adTagUri, String slateId)
47+
throws IOException {
48+
// Initialize client that will be used to send requests. This client only needs to be created
49+
// once, and can be reused for multiple requests. After completing all of your requests, call
50+
// the "close" method on the client to safely clean up any remaining background resources.
51+
try (VideoStitcherServiceClient videoStitcherServiceClient =
52+
VideoStitcherServiceClient.create()) {
53+
CreateLiveSessionRequest createLiveSessionRequest =
54+
CreateLiveSessionRequest.newBuilder()
55+
.setParent(LocationName.of(projectId, location).toString())
56+
.setLiveSession(
57+
LiveSession.newBuilder()
58+
.setSourceUri(sourceUri)
59+
.putAdTagMap("default", AdTag.newBuilder().setUri(adTagUri).build())
60+
.setDefaultSlateId(slateId))
61+
.build();
62+
63+
LiveSession response = videoStitcherServiceClient.createLiveSession(createLiveSessionRequest);
64+
System.out.println("Created live session: " + response.getName());
65+
System.out.println("Play URI: " + response.getPlayUri());
66+
}
67+
}
68+
}
69+
// [END video_stitcher_create_live_session]
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.stitcher;
18+
19+
// [START video_stitcher_get_live_ad_tag_detail]
20+
21+
import com.google.cloud.video.stitcher.v1.GetLiveAdTagDetailRequest;
22+
import com.google.cloud.video.stitcher.v1.LiveAdTagDetail;
23+
import com.google.cloud.video.stitcher.v1.LiveAdTagDetailName;
24+
import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient;
25+
import java.io.IOException;
26+
27+
public class GetLiveAdTagDetail {
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 sessionId = "my-session-id";
34+
String adTagDetailId = "my-ad-tag-detail-id";
35+
36+
getLiveAdTagDetail(projectId, location, sessionId, adTagDetailId);
37+
}
38+
39+
public static void getLiveAdTagDetail(
40+
String projectId, String location, String sessionId, String adTagDetailId)
41+
throws 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 (VideoStitcherServiceClient videoStitcherServiceClient =
46+
VideoStitcherServiceClient.create()) {
47+
GetLiveAdTagDetailRequest getLiveAdTagDetailRequest =
48+
GetLiveAdTagDetailRequest.newBuilder()
49+
.setName(
50+
LiveAdTagDetailName.of(projectId, location, sessionId, adTagDetailId).toString())
51+
.build();
52+
53+
LiveAdTagDetail response =
54+
videoStitcherServiceClient.getLiveAdTagDetail(getLiveAdTagDetailRequest);
55+
System.out.println("Live ad tag detail: " + response.getName());
56+
}
57+
}
58+
}
59+
// [END video_stitcher_get_live_ad_tag_detail]
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.stitcher;
18+
19+
// [START video_stitcher_get_live_session]
20+
21+
import com.google.cloud.video.stitcher.v1.GetLiveSessionRequest;
22+
import com.google.cloud.video.stitcher.v1.LiveSession;
23+
import com.google.cloud.video.stitcher.v1.LiveSessionName;
24+
import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient;
25+
import java.io.IOException;
26+
27+
public class GetLiveSession {
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 sessionId = "my-session-id";
34+
35+
getLiveSession(projectId, location, sessionId);
36+
}
37+
38+
public static void getLiveSession(String projectId, String location, String sessionId)
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 (VideoStitcherServiceClient videoStitcherServiceClient =
44+
VideoStitcherServiceClient.create()) {
45+
GetLiveSessionRequest getLiveSessionRequest =
46+
GetLiveSessionRequest.newBuilder()
47+
.setName(LiveSessionName.of(projectId, location, sessionId).toString())
48+
.build();
49+
50+
LiveSession response = videoStitcherServiceClient.getLiveSession(getLiveSessionRequest);
51+
System.out.println("Live session: " + response.getName());
52+
}
53+
}
54+
}
55+
// [END video_stitcher_get_live_session]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.stitcher;
18+
19+
// [START video_stitcher_list_live_ad_tag_details]
20+
21+
import com.google.cloud.video.stitcher.v1.ListLiveAdTagDetailsRequest;
22+
import com.google.cloud.video.stitcher.v1.LiveAdTagDetail;
23+
import com.google.cloud.video.stitcher.v1.LiveSessionName;
24+
import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient;
25+
import java.io.IOException;
26+
27+
public class ListLiveAdTagDetails {
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 sessionId = "my-session-id";
34+
35+
listLiveAdTagDetails(projectId, location, sessionId);
36+
}
37+
38+
public static void listLiveAdTagDetails(String projectId, String location, String sessionId)
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 (VideoStitcherServiceClient videoStitcherServiceClient =
44+
VideoStitcherServiceClient.create()) {
45+
ListLiveAdTagDetailsRequest listLiveAdTagDetailsRequest =
46+
ListLiveAdTagDetailsRequest.newBuilder()
47+
.setParent(LiveSessionName.of(projectId, location, sessionId).toString())
48+
.build();
49+
50+
VideoStitcherServiceClient.ListLiveAdTagDetailsPagedResponse response =
51+
videoStitcherServiceClient.listLiveAdTagDetails(listLiveAdTagDetailsRequest);
52+
System.out.println("Live ad tag details:");
53+
54+
for (LiveAdTagDetail adTagDetail : response.iterateAll()) {
55+
System.out.println(adTagDetail.toString());
56+
}
57+
}
58+
}
59+
}
60+
// [END video_stitcher_list_live_ad_tag_details]
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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.stitcher;
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 org.junit.After;
29+
import org.junit.Before;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
import org.junit.runner.RunWith;
33+
import org.junit.runners.JUnit4;
34+
35+
@RunWith(JUnit4.class)
36+
public class CreateLiveSessionTest {
37+
38+
private static final String LOCATION = "us-central1";
39+
private static final String LIVE_URI =
40+
"https://storage.googleapis.com/cloud-samples-data/media/hls-live/manifest.m3u8";
41+
// Single Inline Linear
42+
// (https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/tags)
43+
private static final String LIVE_AD_TAG_URI =
44+
"https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/single_ad_samples&sz=640x480&cust_params=sample_ct%3Dlinear&ciu_szs=300x250%2C728x90&gdfp_req=1&output=vast&unviewed_position_start=1&env=vp&impl=s&correlator=";
45+
private static final String SLATE_ID =
46+
"my-slate-" + UUID.randomUUID().toString().substring(0, 25);
47+
private static final String SLATE_URI =
48+
"https://storage.googleapis.com/cloud-samples-data/media/ForBiggerEscapes.mp4";
49+
private static String PROJECT_ID;
50+
private static String SESSION_NAME;
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() throws IOException {
69+
originalOut = System.out;
70+
bout = new ByteArrayOutputStream();
71+
System.setOut(new PrintStream(bout));
72+
// Project number is always returned in the live session name
73+
SESSION_NAME = String.format("locations/%s/liveSessions/", LOCATION);
74+
75+
try {
76+
DeleteSlate.deleteSlate(PROJECT_ID, LOCATION, SLATE_ID);
77+
} catch (NotFoundException e) {
78+
// Don't worry if the slate doesn't already exist.
79+
}
80+
CreateSlate.createSlate(PROJECT_ID, LOCATION, SLATE_ID, SLATE_URI);
81+
bout.reset();
82+
}
83+
84+
@Test
85+
public void test_CreateLiveSession() throws IOException {
86+
CreateLiveSession.createLiveSession(PROJECT_ID, LOCATION, LIVE_URI, LIVE_AD_TAG_URI, SLATE_ID);
87+
String output = bout.toString();
88+
assertThat(output, containsString(SESSION_NAME));
89+
bout.reset();
90+
}
91+
92+
@After
93+
public void tearDown() throws IOException {
94+
DeleteSlate.deleteSlate(PROJECT_ID, LOCATION, SLATE_ID);
95+
// No delete method for a live session
96+
System.setOut(originalOut);
97+
bout.reset();
98+
}
99+
}

0 commit comments

Comments
 (0)