123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- namespace Live\Lib;
- use Dever;
- class Handle
- {
- public function method($config)
- {
- if ($config['type'] == 1) {
- $method = new Qiniu($config);
- }
- return $method;
- }
- # 启动
- public function start($id)
- {
- $info = Dever::db('live/stream')->one($id);
- $config = Dever::db('live/info')->one($info['live_id']);
- $method = $this->method($config);
- $stream = $method->start($info['key']);
- }
- # 禁播
- public function stop($id)
- {
- $info = Dever::db('live/stream')->one($id);
- $config = Dever::db('live/info')->one($info['live_id']);
- $method = $this->method($config);
- $stream = $method->stop($info['key']);
- }
- # 保存
- public function save($id, $log_id)
- {
- $info = Dever::db('live/stream')->one($id);
- $config = Dever::db('live/info')->one($info['live_id']);
- $method = $this->method($config);
- $info_update['where_id'] = $info['id'];
- $info_update['status'] = 3;
- Dever::db('live/stream')->update($info_update);
- Dever::db('live/stream_log')->update(array('edate' => time(), 'where_id' => $log_id));
- return true;
- }
- # 建立直播日志
- public function saveLog($id, $log_id = false, $table = false)
- {
- $info = Dever::db('live/stream')->one($id);
- if (!$log_id) {
- $time = time();
- $data['sdate'] = $time;
- $data['stream_id'] = $id;
- $data['live_id'] = $info['live_id'];
- $data['active_id'] = $info['active_id'];
- $id = Dever::db('live/stream_log')->insert($data);
- } else {
- $config = Dever::db('live/info')->one($info['live_id']);
- $log = Dever::db('live/stream_log')->one($log_id);
- $method = $this->method($config);
- $log['sdate'] = intval($log['sdate']);
- $log['edate'] = intval($log['edate']);
- $data['url_m3u8'] = $config['host'] . $method->save($info['key'],$log['sdate'], $log['edate']);
- $data['url_mp4'] = $config['host'] . $method->save($info['key'], $log['sdate'], $log['edate'], 'mp4');
- $data['where_id'] = $log_id;
- Dever::db('live/stream_log')->update($data);
- $id = $log_id;
- if ($table) {
- $data = Dever::db($table)->state(array('stream_id' => $info['id']));
- if ($data) {
- foreach ($data as $k => $v) {
- if (!$v['url_m3u8'] && $v['sdate'] > 0 && $v['edate'] > 0) {
- $v['sdate'] = intval($v['sdate']);
- $v['edate'] = intval($v['edate']);
- $url_m3u8 = $config['host'] . $method->save($info['key'],$v['sdate'], $v['edate']);
- $url_mp4 = $config['host'] . $method->save($info['key'], $v['sdate'], $v['edate'], 'mp4');
- Dever::db($table)->update(array('where_id' => $v['id'], 'url_m3u8' => $url_m3u8, 'url_mp4' => $url_mp4));
- }
- }
- }
- }
- }
-
- return $id;
- }
- # 开始直播
- public function startLive($id)
- {
- $info = Dever::db('live/stream')->one($id);
- $config = Dever::db('live/info')->one($info['live_id']);
- $method = $this->method($config);
- $data['where_id'] = $info['id'];
- $data['status'] = 2;
- Dever::db('live/stream')->update($data);
- }
- # 初始化流
- public function init($id, $name = '')
- {
- $info = Dever::db('live/stream')->one($id);
- if ($info['url_rtmp']) {
- return;
- }
- if ($name) {
- $info['key'] = $name;
- }
- return $this->get($info, true);
- }
- # 获取流信息 定时跑
- public function get($info, $create = false)
- {
- if (!is_array($info)) {
- $info = Dever::db('live/stream')->one($info);
- }
- $config = Dever::db('live/info')->one($info['live_id']);
-
- $method = $this->method($config);
- if ($create) {
- $stream = $method->create($info['key']);
- } else {
- $stream = $method->getInfo($info['key']);
- }
- $rtmp = $method->getRtmp($info['key'], $info['times'] * 3600);
- $url = $method->getPlay($info['key']);
- $status = array();
- $history = array();
- try {
- $status = $method->getLiveStatus($info['key']);
- } catch (\Exception $e) {
- $status = array();
- }
- try {
- $history = $method->history($info['key']);
- } catch (\Exception $e) {
- $history = array();
- }
- $data['where_id'] = $info['id'];
- $data['live'] = $rtmp;
- $data['url_rtmp'] = $url['rtmp'];
- $data['url_hls'] = $url['hls'];
- $data['url_hdl'] = $url['hdl'];
- $data['url_pic'] = $url['pic'];
- $live = Dever::json_decode($info['info']);
- $live['info'] = $stream;
- $live['live'] = $status;
- $live['hitory'] = $history;
- if ($status) {
- $data['status'] = 2;
- } else {
- $data['status'] = 1;
- }
- $data['info'] = Dever::json_encode($live);
- Dever::db('live/stream')->update($data);
- return $data;
- }
- }
|