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

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: 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 {
minyue 2015/11/11 13:40:10 A slightly bigger problem is that we have no test
ajose 2015/11/12 00:10:41 Would this be redundant with Opus's tests? Do you
minyue 2015/11/12 16:52:31 I think it is just fine to try to decode a stream
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 stereo_source_.OnMoreData(bus.get(), 0);
81 return bus.Pass();
82 }
83
84 MOCK_METHOD3(DoOnEncodedAudio,
85 void(const media::AudioParameters& params,
86 std::string encoded_data,
87 base::TimeTicks timestamp));
88
89 void OnEncodedAudio(const media::AudioParameters& params,
90 scoped_ptr<std::string> encoded_data,
91 base::TimeTicks timestamp) {
92 EXPECT_TRUE(!encoded_data->empty());
93 DoOnEncodedAudio(params, *encoded_data, timestamp);
94 }
95
96 const base::MessageLoop message_loop_;
97
98 // ATR and WebMediaStreamTrack for fooling it.
99 scoped_ptr<AudioTrackRecorder> audio_track_recorder_;
100 blink::WebMediaStreamTrack blink_track_;
101
102 // Two different sets of AudioParameters for testing re-init of ATR.
103 const media::AudioParameters params1_;
104 const media::AudioParameters params2_;
105
106 // AudioSources for creating AudioBuses.
107 media::SineWaveAudioSource mono_source_;
108 media::SineWaveAudioSource stereo_source_;
109
110 private:
111 // Prepares a blink track of a given MediaStreamType and attaches the native
112 // track, which can be used to capture audio data and pass it to the producer.
113 // Adapted from media::WebRTCLocalAudioSourceProviderTest.
114 void PrepareBlinkTrack() {
115 MockMediaConstraintFactory constraint_factory;
116 scoped_refptr<WebRtcAudioCapturer> capturer(
117 WebRtcAudioCapturer::CreateCapturer(
118 -1, StreamDeviceInfo(),
119 constraint_factory.CreateWebMediaConstraints(), NULL, NULL));
120 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter(
121 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL));
122 scoped_ptr<WebRtcLocalAudioTrack> native_track(
123 new WebRtcLocalAudioTrack(adapter.get(), capturer, NULL));
124 blink::WebMediaStreamSource audio_source;
125 audio_source.initialize(base::UTF8ToUTF16("dummy_source_id"),
126 blink::WebMediaStreamSource::TypeAudio,
127 base::UTF8ToUTF16("dummy_source_name"),
128 false /* remote */, true /* readonly */);
129 blink_track_.initialize(blink::WebString::fromUTF8("audio_track"),
130 audio_source);
131 blink_track_.setExtraData(native_track.release());
132 }
133
134 DISALLOW_COPY_AND_ASSIGN(AudioTrackRecorderTest);
135 };
136
137 TEST_F(AudioTrackRecorderTest, OnData) {
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. http://crbug.com/548856
146 const base::TimeTicks time1 = base::TimeTicks::Now();
147 EXPECT_CALL(*this, DoOnEncodedAudio(_, _, time1)).Times(1);
148 audio_track_recorder_->OnData(
149 *NextAudioBus(params1_.channels(), base::TimeDelta::FromMilliseconds(10)),
150 time1);
151
152 // Send more audio.
153 const base::TimeTicks time2 = base::TimeTicks::Now();
154 EXPECT_CALL(*this, DoOnEncodedAudio(_, _, _)).Times(1);
155 audio_track_recorder_->OnData(
156 *NextAudioBus(params1_.channels(), base::TimeDelta::FromMilliseconds(10)),
157 time2);
158
159 // Give ATR new audio parameters.
160 audio_track_recorder_->OnSetFormat(params2_);
161 // Send audio with different params.
162 const base::TimeTicks time3 = base::TimeTicks::Now();
163 EXPECT_CALL(*this, DoOnEncodedAudio(_, _, _))
164 .Times(1)
165 .WillOnce(RunClosure(quit_closure));
166 audio_track_recorder_->OnData(
167 *NextAudioBus(params2_.channels(), base::TimeDelta::FromMilliseconds(10)),
168 time3);
169
170 run_loop.Run();
171 Mock::VerifyAndClearExpectations(this);
172 }
173
174 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698