You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
3.1 KiB

  1. #!/usr/bin/env python
  2. # Copyright 2015 Adafruit Industries.
  3. # Author: Tony DiCola
  4. # License: GNU GPLv2, see LICENSE.txt
  5. import os
  6. import subprocess
  7. import time
  8. class OMXPlayer(object):
  9. def __init__(self, config):
  10. """Create an instance of a video player that runs omxplayer in the
  11. background.
  12. """
  13. self._process = None
  14. self._load_config(config)
  15. def _load_config(self, config):
  16. self._extensions = config.get('omxplayer', 'extensions') \
  17. .translate(None, ' \t\r\n.') \
  18. .split(',')
  19. self._extra_args = config.get('omxplayer', 'extra_args').split()
  20. self._sound = config.get('omxplayer', 'sound').lower()
  21. assert self._sound in ('hdmi', 'local', 'both'), 'Unknown omxplayer sound configuration value: {0} Expected hdmi, local, or both.'.format(self._sound)
  22. def supported_extensions(self):
  23. """Return list of supported file extensions."""
  24. return self._extensions
  25. def play(self, movie, loop=False, vol=0):
  26. """Play the provided movied file, optionally looping it repeatedly."""
  27. self.stop(3) # Up to 3 second delay to let the old player stop.
  28. # Assemble list of arguments.
  29. args = ['omxplayer']
  30. args.extend(['-o', self._sound]) # Add sound arguments.
  31. args.extend(self._extra_args) # Add extra arguments from config.
  32. if vol is not 0:
  33. args.extend(['--vol', str(vol)])
  34. if loop:
  35. args.append('--loop') # Add loop parameter if necessary.
  36. args.append(movie) # Add movie file path.
  37. # Run omxplayer process and direct standard output to /dev/null.
  38. self._process = subprocess.Popen(args,
  39. stdout=open(os.devnull, 'wb'),
  40. close_fds=True)
  41. def is_playing(self):
  42. """Return true if the video player is running, false otherwise."""
  43. if self._process is None:
  44. return False
  45. self._process.poll()
  46. return self._process.returncode is None
  47. def stop(self, block_timeout_sec=None):
  48. """Stop the video player. block_timeout_sec is how many seconds to
  49. block waiting for the player to stop before moving on.
  50. """
  51. # Stop the player if it's running.
  52. if self._process is not None and self._process.returncode is None:
  53. # There are a couple processes used by omxplayer, so kill both
  54. # with a pkill command.
  55. subprocess.call(['pkill', '-9', 'omxplayer'])
  56. # If a blocking timeout was specified, wait up to that amount of time
  57. # for the process to stop.
  58. start = time.time()
  59. while self._process is not None and self._process.returncode is None:
  60. if (time.time() - start) >= block_timeout_sec:
  61. break
  62. time.sleep(0)
  63. # Let the process be garbage collected.
  64. self._process = None
  65. def create_player(config):
  66. """Create new video player based on omxplayer."""
  67. return OMXPlayer(config)