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.

151 lines
4.2 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 <gurney_j@resnet.uroegon.edu>
  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. modules = [ 'dvd', 'mpegtsmod', 'shoutcast', 'ZipStorage' ]
  24. modmap = {}
  25. for i in modules:
  26. modmap[i] = tryloadmodule(i)
  27. for i in modules:
  28. debug.insertnamespace(i, modmap[i])
  29. from DIDLLite import TextItem, AudioItem, VideoItem, ImageItem, Resource, StorageFolder
  30. from FSStorage import FSDirectory
  31. import os
  32. import os.path
  33. import random
  34. import socket
  35. import string
  36. import sys
  37. from twisted.python import log
  38. from twisted.internet import reactor
  39. def generateuuid():
  40. if False:
  41. return 'uuid:asdflkjewoifjslkdfj'
  42. return ''.join([ 'uuid:'] + map(lambda x: random.choice(string.letters), xrange(20)))
  43. listenAddr = sys.argv[1]
  44. if len(sys.argv) > 2:
  45. listenPort = int(sys.argv[2])
  46. if listenPort < 1024 or listenPort > 65535:
  47. raise ValueError, 'port out of range'
  48. else:
  49. listenPort = random.randint(10000, 65000)
  50. log.startLogging(sys.stdout)
  51. # Create SSDP server
  52. from SSDP import SSDPServer, SSDP_PORT, SSDP_ADDR
  53. s = SSDPServer()
  54. debug.insertnamespace('s', s)
  55. port = reactor.listenMulticast(SSDP_PORT, s)
  56. port.joinGroup(SSDP_ADDR)
  57. port.setLoopbackMode(0) # don't get our own sends
  58. uuid = generateuuid()
  59. urlbase = 'http://%s:%d/' % (listenAddr, listenPort)
  60. # Create SOAP server
  61. from twisted.web import server, resource, static
  62. from ContentDirectory import ContentDirectoryServer
  63. from ConnectionManager import ConnectionManagerServer
  64. class WebServer(resource.Resource):
  65. def __init__(self):
  66. resource.Resource.__init__(self)
  67. class RootDevice(static.Data):
  68. def __init__(self):
  69. r = {
  70. 'hostname': socket.gethostname(),
  71. 'uuid': uuid,
  72. 'urlbase': urlbase,
  73. }
  74. d = file('root-device.xml').read() % r
  75. static.Data.__init__(self, d, 'text/xml')
  76. root = WebServer()
  77. debug.insertnamespace('root', root)
  78. content = resource.Resource()
  79. cds = ContentDirectoryServer('My Media Server', klass = FSDirectory, path = 'media', urlbase = os.path.join(urlbase, 'content'), webbase = content) # This sets up the root to be the media dir so we don't have to enumerate the directory
  80. debug.insertnamespace('cds', cds)
  81. root.putChild('ContentDirectory', cds)
  82. cds = cds.control
  83. root.putChild('ConnectionManager', ConnectionManagerServer())
  84. root.putChild('root-device.xml', RootDevice())
  85. root.putChild('content', content)
  86. # Purely to ensure some sane mime-types. On MacOSX I need these.
  87. medianode = static.File('pymediaserv')
  88. medianode.contentTypes.update( {
  89. '.wmv': 'video/x-ms-wmv',
  90. #'.ts': 'video/mp2t',
  91. '.ts': 'video/mpeg', # we may want this instead of mp2t
  92. '.m2t': 'video/mpeg',
  93. '.mp4': 'video/mp4',
  94. #'.mp4': 'video/mpeg',
  95. '.dat': 'video/mpeg', # VCD tracks
  96. '.ogm': 'application/ogg',
  97. '.vob': 'video/mpeg',
  98. #'.m4a': 'audio/mp4', # D-Link can't seem to play AAC files.
  99. })
  100. del medianode
  101. site = server.Site(root)
  102. reactor.listenTCP(listenPort, site)
  103. # we need to do this after the children are there, since we send notifies
  104. s.register('%s::upnp:rootdevice' % uuid,
  105. 'upnp:rootdevice',
  106. urlbase + 'root-device.xml')
  107. s.register(uuid,
  108. uuid,
  109. urlbase + 'root-device.xml')
  110. s.register('%s::urn:schemas-upnp-org:device:MediaServer:1' % uuid,
  111. 'urn:schemas-upnp-org:device:MediaServer:1',
  112. urlbase + 'root-device.xml')
  113. s.register('%s::urn:schemas-upnp-org:service:ConnectionManager:1' % uuid,
  114. 'urn:schemas-upnp-org:device:ConnectionManager:1',
  115. urlbase + 'root-device.xml')
  116. s.register('%s::urn:schemas-upnp-org:service:ContentDirectory:1' % uuid,
  117. 'urn:schemas-upnp-org:device:ContentDirectory:1',
  118. urlbase + 'root-device.xml')
  119. # Main loop
  120. reactor.run()