Scriptico

WebORB and Speex codec

Few days ago Midnight Coders released a new version (4.4.0.0) of WebORB for .NET. Although the latest version contains a number of new features, I would like to demonstrate something that was not included in the WebORB release history – an ability to intercept audio or video stream data from the client on the server side for some additional processing.

To demonstrate the feature I created a sample application based on the simple use-case: client-side gets audio data from the microphone, encodes it with the SPEEX codec, and sends it to the WebORB RTMP server. On the server-side I intercept the received data, decode it, and save into a .wav file.

Client side
The client code is pretty simple, and a code snippet is shown below :

			...
 private var microphone:Microphone;
                        ...
  private function init():void
  {
    microphone = Microphone.getMicrophone();
    microphone.addEventListener(StatusEvent.STATUS, onMicStatus);
    microphone.codec = SoundCodec.SPEEX;
    microphone.encodeQuality = 10;
    microphone.rate = 44;
    microphone.framesPerPacket = 2;
    microphone.gain = 50;
    microphone.setUseEchoSuppression(true);

    connection = new NetConnection();
    connection.addEventListener(NetStatusEvent.NET_STATUS, onConnStatus);
    connection.client = this;
  }
                        ....
 private function doRecord():void
 {
    stream = new NetStream(connection);
    stream.addEventListener( IOErrorEvent.IO_ERROR, handleIOError );

    stream.attachAudio(microphone);
    stream.publish("StreamForRecognition", "live");
    timer.start();
 }

Please note that the recommended version of Flash player for the Speex support is 10.0.22.87 or newer.

Server side
To implement my use-case on the server side I need the following interfaces from the WebORB Messaging API:

Weborb.Messaging.Api.Stream.IStreamListener
Weborb.Messaging.Api.Stream.IStreamAwareScopeHandler

The first one (Weborb.Messaging.Api.Stream.IStreamListener) has only one method void packetReceived(IBroadcastStream stream, IStreamPacket packet), and gives me access to a packet received from the client. So, I created a custom listener to deal with received packets:

using Weborb.Messaging.Api.Event;

public class MyListener : IStreamListener
{
  public  void packetReceived(IBroadcastStream stream, IStreamPacket packet)
  {
    if (packet is AudioData)
    {
      //process audio data
    }
  }
}

The second interface (Weborb.Messaging.Api.Stream.IStreamAwareScopeHandler) provides access to the client stream. I implemented the interface in my application handler and it lets me to register the class shown above (MyListener):

....
public class AppHandler : ApplicationAdapter, IStreamAwareScopeHandler
{
  private MyListener listener;

  public void streamBroadcastStart(IBroadcastStream stream)
  {
    listener = new MyListener();
    ClientBroadcastStream myStream = (ClientBroadcastStream)stream;
    myStream.addStreamListener(listener);
  }

  public void streamBroadcastClose(IBroadcastStream stream)
  {
       // do something...
  }
....

The streamBroadcastStart method will be fired when the client starts streaming, and the instance of MyListener will be added to the stream. For every received packet from the client the packetReceived method will be called in my listener. In the listener I implemented a mechanism to decode and save received data (see attached source projects at the end of the article).

That’s it! At the bottom of the article you will find the complete projects.

Resources

http://www.themidnightcoders.com/products/weborb-for-net/download.html

Category: Examples, WebORB (.NET)

Tagged: , , ,

One Response

  1. […] in version 4.4 of WebORB for .NET. To see a concrete example for this feature, please see the WebORB and Speex codec article. The article includes the information about the APIs as well as a complete project for […]

Leave a Reply

ERROR: si-captcha.php plugin: GD image support not detected in PHP!

Contact your web host and ask them to enable GD image support for PHP.

ERROR: si-captcha.php plugin: imagepng function not detected in PHP!

Contact your web host and ask them to enable imagepng for PHP.