[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 | |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 9 | #include <tuple> |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 10 | #include <utility> |
avi | 7f27756 | 2015-12-25 02:41:26 | [diff] [blame] | 11 | |
Becca Hughes | a0754d7 | 2018-10-09 00:33:40 | [diff] [blame] | 12 | #include "base/containers/adapters.h" |
Henrik Grunell | e7e5291 | 2018-10-18 07:39:53 | [diff] [blame] | 13 | #include "base/feature_list.h" |
avi | 7f27756 | 2015-12-25 02:41:26 | [diff] [blame] | 14 | #include "base/macros.h" |
Dale Curtis | 4a6f54c0 | 2017-06-06 23:32:20 | [diff] [blame] | 15 | #include "base/metrics/histogram_functions.h" |
asvitkine | 8d51e9d | 2016-09-02 23:55:43 | [diff] [blame] | 16 | #include "base/metrics/histogram_macros.h" |
Yeonwoo Jo | 095a273 | 2018-10-03 17:10:40 | [diff] [blame] | 17 | #include "base/stl_util.h" |
[email protected] | 348fbaac | 2013-06-11 06:31:51 | [diff] [blame] | 18 | #include "base/strings/string16.h" |
[email protected] | 9367e03 | 2014-03-07 19:42:37 | [diff] [blame] | 19 | #include "base/strings/string_number_conversions.h" |
[email protected] | 348fbaac | 2013-06-11 06:31:51 | [diff] [blame] | 20 | #include "base/strings/stringprintf.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 21 | #include "base/task/post_task.h" |
avi | 7f27756 | 2015-12-25 02:41:26 | [diff] [blame] | 22 | #include "build/build_config.h" |
Becca Hughes | d3444394 | 2018-08-17 22:15:08 | [diff] [blame] | 23 | #include "content/browser/media/session/media_session_impl.h" |
guidou | 50db1a6 | 2016-06-01 17:12:17 | [diff] [blame] | 24 | #include "content/browser/renderer_host/media/media_stream_manager.h" |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 25 | #include "content/public/browser/browser_context.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 26 | #include "content/public/browser/browser_task_traits.h" |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 27 | #include "content/public/browser/browser_thread.h" |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 28 | #include "content/public/browser/notification_service.h" |
| 29 | #include "content/public/browser/notification_types.h" |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 30 | #include "content/public/browser/render_frame_host.h" |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 31 | #include "content/public/browser/render_process_host.h" |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 32 | #include "content/public/browser/web_contents.h" |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 33 | #include "content/public/browser/web_ui.h" |
Henrik Grunell | e7e5291 | 2018-10-18 07:39:53 | [diff] [blame] | 34 | #include "content/public/common/content_features.h" |
Becca Hughes | 2f7072f | 2018-10-04 00:58:40 | [diff] [blame] | 35 | #include "content/public/common/service_manager_connection.h" |
jrummell | 37a54c0 | 2016-04-22 19:54:27 | [diff] [blame] | 36 | #include "media/base/audio_parameters.h" |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 37 | #include "media/base/media_log_event.h" |
prabhur | 957d46c7 | 2014-11-19 03:01:16 | [diff] [blame] | 38 | #include "media/filters/gpu_video_decoder.h" |
Henrik Grunell | e7e5291 | 2018-10-18 07:39:53 | [diff] [blame] | 39 | #include "media/webrtc/webrtc_switches.h" |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 40 | #include "mojo/public/cpp/bindings/strong_binding.h" |
Henrik Grunell | e7e5291 | 2018-10-18 07:39:53 | [diff] [blame] | 41 | #include "services/service_manager/sandbox/features.h" |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 42 | |
kraush | 4252105 | 2017-05-04 19:14:34 | [diff] [blame] | 43 | #if !defined(OS_ANDROID) |
xhwang | f218939 | 2015-10-19 22:21:51 | [diff] [blame] | 44 | #include "media/filters/decrypting_video_decoder.h" |
| 45 | #endif |
| 46 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 47 | namespace { |
| 48 | |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 49 | base::string16 SerializeUpdate(const std::string& function, |
| 50 | const base::Value* value) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 51 | return content::WebUI::GetJavascriptCall( |
| 52 | function, std::vector<const base::Value*>(1, value)); |
| 53 | } |
| 54 | |
[email protected] | 9367e03 | 2014-03-07 19:42:37 | [diff] [blame] | 55 | std::string EffectsToString(int effects) { |
| 56 | if (effects == media::AudioParameters::NO_EFFECTS) |
| 57 | return "NO_EFFECTS"; |
| 58 | |
| 59 | struct { |
| 60 | int flag; |
| 61 | const char* name; |
| 62 | } flags[] = { |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 63 | {media::AudioParameters::ECHO_CANCELLER, "ECHO_CANCELLER"}, |
| 64 | {media::AudioParameters::DUCKING, "DUCKING"}, |
| 65 | {media::AudioParameters::KEYBOARD_MIC, "KEYBOARD_MIC"}, |
| 66 | {media::AudioParameters::HOTWORD, "HOTWORD"}, |
[email protected] | 9367e03 | 2014-03-07 19:42:37 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | std::string ret; |
viettrungluu | 2dfaba7 | 2014-10-16 05:30:25 | [diff] [blame] | 70 | for (size_t i = 0; i < arraysize(flags); ++i) { |
[email protected] | 9367e03 | 2014-03-07 19:42:37 | [diff] [blame] | 71 | if (effects & flags[i].flag) { |
| 72 | if (!ret.empty()) |
| 73 | ret += " | "; |
| 74 | ret += flags[i].name; |
| 75 | effects &= ~flags[i].flag; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if (effects) { |
| 80 | if (!ret.empty()) |
| 81 | ret += " | "; |
| 82 | ret += base::IntToString(effects); |
| 83 | } |
| 84 | |
| 85 | return ret; |
| 86 | } |
| 87 | |
dalecurtis | b9a6b787 | 2015-02-06 22:41:55 | [diff] [blame] | 88 | std::string FormatToString(media::AudioParameters::Format format) { |
| 89 | switch (format) { |
| 90 | case media::AudioParameters::AUDIO_PCM_LINEAR: |
| 91 | return "pcm_linear"; |
| 92 | case media::AudioParameters::AUDIO_PCM_LOW_LATENCY: |
| 93 | return "pcm_low_latency"; |
tsunghung | 59327d6 | 2016-11-19 16:09:48 | [diff] [blame] | 94 | case media::AudioParameters::AUDIO_BITSTREAM_AC3: |
| 95 | return "ac3"; |
| 96 | case media::AudioParameters::AUDIO_BITSTREAM_EAC3: |
| 97 | return "eac3"; |
dalecurtis | b9a6b787 | 2015-02-06 22:41:55 | [diff] [blame] | 98 | case media::AudioParameters::AUDIO_FAKE: |
| 99 | return "fake"; |
dalecurtis | b9a6b787 | 2015-02-06 22:41:55 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | NOTREACHED(); |
| 103 | return "unknown"; |
| 104 | } |
| 105 | |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 106 | // Whether the player is in incognito mode or ChromeOS guest mode. |
| 107 | bool IsIncognito(int render_process_id) { |
| 108 | content::RenderProcessHost* render_process_host = |
| 109 | content::RenderProcessHost::FromID(render_process_id); |
| 110 | if (!render_process_host) { |
| 111 | // This could happen in tests. |
| 112 | LOG(ERROR) << "Cannot get RenderProcessHost"; |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | content::BrowserContext* browser_context = |
| 117 | render_process_host->GetBrowserContext(); |
| 118 | DCHECK(browser_context); |
| 119 | |
| 120 | return browser_context->IsOffTheRecord(); |
| 121 | } |
| 122 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 123 | const char kAudioLogStatusKey[] = "status"; |
| 124 | const char kAudioLogUpdateFunction[] = "media.updateAudioComponent"; |
| 125 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 126 | } // namespace |
| 127 | |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 128 | namespace content { |
| 129 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 130 | class MediaInternals::AudioLogImpl : public media::mojom::AudioLog, |
| 131 | public media::AudioLog { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 132 | public: |
| 133 | AudioLogImpl(int owner_id, |
| 134 | media::AudioLogFactory::AudioComponent component, |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 135 | content::MediaInternals* media_internals, |
| 136 | int component_id, |
| 137 | int render_process_id, |
| 138 | int render_frame_id); |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 139 | ~AudioLogImpl() override; |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 140 | |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 141 | void OnCreated(const media::AudioParameters& params, |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 142 | const std::string& device_id) override; |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 143 | void OnStarted() override; |
| 144 | void OnStopped() override; |
| 145 | void OnClosed() override; |
| 146 | void OnError() override; |
| 147 | void OnSetVolume(double volume) override; |
| 148 | void OnLogMessage(const std::string& message) override; |
Max Morin | 86d4f13 | 2018-08-30 21:51:13 | [diff] [blame] | 149 | void OnProcessingStateChanged(const std::string& message) override; |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 150 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 151 | private: |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 152 | // If possible, i.e. a WebContents exists for the given RenderFrameHostID, |
| 153 | // tells an existing AudioLogEntry the WebContents title for easier |
| 154 | // differentiation on the UI. Note that the log entry must be created (by |
| 155 | // calling OnCreated() before calling this method. |
| 156 | void SetWebContentsTitle(); |
| 157 | |
| 158 | void SendSingleStringUpdate(const std::string& key, const std::string& value); |
| 159 | void StoreComponentMetadata(base::DictionaryValue* dict); |
| 160 | std::string FormatCacheKey(); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 161 | |
dcheng | 3b4fe47 | 2016-04-08 23:45:13 | [diff] [blame] | 162 | static void SendWebContentsTitleHelper( |
| 163 | const std::string& cache_key, |
| 164 | std::unique_ptr<base::DictionaryValue> dict, |
| 165 | int render_process_id, |
| 166 | int render_frame_id); |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 167 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 168 | const int owner_id_; |
| 169 | const media::AudioLogFactory::AudioComponent component_; |
| 170 | content::MediaInternals* const media_internals_; |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 171 | const int component_id_; |
| 172 | const int render_process_id_; |
| 173 | const int render_frame_id_; |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 174 | |
| 175 | DISALLOW_COPY_AND_ASSIGN(AudioLogImpl); |
| 176 | }; |
| 177 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 178 | MediaInternals::AudioLogImpl::AudioLogImpl( |
| 179 | int owner_id, |
| 180 | media::AudioLogFactory::AudioComponent component, |
| 181 | content::MediaInternals* media_internals, |
| 182 | int component_id, |
| 183 | int render_process_id, |
| 184 | int render_frame_id) |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 185 | : owner_id_(owner_id), |
| 186 | component_(component), |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 187 | media_internals_(media_internals), |
| 188 | component_id_(component_id), |
| 189 | render_process_id_(render_process_id), |
| 190 | render_frame_id_(render_frame_id) {} |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 191 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 192 | MediaInternals::AudioLogImpl::~AudioLogImpl() { |
| 193 | // Ensure log is always closed. This covers the case of crashes in the audio |
| 194 | // service utility process, in which case the log may not be closed |
| 195 | // explicitly. |
| 196 | OnClosed(); |
| 197 | } |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 198 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 199 | void MediaInternals::AudioLogImpl::OnCreated( |
| 200 | const media::AudioParameters& params, |
| 201 | const std::string& device_id) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 202 | base::DictionaryValue dict; |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 203 | StoreComponentMetadata(&dict); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 204 | |
| 205 | dict.SetString(kAudioLogStatusKey, "created"); |
[email protected] | 25d7f89 | 2014-02-13 15:22:45 | [diff] [blame] | 206 | dict.SetString("device_id", device_id); |
dalecurtis | b9a6b787 | 2015-02-06 22:41:55 | [diff] [blame] | 207 | dict.SetString("device_type", FormatToString(params.format())); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 208 | dict.SetInteger("frames_per_buffer", params.frames_per_buffer()); |
| 209 | dict.SetInteger("sample_rate", params.sample_rate()); |
[email protected] | ad82f41 | 2013-11-27 04:20:41 | [diff] [blame] | 210 | dict.SetInteger("channels", params.channels()); |
| 211 | dict.SetString("channel_layout", |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 212 | ChannelLayoutToString(params.channel_layout())); |
[email protected] | 9367e03 | 2014-03-07 19:42:37 | [diff] [blame] | 213 | dict.SetString("effects", EffectsToString(params.effects())); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 214 | |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 215 | media_internals_->UpdateAudioLog(MediaInternals::CREATE, FormatCacheKey(), |
xhwang | 002c154f | 2015-06-16 02:55:54 | [diff] [blame] | 216 | kAudioLogUpdateFunction, &dict); |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 217 | SetWebContentsTitle(); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 218 | } |
| 219 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 220 | void MediaInternals::AudioLogImpl::OnStarted() { |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 221 | SendSingleStringUpdate(kAudioLogStatusKey, "started"); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 222 | } |
| 223 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 224 | void MediaInternals::AudioLogImpl::OnStopped() { |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 225 | SendSingleStringUpdate(kAudioLogStatusKey, "stopped"); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 226 | } |
| 227 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 228 | void MediaInternals::AudioLogImpl::OnClosed() { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 229 | base::DictionaryValue dict; |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 230 | StoreComponentMetadata(&dict); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 231 | dict.SetString(kAudioLogStatusKey, "closed"); |
xhwang | 002c154f | 2015-06-16 02:55:54 | [diff] [blame] | 232 | media_internals_->UpdateAudioLog(MediaInternals::UPDATE_AND_DELETE, |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 233 | FormatCacheKey(), kAudioLogUpdateFunction, |
| 234 | &dict); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 235 | } |
| 236 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 237 | void MediaInternals::AudioLogImpl::OnError() { |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 238 | SendSingleStringUpdate("error_occurred", "true"); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 239 | } |
| 240 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 241 | void MediaInternals::AudioLogImpl::OnSetVolume(double volume) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 242 | base::DictionaryValue dict; |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 243 | StoreComponentMetadata(&dict); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 244 | dict.SetDouble("volume", volume); |
xhwang | 002c154f | 2015-06-16 02:55:54 | [diff] [blame] | 245 | media_internals_->UpdateAudioLog(MediaInternals::UPDATE_IF_EXISTS, |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 246 | FormatCacheKey(), kAudioLogUpdateFunction, |
| 247 | &dict); |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 248 | } |
| 249 | |
Max Morin | 86d4f13 | 2018-08-30 21:51:13 | [diff] [blame] | 250 | void MediaInternals::AudioLogImpl::OnProcessingStateChanged( |
| 251 | const std::string& message) { |
| 252 | SendSingleStringUpdate("processing state", message); |
| 253 | } |
| 254 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 255 | void MediaInternals::AudioLogImpl::OnLogMessage(const std::string& message) { |
guidou | 50db1a6 | 2016-06-01 17:12:17 | [diff] [blame] | 256 | MediaStreamManager::SendMessageToNativeLog(message); |
| 257 | } |
| 258 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 259 | void MediaInternals::AudioLogImpl::SetWebContentsTitle() { |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 260 | if (render_process_id_ < 0 || render_frame_id_ < 0) |
| 261 | return; |
dcheng | 3b4fe47 | 2016-04-08 23:45:13 | [diff] [blame] | 262 | std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 263 | StoreComponentMetadata(dict.get()); |
| 264 | SendWebContentsTitleHelper(FormatCacheKey(), std::move(dict), |
| 265 | render_process_id_, render_frame_id_); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 266 | } |
| 267 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 268 | std::string MediaInternals::AudioLogImpl::FormatCacheKey() { |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 269 | return base::StringPrintf("%d:%d:%d", owner_id_, component_, component_id_); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 270 | } |
| 271 | |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 272 | // static |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 273 | void MediaInternals::AudioLogImpl::SendWebContentsTitleHelper( |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 274 | const std::string& cache_key, |
dcheng | 3b4fe47 | 2016-04-08 23:45:13 | [diff] [blame] | 275 | std::unique_ptr<base::DictionaryValue> dict, |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 276 | int render_process_id, |
| 277 | int render_frame_id) { |
| 278 | // Page title information can only be retrieved from the UI thread. |
| 279 | if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 280 | base::PostTaskWithTraits( |
| 281 | FROM_HERE, {BrowserThread::UI}, |
tzik | ccf160c | 2018-02-20 12:43:13 | [diff] [blame] | 282 | base::BindOnce(&SendWebContentsTitleHelper, cache_key, std::move(dict), |
| 283 | render_process_id, render_frame_id)); |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 284 | return; |
| 285 | } |
| 286 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame^] | 287 | WebContents* web_contents = WebContents::FromRenderFrameHost( |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 288 | RenderFrameHost::FromID(render_process_id, render_frame_id)); |
| 289 | if (!web_contents) |
| 290 | return; |
| 291 | |
| 292 | // Note: by this point the given audio log entry could have been destroyed, so |
| 293 | // we use UPDATE_IF_EXISTS to discard such instances. |
| 294 | dict->SetInteger("render_process_id", render_process_id); |
| 295 | dict->SetString("web_contents_title", web_contents->GetTitle()); |
xhwang | 002c154f | 2015-06-16 02:55:54 | [diff] [blame] | 296 | MediaInternals::GetInstance()->UpdateAudioLog( |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 297 | MediaInternals::UPDATE_IF_EXISTS, cache_key, kAudioLogUpdateFunction, |
| 298 | dict.get()); |
| 299 | } |
| 300 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 301 | void MediaInternals::AudioLogImpl::SendSingleStringUpdate( |
| 302 | const std::string& key, |
| 303 | const std::string& value) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 304 | base::DictionaryValue dict; |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 305 | StoreComponentMetadata(&dict); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 306 | dict.SetString(key, value); |
xhwang | 002c154f | 2015-06-16 02:55:54 | [diff] [blame] | 307 | media_internals_->UpdateAudioLog(MediaInternals::UPDATE_IF_EXISTS, |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 308 | FormatCacheKey(), kAudioLogUpdateFunction, |
| 309 | &dict); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 310 | } |
| 311 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 312 | void MediaInternals::AudioLogImpl::StoreComponentMetadata( |
| 313 | base::DictionaryValue* dict) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 314 | dict->SetInteger("owner_id", owner_id_); |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 315 | dict->SetInteger("component_id", component_id_); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 316 | dict->SetInteger("component_type", component_); |
| 317 | } |
| 318 | |
xhwang | 0fdb831 | 2015-06-10 23:15:38 | [diff] [blame] | 319 | // This class lives on the browser UI thread. |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 320 | class MediaInternals::MediaInternalsUMAHandler { |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 321 | public: |
Dale Curtis | 1adbe6a | 2017-08-02 02:09:13 | [diff] [blame] | 322 | MediaInternalsUMAHandler(); |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 323 | |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 324 | // Called when a render process is terminated. Reports the pipeline status to |
| 325 | // UMA for every player associated with the renderer process and then deletes |
| 326 | // the player state. |
| 327 | void OnProcessTerminated(int render_process_id); |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 328 | |
| 329 | // Helper function to save the event payload to RendererPlayerMap. |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 330 | void SavePlayerState(int render_process_id, |
| 331 | const media::MediaLogEvent& event); |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 332 | |
| 333 | private: |
| 334 | struct PipelineInfo { |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 335 | explicit PipelineInfo(bool is_incognito) : is_incognito(is_incognito) {} |
| 336 | |
dalecurtis | a14620dd | 2016-02-27 02:13:25 | [diff] [blame] | 337 | bool has_pipeline = false; |
dalecurtis | c58ff8e | 2017-02-22 18:20:45 | [diff] [blame] | 338 | bool has_ever_played = false; |
| 339 | bool has_reached_have_enough = false; |
dalecurtis | a14620dd | 2016-02-27 02:13:25 | [diff] [blame] | 340 | media::PipelineStatus last_pipeline_status = media::PIPELINE_OK; |
| 341 | bool has_audio = false; |
| 342 | bool has_video = false; |
| 343 | bool video_dds = false; |
| 344 | bool video_decoder_changed = false; |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 345 | bool has_cdm = false; |
| 346 | bool is_incognito = false; |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 347 | std::string audio_codec_name; |
| 348 | std::string video_codec_name; |
| 349 | std::string video_decoder; |
Dan Sanders | fbf1006 | 2018-07-17 19:56:48 | [diff] [blame] | 350 | bool is_platform_video_decoder = false; |
dalecurtis | 2cff7f37 | 2017-05-24 08:30:08 | [diff] [blame] | 351 | GURL origin_url; |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 352 | }; |
| 353 | |
| 354 | // Helper function to report PipelineStatus associated with a player to UMA. |
| 355 | void ReportUMAForPipelineStatus(const PipelineInfo& player_info); |
| 356 | |
prabhur | c481239 | 2014-12-05 19:59:42 | [diff] [blame] | 357 | // Helper to generate PipelineStatus UMA name for AudioVideo streams. |
| 358 | std::string GetUMANameForAVStream(const PipelineInfo& player_info); |
| 359 | |
xhwang | 0fdb831 | 2015-06-10 23:15:38 | [diff] [blame] | 360 | // Key is player id. |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 361 | typedef std::map<int, PipelineInfo> PlayerInfoMap; |
| 362 | |
xhwang | 0fdb831 | 2015-06-10 23:15:38 | [diff] [blame] | 363 | // Key is renderer id. |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 364 | typedef std::map<int, PlayerInfoMap> RendererPlayerMap; |
| 365 | |
xhwang | 0fdb831 | 2015-06-10 23:15:38 | [diff] [blame] | 366 | // Stores player information per renderer. |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 367 | RendererPlayerMap renderer_info_; |
| 368 | |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 369 | DISALLOW_COPY_AND_ASSIGN(MediaInternalsUMAHandler); |
| 370 | }; |
| 371 | |
Dale Curtis | 1adbe6a | 2017-08-02 02:09:13 | [diff] [blame] | 372 | MediaInternals::MediaInternalsUMAHandler::MediaInternalsUMAHandler() {} |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 373 | |
| 374 | void MediaInternals::MediaInternalsUMAHandler::SavePlayerState( |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 375 | int render_process_id, |
| 376 | const media::MediaLogEvent& event) { |
xhwang | 0fdb831 | 2015-06-10 23:15:38 | [diff] [blame] | 377 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 378 | |
| 379 | PlayerInfoMap& player_info_map = renderer_info_[render_process_id]; |
| 380 | |
| 381 | auto it = player_info_map.find(event.id); |
| 382 | if (it == player_info_map.end()) { |
| 383 | bool success = false; |
| 384 | std::tie(it, success) = player_info_map.emplace( |
| 385 | std::make_pair(event.id, PipelineInfo(IsIncognito(render_process_id)))); |
| 386 | if (!success) { |
| 387 | LOG(ERROR) << "Failed to insert a new PipelineInfo."; |
| 388 | return; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | PipelineInfo& player_info = it->second; |
| 393 | |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 394 | switch (event.type) { |
dalecurtis | 2cff7f37 | 2017-05-24 08:30:08 | [diff] [blame] | 395 | case media::MediaLogEvent::Type::WEBMEDIAPLAYER_CREATED: { |
| 396 | std::string origin_url; |
| 397 | event.params.GetString("origin_url", &origin_url); |
| 398 | player_info.origin_url = GURL(origin_url); |
| 399 | break; |
| 400 | } |
dalecurtis | c58ff8e | 2017-02-22 18:20:45 | [diff] [blame] | 401 | case media::MediaLogEvent::PLAY: { |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 402 | player_info.has_ever_played = true; |
dalecurtis | c58ff8e | 2017-02-22 18:20:45 | [diff] [blame] | 403 | break; |
| 404 | } |
dalecurtis | a14620dd | 2016-02-27 02:13:25 | [diff] [blame] | 405 | case media::MediaLogEvent::PIPELINE_STATE_CHANGED: { |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 406 | player_info.has_pipeline = true; |
dalecurtis | a14620dd | 2016-02-27 02:13:25 | [diff] [blame] | 407 | break; |
| 408 | } |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 409 | case media::MediaLogEvent::PIPELINE_ERROR: { |
Xiaohan Wang | 007b1b42 | 2017-11-01 18:55:18 | [diff] [blame] | 410 | int status = static_cast<media::PipelineStatus>(media::PIPELINE_OK); |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 411 | event.params.GetInteger("pipeline_error", &status); |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 412 | player_info.last_pipeline_status = |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 413 | static_cast<media::PipelineStatus>(status); |
| 414 | break; |
| 415 | } |
| 416 | case media::MediaLogEvent::PROPERTY_CHANGE: |
| 417 | if (event.params.HasKey("found_audio_stream")) { |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 418 | event.params.GetBoolean("found_audio_stream", &player_info.has_audio); |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 419 | } |
| 420 | if (event.params.HasKey("found_video_stream")) { |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 421 | event.params.GetBoolean("found_video_stream", &player_info.has_video); |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 422 | } |
| 423 | if (event.params.HasKey("audio_codec_name")) { |
| 424 | event.params.GetString("audio_codec_name", |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 425 | &player_info.audio_codec_name); |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 426 | } |
| 427 | if (event.params.HasKey("video_codec_name")) { |
| 428 | event.params.GetString("video_codec_name", |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 429 | &player_info.video_codec_name); |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 430 | } |
| 431 | if (event.params.HasKey("video_decoder")) { |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 432 | std::string previous_video_decoder(player_info.video_decoder); |
| 433 | event.params.GetString("video_decoder", &player_info.video_decoder); |
watk | c85d60e7 | 2015-01-14 19:08:28 | [diff] [blame] | 434 | if (!previous_video_decoder.empty() && |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 435 | previous_video_decoder != player_info.video_decoder) { |
| 436 | player_info.video_decoder_changed = true; |
watk | c85d60e7 | 2015-01-14 19:08:28 | [diff] [blame] | 437 | } |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 438 | } |
Dan Sanders | fbf1006 | 2018-07-17 19:56:48 | [diff] [blame] | 439 | if (event.params.HasKey("is_platform_video_decoder")) { |
| 440 | event.params.GetBoolean("is_platform_video_decoder", |
| 441 | &player_info.is_platform_video_decoder); |
| 442 | } |
prabhur | c481239 | 2014-12-05 19:59:42 | [diff] [blame] | 443 | if (event.params.HasKey("video_dds")) { |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 444 | event.params.GetBoolean("video_dds", &player_info.video_dds); |
| 445 | } |
| 446 | if (event.params.HasKey("has_cdm")) { |
| 447 | event.params.GetBoolean("has_cdm", &player_info.has_cdm); |
prabhur | c481239 | 2014-12-05 19:59:42 | [diff] [blame] | 448 | } |
dalecurtis | c58ff8e | 2017-02-22 18:20:45 | [diff] [blame] | 449 | if (event.params.HasKey("pipeline_buffering_state")) { |
| 450 | std::string buffering_state; |
| 451 | event.params.GetString("pipeline_buffering_state", &buffering_state); |
Dale Curtis | ff57655 | 2018-03-30 02:32:44 | [diff] [blame] | 452 | |
| 453 | bool for_suspended_start; |
| 454 | event.params.GetBoolean("for_suspended_start", &for_suspended_start); |
| 455 | |
| 456 | // Ignore the BUFFERING_HAVE_ENOUGH event if it was for a suspended |
| 457 | // start. Otherwise we won't reflect reductions to the HasEverPlayed |
| 458 | // statistic. |
| 459 | if (buffering_state == "BUFFERING_HAVE_ENOUGH" && !for_suspended_start) |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 460 | player_info.has_reached_have_enough = true; |
dalecurtis | c58ff8e | 2017-02-22 18:20:45 | [diff] [blame] | 461 | } |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 462 | break; |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 463 | case media::MediaLogEvent::Type::WEBMEDIAPLAYER_DESTROYED: { |
| 464 | // Upon player destruction report UMA data; if the player is not torn down |
| 465 | // before process exit, it will be logged during OnProcessTerminated(). |
Dave Tapuska | 4b798010 | 2018-01-05 20:05:45 | [diff] [blame] | 466 | ReportUMAForPipelineStatus(player_info); |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 467 | player_info_map.erase(it); |
Nico Weber | 147d1ff | 2018-01-29 19:29:37 | [diff] [blame] | 468 | break; |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 469 | } |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 470 | default: |
| 471 | break; |
| 472 | } |
| 473 | return; |
| 474 | } |
| 475 | |
prabhur | c481239 | 2014-12-05 19:59:42 | [diff] [blame] | 476 | std::string MediaInternals::MediaInternalsUMAHandler::GetUMANameForAVStream( |
| 477 | const PipelineInfo& player_info) { |
xhwang | 0fdb831 | 2015-06-10 23:15:38 | [diff] [blame] | 478 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
prabhur | c481239 | 2014-12-05 19:59:42 | [diff] [blame] | 479 | static const char kPipelineUmaPrefix[] = "Media.PipelineStatus.AudioVideo."; |
| 480 | std::string uma_name = kPipelineUmaPrefix; |
| 481 | if (player_info.video_codec_name == "vp8") { |
| 482 | uma_name += "VP8."; |
| 483 | } else if (player_info.video_codec_name == "vp9") { |
| 484 | uma_name += "VP9."; |
| 485 | } else if (player_info.video_codec_name == "h264") { |
| 486 | uma_name += "H264."; |
| 487 | } else { |
| 488 | return uma_name + "Other"; |
| 489 | } |
| 490 | |
kraush | 4252105 | 2017-05-04 19:14:34 | [diff] [blame] | 491 | #if !defined(OS_ANDROID) |
prabhur | c481239 | 2014-12-05 19:59:42 | [diff] [blame] | 492 | if (player_info.video_decoder == |
| 493 | media::DecryptingVideoDecoder::kDecoderName) { |
| 494 | return uma_name + "DVD"; |
| 495 | } |
xhwang | f218939 | 2015-10-19 22:21:51 | [diff] [blame] | 496 | #endif |
prabhur | c481239 | 2014-12-05 19:59:42 | [diff] [blame] | 497 | |
| 498 | if (player_info.video_dds) { |
| 499 | uma_name += "DDS."; |
| 500 | } |
| 501 | |
Dan Sanders | fbf1006 | 2018-07-17 19:56:48 | [diff] [blame] | 502 | // Note that HW essentailly means 'platform' anyway. MediaCodec has been |
| 503 | // reported as HW forever, regardless of the underlying platform |
| 504 | // implementation. |
| 505 | if (player_info.is_platform_video_decoder) { |
prabhur | c481239 | 2014-12-05 19:59:42 | [diff] [blame] | 506 | uma_name += "HW"; |
| 507 | } else { |
| 508 | uma_name += "SW"; |
| 509 | } |
| 510 | return uma_name; |
| 511 | } |
| 512 | |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 513 | // TODO(xhwang): This function reports more metrics than just pipeline status |
| 514 | // and should be renamed. Similarly, PipelineInfo should be PlayerInfo. |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 515 | void MediaInternals::MediaInternalsUMAHandler::ReportUMAForPipelineStatus( |
| 516 | const PipelineInfo& player_info) { |
xhwang | 0fdb831 | 2015-06-10 23:15:38 | [diff] [blame] | 517 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
dalecurtis | a14620dd | 2016-02-27 02:13:25 | [diff] [blame] | 518 | |
| 519 | // Don't log pipeline status for players which don't actually have a pipeline; |
| 520 | // e.g., the Android MediaSourcePlayer implementation. |
| 521 | if (!player_info.has_pipeline) |
| 522 | return; |
| 523 | |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 524 | if (player_info.has_video && player_info.has_audio) { |
Dale Curtis | 4a6f54c0 | 2017-06-06 23:32:20 | [diff] [blame] | 525 | base::UmaHistogramExactLinear(GetUMANameForAVStream(player_info), |
| 526 | player_info.last_pipeline_status, |
| 527 | media::PIPELINE_STATUS_MAX); |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 528 | } else if (player_info.has_audio) { |
prabhur | dc15c40 | 2014-11-21 22:53:01 | [diff] [blame] | 529 | UMA_HISTOGRAM_ENUMERATION("Media.PipelineStatus.AudioOnly", |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 530 | player_info.last_pipeline_status, |
| 531 | media::PIPELINE_STATUS_MAX + 1); |
| 532 | } else if (player_info.has_video) { |
prabhur | dc15c40 | 2014-11-21 22:53:01 | [diff] [blame] | 533 | UMA_HISTOGRAM_ENUMERATION("Media.PipelineStatus.VideoOnly", |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 534 | player_info.last_pipeline_status, |
| 535 | media::PIPELINE_STATUS_MAX + 1); |
| 536 | } else { |
dalecurtis | 69da898e | 2016-03-18 21:26:37 | [diff] [blame] | 537 | // Note: This metric can be recorded as a result of normal operation with |
| 538 | // Media Source Extensions. If a site creates a MediaSource object but never |
| 539 | // creates a source buffer or appends data, PIPELINE_OK will be recorded. |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 540 | UMA_HISTOGRAM_ENUMERATION("Media.PipelineStatus.Unsupported", |
| 541 | player_info.last_pipeline_status, |
| 542 | media::PIPELINE_STATUS_MAX + 1); |
| 543 | } |
watk | c85d60e7 | 2015-01-14 19:08:28 | [diff] [blame] | 544 | // Report whether video decoder fallback happened, but only if a video decoder |
| 545 | // was reported. |
| 546 | if (!player_info.video_decoder.empty()) { |
| 547 | UMA_HISTOGRAM_BOOLEAN("Media.VideoDecoderFallback", |
| 548 | player_info.video_decoder_changed); |
| 549 | } |
dalecurtis | c58ff8e | 2017-02-22 18:20:45 | [diff] [blame] | 550 | |
| 551 | // Report whether this player ever saw a playback event. Used to measure the |
| 552 | // effectiveness of efforts to reduce loaded-but-never-used players. |
| 553 | if (player_info.has_reached_have_enough) |
| 554 | UMA_HISTOGRAM_BOOLEAN("Media.HasEverPlayed", player_info.has_ever_played); |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 555 | |
| 556 | // Report whether an encrypted playback is in incognito window, excluding |
| 557 | // never-used players. |
| 558 | if (player_info.has_cdm && player_info.has_ever_played) |
| 559 | UMA_HISTOGRAM_BOOLEAN("Media.EME.IsIncognito", player_info.is_incognito); |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 560 | } |
| 561 | |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 562 | void MediaInternals::MediaInternalsUMAHandler::OnProcessTerminated( |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 563 | int render_process_id) { |
xhwang | 0fdb831 | 2015-06-10 23:15:38 | [diff] [blame] | 564 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 565 | |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 566 | auto players_it = renderer_info_.find(render_process_id); |
| 567 | if (players_it == renderer_info_.end()) |
| 568 | return; |
| 569 | auto it = players_it->second.begin(); |
| 570 | while (it != players_it->second.end()) { |
| 571 | ReportUMAForPipelineStatus(it->second); |
| 572 | players_it->second.erase(it++); |
| 573 | } |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 574 | renderer_info_.erase(players_it); |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 575 | } |
| 576 | |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 577 | MediaInternals* MediaInternals::GetInstance() { |
dalecurtis | c3af509 | 2017-02-11 02:08:18 | [diff] [blame] | 578 | static content::MediaInternals* internals = new content::MediaInternals(); |
| 579 | return internals; |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 580 | } |
| 581 | |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 582 | MediaInternals::MediaInternals() |
xhwang | fe338e9 | 2015-06-08 17:36:09 | [diff] [blame] | 583 | : can_update_(false), |
| 584 | owner_ids_(), |
Dale Curtis | 1adbe6a | 2017-08-02 02:09:13 | [diff] [blame] | 585 | uma_handler_(new MediaInternalsUMAHandler()) { |
Dan Sanders | b26585d | 2017-07-12 23:51:30 | [diff] [blame] | 586 | // TODO(sandersd): Is there ever a relevant case where TERMINATED is sent |
| 587 | // without CLOSED also being sent? |
| 588 | registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 589 | NotificationService::AllBrowserContextsAndSources()); |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 590 | registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 591 | NotificationService::AllBrowserContextsAndSources()); |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 592 | } |
| 593 | |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 594 | MediaInternals::~MediaInternals() {} |
| 595 | |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 596 | void MediaInternals::Observe(int type, |
| 597 | const NotificationSource& source, |
| 598 | const NotificationDetails& details) { |
| 599 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 600 | RenderProcessHost* process = Source<RenderProcessHost>(source).ptr(); |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 601 | uma_handler_->OnProcessTerminated(process->GetID()); |
Dan Sanders | b26585d | 2017-07-12 23:51:30 | [diff] [blame] | 602 | // TODO(sandersd): Send a termination event before clearing the log. |
watk | b5983f9 | 2016-08-09 20:47:51 | [diff] [blame] | 603 | saved_events_by_process_.erase(process->GetID()); |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | // Converts the |event| to a |update|. Returns whether the conversion succeeded. |
| 607 | static bool ConvertEventToUpdate(int render_process_id, |
| 608 | const media::MediaLogEvent& event, |
| 609 | base::string16* update) { |
| 610 | DCHECK(update); |
| 611 | |
| 612 | base::DictionaryValue dict; |
| 613 | dict.SetInteger("renderer", render_process_id); |
| 614 | dict.SetInteger("player", event.id); |
| 615 | dict.SetString("type", media::MediaLog::EventTypeToString(event.type)); |
| 616 | |
| 617 | // TODO(dalecurtis): This is technically not correct. TimeTicks "can't" be |
| 618 | // converted to to a human readable time format. See base/time/time.h. |
| 619 | const double ticks = event.time.ToInternalValue(); |
| 620 | const double ticks_millis = ticks / base::Time::kMicrosecondsPerMillisecond; |
| 621 | dict.SetDouble("ticksMillis", ticks_millis); |
| 622 | |
| 623 | // Convert PipelineStatus to human readable string |
| 624 | if (event.type == media::MediaLogEvent::PIPELINE_ERROR) { |
| 625 | int status; |
| 626 | if (!event.params.GetInteger("pipeline_error", &status) || |
| 627 | status < static_cast<int>(media::PIPELINE_OK) || |
| 628 | status > static_cast<int>(media::PIPELINE_STATUS_MAX)) { |
| 629 | return false; |
| 630 | } |
| 631 | media::PipelineStatus error = static_cast<media::PipelineStatus>(status); |
| 632 | dict.SetString("params.pipeline_error", |
| 633 | media::MediaLog::PipelineStatusToString(error)); |
| 634 | } else { |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 635 | dict.SetKey("params", event.params.Clone()); |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | *update = SerializeUpdate("media.onMediaEvent", &dict); |
| 639 | return true; |
| 640 | } |
| 641 | |
[email protected] | 0e7ee58 | 2013-05-04 14:06:59 | [diff] [blame] | 642 | void MediaInternals::OnMediaEvents( |
Dale Curtis | 1adbe6a | 2017-08-02 02:09:13 | [diff] [blame] | 643 | int render_process_id, |
| 644 | const std::vector<media::MediaLogEvent>& events) { |
xhwang | 0fdb831 | 2015-06-10 23:15:38 | [diff] [blame] | 645 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 646 | // Notify observers that |event| has occurred. |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 647 | for (const auto& event : events) { |
Dale Curtis | 1adbe6a | 2017-08-02 02:09:13 | [diff] [blame] | 648 | if (CanUpdate()) { |
| 649 | base::string16 update; |
| 650 | if (ConvertEventToUpdate(render_process_id, event, &update)) |
| 651 | SendUpdate(update); |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 652 | } |
Dale Curtis | 1adbe6a | 2017-08-02 02:09:13 | [diff] [blame] | 653 | SaveEvent(render_process_id, event); |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 654 | uma_handler_->SavePlayerState(render_process_id, event); |
[email protected] | 0e7ee58 | 2013-05-04 14:06:59 | [diff] [blame] | 655 | } |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | void MediaInternals::AddUpdateCallback(const UpdateCallback& callback) { |
xhwang | fe338e9 | 2015-06-08 17:36:09 | [diff] [blame] | 659 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 660 | update_callbacks_.push_back(callback); |
xhwang | fe338e9 | 2015-06-08 17:36:09 | [diff] [blame] | 661 | |
| 662 | base::AutoLock auto_lock(lock_); |
| 663 | can_update_ = true; |
Becca Hughes | 1aa4e67 | 2018-10-23 22:09:08 | [diff] [blame] | 664 | audio_focus_helper_.SetEnabled(true); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | void MediaInternals::RemoveUpdateCallback(const UpdateCallback& callback) { |
xhwang | fe338e9 | 2015-06-08 17:36:09 | [diff] [blame] | 668 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 669 | for (size_t i = 0; i < update_callbacks_.size(); ++i) { |
| 670 | if (update_callbacks_[i].Equals(callback)) { |
| 671 | update_callbacks_.erase(update_callbacks_.begin() + i); |
xhwang | fe338e9 | 2015-06-08 17:36:09 | [diff] [blame] | 672 | break; |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 673 | } |
| 674 | } |
xhwang | fe338e9 | 2015-06-08 17:36:09 | [diff] [blame] | 675 | |
| 676 | base::AutoLock auto_lock(lock_); |
| 677 | can_update_ = !update_callbacks_.empty(); |
Becca Hughes | 1aa4e67 | 2018-10-23 22:09:08 | [diff] [blame] | 678 | audio_focus_helper_.SetEnabled(can_update_); |
xhwang | fe338e9 | 2015-06-08 17:36:09 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | bool MediaInternals::CanUpdate() { |
| 682 | base::AutoLock auto_lock(lock_); |
| 683 | return can_update_; |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 684 | } |
| 685 | |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 686 | void MediaInternals::SendHistoricalMediaEvents() { |
| 687 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
watk | b5983f9 | 2016-08-09 20:47:51 | [diff] [blame] | 688 | for (const auto& saved_events : saved_events_by_process_) { |
| 689 | for (const auto& event : saved_events.second) { |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 690 | base::string16 update; |
watk | b5983f9 | 2016-08-09 20:47:51 | [diff] [blame] | 691 | if (ConvertEventToUpdate(saved_events.first, event, &update)) |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 692 | SendUpdate(update); |
| 693 | } |
| 694 | } |
| 695 | // Do not clear the map/list here so that refreshing the UI or opening a |
| 696 | // second UI still works nicely! |
| 697 | } |
| 698 | |
Henrik Grunell | e7e5291 | 2018-10-18 07:39:53 | [diff] [blame] | 699 | void MediaInternals::SendGeneralAudioInformation() { |
| 700 | base::DictionaryValue audio_info_data; |
| 701 | |
| 702 | // Audio feature information. |
| 703 | auto set_feature_data = [&](auto& feature) { |
| 704 | audio_info_data.SetKey( |
| 705 | feature.name, |
| 706 | base::Value(base::FeatureList::IsEnabled(feature) ? "Enabled" |
| 707 | : "Disabled")); |
| 708 | }; |
| 709 | set_feature_data(features::kAudioServiceAudioStreams); |
| 710 | set_feature_data(features::kAudioServiceOutOfProcess); |
| 711 | set_feature_data(features::kAudioServiceLaunchOnStartup); |
| 712 | set_feature_data(service_manager::features::kAudioServiceSandbox); |
| 713 | set_feature_data(features::kWebRtcApmInAudioService); |
| 714 | |
| 715 | base::string16 audio_info_update = |
| 716 | SerializeUpdate("media.updateGeneralAudioInformation", &audio_info_data); |
| 717 | SendUpdate(audio_info_update); |
| 718 | } |
| 719 | |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 720 | void MediaInternals::SendAudioStreamData() { |
| 721 | base::string16 audio_stream_update; |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 722 | { |
| 723 | base::AutoLock auto_lock(lock_); |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 724 | audio_stream_update = SerializeUpdate("media.onReceiveAudioStreamData", |
| 725 | &audio_streams_cached_data_); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 726 | } |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 727 | SendUpdate(audio_stream_update); |
| 728 | } |
| 729 | |
burnik | 7196356 | 2014-10-17 14:57:14 | [diff] [blame] | 730 | void MediaInternals::SendVideoCaptureDeviceCapabilities() { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 731 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
xhwang | fe338e9 | 2015-06-08 17:36:09 | [diff] [blame] | 732 | |
| 733 | if (!CanUpdate()) |
| 734 | return; |
| 735 | |
burnik | 7196356 | 2014-10-17 14:57:14 | [diff] [blame] | 736 | SendUpdate(SerializeUpdate("media.onReceiveVideoCaptureCapabilities", |
| 737 | &video_capture_capabilities_cached_data_)); |
| 738 | } |
| 739 | |
Becca Hughes | d3444394 | 2018-08-17 22:15:08 | [diff] [blame] | 740 | void MediaInternals::SendAudioFocusState() { |
Becca Hughes | 1aa4e67 | 2018-10-23 22:09:08 | [diff] [blame] | 741 | audio_focus_helper_.SendAudioFocusState(); |
Becca Hughes | 8da7a66a | 2018-08-24 21:58:42 | [diff] [blame] | 742 | } |
Becca Hughes | d3444394 | 2018-08-17 22:15:08 | [diff] [blame] | 743 | |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 744 | void MediaInternals::UpdateVideoCaptureDeviceCapabilities( |
chfremer | 1c38eb4 | 2016-08-02 15:54:31 | [diff] [blame] | 745 | const std::vector<std::tuple<media::VideoCaptureDeviceDescriptor, |
| 746 | media::VideoCaptureFormats>>& |
| 747 | descriptors_and_formats) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 748 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
burnik | 7196356 | 2014-10-17 14:57:14 | [diff] [blame] | 749 | video_capture_capabilities_cached_data_.Clear(); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 750 | |
chfremer | 1c38eb4 | 2016-08-02 15:54:31 | [diff] [blame] | 751 | for (const auto& device_format_pair : descriptors_and_formats) { |
Jeremy Roman | 04f27c37 | 2017-10-27 15:20:55 | [diff] [blame] | 752 | auto format_list = std::make_unique<base::ListValue>(); |
nisse | 7a153fb | 2015-12-07 09:30:59 | [diff] [blame] | 753 | // TODO(nisse): Representing format information as a string, to be |
| 754 | // parsed by the javascript handler, is brittle. Consider passing |
| 755 | // a list of mappings instead. |
| 756 | |
chfremer | 1c38eb4 | 2016-08-02 15:54:31 | [diff] [blame] | 757 | const media::VideoCaptureDeviceDescriptor& descriptor = |
| 758 | std::get<0>(device_format_pair); |
| 759 | const media::VideoCaptureFormats& supported_formats = |
| 760 | std::get<1>(device_format_pair); |
| 761 | for (const auto& format : supported_formats) |
ajose | 5d00e9878 | 2015-07-01 21:09:48 | [diff] [blame] | 762 | format_list->AppendString(media::VideoCaptureFormat::ToString(format)); |
burnik | 7196356 | 2014-10-17 14:57:14 | [diff] [blame] | 763 | |
dcheng | 98e96a7 | 2016-06-11 03:41:48 | [diff] [blame] | 764 | std::unique_ptr<base::DictionaryValue> device_dict( |
| 765 | new base::DictionaryValue()); |
chfremer | 1c38eb4 | 2016-08-02 15:54:31 | [diff] [blame] | 766 | device_dict->SetString("id", descriptor.device_id); |
| 767 | device_dict->SetString("name", descriptor.GetNameAndModel()); |
jdoerrie | 664305c | 2017-06-07 08:34:34 | [diff] [blame] | 768 | device_dict->Set("formats", std::move(format_list)); |
emircan | 86e26b25 | 2015-03-24 20:29:40 | [diff] [blame] | 769 | #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) || \ |
| 770 | defined(OS_ANDROID) |
chfremer | 1c38eb4 | 2016-08-02 15:54:31 | [diff] [blame] | 771 | device_dict->SetString("captureApi", descriptor.GetCaptureApiTypeString()); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 772 | #endif |
dcheng | 98e96a7 | 2016-06-11 03:41:48 | [diff] [blame] | 773 | video_capture_capabilities_cached_data_.Append(std::move(device_dict)); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 774 | } |
burnik | 7196356 | 2014-10-17 14:57:14 | [diff] [blame] | 775 | |
xhwang | fe338e9 | 2015-06-08 17:36:09 | [diff] [blame] | 776 | SendVideoCaptureDeviceCapabilities(); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 777 | } |
| 778 | |
dcheng | 3b4fe47 | 2016-04-08 23:45:13 | [diff] [blame] | 779 | std::unique_ptr<media::AudioLog> MediaInternals::CreateAudioLog( |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 780 | AudioComponent component, |
| 781 | int component_id) { |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 782 | return CreateAudioLogImpl(component, component_id, -1, MSG_ROUTING_NONE); |
[email protected] | e2fd1f7 | 2013-08-16 00:34:25 | [diff] [blame] | 783 | } |
| 784 | |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 785 | media::mojom::AudioLogPtr MediaInternals::CreateMojoAudioLog( |
| 786 | media::AudioLogFactory::AudioComponent component, |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 787 | int component_id, |
| 788 | int render_process_id, |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 789 | int render_frame_id) { |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 790 | media::mojom::AudioLogPtr audio_log_ptr; |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 791 | CreateMojoAudioLog(component, component_id, mojo::MakeRequest(&audio_log_ptr), |
| 792 | render_process_id, render_frame_id); |
Guido Urdaneta | 35a8698 | 2018-02-20 13:34:38 | [diff] [blame] | 793 | return audio_log_ptr; |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 794 | } |
| 795 | |
Guido Urdaneta | ee6cfd0 | 2018-07-12 09:19:34 | [diff] [blame] | 796 | void MediaInternals::CreateMojoAudioLog( |
| 797 | media::AudioLogFactory::AudioComponent component, |
| 798 | int component_id, |
| 799 | media::mojom::AudioLogRequest request, |
| 800 | int render_process_id, |
| 801 | int render_frame_id) { |
| 802 | mojo::MakeStrongBinding( |
| 803 | CreateAudioLogImpl(component, component_id, render_process_id, |
| 804 | render_frame_id), |
| 805 | std::move(request)); |
| 806 | } |
| 807 | |
| 808 | std::unique_ptr<MediaInternals::AudioLogImpl> |
| 809 | MediaInternals::CreateAudioLogImpl( |
| 810 | media::AudioLogFactory::AudioComponent component, |
| 811 | int component_id, |
| 812 | int render_process_id, |
| 813 | int render_frame_id) { |
| 814 | base::AutoLock auto_lock(lock_); |
| 815 | return std::make_unique<AudioLogImpl>(owner_ids_[component]++, component, |
| 816 | this, component_id, render_process_id, |
| 817 | render_frame_id); |
| 818 | } |
| 819 | |
dalecurtis | c45b1c4e | 2017-04-01 07:14:27 | [diff] [blame] | 820 | void MediaInternals::OnProcessTerminatedForTesting(int process_id) { |
| 821 | uma_handler_->OnProcessTerminated(process_id); |
| 822 | } |
| 823 | |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 824 | void MediaInternals::SendUpdate(const base::string16& update) { |
xhwang | fe338e9 | 2015-06-08 17:36:09 | [diff] [blame] | 825 | // SendUpdate() may be called from any thread, but must run on the UI thread. |
| 826 | if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 827 | base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI}, |
| 828 | base::BindOnce(&MediaInternals::SendUpdate, |
| 829 | base::Unretained(this), update)); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 830 | return; |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 831 | } |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 832 | |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 833 | for (size_t i = 0; i < update_callbacks_.size(); i++) |
| 834 | update_callbacks_[i].Run(update); |
| 835 | } |
| 836 | |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 837 | void MediaInternals::SaveEvent(int process_id, |
| 838 | const media::MediaLogEvent& event) { |
xhwang | 0fdb831 | 2015-06-10 23:15:38 | [diff] [blame] | 839 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 840 | |
Dale Curtis | 1ff5a375a | 2017-06-16 21:38:31 | [diff] [blame] | 841 | // Save the event and limit the total number per renderer. At the time of |
| 842 | // writing, 512 events of the kind: { "property": value } together consume |
| 843 | // ~88kb of memory on linux. |
| 844 | #if defined(OS_ANDROID) |
| 845 | const size_t kEventLimit = 128; |
| 846 | #else |
| 847 | const size_t kEventLimit = 512; |
| 848 | #endif |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 849 | |
Dale Curtis | 1ff5a375a | 2017-06-16 21:38:31 | [diff] [blame] | 850 | auto& saved_events = saved_events_by_process_[process_id]; |
watk | b5983f9 | 2016-08-09 20:47:51 | [diff] [blame] | 851 | saved_events.push_back(event); |
Dale Curtis | 1ff5a375a | 2017-06-16 21:38:31 | [diff] [blame] | 852 | if (saved_events.size() > kEventLimit) { |
watk | b5983f9 | 2016-08-09 20:47:51 | [diff] [blame] | 853 | // Remove all events for a given player as soon as we have to remove a |
| 854 | // single event for that player to avoid showing incomplete players. |
Dale Curtis | 1ff5a375a | 2017-06-16 21:38:31 | [diff] [blame] | 855 | const int id_to_remove = saved_events.front().id; |
Yeonwoo Jo | 095a273 | 2018-10-03 17:10:40 | [diff] [blame] | 856 | base::EraseIf(saved_events, [&](const media::MediaLogEvent& event) { |
| 857 | return event.id == id_to_remove; |
| 858 | }); |
watk | b5983f9 | 2016-08-09 20:47:51 | [diff] [blame] | 859 | } |
xhwang | e63e59b8d | 2015-06-10 00:45:35 | [diff] [blame] | 860 | } |
| 861 | |
xhwang | 002c154f | 2015-06-16 02:55:54 | [diff] [blame] | 862 | void MediaInternals::UpdateAudioLog(AudioLogUpdateType type, |
| 863 | const std::string& cache_key, |
| 864 | const std::string& function, |
| 865 | const base::DictionaryValue* value) { |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 866 | { |
| 867 | base::AutoLock auto_lock(lock_); |
| 868 | const bool has_entry = audio_streams_cached_data_.HasKey(cache_key); |
| 869 | if ((type == UPDATE_IF_EXISTS || type == UPDATE_AND_DELETE) && !has_entry) { |
| 870 | return; |
| 871 | } else if (!has_entry) { |
| 872 | DCHECK_EQ(type, CREATE); |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 873 | audio_streams_cached_data_.Set( |
Jeremy Roman | 04f27c37 | 2017-10-27 15:20:55 | [diff] [blame] | 874 | cache_key, std::make_unique<base::Value>(value->Clone())); |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 875 | } else if (type == UPDATE_AND_DELETE) { |
dcheng | 3b4fe47 | 2016-04-08 23:45:13 | [diff] [blame] | 876 | std::unique_ptr<base::Value> out_value; |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 877 | CHECK(audio_streams_cached_data_.Remove(cache_key, &out_value)); |
| 878 | } else { |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 879 | base::DictionaryValue* existing_dict = nullptr; |
dalecurtis | e6aa75f | 2015-03-31 02:39:38 | [diff] [blame] | 880 | CHECK( |
| 881 | audio_streams_cached_data_.GetDictionary(cache_key, &existing_dict)); |
| 882 | existing_dict->MergeDictionary(value); |
| 883 | } |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 884 | } |
| 885 | |
xhwang | 002c154f | 2015-06-16 02:55:54 | [diff] [blame] | 886 | if (CanUpdate()) |
| 887 | SendUpdate(SerializeUpdate(function, value)); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 888 | } |
| 889 | |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 890 | } // namespace content |