LivebackController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * 直播回放
  4. */
  5. namespace app\appapi\controller;
  6. use cmf\controller\HomeBaseController;
  7. use think\facade\Db;
  8. use think\db\Query;
  9. class livebackController extends HomebaseController {
  10. /*
  11. 回调数据格式
  12. {
  13. "channel_id": "2121_15919131751",
  14. "end_time": 1473125627,
  15. "event_type": 100,
  16. "file_format": "flv",
  17. "file_id": "9192487266581821586",
  18. "file_size": 9749353,
  19. "sign": "fef79a097458ed80b5f5574cbc13e1fd",
  20. "start_time": 1473135647,
  21. "stream_id": "2121_15919131751",
  22. "t": 1473126233,
  23. "video_id": "200025724_ac92b781a22c4a3e937c9e61c2624af7",
  24. "video_url": "http://200025724.vod.myqcloud.com/200025724_ac92b781a22c4a3e937c9e61c2624af7.f0.flv"
  25. }
  26. */
  27. public function index(){
  28. $request = file_get_contents("php://input");
  29. //file_put_contents('./im.txt',date('y-m-d H:i:s').' 提交参数信息 callback request:'.$request."\r\n",FILE_APPEND);
  30. $result = array( 'code' => 0 );
  31. $data = json_decode($request, true);
  32. if(!$data){
  33. $this->callbacklog("request para json format error");
  34. $result['code']=4001;
  35. echo json_encode($result);
  36. return;
  37. }
  38. if(/*array_key_exists("t",$data)
  39. && array_key_exists("sign",$data)
  40. &&*/ array_key_exists("event_type",$data)
  41. && array_key_exists("stream_id",$data))
  42. {
  43. // $check_t = $data['t'];
  44. // $check_sign = $data['sign'];
  45. $event_type = $data['event_type'];
  46. $stream_id = $data['stream_id'];
  47. }else {
  48. $this->callbacklog("request para error");
  49. $result['code']=4002;
  50. echo json_encode($result);
  51. return;
  52. }
  53. /* $md5_sign = $this-> GetCallBackSign($check_t);
  54. if( !($check_sign == $md5_sign) ){
  55. $this->callbacklog("check_sign error:" . $check_sign . ":" . $md5_sign);
  56. $result['code']=4003;
  57. echo json_encode($result);
  58. return;
  59. } */
  60. if($event_type == 100){
  61. /* 回放回调 */
  62. if(array_key_exists("video_id",$data) &&
  63. array_key_exists("video_url",$data) &&
  64. array_key_exists("start_time",$data) &&
  65. array_key_exists("end_time",$data) ){
  66. $video_id = $data['video_id'];
  67. $video_url = $data['video_url'];
  68. $start_time = $data['start_time'];
  69. $end_time = $data['end_time'];
  70. }else{
  71. $this->callbacklog("request para error:回放信息参数缺少" );
  72. $result['code']=4002;
  73. echo json_encode($result);
  74. return;
  75. }
  76. }
  77. $ret=0;
  78. if($event_type == 0){
  79. /* 状态回调 断流 */
  80. //$ret=$this->stopRoom('',$stream_id);
  81. $this->upOfftime(1,'',$stream_id);
  82. }elseif ($event_type == 1){
  83. //推流
  84. //$ret = $this->dao_live->callBackLiveStatus($stream_id,1);
  85. $this->upOfftime(0,'',$stream_id);
  86. }elseif ($event_type == 100){
  87. //腾讯云点播回调
  88. //$duration = $end_time - $start_time;
  89. //if ( $duration > 60 ){
  90. $data=array(
  91. "video_url"=>$video_url,
  92. // "duration"=>$duration,
  93. // "file_id"=>$video_id,
  94. );
  95. Db::name("user_liverecord")->where("stream='{$stream_id}'")->update($data);
  96. //}else {
  97. // $ret = 0;
  98. // $this->callbacklog("tape duration too short:" . strval($duration) ."|" . $stream_id . "|" . $video_id);
  99. //}
  100. }
  101. $result['code']=$ret;
  102. echo json_encode($result);
  103. }
  104. public function GetCallBackSign($txTime){
  105. $config=getConfigPri();
  106. $md5_val = md5($config['live_push_key'] . strval($txTime));
  107. return $md5_val;
  108. }
  109. public function callbacklog($msg){
  110. //file_put_contents('./callbacklog.txt',date('Y-m-d H:i:s').' 提交参数信息 :'.$msg."\r\n",FILE_APPEND);
  111. }
  112. public function upOfftime($isoff=1,$uid='',$stream=''){
  113. $where['islive']=1;
  114. if($uid){
  115. $where['uid']=$uid;
  116. }else{
  117. $where['stream']=$stream;
  118. }
  119. $data=[
  120. 'isoff'=>$isoff,
  121. 'offtime'=>0,
  122. ];
  123. if($isoff==1){
  124. $data['offtime']=time();
  125. }
  126. $info=Db::name('user_live')->where($where)->update($data);
  127. return 0;
  128. }
  129. public function stopRoom($uid='',$stream=''){
  130. $where['islive']=1;
  131. if($uid){
  132. $where['uid']=$uid;
  133. //file_put_contents('./im.txt',date('y-m-d H:i:s').' 提交参数信息 im:'."\r\n",FILE_APPEND);
  134. }else{
  135. $where['stream']=$stream;
  136. //file_put_contents('./im.txt',date('y-m-d H:i:s').' 提交参数信息 callback:'."\r\n",FILE_APPEND);
  137. }
  138. $live=Db::name("user_live")->field("uid,showid,starttime,title,province,city,stream,thumb")->where($where)->find();
  139. //file_put_contents('./im.txt',date('y-m-d H:i:s').' 提交参数信息 sql:'.$where."\r\n",FILE_APPEND);
  140. //file_put_contents('./im.txt',date('y-m-d H:i:s').' 提交参数信息 live:'.json_encode($live)."\r\n",FILE_APPEND);
  141. if($live){
  142. Db::name('user_live')->where(['stream'=>$live['stream']])->delete();
  143. // votes
  144. $uid=$live['uid'];
  145. $stream=$live['stream'];
  146. $nowtime=time();
  147. $live['endtime']=$nowtime;
  148. $live['time']=date("Y-m-d",$live['showid']);
  149. $nums=zSize('user_'.$stream);
  150. $live['nums']=0;
  151. if($nums){
  152. $live['nums']=$nums;
  153. }
  154. $live['votes']=0;
  155. $where2['uid']=['neq',$uid];
  156. $where2['touid']=$uid;
  157. $where2['showid']=$live['showid'];
  158. $votes=Db::name('user_coinrecord')
  159. ->where($where2)
  160. ->sum('totalcoin');
  161. if($votes){
  162. $live['votes']=$votes;
  163. }
  164. $result=Db::name('user_liverecord')->insert($live);
  165. hDel("livelist",$uid);
  166. delcache('user_'.$stream);
  167. // 解除本场禁言
  168. $list2=Db::name("user_live_shut")
  169. ->field('uid')
  170. ->where("liveuid={$uid} and showid!=0")
  171. ->select();
  172. Db::name("user_live_shut")->where("liveuid={$uid} and showid!=0")->delete();
  173. foreach($list2 as $k=>$v){
  174. hDel($uid . 'shutup',$v['uid']);
  175. }
  176. }
  177. return 0;
  178. }
  179. /* 定时处理关播-允许短时间 断流续推 */
  180. public function uplive(){
  181. $notime=time();
  182. $offtime=$notime - 30;
  183. $where=[];
  184. $where[]=['islive','=','1'];
  185. $where[]=['isvideo','=','0'];
  186. $where[]=['isoff','=','1'];
  187. $where[]=['offtime','<',$offtime];
  188. $list=Db::name("user_live")->where($where)->select();
  189. $list->each(function($v,$k){
  190. $this->stopRoom('',$v['stream']);
  191. });
  192. echo 'OK';
  193. }
  194. }