Red5 - PHUG Workshops

17

description

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.

Transcript of Red5 - PHUG Workshops

Page 1: Red5 - PHUG Workshops
Page 2: Red5 - PHUG Workshops

What is red5What is red5

• An Open Source Flash Media ServerAn Open Source Flash Media Server• Built on Java (Mina & Spring)Built on Java (Mina & Spring)• Uses RTMP (Real Time Messaging Protocol)Uses RTMP (Real Time Messaging Protocol)

• Streaming Audio/Video (FLV and MP3) Streaming Audio/Video (FLV and MP3) • Recording Client Streams (FLV only) Recording Client Streams (FLV only) • Shared Objects Shared Objects • Live Stream Publishing Live Stream Publishing • Remoting (AMF) Remoting (AMF) • Multi-User Environments Multi-User Environments

Page 3: Red5 - PHUG Workshops

What you need to get What you need to get startedstarted

• Eclipse 3.1Eclipse 3.1• J2EEJ2EE• Flash IDE/Flash Develop/FlexFlash IDE/Flash Develop/Flex

• Red5 (Red5 ( http://svn1.cvsdude.com/osflash/red5_ )

For more info on red5 release visit:http://www.red5world.com/downloads

Page 4: Red5 - PHUG Workshops

Building an applicationBuilding an application

Application DirectoryApplication Directory

red5red5-webapps-webapps

-Application Name-Application Name-WEB-INF (-WEB-INF (contains configuration files &

classes)-src-src-lib-lib-classes-classesweb.xmlweb.xmlred5-web.xmlred5-web.xmlred5-web.propertiesred5-web.properties

*Note:*Note: This structure will always be the same This structure will always be the same

Page 5: Red5 - PHUG Workshops

Building an application Building an application cont…cont…

A closer look at the WEB-INF directoriesA closer look at the WEB-INF directories

WEB-INFWEB-INF- src (contains all .java, .js, .py, .rb, files used to build your app.)- src (contains all .java, .js, .py, .rb, files used to build your app.)- lib (contains all jar files required )- lib (contains all jar files required )- classes (contains the compiled class files from the src directory)- classes (contains the compiled class files from the src directory) web.xml (this is the main configuration file for your app)web.xml (this is the main configuration file for your app)

globalScopeglobalScopecontextConfigLocationlocatorFactorySelectorlocatorFactorySelectorparentContextKeyparentContextKeylog4jConfigLocationlog4jConfigLocationwebAppRootKeywebAppRootKey

Page 6: Red5 - PHUG Workshops

Building an application Building an application cont…cont…

web.xml web.xml ((view sample fileview sample file))

globalScope<context-param><param-name>globalScope</param-name><param-value>default</param-value></context-param>

contextConfigLocationSpecifies 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>

locatorFactorySelectorReferences 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>

Page 7: Red5 - PHUG Workshops

Building an application Building an application cont…cont…

web.xmlweb.xml ( (view sample fileview sample file))

parentContextKeyName of the parent context, this usually is “default.context”::<context-param><param-name>parentContextKey</param-name><param-value>default.context</param-value></context-param>

log4jConfigLocationPath to the configuration file for the logging subsystem::<context-param><param-name>log4jConfigLocation</param-name><param-value>/WEB-INF/log4j.properties</param-value></context-param>

webAppRootKeyUnique name for this application, should be the public name::<context-param><param-name>webAppRootKey</param-name><param-value>/myapp</param-value></context-param>

Page 8: Red5 - PHUG Workshops

Building an application Building an application cont…cont…

red5-web.xml red5-web.xml ((view sample fileview sample file))

Handler configurationEvery 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. ScopesEvery 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.

Page 9: Red5 - PHUG Workshops

Building and application Building and application cont…cont…

red5-web.xml red5-web.xml ((view sample fileview 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, 127.0.0.1" /></bean>

Page 10: Red5 - PHUG Workshops

Building an application Building an application cont…cont…

red5-web.xml red5-web.xml (view sample file)(view sample file)

3. HandlersEvery 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="the.path.to.my.Application“ 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" />

Page 11: Red5 - PHUG Workshops

Building an application Building an application cont…cont…

red5-web.xml red5-web.xml (view sample file)(view sample file)

3.3. Handlers cont… Handlers cont…

Sample handler:package the.path.to.my;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);}nc.call("add", nc, 1, 2);

Page 12: Red5 - PHUG Workshops

red5 and AS3red5 and AS3How to build a red5 applications in AS3 (How to build a red5 applications in AS3 (using the using the

oflaDemooflaDemo))( view .fla )( view .fla )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Initialize Connection// 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..

Page 13: Red5 - PHUG Workshops

red5 and AS3red5 and AS3How to build a red5 applications in AS3 (How to build a red5 applications in AS3 (using the using the

oflaDemooflaDemo))( view .fla )( view .fla )

Here’s an example of how we call our shared object:

function loginUser():void {if (remote_so.data.userArray == undefined) {

userArray = new Array();} else {

userArray=remote_so.data.userArray;}userArray.push({name:userName});

remote_so.setProperty("userArray", userArray);remote_so.setProperty("chatText", chatInputWin.chatInput.text);remote_so.setDirty("userArray");

}

Page 14: Red5 - PHUG Workshops

Coding custom functions in Coding custom functions in red5red5

Let’s take a look at how to code a custom java function in red5 that we Let’s take a look at how to code a custom java function in red5 that we can call later in ActionScript. can call later in ActionScript. *Note: We will be modifying the oflaDemo app (Application.java)*Note: We will be modifying the oflaDemo app (Application.java)

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 conn1=conns3.next();((IServiceCapableConnection)conn1).invoke("userClose",new Object[]{name},this); }catch(Exception e){} }

Page 15: Red5 - PHUG Workshops

Coding custom functions in Coding custom functions in red5red5

JAVA cont…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!

Page 16: Red5 - PHUG Workshops

Coding custom functions in Coding custom functions in red5red5

JAVA cont…JAVA cont…

Now we can call our new function/method in ActionScriptNow we can call our new function/method in ActionScript ( ("userClose“)

function userClose(name:String):void{//Do something

}

That’s all there is to it!

Page 17: Red5 - PHUG Workshops

ResourcesResources

• http://www.red5world.comhttp://www.red5world.com• http://osflash.org/red5http://osflash.org/red5• http://www.nabble.com/Red5-f16328.html http://www.nabble.com/Red5-f16328.html • http://www.springframework.orghttp://www.springframework.org• http://mina.apache.orghttp://mina.apache.org

References:References:Daniel Rossi – Red5 Documentation PDFOSFlash Red5 WikiOsFlash Red5 Mailer List