Beispiel #1
0
	def stop(self):
		self.__status = 'stop'
		# Unpause if paused otherwise music won't play when hitting play
		if (self.__paused):
			self.pause()
		AsyncPlayer.stop(self)
		self.volume = self.__startVolume
Beispiel #2
0
 def __init__(self,args = False,config = False):
     import asyncio
     from mplayer.async import AsyncPlayer
     `rimport os
     from threading import Thread
     import logging
     import socket
     self.player = AsyncPlayer(autospawn=False)
     # Empty Playlist
     self.playlist = []
     if args:
         self.player.args = args
     else:
         # Set argument defaults
         self.player.args = ['-really-quiet', '-msglevel', 'global=6']
     if config:
        self.config = config
     else:
         `r# Set configuration defaults
         home = os.getenv("HOME")
         self.config['api_socket'] = home + '/.mpyr/api.sock'
         self.config['udp_port'] = 38472
         self.config['udp_enabled'] = True
         self.config['udp_host'] = '127.0.0.1'
     if self.config[udp_enable]:
         self.setup_udp()
Beispiel #3
0
    def __init__(self, args=[]):
        # Player instanciation
        self.__player = AsyncPlayer(autospawn=False, stderr=subprocess.PIPE)
        self.__player.stdout.connect(self.__stdoutHandler)
        self.__player.stderr.connect(self.__stderrHandler)
        self.__player._base_args = ('-slave', '-really-quiet', '-msglevel',
                                    'global=4')
        self.__player.args = ['-msglevel', 'input=5:cplayer=5'] + list(args)

        conf = Configuration()
        self.__pollingResolution = conf.values["ResyncPlayer"][
            "Polling Resolution"]
        self.__autoResync = conf.values["ResyncPlayer"]["Auto-resync"]

        # Action mapping
        self.__actions = {
            "Push Delay": self.__pushDelay,
            "Dummy": self.__dummy,
        }

        # Key binding
        self.__keys = conf.values["ResyncPlayer"]["Bindings"]

        self.__videos = []

        self.__lastDelay = (0, 0)
        self.__currentDelayPosition = float("inf")
        self.__pollLock = threading.RLock()
        self.__running = False
Beispiel #4
0
	def pause(self):
		self.__status = 'pause'
		if (self.__playing):
			AsyncPlayer.pause(self)
			# Always change the paused state
			if (self.__paused):
				self.__paused = False
			else:
				self.__paused = True
Beispiel #5
0
  def __init__(self, args=[]):
    # Player instanciation
    self.__player = AsyncPlayer(autospawn=False, stderr=subprocess.PIPE)   
    self.__player.stdout.connect(self.__stdoutHandler)
    self.__player.stderr.connect(self.__stderrHandler)
    self.__player._base_args = ('-slave', '-really-quiet', '-msglevel', 'global=4')
    self.__player.args = ['-msglevel', 'input=5:cplayer=5'] + list(args)

    conf = Configuration()
    self.__pollingResolution = conf.values["ResyncPlayer"]["Polling Resolution"]
    self.__autoResync = conf.values["ResyncPlayer"]["Auto-resync"]

    # Action mapping
    self.__actions = {
      "Push Delay": self.__pushDelay,
      "Dummy": self.__dummy,
    }

    # Key binding
    self.__keys =  conf.values["ResyncPlayer"]["Bindings"]

    self.__videos = []
    
    self.__lastDelay = (0, 0)
    self.__currentDelayPosition = float("inf")
    self.__pollLock = threading.RLock()
    self.__running = False
Beispiel #6
0
def init_player():
    # Don't autospawn because we want to setup the args later
    player = AsyncPlayer(autospawn=False)

    # Setup additional args
    player.args = ['-really-quiet', '-msglevel', 'global=6']

    # hook a subscriber to MPlayer's stdout
    player.stdout.hook(handle_data)

    # Manually spawn the MPlayer process
    player.spawn()

    # play a file
    player.loadfile('/home/pi/y.mp3')

    metadata = player.metadata or {}
    cad = init_cad()
    cad.lcd.write('P: {name}'.format(name=metadata.get('Title', '')))

    listener = pifacecad.SwitchEventListener(chip=cad)

    def play_next(event):
        print(str(event.pin_num))
        player.loadfile('/home/pi/c.mp3')

    # for i in range(8):
    listener.register(0, pifacecad.IODIR_FALLING_EDGE, play_next)

    listener.activate()

    # run the asyncore event loop
    asyncore.loop()
