17

Red5workshop 090619073420-phpapp02

Embed Size (px)

Citation preview

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

What you need to get started

• Eclipse 3.1

• J2EE

• Flash IDE/Flash Develop/Flex

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

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

Building an application

Application Directory

red5

-webapps

-Application Name

-WEB-INF (contains configuration files & classes)

-src

-lib

-classes

web.xml

red5-web.xml

red5-web.properties

*Note: This structure will always be the same

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

Building an application cont…

web.xml (view 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>

Building an application cont…

web.xml (view 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>

Building an application cont…

red5-web.xml (view 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.

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, 127.0.0.1" /></bean>

Building an application cont…

red5-web.xml (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" />

Building an application cont…

red5-web.xml (view sample file)

3. 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);

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

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 (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");

}

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 (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){} }

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!

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!

Resources

• http://www.red5world.com

• http://osflash.org/red5

• http://www.nabble.com/Red5-f16328.html

• http://www.springframework.org

• http://mina.apache.org

References:

Daniel Rossi – Red5 Documentation PDF

OSFlash Red5 Wiki

OsFlash Red5 Mailer List