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.

71 lines
2.6 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 HelloVideoPlayer(object):
  9. def __init__(self, config):
  10. """Create an instance of a video player that runs hello_video.bin 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('hello_video', 'extensions') \
  17. .translate(None, ' \t\r\n.') \
  18. .split(',')
  19. def supported_extensions(self):
  20. """Return list of supported file extensions."""
  21. return self._extensions
  22. def play(self, movie, loop=False, **kwargs):
  23. """Play the provided movied file, optionally looping it repeatedly."""
  24. self.stop(3) # Up to 3 second delay to let the old player stop.
  25. # Assemble list of arguments.
  26. args = ['hello_video.bin']
  27. if loop:
  28. args.append('--loop') # Add loop parameter if necessary.
  29. args.append(movie) # Add movie file path.
  30. # Run hello_video process and direct standard output to /dev/null.
  31. self._process = subprocess.Popen(args,
  32. stdout=open(os.devnull, 'wb'),
  33. close_fds=True)
  34. def is_playing(self):
  35. """Return true if the video player is running, false otherwise."""
  36. if self._process is None:
  37. return False
  38. self._process.poll()
  39. return self._process.returncode is None
  40. def stop(self, block_timeout_sec=None):
  41. """Stop the video player. block_timeout_sec is how many seconds to
  42. block waiting for the player to stop before moving on.
  43. """
  44. # Stop the player if it's running.
  45. if self._process is not None and self._process.returncode is None:
  46. # process.kill() doesn't seem to work reliably if USB drive is
  47. # removed, instead just run a kill -9 on it.
  48. subprocess.call(['kill', '-9', str(self._process.pid)])
  49. # If a blocking timeout was specified, wait up to that amount of time
  50. # for the process to stop.
  51. start = time.time()
  52. while self._process is not None and self._process.returncode is None:
  53. if (time.time() - start) >= block_timeout_sec:
  54. break
  55. time.sleep(0)
  56. # Let the process be garbage collected.
  57. self._process = None
  58. def create_player(config):
  59. """Create new video player based on hello_video."""
  60. return HelloVideoPlayer(config)