我将使用以下JavaScript代码进行WEBRTC(AIortc)通信:

var port = 8080
var ip_address = "192.168.1.10"
var main_pc = {
    "name":"",
    "surname":"",
    "pc":null,
    "dc":null,
    "uid":null,
    "local_audio":null,
    "local_video":null,
    "remote_audio":null,
    "remote_video":null
};
var peer_connections = [];
var closing = false
var controller = null;
var signal;
var stop_time_out = null;

function start(name,surname) {
    $("#control_call_button").addClass("d-none");
    $("#stop_call_button").removeClass("d-none");
    
    $("#signal-audio").trigger("play");
    main_pc = createPeerConnection(main_pc);
    main_pc["name"] = name;
    main_pc["surname"] = surname;
    
    main_pc["dc"] = main_pc["pc"].createDataChannel('chat', {"ordered": true});
    
    main_pc["dc"].onmessage = function(evt) {
        data = JSON.parse(evt.data);
        if(data["type"] == "closing"){
            if (main_pc["uid"] == "uid"){   
                stop_peer_connection();
            }else{
                //stop_client_peer_connection(data["uid"]);
            }
        }
        
        if (data["type"] == "uid"){
            uid = data["uid"];
            main_pc["uid"] = uid;
            console.log(main_pc);
        }
        
        if (data["type"] == "new-client"){
            var uid = data["uid"];
            var client_name = data["name"];
            var client_surname = data["surname"];
            console.log("New client:");
            console.log(uid);
            console.log(client_name);
            console.log(client_surname);
            //start_client(uid,client_name,client_surname);
        }
        
    };

    main_pc["pc"].onconnectionstatechange = (event) => {
       let newCS = main_pc["pc"].connectionState;
       if (newCS == "disconnected" || newCS == "failed" || newCS == "closed") {
            stop_time_out = setTimeout(stop_with_time_out, 7000);   
       }else{
            if (stop_time_out != null){
                clearTimeout(stop_time_out);
                stop_time_out = null;
            }
       }
    }

    
    main_pc["pc"].onclose = function() {
        closing = true;
        stop_peer_connection();
        
        // close data channel
        if (main_pc["dc"]) {
            main_pc["dc"].close();
        }

        // close local audio / video
        main_pc["pc"].getSenders().forEach(function(sender) {
            sender.track.stop();
        });

        // close transceivers
        if (main_pc["pc"].getTransceivers) {
            main_pc["pc"].getTransceivers().forEach(function(transceiver) {
                if (transceiver.stop) {
                    transceiver.stop();
                }
            });
        }
        main_pc["pc"] = null;
        $("#control_call_button").removeClass("d-none");
        $("#stop_call_button").addClass("d-none");
    };
    
    constraints = {audio:true,video:true};
    
    navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
        stream.getTracks().forEach(function(track) {
            
            try {
                main_pc["pc"].addTrack(track, stream);
                if (track.kind == "video"){
                    //correct
                    main_pc["local_video"] = stream;
                    document.getElementById('client-video-1').srcObject = stream;
                }else{
                    main_pc["local_audio"] = stream;
                }
            } catch(e){
            }
        });
        return negotiate();
        }, function(err) {
            alert('Could not acquire media: ' + err);
    });
    
    
}

function createPeerConnection(pc) {
    var config = {
        sdpSemantics: 'unified-plan'
    };
    config.iceServers = [{ urls: ['stun:stun.l.google.com:19302'] }];

    pc["pc"] = new RTCPeerConnection(config);

    // connect audio
    pc["pc"].addEventListener('track', function(evt) {
        if (evt.track.kind == 'audio'){
            $("#signal-audio").trigger("pause");
            $("#signal-audio").currentTime = 0; // Reset time
            document.getElementById('server-audio').srcObject = evt.streams[0];
            
            $("#control_call_button").addClass("d-none")
            $("#stop_call_button").removeClass("d-none")
            
            pc["remote_audio"] = evt.streams[0];
        }else if (evt.track.kind == 'video'){
            document.getElementById('server-video').srcObject = evt.streams[0];
            pc["remote_video"] = evt.streams[0];
        }
    });
    
    return pc;
}

