Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
|
bolian
2015/04/10 21:00:03
2015
Tom Bergan
2015/04/10 21:14:46
Done.
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // This script finds the first video element on a page and collect metrics | |
| 6 // for that element. This is based on src/tools/perf/metrics/media.js. | |
| 7 | |
| 8 (function() { | |
| 9 // VideoWrapper attaches event listeners to collect metrics. | |
| 10 // The constructor starts playing the video. | |
| 11 function VideoWrapper(element) { | |
| 12 if (!(element instanceof HTMLVideoElement)) | |
| 13 throw new Error('Unrecognized video element type ' + element); | |
| 14 metrics['ready'] = false; | |
| 15 this.element = element; | |
| 16 element.loop = false; | |
| 17 // Set the basic event handlers for this HTML5 video element. | |
| 18 this.element.addEventListener('loadedmetadata', this.onLoaded.bind(this)); | |
| 19 this.element.addEventListener('canplay', this.onCanplay.bind(this)); | |
| 20 this.element.addEventListener('ended', this.onEnded.bind(this)); | |
| 21 this.playbackTimer = new Timer(); | |
| 22 element.play() | |
| 23 } | |
| 24 | |
| 25 VideoWrapper.prototype.onLoaded = function(e) { | |
| 26 if (this.element.readyState == HTMLMediaElement.HAVE_NOTHING) { | |
| 27 return | |
| 28 } | |
| 29 metrics['ready'] = true; | |
| 30 metrics['video_height'] = this.element.videoHeight; | |
| 31 metrics['video_width'] = this.element.videoWidth; | |
| 32 metrics['video_duration'] = this.element.duration; | |
| 33 window.__chromeProxyVideoLoaded = true; | |
| 34 }; | |
| 35 | |
| 36 VideoWrapper.prototype.onCanplay = function(event) { | |
| 37 metrics['time_to_play_ms'] = this.playbackTimer.stop(); | |
| 38 }; | |
| 39 | |
| 40 VideoWrapper.prototype.onEnded = function(event) { | |
| 41 var time_to_end = this.playbackTimer.stop() - metrics['time_to_play_ms']; | |
| 42 metrics['buffering_time_ms'] = time_to_end - this.element.duration * 1000; | |
| 43 metrics['decoded_audio_bytes'] = this.element.webkitAudioDecodedByteCount; | |
| 44 metrics['decoded_video_bytes'] = this.element.webkitVideoDecodedByteCount; | |
| 45 metrics['decoded_frames'] = this.element.webkitDecodedFrameCount; | |
| 46 metrics['dropped_frames'] = this.element.webkitDroppedFrameCount; | |
| 47 window.__chromeProxyVideoEnded = true; | |
| 48 }; | |
| 49 | |
| 50 function MediaMetric(element) { | |
| 51 if (element instanceof HTMLMediaElement) | |
| 52 return new VideoWrapper(element); | |
| 53 throw new Error('Unrecognized media element type.'); | |
| 54 } | |
| 55 | |
| 56 function Timer() { | |
| 57 this.start_ = 0; | |
|
bolian
2015/04/10 21:00:03
delete this line?
Tom Bergan
2015/04/10 21:14:46
I think uninitialized fields default to null? See
bolian
2015/04/10 21:46:29
I mean this.start() will initialize it, so we don'
Tom Bergan
2015/04/10 23:41:04
Done.
| |
| 58 this.start(); | |
| 59 } | |
| 60 | |
| 61 Timer.prototype = { | |
| 62 start: function() { | |
| 63 this.start_ = getCurrentTime(); | |
| 64 }, | |
| 65 | |
| 66 stop: function() { | |
| 67 // Return delta time since start in millisecs. | |
|
bolian
2015/04/10 21:00:03
this is in seconds but with precision to ms?
Tom Bergan
2015/04/10 21:14:46
window.performance.now returns a double in ms. Thi
bolian
2015/04/10 21:46:29
ok. lg as is.
Tom Bergan
2015/04/10 23:41:04
Acknowledged.
| |
| 68 return Math.round((getCurrentTime() - this.start_) * 1000) / 1000; | |
| 69 } | |
| 70 }; | |
| 71 | |
| 72 function getCurrentTime() { | |
| 73 if (window.performance) | |
|
bolian
2015/04/10 21:00:03
For chrome only, we don't this complexity?
Tom Bergan
2015/04/10 21:14:46
I don't know why all this code is here. I copied f
bolian
2015/04/10 21:46:29
Probably they copy from general js library to supp
sclittle
2015/04/10 23:01:49
Either way seems fine to me, but I'd recommend kee
Tom Bergan
2015/04/10 23:41:04
Acknowledged.
| |
| 74 return (performance.now || | |
| 75 performance.mozNow || | |
| 76 performance.msNow || | |
| 77 performance.oNow || | |
| 78 performance.webkitNow).call(window.performance); | |
| 79 else | |
| 80 return Date.now(); | |
| 81 } | |
| 82 | |
| 83 function createVideoWrappersForDocument() { | |
| 84 var videos = document.querySelectorAll('video'); | |
| 85 switch (videos.length) { | |
| 86 case 0: | |
| 87 throw new Error('Page has no videos.'); | |
| 88 case 1: | |
| 89 break; | |
| 90 default: | |
| 91 throw new Error('Page too many videos: ' + videos.length.toString()); | |
| 92 } | |
| 93 new VideoWrapper(videos[0]) | |
| 94 } | |
| 95 | |
| 96 metrics = {}; | |
| 97 window.__chromeProxyCreateVideoWrappers = createVideoWrappersForDocument; | |
| 98 window.__chromeProxyVideoMetrics = metrics; | |
| 99 window.__chromeProxyVideoLoaded = false; | |
| 100 window.__chromeProxyVideoEnded = false; | |
| 101 })(); | |
| OLD | NEW |