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.

пре 9 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copyright 2015 Adafruit Industries.
  2. # Author: Tony DiCola
  3. # License: GNU GPLv2, see LICENSE.txt
  4. import os
  5. import subprocess
  6. import time
  7. class HelloVideoPlayer(object):
  8. def __init__(self, config):
  9. """Create an instance of a video player that runs hello_video.bin in the
  10. background.
  11. """
  12. self._process = None
  13. self._load_config(config)
  14. def _load_config(self, config):
  15. self._extensions = config.get('hello_video', 'extensions') \
  16. .translate(None, ' \t\r\n.') \
  17. .split(',')
  18. def supported_extensions(self):
  19. """Return list of supported file extensions."""
  20. return self._extensions
  21. def play(self, movie, loop=False):
  22. """Play the provided movied file, optionally looping it repeatedly."""
  23. self.stop(3) # Up to 3 second delay to let the old player stop.
  24. # Assemble list of arguments.
  25. args = ['hello_video.bin']
  26. if loop:
  27. args.append('--loop') # Add loop parameter if necessary.
  28. args.append(movie) # Add movie file path.
  29. # Run hello_video process and direct standard output to /dev/null.
  30. self._process = subprocess.Popen(args,
  31. stdout=open(os.devnull, 'wb'),
  32. close_fds=True)
  33. def is_playing(self):
  34. """Return true if the video player is running, false otherwise."""
  35. if self._process is None:
  36. return False
  37. self._process.poll()
  38. return self._process.returncode is None
  39. def stop(self, block_timeout_sec=None):
  40. """Stop the video player. block_timeout_sec is how many seconds to
  41. block waiting for the player to stop before moving on.
  42. """
  43. # Stop the player if it's running.
  44. if self._process is not None and self._process.returncode is None:
  45. # process.kill() doesn't seem to work reliably if USB drive is
  46. # removed, instead just run a kill -9 on it.
  47. subprocess.call(['kill', '-9', str(self._process.pid)])
  48. # If a blocking timeout was specified, wait up to that amount of time
  49. # for the process to stop.
  50. start = time.time()
  51. while self._process is not None and self._process.returncode is None:
  52. if (time.time() - start) >= block_timeout_sec:
  53. break
  54. time.sleep(0)
  55. # Let the process be garbage collected.
  56. self._process = None
  57. def create_player(config):
  58. """Create new video player based on hello_video."""
  59. return HelloVideoPlayer(config)