• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 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"""Facade to access the video-related functionality."""
6
7import functools
8import glob
9import os
10
11from autotest_lib.client.bin import utils
12from autotest_lib.client.cros.multimedia import display_facade_native
13from autotest_lib.client.cros.video import native_html5_player
14
15
16class VideoFacadeNativeError(Exception):
17    """Error in VideoFacadeNative."""
18    pass
19
20
21def check_arc_resource(func):
22    """Decorator function for ARC related functions in VideoFacadeNative."""
23    @functools.wraps(func)
24    def wrapper(instance, *args, **kwargs):
25        """Wrapper for the methods to check _arc_resource.
26
27        @param instance: Object instance.
28
29        @raises: VideoFacadeNativeError if there is no ARC resource.
30
31        """
32        if not instance._arc_resource:
33            raise VideoFacadeNativeError('There is no ARC resource.')
34        return func(instance, *args, **kwargs)
35    return wrapper
36
37
38class VideoFacadeNative(object):
39    """Facede to access the video-related functionality.
40
41    The methods inside this class only accept Python native types.
42
43    """
44
45    def __init__(self, resource, arc_resource=None):
46        """Initializes an video facade.
47
48        @param resource: A FacadeResource object.
49        @param arc_resource: An ArcResource object.
50
51        """
52        self._resource = resource
53        self._player = None
54        self._arc_resource = arc_resource
55        self._display_facade = display_facade_native.DisplayFacadeNative(
56                resource)
57        self.bindir = os.path.dirname(os.path.realpath(__file__))
58
59
60    def cleanup(self):
61        """Clean up the temporary files."""
62        for path in glob.glob('/tmp/playback_*'):
63            os.unlink(path)
64
65        if self._arc_resource:
66            self._arc_resource.cleanup()
67
68
69    def prepare_playback(self, file_path, fullscreen=True):
70        """Copies the html file to /tmp and loads the webpage.
71
72        @param file_path: The path to the file.
73        @param fullscreen: Plays the video in fullscreen.
74
75        """
76        # Copies the html file to /tmp to make it accessible.
77        utils.get_file(
78                os.path.join(self.bindir, 'video.html'),
79                '/tmp/playback_video.html')
80
81        html_path = 'file:///tmp/playback_video.html'
82
83        tab = self._resource._browser.tabs.New()
84        tab.Navigate(html_path)
85        self._player = native_html5_player.NativeHtml5Player(
86                tab=tab,
87                full_url=html_path,
88                video_id='video',
89                video_src_path=file_path)
90        self._player.load_video()
91
92        if fullscreen:
93            self._display_facade.set_fullscreen(True)
94
95
96    def start_playback(self, blocking=False):
97        """Starts video playback on the webpage.
98
99        Before calling this method, user should call prepare_playback to
100        put the files to /tmp and load the webpage.
101
102        @param blocking: Blocks this call until playback finishes.
103
104        """
105        self._player.play()
106        if blocking:
107            self._player.wait_video_ended()
108
109
110    def pause_playback(self):
111        """Pauses playback on the webpage."""
112        self._player.pause()
113
114
115    def dropped_frame_count(self):
116        """
117        Gets the number of dropped frames.
118
119        @returns: An integer indicates the number of dropped frame.
120
121        """
122        return self._player.dropped_frame_count()
123
124
125    @check_arc_resource
126    def prepare_arc_playback(self, file_path, fullscreen=True):
127        """Copies the video file to be played into container and starts the app.
128
129        User should call this method to put the file into container before
130        calling start_arc_playback.
131
132        @param file_path: Path to the file to be played on Cros host.
133        @param fullscreen: Plays the video in fullscreen.
134
135        """
136        self._arc_resource.play_video.prepare_playback(file_path, fullscreen)
137
138
139    @check_arc_resource
140    def start_arc_playback(self, blocking_secs=None):
141        """Starts playback through Play Video app.
142
143        Before calling this method, user should call set_arc_playback_file to
144        put the file into container and start the app.
145
146        @param blocking_secs: A positive number indicates the timeout to wait
147                              for the playback is finished. Set None to make
148                              it non-blocking.
149
150
151        """
152        self._arc_resource.play_video.start_playback(blocking_secs)
153
154
155    @check_arc_resource
156    def pause_arc_playback(self):
157        """Pauses playback through Play Video app."""
158        self._arc_resource.play_video.pause_playback()
159
160
161    @check_arc_resource
162    def stop_arc_playback(self):
163        """Stops playback through Play Video app."""
164        self._arc_resource.play_video.stop_playback()
165