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