mplayer_ctl.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/python3
  2. from subprocess import Popen, PIPE, call
  3. from threading import Thread
  4. import time
  5. # Declarations
  6. # ============
  7. # Default configuration for mplayer control thread
  8. default_config = {
  9. "streaming_url" : "http://localhost:9092",
  10. "per_artist_path" : "/artist",
  11. "default_url" : "",
  12. }
  13. # Queue for player commands
  14. cmd_queue = []
  15. # Command object
  16. class Cmd(object):
  17. def __init__(self, name, param={}):
  18. self.name = name
  19. self.param = param
  20. # Command names for the queue
  21. # Stream per artist
  22. # Expected parameter: name - Name of the artist
  23. CMD_NEW_ARTIST_STREAM = "New Artist Stream"
  24. # Stop current stream
  25. CMD_STOP_STREAM = "Stop Stream"
  26. # Internal
  27. # ========
  28. # Internal reference to mplayer process
  29. _player_instance = None
  30. # Internal mplayer control table
  31. _mplayer_control = {
  32. "step-backward" : "\x1B[B",
  33. "backward" : "\x1B[D",
  34. "forward" : "\x1B[C",
  35. "step-forward" : "\x1B[A",
  36. "volume-down" : "9",
  37. "volume-off" : "m",
  38. "volume-up" : "0",
  39. "stop" : "q",
  40. "pause" : " ",
  41. "play" : " ",
  42. }
  43. # Internal config
  44. _config = None
  45. def run():
  46. global _player_instance, _config, _mplayer_control
  47. while True:
  48. try:
  49. # Read the next command
  50. cmd = cmd_queue.pop()
  51. except IndexError:
  52. # If nothing is in the queue wait for a second and try again
  53. time.sleep(1)
  54. else:
  55. if cmd.name == CMD_STOP_STREAM:
  56. # Stop the current stream
  57. _stopPlayer()
  58. if cmd.name == CMD_NEW_ARTIST_STREAM:
  59. # Stop any current stream
  60. _stopPlayer()
  61. # Start a new player with the given artist playlist
  62. _player_instance = Popen(["mplayer", "-slave", _config["streaming_url"] +
  63. _config["per_artist_path"] + "/" + cmd.param["name"]], stdin=PIPE)
  64. def _stopPlayer():
  65. '''
  66. Stop A running player instance.
  67. '''
  68. global _player_instance, _mplayer_control
  69. if _player_instance is not None:
  70. _player_instance.stdin.write(bytes(_mplayer_control["stop"], "UTF-8"))
  71. _player_instance.stdin.flush()
  72. _player_instance.terminate()
  73. _player_instance = None
  74. # Main API
  75. # ========
  76. def start(config=None):
  77. '''
  78. Start the new mplayer control thread.
  79. '''
  80. global _config, _player_instance
  81. c = default_config.copy()
  82. if config is not None:
  83. c.update(config)
  84. _config = c
  85. playerThread = Thread(target=run, args=())
  86. playerThread.daemon = True
  87. playerThread.start()
  88. # Start default playlist if defined
  89. d = config["default_url"]
  90. if d != "":
  91. _player_instance = Popen(["mplayer", "-slave", d], stdin=PIPE)