|
| 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); |
0 commit comments