OLD | NEW |
(Empty) | |
| 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/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/time/time.h" |
| 15 #include "chrome/browser/media/webrtc_log_list.h" |
| 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/common/chrome_switches.h" |
| 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/render_process_host.h" |
| 20 |
| 21 using content::BrowserThread; |
| 22 |
| 23 // Keys used to attach handler to the RenderProcessHost |
| 24 const char AudioDebugRecordingsHandler::kAudioDebugRecordingsHandlerKey[] = |
| 25 "kAudioDebugRecordingsHandlerKey"; |
| 26 |
| 27 namespace { |
| 28 |
| 29 // Returns a path name to be used as prefix for audio debug recordings files. |
| 30 base::FilePath GetAudioDebugRecordingsPrefixPath( |
| 31 const base::FilePath& directory, |
| 32 uint64_t audio_debug_recordings_id) { |
| 33 static const char kAudioDebugRecordingsFilePrefix[] = "AudioDebugRecordings."; |
| 34 return directory.AppendASCII(kAudioDebugRecordingsFilePrefix + |
| 35 base::Int64ToString(audio_debug_recordings_id)); |
| 36 } |
| 37 |
| 38 } // namespace |
| 39 |
| 40 AudioDebugRecordingsHandler::AudioDebugRecordingsHandler(Profile* profile) |
| 41 : profile_(profile), |
| 42 is_audio_debug_recordings_in_progress_(false), |
| 43 current_audio_debug_recordings_id_(0) { |
| 44 DCHECK(profile_); |
| 45 } |
| 46 |
| 47 AudioDebugRecordingsHandler::~AudioDebugRecordingsHandler() {} |
| 48 |
| 49 void AudioDebugRecordingsHandler::StartAudioDebugRecordings( |
| 50 content::RenderProcessHost* host, |
| 51 base::TimeDelta delay, |
| 52 const RecordingDoneCallback& callback, |
| 53 const RecordingErrorCallback& error_callback) { |
| 54 DCHECK(thread_checker_.CalledOnValidThread()); |
| 55 |
| 56 BrowserThread::PostTaskAndReplyWithResult( |
| 57 BrowserThread::FILE, FROM_HERE, |
| 58 base::Bind(&AudioDebugRecordingsHandler::GetLogDirectoryAndEnsureExists, |
| 59 this), |
| 60 base::Bind(&AudioDebugRecordingsHandler::DoStartAudioDebugRecordings, |
| 61 this, host, delay, callback, error_callback)); |
| 62 } |
| 63 |
| 64 void AudioDebugRecordingsHandler::StopAudioDebugRecordings( |
| 65 content::RenderProcessHost* host, |
| 66 const RecordingDoneCallback& callback, |
| 67 const RecordingErrorCallback& error_callback) { |
| 68 DCHECK(thread_checker_.CalledOnValidThread()); |
| 69 const bool is_manual_stop = true; |
| 70 BrowserThread::PostTaskAndReplyWithResult( |
| 71 BrowserThread::FILE, FROM_HERE, |
| 72 base::Bind(&AudioDebugRecordingsHandler::GetLogDirectoryAndEnsureExists, |
| 73 this), |
| 74 base::Bind(&AudioDebugRecordingsHandler::DoStopAudioDebugRecordings, this, |
| 75 host, is_manual_stop, current_audio_debug_recordings_id_, |
| 76 callback, error_callback)); |
| 77 } |
| 78 |
| 79 base::FilePath AudioDebugRecordingsHandler::GetLogDirectoryAndEnsureExists() { |
| 80 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 81 base::FilePath log_dir_path = |
| 82 WebRtcLogList::GetWebRtcLogDirectoryForProfile(profile_->GetPath()); |
| 83 base::File::Error error; |
| 84 if (!base::CreateDirectoryAndGetError(log_dir_path, &error)) { |
| 85 DLOG(ERROR) << "Could not create WebRTC log directory, error: " << error; |
| 86 return base::FilePath(); |
| 87 } |
| 88 return log_dir_path; |
| 89 } |
| 90 |
| 91 void AudioDebugRecordingsHandler::DoStartAudioDebugRecordings( |
| 92 content::RenderProcessHost* host, |
| 93 base::TimeDelta delay, |
| 94 const RecordingDoneCallback& callback, |
| 95 const RecordingErrorCallback& error_callback, |
| 96 const base::FilePath& log_directory) { |
| 97 DCHECK(thread_checker_.CalledOnValidThread()); |
| 98 |
| 99 if (is_audio_debug_recordings_in_progress_) { |
| 100 error_callback.Run("Audio debug recordings already in progress"); |
| 101 return; |
| 102 } |
| 103 |
| 104 is_audio_debug_recordings_in_progress_ = true; |
| 105 base::FilePath prefix_path = GetAudioDebugRecordingsPrefixPath( |
| 106 log_directory, ++current_audio_debug_recordings_id_); |
| 107 host->EnableAudioDebugRecordings(prefix_path); |
| 108 |
| 109 if (delay.is_zero()) { |
| 110 const bool is_stopped = false, is_manual_stop = false; |
| 111 callback.Run(prefix_path.AsUTF8Unsafe(), is_stopped, is_manual_stop); |
| 112 return; |
| 113 } |
| 114 |
| 115 const bool is_manual_stop = false; |
| 116 BrowserThread::PostDelayedTask( |
| 117 BrowserThread::UI, FROM_HERE, |
| 118 base::Bind(&AudioDebugRecordingsHandler::DoStopAudioDebugRecordings, this, |
| 119 host, is_manual_stop, current_audio_debug_recordings_id_, |
| 120 callback, error_callback, log_directory), |
| 121 delay); |
| 122 } |
| 123 |
| 124 void AudioDebugRecordingsHandler::DoStopAudioDebugRecordings( |
| 125 content::RenderProcessHost* host, |
| 126 bool is_manual_stop, |
| 127 uint64_t audio_debug_recordings_id, |
| 128 const RecordingDoneCallback& callback, |
| 129 const RecordingErrorCallback& error_callback, |
| 130 const base::FilePath& log_directory) { |
| 131 DCHECK(thread_checker_.CalledOnValidThread()); |
| 132 DCHECK_LE(audio_debug_recordings_id, current_audio_debug_recordings_id_); |
| 133 |
| 134 base::FilePath prefix_path = GetAudioDebugRecordingsPrefixPath( |
| 135 log_directory, audio_debug_recordings_id); |
| 136 // Prevent an old posted StopAudioDebugRecordings() call to stop a newer dump. |
| 137 // This could happen in a sequence like: |
| 138 // Start(10); // Start dump 1. Post Stop() to run after 10 seconds. |
| 139 // Stop(); // Manually stop dump 1 before 10 seconds; |
| 140 // Start(20); // Start dump 2. Posted Stop() for 1 should not stop dump 2. |
| 141 if (audio_debug_recordings_id < current_audio_debug_recordings_id_) { |
| 142 const bool is_stopped = false; |
| 143 callback.Run(prefix_path.AsUTF8Unsafe(), is_stopped, is_manual_stop); |
| 144 return; |
| 145 } |
| 146 |
| 147 if (!is_audio_debug_recordings_in_progress_) { |
| 148 error_callback.Run("No audio debug recording in progress"); |
| 149 return; |
| 150 } |
| 151 |
| 152 host->DisableAudioDebugRecordings(); |
| 153 is_audio_debug_recordings_in_progress_ = false; |
| 154 const bool is_stopped = true; |
| 155 callback.Run(prefix_path.AsUTF8Unsafe(), is_stopped, is_manual_stop); |
| 156 } |
OLD | NEW |