Skip to content

Instantly share code, notes, and snippets.

@JEEN
Created February 19, 2019 05:53
Show Gist options
  • Save JEEN/66a37cd44eca1dcf8662ac16ec1d6438 to your computer and use it in GitHub Desktop.
Save JEEN/66a37cd44eca1dcf8662ac16ec1d6438 to your computer and use it in GitHub Desktop.
MediaChannel.ts
import {readFileSync} from 'fs';
import Container, { ContainerInfo } from './Container';
import MediaLiveRole from './MediaLiveRole';
import { config } from 'aws-sdk';
export default class LiveChannel {
inputStreamName: string;
mediaLive: AWS.MediaLive;
inputSecurityGroup: AWS.MediaLive.InputSecurityGroup;
input: AWS.MediaLive.Input;
containerEndpoints: Array<ContainerInfo>;
mediaLiveRole: MediaLiveRole;
constructor(inputStreamName: string, containerEndpoints:Array<ContainerInfo>, mediaLive: AWS.MediaLive) {
this.inputStreamName = inputStreamName;
this.containerEndpoints = containerEndpoints;
this.mediaLive = mediaLive;
this.inputSecurityGroup = <AWS.MediaLive.InputSecurityGroup>{};
this.input = <AWS.MediaLive.Input>{};
this.mediaLiveRole = <MediaLiveRole>{};
}
async prepareIAM(iam:AWS.IAM) {
const mediaLiveRole = new MediaLiveRole(iam);
await mediaLiveRole.createMediaLiveAccessRole();
await mediaLiveRole.putRolePolicy();
this.mediaLiveRole = mediaLiveRole;
}
async createInputSecurityGroups(cidr:string) {
const resp = await this.mediaLive.createInputSecurityGroup({
WhitelistRules: [
{
Cidr: cidr
}
]
}).promise();
if (resp.SecurityGroup) {
this.inputSecurityGroup = resp.SecurityGroup;
}
return;
}
async createInput(inputName:string) {
const securityGroupId = this.inputSecurityGroup.Id || 1234;
const resp = await this.mediaLive.createInput({
Destinations: [
{
StreamName: 'input1'
},
{
StreamName: 'input2'
}
],
InputSecurityGroups: [ securityGroupId.toString()
],
Name: inputName,
Type: "RTMP_PUSH"
}).promise();
if (resp.Input) {
this.input = resp.Input;
}
}
_makeChannelConf() {
const liveInput = this.input;
const liveInputId = liveInput.Id || '1234';
const liveInputName = liveInput.Name || 'input';
let configuration = readFileSync('./resources/livechannel-template.json').toString();
configuration = configuration.replace(/__DESTINATION_ID__/gi, 'live')
.replace(/__INPUT_ID__/gi, liveInputId)
.replace(/__INPUT_NAME__/gi, liveInputName)
.replace(/__ROLE_ARN__/gi, this.mediaLiveRole.role.Arn);
this.containerEndpoints.map((containerInfo) => {
console.log(containerInfo);
if (containerInfo.type === "ACTIVE") {
configuration = configuration.replace(/__ACTIVE_MEDIASTORE_ENDPOINT__/gi, containerInfo.endpointAsMediaStoreScheme());
}
else if (containerInfo.type === "STANDBY") {
configuration = configuration.replace(/__STANDBY_MEDIASTORE_ENDPOINT__/gi, containerInfo.endpointAsMediaStoreScheme());
}
});
return configuration;
}
async createChannel() {
let configuration = this._makeChannelConf();
console.log(configuration);
const resp = this.mediaLive.createChannel(JSON.parse(configuration)).promise();
console.log(resp);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment