1 # Copyright (c) 2014 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 import logging 6 import os 7 import re 8 9 from autotest_lib.client.bin import test, utils 10 from autotest_lib.client.common_lib import error 11 from autotest_lib.client.common_lib import file_utils 12 from autotest_lib.client.common_lib.cros import chrome 13 from autotest_lib.client.cros.video import helper_logger 14 15 16 # Chrome flags to use fake camera and skip camera permission. 17 EXTRA_BROWSER_ARGS = ['--use-fake-device-for-media-stream', 18 '--use-fake-ui-for-media-stream'] 19 FAKE_FILE_ARG = '--use-file-for-fake-video-capture="%s"' 20 DOWNLOAD_BASE = 'http://commondatastorage.googleapis.com/chromiumos-test-assets-public/crowd/' 21 VIDEO_NAME = 'crowd720_25frames.y4m' 22 23 RTC_VIDEO_ENCODE = 'Media.RTCVideoEncoderInitEncodeSuccess' 24 RTC_VIDEO_ENCODE_BUCKET = 1 25 RTC_ENCODE_PROFILE = 'Media.RTCVideoEncoderProfile' 26 RTC_ENCODE_PROFILE_BUCKET = 11 27 HISTOGRAMS_URL = 'chrome://histograms/' 28 29 class video_ChromeRTCHWEncodeUsed(test.test): 30 """The test verifies HW Encoding for WebRTC video.""" 31 version = 1 32 33 34 def start_loopback(self, cr): 35 """ 36 Opens WebRTC loopback page. 37 38 @param cr: Autotest Chrome instance. 39 """ 40 tab = cr.browser.tabs.New() 41 tab.Navigate(cr.browser.platform.http_server.UrlOf( 42 os.path.join(self.bindir, 'loopback.html'))) 43 tab.WaitForDocumentReadyStateToBeComplete() 44 45 46 def assert_hardware_accelerated(self, cr): 47 """ 48 Checks if WebRTC decoding is hardware accelerated. 49 50 @param cr: Autotest Chrome instance. 51 52 @raises error.TestError if decoding is not hardware accelerated. 53 """ 54 tab = cr.browser.tabs.New() 55 def _histograms_loaded(histogram): 56 """Returns true if histogram is loaded.""" 57 tab.Navigate(HISTOGRAMS_URL + histogram) 58 tab.WaitForDocumentReadyStateToBeComplete() 59 return tab.EvaluateJavaScript( 60 'document.documentElement.innerText.search("%s") != -1' 61 % histogram) 62 63 def _histogram_success(histogram, bucket): 64 lines = tab.EvaluateJavaScript( 65 'document.documentElement.innerText').split("\n") 66 lines = [line for line in lines if line.strip()] 67 logging.info('Histograms for %s:', histogram ) 68 logging.info(lines) 69 success = False 70 for line in lines: 71 re_string = '^'+ str(bucket) +'\s\s-(.*)100.0%(.*)' 72 if re.match(re_string, line): 73 success = True 74 break 75 if not success: 76 raise error.TestError( 77 '{0} didn\'t show up or is not 100%' 78 ' successful.'.format(histogram)) 79 80 for histogram, bucket in [(RTC_VIDEO_ENCODE, RTC_VIDEO_ENCODE_BUCKET), 81 (RTC_ENCODE_PROFILE, RTC_ENCODE_PROFILE_BUCKET)]: 82 utils.poll_for_condition( 83 lambda: _histograms_loaded(histogram), 84 timeout=5, 85 exception=error.TestError( 86 'Cannot find %s histogram.' % histogram), 87 sleep_interval=1) 88 _histogram_success(histogram, bucket) 89 90 @helper_logger.video_log_wrapper 91 def run_once(self, arc_mode=None): 92 # Download test video. 93 url = DOWNLOAD_BASE + VIDEO_NAME 94 local_path = os.path.join(self.bindir, VIDEO_NAME) 95 file_utils.download_file(url, local_path) 96 97 # Start chrome with test flags. 98 EXTRA_BROWSER_ARGS.append(FAKE_FILE_ARG % local_path) 99 EXTRA_BROWSER_ARGS.append(helper_logger.chrome_vmodule_flag()) 100 with chrome.Chrome(extra_browser_args=EXTRA_BROWSER_ARGS, 101 arc_mode=arc_mode, 102 init_network_controller=True) as cr: 103 # Open WebRTC loopback page. 104 cr.browser.platform.SetHTTPServerDirectories(self.bindir) 105 self.start_loopback(cr) 106 107 # Make sure decode is hardware accelerated. 108 self.assert_hardware_accelerated(cr) 109