Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(95)

Side by Side Diff: content/renderer/media/audio_track_recorder_unittest.cc

Issue 1406113002: Add AudioTrackRecorder for audio component of MediaStream recording. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: minyue@'s comments Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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 "content/renderer/media/audio_track_recorder.h"
6
7 #include "base/run_loop.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "content/renderer/media/media_stream_audio_source.h"
10 #include "content/renderer/media/mock_media_constraint_factory.h"
11 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h"
12 #include "content/renderer/media/webrtc_local_audio_track.h"
13 #include "media/audio/simple_sources.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/WebKit/public/web/WebHeap.h"
17
18 using ::testing::_;
19 using ::testing::DoAll;
20 using ::testing::InSequence;
21 using ::testing::Mock;
22 using ::testing::Return;
23 using ::testing::SaveArg;
24
25 namespace {
26
27 // Input audio format.
28 const media::AudioParameters::Format kInputFormat =
29 media::AudioParameters::AUDIO_PCM_LOW_LATENCY;
30 const int kBitsPerSample = 16;
31 const int kSamplingRate = 48000;
32 // TODO(ajose): Parameterize on buffer duration. http://crbug.com/548856
33 const int kBufferDuration = 60;
34 const int kFramesPerBuffer = kSamplingRate * kBufferDuration / 1000;
35
36 } // namespace
37
38 namespace content {
39
40 ACTION_P(RunClosure, closure) {
41 closure.Run();
42 }
43
44 class AudioTrackRecorderTest : public testing::Test {
45 public:
46 AudioTrackRecorderTest()
47 : params1_(kInputFormat,
48 media::CHANNEL_LAYOUT_MONO,
49 kSamplingRate,
50 kBitsPerSample,
51 kFramesPerBuffer),
52 params2_(kInputFormat,
53 media::CHANNEL_LAYOUT_STEREO,
54 kSamplingRate,
55 kBitsPerSample,
56 kFramesPerBuffer),
57 mono_source_(1 /* # channels */, 440, kSamplingRate),
58 stereo_source_(2 /* # channels */, 440, kSamplingRate) {
59 PrepareBlinkTrack();
60 audio_track_recorder_.reset(new AudioTrackRecorder(
61 blink_track_, base::Bind(&AudioTrackRecorderTest::OnEncodedAudio,
62 base::Unretained(this))));
63 }
64
65 ~AudioTrackRecorderTest() {
66 audio_track_recorder_.reset();
67 blink_track_.reset();
68 blink::WebHeap::collectAllGarbageForTesting();
69 }
70
71 scoped_ptr<media::AudioBus> NextAudioBus(int num_channels,
72 const base::TimeDelta& duration) {
73 // Only supports up to two channels for now.
74 EXPECT_TRUE(num_channels == 1 || num_channels == 2);
75 const int num_samples = static_cast<int>((kSamplingRate * duration) /
76 base::TimeDelta::FromSeconds(1));
77 scoped_ptr<media::AudioBus> bus(
78 media::AudioBus::Create(num_channels, num_samples));
79 if (num_channels == 1)
80 mono_source_.OnMoreData(bus.get(), 0);
81 else
82 stereo_source_.OnMoreData(bus.get(), 0);
83 return bus.Pass();
84 }
85
86 MOCK_METHOD3(DoOnEncodedAudio,
87 void(const media::AudioParameters& params,
88 std::string encoded_data,
89 base::TimeTicks timestamp));
90
91 void OnEncodedAudio(const media::AudioParameters& params,
92 scoped_ptr<std::string> encoded_data,
93 base::TimeTicks timestamp) {
94 EXPECT_TRUE(!encoded_data->empty());
95 DoOnEncodedAudio(params, *encoded_data, timestamp);
96 }
97
98 const base::MessageLoop message_loop_;
99
100 // ATR and WebMediaStreamTrack for fooling it.
101 scoped_ptr<AudioTrackRecorder> audio_track_recorder_;
102 blink::WebMediaStreamTrack blink_track_;
103
104 // Two different sets of AudioParameters for testing re-init of ATR.
105 const media::AudioParameters params1_;
106 const media::AudioParameters params2_;
107
108 // AudioSources for creating AudioBuses.
109 media::SineWaveAudioSource mono_source_;
110 media::SineWaveAudioSource stereo_source_;
111
112 private:
113 // Prepares a blink track of a given MediaStreamType and attaches the native
114 // track, which can be used to capture audio data and pass it to the producer.
115 // Adapted from media::WebRTCLocalAudioSourceProviderTest.
116 void PrepareBlinkTrack() {
117 MockMediaConstraintFactory constraint_factory;
118 scoped_refptr<WebRtcAudioCapturer> capturer(
119 WebRtcAudioCapturer::CreateCapturer(
120 -1, StreamDeviceInfo(),
121 constraint_factory.CreateWebMediaConstraints(), NULL, NULL));
122 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter(
123 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL));
124 scoped_ptr<WebRtcLocalAudioTrack> native_track(
125 new WebRtcLocalAudioTrack(adapter.get(), capturer, NULL));
126 blink::WebMediaStreamSource audio_source;
127 audio_source.initialize(base::UTF8ToUTF16("dummy_source_id"),
128 blink::WebMediaStreamSource::TypeAudio,
129 base::UTF8ToUTF16("dummy_source_name"),
130 false /* remote */, true /* readonly */);
131 blink_track_.initialize(blink::WebString::fromUTF8("audio_track"),
132 audio_source);
133 blink_track_.setExtraData(native_track.release());
134 }
135
136 DISALLOW_COPY_AND_ASSIGN(AudioTrackRecorderTest);
137 };
138
139 TEST_F(AudioTrackRecorderTest, OnData) {
minyue 2015/11/12 16:52:31 also try an invalid sampling frequency and see if
ajose 2015/11/13 00:22:44 I've added a comment to the test-improvement bug h
minyue 2015/11/13 09:27:31 Acknowledged.
140 InSequence s;
141 base::RunLoop run_loop;
142 base::Closure quit_closure = run_loop.QuitClosure();
143
144 // Give ATR initial audio parameters.
145 audio_track_recorder_->OnSetFormat(params1_);
146 // TODO(ajose): consider adding WillOnce(SaveArg...) and inspecting, as done
147 // in VTR unittests. http://crbug.com/548856
148 const base::TimeTicks time1 = base::TimeTicks::Now();
149 EXPECT_CALL(*this, DoOnEncodedAudio(_, _, time1)).Times(1);
150 audio_track_recorder_->OnData(
151 *NextAudioBus(params1_.channels(),
152 base::TimeDelta::FromMilliseconds(kBufferDuration)),
153 time1);
154
155 // Send more audio.
156 const base::TimeTicks time2 = base::TimeTicks::Now();
157 EXPECT_CALL(*this, DoOnEncodedAudio(_, _, _)).Times(1);
158 audio_track_recorder_->OnData(
159 *NextAudioBus(params1_.channels(),
160 base::TimeDelta::FromMilliseconds(kBufferDuration)),
161 time2);
162
163 // Give ATR new audio parameters.
164 audio_track_recorder_->OnSetFormat(params2_);
165 // Send audio with different params.
166 const base::TimeTicks time3 = base::TimeTicks::Now();
167 EXPECT_CALL(*this, DoOnEncodedAudio(_, _, _))
168 .Times(1)
169 .WillOnce(RunClosure(quit_closure));
170 audio_track_recorder_->OnData(
171 *NextAudioBus(params2_.channels(),
172 base::TimeDelta::FromMilliseconds(kBufferDuration)),
173 time3);
174
175 run_loop.Run();
176 Mock::VerifyAndClearExpectations(this);
177 }
178
179 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698