Home | History | Annotate | Download | only in neteq
      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/include/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/base/safe_conversions.h"
     17 #include "webrtc/modules/audio_coding/neteq/accelerate.h"
     18 #include "webrtc/modules/audio_coding/neteq/expand.h"
     19 #include "webrtc/modules/audio_coding/neteq/mock/mock_audio_decoder.h"
     20 #include "webrtc/modules/audio_coding/neteq/mock/mock_buffer_level_filter.h"
     21 #include "webrtc/modules/audio_coding/neteq/mock/mock_decoder_database.h"
     22 #include "webrtc/modules/audio_coding/neteq/mock/mock_delay_manager.h"
     23 #include "webrtc/modules/audio_coding/neteq/mock/mock_delay_peak_detector.h"
     24 #include "webrtc/modules/audio_coding/neteq/mock/mock_dtmf_buffer.h"
     25 #include "webrtc/modules/audio_coding/neteq/mock/mock_dtmf_tone_generator.h"
     26 #include "webrtc/modules/audio_coding/neteq/mock/mock_packet_buffer.h"
     27 #include "webrtc/modules/audio_coding/neteq/mock/mock_payload_splitter.h"
     28 #include "webrtc/modules/audio_coding/neteq/preemptive_expand.h"
     29 #include "webrtc/modules/audio_coding/neteq/sync_buffer.h"
     30 #include "webrtc/modules/audio_coding/neteq/timestamp_scaler.h"
     31 
     32 using ::testing::AtLeast;
     33 using ::testing::Return;
     34 using ::testing::ReturnNull;
     35 using ::testing::_;
     36 using ::testing::SetArgPointee;
     37 using ::testing::SetArrayArgument;
     38 using ::testing::InSequence;
     39 using ::testing::Invoke;
     40 using ::testing::WithArg;
     41 using ::testing::Pointee;
     42 using ::testing::IsNull;
     43 
     44 namespace webrtc {
     45 
     46 // This function is called when inserting a packet list into the mock packet
     47 // buffer. The purpose is to delete all inserted packets properly, to avoid
     48 // memory leaks in the test.
     49 int DeletePacketsAndReturnOk(PacketList* packet_list) {
     50   PacketBuffer::DeleteAllPackets(packet_list);
     51   return PacketBuffer::kOK;
     52 }
     53 
     54 class NetEqImplTest : public ::testing::Test {
     55  protected:
     56   NetEqImplTest()
     57       : neteq_(NULL),
     58         config_(),
     59         mock_buffer_level_filter_(NULL),
     60         buffer_level_filter_(NULL),
     61         use_mock_buffer_level_filter_(true),
     62         mock_decoder_database_(NULL),
     63         decoder_database_(NULL),
     64         use_mock_decoder_database_(true),
     65         mock_delay_peak_detector_(NULL),
     66         delay_peak_detector_(NULL),
     67         use_mock_delay_peak_detector_(true),
     68         mock_delay_manager_(NULL),
     69         delay_manager_(NULL),
     70         use_mock_delay_manager_(true),
     71         mock_dtmf_buffer_(NULL),
     72         dtmf_buffer_(NULL),
     73         use_mock_dtmf_buffer_(true),
     74         mock_dtmf_tone_generator_(NULL),
     75         dtmf_tone_generator_(NULL),
     76         use_mock_dtmf_tone_generator_(true),
     77         mock_packet_buffer_(NULL),
     78         packet_buffer_(NULL),
     79         use_mock_packet_buffer_(true),
     80         mock_payload_splitter_(NULL),
     81         payload_splitter_(NULL),
     82         use_mock_payload_splitter_(true),
     83         timestamp_scaler_(NULL) {
     84     config_.sample_rate_hz = 8000;
     85   }
     86 
     87   void CreateInstance() {
     88     if (use_mock_buffer_level_filter_) {
     89       mock_buffer_level_filter_ = new MockBufferLevelFilter;
     90       buffer_level_filter_ = mock_buffer_level_filter_;
     91     } else {
     92       buffer_level_filter_ = new BufferLevelFilter;
     93     }
     94     if (use_mock_decoder_database_) {
     95       mock_decoder_database_ = new MockDecoderDatabase;
     96       EXPECT_CALL(*mock_decoder_database_, GetActiveCngDecoder())
     97           .WillOnce(ReturnNull());
     98       decoder_database_ = mock_decoder_database_;
     99     } else {
    100       decoder_database_ = new DecoderDatabase;
    101     }
    102     if (use_mock_delay_peak_detector_) {
    103       mock_delay_peak_detector_ = new MockDelayPeakDetector;
    104       EXPECT_CALL(*mock_delay_peak_detector_, Reset()).Times(1);
    105       delay_peak_detector_ = mock_delay_peak_detector_;
    106     } else {
    107       delay_peak_detector_ = new DelayPeakDetector;
    108     }
    109     if (use_mock_delay_manager_) {
    110       mock_delay_manager_ = new MockDelayManager(config_.max_packets_in_buffer,
    111                                                  delay_peak_detector_);
    112       EXPECT_CALL(*mock_delay_manager_, set_streaming_mode(false)).Times(1);
    113       delay_manager_ = mock_delay_manager_;
    114     } else {
    115       delay_manager_ =
    116           new DelayManager(config_.max_packets_in_buffer, delay_peak_detector_);
    117     }
    118     if (use_mock_dtmf_buffer_) {
    119       mock_dtmf_buffer_ = new MockDtmfBuffer(config_.sample_rate_hz);
    120       dtmf_buffer_ = mock_dtmf_buffer_;
    121     } else {
    122       dtmf_buffer_ = new DtmfBuffer(config_.sample_rate_hz);
    123     }
    124     if (use_mock_dtmf_tone_generator_) {
    125       mock_dtmf_tone_generator_ = new MockDtmfToneGenerator;
    126       dtmf_tone_generator_ = mock_dtmf_tone_generator_;
    127     } else {
    128       dtmf_tone_generator_ = new DtmfToneGenerator;
    129     }
    130     if (use_mock_packet_buffer_) {
    131       mock_packet_buffer_ = new MockPacketBuffer(config_.max_packets_in_buffer);
    132       packet_buffer_ = mock_packet_buffer_;
    133     } else {
    134       packet_buffer_ = new PacketBuffer(config_.max_packets_in_buffer);
    135     }
    136     if (use_mock_payload_splitter_) {
    137       mock_payload_splitter_ = new MockPayloadSplitter;
    138       payload_splitter_ = mock_payload_splitter_;
    139     } else {
    140       payload_splitter_ = new PayloadSplitter;
    141     }
    142     timestamp_scaler_ = new TimestampScaler(*decoder_database_);
    143     AccelerateFactory* accelerate_factory = new AccelerateFactory;
    144     ExpandFactory* expand_factory = new ExpandFactory;
    145     PreemptiveExpandFactory* preemptive_expand_factory =
    146         new PreemptiveExpandFactory;
    147 
    148     neteq_ = new NetEqImpl(config_,
    149                            buffer_level_filter_,
    150                            decoder_database_,
    151                            delay_manager_,
    152                            delay_peak_detector_,
    153                            dtmf_buffer_,
    154                            dtmf_tone_generator_,
    155                            packet_buffer_,
    156                            payload_splitter_,
    157                            timestamp_scaler_,
    158                            accelerate_factory,
    159                            expand_factory,
    160                            preemptive_expand_factory);
    161     ASSERT_TRUE(neteq_ != NULL);
    162   }
    163 
    164   void UseNoMocks() {
    165     ASSERT_TRUE(neteq_ == NULL) << "Must call UseNoMocks before CreateInstance";
    166     use_mock_buffer_level_filter_ = false;
    167     use_mock_decoder_database_ = false;
    168     use_mock_delay_peak_detector_ = false;
    169     use_mock_delay_manager_ = false;
    170     use_mock_dtmf_buffer_ = false;
    171     use_mock_dtmf_tone_generator_ = false;
    172     use_mock_packet_buffer_ = false;
    173     use_mock_payload_splitter_ = false;
    174   }
    175 
    176   virtual ~NetEqImplTest() {
    177     if (use_mock_buffer_level_filter_) {
    178       EXPECT_CALL(*mock_buffer_level_filter_, Die()).Times(1);
    179     }
    180     if (use_mock_decoder_database_) {
    181       EXPECT_CALL(*mock_decoder_database_, Die()).Times(1);
    182     }
    183     if (use_mock_delay_manager_) {
    184       EXPECT_CALL(*mock_delay_manager_, Die()).Times(1);
    185     }
    186     if (use_mock_delay_peak_detector_) {
    187       EXPECT_CALL(*mock_delay_peak_detector_, Die()).Times(1);
    188     }
    189     if (use_mock_dtmf_buffer_) {
    190       EXPECT_CALL(*mock_dtmf_buffer_, Die()).Times(1);
    191     }
    192     if (use_mock_dtmf_tone_generator_) {
    193       EXPECT_CALL(*mock_dtmf_tone_generator_, Die()).Times(1);
    194     }
    195     if (use_mock_packet_buffer_) {
    196       EXPECT_CALL(*mock_packet_buffer_, Die()).Times(1);
    197     }
    198     delete neteq_;
    199   }
    200 
    201   NetEqImpl* neteq_;
    202   NetEq::Config config_;
    203   MockBufferLevelFilter* mock_buffer_level_filter_;
    204   BufferLevelFilter* buffer_level_filter_;
    205   bool use_mock_buffer_level_filter_;
    206   MockDecoderDatabase* mock_decoder_database_;
    207   DecoderDatabase* decoder_database_;
    208   bool use_mock_decoder_database_;
    209   MockDelayPeakDetector* mock_delay_peak_detector_;
    210   DelayPeakDetector* delay_peak_detector_;
    211   bool use_mock_delay_peak_detector_;
    212   MockDelayManager* mock_delay_manager_;
    213   DelayManager* delay_manager_;
    214   bool use_mock_delay_manager_;
    215   MockDtmfBuffer* mock_dtmf_buffer_;
    216   DtmfBuffer* dtmf_buffer_;
    217   bool use_mock_dtmf_buffer_;
    218   MockDtmfToneGenerator* mock_dtmf_tone_generator_;
    219   DtmfToneGenerator* dtmf_tone_generator_;
    220   bool use_mock_dtmf_tone_generator_;
    221   MockPacketBuffer* mock_packet_buffer_;
    222   PacketBuffer* packet_buffer_;
    223   bool use_mock_packet_buffer_;
    224   MockPayloadSplitter* mock_payload_splitter_;
    225   PayloadSplitter* payload_splitter_;
    226   bool use_mock_payload_splitter_;
    227   TimestampScaler* timestamp_scaler_;
    228 };
    229 
    230 
    231 // This tests the interface class NetEq.
    232 // TODO(hlundin): Move to separate file?
    233 TEST(NetEq, CreateAndDestroy) {
    234   NetEq::Config config;
    235   NetEq* neteq = NetEq::Create(config);
    236   delete neteq;
    237 }
    238 
    239 TEST_F(NetEqImplTest, RegisterPayloadType) {
    240   CreateInstance();
    241   uint8_t rtp_payload_type = 0;
    242   NetEqDecoder codec_type = NetEqDecoder::kDecoderPCMu;
    243   const std::string kCodecName = "Robert\'); DROP TABLE Students;";
    244   EXPECT_CALL(*mock_decoder_database_,
    245               RegisterPayload(rtp_payload_type, codec_type, kCodecName));
    246   neteq_->RegisterPayloadType(codec_type, kCodecName, rtp_payload_type);
    247 }
    248 
    249 TEST_F(NetEqImplTest, RemovePayloadType) {
    250   CreateInstance();
    251   uint8_t rtp_payload_type = 0;
    252   EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
    253       .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
    254   // Check that kFail is returned when database returns kDecoderNotFound.
    255   EXPECT_EQ(NetEq::kFail, neteq_->RemovePayloadType(rtp_payload_type));
    256 }
    257 
    258 TEST_F(NetEqImplTest, InsertPacket) {
    259   CreateInstance();
    260   const size_t kPayloadLength = 100;
    261   const uint8_t kPayloadType = 0;
    262   const uint16_t kFirstSequenceNumber = 0x1234;
    263   const uint32_t kFirstTimestamp = 0x12345678;
    264   const uint32_t kSsrc = 0x87654321;
    265   const uint32_t kFirstReceiveTime = 17;
    266   uint8_t payload[kPayloadLength] = {0};
    267   WebRtcRTPHeader rtp_header;
    268   rtp_header.header.payloadType = kPayloadType;
    269   rtp_header.header.sequenceNumber = kFirstSequenceNumber;
    270   rtp_header.header.timestamp = kFirstTimestamp;
    271   rtp_header.header.ssrc = kSsrc;
    272 
    273   // Create a mock decoder object.
    274   MockAudioDecoder mock_decoder;
    275   EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
    276   // BWE update function called with first packet.
    277   EXPECT_CALL(mock_decoder, IncomingPacket(_,
    278                                            kPayloadLength,
    279                                            kFirstSequenceNumber,
    280                                            kFirstTimestamp,
    281                                            kFirstReceiveTime));
    282   // BWE update function called with second packet.
    283   EXPECT_CALL(mock_decoder, IncomingPacket(_,
    284                                            kPayloadLength,
    285                                            kFirstSequenceNumber + 1,
    286                                            kFirstTimestamp + 160,
    287                                            kFirstReceiveTime + 155));
    288   EXPECT_CALL(mock_decoder, Die()).Times(1);  // Called when deleted.
    289 
    290   // Expectations for decoder database.
    291   EXPECT_CALL(*mock_decoder_database_, IsRed(kPayloadType))
    292       .WillRepeatedly(Return(false));  // This is not RED.
    293   EXPECT_CALL(*mock_decoder_database_, CheckPayloadTypes(_))
    294       .Times(2)
    295       .WillRepeatedly(Return(DecoderDatabase::kOK));  // Payload type is valid.
    296   EXPECT_CALL(*mock_decoder_database_, IsDtmf(kPayloadType))
    297       .WillRepeatedly(Return(false));  // This is not DTMF.
    298   EXPECT_CALL(*mock_decoder_database_, GetDecoder(kPayloadType))
    299       .Times(3)
    300       .WillRepeatedly(Return(&mock_decoder));
    301   EXPECT_CALL(*mock_decoder_database_, IsComfortNoise(kPayloadType))
    302       .WillRepeatedly(Return(false));  // This is not CNG.
    303   DecoderDatabase::DecoderInfo info;
    304   info.codec_type = NetEqDecoder::kDecoderPCMu;
    305   EXPECT_CALL(*mock_decoder_database_, GetDecoderInfo(kPayloadType))
    306       .WillRepeatedly(Return(&info));
    307 
    308   // Expectations for packet buffer.
    309   EXPECT_CALL(*mock_packet_buffer_, NumPacketsInBuffer())
    310       .WillOnce(Return(0))   // First packet.
    311       .WillOnce(Return(1))   // Second packet.
    312       .WillOnce(Return(2));  // Second packet, checking after it was inserted.
    313   EXPECT_CALL(*mock_packet_buffer_, Empty())
    314       .WillOnce(Return(false));  // Called once after first packet is inserted.
    315   EXPECT_CALL(*mock_packet_buffer_, Flush())
    316       .Times(1);
    317   EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _))
    318       .Times(2)
    319       .WillRepeatedly(DoAll(SetArgPointee<2>(kPayloadType),
    320                             WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
    321   // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
    322   // index) is a pointer, and the variable pointed to is set to kPayloadType.
    323   // Also invoke the function DeletePacketsAndReturnOk to properly delete all
    324   // packets in the list (to avoid memory leaks in the test).
    325   EXPECT_CALL(*mock_packet_buffer_, NextRtpHeader())
    326       .Times(1)
    327       .WillOnce(Return(&rtp_header.header));
    328 
    329   // Expectations for DTMF buffer.
    330   EXPECT_CALL(*mock_dtmf_buffer_, Flush())
    331       .Times(1);
    332 
    333   // Expectations for delay manager.
    334   {
    335     // All expectations within this block must be called in this specific order.
    336     InSequence sequence;  // Dummy variable.
    337     // Expectations when the first packet is inserted.
    338     EXPECT_CALL(*mock_delay_manager_,
    339                 LastDecoderType(NetEqDecoder::kDecoderPCMu))
    340         .Times(1);
    341     EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
    342         .Times(2)
    343         .WillRepeatedly(Return(-1));
    344     EXPECT_CALL(*mock_delay_manager_, set_last_pack_cng_or_dtmf(0))
    345         .Times(1);
    346     EXPECT_CALL(*mock_delay_manager_, ResetPacketIatCount()).Times(1);
    347     // Expectations when the second packet is inserted. Slightly different.
    348     EXPECT_CALL(*mock_delay_manager_,
    349                 LastDecoderType(NetEqDecoder::kDecoderPCMu))
    350         .Times(1);
    351     EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
    352         .WillOnce(Return(0));
    353     EXPECT_CALL(*mock_delay_manager_, SetPacketAudioLength(30))
    354         .WillOnce(Return(0));
    355   }
    356 
    357   // Expectations for payload splitter.
    358   EXPECT_CALL(*mock_payload_splitter_, SplitAudio(_, _))
    359       .Times(2)
    360       .WillRepeatedly(Return(PayloadSplitter::kOK));
    361 
    362   // Insert first packet.
    363   neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime);
    364 
    365   // Insert second packet.
    366   rtp_header.header.timestamp += 160;
    367   rtp_header.header.sequenceNumber += 1;
    368   neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime + 155);
    369 }
    370 
    371 TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
    372   UseNoMocks();
    373   CreateInstance();
    374 
    375   const int kPayloadLengthSamples = 80;
    376   const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;  // PCM 16-bit.
    377   const uint8_t kPayloadType = 17;  // Just an arbitrary number.
    378   const uint32_t kReceiveTime = 17;  // Value doesn't matter for this test.
    379   uint8_t payload[kPayloadLengthBytes] = {0};
    380   WebRtcRTPHeader rtp_header;
    381   rtp_header.header.payloadType = kPayloadType;
    382   rtp_header.header.sequenceNumber = 0x1234;
    383   rtp_header.header.timestamp = 0x12345678;
    384   rtp_header.header.ssrc = 0x87654321;
    385 
    386   EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
    387                             NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
    388 
    389   // Insert packets. The buffer should not flush.
    390   for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
    391     EXPECT_EQ(NetEq::kOK,
    392               neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
    393     rtp_header.header.timestamp += kPayloadLengthSamples;
    394     rtp_header.header.sequenceNumber += 1;
    395     EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
    396   }
    397 
    398   // Insert one more packet and make sure the buffer got flushed. That is, it
    399   // should only hold one single packet.
    400   EXPECT_EQ(NetEq::kOK,
    401             neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
    402   EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
    403   const RTPHeader* test_header = packet_buffer_->NextRtpHeader();
    404   EXPECT_EQ(rtp_header.header.timestamp, test_header->timestamp);
    405   EXPECT_EQ(rtp_header.header.sequenceNumber, test_header->sequenceNumber);
    406 }
    407 
    408 // This test verifies that timestamps propagate from the incoming packets
    409 // through to the sync buffer and to the playout timestamp.
    410 TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
    411   UseNoMocks();
    412   CreateInstance();
    413 
    414   const uint8_t kPayloadType = 17;   // Just an arbitrary number.
    415   const uint32_t kReceiveTime = 17;  // Value doesn't matter for this test.
    416   const int kSampleRateHz = 8000;
    417   const size_t kPayloadLengthSamples =
    418       static_cast<size_t>(10 * kSampleRateHz / 1000);  // 10 ms.
    419   const size_t kPayloadLengthBytes = kPayloadLengthSamples;
    420   uint8_t payload[kPayloadLengthBytes] = {0};
    421   WebRtcRTPHeader rtp_header;
    422   rtp_header.header.payloadType = kPayloadType;
    423   rtp_header.header.sequenceNumber = 0x1234;
    424   rtp_header.header.timestamp = 0x12345678;
    425   rtp_header.header.ssrc = 0x87654321;
    426 
    427   // This is a dummy decoder that produces as many output samples as the input
    428   // has bytes. The output is an increasing series, starting at 1 for the first
    429   // sample, and then increasing by 1 for each sample.
    430   class CountingSamplesDecoder : public AudioDecoder {
    431    public:
    432     CountingSamplesDecoder() : next_value_(1) {}
    433 
    434     // Produce as many samples as input bytes (|encoded_len|).
    435     int DecodeInternal(const uint8_t* encoded,
    436                        size_t encoded_len,
    437                        int /* sample_rate_hz */,
    438                        int16_t* decoded,
    439                        SpeechType* speech_type) override {
    440       for (size_t i = 0; i < encoded_len; ++i) {
    441         decoded[i] = next_value_++;
    442       }
    443       *speech_type = kSpeech;
    444       return encoded_len;
    445     }
    446 
    447     void Reset() override { next_value_ = 1; }
    448 
    449     size_t Channels() const override { return 1; }
    450 
    451     uint16_t next_value() const { return next_value_; }
    452 
    453    private:
    454     int16_t next_value_;
    455   } decoder_;
    456 
    457   EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
    458                             &decoder_, NetEqDecoder::kDecoderPCM16B,
    459                             "dummy name", kPayloadType, kSampleRateHz));
    460 
    461   // Insert one packet.
    462   EXPECT_EQ(NetEq::kOK,
    463             neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
    464 
    465   // Pull audio once.
    466   const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
    467   int16_t output[kMaxOutputSize];
    468   size_t samples_per_channel;
    469   size_t num_channels;
    470   NetEqOutputType type;
    471   EXPECT_EQ(
    472       NetEq::kOK,
    473       neteq_->GetAudio(
    474           kMaxOutputSize, output, &samples_per_channel, &num_channels, &type));
    475   ASSERT_EQ(kMaxOutputSize, samples_per_channel);
    476   EXPECT_EQ(1u, num_channels);
    477   EXPECT_EQ(kOutputNormal, type);
    478 
    479   // Start with a simple check that the fake decoder is behaving as expected.
    480   EXPECT_EQ(kPayloadLengthSamples,
    481             static_cast<size_t>(decoder_.next_value() - 1));
    482 
    483   // The value of the last of the output samples is the same as the number of
    484   // samples played from the decoded packet. Thus, this number + the RTP
    485   // timestamp should match the playout timestamp.
    486   uint32_t timestamp = 0;
    487   EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&timestamp));
    488   EXPECT_EQ(rtp_header.header.timestamp + output[samples_per_channel - 1],
    489             timestamp);
    490 
    491   // Check the timestamp for the last value in the sync buffer. This should
    492   // be one full frame length ahead of the RTP timestamp.
    493   const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
    494   ASSERT_TRUE(sync_buffer != NULL);
    495   EXPECT_EQ(rtp_header.header.timestamp + kPayloadLengthSamples,
    496             sync_buffer->end_timestamp());
    497 
    498   // Check that the number of samples still to play from the sync buffer add
    499   // up with what was already played out.
    500   EXPECT_EQ(kPayloadLengthSamples - output[samples_per_channel - 1],
    501             sync_buffer->FutureLength());
    502 }
    503 
    504 TEST_F(NetEqImplTest, ReorderedPacket) {
    505   UseNoMocks();
    506   CreateInstance();
    507 
    508   const uint8_t kPayloadType = 17;   // Just an arbitrary number.
    509   const uint32_t kReceiveTime = 17;  // Value doesn't matter for this test.
    510   const int kSampleRateHz = 8000;
    511   const size_t kPayloadLengthSamples =
    512       static_cast<size_t>(10 * kSampleRateHz / 1000);  // 10 ms.
    513   const size_t kPayloadLengthBytes = kPayloadLengthSamples;
    514   uint8_t payload[kPayloadLengthBytes] = {0};
    515   WebRtcRTPHeader rtp_header;
    516   rtp_header.header.payloadType = kPayloadType;
    517   rtp_header.header.sequenceNumber = 0x1234;
    518   rtp_header.header.timestamp = 0x12345678;
    519   rtp_header.header.ssrc = 0x87654321;
    520 
    521   // Create a mock decoder object.
    522   MockAudioDecoder mock_decoder;
    523   EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
    524   EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
    525   EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
    526       .WillRepeatedly(Return(0));
    527   int16_t dummy_output[kPayloadLengthSamples] = {0};
    528   // The below expectation will make the mock decoder write
    529   // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
    530   EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
    531                                            kSampleRateHz, _, _))
    532       .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
    533                                           dummy_output + kPayloadLengthSamples),
    534                       SetArgPointee<4>(AudioDecoder::kSpeech),
    535                       Return(kPayloadLengthSamples)));
    536   EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
    537                             &mock_decoder, NetEqDecoder::kDecoderPCM16B,
    538                             "dummy name", kPayloadType, kSampleRateHz));
    539 
    540   // Insert one packet.
    541   EXPECT_EQ(NetEq::kOK,
    542             neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
    543 
    544   // Pull audio once.
    545   const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
    546   int16_t output[kMaxOutputSize];
    547   size_t samples_per_channel;
    548   size_t num_channels;
    549   NetEqOutputType type;
    550   EXPECT_EQ(
    551       NetEq::kOK,
    552       neteq_->GetAudio(
    553           kMaxOutputSize, output, &samples_per_channel, &num_channels, &type));
    554   ASSERT_EQ(kMaxOutputSize, samples_per_channel);
    555   EXPECT_EQ(1u, num_channels);
    556   EXPECT_EQ(kOutputNormal, type);
    557 
    558   // Insert two more packets. The first one is out of order, and is already too
    559   // old, the second one is the expected next packet.
    560   rtp_header.header.sequenceNumber -= 1;
    561   rtp_header.header.timestamp -= kPayloadLengthSamples;
    562   payload[0] = 1;
    563   EXPECT_EQ(NetEq::kOK,
    564             neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
    565   rtp_header.header.sequenceNumber += 2;
    566   rtp_header.header.timestamp += 2 * kPayloadLengthSamples;
    567   payload[0] = 2;
    568   EXPECT_EQ(NetEq::kOK,
    569             neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
    570 
    571   // Expect only the second packet to be decoded (the one with "2" as the first
    572   // payload byte).
    573   EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
    574                                            kSampleRateHz, _, _))
    575       .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
    576                                           dummy_output + kPayloadLengthSamples),
    577                       SetArgPointee<4>(AudioDecoder::kSpeech),
    578                       Return(kPayloadLengthSamples)));
    579 
    580   // Pull audio once.
    581   EXPECT_EQ(
    582       NetEq::kOK,
    583       neteq_->GetAudio(
    584           kMaxOutputSize, output, &samples_per_channel, &num_channels, &type));
    585   ASSERT_EQ(kMaxOutputSize, samples_per_channel);
    586   EXPECT_EQ(1u, num_channels);
    587   EXPECT_EQ(kOutputNormal, type);
    588 
    589   // Now check the packet buffer, and make sure it is empty, since the
    590   // out-of-order packet should have been discarded.
    591   EXPECT_TRUE(packet_buffer_->Empty());
    592 
    593   EXPECT_CALL(mock_decoder, Die());
    594 }
    595 
    596 // This test verifies that NetEq can handle the situation where the first
    597 // incoming packet is rejected.
    598 TEST_F(NetEqImplTest, FirstPacketUnknown) {
    599   UseNoMocks();
    600   CreateInstance();
    601 
    602   const uint8_t kPayloadType = 17;   // Just an arbitrary number.
    603   const uint32_t kReceiveTime = 17;  // Value doesn't matter for this test.
    604   const int kSampleRateHz = 8000;
    605   const size_t kPayloadLengthSamples =
    606       static_cast<size_t>(10 * kSampleRateHz / 1000);  // 10 ms.
    607   const size_t kPayloadLengthBytes = kPayloadLengthSamples;
    608   uint8_t payload[kPayloadLengthBytes] = {0};
    609   WebRtcRTPHeader rtp_header;
    610   rtp_header.header.payloadType = kPayloadType;
    611   rtp_header.header.sequenceNumber = 0x1234;
    612   rtp_header.header.timestamp = 0x12345678;
    613   rtp_header.header.ssrc = 0x87654321;
    614 
    615   // Insert one packet. Note that we have not registered any payload type, so
    616   // this packet will be rejected.
    617   EXPECT_EQ(NetEq::kFail,
    618             neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
    619   EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
    620 
    621   // Pull audio once.
    622   const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
    623   int16_t output[kMaxOutputSize];
    624   size_t samples_per_channel;
    625   size_t num_channels;
    626   NetEqOutputType type;
    627   EXPECT_EQ(NetEq::kOK,
    628             neteq_->GetAudio(kMaxOutputSize, output, &samples_per_channel,
    629                              &num_channels, &type));
    630   ASSERT_LE(samples_per_channel, kMaxOutputSize);
    631   EXPECT_EQ(kMaxOutputSize, samples_per_channel);
    632   EXPECT_EQ(1u, num_channels);
    633   EXPECT_EQ(kOutputPLC, type);
    634 
    635   // Register the payload type.
    636   EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
    637                             NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
    638 
    639   // Insert 10 packets.
    640   for (size_t i = 0; i < 10; ++i) {
    641     rtp_header.header.sequenceNumber++;
    642     rtp_header.header.timestamp += kPayloadLengthSamples;
    643     EXPECT_EQ(NetEq::kOK,
    644               neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
    645     EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
    646   }
    647 
    648   // Pull audio repeatedly and make sure we get normal output, that is not PLC.
    649   for (size_t i = 0; i < 3; ++i) {
    650     EXPECT_EQ(NetEq::kOK,
    651               neteq_->GetAudio(kMaxOutputSize, output, &samples_per_channel,
    652                                &num_channels, &type));
    653     ASSERT_LE(samples_per_channel, kMaxOutputSize);
    654     EXPECT_EQ(kMaxOutputSize, samples_per_channel);
    655     EXPECT_EQ(1u, num_channels);
    656     EXPECT_EQ(kOutputNormal, type)
    657         << "NetEq did not decode the packets as expected.";
    658   }
    659 }
    660 
    661 // This test verifies that NetEq can handle comfort noise and enters/quits codec
    662 // internal CNG mode properly.
    663 TEST_F(NetEqImplTest, CodecInternalCng) {
    664   UseNoMocks();
    665   CreateInstance();
    666 
    667   const uint8_t kPayloadType = 17;   // Just an arbitrary number.
    668   const uint32_t kReceiveTime = 17;  // Value doesn't matter for this test.
    669   const int kSampleRateKhz = 48;
    670   const size_t kPayloadLengthSamples =
    671       static_cast<size_t>(20 * kSampleRateKhz);  // 20 ms.
    672   const size_t kPayloadLengthBytes = 10;
    673   uint8_t payload[kPayloadLengthBytes] = {0};
    674   int16_t dummy_output[kPayloadLengthSamples] = {0};
    675 
    676   WebRtcRTPHeader rtp_header;
    677   rtp_header.header.payloadType = kPayloadType;
    678   rtp_header.header.sequenceNumber = 0x1234;
    679   rtp_header.header.timestamp = 0x12345678;
    680   rtp_header.header.ssrc = 0x87654321;
    681 
    682   // Create a mock decoder object.
    683   MockAudioDecoder mock_decoder;
    684   EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
    685   EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
    686   EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
    687       .WillRepeatedly(Return(0));
    688 
    689   // Pointee(x) verifies that first byte of the payload equals x, this makes it
    690   // possible to verify that the correct payload is fed to Decode().
    691   EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
    692                                            kSampleRateKhz * 1000, _, _))
    693       .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
    694                                           dummy_output + kPayloadLengthSamples),
    695                       SetArgPointee<4>(AudioDecoder::kSpeech),
    696                       Return(kPayloadLengthSamples)));
    697 
    698   EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
    699                                            kSampleRateKhz * 1000, _, _))
    700       .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
    701                                           dummy_output + kPayloadLengthSamples),
    702                       SetArgPointee<4>(AudioDecoder::kComfortNoise),
    703                       Return(kPayloadLengthSamples)));
    704 
    705   EXPECT_CALL(mock_decoder,
    706               DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
    707       .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
    708                                           dummy_output + kPayloadLengthSamples),
    709                       SetArgPointee<4>(AudioDecoder::kComfortNoise),
    710                       Return(kPayloadLengthSamples)));
    711 
    712   EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
    713                                            kSampleRateKhz * 1000, _, _))
    714       .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
    715                                           dummy_output + kPayloadLengthSamples),
    716                       SetArgPointee<4>(AudioDecoder::kSpeech),
    717                       Return(kPayloadLengthSamples)));
    718 
    719   EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
    720                             &mock_decoder, NetEqDecoder::kDecoderOpus,
    721                             "dummy name", kPayloadType, kSampleRateKhz * 1000));
    722 
    723   // Insert one packet (decoder will return speech).
    724   EXPECT_EQ(NetEq::kOK,
    725             neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
    726 
    727   // Insert second packet (decoder will return CNG).
    728   payload[0] = 1;
    729   rtp_header.header.sequenceNumber++;
    730   rtp_header.header.timestamp += kPayloadLengthSamples;
    731   EXPECT_EQ(NetEq::kOK,
    732             neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
    733 
    734   const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
    735   int16_t output[kMaxOutputSize];
    736   size_t samples_per_channel;
    737   size_t num_channels;
    738   uint32_t timestamp;
    739   uint32_t last_timestamp;
    740   NetEqOutputType type;
    741   NetEqOutputType expected_type[8] = {
    742       kOutputNormal, kOutputNormal,
    743       kOutputCNG, kOutputCNG,
    744       kOutputCNG, kOutputCNG,
    745       kOutputNormal, kOutputNormal
    746   };
    747   int expected_timestamp_increment[8] = {
    748       -1,  // will not be used.
    749       10 * kSampleRateKhz,
    750       0, 0,  // timestamp does not increase during CNG mode.
    751       0, 0,
    752       50 * kSampleRateKhz, 10 * kSampleRateKhz
    753   };
    754 
    755   EXPECT_EQ(NetEq::kOK,
    756             neteq_->GetAudio(kMaxOutputSize, output, &samples_per_channel,
    757                              &num_channels, &type));
    758   EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&last_timestamp));
    759 
    760   for (size_t i = 1; i < 6; ++i) {
    761     ASSERT_EQ(kMaxOutputSize, samples_per_channel);
    762     EXPECT_EQ(1u, num_channels);
    763     EXPECT_EQ(expected_type[i - 1], type);
    764     EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&timestamp));
    765     EXPECT_EQ(NetEq::kOK,
    766               neteq_->GetAudio(kMaxOutputSize, output, &samples_per_channel,
    767                                &num_channels, &type));
    768     EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&timestamp));
    769     EXPECT_EQ(timestamp, last_timestamp + expected_timestamp_increment[i]);
    770     last_timestamp = timestamp;
    771   }
    772 
    773   // Insert third packet, which leaves a gap from last packet.
    774   payload[0] = 2;
    775   rtp_header.header.sequenceNumber += 2;
    776   rtp_header.header.timestamp += 2 * kPayloadLengthSamples;
    777   EXPECT_EQ(NetEq::kOK,
    778             neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
    779 
    780   for (size_t i = 6; i < 8; ++i) {
    781     ASSERT_EQ(kMaxOutputSize, samples_per_channel);
    782     EXPECT_EQ(1u, num_channels);
    783     EXPECT_EQ(expected_type[i - 1], type);
    784     EXPECT_EQ(NetEq::kOK,
    785               neteq_->GetAudio(kMaxOutputSize, output, &samples_per_channel,
    786                                &num_channels, &type));
    787     EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&timestamp));
    788     EXPECT_EQ(timestamp, last_timestamp + expected_timestamp_increment[i]);
    789     last_timestamp = timestamp;
    790   }
    791 
    792   // Now check the packet buffer, and make sure it is empty.
    793   EXPECT_TRUE(packet_buffer_->Empty());
    794 
    795   EXPECT_CALL(mock_decoder, Die());
    796 }
    797 
    798 TEST_F(NetEqImplTest, UnsupportedDecoder) {
    799   UseNoMocks();
    800   CreateInstance();
    801   static const size_t kNetEqMaxFrameSize = 2880;  // 60 ms @ 48 kHz.
    802   static const size_t kChannels = 2;
    803 
    804   const uint8_t kPayloadType = 17;   // Just an arbitrary number.
    805   const uint32_t kReceiveTime = 17;  // Value doesn't matter for this test.
    806   const int kSampleRateHz = 8000;
    807 
    808   const size_t kPayloadLengthSamples =
    809       static_cast<size_t>(10 * kSampleRateHz / 1000);  // 10 ms.
    810   const size_t kPayloadLengthBytes = 1;
    811   uint8_t payload[kPayloadLengthBytes]= {0};
    812   int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
    813   WebRtcRTPHeader rtp_header;
    814   rtp_header.header.payloadType = kPayloadType;
    815   rtp_header.header.sequenceNumber = 0x1234;
    816   rtp_header.header.timestamp = 0x12345678;
    817   rtp_header.header.ssrc = 0x87654321;
    818 
    819   class MockAudioDecoder : public AudioDecoder {
    820    public:
    821     void Reset() override {}
    822     MOCK_CONST_METHOD2(PacketDuration, int(const uint8_t*, size_t));
    823     MOCK_METHOD5(DecodeInternal, int(const uint8_t*, size_t, int, int16_t*,
    824                                      SpeechType*));
    825     size_t Channels() const override { return kChannels; }
    826   } decoder_;
    827 
    828   const uint8_t kFirstPayloadValue = 1;
    829   const uint8_t kSecondPayloadValue = 2;
    830 
    831   EXPECT_CALL(decoder_, PacketDuration(Pointee(kFirstPayloadValue),
    832                                        kPayloadLengthBytes))
    833     .Times(AtLeast(1))
    834     .WillRepeatedly(Return(kNetEqMaxFrameSize + 1));
    835 
    836   EXPECT_CALL(decoder_,
    837               DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
    838       .Times(0);
    839 
    840   EXPECT_CALL(decoder_, DecodeInternal(Pointee(kSecondPayloadValue),
    841                                        kPayloadLengthBytes,
    842                                        kSampleRateHz, _, _))
    843       .Times(1)
    844       .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
    845                                           dummy_output +
    846                                           kPayloadLengthSamples * kChannels),
    847                       SetArgPointee<4>(AudioDecoder::kSpeech),
    848                       Return(static_cast<int>(
    849                           kPayloadLengthSamples * kChannels))));
    850 
    851   EXPECT_CALL(decoder_, PacketDuration(Pointee(kSecondPayloadValue),
    852                                        kPayloadLengthBytes))
    853     .Times(AtLeast(1))
    854     .WillRepeatedly(Return(kNetEqMaxFrameSize));
    855 
    856   EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
    857                             &decoder_, NetEqDecoder::kDecoderPCM16B,
    858                             "dummy name", kPayloadType, kSampleRateHz));
    859 
    860   // Insert one packet.
    861   payload[0] = kFirstPayloadValue;  // This will make Decode() fail.
    862   EXPECT_EQ(NetEq::kOK,
    863             neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
    864 
    865   // Insert another packet.
    866   payload[0] = kSecondPayloadValue;  // This will make Decode() successful.
    867   rtp_header.header.sequenceNumber++;
    868   // The second timestamp needs to be at least 30 ms after the first to make
    869   // the second packet get decoded.
    870   rtp_header.header.timestamp += 3 * kPayloadLengthSamples;
    871   EXPECT_EQ(NetEq::kOK,
    872             neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
    873 
    874   const size_t kMaxOutputSize = 10 * kSampleRateHz / 1000 * kChannels;
    875   int16_t output[kMaxOutputSize];
    876   size_t samples_per_channel;
    877   size_t num_channels;
    878   NetEqOutputType type;
    879 
    880   EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(kMaxOutputSize, output,
    881                                            &samples_per_channel, &num_channels,
    882                                            &type));
    883   EXPECT_EQ(NetEq::kOtherDecoderError, neteq_->LastError());
    884   EXPECT_EQ(kMaxOutputSize, samples_per_channel * kChannels);
    885   EXPECT_EQ(kChannels, num_channels);
    886 
    887   EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(kMaxOutputSize, output,
    888                                          &samples_per_channel, &num_channels,
    889                                          &type));
    890   EXPECT_EQ(kMaxOutputSize, samples_per_channel * kChannels);
    891   EXPECT_EQ(kChannels, num_channels);
    892 }
    893 
    894 // This test inserts packets until the buffer is flushed. After that, it asks
    895 // NetEq for the network statistics. The purpose of the test is to make sure
    896 // that even though the buffer size increment is negative (which it becomes when
    897 // the packet causing a flush is inserted), the packet length stored in the
    898 // decision logic remains valid.
    899 TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
    900   UseNoMocks();
    901   CreateInstance();
    902 
    903   const size_t kPayloadLengthSamples = 80;
    904   const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;  // PCM 16-bit.
    905   const uint8_t kPayloadType = 17;   // Just an arbitrary number.
    906   const uint32_t kReceiveTime = 17;  // Value doesn't matter for this test.
    907   uint8_t payload[kPayloadLengthBytes] = {0};
    908   WebRtcRTPHeader rtp_header;
    909   rtp_header.header.payloadType = kPayloadType;
    910   rtp_header.header.sequenceNumber = 0x1234;
    911   rtp_header.header.timestamp = 0x12345678;
    912   rtp_header.header.ssrc = 0x87654321;
    913 
    914   EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
    915                             NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
    916 
    917   // Insert packets until the buffer flushes.
    918   for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
    919     EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
    920     EXPECT_EQ(NetEq::kOK,
    921               neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
    922     rtp_header.header.timestamp +=
    923         rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
    924     ++rtp_header.header.sequenceNumber;
    925   }
    926   EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
    927 
    928   // Ask for network statistics. This should not crash.
    929   NetEqNetworkStatistics stats;
    930   EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
    931 }
    932 
    933 TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
    934   UseNoMocks();
    935   CreateInstance();
    936 
    937   const uint8_t kPayloadType = 17;   // Just an arbitrary number.
    938   const uint32_t kReceiveTime = 17;  // Value doesn't matter for this test.
    939   const int kSampleRateHz = 8000;
    940   const size_t kPayloadLengthSamples =
    941       static_cast<size_t>(10 * kSampleRateHz / 1000);  // 10 ms.
    942   const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
    943   uint8_t payload[kPayloadLengthBytes] = {0};
    944   WebRtcRTPHeader rtp_header;
    945   rtp_header.header.payloadType = kPayloadType;
    946   rtp_header.header.sequenceNumber = 0x1234;
    947   rtp_header.header.timestamp = 0x12345678;
    948   rtp_header.header.ssrc = 0x87654321;
    949 
    950   // Create a mock decoder object.
    951   MockAudioDecoder mock_decoder;
    952   EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
    953   EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
    954   EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
    955       .WillRepeatedly(Return(0));
    956   EXPECT_CALL(mock_decoder, PacketDuration(_, _))
    957       .WillRepeatedly(Return(kPayloadLengthSamples));
    958   int16_t dummy_output[kPayloadLengthSamples] = {0};
    959   // The below expectation will make the mock decoder write
    960   // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
    961   // speech. That is, the decoded length is 5 samples shorter than the expected.
    962   EXPECT_CALL(mock_decoder,
    963               DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
    964       .WillOnce(
    965           DoAll(SetArrayArgument<3>(dummy_output,
    966                                     dummy_output + kPayloadLengthSamples - 5),
    967                 SetArgPointee<4>(AudioDecoder::kSpeech),
    968                 Return(kPayloadLengthSamples - 5)));
    969   EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
    970                             &mock_decoder, NetEqDecoder::kDecoderPCM16B,
    971                             "dummy name", kPayloadType, kSampleRateHz));
    972 
    973   // Insert one packet.
    974   EXPECT_EQ(NetEq::kOK,
    975             neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
    976 
    977   EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
    978 
    979   // Pull audio once.
    980   const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
    981   int16_t output[kMaxOutputSize];
    982   size_t samples_per_channel;
    983   size_t num_channels;
    984   NetEqOutputType type;
    985   EXPECT_EQ(NetEq::kOK,
    986             neteq_->GetAudio(kMaxOutputSize, output, &samples_per_channel,
    987                              &num_channels, &type));
    988   ASSERT_EQ(kMaxOutputSize, samples_per_channel);
    989   EXPECT_EQ(1u, num_channels);
    990   EXPECT_EQ(kOutputNormal, type);
    991 
    992   EXPECT_CALL(mock_decoder, Die());
    993 }
    994 
    995 // This test checks the behavior of NetEq when audio decoder fails.
    996 TEST_F(NetEqImplTest, DecodingError) {
    997   UseNoMocks();
    998   CreateInstance();
    999 
   1000   const uint8_t kPayloadType = 17;   // Just an arbitrary number.
   1001   const uint32_t kReceiveTime = 17;  // Value doesn't matter for this test.
   1002   const int kSampleRateHz = 8000;
   1003   const int kDecoderErrorCode = -97;  // Any negative number.
   1004 
   1005   // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
   1006   const size_t kFrameLengthSamples =
   1007       static_cast<size_t>(5 * kSampleRateHz / 1000);
   1008 
   1009   const size_t kPayloadLengthBytes = 1;  // This can be arbitrary.
   1010 
   1011   uint8_t payload[kPayloadLengthBytes] = {0};
   1012 
   1013   WebRtcRTPHeader rtp_header;
   1014   rtp_header.header.payloadType = kPayloadType;
   1015   rtp_header.header.sequenceNumber = 0x1234;
   1016   rtp_header.header.timestamp = 0x12345678;
   1017   rtp_header.header.ssrc = 0x87654321;
   1018 
   1019   // Create a mock decoder object.
   1020   MockAudioDecoder mock_decoder;
   1021   EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
   1022   EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
   1023   EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
   1024       .WillRepeatedly(Return(0));
   1025   EXPECT_CALL(mock_decoder, PacketDuration(_, _))
   1026       .WillRepeatedly(Return(kFrameLengthSamples));
   1027   EXPECT_CALL(mock_decoder, ErrorCode())
   1028       .WillOnce(Return(kDecoderErrorCode));
   1029   EXPECT_CALL(mock_decoder, HasDecodePlc())
   1030       .WillOnce(Return(false));
   1031   int16_t dummy_output[kFrameLengthSamples] = {0};
   1032 
   1033   {
   1034     InSequence sequence;  // Dummy variable.
   1035     // Mock decoder works normally the first time.
   1036     EXPECT_CALL(mock_decoder,
   1037                 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
   1038         .Times(3)
   1039         .WillRepeatedly(
   1040             DoAll(SetArrayArgument<3>(dummy_output,
   1041                                       dummy_output + kFrameLengthSamples),
   1042                   SetArgPointee<4>(AudioDecoder::kSpeech),
   1043                   Return(kFrameLengthSamples)))
   1044         .RetiresOnSaturation();
   1045 
   1046     // Then mock decoder fails. A common reason for failure can be buffer being
   1047     // too short
   1048     EXPECT_CALL(mock_decoder,
   1049                 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
   1050         .WillOnce(Return(-1))
   1051         .RetiresOnSaturation();
   1052 
   1053     // Mock decoder finally returns to normal.
   1054     EXPECT_CALL(mock_decoder,
   1055                 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
   1056         .Times(2)
   1057         .WillRepeatedly(
   1058             DoAll(SetArrayArgument<3>(dummy_output,
   1059                                       dummy_output + kFrameLengthSamples),
   1060                   SetArgPointee<4>(AudioDecoder::kSpeech),
   1061                   Return(kFrameLengthSamples)));
   1062   }
   1063 
   1064   EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
   1065                             &mock_decoder, NetEqDecoder::kDecoderPCM16B,
   1066                             "dummy name", kPayloadType, kSampleRateHz));
   1067 
   1068   // Insert packets.
   1069   for (int i = 0; i < 6; ++i) {
   1070     rtp_header.header.sequenceNumber += 1;
   1071     rtp_header.header.timestamp += kFrameLengthSamples;
   1072     EXPECT_EQ(NetEq::kOK,
   1073               neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
   1074   }
   1075 
   1076   // Pull audio.
   1077   const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
   1078   int16_t output[kMaxOutputSize];
   1079   size_t samples_per_channel;
   1080   size_t num_channels;
   1081   NetEqOutputType type;
   1082   EXPECT_EQ(NetEq::kOK,
   1083             neteq_->GetAudio(kMaxOutputSize, output, &samples_per_channel,
   1084                              &num_channels, &type));
   1085   EXPECT_EQ(kMaxOutputSize, samples_per_channel);
   1086   EXPECT_EQ(1u, num_channels);
   1087   EXPECT_EQ(kOutputNormal, type);
   1088 
   1089   // Pull audio again. Decoder fails.
   1090   EXPECT_EQ(NetEq::kFail,
   1091             neteq_->GetAudio(kMaxOutputSize, output, &samples_per_channel,
   1092                              &num_channels, &type));
   1093   EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
   1094   EXPECT_EQ(kDecoderErrorCode, neteq_->LastDecoderError());
   1095   EXPECT_EQ(kMaxOutputSize, samples_per_channel);
   1096   EXPECT_EQ(1u, num_channels);
   1097   // TODO(minyue): should NetEq better give kOutputPLC, since it is actually an
   1098   // expansion.
   1099   EXPECT_EQ(kOutputNormal, type);
   1100 
   1101   // Pull audio again, should continue an expansion.
   1102   EXPECT_EQ(NetEq::kOK,
   1103             neteq_->GetAudio(kMaxOutputSize, output, &samples_per_channel,
   1104                              &num_channels, &type));
   1105   EXPECT_EQ(kMaxOutputSize, samples_per_channel);
   1106   EXPECT_EQ(1u, num_channels);
   1107   EXPECT_EQ(kOutputPLC, type);
   1108 
   1109   // Pull audio again, should behave normal.
   1110   EXPECT_EQ(NetEq::kOK,
   1111             neteq_->GetAudio(kMaxOutputSize, output, &samples_per_channel,
   1112                              &num_channels, &type));
   1113   EXPECT_EQ(kMaxOutputSize, samples_per_channel);
   1114   EXPECT_EQ(1u, num_channels);
   1115   EXPECT_EQ(kOutputNormal, type);
   1116 
   1117   EXPECT_CALL(mock_decoder, Die());
   1118 }
   1119 
   1120 // This test checks the behavior of NetEq when audio decoder fails during CNG.
   1121 TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
   1122   UseNoMocks();
   1123   CreateInstance();
   1124 
   1125   const uint8_t kPayloadType = 17;   // Just an arbitrary number.
   1126   const uint32_t kReceiveTime = 17;  // Value doesn't matter for this test.
   1127   const int kSampleRateHz = 8000;
   1128   const int kDecoderErrorCode = -97;  // Any negative number.
   1129 
   1130   // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
   1131   const size_t kFrameLengthSamples =
   1132       static_cast<size_t>(5 * kSampleRateHz / 1000);
   1133 
   1134   const size_t kPayloadLengthBytes = 1;  // This can be arbitrary.
   1135 
   1136   uint8_t payload[kPayloadLengthBytes] = {0};
   1137 
   1138   WebRtcRTPHeader rtp_header;
   1139   rtp_header.header.payloadType = kPayloadType;
   1140   rtp_header.header.sequenceNumber = 0x1234;
   1141   rtp_header.header.timestamp = 0x12345678;
   1142   rtp_header.header.ssrc = 0x87654321;
   1143 
   1144   // Create a mock decoder object.
   1145   MockAudioDecoder mock_decoder;
   1146   EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
   1147   EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
   1148   EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
   1149       .WillRepeatedly(Return(0));
   1150   EXPECT_CALL(mock_decoder, PacketDuration(_, _))
   1151       .WillRepeatedly(Return(kFrameLengthSamples));
   1152   EXPECT_CALL(mock_decoder, ErrorCode())
   1153       .WillOnce(Return(kDecoderErrorCode));
   1154   int16_t dummy_output[kFrameLengthSamples] = {0};
   1155 
   1156   {
   1157     InSequence sequence;  // Dummy variable.
   1158     // Mock decoder works normally the first 2 times.
   1159     EXPECT_CALL(mock_decoder,
   1160                 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
   1161         .Times(2)
   1162         .WillRepeatedly(
   1163             DoAll(SetArrayArgument<3>(dummy_output,
   1164                                       dummy_output + kFrameLengthSamples),
   1165                   SetArgPointee<4>(AudioDecoder::kComfortNoise),
   1166                   Return(kFrameLengthSamples)))
   1167         .RetiresOnSaturation();
   1168 
   1169     // Then mock decoder fails. A common reason for failure can be buffer being
   1170     // too short
   1171     EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
   1172         .WillOnce(Return(-1))
   1173         .RetiresOnSaturation();
   1174 
   1175     // Mock decoder finally returns to normal.
   1176     EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
   1177         .Times(2)
   1178         .WillRepeatedly(
   1179             DoAll(SetArrayArgument<3>(dummy_output,
   1180                                       dummy_output + kFrameLengthSamples),
   1181                   SetArgPointee<4>(AudioDecoder::kComfortNoise),
   1182                   Return(kFrameLengthSamples)));
   1183   }
   1184 
   1185   EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
   1186                             &mock_decoder, NetEqDecoder::kDecoderPCM16B,
   1187                             "dummy name", kPayloadType, kSampleRateHz));
   1188 
   1189   // Insert 2 packets. This will make netEq into codec internal CNG mode.
   1190   for (int i = 0; i < 2; ++i) {
   1191     rtp_header.header.sequenceNumber += 1;
   1192     rtp_header.header.timestamp += kFrameLengthSamples;
   1193     EXPECT_EQ(NetEq::kOK,
   1194               neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
   1195   }
   1196 
   1197   // Pull audio.
   1198   const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
   1199   int16_t output[kMaxOutputSize];
   1200   size_t samples_per_channel;
   1201   size_t num_channels;
   1202   NetEqOutputType type;
   1203   EXPECT_EQ(NetEq::kOK,
   1204             neteq_->GetAudio(kMaxOutputSize, output, &samples_per_channel,
   1205                              &num_channels, &type));
   1206   EXPECT_EQ(kMaxOutputSize, samples_per_channel);
   1207   EXPECT_EQ(1u, num_channels);
   1208   EXPECT_EQ(kOutputCNG, type);
   1209 
   1210   // Pull audio again. Decoder fails.
   1211   EXPECT_EQ(NetEq::kFail,
   1212             neteq_->GetAudio(kMaxOutputSize, output, &samples_per_channel,
   1213                              &num_channels, &type));
   1214   EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
   1215   EXPECT_EQ(kDecoderErrorCode, neteq_->LastDecoderError());
   1216   EXPECT_EQ(kMaxOutputSize, samples_per_channel);
   1217   EXPECT_EQ(1u, num_channels);
   1218   // TODO(minyue): should NetEq better give kOutputPLC, since it is actually an
   1219   // expansion.
   1220   EXPECT_EQ(kOutputCNG, type);
   1221 
   1222   // Pull audio again, should resume codec CNG.
   1223   EXPECT_EQ(NetEq::kOK,
   1224             neteq_->GetAudio(kMaxOutputSize, output, &samples_per_channel,
   1225                              &num_channels, &type));
   1226   EXPECT_EQ(kMaxOutputSize, samples_per_channel);
   1227   EXPECT_EQ(1u, num_channels);
   1228   EXPECT_EQ(kOutputCNG, type);
   1229 
   1230   EXPECT_CALL(mock_decoder, Die());
   1231 }
   1232 
   1233 // Tests that the return value from last_output_sample_rate_hz() is equal to the
   1234 // configured inital sample rate.
   1235 TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
   1236   UseNoMocks();
   1237   config_.sample_rate_hz = 48000;
   1238   CreateInstance();
   1239   EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
   1240 }
   1241 
   1242 }// namespace webrtc
   1243