[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 1 | // Copyright (c) 2012 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 "content/browser/media/media_internals.h" |
| 6 | |
avi | 7f27756 | 2015-12-25 02:41:26 | [diff] [blame] | 7 | #include <stddef.h> |
dcheng | 98e96a7 | 2016-06-11 03:41:48 | [diff] [blame] | 8 | |
Jan Wilken Dörrie | ad587c3 | 2021-03-11 14:09:27 | [diff] [blame] | 9 | #include <string> |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 10 | #include <tuple> |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 11 | #include <utility> |
avi | 7f27756 | 2015-12-25 02:41:26 | [diff] [blame] | 12 | |
Sebastien Marchand | f8cbfab | 2019-01-25 16:02:30 | [diff] [blame] | 13 | #include "base/bind.h" |
Becca Hughes | a0754d7 | 2018-10-09 00:33:40 | [diff] [blame] | 14 | #include "base/containers/adapters.h" |
Henrik Grunell | e7e5291 | 2018-10-18 07:39:53 | [diff] [blame] | 15 | #include "base/feature_list.h" |
Henrik Grunell | 5c1dc36 | 2019-05-20 18:06:17 | [diff] [blame] | 16 | #include "base/metrics/field_trial_params.h" |
Dale Curtis | 4a6f54c0 | 2017-06-06 23:32:20 | [diff] [blame] | 17 | #include "base/metrics/histogram_functions.h" |
asvitkine | 8d51e9d | 2016-09-02 23:55:43 | [diff] [blame] | 18 | #include "base/metrics/histogram_macros.h" |
Yeonwoo Jo | 095a273 | 2018-10-03 17:10:40 | [diff] [blame] | 19 | #include "base/stl_util.h" |
Henrik Grunell | 5c1dc36 | 2019-05-20 18:06:17 | [diff] [blame] | 20 | #include "base/strings/strcat.h" |
[email protected] | 9367e03 | 2014-03-07 19:42:37 | [diff] [blame] | 21 | #include "base/strings/string_number_conversions.h" |
[email protected] | 348fbaac | 2013-06-11 06:31:51 | [diff] [blame] | 22 | #include "base/strings/stringprintf.h" |
avi | 7f27756 | 2015-12-25 02:41:26 | [diff] [blame] | 23 | #include "build/build_config.h" |
Becca Hughes | d3444394 | 2018-08-17 22:15:08 | [diff] [blame] | 24 | #include "content/browser/media/session/media_session_impl.h" |
guidou | 50db1a6 | 2016-06-01 17:12:17 | [diff] [blame] | 25 | #include "content/browser/renderer_host/media/media_stream_manager.h" |
Guido Urdaneta | e02b572 | 2020-07-22 05:58:21 | [diff] [blame] | 26 | #include "content/public/browser/audio_service.h" |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 27 | #include "content/public/browser/browser_context.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 28 | #include "content/public/browser/browser_task_traits.h" |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 29 | #include "content/public/browser/browser_thread.h" |
Guido Urdaneta | e02b572 | 2020-07-22 05:58:21 | [diff] [blame] | 30 | #include "content/public/browser/content_browser_client.h" |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 31 | #include "content/public/browser/notification_service.h" |
| 32 | #include "content/public/browser/notification_types.h" |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 33 | #include "content/public/browser/render_frame_host.h" |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 34 | #include "content/public/browser/render_process_host.h" |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 35 | #include "content/public/browser/web_contents.h" |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 36 | #include "content/public/browser/web_ui.h" |
Guido Urdaneta | e02b572 | 2020-07-22 05:58:21 | [diff] [blame] | 37 | #include "content/public/common/content_client.h" |
Henrik Grunell | e7e5291 | 2018-10-18 07:39:53 | [diff] [blame] | 38 | #include "content/public/common/content_features.h" |
Henrik Grunell | 5c1dc36 | 2019-05-20 18:06:17 | [diff] [blame] | 39 | #include "media/audio/audio_features.h" |
jrummell | 37a54c0 | 2016-04-22 19:54:27 | [diff] [blame] | 40 | #include "media/base/audio_parameters.h" |
Ted Meyer | 380ca98 | 2020-01-12 01:00:29 | [diff] [blame] | 41 | #include "media/base/media_log_record.h" |
Henrik Grunell | e7e5291 | 2018-10-18 07:39:53 | [diff] [blame] | 42 | #include "media/webrtc/webrtc_switches.h" |
Miyoung Shin | 4e4b503 | 2019-10-24 13:54:06 | [diff] [blame] | 43 | #include "mojo/public/cpp/bindings/self_owned_receiver.h" |
Robert Sesek | 7d0b49b | 2020-07-08 18:31:27 | [diff] [blame] | 44 | #include "sandbox/policy/features.h" |
| 45 | #include "sandbox/policy/sandbox_type.h" |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 46 | |
kraush | 4252105 | 2017-05-04 19:14:34 | [diff] [blame] | 47 | #if !defined(OS_ANDROID) |
xhwang | f218939 | 2015-10-19 22:21:51 | [diff] [blame] | 48 | #include "media/filters/decrypting_video_decoder.h" |
| 49 | #endif |
| 50 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 51 | namespace { |
| 52 | |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 53 | std::u16string SerializeUpdate(const std::string& function, |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 54 | const base::Value* value) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 55 | return content::WebUI::GetJavascriptCall( |
| 56 | function, std::vector<const base::Value*>(1, value)); |
| 57 | } |
| 58 | |
[email protected] | 9367e03 | 2014-03-07 19:42:37 | [diff] [blame] | 59 | std::string EffectsToString(int effects) { |
| 60 | if (effects == media::AudioParameters::NO_EFFECTS) |
| 61 | return "NO_EFFECTS"; |
| 62 | |
| 63 | struct { |
| 64 | int flag; |
| 65 | const char* name; |
| 66 | } flags[] = { |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 67 | {media::AudioParameters::ECHO_CANCELLER, "ECHO_CANCELLER"}, |
| 68 | {media::AudioParameters::DUCKING, "DUCKING"}, |
| 69 | {media::AudioParameters::KEYBOARD_MIC, "KEYBOARD_MIC"}, |
| 70 | {media::AudioParameters::HOTWORD, "HOTWORD"}, |
[email protected] | 9367e03 | 2014-03-07 19:42:37 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | std::string ret; |
Avi Drissman | 9d3ded9 | 2018-12-25 20:50:21 | [diff] [blame] | 74 | for (size_t i = 0; i < base::size(flags); ++i) { |
[email protected] | 9367e03 | 2014-03-07 19:42:37 | [diff] [blame] | 75 | if (effects & flags[i].flag) { |
| 76 | if (!ret.empty()) |
| 77 | ret += " | "; |
| 78 | ret += flags[i].name; |
| 79 | effects &= ~flags[i].flag; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if (effects) { |
| 84 | if (!ret.empty()) |
| 85 | ret += " | "; |
Raul Tambre | 6c0c3f5b | 2019-02-04 17:44:17 | [diff] [blame] | 86 | ret += base::NumberToString(effects); |
[email protected] | 9367e03 | 2014-03-07 19:42:37 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | return ret; |
| 90 | } |
| 91 | |
dalecurtis | b9a6b787 | 2015-02-06 22:41:55 | [diff] [blame] | 92 | std::string FormatToString(media::AudioParameters::Format format) { |
| 93 | switch (format) { |
| 94 | case media::AudioParameters::AUDIO_PCM_LINEAR: |
| 95 | return "pcm_linear"; |
| 96 | case media::AudioParameters::AUDIO_PCM_LOW_LATENCY: |
| 97 | return "pcm_low_latency"; |
tsunghung | 59327d6 | 2016-11-19 16:09:48 | [diff] [blame] | 98 | case media::AudioParameters::AUDIO_BITSTREAM_AC3: |
| 99 | return "ac3"; |
| 100 | case media::AudioParameters::AUDIO_BITSTREAM_EAC3: |
| 101 | return "eac3"; |
dalecurtis | b9a6b787 | 2015-02-06 22:41:55 | [diff] [blame] | 102 | case media::AudioParameters::AUDIO_FAKE: |
| 103 | return "fake"; |
dalecurtis | b9a6b787 | 2015-02-06 22:41:55 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | NOTREACHED(); |
| 107 | return "unknown"; |
| 108 | } |
| 109 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 110 | const char kAudioLogStatusKey[] = "status"; |
| 111 | const char kAudioLogUpdateFunction[] = "media.updateAudioComponent"; |
| 112 | |
| 113 | } // namespace |
| 114 | |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 115 | namespace content { |
| 116 | |
Abhijeet Kandalkar | aad4e17 | 2020-02-19 19:31:29 | [diff] [blame] | 117 | // This class works as a receiver of logs of events occurring in the |
| 118 | // media pipeline. Media logs send by the renderer process to the |
| 119 | // browser process is handled by the below implementation in the |
| 120 | // browser side. |
| 121 | class MediaInternals::MediaInternalLogRecordsImpl |
| 122 | : public content::mojom::MediaInternalLogRecords { |
| 123 | public: |
| 124 | MediaInternalLogRecordsImpl(content::MediaInternals* media_internals, |
| 125 | int render_process_id); |
| 126 | ~MediaInternalLogRecordsImpl() override = default; |
| 127 | void Log(const std::vector<::media::MediaLogRecord>& arr) override; |
| 128 | |
| 129 | private: |
| 130 | content::MediaInternals* const media_internals_; |
| 131 | const int render_process_id_; |
| 132 | DISALLOW_COPY_AND_ASSIGN(MediaInternalLogRecordsImpl); |
| 133 | }; |
| 134 | |
| 135 | MediaInternals::MediaInternalLogRecordsImpl::MediaInternalLogRecordsImpl( |
| 136 | content::MediaInternals* media_internals, |
| 137 | int render_process_id) |
| 138 | : media_internals_(media_internals), |
| 139 | render_process_id_(render_process_id) {} |
| 140 | |
| 141 | void MediaInternals::MediaInternalLogRecordsImpl::Log( |
| 142 | const std::vector<::media::MediaLogRecord>& events) { |
| 143 | media_internals_->OnMediaEvents(render_process_id_, events); |
| 144 | } |
| 145 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 146 | class MediaInternals::AudioLogImpl : public media::mojom::AudioLog, |
| 147 | public media::AudioLog { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 148 | public: |
| 149 | AudioLogImpl(int owner_id, |
| 150 | media::AudioLogFactory::AudioComponent component, |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 151 | content::MediaInternals* media_internals, |
| 152 | int component_id, |
| 153 | int render_process_id, |
| 154 | int render_frame_id); |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 155 | ~AudioLogImpl() override; |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 156 | |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 157 | void OnCreated(const media::AudioParameters& params, |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 158 | const std::string& device_id) override; |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 159 | void OnStarted() override; |
| 160 | void OnStopped() override; |
| 161 | void OnClosed() override; |
| 162 | void OnError() override; |
| 163 | void OnSetVolume(double volume) override; |
| 164 | void OnLogMessage(const std::string& message) override; |
Max Morin | 86d4f13 | 2018-08-30 21:51:13 | [diff] [blame] | 165 | void OnProcessingStateChanged(const std::string& message) override; |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 166 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 167 | private: |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 168 | // If possible, i.e. a WebContents exists for the given RenderFrameHostID, |
| 169 | // tells an existing AudioLogEntry the WebContents title for easier |
| 170 | // differentiation on the UI. Note that the log entry must be created (by |
| 171 | // calling OnCreated() before calling this method. |
| 172 | void SetWebContentsTitle(); |
| 173 | |
| 174 | void SendSingleStringUpdate(const std::string& key, const std::string& value); |
| 175 | void StoreComponentMetadata(base::DictionaryValue* dict); |
| 176 | std::string FormatCacheKey(); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 177 | |
dcheng | 3b4fe47 | 2016-04-08 23:45:13 | [diff] [blame] | 178 | static void SendWebContentsTitleHelper( |
| 179 | const std::string& cache_key, |
| 180 | std::unique_ptr<base::DictionaryValue> dict, |
| 181 | int render_process_id, |
| 182 | int render_frame_id); |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 183 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 184 | const int owner_id_; |
| 185 | const media::AudioLogFactory::AudioComponent component_; |
| 186 | content::MediaInternals* const media_internals_; |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 187 | const int component_id_; |
| 188 | const int render_process_id_; |
| 189 | const int render_frame_id_; |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 190 | |
| 191 | DISALLOW_COPY_AND_ASSIGN(AudioLogImpl); |
| 192 | }; |
| 193 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 194 | MediaInternals::AudioLogImpl::AudioLogImpl( |
| 195 | int owner_id, |
| 196 | media::AudioLogFactory::AudioComponent component, |
| 197 | content::MediaInternals* media_internals, |
| 198 | int component_id, |
| 199 | int render_process_id, |
| 200 | int render_frame_id) |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 201 | : owner_id_(owner_id), |
| 202 | component_(component), |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 203 | media_internals_(media_internals), |
| 204 | component_id_(component_id), |
| 205 | render_process_id_(render_process_id), |
| 206 | render_frame_id_(render_frame_id) {} |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 207 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 208 | MediaInternals::AudioLogImpl::~AudioLogImpl() { |
| 209 | // Ensure log is always closed. This covers the case of crashes in the audio |
| 210 | // service utility process, in which case the log may not be closed |
| 211 | // explicitly. |
| 212 | OnClosed(); |
| 213 | } |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 214 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 215 | void MediaInternals::AudioLogImpl::OnCreated( |
| 216 | const media::AudioParameters& params, |
| 217 | const std::string& device_id) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 218 | base::DictionaryValue dict; |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 219 | StoreComponentMetadata(&dict); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 220 | |
| 221 | dict.SetString(kAudioLogStatusKey, "created"); |
[email protected] | 25d7f89 | 2014-02-13 15:22:45 | [diff] [blame] | 222 | dict.SetString("device_id", device_id); |
dalecurtis | b9a6b787 | 2015-02-06 22:41:55 | [diff] [blame] | 223 | dict.SetString("device_type", FormatToString(params.format())); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 224 | dict.SetInteger("frames_per_buffer", params.frames_per_buffer()); |
| 225 | dict.SetInteger("sample_rate", params.sample_rate()); |
[email protected] | ad82f41 | 2013-11-27 04:20:41 | [diff] [blame] | 226 | dict.SetInteger("channels", params.channels()); |
| 227 | dict.SetString("channel_layout", |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 228 | ChannelLayoutToString(params.channel_layout())); |
[email protected] | 9367e03 | 2014-03-07 19:42:37 | [diff] [blame] | 229 | dict.SetString("effects", EffectsToString(params.effects())); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 230 | |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 231 | media_internals_->UpdateAudioLog(MediaInternals::CREATE, FormatCacheKey(), |
xhwang | 002c154f | 2015-06-16 02:55:54 | [diff] [blame] | 232 | kAudioLogUpdateFunction, &dict); |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 233 | SetWebContentsTitle(); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 234 | } |
| 235 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 236 | void MediaInternals::AudioLogImpl::OnStarted() { |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 237 | SendSingleStringUpdate(kAudioLogStatusKey, "started"); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 238 | } |
| 239 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 240 | void MediaInternals::AudioLogImpl::OnStopped() { |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 241 | SendSingleStringUpdate(kAudioLogStatusKey, "stopped"); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 242 | } |
| 243 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 244 | void MediaInternals::AudioLogImpl::OnClosed() { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 245 | base::DictionaryValue dict; |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 246 | StoreComponentMetadata(&dict); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 247 | dict.SetString(kAudioLogStatusKey, "closed"); |
xhwang | 002c154f | 2015-06-16 02:55:54 | [diff] [blame] | 248 | media_internals_->UpdateAudioLog(MediaInternals::UPDATE_AND_DELETE, |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 249 | FormatCacheKey(), kAudioLogUpdateFunction, |
| 250 | &dict); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 251 | } |
| 252 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 253 | void MediaInternals::AudioLogImpl::OnError() { |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 254 | SendSingleStringUpdate("error_occurred", "true"); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 255 | } |
| 256 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 257 | void MediaInternals::AudioLogImpl::OnSetVolume(double volume) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 258 | base::DictionaryValue dict; |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 259 | StoreComponentMetadata(&dict); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 260 | dict.SetDouble("volume", volume); |
xhwang | 002c154f | 2015-06-16 02:55:54 | [diff] [blame] | 261 | media_internals_->UpdateAudioLog(MediaInternals::UPDATE_IF_EXISTS, |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 262 | FormatCacheKey(), kAudioLogUpdateFunction, |
| 263 | &dict); |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 264 | } |
| 265 | |
Max Morin | 86d4f13 | 2018-08-30 21:51:13 | [diff] [blame] | 266 | void MediaInternals::AudioLogImpl::OnProcessingStateChanged( |
| 267 | const std::string& message) { |
| 268 | SendSingleStringUpdate("processing state", message); |
| 269 | } |
| 270 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 271 | void MediaInternals::AudioLogImpl::OnLogMessage(const std::string& message) { |
guidou | 50db1a6 | 2016-06-01 17:12:17 | [diff] [blame] | 272 | MediaStreamManager::SendMessageToNativeLog(message); |
| 273 | } |
| 274 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 275 | void MediaInternals::AudioLogImpl::SetWebContentsTitle() { |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 276 | if (render_process_id_ < 0 || render_frame_id_ < 0) |
| 277 | return; |
dcheng | 3b4fe47 | 2016-04-08 23:45:13 | [diff] [blame] | 278 | std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 279 | StoreComponentMetadata(dict.get()); |
| 280 | SendWebContentsTitleHelper(FormatCacheKey(), std::move(dict), |
| 281 | render_process_id_, render_frame_id_); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 282 | } |
| 283 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 284 | std::string MediaInternals::AudioLogImpl::FormatCacheKey() { |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 285 | return base::StringPrintf("%d:%d:%d", owner_id_, component_, component_id_); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 286 | } |
| 287 | |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 288 | // static |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 289 | void MediaInternals::AudioLogImpl::SendWebContentsTitleHelper( |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 290 | const std::string& cache_key, |
dcheng | 3b4fe47 | 2016-04-08 23:45:13 | [diff] [blame] | 291 | std::unique_ptr<base::DictionaryValue> dict, |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 292 | int render_process_id, |
| 293 | int render_frame_id) { |
| 294 | // Page title information can only be retrieved from the UI thread. |
| 295 | if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 296 | GetUIThreadTaskRunner({})->PostTask( |
| 297 | FROM_HERE, |
tzik | ccf160c | 2018-02-20 12:43:13 | [diff] [blame] | 298 | base::BindOnce(&SendWebContentsTitleHelper, cache_key, std::move(dict), |
| 299 | render_process_id, render_frame_id)); |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 300 | return; |
| 301 | } |
| 302 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 303 | WebContents* web_contents = WebContents::FromRenderFrameHost( |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 304 | RenderFrameHost::FromID(render_process_id, render_frame_id)); |
| 305 | if (!web_contents) |
| 306 | return; |
| 307 | |
| 308 | // Note: by this point the given audio log entry could have been destroyed, so |
| 309 | // we use UPDATE_IF_EXISTS to discard such instances. |
| 310 | dict->SetInteger("render_process_id", render_process_id); |
| 311 | dict->SetString("web_contents_title", web_contents->GetTitle()); |
xhwang | 002c154f | 2015-06-16 02:55:54 | [diff] [blame] | 312 | MediaInternals::GetInstance()->UpdateAudioLog( |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 313 | MediaInternals::UPDATE_IF_EXISTS, cache_key, kAudioLogUpdateFunction, |
| 314 | dict.get()); |
| 315 | } |
| 316 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 317 | void MediaInternals::AudioLogImpl::SendSingleStringUpdate( |
| 318 | const std::string& key, |
| 319 | const std::string& value) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 320 | base::DictionaryValue dict; |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 321 | StoreComponentMetadata(&dict); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 322 | dict.SetString(key, value); |
xhwang | 002c154f | 2015-06-16 02:55:54 | [diff] [blame] | 323 | media_internals_->UpdateAudioLog(MediaInternals::UPDATE_IF_EXISTS, |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 324 | FormatCacheKey(), kAudioLogUpdateFunction, |
| 325 | &dict); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 326 | } |
| 327 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 328 | void MediaInternals::AudioLogImpl::StoreComponentMetadata( |
| 329 | base::DictionaryValue* dict) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 330 | dict->SetInteger("owner_id", owner_id_); |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 331 | dict->SetInteger("component_id", component_id_); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 332 | dict->SetInteger("component_type", component_); |
| 333 | } |
| 334 | |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 335 | MediaInternals* MediaInternals::GetInstance() { |
dalecurtis | c3af509 | 2017-02-11 02:08:18 | [diff] [blame] | 336 | static content::MediaInternals* internals = new content::MediaInternals(); |
| 337 | return internals; |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 338 | } |
| 339 | |
Ted Meyer | d5885f8 | 2019-07-16 19:19:17 | [diff] [blame] | 340 | MediaInternals::MediaInternals() : can_update_(false), owner_ids_() { |
Dan Sanders | b26585d | 2017-07-12 23:51:30 | [diff] [blame] | 341 | // TODO(sandersd): Is there ever a relevant case where TERMINATED is sent |
| 342 | // without CLOSED also being sent? |
| 343 | registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 344 | NotificationService::AllBrowserContextsAndSources()); |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 345 | registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 346 | NotificationService::AllBrowserContextsAndSources()); |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 347 | } |
| 348 | |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 349 | MediaInternals::~MediaInternals() {} |
| 350 | |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 351 | void MediaInternals::Observe(int type, |
| 352 | const NotificationSource& source, |
| 353 | const NotificationDetails& details) { |
| 354 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 355 | RenderProcessHost* process = Source<RenderProcessHost>(source).ptr(); |
Dan Sanders | b26585d | 2017-07-12 23:51:30 | [diff] [blame] | 356 | // TODO(sandersd): Send a termination event before clearing the log. |
watk | b5983f9 | 2016-08-09 20:47:51 | [diff] [blame] | 357 | saved_events_by_process_.erase(process->GetID()); |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | // Converts the |event| to a |update|. Returns whether the conversion succeeded. |
| 361 | static bool ConvertEventToUpdate(int render_process_id, |
Ted Meyer | 380ca98 | 2020-01-12 01:00:29 | [diff] [blame] | 362 | const media::MediaLogRecord& event, |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 363 | std::u16string* update) { |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 364 | DCHECK(update); |
| 365 | |
| 366 | base::DictionaryValue dict; |
| 367 | dict.SetInteger("renderer", render_process_id); |
| 368 | dict.SetInteger("player", event.id); |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 369 | |
| 370 | // TODO(dalecurtis): This is technically not correct. TimeTicks "can't" be |
| 371 | // converted to to a human readable time format. See base/time/time.h. |
| 372 | const double ticks = event.time.ToInternalValue(); |
| 373 | const double ticks_millis = ticks / base::Time::kMicrosecondsPerMillisecond; |
| 374 | dict.SetDouble("ticksMillis", ticks_millis); |
| 375 | |
Ted Meyer | 0134aed | 2020-01-23 23:56:40 | [diff] [blame] | 376 | base::Value cloned_params = event.params.Clone(); |
| 377 | switch (event.type) { |
| 378 | case media::MediaLogRecord::Type::kMessage: |
| 379 | dict.SetString("type", "MEDIA_LOG_ENTRY"); |
| 380 | break; |
| 381 | case media::MediaLogRecord::Type::kMediaPropertyChange: |
| 382 | dict.SetString("type", "PROPERTY_CHANGE"); |
| 383 | break; |
| 384 | case media::MediaLogRecord::Type::kMediaEventTriggered: { |
| 385 | // Delete the "event" param so that it won't spam the log. |
| 386 | base::Optional<base::Value> exists = cloned_params.ExtractPath("event"); |
| 387 | DCHECK(exists.has_value()); |
| 388 | dict.SetKey("type", std::move(exists.value())); |
| 389 | break; |
| 390 | } |
Ted Meyer | 2a41e24 | 2020-03-20 08:01:27 | [diff] [blame] | 391 | case media::MediaLogRecord::Type::kMediaStatus: |
Ted Meyer | 0134aed | 2020-01-23 23:56:40 | [diff] [blame] | 392 | dict.SetString("type", "PIPELINE_ERROR"); |
| 393 | break; |
| 394 | } |
| 395 | |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 396 | // Convert PipelineStatus to human readable string |
Ted Meyer | 2a41e24 | 2020-03-20 08:01:27 | [diff] [blame] | 397 | if (event.type == media::MediaLogRecord::Type::kMediaStatus) { |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 398 | int status; |
| 399 | if (!event.params.GetInteger("pipeline_error", &status) || |
| 400 | status < static_cast<int>(media::PIPELINE_OK) || |
| 401 | status > static_cast<int>(media::PIPELINE_STATUS_MAX)) { |
| 402 | return false; |
| 403 | } |
| 404 | media::PipelineStatus error = static_cast<media::PipelineStatus>(status); |
| 405 | dict.SetString("params.pipeline_error", |
Xiaohan Wang | 866b0cd | 2019-10-31 16:57:51 | [diff] [blame] | 406 | media::PipelineStatusToString(error)); |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 407 | } else { |
Ted Meyer | 0134aed | 2020-01-23 23:56:40 | [diff] [blame] | 408 | dict.SetKey("params", std::move(cloned_params)); |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | *update = SerializeUpdate("media.onMediaEvent", &dict); |
| 412 | return true; |
| 413 | } |
| 414 | |
[email protected] | 0e7ee58 | 2013-05-04 14:06:59 | [diff] [blame] | 415 | void MediaInternals::OnMediaEvents( |
Dale Curtis | 1adbe6a | 2017-08-02 02:09:13 | [diff] [blame] | 416 | int render_process_id, |
Ted Meyer | 380ca98 | 2020-01-12 01:00:29 | [diff] [blame] | 417 | const std::vector<media::MediaLogRecord>& events) { |
xhwang | 0fdb831 | 2015-06-10 23:15:38 | [diff] [blame] | 418 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 419 | // Notify observers that |event| has occurred. |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 420 | for (const auto& event : events) { |
Dale Curtis | 1adbe6a | 2017-08-02 02:09:13 | [diff] [blame] | 421 | if (CanUpdate()) { |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 422 | std::u16string update; |
Dale Curtis | 1adbe6a | 2017-08-02 02:09:13 | [diff] [blame] | 423 | if (ConvertEventToUpdate(render_process_id, event, &update)) |
| 424 | SendUpdate(update); |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 425 | } |
Dale Curtis | 1adbe6a | 2017-08-02 02:09:13 | [diff] [blame] | 426 | SaveEvent(render_process_id, event); |
[email protected] | 0e7ee58 | 2013-05-04 14:06:59 | [diff] [blame] | 427 | } |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 428 | } |
| 429 | |
danakj | d15a2d8 | 2019-12-10 23:53:07 | [diff] [blame] | 430 | void MediaInternals::AddUpdateCallback(UpdateCallback callback) { |
xhwang | fe338e9 | 2015-06-08 17:36:09 | [diff] [blame] | 431 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
danakj | d15a2d8 | 2019-12-10 23:53:07 | [diff] [blame] | 432 | update_callbacks_.push_back(std::move(callback)); |
xhwang | fe338e9 | 2015-06-08 17:36:09 | [diff] [blame] | 433 | |
| 434 | base::AutoLock auto_lock(lock_); |
| 435 | can_update_ = true; |
Becca Hughes | 1aa4e67 | 2018-10-23 22:09:08 | [diff] [blame] | 436 | audio_focus_helper_.SetEnabled(true); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | void MediaInternals::RemoveUpdateCallback(const UpdateCallback& callback) { |
xhwang | fe338e9 | 2015-06-08 17:36:09 | [diff] [blame] | 440 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 441 | for (size_t i = 0; i < update_callbacks_.size(); ++i) { |
Robert Liao | 634e197 | 2019-06-05 18:03:01 | [diff] [blame] | 442 | if (update_callbacks_[i] == callback) { |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 443 | update_callbacks_.erase(update_callbacks_.begin() + i); |
xhwang | fe338e9 | 2015-06-08 17:36:09 | [diff] [blame] | 444 | break; |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 445 | } |
| 446 | } |
xhwang | fe338e9 | 2015-06-08 17:36:09 | [diff] [blame] | 447 | |
| 448 | base::AutoLock auto_lock(lock_); |
| 449 | can_update_ = !update_callbacks_.empty(); |
Becca Hughes | 1aa4e67 | 2018-10-23 22:09:08 | [diff] [blame] | 450 | audio_focus_helper_.SetEnabled(can_update_); |
xhwang | fe338e9 | 2015-06-08 17:36:09 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | bool MediaInternals::CanUpdate() { |
| 454 | base::AutoLock auto_lock(lock_); |
| 455 | return can_update_; |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 456 | } |
| 457 | |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 458 | void MediaInternals::SendHistoricalMediaEvents() { |
| 459 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
watk | b5983f9 | 2016-08-09 20:47:51 | [diff] [blame] | 460 | for (const auto& saved_events : saved_events_by_process_) { |
| 461 | for (const auto& event : saved_events.second) { |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 462 | std::u16string update; |
watk | b5983f9 | 2016-08-09 20:47:51 | [diff] [blame] | 463 | if (ConvertEventToUpdate(saved_events.first, event, &update)) |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 464 | SendUpdate(update); |
| 465 | } |
| 466 | } |
| 467 | // Do not clear the map/list here so that refreshing the UI or opening a |
| 468 | // second UI still works nicely! |
| 469 | } |
| 470 | |
Henrik Grunell | e7e5291 | 2018-10-18 07:39:53 | [diff] [blame] | 471 | void MediaInternals::SendGeneralAudioInformation() { |
| 472 | base::DictionaryValue audio_info_data; |
| 473 | |
| 474 | // Audio feature information. |
| 475 | auto set_feature_data = [&](auto& feature) { |
| 476 | audio_info_data.SetKey( |
| 477 | feature.name, |
| 478 | base::Value(base::FeatureList::IsEnabled(feature) ? "Enabled" |
| 479 | : "Disabled")); |
| 480 | }; |
Henrik Grunell | 5c1dc36 | 2019-05-20 18:06:17 | [diff] [blame] | 481 | |
Guido Urdaneta | d06ae84 | 2019-10-10 20:04:12 | [diff] [blame] | 482 | auto set_explicit_feature_data = [&](auto& feature, bool feature_value) { |
| 483 | audio_info_data.SetKey(feature.name, |
| 484 | base::Value(feature_value ? "Enabled" : "Disabled")); |
| 485 | }; |
| 486 | |
Henrik Grunell | e7e5291 | 2018-10-18 07:39:53 | [diff] [blame] | 487 | set_feature_data(features::kAudioServiceOutOfProcess); |
Henrik Grunell | 5c1dc36 | 2019-05-20 18:06:17 | [diff] [blame] | 488 | |
| 489 | std::string feature_value_string; |
| 490 | if (base::FeatureList::IsEnabled( |
| 491 | features::kAudioServiceOutOfProcessKillAtHang)) { |
| 492 | std::string timeout_value = base::GetFieldTrialParamValueByFeature( |
| 493 | features::kAudioServiceOutOfProcessKillAtHang, "timeout_seconds"); |
| 494 | if (timeout_value.empty()) |
| 495 | timeout_value = "<undefined>"; |
| 496 | feature_value_string = |
| 497 | base::StrCat({"Enabled, timeout = ", timeout_value, " seconds"}); |
| 498 | } else { |
| 499 | feature_value_string = "Disabled"; |
| 500 | } |
| 501 | audio_info_data.SetKey(features::kAudioServiceOutOfProcessKillAtHang.name, |
| 502 | base::Value(feature_value_string)); |
| 503 | |
Henrik Grunell | e7e5291 | 2018-10-18 07:39:53 | [diff] [blame] | 504 | set_feature_data(features::kAudioServiceLaunchOnStartup); |
Guido Urdaneta | e02b572 | 2020-07-22 05:58:21 | [diff] [blame] | 505 | set_explicit_feature_data( |
| 506 | features::kAudioServiceSandbox, |
| 507 | GetContentClient()->browser()->ShouldSandboxAudioService()); |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 508 | std::u16string audio_info_update = |
Henrik Grunell | e7e5291 | 2018-10-18 07:39:53 | [diff] [blame] | 509 | SerializeUpdate("media.updateGeneralAudioInformation", &audio_info_data); |
| 510 | SendUpdate(audio_info_update); |
| 511 | } |
| 512 | |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 513 | void MediaInternals::SendAudioStreamData() { |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 514 | std::u16string audio_stream_update; |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 515 | { |
| 516 | base::AutoLock auto_lock(lock_); |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 517 | audio_stream_update = SerializeUpdate("media.onReceiveAudioStreamData", |
| 518 | &audio_streams_cached_data_); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 519 | } |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 520 | SendUpdate(audio_stream_update); |
| 521 | } |
| 522 | |
burnik | 7196356 | 2014-10-17 14:57:14 | [diff] [blame] | 523 | void MediaInternals::SendVideoCaptureDeviceCapabilities() { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 524 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
xhwang | fe338e9 | 2015-06-08 17:36:09 | [diff] [blame] | 525 | |
| 526 | if (!CanUpdate()) |
| 527 | return; |
| 528 | |
burnik | 7196356 | 2014-10-17 14:57:14 | [diff] [blame] | 529 | SendUpdate(SerializeUpdate("media.onReceiveVideoCaptureCapabilities", |
| 530 | &video_capture_capabilities_cached_data_)); |
| 531 | } |
| 532 | |
Becca Hughes | d3444394 | 2018-08-17 22:15:08 | [diff] [blame] | 533 | void MediaInternals::SendAudioFocusState() { |
Becca Hughes | 1aa4e67 | 2018-10-23 22:09:08 | [diff] [blame] | 534 | audio_focus_helper_.SendAudioFocusState(); |
Becca Hughes | 8da7a66a | 2018-08-24 21:58:42 | [diff] [blame] | 535 | } |
Becca Hughes | d3444394 | 2018-08-17 22:15:08 | [diff] [blame] | 536 | |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 537 | void MediaInternals::UpdateVideoCaptureDeviceCapabilities( |
chfremer | 1c38eb4 | 2016-08-02 15:54:31 | [diff] [blame] | 538 | const std::vector<std::tuple<media::VideoCaptureDeviceDescriptor, |
| 539 | media::VideoCaptureFormats>>& |
| 540 | descriptors_and_formats) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 541 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
burnik | 7196356 | 2014-10-17 14:57:14 | [diff] [blame] | 542 | video_capture_capabilities_cached_data_.Clear(); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 543 | |
chfremer | 1c38eb4 | 2016-08-02 15:54:31 | [diff] [blame] | 544 | for (const auto& device_format_pair : descriptors_and_formats) { |
Eero Häkkinen | 072d913 | 2020-09-30 22:57:35 | [diff] [blame] | 545 | auto control_support = std::make_unique<base::ListValue>(); |
Jeremy Roman | 04f27c37 | 2017-10-27 15:20:55 | [diff] [blame] | 546 | auto format_list = std::make_unique<base::ListValue>(); |
nisse | 7a153fb | 2015-12-07 09:30:59 | [diff] [blame] | 547 | // TODO(nisse): Representing format information as a string, to be |
| 548 | // parsed by the javascript handler, is brittle. Consider passing |
| 549 | // a list of mappings instead. |
| 550 | |
chfremer | 1c38eb4 | 2016-08-02 15:54:31 | [diff] [blame] | 551 | const media::VideoCaptureDeviceDescriptor& descriptor = |
| 552 | std::get<0>(device_format_pair); |
| 553 | const media::VideoCaptureFormats& supported_formats = |
| 554 | std::get<1>(device_format_pair); |
Eero Häkkinen | 072d913 | 2020-09-30 22:57:35 | [diff] [blame] | 555 | if (descriptor.control_support().pan) |
| 556 | control_support->AppendString("pan"); |
| 557 | if (descriptor.control_support().tilt) |
| 558 | control_support->AppendString("tilt"); |
| 559 | if (descriptor.control_support().zoom) |
| 560 | control_support->AppendString("zoom"); |
chfremer | 1c38eb4 | 2016-08-02 15:54:31 | [diff] [blame] | 561 | for (const auto& format : supported_formats) |
ajose | 5d00e9878 | 2015-07-01 21:09:48 | [diff] [blame] | 562 | format_list->AppendString(media::VideoCaptureFormat::ToString(format)); |
burnik | 7196356 | 2014-10-17 14:57:14 | [diff] [blame] | 563 | |
dcheng | 98e96a7 | 2016-06-11 03:41:48 | [diff] [blame] | 564 | std::unique_ptr<base::DictionaryValue> device_dict( |
| 565 | new base::DictionaryValue()); |
chfremer | 1c38eb4 | 2016-08-02 15:54:31 | [diff] [blame] | 566 | device_dict->SetString("id", descriptor.device_id); |
| 567 | device_dict->SetString("name", descriptor.GetNameAndModel()); |
Eero Häkkinen | 072d913 | 2020-09-30 22:57:35 | [diff] [blame] | 568 | device_dict->Set("controlSupport", std::move(control_support)); |
jdoerrie | 664305c | 2017-06-07 08:34:34 | [diff] [blame] | 569 | device_dict->Set("formats", std::move(format_list)); |
chfremer | 1c38eb4 | 2016-08-02 15:54:31 | [diff] [blame] | 570 | device_dict->SetString("captureApi", descriptor.GetCaptureApiTypeString()); |
dcheng | 98e96a7 | 2016-06-11 03:41:48 | [diff] [blame] | 571 | video_capture_capabilities_cached_data_.Append(std::move(device_dict)); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 572 | } |
burnik | 7196356 | 2014-10-17 14:57:14 | [diff] [blame] | 573 | |
xhwang | fe338e9 | 2015-06-08 17:36:09 | [diff] [blame] | 574 | SendVideoCaptureDeviceCapabilities(); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 575 | } |
| 576 | |
dcheng | 3b4fe47 | 2016-04-08 23:45:13 | [diff] [blame] | 577 | std::unique_ptr<media::AudioLog> MediaInternals::CreateAudioLog( |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 578 | AudioComponent component, |
| 579 | int component_id) { |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 580 | return CreateAudioLogImpl(component, component_id, -1, MSG_ROUTING_NONE); |
[email protected] | e2fd1f7 | 2013-08-16 00:34:25 | [diff] [blame] | 581 | } |
| 582 | |
Miyoung Shin | 4e4b503 | 2019-10-24 13:54:06 | [diff] [blame] | 583 | mojo::PendingRemote<media::mojom::AudioLog> MediaInternals::CreateMojoAudioLog( |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 584 | media::AudioLogFactory::AudioComponent component, |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 585 | int component_id, |
| 586 | int render_process_id, |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 587 | int render_frame_id) { |
Miyoung Shin | 4e4b503 | 2019-10-24 13:54:06 | [diff] [blame] | 588 | mojo::PendingRemote<media::mojom::AudioLog> audio_log; |
| 589 | CreateMojoAudioLog(component, component_id, |
| 590 | audio_log.InitWithNewPipeAndPassReceiver(), |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 591 | render_process_id, render_frame_id); |
Miyoung Shin | 4e4b503 | 2019-10-24 13:54:06 | [diff] [blame] | 592 | return audio_log; |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 593 | } |
| 594 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 595 | void MediaInternals::CreateMojoAudioLog( |
| 596 | media::AudioLogFactory::AudioComponent component, |
| 597 | int component_id, |
Miyoung Shin | 4e4b503 | 2019-10-24 13:54:06 | [diff] [blame] | 598 | mojo::PendingReceiver<media::mojom::AudioLog> receiver, |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 599 | int render_process_id, |
| 600 | int render_frame_id) { |
Miyoung Shin | 4e4b503 | 2019-10-24 13:54:06 | [diff] [blame] | 601 | mojo::MakeSelfOwnedReceiver( |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 602 | CreateAudioLogImpl(component, component_id, render_process_id, |
| 603 | render_frame_id), |
Miyoung Shin | 4e4b503 | 2019-10-24 13:54:06 | [diff] [blame] | 604 | std::move(receiver)); |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 605 | } |
| 606 | |
Abhijeet Kandalkar | aad4e17 | 2020-02-19 19:31:29 | [diff] [blame] | 607 | // static |
| 608 | void MediaInternals::CreateMediaLogRecords( |
| 609 | int render_process_id, |
| 610 | mojo::PendingReceiver<content::mojom::MediaInternalLogRecords> receiver) { |
| 611 | mojo::MakeSelfOwnedReceiver( |
| 612 | std::make_unique<MediaInternalLogRecordsImpl>( |
| 613 | MediaInternals::GetInstance(), render_process_id), |
| 614 | std::move(receiver)); |
| 615 | } |
| 616 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 617 | std::unique_ptr<MediaInternals::AudioLogImpl> |
| 618 | MediaInternals::CreateAudioLogImpl( |
| 619 | media::AudioLogFactory::AudioComponent component, |
| 620 | int component_id, |
| 621 | int render_process_id, |
| 622 | int render_frame_id) { |
| 623 | base::AutoLock auto_lock(lock_); |
| 624 | return std::make_unique<AudioLogImpl>(owner_ids_[component]++, component, |
| 625 | this, component_id, render_process_id, |
| 626 | render_frame_id); |
| 627 | } |
| 628 | |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 629 | void MediaInternals::SendUpdate(const std::u16string& update) { |
xhwang | fe338e9 | 2015-06-08 17:36:09 | [diff] [blame] | 630 | // SendUpdate() may be called from any thread, but must run on the UI thread. |
| 631 | if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 632 | GetUIThreadTaskRunner({})->PostTask( |
| 633 | FROM_HERE, base::BindOnce(&MediaInternals::SendUpdate, |
Sami Kyostila | 8e4d5a9 | 2019-08-02 12:45:05 | [diff] [blame] | 634 | base::Unretained(this), update)); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 635 | return; |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 636 | } |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 637 | |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 638 | for (size_t i = 0; i < update_callbacks_.size(); i++) |
| 639 | update_callbacks_[i].Run(update); |
| 640 | } |
| 641 | |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 642 | void MediaInternals::SaveEvent(int process_id, |
Ted Meyer | 380ca98 | 2020-01-12 01:00:29 | [diff] [blame] | 643 | const media::MediaLogRecord& event) { |
xhwang | 0fdb831 | 2015-06-10 23:15:38 | [diff] [blame] | 644 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Dale Curtis | 1ff5a375a | 2017-06-16 21:38:31 | [diff] [blame] | 645 | auto& saved_events = saved_events_by_process_[process_id]; |
watk | b5983f9 | 2016-08-09 20:47:51 | [diff] [blame] | 646 | saved_events.push_back(event); |
Dale Curtis | 904d79e8 | 2020-11-04 03:14:53 | [diff] [blame] | 647 | if (saved_events.size() > media::MediaLog::kLogLimit) { |
watk | b5983f9 | 2016-08-09 20:47:51 | [diff] [blame] | 648 | // Remove all events for a given player as soon as we have to remove a |
| 649 | // single event for that player to avoid showing incomplete players. |
Dale Curtis | 1ff5a375a | 2017-06-16 21:38:31 | [diff] [blame] | 650 | const int id_to_remove = saved_events.front().id; |
Ted Meyer | 380ca98 | 2020-01-12 01:00:29 | [diff] [blame] | 651 | base::EraseIf(saved_events, [&](const media::MediaLogRecord& event) { |
Yeonwoo Jo | 095a273 | 2018-10-03 17:10:40 | [diff] [blame] | 652 | return event.id == id_to_remove; |
| 653 | }); |
watk | b5983f9 | 2016-08-09 20:47:51 | [diff] [blame] | 654 | } |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 655 | } |
| 656 | |
xhwang | 002c154f | 2015-06-16 02:55:54 | [diff] [blame] | 657 | void MediaInternals::UpdateAudioLog(AudioLogUpdateType type, |
| 658 | const std::string& cache_key, |
| 659 | const std::string& function, |
| 660 | const base::DictionaryValue* value) { |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 661 | { |
| 662 | base::AutoLock auto_lock(lock_); |
| 663 | const bool has_entry = audio_streams_cached_data_.HasKey(cache_key); |
| 664 | if ((type == UPDATE_IF_EXISTS || type == UPDATE_AND_DELETE) && !has_entry) { |
| 665 | return; |
| 666 | } else if (!has_entry) { |
| 667 | DCHECK_EQ(type, CREATE); |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 668 | audio_streams_cached_data_.Set( |
Jeremy Roman | 04f27c37 | 2017-10-27 15:20:55 | [diff] [blame] | 669 | cache_key, std::make_unique<base::Value>(value->Clone())); |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 670 | } else if (type == UPDATE_AND_DELETE) { |
dcheng | 3b4fe47 | 2016-04-08 23:45:13 | [diff] [blame] | 671 | std::unique_ptr<base::Value> out_value; |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 672 | CHECK(audio_streams_cached_data_.Remove(cache_key, &out_value)); |
| 673 | } else { |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 674 | base::DictionaryValue* existing_dict = nullptr; |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 675 | CHECK( |
| 676 | audio_streams_cached_data_.GetDictionary(cache_key, &existing_dict)); |
| 677 | existing_dict->MergeDictionary(value); |
| 678 | } |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 679 | } |
| 680 | |
xhwang | 002c154f | 2015-06-16 02:55:54 | [diff] [blame] | 681 | if (CanUpdate()) |
| 682 | SendUpdate(SerializeUpdate(function, value)); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 683 | } |
| 684 | |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 685 | } // namespace content |