Skip to content

Pili Streaming Cloud Server-Side Library For Go, Version 2

License

Notifications You must be signed in to change notification settings

sjzar/pili-sdk-go.v2

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pili Streaming Cloud Server-Side Library for Go

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.BatchLiveStatus(streams)
  • Stream

    • 流信息: stream.Info()
    • 禁用流: stream.DisableTill(till)
    • 启用流: stream.Enable()
    • 查询直播状态: stream.LiveStatus()
    • 保存直播回放: stream.Saveas(options)
    • 保存直播截图: stream.Snapshot(options)
    • 更改流的实时转码规格: stream.UpdateConverts(profiles)
    • 查询直播历史: stream.HistoryActivity(start, end)

Contents

Installation

before next step, install git.

// install latest version
$ go get github.com/pili-engineering/pili-sdk-go.v2/pili

Usage

Configuration

package main

import (
	// ...
	"github.com/pili-engineering/pili-sdk-go.v2/pili"
)

var (
	AccessKey = "<QINIU ACCESS KEY>" // 替换成自己 Qiniu 账号的 AccessKey.
	SecretKey = "<QINIU SECRET KEY>" // 替换成自己 Qiniu 账号的 SecretKey.
	HubName   = "<PILI HUB NAME>"    // Hub 必须事先存在.
)

func main() {
	// ...
	mac := &pili.MAC{AccessKey, []byte(SecretKey)}
	client := pili.New(mac, nil)
	// ...
}

URL

Generate RTMP publish URL

url := pili.RTMPPublishURL("publish-rtmp.test.com", "PiliSDKTest", "streamkey", mac, 60)
fmt.Println(url)
/*
rtmp://publish-rtmp.test.com/PiliSDKTest/streamkey?e=1463023142&token=7O7hf7Ld1RrC_fpZdFvU8aCgOPuhw2K4eapYOdII:-5IVlpFNNGJHwv-2qKwVIakC0ME=
*/

Generate RTMP play URL

url := pili.RTMPPlayURL("live-rtmp.test.com", "PiliSDKTest", "streamkey")
fmt.Println(url)
/*
rtmp://live-rtmp.test.com/PiliSDKTest/streamkey
*/

Generate HLS play URL

url := pili.HLSPlayURL("live-hls.test.com", "PiliSDKTest", "streamkey")
fmt.Println(url)
/*
http://live-hls.test.com/PiliSDKTest/streamkey.m3u8
*/

Generate HDL play URL

url := pili.HDLPlayURL("live-hdl.test.com", "PiliSDKTest", "streamkey")
fmt.Println(url)
/*
http://live-hdl.test.com/PiliSDKTest/streamkey.flv
*/

Generate Snapshot play URL

url := pili.SnapshotPlayURL("live-snapshot.test.com", "PiliSDKTest", "streamkey")
fmt.Println(url)
/*
http://live-snapshot.test.com/PiliSDKTest/streamkey.jpg
*/

Hub

Instantiate a Pili Hub object

func main() {
	mac := &pili.MAC{AccessKey, []byte(SecretKey)}
	client := pili.New(mac, nil)
	hub := client.Hub("PiliSDKTest")
	// ...
}

Create a new Stream

stream, err := hub.Create(key)
if err != nil {
	return
}
info, err := stream.Info()
if err != nil {
	return
}
fmt.Println(info)
/*
{hub:PiliSDKTest,key:streamkey,disabled:false}
*/

Get a Stream

stream := hub.Stream(key)
info, err := stream.Info()
if err != nil {
	return
}
fmt.Println(info)
/*
{hub:PiliSDKTest,key:streamkey,disabled:false}
*/

List Streams

keys, marker, err := hub.List(prefix, 10, "")
if err != nil {
	return
}
fmt.Printf("keys=%v marker=%v\n", keys, marker)
/*
keys=[streamkey] marker=
*/

List live Streams

