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