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

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: address comments, trybots 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(
70 int num_channels, const base::TimeDelta& duration) {
71 // Only supports up to two channels for now.
72 EXPECT_GE(num_channels, 1);
73 EXPECT_LE(num_channels, 2);
mcasas 2015/10/28 20:03:00 why not EXPECT(num_channels == 1 || num_channels =
ajose 2015/10/28 21:54:06 Done.
74 const int num_samples = static_cast<int>((kSamplingRate * duration) /
75 base::TimeDelta::FromSeconds(1));
76 scoped_ptr<media::AudioBus> bus(
77 media::AudioBus::Create(num_channels, num_samples));
78 (num_channels == 1) ? mono_source_.OnMoreData(bus.get(), 0)
79 : stereo_source_.OnMoreData(bus.get(), 0);
mcasas 2015/10/28 20:03:00 I understand the code but I think it's the first t
ajose 2015/10/28 21:54:06 Done.
80 return bus.Pass();
81 }
82
83 MOCK_METHOD3(DoOnEncodedAudio,
84 void(const media::AudioParameters& params,
85 std::string encoded_data,
86 base::TimeTicks timestamp));
87
88 void OnEncodedAudio(const media::AudioParameters& params,
89 scoped_ptr<std::string> encoded_data,
90 base::TimeTicks timestamp) {
91 EXPECT_TRUE(!encoded_data->empty());
92 DoOnEncodedAudio(params, *encoded_data, timestamp);
93 }
94
95 const base::MessageLoop message_loop_;
96
97 // ATR and WebMediaStreamTrack for fooling it.
98 scoped_ptr<AudioTrackRecorder> audio_track_recorder_;
99 blink::WebMediaStreamTrack blink_track_;
100
101 // Two different sets of AudioParameters for testing re-init of ATR.
102 media::AudioParameters params1_;
103 media::AudioParameters params2_;
104
105 // AudioSources for creating AudioBuses.
106 media::SineWaveAudioSource mono_source_;
107 media::SineWaveAudioSource stereo_source_;
108
109 private:
110 // Prepares a blink track of a given MediaStreamType and attaches the native
111 // track, which can be used to capture audio data and pass it to the producer.
112 // Adapted from media::WebRTCLocalAudioSourceProviderTest.
113 void PrepareBlinkTrack() {
114 MockMediaConstraintFactory constraint_factory;
115 scoped_refptr<WebRtcAudioCapturer> capturer(
116 WebRtcAudioCapturer::CreateCapturer(
117 -1, StreamDeviceInfo(),
118 constraint_factory.CreateWebMediaConstraints(), NULL, NULL));
119 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter(
120 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL));
121 scoped_ptr<WebRtcLocalAudioTrack> native_track(
122 new WebRtcLocalAudioTrack(adapter.get(), capturer, NULL));
123 blink::WebMediaStreamSource audio_source;
124 audio_source.initialize(base::UTF8ToUTF16("dummy_source_id"),
125 blink::WebMediaStreamSource::TypeAudio,
126 base::UTF8ToUTF16("dummy_source_name"),
127 false /* remote */, true /* readonly */);
128 blink_track_.initialize(blink::WebString::fromUTF8("audio_track"),
129 audio_source);
130 blink_track_.setExtraData(native_track.release());
131 }
132
133 DISALLOW_COPY_AND_ASSIGN(AudioTrackRecorderTest);
134 };
135
136 TEST_F(AudioTrackRecorderTest, OnData) {
137
138 InSequence s;
139 base::RunLoop run_loop;
140 base::Closure quit_closure = run_loop.QuitClosure();
141
142 // Give ATR initial audio parameters.
143 audio_track_recorder_->OnSetFormat(params1_);
144 // TODO(ajose): consider adding WillOnce(SaveArg...) and inspecting, as done
145 // in VTR unittests.
146 // TODO(ajose): Using 10ms chunks due to hard-coded 100fps framerate.
147 // Need to figure out what to do about framerate.
mcasas 2015/10/28 20:03:00 TODO(s) need to have an associated crbug.com. Or
ajose 2015/10/28 21:54:06 SGTM. Will add a bug for the framerate stuff if it
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(10)), 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(),
159 base::TimeDelta::FromMilliseconds(10)), 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(),
170 base::TimeDelta::FromMilliseconds(10)), 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