• 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
11  if (videoElement instanceof HTMLMediaElement) {
12    function logEventHappened(e) {
13      videoEvents[e.type + '_completed'] = true;
14    }
15
16    function logTimeUpdate(e) {
17      videoEvents.timeupdate.push(getCurrentTime());
18    }
19
20    function onError(e) {
21      console.error('Error playing video: ' + e.type);
22    }
23
24    videoElement.addEventListener('playing', logEventHappened);
25    videoElement.addEventListener('seeking', logEventHappened);
26    videoElement.addEventListener('seeked', logEventHappened);
27    videoElement.addEventListener('ended', logEventHappened);
28    videoElement.addEventListener('timeupdate', logTimeUpdate);
29    videoElement.addEventListener('error', onError);
30    videoElement.addEventListener('abort', onError);
31  }
32  else {
33    console.error('Can not play non HTML5 video element.');
34  }
35
36  function playVideo() {
37    videoElement.play();
38  }
39
40  function pauseVideo() {
41    videoElement.pause();
42  }
43
44  function seek(time) {
45    videoElement.currentTime = time;
46  }
47
48  function seekToAlmostEnd(seconds_before_end) {
49    videoElement.currentTime = getDuration() - seconds_before_end;
50  }
51
52  function setPlaybackQuality(quality) {
53    videoElement.setPlaybackQuality(quality);
54  }
55
56  function getVideoState() {
57    if (videoElement.ended) {
58      return 'ended';
59    }
60    else if (videoElement.paused) {
61      return 'paused';
62    }
63    else if (videoElement.seeking) {
64      return 'seeking';
65    }
66    else {
67      return 'playing';
68    }
69  }
70
71  function getDuration() {
72    return videoElement.duration;
73  }
74
75  function getCurrentTime() {
76    return videoElement.currentTime;
77  }
78
79  function getEventHappened(e) {
80    // Pass in the base event name and it will get automatically converted.
81    return videoEvents[e + '_completed'] === true;
82  }
83
84  function clearEventHappened(e) {
85    delete videoEvents[e + '_completed'];
86  }
87
88  function getLastSecondTimeupdates(e) {
89    var updatesInLastSecond = 0;
90    var duration = getDuration();
91
92    for (var index in videoEvents.timeupdate) {
93      var update_time = videoEvents.timeupdate[index];
94      if (update_time > duration - 1 && update_time < duration) {
95        updatesInLastSecond += 1;
96      }
97    }
98    return updatesInLastSecond;
99  }
100
101  function getPlaybackQuality() {
102    return videoElement.getPlaybackQuality();
103  }
104
105  window.__videoElement = videoElement;
106  window.__playVideo = playVideo;
107  window.__pauseVideo = pauseVideo;
108  window.__seek = seek;
109  window.__seekToAlmostEnd = seekToAlmostEnd;
110  window.__getVideoState = getVideoState;
111  window.__getDuration = getDuration;
112  window.__getCurrentTime = getCurrentTime;
113  window.__getEventHappened = getEventHappened;
114  window.__clearEventHappened = clearEventHappened;
115  window.__getLastSecondTimeupdates = getLastSecondTimeupdates;
116
117  return window.__videoElement !== null;
118})();
119