facebook twitter hatena line email

Flash/red5/ビデオ通話

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

サーバサイドは何も描く必要は無いので、初期デモのoflaDemoを使います。

送信(models/SendCamera.as

package models.camera
{
  import flash.display.Sprite;
  import flash.display.StageScaleMode;
  import flash.display.StageAlign;
  import flash.events.Event;
  import flash.media.Camera;
  import flash.media.Video;
  import flash.net.NetStream;
  import models.CustomNetConnection;
  
  public class SendCamera extends Sprite
  {
    private var camera:Camera;
    private var video:Video;
    private var nc:CustomNetConnection;
    private var stream:NetStream;
    public function SendCamera()
    {
      init();
    }
    private function init():void
    {
      // 送信用
      camera = Camera.getCamera();
      if (camera != null) {
        _setupCamera();
      } else {
        trace("カメラがありません");
        return;
      }
      
      // カスタムコネクション
      nc = new CustomNetConnection();
      // 接続成功イベントハンドラ定義
      nc.addEventListener(Event.CONNECT, _connectSuccess);
    }
    /**
     * 接続成功イベントハンドラ
     */
    private function _connectSuccess(e:Event):void
    {
      // 送信用ビデオストリーム
      _sendVideo();
    }
    /**
     * 送信用カメラ設置
     */
    private function _setupCamera():void
    {
      video = new Video(camera.width,camera.height);
      video.attachCamera(camera);
      addChild(video);
    }
    /**
     * 送信用ビデオストリーム
     */
    private function _sendVideo():void
    {
      stream = new NetStream(nc);
      stream.attachCamera(camera);
      // 動画ID
      stream.publish("myvideo1");
    }
  }
}

受信(ReceiveCamera.as)

package models.camera
{
  import flash.display.Sprite;
  import flash.display.StageScaleMode;
  import flash.display.StageAlign;
  import flash.events.Event;
  import flash.media.Video;
  import flash.net.NetStream;
  import models.CustomNetConnection;
  
  public class ReceiveCamera extends Sprite
  {
    private var video:Video;
    private var nc:CustomNetConnection;
    private var stream:NetStream;
    public function ReceiveCamera()
    {
      init();
    }
    private function init():void
    {
      // カスタムコネクション
      nc = new CustomNetConnection();
      // 接続成功イベントハンドラ定義
      nc.addEventListener(Event.CONNECT, _connectSuccess);
    }
    /**
     * 接続成功イベントハンドラ
     */
    private function _connectSuccess(e:Event):void
    {
      // 受信用ビデオストリーム
      _receiveVideo();
    }
    /**
     * 受信用ビデオストリーム
     */
    private function _receiveVideo():void
    {
      video = new Video(480, 360);
      stream = new NetStream(nc);
      video.attachNetStream(stream);
      // 動画ID
      stream.play("myvideo1");
      addChild(video);
    }
  }
}

コネクション(models/CustomConnection.as

package models
{
  import flash.events.Event;
  import flash.net.NetConnection;
  import flash.events.NetStatusEvent;
  import flash.events.SecurityErrorEvent;
  import flash.events.AsyncErrorEvent;
  import flash.net.ObjectEncoding;
  /**
   * カスタムネットコネクション
   */
  public class CustomNetConnection extends NetConnection
  {
    public function CustomNetConnection() 
    {
      objectEncoding = ObjectEncoding.AMF0;
      addEventListener(NetStatusEvent.NET_STATUS,onNetStatus);
      addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSecurityError);
      connect("rtmp://localhost/oflaDemo");
    }
    private function onNetStatus(evt:NetStatusEvent):void
    {
      switch(evt.info.code) {
        case "NetConnection.Connect.Success":
          trace("接続成功");
          dispatchEvent(new Event(Event.CONNECT));
          break;
        case "NetConnection.Connect.Closed":
          trace("接続解除");
          break;
        case "NetConnection.Connect.Faild":
          trace("接続失敗");
          break;
        case "NetConnection.Connect.Rejected":
          trace("接続拒否");
          break;
        default:
          trace("evt.info.code");
      }
    }
    private function onSecurityError(evt:SecurityErrorEvent):void
    {
      trace("セキュリティーエラー");
    }
    private function onAsyncError(evt:AsyncErrorEvent):void
    {
      trace("同期エラー");
    }
  }
}

Main.as

package  
{
  import flash.display.Sprite;
  import flash.display.StageScaleMode;
  import flash.display.StageAlign;
  import models.camera.ReceiveCamera;
  import models.camera.SendCamera;
  
  /**
   * メイン
   */
  public class Main extends Sprite
  {
    public function Main() 
    {
      _init();
    }
    private function _init():void
    {
      stage.scaleMode = StageScaleMode.NO_SCALE;
      stage.align = StageAlign.TOP_LEFT;
      
      var _receiveCamera:ReceiveCamera = new ReceiveCamera();
      addChild(_receiveCamera);
      
      var _sendCamera:SendCamera = new SendCamera();
      _sendCamera.y = 300;
      
      addChild(_sendCamera);
    }
  }
}

参考URL

http://blog.hereticsintheworld.com/91.html