Skip to content

Commit ace5e5b

Browse files
authored
feat(videostitcher): update other samples using feedback from GoogleCloudPlatform#9412 (GoogleCloudPlatform#9438)
* feat(videostitcher): update other samples using feedback from GoogleCloudPlatform#9412 * fix checkstyle errors * retain original year for copyright
1 parent 7e2ca59 commit ace5e5b

44 files changed

Lines changed: 594 additions & 2039 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎media/stitcher/src/main/java/com/example/stitcher/CreateCdnKey.java‎

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static void main(String[] args) throws Exception {
5151

5252
// createCdnKey creates a Media CDN key or a Cloud CDN key. A CDN key is used to retrieve
5353
// protected media.
54-
public static void createCdnKey(
54+
public static CdnKey createCdnKey(
5555
String projectId,
5656
String location,
5757
String cdnKeyId,
@@ -61,45 +61,46 @@ public static void createCdnKey(
6161
Boolean isMediaCdn)
6262
throws IOException, ExecutionException, InterruptedException, TimeoutException {
6363
// Initialize client that will be used to send requests. This client only needs to be created
64-
// once, and can be reused for multiple requests. After completing all of your requests, call
65-
// the "close" method on the client to safely clean up any remaining background resources.
66-
VideoStitcherServiceClient videoStitcherServiceClient = VideoStitcherServiceClient.create();
67-
CdnKey cdnKey;
68-
if (isMediaCdn) {
69-
cdnKey =
70-
CdnKey.newBuilder()
71-
.setHostname(hostname)
72-
.setMediaCdnKey(
73-
MediaCdnKey.newBuilder()
74-
.setKeyName(keyName)
75-
.setPrivateKey(ByteString.copyFromUtf8(privateKey))
76-
.build())
77-
.build();
78-
} else {
79-
cdnKey =
80-
CdnKey.newBuilder()
81-
.setHostname(hostname)
82-
.setGoogleCdnKey(
83-
GoogleCdnKey.newBuilder()
84-
.setKeyName(keyName)
85-
.setPrivateKey(ByteString.copyFromUtf8(privateKey))
86-
.build())
87-
.build();
88-
}
64+
// once, and can be reused for multiple requests.
65+
try (VideoStitcherServiceClient videoStitcherServiceClient =
66+
VideoStitcherServiceClient.create()) {
67+
CdnKey cdnKey;
68+
if (isMediaCdn) {
69+
cdnKey =
70+
CdnKey.newBuilder()
71+
.setHostname(hostname)
72+
.setMediaCdnKey(
73+
MediaCdnKey.newBuilder()
74+
.setKeyName(keyName)
75+
.setPrivateKey(ByteString.copyFromUtf8(privateKey))
76+
.build())
77+
.build();
78+
} else {
79+
cdnKey =
80+
CdnKey.newBuilder()
81+
.setHostname(hostname)
82+
.setGoogleCdnKey(
83+
GoogleCdnKey.newBuilder()
84+
.setKeyName(keyName)
85+
.setPrivateKey(ByteString.copyFromUtf8(privateKey))
86+
.build())
87+
.build();
88+
}
8989

90-
CreateCdnKeyRequest createCdnKeyRequest =
91-
CreateCdnKeyRequest.newBuilder()
92-
.setParent(LocationName.of(projectId, location).toString())
93-
.setCdnKeyId(cdnKeyId)
94-
.setCdnKey(cdnKey)
95-
.build();
90+
CreateCdnKeyRequest createCdnKeyRequest =
91+
CreateCdnKeyRequest.newBuilder()
92+
.setParent(LocationName.of(projectId, location).toString())
93+
.setCdnKeyId(cdnKeyId)
94+
.setCdnKey(cdnKey)
95+
.build();
9696

97-
CdnKey result =
98-
videoStitcherServiceClient
99-
.createCdnKeyAsync(createCdnKeyRequest)
100-
.get(TIMEOUT_IN_MINUTES, TimeUnit.MINUTES);
101-
System.out.println("Created new CDN key: " + result.getName());
102-
videoStitcherServiceClient.close();
97+
CdnKey result =
98+
videoStitcherServiceClient
99+
.createCdnKeyAsync(createCdnKeyRequest)
100+
.get(TIMEOUT_IN_MINUTES, TimeUnit.MINUTES);
101+
System.out.println("Created new CDN key: " + result.getName());
102+
return result;
103+
}
103104
}
104105
}
105106
// [END videostitcher_create_cdn_key]

‎media/stitcher/src/main/java/com/example/stitcher/CreateCdnKeyAkamai.java‎

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,35 +45,36 @@ public static void main(String[] args) throws Exception {
4545
}
4646

4747
// createCdnKeyAkamai creates an Akamai CDN key. A CDN key is used to retrieve protected media.
48-
public static void createCdnKeyAkamai(
48+
public static CdnKey createCdnKeyAkamai(
4949
String projectId, String location, String cdnKeyId, String hostname, String akamaiTokenKey)
5050
throws IOException, ExecutionException, InterruptedException, TimeoutException {
5151
// Initialize client that will be used to send requests. This client only needs to be created
52-
// once, and can be reused for multiple requests. After completing all of your requests, call
53-
// the "close" method on the client to safely clean up any remaining background resources.
54-
VideoStitcherServiceClient videoStitcherServiceClient = VideoStitcherServiceClient.create();
55-
CdnKey cdnKey =
56-
CdnKey.newBuilder()
57-
.setHostname(hostname)
58-
.setAkamaiCdnKey(
59-
AkamaiCdnKey.newBuilder()
60-
.setTokenKey(ByteString.copyFromUtf8(akamaiTokenKey))
61-
.build())
62-
.build();
52+
// once, and can be reused for multiple requests.
53+
try (VideoStitcherServiceClient videoStitcherServiceClient =
54+
VideoStitcherServiceClient.create()) {
55+
CdnKey cdnKey =
56+
CdnKey.newBuilder()
57+
.setHostname(hostname)
58+
.setAkamaiCdnKey(
59+
AkamaiCdnKey.newBuilder()
60+
.setTokenKey(ByteString.copyFromUtf8(akamaiTokenKey))
61+
.build())
62+
.build();
6363

64-
CreateCdnKeyRequest createCdnKeyRequest =
65-
CreateCdnKeyRequest.newBuilder()
66-
.setParent(LocationName.of(projectId, location).toString())
67-
.setCdnKeyId(cdnKeyId)
68-
.setCdnKey(cdnKey)
69-
.build();
64+
CreateCdnKeyRequest createCdnKeyRequest =
65+
CreateCdnKeyRequest.newBuilder()
66+
.setParent(LocationName.of(projectId, location).toString())
67+
.setCdnKeyId(cdnKeyId)
68+
.setCdnKey(cdnKey)
69+
.build();
7070

71-
CdnKey result =
72-
videoStitcherServiceClient
73-
.createCdnKeyAsync(createCdnKeyRequest)
74-
.get(TIMEOUT_IN_MINUTES, TimeUnit.MINUTES);
75-
System.out.println("Created new CDN key: " + result.getName());
76-
videoStitcherServiceClient.close();
71+
CdnKey result =
72+
videoStitcherServiceClient
73+
.createCdnKeyAsync(createCdnKeyRequest)
74+
.get(TIMEOUT_IN_MINUTES, TimeUnit.MINUTES);
75+
System.out.println("Created new CDN key: " + result.getName());
76+
return result;
77+
}
7778
}
7879
}
7980
// [END videostitcher_create_cdn_key_akamai]

‎media/stitcher/src/main/java/com/example/stitcher/CreateLiveConfig.java‎

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ public static void main(String[] args) throws Exception {
5050
createLiveConfig(projectId, location, liveConfigId, sourceUri, adTagUri, slateId);
5151
}
5252

53-
public static void createLiveConfig(
53+
// Creates a live config. Live configs are used to configure live sessions.
54+
// For more information, see
55+
// https://cloud.google.com/video-stitcher/docs/how-to/managing-live-configs.
56+
public static LiveConfig createLiveConfig(
5457
String projectId,
5558
String location,
5659
String liveConfigId,
@@ -59,29 +62,30 @@ public static void createLiveConfig(
5962
String slateId)
6063
throws IOException, ExecutionException, InterruptedException, TimeoutException {
6164
// Initialize client that will be used to send requests. This client only needs to be created
62-
// once, and can be reused for multiple requests. After completing all of your requests, call
63-
// the "close" method on the client to safely clean up any remaining background resources.
64-
VideoStitcherServiceClient videoStitcherServiceClient = VideoStitcherServiceClient.create();
65-
CreateLiveConfigRequest createLiveConfigRequest =
66-
CreateLiveConfigRequest.newBuilder()
67-
.setParent(LocationName.of(projectId, location).toString())
68-
.setLiveConfigId(liveConfigId)
69-
.setLiveConfig(
70-
LiveConfig.newBuilder()
71-
.setSourceUri(sourceUri)
72-
.setAdTagUri(adTagUri)
73-
.setDefaultSlate(SlateName.format(projectId, location, slateId))
74-
.setAdTracking(AdTracking.SERVER)
75-
.setStitchingPolicy(StitchingPolicy.CUT_CURRENT)
76-
.build())
77-
.build();
65+
// once, and can be reused for multiple requests.
66+
try (VideoStitcherServiceClient videoStitcherServiceClient =
67+
VideoStitcherServiceClient.create()) {
68+
CreateLiveConfigRequest createLiveConfigRequest =
69+
CreateLiveConfigRequest.newBuilder()
70+
.setParent(LocationName.of(projectId, location).toString())
71+
.setLiveConfigId(liveConfigId)
72+
.setLiveConfig(
73+
LiveConfig.newBuilder()
74+
.setSourceUri(sourceUri)
75+
.setAdTagUri(adTagUri)
76+
.setDefaultSlate(SlateName.format(projectId, location, slateId))
77+
.setAdTracking(AdTracking.SERVER)
78+
.setStitchingPolicy(StitchingPolicy.CUT_CURRENT)
79+
.build())
80+
.build();
7881

79-
LiveConfig response =
80-
videoStitcherServiceClient
81-
.createLiveConfigAsync(createLiveConfigRequest)
82-
.get(TIMEOUT_IN_MINUTES, TimeUnit.MINUTES);
83-
System.out.println("Created new live config: " + response.getName());
84-
videoStitcherServiceClient.close();
82+
LiveConfig response =
83+
videoStitcherServiceClient
84+
.createLiveConfigAsync(createLiveConfigRequest)
85+
.get(TIMEOUT_IN_MINUTES, TimeUnit.MINUTES);
86+
System.out.println("Created new live config: " + response.getName());
87+
return response;
88+
}
8589
}
8690
}
8791
// [END videostitcher_create_live_config]

‎media/stitcher/src/main/java/com/example/stitcher/CreateLiveSession.java‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ public static void main(String[] args) throws Exception {
3636
createLiveSession(projectId, location, liveConfigId);
3737
}
3838

39-
public static void createLiveSession(String projectId, String location, String liveConfigId)
40-
throws IOException {
39+
// Creates a live session given the parameters in the supplied live config.
40+
// For more information, see
41+
// https://cloud.google.com/video-stitcher/docs/how-to/managing-live-sessions.
42+
public static LiveSession createLiveSession(
43+
String projectId, String location, String liveConfigId) throws IOException {
4144
// Initialize client that will be used to send requests. This client only needs to be created
42-
// once, and can be reused for multiple requests. In this example, try-with-resources is used
43-
// which automatically calls close() on the client to clean up resources.
45+
// once, and can be reused for multiple requests.
4446
try (VideoStitcherServiceClient videoStitcherServiceClient =
4547
VideoStitcherServiceClient.create()) {
4648
CreateLiveSessionRequest createLiveSessionRequest =
@@ -54,6 +56,7 @@ public static void createLiveSession(String projectId, String location, String l
5456
LiveSession response = videoStitcherServiceClient.createLiveSession(createLiveSessionRequest);
5557
System.out.println("Created live session: " + response.getName());
5658
System.out.println("Play URI: " + response.getPlayUri());
59+
return response;
5760
}
5861
}
5962
}

‎media/stitcher/src/main/java/com/example/stitcher/CreateSlate.java‎

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,30 @@ public static void main(String[] args) throws Exception {
4242
createSlate(projectId, location, slateId, slateUri);
4343
}
4444

45-
public static void createSlate(String projectId, String location, String slateId, String slateUri)
45+
// Creates a slate. Slates are content that can be served when there are gaps in a livestream
46+
// ad break that cannot be filled with a dynamically served ad. For more information, see
47+
// https://cloud.google.com/video-stitcher/docs/how-to/managing-slates.
48+
public static Slate createSlate(
49+
String projectId, String location, String slateId, String slateUri)
4650
throws IOException, ExecutionException, InterruptedException, TimeoutException {
4751
// Initialize client that will be used to send requests. This client only needs to be created
48-
// once, and can be reused for multiple requests. After completing all of your requests, call
49-
// the "close" method on the client to safely clean up any remaining background resources.
50-
VideoStitcherServiceClient videoStitcherServiceClient = VideoStitcherServiceClient.create();
51-
CreateSlateRequest createSlateRequest =
52-
CreateSlateRequest.newBuilder()
53-
.setParent(LocationName.of(projectId, location).toString())
54-
.setSlateId(slateId)
55-
.setSlate(Slate.newBuilder().setUri(slateUri).build())
56-
.build();
52+
// once, and can be reused for multiple requests.
53+
try (VideoStitcherServiceClient videoStitcherServiceClient =
54+
VideoStitcherServiceClient.create()) {
55+
CreateSlateRequest createSlateRequest =
56+
CreateSlateRequest.newBuilder()
57+
.setParent(LocationName.of(projectId, location).toString())
58+
.setSlateId(slateId)
59+
.setSlate(Slate.newBuilder().setUri(slateUri).build())
60+
.build();
5761

58-
Slate response =
59-
videoStitcherServiceClient
60-
.createSlateAsync(createSlateRequest)
61-
.get(TIMEOUT_IN_MINUTES, TimeUnit.MINUTES);
62-
System.out.println("Created new slate: " + response.getName());
63-
videoStitcherServiceClient.close();
62+
Slate response =
63+
videoStitcherServiceClient
64+
.createSlateAsync(createSlateRequest)
65+
.get(TIMEOUT_IN_MINUTES, TimeUnit.MINUTES);
66+
System.out.println("Created new slate: " + response.getName());
67+
return response;
68+
}
6469
}
6570
}
6671
// [END videostitcher_create_slate]

‎media/stitcher/src/main/java/com/example/stitcher/CreateVodSession.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public static void main(String[] args) throws Exception {
3838
}
3939

4040
// Creates a video on demand (VOD) session using the parameters in the designated VOD config.
41+
// For more information, see
42+
// https://cloud.google.com/video-stitcher/docs/how-to/creating-vod-sessions.
4143
public static VodSession createVodSession(String projectId, String location, String vodConfigId)
4244
throws IOException {
4345
// Initialize client that will be used to send requests. This client only needs to be created

‎media/stitcher/src/main/java/com/example/stitcher/DeleteCdnKey.java‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,23 @@ public static void main(String[] args) throws Exception {
3939
deleteCdnKey(projectId, location, cdnKeyId);
4040
}
4141

42+
// Deletes a CDN key.
4243
public static void deleteCdnKey(String projectId, String location, String cdnKeyId)
4344
throws InterruptedException, ExecutionException, TimeoutException, IOException {
4445
// Initialize client that will be used to send requests. This client only needs to be created
45-
// once, and can be reused for multiple requests. After completing all of your requests, call
46-
// the "close" method on the client to safely clean up any remaining background resources.
47-
VideoStitcherServiceClient videoStitcherServiceClient = VideoStitcherServiceClient.create();
48-
DeleteCdnKeyRequest deleteCdnKeyRequest =
49-
DeleteCdnKeyRequest.newBuilder()
50-
.setName(CdnKeyName.of(projectId, location, cdnKeyId).toString())
51-
.build();
52-
53-
videoStitcherServiceClient
54-
.deleteCdnKeyAsync(deleteCdnKeyRequest)
55-
.get(TIMEOUT_IN_MINUTES, TimeUnit.MINUTES);
56-
System.out.println("Deleted CDN key");
57-
videoStitcherServiceClient.close();
46+
// once, and can be reused for multiple requests.
47+
try (VideoStitcherServiceClient videoStitcherServiceClient =
48+
VideoStitcherServiceClient.create()) {
49+
DeleteCdnKeyRequest deleteCdnKeyRequest =
50+
DeleteCdnKeyRequest.newBuilder()
51+
.setName(CdnKeyName.of(projectId, location, cdnKeyId).toString())
52+
.build();
53+
54+
videoStitcherServiceClient
55+
.deleteCdnKeyAsync(deleteCdnKeyRequest)
56+
.get(TIMEOUT_IN_MINUTES, TimeUnit.MINUTES);
57+
System.out.println("Deleted CDN key");
58+
}
5859
}
5960
}
6061
// [END videostitcher_delete_cdn_key]

‎media/stitcher/src/main/java/com/example/stitcher/DeleteLiveConfig.java‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,23 @@ public static void main(String[] args) throws Exception {
3939
deleteLiveConfig(projectId, location, liveConfigId);
4040
}
4141

42+
// Deletes a live config.
4243
public static void deleteLiveConfig(String projectId, String location, String liveConfigId)
4344
throws IOException, ExecutionException, InterruptedException, TimeoutException {
4445
// Initialize client that will be used to send requests. This client only needs to be created
45-
// once, and can be reused for multiple requests. After completing all of your requests, call
46-
// the "close" method on the client to safely clean up any remaining background resources.
47-
VideoStitcherServiceClient videoStitcherServiceClient = VideoStitcherServiceClient.create();
48-
DeleteLiveConfigRequest deleteLiveConfigRequest =
49-
DeleteLiveConfigRequest.newBuilder()
50-
.setName(LiveConfigName.of(projectId, location, liveConfigId).toString())
51-
.build();
52-
53-
videoStitcherServiceClient
54-
.deleteLiveConfigAsync(deleteLiveConfigRequest)
55-
.get(TIMEOUT_IN_MINUTES, TimeUnit.MINUTES);
56-
System.out.println("Deleted live config");
57-
videoStitcherServiceClient.close();
46+
// once, and can be reused for multiple requests.
47+
try (VideoStitcherServiceClient videoStitcherServiceClient =
48+
VideoStitcherServiceClient.create()) {
49+
DeleteLiveConfigRequest deleteLiveConfigRequest =
50+
DeleteLiveConfigRequest.newBuilder()
51+
.setName(LiveConfigName.of(projectId, location, liveConfigId).toString())
52+
.build();
53+
54+
videoStitcherServiceClient
55+
.deleteLiveConfigAsync(deleteLiveConfigRequest)
56+
.get(TIMEOUT_IN_MINUTES, TimeUnit.MINUTES);
57+
System.out.println("Deleted live config");
58+
}
5859
}
5960
}
6061
// [END videostitcher_delete_live_config]

0 commit comments

Comments
 (0)