<?php
namespace Live\Lib;

use Dever;

Dever::apply('sdk/qiniu', 'live');

class Qiniu
{
    private $stream;
    private $config;
    private $space;

    # 连接
    public function __construct($config)
    {
        $this->config = $config;
        $mac = new \Qiniu\Pili\Mac($this->config['appkey'], $this->config['appsecret']);
        $client = new \Qiniu\Pili\Client($mac);
        $this->space = $client->hub($this->config['space']);
    }

    # 创建新流
    public function create($name)
    {
        $result = $this->space->create($name);

        return $this->getInfo($name);
    }

    # 查询流
    public function get($name)
    {
        if (!$this->stream) {
            $this->stream = $this->space->stream($name);
        }

        return $this->stream;
    }

    # 查询流的信息
    public function getInfo($name)
    {
        $stream = $this->get($name);

        return $stream->info();
    }

    # 获取直播状态
    public function getLiveStatus($name)
    {
        $stream = $this->get($name);

        return $stream->liveStatus();
    }

    # 列出所有流
    public function gets($prefix = '', $limit = 10000)
    {
        return $this->space->listStreams($prefix, $limit, '');
    }

    # 列出所有正在直播的流
    public function getLives($prefix = '', $limit = 10000)
    {
        return $this->space->listLiveStreams($prefix, $limit, '');
    }

    # 启动流
    public function start($name = '')
    {
        $stream = $this->get($name);
        $stream->enable();
        return $this->getLiveStatus($name);
    }

    # 禁用流
    public function stop($name = '', $num = 120)
    {
        $stream = $this->get($name);
        $stream->disable(time() + $num);
        return $this->getLiveStatus($name);
    }

    # 保存直播数据
    public function save($name, $start = 0, $end = 0, $format = false)
    {
        $stream = $this->get($name);
        if ($end <= 0) {
            $end = time();
        }
        if ($format) {
            $option = array('format' => $format, 'start' => $start, 'end' => $end);
            $data = $stream->saveas($option);
        } else {
            $data = $stream->save($start, $end);
        }
        
        if (isset($data['fname'])) {
            return $data['fname'];
        }
        return '';
    }

    # 保存直播截图
    public function savePic($name)
    {
        $stream = $this->get($name);
        return $stream->snapshot(array("format" => "jpg"));
    }

    # 查看历史直播流
    public function history($name)
    {
        $stream = $this->get($name);
        return $stream->historyActivity(0, 0);
    }

    # 查询推流历史
    public function update($name, $convert)
    {
        $stream = $this->get($name);
        $stream->updateConverts($convert);
        return $stream->info();
    }

    # 获取推流地址
    public function getRtmp($name, $time = 3600)
    {
        $url = \Qiniu\Pili\RTMPPublishURL($this->config['domain_publish'], $this->config['space'], $name, $time, $this->config['appkey'], $this->config['appsecret']);
        return $url;
    }

    # 获取直播地址
    public function getPlay($name)
    {
        //高清、标清、普清
        $url['rtmp'] = $this->getRtmpPlay($name);
        $url['hls'] = $this->getHlsPlay($name);
        $url['hdl'] = $this->getHdlPlay($name);
        $url['pic'] = $this->getPicPlay($name);
        return $url;
    }

    # 获取直播地址
    public function getRtmpPlay($name)
    {
        $url = \Qiniu\Pili\RTMPPlayURL($this->config['domain_live_rtmp'], $this->config['space'], $name);
        return $url;
    }

    # HLS 直播地址
    public function getHlsPlay($name)
    {
        $url = \Qiniu\Pili\HLSPlayURL($this->config['domain_live_hls'], $this->config['space'], $name);
        return $url;
    }

    # HDL 直播地址
    public function getHdlPlay($name)
    {
        $url = \Qiniu\Pili\HDLPlayURL($this->config['domain_live_hdl'], $this->config['space'], $name);
        return $url;
    }

    # 截图直播地址
    public function getPicPlay($name)
    {
        $url = \Qiniu\Pili\SnapshotPlayURL($this->config['domain_live_pic'], $this->config['space'], $name);
        return $url;
    }
}