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

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: miu'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 const int kFramesPerBuffer = 480;
33
34 } // namespace
35
36 namespace content {
37
38 ACTION_P(RunClosure, closure) {
39 closure.Run();
40 }
41
42 class AudioTrackRecorderTest : public testing::Test {
43 public:
44 AudioTrackRecorderTest()
45 : params1_(kInputFormat,
46 media::CHANNEL_LAYOUT_MONO,
47 kSamplingRate,
48 kBitsPerSample,
49 kFramesPerBuffer),
50 params2_(kInputFormat,
51 media::CHANNEL_LAYOUT_STEREO,
52 kSamplingRate,
53 kBitsPerSample,
54 kFramesPerBuffer),
55 mono_source_(1 /* # channels */, 440, kSamplingRate),
56 stereo_source_(2 /* # channels */, 440, kSamplingRate) {
57 PrepareBlinkTrack();
58 audio_track_recorder_.reset(new AudioTrackRecorder(
59 blink_track_, base::Bind(&AudioTrackRecorderTest::OnEncodedAudio,
60 base::Unretained(this))));
61 }
62
63 ~AudioTrackRecorderTest() {
64 audio_track_recorder_.reset();
65 blink_track_.reset();
66 blink::WebHeap::collectAllGarbageForTesting();
67 }
68
69 scoped_ptr<media::AudioBus> NextAudioBus(int num_channels,
70 const base::TimeDelta& duration) {
71 // Only supports up to two channels for now.
72 EXPECT_TRUE(num_channels == 1 || num_channels == 2);
73 const int num_samples = static_cast<int>((kSamplingRate * duration) /
74 base::TimeDelta::FromSeconds(1));
75 scoped_ptr<media::AudioBus> bus(
76 media::AudioBus::Create(num_channels, num_samples));
77 if (num_channels == 1) {
78 mono_source_.OnMoreData(bus.get(), 0);
79 } else {
80 // two channels
mcasas 2015/10/29 22:58:21 Nit: remove this comment and with that, also the c
ajose 2015/11/02 20:12:51 Done.
81 stereo_source_.OnMoreData(bus.get(), 0);
82 }
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 media::AudioParameters params1_;
mcasas 2015/10/29 22:58:21 nit: I think |params1|, |params2|, |mono_source||
ajose 2015/11/02 20:12:51 media::SineWaveAudioSource doesn't like being cons
106 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) {
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(), base::TimeDelta::FromMilliseconds(10)),
152 time1);
153
154 // Send more audio.
155 const base::TimeTicks time2 = base::TimeTicks::Now();
156 EXPECT_CALL(*this, DoOnEncodedAudio(_, _, _)).Times(1);
157 audio_track_recorder_->OnData(
158 *NextAudioBus(params1_.channels(), base::TimeDelta::FromMilliseconds(10)),
159 time2);
160
161 // Give ATR new audio parameters.
162 audio_track_recorder_->OnSetFormat(params2_);
163 // Send audio with different params.
164 const base::TimeTicks time3 = base::TimeTicks::Now();
165 EXPECT_CALL(*this, DoOnEncodedAudio(_, _, _))
166 .Times(1)
167 .WillOnce(RunClosure(quit_closure));
168 audio_track_recorder_->OnData(
169 *NextAudioBus(params2_.channels(), base::TimeDelta::FromMilliseconds(10)),
170 time3);
171
172 run_loop.Run();
173 Mock::VerifyAndClearExpectations(this);
174 }
175
176 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698