OLD | NEW |
(Empty) | |
| 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 <stddef.h> |
| 6 |
| 7 #include "content/renderer/media/media_stream_audio_level_calculator.h" |
| 8 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h" |
| 9 #include "content/renderer/media/webrtc_audio_capturer.h" |
| 10 #include "content/renderer/media/webrtc_local_audio_track.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/webrtc/api/mediastreaminterface.h" |
| 14 |
| 15 using ::testing::_; |
| 16 using ::testing::AnyNumber; |
| 17 |
| 18 namespace content { |
| 19 |
| 20 namespace { |
| 21 |
| 22 class MockWebRtcAudioSink : public webrtc::AudioTrackSinkInterface { |
| 23 public: |
| 24 MockWebRtcAudioSink() {} |
| 25 ~MockWebRtcAudioSink() {} |
| 26 MOCK_METHOD5(OnData, void(const void* audio_data, |
| 27 int bits_per_sample, |
| 28 int sample_rate, |
| 29 size_t number_of_channels, |
| 30 size_t number_of_frames)); |
| 31 }; |
| 32 |
| 33 } // namespace |
| 34 |
| 35 class WebRtcLocalAudioTrackAdapterTest : public ::testing::Test { |
| 36 public: |
| 37 WebRtcLocalAudioTrackAdapterTest() |
| 38 : params_(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 39 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 480), |
| 40 adapter_(WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)) { |
| 41 track_.reset(new WebRtcLocalAudioTrack(adapter_.get())); |
| 42 } |
| 43 |
| 44 protected: |
| 45 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 std::unique_ptr<WebRtcLocalAudioTrack> track_; |
| 53 }; |
| 54 |
| 55 // Adds and Removes a WebRtcAudioSink to a local audio track. |
| 56 TEST_F(WebRtcLocalAudioTrackAdapterTest, AddAndRemoveSink) { |
| 57 // Add a sink to the webrtc track. |
| 58 std::unique_ptr<MockWebRtcAudioSink> sink(new MockWebRtcAudioSink()); |
| 59 webrtc::AudioTrackInterface* webrtc_track = |
| 60 static_cast<webrtc::AudioTrackInterface*>(adapter_.get()); |
| 61 webrtc_track->AddSink(sink.get()); |
| 62 |
| 63 // Send a packet via |track_| and the data should reach the sink of the |
| 64 // |adapter_|. |
| 65 const std::unique_ptr<media::AudioBus> audio_bus = |
| 66 media::AudioBus::Create(params_); |
| 67 // While this test is not checking the signal data being passed around, the |
| 68 // implementation in WebRtcLocalAudioTrack reads the data for its signal level |
| 69 // computation. Initialize all samples to zero to make the memory sanitizer |
| 70 // happy. |
| 71 audio_bus->Zero(); |
| 72 |
| 73 base::TimeTicks estimated_capture_time = base::TimeTicks::Now(); |
| 74 EXPECT_CALL(*sink, |
| 75 OnData(_, 16, params_.sample_rate(), params_.channels(), |
| 76 params_.frames_per_buffer())); |
| 77 track_->Capture(*audio_bus, estimated_capture_time); |
| 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 estimated_capture_time += |
| 85 params_.frames_per_buffer() * base::TimeDelta::FromSeconds(1) / |
| 86 params_.sample_rate(); |
| 87 track_->Capture(*audio_bus, estimated_capture_time); |
| 88 } |
| 89 |
| 90 TEST_F(WebRtcLocalAudioTrackAdapterTest, GetSignalLevel) { |
| 91 webrtc::AudioTrackInterface* webrtc_track = |
| 92 static_cast<webrtc::AudioTrackInterface*>(adapter_.get()); |
| 93 int signal_level = -1; |
| 94 EXPECT_FALSE(webrtc_track->GetSignalLevel(&signal_level)); |
| 95 MediaStreamAudioLevelCalculator calculator; |
| 96 adapter_->SetLevel(calculator.level()); |
| 97 signal_level = -1; |
| 98 EXPECT_TRUE(webrtc_track->GetSignalLevel(&signal_level)); |
| 99 EXPECT_EQ(0, signal_level); |
| 100 } |
| 101 |
| 102 } // namespace content |
OLD | NEW |