Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Red5 - PHUG Workshops

Setting up the Red5 environment, building sample applications and integrating with flash. We will look at how Red5 works within the flash IDE and build a sample chat application, video streaming, and multi-user environment.

  • Login to see the comments

Red5 - PHUG Workshops

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

×