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

Side by Side Diff: media/audio/audio_debug_recording_helper.cc

Issue 2582703003: Audio output debug recording. (Closed)
Patch Set: Code review. Created 3 years, 9 months 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 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/memory/ptr_util.h"
10 #include "base/single_thread_task_runner.h"
11 #include "media/audio/audio_debug_file_writer.h"
12
13 namespace media {
14
15 AudioDebugRecordingHelper::AudioDebugRecordingHelper(
16 const AudioParameters& params,
17 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
18 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner,
19 base::OnceClosure on_destruction_closure)
20 : params_(params),
21 recording_enabled_(0),
22 task_runner_(std::move(task_runner)),
23 file_task_runner_(std::move(file_task_runner)),
24 on_destruction_closure_(std::move(on_destruction_closure)),
25 weak_factory_(this) {}
26
27 AudioDebugRecordingHelper::~AudioDebugRecordingHelper() {
28 if (on_destruction_closure_)
29 std::move(on_destruction_closure_).Run();
30 }
31
32 void AudioDebugRecordingHelper::EnableDebugRecording(
33 const base::FilePath& file_name) {
34 DCHECK(task_runner_->BelongsToCurrentThread());
35 DCHECK(!debug_writer_);
36 DCHECK(!file_name.empty());
37
38 debug_writer_ = CreateAudioDebugFileWriter(params_, file_task_runner_);
39 debug_writer_->Start(
40 file_name.AddExtension(debug_writer_->GetFileNameExtension()));
41
42 base::subtle::NoBarrier_Store(&recording_enabled_, 1);
43 }
44
45 void AudioDebugRecordingHelper::DisableDebugRecording() {
46 DCHECK(task_runner_->BelongsToCurrentThread());
47
48 base::subtle::NoBarrier_Store(&recording_enabled_, 0);
49
50 if (debug_writer_) {
51 debug_writer_->Stop();
52 debug_writer_.reset();
53 }
54 }
55
56 void AudioDebugRecordingHelper::OnData(const AudioBus* source) {
57 // Check if debug recording is enabled to avoid an unecessary copy and thread
58 // jump if not. Recording can be disabled between the atomic Load() here and
59 // DoWrite(), but it's fine with a single unnecessary copy+jump at disable
60 // time. We use an atomic operation for accessing the flag on different
61 // threads. No memory barrier is needed for the same reason; a race is no
62 // problem at enable and disable time. Missing one buffer of data doesn't
63 // matter.
64 base::subtle::Atomic32 recording_enabled =
65 base::subtle::NoBarrier_Load(&recording_enabled_);
66 if (!recording_enabled)
67 return;
68
69 // TODO(grunell) Don't create a new AudioBus each time. Maybe a pool of
70 // AudioBuses. See also comment in
71 // AudioInputController::AudioCallback::PerformOptionalDebugRecording.
72 std::unique_ptr<AudioBus> audio_bus_copy =
73 AudioBus::Create(source->channels(), source->frames());
74 source->CopyTo(audio_bus_copy.get());
75
76 task_runner_->PostTask(
77 FROM_HERE,
78 base::Bind(&AudioDebugRecordingHelper::DoWrite,
79 weak_factory_.GetWeakPtr(), base::Passed(&audio_bus_copy)));
80 }
81
82 void AudioDebugRecordingHelper::DoWrite(std::unique_ptr<media::AudioBus> data) {
83 DCHECK(task_runner_->BelongsToCurrentThread());
84
85 if (debug_writer_)
86 debug_writer_->Write(std::move(data));
87 }
88
89 std::unique_ptr<AudioDebugFileWriter>
90 AudioDebugRecordingHelper::CreateAudioDebugFileWriter(
91 const AudioParameters& params,
92 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) {
93 return base::MakeUnique<AudioDebugFileWriter>(params, file_task_runner);
94 }
95
96 } // namespace media
OLDNEW
« no previous file with comments | « media/audio/audio_debug_recording_helper.h ('k') | media/audio/audio_debug_recording_helper_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698