当前位置:首页 > 科技  > 软件

WebRTC.Net库开发进阶,教你实现屏幕共享和多路复用!

来源: 责编: 时间:2023-08-05 11:46:09 5392观看
导读WebRTC.Net库:让你的应用更亲民友好,实现视频通话无痛接入! 除了基本用法外,还有一些进阶用法可以更好地利用该库。自定义 STUN/TURN 服务器配置WebRTC.Net 默认使用 Google 的 STUN 服务器和 Coturn 的 TURN 服务器。如

HWa28资讯网——每日最新资讯28at.com

WebRTC.Net库:让你的应用更亲民友好,实现视频通话无痛接入! 除了基本用法外,还有一些进阶用法可以更好地利用该库。HWa28资讯网——每日最新资讯28at.com

自定义 STUN/TURN 服务器配置

WebRTC.Net 默认使用 Google 的 STUN 服务器和 Coturn 的 TURN 服务器。如果你需要使用其他 STUN/TURN 服务器,则可以在初始化 PeerConnectionFactory 和 PeerConnection 时设置自定义配置。HWa28资讯网——每日最新资讯28at.com

例如,以下代码设置了使用 coturn 服务器的 PeerConnectionFactory:HWa28资讯网——每日最新资讯28at.com

var config = new PeerConnectionConfiguration{   IceServers = new List<IceServer>   {      new IceServer{ Urls = new[] { "stun:stun.l.google.com:19302" }},      new IceServer{ Urls = new[] { "turn:my-turn-server.com" }, Username="myusername", Credential="mypassword" }   }};var factory = new PeerConnectionFactory(config);

在不同线程中创建和使用 PeerConnectionFactory 和 PeerConnection 对象:

WebRTC.Net 库本质上是基于线程的,因此它的对象通常在单独的线程中创建和使用。这样可以避免在主线程中对 UI 线程造成大量负担。HWa28资讯网——每日最新资讯28at.com

以下代码在后台线程中创建并使用 PeerConnection 对象HWa28资讯网——每日最新资讯28at.com

