OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 "media/audio/audio_debug_recording_helper.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/files/file_path.h" | |
9 #include "base/single_thread_task_runner.h" | |
10 #include "media/audio/audio_manager.h" | |
11 | |
12 namespace media { | |
13 | |
14 AudioDebugRecordingHelper::AudioDebugRecordingHelper( | |
15 AudioManager* audio_manager, | |
16 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | |
17 : audio_manager_(audio_manager), | |
18 task_runner_(std::move(task_runner)), | |
19 weak_factory_(this) { | |
20 DCHECK(audio_manager_); | |
21 } | |
22 | |
23 AudioDebugRecordingHelper::~AudioDebugRecordingHelper() {} | |
24 | |
25 void AudioDebugRecordingHelper::EnableDebugRecording( | |
26 const AudioParameters& params, | |
27 const base::FilePath& file_name) { | |
28 DCHECK(task_runner_->BelongsToCurrentThread()); | |
29 DCHECK(!debug_writer_); | |
30 | |
31 debug_writer_ = audio_manager_->CreateAudioFileWriter(params); | |
o1ka
2017/01/20 11:36:26
This is an unfortunate dependency on AudioManager.
Henrik Grunell
2017/01/26 10:25:09
Done.
| |
32 | |
33 // The debug writer writes in wave format. | |
34 debug_writer_->Start(file_name.AddExtension("wav")); | |
35 } | |
36 | |
37 void AudioDebugRecordingHelper::DisableDebugRecording() { | |
38 DCHECK(task_runner_->BelongsToCurrentThread()); | |
39 | |
40 if (debug_writer_) { | |
41 debug_writer_->Stop(); | |
42 debug_writer_.reset(); | |
43 } | |
44 } | |
45 | |
46 void AudioDebugRecordingHelper::MaybeWrite(const AudioBus* source) { | |
47 if (!WillWrite()) | |
48 return; | |
49 | |
50 // TODO(grunell) Don't create a new AudioBus each time. Maybe a pool of | |
51 // AudioBuses. See also AudioInputController. | |
52 std::unique_ptr<AudioBus> audio_bus_copy = | |
53 AudioBus::Create(source->channels(), source->frames()); | |
54 source->CopyTo(audio_bus_copy.get()); | |
55 | |
56 task_runner_->PostTask( | |
57 FROM_HERE, | |
58 base::Bind(&AudioDebugRecordingHelper::DoWrite, | |
59 weak_factory_.GetWeakPtr(), base::Passed(&audio_bus_copy))); | |
o1ka
2017/01/20 11:36:26
Can you have one pre-created weak pointer and reus
Henrik Grunell
2017/01/26 10:25:09
I haven't seen it used that way, have you? Doesn't
| |
60 } | |
61 | |
62 bool AudioDebugRecordingHelper::WillWrite() { | |
63 return debug_writer_ && debug_writer_->WillWrite(); | |
o1ka
2017/01/20 11:36:26
Add a comment on why it's thread-safe enough?
Henrik Grunell
2017/01/26 10:25:09
Done. Both here and in header.
| |
64 } | |
65 | |
66 void AudioDebugRecordingHelper::DoWrite(std::unique_ptr<media::AudioBus> data) { | |
67 DCHECK(task_runner_->BelongsToCurrentThread()); | |
68 | |
69 if (debug_writer_) | |
70 debug_writer_->Write(std::move(data)); | |
71 } | |
72 | |
73 } // namspace media | |
OLD | NEW |