Skip to content

Commit ca9ee44

Browse files
authored
feat: [VideoStitcher] add samples and tests (GoogleCloudPlatform#1758)
1 parent 3f2a33f commit ca9ee44

File tree

5 files changed

+357
-2
lines changed

5 files changed

+357
-2
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2022 Google LLC.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the samples:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/media/videostitcher/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\Media\Stitcher;
26+
27+
// [START videostitcher_create_live_session]
28+
use Google\Cloud\Video\Stitcher\V1\AdTag;
29+
use Google\Cloud\Video\Stitcher\V1\VideoStitcherServiceClient;
30+
use Google\Cloud\Video\Stitcher\V1\LiveSession;
31+
32+
/**
33+
* Creates a live session. Live sessions are ephemeral resources that expire
34+
* after a few minutes.
35+
*
36+
* @param string $callingProjectId The project ID to run the API call under
37+
* @param string $location The location of the session
38+
* @param string $sourceUri Uri of the media to stitch; this URI must
39+
* reference either an MPEG-DASH manifest
40+
* (.mpd) file or an M3U playlist manifest
41+
* (.m3u8) file.
42+
* @param string $adTagUri The Uri of the ad tag
43+
* @param string $slateId The user-defined slate ID of the default
44+
* slate to use when no slates are specified
45+
* in an ad break's message
46+
*/
47+
function create_live_session(
48+
string $callingProjectId,
49+
string $location,
50+
string $sourceUri,
51+
string $adTagUri,
52+
string $slateId
53+
): void {
54+
// Instantiate a client.
55+
$stitcherClient = new VideoStitcherServiceClient();
56+
57+
$parent = $stitcherClient->locationName($callingProjectId, $location);
58+
$liveSession = new LiveSession();
59+
$liveSession->setSourceUri($sourceUri);
60+
$liveSession->setAdTagMap([
61+
'default' => (new AdTag())
62+
->setUri($adTagUri)
63+
]);
64+
$liveSession->setDefaultSlateId($slateId);
65+
66+
// Run live session creation request
67+
$response = $stitcherClient->createLiveSession($parent, $liveSession);
68+
69+
// Print results
70+
printf('Live session: %s' . PHP_EOL, $response->getName());
71+
}
72+
// [END videostitcher_create_live_session]
73+
74+
// The following 2 lines are only needed to run the samples
75+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
76+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2022 Google LLC.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the samples:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/media/videostitcher/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\Media\Stitcher;
26+
27+
// [START videostitcher_get_live_ad_tag_detail]
28+
use Google\Cloud\Video\Stitcher\V1\VideoStitcherServiceClient;
29+
30+
/**
31+
* Gets the specified ad tag detail for the live session.
32+
*
33+
* @param string $callingProjectId The project ID to run the API call under
34+
* @param string $location The location of the session
35+
* @param string $sessionId The ID of the session
36+
* @param string $adTagDetailId The ID of the ad tag detail
37+
*/
38+
function get_live_ad_tag_detail(
39+
string $callingProjectId,
40+
string $location,
41+
string $sessionId,
42+
string $adTagDetailId
43+
): void {
44+
// Instantiate a client.
45+
$stitcherClient = new VideoStitcherServiceClient();
46+
47+
$formattedName = $stitcherClient->liveAdTagDetailName($callingProjectId, $location, $sessionId, $adTagDetailId);
48+
$adTagDetail = $stitcherClient->getLiveAdTagDetail($formattedName);
49+
50+
// Print results
51+
printf('Live ad tag detail: %s' . PHP_EOL, $adTagDetail->getName());
52+
}
53+
// [END videostitcher_get_live_ad_tag_detail]
54+
55+
// The following 2 lines are only needed to run the samples
56+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
57+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2022 Google LLC.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the samples:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/media/videostitcher/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\Media\Stitcher;
26+
27+
// [START videostitcher_get_live_session]
28+
use Google\Cloud\Video\Stitcher\V1\VideoStitcherServiceClient;
29+
30+
/**
31+
* Gets a live session.
32+
*
33+
* @param string $callingProjectId The project ID to run the API call under
34+
* @param string $location The location of the session
35+
* @param string $sessionId The ID of the session
36+
*/
37+
function get_live_session(
38+
string $callingProjectId,
39+
string $location,
40+
string $sessionId
41+
): void {
42+
// Instantiate a client.
43+
$stitcherClient = new VideoStitcherServiceClient();
44+
45+
$formattedName = $stitcherClient->liveSessionName($callingProjectId, $location, $sessionId);
46+
$session = $stitcherClient->getLiveSession($formattedName);
47+
48+
// Print results
49+
printf('Live session: %s' . PHP_EOL, $session->getName());
50+
}
51+
// [END videostitcher_get_live_session]
52+
53+
// The following 2 lines are only needed to run the samples
54+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
55+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2022 Google LLC.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the samples:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/media/videostitcher/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\Media\Stitcher;
26+
27+
// [START videostitcher_list_live_ad_tag_details]
28+
use Google\Cloud\Video\Stitcher\V1\VideoStitcherServiceClient;
29+
30+
/**
31+
* Lists the ad tag details for the specified live session.
32+
*
33+
* @param string $callingProjectId The project ID to run the API call under
34+
* @param string $location The location of the session
35+
* @param string $sessionId The ID of the session
36+
*/
37+
function list_live_ad_tag_details(
38+
string $callingProjectId,
39+
string $location,
40+
string $sessionId
41+
): void {
42+
// Instantiate a client.
43+
$stitcherClient = new VideoStitcherServiceClient();
44+
45+
$formattedName = $stitcherClient->liveSessionName($callingProjectId, $location, $sessionId);
46+
$response = $stitcherClient->listLiveAdTagDetails($formattedName);
47+
48+
// Print the ad tag details list.
49+
$adTagDetails = $response->iterateAllElements();
50+
print('Live ad tag details:' . PHP_EOL);
51+
foreach ($adTagDetails as $adTagDetail) {
52+
printf('%s' . PHP_EOL, $adTagDetail->getName());
53+
}
54+
}
55+
// [END videostitcher_list_live_ad_tag_details]
56+
57+
// The following 2 lines are only needed to run the samples
58+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
59+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

media/videostitcher/test/videoStitcherTest.php

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class videoStitcherTest extends TestCase
6868
private static $updatedAkamaiTokenKey = 'VGhpcyBpcyBhbiB1cGRhdGVkIHRlc3Qgc3RyaW5nLg==';
6969

7070
private static $inputBucketName = 'cloud-samples-data';
71-
private static $inputVideoFileName = '/media/hls-vod/manifest.m3u8';
71+
private static $inputVodFileName = '/media/hls-vod/manifest.m3u8';
7272
private static $vodUri;
7373
private static $vodAgTagUri = 'https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/vmap_ad_samples&sz=640x480&cust_params=sample_ar%3Dpreonly&ciu_szs=300x250%2C728x90&gdfp_req=1&ad_rule=1&output=vmap&unviewed_position_start=1&env=vp&impl=s&correlator=';
7474

@@ -79,6 +79,14 @@ class videoStitcherTest extends TestCase
7979
private static $vodStitchDetailId;
8080
private static $vodStitchDetailName;
8181

82+
private static $inputLiveFileName = '/media/hls-live/manifest.m3u8';
83+
private static $liveUri;
84+
private static $liveAgTagUri = '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=';
85+
private static $liveSessionId;
86+
private static $liveSessionName;
87+
private static $liveAdTagDetailId;
88+
private static $liveAdTagDetailName;
89+
8290
public static function setUpBeforeClass(): void
8391
{
8492
self::checkProjectEnvVars();
@@ -90,7 +98,9 @@ public static function setUpBeforeClass(): void
9098
self::$slateUri = sprintf('https://storage.googleapis.com/%s%s', self::$bucket, self::$slateFileName);
9199
self::$updatedSlateUri = sprintf('https://storage.googleapis.com/%s%s', self::$bucket, self::$updatedSlateFileName);
92100

93-
self::$vodUri = sprintf('https://storage.googleapis.com/%s%s', self::$inputBucketName, self::$inputVideoFileName);
101+
self::$vodUri = sprintf('https://storage.googleapis.com/%s%s', self::$inputBucketName, self::$inputVodFileName);
102+
103+
self::$liveUri = sprintf('https://storage.googleapis.com/%s%s', self::$inputBucketName, self::$inputLiveFileName);
94104
}
95105

96106
public function testCreateSlate()
@@ -430,6 +440,104 @@ public function testGetVodStitchDetail()
430440
$this->assertStringContainsString(self::$vodStitchDetailName, $output);
431441
}
432442

