本文主要是介绍mediasoup 源码分析 (八)分析PlainTransport,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
mediasoup 源码分析 (六)分析PlainTransport
- 一、接收裸RTP流
- 二、mediasoup 中udp建立过程
- tips
一、接收裸RTP流
PlainTransport 可以接收裸RTP流,也可以接收AES加密的RTP流。源码中提供了一个通过ffmpeg发送裸RTP流到mediasoup的脚本,具体地址为:mediasoup-demo/broadcasters/ffmpeg.sh
脚本就是通过HTTP Post发送创建PlainTranport请求,然后通过ffmpeg向指定地址+端口,发送RTP流
res=$(${HTTPIE_COMMAND} \POST ${SERVER_URL}/rooms/${ROOM_ID}/broadcasters/${BROADCASTER_ID}/transports \type="plain" \comedia:=true \rtcpMux:=false \2> /dev/null)
ffmpeg发送RTP流
#
# NOTES:
# - We can add ?pkt_size=1200 to each rtp:// URI to limit the max packet size
# to 1200 bytes.
#
ffmpeg \-re \-v info \-stream_loop -1 \-i ${MEDIA_FILE} \-map 0:a:0 \-acodec libopus -ab 128k -ac 2 -ar 48000 \-map 0:v:0 \-pix_fmt yuv420p -c:v libvpx -b:v 1000k -deadline realtime -cpu-used 4 \-f tee \"[select=a:f=rtp:ssrc=${AUDIO_SSRC}:payload_type=${AUDIO_PT}]rtp://${audioTransportIp}:${audioTransportPort}?rtcpport=${audioTransportRtcpPort}|[select=v:f=rtp:ssrc=${VIDEO_SSRC}:payload_type=${VIDEO_PT}]rtp://${videoTransportIp}:${videoTransportPort}?rtcpport=${videoTransportRtcpPort}"
二、mediasoup 中udp建立过程
RTP:UdpSocket的构造函数,开启IP绑定
namespace RTC
{/* Instance methods. */UdpSocket::UdpSocket(Listener* listener, std::string& ip): // This may throw.::UdpSocket::UdpSocket(PortManager::BindUdp(ip)), listener(listener){MS_TRACE();}
真正绑定的地方在PortManager::BindUdp(ip)中,随机从指定Port范围中选出可用Port,然后绑定
// Choose a random port index to start from.portIdx = static_cast<size_t>(Utils::Crypto::GetRandomUInt(static_cast<uint32_t>(0), static_cast<uint32_t>(ports.size() - 1)));..........// Increase current port index.portIdx = (portIdx + 1) % ports.size();// So the corresponding port is the vector position plus the RTC minimum port.port = static_cast<uint16_t>(portIdx + Settings::configuration.rtcMinPort);.....//udp绑定switch (transport){case Transport::UDP:{err = uv_udp_bind(reinterpret_cast<uv_udp_t*>(uvHandle),reinterpret_cast<const struct sockaddr*>(&bindAddr),flags);}
三、全局的UdpSocket绑定接收
UdpSocket::UdpSocket(uv_udp_t* uvHandle) : uvHandle(uvHandle)
{MS_TRACE();int err;this->uvHandle->data = static_cast<void*>(this);err = uv_udp_recv_start(this->uvHandle, static_cast<uv_alloc_cb>(onAlloc), static_cast<uv_udp_recv_cb>(onRecv)); // 接收回调函数
}inline static void onRecv(uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned int flags)
{auto* socket = static_cast<UdpSocket*>(handle->data);if (socket)socket->OnUvRecv(nread, buf, addr, flags);
}
//然后回调到RTC::UdpSocket::UserOnUdpDatagramReceived的函数void UdpSocket::UserOnUdpDatagramReceived(const uint8_t* data, size_t len, const struct sockaddr* addr){MS_TRACE();if (this->listener == nullptr){MS_ERROR("no listener set");return;}// Notify the reader.此处的Listener就是PlainTransportthis->listener->OnUdpSocketPacketReceived(this, data, len, addr);}//PainTransport接收到数据inline void PlainTransport::OnUdpSocketPacketReceived(RTC::UdpSocket* socket, const uint8_t* data, size_t len, const struct sockaddr* remoteAddr){MS_TRACE();//TranportTuple维护socket与客户端地址的关联关系RTC::TransportTuple tuple(socket, remoteAddr);OnPacketReceived(&tuple, data, len);}
//处理接收到的udp包
inline void PlainTransport::OnPacketReceived(RTC::TransportTuple* tuple, const uint8_t* data, size_t len){MS_TRACE();// Increase receive transmission.RTC::Transport::DataReceived(len);// Check if it's RTCP.if (RTC::RTCP::Packet::IsRtcp(data, len)){OnRtcpDataReceived(tuple, data, len);}// Check if it's RTP.else if (RTC::RtpPacket::IsRtp(data, len)){OnRtpDataReceived(tuple, data, len);}// Check if it's SCTP.else if (RTC::SctpAssociation::IsSctp(data, len)){OnSctpDataReceived(tuple, data, len);}else{MS_WARN_DEV("ignoring received packet of unknown type");}}
最后数据传到基类 Transport
Transport处理RTP
tips
更多关于mediasoup的文章可以进入我的专栏查看
http://t.csdnimg.cn/3UQeL
这篇关于mediasoup 源码分析 (八)分析PlainTransport的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!