instrumentation: add next-share/
[cs-p2p-next.git] / instrumentation / next-share / BaseLib / Video / utils.py
1 # Written by Arno Bakker
2 # see LICENSE.txt for license information
3
4 import os
5 import sys
6
7 from BaseLib.Core.Utilities.unicode import unicode2str
8 if (sys.platform == 'win32'):
9     from BaseLib.Core.Utilities.win32regchecker import Win32RegChecker,HKLM
10
11 videoextdefaults = ['aac','asf','avi','dv','divx','flac','flc','flv','mkv','mpeg','mpeg4','mpegts','mpg4','mp3','mp4','mpg','mkv','mov','m4v','ogg','ogm', 'ogv', 'oga', 'ogx','qt', 'rm','swf','ts','vob','wmv','wav', 'webm']
12 # Ric: added svc ext. for enhancement layers
13 svcextdefaults = ['dat']
14
15 DEBUG = False
16
17 def win32_retrieve_video_play_command(ext,videourl):
18     """ Use the specified extension of to find the player in the Windows registry to play the url (or file)"""
19     registry = Win32RegChecker()
20     
21     if DEBUG:
22         print >>sys.stderr,"videoplay: Looking for player for",unicode2str(videourl)
23     if ext == '':
24         return [None,None]
25     
26     contenttype = None
27     winfiletype = registry.readRootKey(ext)
28     if DEBUG:
29         print >>sys.stderr,"videoplay: winfiletype is",winfiletype,type(winfiletype)
30     if winfiletype is None or winfiletype == '':
31         # Darn.... Try this: (VLC seems to be the one messing the registry up in the
32         # first place)
33         winfiletype = registry.readRootKey(ext,value_name="VLC.Backup")
34         if winfiletype is None or winfiletype == '':
35             return [None,None]
36         # Get MIME type
37     if DEBUG:
38         print >>sys.stderr,"videoplay: Looking for player for ext",ext,"which is type",winfiletype
39
40     contenttype = registry.readRootKey(ext,value_name="Content Type")
41     
42     playkey = winfiletype+"\shell\play\command"
43     urlopen = registry.readRootKey(playkey)
44     if urlopen is None:
45         openkey = winfiletype+"\shell\open\command"
46         urlopen = registry.readRootKey(openkey)
47         if urlopen is None:
48             return [None,None]
49
50     # Default is e.g. "C:\Program Files\Windows Media Player\wmplayer.exe" /prefetch:7 /Play "%L"
51     # Replace %L
52     suo = urlopen.strip() # spaces
53     idx = suo.find('%L')
54     if idx == -1:
55         # Hrrrr: Quicktime uses %1 instead of %L and doesn't seem to quote the program path
56         idx = suo.find('%1')
57         if idx == -1:
58             return [None,None]
59         else:
60             replace = '%1'
61             idx2 = suo.find('%2',idx)
62             if idx2 != -1:
63                 # Hmmm, a trailer, let's get rid of it
64                 if suo[idx-1] == '"':
65                     suo = suo[:idx+3] # quoted
66                 else:
67                     suo = suo[:idx+1]
68     else:
69         replace = '%L'
70         
71     # St*pid quicktime doesn't properly quote the program path, e.g.
72     # C:\Program Files\Quicktime\bla.exe "%1" instead of
73     # "C:\Program Files\Quicktime\bla.exe" "%1"
74     if suo[0] != '"':    
75         if idx > 0 and (len(suo)-1) >= idx+2 and suo[idx-1] == '"' and suo[idx+2]=='"':
76             # %x is quoted
77             end = max(0,idx-2)
78         else:
79             end = max(0,idx-1)
80         # I assume everthing till end is the program path
81         progpath = suo[0:end]
82         qprogpath = quote_program_path(progpath)
83         if qprogpath is None:
84             return [None,None]
85         suo = qprogpath+suo[end:]
86         if DEBUG:
87             print >>sys.stderr,"videoplay: new urlopen is",suo
88     return [contenttype,suo.replace(replace,videourl)]
89
90
91 def win32_retrieve_playcmd_from_mimetype(mimetype,videourl):
92     """ Use the specified MIME type to find the player in the Windows registry to play the url (or file)"""
93     registry = Win32RegChecker()
94     
95     if DEBUG:
96         print >>sys.stderr,"videoplay: Looking for player for",unicode2str(videourl)
97     if mimetype == '' or mimetype is None:
98         return [None,None]
99     
100     keyname = '\\SOFTWARE\\Classes\\MIME\\Database\\Content Type\\'+mimetype
101     valuename = 'Extension'
102     ext = registry.readKeyRecursively(HKLM,keyname,value_name=valuename)
103     if DEBUG:
104         print >>sys.stderr,"videoplay: ext winfiletype is",ext
105     if ext is None or ext == '':
106         return [None,None]
107     if DEBUG:
108         print >>sys.stderr,"videoplay: Looking for player for mime",mimetype,"which is ext",ext
109
110     return win32_retrieve_video_play_command(ext,videourl)
111
112
113 def quote_program_path(progpath):
114     idx = progpath.find(' ')
115     if idx != -1:
116         # Contains spaces, should quote if it's really path
117         if not os.access(progpath,os.R_OK):
118             if DEBUG:
119                 print >>sys.stderr,"videoplay: Could not find assumed progpath",progpath
120             return None
121         return '"'+progpath+'"'
122     else:
123         return progpath