443+
public function testCreateLiveSession()
444+
{
445+
# Create a temporary slate for the live session (required)
446+
$tempSlateId = sprintf('php-test-slate-%s-%s', uniqid(), time());
447+
$this->runFunctionSnippet('create_slate', [
448+
self::$projectId,
449+
self::$location,
450+
$tempSlateId,
451+
self::$slateUri
452+
]);
453+
454+
# API returns project number rather than project ID so
455+
# don't include that in $liveSessionName since we don't have it
456+
self::$liveSessionName = sprintf('/locations/%s/liveSessions/', self::$location);
457+
458+
$output = $this->runFunctionSnippet('create_live_session', [
459+
self::$projectId,
460+
self::$location,
461+
self::$liveUri,
462+
self::$liveAgTagUri,
463+
$tempSlateId
464+
]);
465+
$this->assertStringContainsString(self::$liveSessionName, $output);
466+
self::$liveSessionId = explode('/', $output);
467+
self::$liveSessionId = trim(self::$liveSessionId[(count(self::$liveSessionId) - 1)]);
468+
self::$liveSessionName = sprintf('/locations/%s/liveSessions/%s', self::$location, self::$liveSessionId);
469+
470+
# Delete the temporary slate
471+
$this->runFunctionSnippet('delete_slate', [
472+
self::$projectId,
473+
self::$location,
474+
$tempSlateId
475+
]);
476+
}
477+
478+
/** @depends testCreateLiveSession */
479+
public function testGetLiveSession()
480+
{
481+
$output = $this->runFunctionSnippet('get_live_session', [
482+
self::$projectId,
483+
self::$location,
484+
self::$liveSessionId
485+
]);
486+
$this->assertStringContainsString(self::$liveSessionName, $output);
487+
}
488+
489+
/** @depends testGetLiveSession */
490+
public function testListLiveAdTagDetails()
491+
{
492+
# To get ad tag details, you need to curl the main manifest and
493+
# a rendition first. This supplies media player information to the API.
494+
#
495+
# Curl the playUri first. The last line of the response will contain a
496+
# renditions location. Curl the live session name with the rendition
497+
# location appended.
498+
499+
$stitcherClient = new VideoStitcherServiceClient();
500+
$formattedName = $stitcherClient->liveSessionName(self::$projectId, self::$location, self::$liveSessionId);
501+
$session = $stitcherClient->getLiveSession($formattedName);
502+
$playUri = $session->getPlayUri();
503+
504+
$manifest = file_get_contents($playUri);
505+
$tmp = explode("\n", trim($manifest));
506+
$renditions = $tmp[count($tmp) - 1];
507+
508+
# playUri will be in the following format:
509+
# https://videostitcher.googleapis.com/v1/projects/{project}/locations/{location}/liveSessions/{session-id}/manifest.m3u8?signature=...
510+
# Replace manifest.m3u8?signature=... with the renditions location.
511+
512+
$tmp = explode('/', $playUri);
513+
array_pop($tmp);
514+
$renditionsUri = sprintf('%s/%s', join('/', $tmp), $renditions);
515+
file_get_contents($renditionsUri);
516+
517+
self::$liveAdTagDetailName = sprintf('/locations/%s/liveSessions/%s/liveAdTagDetails/', self::$location, self::$liveSessionId);
518+
$output = $this->runFunctionSnippet('list_live_ad_tag_details', [
519+
self::$projectId,
520+
self::$location,
521+
self::$liveSessionId
522+
]);
523+
$this->assertStringContainsString(self::$liveAdTagDetailName, $output);
524+
self::$liveAdTagDetailId = explode('/', $output);
525+
self::$liveAdTagDetailId = trim(self::$liveAdTagDetailId[(count(self::$liveAdTagDetailId) - 1)]);
526+
self::$liveAdTagDetailName = sprintf('/locations/%s/liveSessions/%s/liveAdTagDetails/%s', self::$location, self::$liveSessionId, self::$liveAdTagDetailId);
527+
}
528+
529+
/** @depends testListLiveAdTagDetails */
530+
public function testGetLiveAdTagDetail()
531+
{
532+
$output = $this->runFunctionSnippet('get_live_ad_tag_detail', [
533+
self::$projectId,
534+
self::$location,
535+
self::$liveSessionId,
536+
self::$liveAdTagDetailId
537+
]);
538+
$this->assertStringContainsString(self::$liveAdTagDetailName, $output);
539+
}
540+
433541
private static function deleteOldSlates(): void
434542
{
435543
$stitcherClient = new VideoStitcherServiceClient();

0 commit comments

Comments
 (0)