1# Lint as: python2, python3 2# Copyright 2018 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5import logging 6import os 7 8from autotest_lib.client.common_lib import error 9from autotest_lib.client.common_lib import file_utils 10from autotest_lib.client.cros.power import power_videotest 11 12 13class power_VideoPlayback(power_videotest.power_VideoTest): 14 """class for power_VideoPlayback test.""" 15 version = 1 16 17 # Time in seconds to measure power per video file. 18 _MEASUREMENT_DURATION = 120 19 20 _BASE_URL='http://storage.googleapis.com/chromiumos-test-assets-public/tast/cros/video/power/30s/' 21 22 # list of video name and url. 23 _VIDEOS = [ 24 ('h264_720_30fps', 25 _BASE_URL + '720p30fpsH264_foodmarket_sync_30s.mp4' 26 ), 27 ('h264_720_60fps', 28 _BASE_URL + '720p60fpsH264_boat_sync_30s.mp4' 29 ), 30 ('h264_1080_30fps', 31 _BASE_URL + '1080p30fpsH264_foodmarket_sync_30s.mp4' 32 ), 33 ('h264_1080_60fps', 34 _BASE_URL + '1080p60fpsH264_boat_sync_30s.mp4' 35 ), 36 ('h264_4k_30fps', 37 _BASE_URL + '4k30fpsH264_foodmarket_sync_vod_30s.mp4' 38 ), 39 ('h264_4k_60fps', 40 _BASE_URL + '4k60fpsH264_boat_sync_vod_30s.mp4' 41 ), 42 ('vp8_720_30fps', 43 _BASE_URL + '720p30fpsVP8_foodmarket_sync_30s.webm' 44 ), 45 ('vp8_720_60fps', 46 _BASE_URL + '720p60fpsVP8_boat_sync_30s.webm' 47 ), 48 ('vp8_1080_30fps', 49 _BASE_URL + '1080p30fpsVP8_foodmarket_sync_30s.webm' 50 ), 51 ('vp8_1080_60fps', 52 _BASE_URL + '1080p60fpsVP8_boat_sync_30s.webm' 53 ), 54 ('vp8_4k_30fps', 55 _BASE_URL + '4k30fpsVP8_foodmarket_sync_30s.webm' 56 ), 57 ('vp8_4k_60fps', 58 _BASE_URL + '4k60fpsVP8_boat_sync_30s.webm' 59 ), 60 ('vp9_720_30fps', 61 _BASE_URL + '720p30fpsVP9_foodmarket_sync_30s.webm' 62 ), 63 ('vp9_720_60fps', 64 _BASE_URL + '720p60fpsVP9_boat_sync_30s.webm' 65 ), 66 ('vp9_1080_30fps', 67 _BASE_URL + '1080p30fpsVP9_foodmarket_sync_30s.webm' 68 ), 69 ('vp9_1080_60fps', 70 _BASE_URL + '1080p60fpsVP9_boat_sync_30s.webm' 71 ), 72 ('vp9_4k_30fps', 73 _BASE_URL + '4k30fpsVP9_foodmarket_sync_30s.webm' 74 ), 75 ('vp9_4k_60fps', 76 _BASE_URL + '4k60fpsVP9_boat_sync_30s.webm' 77 ), 78 ('av1_720_30fps', 79 _BASE_URL + '720p30fpsAV1_foodmarket_sync_30s.mp4' 80 ), 81 ('av1_720_60fps', 82 _BASE_URL + '720p60fpsAV1_boat_sync_30s.mp4' 83 ), 84 ('av1_1080_30fps', 85 _BASE_URL + '1080p30fpsAV1_foodmarket_sync_30s.mp4' 86 ), 87 ('av1_1080_60fps', 88 _BASE_URL + '1080p60fpsAV1_boat_sync_30s.mp4' 89 ), 90 ] 91 92 93 _FAST_BASE_URL = 'http://storage.googleapis.com/chromiumos-test-assets-public/tast/cros/video/power/10s/' 94 _FAST_VIDEOS = [ 95 ('h264_720_30fps', 96 _FAST_BASE_URL + '720p30fpsH264_foodmarket_sync_10s.mp4' 97 ), 98 ('vp8_720_30fps', 99 _FAST_BASE_URL + '720p30fpsVP8_foodmarket_sync_10s.webm' 100 ), 101 ('vp9_720_30fps', 102 _FAST_BASE_URL + '720p30fpsVP9_foodmarket_sync_10s.webm' 103 ), 104 ('av1_720_30fps', 105 _FAST_BASE_URL + '720p30fpsAV1_foodmarket_sync_10s.mp4' 106 ), 107 ] 108 109 def _prepare_video(self, cr, url): 110 """Prepare browser session before playing video. 111 112 @param cr: Autotest Chrome instance. 113 @param url: url of video file to play. 114 """ 115 # Download video to ramdisk 116 local_path = os.path.join(self._RAMDISK, os.path.basename(url)) 117 logging.info('Downloading %s to %s', url, local_path) 118 file_utils.download_file(url, local_path) 119 120 def _start_video(self, cr, url): 121 """Start playing video. 122 123 @param cr: Autotest Chrome instance. 124 @param local_path: path to the local video file to play. 125 """ 126 local_path = os.path.join(self._RAMDISK, os.path.basename(url)) 127 tab = cr.browser.tabs[0] 128 # Ensure the tab is activated because Chrome sometimes starts with 129 # and focus on another "What's new" tab. 130 tab.Activate() 131 132 tab.Navigate('file://' + local_path) 133 tab.WaitForDocumentReadyStateToBeComplete() 134 tab.EvaluateJavaScript( 135 "document.getElementsByTagName('video')[0].loop=true") 136 137 def _teardown_video(self, cr, url): 138 """Teardown browser session after playing video. 139 140 @param cr: Autotest Chrome instance. 141 @param url: url of video file to play. 142 """ 143 local_path = os.path.join(self._RAMDISK, os.path.basename(url)) 144 os.remove(local_path) 145 146 def run_once(self, videos=None, secs_per_video=_MEASUREMENT_DURATION, 147 use_hw_decode=True, fast=False): 148 """run_once method. 149 150 @param videos: list of tuple of tagname and video url to test. 151 @param secs_per_video: time in seconds to play video and measure power. 152 @param use_hw_decode: if False, disable hw video decoding. 153 @param fast: Use smaller set of videos when videos is None. 154 """ 155 default_videos = self._FAST_VIDEOS if fast else self._VIDEOS 156 if not videos: 157 videos = default_videos 158 else: 159 for i, (tagname, url) in enumerate(videos): 160 if url: 161 continue 162 # if url is unset & tagname matches default use that path. 163 for default in default_videos: 164 if tagname == default[0]: 165 videos[i] = default 166 break 167 else: 168 estr = 'Unable to find URL for video name %s' % tagname 169 raise error.TestError(estr) 170 171 super(power_VideoPlayback, self).run_once( 172 videos, secs_per_video, use_hw_decode) 173