pulseaudio_sanitizer.py 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright (C) 2010 Google Inc. All rights reserved.
  2. # Copyrigth (C) 2012 Intel Corporation
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are
  6. # met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above
  11. # copyright notice, this list of conditions and the following disclaimer
  12. # in the documentation and/or other materials provided with the
  13. # distribution.
  14. # * Neither the Google name nor the names of its
  15. # contributors may be used to endorse or promote products derived from
  16. # this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. import logging
  30. import os
  31. import subprocess
  32. _log = logging.getLogger(__name__)
  33. # Shared by GTK and EFL for pulseaudio sanitizing before running tests.
  34. class PulseAudioSanitizer:
  35. def unload_pulseaudio_module(self):
  36. # Unload pulseaudio's module-stream-restore, since it remembers
  37. # volume settings from different runs, and could affect
  38. # multimedia tests results
  39. self._pa_module_index = -1
  40. with open(os.devnull, 'w') as devnull:
  41. try:
  42. pactl_process = subprocess.Popen(["pactl", "list", "short", "modules"], stdout=subprocess.PIPE, stderr=devnull)
  43. pactl_process.wait()
  44. except OSError:
  45. # pactl might not be available.
  46. _log.debug('pactl not found. Please install pulseaudio-utils to avoid some potential media test failures.')
  47. return
  48. modules_list = pactl_process.communicate()[0]
  49. for module in modules_list.splitlines():
  50. if module.find("module-stream-restore") >= 0:
  51. # Some pulseaudio-utils versions don't provide
  52. # the index, just an empty string
  53. self._pa_module_index = module.split('\t')[0] or -1
  54. try:
  55. # Since they could provide other stuff (not an index
  56. # nor an empty string, let's make sure this is an int.
  57. if int(self._pa_module_index) != -1:
  58. pactl_process = subprocess.Popen(["pactl", "unload-module", self._pa_module_index])
  59. pactl_process.wait()
  60. if pactl_process.returncode == 0:
  61. _log.debug('Unloaded module-stream-restore successfully')
  62. else:
  63. _log.debug('Unloading module-stream-restore failed')
  64. except ValueError:
  65. # pactl should have returned an index if the module is found
  66. _log.debug('Unable to parse module index. Please check if your pulseaudio-utils version is too old.')
  67. return
  68. def restore_pulseaudio_module(self):
  69. # If pulseaudio's module-stream-restore was previously unloaded,
  70. # restore it back. We shouldn't need extra checks here, since an
  71. # index != -1 here means we successfully unloaded it previously.
  72. if self._pa_module_index != -1:
  73. with open(os.devnull, 'w') as devnull:
  74. pactl_process = subprocess.Popen(["pactl", "load-module", "module-stream-restore"], stdout=devnull, stderr=devnull)
  75. pactl_process.wait()
  76. if pactl_process.returncode == 0:
  77. _log.debug('Restored module-stream-restore successfully')
  78. else:
  79. _log.debug('Restoring module-stream-restore failed')