Skip to content

Commit 21b1b7d

Browse files
irataxygrayside
authored andcommitted
add sample for creating a channel with backup input (#30)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/nodejs-video-live-stream/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [X] Ensure the tests and linter pass - [X] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Fixes b:219762506
1 parent a2d38df commit 21b1b7d

File tree

2 files changed

+185
-17
lines changed

2 files changed

+185
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/**
2+
* Copyright 2022, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
function main(
19+
projectId,
20+
location,
21+
channelId,
22+
primaryInputId,
23+
backupInputId,
24+
outputUri
25+
) {
26+
// [START livestream_create_channel_with_backup_input]
27+
/**
28+
* TODO(developer): Uncomment these variables before running the sample.
29+
*/
30+
// projectId = 'my-project-id';
31+
// location = 'us-central1';
32+
// channelId = 'my-channel';
33+
// primaryInputId = 'my-primary-input';
34+
// backupInputId = 'my-backup-input';
35+
// outputUri = 'gs://my-bucket/my-output-folder/';
36+
37+
// Imports the Livestream library
38+
const {LivestreamServiceClient} = require('@google-cloud/livestream').v1;
39+
40+
// Instantiates a client
41+
const livestreamServiceClient = new LivestreamServiceClient();
42+
43+
async function createChannelWithBackupInput() {
44+
// Construct request
45+
const request = {
46+
parent: livestreamServiceClient.locationPath(projectId, location),
47+
channelId: channelId,
48+
channel: {
49+
inputAttachments: [
50+
{
51+
key: 'my-primary-input',
52+
input: livestreamServiceClient.inputPath(
53+
projectId,
54+
location,
55+
primaryInputId
56+
),
57+
automaticFailover: {
58+
inputKeys: ['my-backup-input'],
59+
},
60+
},
61+
{
62+
key: 'my-backup-input',
63+
input: livestreamServiceClient.inputPath(
64+
projectId,
65+
location,
66+
backupInputId
67+
),
68+
},
69+
],
70+
output: {
71+
uri: outputUri,
72+
},
73+
elementaryStreams: [
74+
{
75+
key: 'es_video',
76+
videoStream: {
77+
h264: {
78+
profile: 'main',
79+
heightPixels: 720,
80+
widthPixels: 1280,
81+
bitrateBps: 1000000,
82+
frameRate: 30,
83+
},
84+
},
85+
},
86+
{
87+
key: 'es_audio',
88+
audioStream: {
89+
codec: 'aac',
90+
channelCount: 2,
91+
bitrateBps: 160000,
92+
},
93+
},
94+
],
95+
muxStreams: [
96+
{
97+
key: 'mux_video',
98+
elementaryStreams: ['es_video'],
99+
segmentSettings: {
100+
seconds: 2,
101+
},
102+
},
103+
{
104+
key: 'mux_audio',
105+
elementaryStreams: ['es_audio'],
106+
segmentSettings: {
107+
seconds: 2,
108+
},
109+
},
110+
],
111+
manifests: [
112+
{
113+
fileName: 'manifest.m3u8',
114+
type: 'HLS',
115+
muxStreams: ['mux_video', 'mux_audio'],
116+
maxSegmentCount: 5,
117+
},
118+
],
119+
},
120+
};
121+
122+
// Run request
123+
const [operation] = await livestreamServiceClient.createChannel(request);
124+
const response = await operation.promise();
125+
const [channel] = response;
126+
console.log(`Channel: ${channel.name}`);
127+
}
128+
129+
createChannelWithBackupInput();
130+
// [END livestream_create_channel_with_backup_input]
131+
}
132+
133+
// node createChannelWithBackupInput.js <projectId> <location> <channelId> <primaryInputId> <backupInputId> <outputUri>
134+
process.on('unhandledRejection', err => {
135+
console.error(err.message);
136+
process.exitCode = 1;
137+
});
138+
main(...process.argv.slice(2));

media/livestream/test/livestream.test.js

+47-17
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ const bucketName = `nodejs-samples-livestream-test-${uniqueID}`;
2727
const projectId = process.env.GCLOUD_PROJECT;
2828
const location = 'us-central1';
2929
const inputId = `nodejs-test-livestream-input-${uniqueID}`;
30-
const inputNameProjectId = `projects/${projectId}/locations/${location}/inputs/${inputId}`;
30+
const inputName = `projects/${projectId}/locations/${location}/inputs/${inputId}`;
31+
const backupInputId = `nodejs-test-livestream-backup-input-${uniqueID}`;
32+
const backupInputName = `projects/${projectId}/locations/${location}/inputs/${backupInputId}`;
3133
const channelId = `nodejs-test-livestream-channel-${uniqueID}`;
32-
const channelIdProjectId = `projects/${projectId}/locations/${location}/channels/${channelId}`;
34+
const channelName = `projects/${projectId}/locations/${location}/channels/${channelId}`;
3335
const eventId = `nodejs-test-livestream-event-${uniqueID}`;
34-
const eventIdProjectId = `projects/${projectId}/locations/${location}/channels/${channelId}/events/${eventId}`;
36+
const eventName = `projects/${projectId}/locations/${location}/channels/${channelId}/events/${eventId}`;
3537
const outputUri = `gs://${bucketName}/test-output-channel/`;
3638
const cwd = path.join(__dirname, '..');
3739

@@ -108,30 +110,30 @@ describe('Input functions', () => {
108110
`node createInput.js ${projectId} ${location} ${inputId}`,
109111
{cwd}
110112
);
111-
assert.ok(output.includes(inputNameProjectId));
113+
assert.ok(output.includes(inputName));
112114
});
113115

