1 // Copyright 2016 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 "chrome/browser/media/webrtc/audio_debug_recordings_handler.h"
6 
7 #include <string>
8 #include <utility>
9 
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/files/file_util.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/task/thread_pool.h"
15 #include "base/time/time.h"
16 #include "components/webrtc_logging/browser/text_log_list.h"
17 #include "content/public/browser/audio_service.h"
18 #include "content/public/browser/browser_context.h"
19 #include "content/public/browser/browser_task_traits.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/render_process_host.h"
22 #include "media/audio/audio_debug_recording_session.h"
23 #include "services/audio/public/cpp/debug_recording_session_factory.h"
24 
25 using content::BrowserThread;
26 
27 // Keys used to attach handler to the RenderProcessHost
28 const char AudioDebugRecordingsHandler::kAudioDebugRecordingsHandlerKey[] =
29     "kAudioDebugRecordingsHandlerKey";
30 
31 namespace {
32 
33 // Returns a path name to be used as prefix for audio debug recordings files.
GetAudioDebugRecordingsPrefixPath(const base::FilePath & directory,uint64_t audio_debug_recordings_id)34 base::FilePath GetAudioDebugRecordingsPrefixPath(
35     const base::FilePath& directory,
36     uint64_t audio_debug_recordings_id) {
37   static const char kAudioDebugRecordingsFilePrefix[] = "AudioDebugRecordings.";
38   return directory.AppendASCII(kAudioDebugRecordingsFilePrefix +
39                                base::NumberToString(audio_debug_recordings_id));
40 }
41 
GetLogDirectoryAndEnsureExists(content::BrowserContext * browser_context)42 base::FilePath GetLogDirectoryAndEnsureExists(
43     content::BrowserContext* browser_context) {
44   base::FilePath log_dir_path =
45       webrtc_logging::TextLogList::GetWebRtcLogDirectoryForBrowserContextPath(
46           browser_context->GetPath());
47   base::File::Error error;
48   if (!base::CreateDirectoryAndGetError(log_dir_path, &error)) {
49     DLOG(ERROR) << "Could not create WebRTC log directory, error: " << error;
50     return base::FilePath();
51   }
52   return log_dir_path;
53 }
54 
55 }  // namespace
56 
AudioDebugRecordingsHandler(content::BrowserContext * browser_context)57 AudioDebugRecordingsHandler::AudioDebugRecordingsHandler(
58     content::BrowserContext* browser_context)
59     : browser_context_(browser_context), current_audio_debug_recordings_id_(0) {
60   DCHECK_CURRENTLY_ON(BrowserThread::UI);
61   DCHECK(browser_context_);
62 }
63 
64 AudioDebugRecordingsHandler::~AudioDebugRecordingsHandler() = default;
65 
StartAudioDebugRecordings(content::RenderProcessHost * host,base::TimeDelta delay,RecordingDoneCallback callback,RecordingErrorCallback error_callback)66 void AudioDebugRecordingsHandler::StartAudioDebugRecordings(
67     content::RenderProcessHost* host,
68     base::TimeDelta delay,
69     RecordingDoneCallback callback,
70     RecordingErrorCallback error_callback) {
71   DCHECK_CURRENTLY_ON(BrowserThread::UI);
72 
73   base::ThreadPool::PostTaskAndReplyWithResult(
74       FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
75       base::BindOnce(&GetLogDirectoryAndEnsureExists, browser_context_),
76       base::BindOnce(&AudioDebugRecordingsHandler::DoStartAudioDebugRecordings,
77                      this, host, delay, std::move(callback),
78                      std::move(error_callback)));
79 }
80 
StopAudioDebugRecordings(content::RenderProcessHost * host,RecordingDoneCallback callback,RecordingErrorCallback error_callback)81 void AudioDebugRecordingsHandler::StopAudioDebugRecordings(
82     content::RenderProcessHost* host,
83     RecordingDoneCallback callback,
84     RecordingErrorCallback error_callback) {
85   DCHECK_CURRENTLY_ON(BrowserThread::UI);
86   const bool is_manual_stop = true;
87   base::ThreadPool::PostTaskAndReplyWithResult(
88       FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
89       base::BindOnce(&GetLogDirectoryAndEnsureExists, browser_context_),
90       base::BindOnce(&AudioDebugRecordingsHandler::DoStopAudioDebugRecordings,
91                      this, host, is_manual_stop,
92                      current_audio_debug_recordings_id_, std::move(callback),
93                      std::move(error_callback)));
94 }
95 
DoStartAudioDebugRecordings(content::RenderProcessHost * host,base::TimeDelta delay,RecordingDoneCallback callback,RecordingErrorCallback error_callback,const base::FilePath & log_directory)96 void AudioDebugRecordingsHandler::DoStartAudioDebugRecordings(
97     content::RenderProcessHost* host,
98     base::TimeDelta delay,
99     RecordingDoneCallback callback,
100     RecordingErrorCallback error_callback,
101     const base::FilePath& log_directory) {
102   DCHECK_CURRENTLY_ON(BrowserThread::UI);
103 
104   if (audio_debug_recording_session_) {
105     std::move(error_callback).Run("Audio debug recordings already in progress");
106     return;
107   }
108 
109   base::FilePath prefix_path = GetAudioDebugRecordingsPrefixPath(
110       log_directory, ++current_audio_debug_recordings_id_);
111   host->EnableAudioDebugRecordings(prefix_path);
112 
113   mojo::PendingRemote<audio::mojom::DebugRecording> debug_recording;
114   content::GetAudioService().BindDebugRecording(
115       debug_recording.InitWithNewPipeAndPassReceiver());
116   audio_debug_recording_session_ = audio::CreateAudioDebugRecordingSession(
117       prefix_path, std::move(debug_recording));
118 
119   if (delay.is_zero()) {
120     const bool is_stopped = false, is_manual_stop = false;
121     std::move(callback).Run(prefix_path.AsUTF8Unsafe(), is_stopped,
122                             is_manual_stop);
123     return;
124   }
125 
126   const bool is_manual_stop = false;
127   content::GetUIThreadTaskRunner({})->PostDelayedTask(
128       FROM_HERE,
129       base::BindOnce(&AudioDebugRecordingsHandler::DoStopAudioDebugRecordings,
130                      this, host, is_manual_stop,
131                      current_audio_debug_recordings_id_, std::move(callback),
132                      std::move(error_callback), log_directory),
133       delay);
134 }
135 
DoStopAudioDebugRecordings(content::RenderProcessHost * host,bool is_manual_stop,uint64_t audio_debug_recordings_id,RecordingDoneCallback callback,RecordingErrorCallback error_callback,const base::FilePath & log_directory)136 void AudioDebugRecordingsHandler::DoStopAudioDebugRecordings(
137     content::RenderProcessHost* host,
138     bool is_manual_stop,
139     uint64_t audio_debug_recordings_id,
140     RecordingDoneCallback callback,
141     RecordingErrorCallback error_callback,
142     const base::FilePath& log_directory) {
143   DCHECK_CURRENTLY_ON(BrowserThread::UI);
144   DCHECK_LE(audio_debug_recordings_id, current_audio_debug_recordings_id_);
145 
146   base::FilePath prefix_path = GetAudioDebugRecordingsPrefixPath(
147       log_directory, audio_debug_recordings_id);
148   // Prevent an old posted StopAudioDebugRecordings() call to stop a newer dump.
149   // This could happen in a sequence like:
150   //   Start(10);  // Start dump 1. Post Stop() to run after 10 seconds.
151   //   Stop();     // Manually stop dump 1 before 10 seconds;
152   //   Start(20);  // Start dump 2. Posted Stop() for 1 should not stop dump 2.
153   if (audio_debug_recordings_id < current_audio_debug_recordings_id_) {
154     const bool is_stopped = false;
155     std::move(callback).Run(prefix_path.AsUTF8Unsafe(), is_stopped,
156                             is_manual_stop);
157     return;
158   }
159 
160   if (!audio_debug_recording_session_) {
161     std::move(error_callback).Run("No audio debug recording in progress");
162     return;
163   }
164 
165   audio_debug_recording_session_.reset();
166 
167   host->DisableAudioDebugRecordings();
168 
169   const bool is_stopped = true;
170   std::move(callback).Run(prefix_path.AsUTF8Unsafe(), is_stopped,
171                           is_manual_stop);
172 }
173