Handle.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. namespace Live\Lib;
  3. use Dever;
  4. class Handle
  5. {
  6. public function method($config)
  7. {
  8. if ($config['type'] == 1) {
  9. $method = new Qiniu($config);
  10. }
  11. return $method;
  12. }
  13. # 启动
  14. public function start($id)
  15. {
  16. $info = Dever::db('live/stream')->one($id);
  17. $config = Dever::db('live/info')->one($info['live_id']);
  18. $method = $this->method($config);
  19. $stream = $method->start($info['key']);
  20. }
  21. # 禁播
  22. public function stop($id)
  23. {
  24. $info = Dever::db('live/stream')->one($id);
  25. $config = Dever::db('live/info')->one($info['live_id']);
  26. $method = $this->method($config);
  27. $stream = $method->stop($info['key']);
  28. }
  29. # 保存
  30. public function save($id, $log_id)
  31. {
  32. $info = Dever::db('live/stream')->one($id);
  33. $config = Dever::db('live/info')->one($info['live_id']);
  34. $method = $this->method($config);
  35. $info_update['where_id'] = $info['id'];
  36. $info_update['status'] = 3;
  37. Dever::db('live/stream')->update($info_update);
  38. Dever::db('live/stream_ios_log')->update(array('edate' => time(), 'where_id' => $log_id));
  39. return true;
  40. }
  41. # 建立ios直播日志
  42. public function saveIosLog($id, $log_id = false, $video = false)
  43. {
  44. $info = Dever::db('live/stream')->one($id);
  45. if (!$log_id) {
  46. $time = time();
  47. $data['sdate'] = $time;
  48. $data['stream_id'] = $id;
  49. $data['live_id'] = $info['live_id'];
  50. $data['active_id'] = $info['active_id'];
  51. $id = Dever::db('live/stream_ios_log')->insert($data);
  52. } else {
  53. $config = Dever::db('live/info')->one($info['live_id']);
  54. $log = Dever::db('live/stream_ios_log')->one($log_id);
  55. $method = $this->method($config);
  56. $log['sdate'] = intval($log['sdate']);
  57. $log['edate'] = intval($log['edate']);
  58. try {
  59. $data['url_m3u8'] = $config['host'] . $method->save($info['key'],$log['sdate'], $log['edate']);
  60. //$data['url_mp4'] = $config['host'] . $method->save($info['key'], $log['sdate'], $log['edate'], 'mp4');
  61. $data['url_mp4'] = '';
  62. } catch (\Exception $e) {
  63. $data['url_m3u8'] = $data['url_mp4'] = '';
  64. }
  65. if (!$data['url_m3u8']) {
  66. return;
  67. }
  68. $data['where_id'] = $log_id;
  69. Dever::db('live/stream_ios_log')->update($data);
  70. $id = $log_id;
  71. if ($video) {
  72. $this->createUrl($video, $method, $config, $info);
  73. }
  74. }
  75. return $id;
  76. }
  77. # 生成回放地址
  78. public function createUrl($video, $method, $config, $info)
  79. {
  80. $where[$video['stream_id']] = $info['id'];
  81. $data = Dever::db($video['table'])->state($where);
  82. if ($data) {
  83. foreach ($data as $k => $v) {
  84. $this->createUrlOne($v, $video, $method, $config, $info);
  85. }
  86. }
  87. }
  88. public function createUrlOne($data, $video, $method, $config, $info)
  89. {
  90. if (!$data[$video['url_m3u8']] && $data[$video['sdate']] > 0 && $data[$video['edate']] > 0) {
  91. $data[$video['sdate']] = intval($data[$video['sdate']]);
  92. $data[$video['edate']] = intval($data[$video['edate']]);
  93. try {
  94. $url_m3u8 = $config['host'] . $method->save($info['key'],$data[$video['sdate']], $data[$video['edate']]);
  95. $url_mp4 = '';
  96. $url_mp4 = $config['host'] . $method->save($info['key'], $data[$video['sdate']], $data[$video['edate']], 'mp4');
  97. } catch (\Exception $e) {
  98. $url_m3u8 = $url_mp4 = '';
  99. }
  100. if (!$url_m3u8) {
  101. return false;
  102. }
  103. Dever::db($video['table'])->update(array('where_id' => $data['id'], $video['url_m3u8'] => $url_m3u8, $video['url_mp4'] => $url_mp4));
  104. return array('id' => $data['id'], 'm3u8' => $url_m3u8, 'mp4' => $url_mp4);
  105. }
  106. return false;
  107. }
  108. # 开始直播
  109. public function startLive($id)
  110. {
  111. $info = Dever::db('live/stream')->one($id);
  112. $config = Dever::db('live/info')->one($info['live_id']);
  113. $method = $this->method($config);
  114. $data['where_id'] = $info['id'];
  115. $data['status'] = 2;
  116. Dever::db('live/stream')->update($data);
  117. }
  118. # 初始化流
  119. public function init($id, $name = '')
  120. {
  121. $info = Dever::db('live/stream')->one($id);
  122. if ($info['url_rtmp']) {
  123. return;
  124. }
  125. if ($name) {
  126. $info['key'] = $name;
  127. }
  128. return $this->get($info, true);
  129. }
  130. # 获取流的直播状态
  131. public function getStatus($info)
  132. {
  133. if (!is_array($info)) {
  134. $info = Dever::db('live/stream')->one($info);
  135. }
  136. $config = Dever::db('live/info')->one($info['live_id']);
  137. $method = $this->method($config);
  138. $status = array();
  139. try {
  140. $status = $method->getLiveStatus($info['key']);
  141. } catch (\Exception $e) {
  142. $status = array();
  143. }
  144. return $status;
  145. }
  146. # 获取流信息 定时跑
  147. public function get($info, $create = false)
  148. {
  149. if (!is_array($info)) {
  150. $info = Dever::db('live/stream')->one($info);
  151. }
  152. $config = Dever::db('live/info')->one($info['live_id']);
  153. $method = $this->method($config);
  154. if ($create) {
  155. $stream = $method->create($info['key']);
  156. } else {
  157. $stream = $method->getInfo($info['key']);
  158. }
  159. # 根据上次得到推流地址的时间计算下次应该得到的地址
  160. if (!$info['live_date']) {
  161. $info['live_date'] = 0;
  162. }
  163. $time = time();
  164. $num = $info['times'];
  165. if ($time - $info['live_date'] > $num) {
  166. $rtmp = $method->getRtmp($info['key'], $info['times'] * 3600);
  167. }
  168. if (!$info['url_rtmp']) {
  169. $url = $method->getPlay($info['key']);
  170. }
  171. $status = array();
  172. $history = array();
  173. try {
  174. $status = $method->getLiveStatus($info['key']);
  175. } catch (\Exception $e) {
  176. $status = array();
  177. }
  178. try {
  179. $history = $method->history($info['key']);
  180. } catch (\Exception $e) {
  181. $history = array();
  182. }
  183. $data['where_id'] = $info['id'];
  184. if (isset($rtmp) && $rtmp) {
  185. $data['live'] = $rtmp;
  186. $data['live_date'] = $time;
  187. }
  188. if (isset($url) && isset($url['rtmp']) && $url['rtmp']) {
  189. $data['url_rtmp'] = $url['rtmp'];
  190. $data['url_hls'] = $url['hls'];
  191. $data['url_hdl'] = $url['hdl'];
  192. $data['url_pic'] = $url['pic'];
  193. }
  194. $live = Dever::json_decode($info['info']);
  195. # 直播信息
  196. if ($stream) {
  197. $live['info'] = $stream;
  198. }
  199. # 直播状态
  200. if ($status) {
  201. $live['live'] = $status;
  202. }
  203. # 直播历史
  204. if ($history) {
  205. $live['history'] = $history;
  206. if (isset($history['items']) && $history['items']) {
  207. foreach ($history['items'] as $k => $v) {
  208. $this->saveLog($info['id'], $v['start'], $v['end']);
  209. }
  210. }
  211. }
  212. if ($status) {
  213. $data['status'] = 2;
  214. } else {
  215. $data['status'] = 1;
  216. }
  217. $data['info'] = Dever::json_encode($live);
  218. Dever::db('live/stream')->update($data);
  219. return $data;
  220. }
  221. # 建立直播日志
  222. public function saveLog($id, $sdate, $edate)
  223. {
  224. $info = Dever::db('live/stream')->one($id);
  225. $data['sdate'] = $sdate;
  226. $data['edate'] = $edate;
  227. $data['stream_id'] = $id;
  228. $data['live_id'] = $info['live_id'];
  229. $data['active_id'] = $info['active_id'];
  230. $log = Dever::db('live/stream_log')->one($data);
  231. if (!$log) {
  232. $id = Dever::db('live/stream_log')->insert($data);
  233. }
  234. return $id;
  235. }
  236. }