1# Copyright 2017 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 7 8from autotest_lib.client.bin import test 9from autotest_lib.client.bin import utils 10from autotest_lib.client.common_lib import error 11from autotest_lib.client.common_lib.cros import chrome 12from autotest_lib.client.cros.video import helper_logger 13 14EXTRA_BROWSER_ARGS = ['--use-fake-ui-for-media-stream', 15 '--use-fake-device-for-media-stream'] 16 17# Polling timeout. 18TIMEOUT = 70 19 20# The test's runtime. 21TEST_RUNTIME_SECONDS = 60 22 23# Number of peer connections to use. 24NUM_PEER_CONNECTIONS = 10 25 26# Delay between each iteration of pausing/playing the feeds. 27PAUSE_PLAY_ITERATION_DELAY_MILLIS = 20; 28 29 30class webrtc_PausePlayPeerConnections(test.test): 31 """Tests many peerconnections randomly paused and played.""" 32 version = 1 33 34 def start_test(self, cr, element_type): 35 """Opens the WebRTC pause-play page. 36 37 @param cr: Autotest Chrome instance. 38 @param element_type: 'video' or 'audio'. String. 39 """ 40 cr.browser.platform.SetHTTPServerDirectories(self.bindir) 41 42 self.tab = cr.browser.tabs[0] 43 self.tab.Navigate(cr.browser.platform.http_server.UrlOf( 44 os.path.join(self.bindir, 'pause-play.html'))) 45 self.tab.WaitForDocumentReadyStateToBeComplete() 46 self.tab.EvaluateJavaScript( 47 "startTest(%d, %d, %d, %s)" % ( 48 TEST_RUNTIME_SECONDS, 49 NUM_PEER_CONNECTIONS, 50 PAUSE_PLAY_ITERATION_DELAY_MILLIS, 51 element_type)) 52 53 def wait_test_completed(self, timeout_secs): 54 """Waits until the test is done. 55 56 @param timeout_secs Max time to wait in seconds. 57 58 @raises TestError on timeout, or javascript eval fails. 59 """ 60 def _test_done(): 61 status = self.tab.EvaluateJavaScript('testRunner.getStatus()') 62 logging.debug(status) 63 return status == 'ok-done' 64 65 utils.poll_for_condition( 66 _test_done, timeout=timeout_secs, sleep_interval=1, 67 desc='pause-play.html reports itself as finished') 68 69 @helper_logger.video_log_wrapper 70 def run_once(self, element_type='video'): 71 """Runs the test.""" 72 with chrome.Chrome(extra_browser_args = EXTRA_BROWSER_ARGS + \ 73 [helper_logger.chrome_vmodule_flag()], 74 init_network_controller = True) as cr: 75 self.start_test(cr, element_type) 76 self.wait_test_completed(TIMEOUT) 77 self.print_result() 78 79 def print_result(self): 80 """Prints results unless status is different from ok-done. 81 82 @raises TestError if the test failed outright. 83 """ 84 status = self.tab.EvaluateJavaScript('testRunner.getStatus()') 85 if status != 'ok-done': 86 raise error.TestFail('Failed: %s' % status) 87 88 results = self.tab.EvaluateJavaScript('testRunner.getResults()') 89 logging.info('runTimeSeconds: %.2f', results['runTimeSeconds']) 90 91 self.output_perf_value( 92 description='run_time', 93 value=results['runTimeSeconds'], 94 units='seconds'); 95