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