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 "content/renderer/media/mock_media_constraint_factory.h"
6 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h"
7 #include "content/renderer/media/webrtc_audio_capturer.h"
8 #include "content/renderer/media/webrtc_local_audio_track.h"
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
14 using ::testing::AnyNumber
;
20 class MockWebRtcAudioSink
: public webrtc::AudioTrackSinkInterface
{
22 MockWebRtcAudioSink() {}
23 ~MockWebRtcAudioSink() {}
24 MOCK_METHOD5(OnData
, void(const void* audio_data
,
27 int number_of_channels
,
28 int number_of_frames
));
33 class WebRtcLocalAudioTrackAdapterTest
: public ::testing::Test
{
35 WebRtcLocalAudioTrackAdapterTest()
36 : params_(media::AudioParameters::AUDIO_PCM_LOW_LATENCY
,
37 media::CHANNEL_LAYOUT_STEREO
, 48000, 16, 480),
38 adapter_(WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
)) {
39 MockMediaConstraintFactory constraint_factory
;
40 capturer_
= WebRtcAudioCapturer::CreateCapturer(
41 -1, StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE
, "", ""),
42 constraint_factory
.CreateWebMediaConstraints(), NULL
, NULL
);
43 track_
.reset(new WebRtcLocalAudioTrack(adapter_
.get(), capturer_
, NULL
));
47 void SetUp() override
{
48 track_
->OnSetFormat(params_
);
49 EXPECT_TRUE(track_
->GetAudioAdapter()->enabled());
52 media::AudioParameters params_
;
53 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter_
;
54 scoped_refptr
<WebRtcAudioCapturer
> capturer_
;
55 scoped_ptr
<WebRtcLocalAudioTrack
> track_
;
58 // Adds and Removes a WebRtcAudioSink to a local audio track.
59 TEST_F(WebRtcLocalAudioTrackAdapterTest
, AddAndRemoveSink
) {
60 // Add a sink to the webrtc track.
61 scoped_ptr
<MockWebRtcAudioSink
> sink(new MockWebRtcAudioSink());
62 webrtc::AudioTrackInterface
* webrtc_track
=
63 static_cast<webrtc::AudioTrackInterface
*>(adapter_
.get());
64 webrtc_track
->AddSink(sink
.get());
66 // Send a packet via |track_| and the data should reach the sink of the
68 const scoped_ptr
<media::AudioBus
> audio_bus
=
69 media::AudioBus::Create(params_
);
70 // While this test is not checking the signal data being passed around, the
71 // implementation in WebRtcLocalAudioTrack reads the data for its signal level
72 // computation. Initialize all samples to zero to make the memory sanitizer
76 base::TimeTicks estimated_capture_time
= base::TimeTicks::Now();
78 OnData(_
, 16, params_
.sample_rate(), params_
.channels(),
79 params_
.frames_per_buffer()));
80 track_
->Capture(*audio_bus
, estimated_capture_time
, false);
82 // Remove the sink from the webrtc track.
83 webrtc_track
->RemoveSink(sink
.get());
86 // Verify that no more callback gets into the sink.
87 estimated_capture_time
+=
88 params_
.frames_per_buffer() * base::TimeDelta::FromSeconds(1) /
89 params_
.sample_rate();
90 track_
->Capture(*audio_bus
, estimated_capture_time
, false);
93 TEST_F(WebRtcLocalAudioTrackAdapterTest
, GetSignalLevel
) {
94 webrtc::AudioTrackInterface
* webrtc_track
=
95 static_cast<webrtc::AudioTrackInterface
*>(adapter_
.get());
97 EXPECT_TRUE(webrtc_track
->GetSignalLevel(&signal_level
));
100 } // namespace content