FP_11 H.264SoftwereEncord + StageVideo SimpleExample
スライダー操作で拡大。
拡大時にマウスを動かすとパンします。
※ カメラ映像のストリーミングにRTMFPを使用しているので、複数台のPCで同時に開くか、同一マシン上だとブラウザの複数のウィンドウか複数のタブで開かないと映像が出ませんのでご注意ください。(同一マシンで開くときは音量に注意してください。音量が大きいとハウります…)
/**
* Copyright mousepancyo ( http://wonderfl.net/user/mousepancyo )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/s6bC
*/
package
{
import com.bit101.components.HSlider;
import com.bit101.components.Label;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.events.StageVideoAvailabilityEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.media.Camera;
import flash.media.H264Level;
import flash.media.H264Profile;
import flash.media.H264VideoStreamSettings;
import flash.media.Microphone;
import flash.media.SoundCodec;
import flash.media.StageVideo;
import flash.media.StageVideoAvailability;
import flash.media.Video;
import flash.net.GroupSpecifier;
import flash.net.NetConnection;
import flash.net.NetGroup;
import flash.net.NetStream;
[SWF(width = "465", height = "465", backgroundColor = "0xFFFFFF", frameRate = "30")]
public class Main extends Sprite
{
private const GROUP_NAME:String = "cam_test";
private var _nc:NetConnection;
private var _ns:NetStream;
private var _rns:NetStream;
private var _ng:NetGroup;
private var _gs:GroupSpecifier;
private var _cam:Camera;
private var _mic:Microphone;
private var _video:Video;
private var _stagevideo:StageVideo;
private var _isAvailable:Boolean;
private var _streamName:String = "MultiCastStream";
private var _slider:HSlider;
private var _mx:Number = 0;
private var _my:Number = 0;
public function Main()
{
Wonderfl.disable_capture();
stage.addEventListener(StageVideoAvailabilityEvent.STAGE_VIDEO_AVAILABILITY, videoSetup);
}
// ------------- Setup ------------------------------------------/
private function setup():void
{
// Cam
_cam = Camera.getCamera();
_cam.setMode(720, 480, 30);
_cam.setQuality(0, 90);
// Mic
_mic = Microphone.getMicrophone();
_mic.codec = SoundCodec.PCMU; // G.711SoundCodec
_mic.setLoopBack();
//
doConnect();
}
private function videoSetup(e:StageVideoAvailabilityEvent):void
{
_isAvailable = e.availability == StageVideoAvailability.AVAILABLE;
var xp:int = (stage.stageWidth *.5 - 160) | 0;
var yp:int = (stage.stageHeight * .5 - 150) | 0;
//
if(_isAvailable)
{
_stagevideo = stage.stageVideos[0];
_stagevideo.viewPort = new Rectangle(xp, yp, 320, 240);
//
addEventListener(Event.ENTER_FRAME, videoPan);
}
else
{
_video = new Video(320, 240);
_video.x = xp;
_video.y = yp;
addChild(_video);
}
// Slider
_slider = new HSlider(this, 83, yp + 290, zoomUpdate);
_slider.scaleX = 3;
var label:Label = new Label(this, 200, yp + 270, "ZOOM SLIDER");
var min:Label = new Label(this, 38, yp + 285, "MIN (X1)");
var max:Label = new Label(this, 390, yp + 285, "MAX (X16)");
//
setup();
}
// ------------- Connect on RTMFP ------------------------------------------/
private function doConnect():void
{
_nc = new NetConnection();
_nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
_nc.connect("rtmfp:");
}
// ------------- netStatusHandler ------------------------------------------/
private function netStatusHandler(e:NetStatusEvent):void
{
trace(e.target, e.info.code);
switch(e.info.code){
case "NetConnection.Connect.Success":
addNetGroup();
break;
case "NetGroup.Connect.Success":
broadCast();
break;
case "NetStream.Connect.Success":
break;
case "NetStream.Publish.Start":
watchStream();
break;
case "NetGroup.Posting.Notify":
break;
}
}
// ------------- Added NetGroup ------------------------------------------/
private function addNetGroup():void
{
_gs = new GroupSpecifier(GROUP_NAME);
_gs.postingEnabled = true;
_gs.ipMulticastMemberUpdatesEnabled = true;
_gs.multicastEnabled = true;
_gs.addIPMulticastAddress("225.225.0.1:30303");
// NetGroup
_ng = new NetGroup(_nc, _gs.groupspecWithAuthorizations());
_ng.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
}
// ------------- BroadCast ------------------------------------------/
private function broadCast():void
{
_ns = new NetStream(_nc, _gs.groupspecWithAuthorizations());
_ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
// H.264VideoCodec
var h264Settings:H264VideoStreamSettings = new H264VideoStreamSettings();
h264Settings.setProfileLevel(H264Profile.MAIN, H264Level.LEVEL_3);
_ns.videoStreamSettings = h264Settings;
// StreamStart
_ns.attachCamera(_cam);
_ns.attachAudio(_mic);
_ns.publish(_streamName);
}
// ------------- Watch ------------------------------------------/
private function watchStream():void{
_rns = new NetStream(_nc, _gs.groupspecWithAuthorizations());
if(_isAvailable) _stagevideo.attachNetStream(_rns);
else _video.attachNetStream(_rns);
_rns.play(_streamName);
}
// ------------- Zoom ------------------------------------------/
private function zoomUpdate(e:Event):void
{
var val:Number = _slider.value;
trace(val);
//
var n:Number = 15 * (val * .01) + 1;
if(n < 1) n = 1;
if(n > 16) n = 16;
_stagevideo.zoom = new Point(n, n);
}
// ------------- VideoPan ------------------------------------------/
private function videoPan(e:Event):void
{
if(_stagevideo.zoom.x <= 1) return;
//
var px:Number = 0;
var py:Number = 0;
_mx += (mouseX - _mx) * .07;
_my += (mouseY - _my) * .07;
px = (_mx - 320 * .5) / (320 * .5);
py = (_my - 240 * .5) / (240 * .5);
if(px > 1) px = 1; if(px < -1) px = -1;
if(py > 1) py = 1; if(py < -1) py = -1;
//
_stagevideo.pan = new Point(px, py);
}
}
}