华为SparkRTC使用

125 阅读1分钟

官方文档:support.huaweicloud.com/rtc/index.h…

本次是使用web方式

<!DOCTYPE html>
<html lang='zh-cn'>

<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="renderer" content="webkit">
    <title>SparkRTC</title>
    

</head>

<body>
    <div id="video_view_1" style="width: 300px;height: 300px;"></div>
    <div id="video_view_2" style="width: 300px;height: 300px;"></div>
    <script src="./jquery.min.js"></script>
    <script src='./sdk/package/lib/hrtc.js'></script>
    <script>
        console.log(HRTC.VERSION)

        async function isBrowserSupport() {
            let check = false
            try {
                check = await HRTC.checkSystemRequirements()
                console.warn('browser isSupport: ' + check)
            } catch (error) {
                console.error(`check browser isSupport error: ${error.getCode()} - ${error.getMsg()}`)
                if (error.getCode() !== 90100025) {
                    console.error(`browser Support part ability of RTC`)
                    check = true
                }
            }
            return check
        };

        isBrowserSupport();

        //在控制台获取
        let config = { appId: '123', domain: '456', countryCode: 'CN' }
        let client = HRTC.createClient(config)

        console.log("==1==", client);

        //设置鉴权时间
        let timestamp = new Date().getTime();//当前的时间戳
        timestamp = timestamp + 12 * 60 * 60 * 1000;
        timestamp = Math.floor(timestamp/1000);

        //roomId唯一,相同的roomId在同一个房间
        var userId = 'u1', roomId = 'r1', ctime = timestamp + '', userName = 'u1', role = 0, signature = null;

        async function joinRoom() {
            try {
				
                let option = { userId: userId, userName: userName, signature: signature, ctime: ctime, role: role }
                console.log('option', option);
                await client.join(roomId, option)
                console.log("==3==" + 'join room success');               
                
                createStream();
            } catch (error) {
                console.log('join room fail', error)
            }
        }


        function createStream(){
            try {
                let stream = HRTC.createStream({ audio:true,video:true });

                console.log("==4==", stream);

                stream.initialize().then(() => {
                //stream.addResolution('90p_1')  //可选,如果要开启双流可以添加另外一个分辨率的视频
                    
                    stream.play('video_view_1', { muted:false })  //播放本地流
                    console.log("==5==", stream);
                    client.publish(stream);
                });
            } catch (error) {
                console.log('createStream fail', error)
            }
        }

        $.ajax({
            async: false,
            url: 'http://localhost/ha-api/ha-case-service-provider/ha/hw/getSig',
            type: 'POST',
            data: JSON.stringify({ "identifier": userId, "room": roomId, "expire": ctime }),
            dataType: 'json',
			contentType: "application/json",
            timeout: 4000,
            success: function (resp) {
                console.log("==2==", resp);
                signature = resp.data;
                joinRoom();
            },
            error: function (error) {
                console.log(error);
            }
        });

        

        client.on('stream-added', (event) => {
            const stream = event.stream
            client.subscribe(stream,{ video:true, audio:true })
        })

        client.on('stream-subscribed', (event) => {
            const stream = event.stream
            //muthed:true 没有声音
            stream.play('video_view_2', { objectFit: 'contain', muted: false })
            console.log("==6==", stream);
        })

    </script>
</body>

</html>