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;
相关推荐
williamdsy4 天前
【Element-Plus】媒体预览模态框优化实战:从复杂到简洁的设计之路
vue·媒体·element-plus
西柚小萌新5 天前
【前端:Html】--4.进阶:媒体
前端·html·媒体
安卓开发者5 天前
鸿蒙Next媒体展示组件实战:Video与动态布局全解析
华为·harmonyos·媒体
云手机掌柜13 天前
海外媒体引流进阶:指纹手机的全维度技术支持与实践应用
智能手机·媒体
一只小风华~14 天前
CSS @media 媒体查询
前端·css·媒体
wanhengidc15 天前
造成云手机闪退的原因有哪些?
服务器·网络·安全·智能手机·媒体
眠りたいです19 天前
Qt音频播放器项目实践:文件过滤、元数据提取与动态歌词显示实现
c++·qt·ui·音视频·媒体·qt5·mime
Byte_Me20 天前
突发新闻 1 小时过时?热更新技术如何让融媒体 App “边播边更”?
媒体
兰亭妙微22 天前
界面设计风格解析 | ABB 3D社交媒体视觉效果设计
3d·媒体
我要学习别拦我~25 天前
读《精益数据分析》:媒体内容平台全链路梳理
大数据·数据分析·媒体