<?php
namespace Live\Lib;

use Dever;

class Manage
{
    # 获取所有的流
	public function getStream_api()
    {
        $handle = new Handle();
    	$info = Dever::db('live/active')->state();
        foreach ($info as $k => $v) {
            $live = Dever::db('live/info')->one($v['live_id']);
            $data = $handle->method($live)->gets();
            if (isset($data['keys']) && $data['keys']) {
                foreach ($data['keys'] as $k1 => $v1) {
                    $this->insertStream($v1, $live['id'], $v['id']);
                }
            }
        }

        return 'yes';
    }

    private function insertStream($name, $live_id, $active_id)
    {
        $data['key'] = $name;
        $info = Dever::db('live/stream')->one($data);

        if (!$info) {
            $data['name'] = $name;
            $data['active_id'] = $active_id;
            $data['live_id'] = $live_id;
            $data['times'] = 1;
            $id = Dever::db('live/stream')->insert($data);
        } else {
            $id = $info['id'];
        }

        $handle = new Handle();
        $data = $handle->init($id, $name);
    }

    # 创建新流
    public function createStream($id, $name, $data)
    {
        $name = Dever::param('key', $data);

        $handle = new Handle();
        $data = $handle->init($id, $name);
    }

    # 禁播流
    public function updateDisabled($id, $name, $data)
    {
        $disabled = Dever::param('disabled', $data);

        $handle = new Handle();
        if ($disabled == 1) {
            $handle->start($id);
        } else {
            $handle->stop($id);
        }
    }

    # 获取流信息
    public function liveData($id)
    {
        $info = Dever::db('live/stream')->one($id);

        $html = $info['name'] . '('.$info['key'].')';
        return $html;
    }

    # 获取直播地址
    public function live($id)
    {
        $info = Dever::db('live/stream')->one($id);

        $table['推流地址'] = $info['live'];
        $table['高清地址'] = $info['url_rtmp'];
        $table['标清地址'] = $info['url_hls'];
        $table['普清地址'] = $info['url_hdl'];
        $table['截图地址'] = $info['url_pic'];
        $table['直播时长'] = $info['times'] . '小时';
        return Dever::table($table);
    }

    # 获取直播信息
    public function liveInfo($id)
    {
        $info = Dever::db('live/stream')->one($id);

        $table = array();
        if ($info['info']) {

            if ($info['status'] == 1) {
                $table['直播状态'] = '未开始';
            } elseif ($info['status'] == 2) {
                $table['直播状态'] = '直播中';
            } elseif ($info['status'] == 3) {
                $table['直播状态'] = '已结束';
            }
            $data = Dever::json_decode($info['info']);

            if (isset($data['live']) && $data['live']) {
                $table['开始时间'] = date('Y-m-d H:i:s', $data['live']['startAt']);
                $table['主播IP'] = $data['live']['clientIP'];
                $table['当前码率'] = $data['live']['bps'];
                $table['音频帧率'] = $data['live']['fps']['audio'];
                $table['视频帧率'] = $data['live']['fps']['video'];
                $table['数据帧率'] = $data['live']['fps']['data'];

            }
            /*
            if (isset($data['history']) && $data['history'] && isset($data['history']['items']) && $data['history']['items']) {
                $history = array();
                foreach ($data['history']['items'] as $k => $v) {
                    $history['第' . ($k+1) . '次直播'] = date('Y-m-d H:i:s', $v['start']) . '~' . date('Y-m-d H:i:s', $v['end']);
                }
                $table['直播历史'] = Dever::table($history);
            }
            */
        }
        
        return Dever::table($table);
    }
}
