WebRtc10: 端对端1v1传输基本流程

媒体能力协商过程

RTCPeerConnection(核心类)

  • 基本格式
    pc = new RTCPeerConnection([configiration]);

RTCPeerConnection方法分类

  • 媒体协商
  • Stream/Track
  • 传输相关方法
  • 统计相关方法

媒体协商过程

协商状态变化

媒体协商方法

  • createOffer
  • createAnswer
  • setLocalDescription
  • setRemoeteDescription

createOffer

  • 基本格式
    aPromise = myPeerConnection.createOffer([options]);

createAnswer

  • 基本格式
    aPromise = myPeerConnection.createAnswer([options]);

setLocalDescription

  • 基本格式
    aPromise = myPc.setLocalDescription(sessionDescription);

setRemoeteDescription

  • 基本格式
    aPromise = myPc.setRemoeteDescription(sessionDescription);

Track方法

  • addTrack
  • removeTrack

addTrack

  • 基本格式
    rtpSender = myPc.addTrack(track, stream...);
  • 参数
    track: 添加到RTCPeerConnection中的媒体轨
    stream:指定track所在的stream

removeTrack

  • 基本格式
    myPc.removeTrack(rtpSender);

重要事件

  • onnegotiationneeded
  • onicecandidate

1:1连接的基本流程

实战:本机内的1:1音视频互通

index.html

html 复制代码
<html>
    <head>
        <title>WebRTC PeerConnection</title>
    </head>
    <body>
        <div>
            <video id="localvideo" autoplay playsinline></video>
            <video id="remotevideo" autoplay playsinline></video>
            <div>
                <button id="start">Start</button>
                <button id="call">Call</button>
                <button id="hangup">HangUp</button>
            </div>
        </div>

        <script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
        <script src="js/main.js"></script>
    </body>
</html>

main.js

js 复制代码
'use strict'

var localVideo = document.querySelector('video#localVideo');
var remoteVideo = document.querySelector('video#remoteVideo');
var btnStart = document.querySelector('button#start');
var btnCall = document.querySelector('button#call');
var btnHangUp= document.querySelector('button#hangup');


var localStream;
var pc1;
var pc2;

function gotMediaStream(stream){
	localVideo.srcObject = stream;
	localStream = stream;
}

function handleError(err){
	console.log("Failed to call getUserMedia", err);
}

function start(){
	var constraints = {
		video: true,
		audio: false 
	}

	if(!navigator.mediaDevices ||
		!navigator.mediaDevices.getUserMedia){
		return;
	}else {
		navigator.mediaDevices.getUserMedia(constraints)
					.then(gotMediaStream)
					.catch(handleError);
	}

}

function gotAnswerDescription(desc){
	pc2.setLocalDescription(desc);

	//send sdp to caller
	//recieve sdp from callee
	
	pc1.setRemoteDescription(desc);

}

function gotLocalDescription(desc){
	pc1.setLocalDescription(desc);

	//send sdp to callee
	
	//receive sdp from caller 
 	pc2.setRemoteDescription(desc);	
	pc2.createAnswer().then(gotAnswerDescription)
			 .catch(handleError);
}

function gotRemoteStream(e){

	if(remoteVideo.srcObject !== e.streams[0]){
		remoteVideo.srcObject = e.streams[0];
	}
}

function call(){
	var offerOptions = {
		offerToReceiveAudio: 0,
		offerToReceiveVideo: 1 
	}

	pc1 = new RTCPeerConnection();
	pc1.onicecandidate = (e) => {
	
		// send candidate to peer
		// receive candidate from peer

		pc2.addIceCandidate(e.candidate)
			.catch(handleError);
		console.log('pc1 ICE candidate:', e.candidate);
	}

	pc1.iceconnectionstatechange = (e) => {
		console.log(`pc1 ICE state: ${pc.iceConnectionState}`);
		console.log('ICE state change event: ', e);
	}


	pc2 = new RTCPeerConnection();
	pc2.onicecandidate = (e)=> {
	
		// send candidate to peer
		// receive candidate from peer

		pc1.addIceCandidate(e.candidate)
			.catch(handleError);
		console.log('pc2 ICE candidate:', e.candidate);
	}

	pc2.iceconnectionstatechange = (e) => {
		console.log(`pc2 ICE state: ${pc.iceConnectionState}`);
		console.log('ICE state change event: ', e);
	}

	pc2.ontrack = gotRemoteStream;

	//add Stream to caller
	localStream.getTracks().forEach((track)=>{
		pc1.addTrack(track, localStream);
	});

	pc1.createOffer(offerOptions)
		.then(gotLocalDescription)
		.catch(handleError);

}


function hangup(){
	pc1.close();
	pc2.close();
	pc1 = null;
	pc2 = null;

}

btnStart.onclick = start;
btnCall.onclick = call;
btnHangUp.onclick = hangup;
相关推荐
小白学大数据1 小时前
Python 多线程爬取社交媒体品牌反馈数据
开发语言·python·媒体
std78791 天前
微软 11 月可选更新修复 Win11 任务栏崩溃和游戏卡顿两大顽疾
科技·媒体
说私域1 天前
社群媒体时代下“开源AI智能名片链动2+1模式S2B2C商城小程序”对社群运营的重要性研究
人工智能·开源·媒体
Yeats_Liao2 天前
CANN Samples(十一):媒体处理接口V1与V2的取舍与迁移
人工智能·媒体
XIAOYU6720132 天前
2026中专生可以考的计算机技能证书有哪些?
媒体
小贤编程手记4 天前
从曝光到转化:全域覆盖资源领域,媒体发布平台哪家好?
经验分享·媒体
suki_lynn4 天前
星界云手机:自媒体运营的效率提升
智能手机·媒体
CIb0la5 天前
Google 将用 Aluminium OS 取代 ChromeOS
运维·生活·媒体
GIOTTO情5 天前
技术深度拆解:Infoseek 媒体发布系统的分布式架构与自动化实现
分布式·架构·媒体
~~李木子~~7 天前
中文社交媒体情感分析实战:基于B站评论的机器学习与深度学习对比
深度学习·机器学习·媒体