Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / media / media_internals.cc
blob7bd5fd35655b03637273768f95ef15fbda281a80
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.
5 #include "content/browser/media/media_internals.h"
7 #include "base/metrics/histogram.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/stringprintf.h"
11 #include "content/public/browser/browser_thread.h"
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_frame_host.h"
17 #include "content/public/browser/render_process_host.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/browser/web_ui.h"
20 #include "media/audio/audio_parameters.h"
21 #include "media/base/media_log_event.h"
22 #include "media/filters/decrypting_video_decoder.h"
23 #include "media/filters/gpu_video_decoder.h"
25 namespace {
27 static base::LazyInstance<content::MediaInternals>::Leaky g_media_internals =
28 LAZY_INSTANCE_INITIALIZER;
30 base::string16 SerializeUpdate(const std::string& function,
31 const base::Value* value) {
32 return content::WebUI::GetJavascriptCall(
33 function, std::vector<const base::Value*>(1, value));
36 std::string EffectsToString(int effects) {
37 if (effects == media::AudioParameters::NO_EFFECTS)
38 return "NO_EFFECTS";
40 struct {
41 int flag;
42 const char* name;
43 } flags[] = {
44 { media::AudioParameters::ECHO_CANCELLER, "ECHO_CANCELLER" },
45 { media::AudioParameters::DUCKING, "DUCKING" },
46 { media::AudioParameters::KEYBOARD_MIC, "KEYBOARD_MIC" },
47 { media::AudioParameters::HOTWORD, "HOTWORD" },
50 std::string ret;
51 for (size_t i = 0; i < arraysize(flags); ++i) {
52 if (effects & flags[i].flag) {
53 if (!ret.empty())
54 ret += " | ";
55 ret += flags[i].name;
56 effects &= ~flags[i].flag;
60 if (effects) {
61 if (!ret.empty())
62 ret += " | ";
63 ret += base::IntToString(effects);
66 return ret;
69 std::string FormatToString(media::AudioParameters::Format format) {
70 switch (format) {
71 case media::AudioParameters::AUDIO_PCM_LINEAR:
72 return "pcm_linear";
73 case media::AudioParameters::AUDIO_PCM_LOW_LATENCY:
74 return "pcm_low_latency";
75 case media::AudioParameters::AUDIO_FAKE:
76 return "fake";
77 case media::AudioParameters::AUDIO_LAST_FORMAT:
78 break;
81 NOTREACHED();
82 return "unknown";
85 const char kAudioLogStatusKey[] = "status";
86 const char kAudioLogUpdateFunction[] = "media.updateAudioComponent";
88 } // namespace
90 namespace content {
92 class AudioLogImpl : public media::AudioLog {
93 public:
94 AudioLogImpl(int owner_id,
95 media::AudioLogFactory::AudioComponent component,
96 content::MediaInternals* media_internals);
97 ~AudioLogImpl() override;
99 void OnCreated(int component_id,
100 const media::AudioParameters& params,
101 const std::string& device_id) override;
102 void OnStarted(int component_id) override;
103 void OnStopped(int component_id) override;
104 void OnClosed(int component_id) override;
105 void OnError(int component_id) override;
106 void OnSetVolume(int component_id, double volume) override;
108 // Called by MediaInternals to update the WebContents title for a stream.
109 void SendWebContentsTitle(int component_id,
110 int render_process_id,
111 int render_frame_id);
113 private:
114 void SendSingleStringUpdate(int component_id,
115 const std::string& key,
116 const std::string& value);
117 void StoreComponentMetadata(int component_id, base::DictionaryValue* dict);
118 std::string FormatCacheKey(int component_id);
120 static void SendWebContentsTitleHelper(const std::string& cache_key,
121 scoped_ptr<base::DictionaryValue> dict,
122 int render_process_id,
123 int render_frame_id);
125 const int owner_id_;
126 const media::AudioLogFactory::AudioComponent component_;
127 content::MediaInternals* const media_internals_;
129 DISALLOW_COPY_AND_ASSIGN(AudioLogImpl);
132 AudioLogImpl::AudioLogImpl(int owner_id,
133 media::AudioLogFactory::AudioComponent component,
134 content::MediaInternals* media_internals)
135 : owner_id_(owner_id),
136 component_(component),
137 media_internals_(media_internals) {}
139 AudioLogImpl::~AudioLogImpl() {}
141 void AudioLogImpl::OnCreated(int component_id,
142 const media::AudioParameters& params,
143 const std::string& device_id) {
144 base::DictionaryValue dict;
145 StoreComponentMetadata(component_id, &dict);
147 dict.SetString(kAudioLogStatusKey, "created");
148 dict.SetString("device_id", device_id);
149 dict.SetString("device_type", FormatToString(params.format()));
150 dict.SetInteger("frames_per_buffer", params.frames_per_buffer());
151 dict.SetInteger("sample_rate", params.sample_rate());
152 dict.SetInteger("channels", params.channels());
153 dict.SetString("channel_layout",
154 ChannelLayoutToString(params.channel_layout()));
155 dict.SetString("effects", EffectsToString(params.effects()));
157 media_internals_->SendAudioLogUpdate(MediaInternals::CREATE,
158 FormatCacheKey(component_id),
159 kAudioLogUpdateFunction, &dict);
162 void AudioLogImpl::OnStarted(int component_id) {
163 SendSingleStringUpdate(component_id, kAudioLogStatusKey, "started");
166 void AudioLogImpl::OnStopped(int component_id) {
167 SendSingleStringUpdate(component_id, kAudioLogStatusKey, "stopped");
170 void AudioLogImpl::OnClosed(int component_id) {
171 base::DictionaryValue dict;
172 StoreComponentMetadata(component_id, &dict);
173 dict.SetString(kAudioLogStatusKey, "closed");
174 media_internals_->SendAudioLogUpdate(MediaInternals::UPDATE_AND_DELETE,
175 FormatCacheKey(component_id),
176 kAudioLogUpdateFunction, &dict);
179 void AudioLogImpl::OnError(int component_id) {
180 SendSingleStringUpdate(component_id, "error_occurred", "true");
183 void AudioLogImpl::OnSetVolume(int component_id, double volume) {
184 base::DictionaryValue dict;
185 StoreComponentMetadata(component_id, &dict);
186 dict.SetDouble("volume", volume);
187 media_internals_->SendAudioLogUpdate(MediaInternals::UPDATE_IF_EXISTS,
188 FormatCacheKey(component_id),
189 kAudioLogUpdateFunction, &dict);
192 void AudioLogImpl::SendWebContentsTitle(int component_id,
193 int render_process_id,
194 int render_frame_id) {
195 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
196 StoreComponentMetadata(component_id, dict.get());
197 SendWebContentsTitleHelper(FormatCacheKey(component_id), dict.Pass(),
198 render_process_id, render_frame_id);
201 std::string AudioLogImpl::FormatCacheKey(int component_id) {
202 return base::StringPrintf("%d:%d:%d", owner_id_, component_, component_id);
205 // static
206 void AudioLogImpl::SendWebContentsTitleHelper(
207 const std::string& cache_key,
208 scoped_ptr<base::DictionaryValue> dict,
209 int render_process_id,
210 int render_frame_id) {
211 // Page title information can only be retrieved from the UI thread.
212 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
213 BrowserThread::PostTask(
214 BrowserThread::UI, FROM_HERE,
215 base::Bind(&SendWebContentsTitleHelper, cache_key, base::Passed(&dict),
216 render_process_id, render_frame_id));
217 return;
220 const WebContents* web_contents = WebContents::FromRenderFrameHost(
221 RenderFrameHost::FromID(render_process_id, render_frame_id));
222 if (!web_contents)
223 return;
225 // Note: by this point the given audio log entry could have been destroyed, so
226 // we use UPDATE_IF_EXISTS to discard such instances.
227 dict->SetInteger("render_process_id", render_process_id);
228 dict->SetString("web_contents_title", web_contents->GetTitle());
229 MediaInternals::GetInstance()->SendAudioLogUpdate(
230 MediaInternals::UPDATE_IF_EXISTS, cache_key, kAudioLogUpdateFunction,
231 dict.get());
234 void AudioLogImpl::SendSingleStringUpdate(int component_id,
235 const std::string& key,
236 const std::string& value) {
237 base::DictionaryValue dict;
238 StoreComponentMetadata(component_id, &dict);
239 dict.SetString(key, value);
240 media_internals_->SendAudioLogUpdate(MediaInternals::UPDATE_IF_EXISTS,
241 FormatCacheKey(component_id),
242 kAudioLogUpdateFunction, &dict);
245 void AudioLogImpl::StoreComponentMetadata(int component_id,
246 base::DictionaryValue* dict) {
247 dict->SetInteger("owner_id", owner_id_);
248 dict->SetInteger("component_id", component_id);
249 dict->SetInteger("component_type", component_);
252 class MediaInternals::MediaInternalsUMAHandler : public NotificationObserver {
253 public:
254 MediaInternalsUMAHandler();
256 // NotificationObserver implementation.
257 void Observe(int type,
258 const NotificationSource& source,
259 const NotificationDetails& details) override;
261 // Reports the pipeline status to UMA for every player
262 // associated with the renderer process and then deletes the player state.
263 void LogAndClearPlayersInRenderer(int render_process_id);
265 // Helper function to save the event payload to RendererPlayerMap.
266 void SavePlayerState(const media::MediaLogEvent& event,
267 int render_process_id);
269 private:
270 struct PipelineInfo {
271 media::PipelineStatus last_pipeline_status;
272 bool has_audio;
273 bool has_video;
274 bool video_dds;
275 bool video_decoder_changed;
276 std::string audio_codec_name;
277 std::string video_codec_name;
278 std::string video_decoder;
279 PipelineInfo()
280 : last_pipeline_status(media::PIPELINE_OK),
281 has_audio(false),
282 has_video(false),
283 video_dds(false),
284 video_decoder_changed(false) {}
287 // Helper function to report PipelineStatus associated with a player to UMA.
288 void ReportUMAForPipelineStatus(const PipelineInfo& player_info);
290 // Helper to generate PipelineStatus UMA name for AudioVideo streams.
291 std::string GetUMANameForAVStream(const PipelineInfo& player_info);
293 // Key is playerid
294 typedef std::map<int, PipelineInfo> PlayerInfoMap;
296 // Key is renderer id
297 typedef std::map<int, PlayerInfoMap> RendererPlayerMap;
299 // Stores player information per renderer
300 RendererPlayerMap renderer_info_;
302 NotificationRegistrar registrar_;
304 DISALLOW_COPY_AND_ASSIGN(MediaInternalsUMAHandler);
307 MediaInternals::MediaInternalsUMAHandler::MediaInternalsUMAHandler() {
308 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_TERMINATED,
309 NotificationService::AllBrowserContextsAndSources());
312 void MediaInternals::MediaInternalsUMAHandler::Observe(
313 int type,
314 const NotificationSource& source,
315 const NotificationDetails& details) {
316 DCHECK_CURRENTLY_ON(BrowserThread::UI);
317 DCHECK_EQ(type, NOTIFICATION_RENDERER_PROCESS_TERMINATED);
318 RenderProcessHost* process = Source<RenderProcessHost>(source).ptr();
320 // Post the task to the IO thread to avoid race in updating renderer_info_ map
321 // by both SavePlayerState & LogAndClearPlayersInRenderer from different
322 // threads.
323 // Using base::Unretained() on MediaInternalsUMAHandler is safe since
324 // it is owned by MediaInternals and share the same lifetime
325 BrowserThread::PostTask(
326 BrowserThread::IO, FROM_HERE,
327 base::Bind(&MediaInternalsUMAHandler::LogAndClearPlayersInRenderer,
328 base::Unretained(this), process->GetID()));
331 void MediaInternals::MediaInternalsUMAHandler::SavePlayerState(
332 const media::MediaLogEvent& event,
333 int render_process_id) {
334 DCHECK_CURRENTLY_ON(BrowserThread::IO);
335 PlayerInfoMap& player_info = renderer_info_[render_process_id];
336 switch (event.type) {
337 case media::MediaLogEvent::PIPELINE_ERROR: {
338 int status;
339 event.params.GetInteger("pipeline_error", &status);
340 player_info[event.id].last_pipeline_status =
341 static_cast<media::PipelineStatus>(status);
342 break;
344 case media::MediaLogEvent::PROPERTY_CHANGE:
345 if (event.params.HasKey("found_audio_stream")) {
346 event.params.GetBoolean("found_audio_stream",
347 &player_info[event.id].has_audio);
349 if (event.params.HasKey("found_video_stream")) {
350 event.params.GetBoolean("found_video_stream",
351 &player_info[event.id].has_video);
353 if (event.params.HasKey("audio_codec_name")) {
354 event.params.GetString("audio_codec_name",
355 &player_info[event.id].audio_codec_name);
357 if (event.params.HasKey("video_codec_name")) {
358 event.params.GetString("video_codec_name",
359 &player_info[event.id].video_codec_name);
361 if (event.params.HasKey("video_decoder")) {
362 std::string previous_video_decoder(player_info[event.id].video_decoder);
363 event.params.GetString("video_decoder",
364 &player_info[event.id].video_decoder);
365 if (!previous_video_decoder.empty() &&
366 previous_video_decoder != player_info[event.id].video_decoder) {
367 player_info[event.id].video_decoder_changed = true;
370 if (event.params.HasKey("video_dds")) {
371 event.params.GetBoolean("video_dds", &player_info[event.id].video_dds);
373 break;
374 default:
375 break;
377 return;
380 std::string MediaInternals::MediaInternalsUMAHandler::GetUMANameForAVStream(
381 const PipelineInfo& player_info) {
382 DCHECK_CURRENTLY_ON(BrowserThread::IO);
383 static const char kPipelineUmaPrefix[] = "Media.PipelineStatus.AudioVideo.";
384 std::string uma_name = kPipelineUmaPrefix;
385 if (player_info.video_codec_name == "vp8") {
386 uma_name += "VP8.";
387 } else if (player_info.video_codec_name == "vp9") {
388 uma_name += "VP9.";
389 } else if (player_info.video_codec_name == "h264") {
390 uma_name += "H264.";
391 } else {
392 return uma_name + "Other";
395 if (player_info.video_decoder ==
396 media::DecryptingVideoDecoder::kDecoderName) {
397 return uma_name + "DVD";
400 if (player_info.video_dds) {
401 uma_name += "DDS.";
404 if (player_info.video_decoder == media::GpuVideoDecoder::kDecoderName) {
405 uma_name += "HW";
406 } else {
407 uma_name += "SW";
409 return uma_name;
412 void MediaInternals::MediaInternalsUMAHandler::ReportUMAForPipelineStatus(
413 const PipelineInfo& player_info) {
414 DCHECK_CURRENTLY_ON(BrowserThread::IO);
415 if (player_info.has_video && player_info.has_audio) {
416 base::LinearHistogram::FactoryGet(
417 GetUMANameForAVStream(player_info), 1, media::PIPELINE_STATUS_MAX,
418 media::PIPELINE_STATUS_MAX + 1,
419 base::HistogramBase::kUmaTargetedHistogramFlag)
420 ->Add(player_info.last_pipeline_status);
421 } else if (player_info.has_audio) {
422 UMA_HISTOGRAM_ENUMERATION("Media.PipelineStatus.AudioOnly",
423 player_info.last_pipeline_status,
424 media::PIPELINE_STATUS_MAX + 1);
425 } else if (player_info.has_video) {
426 UMA_HISTOGRAM_ENUMERATION("Media.PipelineStatus.VideoOnly",
427 player_info.last_pipeline_status,
428 media::PIPELINE_STATUS_MAX + 1);
429 } else {
430 UMA_HISTOGRAM_ENUMERATION("Media.PipelineStatus.Unsupported",
431 player_info.last_pipeline_status,
432 media::PIPELINE_STATUS_MAX + 1);
434 // Report whether video decoder fallback happened, but only if a video decoder
435 // was reported.
436 if (!player_info.video_decoder.empty()) {
437 UMA_HISTOGRAM_BOOLEAN("Media.VideoDecoderFallback",
438 player_info.video_decoder_changed);
442 void MediaInternals::MediaInternalsUMAHandler::LogAndClearPlayersInRenderer(
443 int render_process_id) {
444 DCHECK_CURRENTLY_ON(BrowserThread::IO);
445 auto players_it = renderer_info_.find(render_process_id);
446 if (players_it == renderer_info_.end())
447 return;
448 auto it = players_it->second.begin();
449 while (it != players_it->second.end()) {
450 ReportUMAForPipelineStatus(it->second);
451 players_it->second.erase(it++);
455 MediaInternals* MediaInternals::GetInstance() {
456 return g_media_internals.Pointer();
459 MediaInternals::MediaInternals()
460 : owner_ids_(), uma_handler_(new MediaInternalsUMAHandler()) {
463 MediaInternals::~MediaInternals() {}
465 void MediaInternals::OnMediaEvents(
466 int render_process_id, const std::vector<media::MediaLogEvent>& events) {
467 DCHECK_CURRENTLY_ON(BrowserThread::IO);
468 // Notify observers that |event| has occurred.
469 for (auto event = events.begin(); event != events.end(); ++event) {
470 base::DictionaryValue dict;
471 dict.SetInteger("renderer", render_process_id);
472 dict.SetInteger("player", event->id);
473 dict.SetString("type", media::MediaLog::EventTypeToString(event->type));
475 // TODO(dalecurtis): This is technically not correct. TimeTicks "can't" be
476 // converted to to a human readable time format. See base/time/time.h.
477 const double ticks = event->time.ToInternalValue();
478 const double ticks_millis = ticks / base::Time::kMicrosecondsPerMillisecond;
479 dict.SetDouble("ticksMillis", ticks_millis);
481 // Convert PipelineStatus to human readable string
482 if (event->type == media::MediaLogEvent::PIPELINE_ERROR) {
483 int status;
484 event->params.GetInteger("pipeline_error", &status);
485 media::PipelineStatus error = static_cast<media::PipelineStatus>(status);
486 dict.SetString("params.pipeline_error",
487 media::MediaLog::PipelineStatusToString(error));
488 } else {
489 dict.Set("params", event->params.DeepCopy());
492 SendUpdate(SerializeUpdate("media.onMediaEvent", &dict));
493 uma_handler_->SavePlayerState(*event, render_process_id);
497 void MediaInternals::AddUpdateCallback(const UpdateCallback& callback) {
498 DCHECK_CURRENTLY_ON(BrowserThread::IO);
499 update_callbacks_.push_back(callback);
502 void MediaInternals::RemoveUpdateCallback(const UpdateCallback& callback) {
503 DCHECK_CURRENTLY_ON(BrowserThread::IO);
504 for (size_t i = 0; i < update_callbacks_.size(); ++i) {
505 if (update_callbacks_[i].Equals(callback)) {
506 update_callbacks_.erase(update_callbacks_.begin() + i);
507 return;
510 NOTREACHED();
513 void MediaInternals::SendAudioStreamData() {
514 base::string16 audio_stream_update;
516 base::AutoLock auto_lock(lock_);
517 audio_stream_update = SerializeUpdate(
518 "media.onReceiveAudioStreamData", &audio_streams_cached_data_);
520 SendUpdate(audio_stream_update);
523 void MediaInternals::SendVideoCaptureDeviceCapabilities() {
524 DCHECK_CURRENTLY_ON(BrowserThread::IO);
525 SendUpdate(SerializeUpdate("media.onReceiveVideoCaptureCapabilities",
526 &video_capture_capabilities_cached_data_));
529 void MediaInternals::UpdateVideoCaptureDeviceCapabilities(
530 const media::VideoCaptureDeviceInfos& video_capture_device_infos) {
531 DCHECK_CURRENTLY_ON(BrowserThread::IO);
532 video_capture_capabilities_cached_data_.Clear();
534 for (const auto& video_capture_device_info : video_capture_device_infos) {
535 base::ListValue* format_list = new base::ListValue();
536 for (const auto& format : video_capture_device_info.supported_formats)
537 format_list->AppendString(format.ToString());
539 base::DictionaryValue* device_dict = new base::DictionaryValue();
540 device_dict->SetString("id", video_capture_device_info.name.id());
541 device_dict->SetString(
542 "name", video_capture_device_info.name.GetNameAndModel());
543 device_dict->Set("formats", format_list);
544 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) || \
545 defined(OS_ANDROID)
546 device_dict->SetString(
547 "captureApi", video_capture_device_info.name.GetCaptureApiTypeString());
548 #endif
549 video_capture_capabilities_cached_data_.Append(device_dict);
552 if (update_callbacks_.size() > 0)
553 SendVideoCaptureDeviceCapabilities();
556 scoped_ptr<media::AudioLog> MediaInternals::CreateAudioLog(
557 AudioComponent component) {
558 base::AutoLock auto_lock(lock_);
559 return scoped_ptr<media::AudioLog>(new AudioLogImpl(
560 owner_ids_[component]++, component, this));
563 void MediaInternals::SetWebContentsTitleForAudioLogEntry(
564 int component_id,
565 int render_process_id,
566 int render_frame_id,
567 media::AudioLog* audio_log) {
568 static_cast<AudioLogImpl*>(audio_log)
569 ->SendWebContentsTitle(component_id, render_process_id, render_frame_id);
572 void MediaInternals::SendUpdate(const base::string16& update) {
573 // SendUpdate() may be called from any thread, but must run on the IO thread.
574 // TODO(dalecurtis): This is pretty silly since the update callbacks simply
575 // forward the calls to the UI thread. We should avoid the extra hop.
576 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
577 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
578 &MediaInternals::SendUpdate, base::Unretained(this), update));
579 return;
582 for (size_t i = 0; i < update_callbacks_.size(); i++)
583 update_callbacks_[i].Run(update);
586 void MediaInternals::SendAudioLogUpdate(AudioLogUpdateType type,
587 const std::string& cache_key,
588 const std::string& function,
589 const base::DictionaryValue* value) {
591 base::AutoLock auto_lock(lock_);
592 const bool has_entry = audio_streams_cached_data_.HasKey(cache_key);
593 if ((type == UPDATE_IF_EXISTS || type == UPDATE_AND_DELETE) && !has_entry) {
594 return;
595 } else if (!has_entry) {
596 DCHECK_EQ(type, CREATE);
597 audio_streams_cached_data_.Set(cache_key, value->DeepCopy());
598 } else if (type == UPDATE_AND_DELETE) {
599 scoped_ptr<base::Value> out_value;
600 CHECK(audio_streams_cached_data_.Remove(cache_key, &out_value));
601 } else {
602 base::DictionaryValue* existing_dict = NULL;
603 CHECK(
604 audio_streams_cached_data_.GetDictionary(cache_key, &existing_dict));
605 existing_dict->MergeDictionary(value);
609 SendUpdate(SerializeUpdate(function, value));
612 } // namespace content