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

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

  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  import os 
 19   
 20  from twisted.web.resource import Resource 
 21  from twisted.web.static import Data, File 
 22   
 23  from flumotion.common import log 
 24  from flumotion.common.errors import ComponentStartError 
 25  from flumotion.component.misc.httpserver.httpserver import HTTPFileStreamer 
 26  from flumotion.component.plugs.base import ComponentPlug 
 27  from flumotion.component.plugs.cortado.cortado_location import \ 
 28       getCortadoFilename 
 29  from flumotion.configure import configure 
 30   
 31  __version__ = "$Rev$" 
 32   
 33   
34 -def _htmlbool(value):
35 if value: 36 return 'true' 37 return 'false'
38 39
40 -class CortadoDirectoryResource(Resource):
41 """I generate the directory used to serve a cortado applet 42 It contains:: 43 - a html file, usually called index.html. 44 - cortado.jar - cortado java applet 45 """ 46
47 - def __init__(self, mount_point, properties, filename):
48 Resource.__init__(self) 49 50 index_name = properties.get('index', 'index.html') 51 52 root = mount_point 53 if not root.endswith("/"): 54 root += "/" 55 if index_name != 'index.html': 56 root = None 57 self._mount_point_root = root 58 self._properties = properties 59 self._index_content = self._get_index_content() 60 self._index_name = index_name 61 self._cortado_filename = filename 62 self._addChildren()
63
64 - def _addChildren(self):
65 self.putChild("cortado.jar", 66 File(self._cortado_filename, 67 'application/x-java-archive')) 68 69 self.putChild(self._index_name, 70 self._index_content) 71 self.putChild('', self._index_content)
72
73 - def _get_template_filename(self):
74 filename = self._properties.get('html-template') 75 if not filename: 76 filename = os.path.join(configure.datadir, 77 'cortado-template.html') 78 return filename
79
80 - def _get_index_content(self):
81 html_template = self._get_template_filename() 82 ns = {} 83 ns['has-audio'] = _htmlbool(self._properties['has-audio']) 84 ns['has-video'] = _htmlbool(self._properties['has-video']) 85 for attribute in ['codebase', 86 'width', 87 'height', 88 'stream-url', 89 'buffer-size']: 90 ns[attribute] = self._properties[attribute] 91 92 data = open(html_template, 'r').read() 93 content = data % ns 94 return Data(content, 'text/html')
95 96
97 -class ComponentCortadoPlug(ComponentPlug):
98 """I am a component plug for a http-server which plugs in a 99 http resource containing a cortado java applet. 100 """ 101
102 - def start(self, component):
103 """ 104 @type component: L{HTTPFileStreamer} 105 """ 106 if not isinstance(component, HTTPFileStreamer): 107 raise ComponentStartError( 108 "A CortadoPlug %s must be plugged into a " 109 " HTTPStreamer component, not a %s" % ( 110 self, component.__class__.__name__)) 111 filename = getCortadoFilename() 112 if not filename: 113 raise ComponentStartError( 114 "Could not find cortado jar file") 115 log.debug('cortado', 'Attaching to %r' % (component, )) 116 resource = CortadoDirectoryResource(component.getMountPoint(), 117 self.args['properties'], 118 filename) 119 component.setRootResource(resource)
120 121
122 -def test():
123 import sys 124 from twisted.internet import reactor 125 from twisted.python.log import startLogging 126 from twisted.web.server import Site 127 startLogging(sys.stderr) 128 129 properties = {'has-audio': True, 130 'has-video': True, 131 'codebase': '/', 132 'width': 320, 133 'height': 240, 134 'stream-url': '/stream.ogg', 135 'buffer-size': 40} 136 root = CortadoDirectoryResource('/', properties, getCortadoFilename()) 137 site = Site(root) 138 139 reactor.listenTCP(8080, site) 140 reactor.run()
141 142 if __name__ == "__main__": 143 test() 144