Home | History | Annotate | Download | only in audio_sender
      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/bind.h"
      6 #include "base/bind_helpers.h"
      7 #include "base/memory/scoped_ptr.h"
      8 #include "base/test/simple_test_tick_clock.h"
      9 #include "media/base/media.h"
     10 #include "media/cast/audio_sender/audio_sender.h"
     11 #include "media/cast/cast_config.h"
     12 #include "media/cast/cast_environment.h"
     13 #include "media/cast/net/pacing/mock_paced_packet_sender.h"
     14 #include "media/cast/test/audio_utility.h"
     15 #include "media/cast/test/fake_task_runner.h"
     16 #include "testing/gtest/include/gtest/gtest.h"
     17 
     18 namespace media {
     19 namespace cast {
     20 
     21 static const int64 kStartMillisecond = GG_INT64_C(12345678900000);
     22 
     23 using testing::_;
     24 using testing::AtLeast;
     25 
     26 class AudioSenderTest : public ::testing::Test {
     27  protected:
     28   AudioSenderTest() {
     29     InitializeMediaLibraryForTesting();
     30     testing_clock_.Advance(
     31         base::TimeDelta::FromMilliseconds(kStartMillisecond));
     32   }
     33 
     34   virtual void SetUp() {
     35     task_runner_ = new test::FakeTaskRunner(&testing_clock_);
     36     cast_environment_ = new CastEnvironment(&testing_clock_, task_runner_,
     37         task_runner_, task_runner_, task_runner_, task_runner_,
     38         GetDefaultCastLoggingConfig());
     39     audio_config_.codec = kOpus;
     40     audio_config_.use_external_encoder = false;
     41     audio_config_.frequency = kDefaultAudioSamplingRate;
     42     audio_config_.channels = 2;
     43     audio_config_.bitrate = kDefaultAudioEncoderBitrate;
     44     audio_config_.rtp_payload_type = 127;
     45 
     46     audio_sender_.reset(
     47         new AudioSender(cast_environment_, audio_config_, &mock_transport_));
     48   }
     49 
     50   virtual ~AudioSenderTest() {}
     51 
     52   base::SimpleTestTickClock testing_clock_;
     53   MockPacedPacketSender mock_transport_;
     54   scoped_refptr<test::FakeTaskRunner> task_runner_;
     55   scoped_ptr<AudioSender> audio_sender_;
     56   scoped_refptr<CastEnvironment> cast_environment_;
     57   AudioSenderConfig audio_config_;
     58 };
     59 
     60 TEST_F(AudioSenderTest, Encode20ms) {
     61   EXPECT_CALL(mock_transport_, SendPackets(_)).Times(AtLeast(1));
     62 
     63   const base::TimeDelta kDuration = base::TimeDelta::FromMilliseconds(20);
     64   scoped_ptr<AudioBus> bus(TestAudioBusFactory(
     65       audio_config_.channels, audio_config_.frequency,
     66       TestAudioBusFactory::kMiddleANoteFreq, 0.5f).NextAudioBus(kDuration));
     67 
     68   base::TimeTicks recorded_time = base::TimeTicks::Now();
     69   audio_sender_->InsertAudio(
     70       bus.get(), recorded_time,
     71       base::Bind(base::IgnoreResult(&scoped_ptr<AudioBus>::release),
     72                  base::Unretained(&bus)));
     73   task_runner_->RunTasks();
     74 
     75   EXPECT_TRUE(!bus) << "AudioBus wasn't released after use.";
     76 }
     77 
     78 TEST_F(AudioSenderTest, RtcpTimer) {
     79   EXPECT_CALL(mock_transport_, SendPackets(_)).Times(AtLeast(1));
     80   EXPECT_CALL(mock_transport_, SendRtcpPacket(_)).Times(1);
     81 
     82   const base::TimeDelta kDuration = base::TimeDelta::FromMilliseconds(20);
     83   scoped_ptr<AudioBus> bus(TestAudioBusFactory(
     84       audio_config_.channels, audio_config_.frequency,
     85       TestAudioBusFactory::kMiddleANoteFreq, 0.5f).NextAudioBus(kDuration));
     86 
     87   base::TimeTicks recorded_time = base::TimeTicks::Now();
     88   audio_sender_->InsertAudio(
     89       bus.get(), recorded_time,
     90       base::Bind(base::IgnoreResult(&scoped_ptr<AudioBus>::release),
     91                  base::Unretained(&bus)));
     92   task_runner_->RunTasks();
     93 
     94   // Make sure that we send at least one RTCP packet.
     95   base::TimeDelta max_rtcp_timeout =
     96       base::TimeDelta::FromMilliseconds(1 + kDefaultRtcpIntervalMs * 3 / 2);
     97   testing_clock_.Advance(max_rtcp_timeout);
     98   task_runner_->RunTasks();
     99 }
    100 
    101 }  // namespace cast
    102 }  // namespace media
    103