Package flumotion :: Package component :: Package plugs :: Package html5 :: Module html5
[hide private]

Source Code for Module flumotion.component.plugs.html5.html5

  1  # -*- Mode: Python -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3   
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007,2008,2009 Fluendo, S.L. 
  6  # Copyright (C) 2010,2011 Flumotion Services, S.A. 
  7  # All rights reserved. 
  8  # 
  9  # This file may be distributed and/or modified under the terms of 
 10  # the GNU Lesser General Public License version 2.1 as published by 
 11  # the Free Software Foundation. 
 12  # This file is distributed without any warranty; without even the implied 
 13  # warranty of merchantability or fitness for a particular purpose. 
 14  # See "LICENSE.LGPL" in the source distribution for more information. 
 15  # 
 16  # Headers in this file shall remain intact. 
 17   
 18  from twisted.web.resource import Resource 
 19  from twisted.web.static import Data 
 20   
 21  from flumotion.common import log 
 22  from flumotion.common.errors import ComponentStartError 
 23  from flumotion.component.misc.httpserver.httpserver import HTTPFileStreamer 
 24  from flumotion.component.plugs.base import ComponentPlug 
 25   
 26  __version__ = "$Rev$" 
 27   
 28  HTML5TEMPLATE = \ 
 29  """ 
 30  <html> 
 31  <head><title>Flumotion Stream</title></head> 
 32  <body> 
 33  <video height="%(height)d" width="%(width)d" controls autoplay> 
 34  <source type='%(mime-type)s; codecs="%(codecs)s"' src="%(stream-url)s"> 
 35  </source> 
 36  </video> 
 37  </body> 
 38  """ 
 39   
 40   
41 -def _htmlbool(value):
42 if value: 43 return 'true' 44 return 'false'
45 46
47 -class Html5DirectoryResource(Resource):
48 """I generate the directory used to serve an html5 viewing page 49 It contains:: 50 - a html file, usually called index.html. 51 """ 52
53 - def __init__(self, mount_point, properties):
54 Resource.__init__(self) 55 56 index_name = properties.get('index', 'index.html') 57 58 root = mount_point 59 if not root.endswith("/"): 60 root += "/" 61 if index_name != 'index.html': 62 root = None 63 self._mount_point_root = root 64 self._properties = properties 65 self._index_content = self._get_index_content() 66 self._index_name = index_name 67 self._addChildren()
68
69 - def _addChildren(self):
70 self.putChild(self._index_name, 71 self._index_content) 72 self.putChild('', self._index_content)
73
74 - def _get_index_content(self):
75 ns = {} 76 for attribute in ['codecs', 77 'mime-type', 78 'width', 79 'height', 80 'stream-url']: 81 ns[attribute] = self._properties[attribute] 82 83 content = HTML5TEMPLATE % ns 84 return Data(content, 'text/html')
85 86
87 -class ComponentHtml5Plug(ComponentPlug):
88 """I am a component plug for a http-server which plugs in a 89 http resource containing a html5 viewing page. 90 """ 91
92 - def start(self, component):
93 """ 94 @type component: L{HTTPFileStreamer} 95 """ 96 if not isinstance(component, HTTPFileStreamer): 97 raise ComponentStartError( 98 "An HTML5Plug %s must be plugged into a " 99 " HTTPFileStreamer component, not a %s" % ( 100 self, component.__class__.__name__)) 101 log.debug('html5', 'Attaching to %r' % (component, )) 102 resource = Html5DirectoryResource(component.getMountPoint(), 103 self.args['properties']) 104 component.setRootResource(resource)
105 106
107 -def test():
108 import sys 109 from twisted.internet import reactor 110 from twisted.python.log import startLogging 111 from twisted.web.server import Site 112 startLogging(sys.stderr) 113 114 properties = {'width': 320, 115 'height': 240, 116 'stream-url': '/stream.ogg', 117 'buffer-size': 40} 118 root = Html5DirectoryResource('/', properties) 119 site = Site(root) 120 121 reactor.listenTCP(8080, site) 122 reactor.run()
123 124 if __name__ == "__main__": 125 test() 126