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