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 "base/command_line.h"
6 #include "content/public/common/content_switches.h"
7 #include "content/renderer/media/mock_media_constraint_factory.h"
8 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h"
9 #include "content/renderer/media/webrtc_local_audio_track.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
15 using ::testing::AnyNumber
;
21 class MockWebRtcAudioSink
: public webrtc::AudioTrackSinkInterface
{
23 MockWebRtcAudioSink() {}
24 ~MockWebRtcAudioSink() {}
25 MOCK_METHOD5(OnData
, void(const void* audio_data
,
28 int number_of_channels
,
29 int number_of_frames
));
34 class WebRtcLocalAudioTrackAdapterTest
: public ::testing::Test
{
36 WebRtcLocalAudioTrackAdapterTest()
37 : params_(media::AudioParameters::AUDIO_PCM_LOW_LATENCY
,
38 media::CHANNEL_LAYOUT_STEREO
, 48000, 16, 480),
39 adapter_(WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
)) {
40 MockMediaConstraintFactory constraint_factory
;
41 capturer_
= WebRtcAudioCapturer::CreateCapturer(
42 -1, StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE
, "", ""),
43 constraint_factory
.CreateWebMediaConstraints(), NULL
, NULL
);
44 track_
.reset(new WebRtcLocalAudioTrack(adapter_
.get(), capturer_
, NULL
));
48 virtual void SetUp() override
{
49 track_
->OnSetFormat(params_
);
50 EXPECT_TRUE(track_
->GetAudioAdapter()->enabled());
53 media::AudioParameters params_
;
54 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter_
;
55 scoped_refptr
<WebRtcAudioCapturer
> capturer_
;
56 scoped_ptr
<WebRtcLocalAudioTrack
> track_
;
59 // Adds and Removes a WebRtcAudioSink to a local audio track.
60 TEST_F(WebRtcLocalAudioTrackAdapterTest
, AddAndRemoveSink
) {
61 // Add a sink to the webrtc track.
62 scoped_ptr
<MockWebRtcAudioSink
> sink(new MockWebRtcAudioSink());
63 webrtc::AudioTrackInterface
* webrtc_track
=
64 static_cast<webrtc::AudioTrackInterface
*>(adapter_
.get());
65 webrtc_track
->AddSink(sink
.get());
67 // Send a packet via |track_| and it data should reach the sink of the
69 const int length
= params_
.frames_per_buffer() * params_
.channels();
70 scoped_ptr
<int16
[]> data(new int16
[length
]);
71 // Initialize the data to 0 to avoid Memcheck:Uninitialized warning.
72 memset(data
.get(), 0, length
* sizeof(data
[0]));
75 OnData(_
, 16, params_
.sample_rate(), params_
.channels(),
76 params_
.frames_per_buffer()));
77 track_
->Capture(data
.get(), base::TimeDelta(), 255, false, false);
79 // Remove the sink from the webrtc track.
80 webrtc_track
->RemoveSink(sink
.get());
83 // Verify that no more callback gets into the sink.
84 track_
->Capture(data
.get(), base::TimeDelta(), 255, false, false);
87 TEST_F(WebRtcLocalAudioTrackAdapterTest
, GetSignalLevel
) {
88 webrtc::AudioTrackInterface
* webrtc_track
=
89 static_cast<webrtc::AudioTrackInterface
*>(adapter_
.get());
91 EXPECT_TRUE(webrtc_track
->GetSignalLevel(&signal_level
));
93 // Disable the audio processing in the audio track.
94 CommandLine::ForCurrentProcess()->AppendSwitch(
95 switches::kDisableAudioTrackProcessing
);
96 EXPECT_FALSE(webrtc_track
->GetSignalLevel(&signal_level
));
99 } // namespace content