UNPKG

skyriver

Version:

SkyRiver Streaming Cloud server-side library for NodeJS

440 lines (385 loc) 9.56 kB
'use strict'; var Request = require('./request') , Stream = require('./stream'); var API = {}; exports.API = API; exports.init = function() { if (!API.request) { API.request = new Request(); } }; // client APIs exports.createStream = function(credentials, hub, options, fn) { var data = { 'hub': hub }; if (options) { if (options.title) { data['title'] = options.title; } if (options.publishKey) { data['publishKey'] = options.publishKey; } if (options.publishSecurity) { data['publishSecurity'] = options.publishSecurity; } if (options.type) { data['type'] = options.type; } if (options.notifyUrl) { data['notifyUrl'] = options.notifyUrl; } if (options.record != 'undefined') { data['record'] = options.record; } } API.request.post(credentials, '/streams', data, function(err, json) { if (err) { fn(err, null); } else { var stream = new Stream(credentials, json); fn(null, stream); } }); }; exports.getStream = function(credentials, streamId, fn) { var path = '/streams/' + streamId; API.request.get(credentials, path, null, function(err, data) { if (err) { fn(err, null); } else { var stream = new Stream(credentials, data); fn(null, stream); } }); }; exports.listStreams = function(credentials, hub, options, fn) { var data = { 'hub': hub }; if (options) { if (options.marker) { data['marker'] = options.marker; } if (options.limit) { data['limit'] = options.limit; } if (options.start) { data['start'] = options.start; } if (options.title) { data['title'] = options.title; } } API.request.get(credentials, '/streams', data, function(err, jsonData) { if (err) { fn(err, null, null); } else { var list = []; var total = jsonData['total']; var jsonList = jsonData['items']; if (jsonList) { jsonList.forEach(function(json) { var stream = new Stream(credentials, json); list.push(stream); }); } const jsonResult = { total: total, items: list }; fn(null, jsonResult); } }); }; exports.updateStream = function(credentials, streamId, options, fn) { var path = '/streams/' + streamId; var data = {}; if (options) { if (options.publishKey) { data['publishKey'] = options.publishKey; } if (options.publishSecurity) { data['publishSecurity'] = options.publishSecurity; } if (options.disabled != 'undefined') { data['disabled'] = options.disabled; } if (options.record != 'undefined') { data['record'] = options.record; } } API.request.post(credentials, path, data, function(err, data) { if (err) { fn(err, null); } else { var stream = new Stream(credentials, data); fn(null, stream); } }); }; exports.getStreamSegments = function(credentials, streamId, options, fn) { var path = '/streams/' + streamId + '/segments'; var data = {}; if (options) { if (options.startTime) { data['start'] = options.startTime; } if (options.endTime) { data['end'] = options.endTime; } if (options.limit) { data['limit'] = options.limit; } } API.request.get(credentials, path, data, function(err, jsonObject) { if (err) { fn(err, null); } else { fn(null, jsonObject['segments']); } }); }; exports.getRecords = function(credentials, streamId, fn) { var path = '/streams/' + streamId + '/record'; API.request.get(credentials, path, null, function(err, jsonObject) { if (err) { fn(err, null); } else { fn(null, jsonObject['records']); } }); }; exports.getPlayURL = function(credentials, path, fn) { API.request.get(credentials, path, null, function(err, responseData) { if (err) { fn(err, null); } else { fn(null, responseData); } }); }; exports.getVodURL = function(credentials, path, fn) { API.request.get(credentials, path, null, function(err, responseData) { if (err) { fn(err, null); } else { fn(null, responseData); } }); }; exports.getStreamsConnect = function(credentials, streamIds, fn) { var path = '/streams/connect'; API.request.post(credentials, path, streamIds, fn); }; exports.getStreamStatus = function(credentials, streamId, fn) { var path = '/streams/' + streamId + '/status'; API.request.get(credentials, path, null, fn); }; exports.deleteStream = function(credentials, streamId, fn) { var path = '/streams/' + streamId; API.request.delete(credentials, path, function(err, responseData) { if (err) { fn(err, null); } else { fn(null, responseData); } }); }; exports.deleteRecord = function(credentials, streamId, key, fn) { var path = '/streams/' + streamId + '/record'; var data = {}; if (key) { data['key'] = key; } API.request.delete(credentials, path, function(err, responseData) { if (err) { fn(err, null); } else { fn(null, responseData); } }, data); }; exports.deleteRecord = function(credentials, streamId, key, fn) { var path = '/streams/' + streamId + '/record'; var data = {}; if (key) { data['key'] = key; } API.request.delete(credentials, path, function(err, responseData) { if (err) { fn(err, null); } else { fn(null, responseData); } }, data); }; exports.deleteVodFile = function(credentials, key, streamId, fn) { var path = '/files/' + key; if (streamId) { path += '?streamid=' + streamId; } API.request.delete(credentials, path, function(err, responseData) { if (err) { fn(err, null); } else { fn(null, responseData); } }); }; exports.saveStreamAs = function(credentials, streamId, key, name, format, options, fn) { var path = '/streams/' + streamId + '/saveas'; var data = {}; data['key'] = key; data['name'] = name; data['format'] = format; if (options) { if (options.notifyUrl) { data['notifyUrl'] = options.notifyUrl; } if (options.filelist) { data['filelist'] = options.filelist; } } API.request.post(credentials, path, data, function(err, responseData) { if (err) { fn(err, null); } else { fn(null, responseData); } }); }; exports.snapshotStream = function(credentials, streamId, name, format, options, fn) { var path = '/streams/' + streamId + '/snapshot'; var data = {}; data['name'] = name; data['format'] = format; if (options) { if (options.time) { data['time'] = options.time; } if (options.notifyUrl) { data['notifyUrl'] = options.notifyUrl; } } API.request.post(credentials, path, data, function(err, responseData) { if (err) { fn(err, null); } else { fn(null, responseData); } }); }; exports.noftifyPullStream = function(credentials, streamId, options, fn) { var path = '/streams/' + streamId + '/pull'; var data = {}; if (options) { if (options.pullUrl) { data['pullUrl'] = options.pullUrl; } if (options.action) { data['action'] = options.action; } if (options.notifyUrl) { data['notifyUrl'] = options.notifyUrl; } } API.request.post(credentials, path, data, function(err, responseData) { if (err) { fn(err, null); } else { fn(null, responseData); } }); }; /** * 返回所有可用分发服务器 * @param credentials * @param fn */ exports.getDispatchers = function(credentials, fn) { var path = '/dispatcher/enable'; API.request.get(credentials, path, null, function(err, jsonObject) { if (err) { fn(err, null); } else { fn(null, jsonObject); } }); }; /** * 返回所有可用处理服务器 * @param credentials * @param fn */ exports.getReceivers = function(credentials, fn) { var path = '/receiver/enable'; API.request.get(credentials, path, null, function(err, jsonObject) { if (err) { fn(err, null); } else { fn(null, jsonObject); } }); }; /** * 获取流处理服务器 * @param credentials * @param streamId * @param fn */ exports.getReceiver = function(credentials, streamId, fn) { var path = '/receiver/' + streamId; API.request.get(credentials, path, null, function(err, jsonObject) { if (err) { fn(err, null); } else { fn(null, jsonObject); } }); }; /** * 获取存诸服务器 * @param credentials * @param streamId * @param fn */ exports.getStorage = function(credentials, token, fn) { var path = '/storage/upload' + '?token=' + token; API.request.get(credentials, path, null, function(err, jsonObject) { if (err) { fn(err, null); } else { fn(null, jsonObject); } }); }; /** * 获取点播播放地址 * @param credentials * @param streamId * @param fn */ exports.getVodPlayURL = function(credentials, path, fn) { path = path.replace('/v1', ''); API.request.get(credentials, path, null, function(err, jsonObject) { if (err) { fn(err, null); } else { fn(null, jsonObject); } }); }; /** * 获取转码参数 * @param credentials * @param streamId * @param fn */ exports.getAvthumbConfig = function(credentials, fn) { const path = '/avthumb/config'; API.request.get(credentials, path, null, function(err, jsonObject) { if (err) { fn(err, null); } else { fn(null, jsonObject); } }); };