Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / webrtc / webrtc_local_audio_track_adapter_unittest.cc
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.
4
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"
13
14 using ::testing::_;
15 using ::testing::AnyNumber;
16
17 namespace content {
18
19 namespace {
20
21 class MockWebRtcAudioSink : public webrtc::AudioTrackSinkInterface {
22  public:
23   MockWebRtcAudioSink() {}
24   ~MockWebRtcAudioSink() {}
25   MOCK_METHOD5(OnData, void(const void* audio_data,
26                             int bits_per_sample,
27                             int sample_rate,
28                             int number_of_channels,
29                             int number_of_frames));
30 };
31
32 }  // namespace
33
34 class WebRtcLocalAudioTrackAdapterTest : public ::testing::Test {
35  public:
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));
45   }
46
47  protected:
48   virtual void SetUp() OVERRIDE {
49     track_->OnSetFormat(params_);
50     EXPECT_TRUE(track_->GetAudioAdapter()->enabled());
51   }
52
53   media::AudioParameters params_;
54   scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_;
55   scoped_refptr<WebRtcAudioCapturer> capturer_;
56   scoped_ptr<WebRtcLocalAudioTrack> track_;
57 };
58
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());
66
67   // Send a packet via |track_| and it data should reach the sink of the
68   // |adapter_|.
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]));
73
74   EXPECT_CALL(*sink,
75               OnData(_, 16, params_.sample_rate(), params_.channels(),
76                      params_.frames_per_buffer()));
77   track_->Capture(data.get(), base::TimeDelta(), 255, false, false);
78
79   // Remove the sink from the webrtc track.
80   webrtc_track->RemoveSink(sink.get());
81   sink.reset();
82
83   // Verify that no more callback gets into the sink.
84   track_->Capture(data.get(), base::TimeDelta(), 255, false, false);
85 }
86
87 TEST_F(WebRtcLocalAudioTrackAdapterTest, GetSignalLevel) {
88   webrtc::AudioTrackInterface* webrtc_track =
89       static_cast<webrtc::AudioTrackInterface*>(adapter_.get());
90   int signal_level = 0;
91   EXPECT_TRUE(webrtc_track->GetSignalLevel(&signal_level));
92
93   // Disable the audio processing in the audio track.
94   CommandLine::ForCurrentProcess()->AppendSwitch(
95       switches::kDisableAudioTrackProcessing);
96   EXPECT_FALSE(webrtc_track->GetSignalLevel(&signal_level));
97 }
98
99 }  // namespace content