| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "content/renderer/media/mock_media_constraint_factory.h" | 7 #include "content/renderer/media/mock_media_constraint_factory.h" |
| 8 #include "content/renderer/media/webrtc/processed_local_audio_track.h" |
| 8 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h" | 9 #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_audio_capturer.h" |
| 10 #include "content/renderer/media/webrtc_local_audio_track.h" | |
| 11 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/webrtc/api/mediastreaminterface.h" | 13 #include "third_party/webrtc/api/mediastreaminterface.h" |
| 14 | 14 |
| 15 using ::testing::_; | 15 using ::testing::_; |
| 16 using ::testing::AnyNumber; | 16 using ::testing::AnyNumber; |
| 17 | 17 |
| 18 namespace content { | 18 namespace content { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 class MockWebRtcAudioSink : public webrtc::AudioTrackSinkInterface { | 22 class MockWebRtcAudioSink : public webrtc::AudioTrackSinkInterface { |
| 23 public: | 23 public: |
| 24 MockWebRtcAudioSink() {} | 24 MockWebRtcAudioSink() {} |
| 25 ~MockWebRtcAudioSink() {} | 25 ~MockWebRtcAudioSink() {} |
| 26 MOCK_METHOD5(OnData, void(const void* audio_data, | 26 MOCK_METHOD5(OnData, void(const void* audio_data, |
| 27 int bits_per_sample, | 27 int bits_per_sample, |
| 28 int sample_rate, | 28 int sample_rate, |
| 29 size_t number_of_channels, | 29 size_t number_of_channels, |
| 30 size_t number_of_frames)); | 30 size_t number_of_frames)); |
| 31 }; | 31 }; |
| 32 | 32 |
| 33 } // namespace | 33 } // namespace |
| 34 | 34 |
| 35 class WebRtcLocalAudioTrackAdapterTest : public ::testing::Test { | 35 class WebRtcLocalAudioTrackAdapterTest : public ::testing::Test { |
| 36 public: | 36 public: |
| 37 WebRtcLocalAudioTrackAdapterTest() | 37 WebRtcLocalAudioTrackAdapterTest() |
| 38 : params_(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | 38 : params_(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 39 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 480), | 39 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 480), |
| 40 adapter_(WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)) { | 40 adapter_(WebRtcLocalAudioTrackAdapter::Create(std::string(), nullptr, |
| 41 nullptr)) { |
| 41 MockMediaConstraintFactory constraint_factory; | 42 MockMediaConstraintFactory constraint_factory; |
| 42 capturer_ = WebRtcAudioCapturer::CreateCapturer( | 43 track_.reset(new ProcessedLocalAudioTrack(adapter_.get())); |
| 43 -1, StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE, "", ""), | |
| 44 constraint_factory.CreateWebMediaConstraints(), NULL, NULL); | |
| 45 track_.reset(new WebRtcLocalAudioTrack(adapter_.get(), capturer_, NULL)); | |
| 46 } | 44 } |
| 47 | 45 |
| 48 protected: | 46 protected: |
| 49 void SetUp() override { | 47 void SetUp() override { |
| 50 track_->OnSetFormat(params_); | 48 track_->SetFormat(params_); |
| 51 EXPECT_TRUE(track_->GetAudioAdapter()->enabled()); | 49 EXPECT_TRUE(track_->adapter()->enabled()); |
| 52 } | 50 } |
| 53 | 51 |
| 54 media::AudioParameters params_; | 52 media::AudioParameters params_; |
| 55 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_; | 53 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_; |
| 56 scoped_refptr<WebRtcAudioCapturer> capturer_; | 54 scoped_ptr<ProcessedLocalAudioTrack> track_; |
| 57 scoped_ptr<WebRtcLocalAudioTrack> track_; | |
| 58 }; | 55 }; |
| 59 | 56 |
| 60 // Adds and Removes a WebRtcAudioSink to a local audio track. | 57 // Adds and Removes a WebRtcAudioSink to a local audio track. |
| 61 TEST_F(WebRtcLocalAudioTrackAdapterTest, AddAndRemoveSink) { | 58 TEST_F(WebRtcLocalAudioTrackAdapterTest, AddAndRemoveSink) { |
| 62 // Add a sink to the webrtc track. | 59 // Add a sink to the webrtc track. |
| 63 scoped_ptr<MockWebRtcAudioSink> sink(new MockWebRtcAudioSink()); | 60 scoped_ptr<MockWebRtcAudioSink> sink(new MockWebRtcAudioSink()); |
| 64 webrtc::AudioTrackInterface* webrtc_track = | 61 webrtc::AudioTrackInterface* webrtc_track = |
| 65 static_cast<webrtc::AudioTrackInterface*>(adapter_.get()); | 62 static_cast<webrtc::AudioTrackInterface*>(adapter_.get()); |
| 66 webrtc_track->AddSink(sink.get()); | 63 webrtc_track->AddSink(sink.get()); |
| 67 | 64 |
| 68 // Send a packet via |track_| and the data should reach the sink of the | 65 // Send a packet via |track_| and the data should reach the sink of the |
| 69 // |adapter_|. | 66 // |adapter_|. |
| 70 const scoped_ptr<media::AudioBus> audio_bus = | 67 const scoped_ptr<media::AudioBus> audio_bus = |
| 71 media::AudioBus::Create(params_); | 68 media::AudioBus::Create(params_); |
| 72 // While this test is not checking the signal data being passed around, the | 69 // While this test is not checking the signal data being passed around, the |
| 73 // implementation in WebRtcLocalAudioTrack reads the data for its signal level | 70 // implementation in ProcessedLocalAudioSource reads the data for its signal |
| 74 // computation. Initialize all samples to zero to make the memory sanitizer | 71 // level computation. Initialize all samples to zero to make the memory |
| 75 // happy. | 72 // sanitizer happy. |
| 76 audio_bus->Zero(); | 73 audio_bus->Zero(); |
| 77 | 74 |
| 78 base::TimeTicks estimated_capture_time = base::TimeTicks::Now(); | 75 base::TimeTicks estimated_capture_time = base::TimeTicks::Now(); |
| 79 EXPECT_CALL(*sink, | 76 EXPECT_CALL(*sink, |
| 80 OnData(_, 16, params_.sample_rate(), params_.channels(), | 77 OnData(_, 16, params_.sample_rate(), params_.channels(), |
| 81 params_.frames_per_buffer())); | 78 params_.frames_per_buffer())); |
| 82 track_->Capture(*audio_bus, estimated_capture_time, false); | 79 track_->DeliverDataToSinks(*audio_bus, estimated_capture_time); |
| 83 | 80 |
| 84 // Remove the sink from the webrtc track. | 81 // Remove the sink from the webrtc track. |
| 85 webrtc_track->RemoveSink(sink.get()); | 82 webrtc_track->RemoveSink(sink.get()); |
| 86 sink.reset(); | 83 sink.reset(); |
| 87 | 84 |
| 88 // Verify that no more callback gets into the sink. | 85 // Verify that no more callback gets into the sink. |
| 89 estimated_capture_time += | 86 estimated_capture_time += |
| 90 params_.frames_per_buffer() * base::TimeDelta::FromSeconds(1) / | 87 params_.frames_per_buffer() * base::TimeDelta::FromSeconds(1) / |
| 91 params_.sample_rate(); | 88 params_.sample_rate(); |
| 92 track_->Capture(*audio_bus, estimated_capture_time, false); | 89 track_->DeliverDataToSinks(*audio_bus, estimated_capture_time); |
| 93 } | 90 } |
| 94 | 91 |
| 95 TEST_F(WebRtcLocalAudioTrackAdapterTest, GetSignalLevel) { | 92 TEST_F(WebRtcLocalAudioTrackAdapterTest, GetSignalLevel) { |
| 96 webrtc::AudioTrackInterface* webrtc_track = | 93 webrtc::AudioTrackInterface* webrtc_track = |
| 97 static_cast<webrtc::AudioTrackInterface*>(adapter_.get()); | 94 static_cast<webrtc::AudioTrackInterface*>(adapter_.get()); |
| 98 int signal_level = 0; | 95 int signal_level = 0; |
| 99 EXPECT_TRUE(webrtc_track->GetSignalLevel(&signal_level)); | 96 EXPECT_TRUE(webrtc_track->GetSignalLevel(&signal_level)); |
| 100 } | 97 } |
| 101 | 98 |
| 102 } // namespace content | 99 } // namespace content |
| OLD | NEW |