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;
相关推荐
机智的投媒网10 小时前
内容、媒体、时间:详解影响软文收录的三大变量及优化策略
媒体·内容运营
小雨青年14 小时前
鸿蒙 HarmonyOS 6 | 系统能力 (04):构建专业级媒体应用 PhotoAccessHelper 与复杂媒体库管理
华为·harmonyos·媒体
九章-16 小时前
呼伦贝尔融媒体数据库国产化替换成功案例:筑牢宣传阵地安全底座,金仓KES助力云雀系统高效运转
数据库·安全·媒体
KIHU快狐2 天前
快狐 | 5u 工控一体机标准机架 工业机柜安装适配性强
媒体
纽格立科技2 天前
数字广播内容服务器NGA-101 DRM媒体编码器
网络·音视频·信息与通信·传媒·媒体
aitoolhub3 天前
自媒体视觉物料高效创作新路径:稿定设计如何用AI重构内容生产逻辑
大数据·人工智能·aigc·媒体
终端域名3 天前
初步认识移动互联网:从终端变革到社交与媒体的全新生态
物联网·区块链·媒体
funfan05174 天前
【开源】多平台自媒体发布工具MediaPublishPlatform:一键发布到小红书、抖音、Tiktok等9大平台
开源·媒体
shizhenshide4 天前
社交媒体自动化:批量注册/发帖的验证码难题一站式解决
自动化·php·媒体·验证码·ezcaptcha
Pocker_Spades_A7 天前
【IEEE出版】第二届人工智能、数字媒体技术与社会计算国际学术会议(ICAIDS 2026)
人工智能·媒体