阿里云直播生成推流和播流地址类

API列表:

https://help.aliyun.com/document_detail/87396.html?spm=a2c4g.11186623.6.609.19d83dd2Uejwx7

引入alibaba sdk for php SDK:

git:https://github.com/aliyun/openapi-sdk-php

介绍:

阿里云直播提供的是触发式的推流与播放,您无需提前创建资源,只要添加经过备案的推流域名和播流域名,并完成域名解析、鉴权等操作,即可根据地址拼接规则手动拼接,以快速获取对应的推流地址和播流地址。本文介绍未设置转码的直播活动的推流地址和播流地址的拼接万法。 阿里云直播提供的是触发式的推流与播放,您无需提前创建资源,只要添加经过备案的推流域名和播流域名,并完成域名解析、鉴权等操作,即可根据地址拼接规则手动拼接,以快速获取对应的推流地址和播流地址.本文介绍未设置转码的直播活动的推流地址和播流地址的拼接办法.

【按步骤先配置域名、鉴权,直播地址随便你怎么生成都是有效的,直接拿到能录制直播和观看直播的组件中进行使用就可以了】

测试推荐:OBS推流工具和LVS播流工具

代码参考:

<?php

namespace App\Service;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

class AliyunLive
{
    private $error;
    const DOMAIN_NAME='alive.i***h.cn';  //你的加速域名,和推流域名一致。
    const REGION_ID='cn-shanghai';      //区域
    const ACCESS_KEY_ID='LTAI4*****ibc';  //阿里云秘钥
    const ACCESS_KEY_SECRET='HPg2*****gnNOK';  //阿里云秘钥
    const LIVE_HOST='live.aliyuncs.com';  //写死,阿里云直播CDN域名
    const ALIVE_URL='alive.i***h.cn';    //推域名
    const ALIVE_KEY='oR11sd***';        //推流鉴权KEY,后台拿
    const TLIVE_URL='tlive.i***h.cn';    //关联的播域名
    const TLIVE_KEY='jdU1d***op';    //播流鉴权KEY,后台拿
    
    /**
     * 创建签名
     * @param $path 播放地址 域名后面所有
     * @param $exp //结束时间
     * @param $key
     * @return string
     */
    private static function createSign($path,$exp,$key)
    {
        $rand=0;
        $uid=0;
        $str=sprintf("%s-%s-%s-%s-%s",$path,(string)$exp,(string)$rand,(string)$uid,$key);
        $hashValue=md5($str);
        $authKey=sprintf("%s-%s-%s-%s",(string)$exp,(string)$rand,(string)$uid,$hashValue);
        return "auth_key=$authKey";
    }
    
    /**
     * 创建是直播地址
     * @param $appName 应用名称 ,自定义
     * @param $streamName 房间名称,自定义,该应用下唯一
     * @param $endTime 结束时间
     * @return array|bool  播放流(观看者):alive_url,推流(直播者):tlive_url
     */
    public static function createdLive($appName,$streamName,$endTime)
    {
        if(!$appName || !$streamName || !$endTime || $endTime < time()){
            return false;
        }
        //创建播流
        $path="/$appName/$streamName";
        $aliveUrl='rtmp://'.self::ALIVE_URL."$path?".self::createSign($path,$endTime,self::ALIVE_KEY);
    
        //创建推流
        $tliveUrlRTMP= 'rtmp://'.self::TLIVE_URL."$path?".self::createSign($path,$endTime,self::TLIVE_KEY);
        // $tliveUrlFLV ='http://'.self::TLIVE_URL."$path.flv?".self::createSign($path,$endTime,self::TLIVE_KEY);
        // $tliveUrlM3U8='http://'.self::TLIVE_URL."$path.m3u8?".self::createSign($path,$endTime,self::TLIVE_KEY);
        
        return [
            'alive_url'=>$aliveUrl,
            'tlive_url'=>$tliveUrlRTMP,
        ];
    }
    //停止直播
    public static function stopLive($appName,$streamName)
    {
    
        $query=[
            'RegionId' => self::REGION_ID,
            'AppName' => $appName,
            'StreamName' => $streamName,
            'LiveStreamType' => "publisher",
            'DomainName' => self::DOMAIN_NAME,
            // 'ResumeTime'=>'',
        ];
        $_this=new static();
        $result=$_this->request('ForbidLiveStream',$query);
        return true;
    }
    //恢复直播
    public static function resumeLive($appName,$streamName)
    {
        $query=[
            'RegionId' => self::REGION_ID,
            'LiveStreamType' => "publisher",
            'AppName' => $appName,
            'StreamName' => $streamName,
            'DomainName' => self::DOMAIN_NAME,
        ];
        $_this=new static();
        $result=$_this->request('ResumeLiveStream',$query);
        return true;
    }
    
    //获取直播在线人数
    public static function getOnlineUserNum($appName,$streamNma)
    {
        
        $query=[
            'RegionId' => self::REGION_ID,
            'DomainName' => self::DOMAIN_NAME,
            'AppName'=>$appName,
            'StreamName' =>$streamNma,
        ];
        $_this=new static();
        $result=$_this->request('DescribeLiveStreamOnlineUserNum',$query);
        return $result;
    }
    
    
    
    //日志
    public function log($msg,$info='info')
    {
        $msg='直播接口:'.$msg.($info !='info'?var_export($info,1):'');
        log_write($msg);
    }
    //获取错误
    public static function getError()
    {
        return (new static())->error;
    }
    //请求
    private function request($action,Array $query)
    {
        AlibabaCloud::accessKeyClient(self::ACCESS_KEY_ID, self::ACCESS_KEY_SECRET)
            ->regionId(self::REGION_ID)
            ->asDefaultClient();
        try {
            $result = AlibabaCloud::rpc()
                ->product('live')
                // ->scheme('https') // https | http
                ->version('2016-11-01')
                ->action($action)
                ->method('POST')
                ->host(self::LIVE_HOST)
                ->options([
                    'query' => $query,
                ])
                ->request();
            return $result->toArray();
        } catch (ClientException $e) {
            $this->error=$e->getMessage();
            return false;
        } catch (ServerException $e) {
            $this->error=$e->getMessage();
            return false;
        }
    }
}
通往牛逼的路上,在意的只有远方!
原文地址:https://www.cnblogs.com/ccdr/p/15546183.html