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.

359 lines
12 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2012 Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/media/base/rtpdataengine.h"
  28. #include "talk/base/buffer.h"
  29. #include "talk/base/helpers.h"
  30. #include "talk/base/logging.h"
  31. #include "talk/base/ratelimiter.h"
  32. #include "talk/base/timing.h"
  33. #include "talk/media/base/codec.h"
  34. #include "talk/media/base/constants.h"
  35. #include "talk/media/base/rtputils.h"
  36. #include "talk/media/base/streamparams.h"
  37. namespace cricket {
  38. // We want to avoid IP fragmentation.
  39. static const size_t kDataMaxRtpPacketLen = 1200U;
  40. // We reserve space after the RTP header for future wiggle room.
  41. static const unsigned char kReservedSpace[] = {
  42. 0x00, 0x00, 0x00, 0x00
  43. };
  44. // Amount of overhead SRTP may take. We need to leave room in the
  45. // buffer for it, otherwise SRTP will fail later. If SRTP ever uses
  46. // more than this, we need to increase this number.
  47. static const size_t kMaxSrtpHmacOverhead = 16;
  48. RtpDataEngine::RtpDataEngine() {
  49. data_codecs_.push_back(
  50. DataCodec(cricket::kGoogleDataCodecId,
  51. cricket::kGoogleDataCodecName, 0));
  52. SetTiming(new talk_base::Timing());
  53. }
  54. DataMediaChannel* RtpDataEngine::CreateChannel() {
  55. return new RtpDataMediaChannel(timing_.get());
  56. }
  57. // TODO(pthatcher): Should we move these find/get functions somewhere
  58. // common?
  59. bool FindCodecById(const std::vector<cricket::DataCodec>& codecs,
  60. int id, cricket::DataCodec* codec_out) {
  61. std::vector<cricket::DataCodec>::const_iterator iter;
  62. for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
  63. if (iter->id == id) {
  64. *codec_out = *iter;
  65. return true;
  66. }
  67. }
  68. return false;
  69. }
  70. bool FindCodecByName(const std::vector<cricket::DataCodec>& codecs,
  71. const std::string& name, cricket::DataCodec* codec_out) {
  72. std::vector<cricket::DataCodec>::const_iterator iter;
  73. for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
  74. if (iter->name == name) {
  75. *codec_out = *iter;
  76. return true;
  77. }
  78. }
  79. return false;
  80. }
  81. RtpDataMediaChannel::RtpDataMediaChannel(talk_base::Timing* timing) {
  82. Construct(timing);
  83. }
  84. RtpDataMediaChannel::RtpDataMediaChannel() {
  85. Construct(NULL);
  86. }
  87. void RtpDataMediaChannel::Construct(talk_base::Timing* timing) {
  88. sending_ = false;
  89. receiving_ = false;
  90. timing_ = timing;
  91. send_limiter_.reset(new talk_base::RateLimiter(kDataMaxBandwidth / 8, 1.0));
  92. }
  93. RtpDataMediaChannel::~RtpDataMediaChannel() {
  94. std::map<uint32, RtpClock*>::const_iterator iter;
  95. for (iter = rtp_clock_by_send_ssrc_.begin();
  96. iter != rtp_clock_by_send_ssrc_.end();
  97. ++iter) {
  98. delete iter->second;
  99. }
  100. }
  101. void RtpClock::Tick(
  102. double now, int* seq_num, uint32* timestamp) {
  103. *seq_num = ++last_seq_num_;
  104. *timestamp = timestamp_offset_ + static_cast<uint32>(now * clockrate_);
  105. }
  106. const DataCodec* FindUnknownCodec(const std::vector<DataCodec>& codecs) {
  107. std::vector<cricket::DataCodec>::const_iterator iter;
  108. for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
  109. if (!iter->Matches(kGoogleDataCodecId, kGoogleDataCodecName)) {
  110. return &(*iter);
  111. }
  112. }
  113. return NULL;
  114. }
  115. const DataCodec* FindKnownCodec(const std::vector<DataCodec>& codecs) {
  116. std::vector<cricket::DataCodec>::const_iterator iter;
  117. for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
  118. if (iter->Matches(kGoogleDataCodecId, kGoogleDataCodecName)) {
  119. return &(*iter);
  120. }
  121. }
  122. return NULL;
  123. }
  124. bool RtpDataMediaChannel::SetRecvCodecs(const std::vector<DataCodec>& codecs) {
  125. const DataCodec* unknown_codec = FindUnknownCodec(codecs);
  126. if (unknown_codec) {
  127. LOG(LS_WARNING) << "Failed to SetRecvCodecs because of unknown codec: "
  128. << unknown_codec->ToString();
  129. return false;
  130. }
  131. recv_codecs_ = codecs;
  132. return true;
  133. }
  134. bool RtpDataMediaChannel::SetSendCodecs(const std::vector<DataCodec>& codecs) {
  135. const DataCodec* known_codec = FindKnownCodec(codecs);
  136. if (!known_codec) {
  137. LOG(LS_WARNING) <<
  138. "Failed to SetSendCodecs because there is no known codec.";
  139. return false;
  140. }
  141. send_codecs_ = codecs;
  142. return true;
  143. }
  144. bool RtpDataMediaChannel::AddSendStream(const StreamParams& stream) {
  145. if (!stream.has_ssrcs()) {
  146. return false;
  147. }
  148. StreamParams found_stream;
  149. if (GetStreamBySsrc(send_streams_, stream.first_ssrc(), &found_stream)) {
  150. LOG(LS_WARNING) << "Not adding data send stream '" << stream.name
  151. << "' with ssrc=" << stream.first_ssrc()
  152. << " because stream already exists.";
  153. return false;
  154. }
  155. send_streams_.push_back(stream);
  156. // TODO(pthatcher): This should be per-stream, not per-ssrc.
  157. // And we should probably allow more than one per stream.
  158. rtp_clock_by_send_ssrc_[stream.first_ssrc()] = new RtpClock(
  159. kDataCodecClockrate,
  160. talk_base::CreateRandomNonZeroId(), talk_base::CreateRandomNonZeroId());
  161. LOG(LS_INFO) << "Added data send stream '" << stream.name
  162. << "' with ssrc=" << stream.first_ssrc();
  163. return true;
  164. }
  165. bool RtpDataMediaChannel::RemoveSendStream(uint32 ssrc) {
  166. StreamParams found_stream;
  167. if (!GetStreamBySsrc(send_streams_, ssrc, &found_stream)) {
  168. return false;
  169. }
  170. RemoveStreamBySsrc(&send_streams_, ssrc);
  171. delete rtp_clock_by_send_ssrc_[ssrc];
  172. rtp_clock_by_send_ssrc_.erase(ssrc);
  173. return true;
  174. }
  175. bool RtpDataMediaChannel::AddRecvStream(const StreamParams& stream) {
  176. if (!stream.has_ssrcs()) {
  177. return false;
  178. }
  179. StreamParams found_stream;
  180. if (GetStreamBySsrc(recv_streams_, stream.first_ssrc(), &found_stream)) {
  181. LOG(LS_WARNING) << "Not adding data recv stream '" << stream.name
  182. << "' with ssrc=" << stream.first_ssrc()
  183. << " because stream already exists.";
  184. return false;
  185. }
  186. recv_streams_.push_back(stream);
  187. LOG(LS_INFO) << "Added data recv stream '" << stream.name
  188. << "' with ssrc=" << stream.first_ssrc();
  189. return true;
  190. }
  191. bool RtpDataMediaChannel::RemoveRecvStream(uint32 ssrc) {
  192. RemoveStreamBySsrc(&recv_streams_, ssrc);
  193. return true;
  194. }
  195. void RtpDataMediaChannel::OnPacketReceived(talk_base::Buffer* packet) {
  196. RtpHeader header;
  197. if (!GetRtpHeader(packet->data(), packet->length(), &header)) {
  198. // Don't want to log for every corrupt packet.
  199. // LOG(LS_WARNING) << "Could not read rtp header from packet of length "
  200. // << packet->length() << ".";
  201. return;
  202. }
  203. size_t header_length;
  204. if (!GetRtpHeaderLen(packet->data(), packet->length(), &header_length)) {
  205. // Don't want to log for every corrupt packet.
  206. // LOG(LS_WARNING) << "Could not read rtp header"
  207. // << length from packet of length "
  208. // << packet->length() << ".";
  209. return;
  210. }
  211. const char* data = packet->data() + header_length + sizeof(kReservedSpace);
  212. size_t data_len = packet->length() - header_length - sizeof(kReservedSpace);
  213. if (!receiving_) {
  214. LOG(LS_WARNING) << "Not receiving packet "
  215. << header.ssrc << ":" << header.seq_num
  216. << " before SetReceive(true) called.";
  217. return;
  218. }
  219. DataCodec codec;
  220. if (!FindCodecById(recv_codecs_, header.payload_type, &codec)) {
  221. LOG(LS_WARNING) << "Not receiving packet "
  222. << header.ssrc << ":" << header.seq_num
  223. << " (" << data_len << ")"
  224. << " because unknown payload id: " << header.payload_type;
  225. return;
  226. }
  227. StreamParams found_stream;
  228. if (!GetStreamBySsrc(recv_streams_, header.ssrc, &found_stream)) {
  229. LOG(LS_WARNING) << "Received packet for unknown ssrc: " << header.ssrc;
  230. return;
  231. }
  232. // Uncomment this for easy debugging.
  233. // LOG(LS_INFO) << "Received packet from " << found_stream.nick << ":"
  234. // << ", ssrc=" << header.ssrc
  235. // << ", seqnum=" << header.seq_num
  236. // << ", timestamp=" << header.timestamp
  237. // << ", len=" << data_len;
  238. ReceiveDataParams params;
  239. params.ssrc = header.ssrc;
  240. params.seq_num = header.seq_num;
  241. params.timestamp = header.timestamp;
  242. SignalDataReceived(params, data, data_len);
  243. }
  244. bool RtpDataMediaChannel::SetSendBandwidth(bool autobw, int bps) {
  245. if (autobw || bps <= 0) {
  246. bps = kDataMaxBandwidth;
  247. }
  248. send_limiter_.reset(new talk_base::RateLimiter(bps / 8, 1.0));
  249. LOG(LS_INFO) << "RtpDataMediaChannel::SetSendBandwidth to " << bps << "bps.";
  250. return true;
  251. }
  252. bool RtpDataMediaChannel::SendData(
  253. const SendDataParams& params, const std::string& data) {
  254. if (!sending_) {
  255. LOG(LS_WARNING) << "Not sending packet with ssrc=" << params.ssrc
  256. << " len=" << data.length() << " before SetSend(true).";
  257. return false;
  258. }
  259. StreamParams found_stream;
  260. if (!GetStreamBySsrc(send_streams_, params.ssrc, &found_stream)) {
  261. LOG(LS_WARNING) << "Not sending data because ssrc is unknown: "
  262. << params.ssrc;
  263. return false;
  264. }
  265. DataCodec found_codec;
  266. if (!FindCodecByName(send_codecs_, kGoogleDataCodecName, &found_codec)) {
  267. LOG(LS_WARNING) << "Not sending data because codec is unknown: "
  268. << kGoogleDataCodecName;
  269. return false;
  270. }
  271. size_t packet_len = (kMinRtpPacketLen + sizeof(kReservedSpace)
  272. + data.length() + kMaxSrtpHmacOverhead);
  273. if (packet_len > kDataMaxRtpPacketLen) {
  274. return false;
  275. }
  276. double now = timing_->TimerNow();
  277. if (!send_limiter_->CanUse(packet_len, now)) {
  278. LOG(LS_VERBOSE) << "Dropped data packet of len=" << packet_len
  279. << "; already sent " << send_limiter_->used_in_period()
  280. << "/" << send_limiter_->max_per_period();
  281. return false;
  282. } else {
  283. LOG(LS_VERBOSE) << "Sending data packet of len=" << packet_len
  284. << "; already sent " << send_limiter_->used_in_period()
  285. << "/" << send_limiter_->max_per_period();
  286. }
  287. RtpHeader header;
  288. header.payload_type = found_codec.id;
  289. header.ssrc = params.ssrc;
  290. rtp_clock_by_send_ssrc_[header.ssrc]->Tick(
  291. now, &header.seq_num, &header.timestamp);
  292. talk_base::Buffer packet;
  293. packet.SetCapacity(packet_len);
  294. packet.SetLength(kMinRtpPacketLen);
  295. if (!SetRtpHeader(packet.data(), packet.length(), header)) {
  296. return false;
  297. }
  298. packet.AppendData(&kReservedSpace, sizeof(kReservedSpace));
  299. packet.AppendData(data.data(), data.length());
  300. // Uncomment this for easy debugging.
  301. // LOG(LS_INFO) << "Sent packet: "
  302. // << " stream=" << found_stream.name
  303. // << ", seqnum=" << header.seq_num
  304. // << ", timestamp=" << header.timestamp
  305. // << ", len=" << data_len;
  306. network_interface()->SendPacket(&packet);
  307. send_limiter_->Use(packet_len, now);
  308. return true;
  309. }
  310. } // namespace cricket