Upstream version 7.36.149.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/webrtc/webrtc_local_audio_track_adapter.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"
12
13 using ::testing::_;
14 using ::testing::AnyNumber;
15
16 namespace content {
17
18 namespace {
19
20 class MockWebRtcAudioSink : public webrtc::AudioTrackSinkInterface {
21  public:
22   MockWebRtcAudioSink() {}
23   ~MockWebRtcAudioSink() {}
24   MOCK_METHOD5(OnData, void(const void* audio_data,
25                             int bits_per_sample,
26                             int sample_rate,
27                             int number_of_channels,
28                             int number_of_frames));
29 };
30
31 }  // namespace
32
33 class WebRtcLocalAudioTrackAdapterTest : public ::testing::Test {
34  public:
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         capturer_(WebRtcAudioCapturer::CreateCapturer(
40             -1, StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE, "", ""),
41             blink::WebMediaConstraints(), NULL, NULL)),
42         track_(new WebRtcLocalAudioTrack(adapter_, capturer_, NULL)) {}
43
44  protected:
45   virtual void SetUp() OVERRIDE {
46     track_->OnSetFormat(params_);
47     EXPECT_TRUE(track_->GetAudioAdapter()->enabled());
48   }
49
50   media::AudioParameters params_;
51   scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_;
52   scoped_refptr<WebRtcAudioCapturer> capturer_;
53   scoped_ptr<WebRtcLocalAudioTrack> track_;
54 };
55
56 // Adds and Removes a WebRtcAudioSink to a local audio track.
57 TEST_F(WebRtcLocalAudioTrackAdapterTest, AddAndRemoveSink) {
58   // Add a sink to the webrtc track.
59   scoped_ptr<MockWebRtcAudioSink> sink(new MockWebRtcAudioSink());
60   webrtc::AudioTrackInterface* webrtc_track =
61       static_cast<webrtc::AudioTrackInterface*>(adapter_.get());
62   webrtc_track->AddSink(sink.get());
63
64   // Send a packet via |track_| and it data should reach the sink of the
65   // |adapter_|.
66   const int length = params_.frames_per_buffer() * params_.channels();
67   scoped_ptr<int16[]> data(new int16[length]);
68   // Initialize the data to 0 to avoid Memcheck:Uninitialized warning.
69   memset(data.get(), 0, length * sizeof(data[0]));
70
71   EXPECT_CALL(*sink,
72               OnData(_, 16, params_.sample_rate(), params_.channels(),
73                      params_.frames_per_buffer()));
74   track_->Capture(data.get(), base::TimeDelta(), 255, false, false);
75
76   // Remove the sink from the webrtc track.
77   webrtc_track->RemoveSink(sink.get());
78   sink.reset();
79
80   // Verify that no more callback gets into the sink.
81   track_->Capture(data.get(), base::TimeDelta(), 255, false, false);
82 }
83
84 TEST_F(WebRtcLocalAudioTrackAdapterTest, GetSignalLevel) {
85   webrtc::AudioTrackInterface* webrtc_track =
86       static_cast<webrtc::AudioTrackInterface*>(adapter_.get());
87   int signal_level = 0;
88   EXPECT_FALSE(webrtc_track->GetSignalLevel(&signal_level));
89
90   // Enable the audio processing in the audio track.
91   CommandLine::ForCurrentProcess()->AppendSwitch(
92       switches::kEnableAudioTrackProcessing);
93   EXPECT_TRUE(webrtc_track->GetSignalLevel(&signal_level));
94 }
95
96 }  // namespace content