servicemp3.cpp: more simple/flexible streaming detection
[enigma2.git] / lib / python / Plugins / Extensions / DVDPlayer / plugin.py
index 8e2a9f3a75d242beee20177129d68f2f247b8434..1cee0aac4bc8568a041bad2142dcbfd3850e7878 100644 (file)
@@ -4,9 +4,10 @@ from Screens.Screen import Screen
 from Screens.MessageBox import MessageBox
 from Screens.ChoiceBox import ChoiceBox
 from Screens.HelpMenu import HelpableScreen
-from Screens.InfoBarGenerics import InfoBarSeek, InfoBarPVRState, InfoBarCueSheetSupport, InfoBarShowHide, InfoBarNotifications
+from Screens.InfoBarGenerics import InfoBarSeek, InfoBarPVRState, InfoBarCueSheetSupport, InfoBarShowHide, InfoBarNotifications, InfoBarAudioSelection, InfoBarSubtitleSupport
 from Components.ActionMap import ActionMap, NumberActionMap, HelpableActionMap
 from Components.Label import Label
+from Components.Sources.StaticText import StaticText
 from Components.Pixmap import Pixmap
 from Components.FileList import FileList
 from Components.MenuList import MenuList
@@ -20,13 +21,13 @@ import servicedvd # load c++ part of dvd player plugin
 lastpath = ""
 
 class FileBrowser(Screen):
-       skin = """
-       <screen name="FileBrowser" position="100,100" size="520,376" title="DVD File Browser" >
-               <widget name="filelist" position="0,0" size="520,376" scrollbarMode="showOnDemand" />
-       </screen>"""
+
        def __init__(self, session, dvd_filelist = [ ]):
                Screen.__init__(self, session)
 
+               # for the skin: first try FileBrowser_DVDPlayer, then FileBrowser, this allows individual skinning
+               self.skinName = ["FileBrowser_DVDPlayer", "FileBrowser" ]
+
                self.dvd_filelist = dvd_filelist
                if len(dvd_filelist):   
                        self["filelist"] = MenuList(self.dvd_filelist)
@@ -38,15 +39,25 @@ class FileBrowser(Screen):
                                currDir = "/media/dvd/"
                        if not pathExists(currDir):
                                currDir = "/"
+                       if lastpath == "":  # 'None' is magic to start at the list of mountpoints
+                               currDir = None
 
-                       self.filelist = FileList(currDir, matchingPattern = "(?i)^.*\.(iso)", useServiceRef = True)
+                       inhibitDirs = ["/bin", "/boot", "/dev", "/etc", "/home", "/lib", "/proc", "/sbin", "/share", "/sys", "/tmp", "/usr", "/var"]
+                       self.filelist = FileList(currDir, matchingPattern = "(?i)^.*\.(iso|img)", useServiceRef = True)
                        self["filelist"] = self.filelist
 
