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