Author: dmeyer
Date: Mon Jan 22 17:06:57 2007
New Revision: 9045

Modified:
   trunk/ui/src/application/base.py
   trunk/ui/src/audio/player.py
   trunk/ui/src/video/player.py

Log:
add ressource reservation

Modified: trunk/ui/src/application/base.py
==============================================================================
--- trunk/ui/src/application/base.py    (original)
+++ trunk/ui/src/application/base.py    Mon Jan 22 17:06:57 2007
@@ -107,6 +107,7 @@
         elif status == STATUS_IDLE:
             handler.hide_application(self)
             self._status = status
+            print self, 'stop'
             self.signals['stop'].emit()
         else:
             self._status = status

Modified: trunk/ui/src/audio/player.py
==============================================================================
--- trunk/ui/src/audio/player.py        (original)
+++ trunk/ui/src/audio/player.py        Mon Jan 22 17:06:57 2007
@@ -4,19 +4,13 @@
 # -----------------------------------------------------------------------------
 # $Id$
 #
-# This module provides the audio player. It will use one of the registered
-# player plugins to play the audio item (e.g. audio.mplayer)
-#
-# You can access the player by calling audioplayer()
-#
 # -----------------------------------------------------------------------------
 # Freevo - A Home Theater PC framework
 # Copyright (C) 2002-2005 Krister Lagerstrom, Dirk Meyer, et al.
 #
-# First Edition: ?
-# Maintainer:    ?
+# Maintainer:    Dirk Meyer <[EMAIL PROTECTED]>
 #
-# Please see the file doc/CREDITS for a complete list of authors.
+# Please see the file AUTHORS for a complete list of authors.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -36,6 +30,9 @@
 
 __all__ = [ 'audioplayer' ]
 
+# python imports
+import logging
+
 # kaa imports
 import kaa.popcorn
 import kaa.notifier
@@ -48,14 +45,14 @@
         STATUS_STOPPED, STATUS_IDLE, CAPABILITY_TOGGLE, CAPABILITY_PAUSE, \
         CAPABILITY_FULLSCREEN
 
-import logging
+# get logging object
 log = logging.getLogger('audio')
 
 _singleton = None
 
 def audioplayer():
     """
-    return the global audio player object
+    Return the global audio player object
     """
     global _singleton
     if _singleton == None:
@@ -70,7 +67,7 @@
 
 class AudioPlayer(Application):
     """
-    basic object to handle the different player
+    Audio player object.
     """
     def __init__(self):
         capabilities = (CAPABILITY_TOGGLE, CAPABILITY_PAUSE)
@@ -84,11 +81,37 @@
         """
         play an item
         """
-        # FIXME: handle starting something when self.player.is_playing()
-        # if self.player and self.player.is_playing():
-        #     return
+        if not self.status in (STATUS_IDLE, STATUS_STOPPED):
+            # Already running, stop the current player by sending a STOP
+            # event. The event will also get to the playlist behind the
+            # current item and the whole list will be stopped.
+            Event(STOP, handler=self.eventhandler).post()
+            # Now connect to our own 'stop' signal once to repeat this current
+            # function call without the player playing
+            self.signals['stop'].connect_once(self.play, item)
+            return True
+
+        if not kaa.notifier.running:
+            # Freevo is in shutdown mode, do not start a new player, the old
+            # only stopped because of the shutdown.
+            return False
+
+        # Try to get AUDIO resource. The ressouce will be freed by the system
+        # when the application switches to STATUS_STOPPED or STATUS_IDLE.
+        blocked = self.get_resources('AUDIO')
+        if 'AUDIO' in blocked:
+            # Something has the audio resource blocked, stop it
+            Event(STOP, handler=blocked['AUDIO'].eventhandler).post()
+            # Now connect to the 'stop' signal once to repeat this current
+            # function call without the player playing
+            blocked['AUDIO'].signals['stop'].connect_once(self.play, item)
+            return True
 
+        # store item and playlist
         self.item = item
+        self.playlist = self.item.get_playlist()
+        if self.playlist:
+            self.playlist.select(self.item)
 
         # Calculate some new values
         if not self.item.length:
@@ -131,6 +154,47 @@
         self.status = STATUS_STOPPING
 
 
