Handle.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. $data['url_mp4'] = $config['host'] . $method->save($info['key'], $log['sdate'], $log['edate'], 'mp4');
  63. } catch (\Exception $e) {
  64. $data['url_m3u8'] = $data['url_mp4'] = '';
  65. }
  66. if (!$data['url_m3u8']) {
  67. return;
  68. }
  69. $data['where_id'] = $log_id;
  70. Dever::db('live/stream_ios_log')->update($data);
  71. $id = $log_id;
  72. if ($video) {
  73. $this->createUrl($video, $method, $config, $info);
  74. }
  75. }
  76. return $id;
  77. }
  78. # 生成回放地址
  79. public function createUrl($video, $method, $config, $info)
  80. {
  81. $where[$video['stream_id']] = $info['id'];
  82. $data = Dever::db($video['table'])->state($where);
  83. if ($data) {
  84. foreach ($data as $k => $v) {
  85. $this->createUrlOne($v, $video, $method, $config, $info);
  86. }
  87. }
  88. }
  89. public function createUrlOne($data, $video, $method, $config, $info)
  90. {
  91. if ($data[$video['sdate']] > 0 && $data[$video['edate']] > 0) {
  92. $data[$video['sdate']] = intval($data[$video['sdate']]);
  93. $data[$video['edate']] = intval($data[$video['edate']]);
  94. try {
  95. $url_m3u8 = $config['host'] . $method->save($info['key'],$data[$video['sdate']], $data[$video['edate']]);
  96. $url_mp4 = '';
  97. $url_mp4 = $config['host'] . $method->save($info['key'], $data[$video['sdate']], $data[$video['edate']], 'mp4');
  98. } catch (\Exception $e) {
  99. $url_m3u8 = $url_mp4 = '';
  100. }
  101. if (!$url_m3u8) {
  102. return false;
  103. }
  104. Dever::db($video['table'])->update(array('where_id' => $data['id'], $video['url_m3u8'] => $url_m3u8, $video['url_mp4'] => $url_mp4));
  105. return array('id' => $data['id'], 'm3u8' => $url_m3u8, 'mp4' => $url_mp4);
  106. }
  107. return false;
  108. }
  109. # 开始直播
  110. public function startLive($id)
  111. {
  112. $info = Dever::db('live/stream')->one($id);
  113. $config = Dever::db('live/info')->one($info['live_id']);
  114. $method = $this->method($config);
  115. $data['where_id'] = $info['id'];
  116. $data['status'] = 2;
  117. Dever::db('live/stream')->update($data);
  118. }
  119. # 初始化流
  120. public function init($id, $name = '')
  121. {
  122. $info = Dever::db('live/stream')->one($id);
  123. if ($info['url_rtmp']) {
  124. return;
  125. }
  126. if ($name) {
  127. $info['key'] = $name;
  128. }
  129. return $this->get($info, true);
  130. }
  131. # 获取流的直播状态
  132. public function getStatus($info)
  133. {
  134. if (!is_array($info)) {
  135. $info = Dever::db('live/stream')->one($info);
  136. }
  137. $config = Dever::db('live/info')->one($info['live_id']);
  138. $method = $this->method($config);
  139. $status = array();
  140. try {
  141. $status = $method->getLiveStatus($info['key']);
  142. } catch (\Exception $e) {
  143. $status = array();
  144. }
  145. return $status;
  146. }
  147. # 获取流信息 定时跑
  148. public function get($info, $create = false)
  149. {
  150. if (!is_array($info)) {
  151. $info = Dever::db('live/stream')->one($info);
  152. }
  153. $config = Dever::db('live/info')->one($info['live_id']);
  154. $method = $this->method($config);
  155. if ($create) {
  156. $stream = $method->create($info['key']);
  157. } else {
  158. $stream = $method->getInfo($info['key']);
  159. }
  160. # 根据上次得到推流地址的时间计算下次应该得到的地址
  161. if (!$info['live_date']) {
  162. $info['live_date'] = 0;
  163. }
  164. $time = time();
  165. $num = $info['times'];
  166. if ($time - $info['live_date'] > $num) {
  167. $rtmp = $method->getRtmp($info['key'], $info['times'] * 3600);
  168. }
  169. if (!$info['url_rtmp']) {
  170. $url = $method->getPlay($info['key']);
  171. }
  172. $status = array();
  173. $history = array();
  174. try {
  175. $status = $method->getLiveStatus($info['key']);
  176. } catch (\Exception $e) {
  177. $status = array();
  178. }
  179. try {
  180. $history = $method->history($info['key']);
  181. } catch (\Exception $e) {
  182. $history = array();
  183. }
  184. $data['where_id'] = $info['id'];
  185. if (isset($rtmp) && $rtmp) {
  186. $data['live'] = $rtmp;
  187. $data['live_date'] = $time;
  188. }
  189. if (isset($url) && isset($url['rtmp']) && $url['rtmp']) {
  190. $data['url_rtmp'] = $url['rtmp'];
  191. $data['url_hls'] = $url['hls'];
  192. $data['url_hdl'] = $url['hdl'];
  193. $data['url_pic'] = $url['pic'];
  194. }
  195. $live = Dever::json_decode($info['info']);
  196. # 直播信息
  197. if ($stream) {
  198. $live['info'] = $stream;
  199. }
  200. # 直播状态
  201. if ($status) {
  202. $live['live'] = $status;
  203. }
  204. # 直播历史
  205. if ($history) {
  206. $live['history'] = $history;
  207. if (isset($history['items']) && $history['items']) {
  208. foreach ($history['items'] as $k => $v) {
  209. $this->saveLog($info['id'], $v['start'], $v['end']);
  210. }
  211. }
  212. }
  213. if ($status) {
  214. $data['status'] = 2;
  215. } else {
  216. $data['status'] = 1;
  217. }
  218. $data['info'] = Dever::json_encode($live);
  219. Dever::db('live/stream')->update($data);
  220. return $data;
  221. }
  222. # 建立直播日志
  223. public function saveLog($id, $sdate, $edate)
  224. {
  225. $info = Dever::db('live/stream')->one($id);
  226. $data['sdate'] = $sdate;
  227. $data['edate'] = $edate;
  228. $data['stream_id'] = $id;
  229. $data['live_id'] = $info['live_id'];
  230. $data['active_id'] = $info['active_id'];
  231. $log = Dever::db('live/stream_log')->one($data);
  232. if (!$log) {
  233. $id = Dever::db('live/stream_log')->insert($data);
  234. }
  235. return $id;
  236. }
  237. }