Search code examples
apache-flexstreamsoundcloud-stratusadobe-cirrus

Can't read a simple Adobe Cirrus/Stratus incoming stream with Flex


I'm trying to put in place a Flex application that allows a user to send a video stream and let the others to display it.

I created a sender page (below).The code is very simple and works perfectly:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.core.UIComponent;
            import mx.events.FlexEvent;

            // Network properties
            private var connection:NetConnection;
            private var outStream:NetStream;
            private var RTMFP_CODE:String = "A_CODE";

            // Device properties    
            private var camera:Camera;
            private var microphone:Microphone;

            // Video properties
            private var outVideo:Video;

            // Flex components
            private var outVideoWrapper:UIComponent;

            protected function button1_clickHandler(event:MouseEvent):void
            {
                connection = new NetConnection();    
                connection.connect("rtmfp://p2p.rtmfp.net/" + this.RTMFP_CODE);
                connection.addEventListener(NetStatusEvent.NET_STATUS, onConnected);
            }

            private function onConnected(event:NetStatusEvent):void{
                if (event.info.code == "NetConnection.Connect.Success"){
                    setupVideo();
                }
            }

            private function setupVideo():void{
                // Setup outgoing devices
                camera = Camera.getCamera();
                microphone = Microphone.getMicrophone();

                // Setup outgoing stream

                outStream = new NetStream(connection);
                outStream.attachCamera(camera);
                outStream.attachAudio(microphone);
                outStream.publish("flex_rocks");

                // Setup outgoing video and attach outgoing devices
                outVideo = new Video();
                outVideo.attachCamera(camera);

                // Wrap the video object
                outVideoWrapper = new UIComponent;
                outVideoWrapper.addChild(outVideo);
                addElement(outVideoWrapper);
            }
        ]]>
    </fx:Script>
    <s:Button x="885" y="0" label="Send video" click="button1_clickHandler(event)"/>
</s:Application>

Then comes the receiver page code (below):

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.core.UIComponent;
            import mx.events.FlexEvent;

            // Network properties
            private var connection:NetConnection;
            private var inStream:NetStream;
            private var RTMFP_CODE:String = "A_CODE";

            // Device properties
            private var camera:Camera;
            private var microphone:Microphone;

            // Video properties
            private var inVideo:Video;

            // Flex components

            // private var inVideoWrapper:UIComponent;

            protected function button1_clickHandler(event:MouseEvent):void
            {
                connection = new NetConnection();
                connection.connect("rtmfp://p2p.rtmfp.net/" + this.RTMFP_CODE);
                connection.addEventListener(NetStatusEvent.NET_STATUS, onConnected);
            }

            private function onConnected(event:NetStatusEvent):void{
                Alert.show(event.info.code);
                if (event.info.code == "NetConnection.Connect.Success"){
                    setupVideo();
                }
            }

            private function setupVideo():void{
                // Setup outgoing devices

                camera = Camera.getCamera();
                microphone = Microphone.getMicrophone();

                // Setup incoming stream
                inStream = new NetStream(connection);
                inStream.play("flex_rocks");

                // Setup incoming video and attach incoming stream
                inVideo = new Video();
                inVideo.attachNetStream(inStream);

                // Wrap the video object

                // inVideoWrapper = new UIComponent();   
                inVideoWrapper.addChild(inVideo);
                addElement(inVideoWrapper);
            }
        ]]>

    </fx:Script>
    <s:Button x="885" y="0" label="Receive a stream" click="button1_clickHandler(event)"/>
    <mx:UIComponent id="inVideoWrapper" x="0" y="0" width="500" height="500">
    </mx:UIComponent>    
</s:Application>

I don't know why in the receiver page I can't display the incoming stream? I put a simple alert to show the returned connection code, it shows "Success".

Am i doing something wrong?

Regards.

(PS: I'm using Flex Builder 4 under Windows Seven 64 bits).


Solution

  • If you're using Cirrus for p2p, just having both connect to Cirrus won't help you at all. What needs to happen is that one connects to Cirrus, gets the ID it needs from the service using event.nearID in your onConnected handler. You also need to specify the Stream to be able to directly connect between 2 clients like this:

    outStream = new NetStream(connection, NetStream.DIRECT_CONNECTIONS);

    And this is where things gets interesting, you now need to transfer that ID to the receiver. Cirrus does not do that for you. You need to have a service to send it over or input it manually. Say you do input it manually, your receiving end then needs to connect using this:

    inStream = new NetStream(connection, cirrusID);

    And that will in turn connect both clients directly to one another.