Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Side by Side Diff: webrtc/modules/video_coding/test/rtp_player.cc

Issue 2748183006: Delete unused test code in modules/video_coding/test/ (Closed)
Patch Set: Make NullEvent private so that more tests won't start using it. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
11 #include "webrtc/modules/video_coding/test/rtp_player.h"
12
13 #include <stdio.h>
14
15 #include <cstdlib>
16 #include <map>
17 #include <memory>
18
19 #include "webrtc/base/constructormagic.h"
20 #include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
21 #include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
22 #include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
23 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
24 #include "webrtc/modules/video_coding/internal_defines.h"
25 #include "webrtc/modules/video_coding/test/test_util.h"
26 #include "webrtc/system_wrappers/include/clock.h"
27 #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
28 #include "webrtc/test/rtp_file_reader.h"
29
30 #if 1
31 #define DEBUG_LOG1(text, arg)
32 #else
33 #define DEBUG_LOG1(text, arg) (printf(text "\n", arg))
34 #endif
35
36 namespace webrtc {
37 namespace rtpplayer {
38
39 enum {
40 kMaxPacketBufferSize = 4096,
41 kDefaultTransmissionTimeOffsetExtensionId = 2
42 };
43
44 class RawRtpPacket {
45 public:
46 RawRtpPacket(const uint8_t* data,
47 size_t length,
48 uint32_t ssrc,
49 uint16_t seq_num)
50 : data_(new uint8_t[length]),
51 length_(length),
52 resend_time_ms_(-1),
53 ssrc_(ssrc),
54 seq_num_(seq_num) {
55 assert(data);
56 memcpy(data_.get(), data, length_);
57 }
58
59 const uint8_t* data() const { return data_.get(); }
60 size_t length() const { return length_; }
61 int64_t resend_time_ms() const { return resend_time_ms_; }
62 void set_resend_time_ms(int64_t timeMs) { resend_time_ms_ = timeMs; }
63 uint32_t ssrc() const { return ssrc_; }
64 uint16_t seq_num() const { return seq_num_; }
65
66 private:
67 std::unique_ptr<uint8_t[]> data_;
68 size_t length_;
69 int64_t resend_time_ms_;
70 uint32_t ssrc_;
71 uint16_t seq_num_;
72
73 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RawRtpPacket);
74 };
75
76 class LostPackets {
77 public:
78 LostPackets(Clock* clock, int64_t rtt_ms)
79 : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
80 debug_file_(fopen("PacketLossDebug.txt", "w")),
81 loss_count_(0),
82 packets_(),
83 clock_(clock),
84 rtt_ms_(rtt_ms) {
85 assert(clock);
86 }
87
88 ~LostPackets() {
89 if (debug_file_) {
90 fclose(debug_file_);
91 debug_file_ = NULL;
92 }
93 while (!packets_.empty()) {
94 delete packets_.back();
95 packets_.pop_back();
96 }
97 }
98
99 void AddPacket(RawRtpPacket* packet) {
100 assert(packet);
101 printf("Throw: %08x:%u\n", packet->ssrc(), packet->seq_num());
102 CriticalSectionScoped cs(crit_sect_.get());
103 if (debug_file_) {
104 fprintf(debug_file_, "%u Lost packet: %u\n", loss_count_,
105 packet->seq_num());
106 }
107 packets_.push_back(packet);
108 loss_count_++;
109 }
110
111 void SetResendTime(uint32_t ssrc, int16_t resendSeqNum) {
112 int64_t resend_time_ms = clock_->TimeInMilliseconds() + rtt_ms_;
113 int64_t now_ms = clock_->TimeInMilliseconds();
114 CriticalSectionScoped cs(crit_sect_.get());
115 for (RtpPacketIterator it = packets_.begin(); it != packets_.end(); ++it) {
116 RawRtpPacket* packet = *it;
117 if (ssrc == packet->ssrc() && resendSeqNum == packet->seq_num() &&
118 packet->resend_time_ms() + 10 < now_ms) {
119 if (debug_file_) {
120 fprintf(debug_file_, "Resend %u at %u\n", packet->seq_num(),
121 MaskWord64ToUWord32(resend_time_ms));
122 }
123 packet->set_resend_time_ms(resend_time_ms);
124 return;
125 }
126 }
127 // We may get here since the captured stream may itself be missing packets.
128 }
129
130 RawRtpPacket* NextPacketToResend(int64_t time_now) {
131 CriticalSectionScoped cs(crit_sect_.get());
132 for (RtpPacketIterator it = packets_.begin(); it != packets_.end(); ++it) {
133 RawRtpPacket* packet = *it;
134 if (time_now >= packet->resend_time_ms() &&
135 packet->resend_time_ms() != -1) {
136 packets_.erase(it);
137 return packet;
138 }
139 }
140 return NULL;
141 }
142
143 int NumberOfPacketsToResend() const {
144 CriticalSectionScoped cs(crit_sect_.get());
145 int count = 0;
146 for (ConstRtpPacketIterator it = packets_.begin(); it != packets_.end();
147 ++it) {
148 if ((*it)->resend_time_ms() >= 0) {
149 count++;
150 }
151 }
152 return count;
153 }
154
155 void LogPacketResent(RawRtpPacket* packet) {
156 int64_t now_ms = clock_->TimeInMilliseconds();
157 CriticalSectionScoped cs(crit_sect_.get());
158 if (debug_file_) {
159 fprintf(debug_file_, "Resent %u at %u\n", packet->seq_num(),
160 MaskWord64ToUWord32(now_ms));
161 }
162 }
163
164 void Print() const {
165 CriticalSectionScoped cs(crit_sect_.get());
166 printf("Lost packets: %u\n", loss_count_);
167 printf("Packets waiting to be resent: %d\n", NumberOfPacketsToResend());
168 printf("Packets still lost: %zd\n", packets_.size());
169 printf("Sequence numbers:\n");
170 for (ConstRtpPacketIterator it = packets_.begin(); it != packets_.end();
171 ++it) {
172 printf("%u, ", (*it)->seq_num());
173 }
174 printf("\n");
175 }
176
177 private:
178 typedef std::vector<RawRtpPacket*> RtpPacketList;
179 typedef RtpPacketList::iterator RtpPacketIterator;
180 typedef RtpPacketList::const_iterator ConstRtpPacketIterator;
181
182 std::unique_ptr<CriticalSectionWrapper> crit_sect_;
183 FILE* debug_file_;
184 int loss_count_;
185 RtpPacketList packets_;
186 Clock* clock_;
187 int64_t rtt_ms_;
188
189 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(LostPackets);
190 };
191
192 class SsrcHandlers {
193 public:
194 SsrcHandlers(PayloadSinkFactoryInterface* payload_sink_factory,
195 const PayloadTypes& payload_types)
196 : payload_sink_factory_(payload_sink_factory),
197 payload_types_(payload_types),
198 handlers_() {
199 assert(payload_sink_factory);
200 }
201
202 ~SsrcHandlers() {
203 while (!handlers_.empty()) {
204 delete handlers_.begin()->second;
205 handlers_.erase(handlers_.begin());
206 }
207 }
208
209 int RegisterSsrc(uint32_t ssrc, LostPackets* lost_packets, Clock* clock) {
210 if (handlers_.count(ssrc) > 0) {
211 return 0;
212 }
213 DEBUG_LOG1("Registering handler for ssrc=%08x", ssrc);
214
215 std::unique_ptr<Handler> handler(
216 new Handler(ssrc, payload_types_, lost_packets));
217 handler->payload_sink_.reset(payload_sink_factory_->Create(handler.get()));
218 if (handler->payload_sink_.get() == NULL) {
219 return -1;
220 }
221
222 RtpRtcp::Configuration configuration;
223 configuration.clock = clock;
224 configuration.audio = false;
225 handler->rtp_module_.reset(RtpReceiver::CreateVideoReceiver(
226 configuration.clock, handler->payload_sink_.get(), NULL,
227 handler->rtp_payload_registry_.get()));
228 if (handler->rtp_module_.get() == NULL) {
229 return -1;
230 }
231
232 handler->rtp_header_parser_->RegisterRtpHeaderExtension(
233 kRtpExtensionTransmissionTimeOffset,
234 kDefaultTransmissionTimeOffsetExtensionId);
235
236 for (PayloadTypesIterator it = payload_types_.begin();
237 it != payload_types_.end(); ++it) {
238 VideoCodec codec;
239 memset(&codec, 0, sizeof(codec));
240 strncpy(codec.plName, it->name().c_str(), sizeof(codec.plName) - 1);
241 codec.plType = it->payload_type();
242 codec.codecType = it->codec_type();
243 if (handler->rtp_module_->RegisterReceivePayload(
244 codec.plName, codec.plType, 90000, 0, codec.maxBitrate) < 0) {
245 return -1;
246 }
247 }
248
249 handlers_[ssrc] = handler.release();
250 return 0;
251 }
252
253 void IncomingPacket(const uint8_t* data, size_t length) {
254 for (HandlerMapIt it = handlers_.begin(); it != handlers_.end(); ++it) {
255 if (!it->second->rtp_header_parser_->IsRtcp(data, length)) {
256 RTPHeader header;
257 it->second->rtp_header_parser_->Parse(data, length, &header);
258 PayloadUnion payload_specific;
259 it->second->rtp_payload_registry_->GetPayloadSpecifics(
260 header.payloadType, &payload_specific);
261 it->second->rtp_module_->IncomingRtpPacket(header, data, length,
262 payload_specific, true);
263 }
264 }
265 }
266
267 private:
268 class Handler : public RtpStreamInterface {
269 public:
270 Handler(uint32_t ssrc,
271 const PayloadTypes& payload_types,
272 LostPackets* lost_packets)
273 : rtp_header_parser_(RtpHeaderParser::Create()),
274 rtp_payload_registry_(new RTPPayloadRegistry()),
275 rtp_module_(),
276 payload_sink_(),
277 ssrc_(ssrc),
278 payload_types_(payload_types),
279 lost_packets_(lost_packets) {
280 assert(lost_packets);
281 }
282 virtual ~Handler() {}
283
284 virtual void ResendPackets(const uint16_t* sequence_numbers,
285 uint16_t length) {
286 assert(sequence_numbers);
287 for (uint16_t i = 0; i < length; i++) {
288 lost_packets_->SetResendTime(ssrc_, sequence_numbers[i]);
289 }
290 }
291
292 virtual uint32_t ssrc() const { return ssrc_; }
293 virtual const PayloadTypes& payload_types() const { return payload_types_; }
294
295 std::unique_ptr<RtpHeaderParser> rtp_header_parser_;
296 std::unique_ptr<RTPPayloadRegistry> rtp_payload_registry_;
297 std::unique_ptr<RtpReceiver> rtp_module_;
298 std::unique_ptr<PayloadSinkInterface> payload_sink_;
299
300 private:
301 uint32_t ssrc_;
302 const PayloadTypes& payload_types_;
303 LostPackets* lost_packets_;
304
305 RTC_DISALLOW_COPY_AND_ASSIGN(Handler);
306 };
307
308 typedef std::map<uint32_t, Handler*> HandlerMap;
309 typedef std::map<uint32_t, Handler*>::iterator HandlerMapIt;
310
311 PayloadSinkFactoryInterface* payload_sink_factory_;
312 PayloadTypes payload_types_;
313 HandlerMap handlers_;
314
315 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(SsrcHandlers);
316 };
317
318 class RtpPlayerImpl : public RtpPlayerInterface {
319 public:
320 RtpPlayerImpl(PayloadSinkFactoryInterface* payload_sink_factory,
321 const PayloadTypes& payload_types,
322 Clock* clock,
323 std::unique_ptr<test::RtpFileReader>* packet_source,
324 float loss_rate,
325 int64_t rtt_ms,
326 bool reordering)
327 : ssrc_handlers_(payload_sink_factory, payload_types),
328 clock_(clock),
329 next_rtp_time_(0),
330 first_packet_(true),
331 first_packet_rtp_time_(0),
332 first_packet_time_ms_(0),
333 loss_rate_(loss_rate),
334 lost_packets_(clock, rtt_ms),
335 resend_packet_count_(0),
336 no_loss_startup_(100),
337 end_of_file_(false),
338 reordering_(false),
339 reorder_buffer_() {
340 assert(clock);
341 assert(packet_source);
342 assert(packet_source->get());
343 packet_source_.swap(*packet_source);
344 std::srand(321);
345 }
346
347 virtual ~RtpPlayerImpl() {}
348
349 virtual int NextPacket(int64_t time_now) {
350 // Send any packets ready to be resent.
351 for (RawRtpPacket* packet = lost_packets_.NextPacketToResend(time_now);
352 packet != NULL; packet = lost_packets_.NextPacketToResend(time_now)) {
353 int ret = SendPacket(packet->data(), packet->length());
354 if (ret > 0) {
355 printf("Resend: %08x:%u\n", packet->ssrc(), packet->seq_num());
356 lost_packets_.LogPacketResent(packet);
357 resend_packet_count_++;
358 }
359 delete packet;
360 if (ret < 0) {
361 return ret;
362 }
363 }
364
365 // Send any packets from packet source.
366 if (!end_of_file_ && (TimeUntilNextPacket() == 0 || first_packet_)) {
367 if (first_packet_) {
368 if (!packet_source_->NextPacket(&next_packet_))
369 return 0;
370 first_packet_rtp_time_ = next_packet_.time_ms;
371 first_packet_time_ms_ = clock_->TimeInMilliseconds();
372 first_packet_ = false;
373 }
374
375 if (reordering_ && reorder_buffer_.get() == NULL) {
376 reorder_buffer_.reset(
377 new RawRtpPacket(next_packet_.data, next_packet_.length, 0, 0));
378 return 0;
379 }
380 int ret = SendPacket(next_packet_.data, next_packet_.length);
381 if (reorder_buffer_.get()) {
382 SendPacket(reorder_buffer_->data(), reorder_buffer_->length());
383 reorder_buffer_.reset(NULL);
384 }
385 if (ret < 0) {
386 return ret;
387 }
388
389 if (!packet_source_->NextPacket(&next_packet_)) {
390 end_of_file_ = true;
391 return 0;
392 } else if (next_packet_.length == 0) {
393 return 0;
394 }
395 }
396
397 if (end_of_file_ && lost_packets_.NumberOfPacketsToResend() == 0) {
398 return 1;
399 }
400 return 0;
401 }
402
403 virtual uint32_t TimeUntilNextPacket() const {
404 int64_t time_left = (next_rtp_time_ - first_packet_rtp_time_) -
405 (clock_->TimeInMilliseconds() - first_packet_time_ms_);
406 if (time_left < 0) {
407 return 0;
408 }
409 return static_cast<uint32_t>(time_left);
410 }
411
412 virtual void Print() const {
413 printf("Resent packets: %u\n", resend_packet_count_);
414 lost_packets_.Print();
415 }
416
417 private:
418 int SendPacket(const uint8_t* data, size_t length) {
419 assert(data);
420 assert(length > 0);
421
422 std::unique_ptr<RtpHeaderParser> rtp_header_parser(
423 RtpHeaderParser::Create());
424 if (!rtp_header_parser->IsRtcp(data, length)) {
425 RTPHeader header;
426 if (!rtp_header_parser->Parse(data, length, &header)) {
427 return -1;
428 }
429 uint32_t ssrc = header.ssrc;
430 if (ssrc_handlers_.RegisterSsrc(ssrc, &lost_packets_, clock_) < 0) {
431 DEBUG_LOG1("Unable to register ssrc: %d", ssrc);
432 return -1;
433 }
434
435 if (no_loss_startup_ > 0) {
436 no_loss_startup_--;
437 } else if ((std::rand() + 1.0) / (RAND_MAX + 1.0) <
438 loss_rate_) { // NOLINT
439 uint16_t seq_num = header.sequenceNumber;
440 lost_packets_.AddPacket(new RawRtpPacket(data, length, ssrc, seq_num));
441 DEBUG_LOG1("Dropped packet: %d!", header.header.sequenceNumber);
442 return 0;
443 }
444 }
445
446 ssrc_handlers_.IncomingPacket(data, length);
447 return 1;
448 }
449
450 SsrcHandlers ssrc_handlers_;
451 Clock* clock_;
452 std::unique_ptr<test::RtpFileReader> packet_source_;
453 test::RtpPacket next_packet_;
454 uint32_t next_rtp_time_;
455 bool first_packet_;
456 int64_t first_packet_rtp_time_;
457 int64_t first_packet_time_ms_;
458 float loss_rate_;
459 LostPackets lost_packets_;
460 uint32_t resend_packet_count_;
461 uint32_t no_loss_startup_;
462 bool end_of_file_;
463 bool reordering_;
464 std::unique_ptr<RawRtpPacket> reorder_buffer_;
465
466 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RtpPlayerImpl);
467 };
468
469 RtpPlayerInterface* Create(const std::string& input_filename,
470 PayloadSinkFactoryInterface* payload_sink_factory,
471 Clock* clock,
472 const PayloadTypes& payload_types,
473 float loss_rate,
474 int64_t rtt_ms,
475 bool reordering) {
476 std::unique_ptr<test::RtpFileReader> packet_source(
477 test::RtpFileReader::Create(test::RtpFileReader::kRtpDump,
478 input_filename));
479 if (packet_source.get() == NULL) {
480 packet_source.reset(test::RtpFileReader::Create(test::RtpFileReader::kPcap,
481 input_filename));
482 if (packet_source.get() == NULL) {
483 return NULL;
484 }
485 }
486
487 std::unique_ptr<RtpPlayerImpl> impl(
488 new RtpPlayerImpl(payload_sink_factory, payload_types, clock,
489 &packet_source, loss_rate, rtt_ms, reordering));
490 return impl.release();
491 }
492 } // namespace rtpplayer
493 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/video_coding/test/rtp_player.h ('k') | webrtc/modules/video_coding/test/stream_generator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698