keys, marker, err := hub.ListLive(prefix, 10, "")
if err != nil {
	return
}
fmt.Printf("keys=%v marker=%v\n", keys, marker)
/*
keys=[streamkey] marker=
*/

Batch query live status

items, err := hub.BatchLiveStatus(streams)
if err != nil {
	return
}
fmt.Println(items)
/*
[{streamKey1 {1487766696 172.21.2.14:63422 1042240 {46 24 0}}} {streamKey2 {1487768638 172.21.2.14:63793 1201352 {51 22 0}}}]
*/

Stream

Get Stream info

stream := hub.Stream(key)
info, err := stream.Info()
if err != nil {
	return
}
fmt.Println(info)
/*
{hub:PiliSDKTest,key:streamkey,disabled:false}
*/

Disable a Stream

stream := hub.Stream(key)
info, err := stream.Info()
if err != nil {
	return
}
fmt.Println("before disable:", info)

err = stream.DisableTill(time.Now().Add(time.Minute).Unix())
if err != nil {
	return
}

info, err = stream.Info()
if err != nil {
	return
}
fmt.Println("after call disable:", info)

time.Sleep(time.Minute)

info, err = stream.Info()
if err != nil {
    return
}
fmt.Println("after time.Minute:", info)

/*
before disable: {hub:PiliSDKTest,key:sdkexample1487765261435788231A,disabled:false}
after call disable: {hub:PiliSDKTest,key:sdkexample1487765261435788231A,disabled:true}
after time.Minute: {hub:PiliSDKTest,key:sdkexample1487765261435788231A,disabled:false}
*/

Enable a Stream

stream := hub.Stream(key)
info, err := stream.Info()
if err != nil {
	return
}
fmt.Println("before enable:", info)

err = stream.Enable()
if err != nil {
	return
}

info, err = stream.Info()
if err != nil {
	return
}
fmt.Println("after enable:", info)
/*
before enable: {hub:PiliSDKTest,key:streamkey,disabled:true}
after enable: {hub:PiliSDKTest,key:streamkey,disabled:false}
*/

Get Stream live status

stream := hub.Stream(key)
status, err := stream.LiveStatus()
if err != nil {
	return
}
fmt.Printf("%+v\n", status)
/*
&{StartAt:1463382400 ClientIP:172.21.1.214:52897 BPS:128854 FPS:{Audio:38 Video:23 Data:0}}
*/

Get Stream history activity

stream := hub.Stream(key)
records, err := stream.HistoryActivity(0, 0)
if err != nil {
	return
}
fmt.Println(records)
/*
[{1463382401 1463382441}]
*/

Save Stream live playback

stream := hub.Stream(key)
opts := &pili.SaveasOptions{
	Format: "mp4",
}
fname, persistentID, err := stream.Saveas(opts)
if err != nil {
	return
}
fmt.Println(fname, persistentID)
/*
recordings/z1.PiliSDKTest.streamkey/1463156847_1463157463.mp4 z1.58ae57dc254f0e4d3f00000e
*/

Update Stream converts

stream := hub.Stream(key)
info, err := stream.Info()
if err != nil {
	return
}
fmt.Println("before UpdateConverts:", info)
err = stream.UpdateConverts([]string{"480p", "720p"})
if err != nil {
	return
}
info, err = stream.Info()
if err != nil {
	return
}
fmt.Println("after UpdateConverts:", info)
/*
before UpdateConverts: {hub:PiliSDKTest,key:sdkexample1487834862156173949A,disabled:false,converts:[]}
after UpdateConverts: {hub:PiliSDKTest,key:sdkexample1487834862156173949A,disabled:false,converts:[480p 720p]}
*/

Save Stream snapshot

stream := hub.Stream(key)
opts := &pili.SnapshotOptions{
	Format: "jpg",
}
fname, err := stream.Snapshot(opts)
if err != nil {
	return
}
fmt.Println(fname)
/*
streamkey-2741961933532577110.jpg
*/

About

Pili Streaming Cloud Server-Side Library For Go, Version 2

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 99.5%
  • Makefile 0.5%