Remove INJECT_EVENTS permissions from test APKs.
[chromium-blink-merge.git] / media / cast / sender / audio_sender_unittest.cc
blob1f4ae9edf5fe58c8443bf25ef0f59b9417c67f77
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.
5 #include <stdint.h>
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"
21 namespace media {
22 namespace cast {
24 namespace {
26 void SaveOperationalStatus(OperationalStatus* out_status,
27 OperationalStatus in_status) {
28 DVLOG(1) << "OperationalStatus transitioning from " << *out_status << " to "
29 << in_status;
30 *out_status = in_status;
33 } // namespace
35 class TestPacketSender : public PacketSender {
36 public:
37 TestPacketSender() : number_of_rtp_packets_(0), number_of_rtcp_packets_(0) {}
39 bool SendPacket(PacketRef packet, const base::Closure& cb) final {
40 if (Rtcp::IsRtcpPacket(&packet->data[0], packet->data.size())) {
41 ++number_of_rtcp_packets_;
42 } else {
43 // Check that at least one RTCP packet was sent before the first RTP
44 // packet. This confirms that the receiver will have the necessary lip
45 // sync info before it has to calculate the playout time of the first
46 // frame.
47 if (number_of_rtp_packets_ == 0)
48 EXPECT_LE(1, number_of_rtcp_packets_);
49 ++number_of_rtp_packets_;
51 return true;
54 int64 GetBytesSent() final { return 0; }
56 int number_of_rtp_packets() const { return number_of_rtp_packets_; }
58 int number_of_rtcp_packets() const { return number_of_rtcp_packets_; }
60 private:
61 int number_of_rtp_packets_;
62 int number_of_rtcp_packets_;
64 DISALLOW_COPY_AND_ASSIGN(TestPacketSender);
67 class AudioSenderTest : public ::testing::Test {
68 protected:
69 AudioSenderTest() {
70 InitializeMediaLibrary();
71 testing_clock_ = new base::SimpleTestTickClock();
72 testing_clock_->Advance(base::TimeTicks::Now() - base::TimeTicks());
73 task_runner_ = new test::FakeSingleThreadTaskRunner(testing_clock_);
74 cast_environment_ =
75 new CastEnvironment(scoped_ptr<base::TickClock>(testing_clock_).Pass(),
76 task_runner_,
77 task_runner_,
78 task_runner_);
79 audio_config_.codec = CODEC_AUDIO_OPUS;
80 audio_config_.use_external_encoder = false;
81 audio_config_.frequency = kDefaultAudioSamplingRate;
82 audio_config_.channels = 2;
83 audio_config_.bitrate = kDefaultAudioEncoderBitrate;
84 audio_config_.rtp_payload_type = 127;
86 net::IPEndPoint dummy_endpoint;
88 transport_sender_.reset(new CastTransportSenderImpl(
89 NULL,
90 testing_clock_,
91 net::IPEndPoint(),
92 dummy_endpoint,
93 make_scoped_ptr(new base::DictionaryValue),
94 base::Bind(&UpdateCastTransportStatus),
95 BulkRawEventsCallback(),
96 base::TimeDelta(),
97 task_runner_,
98 PacketReceiverCallback(),
99 &transport_));
100 OperationalStatus operational_status = STATUS_UNINITIALIZED;
101 audio_sender_.reset(new AudioSender(
102 cast_environment_,
103 audio_config_,
104 base::Bind(&SaveOperationalStatus, &operational_status),
105 transport_sender_.get()));
106 task_runner_->RunTasks();
107 CHECK_EQ(STATUS_INITIALIZED, operational_status);
110 ~AudioSenderTest() override {}
112 static void UpdateCastTransportStatus(CastTransportStatus status) {
113 EXPECT_EQ(TRANSPORT_AUDIO_INITIALIZED, status);
116 base::SimpleTestTickClock* testing_clock_; // Owned by CastEnvironment.
117 TestPacketSender transport_;
118 scoped_ptr<CastTransportSenderImpl> transport_sender_;
119 scoped_refptr<test::FakeSingleThreadTaskRunner> task_runner_;
120 scoped_ptr<AudioSender> audio_sender_;
121 scoped_refptr<CastEnvironment> cast_environment_;
122 AudioSenderConfig audio_config_;
125 TEST_F(AudioSenderTest, Encode20ms) {
126 const base::TimeDelta kDuration = base::TimeDelta::FromMilliseconds(20);
127 scoped_ptr<AudioBus> bus(
128 TestAudioBusFactory(audio_config_.channels,
129 audio_config_.frequency,
130 TestAudioBusFactory::kMiddleANoteFreq,
131 0.5f).NextAudioBus(kDuration));
133 audio_sender_->InsertAudio(bus.Pass(), testing_clock_->NowTicks());
134 task_runner_->RunTasks();
135 EXPECT_LE(1, transport_.number_of_rtp_packets());
136 EXPECT_LE(1, transport_.number_of_rtcp_packets());
139 TEST_F(AudioSenderTest, RtcpTimer) {
140 const base::TimeDelta kDuration = base::TimeDelta::FromMilliseconds(20);
141 scoped_ptr<AudioBus> bus(
142 TestAudioBusFactory(audio_config_.channels,
143 audio_config_.frequency,
144 TestAudioBusFactory::kMiddleANoteFreq,
145 0.5f).NextAudioBus(kDuration));
147 audio_sender_->InsertAudio(bus.Pass(), testing_clock_->NowTicks());
148 task_runner_->RunTasks();
150 // Make sure that we send at least one RTCP packet.
151 base::TimeDelta max_rtcp_timeout =
152 base::TimeDelta::FromMilliseconds(1 + kDefaultRtcpIntervalMs * 3 / 2);
153 testing_clock_->Advance(max_rtcp_timeout);
154 task_runner_->RunTasks();
155 EXPECT_LE(1, transport_.number_of_rtp_packets());
156 EXPECT_LE(1, transport_.number_of_rtcp_packets());
159 } // namespace cast
160 } // namespace media