瀏覽代碼

直播推流播放

panda 5 月之前
父節點
當前提交
18110f1f02

+ 34 - 0
application/api/controller/Live.php

@@ -0,0 +1,34 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use app\utils\Service\Tencent\TencentIm;
+
+/**
+ * 直播接口
+ */
+class Live extends Api
+{
+    protected $noNeedLogin = [''];
+    protected $noNeedRight = ['*'];
+
+    /**
+     * 直播推流和播放地址
+     * @return true
+     */
+    public function im_live_url()
+    {
+        $push_domain = 'xiaoshanpush.huxiukeji.cn';
+        $play_domain = 'xiaoshanplay.huxiukeji.cn';
+        $stream_name = 'xiaoshan01';
+        $push_key    = '65b5e18d53b58c2df0355f53c404d2ee';
+        $im = new TencentIm();
+        return $this->success('获取成功',[
+            'push_url' => $im->getLivePushUrl($push_domain,$stream_name,$push_key,date('Y-m-d H:i:s',strtotime('+1 day'))),
+            'play_url' => $im->getLivePlayUrl($play_domain,$stream_name)
+        ]);
+    }
+
+
+}

+ 0 - 9
application/common/controller/Api.php

@@ -95,8 +95,6 @@ class Api
         $this->_initialize();
         //日志
         $this->request_log();
-        //用户活跃
-        $this->user_active();
 
         // 前置操作方法
         if ($this->beforeActionList) {
@@ -447,13 +445,6 @@ class Api
         }
     }
 
-    //更新用户活跃
-    protected function user_active(){
-        if($this->auth->isLogin()){
-            db('user_active')->where('user_id',$this->auth->id)->update(['requesttime'=>time()]);
-        }
-    }
-
     //获取用户是否活跃,7200秒,2小时
     //1活跃,0不活跃
     protected function user_activeinfo($user_id,$requesttime = 0){

+ 30 - 0
application/utils/Service/Tencent/TencentIm.php

@@ -472,6 +472,36 @@ class TencentIm extends Module
         return $this->success('获取成功', $res);
     }
 
+    /**
+     * 获取直播推流地址
+     * @param string $domain 您用来推流的域名
+     * @param string $streamName 您用来区别不同推流地址的唯一流名称
+     * @param $key 安全密钥
+     * @param $time 过期时间 sample 2016-11-12 12:00:00
+     * @return string
+     */
+    public function getLivePushUrl(string $domain,string $streamName,$key = null, $time = null)
+    {
+        if($key && $time){
+            $txTime = strtoupper(base_convert(strtotime($time),10,16));
+            //txSecret = MD5( KEY + streamName + txTime )
+            $txSecret = md5($key.$streamName.$txTime);
+            $ext_str = "?".http_build_query(["txSecret"=> $txSecret, "txTime"=> $txTime]);
+        }
+        return "rtmp://".$domain."/live/".$streamName . ($ext_str ?? "");
+    }
+
+    /**
+     * 获取直播播放地址
+     * @param string $domain 您用来推流的域名
+     * @param string $streamName 您用来区别不同推流地址的唯一流名称
+     * @return string
+     */
+    public function getLivePlayUrl(string $domain,string $streamName)
+    {
+        return "rtmp://".$domain."/live/".$streamName;
+    }
+
     // 下载最近消息记录
     public function get_history($MsgTime,$ChatType = 1)
     {