WebRTC音视频开发读书笔记(六)

devtools/2024/9/19 12:01:00/ 标签: webrtc

数据通道不仅可以发送文本消息, 还可以发送图片、二进制文件,将其类型binaryType属性设置成arraybuffer类型即可.

九\、文件传输

1、文件传输流程

(1)使用表单file打开本地文件

(2)使用FileReader读取文件的二进制数据

 (3)创建对等连接,本地连接及远端连接

  (4)创建发送数据通道

  (5)创建接收数据通道

   (6)将读取的二进制数据 切割成一个个切片,然后使用数据通道的send方法发送数据 。

  (7)使用接收数据通道二进制数据 将其放入数据缓存

  (8)根据数据缓存生成Blob 文件

   (10)生成文件连接并提供下载。

读取流程中文件二进制数据使用FileReader对象,还需要对数据进行切片处理,在接收方法中使用缓存将接收的数据缓存起来,最后生成blob下载文件。处理流程如下所示:

2、FileReader

FileReader对象允许Web应用程序异步读取存储在用户计算机上的文件内容。常用事件如下所示:

使用File或Blob对象指定要读取的文件或数据,当监听到load事件后,即可通过事件的结果属性拿到数据,然后使用发送通道的send方法将数据发送出去。代码如下:

sendChannel.send(e.target.result)

读取二进制数据可以使用FileReader的readAsArrayBuffer方法,结果为ArrayBuffer对象。还有其它处理方法,如下所示:

读取数据 时需要将数据分成一段一段的,然后发送,接收时再按顺序读取到数据缓存中,数据流的走向如图所示:

上图中数据块叫做Chunk,DataChannel每次发送的数据即为Chunk,  使用对象的slice方法按顺序切割 出数据块。如下面代码所示:

let slice=file.slice(offset,offset+chunksize)

假设chunksize的大小为16348,整个文件的数据块如下所示:

3、发送文件示例

本示例为文件具休步骤如下:

(1)首先在界面上添加input,类型为file,用于选择并读取文件,progress两个,分别用于发送及接收文件进度展示。

 (2)创建本地和远端连接,发送和接收数据通道,发送和接收通道的数据类型设置为arraybuffer。

  (3)建立本地和远端连接

  (4)实例化FileReader对象,并添加如下事件:

                      error:  读取文件错误

                     abort:  读取文件取消

                      load:  文件加载完成

         load事件表示数据已经准备好,可以进行切割发送,具体处理逻辑如下所示:

