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.

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