mediasoup c++ worker源码分析
·
worker主要类创建和管理参数
1. RTC::Shared 注册全局请求和通知的对象
1.创建shared
this->shared = new RTC::Shared(
/*channelMessageRegistrator*/ new ChannelMessageRegistrator(),
/*channelNotifier*/ new Channel::ChannelNotifier(this->channel));
2.channelMessageRegistrator注册request notification
this->shared->channelMessageRegistrator->RegisterHandler(
this->id,
/*channelRequestHandler*/ this,
/*channelNotificationHandler*/ this);
HandleRequest(Channel::ChannelRequest* request)
3.使用notification
auto dtlsStateChangeOffset = FBS::WebRtcTransport::CreateDtlsStateChangeNotification(
this->shared->channelNotifier->GetBufferBuilder(), FBS::WebRtcTransport::DtlsState::CLOSED);
this->shared->channelNotifier->Emit(
this->id,
FBS::Notification::Event::WEBRTCTRANSPORT_DTLS_STATE_CHANGE,
FBS::Notification::Body::WebRtcTransport_DtlsStateChangeNotification,
dtlsStateChangeOffset);
HandleNotification(Channel::ChannelNotification* notification)
2. RTC::WebRtcServer 创建webrtc服务
a.封装底层 WebRTC ICE + DTLS + UDP/TCP 套接字的监听和接入
b.提供连接能力(收包、收握手)
c.是 Transport 的“网口”
// c++
auto* webRtcServer = new RTC::WebRtcServer(this->shared, webRtcServerId, body->listenInfos());
this->mapWebRtcServers[webRtcServerId] = webRtcServer;
// 关联参数
static const char * const names[] = {
"webRtcServerId",
"listenInfos"
};
static const char * const names[] = {
"protocol",
"ip",
"announcedAddress",
"port",
"portRange",
"flags",
"sendBufferSize",
"recvBufferSize"
};
3. RTC::Router
a.是媒体路由核心,管理 RTP 编解码能力、Producer、Consumer
b.用于同一“房间”中的用户媒体流转发与桥接
c.一个 Router 表示一个独立的 RTP 空间
// c++
auto* router = new RTC::Router(this->shared, routerId, this);
this->mapRouters[routerId] = router;
// 关联参数
static const char * const names[] = {
"routerId"
};
4. RTC::WebRtcTransport
a.接收远端客户端的 WebRTC ICE 候选(ufrag/password)
b.与远端协商 DTLS,然后收发 RTP/RTCP
c.挂载 Producer/Consumer 用于音视频转发
// c++ 绑定webRtcServer()
auto iceCandidates = webRtcServer->GetIceCandidates(options->enableUdp(), options->enableTcp(), options->preferUdp() options->preferTcp());
// This may throw.
auto* webRtcTransport = new RTC::WebRtcTransport(this->shared, transportId, this, webRtcServer, iceCandidates, options);
// Insert into the map.
this->mapTransports[transportId] = webRtcTransport;
// 关联参数
static const char * const names[] = {
"transportId",
"options"
};
static const char * const names[] = {
"base",
"listen_type",
"listen",
"enableUdp",
"enableTcp",
"preferUdp",
"preferTcp",
"iceConsentTimeout"
};
// ICE DTLS
// Create a ICE server.
this->iceServer = new RTC::IceServer(this, Utils::Crypto::GetRandomString(32), Utils::Crypto::GetRandomString(32), iceConsentTimeout);
// Create a DTLS transport.
this->dtlsTransport = new RTC::DtlsTransport(this);
// SRTP
// OnDtlsTransportConnected
this->srtpSendSession = new RTC::SrtpSession(RTC::SrtpSession::Type::OUTBOUND, srtpCryptoSuite, srtpLocalKey, srtpLocalKeyLen);
this->srtpRecvSession = new RTC::SrtpSession(RTC::SrtpSession::Type::INBOUND, srtpCryptoSuite, srtpRemoteKey, srtpRemoteKeyLen);
## Transport对比
| 类型 | 用途 | 使用网络 | 支持SRTP | 典型使用者 |
| WebRtcTransport | 浏览器/WebRTC SDK | ✅ | ✅ | WebRTC 客户端 |
| PlainTransport | RTP 推流工具/FFmpeg/GStreamer | ✅ | ❌ | RTP encoder/decoder |
| PipeTransport | Router <-> Router 内部通信 | ✅ | ❌ | mediasoup 内部连接 |
| DirectTransport | 注入裸帧/本地媒体桥接 | ❌ | ❌ | 服务器本地 TTS、混音等 |
5. RTC::Producer
auto* producer = new RTC::Producer(this->shared, producerId, this, body);
// 关联参数
static const char * const names[] = {
"producerId",
"kind",
"rtpParameters",
"rtpMapping",
"keyFrameRequestDelay",
"paused"
};
//MediaKindTypeTable
static const char * const names[] = {
"AUDIO",
"VIDEO"
};
//RtpParametersTypeTable
static const char * const names[] = {
"mid",
"codecs",
"headerExtensions",
"encodings",
"rtcp"
};
//RtpMappingTypeTable
static const char * const names[] = {
"codecs",
"encodings"
};
6 RTC::SimpleConsumer
consumer = new RTC::SimpleConsumer(this->shared, consumerId, producerId, this, body);
//关联参数
static const char * const names[] = {
"consumerId",
"producerId",
"kind",
"rtpParameters",
"type",
"consumableRtpEncodings",
"paused",
"preferredLayers",
"ignoreDtx"
};
//MediaKindTypeTable
static const char * const names[] = {
"AUDIO",
"VIDEO"
};
//RtpParametersTypeTable
static const char * const names[] = {
"mid",
"codecs",
"headerExtensions",
"encodings",
"rtcp"
};
//TypeTypeTable
static const char * const names[] = {
"SIMPLE",
"SIMULCAST",
"SVC",
"PIPE"
};
//RtpEncodingParametersTypeTable
static const char * const names[] = {
"ssrc",
"rid",
"codecPayloadType",
"rtx",
"dtx",
"scalabilityMode",
"maxBitrate"
};
//ConsumerLayersTypeTable
static const char * const names[] = {
"spatialLayer",
"temporalLayer"
};
7. RTC::RtpStreamRecv
RTC::RtpStream::Params params;
params.encodingIdx = encodingIdx;
params.ssrc = ssrc;
params.payloadType = mediaCodec.payloadType;
params.mimeType = mediaCodec.mimeType;
params.clockRate = mediaCodec.clockRate;
params.rid = encoding.rid;
params.cname = this->rtpParameters.rtcp.cname;
params.spatialLayers = encoding.spatialLayers;
params.temporalLayers = encoding.temporalLayers;
auto* rtpStream = new RTC::RtpStreamRecv(this, params, SendNackDelay, useRtpInactivityCheck);
8. RtpStreamSend
RTC::RtpStream::Params params;
params.ssrc = encoding.ssrc;
params.payloadType = mediaCodec->payloadType;
params.mimeType = mediaCodec->mimeType;
params.clockRate = mediaCodec->clockRate;
params.cname = this->rtpParameters.rtcp.cname;
this->rtpStream = new RTC::RtpStreamSend(this, params, this->rtpParameters.mid);
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)