1# Lint as: python2, python3 2# Copyright 2016 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. 5 6"""Utils for webrtc-related functionality. 7 8Note that this module is shared by both server side and client side. 9Do not put something only usable in client side in this module. 10 11""" 12 13from __future__ import absolute_import 14from __future__ import division 15from __future__ import print_function 16import logging 17import time 18import uuid 19import six 20 21 22class AppRTCParameters(object): 23 """Class to hold parameters for AppRTC webpage.""" 24 def __init__(self): 25 """Initializes an AppRTCParameters.""" 26 self.debug = 'loopback' 27 self.audio = {'googEchoCancellation': False, 28 'googAutoGainControl': False, 29 'googNoiseReduction': False} 30 31 32 def _get_audio_parameter_string(self): 33 """Converts the audio parameters into parameter string used in URL. 34 35 @return: Audio parameter string like "audio=googEchoCancellation=False,..." 36 37 """ 38 audio_params = [] 39 for key, value in six.iteritems(self.audio): 40 audio_params.append('%s=%s' % (key, 'true' if value else 'false')) 41 audio_params_str = ','.join(audio_params) 42 return audio_params_str 43 44 45 def get_parameter_string(self): 46 """Converts the parameters into parameter string used in URL. 47 48 @return: Parameter string used in URL. 49 50 """ 51 param_str = '?debug=%s' % self.debug 52 param_str += '&' 53 param_str += 'audio=' + self._get_audio_parameter_string() 54 return param_str 55 56 57class AppRTCController(object): 58 """Class to control AppRTC webpage.""" 59 60 BASE_URL = 'https://appr.tc/r/' 61 WAIT_FOR_JOIN_CALL_SECS = 10 62 CLICK_JOIN_BUTTON_TIMEOUT_SECS = 10 63 64 def __init__(self, browser_facade): 65 """Initializes an AppRTCController. 66 67 @param browser_facade: A BrowserFacadeNative (for client side) or 68 BrowserFacadeAdapter (for server side). 69 70 """ 71 # Only use default parameters for now. If different parameter is needed 72 # we can takes an AppRTCParameters from argument. 73 self.param = AppRTCParameters() 74 self.browser_facade = browser_facade 75 76 77 def new_apprtc_loopback_page(self): 78 """Loads a AppRTC webpage in a new tab with loopback enabled.""" 79 room_name = str(uuid.uuid4()) 80 url = self.BASE_URL + room_name + self.param.get_parameter_string() 81 logging.debug('Open AppRTC loopback page %s', url) 82 tab_desc = self.browser_facade.new_tab(url) 83 self.click_join_button(tab_desc) 84 # After clicking join button, it takes some time to actually join the 85 # call. 86 time.sleep(self.WAIT_FOR_JOIN_CALL_SECS) 87 88 89 def click_join_button(self, tab_desc): 90 """Clicks 'join' button on the webpage. 91 92 @param tab_desc: Tab descriptor returned by new_tab of browser facade. 93 94 """ 95 click_button_js = """document.getElementById('confirm-join-button').click();""" 96 self.browser_facade.execute_javascript( 97 tab_desc, click_button_js, self.CLICK_JOIN_BUTTON_TIMEOUT_SECS) 98