Home | History | Annotate | Download | only in audio_receiver
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "base/test/simple_test_tick_clock.h"
      6 #include "media/cast/audio_receiver/audio_decoder.h"
      7 #include "media/cast/cast_environment.h"
      8 #include "media/cast/test/fake_task_runner.h"
      9 #include "testing/gmock/include/gmock/gmock.h"
     10 
     11 namespace media {
     12 namespace cast {
     13 
     14 namespace {
     15 class TestRtpPayloadFeedback : public RtpPayloadFeedback {
     16  public:
     17   TestRtpPayloadFeedback() {}
     18   virtual ~TestRtpPayloadFeedback() {}
     19 
     20   virtual void CastFeedback(const RtcpCastMessage& cast_feedback) OVERRIDE {
     21     EXPECT_EQ(1u, cast_feedback.ack_frame_id_);
     22     EXPECT_EQ(0u, cast_feedback.missing_frames_and_packets_.size());
     23   }
     24 };
     25 }  // namespace.
     26 
     27 class AudioDecoderTest : public ::testing::Test {
     28  protected:
     29   AudioDecoderTest() {
     30     testing_clock_.Advance(base::TimeDelta::FromMilliseconds(1234));
     31     task_runner_ = new test::FakeTaskRunner(&testing_clock_);
     32     cast_environment_ = new CastEnvironment(&testing_clock_, task_runner_,
     33         task_runner_, task_runner_, task_runner_, task_runner_,
     34         GetDefaultCastLoggingConfig());
     35   }
     36   virtual ~AudioDecoderTest() {}
     37 
     38   void Configure(const AudioReceiverConfig& audio_config) {
     39     audio_decoder_.reset(
     40         new AudioDecoder(cast_environment_, audio_config, &cast_feedback_));
     41   }
     42 
     43   TestRtpPayloadFeedback cast_feedback_;
     44   base::SimpleTestTickClock testing_clock_;
     45   scoped_refptr<test::FakeTaskRunner> task_runner_;
     46   scoped_refptr<CastEnvironment> cast_environment_;
     47   scoped_ptr<AudioDecoder> audio_decoder_;
     48 };
     49 
     50 TEST_F(AudioDecoderTest, Pcm16MonoNoResampleOnePacket) {
     51   AudioReceiverConfig audio_config;
     52   audio_config.rtp_payload_type = 127;
     53   audio_config.frequency = 16000;
     54   audio_config.channels = 1;
     55   audio_config.codec = kPcm16;
     56   audio_config.use_external_decoder = false;
     57   Configure(audio_config);
     58 
     59   RtpCastHeader rtp_header;
     60   rtp_header.webrtc.header.payloadType = 127;
     61   rtp_header.webrtc.header.sequenceNumber = 1234;
     62   rtp_header.webrtc.header.timestamp = 0x87654321;
     63   rtp_header.webrtc.header.ssrc = 0x12345678;
     64   rtp_header.webrtc.header.paddingLength = 0;
     65   rtp_header.webrtc.header.headerLength = 12;
     66   rtp_header.webrtc.type.Audio.channel = 1;
     67   rtp_header.webrtc.type.Audio.isCNG = false;
     68 
     69   std::vector<int16> payload(640, 0x1234);
     70   int number_of_10ms_blocks = 4;
     71   int desired_frequency = 16000;
     72   PcmAudioFrame audio_frame;
     73   uint32 rtp_timestamp;
     74 
     75   EXPECT_FALSE(audio_decoder_->GetRawAudioFrame(number_of_10ms_blocks,
     76                                                 desired_frequency,
     77                                                 &audio_frame,
     78                                                 &rtp_timestamp));
     79 
     80   uint8* payload_data = reinterpret_cast<uint8*>(&payload[0]);
     81   size_t payload_size = payload.size() * sizeof(int16);
     82 
     83   audio_decoder_->IncomingParsedRtpPacket(payload_data,
     84       payload_size, rtp_header);
     85 
     86   EXPECT_TRUE(audio_decoder_->GetRawAudioFrame(number_of_10ms_blocks,
     87                                                desired_frequency,
     88                                                &audio_frame,
     89                                                &rtp_timestamp));
     90   EXPECT_EQ(1, audio_frame.channels);
     91   EXPECT_EQ(16000, audio_frame.frequency);
     92   EXPECT_EQ(640ul, audio_frame.samples.size());
     93   // First 10 samples per channel are 0 from NetEq.
     94   for (size_t i = 10; i < audio_frame.samples.size(); ++i) {
     95     EXPECT_EQ(0x3412, audio_frame.samples[i]);
     96   }
     97 }
     98 
     99 TEST_F(AudioDecoderTest, Pcm16StereoNoResampleTwoPackets) {
    100   AudioReceiverConfig audio_config;
    101   audio_config.rtp_payload_type = 127;
    102   audio_config.frequency = 16000;
    103   audio_config.channels = 2;
    104   audio_config.codec = kPcm16;
    105   audio_config.use_external_decoder = false;
    106   Configure(audio_config);
    107 
    108   RtpCastHeader rtp_header;
    109   rtp_header.frame_id = 0;
    110   rtp_header.webrtc.header.payloadType = 127;
    111   rtp_header.webrtc.header.sequenceNumber = 1234;
    112   rtp_header.webrtc.header.timestamp = 0x87654321;
    113   rtp_header.webrtc.header.ssrc = 0x12345678;
    114   rtp_header.webrtc.header.paddingLength = 0;
    115   rtp_header.webrtc.header.headerLength = 12;
    116 
    117   rtp_header.webrtc.type.Audio.isCNG = false;
    118   rtp_header.webrtc.type.Audio.channel = 2;
    119 
    120   std::vector<int16> payload(640, 0x1234);
    121 
    122   uint8* payload_data = reinterpret_cast<uint8*>(&payload[0]);
    123   size_t payload_size = payload.size() * sizeof(int16);
    124 
    125   audio_decoder_->IncomingParsedRtpPacket(payload_data,
    126       payload_size, rtp_header);
    127 
    128   int number_of_10ms_blocks = 2;
    129   int desired_frequency = 16000;
    130   PcmAudioFrame audio_frame;
    131   uint32 rtp_timestamp;
    132 
    133   EXPECT_TRUE(audio_decoder_->GetRawAudioFrame(number_of_10ms_blocks,
    134                                                desired_frequency,
    135                                                &audio_frame,
    136                                                &rtp_timestamp));
    137   EXPECT_EQ(2, audio_frame.channels);
    138   EXPECT_EQ(16000, audio_frame.frequency);
    139   EXPECT_EQ(640ul, audio_frame.samples.size());
    140   // First 10 samples per channel are 0 from NetEq.
    141   for (size_t i = 10 * audio_config.channels; i < audio_frame.samples.size();
    142       ++i) {
    143     EXPECT_EQ(0x3412, audio_frame.samples[i]);
    144   }
    145 
    146   rtp_header.frame_id++;
    147   rtp_header.webrtc.header.sequenceNumber++;
    148   rtp_header.webrtc.header.timestamp += (audio_config.frequency / 100) * 2 * 2;
    149 
    150   audio_decoder_->IncomingParsedRtpPacket(payload_data,
    151       payload_size, rtp_header);
    152 
    153   EXPECT_TRUE(audio_decoder_->GetRawAudioFrame(number_of_10ms_blocks,
    154                                                desired_frequency,
    155                                                &audio_frame,
    156                                                &rtp_timestamp));
    157   EXPECT_EQ(2, audio_frame.channels);
    158   EXPECT_EQ(16000, audio_frame.frequency);
    159   EXPECT_EQ(640ul, audio_frame.samples.size());
    160   for (size_t i = 0; i < audio_frame.samples.size(); ++i) {
    161     EXPECT_NEAR(0x3412, audio_frame.samples[i], 1000);
    162   }
    163   // Test cast callback.
    164   audio_decoder_->SendCastMessage();
    165   testing_clock_.Advance(base::TimeDelta::FromMilliseconds(33));
    166   audio_decoder_->SendCastMessage();
    167 }
    168 
    169 TEST_F(AudioDecoderTest, Pcm16Resample) {
    170   AudioReceiverConfig audio_config;
    171   audio_config.rtp_payload_type = 127;
    172   audio_config.frequency = 16000;
    173   audio_config.channels = 2;
    174   audio_config.codec = kPcm16;
    175   audio_config.use_external_decoder = false;
    176   Configure(audio_config);
    177 
    178   RtpCastHeader rtp_header;
    179   rtp_header.webrtc.header.payloadType = 127;
    180   rtp_header.webrtc.header.sequenceNumber = 1234;
    181   rtp_header.webrtc.header.timestamp = 0x87654321;
    182   rtp_header.webrtc.header.ssrc = 0x12345678;
    183   rtp_header.webrtc.header.paddingLength = 0;
    184   rtp_header.webrtc.header.headerLength = 12;
    185 
    186   rtp_header.webrtc.type.Audio.isCNG = false;
    187   rtp_header.webrtc.type.Audio.channel = 2;
    188 
    189   std::vector<int16> payload(640, 0x1234);
    190 
    191   uint8* payload_data = reinterpret_cast<uint8*>(&payload[0]);
    192   size_t payload_size = payload.size() * sizeof(int16);
    193 
    194   audio_decoder_->IncomingParsedRtpPacket(payload_data,
    195       payload_size, rtp_header);
    196 
    197   int number_of_10ms_blocks = 2;
    198   int desired_frequency = 48000;
    199   PcmAudioFrame audio_frame;
    200   uint32 rtp_timestamp;
    201 
    202   EXPECT_TRUE(audio_decoder_->GetRawAudioFrame(number_of_10ms_blocks,
    203                                                desired_frequency,
    204                                                &audio_frame,
    205                                                &rtp_timestamp));
    206 
    207   EXPECT_EQ(2, audio_frame.channels);
    208   EXPECT_EQ(48000, audio_frame.frequency);
    209   EXPECT_EQ(1920ul, audio_frame.samples.size());  // Upsampled to 48 KHz.
    210   int count = 0;
    211   // Resampling makes the variance worse.
    212   for (size_t i = 100 * audio_config.channels; i < audio_frame.samples.size();
    213       ++i) {
    214     EXPECT_NEAR(0x3412, audio_frame.samples[i], 400);
    215     if (0x3412 == audio_frame.samples[i])  count++;
    216   }
    217 }
    218 
    219 }  // namespace cast
    220 }  // namespace media
    221