+    @kaa.notifier.yield_execution()
+    def pause(self):
+        """
+        Pause Playback. Return False if already paused.
+        """
+        # FIXME: make sure this function is not called twice
+        if not self.status == STATUS_RUNNING:
+            yield False
+        if self.player.get_state() == kaa.popcorn.STATE_PAUSED:
+            yield None
+        # FIXME: what happens if we send pause() the same time the file
+        # is finished? This would create a race condition.
+        self.player.pause()
+        # TODO: lines like the next trhree lines happen very often,
+        # maybe there is a way to make it shorter, e.g. yield a signal
+        wait = kaa.notifier.YieldCallback()
+        self.player.signals['pause'].connect_once(wait)
+        yield wait
+        self.free_resources()
+
+
+    @kaa.notifier.yield_execution()
+    def resume(self):
+        """
+        Resume Playback. Return False if already resumed.
+        """
+        # FIXME: make sure this function is not called twice
+        if not self.status == STATUS_RUNNING:
+            yield False
+        if self.player.get_state() == kaa.popcorn.STATE_PLAYING:
+            yield False
+        if self.get_resources('AUDIO'):
+            # FIXME: what to do in this case?
+            log.error('unable to get AUDIO ressource')
+            yield False
+        self.player.resume()
+        wait = kaa.notifier.YieldCallback()
+        self.player.signals['play'].connect_once(wait)
+        yield wait
+
+
     def elapsed(self):
         """
         Callback for elapsed time changes.

Modified: trunk/ui/src/video/player.py
==============================================================================
--- trunk/ui/src/video/player.py        (original)
+++ trunk/ui/src/video/player.py        Mon Jan 22 17:06:57 2007
@@ -4,19 +4,14 @@
 # -----------------------------------------------------------------------------
 # $Id$
 #
-# This module provides the video player. It will use one of the registered
-# player plugins to play the video item (e.g. video.mplayer)
-#
-# You can access the player by calling videoplayer()
-#
 # -----------------------------------------------------------------------------
 # Freevo - A Home Theater PC framework
-# Copyright (C) 2002-2005 Krister Lagerstrom, Dirk Meyer, et al.
+# Copyright (C) 2007 Dirk Meyer, et al.
 #
-# First Edition: ?
-# Maintainer:    ?
+# First Edition: Dirk Meyer <[EMAIL PROTECTED]>
+# Maintainer:    Dirk Meyer <[EMAIL PROTECTED]>
 #
-# Please see the file doc/CREDITS for a complete list of authors.
+# Please see the file AUTHORS for a complete list of authors.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -36,6 +31,9 @@
 
 __all__ = [ 'videoplayer' ]
 
+# python imports
+import logging
+
 # kaa imports
 import kaa.popcorn
 import kaa.notifier
@@ -48,14 +46,14 @@
         STATUS_STOPPED, STATUS_IDLE, CAPABILITY_TOGGLE, CAPABILITY_PAUSE, \
         CAPABILITY_FULLSCREEN
 
-import logging
+# get logging object
 log = logging.getLogger('video')
 
 _singleton = None
 
 def videoplayer():
     """
-    return the global video player object
+    Return the global video player object.
     """
     global _singleton
     if _singleton == None:
@@ -65,10 +63,10 @@
 
 class VideoPlayer(Application):
     """
-    basic object to handle the different player
+    Video player object.
     """
     def __init__(self):
-        capabilities = (CAPABILITY_PAUSE, CAPABILITY_FULLSCREEN)
+        capabilities = (CAPABILITY_FULLSCREEN, )
         Application.__init__(self, 'videoplayer', 'video', capabilities)
         self.player = kaa.popcorn.Player()
         self.player.set_window(self.engine.get_window())
@@ -79,11 +77,64 @@
         """
         play an item
         """
-        # FIXME: handle starting something when self.player.is_playing()
-        # if self.player and self.player.is_playing():
-        #     return
+        if not self.status in (STATUS_IDLE, STATUS_STOPPED):
+            # Already running, stop the current player by sending a STOP
+            # event. The event will also get to the playlist behind the
+            # current item and the whole list will be stopped.
+            Event(STOP, handler=self.eventhandler).post()
+            # Now connect to our own 'stop' signal once to repeat this current
+            # function call without the player playing
+            self.signals['stop'].connect_once(self.play, item)
+            return True
+
+        if not kaa.notifier.running:
+            # Freevo is in shutdown mode, do not start a new player, the old
+            # only stopped because of the shutdown.
+            return False
+
+        # Try to get VIDEO and AUDIO resources. The ressouces will be freed
+        # by the system when the application switches to STATUS_STOPPED or
+        # STATUS_IDLE.
+        blocked = self.get_resources('AUDIO', 'VIDEO')
+        if 'VIDEO' in blocked:
+            # Something has the video resource blocked. The only application
+            # possible is the tv player right now. It would be possible to
+            # ask the user what to do with a popup but we assume the user
+            # does not care about the tv and just stop it.
+            Event(STOP, handler=blocked['VIDEO'].eventhandler).post()
+            # Now connect to the 'stop' signal once to repeat this current
+            # function call without the player playing
+            blocked['VIDEO'].signals['stop'].connect_once(self.play, item)
+            return True
+        if 'AUDIO' in blocked:
+            # AUDIO is blocked, VIDEO is not. This is most likely the audio
+            # player and we can pause it. Do this if possible.
+            if not blocked['AUDIO'].has_capability(CAPABILITY_PAUSE):
+                # Unable to pause, just stop it
+                Event(STOP, handler=blocked['AUDIO'].eventhandler).post()
+                # Now connect to the 'stop' signal once to repeat this current
+                # function call without the player playing
+                blocked['AUDIO'].signals['stop'].connect_once(self.play, item)
+                return True
+            # Now pause the current player. On its pause signal this player can
+            # play again. And on the stop signal of this player (STATUS_IDLE)
+            # the other player can be resumed again.
+            in_progress = blocked['AUDIO'].pause()
+            if isinstance(in_progress, kaa.notifier.InProgress):
+                # takes some time, wait
+                cb = kaa.notifier.Callback(self.play, item)
+                cb.set_ignore_caller_args()
+                in_progress.connect(cb)
+            if in_progress is not False:
+                # we paused the application, resume on our stop
+                self.signals['stop'].connect_once(blocked['AUDIO'].resume)
+            return True
 
+        # store item and playlist
         self.item = item
+        self.playlist = self.item.get_playlist()
+        if self.playlist:
+            self.playlist.select(self.item)
 
         # set the current item to the gui engine
         self.engine.set_item(self.item)
@@ -169,6 +220,6 @@
             self.item.info['interlaced'] = interlaced
             self.player.set_property('deinterlace', interlaced)
             return True
-        
+
         # give it to the item
         return self.item.eventhandler(event)

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to