Handle.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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_log')->update(array('edate' => time(), 'where_id' => $log_id));
  39. return true;
  40. }
  41. # 建立直播日志
  42. public function saveLog($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_log')->insert($data);
  52. } else {
  53. $config = Dever::db('live/info')->one($info['live_id']);
  54. $log = Dever::db('live/stream_log')->one($log_id);
  55. $method = $this->method($config);
  56. $log['sdate'] = intval($log['sdate']);
  57. $log['edate'] = intval($log['edate']);
  58. $data['url_m3u8'] = $config['host'] . $method->save($info['key'],$log['sdate'], $log['edate']);
  59. $data['url_mp4'] = $config['host'] . $method->save($info['key'], $log['sdate'], $log['edate'], 'mp4');
  60. $data['where_id'] = $log_id;
  61. Dever::db('live/stream_log')->update($data);
  62. $id = $log_id;
  63. if ($table) {
  64. $data = Dever::db($table)->state(array('stream_id' => $info['id']));
  65. if ($data) {
  66. foreach ($data as $k => $v) {
  67. if (!$v['url_m3u8'] && $v['sdate'] > 0 && $v['edate'] > 0) {
  68. $v['sdate'] = intval($v['sdate']);
  69. $v['edate'] = intval($v['edate']);
  70. $url_m3u8 = $config['host'] . $method->save($info['key'],$v['sdate'], $v['edate']);
  71. $url_mp4 = $config['host'] . $method->save($info['key'], $v['sdate'], $v['edate'], 'mp4');
  72. Dever::db($table)->update(array('where_id' => $v['id'], 'url_m3u8' => $url_m3u8, 'url_mp4' => $url_mp4));
  73. }
  74. }
  75. }
  76. }
  77. }
  78. return $id;
  79. }
  80. # 开始直播
  81. public function startLive($id)
  82. {
  83. $info = Dever::db('live/stream')->one($id);
  84. $config = Dever::db('live/info')->one($info['live_id']);
  85. $method = $this->method($config);
  86. $data['where_id'] = $info['id'];
  87. $data['status'] = 2;
  88. Dever::db('live/stream')->update($data);
  89. }
  90. # 初始化流
  91. public function init($id, $name = '')
  92. {
  93. $info = Dever::db('live/stream')->one($id);
  94. if ($info['url_rtmp']) {
  95. return;
  96. }
  97. if ($name) {
  98. $info['key'] = $name;
  99. }
  100. return $this->get($info, true);
  101. }
  102. # 获取流信息 定时跑
  103. public function get($info, $create = false)
  104. {
  105. if (!is_array($info)) {
  106. $info = Dever::db('live/stream')->one($info);
  107. }
  108. $config = Dever::db('live/info')->one($info['live_id']);
  109. $method = $this->method($config);
  110. if ($create) {
  111. $stream = $method->create($info['key']);
  112. } else {
  113. $stream = $method->getInfo($info['key']);
  114. }
  115. $rtmp = $method->getRtmp($info['key'], $info['times'] * 3600);
  116. $url = $method->getPlay($info['key']);
  117. $status = array();
  118. $history = array();
  119. try {
  120. $status = $method->getLiveStatus($info['key']);
  121. } catch (\Exception $e) {
  122. $status = array();
  123. }
  124. try {
  125. $history = $method->history($info['key']);
  126. } catch (\Exception $e) {
  127. $history = array();
  128. }
  129. $data['where_id'] = $info['id'];
  130. $data['live'] = $rtmp;
  131. $data['url_rtmp'] = $url['rtmp'];
  132. $data['url_hls'] = $url['hls'];
  133. $data['url_hdl'] = $url['hdl'];
  134. $data['url_pic'] = $url['pic'];
  135. $live = Dever::json_decode($info['info']);
  136. $live['info'] = $stream;
  137. $live['live'] = $status;
  138. $live['hitory'] = $history;
  139. if ($status) {
  140. $data['status'] = 2;
  141. } else {
  142. $data['status'] = 1;
  143. }
  144. $data['info'] = Dever::json_encode($live);
  145. Dever::db('live/stream')->update($data);
  146. return $data;
  147. }
  148. }