Handle.php 7.3 KB

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