Exemplo n.º 1
0
def display_rtsp_stream(number):
    camera_url = cfg['cam' + str(number)]['url']
    lock.acquire()
    global g_player
    if not g_player:
        # first call - for parameters see https://github.com/popcornmix/omxplayer#synopsis
        g_player = OMXPlayer(camera_url, ['--no-osd', '--no-keys', '--live'])
    else:
        if g_player.get_source() != camera_url:
            g_player.load(camera_url)  # play new stream

    lock.release()
Exemplo n.º 2
0
class Omx:
    def __init__(self, media_folder):
        self.player = None
        self.media_folder = media_folder
        self.expects_loading_exit = False
        self.looping = False

    def play(self, filename, loop=False):
        if self.player:
            self.expects_loading_exit = True
            self.player.load(filename)
        else:
            args = ['-b', '--no-osd', '-o', 'both']

            if loop:
                args += ['--loop']
                self.looping = True
            else:
                self.looping = False

            try:
                self.player = OMXPlayer(filename, args=args)
            except SystemError as e:
                print(e)

            self.player.stopEvent += self.on_player_stop
            self.player.exitEvent += self.on_player_exit

    def on_player_stop(self, player):
        self.player = None

    def on_player_exit(self, player, exit_status):
        if self.expects_loading_exit:
            self.expects_loading_exit = False
        else:
            self.player = None

    def stop(self):
        if not self.player:
            return

        self.player.stop()
        self.player = None

    def pause(self):
        if not self.player:
            return

        self.player.play_pause()

    def seek_fraction(self, fraction):
        if not self.player:
            return

        duration = self.player.duration()
        self.player.set_position(fraction * duration)

    def set_volume(self, volume):
        if not self.player:
            return

        if volume > 10:
            volume = 10
        elif volume < 0:
            volume = 0

        self.player.set_volume(volume)

    def get_source(self):
        """ Get player source and remove media folder """
        source = self.player.get_source()
        if source.startswith(self.media_folder + "/"):
            return source[len(self.media_folder) + 1:]
        else:
            return source

    def status(self):
        if not self.player:
            return {
                'status': 'stopped',
                'source': None,
            }

        return {
            'status': self.player.playback_status(),
            'source': self.get_source(),
            'position': self.player.position(),
            'duration': self.player.duration(),
            'volume': self.player.volume(),
            'looping': self.looping,
        }
