当前位置: 首页 > news >正文

【MediaSoup---源码篇】(三)Transport

概述

RTC::Transport是mediasoup中的一个重要概念,它用于在mediasoup与客户端之间传输实时音视频数据。

Transport继承着众多的类,主要用于Transport的整体感知

	class Transport : public RTC::Producer::Listener,public RTC::Consumer::Listener,public RTC::DataProducer::Listener,public RTC::DataConsumer::Listener,public RTC::SctpAssociation::Listener,public RTC::TransportCongestionControlClient::Listener,public RTC::TransportCongestionControlServer::Listener,public Channel::ChannelSocket::RequestHandler,public PayloadChannel::PayloadChannelSocket::RequestHandler,public PayloadChannel::PayloadChannelSocket::NotificationHandler,
分析Transport的创建流程

在Router层会根据类型来进入分支创建transport,以PlainTransport为例

			case Channel::ChannelRequest::MethodId::ROUTER_CREATE_PLAIN_TRANSPORT:{std::string transportId;//从上层获取到transport的ID// This may throwSetNewTransportIdFromData(request->data, transportId);auto* plainTransport =new RTC::PlainTransport(this->shared, transportId, this, request->data);// Insert into the map.绑定ID与transport的映射this->mapTransports[transportId] = plainTransport;MS_DEBUG_DEV("PlainTransport created [transportId:%s]", transportId.c_str());json data = json::object();plainTransport->FillJson(data);request->Accept(data);break;}

在构造函数中,创建了socket,并且绑定了通道消息回调

	PlainTransport::PlainTransport(RTC::Shared* shared, const std::string& id, RTC::Transport::Listener* listener, json& data): RTC::Transport::Transport(shared, id, listener, data){MS_TRACE();auto jsonListenIpIt = data.find("listenIp");if (jsonListenIpIt == data.end())MS_THROW_TYPE_ERROR("missing listenIp");else if (!jsonListenIpIt->is_object())MS_THROW_TYPE_ERROR("wrong listenIp (not an object)");auto jsonIpIt = jsonListenIpIt->find("ip");if (jsonIpIt == jsonListenIpIt->end())MS_THROW_TYPE_ERROR("missing listenIp.ip");else if (!jsonIpIt->is_string())MS_THROW_TYPE_ERROR("wrong listenIp.ip (not an string)");//获得内网IPthis->listenIp.ip.assign(jsonIpIt->get<std::string>());// This may throw.Utils::IP::NormalizeIp(this->listenIp.ip);auto jsonAnnouncedIpIt = jsonListenIpIt->find("announcedIp");if (jsonAnnouncedIpIt != jsonListenIpIt->end()){if (!jsonAnnouncedIpIt->is_string())MS_THROW_TYPE_ERROR("wrong listenIp.announcedIp (not an string");//获得外网IPthis->listenIp.announcedIp.assign(jsonAnnouncedIpIt->get<std::string>());}//获取端口uint16_t port{ 0 };auto jsonPortIt = data.find("port");//判断端口是否合法if (jsonPortIt != data.end()){if (!(jsonPortIt->is_number() && Utils::Json::IsPositiveInteger(*jsonPortIt)))MS_THROW_TYPE_ERROR("wrong port (not a positive number)");port = jsonPortIt->get<uint16_t>();}auto jsonRtcpMuxIt = data.find("rtcpMux");if (jsonRtcpMuxIt != data.end()){if (!jsonRtcpMuxIt->is_boolean())MS_THROW_TYPE_ERROR("wrong rtcpMux (not a boolean)");this->rtcpMux = jsonRtcpMuxIt->get<bool>();}auto jsonComediaIt = data.find("comedia");if (jsonComediaIt != data.end()){if (!jsonComediaIt->is_boolean())MS_THROW_TYPE_ERROR("wrong comedia (not a boolean)");this->comedia = jsonComediaIt->get<bool>();}auto jsonEnableSrtpIt = data.find("enableSrtp");// clang-format offif (jsonEnableSrtpIt != data.end() &&jsonEnableSrtpIt->is_boolean() &&jsonEnableSrtpIt->get<bool>())// clang-format on{auto jsonSrtpCryptoSuiteIt = data.find("srtpCryptoSuite");if (jsonSrtpCryptoSuiteIt == data.end() || !jsonSrtpCryptoSuiteIt->is_string())MS_THROW_TYPE_ERROR("missing srtpCryptoSuite)");// Ensure it's a crypto suite supported by us.auto it =PlainTransport::string2SrtpCryptoSuite.find(jsonSrtpCryptoSuiteIt->get<std::string>());if (it == PlainTransport::string2SrtpCryptoSuite.end())MS_THROW_TYPE_ERROR("invalid/unsupported srtpCryptoSuite");// NOTE: The SRTP crypto suite may change later on connect().this->srtpCryptoSuite = it->second;switch (this->srtpCryptoSuite){case RTC::SrtpSession::CryptoSuite::AEAD_AES_256_GCM:{this->srtpMasterLength = SrtpAesGcm256MasterLength;break;}case RTC::SrtpSession::CryptoSuite::AEAD_AES_128_GCM:{this->srtpMasterLength = SrtpAesGcm128MasterLength;break;}case RTC::SrtpSession::CryptoSuite::AES_CM_128_HMAC_SHA1_80:case RTC::SrtpSession::CryptoSuite::AES_CM_128_HMAC_SHA1_32:{this->srtpMasterLength = SrtpMasterLength;break;}default:{MS_ABORT("unknown SRTP crypto suite");}}this->srtpKey       = Utils::Crypto::GetRandomString(this->srtpMasterLength);this->srtpKeyBase64 = Utils::String::Base64Encode(this->srtpKey);//进行base64编码}try{// This may throw. 通过 listenIp, port 创建 UdpSocketif (port != 0)this->udpSocket = new RTC::UdpSocket(this, this->listenIp.ip, port);elsethis->udpSocket = new RTC::UdpSocket(this, this->listenIp.ip);if (!this->rtcpMux){// This may throw.this->rtcpUdpSocket = new RTC::UdpSocket(this, this->listenIp.ip);}// NOTE: This may throw.this->shared->channelMessageRegistrator->RegisterHandler(this->id,/*channelRequestHandler*/ this,/*payloadChannelRequestHandler*/ this,/*payloadChannelNotificationHandler*/ this);}catch (const MediaSoupError& error){delete this->udpSocket;this->udpSocket = nullptr;delete this->rtcpUdpSocket;this->rtcpUdpSocket = nullptr;throw;}}

http://www.lryc.cn/news/172609.html

相关文章:

  • 爱分析《商业智能最佳实践案例》
  • golang:context
  • 探讨代理IP与Socks5代理在跨界电商中的网络安全应用
  • Guava Cache介绍-面试用
  • ARM 汇编指令作业(求公约数、for循环实现1-100之间和、从SVC模式切换到user模式简单写法)
  • Go - 【字符串,数组,哈希表】常用操作
  • vue 普通组件的 局部注册
  • 医疗虚拟仿真和虚拟现实有什么区别?哪个更好?
  • 【.net core】yisha框架使用nginx代理swagger接口无法访问问题
  • uniapp录音功能和音频播放功能制作
  • 服务器数据恢复-LINUX操作系统下各文件系统误删除/格式化数据的恢复方案
  • python/C++二分查找库函数(lower_bound() 、upper_bound,bisect_left,bisect_right)
  • 爬虫 — App 爬虫(二)
  • 汽车电子相关术语
  • Python 找出最大数
  • Spring Security 用了那么久,你对它有整体把控吗?
  • vue+minio实现文件上传操作
  • 使用JavaScript实现无限滚动的方法
  • html学习综合案例1
  • 神经节苷脂抗体——博迈伦
  • 【Unity】简单的深度虚化shader
  • 启动 React APP 后经历了哪些过程
  • 带自动采集小说网站源码 小说听书网站源码 小说网站源码 带教程
  • MySQL学习笔记2
  • 【python爬虫】—星巴克产品
  • 算法 矩阵最长递增路径-(递归回溯+动态规划)
  • 四、数学建模之图与网络模型
  • php在header增加key,sign,timestamp,实现鉴权
  • Spring实例化源码解析之ConfigurationClassParser(三)
  • 在 Substance Painter中实现Unity Standard Shader