A Python UPnP Media Server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
4.8 KiB

  1. #!/usr/bin/env python
  2. # Licensed under the MIT license
  3. # http://opensource.org/licenses/mit-license.php
  4. # Copyright 2005, Tim Potter <tpot@samba.org>
  5. # Copyright 2006 John-Mark Gurney <jmg@funkthat.com>
  6. __version__ = '$Change$'
  7. # $Id$
  8. # make sure debugging is initalized first, other modules can be pulled in
  9. # before the "real" debug stuff is setup. (hmm I could make this a two
  10. # stage, where we simulate a namespace to either be thrown away when the
  11. # time comes, or merge into the correct one)
  12. import debug # my debugging module
  13. debug.doDebugging(True) # open up debugging port
  14. # Modules to import, maybe config file or something?
  15. def tryloadmodule(mod):
  16. try:
  17. return __import__(mod)
  18. except ImportError:
  19. #import traceback
  20. #traceback.print_exc()
  21. pass
  22. # ZipStorage w/ tar support should be last as it will gobble up empty files.
  23. # These should be sorted by how much work they do, the least work the earlier.
  24. # mpegtsmod can be really expensive.
  25. modules = [
  26. 'pyvr',
  27. 'dvd',
  28. 'ZipStorage',
  29. 'mpegtsmod',
  30. ]
  31. modmap = {}
  32. for i in modules:
  33. modmap[i] = tryloadmodule(i)
  34. for i in modules:
  35. debug.insertnamespace(i, modmap[i])
  36. from DIDLLite import TextItem, AudioItem, VideoItem, ImageItem, Resource, StorageFolder
  37. from FSStorage import FSDirectory
  38. import os
  39. import os.path
  40. import random
  41. import socket
  42. import string
  43. import sys
  44. from twisted.python import log
  45. from twisted.internet import reactor
  46. def generateuuid():
  47. if False:
  48. return 'uuid:asdflkjewoifjslkdfj'
  49. return ''.join([ 'uuid:'] + map(lambda x: random.choice(string.letters), xrange(20)))
  50. listenAddr = sys.argv[1]
  51. if len(sys.argv) > 2:
  52. listenPort = int(sys.argv[2])
  53. if listenPort < 1024 or listenPort > 65535:
  54. raise ValueError, 'port out of range'
  55. else:
  56. listenPort = random.randint(10000, 65000)
  57. log.startLogging(sys.stdout)
  58. # Create SSDP server
  59. from SSDP import SSDPServer, SSDP_PORT, SSDP_ADDR
  60. s = SSDPServer()
  61. debug.insertnamespace('s', s)
  62. port = reactor.listenMulticast(SSDP_PORT, s, listenMultiple=True)
  63. port.joinGroup(SSDP_ADDR)
  64. port.setLoopbackMode(0) # don't get our own sends
  65. uuid = generateuuid()
  66. urlbase = 'http://%s:%d/' % (listenAddr, listenPort)
  67. # Create SOAP server
  68. from twisted.web import server, resource, static
  69. from ContentDirectory import ContentDirectoryServer
  70. from ConnectionManager import ConnectionManagerServer
  71. class WebServer(resource.Resource):
  72. def __init__(self):
  73. resource.Resource.__init__(self)
  74. class RootDevice(static.Data):
  75. def __init__(self):
  76. r = {
  77. 'hostname': socket.gethostname(),
  78. 'uuid': uuid,
  79. 'urlbase': urlbase,
  80. }
  81. d = file('root-device.xml').read() % r
  82. static.Data.__init__(self, d, 'text/xml')
  83. root = WebServer()
  84. debug.insertnamespace('root', root)
  85. content = resource.Resource()
  86. mediapath = 'media'
  87. if not os.path.isdir(mediapath):
  88. print >>sys.stderr, \
  89. 'Sorry, %s is not a directory, no content to serve.' % `mediapath`
  90. sys.exit(1)
  91. # This sets up the root to be the media dir so we don't have to
  92. # enumerate the directory
  93. cds = ContentDirectoryServer('My Media Server', klass=FSDirectory,
  94. path=mediapath, urlbase=os.path.join(urlbase, 'content'), webbase=content)
  95. debug.insertnamespace('cds', cds)
  96. root.putChild('ContentDirectory', cds)
  97. cds = cds.control
  98. root.putChild('ConnectionManager', ConnectionManagerServer())
  99. root.putChild('root-device.xml', RootDevice())
  100. root.putChild('content', content)
  101. # Purely to ensure some sane mime-types. On MacOSX I need these.
  102. medianode = static.File('pymediaserv')
  103. medianode.contentTypes.update( {
  104. # From: http://support.microsoft.com/kb/288102
  105. '.asf': 'video/x-ms-asf',
  106. '.asx': 'video/x-ms-asf',
  107. '.wma': 'audio/x-ms-wma',
  108. '.wax': 'audio/x-ms-wax',
  109. '.wmv': 'video/x-ms-wmv',
  110. '.wvx': 'video/x-ms-wvx',
  111. '.wm': 'video/x-ms-wm',
  112. '.wmx': 'video/x-ms-wmx',
  113. #'.ts': 'video/mp2t',
  114. '.ts': 'video/mpeg', # we may want this instead of mp2t
  115. '.m2t': 'video/mpeg',
  116. '.m2ts': 'video/mpeg',
  117. '.mp4': 'video/mp4',
  118. #'.mp4': 'video/mpeg',
  119. '.dat': 'video/mpeg', # VCD tracks
  120. '.ogm': 'application/ogg',
  121. '.vob': 'video/mpeg',
  122. #'.m4a': 'audio/mp4', # D-Link can't seem to play AAC files.
  123. })
  124. del medianode
  125. site = server.Site(root)
  126. reactor.listenTCP(listenPort, site)
  127. # we need to do this after the children are there, since we send notifies
  128. s.register('%s::upnp:rootdevice' % uuid,
  129. 'upnp:rootdevice',
  130. urlbase + 'root-device.xml')
  131. s.register(uuid,
  132. uuid,
  133. urlbase + 'root-device.xml')
  134. s.register('%s::urn:schemas-upnp-org:device:MediaServer:1' % uuid,
  135. 'urn:schemas-upnp-org:device:MediaServer:1',
  136. urlbase + 'root-device.xml')
  137. s.register('%s::urn:schemas-upnp-org:service:ConnectionManager:1' % uuid,
  138. 'urn:schemas-upnp-org:device:ConnectionManager:1',
  139. urlbase + 'root-device.xml')
  140. s.register('%s::urn:schemas-upnp-org:service:ContentDirectory:1' % uuid,
  141. 'urn:schemas-upnp-org:device:ContentDirectory:1',
  142. urlbase + 'root-device.xml')
  143. # Main loop
  144. reactor.run()