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