Home | History | Annotate | Download | only in acm2
      1 /*
      2  *  Copyright (c) 2014 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 <string.h>
     12 #include <vector>
     13 
     14 #include "testing/gtest/include/gtest/gtest.h"
     15 #include "webrtc/base/md5digest.h"
     16 #include "webrtc/base/thread_annotations.h"
     17 #include "webrtc/modules/audio_coding/main/acm2/acm_receive_test_oldapi.h"
     18 #include "webrtc/modules/audio_coding/main/acm2/acm_send_test_oldapi.h"
     19 #include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h"
     20 #include "webrtc/modules/audio_coding/main/interface/audio_coding_module_typedefs.h"
     21 #include "webrtc/modules/audio_coding/neteq/tools/audio_checksum.h"
     22 #include "webrtc/modules/audio_coding/neteq/tools/audio_loop.h"
     23 #include "webrtc/modules/audio_coding/neteq/tools/input_audio_file.h"
     24 #include "webrtc/modules/audio_coding/neteq/tools/output_audio_file.h"
     25 #include "webrtc/modules/audio_coding/neteq/tools/packet.h"
     26 #include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h"
     27 #include "webrtc/modules/interface/module_common_types.h"
     28 #include "webrtc/system_wrappers/interface/clock.h"
     29 #include "webrtc/system_wrappers/interface/compile_assert.h"
     30 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
     31 #include "webrtc/system_wrappers/interface/event_wrapper.h"
     32 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
     33 #include "webrtc/system_wrappers/interface/sleep.h"
     34 #include "webrtc/system_wrappers/interface/thread_wrapper.h"
     35 #include "webrtc/test/testsupport/fileutils.h"
     36 #include "webrtc/test/testsupport/gtest_disable.h"
     37 
     38 namespace webrtc {
     39 
     40 const int kSampleRateHz = 16000;
     41 const int kNumSamples10ms = kSampleRateHz / 100;
     42 const int kFrameSizeMs = 10;  // Multiple of 10.
     43 const int kFrameSizeSamples = kFrameSizeMs / 10 * kNumSamples10ms;
     44 const int kPayloadSizeBytes = kFrameSizeSamples * sizeof(int16_t);
     45 const uint8_t kPayloadType = 111;
     46 
     47 class RtpUtility {
     48  public:
     49   RtpUtility(int samples_per_packet, uint8_t payload_type)
     50       : samples_per_packet_(samples_per_packet), payload_type_(payload_type) {}
     51 
     52   virtual ~RtpUtility() {}
     53 
     54   void Populate(WebRtcRTPHeader* rtp_header) {
     55     rtp_header->header.sequenceNumber = 0xABCD;
     56     rtp_header->header.timestamp = 0xABCDEF01;
     57     rtp_header->header.payloadType = payload_type_;
     58     rtp_header->header.markerBit = false;
     59     rtp_header->header.ssrc = 0x1234;
     60     rtp_header->header.numCSRCs = 0;
     61     rtp_header->frameType = kAudioFrameSpeech;
     62 
     63     rtp_header->header.payload_type_frequency = kSampleRateHz;
     64     rtp_header->type.Audio.channel = 1;
     65     rtp_header->type.Audio.isCNG = false;
     66   }
     67 
     68   void Forward(WebRtcRTPHeader* rtp_header) {
     69     ++rtp_header->header.sequenceNumber;
     70     rtp_header->header.timestamp += samples_per_packet_;
     71   }
     72 
     73  private:
     74   int samples_per_packet_;
     75   uint8_t payload_type_;
     76 };
     77 
     78 class PacketizationCallbackStub : public AudioPacketizationCallback {
     79  public:
     80   PacketizationCallbackStub()
     81       : num_calls_(0),
     82         crit_sect_(CriticalSectionWrapper::CreateCriticalSection()) {}
     83 
     84   virtual int32_t SendData(
     85       FrameType frame_type,
     86       uint8_t payload_type,
     87       uint32_t timestamp,
     88       const uint8_t* payload_data,
     89       uint16_t payload_len_bytes,
     90       const RTPFragmentationHeader* fragmentation) OVERRIDE {
     91     CriticalSectionScoped lock(crit_sect_.get());
     92     ++num_calls_;
     93     last_payload_vec_.assign(payload_data, payload_data + payload_len_bytes);
     94     return 0;
     95   }
     96 
     97   int num_calls() const {
     98     CriticalSectionScoped lock(crit_sect_.get());
     99     return num_calls_;
    100   }
    101 
    102   int last_payload_len_bytes() const {
    103     CriticalSectionScoped lock(crit_sect_.get());
    104     return last_payload_vec_.size();
    105   }
    106 
    107   void SwapBuffers(std::vector<uint8_t>* payload) {
    108     CriticalSectionScoped lock(crit_sect_.get());
    109     last_payload_vec_.swap(*payload);
    110   }
    111 
    112  private:
    113   int num_calls_ GUARDED_BY(crit_sect_);
    114   std::vector<uint8_t> last_payload_vec_ GUARDED_BY(crit_sect_);
    115   const scoped_ptr<CriticalSectionWrapper> crit_sect_;
    116 };
    117 
    118 class AudioCodingModuleTestOldApi : public ::testing::Test {
    119  protected:
    120   AudioCodingModuleTestOldApi()
    121       : id_(1),
    122         rtp_utility_(new RtpUtility(kFrameSizeSamples, kPayloadType)),
    123         clock_(Clock::GetRealTimeClock()) {}
    124 
    125   ~AudioCodingModuleTestOldApi() {}
    126 
    127   void TearDown() {}
    128 
    129   void SetUp() {
    130     acm_.reset(AudioCodingModule::Create(id_, clock_));
    131 
    132     RegisterCodec();
    133 
    134     rtp_utility_->Populate(&rtp_header_);
    135 
    136     input_frame_.sample_rate_hz_ = kSampleRateHz;
    137     input_frame_.num_channels_ = 1;
    138     input_frame_.samples_per_channel_ = kSampleRateHz * 10 / 1000;  // 10 ms.
    139     COMPILE_ASSERT(kSampleRateHz * 10 / 1000 <= AudioFrame::kMaxDataSizeSamples,
    140                    audio_frame_too_small);
    141     memset(input_frame_.data_,
    142            0,
    143            input_frame_.samples_per_channel_ * sizeof(input_frame_.data_[0]));
    144 
    145     ASSERT_EQ(0, acm_->RegisterTransportCallback(&packet_cb_));
    146   }
    147 
    148   virtual void RegisterCodec() {
    149     AudioCodingModule::Codec("L16", &codec_, kSampleRateHz, 1);
    150     codec_.pltype = kPayloadType;
    151 
    152     // Register L16 codec in ACM.
    153     ASSERT_EQ(0, acm_->RegisterReceiveCodec(codec_));
    154     ASSERT_EQ(0, acm_->RegisterSendCodec(codec_));
    155   }
    156 
    157   virtual void InsertPacketAndPullAudio() {
    158     InsertPacket();
    159     PullAudio();
    160   }
    161 
    162   virtual void InsertPacket() {
    163     const uint8_t kPayload[kPayloadSizeBytes] = {0};
    164     ASSERT_EQ(0,
    165               acm_->IncomingPacket(kPayload, kPayloadSizeBytes, rtp_header_));
    166     rtp_utility_->Forward(&rtp_header_);
    167   }
    168 
    169   virtual void PullAudio() {
    170     AudioFrame audio_frame;
    171     ASSERT_EQ(0, acm_->PlayoutData10Ms(-1, &audio_frame));
    172   }
    173 
    174   virtual void InsertAudio() {
    175     ASSERT_EQ(0, acm_->Add10MsData(input_frame_));
    176     input_frame_.timestamp_ += kNumSamples10ms;
    177   }
    178 
    179   virtual void Encode() {
    180     int32_t encoded_bytes = acm_->Process();
    181     // Expect to get one packet with two bytes per sample, or no packet at all,
    182     // depending on how many 10 ms blocks go into |codec_.pacsize|.
    183     EXPECT_TRUE(encoded_bytes == 2 * codec_.pacsize || encoded_bytes == 0);
    184   }
    185 
    186   const int id_;
    187   scoped_ptr<RtpUtility> rtp_utility_;
    188   scoped_ptr<AudioCodingModule> acm_;
    189   PacketizationCallbackStub packet_cb_;
    190   WebRtcRTPHeader rtp_header_;
    191   AudioFrame input_frame_;
    192   CodecInst codec_;
    193   Clock* clock_;
    194 };
    195 
    196 // Check if the statistics are initialized correctly. Before any call to ACM
    197 // all fields have to be zero.
    198 TEST_F(AudioCodingModuleTestOldApi, DISABLED_ON_ANDROID(InitializedToZero)) {
    199   AudioDecodingCallStats stats;
    200   acm_->GetDecodingCallStatistics(&stats);
    201   EXPECT_EQ(0, stats.calls_to_neteq);
    202   EXPECT_EQ(0, stats.calls_to_silence_generator);
    203   EXPECT_EQ(0, stats.decoded_normal);
    204   EXPECT_EQ(0, stats.decoded_cng);
    205   EXPECT_EQ(0, stats.decoded_plc);
    206   EXPECT_EQ(0, stats.decoded_plc_cng);
    207 }
    208 
    209 // Apply an initial playout delay. Calls to AudioCodingModule::PlayoutData10ms()
    210 // should result in generating silence, check the associated field.
    211 TEST_F(AudioCodingModuleTestOldApi,
    212        DISABLED_ON_ANDROID(SilenceGeneratorCalled)) {
    213   AudioDecodingCallStats stats;
    214   const int kInitialDelay = 100;
    215 
    216   acm_->SetInitialPlayoutDelay(kInitialDelay);
    217 
    218   int num_calls = 0;
    219   for (int time_ms = 0; time_ms < kInitialDelay;
    220        time_ms += kFrameSizeMs, ++num_calls) {
    221     InsertPacketAndPullAudio();
    222   }
    223   acm_->GetDecodingCallStatistics(&stats);
    224   EXPECT_EQ(0, stats.calls_to_neteq);
    225   EXPECT_EQ(num_calls, stats.calls_to_silence_generator);
    226   EXPECT_EQ(0, stats.decoded_normal);
    227   EXPECT_EQ(0, stats.decoded_cng);
    228   EXPECT_EQ(0, stats.decoded_plc);
    229   EXPECT_EQ(0, stats.decoded_plc_cng);
    230 }
    231 
    232 // Insert some packets and pull audio. Check statistics are valid. Then,
    233 // simulate packet loss and check if PLC and PLC-to-CNG statistics are
    234 // correctly updated.
    235 TEST_F(AudioCodingModuleTestOldApi, DISABLED_ON_ANDROID(NetEqCalls)) {
    236   AudioDecodingCallStats stats;
    237   const int kNumNormalCalls = 10;
    238 
    239   for (int num_calls = 0; num_calls < kNumNormalCalls; ++num_calls) {
    240     InsertPacketAndPullAudio();
    241   }
    242   acm_->GetDecodingCallStatistics(&stats);
    243   EXPECT_EQ(kNumNormalCalls, stats.calls_to_neteq);
    244   EXPECT_EQ(0, stats.calls_to_silence_generator);
    245   EXPECT_EQ(kNumNormalCalls, stats.decoded_normal);
    246   EXPECT_EQ(0, stats.decoded_cng);
    247   EXPECT_EQ(0, stats.decoded_plc);
    248   EXPECT_EQ(0, stats.decoded_plc_cng);
    249 
    250   const int kNumPlc = 3;
    251   const int kNumPlcCng = 5;
    252 
    253   // Simulate packet-loss. NetEq first performs PLC then PLC fades to CNG.
    254   for (int n = 0; n < kNumPlc + kNumPlcCng; ++n) {
    255     PullAudio();
    256   }
    257   acm_->GetDecodingCallStatistics(&stats);
    258   EXPECT_EQ(kNumNormalCalls + kNumPlc + kNumPlcCng, stats.calls_to_neteq);
    259   EXPECT_EQ(0, stats.calls_to_silence_generator);
    260   EXPECT_EQ(kNumNormalCalls, stats.decoded_normal);
    261   EXPECT_EQ(0, stats.decoded_cng);
    262   EXPECT_EQ(kNumPlc, stats.decoded_plc);
    263   EXPECT_EQ(kNumPlcCng, stats.decoded_plc_cng);
    264 }
    265 
    266 TEST_F(AudioCodingModuleTestOldApi, VerifyOutputFrame) {
    267   AudioFrame audio_frame;
    268   const int kSampleRateHz = 32000;
    269   EXPECT_EQ(0, acm_->PlayoutData10Ms(kSampleRateHz, &audio_frame));
    270   EXPECT_EQ(id_, audio_frame.id_);
    271   EXPECT_EQ(0u, audio_frame.timestamp_);
    272   EXPECT_GT(audio_frame.num_channels_, 0);
    273   EXPECT_EQ(kSampleRateHz / 100, audio_frame.samples_per_channel_);
    274   EXPECT_EQ(kSampleRateHz, audio_frame.sample_rate_hz_);
    275 }
    276 
    277 TEST_F(AudioCodingModuleTestOldApi, FailOnZeroDesiredFrequency) {
    278   AudioFrame audio_frame;
    279   EXPECT_EQ(-1, acm_->PlayoutData10Ms(0, &audio_frame));
    280 }
    281 
    282 // A multi-threaded test for ACM. This base class is using the PCM16b 16 kHz
    283 // codec, while the derive class AcmIsacMtTest is using iSAC.
    284 class AudioCodingModuleMtTestOldApi : public AudioCodingModuleTestOldApi {
    285  protected:
    286   static const int kNumPackets = 500;
    287   static const int kNumPullCalls = 500;
    288 
    289   AudioCodingModuleMtTestOldApi()
    290       : AudioCodingModuleTestOldApi(),
    291         send_thread_(ThreadWrapper::CreateThread(CbSendThread,
    292                                                  this,
    293                                                  kRealtimePriority,
    294                                                  "send")),
    295         insert_packet_thread_(ThreadWrapper::CreateThread(CbInsertPacketThread,
    296                                                           this,
    297                                                           kRealtimePriority,
    298                                                           "insert_packet")),
    299         pull_audio_thread_(ThreadWrapper::CreateThread(CbPullAudioThread,
    300                                                        this,
    301                                                        kRealtimePriority,
    302                                                        "pull_audio")),
    303         test_complete_(EventWrapper::Create()),
    304         send_count_(0),
    305         insert_packet_count_(0),
    306         pull_audio_count_(0),
    307         crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
    308         next_insert_packet_time_ms_(0),
    309         fake_clock_(new SimulatedClock(0)) {
    310     clock_ = fake_clock_.get();
    311   }
    312 
    313   void SetUp() {
    314     AudioCodingModuleTestOldApi::SetUp();
    315     StartThreads();
    316   }
    317 
    318   void StartThreads() {
    319     unsigned int thread_id = 0;
    320     ASSERT_TRUE(send_thread_->Start(thread_id));
    321     ASSERT_TRUE(insert_packet_thread_->Start(thread_id));
    322     ASSERT_TRUE(pull_audio_thread_->Start(thread_id));
    323   }
    324 
    325   void TearDown() {
    326     AudioCodingModuleTestOldApi::TearDown();
    327     pull_audio_thread_->Stop();
    328     send_thread_->Stop();
    329     insert_packet_thread_->Stop();
    330   }
    331 
    332   EventTypeWrapper RunTest() {
    333     return test_complete_->Wait(10 * 60 * 1000);  // 10 minutes' timeout.
    334   }
    335 
    336   virtual bool TestDone() {
    337     if (packet_cb_.num_calls() > kNumPackets) {
    338       CriticalSectionScoped lock(crit_sect_.get());
    339       if (pull_audio_count_ > kNumPullCalls) {
    340         // Both conditions for completion are met. End the test.
    341         return true;
    342       }
    343     }
    344     return false;
    345   }
    346 
    347   static bool CbSendThread(void* context) {
    348     return reinterpret_cast<AudioCodingModuleMtTestOldApi*>(context)
    349         ->CbSendImpl();
    350   }
    351 
    352   // The send thread doesn't have to care about the current simulated time,
    353   // since only the AcmReceiver is using the clock.
    354   bool CbSendImpl() {
    355     SleepMs(1);
    356     if (HasFatalFailure()) {
    357       // End the test early if a fatal failure (ASSERT_*) has occurred.
    358       test_complete_->Set();
    359     }
    360     ++send_count_;
    361     InsertAudio();
    362     Encode();
    363     if (TestDone()) {
    364       test_complete_->Set();
    365     }
    366     return true;
    367   }
    368 
    369   static bool CbInsertPacketThread(void* context) {
    370     return reinterpret_cast<AudioCodingModuleMtTestOldApi*>(context)
    371         ->CbInsertPacketImpl();
    372   }
    373 
    374   bool CbInsertPacketImpl() {
    375     SleepMs(1);
    376     {
    377       CriticalSectionScoped lock(crit_sect_.get());
    378       if (clock_->TimeInMilliseconds() < next_insert_packet_time_ms_) {
    379         return true;
    380       }
    381       next_insert_packet_time_ms_ += 10;
    382     }
    383     // Now we're not holding the crit sect when calling ACM.
    384     ++insert_packet_count_;
    385     InsertPacket();
    386     return true;
    387   }
    388 
    389   static bool CbPullAudioThread(void* context) {
    390     return reinterpret_cast<AudioCodingModuleMtTestOldApi*>(context)
    391         ->CbPullAudioImpl();
    392   }
    393 
    394   bool CbPullAudioImpl() {
    395     SleepMs(1);
    396     {
    397       CriticalSectionScoped lock(crit_sect_.get());
    398       // Don't let the insert thread fall behind.
    399       if (next_insert_packet_time_ms_ < clock_->TimeInMilliseconds()) {
    400         return true;
    401       }
    402       ++pull_audio_count_;
    403     }
    404     // Now we're not holding the crit sect when calling ACM.
    405     PullAudio();
    406     fake_clock_->AdvanceTimeMilliseconds(10);
    407     return true;
    408   }
    409 
    410   scoped_ptr<ThreadWrapper> send_thread_;
    411   scoped_ptr<ThreadWrapper> insert_packet_thread_;
    412   scoped_ptr<ThreadWrapper> pull_audio_thread_;
    413   const scoped_ptr<EventWrapper> test_complete_;
    414   int send_count_;
    415   int insert_packet_count_;
    416   int pull_audio_count_ GUARDED_BY(crit_sect_);
    417   const scoped_ptr<CriticalSectionWrapper> crit_sect_;
    418   int64_t next_insert_packet_time_ms_ GUARDED_BY(crit_sect_);
    419   scoped_ptr<SimulatedClock> fake_clock_;
    420 };
    421 
    422 TEST_F(AudioCodingModuleMtTestOldApi, DoTest) {
    423   EXPECT_EQ(kEventSignaled, RunTest());
    424 }
    425 
    426 // This is a multi-threaded ACM test using iSAC. The test encodes audio
    427 // from a PCM file. The most recent encoded frame is used as input to the
    428 // receiving part. Depending on timing, it may happen that the same RTP packet
    429 // is inserted into the receiver multiple times, but this is a valid use-case,
    430 // and simplifies the test code a lot.
    431 class AcmIsacMtTestOldApi : public AudioCodingModuleMtTestOldApi {
    432  protected:
    433   static const int kNumPackets = 500;
    434   static const int kNumPullCalls = 500;
    435 
    436   AcmIsacMtTestOldApi()
    437       : AudioCodingModuleMtTestOldApi(), last_packet_number_(0) {}
    438 
    439   ~AcmIsacMtTestOldApi() {}
    440 
    441   void SetUp() {
    442     AudioCodingModuleTestOldApi::SetUp();
    443 
    444     // Set up input audio source to read from specified file, loop after 5
    445     // seconds, and deliver blocks of 10 ms.
    446     const std::string input_file_name =
    447         webrtc::test::ResourcePath("audio_coding/speech_mono_16kHz", "pcm");
    448     audio_loop_.Init(input_file_name, 5 * kSampleRateHz, kNumSamples10ms);
    449 
    450     // Generate one packet to have something to insert.
    451     int loop_counter = 0;
    452     while (packet_cb_.last_payload_len_bytes() == 0) {
    453       InsertAudio();
    454       Encode();
    455       ASSERT_LT(loop_counter++, 10);
    456     }
    457     // Set |last_packet_number_| to one less that |num_calls| so that the packet
    458     // will be fetched in the next InsertPacket() call.
    459     last_packet_number_ = packet_cb_.num_calls() - 1;
    460 
    461     StartThreads();
    462   }
    463 
    464   virtual void RegisterCodec() {
    465     COMPILE_ASSERT(kSampleRateHz == 16000, test_designed_for_isac_16khz);
    466     AudioCodingModule::Codec("ISAC", &codec_, kSampleRateHz, 1);
    467     codec_.pltype = kPayloadType;
    468 
    469     // Register iSAC codec in ACM, effectively unregistering the PCM16B codec
    470     // registered in AudioCodingModuleTestOldApi::SetUp();
    471     ASSERT_EQ(0, acm_->RegisterReceiveCodec(codec_));
    472     ASSERT_EQ(0, acm_->RegisterSendCodec(codec_));
    473   }
    474 
    475   void InsertPacket() {
    476     int num_calls = packet_cb_.num_calls();  // Store locally for thread safety.
    477     if (num_calls > last_packet_number_) {
    478       // Get the new payload out from the callback handler.
    479       // Note that since we swap buffers here instead of directly inserting
    480       // a pointer to the data in |packet_cb_|, we avoid locking the callback
    481       // for the duration of the IncomingPacket() call.
    482       packet_cb_.SwapBuffers(&last_payload_vec_);
    483       ASSERT_GT(last_payload_vec_.size(), 0u);
    484       rtp_utility_->Forward(&rtp_header_);
    485       last_packet_number_ = num_calls;
    486     }
    487     ASSERT_GT(last_payload_vec_.size(), 0u);
    488     ASSERT_EQ(
    489         0,
    490         acm_->IncomingPacket(
    491             &last_payload_vec_[0], last_payload_vec_.size(), rtp_header_));
    492   }
    493 
    494   void InsertAudio() {
    495     memcpy(input_frame_.data_, audio_loop_.GetNextBlock(), kNumSamples10ms);
    496     AudioCodingModuleTestOldApi::InsertAudio();
    497   }
    498 
    499   void Encode() { ASSERT_GE(acm_->Process(), 0); }
    500 
    501   // This method is the same as AudioCodingModuleMtTestOldApi::TestDone(), but
    502   // here it is using the constants defined in this class (i.e., shorter test
    503   // run).
    504   virtual bool TestDone() {
    505     if (packet_cb_.num_calls() > kNumPackets) {
    506       CriticalSectionScoped lock(crit_sect_.get());
    507       if (pull_audio_count_ > kNumPullCalls) {
    508         // Both conditions for completion are met. End the test.
    509         return true;
    510       }
    511     }
    512     return false;
    513   }
    514 
    515   int last_packet_number_;
    516   std::vector<uint8_t> last_payload_vec_;
    517   test::AudioLoop audio_loop_;
    518 };
    519 
    520 TEST_F(AcmIsacMtTestOldApi, DoTest) {
    521   EXPECT_EQ(kEventSignaled, RunTest());
    522 }
    523 
    524 class AcmReceiverBitExactnessOldApi : public ::testing::Test {
    525  public:
    526   static std::string PlatformChecksum(std::string win64,
    527                                       std::string android,
    528                                       std::string others) {
    529 #if defined(_WIN32) && defined(WEBRTC_ARCH_64_BITS)
    530     return win64;
    531 #elif defined(WEBRTC_ANDROID)
    532     return android;
    533 #else
    534     return others;
    535 #endif
    536   }
    537 
    538  protected:
    539   void Run(int output_freq_hz, const std::string& checksum_ref) {
    540     const std::string input_file_name =
    541         webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
    542     scoped_ptr<test::RtpFileSource> packet_source(
    543         test::RtpFileSource::Create(input_file_name));
    544 #ifdef WEBRTC_ANDROID
    545     // Filter out iLBC and iSAC-swb since they are not supported on Android.
    546     packet_source->FilterOutPayloadType(102);  // iLBC.
    547     packet_source->FilterOutPayloadType(104);  // iSAC-swb.
    548 #endif
    549 
    550     test::AudioChecksum checksum;
    551     const std::string output_file_name =
    552         webrtc::test::OutputPath() +
    553         ::testing::UnitTest::GetInstance()
    554             ->current_test_info()
    555             ->test_case_name() +
    556         "_" + ::testing::UnitTest::GetInstance()->current_test_info()->name() +
    557         "_output.pcm";
    558     test::OutputAudioFile output_file(output_file_name);
    559     test::AudioSinkFork output(&checksum, &output_file);
    560 
    561     test::AcmReceiveTestOldApi test(
    562         packet_source.get(),
    563         &output,
    564         output_freq_hz,
    565         test::AcmReceiveTestOldApi::kArbitraryChannels);
    566     ASSERT_NO_FATAL_FAILURE(test.RegisterNetEqTestCodecs());
    567     test.Run();
    568 
    569     std::string checksum_string = checksum.Finish();
    570     EXPECT_EQ(checksum_ref, checksum_string);
    571   }
    572 };
    573 
    574 TEST_F(AcmReceiverBitExactnessOldApi, 8kHzOutput) {
    575   Run(8000,
    576       PlatformChecksum("bd6f8d9602cd82444ea2539e674df747",
    577                        "6ac89c7145072c26bfeba602cd661afb",
    578                        "8a8440f5511eb729221b9aac25cda3a0"));
    579 }
    580 
    581 TEST_F(AcmReceiverBitExactnessOldApi, 16kHzOutput) {
    582   Run(16000,
    583       PlatformChecksum("a39bc6ee0c4eb15f3ad2f43cebcc571d",
    584                        "3e888eb04f57db2c6ef952fe64f17fe6",
    585                        "7be583092c5adbcb0f6cd66eca20ea63"));
    586 }
    587 
    588 TEST_F(AcmReceiverBitExactnessOldApi, 32kHzOutput) {
    589   Run(32000,
    590       PlatformChecksum("80964572aaa2dc92f9e34896dd3802b3",
    591                        "aeca37e963310f5b6552b7edea23c2f1",
    592                        "3a84188abe9fca25fedd6034760f3e22"));
    593 }
    594 
    595 TEST_F(AcmReceiverBitExactnessOldApi, 48kHzOutput) {
    596   Run(48000,
    597       PlatformChecksum("8aacde91f390e0d5a9c2ed571a25fd37",
    598                        "76b9e99e0a3998aa28355e7a2bd836f7",
    599                        "89b4b19bdb4de40f1d88302ef8cb9f9b"));
    600 }
    601 
    602 // This test verifies bit exactness for the send-side of ACM. The test setup is
    603 // a chain of three different test classes:
    604 //
    605 // test::AcmSendTest -> AcmSenderBitExactness -> test::AcmReceiveTest
    606 //
    607 // The receiver side is driving the test by requesting new packets from
    608 // AcmSenderBitExactness::NextPacket(). This method, in turn, asks for the
    609 // packet from test::AcmSendTest::NextPacket, which inserts audio from the
    610 // input file until one packet is produced. (The input file loops indefinitely.)
    611 // Before passing the packet to the receiver, this test class verifies the
    612 // packet header and updates a payload checksum with the new payload. The
    613 // decoded output from the receiver is also verified with a (separate) checksum.
    614 class AcmSenderBitExactnessOldApi : public ::testing::Test,
    615                                     public test::PacketSource {
    616  protected:
    617   static const int kTestDurationMs = 1000;
    618 
    619   AcmSenderBitExactnessOldApi()
    620       : frame_size_rtp_timestamps_(0),
    621         packet_count_(0),
    622         payload_type_(0),
    623         last_sequence_number_(0),
    624         last_timestamp_(0) {}
    625 
    626   // Sets up the test::AcmSendTest object. Returns true on success, otherwise
    627   // false.
    628   bool SetUpSender() {
    629     const std::string input_file_name =
    630         webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
    631     // Note that |audio_source_| will loop forever. The test duration is set
    632     // explicitly by |kTestDurationMs|.
    633     audio_source_.reset(new test::InputAudioFile(input_file_name));
    634     static const int kSourceRateHz = 32000;
    635     send_test_.reset(new test::AcmSendTestOldApi(
    636         audio_source_.get(), kSourceRateHz, kTestDurationMs));
    637     return send_test_.get() != NULL;
    638   }
    639 
    640   // Registers a send codec in the test::AcmSendTest object. Returns true on
    641   // success, false on failure.
    642   bool RegisterSendCodec(const char* payload_name,
    643                          int sampling_freq_hz,
    644                          int channels,
    645                          int payload_type,
    646                          int frame_size_samples,
    647                          int frame_size_rtp_timestamps) {
    648     payload_type_ = payload_type;
    649     frame_size_rtp_timestamps_ = frame_size_rtp_timestamps;
    650     return send_test_->RegisterCodec(payload_name,
    651                                      sampling_freq_hz,
    652                                      channels,
    653                                      payload_type,
    654                                      frame_size_samples);
    655   }
    656 
    657   // Runs the test. SetUpSender() and RegisterSendCodec() must have been called
    658   // before calling this method.
    659   void Run(const std::string& audio_checksum_ref,
    660            const std::string& payload_checksum_ref,
    661            int expected_packets,
    662            test::AcmReceiveTestOldApi::NumOutputChannels expected_channels) {
    663     // Set up the receiver used to decode the packets and verify the decoded
    664     // output.
    665     test::AudioChecksum audio_checksum;
    666     const std::string output_file_name =
    667         webrtc::test::OutputPath() +
    668         ::testing::UnitTest::GetInstance()
    669             ->current_test_info()
    670             ->test_case_name() +
    671         "_" + ::testing::UnitTest::GetInstance()->current_test_info()->name() +
    672         "_output.pcm";
    673     test::OutputAudioFile output_file(output_file_name);
    674     // Have the output audio sent both to file and to the checksum calculator.
    675     test::AudioSinkFork output(&audio_checksum, &output_file);
    676     const int kOutputFreqHz = 8000;
    677     test::AcmReceiveTestOldApi receive_test(
    678         this, &output, kOutputFreqHz, expected_channels);
    679     ASSERT_NO_FATAL_FAILURE(receive_test.RegisterDefaultCodecs());
    680 
    681     // This is where the actual test is executed.
    682     receive_test.Run();
    683 
    684     // Extract and verify the audio checksum.
    685     std::string checksum_string = audio_checksum.Finish();
    686     EXPECT_EQ(audio_checksum_ref, checksum_string);
    687 
    688     // Extract and verify the payload checksum.
    689     char checksum_result[rtc::Md5Digest::kSize];
    690     payload_checksum_.Finish(checksum_result, rtc::Md5Digest::kSize);
    691     checksum_string = rtc::hex_encode(checksum_result, rtc::Md5Digest::kSize);
    692     EXPECT_EQ(payload_checksum_ref, checksum_string);
    693 
    694     // Verify number of packets produced.
    695     EXPECT_EQ(expected_packets, packet_count_);
    696   }
    697 
    698   // Returns a pointer to the next packet. Returns NULL if the source is
    699   // depleted (i.e., the test duration is exceeded), or if an error occurred.
    700   // Inherited from test::PacketSource.
    701   test::Packet* NextPacket() OVERRIDE {
    702     // Get the next packet from AcmSendTest. Ownership of |packet| is
    703     // transferred to this method.
    704     test::Packet* packet = send_test_->NextPacket();
    705     if (!packet)
    706       return NULL;
    707 
    708     VerifyPacket(packet);
    709     // TODO(henrik.lundin) Save the packet to file as well.
    710 
    711     // Pass it on to the caller. The caller becomes the owner of |packet|.
    712     return packet;
    713   }
    714 
    715   // Verifies the packet.
    716   void VerifyPacket(const test::Packet* packet) {
    717     EXPECT_TRUE(packet->valid_header());
    718     // (We can check the header fields even if valid_header() is false.)
    719     EXPECT_EQ(payload_type_, packet->header().payloadType);
    720     if (packet_count_ > 0) {
    721       // This is not the first packet.
    722       uint16_t sequence_number_diff =
    723           packet->header().sequenceNumber - last_sequence_number_;
    724       EXPECT_EQ(1, sequence_number_diff);
    725       uint32_t timestamp_diff = packet->header().timestamp - last_timestamp_;
    726       EXPECT_EQ(frame_size_rtp_timestamps_, timestamp_diff);
    727     }
    728     ++packet_count_;
    729     last_sequence_number_ = packet->header().sequenceNumber;
    730     last_timestamp_ = packet->header().timestamp;
    731     // Update the checksum.
    732     payload_checksum_.Update(packet->payload(), packet->payload_length_bytes());
    733   }
    734 
    735   void SetUpTest(const char* codec_name,
    736                  int codec_sample_rate_hz,
    737                  int channels,
    738                  int payload_type,
    739                  int codec_frame_size_samples,
    740                  int codec_frame_size_rtp_timestamps) {
    741     ASSERT_TRUE(SetUpSender());
    742     ASSERT_TRUE(RegisterSendCodec(codec_name,
    743                                   codec_sample_rate_hz,
    744                                   channels,
    745                                   payload_type,
    746                                   codec_frame_size_samples,
    747                                   codec_frame_size_rtp_timestamps));
    748   }
    749 
    750   scoped_ptr<test::AcmSendTestOldApi> send_test_;
    751   scoped_ptr<test::InputAudioFile> audio_source_;
    752   uint32_t frame_size_rtp_timestamps_;
    753   int packet_count_;
    754   uint8_t payload_type_;
    755   uint16_t last_sequence_number_;
    756   uint32_t last_timestamp_;
    757   rtc::Md5Digest payload_checksum_;
    758 };
    759 
    760 TEST_F(AcmSenderBitExactnessOldApi, IsacWb30ms) {
    761   ASSERT_NO_FATAL_FAILURE(SetUpTest("ISAC", 16000, 1, 103, 480, 480));
    762   Run(AcmReceiverBitExactnessOldApi::PlatformChecksum(
    763           "c7e5bdadfa2871df95639fcc297cf23d",
    764           "0499ca260390769b3172136faad925b9",
    765           "0b58f9eeee43d5891f5f6c75e77984a3"),
    766       AcmReceiverBitExactnessOldApi::PlatformChecksum(
    767           "d42cb5195463da26c8129bbfe73a22e6",
    768           "83de248aea9c3c2bd680b6952401b4ca",
    769           "3c79f16f34218271f3dca4e2b1dfe1bb"),
    770       33,
    771       test::AcmReceiveTestOldApi::kMonoOutput);
    772 }
    773 
    774 TEST_F(AcmSenderBitExactnessOldApi, IsacWb60ms) {
    775   ASSERT_NO_FATAL_FAILURE(SetUpTest("ISAC", 16000, 1, 103, 960, 960));
    776   Run(AcmReceiverBitExactnessOldApi::PlatformChecksum(
    777           "14d63c5f08127d280e722e3191b73bdd",
    778           "8da003e16c5371af2dc2be79a50f9076",
    779           "1ad29139a04782a33daad8c2b9b35875"),
    780       AcmReceiverBitExactnessOldApi::PlatformChecksum(
    781           "ebe04a819d3a9d83a83a17f271e1139a",
    782           "97aeef98553b5a4b5a68f8b716e8eaf0",
    783           "9e0a0ab743ad987b55b8e14802769c56"),
    784       16,
    785       test::AcmReceiveTestOldApi::kMonoOutput);
    786 }
    787 
    788 TEST_F(AcmSenderBitExactnessOldApi, DISABLED_ON_ANDROID(IsacSwb30ms)) {
    789   ASSERT_NO_FATAL_FAILURE(SetUpTest("ISAC", 32000, 1, 104, 960, 960));
    790   Run(AcmReceiverBitExactnessOldApi::PlatformChecksum(
    791           "98d960600eb4ddb3fcbe11f5057ddfd7",
    792           "",
    793           "2f6dfe142f735f1d96f6bd86d2526f42"),
    794       AcmReceiverBitExactnessOldApi::PlatformChecksum(
    795           "cc9d2d86a71d6f99f97680a5c27e2762",
    796           "",
    797           "7b214fc3a5e33d68bf30e77969371f31"),
    798       33,
    799       test::AcmReceiveTestOldApi::kMonoOutput);
    800 }
    801 
    802 TEST_F(AcmSenderBitExactnessOldApi, Pcm16_8000khz_10ms) {
    803   ASSERT_NO_FATAL_FAILURE(SetUpTest("L16", 8000, 1, 107, 80, 80));
    804   Run("de4a98e1406f8b798d99cd0704e862e2",
    805       "c1edd36339ce0326cc4550041ad719a0",
    806       100,
    807       test::AcmReceiveTestOldApi::kMonoOutput);
    808 }
    809 
    810 TEST_F(AcmSenderBitExactnessOldApi, Pcm16_16000khz_10ms) {
    811   ASSERT_NO_FATAL_FAILURE(SetUpTest("L16", 16000, 1, 108, 160, 160));
    812   Run("ae646d7b68384a1269cc080dd4501916",
    813       "ad786526383178b08d80d6eee06e9bad",
    814       100,
    815       test::AcmReceiveTestOldApi::kMonoOutput);
    816 }
    817 
    818 TEST_F(AcmSenderBitExactnessOldApi, Pcm16_32000khz_10ms) {
    819   ASSERT_NO_FATAL_FAILURE(SetUpTest("L16", 32000, 1, 109, 320, 320));
    820   Run("7fe325e8fbaf755e3c5df0b11a4774fb",
    821       "5ef82ea885e922263606c6fdbc49f651",
    822       100,
    823       test::AcmReceiveTestOldApi::kMonoOutput);
    824 }
    825 
    826 TEST_F(AcmSenderBitExactnessOldApi, Pcm16_stereo_8000khz_10ms) {
    827   ASSERT_NO_FATAL_FAILURE(SetUpTest("L16", 8000, 2, 111, 80, 80));
    828   Run("fb263b74e7ac3de915474d77e4744ceb",
    829       "62ce5adb0d4965d0a52ec98ae7f98974",
    830       100,
    831       test::AcmReceiveTestOldApi::kStereoOutput);
    832 }
    833 
    834 TEST_F(AcmSenderBitExactnessOldApi, Pcm16_stereo_16000khz_10ms) {
    835   ASSERT_NO_FATAL_FAILURE(SetUpTest("L16", 16000, 2, 112, 160, 160));
    836   Run("d09e9239553649d7ac93e19d304281fd",
    837       "41ca8edac4b8c71cd54fd9f25ec14870",
    838       100,
    839       test::AcmReceiveTestOldApi::kStereoOutput);
    840 }
    841 
    842 TEST_F(AcmSenderBitExactnessOldApi, Pcm16_stereo_32000khz_10ms) {
    843   ASSERT_NO_FATAL_FAILURE(SetUpTest("L16", 32000, 2, 113, 320, 320));
    844   Run("5f025d4f390982cc26b3d92fe02e3044",
    845       "50e58502fb04421bf5b857dda4c96879",
    846       100,
    847       test::AcmReceiveTestOldApi::kStereoOutput);
    848 }
    849 
    850 TEST_F(AcmSenderBitExactnessOldApi, Pcmu_20ms) {
    851   ASSERT_NO_FATAL_FAILURE(SetUpTest("PCMU", 8000, 1, 0, 160, 160));
    852   Run("81a9d4c0bb72e9becc43aef124c981e9",
    853       "8f9b8750bd80fe26b6cbf6659b89f0f9",
    854       50,
    855       test::AcmReceiveTestOldApi::kMonoOutput);
    856 }
    857 
    858 TEST_F(AcmSenderBitExactnessOldApi, Pcma_20ms) {
    859   ASSERT_NO_FATAL_FAILURE(SetUpTest("PCMA", 8000, 1, 8, 160, 160));
    860   Run("39611f798969053925a49dc06d08de29",
    861       "6ad745e55aa48981bfc790d0eeef2dd1",
    862       50,
    863       test::AcmReceiveTestOldApi::kMonoOutput);
    864 }
    865 
    866 TEST_F(AcmSenderBitExactnessOldApi, Pcmu_stereo_20ms) {
    867   ASSERT_NO_FATAL_FAILURE(SetUpTest("PCMU", 8000, 2, 110, 160, 160));
    868   Run("437bec032fdc5cbaa0d5175430af7b18",
    869       "60b6f25e8d1e74cb679cfe756dd9bca5",
    870       50,
    871       test::AcmReceiveTestOldApi::kStereoOutput);
    872 }
    873 
    874 TEST_F(AcmSenderBitExactnessOldApi, Pcma_stereo_20ms) {
    875   ASSERT_NO_FATAL_FAILURE(SetUpTest("PCMA", 8000, 2, 118, 160, 160));
    876   Run("a5c6d83c5b7cedbeff734238220a4b0c",
    877       "92b282c83efd20e7eeef52ba40842cf7",
    878       50,
    879       test::AcmReceiveTestOldApi::kStereoOutput);
    880 }
    881 
    882 TEST_F(AcmSenderBitExactnessOldApi, DISABLED_ON_ANDROID(Ilbc_30ms)) {
    883   ASSERT_NO_FATAL_FAILURE(SetUpTest("ILBC", 8000, 1, 102, 240, 240));
    884   Run(AcmReceiverBitExactnessOldApi::PlatformChecksum(
    885           "7b6ec10910debd9af08011d3ed5249f7",
    886           "android_audio",
    887           "7b6ec10910debd9af08011d3ed5249f7"),
    888       AcmReceiverBitExactnessOldApi::PlatformChecksum(
    889           "cfae2e9f6aba96e145f2bcdd5050ce78",
    890           "android_payload",
    891           "cfae2e9f6aba96e145f2bcdd5050ce78"),
    892       33,
    893       test::AcmReceiveTestOldApi::kMonoOutput);
    894 }
    895 
    896 TEST_F(AcmSenderBitExactnessOldApi, DISABLED_ON_ANDROID(G722_20ms)) {
    897   ASSERT_NO_FATAL_FAILURE(SetUpTest("G722", 16000, 1, 9, 320, 160));
    898   Run(AcmReceiverBitExactnessOldApi::PlatformChecksum(
    899           "7d759436f2533582950d148b5161a36c",
    900           "android_audio",
    901           "7d759436f2533582950d148b5161a36c"),
    902       AcmReceiverBitExactnessOldApi::PlatformChecksum(
    903           "fc68a87e1380614e658087cb35d5ca10",
    904           "android_payload",
    905           "fc68a87e1380614e658087cb35d5ca10"),
    906       50,
    907       test::AcmReceiveTestOldApi::kMonoOutput);
    908 }
    909 
    910 TEST_F(AcmSenderBitExactnessOldApi, DISABLED_ON_ANDROID(G722_stereo_20ms)) {
    911   ASSERT_NO_FATAL_FAILURE(SetUpTest("G722", 16000, 2, 119, 320, 160));
    912   Run(AcmReceiverBitExactnessOldApi::PlatformChecksum(
    913           "7190ee718ab3d80eca181e5f7140c210",
    914           "android_audio",
    915           "7190ee718ab3d80eca181e5f7140c210"),
    916       AcmReceiverBitExactnessOldApi::PlatformChecksum(
    917           "66516152eeaa1e650ad94ff85f668dac",
    918           "android_payload",
    919           "66516152eeaa1e650ad94ff85f668dac"),
    920       50,
    921       test::AcmReceiveTestOldApi::kStereoOutput);
    922 }
    923 
    924 TEST_F(AcmSenderBitExactnessOldApi, Opus_stereo_20ms) {
    925   ASSERT_NO_FATAL_FAILURE(SetUpTest("opus", 48000, 2, 120, 960, 960));
    926   Run(AcmReceiverBitExactnessOldApi::PlatformChecksum(
    927           "855041f2490b887302bce9d544731849",
    928           "1e1a0fce893fef2d66886a7f09e2ebce",
    929           "855041f2490b887302bce9d544731849"),
    930       AcmReceiverBitExactnessOldApi::PlatformChecksum(
    931           "d781cce1ab986b618d0da87226cdde30",
    932           "1a1fe04dd12e755949987c8d729fb3e0",
    933           "d781cce1ab986b618d0da87226cdde30"),
    934       50,
    935       test::AcmReceiveTestOldApi::kStereoOutput);
    936 }
    937 
    938 }  // namespace webrtc
    939