Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_coding / neteq / neteq_impl_unittest.cc
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/audio_coding/neteq/interface/neteq.h"
12 #include "webrtc/modules/audio_coding/neteq/neteq_impl.h"
13
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "webrtc/modules/audio_coding/neteq/accelerate.h"
17 #include "webrtc/modules/audio_coding/neteq/expand.h"
18 #include "webrtc/modules/audio_coding/neteq/mock/mock_audio_decoder.h"
19 #include "webrtc/modules/audio_coding/neteq/mock/mock_buffer_level_filter.h"
20 #include "webrtc/modules/audio_coding/neteq/mock/mock_decoder_database.h"
21 #include "webrtc/modules/audio_coding/neteq/mock/mock_delay_manager.h"
22 #include "webrtc/modules/audio_coding/neteq/mock/mock_delay_peak_detector.h"
23 #include "webrtc/modules/audio_coding/neteq/mock/mock_dtmf_buffer.h"
24 #include "webrtc/modules/audio_coding/neteq/mock/mock_dtmf_tone_generator.h"
25 #include "webrtc/modules/audio_coding/neteq/mock/mock_packet_buffer.h"
26 #include "webrtc/modules/audio_coding/neteq/mock/mock_payload_splitter.h"
27 #include "webrtc/modules/audio_coding/neteq/preemptive_expand.h"
28 #include "webrtc/modules/audio_coding/neteq/sync_buffer.h"
29 #include "webrtc/modules/audio_coding/neteq/timestamp_scaler.h"
30
31 using ::testing::Return;
32 using ::testing::ReturnNull;
33 using ::testing::_;
34 using ::testing::SetArgPointee;
35 using ::testing::SetArrayArgument;
36 using ::testing::InSequence;
37 using ::testing::Invoke;
38 using ::testing::WithArg;
39 using ::testing::Pointee;
40
41 namespace webrtc {
42
43 // This function is called when inserting a packet list into the mock packet
44 // buffer. The purpose is to delete all inserted packets properly, to avoid
45 // memory leaks in the test.
46 int DeletePacketsAndReturnOk(PacketList* packet_list) {
47   PacketBuffer::DeleteAllPackets(packet_list);
48   return PacketBuffer::kOK;
49 }
50
51 class NetEqImplTest : public ::testing::Test {
52  protected:
53   NetEqImplTest()
54       : neteq_(NULL),
55         config_(),
56         mock_buffer_level_filter_(NULL),
57         buffer_level_filter_(NULL),
58         use_mock_buffer_level_filter_(true),
59         mock_decoder_database_(NULL),
60         decoder_database_(NULL),
61         use_mock_decoder_database_(true),
62         mock_delay_peak_detector_(NULL),
63         delay_peak_detector_(NULL),
64         use_mock_delay_peak_detector_(true),
65         mock_delay_manager_(NULL),
66         delay_manager_(NULL),
67         use_mock_delay_manager_(true),
68         mock_dtmf_buffer_(NULL),
69         dtmf_buffer_(NULL),
70         use_mock_dtmf_buffer_(true),
71         mock_dtmf_tone_generator_(NULL),
72         dtmf_tone_generator_(NULL),
73         use_mock_dtmf_tone_generator_(true),
74         mock_packet_buffer_(NULL),
75         packet_buffer_(NULL),
76         use_mock_packet_buffer_(true),
77         mock_payload_splitter_(NULL),
78         payload_splitter_(NULL),
79         use_mock_payload_splitter_(true),
80         timestamp_scaler_(NULL) {
81     config_.sample_rate_hz = 8000;
82   }
83
84   void CreateInstance() {
85     if (use_mock_buffer_level_filter_) {
86       mock_buffer_level_filter_ = new MockBufferLevelFilter;
87       buffer_level_filter_ = mock_buffer_level_filter_;
88     } else {
89       buffer_level_filter_ = new BufferLevelFilter;
90     }
91     if (use_mock_decoder_database_) {
92       mock_decoder_database_ = new MockDecoderDatabase;
93       EXPECT_CALL(*mock_decoder_database_, GetActiveCngDecoder())
94           .WillOnce(ReturnNull());
95       decoder_database_ = mock_decoder_database_;
96     } else {
97       decoder_database_ = new DecoderDatabase;
98     }
99     if (use_mock_delay_peak_detector_) {
100       mock_delay_peak_detector_ = new MockDelayPeakDetector;
101       EXPECT_CALL(*mock_delay_peak_detector_, Reset()).Times(1);
102       delay_peak_detector_ = mock_delay_peak_detector_;
103     } else {
104       delay_peak_detector_ = new DelayPeakDetector;
105     }
106     if (use_mock_delay_manager_) {
107       mock_delay_manager_ = new MockDelayManager(config_.max_packets_in_buffer,
108                                                  delay_peak_detector_);
109       EXPECT_CALL(*mock_delay_manager_, set_streaming_mode(false)).Times(1);
110       delay_manager_ = mock_delay_manager_;
111     } else {
112       delay_manager_ =
113           new DelayManager(config_.max_packets_in_buffer, delay_peak_detector_);
114     }
115     if (use_mock_dtmf_buffer_) {
116       mock_dtmf_buffer_ = new MockDtmfBuffer(config_.sample_rate_hz);
117       dtmf_buffer_ = mock_dtmf_buffer_;
118     } else {
119       dtmf_buffer_ = new DtmfBuffer(config_.sample_rate_hz);
120     }
121     if (use_mock_dtmf_tone_generator_) {
122       mock_dtmf_tone_generator_ = new MockDtmfToneGenerator;
123       dtmf_tone_generator_ = mock_dtmf_tone_generator_;
124     } else {
125       dtmf_tone_generator_ = new DtmfToneGenerator;
126     }
127     if (use_mock_packet_buffer_) {
128       mock_packet_buffer_ = new MockPacketBuffer(config_.max_packets_in_buffer);
129       packet_buffer_ = mock_packet_buffer_;
130     } else {
131       packet_buffer_ = new PacketBuffer(config_.max_packets_in_buffer);
132     }
133     if (use_mock_payload_splitter_) {
134       mock_payload_splitter_ = new MockPayloadSplitter;
135       payload_splitter_ = mock_payload_splitter_;
136     } else {
137       payload_splitter_ = new PayloadSplitter;
138     }
139     timestamp_scaler_ = new TimestampScaler(*decoder_database_);
140     AccelerateFactory* accelerate_factory = new AccelerateFactory;
141     ExpandFactory* expand_factory = new ExpandFactory;
142     PreemptiveExpandFactory* preemptive_expand_factory =
143         new PreemptiveExpandFactory;
144
145     neteq_ = new NetEqImpl(config_,
146                            buffer_level_filter_,
147                            decoder_database_,
148                            delay_manager_,
149                            delay_peak_detector_,
150                            dtmf_buffer_,
151                            dtmf_tone_generator_,
152                            packet_buffer_,
153                            payload_splitter_,
154                            timestamp_scaler_,
155                            accelerate_factory,
156                            expand_factory,
157                            preemptive_expand_factory);
158     ASSERT_TRUE(neteq_ != NULL);
159   }
160
161   void UseNoMocks() {
162     ASSERT_TRUE(neteq_ == NULL) << "Must call UseNoMocks before CreateInstance";
163     use_mock_buffer_level_filter_ = false;
164     use_mock_decoder_database_ = false;
165     use_mock_delay_peak_detector_ = false;
166     use_mock_delay_manager_ = false;
167     use_mock_dtmf_buffer_ = false;
168     use_mock_dtmf_tone_generator_ = false;
169     use_mock_packet_buffer_ = false;
170     use_mock_payload_splitter_ = false;
171   }
172
173   virtual ~NetEqImplTest() {
174     if (use_mock_buffer_level_filter_) {
175       EXPECT_CALL(*mock_buffer_level_filter_, Die()).Times(1);
176     }
177     if (use_mock_decoder_database_) {
178       EXPECT_CALL(*mock_decoder_database_, Die()).Times(1);
179     }
180     if (use_mock_delay_manager_) {
181       EXPECT_CALL(*mock_delay_manager_, Die()).Times(1);
182     }
183     if (use_mock_delay_peak_detector_) {
184       EXPECT_CALL(*mock_delay_peak_detector_, Die()).Times(1);
185     }
186     if (use_mock_dtmf_buffer_) {
187       EXPECT_CALL(*mock_dtmf_buffer_, Die()).Times(1);
188     }
189     if (use_mock_dtmf_tone_generator_) {
190       EXPECT_CALL(*mock_dtmf_tone_generator_, Die()).Times(1);
191     }
192     if (use_mock_packet_buffer_) {
193       EXPECT_CALL(*mock_packet_buffer_, Die()).Times(1);
194     }
195     delete neteq_;
196   }
197
198   NetEqImpl* neteq_;
199   NetEq::Config config_;
200   MockBufferLevelFilter* mock_buffer_level_filter_;
201   BufferLevelFilter* buffer_level_filter_;
202   bool use_mock_buffer_level_filter_;
203   MockDecoderDatabase* mock_decoder_database_;
204   DecoderDatabase* decoder_database_;
205   bool use_mock_decoder_database_;
206   MockDelayPeakDetector* mock_delay_peak_detector_;
207   DelayPeakDetector* delay_peak_detector_;
208   bool use_mock_delay_peak_detector_;
209   MockDelayManager* mock_delay_manager_;
210   DelayManager* delay_manager_;
211   bool use_mock_delay_manager_;
212   MockDtmfBuffer* mock_dtmf_buffer_;
213   DtmfBuffer* dtmf_buffer_;
214   bool use_mock_dtmf_buffer_;
215   MockDtmfToneGenerator* mock_dtmf_tone_generator_;
216   DtmfToneGenerator* dtmf_tone_generator_;
217   bool use_mock_dtmf_tone_generator_;
218   MockPacketBuffer* mock_packet_buffer_;
219   PacketBuffer* packet_buffer_;
220   bool use_mock_packet_buffer_;
221   MockPayloadSplitter* mock_payload_splitter_;
222   PayloadSplitter* payload_splitter_;
223   bool use_mock_payload_splitter_;
224   TimestampScaler* timestamp_scaler_;
225 };
226
227
228 // This tests the interface class NetEq.
229 // TODO(hlundin): Move to separate file?
230 TEST(NetEq, CreateAndDestroy) {
231   NetEq::Config config;
232   NetEq* neteq = NetEq::Create(config);
233   delete neteq;
234 }
235
236 TEST_F(NetEqImplTest, RegisterPayloadType) {
237   CreateInstance();
238   uint8_t rtp_payload_type = 0;
239   NetEqDecoder codec_type = kDecoderPCMu;
240   EXPECT_CALL(*mock_decoder_database_,
241               RegisterPayload(rtp_payload_type, codec_type));
242   neteq_->RegisterPayloadType(codec_type, rtp_payload_type);
243 }
244
245 TEST_F(NetEqImplTest, RemovePayloadType) {
246   CreateInstance();
247   uint8_t rtp_payload_type = 0;
248   EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
249       .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
250   // Check that kFail is returned when database returns kDecoderNotFound.
251   EXPECT_EQ(NetEq::kFail, neteq_->RemovePayloadType(rtp_payload_type));
252 }
253
254 TEST_F(NetEqImplTest, InsertPacket) {
255   CreateInstance();
256   const int kPayloadLength = 100;
257   const uint8_t kPayloadType = 0;
258   const uint16_t kFirstSequenceNumber = 0x1234;
259   const uint32_t kFirstTimestamp = 0x12345678;
260   const uint32_t kSsrc = 0x87654321;
261   const uint32_t kFirstReceiveTime = 17;
262   uint8_t payload[kPayloadLength] = {0};
263   WebRtcRTPHeader rtp_header;
264   rtp_header.header.payloadType = kPayloadType;
265   rtp_header.header.sequenceNumber = kFirstSequenceNumber;
266   rtp_header.header.timestamp = kFirstTimestamp;
267   rtp_header.header.ssrc = kSsrc;
268
269   // Create a mock decoder object.
270   MockAudioDecoder mock_decoder;
271   // BWE update function called with first packet.
272   EXPECT_CALL(mock_decoder, IncomingPacket(_,
273                                            kPayloadLength,
274                                            kFirstSequenceNumber,
275                                            kFirstTimestamp,
276                                            kFirstReceiveTime));
277   // BWE update function called with second packet.
278   EXPECT_CALL(mock_decoder, IncomingPacket(_,
279                                            kPayloadLength,
280                                            kFirstSequenceNumber + 1,
281                                            kFirstTimestamp + 160,
282                                            kFirstReceiveTime + 155));
283   EXPECT_CALL(mock_decoder, Die()).Times(1);  // Called when deleted.
284
285   // Expectations for decoder database.
286   EXPECT_CALL(*mock_decoder_database_, IsRed(kPayloadType))
287       .WillRepeatedly(Return(false));  // This is not RED.
288   EXPECT_CALL(*mock_decoder_database_, CheckPayloadTypes(_))
289       .Times(2)
290       .WillRepeatedly(Return(DecoderDatabase::kOK));  // Payload type is valid.
291   EXPECT_CALL(*mock_decoder_database_, IsDtmf(kPayloadType))
292       .WillRepeatedly(Return(false));  // This is not DTMF.
293   EXPECT_CALL(*mock_decoder_database_, GetDecoder(kPayloadType))
294       .Times(3)
295       .WillRepeatedly(Return(&mock_decoder));
296   EXPECT_CALL(*mock_decoder_database_, IsComfortNoise(kPayloadType))
297       .WillRepeatedly(Return(false));  // This is not CNG.
298   DecoderDatabase::DecoderInfo info;
299   info.codec_type = kDecoderPCMu;
300   EXPECT_CALL(*mock_decoder_database_, GetDecoderInfo(kPayloadType))
301       .WillRepeatedly(Return(&info));
302
303   // Expectations for packet buffer.
304   EXPECT_CALL(*mock_packet_buffer_, NumPacketsInBuffer())
305       .WillOnce(Return(0))   // First packet.
306       .WillOnce(Return(1))   // Second packet.
307       .WillOnce(Return(2));  // Second packet, checking after it was inserted.
308   EXPECT_CALL(*mock_packet_buffer_, Empty())
309       .WillOnce(Return(false));  // Called once after first packet is inserted.
310   EXPECT_CALL(*mock_packet_buffer_, Flush())
311       .Times(1);
312   EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _))
313       .Times(2)
314       .WillRepeatedly(DoAll(SetArgPointee<2>(kPayloadType),
315                             WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
316   // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
317   // index) is a pointer, and the variable pointed to is set to kPayloadType.
318   // Also invoke the function DeletePacketsAndReturnOk to properly delete all
319   // packets in the list (to avoid memory leaks in the test).
320   EXPECT_CALL(*mock_packet_buffer_, NextRtpHeader())
321       .Times(1)
322       .WillOnce(Return(&rtp_header.header));
323
324   // Expectations for DTMF buffer.
325   EXPECT_CALL(*mock_dtmf_buffer_, Flush())
326       .Times(1);
327
328   // Expectations for delay manager.
329   {
330     // All expectations within this block must be called in this specific order.
331     InSequence sequence;  // Dummy variable.
332     // Expectations when the first packet is inserted.
333     EXPECT_CALL(*mock_delay_manager_, LastDecoderType(kDecoderPCMu))
334         .Times(1);
335     EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
336         .Times(2)
337         .WillRepeatedly(Return(-1));
338     EXPECT_CALL(*mock_delay_manager_, set_last_pack_cng_or_dtmf(0))
339         .Times(1);
340     EXPECT_CALL(*mock_delay_manager_, ResetPacketIatCount()).Times(1);
341     // Expectations when the second packet is inserted. Slightly different.
342     EXPECT_CALL(*mock_delay_manager_, LastDecoderType(kDecoderPCMu))
343         .Times(1);
344     EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
345         .WillOnce(Return(0));
346     EXPECT_CALL(*mock_delay_manager_, SetPacketAudioLength(30))
347         .WillOnce(Return(0));
348   }
349
350   // Expectations for payload splitter.
351   EXPECT_CALL(*mock_payload_splitter_, SplitAudio(_, _))
352       .Times(2)
353       .WillRepeatedly(Return(PayloadSplitter::kOK));
354
355   // Insert first packet.
356   neteq_->InsertPacket(rtp_header, payload, kPayloadLength, kFirstReceiveTime);
357
358   // Insert second packet.
359   rtp_header.header.timestamp += 160;
360   rtp_header.header.sequenceNumber += 1;
361   neteq_->InsertPacket(rtp_header, payload, kPayloadLength,
362                        kFirstReceiveTime + 155);
363 }
364
365 TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
366   UseNoMocks();
367   CreateInstance();
368
369   const int kPayloadLengthSamples = 80;
370   const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;  // PCM 16-bit.
371   const uint8_t kPayloadType = 17;  // Just an arbitrary number.
372   const uint32_t kReceiveTime = 17;  // Value doesn't matter for this test.
373   uint8_t payload[kPayloadLengthBytes] = {0};
374   WebRtcRTPHeader rtp_header;
375   rtp_header.header.payloadType = kPayloadType;
376   rtp_header.header.sequenceNumber = 0x1234;
377   rtp_header.header.timestamp = 0x12345678;
378   rtp_header.header.ssrc = 0x87654321;
379
380   EXPECT_EQ(NetEq::kOK,
381             neteq_->RegisterPayloadType(kDecoderPCM16B, kPayloadType));
382
383   // Insert packets. The buffer should not flush.
384   for (int i = 1; i <= config_.max_packets_in_buffer; ++i) {
385     EXPECT_EQ(NetEq::kOK,
386               neteq_->InsertPacket(
387                   rtp_header, payload, kPayloadLengthBytes, kReceiveTime));
388     rtp_header.header.timestamp += kPayloadLengthSamples;
389     rtp_header.header.sequenceNumber += 1;
390     EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
391   }
392
393   // Insert one more packet and make sure the buffer got flushed. That is, it
394   // should only hold one single packet.
395   EXPECT_EQ(NetEq::kOK,
396             neteq_->InsertPacket(
397                 rtp_header, payload, kPayloadLengthBytes, kReceiveTime));
398   EXPECT_EQ(1, packet_buffer_->NumPacketsInBuffer());
399   const RTPHeader* test_header = packet_buffer_->NextRtpHeader();
400   EXPECT_EQ(rtp_header.header.timestamp, test_header->timestamp);
401   EXPECT_EQ(rtp_header.header.sequenceNumber, test_header->sequenceNumber);
402 }
403
404 // This test verifies that timestamps propagate from the incoming packets
405 // through to the sync buffer and to the playout timestamp.
406 TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
407   UseNoMocks();
408   CreateInstance();
409
410   const uint8_t kPayloadType = 17;   // Just an arbitrary number.
411   const uint32_t kReceiveTime = 17;  // Value doesn't matter for this test.
412   const int kSampleRateHz = 8000;
413   const int kPayloadLengthSamples = 10 * kSampleRateHz / 1000;  // 10 ms.
414   const size_t kPayloadLengthBytes = kPayloadLengthSamples;
415   uint8_t payload[kPayloadLengthBytes] = {0};
416   WebRtcRTPHeader rtp_header;
417   rtp_header.header.payloadType = kPayloadType;
418   rtp_header.header.sequenceNumber = 0x1234;
419   rtp_header.header.timestamp = 0x12345678;
420   rtp_header.header.ssrc = 0x87654321;
421
422   // This is a dummy decoder that produces as many output samples as the input
423   // has bytes. The output is an increasing series, starting at 1 for the first
424   // sample, and then increasing by 1 for each sample.
425   class CountingSamplesDecoder : public AudioDecoder {
426    public:
427     CountingSamplesDecoder() : next_value_(1) {}
428
429     // Produce as many samples as input bytes (|encoded_len|).
430     virtual int Decode(const uint8_t* encoded,
431                        size_t encoded_len,
432                        int16_t* decoded,
433                        SpeechType* speech_type) {
434       for (size_t i = 0; i < encoded_len; ++i) {
435         decoded[i] = next_value_++;
436       }
437       *speech_type = kSpeech;
438       return encoded_len;
439     }
440
441     virtual int Init() {
442       next_value_ = 1;
443       return 0;
444     }
445
446     uint16_t next_value() const { return next_value_; }
447
448    private:
449     int16_t next_value_;
450   } decoder_;
451
452   EXPECT_EQ(NetEq::kOK,
453             neteq_->RegisterExternalDecoder(
454                 &decoder_, kDecoderPCM16B, kPayloadType));
455
456   // Insert one packet.
457   EXPECT_EQ(NetEq::kOK,
458             neteq_->InsertPacket(
459                 rtp_header, payload, kPayloadLengthBytes, kReceiveTime));
460
461   // Pull audio once.
462   const int kMaxOutputSize = 10 * kSampleRateHz / 1000;
463   int16_t output[kMaxOutputSize];
464   int samples_per_channel;
465   int num_channels;
466   NetEqOutputType type;
467   EXPECT_EQ(
468       NetEq::kOK,
469       neteq_->GetAudio(
470           kMaxOutputSize, output, &samples_per_channel, &num_channels, &type));
471   ASSERT_EQ(kMaxOutputSize, samples_per_channel);
472   EXPECT_EQ(1, num_channels);
473   EXPECT_EQ(kOutputNormal, type);
474
475   // Start with a simple check that the fake decoder is behaving as expected.
476   EXPECT_EQ(kPayloadLengthSamples, decoder_.next_value() - 1);
477
478   // The value of the last of the output samples is the same as the number of
479   // samples played from the decoded packet. Thus, this number + the RTP
480   // timestamp should match the playout timestamp.
481   uint32_t timestamp = 0;
482   EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&timestamp));
483   EXPECT_EQ(rtp_header.header.timestamp + output[samples_per_channel - 1],
484             timestamp);
485
486   // Check the timestamp for the last value in the sync buffer. This should
487   // be one full frame length ahead of the RTP timestamp.
488   const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
489   ASSERT_TRUE(sync_buffer != NULL);
490   EXPECT_EQ(rtp_header.header.timestamp + kPayloadLengthSamples,
491             sync_buffer->end_timestamp());
492
493   // Check that the number of samples still to play from the sync buffer add
494   // up with what was already played out.
495   EXPECT_EQ(kPayloadLengthSamples - output[samples_per_channel - 1],
496             static_cast<int>(sync_buffer->FutureLength()));
497 }
498
499 TEST_F(NetEqImplTest, ReorderedPacket) {
500   UseNoMocks();
501   CreateInstance();
502
503   const uint8_t kPayloadType = 17;   // Just an arbitrary number.
504   const uint32_t kReceiveTime = 17;  // Value doesn't matter for this test.
505   const int kSampleRateHz = 8000;
506   const int kPayloadLengthSamples = 10 * kSampleRateHz / 1000;  // 10 ms.
507   const size_t kPayloadLengthBytes = kPayloadLengthSamples;
508   uint8_t payload[kPayloadLengthBytes] = {0};
509   WebRtcRTPHeader rtp_header;
510   rtp_header.header.payloadType = kPayloadType;
511   rtp_header.header.sequenceNumber = 0x1234;
512   rtp_header.header.timestamp = 0x12345678;
513   rtp_header.header.ssrc = 0x87654321;
514
515   // Create a mock decoder object.
516   MockAudioDecoder mock_decoder;
517   EXPECT_CALL(mock_decoder, Init()).WillRepeatedly(Return(0));
518   EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
519       .WillRepeatedly(Return(0));
520   int16_t dummy_output[kPayloadLengthSamples] = {0};
521   // The below expectation will make the mock decoder write
522   // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
523   EXPECT_CALL(mock_decoder, Decode(Pointee(0), kPayloadLengthBytes, _, _))
524       .WillOnce(DoAll(SetArrayArgument<2>(dummy_output,
525                                           dummy_output + kPayloadLengthSamples),
526                       SetArgPointee<3>(AudioDecoder::kSpeech),
527                       Return(kPayloadLengthSamples)));
528   EXPECT_EQ(NetEq::kOK,
529             neteq_->RegisterExternalDecoder(
530                 &mock_decoder, kDecoderPCM16B, kPayloadType));
531
532   // Insert one packet.
533   EXPECT_EQ(NetEq::kOK,
534             neteq_->InsertPacket(
535                 rtp_header, payload, kPayloadLengthBytes, kReceiveTime));
536
537   // Pull audio once.
538   const int kMaxOutputSize = 10 * kSampleRateHz / 1000;
539   int16_t output[kMaxOutputSize];
540   int samples_per_channel;
541   int num_channels;
542   NetEqOutputType type;
543   EXPECT_EQ(
544       NetEq::kOK,
545       neteq_->GetAudio(
546           kMaxOutputSize, output, &samples_per_channel, &num_channels, &type));
547   ASSERT_EQ(kMaxOutputSize, samples_per_channel);
548   EXPECT_EQ(1, num_channels);
549   EXPECT_EQ(kOutputNormal, type);
550
551   // Insert two more packets. The first one is out of order, and is already too
552   // old, the second one is the expected next packet.
553   rtp_header.header.sequenceNumber -= 1;
554   rtp_header.header.timestamp -= kPayloadLengthSamples;
555   payload[0] = 1;
556   EXPECT_EQ(NetEq::kOK,
557             neteq_->InsertPacket(
558                 rtp_header, payload, kPayloadLengthBytes, kReceiveTime));
559   rtp_header.header.sequenceNumber += 2;
560   rtp_header.header.timestamp += 2 * kPayloadLengthSamples;
561   payload[0] = 2;
562   EXPECT_EQ(NetEq::kOK,
563             neteq_->InsertPacket(
564                 rtp_header, payload, kPayloadLengthBytes, kReceiveTime));
565
566   // Expect only the second packet to be decoded (the one with "2" as the first
567   // payload byte).
568   EXPECT_CALL(mock_decoder, Decode(Pointee(2), kPayloadLengthBytes, _, _))
569       .WillOnce(DoAll(SetArrayArgument<2>(dummy_output,
570                                           dummy_output + kPayloadLengthSamples),
571                       SetArgPointee<3>(AudioDecoder::kSpeech),
572                       Return(kPayloadLengthSamples)));
573
574   // Pull audio once.
575   EXPECT_EQ(
576       NetEq::kOK,
577       neteq_->GetAudio(
578           kMaxOutputSize, output, &samples_per_channel, &num_channels, &type));
579   ASSERT_EQ(kMaxOutputSize, samples_per_channel);
580   EXPECT_EQ(1, num_channels);
581   EXPECT_EQ(kOutputNormal, type);
582
583   // Now check the packet buffer, and make sure it is empty, since the
584   // out-of-order packet should have been discarded.
585   EXPECT_TRUE(packet_buffer_->Empty());
586
587   EXPECT_CALL(mock_decoder, Die());
588 }
589
590 }  // namespace webrtc