Skip to content

Commit 0ae22bc

Browse files
authored
feat: [LiveStream] add Channel samples and tests (GoogleCloudPlatform#1760)
1 parent 8c85087 commit 0ae22bc

File tree

9 files changed

+866
-0
lines changed

9 files changed

+866
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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/livestream/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\Media\LiveStream;
26+
27+
// [START livestream_create_channel]
28+
use Google\Cloud\Video\LiveStream\V1\LivestreamServiceClient;
29+
use Google\Cloud\Video\LiveStream\V1\AudioStream;
30+
use Google\Cloud\Video\LiveStream\V1\Channel;
31+
use Google\Cloud\Video\LiveStream\V1\ElementaryStream;
32+
use Google\Cloud\Video\LiveStream\V1\InputAttachment;
33+
use Google\Cloud\Video\LiveStream\V1\Manifest;
34+
use Google\Cloud\Video\LiveStream\V1\MuxStream;
35+
use Google\Cloud\Video\LiveStream\V1\SegmentSettings;
36+
use Google\Cloud\Video\LiveStream\V1\VideoStream;
37+
use Google\Protobuf\Duration;
38+
39+
/**
40+
* Creates a channel.
41+
*
42+
* @param string $callingProjectId The project ID to run the API call under
43+
* @param string $location The location of the channel
44+
* @param string $channelId The ID of the channel to be created
45+
* @param string $inputId The ID of the input for the channel
46+
* @param string $outputUri Uri of the channel output folder in a
47+
* Cloud Storage bucket. (e.g.
48+
* "gs://my-bucket/my-output-folder/")
49+
*/
50+
function create_channel(
51+
string $callingProjectId,
52+
string $location,
53+
string $channelId,
54+
string $inputId,
55+
string $outputUri
56+
): void {
57+
// Instantiate a client.
58+
$livestreamClient = new LivestreamServiceClient();
59+
60+
$parent = $livestreamClient->locationName($callingProjectId, $location);
61+
$channelName = $livestreamClient->channelName($callingProjectId, $location, $channelId);
62+
$inputName = $livestreamClient->inputName($callingProjectId, $location, $inputId);
63+
64+
$channel = (new Channel())
65+
->setName($channelName)
66+
->setInputAttachments([
67+
new InputAttachment([
68+
'key' => 'my-input',
69+
'input' => $inputName
70+
])
71+
])
72+
->setElementaryStreams([
73+
new ElementaryStream([
74+
'key' => 'es_video',
75+
'video_stream' => new VideoStream([
76+
'h264' => new VideoStream\H264CodecSettings([
77+
'profile' => 'high',
78+
'width_pixels' => 1280,
79+
'height_pixels' => 720,
80+
'bitrate_bps' => 3000000,
81+
'frame_rate' => 30
82+
])
83+
]),
84+
]),
85+
new ElementaryStream([
86+
'key' => 'es_audio',
87+
'audio_stream' => new AudioStream([
88+
'codec' => 'aac',
89+
'channel_count' => 2,
90+
'bitrate_bps' => 160000
91+
])
92+
])
93+
])
94+
->setOutput(new Channel\Output(['uri' => $outputUri]))
95+
->setMuxStreams([
96+
new MuxStream([
97+
'key' => 'mux_video',
98+
'elementary_streams' => ['es_video'],
99+
'segment_settings' => new SegmentSettings([
100+
'segment_duration' => new Duration(['seconds' => 2])
101+
])
102+
]),
103+
new MuxStream([
104+
'key' => 'mux_audio',
105+
'elementary_streams' => ['es_audio'],
106+
'segment_settings' => new SegmentSettings([
107+
'segment_duration' => new Duration(['seconds' => 2])
108+
])
109+
]),
110+
])
111+
->setManifests([
112+
new Manifest([
113+
'file_name' => 'manifest.m3u8',
114+
'type' => Manifest\ManifestType::HLS,
115+
'mux_streams' => ['mux_video', 'mux_audio'],
116+
'max_segment_count' => 5
117+
])
118+
]);
119+
120+
// Run the channel creation request. The response is a long-running operation ID.
121+
$operationResponse = $livestreamClient->createChannel($parent, $channel, $channelId);
122+
$operationResponse->pollUntilComplete();
123+
if ($operationResponse->operationSucceeded()) {
124+
$result = $operationResponse->getResult();
125+
// Print results
126+
printf('Channel: %s' . PHP_EOL, $result->getName());
127+
} else {
128+
$error = $operationResponse->getError();
129+
// handleError($error)
130+
}
131+
}
132+
// [END livestream_create_channel]
133+
134+
// The following 2 lines are only needed to run the samples
135+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
136+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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/livestream/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\Media\LiveStream;
26+
27+
// [START livestream_create_channel_with_backup_input]
28+
use Google\Cloud\Video\LiveStream\V1\LivestreamServiceClient;
29+
use Google\Cloud\Video\LiveStream\V1\AudioStream;
30+
use Google\Cloud\Video\LiveStream\V1\Channel;
31+
use Google\Cloud\Video\LiveStream\V1\ElementaryStream;
32+
use Google\Cloud\Video\LiveStream\V1\InputAttachment;
33+
use Google\Cloud\Video\LiveStream\V1\Manifest;
34+
use Google\Cloud\Video\LiveStream\V1\MuxStream;
35+
use Google\Cloud\Video\LiveStream\V1\SegmentSettings;
36+
use Google\Cloud\Video\LiveStream\V1\VideoStream;
37+
use Google\Protobuf\Duration;
38+
39+
/**
40+
* Creates a channel with a backup input.
41+
*
42+
* @param string $callingProjectId The project ID to run the API call under
43+
* @param string $location The location of the channel
44+
* @param string $channelId The ID of the channel to be created
45+
* @param string $primaryInputId The ID of the primary input for the channel
46+
* @param string $backupInputId The ID of the backup input for the channel
47+
* @param string $outputUri Uri of the channel output folder in a
48+
* Cloud Storage bucket. (e.g.
49+
* "gs://my-bucket/my-output-folder/")
50+
*/
51+
function create_channel_with_backup_input(
52+
string $callingProjectId,
53+
string $location,
54+
string $channelId,
55+
string $primaryInputId,
56+
string $backupInputId,
57+
string $outputUri
58+
): void {
59+
// Instantiate a client.
60+
$livestreamClient = new LivestreamServiceClient();
61+
62+
$parent = $livestreamClient->locationName($callingProjectId, $location);
63+
$channelName = $livestreamClient->channelName($callingProjectId, $location, $channelId);
64+
$primaryInputName = $livestreamClient->inputName($callingProjectId, $location, $primaryInputId);
65+
$backupInputName = $livestreamClient->inputName($callingProjectId, $location, $backupInputId);
66+
67+
$channel = (new Channel())
68+
->setName($channelName)
69+
->setInputAttachments([
70+
new InputAttachment([
71+
'key' => 'my-primary-input',
72+
'input' => $primaryInputName,
73+
'automatic_failover' => new InputAttachment\AutomaticFailover([
74+
'input_keys' => ['my-backup-input']
75+
])
76+
]),
77+
new InputAttachment([
78+
'key' => 'my-backup-input',
79+
'input' => $backupInputName
80+
])
81+
])
82+
->setElementaryStreams([
83+
new ElementaryStream([
84+
'key' => 'es_video',
85+
'video_stream' => new VideoStream([
86+
'h264' => new VideoStream\H264CodecSettings([
87+
'profile' => 'high',
88+
'width_pixels' => 1280,
89+
'height_pixels' => 720,
90+
'bitrate_bps' => 3000000,
91+
'frame_rate' => 30
92+
])
93+
]),
94+
]),
95+
new ElementaryStream([
96+
'key' => 'es_audio',
97+
'audio_stream' => new AudioStream([
98+
'codec' => 'aac',
99+
'channel_count' => 2,
100+
'bitrate_bps' => 160000
101+
])
102+
])
103+
])
104+
->setOutput(new Channel\Output(['uri' => $outputUri]))
105+
->setMuxStreams([
106+
new MuxStream([
107+
'key' => 'mux_video',
108+
'elementary_streams' => ['es_video'],
109+
'segment_settings' => new SegmentSettings([
110+
'segment_duration' => new Duration(['seconds' => 2])
111+
])
112+
]),
113+
new MuxStream([
114+
'key' => 'mux_audio',
115+
'elementary_streams' => ['es_audio'],
116+
'segment_settings' => new SegmentSettings([
117+
'segment_duration' => new Duration(['seconds' => 2])
118+
])
119+
]),
120+
])
121+
->setManifests([
122+
new Manifest([
123+
'file_name' => 'manifest.m3u8',
124+
'type' => Manifest\ManifestType::HLS,
125+
'mux_streams' => ['mux_video', 'mux_audio'],
126+
'max_segment_count' => 5
127+
])
128+
]);
129+
130+
// Run the channel creation request. The response is a long-running operation ID.
131+
$operationResponse = $livestreamClient->createChannel($parent, $channel, $channelId);
132+
$operationResponse->pollUntilComplete();
133+
if ($operationResponse->operationSucceeded()) {
134+
$result = $operationResponse->getResult();
135+
// Print results
136+
printf('Channel: %s' . PHP_EOL, $result->getName());
137+
} else {
138+
$error = $operationResponse->getError();
139+
// handleError($error)
140+
}
141+
}
142+
// [END livestream_create_channel_with_backup_input]
143+
144+
// The following 2 lines are only needed to run the samples
145+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
146+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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/livestream/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\Media\LiveStream;
26+
27+
// [START livestream_delete_channel]
28+
use Google\Cloud\Video\LiveStream\V1\LivestreamServiceClient;
29+
30+
/**
31+
* Deletes a channel.
32+
*
33+
* @param string $callingProjectId The project ID to run the API call under
34+
* @param string $location The location of the channel
35+
* @param string $channelId The ID of the channel to be deleted
36+
*/
37+
function delete_channel(
38+
string $callingProjectId,
39+
string $location,
40+
string $channelId
41+
): void {
42+
// Instantiate a client.
43+
$livestreamClient = new LivestreamServiceClient();
44+
$formattedName = $livestreamClient->channelName($callingProjectId, $location, $channelId);
45+
46+
// Run the channel deletion request. The response is a long-running operation ID.
47+
$operationResponse = $livestreamClient->deleteChannel($formattedName);
48+
$operationResponse->pollUntilComplete();
49+
if ($operationResponse->operationSucceeded()) {
50+
// Print status
51+
printf('Deleted channel %s' . PHP_EOL, $channelId);
52+
} else {
53+
$error = $operationResponse->getError();
54+
// handleError($error)
55+
}
56+
}
57+
// [END livestream_delete_channel]
58+
59+
// The following 2 lines are only needed to run the samples
60+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
61+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)