Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
mcasas | 6b8c0c8 | 2016-07-29 23:04:35 | [diff] [blame] | 5 | #include "content/browser/webrtc/webrtc_internals.h" |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 6 | |
avi | 7f27756 | 2015-12-25 02:41:26 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
dcheng | 031a8f8 | 2016-09-08 21:04:33 | [diff] [blame] | 9 | #include <memory> |
| 10 | #include <utility> |
| 11 | |
Elad Alon | c0928bf1 | 2018-02-28 19:35:17 | [diff] [blame] | 12 | #include "base/command_line.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 13 | #include "base/functional/bind.h" |
David Sanders | d4bf5eb | 2022-03-17 07:12:05 | [diff] [blame] | 14 | #include "base/observer_list.h" |
[email protected] | 84e83b3 | 2014-07-01 23:10:15 | [diff] [blame] | 15 | #include "base/strings/string_number_conversions.h" |
avi | 7f27756 | 2015-12-25 02:41:26 | [diff] [blame] | 16 | #include "build/build_config.h" |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 17 | #include "content/browser/web_contents/web_contents_view.h" |
Tarun Bansal | f153d683 | 2019-06-17 18:18:05 | [diff] [blame] | 18 | #include "content/browser/webrtc/webrtc_internals_connections_observer.h" |
mcasas | 6b8c0c8 | 2016-07-29 23:04:35 | [diff] [blame] | 19 | #include "content/browser/webrtc/webrtc_internals_ui_observer.h" |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 20 | #include "content/public/browser/audio_service.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 21 | #include "content/public/browser/browser_task_traits.h" |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 22 | #include "content/public/browser/browser_thread.h" |
[email protected] | 820b9b26 | 2014-05-09 14:47:16 | [diff] [blame] | 23 | #include "content/public/browser/content_browser_client.h" |
Ken Rockot | 44d73be | 2019-12-12 22:25:59 | [diff] [blame] | 24 | #include "content/public/browser/device_service.h" |
David Sanders | 2ee1f350 | 2022-03-16 07:21:19 | [diff] [blame] | 25 | #include "content/public/browser/render_process_host.h" |
[email protected] | bf3657b | 2013-12-17 12:36:17 | [diff] [blame] | 26 | #include "content/public/browser/web_contents.h" |
Elad Alon | 12ecdf0 | 2018-03-14 12:07:31 | [diff] [blame] | 27 | #include "content/public/browser/webrtc_event_logger.h" |
Hans Wennborg | 5ffd139 | 2019-10-16 11:00:02 | [diff] [blame] | 28 | #include "content/public/common/content_client.h" |
Elad Alon | c0928bf1 | 2018-02-28 19:35:17 | [diff] [blame] | 29 | #include "content/public/common/content_switches.h" |
Marina Ciocea | 741a4fb1 | 2018-01-27 09:55:12 | [diff] [blame] | 30 | #include "media/audio/audio_debug_recording_session.h" |
grunell | 086a411 | 2017-03-02 09:23:04 | [diff] [blame] | 31 | #include "media/audio/audio_manager.h" |
Scott Violet | a35f9a4 | 2018-03-22 22:00:44 | [diff] [blame] | 32 | #include "media/media_buildflags.h" |
Miyoung Shin | ff87666 | 2019-09-05 17:52:04 | [diff] [blame] | 33 | #include "mojo/public/cpp/bindings/pending_receiver.h" |
Marina Ciocea | c6d46d2f | 2018-02-26 15:21:28 | [diff] [blame] | 34 | #include "services/audio/public/cpp/debug_recording_session_factory.h" |
Ke He | 31d0bb0 | 2018-02-24 07:16:24 | [diff] [blame] | 35 | #include "services/device/public/mojom/wake_lock_provider.mojom.h" |
Brett Wilson | 804e83c | 2017-08-18 22:57:33 | [diff] [blame] | 36 | #include "ui/shell_dialogs/select_file_policy.h" |
Avi Drissman | d0b88a2b | 2023-12-28 22:49:38 | [diff] [blame] | 37 | #include "ui/shell_dialogs/selected_file_info.h" |
ivoc | cf0887d | 2016-07-07 18:23:53 | [diff] [blame] | 38 | |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 39 | using base::ProcessId; |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 40 | using std::string; |
| 41 | |
[email protected] | 64ccac3 | 2013-01-29 21:40:04 | [diff] [blame] | 42 | namespace content { |
| 43 | |
| 44 | namespace { |
marshall | 8ad2742 | 2014-09-19 18:53:00 | [diff] [blame] | 45 | |
Elad Alon | c0928bf1 | 2018-02-28 19:35:17 | [diff] [blame] | 46 | const base::FilePath::CharType kEventLogFilename[] = |
| 47 | FILE_PATH_LITERAL("event_log"); |
| 48 | |
Philipp Hancke | 89add41 | 2022-11-18 14:07:07 | [diff] [blame] | 49 | constexpr char kGetUserMedia[] = "getUserMedia"; |
| 50 | constexpr char kGetDisplayMedia[] = "getDisplayMedia"; |
| 51 | |
Guido Urdaneta | 645fc98 | 2018-02-20 19:46:03 | [diff] [blame] | 52 | // This is intended to limit DoS attacks against the browser process consisting |
Philipp Hancke | 89add41 | 2022-11-18 14:07:07 | [diff] [blame] | 53 | // of many getUserMedia()/getDisplayMedia() calls. See https://crbug.com/804440. |
| 54 | const size_t kMaxMediaEntries = 1000; |
Guido Urdaneta | 645fc98 | 2018-02-20 19:46:03 | [diff] [blame] | 55 | |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 56 | // Makes sure that |dict| has a List under path "log". |
| 57 | base::Value::List& EnsureLogList(base::Value::Dict& dict) { |
| 58 | base::Value::List* log = dict.FindList("log"); |
| 59 | if (log) |
| 60 | return *log; |
| 61 | return dict.Set("log", base::Value::List())->GetList(); |
[email protected] | c054f09 | 2013-01-27 08:12:49 | [diff] [blame] | 62 | } |
| 63 | |
tommi | 15e9cea | 2017-03-06 13:56:28 | [diff] [blame] | 64 | // Removes the log entry associated with a given record. |
| 65 | void FreeLogList(base::Value* value) { |
jdoerrie | e48b26a | 2017-12-09 14:19:08 | [diff] [blame] | 66 | DCHECK(value->is_dict()); |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 67 | value->GetDict().Remove("log"); |
tommi | 15e9cea | 2017-03-06 13:56:28 | [diff] [blame] | 68 | } |
| 69 | |
[email protected] | 64ccac3 | 2013-01-29 21:40:04 | [diff] [blame] | 70 | } // namespace |
| 71 | |
Elad Alon | a619d556 | 2018-01-13 01:30:15 | [diff] [blame] | 72 | WebRTCInternals* WebRTCInternals::g_webrtc_internals = nullptr; |
| 73 | |
Maks Orlovich | 442f378 | 2021-06-04 18:05:09 | [diff] [blame] | 74 | WebRTCInternals::PendingUpdate::PendingUpdate(const std::string& event_name, |
| 75 | base::Value event_data) |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 76 | : event_name_(event_name), event_data_(std::move(event_data)) {} |
tommi | 75d54fe84 | 2016-02-09 14:56:30 | [diff] [blame] | 77 | |
| 78 | WebRTCInternals::PendingUpdate::PendingUpdate(PendingUpdate&& other) |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 79 | : event_name_(other.event_name_), |
| 80 | event_data_(std::move(other.event_data_)) {} |
tommi | 75d54fe84 | 2016-02-09 14:56:30 | [diff] [blame] | 81 | |
| 82 | WebRTCInternals::PendingUpdate::~PendingUpdate() { |
| 83 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 84 | } |
| 85 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 86 | const std::string& WebRTCInternals::PendingUpdate::event_name() const { |
tommi | 75d54fe84 | 2016-02-09 14:56:30 | [diff] [blame] | 87 | DCHECK(thread_checker_.CalledOnValidThread()); |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 88 | return event_name_; |
tommi | 75d54fe84 | 2016-02-09 14:56:30 | [diff] [blame] | 89 | } |
| 90 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 91 | const base::Value* WebRTCInternals::PendingUpdate::event_data() const { |
tommi | 75d54fe84 | 2016-02-09 14:56:30 | [diff] [blame] | 92 | DCHECK(thread_checker_.CalledOnValidThread()); |
Maks Orlovich | 442f378 | 2021-06-04 18:05:09 | [diff] [blame] | 93 | return event_data_.is_none() ? nullptr : &event_data_; |
tommi | 75d54fe84 | 2016-02-09 14:56:30 | [diff] [blame] | 94 | } |
| 95 | |
Elad Alon | 67ac27bc4 | 2022-05-03 15:30:26 | [diff] [blame] | 96 | WebRTCInternals::WebRTCInternals() : WebRTCInternals(500, true) { |
| 97 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 98 | } |
tommi | 75d54fe84 | 2016-02-09 14:56:30 | [diff] [blame] | 99 | |
maxmorin | c369574d | 2016-07-25 15:28:17 | [diff] [blame] | 100 | WebRTCInternals::WebRTCInternals(int aggregate_updates_ms, |
| 101 | bool should_block_power_saving) |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 102 | : peer_connection_data_(base::Value::List()), |
| 103 | selection_type_(SelectionType::kAudioDebugRecordings), |
Elad Alon | c0928bf1 | 2018-02-28 19:35:17 | [diff] [blame] | 104 | command_line_derived_logging_path_( |
| 105 | base::CommandLine::ForCurrentProcess()->GetSwitchValuePath( |
| 106 | switches::kWebRtcLocalEventLogging)), |
ivoc | add54f0d | 2015-12-18 23:17:05 | [diff] [blame] | 107 | event_log_recordings_(false), |
Guido Urdaneta | 0bc103a | 2018-11-16 19:54:33 | [diff] [blame] | 108 | num_connected_connections_(0), |
maxmorin | c369574d | 2016-07-25 15:28:17 | [diff] [blame] | 109 | should_block_power_saving_(should_block_power_saving), |
Jeremy Roman | 3bca4bf | 2019-07-11 03:41:25 | [diff] [blame] | 110 | aggregate_updates_ms_(aggregate_updates_ms) { |
Elad Alon | c0928bf1 | 2018-02-28 19:35:17 | [diff] [blame] | 111 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Elad Alon | a619d556 | 2018-01-13 01:30:15 | [diff] [blame] | 112 | DCHECK(!g_webrtc_internals); |
| 113 | |
Philip Eliasson | 9b5072d4 | 2025-05-13 13:13:49 | [diff] [blame] | 114 | const base::FilePath default_path = |
[email protected] | 820b9b26 | 2014-05-09 14:47:16 | [diff] [blame] | 115 | GetContentClient()->browser()->GetDefaultDownloadDirectory(); |
Philip Eliasson | 9b5072d4 | 2025-05-13 13:13:49 | [diff] [blame] | 116 | audio_debug_recordings_file_path_ = default_path; |
| 117 | event_log_recordings_file_path_ = default_path; |
| 118 | data_channel_recordings_file_path_ = default_path; |
ivoc | add54f0d | 2015-12-18 23:17:05 | [diff] [blame] | 119 | |
Philip Eliasson | 9b5072d4 | 2025-05-13 13:13:49 | [diff] [blame] | 120 | if (!default_path.empty()) { |
grunell | 7566ffc | 2015-09-07 07:39:44 | [diff] [blame] | 121 | audio_debug_recordings_file_path_ = |
| 122 | audio_debug_recordings_file_path_.Append( |
| 123 | FILE_PATH_LITERAL("audio_debug")); |
ivoc | add54f0d | 2015-12-18 23:17:05 | [diff] [blame] | 124 | event_log_recordings_file_path_ = |
Elad Alon | c0928bf1 | 2018-02-28 19:35:17 | [diff] [blame] | 125 | event_log_recordings_file_path_.Append(kEventLogFilename); |
Philip Eliasson | 9b5072d4 | 2025-05-13 13:13:49 | [diff] [blame] | 126 | data_channel_recordings_file_path_ = |
| 127 | data_channel_recordings_file_path_.Append( |
| 128 | FILE_PATH_LITERAL("data_channel")); |
Elad Alon | c0928bf1 | 2018-02-28 19:35:17 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // Allow command-line based setting of (local) WebRTC event logging. |
| 132 | if (!command_line_derived_logging_path_.empty()) { |
| 133 | const base::FilePath local_logs_path = |
| 134 | command_line_derived_logging_path_.Append(kEventLogFilename); |
Elad Alon | 12ecdf0 | 2018-03-14 12:07:31 | [diff] [blame] | 135 | WebRtcEventLogger* const logger = WebRtcEventLogger::Get(); |
| 136 | if (logger) { |
Patrick Monette | 2169e67 | 2021-03-30 15:54:22 | [diff] [blame] | 137 | logger->EnableLocalLogging(local_logs_path); |
Elad Alon | 12ecdf0 | 2018-03-14 12:07:31 | [diff] [blame] | 138 | } |
Elad Alon | c0928bf1 | 2018-02-28 19:35:17 | [diff] [blame] | 139 | // For clarity's sake, though these aren't supposed to be regarded now: |
| 140 | event_log_recordings_ = true; |
| 141 | event_log_recordings_file_path_.clear(); |
[email protected] | 820b9b26 | 2014-05-09 14:47:16 | [diff] [blame] | 142 | } |
Elad Alon | a619d556 | 2018-01-13 01:30:15 | [diff] [blame] | 143 | |
| 144 | g_webrtc_internals = this; |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 145 | } |
| 146 | |
[email protected] | 64ccac3 | 2013-01-29 21:40:04 | [diff] [blame] | 147 | WebRTCInternals::~WebRTCInternals() { |
Elad Alon | 67ac27bc4 | 2022-05-03 15:30:26 | [diff] [blame] | 148 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Elad Alon | a619d556 | 2018-01-13 01:30:15 | [diff] [blame] | 149 | DCHECK(g_webrtc_internals); |
| 150 | g_webrtc_internals = nullptr; |
Lei Zhang | ad220ec | 2023-09-28 18:28:55 | [diff] [blame] | 151 | |
| 152 | if (select_file_dialog_) { |
| 153 | select_file_dialog_->ListenerDestroyed(); |
| 154 | } |
Elad Alon | a619d556 | 2018-01-13 01:30:15 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | WebRTCInternals* WebRTCInternals::CreateSingletonInstance() { |
Elad Alon | 67ac27bc4 | 2022-05-03 15:30:26 | [diff] [blame] | 158 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Elad Alon | a619d556 | 2018-01-13 01:30:15 | [diff] [blame] | 159 | DCHECK(!g_webrtc_internals); |
| 160 | g_webrtc_internals = new WebRTCInternals; |
| 161 | return g_webrtc_internals; |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 162 | } |
| 163 | |
[email protected] | 64ccac3 | 2013-01-29 21:40:04 | [diff] [blame] | 164 | WebRTCInternals* WebRTCInternals::GetInstance() { |
Alison Gale | 47d1537d | 2024-04-19 21:31:46 | [diff] [blame] | 165 | // TODO(crbug.com/40837773): DCHECK calling from UI thread. |
Elad Alon | 67ac27bc4 | 2022-05-03 15:30:26 | [diff] [blame] | 166 | // Currently, some unit tests call this from outside of the UI thread, |
| 167 | // but that's not a real issue as these tests neglect setting |
| 168 | // `g_webrtc_internals` to begin with, and therefore just ignore it. |
| 169 | DCHECK(!g_webrtc_internals || BrowserThread::CurrentlyOn(BrowserThread::UI)); |
Elad Alon | a619d556 | 2018-01-13 01:30:15 | [diff] [blame] | 170 | return g_webrtc_internals; |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 171 | } |
| 172 | |
Alexander Timin | 8690530c | 2021-06-19 00:34:32 | [diff] [blame] | 173 | void WebRTCInternals::OnPeerConnectionAdded(GlobalRenderFrameHostId frame_id, |
Patrick Monette | 948b473 | 2021-03-30 10:00:30 | [diff] [blame] | 174 | int lid, |
Patrick Monette | ce1ee72 | 2021-03-30 18:41:05 | [diff] [blame] | 175 | ProcessId pid, |
Patrick Monette | 948b473 | 2021-03-30 10:00:30 | [diff] [blame] | 176 | const string& url, |
Harald Alvestrand | 39468ab4 | 2024-02-08 18:47:23 | [diff] [blame] | 177 | const string& rtc_configuration) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 178 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 179 | |
tommi | 15e9cea | 2017-03-06 13:56:28 | [diff] [blame] | 180 | // TODO(tommi): Consider changing this design so that webrtc-internals has |
| 181 | // minimal impact if chrome://webrtc-internals isn't open. |
| 182 | |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 183 | base::Value::Dict dict; |
| 184 | dict.Set("rid", frame_id.child_id); |
| 185 | dict.Set("lid", lid); |
| 186 | dict.Set("pid", static_cast<int>(pid)); |
| 187 | dict.Set("rtcConfiguration", rtc_configuration); |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 188 | dict.Set("url", url); |
| 189 | dict.Set("isOpen", true); |
| 190 | dict.Set("connected", false); |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 191 | |
Mitsuru Oshima | 69031e6 | 2021-01-20 18:01:58 | [diff] [blame] | 192 | if (!observers_.empty()) |
Maks Orlovich | 442f378 | 2021-06-04 18:05:09 | [diff] [blame] | 193 | SendUpdate("add-peer-connection", dict.Clone()); |
amistry | 163a4a3 | 2015-05-25 23:48:15 | [diff] [blame] | 194 | |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 195 | peer_connection_data().Append(std::move(dict)); |
dcheng | 031a8f8 | 2016-09-08 21:04:33 | [diff] [blame] | 196 | |
Patrick Monette | ce1ee72 | 2021-03-30 18:41:05 | [diff] [blame] | 197 | if (render_process_id_set_.insert(frame_id.child_id).second) { |
| 198 | RenderProcessHost* host = RenderProcessHost::FromID(frame_id.child_id); |
amistry | 163a4a3 | 2015-05-25 23:48:15 | [diff] [blame] | 199 | if (host) |
| 200 | host->AddObserver(this); |
| 201 | } |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 202 | } |
| 203 | |
Alexander Timin | 8690530c | 2021-06-19 00:34:32 | [diff] [blame] | 204 | void WebRTCInternals::OnPeerConnectionRemoved(GlobalRenderFrameHostId frame_id, |
Patrick Monette | ce1ee72 | 2021-03-30 18:41:05 | [diff] [blame] | 205 | int lid) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 206 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 207 | |
Maks Orlovich | 442f378 | 2021-06-04 18:05:09 | [diff] [blame] | 208 | auto it = FindRecord(frame_id, lid); |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 209 | if (it != peer_connection_data().end()) { |
Elaine Chien | 5df4237b | 2022-01-07 18:51:39 | [diff] [blame] | 210 | MaybeClosePeerConnection(*it); |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 211 | peer_connection_data().erase(it); |
tommi | 15e9cea | 2017-03-06 13:56:28 | [diff] [blame] | 212 | } |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 213 | |
Mitsuru Oshima | 69031e6 | 2021-01-20 18:01:58 | [diff] [blame] | 214 | if (!observers_.empty()) { |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 215 | base::Value::Dict id; |
| 216 | id.Set("rid", frame_id.child_id); |
| 217 | id.Set("lid", lid); |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 218 | SendUpdate("remove-peer-connection", std::move(id)); |
[email protected] | c054f09 | 2013-01-27 08:12:49 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Alexander Timin | 8690530c | 2021-06-19 00:34:32 | [diff] [blame] | 222 | void WebRTCInternals::OnPeerConnectionUpdated(GlobalRenderFrameHostId frame_id, |
Patrick Monette | 948b473 | 2021-03-30 10:00:30 | [diff] [blame] | 223 | int lid, |
| 224 | const string& type, |
| 225 | const string& value) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 226 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | c054f09 | 2013-01-27 08:12:49 | [diff] [blame] | 227 | |
Maks Orlovich | 442f378 | 2021-06-04 18:05:09 | [diff] [blame] | 228 | auto it = FindRecord(frame_id, lid); |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 229 | if (it == peer_connection_data().end()) |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 230 | return; |
tommi | 15e9cea | 2017-03-06 13:56:28 | [diff] [blame] | 231 | |
Philipp Hancke | 151dc55 | 2021-11-04 15:18:04 | [diff] [blame] | 232 | if (type == "iceconnectionstatechange") { |
Guido Urdaneta | 0bc103a | 2018-11-16 19:54:33 | [diff] [blame] | 233 | if (value == "connected" || value == "checking" || value == "completed") { |
Elaine Chien | 5df4237b | 2022-01-07 18:51:39 | [diff] [blame] | 234 | MaybeMarkPeerConnectionAsConnected(*it); |
Guido Urdaneta | 0bc103a | 2018-11-16 19:54:33 | [diff] [blame] | 235 | } else if (value == "failed" || value == "disconnected" || |
| 236 | value == "closed" || value == "new") { |
Elaine Chien | 5df4237b | 2022-01-07 18:51:39 | [diff] [blame] | 237 | MaybeMarkPeerConnectionAsNotConnected(*it); |
Guido Urdaneta | 0bc103a | 2018-11-16 19:54:33 | [diff] [blame] | 238 | } |
Philipp Hancke | 951e042e | 2021-11-01 13:44:18 | [diff] [blame] | 239 | } else if (type == "close") { |
Elaine Chien | 5df4237b | 2022-01-07 18:51:39 | [diff] [blame] | 240 | MaybeClosePeerConnection(*it); |
Philipp Hancke | 1a45627 | 2021-03-24 12:10:55 | [diff] [blame] | 241 | } else if (type == "setConfiguration") { |
| 242 | // Update the configuration we have for this connection. |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 243 | it->GetDict().Set("rtcConfiguration", value); |
Guido Urdaneta | 0bc103a | 2018-11-16 19:54:33 | [diff] [blame] | 244 | } |
tommi | 15e9cea | 2017-03-06 13:56:28 | [diff] [blame] | 245 | |
| 246 | // Don't update entries if there aren't any observers. |
Mitsuru Oshima | 69031e6 | 2021-01-20 18:01:58 | [diff] [blame] | 247 | if (observers_.empty()) |
tommi | 15e9cea | 2017-03-06 13:56:28 | [diff] [blame] | 248 | return; |
| 249 | |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 250 | base::Value::Dict log_entry; |
tommi | 15e9cea | 2017-03-06 13:56:28 | [diff] [blame] | 251 | |
Peter Kasting | 08b91b4 | 2023-10-21 03:46:09 | [diff] [blame] | 252 | double epoch_time = base::Time::Now().InMillisecondsFSinceUnixEpoch(); |
Brett Wilson | 5ed06e7 | 2017-12-01 01:25:11 | [diff] [blame] | 253 | string time = base::NumberToString(epoch_time); |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 254 | log_entry.Set("time", time); |
| 255 | log_entry.Set("type", type); |
| 256 | log_entry.Set("value", value); |
tommi | 15e9cea | 2017-03-06 13:56:28 | [diff] [blame] | 257 | |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 258 | base::Value::Dict update; |
| 259 | update.Set("rid", frame_id.child_id); |
| 260 | update.Set("lid", lid); |
| 261 | update.Merge(log_entry.Clone()); |
tommi | 15e9cea | 2017-03-06 13:56:28 | [diff] [blame] | 262 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 263 | SendUpdate("update-peer-connection", std::move(update)); |
tommi | 15e9cea | 2017-03-06 13:56:28 | [diff] [blame] | 264 | |
| 265 | // Append the update to the end of the log. |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 266 | EnsureLogList(it->GetDict()).Append(std::move(log_entry)); |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 267 | } |
| 268 | |
Alexander Timin | 8690530c | 2021-06-19 00:34:32 | [diff] [blame] | 269 | void WebRTCInternals::OnAddStandardStats(GlobalRenderFrameHostId frame_id, |
Henrik Boström | 33ece3e9 | 2019-04-23 10:08:53 | [diff] [blame] | 270 | int lid, |
Fabrice de Gans | 432f4de | 2022-04-04 17:47:41 | [diff] [blame] | 271 | base::Value::List value) { |
Elad Alon | 67ac27bc4 | 2022-05-03 15:30:26 | [diff] [blame] | 272 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 273 | |
Mitsuru Oshima | 69031e6 | 2021-01-20 18:01:58 | [diff] [blame] | 274 | if (observers_.empty()) |
[email protected] | 6efd5f2 | 2013-02-06 19:20:37 | [diff] [blame] | 275 | return; |
| 276 | |
Fabrice de Gans | 432f4de | 2022-04-04 17:47:41 | [diff] [blame] | 277 | base::Value::Dict dict; |
| 278 | dict.Set("rid", frame_id.child_id); |
| 279 | dict.Set("lid", lid); |
[email protected] | 6efd5f2 | 2013-02-06 19:20:37 | [diff] [blame] | 280 | |
Fabrice de Gans | 432f4de | 2022-04-04 17:47:41 | [diff] [blame] | 281 | dict.Set("reports", std::move(value)); |
[email protected] | 6efd5f2 | 2013-02-06 19:20:37 | [diff] [blame] | 282 | |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 283 | SendUpdate("add-standard-stats", std::move(dict)); |
Henrik Boström | 33ece3e9 | 2019-04-23 10:08:53 | [diff] [blame] | 284 | } |
| 285 | |
Philipp Hancke | 89add41 | 2022-11-18 14:07:07 | [diff] [blame] | 286 | void WebRTCInternals::OnGetMedia(const std::string& request_type, |
| 287 | GlobalRenderFrameHostId frame_id, |
| 288 | base::ProcessId pid, |
| 289 | int request_id, |
| 290 | bool audio, |
| 291 | bool video, |
| 292 | const std::string& audio_constraints, |
| 293 | const std::string& video_constraints) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 294 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | a0437b20 | 2014-01-02 22:45:51 | [diff] [blame] | 295 | |
Philipp Hancke | 89add41 | 2022-11-18 14:07:07 | [diff] [blame] | 296 | if (get_user_media_requests_.size() >= kMaxMediaEntries) { |
| 297 | LOG(WARNING) << "Maximum number of tracked getUserMedia/getDisplayMedia " |
[email protected] | deb232b | 2022-11-28 11:22:16 | [diff] [blame] | 298 | "requests reached in webrtc-internals."; |
Guido Urdaneta | 645fc98 | 2018-02-20 19:46:03 | [diff] [blame] | 299 | return; |
| 300 | } |
| 301 | |
Peter Kasting | a06ccf71 | 2022-08-24 07:43:12 | [diff] [blame] | 302 | RenderFrameHost* rfh = RenderFrameHost::FromID(frame_id); |
Philipp Hancke | db92acef | 2021-12-01 09:57:43 | [diff] [blame] | 303 | // Frame may be gone (and does not exist in tests). |
Peter Kasting | a06ccf71 | 2022-08-24 07:43:12 | [diff] [blame] | 304 | std::string origin = rfh ? rfh->GetLastCommittedOrigin().Serialize() : ""; |
Philipp Hancke | db92acef | 2021-12-01 09:57:43 | [diff] [blame] | 305 | |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 306 | base::Value::Dict dict; |
| 307 | dict.Set("rid", frame_id.child_id); |
| 308 | dict.Set("pid", static_cast<int>(pid)); |
| 309 | dict.Set("request_id", request_id); |
Philipp Hancke | 89add41 | 2022-11-18 14:07:07 | [diff] [blame] | 310 | dict.Set("request_type", request_type); |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 311 | dict.Set("origin", origin); |
Peter Kasting | 08b91b4 | 2023-10-21 03:46:09 | [diff] [blame] | 312 | dict.Set("timestamp", base::Time::Now().InMillisecondsFSinceUnixEpoch()); |
[email protected] | a0437b20 | 2014-01-02 22:45:51 | [diff] [blame] | 313 | if (audio) |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 314 | dict.Set("audio", audio_constraints); |
[email protected] | a0437b20 | 2014-01-02 22:45:51 | [diff] [blame] | 315 | if (video) |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 316 | dict.Set("video", video_constraints); |
[email protected] | a0437b20 | 2014-01-02 22:45:51 | [diff] [blame] | 317 | |
Mitsuru Oshima | 69031e6 | 2021-01-20 18:01:58 | [diff] [blame] | 318 | if (!observers_.empty()) |
[email protected] | deb232b | 2022-11-28 11:22:16 | [diff] [blame] | 319 | SendUpdate("add-media", dict.Clone()); |
amistry | 163a4a3 | 2015-05-25 23:48:15 | [diff] [blame] | 320 | |
dcheng | 031a8f8 | 2016-09-08 21:04:33 | [diff] [blame] | 321 | get_user_media_requests_.Append(std::move(dict)); |
| 322 | |
Patrick Monette | ce1ee72 | 2021-03-30 18:41:05 | [diff] [blame] | 323 | if (render_process_id_set_.insert(frame_id.child_id).second) { |
Peter Kasting | a06ccf71 | 2022-08-24 07:43:12 | [diff] [blame] | 324 | RenderProcessHost* rph = RenderProcessHost::FromID(frame_id.child_id); |
| 325 | if (rph) |
| 326 | rph->AddObserver(this); |
amistry | 163a4a3 | 2015-05-25 23:48:15 | [diff] [blame] | 327 | } |
[email protected] | a0437b20 | 2014-01-02 22:45:51 | [diff] [blame] | 328 | } |
Philipp Hancke | 17347d08 | 2021-11-24 06:51:26 | [diff] [blame] | 329 | |
Philipp Hancke | 89add41 | 2022-11-18 14:07:07 | [diff] [blame] | 330 | void WebRTCInternals::OnGetMediaSuccess(const std::string& request_type, |
| 331 | GlobalRenderFrameHostId frame_id, |
| 332 | base::ProcessId pid, |
| 333 | int request_id, |
| 334 | const std::string& stream_id, |
| 335 | const std::string& audio_track_info, |
| 336 | const std::string& video_track_info) { |
Philipp Hancke | d013ad9 | 2021-11-22 22:23:08 | [diff] [blame] | 337 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 338 | |
Philipp Hancke | 89add41 | 2022-11-18 14:07:07 | [diff] [blame] | 339 | if (get_user_media_requests_.size() >= kMaxMediaEntries) { |
| 340 | LOG(WARNING) << "Maximum number of tracked getUserMedia/getDisplayMedia " |
[email protected] | deb232b | 2022-11-28 11:22:16 | [diff] [blame] | 341 | "requests reached in webrtc-internals."; |
Philipp Hancke | d013ad9 | 2021-11-22 22:23:08 | [diff] [blame] | 342 | return; |
| 343 | } |
| 344 | |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 345 | base::Value::Dict dict; |
| 346 | dict.Set("rid", frame_id.child_id); |
| 347 | dict.Set("pid", static_cast<int>(pid)); |
| 348 | dict.Set("request_id", request_id); |
Philipp Hancke | 89add41 | 2022-11-18 14:07:07 | [diff] [blame] | 349 | dict.Set("request_type", request_type); |
Peter Kasting | 08b91b4 | 2023-10-21 03:46:09 | [diff] [blame] | 350 | dict.Set("timestamp", base::Time::Now().InMillisecondsFSinceUnixEpoch()); |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 351 | dict.Set("stream_id", stream_id); |
Philipp Hancke | d013ad9 | 2021-11-22 22:23:08 | [diff] [blame] | 352 | if (!audio_track_info.empty()) |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 353 | dict.Set("audio_track_info", audio_track_info); |
Philipp Hancke | d013ad9 | 2021-11-22 22:23:08 | [diff] [blame] | 354 | if (!video_track_info.empty()) |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 355 | dict.Set("video_track_info", video_track_info); |
Philipp Hancke | d013ad9 | 2021-11-22 22:23:08 | [diff] [blame] | 356 | |
| 357 | if (!observers_.empty()) |
[email protected] | deb232b | 2022-11-28 11:22:16 | [diff] [blame] | 358 | SendUpdate("update-media", dict.Clone()); |
Philipp Hancke | d013ad9 | 2021-11-22 22:23:08 | [diff] [blame] | 359 | |
| 360 | get_user_media_requests_.Append(std::move(dict)); |
| 361 | |
| 362 | if (render_process_id_set_.insert(frame_id.child_id).second) { |
| 363 | RenderProcessHost* host = RenderProcessHost::FromID(frame_id.child_id); |
| 364 | if (host) |
| 365 | host->AddObserver(this); |
| 366 | } |
| 367 | } |
[email protected] | a0437b20 | 2014-01-02 22:45:51 | [diff] [blame] | 368 | |
Philipp Hancke | 89add41 | 2022-11-18 14:07:07 | [diff] [blame] | 369 | void WebRTCInternals::OnGetMediaFailure(const std::string& request_type, |
| 370 | GlobalRenderFrameHostId frame_id, |
| 371 | base::ProcessId pid, |
| 372 | int request_id, |
| 373 | const std::string& error, |
| 374 | const std::string& error_message) { |
Philipp Hancke | 17347d08 | 2021-11-24 06:51:26 | [diff] [blame] | 375 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 376 | |
Philipp Hancke | 89add41 | 2022-11-18 14:07:07 | [diff] [blame] | 377 | if (get_user_media_requests_.size() >= kMaxMediaEntries) { |
[email protected] | deb232b | 2022-11-28 11:22:16 | [diff] [blame] | 378 | LOG(WARNING) << "Maximum number of tracked /getDisplayMedia " |
| 379 | "requests reached in webrtc-internals."; |
Philipp Hancke | 17347d08 | 2021-11-24 06:51:26 | [diff] [blame] | 380 | return; |
| 381 | } |
| 382 | |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 383 | base::Value::Dict dict; |
| 384 | dict.Set("rid", frame_id.child_id); |
| 385 | dict.Set("pid", static_cast<int>(pid)); |
| 386 | dict.Set("request_id", request_id); |
Philipp Hancke | 89add41 | 2022-11-18 14:07:07 | [diff] [blame] | 387 | dict.Set("request_type", request_type); |
Peter Kasting | 08b91b4 | 2023-10-21 03:46:09 | [diff] [blame] | 388 | dict.Set("timestamp", base::Time::Now().InMillisecondsFSinceUnixEpoch()); |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 389 | dict.Set("error", error); |
| 390 | dict.Set("error_message", error_message); |
Philipp Hancke | 17347d08 | 2021-11-24 06:51:26 | [diff] [blame] | 391 | |
| 392 | if (!observers_.empty()) |
[email protected] | deb232b | 2022-11-28 11:22:16 | [diff] [blame] | 393 | SendUpdate("update-media", dict.Clone()); |
Philipp Hancke | 17347d08 | 2021-11-24 06:51:26 | [diff] [blame] | 394 | |
| 395 | get_user_media_requests_.Append(std::move(dict)); |
| 396 | |
| 397 | if (render_process_id_set_.insert(frame_id.child_id).second) { |
| 398 | RenderProcessHost* host = RenderProcessHost::FromID(frame_id.child_id); |
| 399 | if (host) |
| 400 | host->AddObserver(this); |
| 401 | } |
| 402 | } |
| 403 | |
Philipp Hancke | 89add41 | 2022-11-18 14:07:07 | [diff] [blame] | 404 | void WebRTCInternals::OnGetUserMedia(GlobalRenderFrameHostId frame_id, |
| 405 | base::ProcessId pid, |
| 406 | int request_id, |
| 407 | bool audio, |
| 408 | bool video, |
| 409 | const std::string& audio_constraints, |
| 410 | const std::string& video_constraints) { |
| 411 | OnGetMedia(kGetUserMedia, frame_id, pid, request_id, audio, video, |
| 412 | audio_constraints, video_constraints); |
| 413 | } |
| 414 | |
| 415 | void WebRTCInternals::OnGetUserMediaSuccess( |
| 416 | GlobalRenderFrameHostId frame_id, |
| 417 | base::ProcessId pid, |
| 418 | int request_id, |
| 419 | const std::string& stream_id, |
| 420 | const std::string& audio_track_info, |
| 421 | const std::string& video_track_info) { |
| 422 | OnGetMediaSuccess(kGetUserMedia, frame_id, pid, request_id, stream_id, |
| 423 | audio_track_info, video_track_info); |
| 424 | } |
| 425 | |
| 426 | void WebRTCInternals::OnGetUserMediaFailure(GlobalRenderFrameHostId frame_id, |
| 427 | base::ProcessId pid, |
| 428 | int request_id, |
| 429 | const std::string& error, |
| 430 | const std::string& error_message) { |
| 431 | OnGetMediaFailure(kGetUserMedia, frame_id, pid, request_id, error, |
| 432 | error_message); |
| 433 | } |
| 434 | |
| 435 | void WebRTCInternals::OnGetDisplayMedia(GlobalRenderFrameHostId frame_id, |
| 436 | base::ProcessId pid, |
| 437 | int request_id, |
| 438 | bool audio, |
| 439 | bool video, |
| 440 | const std::string& audio_constraints, |
| 441 | const std::string& video_constraints) { |
| 442 | OnGetMedia(kGetDisplayMedia, frame_id, pid, request_id, audio, video, |
| 443 | audio_constraints, video_constraints); |
| 444 | } |
| 445 | |
| 446 | void WebRTCInternals::OnGetDisplayMediaSuccess( |
| 447 | GlobalRenderFrameHostId frame_id, |
| 448 | base::ProcessId pid, |
| 449 | int request_id, |
| 450 | const std::string& stream_id, |
| 451 | const std::string& audio_track_info, |
| 452 | const std::string& video_track_info) { |
| 453 | OnGetMediaSuccess(kGetDisplayMedia, frame_id, pid, request_id, stream_id, |
| 454 | audio_track_info, video_track_info); |
| 455 | } |
| 456 | |
| 457 | void WebRTCInternals::OnGetDisplayMediaFailure( |
| 458 | GlobalRenderFrameHostId frame_id, |
| 459 | base::ProcessId pid, |
| 460 | int request_id, |
| 461 | const std::string& error, |
| 462 | const std::string& error_message) { |
| 463 | OnGetMediaFailure(kGetDisplayMedia, frame_id, pid, request_id, error, |
| 464 | error_message); |
| 465 | } |
| 466 | |
tommi | 75d54fe84 | 2016-02-09 14:56:30 | [diff] [blame] | 467 | void WebRTCInternals::AddObserver(WebRTCInternalsUIObserver* observer) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 468 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 469 | observers_.AddObserver(observer); |
| 470 | } |
| 471 | |
tommi | 75d54fe84 | 2016-02-09 14:56:30 | [diff] [blame] | 472 | void WebRTCInternals::RemoveObserver(WebRTCInternalsUIObserver* observer) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 473 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 474 | observers_.RemoveObserver(observer); |
Mitsuru Oshima | 69031e6 | 2021-01-20 18:01:58 | [diff] [blame] | 475 | if (!observers_.empty()) |
tommi | 15e9cea | 2017-03-06 13:56:28 | [diff] [blame] | 476 | return; |
[email protected] | bf3657b | 2013-12-17 12:36:17 | [diff] [blame] | 477 | |
ivoc | cf0887d | 2016-07-07 18:23:53 | [diff] [blame] | 478 | // Disables event log and audio debug recordings if enabled and the last |
grunell | 7566ffc | 2015-09-07 07:39:44 | [diff] [blame] | 479 | // webrtc-internals page is going away. |
tommi | 15e9cea | 2017-03-06 13:56:28 | [diff] [blame] | 480 | DisableAudioDebugRecordings(); |
Philipp Hancke | f922d4b1 | 2022-09-09 16:04:33 | [diff] [blame] | 481 | if (CanToggleEventLogRecordings()) { |
| 482 | // Do not disable event log recording when the browser was started |
| 483 | // with the flag to enable the recordings on the command line. |
| 484 | DisableLocalEventLogRecordings(); |
| 485 | } |
tommi | 15e9cea | 2017-03-06 13:56:28 | [diff] [blame] | 486 | |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 487 | // TODO(tommi): Consider removing all the peer_connection_data(). |
| 488 | for (auto& dictionary : peer_connection_data()) |
jdoerrie | a5676c6 | 2017-04-11 18:09:14 | [diff] [blame] | 489 | FreeLogList(&dictionary); |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 490 | } |
| 491 | |
Tarun Bansal | f153d683 | 2019-06-17 18:18:05 | [diff] [blame] | 492 | void WebRTCInternals::AddConnectionsObserver( |
| 493 | WebRtcInternalsConnectionsObserver* observer) { |
| 494 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 495 | connections_observers_.AddObserver(observer); |
| 496 | } |
| 497 | |
| 498 | void WebRTCInternals::RemoveConnectionsObserver( |
| 499 | WebRtcInternalsConnectionsObserver* observer) { |
| 500 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 501 | connections_observers_.RemoveObserver(observer); |
| 502 | } |
| 503 | |
[email protected] | a0437b20 | 2014-01-02 22:45:51 | [diff] [blame] | 504 | void WebRTCInternals::UpdateObserver(WebRTCInternalsUIObserver* observer) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 505 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 506 | if (peer_connection_data().size() > 0) |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 507 | observer->OnUpdate("update-all-peer-connections", &peer_connection_data_); |
[email protected] | a0437b20 | 2014-01-02 22:45:51 | [diff] [blame] | 508 | |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 509 | for (const auto& request : get_user_media_requests_) { |
Philipp Hancke | 17347d08 | 2021-11-24 06:51:26 | [diff] [blame] | 510 | // If there is a stream_id key or an error key this is an update. |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 511 | if (request.GetDict().FindString("stream_id") || |
| 512 | request.GetDict().FindString("error")) { |
[email protected] | deb232b | 2022-11-28 11:22:16 | [diff] [blame] | 513 | observer->OnUpdate("update-media", &request); |
Philipp Hancke | d013ad9 | 2021-11-22 22:23:08 | [diff] [blame] | 514 | } else { |
[email protected] | deb232b | 2022-11-28 11:22:16 | [diff] [blame] | 515 | observer->OnUpdate("add-media", &request); |
Philipp Hancke | d013ad9 | 2021-11-22 22:23:08 | [diff] [blame] | 516 | } |
[email protected] | a0437b20 | 2014-01-02 22:45:51 | [diff] [blame] | 517 | } |
[email protected] | c054f09 | 2013-01-27 08:12:49 | [diff] [blame] | 518 | } |
| 519 | |
grunell | 7566ffc | 2015-09-07 07:39:44 | [diff] [blame] | 520 | void WebRTCInternals::EnableAudioDebugRecordings( |
| 521 | content::WebContents* web_contents) { |
grunell | 7566ffc | 2015-09-07 07:39:44 | [diff] [blame] | 522 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Xiaohan Wang | f096c632 | 2022-01-15 18:46:20 | [diff] [blame] | 523 | #if BUILDFLAG(IS_ANDROID) |
grunell | 7566ffc | 2015-09-07 07:39:44 | [diff] [blame] | 524 | EnableAudioDebugRecordingsOnAllRenderProcessHosts(); |
[email protected] | f146d0d | 2014-02-15 14:30:24 | [diff] [blame] | 525 | #else |
Philip Eliasson | d04b787 | 2025-05-14 08:15:58 | [diff] [blame] | 526 | MaybeShowSelectFileDialog(web_contents, SelectionType::kAudioDebugRecordings); |
[email protected] | bf3657b | 2013-12-17 12:36:17 | [diff] [blame] | 527 | #endif |
| 528 | } |
| 529 | |
grunell | 7566ffc | 2015-09-07 07:39:44 | [diff] [blame] | 530 | void WebRTCInternals::DisableAudioDebugRecordings() { |
grunell | 7566ffc | 2015-09-07 07:39:44 | [diff] [blame] | 531 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Marina Ciocea | 741a4fb1 | 2018-01-27 09:55:12 | [diff] [blame] | 532 | if (!audio_debug_recording_session_) |
tommi | 15e9cea | 2017-03-06 13:56:28 | [diff] [blame] | 533 | return; |
Marina Ciocea | 741a4fb1 | 2018-01-27 09:55:12 | [diff] [blame] | 534 | audio_debug_recording_session_.reset(); |
[email protected] | a43fc4a | 2014-06-03 14:47:05 | [diff] [blame] | 535 | |
[email protected] | bf3657b | 2013-12-17 12:36:17 | [diff] [blame] | 536 | for (RenderProcessHost::iterator i( |
| 537 | content::RenderProcessHost::AllHostsIterator()); |
| 538 | !i.IsAtEnd(); i.Advance()) { |
grunell | 7566ffc | 2015-09-07 07:39:44 | [diff] [blame] | 539 | i.GetCurrentValue()->DisableAudioDebugRecordings(); |
[email protected] | bf3657b | 2013-12-17 12:36:17 | [diff] [blame] | 540 | } |
[email protected] | bf3657b | 2013-12-17 12:36:17 | [diff] [blame] | 541 | } |
| 542 | |
grunell | 7566ffc | 2015-09-07 07:39:44 | [diff] [blame] | 543 | bool WebRTCInternals::IsAudioDebugRecordingsEnabled() const { |
| 544 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Marina Ciocea | 741a4fb1 | 2018-01-27 09:55:12 | [diff] [blame] | 545 | return !!audio_debug_recording_session_; |
grunell | 7566ffc | 2015-09-07 07:39:44 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | const base::FilePath& WebRTCInternals::GetAudioDebugRecordingsFilePath() const { |
| 549 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 550 | return audio_debug_recordings_file_path_; |
| 551 | } |
| 552 | |
Elad Alon | 2afe249 | 2017-12-01 15:11:46 | [diff] [blame] | 553 | void WebRTCInternals::EnableLocalEventLogRecordings( |
ivoc | add54f0d | 2015-12-18 23:17:05 | [diff] [blame] | 554 | content::WebContents* web_contents) { |
| 555 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Elad Alon | 67ac27bc4 | 2022-05-03 15:30:26 | [diff] [blame] | 556 | DCHECK(web_contents); |
Elad Alon | c0928bf1 | 2018-02-28 19:35:17 | [diff] [blame] | 557 | DCHECK(CanToggleEventLogRecordings()); |
Elad Alon | 67ac27bc4 | 2022-05-03 15:30:26 | [diff] [blame] | 558 | |
Xiaohan Wang | f096c632 | 2022-01-15 18:46:20 | [diff] [blame] | 559 | #if BUILDFLAG(IS_ANDROID) |
Elad Alon | 12ecdf0 | 2018-03-14 12:07:31 | [diff] [blame] | 560 | WebRtcEventLogger* const logger = WebRtcEventLogger::Get(); |
| 561 | if (logger) { |
Patrick Monette | 2169e67 | 2021-03-30 15:54:22 | [diff] [blame] | 562 | logger->EnableLocalLogging(event_log_recordings_file_path_); |
Elad Alon | 12ecdf0 | 2018-03-14 12:07:31 | [diff] [blame] | 563 | } |
ivoc | add54f0d | 2015-12-18 23:17:05 | [diff] [blame] | 564 | #else |
Philip Eliasson | d04b787 | 2025-05-14 08:15:58 | [diff] [blame] | 565 | MaybeShowSelectFileDialog(web_contents, SelectionType::kRtcEventLogs); |
ivoc | add54f0d | 2015-12-18 23:17:05 | [diff] [blame] | 566 | #endif |
ivoc | cf0887d | 2016-07-07 18:23:53 | [diff] [blame] | 567 | } |
ivoc | add54f0d | 2015-12-18 23:17:05 | [diff] [blame] | 568 | |
Elad Alon | 2afe249 | 2017-12-01 15:11:46 | [diff] [blame] | 569 | void WebRTCInternals::DisableLocalEventLogRecordings() { |
Elad Alon | 67ac27bc4 | 2022-05-03 15:30:26 | [diff] [blame] | 570 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 571 | |
ivoc | cf0887d | 2016-07-07 18:23:53 | [diff] [blame] | 572 | event_log_recordings_ = false; |
Elad Alon | c0928bf1 | 2018-02-28 19:35:17 | [diff] [blame] | 573 | DCHECK(CanToggleEventLogRecordings()); |
Elad Alon | 12ecdf0 | 2018-03-14 12:07:31 | [diff] [blame] | 574 | WebRtcEventLogger* const logger = WebRtcEventLogger::Get(); |
| 575 | if (logger) { |
Patrick Monette | 2169e67 | 2021-03-30 15:54:22 | [diff] [blame] | 576 | logger->DisableLocalLogging(); |
Elad Alon | 12ecdf0 | 2018-03-14 12:07:31 | [diff] [blame] | 577 | } |
ivoc | add54f0d | 2015-12-18 23:17:05 | [diff] [blame] | 578 | } |
| 579 | |
Philip Eliasson | 9b5072d4 | 2025-05-13 13:13:49 | [diff] [blame] | 580 | void WebRTCInternals::EnableDataChannelRecordings( |
| 581 | content::WebContents* web_contents) { |
| 582 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 583 | #if BUILDFLAG(IS_ANDROID) |
| 584 | WebRtcEventLogger* const logger = WebRtcEventLogger::Get(); |
| 585 | if (logger) { |
| 586 | logger->EnableDataChannelLogging(data_channel_recordings_file_path_); |
| 587 | } |
| 588 | #else |
Philip Eliasson | d04b787 | 2025-05-14 08:15:58 | [diff] [blame] | 589 | MaybeShowSelectFileDialog(web_contents, |
| 590 | SelectionType::kDataChannelRecordings); |
Philip Eliasson | 9b5072d4 | 2025-05-13 13:13:49 | [diff] [blame] | 591 | #endif |
| 592 | } |
| 593 | |
| 594 | void WebRTCInternals::DisableDataChannelRecordings() { |
| 595 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 596 | |
| 597 | data_channel_recording_active_ = false; |
Philip Eliasson | 9b5072d4 | 2025-05-13 13:13:49 | [diff] [blame] | 598 | WebRtcEventLogger* const logger = WebRtcEventLogger::Get(); |
| 599 | if (logger) { |
| 600 | logger->DisableDataChannelLogging(); |
| 601 | } |
| 602 | } |
| 603 | |
ivoc | add54f0d | 2015-12-18 23:17:05 | [diff] [blame] | 604 | bool WebRTCInternals::IsEventLogRecordingsEnabled() const { |
| 605 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 606 | return event_log_recordings_; |
| 607 | } |
| 608 | |
Philip Eliasson | 9b5072d4 | 2025-05-13 13:13:49 | [diff] [blame] | 609 | bool WebRTCInternals::IsDataChannelRecordingsEnabled() const { |
| 610 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 611 | return data_channel_recording_active_; |
| 612 | } |
| 613 | |
Elad Alon | c0928bf1 | 2018-02-28 19:35:17 | [diff] [blame] | 614 | bool WebRTCInternals::CanToggleEventLogRecordings() const { |
Elad Alon | 67ac27bc4 | 2022-05-03 15:30:26 | [diff] [blame] | 615 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Elad Alon | c0928bf1 | 2018-02-28 19:35:17 | [diff] [blame] | 616 | return command_line_derived_logging_path_.empty(); |
| 617 | } |
| 618 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 619 | void WebRTCInternals::SendUpdate(const std::string& event_name, |
Maks Orlovich | 442f378 | 2021-06-04 18:05:09 | [diff] [blame] | 620 | base::Value event_data) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 621 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Mitsuru Oshima | 69031e6 | 2021-01-20 18:01:58 | [diff] [blame] | 622 | DCHECK(!observers_.empty()); |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 623 | |
tommi | 75d54fe84 | 2016-02-09 14:56:30 | [diff] [blame] | 624 | bool queue_was_empty = pending_updates_.empty(); |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 625 | pending_updates_.push(PendingUpdate(event_name, std::move(event_data))); |
tommi | 75d54fe84 | 2016-02-09 14:56:30 | [diff] [blame] | 626 | |
| 627 | if (queue_was_empty) { |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 628 | GetUIThreadTaskRunner({})->PostDelayedTask( |
| 629 | FROM_HERE, |
tzik | e2aca99 | 2017-09-05 08:50:54 | [diff] [blame] | 630 | base::BindOnce(&WebRTCInternals::ProcessPendingUpdates, |
| 631 | weak_factory_.GetWeakPtr()), |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 632 | base::Milliseconds(aggregate_updates_ms_)); |
tommi | 75d54fe84 | 2016-02-09 14:56:30 | [diff] [blame] | 633 | } |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 634 | } |
[email protected] | 64ccac3 | 2013-01-29 21:40:04 | [diff] [blame] | 635 | |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 636 | void WebRTCInternals::SendUpdate(const std::string& event_name, |
| 637 | base::Value::Dict event_data) { |
| 638 | SendUpdate(event_name, base::Value(std::move(event_data))); |
| 639 | } |
| 640 | |
Bo Liu | 2a48940 | 2018-04-24 23:41:27 | [diff] [blame] | 641 | void WebRTCInternals::RenderProcessExited( |
| 642 | RenderProcessHost* host, |
| 643 | const ChildProcessTerminationInfo& info) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 644 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Emily Andrews | d15fd76 | 2024-12-10 20:41:54 | [diff] [blame] | 645 | OnRendererExit(host->GetDeprecatedID()); |
| 646 | render_process_id_set_.erase(host->GetDeprecatedID()); |
amistry | 163a4a3 | 2015-05-25 23:48:15 | [diff] [blame] | 647 | host->RemoveObserver(this); |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 648 | } |
[email protected] | 020633b | 2013-01-30 23:35:35 | [diff] [blame] | 649 | |
Philip Eliasson | d04b787 | 2025-05-14 08:15:58 | [diff] [blame] | 650 | void WebRTCInternals::MaybeShowSelectFileDialog( |
| 651 | content::WebContents* web_contents, |
| 652 | SelectionType log_type) { |
| 653 | if (select_file_dialog_) { |
| 654 | return; |
| 655 | } |
| 656 | |
| 657 | base::FilePath* file_path = nullptr; |
| 658 | switch (log_type) { |
| 659 | case (SelectionType::kRtcEventLogs): { |
| 660 | file_path = &event_log_recordings_file_path_; |
| 661 | break; |
| 662 | } |
| 663 | case (SelectionType::kAudioDebugRecordings): { |
| 664 | file_path = &audio_debug_recordings_file_path_; |
| 665 | break; |
| 666 | } |
| 667 | case (SelectionType::kDataChannelRecordings): { |
| 668 | file_path = &data_channel_recordings_file_path_; |
| 669 | break; |
| 670 | } |
| 671 | } |
| 672 | CHECK(file_path); |
| 673 | |
| 674 | selection_type_ = log_type; |
| 675 | select_file_dialog_ = ui::SelectFileDialog::Create( |
| 676 | this, |
| 677 | GetContentClient()->browser()->CreateSelectFilePolicy(web_contents)); |
| 678 | select_file_dialog_->SelectFile( |
| 679 | ui::SelectFileDialog::SELECT_SAVEAS_FILE, |
| 680 | /*title=*/std::u16string(), *file_path, /*file_types=*/nullptr, |
| 681 | /*file_type_index=*/0, |
| 682 | /*default_extension=*/base::FilePath::StringType(), |
| 683 | web_contents->GetTopLevelNativeWindow()); |
| 684 | } |
| 685 | |
Avi Drissman | d0b88a2b | 2023-12-28 22:49:38 | [diff] [blame] | 686 | void WebRTCInternals::FileSelected(const ui::SelectedFileInfo& file, |
Elly | b4b57fa | 2024-07-02 16:17:40 | [diff] [blame] | 687 | int /* unused_index */) { |
grunell | 7566ffc | 2015-09-07 07:39:44 | [diff] [blame] | 688 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Elad Alon | b0a304d | 2017-11-02 19:08:43 | [diff] [blame] | 689 | switch (selection_type_) { |
Elad Alon | 71ec4bb | 2018-01-15 11:38:03 | [diff] [blame] | 690 | case SelectionType::kRtcEventLogs: { |
Avi Drissman | d0b88a2b | 2023-12-28 22:49:38 | [diff] [blame] | 691 | event_log_recordings_file_path_ = file.path(); |
Elad Alon | 6150601 | 2017-12-12 08:45:54 | [diff] [blame] | 692 | event_log_recordings_ = true; |
Elad Alon | 12ecdf0 | 2018-03-14 12:07:31 | [diff] [blame] | 693 | WebRtcEventLogger* const logger = WebRtcEventLogger::Get(); |
| 694 | if (logger) { |
Avi Drissman | d0b88a2b | 2023-12-28 22:49:38 | [diff] [blame] | 695 | logger->EnableLocalLogging(file.path()); |
Elad Alon | 12ecdf0 | 2018-03-14 12:07:31 | [diff] [blame] | 696 | } |
Elad Alon | b0a304d | 2017-11-02 19:08:43 | [diff] [blame] | 697 | break; |
Elad Alon | 71ec4bb | 2018-01-15 11:38:03 | [diff] [blame] | 698 | } |
| 699 | case SelectionType::kAudioDebugRecordings: { |
Avi Drissman | d0b88a2b | 2023-12-28 22:49:38 | [diff] [blame] | 700 | audio_debug_recordings_file_path_ = file.path(); |
Elad Alon | b0a304d | 2017-11-02 19:08:43 | [diff] [blame] | 701 | EnableAudioDebugRecordingsOnAllRenderProcessHosts(); |
| 702 | break; |
Elad Alon | 71ec4bb | 2018-01-15 11:38:03 | [diff] [blame] | 703 | } |
Philip Eliasson | 9b5072d4 | 2025-05-13 13:13:49 | [diff] [blame] | 704 | case SelectionType::kDataChannelRecordings: { |
| 705 | data_channel_recordings_file_path_ = file.path(); |
| 706 | data_channel_recording_active_ = true; |
| 707 | WebRtcEventLogger* const logger = WebRtcEventLogger::Get(); |
| 708 | if (logger) { |
| 709 | logger->EnableDataChannelLogging(file.path()); |
| 710 | } |
| 711 | break; |
| 712 | } |
Peter Boström | 188188d | 2024-05-17 21:35:58 | [diff] [blame] | 713 | default: { |
Peter Boström | fc7ddc18 | 2024-10-31 19:37:21 | [diff] [blame] | 714 | NOTREACHED(); |
Peter Boström | 188188d | 2024-05-17 21:35:58 | [diff] [blame] | 715 | } |
ivoc | add54f0d | 2015-12-18 23:17:05 | [diff] [blame] | 716 | } |
Philip Eliasson | be1d34f | 2025-05-14 08:58:15 | [diff] [blame] | 717 | select_file_dialog_.reset(); |
[email protected] | bf3657b | 2013-12-17 12:36:17 | [diff] [blame] | 718 | } |
| 719 | |
Elly | b4b57fa | 2024-07-02 16:17:40 | [diff] [blame] | 720 | void WebRTCInternals::FileSelectionCanceled() { |
grunell | 7566ffc | 2015-09-07 07:39:44 | [diff] [blame] | 721 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Elad Alon | b0a304d | 2017-11-02 19:08:43 | [diff] [blame] | 722 | switch (selection_type_) { |
Elad Alon | 5e351e0 | 2017-11-06 17:53:35 | [diff] [blame] | 723 | case SelectionType::kRtcEventLogs: |
Maks Orlovich | 442f378 | 2021-06-04 18:05:09 | [diff] [blame] | 724 | SendUpdate("event-log-recordings-file-selection-cancelled", |
| 725 | base::Value()); |
Elad Alon | b0a304d | 2017-11-02 19:08:43 | [diff] [blame] | 726 | break; |
Elad Alon | 5e351e0 | 2017-11-06 17:53:35 | [diff] [blame] | 727 | case SelectionType::kAudioDebugRecordings: |
Maks Orlovich | 442f378 | 2021-06-04 18:05:09 | [diff] [blame] | 728 | SendUpdate("audio-debug-recordings-file-selection-cancelled", |
| 729 | base::Value()); |
Elad Alon | b0a304d | 2017-11-02 19:08:43 | [diff] [blame] | 730 | break; |
Philip Eliasson | 9b5072d4 | 2025-05-13 13:13:49 | [diff] [blame] | 731 | case SelectionType::kDataChannelRecordings: |
| 732 | SendUpdate("data-channel-recordings-file-selection-cancelled", |
| 733 | base::Value()); |
| 734 | break; |
Elad Alon | b0a304d | 2017-11-02 19:08:43 | [diff] [blame] | 735 | default: |
Peter Boström | fc7ddc18 | 2024-10-31 19:37:21 | [diff] [blame] | 736 | NOTREACHED(); |
ivoc | add54f0d | 2015-12-18 23:17:05 | [diff] [blame] | 737 | } |
Philip Eliasson | be1d34f | 2025-05-14 08:58:15 | [diff] [blame] | 738 | select_file_dialog_.reset(); |
[email protected] | 09f12a0 | 2014-03-28 16:54:08 | [diff] [blame] | 739 | } |
| 740 | |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 741 | void WebRTCInternals::OnRendererExit(int render_process_id) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 742 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 32eadb44 | 2014-08-08 16:55:39 | [diff] [blame] | 743 | |
[email protected] | 020633b | 2013-01-30 23:35:35 | [diff] [blame] | 744 | // Iterates from the end of the list to remove the PeerConnections created |
Maks Orlovich | 442f378 | 2021-06-04 18:05:09 | [diff] [blame] | 745 | // by the exiting renderer. |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 746 | for (int i = peer_connection_data().size() - 1; i >= 0; --i) { |
| 747 | DCHECK(peer_connection_data()[i].is_dict()); |
[email protected] | 020633b | 2013-01-30 23:35:35 | [diff] [blame] | 748 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 749 | std::optional<int> this_rid, this_lid; |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 750 | this_rid = peer_connection_data()[i].GetDict().FindInt("rid"); |
| 751 | this_lid = peer_connection_data()[i].GetDict().FindInt("lid"); |
[email protected] | 020633b | 2013-01-30 23:35:35 | [diff] [blame] | 752 | |
Maks Orlovich | 442f378 | 2021-06-04 18:05:09 | [diff] [blame] | 753 | if (this_rid.value_or(0) == render_process_id) { |
Mitsuru Oshima | 69031e6 | 2021-01-20 18:01:58 | [diff] [blame] | 754 | if (!observers_.empty()) { |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 755 | base::Value::Dict update; |
| 756 | update.Set("rid", this_rid.value_or(0)); |
| 757 | update.Set("lid", this_lid.value_or(0)); |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 758 | SendUpdate("remove-peer-connection", std::move(update)); |
[email protected] | 020633b | 2013-01-30 23:35:35 | [diff] [blame] | 759 | } |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 760 | MaybeClosePeerConnection(peer_connection_data()[i]); |
| 761 | peer_connection_data().erase(peer_connection_data().begin() + i); |
[email protected] | 020633b | 2013-01-30 23:35:35 | [diff] [blame] | 762 | } |
| 763 | } |
ke.he | 1671f06 | 2017-05-20 00:34:52 | [diff] [blame] | 764 | UpdateWakeLock(); |
[email protected] | a0437b20 | 2014-01-02 22:45:51 | [diff] [blame] | 765 | |
| 766 | bool found_any = false; |
| 767 | // Iterates from the end of the list to remove the getUserMedia requests |
| 768 | // created by the exiting renderer. |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 769 | for (int i = get_user_media_requests_.size() - 1; i >= 0; --i) { |
| 770 | DCHECK(get_user_media_requests_[i].is_dict()); |
[email protected] | a0437b20 | 2014-01-02 22:45:51 | [diff] [blame] | 771 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 772 | std::optional<int> this_rid = |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 773 | get_user_media_requests_[i].GetDict().FindInt("rid"); |
[email protected] | a0437b20 | 2014-01-02 22:45:51 | [diff] [blame] | 774 | |
Maks Orlovich | 442f378 | 2021-06-04 18:05:09 | [diff] [blame] | 775 | if (this_rid.value_or(0) == render_process_id) { |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 776 | get_user_media_requests_.erase(get_user_media_requests_.begin() + i); |
[email protected] | a0437b20 | 2014-01-02 22:45:51 | [diff] [blame] | 777 | found_any = true; |
| 778 | } |
| 779 | } |
| 780 | |
Mitsuru Oshima | 69031e6 | 2021-01-20 18:01:58 | [diff] [blame] | 781 | if (found_any && !observers_.empty()) { |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 782 | base::Value::Dict update; |
| 783 | update.Set("rid", render_process_id); |
[email protected] | deb232b | 2022-11-28 11:22:16 | [diff] [blame] | 784 | SendUpdate("remove-media-for-renderer", std::move(update)); |
[email protected] | a0437b20 | 2014-01-02 22:45:51 | [diff] [blame] | 785 | } |
[email protected] | 020633b | 2013-01-30 23:35:35 | [diff] [blame] | 786 | } |
| 787 | |
grunell | 7566ffc | 2015-09-07 07:39:44 | [diff] [blame] | 788 | void WebRTCInternals::EnableAudioDebugRecordingsOnAllRenderProcessHosts() { |
| 789 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Marina Ciocea | 741a4fb1 | 2018-01-27 09:55:12 | [diff] [blame] | 790 | DCHECK(!audio_debug_recording_session_); |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 791 | mojo::PendingRemote<audio::mojom::DebugRecording> debug_recording; |
| 792 | content::GetAudioService().BindDebugRecording( |
| 793 | debug_recording.InitWithNewPipeAndPassReceiver()); |
Marina Ciocea | c6d46d2f | 2018-02-26 15:21:28 | [diff] [blame] | 794 | audio_debug_recording_session_ = audio::CreateAudioDebugRecordingSession( |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 795 | audio_debug_recordings_file_path_, std::move(debug_recording)); |
grunell | 086a411 | 2017-03-02 09:23:04 | [diff] [blame] | 796 | |
[email protected] | f146d0d | 2014-02-15 14:30:24 | [diff] [blame] | 797 | for (RenderProcessHost::iterator i( |
| 798 | content::RenderProcessHost::AllHostsIterator()); |
| 799 | !i.IsAtEnd(); i.Advance()) { |
grunell | 7566ffc | 2015-09-07 07:39:44 | [diff] [blame] | 800 | i.GetCurrentValue()->EnableAudioDebugRecordings( |
| 801 | audio_debug_recordings_file_path_); |
[email protected] | f146d0d | 2014-02-15 14:30:24 | [diff] [blame] | 802 | } |
[email protected] | a0437b20 | 2014-01-02 22:45:51 | [diff] [blame] | 803 | } |
| 804 | |
Elaine Chien | 5df4237b | 2022-01-07 18:51:39 | [diff] [blame] | 805 | void WebRTCInternals::MaybeClosePeerConnection(base::Value& record) { |
Elad Alon | 67ac27bc4 | 2022-05-03 15:30:26 | [diff] [blame] | 806 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 807 | std::optional<bool> is_open = record.GetDict().FindBool("isOpen"); |
Maks Orlovich | 442f378 | 2021-06-04 18:05:09 | [diff] [blame] | 808 | DCHECK(is_open.has_value()); |
| 809 | if (!*is_open) |
guidou | 542c74f | 2016-11-17 18:34:28 | [diff] [blame] | 810 | return; |
| 811 | |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 812 | record.GetDict().Set("isOpen", false); |
Guido Urdaneta | 0bc103a | 2018-11-16 19:54:33 | [diff] [blame] | 813 | MaybeMarkPeerConnectionAsNotConnected(record); |
| 814 | } |
| 815 | |
Elaine Chien | 5df4237b | 2022-01-07 18:51:39 | [diff] [blame] | 816 | void WebRTCInternals::MaybeMarkPeerConnectionAsConnected(base::Value& record) { |
Elad Alon | 67ac27bc4 | 2022-05-03 15:30:26 | [diff] [blame] | 817 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 818 | bool was_connected = record.GetDict().FindBool("connected").value_or(true); |
Guido Urdaneta | 0bc103a | 2018-11-16 19:54:33 | [diff] [blame] | 819 | if (!was_connected) { |
| 820 | ++num_connected_connections_; |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 821 | record.GetDict().Set("connected", true); |
Guido Urdaneta | 0bc103a | 2018-11-16 19:54:33 | [diff] [blame] | 822 | UpdateWakeLock(); |
Tarun Bansal | f153d683 | 2019-06-17 18:18:05 | [diff] [blame] | 823 | for (auto& observer : connections_observers_) |
| 824 | observer.OnConnectionsCountChange(num_connected_connections_); |
Guido Urdaneta | 0bc103a | 2018-11-16 19:54:33 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | |
| 828 | void WebRTCInternals::MaybeMarkPeerConnectionAsNotConnected( |
Elaine Chien | 5df4237b | 2022-01-07 18:51:39 | [diff] [blame] | 829 | base::Value& record) { |
Elad Alon | 67ac27bc4 | 2022-05-03 15:30:26 | [diff] [blame] | 830 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 831 | bool was_connected = record.GetDict().FindBool("connected").value_or(false); |
Guido Urdaneta | 0bc103a | 2018-11-16 19:54:33 | [diff] [blame] | 832 | if (was_connected) { |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 833 | record.GetDict().Set("connected", false); |
Guido Urdaneta | 0bc103a | 2018-11-16 19:54:33 | [diff] [blame] | 834 | --num_connected_connections_; |
| 835 | DCHECK_GE(num_connected_connections_, 0); |
| 836 | UpdateWakeLock(); |
Tarun Bansal | f153d683 | 2019-06-17 18:18:05 | [diff] [blame] | 837 | for (auto& observer : connections_observers_) |
| 838 | observer.OnConnectionsCountChange(num_connected_connections_); |
Guido Urdaneta | 0bc103a | 2018-11-16 19:54:33 | [diff] [blame] | 839 | } |
guidou | 542c74f | 2016-11-17 18:34:28 | [diff] [blame] | 840 | } |
| 841 | |
ke.he | 1671f06 | 2017-05-20 00:34:52 | [diff] [blame] | 842 | void WebRTCInternals::UpdateWakeLock() { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 843 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
maxmorin | c369574d | 2016-07-25 15:28:17 | [diff] [blame] | 844 | if (!should_block_power_saving_) |
| 845 | return; |
[email protected] | 32eadb44 | 2014-08-08 16:55:39 | [diff] [blame] | 846 | |
Guido Urdaneta | 0bc103a | 2018-11-16 19:54:33 | [diff] [blame] | 847 | if (num_connected_connections_ == 0) { |
ke.he | 1671f06 | 2017-05-20 00:34:52 | [diff] [blame] | 848 | DVLOG(1) |
| 849 | << ("Cancel the wake lock on application suspension since no " |
| 850 | "PeerConnections are active anymore."); |
juncai | 8e8d6ec | 2017-06-06 20:48:18 | [diff] [blame] | 851 | GetWakeLock()->CancelWakeLock(); |
Guido Urdaneta | 0bc103a | 2018-11-16 19:54:33 | [diff] [blame] | 852 | } else { |
| 853 | DCHECK_GT(num_connected_connections_, 0); |
[email protected] | 32eadb44 | 2014-08-08 16:55:39 | [diff] [blame] | 854 | DVLOG(1) << ("Preventing the application from being suspended while one or " |
| 855 | "more PeerConnections are active."); |
juncai | 8e8d6ec | 2017-06-06 20:48:18 | [diff] [blame] | 856 | GetWakeLock()->RequestWakeLock(); |
[email protected] | 32eadb44 | 2014-08-08 16:55:39 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | |
juncai | 8e8d6ec | 2017-06-06 20:48:18 | [diff] [blame] | 860 | device::mojom::WakeLock* WebRTCInternals::GetWakeLock() { |
Elad Alon | 67ac27bc4 | 2022-05-03 15:30:26 | [diff] [blame] | 861 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
ke.he | 1671f06 | 2017-05-20 00:34:52 | [diff] [blame] | 862 | // Here is a lazy binding, and will not reconnect after connection error. |
juncai | 8e8d6ec | 2017-06-06 20:48:18 | [diff] [blame] | 863 | if (!wake_lock_) { |
Ken Rockot | 44d73be | 2019-12-12 22:25:59 | [diff] [blame] | 864 | mojo::Remote<device::mojom::WakeLockProvider> wake_lock_provider; |
| 865 | GetDeviceService().BindWakeLockProvider( |
| 866 | wake_lock_provider.BindNewPipeAndPassReceiver()); |
| 867 | wake_lock_provider->GetWakeLockWithoutContext( |
| 868 | device::mojom::WakeLockType::kPreventAppSuspension, |
| 869 | device::mojom::WakeLockReason::kOther, |
| 870 | "WebRTC has active PeerConnections", |
| 871 | wake_lock_.BindNewPipeAndPassReceiver()); |
ke.he | 1671f06 | 2017-05-20 00:34:52 | [diff] [blame] | 872 | } |
juncai | 8e8d6ec | 2017-06-06 20:48:18 | [diff] [blame] | 873 | return wake_lock_.get(); |
ke.he | 1671f06 | 2017-05-20 00:34:52 | [diff] [blame] | 874 | } |
| 875 | |
tommi | 75d54fe84 | 2016-02-09 14:56:30 | [diff] [blame] | 876 | void WebRTCInternals::ProcessPendingUpdates() { |
| 877 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 878 | while (!pending_updates_.empty()) { |
| 879 | const auto& update = pending_updates_.front(); |
ericwilligers | 254597b | 2016-10-17 10:32:31 | [diff] [blame] | 880 | for (auto& observer : observers_) |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 881 | observer.OnUpdate(update.event_name(), update.event_data()); |
tommi | 75d54fe84 | 2016-02-09 14:56:30 | [diff] [blame] | 882 | pending_updates_.pop(); |
| 883 | } |
| 884 | } |
| 885 | |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 886 | base::Value::List::iterator WebRTCInternals::FindRecord( |
Alexander Timin | 8690530c | 2021-06-19 00:34:32 | [diff] [blame] | 887 | GlobalRenderFrameHostId frame_id, |
Maks Orlovich | 442f378 | 2021-06-04 18:05:09 | [diff] [blame] | 888 | int lid) { |
tommi | 15e9cea | 2017-03-06 13:56:28 | [diff] [blame] | 889 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 890 | |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 891 | for (auto it = peer_connection_data().begin(); |
| 892 | it != peer_connection_data().end(); ++it) { |
Maks Orlovich | 442f378 | 2021-06-04 18:05:09 | [diff] [blame] | 893 | DCHECK(it->is_dict()); |
tommi | 15e9cea | 2017-03-06 13:56:28 | [diff] [blame] | 894 | |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 895 | int this_rid = it->GetDict().FindInt("rid").value_or(0); |
| 896 | int this_lid = it->GetDict().FindInt("lid").value_or(0); |
tommi | 15e9cea | 2017-03-06 13:56:28 | [diff] [blame] | 897 | |
Maks Orlovich | 442f378 | 2021-06-04 18:05:09 | [diff] [blame] | 898 | if (this_rid == frame_id.child_id && this_lid == lid) |
| 899 | return it; |
tommi | 15e9cea | 2017-03-06 13:56:28 | [diff] [blame] | 900 | } |
Matt Menke | 67484ed3 | 2022-07-22 18:54:33 | [diff] [blame] | 901 | return peer_connection_data().end(); |
tommi | 15e9cea | 2017-03-06 13:56:28 | [diff] [blame] | 902 | } |
[email protected] | 64ccac3 | 2013-01-29 21:40:04 | [diff] [blame] | 903 | } // namespace content |