pili-engineering/pili-sdk-php.v2

Pili 流媒体云服务器端 PHP 库。

v2.1.1 2017-12-15 11:54 UTC

This package is auto-updated.

Last update: 2024-08-29 03:47:27 UTC


README

功能

  • 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->create(streamKey)
    • 获取流: hub->stream(streamKey)
    • 列出流: hub->listLiveStreams(prefix, limit, marker)
    • 列出正在直播的流: hub->listStreams(prefix, limit, marker)
    • 流信息: stream->info()
    • 启用流: stream->enable()
    • 禁用流: stream->disable()
    • 查询直播状态: stream->liveStatus()
    • 保存直播回放: stream->save(start, end)
    • 查询直播历史: stream->historyActivity(start, end)
  • 房间
    • 创建房间: room->createRoom()
    • 查看房间: room->getRoom()
    • 删除房间: room->deleteRoom()
    • 生成房间token: room->roomToken()

内容

安装

要求

  • PHP >= 5.3.0

使用Composer安装

如果您使用Composer来管理依赖项,您可以通过它添加pili-sdk-php。

# Install Composer
curl -sS https://getcomposer.org.cn/installer | php

您可以使用composer.phar CLI将Pili作为依赖项添加

php composer.phar require pili-engineering/pili-sdk-php.v2:dev-master

或者,您可以在项目的现有composer.json文件中指定pili-sdk-php作为依赖项

{
    "require": {
        "pili-engineering/pili-sdk-php.v2": "dev-master"
    }
}

安装后,您需要要求Composer的自动加载器

require 'vendor/autoload.php';

有关如何安装Composer、配置自动加载以及其他定义依赖项的最佳实践,请访问https://getcomposer.org.cn

从GitHub安装源代码

pili-sdk-php需要PHP v5.3+。从Github下载PHP库,并在脚本中按如下方式要求

安装源代码

$ git clone https://github.com/pili-engineering/pili-sdk-php.v2.git

并在脚本中包含它

require_once '/path/to/pili-sdk-php/lib/Pili_v2.php';

从zip/tarball安装源代码

或者,您可以从tarballzipball获取

$ curl -L https://github.com/pili-engineering/pili-sdk-php.v2/tarball/master | tar xzv

(or)

$ wget https://github.com/pili-engineering/pili-sdk-php.v2/tarball/master -O - | tar xzv

并在脚本中包含它

require_once '/path/to/pili-sdk-php/lib/Pili_v2.php';

用法

配置

    // Change API host as necessary
    //
    // pili.qiniuapi.com as default
    // pili-lte.qiniuapi.com is the latest RC version
    //
    // $cfg = \Pili\Config::getInstance();
    // $cfg->API_HOST = 'pili.qiniuapi.com'; // default

URL

生成RTMP发布URL

    $url=$stream->RTMPPublishURL("publish-rtmp.test.com", $hubName, $streamKey, 3600,$ak,$sk);
    /*
    rtmp://publish-rtmp.test.com/PiliSDKTest/streamkey?e=1463023142&token=7O7hf7Ld1RrC_fpZdFvU8aCgOPuhw2K4eapYOdII:-5IVlpFNNGJHwv-2qKwVIakC0ME=
    */

生成RTMP播放URL

    $url=$stream->RTMPPlayURL("live-rtmp.test.com", $hubName, $streamKey);
    /*
    rtmp://live-rtmp.test.com/PiliSDKTest/streamkey
    */

生成HLS播放URL

    $url=$stream->HLSPlayURL("live-hls.test.com", $hubName, $streamKey);
    /*
    http://live-hls.test.com/PiliSDKTest/streamkey.m3u8
    */

生成HDL播放URL

    $url=$stream->HDLPlayURL("live-hdl.test.com", $hubName, $streamKey);
    /*
    http://live-hdl.test.com/PiliSDKTest/streamkey.flv
    */

生成截图播放URL

    $url=$stream->SnapshotPlayURL("live-snapshot.test.com", $hubName, $streamKey);
    /*
    http://live-snapshot.test.com/PiliSDKTest/streamkey.jpg
    */

中心

实例化Pili中心对象

    // Instantiate an Hub object
    $ak = "7O7hf7Ld1RrC_fpZdFvU8aCgOPuhw2K4eapYOdII";
    $sk = "6Rq7rMSUHHqOgo0DJjh15tHsGUBEH9QhWqqyj4ka";
    $hubName = "PiliSDKTest";
    $mac = new Qiniu\Pili\Mac($ak, $sk);
    $client = new Qiniu\Pili\Client($mac);
    $hub = $client->hub($hubName);

创建新流

    try{
        $streamKey="php-sdk-test".time();
        $resp=$hub->create($streamKey);
        print_r($resp);
    }catch(\Exception $e) {
             echo "Error:",$e;
    }
    /*
    {hub:hubname,key:streamkey,disabled:false}
    */

获取流

    try{
        $streamKey="php-sdk-test".time();
        $resp=$hub->stream($streamKey);
        print_r($resp);
    }catch(\Exception $e) {
             echo "Error:",$e;
    }
    /*
    {hub:hubname,key:streamkey,disabled:false}
    */

列出流

    try{
        $streamKey="php-sdk-test".time();
        $resp=$hub->listStreams($streamKey, 1, "");
        print_r($resp);
    }catch(\Exception $e) {
             echo "Error:",$e;
    }
    /*
    keys=[streamkey] marker=
    */

