采集设备:
负责采集直播视频画面
JVC/杰伟世 GZ-RY980HAC 4K四防直播婚庆会议教学网红摄像机 灰色 价格:5000左右
推流设备:
负责将视频以流的形式推送到直播平台
奥顿Q8 4G多卡聚合直播编码器HDMI SDI双路高清视频推流导播机 价格:3000左右
直播平台:
阿里云直播平台
PHP SDK安装:
功能点实现:
1.可获取直播推流地址与直播播流地址(播放地址)
2.可获取直播实时观看人数(会有延迟)
代码:
private $accessKeyId = '********';//阿里云 AccessKey ID。 private $accessSecret = '********';//阿里云 Access Key Secret
private $regionId = 'cn-beijing';
private $AppName = 0;//直播流所属应用名称。
private $StreamName = 0;//直播流名称。
private $pushHost = "****";//推流地址
private $pushHostKey = "********";//推流地址鉴权 key
private $playHost = "********";//播流地址 private $playHostKey = "********";//播流地址鉴权 key public function __construct(array $data)
{
parent::__construct($data);
$this->AppName = $data["AppName"];
$this->StreamName = $data["StreamName"];
}
/**
* 生成推流地址
*/
public function createPushFlowUrl($end_time)
{
$push_url = "/".$this->AppName."/".$this->StreamName."-".$end_time."-0-0-".$this->pushHostKey;
return "rtmp://".$this->pushHost."/".$this->AppName."/".$this->StreamName."?auth_key=".$end_time."-0-0-".md5($push_url);
}
/**
* 生成播流地址
*/
public function createPlayFlowUrl($end_time)
{
$play_rtmp_url = "/".$this->AppName."/".$this->StreamName."-".$end_time."-0-0-".$this->playHostKey;
$play_flv_url = "/".$this->AppName."/".$this->StreamName.".flv-".$end_time."-0-0-".$this->playHostKey;
$play_m3u8_url = "/".$this->AppName."/".$this->StreamName.".m3u8-".$end_time."-0-0-".$this->playHostKey;
return [
'RTMP'=>"rtmp://".$this->playHost."/".$this->AppName."/".$this->StreamName."?auth_key=".$end_time."-0-0-".md5($play_rtmp_url),
'FLV'=>"http://".$this->playHost."/".$this->AppName."/".$this->StreamName.".flv?auth_key=".$end_time."-0-0-".md5($play_flv_url),
'M3U8'=>"https://".$this->playHost."/".$this->AppName."/".$this->StreamName.".m3u8?auth_key=".$end_time."-0-0-".md5($play_m3u8_url),
];
}
/**
* 实时获取在线人数
*/
public function getMeanwhileLookNumber()
{
AlibabaCloud::accessKeyClient($this->accessKeyId, $this->accessSecret)
->regionId($this->regionId)
->asDefaultClient();
try {
$result = AlibabaCloud::rpc()
->product('live')
->version('2016-11-01')
->action('DescribeLiveStreamOnlineUserNum')
->method('POST')
->host('live.aliyuncs.com')
->options([
'query' => [
'RegionId' => $this->regionId,
'DomainName' => $this->playHost,
'AppName' => $this->AppName,
'StreamName' => $this->StreamName,
],
])->request();
$result = $result->toArray();
if(array_key_exists('code',$result)){
return false;
}else{
return $result['TotalUserNumber'];
}
} catch (ClientException $e) {
echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
echo $e->getErrorMessage() . PHP_EOL;
}
}
简单原理:
将阿里云推流地址配置到推溜设备上面,设备将视频流推送到直播平台,由直播平台分发到每个观看直播的用户端
注意事项:
目前只试过小程序对接,使用的小程序 live-push 组件,播放的是rtmp 播流地址,小程序需要开通经营类目->视频客服 功能