-               self["FilelistActions"] = ActionMap(["OkCancelActions"],
+               self["FilelistActions"] = ActionMap(["SetupActions"],
                        {
+                               "save": self.ok,
                                "ok": self.ok,
                                "cancel": self.exit
                        })
+               self["key_red"] = StaticText(_("Cancel"))
+               self["key_green"] = StaticText(_("OK"))
+               self.onLayoutFinish.append(self.layoutFinished)
+
+       def layoutFinished(self):
+               self.setTitle(_("DVD File Browser"))
 
        def ok(self):
                if len(self.dvd_filelist):
@@ -71,6 +82,12 @@ class FileBrowser(Screen):
                                        lastpath = (pathname.rstrip("/").rsplit("/",1))[0]
                                        print "lastpath video_ts.ifo=", lastpath
                                        self.close(pathname)
+                               if fileExists(pathname+"VIDEO_TS/VIDEO_TS.IFO"):
+                                       print "dvd structure found, trying to open..."
+                                       lastpath = (pathname.rstrip("/").rsplit("/",1))[0]
+                                       print "lastpath video_ts.ifo=", lastpath
+                                       pathname += "VIDEO_TS"
+                                       self.close(pathname)
                        else:
                                lastpath = filename[0:filename.rfind("/")]
                                print "lastpath directory=", lastpath
@@ -80,12 +97,12 @@ class FileBrowser(Screen):
                self.close(None)
 
 class DVDSummary(Screen):
-       skin = """
-       <screen position="0,0" size="132,64">
+       skin = (
+       """<screen name="DVDSummary" position="0,0" size="132,64" id="1">
                <widget source="session.CurrentService" render="Label" position="5,4" size="120,28" font="Regular;12" transparent="1" >
                        <convert type="ServiceName">Name</convert>
                </widget>
-               <widget name="DVDPlayer" position="5,30" size="66,16" font="Regular;12" transparent="1" />
+               <widget name="DVDPlayer" position="5,30" size="66,16" font="Regular;11" transparent="1" />
                <widget name="Chapter" position="72,30" size="54,16" font="Regular;12" transparent="1" halign="right" />
                <widget source="session.CurrentService" render="Label" position="66,46" size="60,18" font="Regular;16" transparent="1" halign="right" >
                        <convert type="ServicePosition">Position</convert>
@@ -93,7 +110,20 @@ class DVDSummary(Screen):
                <widget source="session.CurrentService" render="Progress" position="6,46" size="60,18" borderWidth="1" >
                        <convert type="ServicePosition">Position</convert>
                </widget>
-       </screen>"""
+       </screen>""",
+       """<screen name="DVDSummary" position="0,0" size="96,64" id="2">
+               <widget source="session.CurrentService" render="Label" position="0,0" size="96,25" font="Regular;12" transparent="1" >
+                       <convert type="ServiceName">Name</convert>
+               </widget>
+               <widget name="DVDPlayer" position="0,26" size="96,12" font="Regular;10" transparent="1" />
+               <widget name="Chapter" position="0,40" size="66,12" font="Regular;10" transparent="1" halign="left" />
+               <widget source="session.CurrentService" render="Label" position="66,40" size="30,12" font="Regular;10" transparent="1" halign="right" >
+                       <convert type="ServicePosition">Position</convert>
+               </widget>
+               <widget source="session.CurrentService" render="Progress" position="0,52" size="96,12" borderWidth="1" >
+                       <convert type="ServicePosition">Position</convert>
+               </widget>
+       </screen>""")
 
        def __init__(self, session, parent):
                Screen.__init__(self, session, parent)
@@ -165,8 +195,8 @@ class ChapterZap(Screen):
                self.Timer.callback.append(self.keyOK)
                self.Timer.start(3000, True)
 
