1 // Copyright 2014 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 <stdint.h> 6 7 #include "base/bind.h" 8 #include "base/bind_helpers.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "base/test/simple_test_tick_clock.h" 11 #include "media/base/media.h" 12 #include "media/cast/cast_config.h" 13 #include "media/cast/cast_environment.h" 14 #include "media/cast/net/cast_transport_config.h" 15 #include "media/cast/net/cast_transport_sender_impl.h" 16 #include "media/cast/sender/audio_sender.h" 17 #include "media/cast/test/fake_single_thread_task_runner.h" 18 #include "media/cast/test/utility/audio_utility.h" 19 #include "testing/gtest/include/gtest/gtest.h" 20 21 namespace media { 22 namespace cast { 23 24 class TestPacketSender : public PacketSender { 25 public: 26 TestPacketSender() : number_of_rtp_packets_(0), number_of_rtcp_packets_(0) {} 27 28 virtual bool SendPacket(PacketRef packet, 29 const base::Closure& cb) OVERRIDE { 30 if (Rtcp::IsRtcpPacket(&packet->data[0], packet->data.size())) { 31 ++number_of_rtcp_packets_; 32 } else { 33 // Check that at least one RTCP packet was sent before the first RTP 34 // packet. This confirms that the receiver will have the necessary lip 35 // sync info before it has to calculate the playout time of the first 36 // frame. 37 if (number_of_rtp_packets_ == 0) 38 EXPECT_LE(1, number_of_rtcp_packets_); 39 ++number_of_rtp_packets_; 40 } 41 return true; 42 } 43 44 virtual int64 GetBytesSent() OVERRIDE { 45 return 0; 46 } 47 48 int number_of_rtp_packets() const { return number_of_rtp_packets_; } 49 50 int number_of_rtcp_packets() const { return number_of_rtcp_packets_; } 51 52 private: 53 int number_of_rtp_packets_; 54 int number_of_rtcp_packets_; 55 56 DISALLOW_COPY_AND_ASSIGN(TestPacketSender); 57 }; 58 59 class AudioSenderTest : public ::testing::Test { 60 protected: 61 AudioSenderTest() { 62 InitializeMediaLibraryForTesting(); 63 testing_clock_ = new base::SimpleTestTickClock(); 64 testing_clock_->Advance(base::TimeTicks::Now() - base::TimeTicks()); 65 task_runner_ = new test::FakeSingleThreadTaskRunner(testing_clock_); 66 cast_environment_ = 67 new CastEnvironment(scoped_ptr<base::TickClock>(testing_clock_).Pass(), 68 task_runner_, 69 task_runner_, 70 task_runner_); 71 audio_config_.codec = CODEC_AUDIO_OPUS; 72 audio_config_.use_external_encoder = false; 73 audio_config_.frequency = kDefaultAudioSamplingRate; 74 audio_config_.channels = 2; 75 audio_config_.bitrate = kDefaultAudioEncoderBitrate; 76 audio_config_.rtp_payload_type = 127; 77 78 net::IPEndPoint dummy_endpoint; 79 80 transport_sender_.reset(new CastTransportSenderImpl( 81 NULL, 82 testing_clock_, 83 dummy_endpoint, 84 make_scoped_ptr(new base::DictionaryValue), 85 base::Bind(&UpdateCastTransportStatus), 86 BulkRawEventsCallback(), 87 base::TimeDelta(), 88 task_runner_, 89 &transport_)); 90 audio_sender_.reset(new AudioSender( 91 cast_environment_, audio_config_, transport_sender_.get())); 92 task_runner_->RunTasks(); 93 } 94 95 virtual ~AudioSenderTest() {} 96 97 static void UpdateCastTransportStatus(CastTransportStatus status) { 98 EXPECT_EQ(TRANSPORT_AUDIO_INITIALIZED, status); 99 } 100 101 base::SimpleTestTickClock* testing_clock_; // Owned by CastEnvironment. 102 TestPacketSender transport_; 103 scoped_ptr<CastTransportSenderImpl> transport_sender_; 104 scoped_refptr<test::FakeSingleThreadTaskRunner> task_runner_; 105 scoped_ptr<AudioSender> audio_sender_; 106 scoped_refptr<CastEnvironment> cast_environment_; 107 AudioSenderConfig audio_config_; 108 }; 109 110 TEST_F(AudioSenderTest, Encode20ms) { 111 const base::TimeDelta kDuration = base::TimeDelta::FromMilliseconds(20); 112 scoped_ptr<AudioBus> bus( 113 TestAudioBusFactory(audio_config_.channels, 114 audio_config_.frequency, 115 TestAudioBusFactory::kMiddleANoteFreq, 116 0.5f).NextAudioBus(kDuration)); 117 118 audio_sender_->InsertAudio(bus.Pass(), testing_clock_->NowTicks()); 119 task_runner_->RunTasks(); 120 EXPECT_LE(1, transport_.number_of_rtp_packets()); 121 EXPECT_LE(1, transport_.number_of_rtcp_packets()); 122 } 123 124 TEST_F(AudioSenderTest, RtcpTimer) { 125 const base::TimeDelta kDuration = base::TimeDelta::FromMilliseconds(20); 126 scoped_ptr<AudioBus> bus( 127 TestAudioBusFactory(audio_config_.channels, 128 audio_config_.frequency, 129 TestAudioBusFactory::kMiddleANoteFreq, 130 0.5f).NextAudioBus(kDuration)); 131 132 audio_sender_->InsertAudio(bus.Pass(), testing_clock_->NowTicks()); 133 task_runner_->RunTasks(); 134 135 // Make sure that we send at least one RTCP packet. 136 base::TimeDelta max_rtcp_timeout = 137 base::TimeDelta::FromMilliseconds(1 + kDefaultRtcpIntervalMs * 3 / 2); 138 testing_clock_->Advance(max_rtcp_timeout); 139 task_runner_->RunTasks(); 140 EXPECT_LE(1, transport_.number_of_rtp_packets()); 141 EXPECT_LE(1, transport_.number_of_rtcp_packets()); 142 } 143 144 } // namespace cast 145 } // namespace media 146