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.

128 lines
3.4 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. #
  7. # $Id$
  8. #
  9. # Modules to import, maybe config file or something?
  10. import ZipStorage
  11. from DIDLLite import TextItem, AudioItem, VideoItem, ImageItem, Resource, StorageFolder
  12. from FSStorage import FSDirectory
  13. import os
  14. import os.path
  15. import random
  16. import socket
  17. import string
  18. import sys
  19. from twisted.python import log
  20. from twisted.internet import reactor
  21. def generateuuid():
  22. if False:
  23. return 'uuid:asdflkjewoifjslkdfj'
  24. return ''.join([ 'uuid:'] + map(lambda x: random.choice(string.letters), xrange(20)))
  25. listenAddr = sys.argv[1]
  26. if len(sys.argv) > 2:
  27. listenPort = int(sys.argv[2])
  28. if listenPort < 1024 or listenPort > 65535:
  29. raise ValueError, 'port out of range'
  30. else:
  31. listenPort = random.randint(10000, 65000)
  32. log.startLogging(sys.stdout)
  33. # Create SSDP server
  34. from SSDP import SSDPServer, SSDP_PORT, SSDP_ADDR
  35. s = SSDPServer()
  36. port = reactor.listenMulticast(SSDP_PORT, s)
  37. port.joinGroup(SSDP_ADDR)
  38. port.setLoopbackMode(0) # don't get our own sends
  39. uuid = generateuuid()
  40. urlbase = 'http://%s:%d/' % (listenAddr, listenPort)
  41. # Create SOAP server
  42. from twisted.web import server, resource, static
  43. from ContentDirectory import ContentDirectoryServer
  44. from ConnectionManager import ConnectionManagerServer
  45. class WebServer(resource.Resource):
  46. def __init__(self):
  47. resource.Resource.__init__(self)
  48. class RootDevice(static.Data):
  49. def __init__(self):
  50. r = {
  51. 'hostname': socket.gethostname(),
  52. 'uuid': uuid,
  53. 'urlbase': urlbase,
  54. }
  55. d = file('root-device.xml').read() % r
  56. static.Data.__init__(self, d, 'text/xml')
  57. root = WebServer()
  58. content = resource.Resource()
  59. 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
  60. root.putChild('ContentDirectory', cds)
  61. cds = cds.control
  62. root.putChild('ConnectionManager', ConnectionManagerServer())
  63. root.putChild('root-device.xml', RootDevice())
  64. root.putChild('content', content)
  65. # Area of server to serve media files from
  66. from MediaServer import MediaServer
  67. medianode = static.File('media')
  68. medianode.contentTypes.update( {
  69. '.wmv': 'video/x-ms-wmv',
  70. #'.ts': 'video/mp2t',
  71. '.ts': 'video/mpeg', # we may want this instead of mp2t
  72. #'.mp4': 'video/mp4',
  73. '.mp4': 'video/mpeg',
  74. '.dat': 'video/mpeg', # VCD tracks
  75. '.ogm': 'application/ogg',
  76. '.vob': 'video/mpeg',
  77. #'.m4a': 'audio/mp4', # D-Link can't seem to play AAC files.
  78. })
  79. root.putChild('media', medianode)
  80. site = server.Site(root)
  81. reactor.listenTCP(listenPort, site)
  82. # we need to do this after the children are there, since we send notifies
  83. s.register('%s::upnp:rootdevice' % uuid,
  84. 'upnp:rootdevice',
  85. urlbase + 'root-device.xml')
  86. s.register(uuid,
  87. uuid,
  88. urlbase + 'root-device.xml')
  89. s.register('%s::urn:schemas-upnp-org:device:MediaServer:1' % uuid,
  90. 'urn:schemas-upnp-org:device:MediaServer:1',
  91. urlbase + 'root-device.xml')
  92. s.register('%s::urn:schemas-upnp-org:service:ConnectionManager:1' % uuid,
  93. 'urn:schemas-upnp-org:device:ConnectionManager:1',
  94. urlbase + 'root-device.xml')
  95. s.register('%s::urn:schemas-upnp-org:service:ContentDirectory:1' % uuid,
  96. 'urn:schemas-upnp-org:device:ContentDirectory:1',
  97. urlbase + 'root-device.xml')
  98. # Main loop
  99. reactor.run()