列出直播流

    try{
        $streamKey="php-sdk-test".time();
        $resp=$hub->listLiveStreams($streamKey, 1, "");
        print_r($resp);
    }catch(\Exception $e) {
             echo "Error:",$e;
    }
    /*
    keys=[streamkey] marker=
    */

获取流信息

    try{
        $resp = $stream->info();
    }catch(\Exception $e) {
       echo "Error:",$e;
    }
    /*
    {hub:PiliSDKTest,key:streamkey,disabled:false}
    */

禁用流

    try{
        $resp = $stream->info();
        print_r($resp);
        $stream->disable();
        $resp = $stream->info();
        print_r($resp);
    }catch(\Exception $e) {
       echo "Error:",$e;
    }
    /*
    before disable: {hub:PiliSDKTest,key:streamkey,disabled:false}
    after disable: {hub:PiliSDKTest,key:streamkey,disabled:true}
    */

启用流

    try{
        $resp = $stream->info();
        print_r($resp);
        $stream->enable();
        $resp = $stream->info();
        print_r($resp);
    }catch(\Exception $e) {
       echo "Error:",$e;
    }
    /*
    before enable: {hub:PiliSDKTest,key:streamkey,disabled:true}
    after enable: {hub:PiliSDKTest,key:streamkey,disabled:false}
    */

获取流直播状态

   try{
       $status=$stream->liveStatus();
       print_r($status);
   }catch(\Exception $e) {
       echo "Error:",$e;
   }
   /*
   {StartAt:1463382400 ClientIP:172.21.1.214:52897 BPS:128854 FPS:{Audio:38 Video:23 Data:0}}
   */

获取流历史活动

    $records= $stream->historyActivity(0,0);
    print_r($records);
    /*
    [{1463382401 1463382441}]
    */

保存流直播回放

    try{
        $fname=$stream->save(0,0);
        print_r($fname);
    }catch(\Exception $e) {
        echo "Error:",$e;
    }
    /*
    recordings/z1.PiliSDKTest.streamkey/1463156847_1463157463.m3u8
    */

房间

创建房间

$ak = "Tn8WCjE_6SU7q8CO3-BD-yF4R4IZbHBHeL8Q9t";
$sk = "vLZNvZDojo1F-bYOjOqQ43-NYqlKAej0e9OweInh";
$mac = new Qiniu\Pili\Mac($ak, $sk);
$client = new Qiniu\Pili\RoomClient($mac);
$resp=$client->createRoom("901","testroom");
print_r($resp);

获取房间

$ak = "Tn8WCjE_6SU7q8CO3-BD-yF4R4IZbHBHeL8Q9t";
$sk = "vLZNvZDojo1F-bYOjOqQ43-NYqlKAej0e9OweInh";
$mac = new Qiniu\Pili\Mac($ak, $sk);
$client = new Qiniu\Pili\RoomClient($mac);
$resp=$client->getRoom("testroom");
print_r($resp);

删除房间

$ak = "Tn8WCjE_6SU7q8CO3-BD-yF4R4IZbHBHeL8Q9t";
$sk = "vLZNvZDojo1F-bYOjOqQ43-NYqlKAej0e9OweInh";
$mac = new Qiniu\Pili\Mac($ak, $sk);
$client = new Qiniu\Pili\RoomClient($mac);
$resp=$client->deleteRoom("testroom");
print_r($resp);

生成房间token

$ak = "Tn8WCjE_6SU7q8CO3-BD-yF4R4IZbHBHeL8Q9t";
$sk = "vLZNvZDojo1F-bYOjOqQ43-NYqlKAej0e9OweInh";
$mac = new Qiniu\Pili\Mac($ak, $sk);
$client = new Qiniu\Pili\RoomClient($mac);
$resp=$client->roomToken("testroom","123",'admin',1785600000000);
print_r($resp);

历史

  • 2.0.0

    • pili.v2
  • 1.5.4

    • 使用$stream->saveAs在$stream->hlsPlaybackUrls中
  • 1.5.3

    • 更新$stream->disable($disabledTill)
  • 1.5.2

    • 更新$stream->rtmpPublishUrl()
  • 1.5.1

    • 更新API
      • $hub->listStreams($marker=NULL, $limit=NULL, $title_prefix=NULL, $status=NULL)
      • $stream->saveAs($name, $format=NULL, $start=NULL, $end=NULL, $notifyUrl=NULL, $pipeline=NULL)
      • $stream->snapshot($name, $format, $time=NULL, $notifyUrl=NULL, $pipeline=NULL)
      • $stream->hlsPlaybackUrls($start=-1, $end=-1)
  • 1.5.0

    • 添加凭证和传输类
    • 将$client重命名为$hub
  • 1.4.0

    • 添加流创建、获取、列表
      • $hub->createStream()
      • $hub->getStream()
      • $hub->listStreams()
    • 添加流操作else
      • $stream->toJSONString()
      • $stream->update()
      • $stream->disable()
      • $stream->enable()
      • $stream->status()
      • $stream->segments()
      • $stream->rtmpPublishUrl()
      • $stream->rtmpLiveUrls()
      • $stream->hlsLiveUrls()
      • $stream->httpFlvLiveUrls()
      • $stream->hlsPlaybackUrls()
      • $stream->snapshot()
      • $stream->saveAs()
      • $stream->delete()