Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / renderer_host / media / media_stream_track_metrics_host.cc
blob2d3c7cdbf7fc485220cf379cf9e9ef27423f8b59
1 // Copyright 2014 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/renderer_host/media/media_stream_track_metrics_host.h"
7 #include "base/metrics/histogram.h"
8 #include "content/common/media/media_stream_track_metrics_host_messages.h"
10 // We use a histogram with a maximum bucket of 16 hours to infinity
11 // for track durations.
12 #define UMA_HISTOGRAM_TIMES_16H(name, sample) \
13 UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, \
14 base::TimeDelta::FromMilliseconds(100), \
15 base::TimeDelta::FromHours(16), \
16 50);
18 namespace content {
20 MediaStreamTrackMetricsHost::MediaStreamTrackMetricsHost()
21 : BrowserMessageFilter(MediaStreamTrackMetricsHostMsgStart) {
24 MediaStreamTrackMetricsHost::~MediaStreamTrackMetricsHost() {
25 // Our render process has exited. We won't receive any more IPC
26 // messages from it. Assume all tracks ended now.
27 for (TrackMap::iterator it = tracks_.begin();
28 it != tracks_.end();
29 ++it) {
30 TrackInfo& info = it->second;
31 ReportDuration(info);
33 tracks_.clear();
36 bool MediaStreamTrackMetricsHost::OnMessageReceived(
37 const IPC::Message& message,
38 bool* message_was_ok) {
39 bool handled = true;
41 IPC_BEGIN_MESSAGE_MAP_EX(MediaStreamTrackMetricsHost,
42 message,
43 *message_was_ok)
44 IPC_MESSAGE_HANDLER(MediaStreamTrackMetricsHost_AddTrack, OnAddTrack)
45 IPC_MESSAGE_HANDLER(MediaStreamTrackMetricsHost_RemoveTrack, OnRemoveTrack)
46 IPC_MESSAGE_UNHANDLED(handled = false)
47 IPC_END_MESSAGE_MAP_EX()
49 return handled;
52 void MediaStreamTrackMetricsHost::OnAddTrack(uint64 id,
53 bool is_audio,
54 bool is_remote) {
55 DCHECK(tracks_.find(id) == tracks_.end());
56 TrackInfo info = {is_audio, is_remote, base::TimeTicks::Now()};
57 tracks_[id] = info;
60 void MediaStreamTrackMetricsHost::OnRemoveTrack(uint64 id) {
61 DCHECK(tracks_.find(id) != tracks_.end());
63 TrackInfo& info = tracks_[id];
64 ReportDuration(info);
65 tracks_.erase(id);
68 void MediaStreamTrackMetricsHost::ReportDuration(const TrackInfo& info) {
69 base::TimeDelta duration = base::TimeTicks::Now() - info.timestamp;
70 if (info.is_remote) {
71 if (info.is_audio) {
72 DVLOG(3) << "WebRTC.ReceivedAudioTrackDuration: " << duration.InSeconds();
73 UMA_HISTOGRAM_TIMES_16H("WebRTC.ReceivedAudioTrackDuration", duration);
74 } else {
75 DVLOG(3) << "WebRTC.ReceivedVideoTrackDuration: " << duration.InSeconds();
76 UMA_HISTOGRAM_TIMES_16H("WebRTC.ReceivedVideoTrackDuration", duration);
78 } else {
79 if (info.is_audio) {
80 DVLOG(3) << "WebRTC.SentAudioTrackDuration: " << duration.InSeconds();
81 UMA_HISTOGRAM_TIMES_16H("WebRTC.SentAudioTrackDuration", duration);
82 } else {
83 DVLOG(3) << "WebRTC.SentVideoTrackDuration: " << duration.InSeconds();
84 UMA_HISTOGRAM_TIMES_16H("WebRTC.SentVideoTrackDuration", duration);
89 } // namespace content