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.
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/rtcp/rtcp.h"
14 #include "media/cast/test/fake_single_thread_task_runner.h"
15 #include "media/cast/test/utility/audio_utility.h"
16 #include "media/cast/transport/cast_transport_config.h"
17 #include "media/cast/transport/cast_transport_sender_impl.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
24 static const int64 kStartMillisecond
= GG_INT64_C(12345678900000);
27 using testing::Exactly
;
29 class TestPacketSender
: public transport::PacketSender
{
31 TestPacketSender() : number_of_rtp_packets_(0), number_of_rtcp_packets_(0) {}
33 virtual bool SendPacket(const Packet
& packet
) OVERRIDE
{
34 if (Rtcp::IsRtcpPacket(&packet
[0], packet
.size())) {
35 ++number_of_rtcp_packets_
;
37 ++number_of_rtp_packets_
;
42 int number_of_rtp_packets() const { return number_of_rtp_packets_
; }
44 int number_of_rtcp_packets() const { return number_of_rtcp_packets_
; }
47 int number_of_rtp_packets_
;
48 int number_of_rtcp_packets_
;
50 DISALLOW_COPY_AND_ASSIGN(TestPacketSender
);
53 class AudioSenderTest
: public ::testing::Test
{
55 MOCK_METHOD0(InsertAudioCallback
, void());
59 InitializeMediaLibraryForTesting();
60 testing_clock_
= new base::SimpleTestTickClock();
61 testing_clock_
->Advance(
62 base::TimeDelta::FromMilliseconds(kStartMillisecond
));
63 task_runner_
= new test::FakeSingleThreadTaskRunner(testing_clock_
);
65 new CastEnvironment(scoped_ptr
<base::TickClock
>(testing_clock_
).Pass(),
72 GetDefaultCastSenderLoggingConfig());
73 audio_config_
.codec
= transport::kOpus
;
74 audio_config_
.use_external_encoder
= false;
75 audio_config_
.frequency
= kDefaultAudioSamplingRate
;
76 audio_config_
.channels
= 2;
77 audio_config_
.bitrate
= kDefaultAudioEncoderBitrate
;
78 audio_config_
.rtp_config
.payload_type
= 127;
80 transport::CastTransportConfig transport_config
;
81 transport_config
.audio_rtp_config
.payload_type
= 127;
82 transport_config
.audio_channels
= 2;
83 transport_sender_
.reset(new transport::CastTransportSenderImpl(
86 base::Bind(&UpdateCastTransportStatus
),
89 audio_sender_
.reset(new AudioSender(
90 cast_environment_
, audio_config_
, transport_sender_
.get()));
91 task_runner_
->RunTasks();
94 virtual ~AudioSenderTest() {}
96 static void UpdateCastTransportStatus(transport::CastTransportStatus status
) {
97 EXPECT_EQ(status
, transport::TRANSPORT_INITIALIZED
);
100 base::SimpleTestTickClock
* testing_clock_
; // Owned by CastEnvironment.
101 TestPacketSender transport_
;
102 scoped_ptr
<transport::CastTransportSenderImpl
> transport_sender_
;
103 scoped_refptr
<test::FakeSingleThreadTaskRunner
> task_runner_
;
104 scoped_ptr
<AudioSender
> audio_sender_
;
105 scoped_refptr
<CastEnvironment
> cast_environment_
;
106 AudioSenderConfig audio_config_
;
109 TEST_F(AudioSenderTest
, Encode20ms
) {
110 EXPECT_CALL(*this, InsertAudioCallback()).Times(Exactly(1));
112 const base::TimeDelta kDuration
= base::TimeDelta::FromMilliseconds(20);
113 scoped_ptr
<AudioBus
> bus(
114 TestAudioBusFactory(audio_config_
.channels
,
115 audio_config_
.frequency
,
116 TestAudioBusFactory::kMiddleANoteFreq
,
117 0.5f
).NextAudioBus(kDuration
));
119 base::TimeTicks recorded_time
= base::TimeTicks::Now();
120 audio_sender_
->InsertAudio(bus
.get(),
122 base::Bind(&AudioSenderTest::InsertAudioCallback
,
123 base::Unretained(this)));
124 task_runner_
->RunTasks();
126 transport_
.number_of_rtp_packets() + transport_
.number_of_rtcp_packets(),
130 TEST_F(AudioSenderTest
, RtcpTimer
) {
131 EXPECT_CALL(*this, InsertAudioCallback()).Times(Exactly(1));
133 const base::TimeDelta kDuration
= base::TimeDelta::FromMilliseconds(20);
134 scoped_ptr
<AudioBus
> bus(
135 TestAudioBusFactory(audio_config_
.channels
,
136 audio_config_
.frequency
,
137 TestAudioBusFactory::kMiddleANoteFreq
,
138 0.5f
).NextAudioBus(kDuration
));
140 base::TimeTicks recorded_time
= base::TimeTicks::Now();
141 audio_sender_
->InsertAudio(bus
.get(),
143 base::Bind(&AudioSenderTest::InsertAudioCallback
,
144 base::Unretained(this)));
145 task_runner_
->RunTasks();
147 // Make sure that we send at least one RTCP packet.
148 base::TimeDelta max_rtcp_timeout
=
149 base::TimeDelta::FromMilliseconds(1 + kDefaultRtcpIntervalMs
* 3 / 2);
150 testing_clock_
->Advance(max_rtcp_timeout
);
151 task_runner_
->RunTasks();
152 EXPECT_GE(transport_
.number_of_rtp_packets(), 1);
153 EXPECT_EQ(transport_
.number_of_rtcp_packets(), 1);