Beispiel #7
0
	def __init__(self, logging):
		AsyncPlayer.__init__(self)
		self.__logging = logging
Beispiel #8
0
class ResyncPlayer(object):
  NO_BIND = re.compile(".*No bind found for key '(.+)'\.")
  SUB_FILE = re.compile("SUB: Added subtitle file \([0-9]+\): (.+)$")
  FILE_NAME = re.compile("Playing (.+)\.$")

  def __init__(self, args=[]):
    # Player instanciation
    self.__player = AsyncPlayer(autospawn=False, stderr=subprocess.PIPE)   
    self.__player.stdout.connect(self.__stdoutHandler)
    self.__player.stderr.connect(self.__stderrHandler)
    self.__player._base_args = ('-slave', '-really-quiet', '-msglevel', 'global=4')
    self.__player.args = ['-msglevel', 'input=5:cplayer=5'] + list(args)

    conf = Configuration()
    self.__pollingResolution = conf.values["ResyncPlayer"]["Polling Resolution"]
    self.__autoResync = conf.values["ResyncPlayer"]["Auto-resync"]

    # Action mapping
    self.__actions = {
      "Push Delay": self.__pushDelay,
      "Dummy": self.__dummy,
    }

    # Key binding
    self.__keys =  conf.values["ResyncPlayer"]["Bindings"]

    self.__videos = []
    
    self.__lastDelay = (0, 0)
    self.__currentDelayPosition = float("inf")
    self.__pollLock = threading.RLock()
    self.__running = False

  @property
  def videos(self):
    return tuple(self.__videos)

  def __poll(self):
    with self.__pollLock:
      # Get sub_delay and time_pos from mplayer
      delay = (self.__player.sub_delay, self.__player.time_pos)
      # Check if we have all the right values
      if delay[0] is not None and delay[1] is not None:
        # Auto-resync on seek before the last pushed delay
        if self.__autoResync and len(self.__videos) > 0 and len(self.__videos[-1]._delays) > 0 and delay[1] < self.__videos[-1]._delays[-1][1]:
          for d in self.__videos[-1]._delays:
            if delay[1] < d[1]:
              # Auto resync only once per delay position
              if d[1] != self.__currentDelayPosition:
                self.__player.sub_delay = -d[0]
                print("[ResyncPlayer] Auto-resync to %.3f" %(d[0]), file=sys.stderr)
                self.__player.osd_show_text("Sub delay: %s ms" %(int(-d[0] * 1000)))
                self.__currentDelayPosition = d[1]
                # sub_delay has changed
                delay = (d[0], delay[1])
              break
        # Update lastDelay
        # msubresync delays follow aeidon's convention, which is the opposite of mplayer
        self.__lastDelay = (-delay[0], delay[1])

      # Retreive the length of the video if we haven't done it yet
      if len(self.__videos) > 0 and self.__videos[-1].length is None:
        self.__videos[-1].length = self.__player.length

    # Rearm timer
    if self.__running:
      self.__pollTimer = threading.Timer(self.__pollingResolution, self.__poll)
      self.__pollTimer.start()

  def __keyHandler(self, key, default=None):
    self.__actions.get(self.__keys.get(key, "Dummy" if default is None else default), self.__dummy)()

  def __dummy(self):
    pass

  def __pushDelay(self):
    if len(self.__videos) > 0:
      with self.__pollLock:
        # Filter out later delays
        self.__videos[-1]._delays = [x for x in self.__videos[-1]._delays if x[1] < self.__lastDelay[1]]
        # Append lastDelay
        self.__videos[-1]._delays.append(self.__lastDelay)
      s = "%+.3f --> %s" %(self.__videos[-1]._delays[-1][0], secToHMS(self.__videos[-1]._delays[-1][1]))
      print(s)
      self.__player.osd_show_text(s)

  def __stdoutHandler(self, data):
    if data.startswith('EOF code'):
      self.__player.quit()
      return

    matches = self.FILE_NAME.match(data)
    if matches is not None:
      with self.__pollLock:
        if len(self.__videos) > 0:
          # Changed video - fixup last delay
          self.__videos[-1]._delays.append((self.__lastDelay[0], self.__videos[-1].length))
          s = "%+.3f --> %s" %(self.__lastDelay[0], secToHMS(self.__videos[-1].length))
          print(s)

        # New video
        print("[ResyncPlayer] New video: %s" %matches.groups()[0], file=sys.stderr)
        self.__videos.append(VideoDelays(matches.groups()[0]))
        self.__lastDelay = (0, 0)
        self.__currentDelayPosition = float("inf")

    matches = self.SUB_FILE.match(data)
    if matches is not None and len(self.__videos) > 0:
      self.__videos[-1]._subFiles.append(matches.groups()[0])
    
    print("%s" %data)

  def __stderrHandler(self, data):
    matches = self.NO_BIND.match(data)
    if matches:
      self.__keyHandler(matches.groups()[0])
    else:
      print("%s" %data, file=sys.stderr)

  def run(self):
    self.__player.spawn()
    # Workaround for uncaught exceptions unexpectedly closing the channels
    self.__player.stdout._dispatcher.handle_error = self.__dummy
    self.__player.stderr._dispatcher.handle_error = self.__dummy
    self.__running = True
    # Start polling
    self.__poll()
    # Main loop
    asyncore.loop()
    #print("asyncore loop stopped", file=sys.stderr)
    self.__running = False
    self.__pollTimer.cancel()
    # Fixup the last video delays
    if len(self.__videos) > 0 and self.__videos[-1].length is not None:
      self.__videos[-1]._delays.append((self.__lastDelay[0], self.__videos[-1].length))
      s = "%+.3f --> %s" %(self.__lastDelay[0], secToHMS(self.__videos[-1].length))
      print(s)
