[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 | |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 7 | #include "base/metrics/histogram.h" |
[email protected] | 348fbaac | 2013-06-11 06:31:51 | [diff] [blame] | 8 | #include "base/strings/string16.h" |
[email protected] | 9367e03 | 2014-03-07 19:42:37 | [diff] [blame] | 9 | #include "base/strings/string_number_conversions.h" |
[email protected] | 348fbaac | 2013-06-11 06:31:51 | [diff] [blame] | 10 | #include "base/strings/stringprintf.h" |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 11 | #include "content/public/browser/browser_thread.h" |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 12 | #include "content/public/browser/notification_observer.h" |
| 13 | #include "content/public/browser/notification_registrar.h" |
| 14 | #include "content/public/browser/notification_service.h" |
| 15 | #include "content/public/browser/notification_types.h" |
| 16 | #include "content/public/browser/render_process_host.h" |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 17 | #include "content/public/browser/web_ui.h" |
[email protected] | e2fd1f7 | 2013-08-16 00:34:25 | [diff] [blame] | 18 | #include "media/audio/audio_parameters.h" |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 19 | #include "media/base/media_log_event.h" |
prabhur | c481239 | 2014-12-05 19:59:42 | [diff] [blame^] | 20 | #include "media/filters/decrypting_video_decoder.h" |
prabhur | 957d46c7 | 2014-11-19 03:01:16 | [diff] [blame] | 21 | #include "media/filters/gpu_video_decoder.h" |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 22 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | static base::LazyInstance<content::MediaInternals>::Leaky g_media_internals = |
| 26 | LAZY_INSTANCE_INITIALIZER; |
| 27 | |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 28 | base::string16 SerializeUpdate(const std::string& function, |
| 29 | const base::Value* value) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 30 | return content::WebUI::GetJavascriptCall( |
| 31 | function, std::vector<const base::Value*>(1, value)); |
| 32 | } |
| 33 | |
[email protected] | 9367e03 | 2014-03-07 19:42:37 | [diff] [blame] | 34 | std::string EffectsToString(int effects) { |
| 35 | if (effects == media::AudioParameters::NO_EFFECTS) |
| 36 | return "NO_EFFECTS"; |
| 37 | |
| 38 | struct { |
| 39 | int flag; |
| 40 | const char* name; |
| 41 | } flags[] = { |
| 42 | { media::AudioParameters::ECHO_CANCELLER, "ECHO_CANCELLER" }, |
| 43 | { media::AudioParameters::DUCKING, "DUCKING" }, |
[email protected] | 81495eb | 2014-03-19 06:08:18 | [diff] [blame] | 44 | { media::AudioParameters::KEYBOARD_MIC, "KEYBOARD_MIC" }, |
[email protected] | 9367e03 | 2014-03-07 19:42:37 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | std::string ret; |
viettrungluu | 2dfaba7 | 2014-10-16 05:30:25 | [diff] [blame] | 48 | for (size_t i = 0; i < arraysize(flags); ++i) { |
[email protected] | 9367e03 | 2014-03-07 19:42:37 | [diff] [blame] | 49 | if (effects & flags[i].flag) { |
| 50 | if (!ret.empty()) |
| 51 | ret += " | "; |
| 52 | ret += flags[i].name; |
| 53 | effects &= ~flags[i].flag; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if (effects) { |
| 58 | if (!ret.empty()) |
| 59 | ret += " | "; |
| 60 | ret += base::IntToString(effects); |
| 61 | } |
| 62 | |
| 63 | return ret; |
| 64 | } |
| 65 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 66 | const char kAudioLogStatusKey[] = "status"; |
| 67 | const char kAudioLogUpdateFunction[] = "media.updateAudioComponent"; |
| 68 | |
| 69 | } // namespace |
| 70 | |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 71 | namespace content { |
| 72 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 73 | class AudioLogImpl : public media::AudioLog { |
| 74 | public: |
| 75 | AudioLogImpl(int owner_id, |
| 76 | media::AudioLogFactory::AudioComponent component, |
| 77 | content::MediaInternals* media_internals); |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 78 | ~AudioLogImpl() override; |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 79 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 80 | void OnCreated(int component_id, |
| 81 | const media::AudioParameters& params, |
| 82 | const std::string& device_id) override; |
| 83 | void OnStarted(int component_id) override; |
| 84 | void OnStopped(int component_id) override; |
| 85 | void OnClosed(int component_id) override; |
| 86 | void OnError(int component_id) override; |
| 87 | void OnSetVolume(int component_id, double volume) override; |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 88 | |
| 89 | private: |
| 90 | void SendSingleStringUpdate(int component_id, |
| 91 | const std::string& key, |
| 92 | const std::string& value); |
| 93 | void StoreComponentMetadata(int component_id, base::DictionaryValue* dict); |
| 94 | std::string FormatCacheKey(int component_id); |
| 95 | |
| 96 | const int owner_id_; |
| 97 | const media::AudioLogFactory::AudioComponent component_; |
| 98 | content::MediaInternals* const media_internals_; |
| 99 | |
| 100 | DISALLOW_COPY_AND_ASSIGN(AudioLogImpl); |
| 101 | }; |
| 102 | |
| 103 | AudioLogImpl::AudioLogImpl(int owner_id, |
| 104 | media::AudioLogFactory::AudioComponent component, |
| 105 | content::MediaInternals* media_internals) |
| 106 | : owner_id_(owner_id), |
| 107 | component_(component), |
| 108 | media_internals_(media_internals) {} |
| 109 | |
| 110 | AudioLogImpl::~AudioLogImpl() {} |
| 111 | |
| 112 | void AudioLogImpl::OnCreated(int component_id, |
| 113 | const media::AudioParameters& params, |
[email protected] | 25d7f89 | 2014-02-13 15:22:45 | [diff] [blame] | 114 | const std::string& device_id) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 115 | base::DictionaryValue dict; |
| 116 | StoreComponentMetadata(component_id, &dict); |
| 117 | |
| 118 | dict.SetString(kAudioLogStatusKey, "created"); |
[email protected] | 25d7f89 | 2014-02-13 15:22:45 | [diff] [blame] | 119 | dict.SetString("device_id", device_id); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 120 | dict.SetInteger("frames_per_buffer", params.frames_per_buffer()); |
| 121 | dict.SetInteger("sample_rate", params.sample_rate()); |
[email protected] | ad82f41 | 2013-11-27 04:20:41 | [diff] [blame] | 122 | dict.SetInteger("channels", params.channels()); |
| 123 | dict.SetString("channel_layout", |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 124 | ChannelLayoutToString(params.channel_layout())); |
[email protected] | 9367e03 | 2014-03-07 19:42:37 | [diff] [blame] | 125 | dict.SetString("effects", EffectsToString(params.effects())); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 126 | |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 127 | media_internals_->SendUpdateAndCacheAudioStreamKey( |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 128 | FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict); |
| 129 | } |
| 130 | |
| 131 | void AudioLogImpl::OnStarted(int component_id) { |
| 132 | SendSingleStringUpdate(component_id, kAudioLogStatusKey, "started"); |
| 133 | } |
| 134 | |
| 135 | void AudioLogImpl::OnStopped(int component_id) { |
| 136 | SendSingleStringUpdate(component_id, kAudioLogStatusKey, "stopped"); |
| 137 | } |
| 138 | |
| 139 | void AudioLogImpl::OnClosed(int component_id) { |
| 140 | base::DictionaryValue dict; |
| 141 | StoreComponentMetadata(component_id, &dict); |
| 142 | dict.SetString(kAudioLogStatusKey, "closed"); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 143 | media_internals_->SendUpdateAndPurgeAudioStreamCache( |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 144 | FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict); |
| 145 | } |
| 146 | |
| 147 | void AudioLogImpl::OnError(int component_id) { |
| 148 | SendSingleStringUpdate(component_id, "error_occurred", "true"); |
| 149 | } |
| 150 | |
| 151 | void AudioLogImpl::OnSetVolume(int component_id, double volume) { |
| 152 | base::DictionaryValue dict; |
| 153 | StoreComponentMetadata(component_id, &dict); |
| 154 | dict.SetDouble("volume", volume); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 155 | media_internals_->SendUpdateAndCacheAudioStreamKey( |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 156 | FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict); |
| 157 | } |
| 158 | |
| 159 | std::string AudioLogImpl::FormatCacheKey(int component_id) { |
| 160 | return base::StringPrintf("%d:%d:%d", owner_id_, component_, component_id); |
| 161 | } |
| 162 | |
| 163 | void AudioLogImpl::SendSingleStringUpdate(int component_id, |
| 164 | const std::string& key, |
| 165 | const std::string& value) { |
| 166 | base::DictionaryValue dict; |
| 167 | StoreComponentMetadata(component_id, &dict); |
| 168 | dict.SetString(key, value); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 169 | media_internals_->SendUpdateAndCacheAudioStreamKey( |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 170 | FormatCacheKey(component_id), kAudioLogUpdateFunction, &dict); |
| 171 | } |
| 172 | |
| 173 | void AudioLogImpl::StoreComponentMetadata(int component_id, |
| 174 | base::DictionaryValue* dict) { |
| 175 | dict->SetInteger("owner_id", owner_id_); |
| 176 | dict->SetInteger("component_id", component_id); |
| 177 | dict->SetInteger("component_type", component_); |
| 178 | } |
| 179 | |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 180 | class MediaInternals::MediaInternalsUMAHandler : public NotificationObserver { |
| 181 | public: |
| 182 | MediaInternalsUMAHandler(); |
| 183 | |
| 184 | // NotificationObserver implementation. |
| 185 | void Observe(int type, |
| 186 | const NotificationSource& source, |
| 187 | const NotificationDetails& details) override; |
| 188 | |
| 189 | // Reports the pipeline status to UMA for every player |
| 190 | // associated with the renderer process and then deletes the player state. |
| 191 | void LogAndClearPlayersInRenderer(int render_process_id); |
| 192 | |
| 193 | // Helper function to save the event payload to RendererPlayerMap. |
| 194 | void SavePlayerState(const media::MediaLogEvent& event, |
| 195 | int render_process_id); |
| 196 | |
| 197 | private: |
| 198 | struct PipelineInfo { |
| 199 | media::PipelineStatus last_pipeline_status; |
| 200 | bool has_audio; |
| 201 | bool has_video; |
prabhur | c481239 | 2014-12-05 19:59:42 | [diff] [blame^] | 202 | bool video_dds; |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 203 | std::string audio_codec_name; |
| 204 | std::string video_codec_name; |
| 205 | std::string video_decoder; |
| 206 | PipelineInfo() |
| 207 | : last_pipeline_status(media::PIPELINE_OK), |
| 208 | has_audio(false), |
prabhur | c481239 | 2014-12-05 19:59:42 | [diff] [blame^] | 209 | has_video(false), |
| 210 | video_dds(false) {} |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | // Helper function to report PipelineStatus associated with a player to UMA. |
| 214 | void ReportUMAForPipelineStatus(const PipelineInfo& player_info); |
| 215 | |
prabhur | c481239 | 2014-12-05 19:59:42 | [diff] [blame^] | 216 | // Helper to generate PipelineStatus UMA name for AudioVideo streams. |
| 217 | std::string GetUMANameForAVStream(const PipelineInfo& player_info); |
| 218 | |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 219 | // Key is playerid |
| 220 | typedef std::map<int, PipelineInfo> PlayerInfoMap; |
| 221 | |
| 222 | // Key is renderer id |
| 223 | typedef std::map<int, PlayerInfoMap> RendererPlayerMap; |
| 224 | |
| 225 | // Stores player information per renderer |
| 226 | RendererPlayerMap renderer_info_; |
| 227 | |
| 228 | NotificationRegistrar registrar_; |
| 229 | |
| 230 | DISALLOW_COPY_AND_ASSIGN(MediaInternalsUMAHandler); |
| 231 | }; |
| 232 | |
| 233 | MediaInternals::MediaInternalsUMAHandler::MediaInternalsUMAHandler() { |
| 234 | registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 235 | NotificationService::AllBrowserContextsAndSources()); |
| 236 | } |
| 237 | |
| 238 | void MediaInternals::MediaInternalsUMAHandler::Observe( |
| 239 | int type, |
| 240 | const NotificationSource& source, |
| 241 | const NotificationDetails& details) { |
| 242 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 243 | DCHECK_EQ(type, NOTIFICATION_RENDERER_PROCESS_TERMINATED); |
| 244 | RenderProcessHost* process = Source<RenderProcessHost>(source).ptr(); |
| 245 | |
| 246 | // Post the task to the IO thread to avoid race in updating renderer_info_ map |
| 247 | // by both SavePlayerState & LogAndClearPlayersInRenderer from different |
| 248 | // threads. |
| 249 | // Using base::Unretained() on MediaInternalsUMAHandler is safe since |
| 250 | // it is owned by MediaInternals and share the same lifetime |
| 251 | BrowserThread::PostTask( |
| 252 | BrowserThread::IO, FROM_HERE, |
| 253 | base::Bind(&MediaInternalsUMAHandler::LogAndClearPlayersInRenderer, |
| 254 | base::Unretained(this), process->GetID())); |
| 255 | } |
| 256 | |
| 257 | void MediaInternals::MediaInternalsUMAHandler::SavePlayerState( |
| 258 | const media::MediaLogEvent& event, |
| 259 | int render_process_id) { |
| 260 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 261 | PlayerInfoMap& player_info = renderer_info_[render_process_id]; |
| 262 | switch (event.type) { |
| 263 | case media::MediaLogEvent::WEBMEDIAPLAYER_CREATED: { |
| 264 | // Nothing to do here |
| 265 | break; |
| 266 | } |
| 267 | case media::MediaLogEvent::PIPELINE_ERROR: { |
| 268 | int status; |
| 269 | event.params.GetInteger("pipeline_error", &status); |
| 270 | player_info[event.id].last_pipeline_status = |
| 271 | static_cast<media::PipelineStatus>(status); |
| 272 | break; |
| 273 | } |
| 274 | case media::MediaLogEvent::PROPERTY_CHANGE: |
| 275 | if (event.params.HasKey("found_audio_stream")) { |
| 276 | event.params.GetBoolean("found_audio_stream", |
| 277 | &player_info[event.id].has_audio); |
| 278 | } |
| 279 | if (event.params.HasKey("found_video_stream")) { |
| 280 | event.params.GetBoolean("found_video_stream", |
| 281 | &player_info[event.id].has_video); |
| 282 | } |
| 283 | if (event.params.HasKey("audio_codec_name")) { |
| 284 | event.params.GetString("audio_codec_name", |
| 285 | &player_info[event.id].audio_codec_name); |
| 286 | } |
| 287 | if (event.params.HasKey("video_codec_name")) { |
| 288 | event.params.GetString("video_codec_name", |
| 289 | &player_info[event.id].video_codec_name); |
| 290 | } |
| 291 | if (event.params.HasKey("video_decoder")) { |
| 292 | event.params.GetString("video_decoder", |
| 293 | &player_info[event.id].video_decoder); |
| 294 | } |
prabhur | c481239 | 2014-12-05 19:59:42 | [diff] [blame^] | 295 | if (event.params.HasKey("video_dds")) { |
| 296 | event.params.GetBoolean("video_dds", &player_info[event.id].video_dds); |
| 297 | } |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 298 | break; |
| 299 | default: |
| 300 | break; |
| 301 | } |
| 302 | return; |
| 303 | } |
| 304 | |
prabhur | c481239 | 2014-12-05 19:59:42 | [diff] [blame^] | 305 | std::string MediaInternals::MediaInternalsUMAHandler::GetUMANameForAVStream( |
| 306 | const PipelineInfo& player_info) { |
| 307 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 308 | static const char kPipelineUmaPrefix[] = "Media.PipelineStatus.AudioVideo."; |
| 309 | std::string uma_name = kPipelineUmaPrefix; |
| 310 | if (player_info.video_codec_name == "vp8") { |
| 311 | uma_name += "VP8."; |
| 312 | } else if (player_info.video_codec_name == "vp9") { |
| 313 | uma_name += "VP9."; |
| 314 | } else if (player_info.video_codec_name == "h264") { |
| 315 | uma_name += "H264."; |
| 316 | } else { |
| 317 | return uma_name + "Other"; |
| 318 | } |
| 319 | |
| 320 | if (player_info.video_decoder == |
| 321 | media::DecryptingVideoDecoder::kDecoderName) { |
| 322 | return uma_name + "DVD"; |
| 323 | } |
| 324 | |
| 325 | if (player_info.video_dds) { |
| 326 | uma_name += "DDS."; |
| 327 | } |
| 328 | |
| 329 | if (player_info.video_decoder == media::GpuVideoDecoder::kDecoderName) { |
| 330 | uma_name += "HW"; |
| 331 | } else { |
| 332 | uma_name += "SW"; |
| 333 | } |
| 334 | return uma_name; |
| 335 | } |
| 336 | |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 337 | void MediaInternals::MediaInternalsUMAHandler::ReportUMAForPipelineStatus( |
| 338 | const PipelineInfo& player_info) { |
| 339 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 340 | if (player_info.has_video && player_info.has_audio) { |
prabhur | c481239 | 2014-12-05 19:59:42 | [diff] [blame^] | 341 | base::LinearHistogram::FactoryGet( |
| 342 | GetUMANameForAVStream(player_info), 1, media::PIPELINE_STATUS_MAX, |
| 343 | media::PIPELINE_STATUS_MAX + 1, |
| 344 | base::HistogramBase::kUmaTargetedHistogramFlag) |
| 345 | ->Add(player_info.last_pipeline_status); |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 346 | } else if (player_info.has_audio) { |
prabhur | dc15c40 | 2014-11-21 22:53:01 | [diff] [blame] | 347 | UMA_HISTOGRAM_ENUMERATION("Media.PipelineStatus.AudioOnly", |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 348 | player_info.last_pipeline_status, |
| 349 | media::PIPELINE_STATUS_MAX + 1); |
| 350 | } else if (player_info.has_video) { |
prabhur | dc15c40 | 2014-11-21 22:53:01 | [diff] [blame] | 351 | UMA_HISTOGRAM_ENUMERATION("Media.PipelineStatus.VideoOnly", |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 352 | player_info.last_pipeline_status, |
| 353 | media::PIPELINE_STATUS_MAX + 1); |
| 354 | } else { |
| 355 | UMA_HISTOGRAM_ENUMERATION("Media.PipelineStatus.Unsupported", |
| 356 | player_info.last_pipeline_status, |
| 357 | media::PIPELINE_STATUS_MAX + 1); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | void MediaInternals::MediaInternalsUMAHandler::LogAndClearPlayersInRenderer( |
| 362 | int render_process_id) { |
| 363 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 364 | auto players_it = renderer_info_.find(render_process_id); |
| 365 | if (players_it == renderer_info_.end()) |
| 366 | return; |
| 367 | auto it = players_it->second.begin(); |
| 368 | while (it != players_it->second.end()) { |
| 369 | ReportUMAForPipelineStatus(it->second); |
| 370 | players_it->second.erase(it++); |
| 371 | } |
| 372 | } |
| 373 | |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 374 | MediaInternals* MediaInternals::GetInstance() { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 375 | return g_media_internals.Pointer(); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 376 | } |
| 377 | |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 378 | MediaInternals::MediaInternals() |
| 379 | : owner_ids_(), uma_handler_(new MediaInternalsUMAHandler()) { |
| 380 | } |
| 381 | |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 382 | MediaInternals::~MediaInternals() {} |
| 383 | |
[email protected] | 0e7ee58 | 2013-05-04 14:06:59 | [diff] [blame] | 384 | void MediaInternals::OnMediaEvents( |
| 385 | int render_process_id, const std::vector<media::MediaLogEvent>& events) { |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 386 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 387 | // Notify observers that |event| has occurred. |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 388 | for (auto event = events.begin(); event != events.end(); ++event) { |
[email protected] | 0e7ee58 | 2013-05-04 14:06:59 | [diff] [blame] | 389 | base::DictionaryValue dict; |
| 390 | dict.SetInteger("renderer", render_process_id); |
| 391 | dict.SetInteger("player", event->id); |
| 392 | dict.SetString("type", media::MediaLog::EventTypeToString(event->type)); |
[email protected] | fbc46bd | 2013-06-26 04:21:41 | [diff] [blame] | 393 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 394 | // TODO(dalecurtis): This is technically not correct. TimeTicks "can't" be |
| 395 | // converted to to a human readable time format. See base/time/time.h. |
| 396 | const double ticks = event->time.ToInternalValue(); |
| 397 | const double ticks_millis = ticks / base::Time::kMicrosecondsPerMillisecond; |
[email protected] | fbc46bd | 2013-06-26 04:21:41 | [diff] [blame] | 398 | dict.SetDouble("ticksMillis", ticks_millis); |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 399 | |
| 400 | // Convert PipelineStatus to human readable string |
| 401 | if (event->type == media::MediaLogEvent::PIPELINE_ERROR) { |
| 402 | int status; |
| 403 | event->params.GetInteger("pipeline_error", &status); |
| 404 | media::PipelineStatus error = static_cast<media::PipelineStatus>(status); |
| 405 | dict.SetString("params.pipeline_error", |
| 406 | media::MediaLog::PipelineStatusToString(error)); |
| 407 | } else { |
| 408 | dict.Set("params", event->params.DeepCopy()); |
| 409 | } |
| 410 | |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 411 | SendUpdate(SerializeUpdate("media.onMediaEvent", &dict)); |
prabhur | 53bb918 | 2014-11-13 03:25:17 | [diff] [blame] | 412 | uma_handler_->SavePlayerState(*event, render_process_id); |
[email protected] | 0e7ee58 | 2013-05-04 14:06:59 | [diff] [blame] | 413 | } |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | void MediaInternals::AddUpdateCallback(const UpdateCallback& callback) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 417 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 418 | update_callbacks_.push_back(callback); |
| 419 | } |
| 420 | |
| 421 | void MediaInternals::RemoveUpdateCallback(const UpdateCallback& callback) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 422 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 423 | for (size_t i = 0; i < update_callbacks_.size(); ++i) { |
| 424 | if (update_callbacks_[i].Equals(callback)) { |
| 425 | update_callbacks_.erase(update_callbacks_.begin() + i); |
| 426 | return; |
| 427 | } |
| 428 | } |
| 429 | NOTREACHED(); |
| 430 | } |
| 431 | |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 432 | void MediaInternals::SendAudioStreamData() { |
| 433 | base::string16 audio_stream_update; |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 434 | { |
| 435 | base::AutoLock auto_lock(lock_); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 436 | audio_stream_update = SerializeUpdate( |
| 437 | "media.onReceiveAudioStreamData", &audio_streams_cached_data_); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 438 | } |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 439 | SendUpdate(audio_stream_update); |
| 440 | } |
| 441 | |
burnik | 7196356 | 2014-10-17 14:57:14 | [diff] [blame] | 442 | void MediaInternals::SendVideoCaptureDeviceCapabilities() { |
| 443 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 444 | SendUpdate(SerializeUpdate("media.onReceiveVideoCaptureCapabilities", |
| 445 | &video_capture_capabilities_cached_data_)); |
| 446 | } |
| 447 | |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 448 | void MediaInternals::UpdateVideoCaptureDeviceCapabilities( |
| 449 | const media::VideoCaptureDeviceInfos& video_capture_device_infos) { |
burnik | 7196356 | 2014-10-17 14:57:14 | [diff] [blame] | 450 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 451 | video_capture_capabilities_cached_data_.Clear(); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 452 | |
| 453 | for (const auto& video_capture_device_info : video_capture_device_infos) { |
burnik | 7196356 | 2014-10-17 14:57:14 | [diff] [blame] | 454 | base::ListValue* format_list = new base::ListValue(); |
| 455 | for (const auto& format : video_capture_device_info.supported_formats) |
| 456 | format_list->AppendString(format.ToString()); |
| 457 | |
| 458 | base::DictionaryValue* device_dict = new base::DictionaryValue(); |
| 459 | device_dict->SetString("id", video_capture_device_info.name.id()); |
| 460 | device_dict->SetString( |
| 461 | "name", video_capture_device_info.name.GetNameAndModel()); |
| 462 | device_dict->Set("formats", format_list); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 463 | #if defined(OS_WIN) || defined(OS_MACOSX) |
burnik | 4bf4ed1 | 2014-10-21 22:35:01 | [diff] [blame] | 464 | device_dict->SetString( |
burnik | 7196356 | 2014-10-17 14:57:14 | [diff] [blame] | 465 | "captureApi", |
burnik | 4bf4ed1 | 2014-10-21 22:35:01 | [diff] [blame] | 466 | video_capture_device_info.name.GetCaptureApiTypeString()); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 467 | #endif |
burnik | 7196356 | 2014-10-17 14:57:14 | [diff] [blame] | 468 | video_capture_capabilities_cached_data_.Append(device_dict); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 469 | } |
burnik | 7196356 | 2014-10-17 14:57:14 | [diff] [blame] | 470 | |
| 471 | if (update_callbacks_.size() > 0) |
| 472 | SendVideoCaptureDeviceCapabilities(); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | scoped_ptr<media::AudioLog> MediaInternals::CreateAudioLog( |
| 476 | AudioComponent component) { |
| 477 | base::AutoLock auto_lock(lock_); |
| 478 | return scoped_ptr<media::AudioLog>(new AudioLogImpl( |
| 479 | owner_ids_[component]++, component, this)); |
[email protected] | e2fd1f7 | 2013-08-16 00:34:25 | [diff] [blame] | 480 | } |
| 481 | |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 482 | void MediaInternals::SendUpdate(const base::string16& update) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 483 | // SendUpdate() may be called from any thread, but must run on the IO thread. |
| 484 | // TODO(dalecurtis): This is pretty silly since the update callbacks simply |
| 485 | // forward the calls to the UI thread. We should avoid the extra hop. |
| 486 | if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 487 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( |
| 488 | &MediaInternals::SendUpdate, base::Unretained(this), update)); |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 489 | return; |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 490 | } |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 491 | |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 492 | for (size_t i = 0; i < update_callbacks_.size(); i++) |
| 493 | update_callbacks_[i].Run(update); |
| 494 | } |
| 495 | |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 496 | void MediaInternals::SendUpdateAndCacheAudioStreamKey( |
| 497 | const std::string& cache_key, |
| 498 | const std::string& function, |
| 499 | const base::DictionaryValue* value) { |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 500 | SendUpdate(SerializeUpdate(function, value)); |
| 501 | |
| 502 | base::AutoLock auto_lock(lock_); |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 503 | if (!audio_streams_cached_data_.HasKey(cache_key)) { |
| 504 | audio_streams_cached_data_.Set(cache_key, value->DeepCopy()); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 505 | return; |
| 506 | } |
| 507 | |
| 508 | base::DictionaryValue* existing_dict = NULL; |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 509 | CHECK(audio_streams_cached_data_.GetDictionary(cache_key, &existing_dict)); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 510 | existing_dict->MergeDictionary(value); |
| 511 | } |
| 512 | |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 513 | void MediaInternals::SendUpdateAndPurgeAudioStreamCache( |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 514 | const std::string& cache_key, |
| 515 | const std::string& function, |
| 516 | const base::DictionaryValue* value) { |
| 517 | SendUpdate(SerializeUpdate(function, value)); |
| 518 | |
| 519 | base::AutoLock auto_lock(lock_); |
| 520 | scoped_ptr<base::Value> out_value; |
mcasas | fcb5c7de | 2014-10-11 20:22:17 | [diff] [blame] | 521 | CHECK(audio_streams_cached_data_.Remove(cache_key, &out_value)); |
[email protected] | 69946cf | 2013-11-27 00:11:42 | [diff] [blame] | 522 | } |
| 523 | |
[email protected] | 11158e2d | 2013-02-01 02:31:56 | [diff] [blame] | 524 | } // namespace content |