转载地址:PHP+阿里云直播功能_帝在哭泣!的博客-CSDN博客_php实现直播功能
前提条件
已注册阿里云账号,注册流程请参见注册阿里云账号。
账号已进行实名认证,实名认证流程请参见个人实名认证或企业实名认证。
准备2个已完成备案的域名,备案流程请参见备案。
若您要添加的推流域名和播流域名,是同一个一级域名下的两个二级域名,则只需要将一级域名进行备案即可。
步骤如下
1.添加推流域名和播流域名
2.配置CNAME解析
3. 关联推流和播流
在域名管理页面,选择您添加的播流域名,单击域名配置。
进入直播管理 > 基本配置页面,单击基础信息页签。
在此页面,您可以查看该域名的CNAME、创建时间、业务类型、直播中心、加速区域。
单击推流信息页签。
单击添加推流信息或推流域名后的编辑图标,选择需要关联的推流域名,并单击确定。
4.(可选)配置自定义鉴权(可选)
在视频直播控制台左侧导航栏,单击域名管理,进入域名管理页面。
选择您要配置的播流域名,单击域名配置。
单击直播管理 > 访问控制,并选择URL鉴权页签,单击修改配置。
配置URL鉴权信息,单击确定。
配置项和说明如下表所示。
5.生成推流地址和播流地址
进入工具箱 > 地址生成器页面。
选择播流域名及其关联的推流域名。
输入AppName和StreamName。
根据实际需求选择转码模版。
6.推流和播放
推流是把采集阶段封装好的音视频直播流推送到阿里云直播服务中心的过程。播放是将直播服务中心已有直播内容分发到播放器进行播放的过程。以下内容以PC端OBS推流插件Demo(OBS为第三方工具,插件由视频直播提供)、PC端播放工具VLC(VLC为第三方工具)、阿里云直播(视频直播Demo应用)以及阿里云Web播放器为例,指引您完成推流与播放操作。
阿里云官方文档:快速入门 - 视频直播 - 阿里云
7. PHP 代码实现
7.1 SDK下载 composer require alibabacloud/live
7. 2 实现代码
<?php
namespace App\Server;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
class LiveA extends Controller
{
private $error;
const DOMAIN_NAME='';//你的加速域名,和推流域名一致。
const REGION_ID='cn-shanghai';//区域
const ACCESS_KEY_ID='';//阿里云秘钥
const ACCESS_KEY_SECRET='';//阿里云秘钥
const LIVE_HOST='live.aliyuncs.com';//写死,阿里云直播CDN域名
const ALIVE_URL='';//推域名
const ALIVE_KEY=''; //推流鉴权KEY,后台拿
const TLIVE_URL='';//关联的播域名
const TLIVE_KEY=''; //播流鉴权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);
//创建推流
$rtmp_sstring = '/'.$appName.'/'.$streamName.'-'.$endTime.'-0-0-'.self::TLIVE_KEY;
$rtmp_md5hash = md5($rtmp_sstring);
$rtmp_play_url = 'rtmp://'.self::TLIVE_URL.'/'.$appName.'/'.$streamName.'?auth_key='.$endTime.'-0-0-'.$rtmp_md5hash;
$flv_sstring = '/'.$appName.'/'.$streamName.'.flv-'.$endTime.'-0-0-'.self::TLIVE_KEY;
$flv_md5hash = md5($flv_sstring);
$flv_play_url = 'http://'.self::TLIVE_URL.'/'.$appName.'/'.$streamName.'.flv?auth_key='.$endTime.'-0-0-'.$flv_md5hash;
$hls_sstring = '/'.$appName.'/'.$streamName.'.m3u8-'.$endTime.'-0-0-'.self::TLIVE_KEY;
$hls_md5hash = md5($hls_sstring);
$tliveUrlM3U8 = 'http://'.self::TLIVE_URL.'/'.$appName.'/'.$streamName.'.m3u8?auth_key='.$endTime.'-0-0-'.$hls_md5hash;
$data = [
'rtmp'=>$rtmp_play_url,
'flv'=>$flv_play_url,
'hls'=>$tliveUrlM3U8,
];
return [
'alive_url'=>$aliveUrl,
'tlive_url'=>$data,
];
}
//停止直播
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 $result;
}
//恢复直播
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 $result;
}
//获取直播在线人数
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 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;
}
}
}
以及 m3u8 的网页播放代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="IE=edge" >
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"/>
<title>Aliplayer Online Settings</title>
<link rel="stylesheet" href="https://g.alicdn.com/de/prismplayer/2.9.16/skins/default/aliplayer-min.css" />
<script charset="utf-8" type="text/javascript" src="https://g.alicdn.com/de/prismplayer/2.9.16/aliplayer-min.js"></script>
</head>
<body>
<div class="prism-player" id="player-con"></div>
<script>
var player = new Aliplayer({
"id": "player-con",
//M3U8格式使用
"source": "http://bo.liyan.shop/447/447.m3u8?auth_key=1644577599-0-0-a44d0cc9d7619a879ccc1328a9aeeb48",
"width": "100%",
"height": "500px",
"autoplay": true,
//直播参数必须为true
"isLive": true,
"rePlay": false,
"playsinline": true,
"preload": true,
"controlBarVisibility": "hover",
"useH5Prism": true
}, function (player) {
console.log("The player is created");
}
);
</script>
</body>