Skip to content

pili-engineering/pili-sdk-nodejs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Table of Contents generated with DocToc

Pili Streaming Cloud server-side library for NodeJS

Features

  • URL
    • RTMP推流地址: RTMPPublishURL(domain, hub, streamKey, mac, expireAfterSeconds)
    • RTMP直播地址: RTMPPlayURL(domain, hub, streamKey)
    • HLS直播地址: HLSPlayURL(domain, hub, streamKey)
    • HDL直播地址: HDLPlayURL(domain, hub, streamKey)
    • 截图直播地址: SnapshotPlayURL(domain, hub, streamKey)
  • Hub
    • 创建流: hub.Create(streamKey)
    • 获得流: hub.Stream(streamKey)
    • 列出流: hub.List(prefix, limit, marker)
    • 列出正在直播的流: hub.ListLive(prefix, limit, marker)
    • 批量查询直播实时信息: hub.batchLiveStreamsInfo(items)
  • Stream
    • 流信息: stream.Info()
    • 禁用流: stream.Disable()
    • 启用流: stream.Enable()
    • 查询直播状态: stream.LiveStatus()
    • 保存直播回放: stream.Save(start, end)
    • 查询直播历史: stream.HistoryActivity(start, end)
    • 保存直播截图: stream.saveSnapshot()
    • 修改流转码配置: stream.updateConverts(profiles)

Installaion

// install latest version
npm install piliv2 --save

Usage

Configuration

var Pili = require('piliv2');

var ACCESS_KEY  = 'QiniuAccessKey';
var SECRET_KEY  = 'QiniuSecretKey';

var HUB = 'PiliHubName'; // The Hub must be exists before use

var credentials = new Pili.Credentials(ACCESS_KEY, SECRET_KEY);

URL

Generate RTMP publish URL

var url = Pili.publishURL(credentials,'publish-rtmp.test.com', 'PiliSDKTest', 'streamkey', 60);
console.log(url);
// rtmp://publish-rtmp.test.com/PiliSDKTest/streamkey?e=1466652726&token=9roGzeeJkZh4y5gHpzT7Uv1CIw0KiVV8K4dfXRY0:bYZGIdK-tjjAfyIwbShQ5Bb1kBY=

Generate RTMP play URL

var url = Pili.rtmpPlayURL('live-rtmp.test.com', 'PiliSDKTest', 'streamkey');
console.log(url);
// rtmp://live-rtmp.test.com/PiliSDKTest/streamkey

Generate HLS play URL

var url = Pili.hlsPlayURL('live-rtmp.test.com', 'PiliSDKTest', 'streamkey');
console.log(url);
// http://live-rtmp.test.com/PiliSDKTest/streamkey.m3u8

Generate HDL play URL

var url = Pili.hdlPlayURL('live-rtmp.test.com', 'PiliSDKTest', 'streamkey');
console.log(url);
// http://live-rtmp.test.com/PiliSDKTest/streamkey.flv

Generate Snapshot play URL

var url = Pili.snapshotPlayURL('live-rtmp.test.com', 'PiliSDKTest', 'streamkey');
console.log(url);
// http://live-rtmp.test.com/PiliSDKTest/streamkey.jpg

Hub

Instantiate a Pili Hub object

var hub = new Pili.Hub(credentials, HUB);

Create a new Stream

hub.createStream(streamKey, function(err, stream) {

  if (!err) {
      console.log(stream);
  } else {
    // Log error
    console.log(err, err.errorCode, err.httpCode);
  }
});

Get a Stream

var stream = hub.newStream(streamKey);

stream.loadInfo(function(err) {
	if (!err) {
		console.log(stream);
	} else {
		console.log(err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
	}
})

LiveInfo

stream.liveInfo(function(err, status) {
    if (!err) {
			console.log(status);
		} else {
			console.log(err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
		}
});

List Stream

var listOptions = {
	'liveonly': false,
	'prefix': '',
	'limit': 2,
};

var listCallBack = function(err, marker, streams) {
	if (!err) {
		streams.forEach(function(stream) {
			console.log(stream);
		});
		if (marker) {
			listOptions.marker = marker;
			hub.listStreams(listOptions, listCallBack);
		}
	} else {
		 console.log(err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
	}
}

hub.listStreams(listOptions, listCallBack);

Get Streams live status

hub.batchLiveStreamsInfo([streamKey], function (err, items) {
    if (!err) {
        console.log('live streams: ');
        items.forEach(function(item) {
            console.log(item);
        });
    }
    else {
        console.log(err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
    }
});

Stream

Disable a Stream

stream.disable(-1, function(err) {
    console.log(stream.disabledTill);
});

Enable a Stream

stream.enable(function(err) {
    console.log(stream.disabledTill);
});

Get Stream live status

stream.liveInfo(function(err, status) {
    if (!err) {
			console.log(status);
		} else {
			console.log(err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
		}
});

Get Stream history activity

var publishHistoryOptions = {
   start : null,    // optional, in second, unix timestamp
   end   : null,    // optional, in second, unix timestamp
};
stream.publishHistory(publishHistoryOptions, function(err, history) {
	if (!err) {
		console.log(history);
	} else {
			console.log(err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
	}
})

Save Stream live playback

var savePlaybackOptions = {
   start : null,    // optional, in second, unix timestamp
   end   : null,    // optional, in second, unix timestamp
   format: null,    // optional, file format
};

stream.savePlayback(savePlaybackOptions, function(err, m3u8Name) {

    if (!err) {
			console.log(m3u8Name);
		} else {
			console.log(err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
		}
});

Save Stream snapshot

var saveSnapshotOptions = {
    fname: streamKey    // optional, file name
};

stream.saveSnapshot(saveSnapshotOptions, function(err, snapshotName) {
    if (!err) {
        console.log(snapshotName);
    } else {
        console.log(err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
    }
});

Update Stream converts

stream.updateConverts(['480p', '720p'], function (err) {
    if (!err) {
        console.log('update converts success');
    }
    else {
        console.log('update converts error: ' + err + 'error code: ' + err.errorCode + 'http code: ' + err.httpCode);
    }
})