Skip to content

Commit

Permalink
add sample for creating a channel with backup input (#30)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
irataxy authored May 4, 2022
1 parent 968094f commit 56e9489
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 17 deletions.
138 changes: 138 additions & 0 deletions media/livestream/createChannelWithBackupInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/**
* Copyright 2022, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

function main(
projectId,
location,
channelId,
primaryInputId,
backupInputId,
outputUri
) {
// [START livestream_create_channel_with_backup_input]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// projectId = 'my-project-id';
// location = 'us-central1';
// channelId = 'my-channel';
// primaryInputId = 'my-primary-input';
// backupInputId = 'my-backup-input';
// outputUri = 'gs://my-bucket/my-output-folder/';

// Imports the Livestream library
const {LivestreamServiceClient} = require('@google-cloud/livestream').v1;

// Instantiates a client
const livestreamServiceClient = new LivestreamServiceClient();

async function createChannelWithBackupInput() {
// Construct request
const request = {
parent: livestreamServiceClient.locationPath(projectId, location),
channelId: channelId,
channel: {
inputAttachments: [
{
key: 'my-primary-input',
input: livestreamServiceClient.inputPath(
projectId,
location,
primaryInputId
),
automaticFailover: {
inputKeys: ['my-backup-input'],
},
},
{
key: 'my-backup-input',
input: livestreamServiceClient.inputPath(
projectId,
location,
backupInputId
),
},
],
output: {
uri: outputUri,
},
elementaryStreams: [
{
key: 'es_video',
videoStream: {
h264: {
profile: 'main',
heightPixels: 720,
widthPixels: 1280,
bitrateBps: 1000000,
frameRate: 30,
},
},
},
{
key: 'es_audio',
audioStream: {
codec: 'aac',
channelCount: 2,
bitrateBps: 160000,
},
},
],
muxStreams: [
{
key: 'mux_video',
elementaryStreams: ['es_video'],
segmentSettings: {
seconds: 2,
},
},
{
key: 'mux_audio',
elementaryStreams: ['es_audio'],
segmentSettings: {
seconds: 2,
},
},
],
manifests: [
{
fileName: 'manifest.m3u8',
type: 'HLS',
muxStreams: ['mux_video', 'mux_audio'],
maxSegmentCount: 5,
},
],
},
};

// Run request
const [operation] = await livestreamServiceClient.createChannel(request);
const response = await operation.promise();
const [channel] = response;
console.log(`Channel: ${channel.name}`);
}

createChannelWithBackupInput();
// [END livestream_create_channel_with_backup_input]
}

// node createChannelWithBackupInput.js <projectId> <location> <channelId> <primaryInputId> <backupInputId> <outputUri>
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
64 changes: 47 additions & 17 deletions media/livestream/test/livestream.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ const bucketName = `nodejs-samples-livestream-test-${uniqueID}`;
const projectId = process.env.GCLOUD_PROJECT;
const location = 'us-central1';
const inputId = `nodejs-test-livestream-input-${uniqueID}`;
const inputNameProjectId = `projects/${projectId}/locations/${location}/inputs/${inputId}`;
const inputName = `projects/${projectId}/locations/${location}/inputs/${inputId}`;
const backupInputId = `nodejs-test-livestream-backup-input-${uniqueID}`;
const backupInputName = `projects/${projectId}/locations/${location}/inputs/${backupInputId}`;
const channelId = `nodejs-test-livestream-channel-${uniqueID}`;
const channelIdProjectId = `projects/${projectId}/locations/${location}/channels/${channelId}`;
const channelName = `projects/${projectId}/locations/${location}/channels/${channelId}`;
const eventId = `nodejs-test-livestream-event-${uniqueID}`;
const eventIdProjectId = `projects/${projectId}/locations/${location}/channels/${channelId}/events/${eventId}`;
const eventName = `projects/${projectId}/locations/${location}/channels/${channelId}/events/${eventId}`;
const outputUri = `gs://${bucketName}/test-output-channel/`;
const cwd = path.join(__dirname, '..');

Expand Down Expand Up @@ -108,30 +110,30 @@ describe('Input functions', () => {
`node createInput.js ${projectId} ${location} ${inputId}`,
{cwd}
);
assert.ok(output.includes(inputNameProjectId));
assert.ok(output.includes(inputName));
});

it('should show a list of inputs', () => {
const output = execSync(`node listInputs.js ${projectId} ${location}`, {
cwd,
});
assert.ok(output.includes(inputNameProjectId));
assert.ok(output.includes(inputName));
});

it('should update an input', () => {
const output = execSync(
`node updateInput.js ${projectId} ${location} ${inputId}`,
{cwd}
);
assert.ok(output.includes(inputNameProjectId));
assert.ok(output.includes(inputName));
});

it('should get an input', () => {
const output = execSync(
`node getInput.js ${projectId} ${location} ${inputId}`,
{cwd}
);
assert.ok(output.includes(inputNameProjectId));
assert.ok(output.includes(inputName));
});

it('should delete an input', () => {
Expand All @@ -149,7 +151,13 @@ describe('Channel functions', () => {
`node createInput.js ${projectId} ${location} ${inputId}`,
{cwd}
);
assert.ok(output.includes(inputNameProjectId));
assert.ok(output.includes(inputName));

const output2 = execSync(
`node createInput.js ${projectId} ${location} ${backupInputId}`,
{cwd}
);
assert.ok(output2.includes(backupInputName));
});

after(() => {
Expand All @@ -158,37 +166,43 @@ describe('Channel functions', () => {
{cwd}
);
assert.ok(output.includes('Deleted input'));

const output2 = execSync(
`node deleteInput.js ${projectId} ${location} ${backupInputId}`,
{cwd}
);
assert.ok(output2.includes('Deleted input'));
});

it('should create a channel', () => {
const output = execSync(
`node createChannel.js ${projectId} ${location} ${channelId} ${inputId} ${outputUri}`,
{cwd}
);
assert.ok(output.includes(channelIdProjectId));
assert.ok(output.includes(channelName));
});

it('should show a list of channels', () => {
const output = execSync(`node listChannels.js ${projectId} ${location}`, {
cwd,
});
assert.ok(output.includes(channelIdProjectId));
assert.ok(output.includes(channelName));
});

it('should update an channel', () => {
const output = execSync(
`node updateChannel.js ${projectId} ${location} ${channelId} ${inputId}`,
{cwd}
);
assert.ok(output.includes(channelIdProjectId));
assert.ok(output.includes(channelName));
});

it('should get an channel', () => {
const output = execSync(
`node getChannel.js ${projectId} ${location} ${channelId}`,
{cwd}
);
assert.ok(output.includes(channelIdProjectId));
assert.ok(output.includes(channelName));
});

it('should start a channel', () => {
Expand All @@ -214,6 +228,22 @@ describe('Channel functions', () => {
);
assert.ok(output.includes('Deleted channel'));
});

it('should create a channel with backup input', () => {
const output = execSync(
`node createChannelWithBackupInput.js ${projectId} ${location} ${channelId} ${inputId} ${backupInputId} ${outputUri}`,
{cwd}
);
assert.ok(output.includes(channelName));
});

it('should delete a channel with backup input', () => {
const output = execSync(
`node deleteChannel.js ${projectId} ${location} ${channelId}`,
{cwd}
);
assert.ok(output.includes('Deleted channel'));
});
});

describe('Channel event functions', () => {
Expand All @@ -222,13 +252,13 @@ describe('Channel event functions', () => {
`node createInput.js ${projectId} ${location} ${inputId}`,
{cwd}
);
assert.ok(output.includes(inputNameProjectId));
assert.ok(output.includes(inputName));

output = execSync(
`node createChannel.js ${projectId} ${location} ${channelId} ${inputId} ${outputUri}`,
{cwd}
);
assert.ok(output.includes(channelIdProjectId));
assert.ok(output.includes(channelName));

output = execSync(
`node startChannel.js ${projectId} ${location} ${channelId}`,
Expand Down Expand Up @@ -262,23 +292,23 @@ describe('Channel event functions', () => {
`node createChannelEvent.js ${projectId} ${location} ${channelId} ${eventId}`,
{cwd}
);
assert.ok(output.includes(eventIdProjectId));
assert.ok(output.includes(eventName));
});

it('should show a list of channel events', () => {
const output = execSync(
`node listChannelEvents.js ${projectId} ${location} ${channelId}`,
{cwd}
);
assert.ok(output.includes(eventIdProjectId));
assert.ok(output.includes(eventName));
});

it('should get a channel event', () => {
const output = execSync(
`node getChannelEvent.js ${projectId} ${location} ${channelId} ${eventId}`,
{cwd}
);
assert.ok(output.includes(eventIdProjectId));
assert.ok(output.includes(eventName));
});

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

0 comments on commit 56e9489

Please sign in to comment.