114116
it('should show a list of inputs', () => {
115117
const output = execSync(`node listInputs.js ${projectId} ${location}`, {
116118
cwd,
117119
});
118-
assert.ok(output.includes(inputNameProjectId));
120+
assert.ok(output.includes(inputName));
119121
});
120122

121123
it('should update an input', () => {
122124
const output = execSync(
123125
`node updateInput.js ${projectId} ${location} ${inputId}`,
124126
{cwd}
125127
);
126-
assert.ok(output.includes(inputNameProjectId));
128+
assert.ok(output.includes(inputName));
127129
});
128130

129131
it('should get an input', () => {
130132
const output = execSync(
131133
`node getInput.js ${projectId} ${location} ${inputId}`,
132134
{cwd}
133135
);
134-
assert.ok(output.includes(inputNameProjectId));
136+
assert.ok(output.includes(inputName));
135137
});
136138

137139
it('should delete an input', () => {
@@ -149,7 +151,13 @@ describe('Channel functions', () => {
149151
`node createInput.js ${projectId} ${location} ${inputId}`,
150152
{cwd}
151153
);
152-
assert.ok(output.includes(inputNameProjectId));
154+
assert.ok(output.includes(inputName));
155+
156+
const output2 = execSync(
157+
`node createInput.js ${projectId} ${location} ${backupInputId}`,
158+
{cwd}
159+
);
160+
assert.ok(output2.includes(backupInputName));
153161
});
154162

155163
after(() => {
@@ -158,37 +166,43 @@ describe('Channel functions', () => {
158166
{cwd}
159167
);
160168
assert.ok(output.includes('Deleted input'));
169+
170+
const output2 = execSync(
171+
`node deleteInput.js ${projectId} ${location} ${backupInputId}`,
172+
{cwd}
173+
);
174+
assert.ok(output2.includes('Deleted input'));
161175
});
162176

163177
it('should create a channel', () => {
164178
const output = execSync(
165179
`node createChannel.js ${projectId} ${location} ${channelId} ${inputId} ${outputUri}`,
166180
{cwd}
167181
);
168-
assert.ok(output.includes(channelIdProjectId));
182+
assert.ok(output.includes(channelName));
169183
});
170184

171185
it('should show a list of channels', () => {
172186
const output = execSync(`node listChannels.js ${projectId} ${location}`, {
173187
cwd,
174188
});
175-
assert.ok(output.includes(channelIdProjectId));
189+
assert.ok(output.includes(channelName));
176190
});
177191

178192
it('should update an channel', () => {
179193
const output = execSync(
180194
`node updateChannel.js ${projectId} ${location} ${channelId} ${inputId}`,
181195
{cwd}
182196
);
183-
assert.ok(output.includes(channelIdProjectId));
197+
assert.ok(output.includes(channelName));
184198
});
185199

186200
it('should get an channel', () => {
187201
const output = execSync(
188202
`node getChannel.js ${projectId} ${location} ${channelId}`,
189203
{cwd}
190204
);
191-
assert.ok(output.includes(channelIdProjectId));
205+
assert.ok(output.includes(channelName));
192206
});
193207

194208
it('should start a channel', () => {
@@ -214,6 +228,22 @@ describe('Channel functions', () => {
214228
);
215229
assert.ok(output.includes('Deleted channel'));
216230
});
231+
232+
it('should create a channel with backup input', () => {
233+
const output = execSync(
234+
`node createChannelWithBackupInput.js ${projectId} ${location} ${channelId} ${inputId} ${backupInputId} ${outputUri}`,
235+
{cwd}
236+
);
237+
assert.ok(output.includes(channelName));
238+
});
239+
240+
it('should delete a channel with backup input', () => {
241+
const output = execSync(
242+
`node deleteChannel.js ${projectId} ${location} ${channelId}`,
243+
{cwd}
244+
);
245+
assert.ok(output.includes('Deleted channel'));
246+
});
217247
});
218248

219249
describe('Channel event functions', () => {
@@ -222,13 +252,13 @@ describe('Channel event functions', () => {
222252
`node createInput.js ${projectId} ${location} ${inputId}`,
223253
{cwd}
224254
);
225-
assert.ok(output.includes(inputNameProjectId));
255+
assert.ok(output.includes(inputName));
226256

227257
output = execSync(
228258
`node createChannel.js ${projectId} ${location} ${channelId} ${inputId} ${outputUri}`,
229259
{cwd}
230260
);
231-
assert.ok(output.includes(channelIdProjectId));
261+
assert.ok(output.includes(channelName));
232262

233263
output = execSync(
234264
`node startChannel.js ${projectId} ${location} ${channelId}`,
@@ -262,23 +292,23 @@ describe('Channel event functions', () => {
262292
`node createChannelEvent.js ${projectId} ${location} ${channelId} ${eventId}`,
263293
{cwd}
264294
);
265-
assert.ok(output.includes(eventIdProjectId));
295+
assert.ok(output.includes(eventName));
266296
});
267297

268298
it('should show a list of channel events', () => {
269299
const output = execSync(
270300
`node listChannelEvents.js ${projectId} ${location} ${channelId}`,
271301
{cwd}
272302
);
273-
assert.ok(output.includes(eventIdProjectId));
303+
assert.ok(output.includes(eventName));
274304
});
275305

276306
it('should get a channel event', () => {
277307
const output = execSync(
278308
`node getChannelEvent.js ${projectId} ${location} ${channelId} ${eventId}`,
279309
{cwd}
280310
);
281-
assert.ok(output.includes(eventIdProjectId));
311+
assert.ok(output.includes(eventName));
282312
});
283313

284314
it('should delete a channel event', () => {

0 commit comments

Comments
 (0)