• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (c) 2013 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(function() {
6  var videoElement = document.querySelector('#movie_player video');
7  var videoEvents = {
8    timeupdate: []
9  };
10  var currentlyFullscreen = false
11
12  if (videoElement instanceof HTMLMediaElement) {
13    function logEventHappened(e) {
14      videoEvents[e.type + '_completed'] = true;
15    }
16
17    function logTimeUpdate(e) {
18      videoEvents.timeupdate.push(getCurrentTime());
19    }
20
21    function onError(e) {
22      console.error('Error playing video: ' + e.type);
23    }
24
25    videoElement.addEventListener('playing', logEventHappened);
26    videoElement.addEventListener('seeking', logEventHappened);
27    videoElement.addEventListener('seeked', logEventHappened);
28    videoElement.addEventListener('ended', logEventHappened);
29    videoElement.addEventListener('timeupdate', logTimeUpdate);
30    videoElement.addEventListener('error', onError);
31    videoElement.addEventListener('abort', onError);
32  }
33  else {
34    console.error('Can not play non HTML5 video element.');
35  }
36
37  /**
38   * Event handler to monitor document fullscreenchange events. We are
39   * currently fullscreened if a fullscreenElement exists.
40   * @see https://fullscreen.spec.whatwg.org/
41   */
42  function onFullscreen(e) {
43    if (document.webkitFullscreenElement === null ||
44        document.fullscreenElement === null) {
45      currentlyFullscreen = false;
46    } else {
47      currentlyFullscreen = true;
48    }
49  }
50
51  function playVideo() {
52    videoElement.play();
53  }
54
55  function pauseVideo() {
56    videoElement.pause();
57  }
58
59  function seek(time) {
60    videoElement.currentTime = time;
61  }
62
63  function seekToAlmostEnd(seconds_before_end) {
64    videoElement.currentTime = getDuration() - seconds_before_end;
65  }
66
67  function setPlaybackQuality(quality) {
68    videoElement.setPlaybackQuality(quality);
69  }
70
71  function getVideoState() {
72    if (videoElement.ended) {
73      return 'ended';
74    }
75    else if (videoElement.paused) {
76      return 'paused';
77    }
78    else if (videoElement.seeking) {
79      return 'seeking';
80    }
81    else {
82      return 'playing';
83    }
84  }
85
86  function getDuration() {
87    return videoElement.duration;
88  }
89
90  function getCurrentTime() {
91    return videoElement.currentTime;
92  }
93
94  function getEventHappened(e) {
95    // Pass in the base event name and it will get automatically converted.
96    return videoEvents[e + '_completed'] === true;
97  }
98
99  function clearEventHappened(e) {
100    delete videoEvents[e + '_completed'];
101  }
102
103  function getLastSecondTimeupdates(e) {
104    var updatesInLastSecond = 0;
105    var duration = getDuration();
106
107    for (var index in videoEvents.timeupdate) {
108      var update_time = videoEvents.timeupdate[index];
109      if (update_time > duration - 1 && update_time < duration) {
110        updatesInLastSecond += 1;
111      }
112    }
113    return updatesInLastSecond;
114  }
115
116  function getPlaybackQuality() {
117    return videoElement.getPlaybackQuality();
118  }
119
120  function getFramesStatistics() {
121    droppedFramesPercentage = 100 * (videoElement.webkitDroppedFrameCount /
122                                     videoElement.webkitDecodedFrameCount);
123
124    return {'droppedFrameCount': videoElement.webkitDroppedFrameCount,
125            'decodedFrameCount': videoElement.webkitDecodedFrameCount,
126            'droppedFramesPercentage': droppedFramesPercentage};
127  }
128
129  function isCurrentlyFullscreen() {
130    return currentlyFullscreen;
131  }
132
133  window.__videoElement = videoElement;
134  window.__playVideo = playVideo;
135  window.__pauseVideo = pauseVideo;
136  window.__seek = seek;
137  window.__seekToAlmostEnd = seekToAlmostEnd;
138  window.__getVideoState = getVideoState;
139  window.__getDuration = getDuration;
140  window.__getCurrentTime = getCurrentTime;
141  window.__getEventHappened = getEventHappened;
142  window.__clearEventHappened = clearEventHappened;
143  window.__getLastSecondTimeupdates = getLastSecondTimeupdates;
144  window.__getFramesStatistics = getFramesStatistics;
145  window.__isCurrentlyFullscreen = isCurrentlyFullscreen;
146
147  document.addEventListener('webkitfullscreenchange', onFullscreen);
148  document.addEventListener('fullscreenchange', onFullscreen);
149
150  return window.__videoElement !== null;
151})();
152