1# Copyright (c) 2013 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
5import os
6import time
7import shutil
8
9from autotest_lib.client.bin import test
10from autotest_lib.client.common_lib import error, utils
11from autotest_lib.client.common_lib.cros import chrome
12from autotest_lib.client.cros.video import histogram_verifier
13from autotest_lib.client.cros.video import constants
14from autotest_lib.client.cros.video import native_html5_player
15
16
17class video_ChromeHWDecodeUsed(test.test):
18    """This test verifies VDA works in Chrome."""
19    version = 1
20
21    def is_skipping_test(self, codec):
22        """Determine whether this test should skip.
23
24        @param codec: the codec to be tested. Example values: 'vp8', 'vp9', 'h264'.
25        """
26        blacklist = [
27                # (board, milestone, codec); None if don't care.
28
29                # kevin did support hw decode, but not ready in M54 and M55.
30                ('kevin', 54, 'vp8'),('kevin', 55, 'vp8')
31        ]
32
33        entry = (utils.get_current_board(), utils.get_chrome_milestone(), codec)
34        for black_entry in blacklist:
35            for i, to_match in enumerate(black_entry):
36                if to_match and str(to_match) != entry[i]:
37                    break
38            else:
39                return True
40
41        return False
42
43
44    def run_once(self, codec, is_mse, video_file, arc_mode=None):
45        """
46        Tests whether VDA works by verifying histogram for the loaded video.
47
48        @param is_mse: bool, True if the content uses MSE, False otherwise.
49        @param video_file: Sample video file to be loaded in Chrome.
50
51        """
52        if self.is_skipping_test(codec):
53            raise error.TestNAError('Skipping test run on this board.')
54
55        with chrome.Chrome(arc_mode=arc_mode,
56                           init_network_controller=True) as cr:
57            # This will execute for MSE video by accesing shaka player
58            if is_mse:
59                 tab1 = cr.browser.tabs.New()
60                 tab1.Navigate(video_file)
61                 tab1.WaitForDocumentReadyStateToBeComplete()
62                 # Running the test longer to check errors and longer playback
63                 # for MSE videos.
64                 time.sleep(30)
65            else:
66                 #This execute for normal video for downloading html file
67                 shutil.copy2(constants.VIDEO_HTML_FILEPATH, self.bindir)
68                 video_path = os.path.join(constants.CROS_VIDEO_DIR,
69                                           'files', video_file)
70                 shutil.copy2(video_path, self.bindir)
71
72                 cr.browser.platform.SetHTTPServerDirectories(self.bindir)
73                 tab = cr.browser.tabs.New()
74                 html_fullpath = os.path.join(self.bindir, 'video.html')
75                 url = cr.browser.platform.http_server.UrlOf(html_fullpath)
76
77                 player = native_html5_player.NativeHtml5Player(
78                         tab,
79                         full_url = url,
80                         video_id = 'video',
81                         video_src_path = video_file,
82                         event_timeout = 120)
83                 player.load_video()
84                 player.play()
85
86            # Waits for histogram updated for the test video.
87            histogram_verifier.verify(
88                    cr,
89                    constants.MEDIA_GVD_INIT_STATUS,
90                    constants.MEDIA_GVD_BUCKET)
91