Skip to content

Instantly share code, notes, and snippets.

@gtk2k
Created May 12, 2020 14:31
Show Gist options
  • Select an option

  • Save gtk2k/aca3c2d55221c2efc697304938a41433 to your computer and use it in GitHub Desktop.

Select an option

Save gtk2k/aca3c2d55221c2efc697304938a41433 to your computer and use it in GitHub Desktop.
Wowza REST API JavaScript(Node.js)
import crypto from 'crypto';
import https from 'https';
export class WowzaAPI {
constructor(apiKey, accessKey) {
this.apiVersion = '1.4';
this.apiSignaturePath = '/api/v1.4';
this.apiKey = apiKey;
this.accessKey = accessKey;
}
get basePath() {
return `https://api.cloud.wowza.com`;
}
async createLiveStream(requestParams) {
const res = await this.exec('/live_streams', 'live_stream', 'POST', requestParams);
return res;
}
async startLiveStream(id) {
const res = await this.exec(`/live_streams/${id}/start`, 'live_stream', 'PUT');
return res;
}
async stopLiveStream(id) {
const res = await this.exec(`/live_streams/${id}/stop`, 'live_stream', 'PUT');
return res;
}
async updateLiveStream(id) {
const res = await this.exec(`/live_streams/${id}`, 'live_stream', 'PATCH');
return res;
}
async deleteLiveStream(id) {
const res = await this.exec(`/live_streams/${id}`, 'live_stream', 'DELETE');
return res;
}
async stateLiveStream(id) {
const res = await this.exec(`/live_streams/${id}/state`, 'live_stream', 'GET');
return res;
}
async createUltraLowLatencyStreamTarget() {
const res = await this.exec('/stream_targets/ull', 'stream_target_ull', 'POST');
return res;
}
async exec(requestPath, jsonRootKey, method, postData = null) {
const timeStamp = Date.now().toString();
postData = postData ? { [jsonRootKey]: postData } : null;
const signature = this.createRequestSignature(requestPath, timeStamp);
const opt = {
host: 'api.cloud.wowza.com',
port: 443,
path: `${this.apiSignaturePath}${requestPath}`,
method,
headers: {
'Content-Type': 'application/json',
'wsc-access-key': this.accessKey,
'wsc-timestamp': timeStamp,
'wsc-signature': signature
}
};
const res = await new Promise(r => {
const req = https.request(opt, res => {
res.on('data', d => r(d));
});
if (method === 'POST' && postData !== null) {
req.write(JSON.stringify(postData));
}
req.end();
});
if (rootObj) {
if (rootObj.meta) {
const meta = rootObj.meta;
return meta;
}
return rootObj[jsonRootKey];
}
if (method === 'DELETE')
return { state: 'deleted' };
else
return null;
}
createRequestSignature(requestPath, timeStamp) {
requestPath = requestPath.split('?')[0];
if (!requestPath.startsWith('/'))
requestPath = `/${requestPath}`;
if (requestPath.endsWith('/'))
requestPath = requestPath.substr(0, requestPath.Length - 1);
requestPath = this.apiSignaturePath + requestPath;
const data = `${timeStamp}:${requestPath}:${this.apiKey}`;
const hmac = crypto.createHmac('sha256', this.apiKey);
hmac.update(data);
const ret = hmac.digest('hex');
return ret;
}
}
export const billing_modes = {
pay_as_you_go: 'pay_as_you_go',
twentyfour_seven: 'twentyfour_seven'
};
export const broadcast_locations = {
asia_pacific_australia: 'asia_pacific_australia',
asia_pacific_india: 'asia_pacific_india',
asia_pacific_japan: 'asia_pacific_japan',
asia_pacific_singapore: 'asia_pacific_singapore',
asia_pacific_s_korea: 'asia_pacific_s_korea',
asia_pacific_taiwan: 'asia_pacific_taiwan',
eu_belgium: 'eu_belgium',
eu_germany: 'eu_germany',
eu_ireland: 'eu_ireland',
south_america_brazil: 'south_america_brazil',
us_central_iowa: 'us_central_iowa',
us_east_s_carolina: 'us_east_s_carolina',
us_east_virginia: 'us_east_virginia',
us_west_california: 'us_west_california',
us_west_oregon: 'us_west_oregon'
};
export const encoders = {
wowza_clearcaster: 'wowza_clearcaster',
wowza_gocoder: 'wowza_gocoder',
wowza_streaming_engine: 'wowza_streaming_engine',
media_ds: 'media_ds',
axis: 'axis',
epiphan: 'epiphan',
hauppauge: 'hauppauge',
jvc: 'jvc',
live_u: 'live_u',
matrox: 'matrox',
newtek_tricaster: 'newtek_tricaster',
osprey: 'osprey',
sony: 'sony',
telestream_wirecast: 'telestream_wirecast',
teradek_cube: 'teradek_cube',
vmix: 'vmix',
x_split: 'x_split',
ipcamera: 'ipcamera',
other_rtmp: 'other_rtmp',
other_rtsp: 'other_rtsp',
other_webrtc: 'other_webrtc'
};
export const transcoder_types = {
transcoded: 'transcoded',
passthrough: 'passthrough'
};
export const closed_caption_types = {
none: 'none',
cea: 'cea',
on_text: 'on_text',
both: 'both'
};
export const delivery_methods = {
pull: 'pull',
cdn: 'cdn',
push: 'push'
};
export const delivery_types = {
single_bitrate: 'single-bitrate',
multi_bitrate: 'multi-bitrate'
};
export const player_logo_positions = {
top_left: 'top-left',
top_right: 'top-right',
bottom_left: 'bottom-left',
bottom_right: 'bottom-right'
};
export const player_types = {
original_html5: 'original_html5',
wowza_player: 'wowza_player'
};
export const target_delivery_protocols = {
hls_https: 'hls-https',
hls_hds: 'hls-hds'
}
export const source_delivery_types = {
push: 'push',
pull: 'pull'
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment