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