前置条件:推流和播流地址,播流地址需要备案的域名才可使用,如果没有备案域名,可以让有备案域名的朋友建个二级域名来使用。
推流和播流:

推流和播流的key都在管理当中
代码:
private $config;
public function __construct()
{
$this->config = [
'PushDomain'=>'你的推流域名',//推流域名
'LiveDomain'=>'你的播流域名',//播流域名
'PushKey'=>'推流key',//推流key
'LiveKey'=>'播流key',//播流key
'time'=>Carbon::now()->addDay()
];
}
//推流
function PushUrl($name){
//查询直播间名称
$live=\App\Models\video\live::findLive($name);
if ($live) {
//dd($live);
return $this->getLiveUrl($this->config['PushDomain'], $live[0]['id'] . $live[0]['name'], $this->config['PushKey'], $this->config['time']);
}
$data= \App\Models\video\live::setLive(['name'=>$name]);
//dd($data);
return $this->getLiveUrl($this->config['PushDomain'],$data->id.$data->name,$this->config['key'],$this->config['time']);
}
//播流
function LiveUrl($name){
$live = \App\Models\video\live::findLive($name);
//dd($live);
return $this->getLiveUrl($this->config['LiveDomain'],$live[0]['id'].$live[0]['name'],$this->config['LiveKey'],$this->config['time']);
}
//推流&播流
private function getLiveUrl($domain, $streamName, $key = null, $time = null){
if($key && $time){
//base_convert() 函数在任意进制之间转换数字
//strtotime将任何字符串的日期时间描述解析为 Unix 时间戳
//strtoupper把所有字符转换为大写
$txTime = strtoupper(base_convert(strtotime($time),10,16));
$txSecret = md5($key.$streamName.$txTime);
$ext_str = "?".http_build_query(array(
"txSecret"=> $txSecret,
"txTime"=> $txTime
));
}
return "webrtc://".$domain."/live/".$streamName . (isset($ext_str) ? $ext_str : "");
}
我这边是通过直播间名称来查询直播是否存在

调用接口到页面来实现直播
开启直播

观看直播


1762

被折叠的 条评论
为什么被折叠?



