You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

338 lines
11 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "media/base/rtp_data_engine.h"
  11. #include <map>
  12. #include "absl/strings/match.h"
  13. #include "media/base/codec.h"
  14. #include "media/base/media_constants.h"
  15. #include "media/base/rtp_utils.h"
  16. #include "media/base/stream_params.h"
  17. #include "rtc_base/copy_on_write_buffer.h"
  18. #include "rtc_base/data_rate_limiter.h"
  19. #include "rtc_base/helpers.h"
  20. #include "rtc_base/logging.h"
  21. #include "rtc_base/sanitizer.h"
  22. namespace cricket {
  23. // We want to avoid IP fragmentation.
  24. static const size_t kDataMaxRtpPacketLen = 1200U;
  25. // We reserve space after the RTP header for future wiggle room.
  26. static const unsigned char kReservedSpace[] = {0x00, 0x00, 0x00, 0x00};
  27. // Amount of overhead SRTP may take. We need to leave room in the
  28. // buffer for it, otherwise SRTP will fail later. If SRTP ever uses
  29. // more than this, we need to increase this number.
  30. static const size_t kMaxSrtpHmacOverhead = 16;
  31. RtpDataEngine::RtpDataEngine() {
  32. data_codecs_.push_back(
  33. DataCodec(kGoogleRtpDataCodecPlType, kGoogleRtpDataCodecName));
  34. }
  35. DataMediaChannel* RtpDataEngine::CreateChannel(const MediaConfig& config) {
  36. return new RtpDataMediaChannel(config);
  37. }
  38. static const DataCodec* FindCodecByName(const std::vector<DataCodec>& codecs,
  39. const std::string& name) {
  40. for (const DataCodec& codec : codecs) {
  41. if (absl::EqualsIgnoreCase(name, codec.name))
  42. return &codec;
  43. }
  44. return nullptr;
  45. }
  46. RtpDataMediaChannel::RtpDataMediaChannel(const MediaConfig& config)
  47. : DataMediaChannel(config) {
  48. Construct();
  49. SetPreferredDscp(rtc::DSCP_AF41);
  50. }
  51. void RtpDataMediaChannel::Construct() {
  52. sending_ = false;
  53. receiving_ = false;
  54. send_limiter_.reset(new rtc::DataRateLimiter(kDataMaxBandwidth / 8, 1.0));
  55. }
  56. RtpDataMediaChannel::~RtpDataMediaChannel() {
  57. std::map<uint32_t, RtpClock*>::const_iterator iter;
  58. for (iter = rtp_clock_by_send_ssrc_.begin();
  59. iter != rtp_clock_by_send_ssrc_.end(); ++iter) {
  60. delete iter->second;
  61. }
  62. }
  63. void RTC_NO_SANITIZE("float-cast-overflow") // bugs.webrtc.org/8204
  64. RtpClock::Tick(double now, int* seq_num, uint32_t* timestamp) {
  65. *seq_num = ++last_seq_num_;
  66. *timestamp = timestamp_offset_ + static_cast<uint32_t>(now * clockrate_);
  67. // UBSan: 5.92374e+10 is outside the range of representable values of type
  68. // 'unsigned int'
  69. }
  70. const DataCodec* FindUnknownCodec(const std::vector<DataCodec>& codecs) {
  71. DataCodec data_codec(kGoogleRtpDataCodecPlType, kGoogleRtpDataCodecName);
  72. std::vector<DataCodec>::const_iterator iter;
  73. for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
  74. if (!iter->Matches(data_codec)) {
  75. return &(*iter);
  76. }
  77. }
  78. return NULL;
  79. }
  80. const DataCodec* FindKnownCodec(const std::vector<DataCodec>& codecs) {
  81. DataCodec data_codec(kGoogleRtpDataCodecPlType, kGoogleRtpDataCodecName);
  82. std::vector<DataCodec>::const_iterator iter;
  83. for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
  84. if (iter->Matches(data_codec)) {
  85. return &(*iter);
  86. }
  87. }
  88. return NULL;
  89. }
  90. bool RtpDataMediaChannel::SetRecvCodecs(const std::vector<DataCodec>& codecs) {
  91. const DataCodec* unknown_codec = FindUnknownCodec(codecs);
  92. if (unknown_codec) {
  93. RTC_LOG(LS_WARNING) << "Failed to SetRecvCodecs because of unknown codec: "
  94. << unknown_codec->ToString();
  95. return false;
  96. }
  97. recv_codecs_ = codecs;
  98. return true;
  99. }
  100. bool RtpDataMediaChannel::SetSendCodecs(const std::vector<DataCodec>& codecs) {
  101. const DataCodec* known_codec = FindKnownCodec(codecs);
  102. if (!known_codec) {
  103. RTC_LOG(LS_WARNING)
  104. << "Failed to SetSendCodecs because there is no known codec.";
  105. return false;
  106. }
  107. send_codecs_ = codecs;
  108. return true;
  109. }
  110. bool RtpDataMediaChannel::SetSendParameters(const DataSendParameters& params) {
  111. return (SetSendCodecs(params.codecs) &&
  112. SetMaxSendBandwidth(params.max_bandwidth_bps));
  113. }
  114. bool RtpDataMediaChannel::SetRecvParameters(const DataRecvParameters& params) {
  115. return SetRecvCodecs(params.codecs);
  116. }
  117. bool RtpDataMediaChannel::AddSendStream(const StreamParams& stream) {
  118. if (!stream.has_ssrcs()) {
  119. return false;
  120. }
  121. if (GetStreamBySsrc(send_streams_, stream.first_ssrc())) {
  122. RTC_LOG(LS_WARNING) << "Not adding data send stream '" << stream.id
  123. << "' with ssrc=" << stream.first_ssrc()
  124. << " because stream already exists.";
  125. return false;
  126. }
  127. send_streams_.push_back(stream);
  128. // TODO(pthatcher): This should be per-stream, not per-ssrc.
  129. // And we should probably allow more than one per stream.
  130. rtp_clock_by_send_ssrc_[stream.first_ssrc()] =
  131. new RtpClock(kDataCodecClockrate, rtc::CreateRandomNonZeroId(),
  132. rtc::CreateRandomNonZeroId());
  133. RTC_LOG(LS_INFO) << "Added data send stream '" << stream.id
  134. << "' with ssrc=" << stream.first_ssrc();
  135. return true;
  136. }
  137. bool RtpDataMediaChannel::RemoveSendStream(uint32_t ssrc) {
  138. if (!GetStreamBySsrc(send_streams_, ssrc)) {
  139. return false;
  140. }
  141. RemoveStreamBySsrc(&send_streams_, ssrc);
  142. delete rtp_clock_by_send_ssrc_[ssrc];
  143. rtp_clock_by_send_ssrc_.erase(ssrc);
  144. return true;
  145. }
  146. bool RtpDataMediaChannel::AddRecvStream(const StreamParams& stream) {
  147. if (!stream.has_ssrcs()) {
  148. return false;
  149. }
  150. if (GetStreamBySsrc(recv_streams_, stream.first_ssrc())) {
  151. RTC_LOG(LS_WARNING) << "Not adding data recv stream '" << stream.id
  152. << "' with ssrc=" << stream.first_ssrc()
  153. << " because stream already exists.";
  154. return false;
  155. }
  156. recv_streams_.push_back(stream);
  157. RTC_LOG(LS_INFO) << "Added data recv stream '" << stream.id
  158. << "' with ssrc=" << stream.first_ssrc();
  159. return true;
  160. }
  161. bool RtpDataMediaChannel::RemoveRecvStream(uint32_t ssrc) {
  162. RemoveStreamBySsrc(&recv_streams_, ssrc);
  163. return true;
  164. }
  165. // Not implemented.
  166. void RtpDataMediaChannel::ResetUnsignaledRecvStream() {}
  167. void RtpDataMediaChannel::OnPacketReceived(rtc::CopyOnWriteBuffer packet,
  168. int64_t /* packet_time_us */) {
  169. RtpHeader header;
  170. if (!GetRtpHeader(packet.cdata(), packet.size(), &header)) {
  171. return;
  172. }
  173. size_t header_length;
  174. if (!GetRtpHeaderLen(packet.cdata(), packet.size(), &header_length)) {
  175. return;
  176. }
  177. const char* data =
  178. packet.cdata<char>() + header_length + sizeof(kReservedSpace);
  179. size_t data_len = packet.size() - header_length - sizeof(kReservedSpace);
  180. if (!receiving_) {
  181. RTC_LOG(LS_WARNING) << "Not receiving packet " << header.ssrc << ":"
  182. << header.seq_num << " before SetReceive(true) called.";
  183. return;
  184. }
  185. if (!FindCodecById(recv_codecs_, header.payload_type)) {
  186. return;
  187. }
  188. if (!GetStreamBySsrc(recv_streams_, header.ssrc)) {
  189. RTC_LOG(LS_WARNING) << "Received packet for unknown ssrc: " << header.ssrc;
  190. return;
  191. }
  192. // Uncomment this for easy debugging.
  193. // const auto* found_stream = GetStreamBySsrc(recv_streams_, header.ssrc);
  194. // RTC_LOG(LS_INFO) << "Received packet"
  195. // << " groupid=" << found_stream.groupid
  196. // << ", ssrc=" << header.ssrc
  197. // << ", seqnum=" << header.seq_num
  198. // << ", timestamp=" << header.timestamp
  199. // << ", len=" << data_len;
  200. ReceiveDataParams params;
  201. params.ssrc = header.ssrc;
  202. params.seq_num = header.seq_num;
  203. params.timestamp = header.timestamp;
  204. SignalDataReceived(params, data, data_len);
  205. }
  206. bool RtpDataMediaChannel::SetMaxSendBandwidth(int bps) {
  207. if (bps <= 0) {
  208. bps = kDataMaxBandwidth;
  209. }
  210. send_limiter_.reset(new rtc::DataRateLimiter(bps / 8, 1.0));
  211. RTC_LOG(LS_INFO) << "RtpDataMediaChannel::SetSendBandwidth to " << bps
  212. << "bps.";
  213. return true;
  214. }
  215. bool RtpDataMediaChannel::SendData(const SendDataParams& params,
  216. const rtc::CopyOnWriteBuffer& payload,
  217. SendDataResult* result) {
  218. if (result) {
  219. // If we return true, we'll set this to SDR_SUCCESS.
  220. *result = SDR_ERROR;
  221. }
  222. if (!sending_) {
  223. RTC_LOG(LS_WARNING) << "Not sending packet with ssrc=" << params.ssrc
  224. << " len=" << payload.size()
  225. << " before SetSend(true).";
  226. return false;
  227. }
  228. if (params.type != cricket::DMT_TEXT) {
  229. RTC_LOG(LS_WARNING)
  230. << "Not sending data because binary type is unsupported.";
  231. return false;
  232. }
  233. const StreamParams* found_stream =
  234. GetStreamBySsrc(send_streams_, params.ssrc);
  235. if (!found_stream) {
  236. RTC_LOG(LS_WARNING) << "Not sending data because ssrc is unknown: "
  237. << params.ssrc;
  238. return false;
  239. }
  240. const DataCodec* found_codec =
  241. FindCodecByName(send_codecs_, kGoogleRtpDataCodecName);
  242. if (!found_codec) {
  243. RTC_LOG(LS_WARNING) << "Not sending data because codec is unknown: "
  244. << kGoogleRtpDataCodecName;
  245. return false;
  246. }
  247. size_t packet_len = (kMinRtpPacketLen + sizeof(kReservedSpace) +
  248. payload.size() + kMaxSrtpHmacOverhead);
  249. if (packet_len > kDataMaxRtpPacketLen) {
  250. return false;
  251. }
  252. double now =
  253. rtc::TimeMicros() / static_cast<double>(rtc::kNumMicrosecsPerSec);
  254. if (!send_limiter_->CanUse(packet_len, now)) {
  255. RTC_LOG(LS_VERBOSE) << "Dropped data packet of len=" << packet_len
  256. << "; already sent " << send_limiter_->used_in_period()
  257. << "/" << send_limiter_->max_per_period();
  258. return false;
  259. }
  260. RtpHeader header;
  261. header.payload_type = found_codec->id;
  262. header.ssrc = params.ssrc;
  263. rtp_clock_by_send_ssrc_[header.ssrc]->Tick(now, &header.seq_num,
  264. &header.timestamp);
  265. rtc::CopyOnWriteBuffer packet(kMinRtpPacketLen, packet_len);
  266. if (!SetRtpHeader(packet.data(), packet.size(), header)) {
  267. return false;
  268. }
  269. packet.AppendData(kReservedSpace);
  270. packet.AppendData(payload);
  271. RTC_LOG(LS_VERBOSE) << "Sent RTP data packet: "
  272. << " stream=" << found_stream->id
  273. << " ssrc=" << header.ssrc
  274. << ", seqnum=" << header.seq_num
  275. << ", timestamp=" << header.timestamp
  276. << ", len=" << payload.size();
  277. rtc::PacketOptions options;
  278. options.info_signaled_after_sent.packet_type = rtc::PacketType::kData;
  279. MediaChannel::SendPacket(&packet, options);
  280. send_limiter_->Use(packet_len, now);
  281. if (result) {
  282. *result = SDR_SUCCESS;
  283. }
  284. return true;
  285. }
  286. } // namespace cricket