其他分享
首页 > 其他分享> > 4┃音视频直播系统之浏览器中通过 WebRTC 进行桌面共享

4┃音视频直播系统之浏览器中通过 WebRTC 进行桌面共享

作者:互联网

一、共享桌面原理

 

二、抓取桌面

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>share desktop by WebRTC</title>
</head>

<body>
    <button onclick="shareDesktop()">抓取桌面</button>
</body>
<script>
    // 抓取桌面
    function shareDesktop() {
        // 只有在 PC 下才能抓取桌面
        if (IsPC()) {
            // 开始捕获桌面数据
            navigator.mediaDevices.getDisplayMedia({ video: true })
                .then(getDeskStream)
                .catch(handleError);
            return true;
        }
        return false;
    }

    // 得到桌面数据流
    function getDeskStream(stream) {
        localStream = stream;
    }

    // 判断是否是PC
    function IsPC() {
        var userAgentInfo = navigator.userAgent;
        var Agents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad', 'iPod'];
        var flag = true;
        for (var v = 0; v < Agents.length; v++) {
            if (userAgentInfo.indexOf(Agents[v]) > 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }
</script>

</html>

 

 

三、桌面展示

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>share desktop by WebRTC</title>
</head>

<body>
    <video autoplay playsinline id="deskVideo"></video>
    <button onclick="shareDesktop()">抓取桌面</button>
</body>
<script>
    var deskVideo = document.querySelect("video/deskVideo");

    // 抓取桌面
    function shareDesktop() {
        // 只有在 PC 下才能抓取桌面
        if (IsPC()) {
            // 开始捕获桌面数据
            navigator.mediaDevices.getDisplayMedia({ video: true })
                .then(getDeskStream)
                .catch(handleError);
            return true;
        }
        return false;
    }

    // 得到桌面数据流并播放
    function getDeskStream(stream) {
        localStream = stream;
        deskVideo.srcObject = stream;
    }

    // 判断是否是PC
    function IsPC() {
        var userAgentInfo = navigator.userAgent;
        var Agents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad', 'iPod'];
        var flag = true;
        for (var v = 0; v < Agents.length; v++) {
            if (userAgentInfo.indexOf(Agents[v]) > 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }
</script>

</html>

 

四、录制桌面视频

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>share desktop by WebRTC</title>
</head>

<body>
    <button onclick="startRecord()">开始录制</button>
</body>
<script>
    var buffer;
    
    function startRecord() {
        // 定义一个数组,用于缓存桌面数据,最终将数据存储到文件中
        buffer = [];

        var options = {
            mimeType: 'video/webm;codecs=vp8'
        }

        if (!MediaRecorder.isTypeSupported(options.mimeType)) {
            console.error(`${options.mimeType} is not supported!`);
            return;
        }

        try {
            // 创建录制对象,用于将桌面数据录制下来
            mediaRecorder = new MediaRecorder(localStream, options);
        } catch (e) {
            console.error('Failed to create MediaRecorder:', e);
            return;
        }
        // 当捕获到桌面数据后,该事件触发
        mediaRecorder.ondataavailable = handleDataAvailable;
        mediaRecorder.start(10);
    }

    function handleDataAvailable(e) {
        if (e && e.data && e.data.size > 0) {
            buffer.push(e.data);
        }
    }
</script>

</html>

 

标签:function,桌面,抓取,音视频,return,var,浏览器,共享,WebRTC
来源: https://www.cnblogs.com/autofelix/p/16265556.html