Skip to content

Commit 1fda5b3

Browse files
irataxydizcology
authored andcommitted
docs(samples): add sample for creating a channel with a failover backup input (#37)
1 parent 4b51400 commit 1fda5b3

File tree

2 files changed

+187
-0
lines changed

2 files changed

+187
-0
lines changed

video/live-stream/channel_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import pytest
2020

2121
import create_channel
22+
import create_channel_with_backup_input
2223
import create_input
2324
import delete_channel
2425
import delete_channel_event
@@ -134,7 +135,14 @@ def test_channel_operations(capsys: pytest.fixture) -> None:
134135
out, _ = capsys.readouterr()
135136
assert "Deleted channel" in out
136137

138+
create_channel_with_backup_input.create_channel_with_backup_input(
139+
project_name, location, channel_id, input_id, updated_input_id, output_uri
140+
)
141+
out, _ = capsys.readouterr()
142+
assert channel_name_project_id in out
143+
137144
# Clean up
138145

146+
delete_channel.delete_channel(project_name, location, channel_id)
139147
delete_input.delete_input(project_name, location, input_id)
140148
delete_input.delete_input(project_name, location, updated_input_id)
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2022 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Google Cloud Live Stream sample for creating a channel with a backup input.
18+
A channel resource represents the processor that performs a user-defined
19+
"streaming" operation.
20+
Example usage:
21+
python create_channel_with_backup_input.py --project_id <project-id> \
22+
--location <location> --channel_id <channel-id> \
23+
--primary_input_id <primary-input-id> \
24+
--backup_input_id <backup-input-id> --output_uri <uri>
25+
"""
26+
27+
# [START livestream_create_channel_with_backup_input]
28+
29+
import argparse
30+
31+
from google.cloud.video import live_stream_v1
32+
from google.cloud.video.live_stream_v1.services.livestream_service import (
33+
LivestreamServiceClient,
34+
)
35+
from google.protobuf import duration_pb2 as duration
36+
37+
38+
def create_channel_with_backup_input(
39+
project_id: str,
40+
location: str,
41+
channel_id: str,
42+
primary_input_id: str,
43+
backup_input_id: str,
44+
output_uri: str,
45+
) -> str:
46+
"""Creates a channel.
47+
Args:
48+
project_id: The GCP project ID.
49+
location: The location in which to create the channel.
50+
channel_id: The user-defined channel ID.
51+
primary_input_id: The user-defined primary input ID.
52+
backup_input_id: The user-defined backup input ID.
53+
output_uri: Uri of the channel output folder in a Cloud Storage bucket."""
54+
55+
client = LivestreamServiceClient()
56+
parent = f"projects/{project_id}/locations/{location}"
57+
primary_input = (
58+
f"projects/{project_id}/locations/{location}/inputs/{primary_input_id}"
59+
)
60+
backup_input = (
61+
f"projects/{project_id}/locations/{location}/inputs/{backup_input_id}"
62+
)
63+
name = f"projects/{project_id}/locations/{location}/channels/{channel_id}"
64+
65+
channel = live_stream_v1.types.Channel(
66+
name=name,
67+
input_attachments=[
68+
live_stream_v1.types.InputAttachment(
69+
key="my-primary-input",
70+
input=primary_input,
71+
automatic_failover=live_stream_v1.types.InputAttachment.AutomaticFailover(
72+
input_keys=["my-backup-input"],
73+
),
74+
),
75+
live_stream_v1.types.InputAttachment(
76+
key="my-backup-input",
77+
input=backup_input,
78+
),
79+
],
80+
output=live_stream_v1.types.Channel.Output(
81+
uri=output_uri,
82+
),
83+
elementary_streams=[
84+
live_stream_v1.types.ElementaryStream(
85+
key="es_video",
86+
video_stream=live_stream_v1.types.VideoStream(
87+
h264=live_stream_v1.types.VideoStream.H264CodecSettings(
88+
profile="main",
89+
width_pixels=1280,
90+
height_pixels=720,
91+
bitrate_bps=1000000,
92+
frame_rate=30,
93+
),
94+
),
95+
),
96+
live_stream_v1.types.ElementaryStream(
97+
key="es_audio",
98+
audio_stream=live_stream_v1.types.AudioStream(
99+
codec="aac", channel_count=2, bitrate_bps=160000
100+
),
101+
),
102+
],
103+
mux_streams=[
104+
live_stream_v1.types.MuxStream(
105+
key="mux_video",
106+
elementary_streams=["es_video"],
107+
segment_settings=live_stream_v1.types.SegmentSettings(
108+
segment_duration=duration.Duration(
109+
seconds=2,
110+
),
111+
),
112+
),
113+
live_stream_v1.types.MuxStream(
114+
key="mux_audio",
115+
elementary_streams=["es_audio"],
116+
segment_settings=live_stream_v1.types.SegmentSettings(
117+
segment_duration=duration.Duration(
118+
seconds=2,
119+
),
120+
),
121+
),
122+
],
123+
manifests=[
124+
live_stream_v1.types.Manifest(
125+
file_name="manifest.m3u8",
126+
type_="HLS",
127+
mux_streams=["mux_video", "mux_audio"],
128+
max_segment_count=5,
129+
),
130+
],
131+
)
132+
operation = client.create_channel(
133+
parent=parent, channel=channel, channel_id=channel_id
134+
)
135+
response = operation.result(60)
136+
print(f"Channel: {response.name}")
137+
138+
return response
139+
140+
141+
# [END livestream_create_channel_with_backup_input]
142+
143+
if __name__ == "__main__":
144+
parser = argparse.ArgumentParser()
145+
parser.add_argument("--project_id", help="Your Cloud project ID.", required=True)
146+
parser.add_argument(
147+
"--location",
148+
help="The location in which to create the channel.",
149+
default="us-central1",
150+
)
151+
parser.add_argument(
152+
"--channel_id",
153+
help="The user-defined channel ID.",
154+
required=True,
155+
)
156+
parser.add_argument(
157+
"--primary_input_id",
158+
help="The user-defined primary input ID.",
159+
required=True,
160+
)
161+
parser.add_argument(
162+
"--backup_input_id",
163+
help="The user-defined backup input ID.",
164+
required=True,
165+
)
166+
parser.add_argument(
167+
"--output_uri",
168+
help="The Cloud Storage bucket (and optional folder) in which to save the livestream output.",
169+
required=True,
170+
)
171+
args = parser.parse_args()
172+
create_channel_with_backup_input(
173+
args.project_id,
174+
args.location,
175+
args.channel_id,
176+
args.primary_input_id,
177+
args.backup_input_id,
178+
args.output_uri,
179+
)

0 commit comments

Comments
 (0)