blob: 923b25476ffda9abddc9fef402eb8d53e4db8bf9 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
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
28#include "talk/media/base/rtpdataengine.h"
29
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000030#include "talk/media/base/codec.h"
31#include "talk/media/base/constants.h"
32#include "talk/media/base/rtputils.h"
33#include "talk/media/base/streamparams.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034#include "webrtc/base/buffer.h"
35#include "webrtc/base/helpers.h"
36#include "webrtc/base/logging.h"
37#include "webrtc/base/ratelimiter.h"
38#include "webrtc/base/timing.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039
40namespace cricket {
41
42// We want to avoid IP fragmentation.
43static const size_t kDataMaxRtpPacketLen = 1200U;
44// We reserve space after the RTP header for future wiggle room.
45static const unsigned char kReservedSpace[] = {
46 0x00, 0x00, 0x00, 0x00
47};
48
49// Amount of overhead SRTP may take. We need to leave room in the
50// buffer for it, otherwise SRTP will fail later. If SRTP ever uses
51// more than this, we need to increase this number.
52static const size_t kMaxSrtpHmacOverhead = 16;
53
54RtpDataEngine::RtpDataEngine() {
55 data_codecs_.push_back(
56 DataCodec(kGoogleRtpDataCodecId,
57 kGoogleRtpDataCodecName, 0));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000058 SetTiming(new rtc::Timing());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059}
60
61DataMediaChannel* RtpDataEngine::CreateChannel(
62 DataChannelType data_channel_type) {
63 if (data_channel_type != DCT_RTP) {
64 return NULL;
65 }
66 return new RtpDataMediaChannel(timing_.get());
67}
68
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069bool FindCodecByName(const std::vector<DataCodec>& codecs,
70 const std::string& name, DataCodec* codec_out) {
71 std::vector<DataCodec>::const_iterator iter;
72 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
73 if (iter->name == name) {
74 *codec_out = *iter;
75 return true;
76 }
77 }
78 return false;
79}
80
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000081RtpDataMediaChannel::RtpDataMediaChannel(rtc::Timing* timing) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 Construct(timing);
83}
84
85RtpDataMediaChannel::RtpDataMediaChannel() {
86 Construct(NULL);
87}
88
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000089void RtpDataMediaChannel::Construct(rtc::Timing* timing) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 sending_ = false;
91 receiving_ = false;
92 timing_ = timing;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000093 send_limiter_.reset(new rtc::RateLimiter(kDataMaxBandwidth / 8, 1.0));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094}
95
96
97RtpDataMediaChannel::~RtpDataMediaChannel() {
98 std::map<uint32, RtpClock*>::const_iterator iter;
99 for (iter = rtp_clock_by_send_ssrc_.begin();
100 iter != rtp_clock_by_send_ssrc_.end();
101 ++iter) {
102 delete iter->second;
103 }
104}
105
106void RtpClock::Tick(
107 double now, int* seq_num, uint32* timestamp) {
108 *seq_num = ++last_seq_num_;
109 *timestamp = timestamp_offset_ + static_cast<uint32>(now * clockrate_);
110}
111
112const DataCodec* FindUnknownCodec(const std::vector<DataCodec>& codecs) {
113 DataCodec data_codec(kGoogleRtpDataCodecId, kGoogleRtpDataCodecName, 0);
114 std::vector<DataCodec>::const_iterator iter;
115 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
116 if (!iter->Matches(data_codec)) {
117 return &(*iter);
118 }
119 }
120 return NULL;
121}
122
123const DataCodec* FindKnownCodec(const std::vector<DataCodec>& codecs) {
124 DataCodec data_codec(kGoogleRtpDataCodecId, kGoogleRtpDataCodecName, 0);
125 std::vector<DataCodec>::const_iterator iter;
126 for (iter = codecs.begin(); iter != codecs.end(); ++iter) {
127 if (iter->Matches(data_codec)) {
128 return &(*iter);
129 }
130 }
131 return NULL;
132}
133
134bool RtpDataMediaChannel::SetRecvCodecs(const std::vector<DataCodec>& codecs) {
135 const DataCodec* unknown_codec = FindUnknownCodec(codecs);
136 if (unknown_codec) {
137 LOG(LS_WARNING) << "Failed to SetRecvCodecs because of unknown codec: "
138 << unknown_codec->ToString();
139 return false;
140 }
141
142 recv_codecs_ = codecs;
143 return true;
144}
145
146bool RtpDataMediaChannel::SetSendCodecs(const std::vector<DataCodec>& codecs) {
147 const DataCodec* known_codec = FindKnownCodec(codecs);
148 if (!known_codec) {
149 LOG(LS_WARNING) <<
150 "Failed to SetSendCodecs because there is no known codec.";
151 return false;
152 }
153
154 send_codecs_ = codecs;
155 return true;
156}
157
158bool RtpDataMediaChannel::AddSendStream(const StreamParams& stream) {
159 if (!stream.has_ssrcs()) {
160 return false;
161 }
162
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000163 if (GetStreamBySsrc(send_streams_, stream.first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 LOG(LS_WARNING) << "Not adding data send stream '" << stream.id
165 << "' with ssrc=" << stream.first_ssrc()
166 << " because stream already exists.";
167 return false;
168 }
169
170 send_streams_.push_back(stream);
171 // TODO(pthatcher): This should be per-stream, not per-ssrc.
172 // And we should probably allow more than one per stream.
173 rtp_clock_by_send_ssrc_[stream.first_ssrc()] = new RtpClock(
174 kDataCodecClockrate,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000175 rtc::CreateRandomNonZeroId(), rtc::CreateRandomNonZeroId());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176
177 LOG(LS_INFO) << "Added data send stream '" << stream.id
178 << "' with ssrc=" << stream.first_ssrc();
179 return true;
180}
181
182bool RtpDataMediaChannel::RemoveSendStream(uint32 ssrc) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000183 if (!GetStreamBySsrc(send_streams_, ssrc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184 return false;
185 }
186
187 RemoveStreamBySsrc(&send_streams_, ssrc);
188 delete rtp_clock_by_send_ssrc_[ssrc];
189 rtp_clock_by_send_ssrc_.erase(ssrc);
190 return true;
191}
192
193bool RtpDataMediaChannel::AddRecvStream(const StreamParams& stream) {
194 if (!stream.has_ssrcs()) {
195 return false;
196 }
197
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000198 if (GetStreamBySsrc(recv_streams_, stream.first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 LOG(LS_WARNING) << "Not adding data recv stream '" << stream.id
200 << "' with ssrc=" << stream.first_ssrc()
201 << " because stream already exists.";
202 return false;
203 }
204
205 recv_streams_.push_back(stream);
206 LOG(LS_INFO) << "Added data recv stream '" << stream.id
207 << "' with ssrc=" << stream.first_ssrc();
208 return true;
209}
210
211bool RtpDataMediaChannel::RemoveRecvStream(uint32 ssrc) {
212 RemoveStreamBySsrc(&recv_streams_, ssrc);
213 return true;
214}
215
wu@webrtc.orga9890802013-12-13 00:21:03 +0000216void RtpDataMediaChannel::OnPacketReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000217 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218 RtpHeader header;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000219 if (!GetRtpHeader(packet->data(), packet->size(), &header)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220 // Don't want to log for every corrupt packet.
221 // LOG(LS_WARNING) << "Could not read rtp header from packet of length "
222 // << packet->length() << ".";
223 return;
224 }
225
226 size_t header_length;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000227 if (!GetRtpHeaderLen(packet->data(), packet->size(), &header_length)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228 // Don't want to log for every corrupt packet.
229 // LOG(LS_WARNING) << "Could not read rtp header"
230 // << length from packet of length "
231 // << packet->length() << ".";
232 return;
233 }
234 const char* data = packet->data() + header_length + sizeof(kReservedSpace);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000235 size_t data_len = packet->size() - header_length - sizeof(kReservedSpace);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236
237 if (!receiving_) {
238 LOG(LS_WARNING) << "Not receiving packet "
239 << header.ssrc << ":" << header.seq_num
240 << " before SetReceive(true) called.";
241 return;
242 }
243
244 DataCodec codec;
245 if (!FindCodecById(recv_codecs_, header.payload_type, &codec)) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000246 // For bundling, this will be logged for every message.
247 // So disable this logging.
248 // LOG(LS_WARNING) << "Not receiving packet "
249 // << header.ssrc << ":" << header.seq_num
250 // << " (" << data_len << ")"
251 // << " because unknown payload id: " << header.payload_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252 return;
253 }
254
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000255 if (!GetStreamBySsrc(recv_streams_, header.ssrc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256 LOG(LS_WARNING) << "Received packet for unknown ssrc: " << header.ssrc;
257 return;
258 }
259
260 // Uncomment this for easy debugging.
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000261 // const auto* found_stream = GetStreamBySsrc(recv_streams_, header.ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262 // LOG(LS_INFO) << "Received packet"
263 // << " groupid=" << found_stream.groupid
264 // << ", ssrc=" << header.ssrc
265 // << ", seqnum=" << header.seq_num
266 // << ", timestamp=" << header.timestamp
267 // << ", len=" << data_len;
268
269 ReceiveDataParams params;
270 params.ssrc = header.ssrc;
271 params.seq_num = header.seq_num;
272 params.timestamp = header.timestamp;
273 SignalDataReceived(params, data, data_len);
274}
275
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000276bool RtpDataMediaChannel::SetMaxSendBandwidth(int bps) {
277 if (bps <= 0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278 bps = kDataMaxBandwidth;
279 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000280 send_limiter_.reset(new rtc::RateLimiter(bps / 8, 1.0));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 LOG(LS_INFO) << "RtpDataMediaChannel::SetSendBandwidth to " << bps << "bps.";
282 return true;
283}
284
285bool RtpDataMediaChannel::SendData(
286 const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000287 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288 SendDataResult* result) {
289 if (result) {
290 // If we return true, we'll set this to SDR_SUCCESS.
291 *result = SDR_ERROR;
292 }
293 if (!sending_) {
294 LOG(LS_WARNING) << "Not sending packet with ssrc=" << params.ssrc
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000295 << " len=" << payload.size() << " before SetSend(true).";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296 return false;
297 }
298
299 if (params.type != cricket::DMT_TEXT) {
300 LOG(LS_WARNING) << "Not sending data because binary type is unsupported.";
301 return false;
302 }
303
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000304 const StreamParams* found_stream =
305 GetStreamBySsrc(send_streams_, params.ssrc);
306 if (!found_stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307 LOG(LS_WARNING) << "Not sending data because ssrc is unknown: "
308 << params.ssrc;
309 return false;
310 }
311
312 DataCodec found_codec;
313 if (!FindCodecByName(send_codecs_, kGoogleRtpDataCodecName, &found_codec)) {
314 LOG(LS_WARNING) << "Not sending data because codec is unknown: "
315 << kGoogleRtpDataCodecName;
316 return false;
317 }
318
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000319 size_t packet_len = (kMinRtpPacketLen + sizeof(kReservedSpace) +
320 payload.size() + kMaxSrtpHmacOverhead);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321 if (packet_len > kDataMaxRtpPacketLen) {
322 return false;
323 }
324
325 double now = timing_->TimerNow();
326
327 if (!send_limiter_->CanUse(packet_len, now)) {
328 LOG(LS_VERBOSE) << "Dropped data packet of len=" << packet_len
329 << "; already sent " << send_limiter_->used_in_period()
330 << "/" << send_limiter_->max_per_period();
331 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332 }
333
334 RtpHeader header;
335 header.payload_type = found_codec.id;
336 header.ssrc = params.ssrc;
337 rtp_clock_by_send_ssrc_[header.ssrc]->Tick(
338 now, &header.seq_num, &header.timestamp);
339
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000340 rtc::Buffer packet;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341 packet.SetCapacity(packet_len);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000342 packet.SetSize(kMinRtpPacketLen);
343 if (!SetRtpHeader(packet.data(), packet.size(), header)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344 return false;
345 }
346 packet.AppendData(&kReservedSpace, sizeof(kReservedSpace));
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000347 packet.AppendData(payload.data(), payload.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000349 LOG(LS_VERBOSE) << "Sent RTP data packet: "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000350 << " stream=" << found_stream->id << " ssrc=" << header.ssrc
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000351 << ", seqnum=" << header.seq_num
352 << ", timestamp=" << header.timestamp
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000353 << ", len=" << payload.size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000355 MediaChannel::SendPacket(&packet);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356 send_limiter_->Use(packet_len, now);
357 if (result) {
358 *result = SDR_SUCCESS;
359 }
360 return true;
361}
362
363} // namespace cricket