Beispiel #9
0
class ResyncPlayer(object):
    NO_BIND = re.compile(".*No bind found for key '(.+)'\.")
    SUB_FILE = re.compile("SUB: Added subtitle file \([0-9]+\): (.+)$")
    FILE_NAME = re.compile("Playing (.+)\.$")

    def __init__(self, args=[]):
        # Player instanciation
        self.__player = AsyncPlayer(autospawn=False, stderr=subprocess.PIPE)
        self.__player.stdout.connect(self.__stdoutHandler)
        self.__player.stderr.connect(self.__stderrHandler)
        self.__player._base_args = ('-slave', '-really-quiet', '-msglevel',
                                    'global=4')
        self.__player.args = ['-msglevel', 'input=5:cplayer=5'] + list(args)

        conf = Configuration()
        self.__pollingResolution = conf.values["ResyncPlayer"][
            "Polling Resolution"]
        self.__autoResync = conf.values["ResyncPlayer"]["Auto-resync"]

        # Action mapping
        self.__actions = {
            "Push Delay": self.__pushDelay,
            "Dummy": self.__dummy,
        }

        # Key binding
        self.__keys = conf.values["ResyncPlayer"]["Bindings"]

        self.__videos = []

        self.__lastDelay = (0, 0)
        self.__currentDelayPosition = float("inf")
        self.__pollLock = threading.RLock()
        self.__running = False

    @property
    def videos(self):
        return tuple(self.__videos)

    def __poll(self):
        with self.__pollLock:
            # Get sub_delay and time_pos from mplayer
            delay = (self.__player.sub_delay, self.__player.time_pos)
            # Check if we have all the right values
            if delay[0] is not None and delay[1] is not None:
                # Auto-resync on seek before the last pushed delay
                if self.__autoResync and len(self.__videos) > 0 and len(
                        self.__videos[-1]._delays
                ) > 0 and delay[1] < self.__videos[-1]._delays[-1][1]:
                    for d in self.__videos[-1]._delays:
                        if delay[1] < d[1]:
                            # Auto resync only once per delay position
                            if d[1] != self.__currentDelayPosition:
                                self.__player.sub_delay = -d[0]
                                print("[ResyncPlayer] Auto-resync to %.3f" %
                                      (d[0]),
                                      file=sys.stderr)
                                self.__player.osd_show_text(
                                    "Sub delay: %s ms" % (int(-d[0] * 1000)))
                                self.__currentDelayPosition = d[1]
                                # sub_delay has changed
                                delay = (d[0], delay[1])
                            break
                # Update lastDelay
                # msubresync delays follow aeidon's convention, which is the opposite of mplayer
                self.__lastDelay = (-delay[0], delay[1])

            # Retreive the length of the video if we haven't done it yet
            if len(self.__videos) > 0 and self.__videos[-1].length is None:
                self.__videos[-1].length = self.__player.length

        # Rearm timer
        if self.__running:
            self.__pollTimer = threading.Timer(self.__pollingResolution,
                                               self.__poll)
            self.__pollTimer.start()

    def __keyHandler(self, key, default=None):
        self.__actions.get(
            self.__keys.get(key, "Dummy" if default is None else default),
            self.__dummy)()

    def __dummy(self):
        pass

    def __pushDelay(self):
        if len(self.__videos) > 0:
            with self.__pollLock:
                # Filter out later delays
                self.__videos[-1]._delays = [
                    x for x in self.__videos[-1]._delays
                    if x[1] < self.__lastDelay[1]
                ]
                # Append lastDelay
                self.__videos[-1]._delays.append(self.__lastDelay)
            s = "%+.3f --> %s" % (self.__videos[-1]._delays[-1][0],
                                  secToHMS(self.__videos[-1]._delays[-1][1]))
            print(s)
            self.__player.osd_show_text(s)

    def __stdoutHandler(self, data):
        if data.startswith('EOF code'):
            self.__player.quit()
            return

        matches = self.FILE_NAME.match(data)
        if matches is not None:
            with self.__pollLock:
                if len(self.__videos) > 0:
                    # Changed video - fixup last delay
                    self.__videos[-1]._delays.append(
                        (self.__lastDelay[0], self.__videos[-1].length))
                    s = "%+.3f --> %s" % (self.__lastDelay[0],
                                          secToHMS(self.__videos[-1].length))
                    print(s)

                # New video
                print("[ResyncPlayer] New video: %s" % matches.groups()[0],
                      file=sys.stderr)
                self.__videos.append(VideoDelays(matches.groups()[0]))
                self.__lastDelay = (0, 0)
                self.__currentDelayPosition = float("inf")

        matches = self.SUB_FILE.match(data)
        if matches is not None and len(self.__videos) > 0:
            self.__videos[-1]._subFiles.append(matches.groups()[0])

        print("%s" % data)

    def __stderrHandler(self, data):
        matches = self.NO_BIND.match(data)
        if matches:
            self.__keyHandler(matches.groups()[0])
        else:
            print("%s" % data, file=sys.stderr)

    def run(self):
        self.__player.spawn()
        # Workaround for uncaught exceptions unexpectedly closing the channels
        self.__player.stdout._dispatcher.handle_error = self.__dummy
        self.__player.stderr._dispatcher.handle_error = self.__dummy
        self.__running = True
        # Start polling
        self.__poll()
        # Main loop
        asyncore.loop()
        #print("asyncore loop stopped", file=sys.stderr)
        self.__running = False
        self.__pollTimer.cancel()
        # Fixup the last video delays
        if len(self.__videos) > 0 and self.__videos[-1].length is not None:
            self.__videos[-1]._delays.append(
                (self.__lastDelay[0], self.__videos[-1].length))
            s = "%+.3f --> %s" % (self.__lastDelay[0],
                                  secToHMS(self.__videos[-1].length))
            print(s)