blob: a6c91956d05700dc62359d53bd0fb021cddf4285 [file] [log] [blame]
# Copyright 2018 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import logging
import os
import time
from autotest_lib.client.bin import utils
from autotest_lib.client.common_lib import file_utils
from autotest_lib.client.common_lib.cros import chrome
from import audio_helper
from autotest_lib.client.cros.input_playback import keyboard
from autotest_lib.client.cros.power import power_test
class power_VideoPlayback(power_test.power_Test):
"""class for power_VideoPlayback test.
version = 1
# list of video name and url.
# Ram disk location to download video file.
# We use ram disk to avoid power hit from network / disk usage.
_RAMDISK = '/tmp/ramdisk'
# Time in seconds to wait after set up before starting test.
# Time in seconds to measure power per video file.
# Chrome arguemnts to disable HW video decode
_DISABLE_HW_VIDEO_DECODE_ARGS = '--disable-accelerated-video-decode'
def initialize(self):
"""Create and mount ram disk to download video."""
super(power_VideoPlayback, self).initialize(seconds_period=5)'mkdir -p %s' % self._RAMDISK)
# Don't throw an exception on errors.
result ='mount -t ramfs -o context=u:object_r:tmpfs:s0 '
'ramfs %s' % self._RAMDISK, ignore_status=True)
if result.exit_status:'cannot mount ramfs with context=u:object_r:tmpfs:s0,'
' trying plain mount')
# Try again without selinux options. This time fail on error.'mount -t ramfs ramfs %s' % self._RAMDISK)
audio_helper.set_volume_levels(10, 10)
def _play_video(self, cr, local_path):
"""Opens the video and plays it.
@param cr: Autotest Chrome instance.
@param local_path: path to the local video file to play.
tab = cr.browser.tabs[0]
def _calculate_dropped_frame_percent(self, tab):
"""Calculate percent of dropped frame.
@param tab: tab object that played video in Autotest Chrome instance.
decoded_frame_count = tab.EvaluateJavaScript(
dropped_frame_count = tab.EvaluateJavaScript(
if decoded_frame_count != 0:
dropped_frame_percent = \
100.0 * dropped_frame_count / decoded_frame_count
logging.error("No frame is decoded. Set drop percent to 100.")
dropped_frame_percent = 100.0"Decoded frames=%d, dropped frames=%d, percent=%f",
decoded_frame_count, dropped_frame_count, dropped_frame_percent)
return dropped_frame_percent
def run_once(self, videos=None, secs_per_video=_MEASUREMENT_DURATION,
"""run_once method.
@param videos: list of tuple of tagname and video url to test.
@param secs_per_video: time in seconds to play video and measure power.
@param use_hw_decode: if False, disable hw video decoding.
videos_local = []
loop = 0
if not videos:
videos = self._VIDEOS
# Download video to ramdisk
for name, url in videos:
local_path = os.path.join(self._RAMDISK, os.path.basename(url))'Downloading %s to %s', url, local_path)
file_utils.download_file(url, local_path)
videos_local.append((name, local_path))
extra_browser_args = []
if not use_hw_decode:
with chrome.Chrome(extra_browser_args=extra_browser_args,
init_network_controller=True) as
tab =
# Just measure power in full-screen.
fullscreen = tab.EvaluateJavaScript('document.webkitIsFullScreen')
if not fullscreen:
with keyboard.Keyboard() as keys:
for name, url in videos_local:'Playing video: %s', name)
self._play_video(, url)
tagname = '%s_%s' % (self.tagged_testname, name)
loop_start = time.time()
self.loop_sleep(loop, secs_per_video)
self.keyvals[name + '_dropped_frame_percent'] = \
self.checkpoint_measurements(tagname, loop_start)
loop += 1
def cleanup(self):
"""Unmount ram disk."""'umount %s' % self._RAMDISK)
super(power_VideoPlayback, self).cleanup()