Connecting Red5 and Amf-php

Its been a long goal for me to connect Red5 to amfphp. You will see frequent needs of interacting with MySQL database from red5. Many people suggest using JDBC. Although thats  valid suggestion, its more of a headache for a non Java developer and the server administrator. Many people also suggest using flash to amfphp direct., but mind it thats the most incorrect way of doing things, when you want it secure.

Here is how you actually get Java to talk to php, the modern style :).  First anf foremost grab a copy of amfphp 1.2 from Sourceforge. Note: download amfphp 1.2,  as amfphp 1.9 is not compatible with red5′s amf decoding.

Step 1.  Setup the amfphp gateway at your server root. Like so:

http://localhost/amfphp

See video on setting up amfphp

Step 2. Download and install red5, then setup a new application using red5plugin in eclipse. (i wont discuss how to setup red5 application here. you can refer to external links for that. Mainly: Red5 developer series videos)

Step 3.  Create a new amfphp service class like this and save as Red5Service.php in services folder of amfphp.

<?php
class Red5Service
{

function  Red5Service()
{
$this->methodTable = array(
"login" => array(
"description" => "login user -> returns true/false",
"access" => "remote",
"arguments" => array ("username","password")
),
"logout" => array(
"description" => "logout user -> returns nothing",
"access" => "remote",
"arguments" => array ("username")
)  
);
}  

function login($username,$password)
{
return true;
}

function logout($username)
{
return;
}
}
?>

Step 4. In your red5 application, connect method  place a call to your amfphp service,  as shown below:

/** {@inheritDoc} */
@Override
public boolean connect(IConnection conn, IScope scope, Object[] params)
{
try
{
RemotingClient client = new RemotingClient("http://localhost/amfphp/gateway.php");
Object[] args = new Object[]{'rajdeep','xyz123'};
Object result = client.invokeMethod("Red5Service.login", args);

if(!result.equals(true)) return false; 
}
catch (IOException e)
{
return false;
}
return true;   
}

As you will see when a user connects to red5 application, the connect handler makes a connection to amfphp gateway and sends the parameters ‘rajdeep’ and ‘xyz123′ as arguments. the amf service can then do its own computations as you want it to and return a boolean expression – true/false at the end. If the java RemotingClient receives a true it lets the connect method accepts the connection else reject it. Also note the try catch block.  also since we dont know what network situations may prevail, so need to be sure that if red5 cannot connect to amfphp it will reject the client connection anyways.

Thats all  for the basics to make things hotter try experimenting with params sent to red5 from flash and the database stuff :)

Red5 to mysql without JDBC

In this tutorial we see how to connect to a mysql database, regardless of whether you have access to JDBC adapter on your Red5 running server. Definitely JDBC would be faster, but this method will get you across just fine in any Red5 project involving databases.

Since we wont be using Java way of connecting to mysql, i pick php as the delegate for that. so our communication will be like this:

JAVA -> PHP -> MYSQL

and

MYSQL -> PHP -> JAVA
(more…)

Handling application stop event in tomcat based red5 compilations

As you would know red5 is a media server package, which runs on top or other server cores  like Tomcat and Jetty.  There are few minor differences , hence in the way both compilations handle events and behaviors. The one we will be talking about here is specifically the “appStop” event. The “appStop” is suppose to fire off whenever the application stops/shuts down. The anomaly here is that on a tomcat based red5 compilations (which is the default one) the “appStop” does not fire up. This is because the is no straight forward  ”hook” / “point of entry” to determine when the server core stops. Hence we need to create a “Shutdown Hook” to handle the server/application shutdown by ourself.

The best way to do this is to create a internal / private class in your Application class source.  Sample:

/* ShutDown Hook for Red5 Application */
class ShutdownHook extends Thread
{
Application app;

public ShutdownHook(Application app)
{
this.app = app;
}

public void run()
{
// do something here
}
}

Next we relate it to our Application class by the “addShutdownHook” method. This method is not something for red5 only. Its the most common way to intercept and handle your java application shutdown. So in the Application class appStart() method we can add:

public boolean appStart(IScope app)
{

Runtime.getRuntime().addShutdownHook(new ShutdownHook(this));

return true;
}

We add it to the appStart() method to make sure it is called only once.
Note how we pass the reference to the Application class itself to the constructor “ShutdownHook”. Now by defining the properties as “protected” or “public” you can access then when the application/server is shutting down and do any cleanup jobs that you may have.

SwfMetaDataInjector – a flash SEO tool

swfmetadatainjector

I have always found it very difficult to understand and build java applications. Hence i always used to run away from the sound of it. But believe it or not now i find, a person who has worked well with flash/flex will find java to be very understandable. You can say flash added a expression to java over the few years.  So anyway to quench my thirst i decided to work on a java GUI application this week. And here it is –  ”SwfMetaDataInjector”. Flash has supported metadata even before Google  / Yahoo started indexing swfs wit their  special algorithms. This meta data is stored as a markup, in RDF (Resource Description Framework) format. To know more about this you can read a very intuitive article by Robert Reinhardt

SwfMetaDataInjector lets you write back meta information into swf files even without their fla sources. currently it supports swfs published for flash player 8 and below. i hope to do better with it over time. The application will require you to have the latest java runtime. I this works out forthcoming versions will have batch processing capabilities and better UI. Currently this application is at version 0.01 .

You can download the application here