Exemplo n.º 3
0
class OmxPlayer():
    def __init__(self):
        self.player = None
        self.paired = False
        self.masterIp = None
        self.audio_volume = 100.0

    # omxplayer callbacks

    def posEvent(self, a, b):
        print('Position event!' + str(a) + " " + str(b))
        # print('Position: ' + str(player.position()) + "s")
        return

    def seekEvent(self, a, b):
        print('seek event! ' + str(b))
        return

    def triggerStart(self, pathToTrack, withPause=False):
        # lrpi_player#105
        # Audio output can be routed through hdmi or the jack,
        # if settings.json is corrupted, default to the hdmi

        settings_json = settings.get_settings()
        output_route = settings_json.get("audio_output")
        normalised_output_route = 'hdmi'
        omxArgs = []

        if output_route == 'hdmi':
            normalised_output_route = 'hdmi'
            omxArgs += ['-w', '--layout', '5.1']
        elif output_route == 'jack':
            normalised_output_route = 'local'

        omxArgs += ['-o', normalised_output_route]

        print('OUTPUT: ' + normalised_output_route)
        print('Full playing args: ' + str(omxArgs))

        if not withPause:
            self.player = OMXPlayer(
                pathToTrack,
                args=omxArgs,
                dbus_name='org.mpris.MediaPlayer2.omxplayer0')
            sleep(0.25)
        elif withPause:
            self.player = OMXPlayer(
                pathToTrack,
                args=omxArgs,
                dbus_name='org.mpris.MediaPlayer2.omxplayer0',
                pause=True)
            # Might need to set the volume to 0 a different way,
            # for some tracks omxplayer plays a short, sharp, shock
            # before setting the volume to 0
            self.player.set_volume(0)
            sleep(0.5)

    def primeForStart(self, pathToTrack):
        self.triggerStart(pathToTrack, withPause=True)

    def start(self, pathToTrack, syncTimestamp=None, master=False):
        print("Playing on omx... :", master)
        print("\n")
        print(pathToTrack)

        settings_json = settings.get_settings()
        volume = settings_json.get("audio_volume")

        try:
            if not master:
                if self.player:
                    self.player.quit()
                self.player = None

            if syncTimestamp:
                pause.until(syncTimestamp)

            if self.player is None or syncTimestamp is None:
                self.triggerStart(pathToTrack)

            self.player.positionEvent += self.posEvent
            self.player.seekEvent += self.seekEvent
            # self.player.set_position(0)

            if volume is not None:
                self.audio_volume = volume
                print("Volume set to %s" % self.audio_volume)

            self.player.set_volume(float(self.audio_volume) / 100.0)

            print('synctime in omxplayer: ', ctime(syncTimestamp))
            if master:
                self.player.play()
            return str(self.player.duration())
        except Exception as e:
            print(
                "ERROR: Could not start player... but audio may still be playing!"
            )
            print("Why: ", e)
            print("returning position 0...")
            return str(0)

    # action 16 is emulated keypress for playPause
    def playPause(self, syncTimestamp=None):
        print("Playpausing with syncTimeStamp: ", syncTimestamp)
        if syncTimestamp:
            pause.until(syncTimestamp)
        self.player.action(16)
        return str(self.player.duration())

    def getPosition(self):
        return self.player.position()

    def getDuration(self):
        return str(self.player.duration())

    def mute(self):
        print(self.player.volume())
        self.player.mute()

    def volumeUp(self):
        print("upper: ", self.player.volume())
        self.player.set_volume(self.player.volume() + 0.1)

    def volumeDown(self, interval):
        # If we're right at the end of the track, don't try to
        # lower the volume or else dbus will disconnect and
        # the server will look at though it's crashed

        if self.player.duration() - self.player.position() > 1:
            print("omx downer: ", self.player.volume())
            if (self.player.volume() <= 0.07 or interval == 0):
                return False
            else:
                self.player.set_volume(self.player.volume() -
                                       ((1.0 / interval) / 4.0))
                return True
        return False

    def seek(self, position, syncTimestamp=None):
        if self.player.can_seek():
            self.player.set_position(self.player.duration() *
                                     (position / 100.0))
        return self.player.duration() * (position / 100.0)

    def status(self, status):
        if self.player != None:
            print('status requested from omxplayer!')
            try:
                status["source"] = self.player.get_source()
                status["playerState"] = self.player.playback_status()
                status["canControl"] = self.player.can_control()
                status["position"] = self.player.position()
                status["trackDuration"] = self.player.duration()
                status["error"] = ""
            except Exception as e:
                status["playerState"] = ""
                status["canControl"] = False
                status[
                    "error"] = "Something went wrong with player status request: " + str(
                        e)

        else:
            status["playerState"] = ""
            status["canControl"] = False
            status["error"] = "Player is not initialized!"

        status["paired"] = self.paired
        status["master_ip"] = self.masterIp

        return status

    def setPaired(self, val, masterIp):
        self.paired = val
        self.masterIp = masterIp
        print('paired set to: ', val)
        print('master_ip set to: ', masterIp)

    def exit(self, syncTimestamp=None):
        if syncTimestamp:
            pause.until(syncTimestamp)

        if self.player:
            self.player.quit()
            self.__del__()
            killOmx()
        else:
            return 1

    def __del__(self):
        if self.player:
            self.player.quit()
        self.player = None
        killOmx()
        print("OMX died")