actionscript-3p2pflash-media-server

How to stablish a Flash Peer-To-Peer Communication Over LAN (Without Cirrus/Stratus) with AIR


I'm trying to stream audio/video (I'm really only interested in Audio) in a LAN.

I've followed tutorials in internet which I've found very informative like the ones from Tom Krcha and thought would solve my problem. But until now I've not been successful in receiving the stream.

This is the code I'm using, can someone please point me out what am I missing?

package {

import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.media.Camera;
import flash.media.Microphone;
import flash.media.Video;
import flash.net.GroupSpecifier;
import flash.net.NetConnection;
import flash.net.NetGroup;
import flash.net.NetStream;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;

[SWF(width="640", height="920", frameRate="60")]
public class LoudApp extends Sprite {

    private var _interpreterStartButton:TextField = new TextField();
    private var _listenerStartButton:TextField = new TextField();
    private var _connectedLabel:TextField = new TextField();
    private var _userTextField:TextField = new TextField();

    private var _stream:NetStream;
    private var _netConnection:NetConnection;
    private var _netGroup:NetGroup;
    private var _isConnected:Boolean;
    private var _listenerStream:NetStream;
    private var _isListener:Boolean;
    private var _video:Video;

    public function LoudApp() {
        addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
    }

    private function connect():void{
        _netConnection = new NetConnection();
        _netConnection.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
        _netConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
        _netConnection.connect("rtmfp:");
    }

    private function securityErrorHandler(event:SecurityErrorEvent):void {
        trace("securityErrorHandler: " + event);
    }

    private function init(event:Event):void {
        _listenerStartButton.border = _interpreterStartButton.border = true;
        _listenerStartButton.backgroundColor = _interpreterStartButton.borderColor = 0;
        _listenerStartButton.autoSize = _interpreterStartButton.autoSize = TextFieldAutoSize.LEFT;
        _listenerStartButton.selectable = _interpreterStartButton.selectable = false;

        _connectedLabel = new TextField();
        _connectedLabel.y = 70;
        _connectedLabel.text = "not connected";

        _listenerStartButton.addEventListener(MouseEvent.CLICK, onButtonClicked, false, 0, true);
        _interpreterStartButton.addEventListener(MouseEvent.CLICK, onButtonClicked, false, 0, true);

        _interpreterStartButton.text = " Start\rTalking! ";
        _listenerStartButton.text = " Start\rListening! ";

        addChild(_interpreterStartButton);
        addChild(_listenerStartButton);
        addChild(_connectedLabel);

        _listenerStartButton.x = 120;
        var textFormat:TextFormat = new TextFormat(null, 30, 0x000000, true);
        _listenerStartButton.setTextFormat(textFormat);
        _interpreterStartButton.setTextFormat(textFormat);


//        Init the Video

        _video = new Video(stage.stageWidth, stage.stageHeight - 100);
        _video.y = 100;
        addChild(_video);

       connect();
    }

    private function onButtonClicked(event:MouseEvent):void {
        _isListener = event.target == _listenerStartButton;


        _listenerStartButton.removeEventListener(MouseEvent.CLICK, onButtonClicked);
        _interpreterStartButton.removeEventListener(MouseEvent.CLICK, onButtonClicked);

        removeChild(_listenerStartButton) && removeChild(_interpreterStartButton);

        /*_isConnected && */setupStream();

    }

    private function setupStream():void{
        var groupSpecifier:GroupSpecifier = new GroupSpecifier("en-GB");
        groupSpecifier.serverChannelEnabled = true;
        groupSpecifier.multicastEnabled = true;
        groupSpecifier.ipMulticastMemberUpdatesEnabled = true;
        groupSpecifier.addIPMulticastAddress("225.225.0.1:30303");

//        _netGroup = new NetGroup(_netConnection, groupSpecifier.groupspecWithAuthorizations());
//        _netGroup.addEventListener(NetStatusEvent.NET_STATUS, netStatus);

        _stream = new NetStream(_netConnection, groupSpecifier.groupspecWithAuthorizations());
        _stream.addEventListener(NetStatusEvent.NET_STATUS, netStatus);

        if(_isListener){
            _video.attachNetStream(_stream);
            _stream.receiveAudio(true);
//            _stream.receiveVideo(false);
            _stream.play('sound');
            return;
        }else{
            if(!Microphone.isSupported) return;

            _stream.attachAudio(Microphone.getMicrophone());
//            var camera:Camera = Camera.getCamera();
//            _stream.attachCamera(camera);
            _stream.publish("sound", "live");
//            _video.attachCamera(camera);

        }


    }

    private function netStatus(event:NetStatusEvent):void{
        switch(event.info.code){
            case "NetConnection.Connect.Success":
//                _isConnected = true;
//                _connectedLabel.text = "CONNECTED !";
                break;
            case "NetGroup.Connect.Success":
                _isConnected = true;
                _connectedLabel.text = "CONNECTED !";
                break;
            case "NetStream.Connect.Success":

                break;
            case "NetStream.Publish.Start":
                break;
        }
    }

    private function set listenerStream(value:NetStream):void {
        _listenerStream = value;
    }

}
}

Thank you in forward.


Solution

  • I was pointed out by Tom Krcha that some networks block multicast. And it was in deed the case.

    I got a fresh router to test the apps again and it all worked perfectly.