-class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarPVRState, InfoBarShowHide, HelpableScreen, InfoBarCueSheetSupport):
-#      ALLOW_SUSPEND = True
+class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarPVRState, InfoBarShowHide, HelpableScreen, InfoBarCueSheetSupport, InfoBarAudioSelection, InfoBarSubtitleSupport):
+       ALLOW_SUSPEND = Screen.SUSPEND_PAUSES
        ENABLE_RESUME_SUPPORT = True
        
        skin = """
@@ -214,8 +244,6 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
                self.saved_config_speeds_backward = config.seek.speeds_backward.value
                self.saved_config_enter_forward = config.seek.enter_forward.value
                self.saved_config_enter_backward = config.seek.enter_backward.value
-               self.saved_config_seek_stepwise_minspeed = config.seek.stepwise_minspeed.value
-               self.saved_config_seek_stepwise_repeat = config.seek.stepwise_repeat.value
                self.saved_config_seek_on_pause = config.seek.on_pause.value
                self.saved_config_seek_speeds_slowmotion = config.seek.speeds_slowmotion.value
 
@@ -225,8 +253,6 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
                config.seek.speeds_slowmotion.value = [ ]
                config.seek.enter_forward.value = "2"
                config.seek.enter_backward.value = "2"
-               config.seek.stepwise_minspeed.value = "Never"
-               config.seek.stepwise_repeat.value = "3"
                config.seek.on_pause.value = "play"
 
        def restore_infobar_seek_config(self):
@@ -235,8 +261,6 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
                config.seek.speeds_slowmotion.value = self.saved_config_seek_speeds_slowmotion
                config.seek.enter_forward.value = self.saved_config_enter_forward
                config.seek.enter_backward.value = self.saved_config_enter_backward
-               config.seek.stepwise_minspeed.value = self.saved_config_seek_stepwise_minspeed
-               config.seek.stepwise_repeat.value = self.saved_config_seek_stepwise_repeat
                config.seek.on_pause.value = self.saved_config_seek_on_pause
 
        def __init__(self, session, dvd_device = None, dvd_filelist = [ ], args = None):
@@ -245,10 +269,12 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
                InfoBarNotifications.__init__(self)
                InfoBarCueSheetSupport.__init__(self, actionmap = "MediaPlayerCueSheetActions")
                InfoBarShowHide.__init__(self)
+               InfoBarAudioSelection.__init__(self)
+               InfoBarSubtitleSupport.__init__(self)
                HelpableScreen.__init__(self)
                self.save_infobar_seek_config()
                self.change_infobar_seek_config()
-               InfoBarSeek.__init__(self, useSeekBackHack=False)
+               InfoBarSeek.__init__(self)
                InfoBarPVRState.__init__(self)
                self.dvdScreen = self.session.instantiateDialog(DVDOverlay)
 
@@ -324,6 +350,7 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
                                "prevTitle": (self.prevTitle, _("jump back to the previous title")),
                                "tv": (self.askLeavePlayer, _("exit DVD player or return to file browser")),
                                "dvdAudioMenu": (self.enterDVDAudioMenu, _("(show optional DVD audio menu)")),
+                               "AudioSelection": (self.enterAudioSelection, _("Select audio track")),
                                "nextAudioTrack": (self.nextAudioTrack, _("switch to the next audio track")),
                                "nextSubtitleTrack": (self.nextSubtitleTrack, _("switch to the next subtitle language")),
                                "nextAngle": (self.nextAngle, _("switch to the next angle")),
@@ -345,28 +372,19 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
                        })
 
                self.onClose.append(self.__onClose)
-               self.physicalDVD = False
-               self.dvd_device = None
+
+               from Plugins.SystemPlugins.Hotplug.plugin import hotplugNotifier
+               hotplugNotifier.append(self.hotplugCB)
+               
+               self.autoplay = dvd_device or dvd_filelist
+
                if dvd_device:
-                               self.dvd_device = dvd_device
-                               self.physicalDVD = True
+                       self.physicalDVD = True
                else:
-                       devicepath = harddiskmanager.getAutofsMountpoint(harddiskmanager.getCD())
-                       if pathExists(devicepath):
-                               from Components.Scanner import scanDevice
-                               res = scanDevice(devicepath)
-                               list = [ (r.description, r, res[r], self.session) for r in res ]
-                               if list:
-                                       (desc, scanner, files, session) = list[0]
-                                       for file in files:
-                                               print file
-                                               if file.mimetype == "video/x-dvd":
-                                                       self.dvd_device = devicepath
-                                                       print "physical dvd found:", self.dvd_device
-                                                       self.physicalDVD = True
+                       self.scanHotplug()
 
                self.dvd_filelist = dvd_filelist
-               self.onFirstExecBegin.append(self.showFileBrowser)
+               self.onFirstExecBegin.append(self.opened)
                self.service = None
                self.in_menu = False
 
@@ -511,8 +529,12 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
                
        def askLeavePlayer(self):
                choices = [(_("Exit"), "exit"), (_("Continue playing"), "play")]
-               if not self.physicalDVD:
+               if True or not self.physicalDVD:
                        choices.insert(1,(_("Return to file browser"), "browser"))
+               if self.physicalDVD:
+                       cur = self.session.nav.getCurrentlyPlayingServiceReference()
+                       if cur and not cur.toString().endswith(harddiskmanager.getAutofsMountpoint(harddiskmanager.getCD())):
+                           choices.insert(0,(_("Play DVD"), "playPhysical" ))
                self.session.openWithCallback(self.exitCB, ChoiceBox, title=_("Leave DVD Player?"), list = choices)
 
        def sendKey(self, key):
@@ -521,6 +543,9 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
                        keys.keyPressed(key)
                return keys
 
+       def enterAudioSelection(self):
+               self.audioSelection()
+
        def nextAudioTrack(self):
                self.sendKey(iServiceKeys.keyUser)
 
@@ -581,23 +606,24 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
        def keyCancel(self):
                self.askLeavePlayer()
 
-       def showFileBrowser(self):
-               if self.physicalDVD and len(self.dvd_filelist) == 0:
-                       if self.dvd_device == harddiskmanager.getAutofsMountpoint(harddiskmanager.getCD()):
-                               self.session.openWithCallback(self.DVDdriveCB, MessageBox, text=_("Do you want to play DVD in drive?"), timeout=5 )
-                       else:
-                               self.DVDdriveCB(True)
-               elif len(self.dvd_filelist) == 1:
+       def opened(self):
+               if self.autoplay and self.dvd_filelist:
+                       # opened via autoplay
                        self.FileBrowserClosed(self.dvd_filelist[0])
+               elif self.autoplay and self.physicalDVD:
+                       self.playPhysicalCB(True)
+               elif self.physicalDVD:
+                       # opened from menu with dvd in drive
+                       self.session.openWithCallback(self.playPhysicalCB, MessageBox, text=_("Do you want to play DVD in drive?"), timeout=5 )
                else:
+                       # opened from menu without dvd in drive
                        self.session.openWithCallback(self.FileBrowserClosed, FileBrowser, self.dvd_filelist)
-       
-       def DVDdriveCB(self, answer):
+
+       def playPhysicalCB(self, answer):
                if answer == True:
-                       self.FileBrowserClosed(self.dvd_device)
+                       self.FileBrowserClosed(harddiskmanager.getAutofsMountpoint(harddiskmanager.getCD()))
                else:
                        self.session.openWithCallback(self.FileBrowserClosed, FileBrowser)
-                       self.physicalDVD = False
 
        def FileBrowserClosed(self, val):
                curref = self.session.nav.getCurrentlyPlayingServiceReference()
@@ -608,6 +634,14 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
                        newref = eServiceReference(4369, 0, val)
                        print "play", newref.toString()
                        if curref is None or curref != newref:
+                               if newref.toString().endswith("/VIDEO_TS") or newref.toString().endswith("/"):
+                                       names = newref.toString().rsplit("/",3)
+                                       if names[2].startswith("Disk ") or names[2].startswith("DVD "):
+                                               name = str(names[1]) + " - " + str(names[2])
+                                       else:
+                                               name = names[2]
+                                       print "setting name to: ", self.service
+                                       newref.setName(str(name))
                                self.session.nav.playService(newref)
                                self.service = self.session.nav.getCurrentService()
                                print "self.service", self.service
@@ -627,13 +661,19 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
                                #else
                                if self.service:
                                        self.service = None
-                               self.showFileBrowser()
+                               self.session.openWithCallback(self.FileBrowserClosed, FileBrowser)
+                       if answer[1] == "playPhysical":
+                               if self.service:
+                                       self.service = None
+                               self.playPhysicalCB(True)
                        else:
                                pass
 
        def __onClose(self):
                self.restore_infobar_seek_config()
                self.session.nav.playService(self.oldService)
+               from Plugins.SystemPlugins.Hotplug.plugin import hotplugNotifier
+               hotplugNotifier.remove(self.hotplugCB)
 
        def playLastCB(self, answer): # overwrite infobar cuesheet function
                print "playLastCB", answer, self.resume_point
@@ -660,6 +700,30 @@ class DVDPlayer(Screen, InfoBarBase, InfoBarNotifications, InfoBarSeek, InfoBarP
        def calcRemainingTime(self):
                return 0
 
+       def hotplugCB(self, dev, media_state):
+               print "[hotplugCB]", dev, media_state
+               if dev == harddiskmanager.getCD():
+                       if media_state == "1":
+                               self.scanHotplug()
+                       else:
+                               self.physicalDVD = False
+
+       def scanHotplug(self):
+               devicepath = harddiskmanager.getAutofsMountpoint(harddiskmanager.getCD())
+               if pathExists(devicepath):
+                       from Components.Scanner import scanDevice
+                       res = scanDevice(devicepath)
+                       list = [ (r.description, r, res[r], self.session) for r in res ]
+                       if list:
+                               (desc, scanner, files, session) = list[0]
+                               for file in files:
+                                       print file
+                                       if file.mimetype == "video/x-dvd":
+                                               print "physical dvd found:", devicepath
+                                               self.physicalDVD = True
+                                               return
+               self.physicalDVD = False
+
 def main(session, **kwargs):
        session.open(DVDPlayer)
 
@@ -702,13 +766,14 @@ def filescan(**kwargs):
                        paths_to_scan =
                                [
                                        ScanPath(path = "video_ts", with_subdirs = False),
+                                       ScanPath(path = "VIDEO_TS", with_subdirs = False),
                                        ScanPath(path = "", with_subdirs = False),
                                ],
                        name = "DVD",
-                       description = "Play DVD",
+                       description = _("Play DVD"),
                        openfnc = filescan_open,
                )]              
 
 def Plugins(**kwargs):
-       return [PluginDescriptor(name = "DVDPlayer", description = "Play DVDs", where = PluginDescriptor.WHERE_MENU, fnc = menu),
-                       PluginDescriptor(where = PluginDescriptor.WHERE_FILESCAN, fnc = filescan)]
+       return [PluginDescriptor(name = "DVDPlayer", description = "Play DVDs", where = PluginDescriptor.WHERE_MENU, needsRestart = True, fnc = menu),
+                       PluginDescriptor(where = PluginDescriptor.WHERE_FILESCAN, needsRestart = True, fnc = filescan)]