Home | History | Annotate | Download | only in video
      1 # Copyright 2015 The Chromium OS 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 
      6 from autotest_lib.client.cros.video import video_player
      7 
      8 import py_utils
      9 import logging
     10 
     11 
     12 class NativeHtml5Player(video_player.VideoPlayer):
     13     """
     14     Provides an interface to interact with native html5 player in chrome.
     15 
     16     """
     17 
     18 
     19     def inject_source_file(self):
     20         """
     21         Injects the path to the video file under test into the html doc.
     22 
     23 
     24         """
     25         self.tab.ExecuteJavaScript(
     26             'loadVideoSource("%s")' % self.video_src_path)
     27 
     28 
     29     def is_video_ready(self):
     30         """
     31         Determines if a native html5 video is ready by using javascript.
     32 
     33         returns: bool, True if video is ready, else False.
     34 
     35         """
     36         return self.tab.EvaluateJavaScript('canplay()')
     37 
     38 
     39     def is_javascript_ready(self):
     40         """
     41         returns: True if javascript variables and functions have been defined,
     42 
     43         else False.
     44 
     45         """
     46         return self.tab.EvaluateJavaScript(
     47                     'typeof script_ready!="undefined" && script_ready == true')
     48 
     49 
     50     def play(self):
     51         """
     52         Plays the video.
     53 
     54         """
     55         self.tab.ExecuteJavaScript('play()')
     56 
     57 
     58     def pause(self):
     59         """
     60         Pauses the video.
     61 
     62         """
     63         self.tab.ExecuteJavaScript('pause()')
     64 
     65 
     66     def paused(self):
     67         """
     68         Checks whether video paused.
     69 
     70         """
     71         cmd = '%s.paused' % self.video_id
     72         return self.tab.EvaluateJavaScript(cmd)
     73 
     74 
     75     def ended(self):
     76         """
     77         Checks whether video paused.
     78 
     79         """
     80         cmd = '%s.ended' % self.video_id
     81         return self.tab.EvaluateJavaScript(cmd)
     82 
     83 
     84     def currentTime(self):
     85         """
     86         Returns the current time of the video element.
     87 
     88         """
     89         return self.tab.EvaluateJavaScript('currentTime()')
     90 
     91 
     92     def seek_to(self, t):
     93         """
     94         Seeks a video to a time stamp.
     95 
     96         @param t: timedelta, time value to seek to.
     97 
     98         """
     99         cmd = '%s.currentTime=%.3f' % (self.video_id, t.total_seconds())
    100         self.tab.ExecuteJavaScript(cmd)
    101 
    102 
    103     def has_video_finished_seeking(self):
    104         """
    105         Determines if the video has finished seeking.
    106 
    107         """
    108         return self.tab.EvaluateJavaScript('finishedSeeking()')
    109 
    110 
    111     def wait_for_error(self):
    112         """
    113         Determines if the video has any errors
    114 
    115         """
    116         return self.tab.WaitForJavaScriptCondition('errorDetected();',
    117                                                    timeout=30)
    118 
    119 
    120     def reload_page(self):
    121         """
    122         Reloads current page
    123 
    124         """
    125         self.tab.ExecuteJavaScript('location.reload()')
    126 
    127 
    128     def enable_VideoControls(self):
    129         """
    130         For enabling controls
    131 
    132         """
    133         self.tab.ExecuteJavaScript('setControls()')
    134 
    135 
    136     def dropped_frame_count(self):
    137         """
    138         Gets the number of dropped frames.
    139 
    140         @returns: An integer indicates the number of dropped frame.
    141 
    142         """
    143         cmd = '%s.webkitDroppedFrameCount' % self.video_id
    144         return self.tab.EvaluateJavaScript(cmd)
    145 
    146 
    147     def duration(self):
    148         """
    149         Gets the duration of the video.
    150 
    151         @returns: An number indicates the duration of the video.
    152 
    153         """
    154         cmd = '%s.duration' % self.video_id
    155         return self.tab.EvaluateJavaScript(cmd)
    156 
    157 
    158     def wait_video_ended(self):
    159         """
    160         Waits until the video playback is ended.
    161 
    162         """
    163         cmd = '%s.ended' % self.video_id
    164         self.tab.WaitForJavaScriptCondition(cmd, timeout=(self.duration() * 2))
    165 
    166 
    167     def wait_ended_or_error(self):
    168         """
    169         Waits until the video ends or an error happens.
    170 
    171         """
    172         try:
    173             # unit of timeout is second.
    174             self.tab.WaitForJavaScriptCondition('endOrError()',
    175                                                 timeout=(self.duration() + 10))
    176         except py_utils.TimeoutException:
    177             logging.error('Timeout in waiting endOrError()')
    178             raise
    179 
    180 
    181     def check_error(self):
    182         """
    183         Check whether an error happens.
    184 
    185         """
    186         return self.tab.EvaluateJavaScript('errorDetected()')
    187 
    188 
    189     def get_error_info(self):
    190         """
    191         Get error code and message
    192         @returns string,string: error code and message
    193 
    194         """
    195         error_code = self.tab.EvaluateJavaScript(
    196                           '%s.error.code' % self.video_id)
    197         error_message = self.tab.EvaluateJavaScript(
    198                           '%s.error.message' % self.video_id)
    199         return error_code, error_message
    200