obs

声网和腾讯ob开播分享

发布于 2025-07-12 11:17:24

声网对接

需求:手机开播几个小时会发热卡顿,需要用电脑开播,obs加摄像头。

项目:天九阁

1、后台开播,确定obs开播的话,调用声网生成推流码的接口。其中入参推流码区域可选中国、除中国外亚洲地区、北美、欧中,这个跟后配配置的直播设置保持一致;频道用开播的流名,文档地址:https://doc.shengwang.cn/doc/rtmp-gateway/restful/rtmp-gateway/operations/post-region-v1-projects-appId-rtls-ingress-streamkeys

2、接口调用成功后,生成对应推流码,推流地址直接用声网地址:rtmp://rtls-ingress-prod-".$region.".agoramdn.com/live,其中的$region就是第一步选择的区域。如需用自己的域名联系声网商务。

3、对应数据存入数据库,推流地址和推流码返回给前端展示即可。app端用户加入对应频道即可看到直播画面

注意:1、卡顿可能是开播电脑网络问题,建议升级网速再看;2、推流码有有效期设置,最好设置长一点,过期再开播就会导致obs一直提示断开连接

代码:

function createCode($uid,$stream,$expire=7200){

        $configpri = getConfigPri();

        // 声网API参数

        $region = $configpri['sw_media_region']; // 根据实际区域填写

        $appId = $configpri['sw_app_id']; // 替换为你的声网AppId

        $customerKey = $configpri['sw_key_id'];

        $customerSecret = $configpri['sw_key_secret'];

        // 拼接客户 ID 和客户密钥

        $credentials = $customerKey . ":" . $customerSecret;

        // 使用 base64 进行编码

        $token = base64\_encode($credentials);

        if($region=='cn'){

            $sw_request_url='https://api.sd-rtn.com';

        }else{

            $sw_request_url='https://api.agora.io';

        }

        $url = "{$sw_request_url}/{$region}/v1/projects/{$appId}/rtls/ingress/streamkeys";

        $postData = ['settings' =>[

            'channel' => strval($stream),

            'uid' => strval($uid),

            'expiresAfter' => $expire

        ]];

        $headers = [

            'Content-Type: application/json',

            'Authorization: Basic ' . $token

        ];

        $ch = curl_init();

        curl_setopt($ch, CURLOPT\_URL, $url);

        curl_setopt($ch, CURLOPT_POST, 1);

        curl_setopt($ch, CURLOPT\_POSTFIELDS, json\_encode($postData));

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT\_HTTPHEADER, $headers);

        $result = curl\_exec($ch);

        $errno = curl\_errno($ch);

        $errmsg = curl\_error($ch);

        curl_close($ch);

        if ($errno) {

            return ['code'=>2, 'msg'=>'curl请求失败: ' . $errmsg, 'info'=>''];

        }

        $data = json\_decode($result, true);

        if (isset($data\['status'\]) && $data['status'] == 'success') {

            return ['code'=>0, 'msg'=>'success', 'info'=> $data['data']['streamKey']];

        } else {

            return ['code'=>3, 'msg'=>'声网API返回异常: ' . $result, 'info'=>''];

        }

    }

腾讯云对接

项目:pk直播

原生成直播的推流地址

推流地址是rtmp+后台配置的推流域名+live

推流码是:原直播地址对应的后半部分

代码:

function getPushInfo($stream)

{

$configpri=getConfigPri();

        $bizid = $configpri['tx_bizid'];

$push\_url\_key=$configpri['tx_push_key'];

$push=$configpri['tx_push'];

$pull=$configpri['tx_pull'];

$stream\_a=explode('.',$stream);

$streamKey = $stream_a[0];

$live\_code = $streamKey;

$now_time = time() + 3*60*60;

$txTime = dechex($now_time);

$txSecret = md5($push_url_key . $live\_code . $txTime);

$safe\_url = "?txSecret=" .$txSecret."&txTime=" .$txTime;

$url=array(

'cdn'=>"rtmp://{$push}/live",

'stream'=>$live\_code.$safe_url,

);

return $url;

}

0 条评论

发布
问题