Hsinyu Chao | 4b8300e | 2011-11-15 13:07:32 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # Copyright (c) 2012 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 | import logging |
| 7 | import os |
| 8 | |
| 9 | from autotest_lib.client.bin import utils |
| 10 | from autotest_lib.client.common_lib import error |
| 11 | |
| 12 | LD_LIBRARY_PATH = 'LD_LIBRARY_PATH' |
| 13 | |
| 14 | class AudioHelper(object): |
| 15 | ''' |
| 16 | A helper class contains audio related utility functions. |
| 17 | ''' |
| 18 | def __init__(self, test): |
| 19 | self._test = test |
| 20 | |
| 21 | def setup_deps(self, deps): |
| 22 | ''' |
| 23 | Sets up audio related dependencies. |
| 24 | ''' |
| 25 | for dep in deps: |
| 26 | if dep == 'test_tones': |
| 27 | dep_dir = os.path.join(self._test.autodir, 'deps', dep) |
| 28 | self._test.job.install_pkg(dep, 'dep', dep_dir) |
| 29 | self.test_tones_path = os.path.join(dep_dir, 'src', dep) |
| 30 | elif dep == 'audioloop': |
| 31 | dep_dir = os.path.join(self._test.autodir, 'deps', dep) |
| 32 | self._test.job.install_pkg(dep, 'dep', dep_dir) |
| 33 | self.audioloop_path = os.path.join(dep_dir, 'src', |
| 34 | 'looptest') |
| 35 | elif dep == 'sox': |
| 36 | dep_dir = os.path.join(self._test.autodir, 'deps', dep) |
| 37 | self._test.job.install_pkg(dep, 'dep', dep_dir) |
| 38 | self.sox_path = os.path.join(dep_dir, 'bin', dep) |
| 39 | self.sox_lib_path = os.path.join(dep_dir, 'lib') |
| 40 | if os.environ.has_key(LD_LIBRARY_PATH): |
| 41 | paths = os.environ[LD_LIBRARY_PATH].split(':') |
| 42 | if not self.sox_lib_path in paths: |
| 43 | paths.append(self.sox_lib_path) |
| 44 | os.environ[LD_LIBRARY_PATH] = ':'.join(paths) |
| 45 | else: |
| 46 | os.environ[LD_LIBRARY_PATH] = self.sox_lib_path |
| 47 | |
| 48 | def cleanup_deps(self, deps): |
| 49 | ''' |
| 50 | Cleans up environments which has been setup for dependencies. |
| 51 | ''' |
| 52 | for dep in deps: |
| 53 | if dep == 'sox': |
| 54 | if (os.environ.has_key(LD_LIBRARY_PATH) |
| 55 | and hasattr(self, 'sox_lib_path')): |
| 56 | paths = filter(lambda x: x != self.sox_lib_path, |
| 57 | os.environ[LD_LIBRARY_PATH].split(':')) |
| 58 | os.environ[LD_LIBRARY_PATH] = ':'.join(paths) |
| 59 | |
| 60 | def set_mixer_controls(self, mixer_settings={}, card='0'): |
| 61 | ''' |
| 62 | Sets all mixer controls listed in the mixer settings on card. |
| 63 | ''' |
| 64 | logging.info('Setting mixer control values on %s' % card) |
| 65 | for item in mixer_settings: |
| 66 | logging.info('Setting %s to %s on card %s' % |
| 67 | (item['name'], item['value'], card)) |
| 68 | cmd = 'amixer -c %s cset name=%s %s' |
| 69 | cmd = cmd % (card, item['name'], item['value']) |
| 70 | try: |
| 71 | utils.system(cmd) |
| 72 | except error.CmdError: |
| 73 | # A card is allowed not to support all the controls, so don't |
| 74 | # fail the test here if we get an error. |
| 75 | logging.info('amixer command failed: %s' % cmd) |