//监听load事件
fileReader.addEventListener('load',(e)=>{...//发送文件数据sendChannel.send(e.target.result);//偏移量offer+=e.target.result.byteLength;...//判断偏移量是否小于文件大小if(offer<file.size){//继续读取readSlice(offser)}
});//读取切片大小
let readSlice=(o)=>{...//开始切片let slice=file.slice(offset ,o+chunksize);//读取二进制数据  fileReader.readAsArrayBuffer(slice);}

   (5)数据接收处理,将收到第一个数据放入receiveBuffer缓存,处理逻辑如下所示:

//接收消息处理
onReceiveMessageCallback=(event)=>{
//将接收的数据添加到接收缓存里receiveBuffer.push(event.data);
//设置当前接收文件的大小receivedSize+=event.data.byteLength;...const file=fileInput.files[0];
//判断当前接收的文件大小是否等于文件的大小
if(receivedSize===file.size){//根据缓存生成Blob文件const received=new Blob(receiveBuffer)//将缓存数据设置为空receiveBuffer=[];...//创建下载文件对象及连接...}
}

完整代码如下:

import React from "react";
import { Button } from "antd";//本地连接对象
let localConnection;
//远端连接对象
let remoteConnection;
//发送通道
let sendChannel;
//接收通道
let receiveChannel;
//文件读取
let fileReader;
//接收数据缓存
let receiveBuffer = [];
//接收到的数据大小
let receivedSize = 0;
//文件选择
let fileInput;
//发送进度条
let sendProgress;
//接收进度条
let receiveProgress;/*** 数据通道发送文件示例*/
class DataChannelFile extends React.Component {componentDidMount() {sendProgress =document.getElementById('sendProgress') receiveProgress =document.getElementById('receiveProgress')fileInput = document.getElementById('fileInput');//监听change事件,判断文件是否选择fileInput.addEventListener('change', async () => {const file = fileInput.files[0];if (!file) {console.log('没有选择文件');} else {console.log('选择的文件是:' + file.name);}});}//建立对等连接并发送文件startSendFile = async () => {//创建RTCPeerConnection对象localConnection = new RTCPeerConnection();console.log('创建本地PeerConnection成功:localConnection');//监听返回的Candidate信息localConnection.addEventListener('icecandidate', this.onLocalIceCandidate);//实例化发送通道sendChannel = localConnection.createDataChannel('webrtc-datachannel');//数据类型为二进制sendChannel.binaryType = 'arraybuffer';//onopen事件监听sendChannel.addEventListener('open', this.onSendChannelStateChange);//onclose事件监听sendChannel.addEventListener('close', this.onSendChannelStateChange);//创建RTCPeerConnection对象remoteConnection = new RTCPeerConnection();console.log('创建本地PeerConnection成功:remoteConnection');//监听返回的Candidate信息remoteConnection.addEventListener('icecandidate', this.onRemoteIceCandidate);//远端连接数据到达事件监听remoteConnection.addEventListener('datachannel', this.receiveChannelCallback);//监听ICE状态变化localConnection.addEventListener('iceconnectionstatechange', this.onLocalIceStateChange);//监听ICE状态变化remoteConnection.addEventListener('iceconnectionstatechange', this.onRemoteIceStateChange);try {console.log('localConnection创建提议Offer开始');//创建提议Offerconst offer = await localConnection.createOffer();//创建Offer成功await this.onCreateOfferSuccess(offer);} catch (e) {//创建Offer失败this.onCreateSessionDescriptionError(e);}}//创建会话描述错误onCreateSessionDescriptionError = (error) => {console.log(`创建会话描述SD错误: ${error.toString()}`);}//创建提议Offer成功onCreateOfferSuccess = async (desc) => {//localConnection创建Offer返回的SDP信息console.log(`localConnection创建Offer返回的SDP信息\n${desc.sdp}`);console.log('设置localConnection的本地描述start');try {//设置localConnection的本地描述await localConnection.setLocalDescription(desc);this.onSetLocalSuccess(localConnection);} catch (e) {this.onSetSessionDescriptionError();}console.log('remoteConnection开始设置远端描述');try {//设置remoteConnection的远端描述await remoteConnection.setRemoteDescription(desc);this.onSetRemoteSuccess(remoteConnection);} catch (e) {//创建会话描述错误this.onSetSessionDescriptionError();}console.log('remoteConnection开始创建应答Answer');try {//创建应答Answerconst answer = await remoteConnection.createAnswer();//创建应答成功await this.onCreateAnswerSuccess(answer);} catch (e) {//创建会话描述错误this.onCreateSessionDescriptionError(e);}}//设置本地描述完成onSetLocalSuccess = (pc) => {console.log(`${this.getName(pc)}设置本地描述完成:setLocalDescription`);}//设置远端描述完成onSetRemoteSuccess = (pc) => {console.log(`${this.getName(pc)}设置远端描述完成:setRemoteDescription`);}//设置描述SD错误onSetSessionDescriptionError = (error) => {console.log(`设置描述SD错误: ${error.toString()}`);}getName = (pc) => {return (pc === localConnection) ? 'localConnection' : 'remoteConnection';}//创建应答成功onCreateAnswerSuccess = async (desc) => {//输出SDP信息console.log(`remoteConnection的应答Answer数据:\n${desc.sdp}`);console.log('remoteConnection设置本地描述开始:setLocalDescription');try {//设置remoteConnection的本地描述信息await remoteConnection.setLocalDescription(desc);this.onSetLocalSuccess(remoteConnection);} catch (e) {this.onSetSessionDescriptionError(e);}console.log('localConnection设置远端描述开始:setRemoteDescription');try {//设置localConnection的远端描述,即remoteConnection的应答信息await localConnection.setRemoteDescription(desc);this.onSetRemoteSuccess(localConnection);} catch (e) {this.onSetSessionDescriptionError(e);}}//Candidate事件回调方法onLocalIceCandidate = async (event) => {try {if (event.candidate) {//将会localConnection的Candidate添加至remoteConnection里await remoteConnection.addIceCandidate(event.candidate);this.onAddIceCandidateSuccess(remoteConnection);}} catch (e) {this.onAddIceCandidateError(remoteConnection, e);}console.log(`IceCandidate数据:\n${event.candidate ? event.candidate.candidate : '(null)'}`);}//Candidate事件回调方法onRemoteIceCandidate = async (event) => {try {if (event.candidate) {//将会remoteConnection的Candidate添加至localConnection里await localConnection.addIceCandidate(event.candidate);this.onAddIceCandidateSuccess(localConnection);}} catch (e) {this.onAddIceCandidateError(localConnection, e);}console.log(`IceCandidate数据:\n${event.candidate ? event.candidate.candidate : '(null)'}`);}//添加Candidate成功onAddIceCandidateSuccess = (pc) => {console.log(`${this.getName(pc)}添加IceCandidate成功`);}//添加Candidate失败onAddIceCandidateError = (pc, error) => {console.log(`${this.getName(pc)}添加IceCandidate失败: ${error.toString()}`);}//监听ICE状态变化事件回调方法onLocalIceStateChange = (event) => {console.log(`localConnection连接的ICE状态: ${localConnection.iceConnectionState}`);console.log('ICE状态改变事件: ', event);}//监听ICE状态变化事件回调方法onRemoteIceStateChange = (event) => {console.log(`remoteConnection连接的ICE状态: ${remoteConnection.iceConnectionState}`);console.log('ICE状态改变事件: ', event);}//关闭数据通道closeChannel = () => {console.log('关闭数据通道');sendChannel.close();if (receiveChannel) {receiveChannel.close();}//关闭localConnectionlocalConnection.close();//关闭remoteConnectionremoteConnection.close();//localConnection置为空localConnection = null;//remoteConnection置为空remoteConnection = null;}//发送数据sendData = () => {let file = fileInput.files[0];console.log(`文件是: ${[file.name, file.size, file.type].join(' ')}`);//设置发送进度条的最大值sendProgress.max = file.size;//设置接收进度条的最大值receiveProgress.max = file.size;//文件切片大小,即每次读取的文件大小let chunkSize = 16384;//实例化文件读取对象fileReader = new FileReader();//偏移量可用于表示进度let offset = 0;//监听error事件fileReader.addEventListener('error', (error) => {console.error('读取文件出错:', error)});//监听abort事件fileReader.addEventListener('abort', (event) => {console.log('读取文件取消:', event)});//监听load事件fileReader.addEventListener('load', (e) => {console.log('文件加载完成 ', e);//使用发送通道开始发送文件数据sendChannel.send(e.target.result);//使用文件二进制数据长度作为偏移量offset += e.target.result.byteLength;//使用偏移量作为发送进度sendProgress.value = offset;console.log('当前文件发送进度为:', offset);//判断偏移量是否小于文件大小if (offset < file.size) {//继续读取readSlice(offset);}});//读取切片大小let readSlice = (o) => {console.log('readSlice ', o);//将文件的某一段切割下来,从offset到offset + chunkSize位置切下let slice = file.slice(offset, o + chunkSize);//读取切片的二进制数据fileReader.readAsArrayBuffer(slice);};//首次读取0到chunkSize大小的切片数据readSlice(0);}//接收通道数据到达回调方法receiveChannelCallback = (event) => {//实例化接收通道receiveChannel = event.channel;//数据类型为二进制receiveChannel.binaryType = 'arraybuffer';//接收消息事件监听receiveChannel.onmessage = this.onReceiveMessageCallback;//onopen事件监听receiveChannel.onopen = this.onReceiveChannelStateChange;//onclose事件监听receiveChannel.onclose = this.onReceiveChannelStateChange;receivedSize = 0;}//接收消息处理onReceiveMessageCallback = (event) => {console.log(`接收的数据 ${event.data.byteLength}`);//将接收到的数据添加到接收缓存里receiveBuffer.push(event.data);//设置当前接收文件的大小receivedSize += event.data.byteLength;//使用接收文件的大小表示当前接收进度receiveProgress.value = receivedSize;const file = fileInput.files[0];//判断当前接收的文件大小是否等于文件的大小if (receivedSize === file.size) {//根据缓存数据生成Blob文件const received = new Blob(receiveBuffer);//将缓存数据置为空receiveBuffer = [];//获取下载连接对象let download = document.getElementById('download');//创建下载文件对象及链接download.href = URL.createObjectURL(received);download.download = file.name;download.textContent = `点击下载'${file.name}'(${file.size} bytes)`;download.style.display = 'block';}}//发送通道状态变化onSendChannelStateChange = () => {const readyState = sendChannel.readyState;console.log('发送通道状态: ' + readyState);if (readyState === 'open') {this.sendData();}}//接收通道状态变化onReceiveChannelStateChange = () => {const readyState = receiveChannel.readyState;console.log('接收通道状态:' + readyState);}//取消发送文件cancleSendFile = () => {if (fileReader && fileReader.readyState === 1) {console.log('取消读取文件');fileReader.abort();}}render() {return (<div className="container"><div><form id="fileInfo"><input type="file" id="fileInput" name="files" /></form><div><h2>发送</h2><progress id="sendProgress" max="0" value="0" style={{width:'500px'}}></progress></div><div><h2>接收</h2><progress id="receiveProgress" max="0" value="0" style={{width:'500px'}}></progress></div></div><a id="download"></a><div><Button onClick={this.startSendFile} style={{ marginRight: "10px" }}>发送</Button><Button onClick={this.cancleSendFile} style={{ marginRight: "10px" }}>取消</Button><Button onClick={this.closeChannel} style={{ marginRight: "10px" }}>关闭</Button></div></div>);}
}
//导出组件
export default DataChannelFile;


http://www.ppmy.cn/devtools/97978.html

相关文章

Java面试篇(多线程相关专题)

文章目录 0. 前言1. 线程基础1.1 线程和进程1.1.1 进程1.1.2 线程1.1.3 进程和线程的区别 1.2 并行和并发1.2.1 单核 CPU 的情况1.2.2 多核 CPU 的情况1.2.3 并行和并发的区别 1.3 线程创建的方式1.3.1 继承 Thread 类&#xff0c;重写 run 方法1.3.2 实现 Runnable 接口&#…

KeyShot 2024.2:卓越的Mac与Windows 3D渲染与动画制作软件

KeyShot 2024.2作为一款专为Mac和Windows用户设计的3D渲染与动画制作软件&#xff0c;凭借其出色的性能和丰富的功能&#xff0c;在业界树立了新的标杆。这款软件不仅继承了KeyShot系列一贯的实时渲染和动画优势&#xff0c;还在多个方面进行了全面升级和优化&#xff0c;为3D设…

安装MySQL入门基础指令

一.安装MySQL(以5.7版本为例) 1.一路默认安装&#xff0c;截图供大家参考 修改自己window安装名字即可 2.配置环境变量 C:\Program Files\MySQL\MySQL Server 5.7\bin 写入系统环境变量即可在window窗口使用其服务了 3.登录MySQL服务 进入控制台输入命令 mysql -u root …

【Python技术】使用同花顺wencai抓取、pandas分析涨停数据

最近A股市场不好&#xff0c;今天又是3000多只待涨&#xff0c; 市场一片哀鸿遍野。打开同花顺问财看了一眼&#xff0c;顿时索然无味。 原本没什么灵感&#xff0c; 突然有了写作思路&#xff0c;要不把wencai数据简单抓取分析下。之前写过一篇 【Python技术】使用akshare、pa…

js原型和原型链的概念

原型和原型链是JavaScript中重要的概念&#xff0c;理解它们对于理解如何构建对象以及继承特性非常关键。 原型&#xff1a; 在JavaScript中&#xff0c;每个函数都有一个特殊的属性叫做prototype。这个属性指向一个对象&#xff0c;这个对象作为原型&#xff0c;该函数所创建…

JAVA—IO流

存储数据的方案File和文件数据的操作IO流&#xff0c;学习字节流和字符流&#xff0c;了解框架和IO框架Commons IO 目录 1.File &#xff08;1&#xff09;创建对象 &#xff08;2&#xff09;常用方法 【1】判断类型&#xff0c;获取信息 【2】创建文件&#xff0c;删除…

colmap的几种相机类型和内外参取得方法

colmap的相机类型可以参考models.h文件。 主要有以下几种相机类型&#xff1a; SimplePinhole&#xff1a; 内参格式&#xff1a;f, cx, cy 实际用的时候&#xff1a;fxfyf Pinhole: 内参格式&#xff1a;fx, fy, cx, cy 其他可以自行查看models.h文件。 内参存放在images.b…

WaterCloud学习部署

简介 官网 WaterCloud是一套基于ASP.NET 6.0 MVC API SqlSugar LayUI的框架&#xff0c;源代码完全开源&#xff0c;可以帮助你解决C#.NET项目的重复工作&#xff01; 采用主流架构思想&#xff0c;容易上手&#xff0c;简单易学&#xff0c;学习成本低。 可完全实现二次开…

Java中的Stream API详解

Stream API是Java 8引入的重要特性&#xff0c;它提供了一种新的处理数据集合的方式&#xff0c;能够使代码更加简洁、表达力更强&#xff0c;并且更容易进行并行处理。本文将详细介绍Java中的Stream API&#xff0c;包括其基本概念、操作、性能考虑以及最佳实践等。 1. Strea…

Unity转Unreal5从入门到精通之UMG的使用

前言 UMG (Unreal Motion Graphics UI Designer)是Unreal种的可视化 UI 工具。它就类似于Unity中的UGUI,可用于为用户创建游戏内 HUD、菜单和其他与界面相关的图形。 UMG 的核心是UI控件。它可用于创建UI界面&#xff08;按钮、复选框、滑块、进度条等&#xff09;。 快速入…

Linux VSFTP 部署与配置

一、VSFTP 简介与应用 VSFTP&#xff08;Very Secure FTP Daemon&#xff09;是一款功能强大、安全可靠的FTP服务器软件&#xff0c;广泛应用于Linux/Unix系统中。它提供了高效的文件传输服务&#xff0c;并具备诸多安全特性&#xff0c;如用户认证、权限控制、SSL/TLS加密等。…

二叉树入门学习 优势对比 以及 完全二叉树c++代码的实现

二叉树介绍文档 一、概述 二叉树是一种常见的数据结构&#xff0c;它是一种树形结构&#xff0c;每个节点最多有两个子节点&#xff0c;通常称为左子节点和右子节点。二叉树的基本概念如下&#xff1a; 节点&#xff08;Node&#xff09;&#xff1a;二叉树的基本单元&#…

达梦数据库的系统视图v$db_cache

达梦数据库的系统视图v$db_cache 在达梦数据库&#xff08;DM Database&#xff09;中&#xff0c;V$DB_CACHE 是一个系统视图&#xff0c;提供了数据库缓存的相关信息。数据库缓存是数据库系统用来加速数据访问的关键组件&#xff0c;通过缓存常用数据来减少对磁盘操作的依赖…

python 可迭代对象,迭代器,生成器,装饰器

引言&#xff1a; 在Python中&#xff0c;理解可迭代对象、迭代器、生成器和装饰器的概念是非常重要的&#xff0c;这些概念构成了Python中处理数据和函数的高级特性。 可迭代对象 可迭代对象(Iterable)是指那些可以使用iter()函数来获取迭代器的对象。常见的可迭代对象包括…

SpringBoot项目多线程实现定时任务-只需要三步

众所周知&#xff0c;项目中需要使用定时任务发布的需求时非常常见的&#xff0c;例如&#xff1a;数据同步&#xff0c;清理垃圾文件&#xff0c;清理过期用户等需求&#xff0c;可能需要我们定时去清理数据。 但是我们如果集成xxl-job&#xff0c;Quartz&#xff0c;spring …

C# 使用泛型协变性

在 C# 中处理多个类型的生产者时&#xff0c;可以使用泛型接口结合协变性。以下是一个示例&#xff0c;展示如何实现一个支持多个类型的生产者。 协变性 using System; using System.Collections.Generic;public interface IProducer<out T> {T Produce(); }public cla…

linux下QOS:理论篇

关于qos &#xff0c;也是linux下面必备功能之一&#xff0c;一般只需要结合iptables/etables/iproute2 和tc配合即可实现大部分功能. 网上讲这么方面的资料很多&#xff0c;大部分都讲tc命令的应用.这里就先从理论入手. QoS&#xff08;Quality of Service&#xff09;服务质…

【ARM系统】基础知识总结

16_ARM_SYSTEM 文章目录 16_ARM_SYSTEM1.计算机硬件基础地址空间CPU执行指令的过程 2.ARM处理器概述ARM处理器指令集指令集类型指令集关键方面指令集的重要性 ARM工作模式 3.ARM寄存器组织通用寄存器专用寄存器控制寄存器 4.ARM异常处理常见异常源IRQ异常 5.ARM汇编指令集数据传…

【ARM Hypervisor And SMMU 系列 5 -- SMMU 和 IOMMU技术】

文章目录 SMMU 和 IOMMU技术ARM 的 SMMUTranslation process overviewTBU 和 TCU 的关系TBUTCUTLBSMMU 和 IOMMU技术 文章 讲到了为支持I/O透传机制中的DMA设备传输而引入的IOMMU/SMMU技术,同时留了一个问题:IOMMU/SMMU是否可以同时支持GVA->GPA和GPA->HPA的转换? 答案…

乘积小于 K 的子数组(LeetCode)

题目 给你一个整数数组 nums 和一个整数 k &#xff0c;请你返回子数组内所有元素的乘积严格小于 k 的连续子数组的数目。 解题 """ 时间复杂度&#xff1a;O(n)&#xff0c;其中 n 是数组的长度。每个元素最多被访问两次&#xff08;一次作为右端点&#xff0…