xuzhengzong

1、阿里云直播官方文档:https://help.aliyun.com/document_detail/87396.html?spm=a2c4g.11186623.6.609.19d83dd2Uejwx7

     在线调试api工具:https://help.aliyun.com/document_detail/48207.html?spm=a2c4g.11186623.6.689.39f830f63Otms9

2、引入alibaba sdk for php SDK,git:https://github.com/aliyun/openapi-sdk-php

3、官方直播文档,有句话很重要:https://help.aliyun.com/document_detail/87396.html?spm=a2c4g.11186623.6.609.19d83dd2Uejwx7

 

 这句话什么意思?【就是说,你按步骤先配置域名、鉴权,直播地址随便你怎么生成都是有效的,直接拿到能录制直播和观看直播的组件中进行使用就可以了。测试推荐:OBS推工具和LVS播工具】

 

4、自定义阿里云直播 操作类(实现生成推流和播流地址、暂停直播、恢复直播、直播间人数显示等功能),不废话:

<?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;
        }
    }
}

 

分类:

技术点:

相关文章: