OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/video_coding/test/rtp_player.h" | 11 #include "webrtc/modules/video_coding/test/rtp_player.h" |
12 | 12 |
13 #include <stdio.h> | 13 #include <stdio.h> |
14 | 14 |
15 #include <map> | 15 #include <map> |
16 | 16 |
17 #include "webrtc/base/scoped_ptr.h" | 17 #include "webrtc/base/scoped_ptr.h" |
18 #include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h" | 18 #include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h" |
19 #include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h" | 19 #include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h" |
20 #include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h" | 20 #include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h" |
21 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" | 21 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" |
22 #include "webrtc/modules/video_coding/internal_defines.h" | 22 #include "webrtc/modules/video_coding/internal_defines.h" |
23 #include "webrtc/modules/video_coding/test/test_util.h" | 23 #include "webrtc/modules/video_coding/test/test_util.h" |
24 #include "webrtc/system_wrappers/include/clock.h" | 24 #include "webrtc/system_wrappers/include/clock.h" |
25 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | 25 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
26 #include "webrtc/test/rtp_file_reader.h" | 26 #include "webrtc/test/rtp_file_reader.h" |
27 | 27 |
28 #if 1 | 28 #if 1 |
29 # define DEBUG_LOG1(text, arg) | 29 #define DEBUG_LOG1(text, arg) |
30 #else | 30 #else |
31 # define DEBUG_LOG1(text, arg) (printf(text "\n", arg)) | 31 #define DEBUG_LOG1(text, arg) (printf(text "\n", arg)) |
32 #endif | 32 #endif |
33 | 33 |
34 namespace webrtc { | 34 namespace webrtc { |
35 namespace rtpplayer { | 35 namespace rtpplayer { |
36 | 36 |
37 enum { | 37 enum { |
38 kMaxPacketBufferSize = 4096, | 38 kMaxPacketBufferSize = 4096, |
39 kDefaultTransmissionTimeOffsetExtensionId = 2 | 39 kDefaultTransmissionTimeOffsetExtensionId = 2 |
40 }; | 40 }; |
41 | 41 |
42 class RawRtpPacket { | 42 class RawRtpPacket { |
43 public: | 43 public: |
44 RawRtpPacket(const uint8_t* data, size_t length, uint32_t ssrc, | 44 RawRtpPacket(const uint8_t* data, |
| 45 size_t length, |
| 46 uint32_t ssrc, |
45 uint16_t seq_num) | 47 uint16_t seq_num) |
46 : data_(new uint8_t[length]), | 48 : data_(new uint8_t[length]), |
47 length_(length), | 49 length_(length), |
48 resend_time_ms_(-1), | 50 resend_time_ms_(-1), |
49 ssrc_(ssrc), | 51 ssrc_(ssrc), |
50 seq_num_(seq_num) { | 52 seq_num_(seq_num) { |
51 assert(data); | 53 assert(data); |
52 memcpy(data_.get(), data, length_); | 54 memcpy(data_.get(), data, length_); |
53 } | 55 } |
54 | 56 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 return packet; | 135 return packet; |
134 } | 136 } |
135 } | 137 } |
136 return NULL; | 138 return NULL; |
137 } | 139 } |
138 | 140 |
139 int NumberOfPacketsToResend() const { | 141 int NumberOfPacketsToResend() const { |
140 CriticalSectionScoped cs(crit_sect_.get()); | 142 CriticalSectionScoped cs(crit_sect_.get()); |
141 int count = 0; | 143 int count = 0; |
142 for (ConstRtpPacketIterator it = packets_.begin(); it != packets_.end(); | 144 for (ConstRtpPacketIterator it = packets_.begin(); it != packets_.end(); |
143 ++it) { | 145 ++it) { |
144 if ((*it)->resend_time_ms() >= 0) { | 146 if ((*it)->resend_time_ms() >= 0) { |
145 count++; | 147 count++; |
146 } | 148 } |
147 } | 149 } |
148 return count; | 150 return count; |
149 } | 151 } |
150 | 152 |
151 void LogPacketResent(RawRtpPacket* packet) { | 153 void LogPacketResent(RawRtpPacket* packet) { |
152 int64_t now_ms = clock_->TimeInMilliseconds(); | 154 int64_t now_ms = clock_->TimeInMilliseconds(); |
153 CriticalSectionScoped cs(crit_sect_.get()); | 155 CriticalSectionScoped cs(crit_sect_.get()); |
154 if (debug_file_) { | 156 if (debug_file_) { |
155 fprintf(debug_file_, "Resent %u at %u\n", packet->seq_num(), | 157 fprintf(debug_file_, "Resent %u at %u\n", packet->seq_num(), |
156 MaskWord64ToUWord32(now_ms)); | 158 MaskWord64ToUWord32(now_ms)); |
157 } | 159 } |
158 } | 160 } |
159 | 161 |
160 void Print() const { | 162 void Print() const { |
161 CriticalSectionScoped cs(crit_sect_.get()); | 163 CriticalSectionScoped cs(crit_sect_.get()); |
162 printf("Lost packets: %u\n", loss_count_); | 164 printf("Lost packets: %u\n", loss_count_); |
163 printf("Packets waiting to be resent: %d\n", NumberOfPacketsToResend()); | 165 printf("Packets waiting to be resent: %d\n", NumberOfPacketsToResend()); |
164 printf("Packets still lost: %zd\n", packets_.size()); | 166 printf("Packets still lost: %zd\n", packets_.size()); |
165 printf("Sequence numbers:\n"); | 167 printf("Sequence numbers:\n"); |
166 for (ConstRtpPacketIterator it = packets_.begin(); it != packets_.end(); | 168 for (ConstRtpPacketIterator it = packets_.begin(); it != packets_.end(); |
167 ++it) { | 169 ++it) { |
168 printf("%u, ", (*it)->seq_num()); | 170 printf("%u, ", (*it)->seq_num()); |
169 } | 171 } |
170 printf("\n"); | 172 printf("\n"); |
171 } | 173 } |
172 | 174 |
173 private: | 175 private: |
174 typedef std::vector<RawRtpPacket*> RtpPacketList; | 176 typedef std::vector<RawRtpPacket*> RtpPacketList; |
175 typedef RtpPacketList::iterator RtpPacketIterator; | 177 typedef RtpPacketList::iterator RtpPacketIterator; |
176 typedef RtpPacketList::const_iterator ConstRtpPacketIterator; | 178 typedef RtpPacketList::const_iterator ConstRtpPacketIterator; |
177 | 179 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 if (handler->rtp_module_.get() == NULL) { | 226 if (handler->rtp_module_.get() == NULL) { |
225 return -1; | 227 return -1; |
226 } | 228 } |
227 | 229 |
228 handler->rtp_module_->SetNACKStatus(kNackOff); | 230 handler->rtp_module_->SetNACKStatus(kNackOff); |
229 handler->rtp_header_parser_->RegisterRtpHeaderExtension( | 231 handler->rtp_header_parser_->RegisterRtpHeaderExtension( |
230 kRtpExtensionTransmissionTimeOffset, | 232 kRtpExtensionTransmissionTimeOffset, |
231 kDefaultTransmissionTimeOffsetExtensionId); | 233 kDefaultTransmissionTimeOffsetExtensionId); |
232 | 234 |
233 for (PayloadTypesIterator it = payload_types_.begin(); | 235 for (PayloadTypesIterator it = payload_types_.begin(); |
234 it != payload_types_.end(); ++it) { | 236 it != payload_types_.end(); ++it) { |
235 VideoCodec codec; | 237 VideoCodec codec; |
236 memset(&codec, 0, sizeof(codec)); | 238 memset(&codec, 0, sizeof(codec)); |
237 strncpy(codec.plName, it->name().c_str(), sizeof(codec.plName)-1); | 239 strncpy(codec.plName, it->name().c_str(), sizeof(codec.plName) - 1); |
238 codec.plType = it->payload_type(); | 240 codec.plType = it->payload_type(); |
239 codec.codecType = it->codec_type(); | 241 codec.codecType = it->codec_type(); |
240 if (handler->rtp_module_->RegisterReceivePayload(codec.plName, | 242 if (handler->rtp_module_->RegisterReceivePayload( |
241 codec.plType, | 243 codec.plName, codec.plType, 90000, 0, codec.maxBitrate) < 0) { |
242 90000, | |
243 0, | |
244 codec.maxBitrate) < 0) { | |
245 return -1; | 244 return -1; |
246 } | 245 } |
247 } | 246 } |
248 | 247 |
249 handlers_[ssrc] = handler.release(); | 248 handlers_[ssrc] = handler.release(); |
250 return 0; | 249 return 0; |
251 } | 250 } |
252 | 251 |
253 void IncomingPacket(const uint8_t* data, size_t length) { | 252 void IncomingPacket(const uint8_t* data, size_t length) { |
254 for (HandlerMapIt it = handlers_.begin(); it != handlers_.end(); ++it) { | 253 for (HandlerMapIt it = handlers_.begin(); it != handlers_.end(); ++it) { |
255 if (!it->second->rtp_header_parser_->IsRtcp(data, length)) { | 254 if (!it->second->rtp_header_parser_->IsRtcp(data, length)) { |
256 RTPHeader header; | 255 RTPHeader header; |
257 it->second->rtp_header_parser_->Parse(data, length, &header); | 256 it->second->rtp_header_parser_->Parse(data, length, &header); |
258 PayloadUnion payload_specific; | 257 PayloadUnion payload_specific; |
259 it->second->rtp_payload_registry_->GetPayloadSpecifics( | 258 it->second->rtp_payload_registry_->GetPayloadSpecifics( |
260 header.payloadType, &payload_specific); | 259 header.payloadType, &payload_specific); |
261 it->second->rtp_module_->IncomingRtpPacket(header, data, length, | 260 it->second->rtp_module_->IncomingRtpPacket(header, data, length, |
262 payload_specific, true); | 261 payload_specific, true); |
263 } | 262 } |
264 } | 263 } |
265 } | 264 } |
266 | 265 |
267 private: | 266 private: |
268 class Handler : public RtpStreamInterface { | 267 class Handler : public RtpStreamInterface { |
269 public: | 268 public: |
270 Handler(uint32_t ssrc, const PayloadTypes& payload_types, | 269 Handler(uint32_t ssrc, |
| 270 const PayloadTypes& payload_types, |
271 LostPackets* lost_packets) | 271 LostPackets* lost_packets) |
272 : rtp_header_parser_(RtpHeaderParser::Create()), | 272 : rtp_header_parser_(RtpHeaderParser::Create()), |
273 rtp_payload_registry_(new RTPPayloadRegistry( | 273 rtp_payload_registry_(new RTPPayloadRegistry( |
274 RTPPayloadStrategy::CreateStrategy(false))), | 274 RTPPayloadStrategy::CreateStrategy(false))), |
275 rtp_module_(), | 275 rtp_module_(), |
276 payload_sink_(), | 276 payload_sink_(), |
277 ssrc_(ssrc), | 277 ssrc_(ssrc), |
278 payload_types_(payload_types), | 278 payload_types_(payload_types), |
279 lost_packets_(lost_packets) { | 279 lost_packets_(lost_packets) { |
280 assert(lost_packets); | 280 assert(lost_packets); |
281 } | 281 } |
282 virtual ~Handler() {} | 282 virtual ~Handler() {} |
283 | 283 |
284 virtual void ResendPackets(const uint16_t* sequence_numbers, | 284 virtual void ResendPackets(const uint16_t* sequence_numbers, |
285 uint16_t length) { | 285 uint16_t length) { |
286 assert(sequence_numbers); | 286 assert(sequence_numbers); |
287 for (uint16_t i = 0; i < length; i++) { | 287 for (uint16_t i = 0; i < length; i++) { |
288 lost_packets_->SetResendTime(ssrc_, sequence_numbers[i]); | 288 lost_packets_->SetResendTime(ssrc_, sequence_numbers[i]); |
289 } | 289 } |
290 } | 290 } |
291 | 291 |
292 virtual uint32_t ssrc() const { return ssrc_; } | 292 virtual uint32_t ssrc() const { return ssrc_; } |
293 virtual const PayloadTypes& payload_types() const { | 293 virtual const PayloadTypes& payload_types() const { return payload_types_; } |
294 return payload_types_; | |
295 } | |
296 | 294 |
297 rtc::scoped_ptr<RtpHeaderParser> rtp_header_parser_; | 295 rtc::scoped_ptr<RtpHeaderParser> rtp_header_parser_; |
298 rtc::scoped_ptr<RTPPayloadRegistry> rtp_payload_registry_; | 296 rtc::scoped_ptr<RTPPayloadRegistry> rtp_payload_registry_; |
299 rtc::scoped_ptr<RtpReceiver> rtp_module_; | 297 rtc::scoped_ptr<RtpReceiver> rtp_module_; |
300 rtc::scoped_ptr<PayloadSinkInterface> payload_sink_; | 298 rtc::scoped_ptr<PayloadSinkInterface> payload_sink_; |
301 | 299 |
302 private: | 300 private: |
303 uint32_t ssrc_; | 301 uint32_t ssrc_; |
304 const PayloadTypes& payload_types_; | 302 const PayloadTypes& payload_types_; |
305 LostPackets* lost_packets_; | 303 LostPackets* lost_packets_; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 assert(packet_source->get()); | 342 assert(packet_source->get()); |
345 packet_source_.swap(*packet_source); | 343 packet_source_.swap(*packet_source); |
346 srand(321); | 344 srand(321); |
347 } | 345 } |
348 | 346 |
349 virtual ~RtpPlayerImpl() {} | 347 virtual ~RtpPlayerImpl() {} |
350 | 348 |
351 virtual int NextPacket(int64_t time_now) { | 349 virtual int NextPacket(int64_t time_now) { |
352 // Send any packets ready to be resent. | 350 // Send any packets ready to be resent. |
353 for (RawRtpPacket* packet = lost_packets_.NextPacketToResend(time_now); | 351 for (RawRtpPacket* packet = lost_packets_.NextPacketToResend(time_now); |
354 packet != NULL; | 352 packet != NULL; packet = lost_packets_.NextPacketToResend(time_now)) { |
355 packet = lost_packets_.NextPacketToResend(time_now)) { | |
356 int ret = SendPacket(packet->data(), packet->length()); | 353 int ret = SendPacket(packet->data(), packet->length()); |
357 if (ret > 0) { | 354 if (ret > 0) { |
358 printf("Resend: %08x:%u\n", packet->ssrc(), packet->seq_num()); | 355 printf("Resend: %08x:%u\n", packet->ssrc(), packet->seq_num()); |
359 lost_packets_.LogPacketResent(packet); | 356 lost_packets_.LogPacketResent(packet); |
360 resend_packet_count_++; | 357 resend_packet_count_++; |
361 } | 358 } |
362 delete packet; | 359 delete packet; |
363 if (ret < 0) { | 360 if (ret < 0) { |
364 return ret; | 361 return ret; |
365 } | 362 } |
(...skipping 19 matching lines...) Expand all Loading... |
385 SendPacket(reorder_buffer_->data(), reorder_buffer_->length()); | 382 SendPacket(reorder_buffer_->data(), reorder_buffer_->length()); |
386 reorder_buffer_.reset(NULL); | 383 reorder_buffer_.reset(NULL); |
387 } | 384 } |
388 if (ret < 0) { | 385 if (ret < 0) { |
389 return ret; | 386 return ret; |
390 } | 387 } |
391 | 388 |
392 if (!packet_source_->NextPacket(&next_packet_)) { | 389 if (!packet_source_->NextPacket(&next_packet_)) { |
393 end_of_file_ = true; | 390 end_of_file_ = true; |
394 return 0; | 391 return 0; |
395 } | 392 } else if (next_packet_.length == 0) { |
396 else if (next_packet_.length == 0) { | |
397 return 0; | 393 return 0; |
398 } | 394 } |
399 } | 395 } |
400 | 396 |
401 if (end_of_file_ && lost_packets_.NumberOfPacketsToResend() == 0) { | 397 if (end_of_file_ && lost_packets_.NumberOfPacketsToResend() == 0) { |
402 return 1; | 398 return 1; |
403 } | 399 } |
404 return 0; | 400 return 0; |
405 } | 401 } |
406 | 402 |
407 virtual uint32_t TimeUntilNextPacket() const { | 403 virtual uint32_t TimeUntilNextPacket() const { |
408 int64_t time_left = (next_rtp_time_ - first_packet_rtp_time_) - | 404 int64_t time_left = (next_rtp_time_ - first_packet_rtp_time_) - |
409 (clock_->TimeInMilliseconds() - first_packet_time_ms_); | 405 (clock_->TimeInMilliseconds() - first_packet_time_ms_); |
410 if (time_left < 0) { | 406 if (time_left < 0) { |
411 return 0; | 407 return 0; |
412 } | 408 } |
413 return static_cast<uint32_t>(time_left); | 409 return static_cast<uint32_t>(time_left); |
414 } | 410 } |
415 | 411 |
416 virtual void Print() const { | 412 virtual void Print() const { |
417 printf("Resent packets: %u\n", resend_packet_count_); | 413 printf("Resent packets: %u\n", resend_packet_count_); |
418 lost_packets_.Print(); | 414 lost_packets_.Print(); |
419 } | 415 } |
(...skipping 11 matching lines...) Expand all Loading... |
431 return -1; | 427 return -1; |
432 } | 428 } |
433 uint32_t ssrc = header.ssrc; | 429 uint32_t ssrc = header.ssrc; |
434 if (ssrc_handlers_.RegisterSsrc(ssrc, &lost_packets_, clock_) < 0) { | 430 if (ssrc_handlers_.RegisterSsrc(ssrc, &lost_packets_, clock_) < 0) { |
435 DEBUG_LOG1("Unable to register ssrc: %d", ssrc); | 431 DEBUG_LOG1("Unable to register ssrc: %d", ssrc); |
436 return -1; | 432 return -1; |
437 } | 433 } |
438 | 434 |
439 if (no_loss_startup_ > 0) { | 435 if (no_loss_startup_ > 0) { |
440 no_loss_startup_--; | 436 no_loss_startup_--; |
441 } else if ((rand() + 1.0)/(RAND_MAX + 1.0) < loss_rate_) { | 437 } else if ((rand() + 1.0) / (RAND_MAX + 1.0) < loss_rate_) { // NOLINT |
442 uint16_t seq_num = header.sequenceNumber; | 438 uint16_t seq_num = header.sequenceNumber; |
443 lost_packets_.AddPacket(new RawRtpPacket(data, length, ssrc, seq_num)); | 439 lost_packets_.AddPacket(new RawRtpPacket(data, length, ssrc, seq_num)); |
444 DEBUG_LOG1("Dropped packet: %d!", header.header.sequenceNumber); | 440 DEBUG_LOG1("Dropped packet: %d!", header.header.sequenceNumber); |
445 return 0; | 441 return 0; |
446 } | 442 } |
447 } | 443 } |
448 | 444 |
449 ssrc_handlers_.IncomingPacket(data, length); | 445 ssrc_handlers_.IncomingPacket(data, length); |
450 return 1; | 446 return 1; |
451 } | 447 } |
(...skipping 11 matching lines...) Expand all Loading... |
463 uint32_t resend_packet_count_; | 459 uint32_t resend_packet_count_; |
464 uint32_t no_loss_startup_; | 460 uint32_t no_loss_startup_; |
465 bool end_of_file_; | 461 bool end_of_file_; |
466 bool reordering_; | 462 bool reordering_; |
467 rtc::scoped_ptr<RawRtpPacket> reorder_buffer_; | 463 rtc::scoped_ptr<RawRtpPacket> reorder_buffer_; |
468 | 464 |
469 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RtpPlayerImpl); | 465 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RtpPlayerImpl); |
470 }; | 466 }; |
471 | 467 |
472 RtpPlayerInterface* Create(const std::string& input_filename, | 468 RtpPlayerInterface* Create(const std::string& input_filename, |
473 PayloadSinkFactoryInterface* payload_sink_factory, Clock* clock, | 469 PayloadSinkFactoryInterface* payload_sink_factory, |
474 const PayloadTypes& payload_types, float loss_rate, int64_t rtt_ms, | 470 Clock* clock, |
475 bool reordering) { | 471 const PayloadTypes& payload_types, |
| 472 float loss_rate, |
| 473 int64_t rtt_ms, |
| 474 bool reordering) { |
476 rtc::scoped_ptr<test::RtpFileReader> packet_source( | 475 rtc::scoped_ptr<test::RtpFileReader> packet_source( |
477 test::RtpFileReader::Create(test::RtpFileReader::kRtpDump, | 476 test::RtpFileReader::Create(test::RtpFileReader::kRtpDump, |
478 input_filename)); | 477 input_filename)); |
479 if (packet_source.get() == NULL) { | 478 if (packet_source.get() == NULL) { |
480 packet_source.reset(test::RtpFileReader::Create(test::RtpFileReader::kPcap, | 479 packet_source.reset(test::RtpFileReader::Create(test::RtpFileReader::kPcap, |
481 input_filename)); | 480 input_filename)); |
482 if (packet_source.get() == NULL) { | 481 if (packet_source.get() == NULL) { |
483 return NULL; | 482 return NULL; |
484 } | 483 } |
485 } | 484 } |
486 | 485 |
487 rtc::scoped_ptr<RtpPlayerImpl> impl( | 486 rtc::scoped_ptr<RtpPlayerImpl> impl( |
488 new RtpPlayerImpl(payload_sink_factory, payload_types, clock, | 487 new RtpPlayerImpl(payload_sink_factory, payload_types, clock, |
489 &packet_source, loss_rate, rtt_ms, reordering)); | 488 &packet_source, loss_rate, rtt_ms, reordering)); |
490 return impl.release(); | 489 return impl.release(); |
491 } | 490 } |
492 } // namespace rtpplayer | 491 } // namespace rtpplayer |
493 } // namespace webrtc | 492 } // namespace webrtc |
OLD | NEW |