Browse Source

拉流转推-拉流地址鉴权-推流地址鉴权

urbanu619 6 months ago
parent
commit
d9f4d4c9ce
1 changed files with 479 additions and 4 deletions
  1. 479 4
      web/app/admin/controller/LiveingController.php

+ 479 - 4
web/app/admin/controller/LiveingController.php

@@ -13,7 +13,12 @@
  */
 namespace app\admin\controller;
 
+require CMF_ROOT . 'vendor/tencentcloud/vendor/autoload.php';
+
 use cmf\controller\AdminBaseController;
+use TencentCloud\Live\V20180801\Models\DescribeLivePullStreamTasksRequest;
+use TencentCloud\Live\V20180801\Models\DescribeLiveStreamOnlineListRequest;
+use TencentCloud\Live\V20180801\Models\DescribeLiveStreamPublishedListRequest;
 use think\Db;
 
 // 根据腾讯云sdk请求 创建直播拉流
@@ -28,6 +33,15 @@ use think\Exception;
 use think\exception\PDOException;
 
 class LiveingController extends AdminbaseController {
+
+    private $secretId   = 'AKID575vicM9rU6iBiWJNi09HF8xhMxOk4Od';
+    private $secretKey  = 'MiDkHwgOFpMpGf2KXcZIo9MYY5qnmhU5';
+    private $pushDomain = 'push.umotool.com';
+
+    private $pushKey    = 'd79106bd037188b132bc2d4cc272aa98';
+    private $pullKey    = 'JYthadi3fG4C4fbdmF6R';
+    private $pullDomain = 'tintonkangdahotel.com';
+
     protected function getLiveClass(){
 
         $liveclass=Db::name("live_class")->order('list_order asc, id desc')->column('id,name');
@@ -153,7 +167,7 @@ class LiveingController extends AdminbaseController {
 
             $pull_key = cmf_get_option('configpri')['tx_play_key'];
 
-            require CMF_ROOT . 'vendor/tencentcloud/vendor/autoload.php';
+
 
             try {
                 // 为了保护密钥安全,建议将密钥设置在环境变量中或者配置文件中。
@@ -182,7 +196,7 @@ class LiveingController extends AdminbaseController {
                 $params = array(
                     "SourceType" => "PullLivePushLive",
                     "SourceUrls" => [
-                        'https://umotool.com/live/'.$uid.'.m3u8',
+                        'https://tintonkangdahotel.com/live/'.$uid.'.flv',
                     ],
                     "DomainName" => 'push.umotool.com',
                     "PushArgs"   => 'txSecret='.$txSecret.'&txTime='.$txTime,
@@ -198,7 +212,9 @@ class LiveingController extends AdminbaseController {
 
                 // 推流地址、播流地址、taskId
                 $data['push'] = 'rtmp://push.umotool.com/live/'. $uid . '?' .$params['PushArgs'];
-                $data['pull'] = 'https://umotool.com/live/'.$uid.'.m3u8?' .$params['PushArgs'];
+
+                $pull_urls = $this->getPullUrl( 'live', $uid, $end_timestamp );
+                $data['pull'] = $pull_urls;
                 $data['TaskId'] = $resp->TaskId;
 
                 echo json_encode( $data , JSON_UNESCAPED_UNICODE );
@@ -409,5 +425,464 @@ class LiveingController extends AdminbaseController {
             $this->success("修改成功!");
         }
     }
-        
+
+
+
+
+    /**
+     * 获取推流地址
+     * 如果不传key和过期时间,将返回不含防盗链的url
+     * @param domain 您用来推流的域名
+     *        streamName 您用来区别不同推流地址的唯一流名称
+     *        key 安全密钥
+     *        time 过期时间 sample 2016-11-12 12:00:00
+     * @return String url
+     */
+    public function getPushUrl( $appName, $streamName, $time ): string
+    {
+        $domain = $this->pushDomain;
+        $key    = $this->pushKey;
+
+        $txTime = strtoupper( base_convert( strtotime( $time ), 10, 16 ) );
+        //txSecret = MD5( KEY + streamName + txTime )
+        $txSecret = md5( $key . $streamName . $txTime );
+        return "?" . http_build_query( array(
+                "txSecret" => $txSecret,
+                "txTime"   => $txTime
+            ) );
+//        return $domain . ( $ext_str ?? "" );
+    }
+
+
+    public function getPullUrl( $appName, $streamName, $time )
+    {
+        $domain = $this->pullDomain;
+        $key    = $this->pullKey;
+
+        $txTime = strtoupper( base_convert( strtotime( $time ), 10, 16 ) );
+        //txSecret = MD5( KEY + streamName + txTime )
+        $txSecret   = md5( $key . $streamName . $txTime );
+        $build_data = [
+            "txSecret" => $txSecret,
+            "txTime"   => $txTime
+        ];
+        $ext_str    = "?" . http_build_query( $build_data );
+        return [
+            "rtmp://" . $domain . "/" . $appName . "/" . $streamName . $ext_str,
+            "https://" . $domain . "/" . $appName . "/" . $streamName . ".m3u8" . $ext_str,
+            "https://" . $domain . "/" . $appName . "/" . $streamName . ".flv" . $ext_str
+        ];
+
+    }
+
+    public function getPullUrl2( $appName, $streamName, $time )
+    {
+        $domain = $this->pullDomain;
+        $key    = $this->pullKey;
+
+        $txTime = strtoupper( base_convert( strtotime( $time ), 10, 16 ) );
+        //txSecret = MD5( KEY + streamName + txTime )
+        $txSecret   = md5( $key . $streamName . $txTime );
+        $build_data = [
+            "txSecret" => $txSecret,
+            "txTime"   => $txTime
+        ];
+        $ext_str    = "?" . http_build_query( $build_data );
+
+        return [
+            "rtmp://" . $domain . "/" . $appName . "/" . $streamName,
+            "https://" . $domain . "/" . $appName . "/" . $streamName . ".m3u8",
+            "https://" . $domain . "/" . $appName . "/" . $streamName . ".flv"
+        ];
+
+    }
+
+
+    /**
+     * @param $sport_id
+     * @param $live_data
+     * @return false|void
+     * 拉流转推
+     */
+    public function setRtmpGetHttp( $sport_id, $live_data ,$play_urls_count=2)
+    {
+
+        try {
+            // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
+            // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
+            // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
+            $cred = new Credential( $this->secretId, $this->secretKey );
+            // 实例化一个http选项,可选的,没有特殊需求可以跳过
+            $httpProfile = new HttpProfile();
+            $httpProfile->setEndpoint( "live.tencentcloudapi.com" );
+
+            // 实例化一个client选项,可选的,没有特殊需求可以跳过
+            $clientProfile = new ClientProfile();
+            $clientProfile->setHttpProfile( $httpProfile );
+            // 实例化要请求产品的client对象,clientProfile是可选的
+            $client = new LiveClient( $cred, "ap-bangkok", $clientProfile );
+
+            // 实例化一个请求对象,每个接口都会对应一个request对象
+            $req = new CreateLivePullStreamTaskRequest();
+
+            $SourceUrls = [];
+            if(count($live_data[ 'urls' ]) == 1)
+            {
+                $SourceUrls[] = $live_data[ 'urls' ][0]['m3u8'];
+            }else{
+                if(!empty($live_data[ 'urls' ][$play_urls_count]['flv']))
+                {
+                    $SourceUrls[] = $live_data[ 'urls' ][$play_urls_count]['flv'];
+                }else{
+                    foreach ( $live_data[ 'urls' ] as $item ) {
+                        if ( !empty( $item[ 'flv' ] && empty( $SourceUrls ) ) ) {
+                            $SourceUrls[] = $item[ 'flv' ];
+                        }
+                    }
+                }
+            }
+
+            if ( empty( $SourceUrls ) ) {
+                return false;
+            }
+
+
+            $AppName    = 'sport_test_live';
+            $StreamName = 'sport_test_live_' . $sport_id;
+            $now        = time();
+
+            $_now       = $this->getNow( $live_data );
+
+            $time      = date( 'Y-m-d H:i:s', $_now );
+            $StartTime = date( 'Y-m-d\TH:i:s\Z', ( $now - 28800 ) );
+            $EndTime   = date( 'Y-m-d\TH:i:s\Z', ( $_now - 28800 ) );
+            $push_url  = $this->getPushUrl( $AppName, $StreamName, $time );
+
+            //播放链接限制10分钟
+            $_time     = date( 'Y-m-d H:i:s', time() + 600 );
+            $pull_urls = $this->getPullUrl( $AppName, $StreamName, $_time );
+
+            $params = array(
+                "SourceType" => "PullLivePushLive",
+                "SourceUrls" => $SourceUrls,
+                "DomainName" => $this->pushDomain,
+                "PushArgs"   => $push_url,
+                "AppName"    => $AppName,
+                "StreamName" => $StreamName,
+                "StartTime"  => $StartTime,
+                "EndTime"    => $EndTime,
+                "Operator"   => 'tom001',
+            );
+            $req->fromJsonString( json_encode( $params ) );
+            // 返回的resp是一个CreateLivePullStreamTaskResponse的实例,与请求对象对应
+            $resp   = $client->CreateLivePullStreamTask( $req );
+            $v_data = [
+                'tencent_res' => json_decode( json_encode( $resp ), 1 ),
+                'play_urls'   => $pull_urls
+            ];
+            return $v_data;
+        } catch ( TencentCloudSDKException $e ) {
+            return [ 'ode' => $e->getCode(), 'msg' => $e->getMessage() ];
+        }
+
+    }
+
+
+    public function getNow( $live_data )
+    {
+
+        $start_time = $live_data[ 'kickoffTS' ] + ( 12 * 60 * 60 );
+
+        if ( $start_time >= time() ) {
+            return ( $start_time + 10800 );
+        }
+
+        $sy_time = time() - $start_time;
+
+        $zy = 10800 - $sy_time;
+
+        if ( $sy_time > 9500 ) {
+            $add_time = time() + 1800;
+        } else {
+            $add_time = time() + $zy;
+        }
+        return $add_time;
+    }
+
+
+    public function upRtmpGetHttp( $sport_id, $live_data, $taskId )
+    {
+        try {
+            // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
+            // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。密钥可前往官网控制台 https://console.tencentcloud.com/capi 进行获取
+            $cred = new Credential( $this->secretId, $this->secretKey );
+            // 实例化一个http选项,可选的,没有特殊需求可以跳过
+            $httpProfile = new HttpProfile();
+            $httpProfile->setEndpoint( "live.tencentcloudapi.com" );
+
+            // 实例化一个client选项,可选的,没有特殊需求可以跳过
+            $clientProfile = new ClientProfile();
+            $clientProfile->setHttpProfile( $httpProfile );
+            // 实例化要请求产品的client对象,clientProfile是可选的
+            $client = new LiveClient( $cred, "ap-bangkok", $clientProfile );
+
+            // 实例化一个请求对象,每个接口都会对应一个request对象
+            $req        = new ModifyLivePullStreamTaskRequest();
+            $SourceUrls = [];
+
+            foreach ( $live_data as $item ) {
+                if ( !empty( $item[ 'm3u8' ] && empty( $SourceUrls ) ) ) {
+                    $SourceUrls[] = $item[ 'm3u8' ];
+                }
+            }
+            if ( empty( $SourceUrls ) ) {
+                return false;
+            }
+            $AppName    = 'sport_test_live';
+            $StreamName = 'sport_test_live_' . $sport_id;
+            $now        = time();
+            $_now       = time() + 600;
+            $time       = date( 'Y-m-d H:i:s', $_now );
+            $StartTime  = date( 'Y-m-d\TH:i:s\Z', ( $now - 28800 ) );
+            $EndTime    = date( 'Y-m-d\TH:i:s\Z', ( $_now - 28800 ) );
+
+            $pull_urls = $this->getPullUrl( $AppName, $StreamName, $time );
+            $params    = array(
+                "TaskId"     => $taskId,
+                "SourceUrls" => $SourceUrls,
+                "StartTime"  => $StartTime,
+                "EndTime"    => $EndTime,
+                "Operator"   => 'tom001',
+
+            );
+            $req->fromJsonString( json_encode( $params ) );
+
+            // 返回的resp是一个ModifyLivePullStreamTaskResponse的实例,与请求对象对应
+            $resp = $client->ModifyLivePullStreamTask( $req );
+
+            $v_data = [
+                'tencent_res' => json_decode( json_encode( $resp ), 1 ),
+                'play_urls'   => $pull_urls
+            ];
+            // 输出json格式的字符串回包
+            return $v_data;
+
+            // 输出json格式的字符串回包
+//            print_r( $resp->toJsonString() );
+        } catch ( TencentCloudSDKException $e ) {
+            return [ 'ode' => $e->getCode(), 'msg' => $e->getMessage() ];
+        }
+    }
+
+
+    public function upRtmpGetHttpUrl( $sport_id )
+    {
+        $AppName    = 'sport_test_live';
+        $StreamName = 'sport_test_live_' . $sport_id;
+        $_now       = time() + 600;
+        $time       = date( 'Y-m-d H:i:s', $_now );
+        $pull_urls  = $this->getPullUrl( $AppName, $StreamName, $time );
+        return $pull_urls;
+    }
+
+
+    public function delSportExpired( $taskId )
+    {
+        try {
+            // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
+            // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。密钥可前往官网控制台 https://console.tencentcloud.com/capi 进行获取
+            $cred = new Credential( $this->secretId, $this->secretKey );
+            // 实例化一个http选项,可选的,没有特殊需求可以跳过
+            $httpProfile = new HttpProfile();
+            $httpProfile->setEndpoint( "live.tencentcloudapi.com" );
+
+            // 实例化一个client选项,可选的,没有特殊需求可以跳过
+            $clientProfile = new ClientProfile();
+            $clientProfile->setHttpProfile( $httpProfile );
+            // 实例化要请求产品的client对象,clientProfile是可选的
+            $client = new LiveClient( $cred, "ap-bangkok", $clientProfile );
+
+            // 实例化一个请求对象,每个接口都会对应一个request对象
+            $req = new DeleteLivePullStreamTaskRequest();
+
+            $params = array(
+                "TaskId"   => $taskId,
+                "Operator" => "tom001"
+            );
+            $req->fromJsonString( json_encode( $params ) );
+            // 返回的resp是一个DeleteLivePullStreamTaskResponse的实例,与请求对象对应
+            $resp = $client->DeleteLivePullStreamTask( $req );
+            // 输出json格式的字符串回包
+            $json = $resp->toJsonString();
+            return json_decode( $json, 1 );
+
+        } catch ( TencentCloudSDKException $e ) {
+            return [ 'ode' => $e->getCode(), 'msg' => $e->getMessage() ];
+        }
+    }
+
+
+    public function getLiveAll( $page = 1, $limit = 20, $liveid = '' )
+    {
+        try {
+            // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
+            // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。密钥可前往官网控制台 https://console.tencentcloud.com/capi 进行获取
+            $cred = new Credential( $this->secretId, $this->secretKey );
+            // 实例化一个http选项,可选的,没有特殊需求可以跳过
+            $httpProfile = new HttpProfile();
+            $httpProfile->setEndpoint( "live.tencentcloudapi.com" );
+
+            // 实例化一个client选项,可选的,没有特殊需求可以跳过
+            $clientProfile = new ClientProfile();
+            $clientProfile->setHttpProfile( $httpProfile );
+            // 实例化要请求产品的client对象,clientProfile是可选的
+            $client = new LiveClient( $cred, "ap-bangkok", $clientProfile );
+
+            // 实例化一个请求对象,每个接口都会对应一个request对象
+            $req = new DescribeLivePullStreamTasksRequest();
+
+
+            $params = [
+                'PageNum'  => $page,
+                'PageSize' => $limit
+            ];
+            if ( $liveid ) {
+                $params[ 'TaskId' ] = $liveid;
+            }
+
+            $req->fromJsonString( json_encode( $params ) );
+
+            // 返回的resp是一个DescribeLivePullStreamTasksResponse的实例,与请求对象对应
+            $resp = $client->DescribeLivePullStreamTasks( $req );
+
+            // 输出json格式的字符串回包
+            return json_decode( $resp->toJsonString(), 1 );
+        } catch ( TencentCloudSDKException $e ) {
+            return [ 'ode' => $e->getCode(), 'msg' => $e->getMessage() ];
+        }
+    }
+
+
+    public function getLiveOnlineList( $page = 1, $limit = 100, $AppName = 'A-live' )
+    {
+        try {
+            // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
+            // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
+            // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
+            $cred = new Credential( $this->secretId, $this->secretKey );
+            // 实例化一个http选项,可选的,没有特殊需求可以跳过
+            $httpProfile = new HttpProfile();
+            $httpProfile->setEndpoint( "live.tencentcloudapi.com" );
+
+            // 实例化一个client选项,可选的,没有特殊需求可以跳过
+            $clientProfile = new ClientProfile();
+            $clientProfile->setHttpProfile( $httpProfile );
+            // 实例化要请求产品的client对象,clientProfile是可选的
+            $client = new LiveClient( $cred, "", $clientProfile );
+
+            // 实例化一个请求对象,每个接口都会对应一个request对象
+            $req = new DescribeLiveStreamOnlineListRequest();
+
+            $params = array(
+                "AppName"  => $AppName,
+                "PageNum"  => $page,
+                "PageSize" => $limit
+            );
+            $req->fromJsonString( json_encode( $params ) );
+
+            // 返回的resp是一个DescribeLiveStreamOnlineListResponse的实例,与请求对象对应
+            $resp = $client->DescribeLiveStreamOnlineList( $req );
+
+            return json_decode( $resp->toJsonString(), 1 );
+        } catch ( TencentCloudSDKException $e ) {
+            return [ 'ode' => $e->getCode(), 'msg' => $e->getMessage() ];
+        }
+    }
+
+
+    public function getLiveStreamPublishedList( $AppName = 'A-live', $StreamName = '' )
+    {
+        try {
+            // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
+            // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
+            // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
+            $cred = new Credential( $this->secretId, $this->secretKey );
+            // 实例化一个http选项,可选的,没有特殊需求可以跳过
+            $httpProfile = new HttpProfile();
+            $httpProfile->setEndpoint( "live.tencentcloudapi.com" );
+
+            // 实例化一个client选项,可选的,没有特殊需求可以跳过
+            $clientProfile = new ClientProfile();
+            $clientProfile->setHttpProfile( $httpProfile );
+            // 实例化要请求产品的client对象,clientProfile是可选的
+            $client = new LiveClient( $cred, "", $clientProfile );
+
+            // 实例化一个请求对象,每个接口都会对应一个request对象
+            $req       = new DescribeLiveStreamPublishedListRequest();
+            $now       = time();
+            $StartTime = date( 'Y-m-d\TH:i:s\Z', ( $now - 86400 ) );
+            $EndTime   = date( 'Y-m-d\TH:i:s\Z', $now );
+
+            $params = array(
+                'DomainName' => $this->pushDomain,
+                "StartTime"  => $StartTime,
+                "EndTime"    => $EndTime,
+                "AppName"    => $AppName
+            );
+
+            if ( !empty( $StreamName ) ) {
+                $params[ 'StreamName' ] = $StreamName;
+            }
+
+            $req->fromJsonString( json_encode( $params ) );
+
+            // 返回的resp是一个DescribeLiveStreamPublishedListResponse的实例,与请求对象对应
+            $resp = $client->DescribeLiveStreamPublishedList( $req );
+
+            return json_decode( $resp->toJsonString(), 1 );
+        } catch ( TencentCloudSDKException $e ) {
+            return [ 'ode' => $e->getCode(), 'msg' => $e->getMessage() ];
+        }
+    }
+
+
+    public function startAnchorRtmp( $roomid, $sportId )
+    {
+
+
+        $AppName    = 'A-live';
+        $StreamName = 'A-live-' . $roomid . '-' . $sportId;
+        $now        = time();
+
+        //推球时间 过期时间是 12个小时
+        $start_time = date( 'Y-m-d H:i:s', $now + ( 12 * 60 * 60 ) );
+        //播放链接 有效时间是10分钟,每5分钟更新一次
+        $end_time = date( 'Y-m-d H:i:s', $now + 600 );
+
+        $ext_str = $this->getPushUrl( $AppName, $StreamName, $start_time );
+
+        $rtmpPushUrl    = "rtmp://" . $this->pushDomain . "/" . $AppName . "/" . $StreamName . $ext_str;
+        $rtmp_push_url  = "rtmp://" . $this->pushDomain . "/" . $AppName . "/";
+        $rtmp_push_code = $StreamName . $ext_str;
+
+        $pull_urls = $this->getPullUrl2( $AppName, $StreamName, $end_time );
+
+        $urls = [
+            'rtmp_push_url'  => $rtmp_push_url,
+            'rtmp_push_code' => $rtmp_push_code,
+            'rtmp_push'      => $rtmpPushUrl,
+            'hls_pull'       => $pull_urls[ 1 ],
+            'http_pull'      => $pull_urls[ 2 ],
+            'rtmp_pull'      => $pull_urls[ 0 ],
+        ];
+
+
+        return [
+            'code' => 0,
+            'data' => $urls
+        ];
+
+    }
+
+
 }