OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This file contains common utilities to find video/audio elements on a page | 5 // This file contains common utilities to find video/audio elements on a page |
6 // and collect metrics for each. | 6 // and collect metrics for each. |
7 | 7 |
8 (function() { | 8 (function() { |
9 // MediaMetric class responsible for collecting metrics on a media element. | 9 // MediaMetric class responsible for collecting metrics on a media element. |
10 // It attaches required event listeners in order to collect different metrics. | 10 // It attaches required event listeners in order to collect different metrics. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 }); | 47 }); |
48 this.setID(); | 48 this.setID(); |
49 | 49 |
50 // Listen to when a Telemetry actions gets called. | 50 // Listen to when a Telemetry actions gets called. |
51 this.element.addEventListener('willPlay', function (e) { | 51 this.element.addEventListener('willPlay', function (e) { |
52 metric.onWillPlay(e); | 52 metric.onWillPlay(e); |
53 }, false); | 53 }, false); |
54 this.element.addEventListener('willSeek', function (e) { | 54 this.element.addEventListener('willSeek', function (e) { |
55 metric.onWillSeek(e); | 55 metric.onWillSeek(e); |
56 }, false); | 56 }, false); |
| 57 this.element.addEventListener('willLoop', function (e) { |
| 58 metric.onWillLoop(e); |
| 59 }, false); |
57 } | 60 } |
58 | 61 |
59 HTMLMediaMetric.prototype = new MediaMetricBase(); | 62 HTMLMediaMetric.prototype = new MediaMetricBase(); |
60 HTMLMediaMetric.prototype.constructor = HTMLMediaMetric; | 63 HTMLMediaMetric.prototype.constructor = HTMLMediaMetric; |
61 | 64 |
62 HTMLMediaMetric.prototype.setID = function() { | 65 HTMLMediaMetric.prototype.setID = function() { |
63 if (this.element.src) | 66 if (this.element.src) |
64 this.id = this.element.src.substring(this.element.src.lastIndexOf("/")+1); | 67 this.id = this.element.src.substring(this.element.src.lastIndexOf("/")+1); |
65 else if (this.element.id) | 68 else if (this.element.id) |
66 this.id = this.element.id; | 69 this.id = this.element.id; |
(...skipping 11 matching lines...) Expand all Loading... |
78 seekLabel = '_' + e.seekLabel; | 81 seekLabel = '_' + e.seekLabel; |
79 var metric = this; | 82 var metric = this; |
80 var onSeeked = function(e) { | 83 var onSeeked = function(e) { |
81 metric.appendMetric('seek' + seekLabel, metric.seekTimer.stop()) | 84 metric.appendMetric('seek' + seekLabel, metric.seekTimer.stop()) |
82 e.target.removeEventListener('seeked', onSeeked); | 85 e.target.removeEventListener('seeked', onSeeked); |
83 }; | 86 }; |
84 this.seekTimer = new Timer(); | 87 this.seekTimer = new Timer(); |
85 this.element.addEventListener('seeked', onSeeked); | 88 this.element.addEventListener('seeked', onSeeked); |
86 }; | 89 }; |
87 | 90 |
| 91 HTMLMediaMetric.prototype.onWillLoop = function(e) { |
| 92 var loopTimer = new Timer(); |
| 93 var metric = this; |
| 94 var loopCount = e.loopCount; |
| 95 var onEndLoop = function(e) { |
| 96 var actualDuration = loopTimer.stop(); |
| 97 var idealDuration = metric.element.duration * loopCount; |
| 98 var avg_loop_time = (actualDuration - idealDuration) / loopCount; |
| 99 metric.metrics['avg_loop_time'] = avg_loop_time.toFixed(3); |
| 100 e.target.removeEventListener('endLoop', onEndLoop); |
| 101 }; |
| 102 this.element.addEventListener('endLoop', onEndLoop); |
| 103 }; |
| 104 |
88 HTMLMediaMetric.prototype.appendMetric = function(metric, value) { | 105 HTMLMediaMetric.prototype.appendMetric = function(metric, value) { |
89 if (!this.metrics[metric]) | 106 if (!this.metrics[metric]) |
90 this.metrics[metric] = []; | 107 this.metrics[metric] = []; |
91 this.metrics[metric].push(value); | 108 this.metrics[metric].push(value); |
92 } | 109 } |
93 | 110 |
94 HTMLMediaMetric.prototype.onPlaying = function(event) { | 111 HTMLMediaMetric.prototype.onPlaying = function(event) { |
95 // Playing event can fire more than once if seeking. | 112 // Playing event can fire more than once if seeking. |
96 if (!this.metrics['time_to_play']) | 113 if (!this.metrics['time_to_play']) |
97 this.metrics['time_to_play'] = this.playbackTimer.stop(); | 114 this.metrics['time_to_play'] = this.playbackTimer.stop(); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 for (var i = 0; i < window.__mediaMetrics.length; i++) | 184 for (var i = 0; i < window.__mediaMetrics.length; i++) |
168 metrics.push(window.__mediaMetrics[i].getSummary()); | 185 metrics.push(window.__mediaMetrics[i].getSummary()); |
169 return metrics; | 186 return metrics; |
170 } | 187 } |
171 | 188 |
172 window.__globalCounter = 0; | 189 window.__globalCounter = 0; |
173 window.__mediaMetrics = []; | 190 window.__mediaMetrics = []; |
174 window.__getAllMetrics = getAllMetrics; | 191 window.__getAllMetrics = getAllMetrics; |
175 window.__createMediaMetricsForDocument = createMediaMetricsForDocument; | 192 window.__createMediaMetricsForDocument = createMediaMetricsForDocument; |
176 })(); | 193 })(); |
OLD | NEW |