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.
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"
24 class TestPacketSender
: public PacketSender
{
26 TestPacketSender() : number_of_rtp_packets_(0), number_of_rtcp_packets_(0) {}
28 bool SendPacket(PacketRef packet
, const base::Closure
& cb
) override
{
29 if (Rtcp::IsRtcpPacket(&packet
->data
[0], packet
->data
.size())) {
30 ++number_of_rtcp_packets_
;
32 // Check that at least one RTCP packet was sent before the first RTP
33 // packet. This confirms that the receiver will have the necessary lip
34 // sync info before it has to calculate the playout time of the first
36 if (number_of_rtp_packets_
== 0)
37 EXPECT_LE(1, number_of_rtcp_packets_
);
38 ++number_of_rtp_packets_
;
43 int64
GetBytesSent() override
{ return 0; }
45 int number_of_rtp_packets() const { return number_of_rtp_packets_
; }
47 int number_of_rtcp_packets() const { return number_of_rtcp_packets_
; }
50 int number_of_rtp_packets_
;
51 int number_of_rtcp_packets_
;
53 DISALLOW_COPY_AND_ASSIGN(TestPacketSender
);
56 class AudioSenderTest
: public ::testing::Test
{
59 InitializeMediaLibraryForTesting();
60 testing_clock_
= new base::SimpleTestTickClock();
61 testing_clock_
->Advance(base::TimeTicks::Now() - base::TimeTicks());
62 task_runner_
= new test::FakeSingleThreadTaskRunner(testing_clock_
);
64 new CastEnvironment(scoped_ptr
<base::TickClock
>(testing_clock_
).Pass(),
68 audio_config_
.codec
= CODEC_AUDIO_OPUS
;
69 audio_config_
.use_external_encoder
= false;
70 audio_config_
.frequency
= kDefaultAudioSamplingRate
;
71 audio_config_
.channels
= 2;
72 audio_config_
.bitrate
= kDefaultAudioEncoderBitrate
;
73 audio_config_
.rtp_payload_type
= 127;
75 net::IPEndPoint dummy_endpoint
;
77 transport_sender_
.reset(new CastTransportSenderImpl(
81 make_scoped_ptr(new base::DictionaryValue
),
82 base::Bind(&UpdateCastTransportStatus
),
83 BulkRawEventsCallback(),
87 audio_sender_
.reset(new AudioSender(
88 cast_environment_
, audio_config_
, transport_sender_
.get()));
89 task_runner_
->RunTasks();
92 ~AudioSenderTest() override
{}
94 static void UpdateCastTransportStatus(CastTransportStatus status
) {
95 EXPECT_EQ(TRANSPORT_AUDIO_INITIALIZED
, status
);
98 base::SimpleTestTickClock
* testing_clock_
; // Owned by CastEnvironment.
99 TestPacketSender transport_
;
100 scoped_ptr
<CastTransportSenderImpl
> transport_sender_
;
101 scoped_refptr
<test::FakeSingleThreadTaskRunner
> task_runner_
;
102 scoped_ptr
<AudioSender
> audio_sender_
;
103 scoped_refptr
<CastEnvironment
> cast_environment_
;
104 AudioSenderConfig audio_config_
;
107 TEST_F(AudioSenderTest
, Encode20ms
) {
108 const base::TimeDelta kDuration
= base::TimeDelta::FromMilliseconds(20);
109 scoped_ptr
<AudioBus
> bus(
110 TestAudioBusFactory(audio_config_
.channels
,
111 audio_config_
.frequency
,
112 TestAudioBusFactory::kMiddleANoteFreq
,
113 0.5f
).NextAudioBus(kDuration
));
115 audio_sender_
->InsertAudio(bus
.Pass(), testing_clock_
->NowTicks());
116 task_runner_
->RunTasks();
117 EXPECT_LE(1, transport_
.number_of_rtp_packets());
118 EXPECT_LE(1, transport_
.number_of_rtcp_packets());
121 TEST_F(AudioSenderTest
, RtcpTimer
) {
122 const base::TimeDelta kDuration
= base::TimeDelta::FromMilliseconds(20);
123 scoped_ptr
<AudioBus
> bus(
124 TestAudioBusFactory(audio_config_
.channels
,
125 audio_config_
.frequency
,
126 TestAudioBusFactory::kMiddleANoteFreq
,
127 0.5f
).NextAudioBus(kDuration
));
129 audio_sender_
->InsertAudio(bus
.Pass(), testing_clock_
->NowTicks());
130 task_runner_
->RunTasks();
132 // Make sure that we send at least one RTCP packet.
133 base::TimeDelta max_rtcp_timeout
=
134 base::TimeDelta::FromMilliseconds(1 + kDefaultRtcpIntervalMs
* 3 / 2);
135 testing_clock_
->Advance(max_rtcp_timeout
);
136 task_runner_
->RunTasks();
137 EXPECT_LE(1, transport_
.number_of_rtp_packets());
138 EXPECT_LE(1, transport_
.number_of_rtcp_packets());