Task.Run(() =>{   var config = new PeerConnectionConfiguration { IceServers = new List<IceServer> { new IceServer { Urls = new[] { "stun:stun.l.google.com:19302" } } } };   var factory = new PeerConnectionFactory(config);   var pc = factory.CreatePeerConnection(config);      // 在这里使用 PeerConnection 对象,不会阻塞主线程}).Wait();

选择视频和音频设备

在创建 PeerConnectionFactory 对象时,可以设置 defaultAudioDevice 和 defaultVideoDevice 参数以选择默认的音频和视频设备。HWa28资讯网——每日最新资讯28at.com

例如,以下如何通过设备名称选择视频和音频设备:HWa28资讯网——每日最新资讯28at.com

var config = new PeerConnectionConfiguration{   IceServers = new List<IceServer> { new IceServer { Urls = new[] { "stun:stun.l.google.com:19302" } } },   DefaultVideoDevice = VideoCaptureDevice.GetDevices().FirstOrDefault(x => x.Name == "MyCameraName"),   DefaultAudioDevice = AudioCaptureDevice.GetDevices().FirstOrDefault(x => x.Name == "MyMicrophoneName")};var factory = new PeerConnectionFactory(config);

实现数据通道

WebRTC.Net 库不仅支持音视频传输,还支持实现数据通道(DataChannel)。使用数据通道,应用程序可以在客户端之间传输任意类型的数据,例如聊天消息、游戏状态等。HWa28资讯网——每日最新资讯28at.com

以下代码如何创建数据通道:HWa28资讯网——每日最新资讯28at.com

// 创建 PeerConnection 对象var config = new PeerConnectionConfiguration { IceServers = new List<IceServer> { new IceServer { Urls = new[] { "stun:stun.l.google.com:19302" } } } };var factory = new PeerConnectionFactory(config);var pc = factory.CreatePeerConnection(config);// 创建数据通道var dcConfig = new DataChannelInit { Ordered = true };var dc = pc.CreateDataChannel("mydatachannel", dcConfig);// 监听数据通道事件dc.MessageReceived += (sender, e) =>{   // 处理接收到的数据};

实现屏幕共享

除了音视频传输和数据通道,WebRTC.Net 还支持屏幕共享。这意味着应用程序可以捕获屏幕上的内容并将其共享给其他客户端。HWa28资讯网——每日最新资讯28at.com

以下是使用 WinForm 技术栈和 WebRTC.Net 库实现桌面共享的示例代码。HWa28资讯网——每日最新资讯28at.com

using System;using System.Drawing;using System.Threading.Tasks;using System.Windows.Forms;using Windows.Graphics.Capture;using Windows.Graphics.DirectX.Direct3D11;using Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT;using Org.WebRtc;namespace DesktopStreaming{    public partial class MainForm : Form    {        private PeerConnection _peerConnection;        private DataChannel _dataChannel;        private Direct3D11CaptureFramePool _framePool;        private GraphicsCaptureSession _session;        private VideoTrack _videoTrack;        public MainForm()        {            InitializeComponent();            // 初始化 WebRTC            WebRTC.Initialize(new WebRTCInitializationOptions { EnableAudioBufferLog = false });            // 创建 PeerConnectionFactory 对象            var config = new PeerConnectionConfiguration { IceServers = new[] { new IceServer { Urls = new[] { "stun:stun.l.google.com:19302" } } } };            var factory = new PeerConnectionFactory(config);            // 创建 PeerConnection 对象            _peerConnection = factory.CreatePeerConnection();            // 创建数据通道            _dataChannel = _peerConnection.CreateDataChannel("mychannel");            // 订阅数据通道的消息事件            _dataChannel.MessageReceived += (sender, args) =>            {                // 处理收到的消息            };            // 创建 Direct3D11CaptureFramePool 对象            var device = Direct3D11Helpers.CreateDevice();            var size = new Size(800, 600);            _framePool = Direct3D11CaptureFramePool.CreateFreeThreaded(                device,                Direct3DPixelFormat.B8G8R8A8UIntNormalized,                1,                size);            // 订阅 FrameArrived 事件            _framePool.FrameArrived += (sender, args) =>            {                // 获取最新的桌面帧                using var frame = sender.TryGetNextFrame();                if (frame == null) return;                // 将桌面帧转换为 RTCVideoFrame 对象                var videoFrame = new RTCVideoFrame(frame.ContentSize.Width, frame.ContentSize.Height, RTCVideoFrameType.RTCVideoFrameTypeI420);                videoFrame.ConvertFromArgb32(frame.Surface.Direct3D11Device, frame.Surface);                // 将 RTCVideoFrame 对象转换为 VideoTrack 对象并发送                if (_videoTrack != null)                    _videoTrack.PushFrame(videoFrame);            };            // 创建 GraphicsCaptureItem 对象            var item = ScreenCapture.GetDefault();            // 创建 GraphicsCaptureSession 对象            _session = _framePool.CreateCaptureSession(item);        }        private async void btnStart_Click(object sender, EventArgs e)        {            // 开始共享桌面            await _session.StartAsync();            // 创建视频轨道            _videoTrack = await PeerConnectionFactory.GetVideoTrackSourceAsync(_framePool);            // 添加视频轨道到 PeerConnection 对象            await _peerConnection.AddTrack(_videoTrack);            // 创建 Offer SDP 并设置本地描述符            var offerSdp = await _peerConnection.CreateOffer();            await _peerConnection.SetLocalDescription(offerSdp);            // 发送 Offer SDP 到远端            SendSdp(offerSdp);        }        private void SendSdp(RTCSessionDescription sdp)        {            // 将 SDP 转换为 JSON 格式并发送到远端            var json = Newtonsoft.Json.JsonConvert.SerializeObject(new { type = sdp.Type, sdp = sdp.Sdp });            _dataChannel.Send(json);        }        private async void MainForm_FormClosing(object sender, FormClosingEventArgs e)        {            // 关闭 PeerConnection 和 GraphicsCaptureSession 对象            await _peerConnection.CloseAsync();            _session.Dispose();        }    }}

上述代码中,我们使用了 ScreenCapture 类来获取默认的桌面捕获项目,然后创建了 GraphicsCaptureSession 对象来捕获桌面帧。我们还使用了
Direct3D11CaptureFramePool 类来创建一个 Direct3D 11 帧池,并订阅了 FrameArrived 事件以获取最新的桌面帧。在每次收到桌面帧时,我们将其转换为 RTCVideoFrame 对象,再将其发送到 WebRTC 连接中。通过这种方式,我们就实现了桌面共享的功能。
HWa28资讯网——每日最新资讯28at.com

需要注意的是,由于 WebRTC 是基于 p2p 的实时通信协议,因此本示例代码中仅演示了如何将桌面共享的数据发送给远端客户端,而没有涉及如何在远端客户端上解析和显示收到的数据。HWa28资讯网——每日最新资讯28at.com

处理 ICE 连接状态

WebRTC.Net 使用 ICE(Interactive Connectivity Establishment)协议来建立和维护客户端之间的连接。ICE 协议涉及多个状态和事件,例如 gathering、connected、disconnected 等等。应用程序可以订阅 PeerConnection 对象上的各种事件来处理这些状态。HWa28资讯网——每日最新资讯28at.com

以下代码如何订阅 PeerConnection 对象上的连接状态:HWa28资讯网——每日最新资讯28at.com

// 创建 PeerConnection 对象var config = new PeerConnectionConfiguration { IceServers = new List<IceServer> { new IceServer { Urls = new[] { "stun:stun.l.google.com:19302" } } } };var factory = new PeerConnectionFactory(config);var pc = factory.CreatePeerConnection(config);// 订阅 PeerConnection 对象上的连接状态pc.IceStateChanged += (sender, iceState) =>{   if (iceState == IceConnectionState.Connected)   {      // 客户端已成功连接   }   else if (iceState == IceConnectionState.Disconnected)   {      // 客户端已断开连接   }};

实现多路复用

WebRTC.Net 支持实现多路复用(Multiplexing),这意味着应用程序可以在同一个数据通道上同时传输多种类型的数据,例如音频、视频、文件等。HWa28资讯网——每日最新资讯28at.com

下面是使用 WinForm 技术栈和 WebRTC.Net 库实现多路复用的示例代码。HWa28资讯网——每日最新资讯28at.com

Copy Codeusing System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Windows.Forms;using Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT;using Org.WebRtc;namespace WebRTC_Multiplexing{    public partial class Form1 : Form    {        private PeerConnection _peerConnection;        private List<DataChannel> _dataChannels = new List<DataChannel>();        public Form1()        {            InitializeComponent();            // 初始化 WebRTC            WebRTC.Initialize(new WebRTCInitializationOptions { EnableAudioBufferLog = false });            // 创建 PeerConnectionFactory 对象            var config = new PeerConnectionConfiguration { IceServers = new[] { new IceServer { Urls = new[] { "stun:stun.l.google.com:19302" } } } };            var factory = new PeerConnectionFactory(config);            // 创建 PeerConnection 对象            _peerConnection = factory.CreatePeerConnection();            // 订阅 PeerConnection 的连接状态改变事件            _peerConnection.ConnectionStateChanged += (sender, args) =>            {                // 处理连接状态改变事件                BeginInvoke(new Action(() => txtOutput.AppendText($"连接状态:{args.NewState.ToString()}/r/n")));            };            // 订阅 PeerConnection 的数据通道回调事件            _peerConnection.DataChannelAdded += (sender, args) =>            {                // 处理数据通道回调事件                var dataChannel = args.Channel;                dataChannel.MessageReceived += DataChannel_MessageReceived;                _dataChannels.Add(dataChannel);                BeginInvoke(new Action(() => txtOutput.AppendText($"收到数据通道:{dataChannel.Label}/r/n")));            };        }        private async void btnCreateOffer_Click(object sender, EventArgs e)        {            // 创建 Offer SDP 并设置本地描述符            var offerSdp = await _peerConnection.CreateOffer();            await _peerConnection.SetLocalDescription(offerSdp);            // 发送 Offer SDP 到对端            SendSdp(offerSdp);        }        private void SendSdp(RTCSessionDescription sdp)        {            // 将 SDP 转换为 JSON 格式并发送到对端            var json = Newtonsoft.Json.JsonConvert.SerializeObject(new { type = sdp.Type, sdp = sdp.Sdp });            _dataChannels.ForEach(dc => dc.Send(json));        }        private async void DataChannel_MessageReceived(object sender, DataChannelMessageEventArgs e)        {            // 收到数据通道消息后将其转换为 RTCSessionDescription 对象            if (e.MessageType == DataMessageType.Text)            {                var text = e.Data;                var sdp = Newtonsoft.Json.JsonConvert.DeserializeObject<RTCSessionDescription>(text);                // 设置远端描述符并完成连接                await _peerConnection.SetRemoteDescription(sdp);                if (sdp.Type == RTCSessionDescriptionType.Offer) await _peerConnection.CreateAnswer();            }        }    }}

上述代码中,我们创建了一个 PeerConnectionFactory 对象和一个 PeerConnection 对象,用于建立 WebRTC 连接。我们还创建了一个 _dataChannels 列表来保存所有的数据通道对象,每当 PeerConnection 对象添加一个新的数据通道时,我们就将其添加到 _dataChannels 列表中。HWa28资讯网——每日最新资讯28at.com

在 btnCreateOffer_Click 事件处理方法中,我们创建了一个 Offer SDP 并设置本地描述符,然后将其发送到所有的数据通道对象中。当收到对端发送过来的 SDP 消息时,我们将其转换为 RTCSessionDescription 对象,并调用 SetRemoteDescription 方法设置远端描述符。如果收到来自对端的 Offer SDP,则执行 CreateAnswer 方法创建 Answer SDP 并将其发送回对端。HWa28资讯网——每日最新资讯28at.com

通过这种方式,我们就可以使用同一个 PeerConnection 对象来支持多路复用。每当需要发送数据时,只需要将数据发送到指定的数据通道对象即可。需要注意的是,在使用多路复用时,我们需要为不同的数据通道设置不同的标签(Label),以便在接收端识别不同的通道。HWa28资讯网——每日最新资讯28at.com

本文链接:http://www.28at.com/showinfo-26-147-0.htmlWebRTC.Net库开发进阶,教你实现屏幕共享和多路复用!

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com

上一篇: 一个注解实现接口幂等,这样才优雅!

下一篇: Python异步IO编程的进程/线程通信实现

标签:
  • 热门焦点
  • 中兴AX5400Pro+上手体验:再升级 双2.5G网口+USB 3.0这次全都有

    2021年11月的时候,中兴先后发布了两款路由器产品,中兴AX5400和中兴AX5400 Pro,从产品命名上就不难看出这是隶属于同一系列的,但在外观设计上这两款产品可以说是完全没一点关系
  • 三言两语说透设计模式的艺术-简单工厂模式

    一、写在前面工厂模式是最常见的一种创建型设计模式,通常说的工厂模式指的是工厂方法模式,是使用频率最高的工厂模式。简单工厂模式又称为静态工厂方法模式,不属于GoF 23种设计
  • CSS单标签实现转转logo

    转转品牌升级后更新了全新的Logo,今天我们用纯CSS来实现转转的新Logo,为了有一定的挑战性,这里我们只使用一个标签实现,将最大化的使用CSS能力完成Logo的绘制与动画效果。新logo
  • 之家push系统迭代之路

    前言在这个信息爆炸的互联网时代,能够及时准确获取信息是当今社会要解决的关键问题之一。随着之家用户体量和内容规模的不断增大,传统的靠"主动拉"获取信息的方式已不能满足用
  • 如何通过Python线程池实现异步编程?

    线程池的概念和基本原理线程池是一种并发处理机制,它可以在程序启动时创建一组线程,并将它们置于等待任务的状态。当任务到达时,线程池中的某个线程会被唤醒并执行任务,执行完任
  • 一文搞定Java NIO,以及各种奇葩流

    大家好,我是哪吒。很多朋友问我,如何才能学好IO流,对各种流的概念,云里雾里的,不求甚解。用到的时候,现百度,功能虽然实现了,但是为什么用这个?不知道。更别说效率问题了~下次再遇到,
  • 得物宠物生意「狂飙」,发力“它经济”

    作者|花花小萌主近日,得物宣布正式上线宠物鉴别,通过得物App内的&ldquo;在线鉴别&rdquo;,可找到鉴别宠物的选项。通过上传自家宠物的部位细节,就能收获拥有专业资质认证的得物鉴
  • 10天营收超1亿美元,《星铁》比《原神》差在哪?

    来源:伯虎财经作者:陈平安即便你没玩过《原神》,你一定听说过的它的大名。恨它的人把《原神》开服那天称作是中国游戏史上最黑暗的一天,有粉丝因为索尼在PS平台上线《原神》,怒而
  • 与兆芯合作 联想推出全新旗舰版笔记本电脑开天N7系列

    联想与兆芯合作推出全新联想旗舰版笔记本电脑开天 N7系列。这个系列采用兆芯KX-6640MA处理器平台,KX-6640MA 处理器是采用了陆家嘴架构,16nm 工艺,4 核 4 线
Top