Red5workshop 090619073420-phpapp02


Published on

Published in: Engineering, Technology
  • Be the first to comment

  • Be the first to like this

Red5workshop 090619073420-phpapp02

  1. 1. What is red5 • An Open Source Flash Media Server • Built on Java (Mina & Spring) • Uses RTMP (Real Time Messaging Protocol) • Streaming Audio/Video (FLV and MP3) • Recording Client Streams (FLV only) • Shared Objects • Live Stream Publishing • Remoting (AMF) • Multi-User Environments
  2. 2. What you need to get started • Eclipse 3.1 • J2EE • Flash IDE/Flash Develop/Flex • Red5 ( ) For more info on red5 release visit:
  3. 3. Building an application Application Directory red5 -webapps -Application Name -WEB-INF (contains configuration files & classes) -src -lib -classes web.xml red5-web.xml *Note: This structure will always be the same
  4. 4. Building an application cont… A closer look at the WEB-INF directories WEB-INF - src (contains all .java, .js, .py, .rb, files used to build your app.) - lib (contains all jar files required ) - classes (contains the compiled class files from the src directory) web.xml (this is the main configuration file for your app) globalScope contextConfigLocation locatorFactorySelector parentContextKey log4jConfigLocation webAppRootKey
  5. 5. Building an application cont… web.xml (view sample file) globalScope <context-param> <param-name>globalScope</param-name> <param-value>default</param-value> </context-param> contextConfigLocation Specifies the name(s) of handler configuration files for this application. Additionally, the handler configuration files specify the scope hierarchy for these classes. The path name given here can contain wildcards to load multiple files: <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/red5-*.xml</param-value> </context-param> locatorFactorySelector References the configuration file of the root application context which usually is “red5.xml”:: <context-param> <param-name>locatorFactorySelector</param-name> <param-value>red5.xml</param-value> </context-param>
  6. 6. Building an application cont… web.xml (view sample file) parentContextKey Name of the parent context, this usually is “default.context”:: <context-param> <param-name>parentContextKey</param-name> <param-value>default.context</param-value> </context-param> log4jConfigLocation Path to the configuration file for the logging subsystem:: <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/</param-value> </context-param> webAppRootKey Unique name for this application, should be the public name:: <context-param> <param-name>webAppRootKey</param-name> <param-value>/myapp</param-value> </context-param>
  7. 7. Building an application cont… red5-web.xml (view sample file) Handler configuration Every handler configuration file must contain at least three beans: 1. Context (The context bean has the reserved name web.context and is used to map paths to scopes, lookup services and handlers.) By default this bean is specified as: <bean id="web.context" class="org.red5.server.Context" autowire="byType" /> Every application can only have one context. However this context can be shared across multiple scopes. 2. Scopes Every application needs at least one scope that links the handler to the context and the server. The scopes can be used to build a tree where clients can connect to every node and share objects inside this scope (like shared objects or live streams). You can see the scopes as rooms or instances. The default scope usually has the name web.scope, but the name can be chosen arbitrarily.
  8. 8. Building and application cont… red5-web.xml (view sample file) 2. Scopes cont… The bean has the following properties: • server (This references the global server) • red5.server. parent (References the parent for this scope and usually is global.scope.) • context (The server context for this scope, use the web.context from above.) • handler (The handler for this scope (see below)) • contextPath (The path to use when connecting to this scope.) • virtualHosts (A comma separated list of hostnames or IP addresses this scope runs at.) Sample definition: <bean id="web.scope" class="org.red5.server.WebScope" init-method="register"> <property name="server" ref="red5.server" /> <property name="parent" ref="global.scope" /> <property name="context" ref="web.context" /> <property name="handler" ref="web.handler" /> <property name="contextPath" value="/myapp" /> <property name="virtualHosts" value="localhost," /> </bean>
  9. 9. Building an application cont… red5-web.xml (view sample file) 3. Handlers Every context needs a handler that implements the methods called when a client connects to the scope, leaves it and that contains additional methods that can be called by the client. Sample implementation: org.red5.server.adapter.ApplicationAdapter The bean for a scope handler is configured by: <bean id="web.handler“ class="“ singleton="true" /> The id attribute is referenced by the scope definition above. If you don't need any special server-side logic, you can use the default application handler provided by Red5: <bean id="web.handler“ class="org.red5.server.adapter.ApplicationAdapter“ singleton="true" />
  10. 10. Building an application cont… red5-web.xml (view sample file) 3. Handlers cont… Sample handler: package; import org.red5.server.adapter.ApplicationAdapter; public class Application extends ApplicationAdapter { public Double add(Double a, Double b){ return a + b; } } You can call this method using the following ActionScript: nc = new NetConnection(); nc.connect("rtmp://localhost/myapp"); nc.onResult = function(obj) { trace("The result is " + obj); }"add", nc, 1, 2);
  11. 11. red5 and AS3 How to build a red5 applications in AS3 (using the oflaDemo) ( view .fla ) /////////////////////////////////////////////////////////////////////////////////////////////////////// // Initialize Connection ////////////////////////////////////////////////////////////////////////////////////////////////////// nc = new NetConnection(); nc.client = this; nc.addEventListener(NetStatusEvent.NET_STATUS, onConnectHandler); nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onConnectErrorHandler); nc.connect("rtmp://localhost/oflaDemo"); remote_so = SharedObject.getRemote("chat"+roomNum, nc.uri, false); remote_so.addEventListener(SyncEvent.SYNC, onSyncHandler); remote_so.connect(nc); At this point you call your shared object „remote_so‟ anywhere in your AS to build functions around it, store data, retrieve date, etc..
  12. 12. red5 and AS3 How to build a red5 applications in AS3 (using the oflaDemo) ( view .fla ) Here’s an example of how we call our shared object: function loginUser():void { if ( == undefined) { userArray = new Array(); } else {; } userArray.push({name:userName}); remote_so.setProperty("userArray", userArray); remote_so.setProperty("chatText", chatInputWin.chatInput.text); remote_so.setDirty("userArray"); }
  13. 13. Coding custom functions in red5 Let‟s take a look at how to code a custom java function in red5 that we can call later in ActionScript. *Note: We will be modifying the oflaDemo app ( JAVA SIDE FIRST: add some code to onconnect and ondisconnect inside... public boolean appConnect(IConnection conn, Object[] params) add... conn.getClient().setAttribute("name",params[0]); inside... public void appDisconnect(IConnection conn) add... String name=(String)conn.getClient().getAttribute("name"); Iterator<IConnection> conns3 = appScope.getConnections(); while(conns3.hasNext()) { try{ //to notify clients IConnection; ((IServiceCapableConnection)conn1).invoke("userClose",new Object[]{name},this); } catch(Exception e){} }
  14. 14. Coding custom functions in red5 JAVA cont… Now we add some imports: import java.util.Iterator; import org.red5.server.api.service.IServiceCapableConnection; import org.red5.server.api.service.IPendingServiceCall; import org.red5.server.api.service.IPendingServiceCallback; Then we will add to our declaration: class Application extends ApplicationAdapter implements IPendingServiceCallback { Then we will need to add one method to our java: public void resultReceived(IPendingServiceCall call) {} Now we can compile it!
  15. 15. Coding custom functions in red5 JAVA cont… Now we can call our new function/method in ActionScript ("userClose“) function userClose(name:String):void{ //Do something } That’s all there is to it!
  16. 16. Resources • • • • • References: Daniel Rossi – Red5 Documentation PDF OSFlash Red5 Wiki OsFlash Red5 Mailer List