function negotiate() {
    return main_pc["pc"].createOffer({"offerToReceiveAudio":true,"offerToReceiveVideo":true}).then(function(offer) {
        return main_pc["pc"].setLocalDescription(offer);
    }).then(function() {
        // wait for ICE gathering to complete
        return new Promise(function(resolve) {
            if (main_pc["pc"].iceGatheringState === 'complete') {
                resolve();
            } else {
                function checkState() {
                    if (main_pc["pc"].iceGatheringState === 'complete') {
                        main_pc["pc"].removeEventListener('icegatheringstatechange', checkState);
                        resolve();
                    }
                }
                main_pc["pc"].addEventListener('icegatheringstatechange', checkState);

            }
        });
    }).then(function() {
        var offer = main_pc["pc"].localDescription;
        controller = new AbortController();
        signal = controller.signal;
        try{
            promise = timeoutPromise(60000, fetch('http://'+ip_address+':'+port+'/offer', {
                body: JSON.stringify({
                    sdp: offer.sdp,
                    type: offer.type,
                    "name":name,
                    "surname":surname
                }),
                headers: {
                    'Content-Type': 'application/json'
                },
                method: 'POST',
                signal
            }));
            return promise;
        }catch (error){
            console.log(error);
            stop_peer_connection();
        }
    }).then(function(response) {
        if (response.ok){
            return response.json();
        }else{
            stop_peer_connection();
        }
    }).then(function(answer) {
        console.log(answer);
        if (answer.sdp == "" && answer.type == ""){
            stop_peer_connection();
            return null;
        }else{
            return main_pc["pc"].setRemoteDescription(answer);
        }
    }).catch(function(e) {
        console.log(e);
        stop_peer_connection();
        return null;
    });
    
}

function timeoutPromise(ms, promise) {
  return new Promise((resolve, reject) => {
    const timeoutId = setTimeout(() => {
      reject(new Error("promise timeout"))
    }, ms);
    promise.then(
      (res) => {
        clearTimeout(timeoutId);
        resolve(res);
      },
      (err) => {
        clearTimeout(timeoutId);
        reject(err);
      }
    );
  })
}

function stop_peer_connection(dc_message=true) {
    $("#signal-audio").trigger("pause");
    $("#signal-audio").currentTime = 0; // Reset time       
    // send disconnect message because iceconnectionstate slow to go in failed or in closed state
    try{
        if (main_pc["dc"].readyState == "open"){
            if (dc_message){
                main_pc["dc"].send(JSON.stringify({"type":"disconnected"}));
            }
        }
    }catch (e){
    }
    try{
        if (main_pc["local_audio"] != null){
            main_pc["local_audio"].stop();
            main_pc["local_video"].stop();
            main_pc["local_audio"] = null;
            main_pc["local_video"] = null;
            
            //main_pc["remote_audio"].stop();
            //main_pc["remote_video"].stop();
            //main_pc["remote_audio"] = null;
            //main_pc["remote_video"] = null;
        }
    }
    catch (e){
    }
    
    
    
    document.getElementById('client-video-1').srcObject = null;
    document.getElementById('server-video').srcObject = null;
    document.getElementById('server-audio').srcObject = null;
    document.getElementById('client-audio-2').srcObject = null;
    document.getElementById('client-video-2').srcObject = null;
    document.getElementById('client-audio-3').srcObject = null;
    document.getElementById('client-video-3').srcObject = null;

    try{
        if (controller != null){
            controller.abort();
        }
        if (main_pc["dc"].readyState != "open"){
            main_pc["pc"].close();
        }
    }catch (e){
    }
    $("#control_call_button").removeClass("d-none")
    $("#stop_call_button").addClass("d-none")
}

function stop_with_time_out(){
    stop_peer_connection(false);
    stop_time_out = null;
}

$(document).ready(function(){
    $("#control_call_button").on( "click", function() {
        name = $("#name").val();
        surname = $("#surname").val();
        $("#me-name").html(name+" "+surname)
        closing = false;
        controller = null;
        start(name,surname);
    });
    $("#stop_call_button").on( "click", function() {
        closing = true;
        stop_peer_connection();
    });
})

我只是想当连接关闭的相机LED也将关闭. 有什么我能做的吗?

推荐答案

您正在try 在一个MediaStream对象上调用stop()方法,例如main_pc["local_audio"],这不再是您应该做的方式.

main_pc["local_audio"].getTracks().forEach(track => track.stop())

一旦所有本地曲目停止,摄像头灯将熄灭,您需要再次调用getUserMedia.

Javascript相关问答推荐

为什么从liveWire info js代码传递数组我出现错误?

如何访问Json返回的ASP.NET Core 6中的导航图像属性

在页面上滚动 timeshift 动垂直滚动条

GrapeJS -如何保存和加载自定义页面

配置WebAssembly/Emscripten本地生成问题

自定义高图中的x轴标签序列

在我的html表单中的用户输入没有被传送到我的google表单中

您能在卸载程序(QtInsteller框架)上添加WizardPage吗?

如何迭代叔父元素的子HTML元素

类构造函数不能在没有用With Router包装的情况下调用

在不删除代码的情况下禁用Java弹出功能WordPress

我想将Sitecore搜索面过滤器从多个转换为单个

为什么当我更新数据库时,我的所有组件都重新呈现?

JS Animate()方法未按预期工作

无法检索与Puppeteer的蒸汽游戏的Bundle 包价格

处理app.param()中的多个参数

如何压缩图像并将其编码为文本?

连续添加promise 时,如何在所有promise 都已结算时解除加载覆盖

从异步操作返回对象

在点击链接后重定向至url之前暂停