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 🙂

Passing parameters to Red5 application with OSMF

Recently working on a Flex project i had the need to implement a video player in my Flex application. Although my needs did not involve media streaming, rather only playing live streams, i still needed a  industry standard code to my solution. Thats where OSMF (Strobe)  came handy. Tracking OSMF for a few months, seems like we finally have a usable version – OSMF 1.0  (0.93).

The second hurdle was passing parameters to RTMP application. As the traditional Netconnection class allows connection params in the constructor, i was unable  to find a example on the web involving OSMF and passing parameters to RTMP applications. Finally after some struggle , god’s grace 🙂 i think i nailed it. Here is the code snippet showing you how to connect to a RTMP application and passing parameters when connecting.

Note: Make sure you have latest OSMF revision from trunk added to your project classpath.


<?xml version="1.0" encoding="utf-8"?>
<mx:Application applicationComplete="application1_applicationCompleteHandler(event)" xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="1024" minHeight="768">
<mx:Script>
<![CDATA[
import mx.events.FlexEvent;

import org.osmf.containers.MediaContainer;
import org.osmf.elements.VideoElement;
import org.osmf.events.MediaPlayerStateChangeEvent;
import org.osmf.media.MediaPlayer;
import org.osmf.net.NetLoader;
import org.osmf.net.StreamType;
import org.osmf.net.StreamingURLResource;

private static const REMOTE_STREAMING:String = "rtmp://localhost/RtmpAuthenticate/DarkKnight.flv";
private var mediaElement:VideoElement;
private var mediaPlayer:MediaPlayer;
private var container:MediaContainer;



protected function application1_applicationCompleteHandler(event:FlexEvent):void
{
var vector:Vector.<Object> = new Vector.<Object>();
vector[0] = "rajdeep";
vector[1] = "xyz123";

// TODO Auto-generated method stub

mediaElement = new VideoElement(new StreamingURLResource(REMOTE_STREAMING,StreamType.RECORDED,NaN,NaN,vector),new NetLoader);
container = new MediaContainer();
container.addMediaElement( mediaElement );

// Flex requires a DisplayObject to be wrapped in Flex Framework UI Abstraction
videoParent.addChild( container );

// MediaPlayer is a virtual controller. Assign it a media element and it will autoplay the media by default.
mediaPlayer = new MediaPlayer(mediaElement);
mediaPlayer.addEventListener(MediaPlayerStateChangeEvent.
MEDIA_PLAYER_STATE_CHANGE,onMediaPlayerState);

container.width = 320;
container.height = 240;
}

private function onMediaPlayerState(me:MediaPlayerStateChangeEvent):void
{
trace(me.state);
}

]]>
</mx:Script>
<mx:UIComponent x="144" y="110" width="409" height="325" id="videoParent" />
</mx:Application>

Red5 0.9 out and shining

Red5-0.9-release

The new Red5 0.9 is out. I guess i missed it on release date  🙁 . Nevertheless..  the Red5 team   seems to be doing a wonderful job as they near to the first stable version (1.0) of Red5.  And since its the only good to use and friendly Open source RTMP server out there, its no wonder that everyone have their eye on its progress.  Great job Red5 Team !!

HTTP vs RTMP streaming – a visual comparison


For those of you , who have been wondering about “whats the fuss about RTMP and streaming”, here is a little eye opener. Though many think of saving cash by having to invest less on storing flv videos in your web server accounts, the point to create a good user experience is beaten. The advantage of RTMP streams over the casual HTTP based progressive downloading is far too realistic to ignore. Below are two examples that feature HTTP vs RTMP streams playing side by side to let you understand the difference. (more…)