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 const AudioParameters& params, | |
16 const AudioFileWriter::CreateCallback& create_audio_file_writer_callback, | |
17 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
18 base::OnceClosure on_destruction_closure) | |
19 : params_(params), | |
20 create_audio_file_writer_callback_(create_audio_file_writer_callback), | |
21 recording_enabled_(0), | |
22 task_runner_(std::move(task_runner)), | |
23 on_destruction_closure_(std::move(on_destruction_closure)), | |
24 weak_factory_(this) { | |
25 DCHECK(create_audio_file_writer_callback_); | |
26 } | |
27 | |
28 AudioDebugRecordingHelper::~AudioDebugRecordingHelper() { | |
29 if (on_destruction_closure_) | |
30 std::move(on_destruction_closure_).Run(); | |
31 } | |
32 | |
33 void AudioDebugRecordingHelper::EnableDebugRecording( | |
34 const base::FilePath& file_name) { | |
35 DCHECK(task_runner_->BelongsToCurrentThread()); | |
36 DCHECK(!debug_writer_); | |
37 DCHECK(!file_name.empty()); | |
38 | |
39 debug_writer_ = create_audio_file_writer_callback_.Run(params_); | |
40 debug_writer_->Start( | |
41 file_name.AddExtension(debug_writer_->GetFileNameExtension())); | |
42 | |
43 base::subtle::Release_Store(&recording_enabled_, 1); | |
o1ka
2017/02/10 15:21:53
Let's do NoBarrier everywhere as per offline discu
Henrik Grunell
2017/02/13 15:16:48
Done.
| |
44 } | |
45 | |
46 void AudioDebugRecordingHelper::DisableDebugRecording() { | |
47 DCHECK(task_runner_->BelongsToCurrentThread()); | |
48 | |
49 base::subtle::Acquire_Store(&recording_enabled_, 0); | |
50 | |
51 if (debug_writer_) { | |
52 debug_writer_->Stop(); | |
53 debug_writer_.reset(); | |
54 } | |
55 } | |
56 | |
57 void AudioDebugRecordingHelper::OnData(const AudioBus* source) { | |
58 base::subtle::Atomic32 recording_enabled = | |
59 base::subtle::Acquire_Load(&recording_enabled_); | |
60 if (!recording_enabled) | |
61 return; | |
62 | |
63 // TODO(grunell) Don't create a new AudioBus each time. Maybe a pool of | |
64 // AudioBuses. See also comment in | |
65 // AudioInputController::AudioCallback::PerformOptionalDebugRecording. | |
66 std::unique_ptr<AudioBus> audio_bus_copy = | |
67 AudioBus::Create(source->channels(), source->frames()); | |
68 source->CopyTo(audio_bus_copy.get()); | |
69 | |
70 task_runner_->PostTask( | |
71 FROM_HERE, | |
72 base::Bind(&AudioDebugRecordingHelper::DoWrite, | |
73 weak_factory_.GetWeakPtr(), base::Passed(&audio_bus_copy))); | |
74 } | |
75 | |
76 void AudioDebugRecordingHelper::DoWrite(std::unique_ptr<media::AudioBus> data) { | |
77 DCHECK(task_runner_->BelongsToCurrentThread()); | |
78 | |
79 if (debug_writer_) | |
80 debug_writer_->Write(std::move(data)); | |
81 } | |
82 | |
83 } // namespace media | |
OLD | NEW |