skyriver
Version:
SkyRiver Streaming Cloud server-side library for NodeJS
285 lines (261 loc) • 8.73 kB
JavaScript
'use strict';
const SkyRiver = require('skyriver');
const Utils = require('../utils');
const Config = require('../config');
const path = require('path');
const logger = Utils.logger(__filename);
const Promise = require('bluebird');
const filepath = path.join(__dirname, 'streams.json');
SkyRiver.config.API_HOST = Config.skyriver.api_host;
SkyRiver.config.API_PORT = Config.skyriver.api_port;
SkyRiver.config.APP_KEY = Config.skyriver.app_key;
SkyRiver.config.APP_SECRET = Config.skyriver.app_secret;
const credentials = new SkyRiver.Credentials();
const hub = new SkyRiver.Hub(credentials);
Promise.promisifyAll(hub);
Promise.promisifyAll(SkyRiver.Stream.prototype);
/**
* Create a new Stream
*/
exports.createStream = function * () {
try {
const options = {
title: null, // optional, auto-generated as default
publishKey: null, // optional, auto-generated as default
publishSecurity: 'static' // optional, can be "dynamic" or "static", "dynamic" as default
};
const title = this.request.body.p1;
const publishkey = this.request.body.p2;
const pubkey = this.request.body.p3;
console.dir(this.request.body);
logger.info('pubkey=============' + pubkey);
options.title = title;
options.publishKey = publishkey;
const sinfo = yield Utils.fs.readFileAsync(filepath);
const steamArrs = JSON.parse(sinfo);
const resstreamInfo = yield hub.createStreamAsync(options);
const streamInfo = resstreamInfo;
streamInfo.playurl = resstreamInfo.playUrl(); // 生成天河请求播放地址接口,带token
streamInfo.rtmpPublishUrl = resstreamInfo.rtmpPublishUrl();// 生成完整推流地址,带token
steamArrs[pubkey] = streamInfo;
yield Utils.fs.writeFileAsync(filepath, JSON.stringify(steamArrs));
this.body = streamInfo;
} catch (err) {
this.body = err.message;
}
}
;
exports.getStream = function * () {
try {
const streamId = this.params.key;
const resstreamInfo = yield hub.getStreamAsync(streamId);
console.log(JSON.stringify(resstreamInfo));
this.body = resstreamInfo;
} catch (err) {
this.body = err.message;
}
}
;
exports.getStreamList = function * () {
try {
const option = {
limit: 10,
start: 0,
title: '' // 直播流名称,模糊匹配
};
const resstreamInfo = yield hub.listStreamsAsync(option);
console.log('testtttt' + JSON.stringify(resstreamInfo));
this.body = resstreamInfo;
} catch (err) {
this.body = err.message;
}
}
;
exports.delStream = function * () {
try {
const streamId = this.params.key;
const resstreamInfo = yield hub.getStreamAsync(streamId);
const liveStream = new SkyRiver.Stream(credentials, resstreamInfo);
const res = yield liveStream.deleteAsync();
this.body = res;
} catch (err) {
this.body = err.message;
}
}
;
exports.delVod = function * () {
try {
const streamId = this.params.key;
const name = this.query.p5 || this.request.body.p5 || streamId;
const options = { name };
const resstreamInfo = yield hub.getStreamAsync(streamId);
const liveStream = new SkyRiver.Stream(credentials, resstreamInfo);
const res = yield liveStream.delVodAsync(options);
this.body = res;
} catch (err) {
this.body = err.message;
}
}
;
exports.saves = function * () {
try {
const streamId = this.params.key;
const resstreamInfo = yield hub.getStreamAsync(streamId);
const liveStream = new SkyRiver.Stream(credentials, resstreamInfo);
const name = this.request.body.p5 || streamId;
const start = 0;
const end = 30;
const format = 'mp4';
const options = { filelist: JSON.parse(this.request.body.p2) || [] };
const res = yield liveStream.saveAsAsync(name, format, start, end, options);
this.body = res;
} catch (err) {
this.body = err.message;
}
}
;
exports.snapshot = function * () {
try {
const streamId = this.params.key;
const resstreamInfo = yield hub.getStreamAsync(streamId);
const liveStream = new SkyRiver.Stream(credentials, resstreamInfo);
const name = 'test' + streamId;
const format = 'jpg';
const options = {
time: this.request.body.p4 || 10
};
const res = yield liveStream.snapshotAsync(name, format, options);
this.body = res;
} catch (err) {
this.body = err.message;
}
}
;
exports.disableStream = function * () {
try {
const streamId = this.params.key;
if (!streamId) {
this.body = '直播流ID不能为空';
return;
}
const resstreamInfo = yield hub.getStreamAsync(streamId);
const liveStream = new SkyRiver.Stream(credentials, resstreamInfo);
const res = yield liveStream.disableAsync();
this.body = res;
} catch (err) {
this.body = err.message;
}
}
;
exports.enableStream = function * () {
try {
const streamId = this.params.key;
if (!streamId) {
this.body = '直播流ID不能为空';
return;
}
const resstreamInfo = yield hub.getStreamAsync(streamId);
const liveStream = new SkyRiver.Stream(credentials, resstreamInfo);
const res = yield liveStream.enableAsync();
this.body = res;
} catch (err) {
this.body = err.message;
}
}
;
exports.getSegments = function * () {
try {
const streamId = this.params.key;
if (!streamId) {
this.body = '直播流ID不能为空';
return;
}
const resstreamInfo = yield hub.getStreamAsync(streamId);
const liveStream = new SkyRiver.Stream(credentials, resstreamInfo);
const options = {};
const res = yield liveStream.segmentsAsync(options);
this.body = res;
} catch (err) {
this.body = err.message;
}
}
;
exports.getRecordsUrl = function * () {
try {
const streamId = this.params.key;
if (!streamId) {
this.body = '直播流ID不能为空';
return;
}
const resstreamInfo = yield hub.getStreamAsync(streamId);
console.dir(resstreamInfo);
const liveStream = new SkyRiver.Stream(credentials, resstreamInfo);
const res = yield liveStream.recordsAsync();
this.body = res;
} catch (err) {
this.body = err.message;
}
}
;
/**
* 获取实际点播地址,即分发服务器地址
*/
exports.getVodURL = function * () {
try {
const streamId = this.params.key;
if (!streamId) {
this.body = '直播流ID不能为空';
return;
}
const name = this.request.body.p5 || streamId;
const options = { name };
console.dir(this.query);
const resstreamInfo = yield hub.getStreamAsync(streamId);
const liveStream = new SkyRiver.Stream(credentials, resstreamInfo);
const res = yield liveStream.getVodUrlAsync(options);
this.body = res;
} catch (err) {
this.body = err.message;
}
}
;
const getUpToken = function() {
const uptoken = new SkyRiver.rs.PutPolicy();
uptoken.persistentOps = `odconv/png/page/1|saveas/11111;`;
return uptoken.token();
};
/**
* 获取实际播放地址, 即分发服务器地址
*/
exports.getPlayURL = function * () {
try {
const streamId = this.params.key;
if (!streamId) {
this.body = '直播流ID不能为空';
return;
}
const resstreamInfo = yield hub.getStreamAsync(streamId);
const liveStream = new SkyRiver.Stream(credentials, resstreamInfo);
const res = yield liveStream.getPlayUrlAsync();
console.dir(res);
this.body = getUpToken();
} catch (err) {
this.body = err.message;
}
}
;
exports.getPublishUrl = function * () {
try {
const key = this.params.key;
const sinfo = yield Utils.fs.readFileAsync(filepath);
const streamsJson = JSON.parse(sinfo);
const streamInfo = streamsJson[key];
const liveStream = new SkyRiver.Stream(credentials, streamInfo);
const publishUrl = liveStream.rtmpPublishUrl();
const publishServerUrl = liveStream.publishServerUrl();
const streamId = liveStream.id;
this.body = { streamId, publishUrl, publishServerUrl };
} catch (err) {
this.body = err.message;
}
};