Commit e59216dd authored by O'Reilly Media, Inc's avatar O'Reilly Media, Inc
Browse files

Initial commit

parents
package {
import flash.display.Sprite;
import flash.events.NetStatusEvent;
import flash.media.Camera;
import flash.media.H264Level;
import flash.media.H264Profile;
import flash.media.H264VideoStreamSettings;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.text.TextField;
import flash.text.TextFormat;
[SWF(width="600", height="500", backgroundColor="#CCCCCC")]
public class AVCEncode extends Sprite {
private var traceField:TextField;
private var video:Video;
private var camera:Camera;
private var connection:NetConnection;
private var stream:NetStream;
private var streamClient:Object;
public function AVCEncode() {
generateDisplayObjects();
performOperations();
}
protected function generateDisplayObjects():void {
video = new Video(stage.stageWidth, stage.stageHeight);
addChild(video);
var defaultFormat:TextFormat = new TextFormat();
defaultFormat.font = "Arial";
defaultFormat.size = 24;
defaultFormat.color = 0xFFFFFF;
traceField = new TextField();
traceField.backgroundColor = 0x000000;
traceField.alpha = 0.7;
traceField.autoSize = "left";
traceField.background = true;
traceField.defaultTextFormat = defaultFormat;
addChild(traceField);
}
protected function performOperations():void {
camera = Camera.getCamera();
camera.setMode(stage.stageWidth, stage.stageHeight, 30);
camera.setQuality(60000, 80);
video.attachCamera(camera);
streamClient = new Object();
streamClient.onBWDone = onBWDone;
connection = new NetConnection();
connection.client = streamClient;
connection.addEventListener(NetStatusEvent.NET_STATUS, monitorStatus);
connection.connect("rtmp://localhost/live");
}
protected function monitorStatus(e:NetStatusEvent):void {
traceField.appendText(e.info.code + "\n");
if(e.info.code == "NetConnection.Connect.Success"){
beginStreaming();
}else if(e.info.code == "NetStream.Publish.Start"){
traceField.appendText("\n" + e.info.description + "\n");
traceField.appendText("codec: " + stream.videoStreamSettings.codec);
}
}
protected function beginStreaming():void {
var h264Settings:H264VideoStreamSettings = new H264VideoStreamSettings();
h264Settings.setProfileLevel(H264Profile.BASELINE, H264Level.LEVEL_2);
stream = new NetStream(connection);
stream.addEventListener(NetStatusEvent.NET_STATUS, monitorStatus);
stream.videoStreamSettings = h264Settings;
stream.attachCamera(camera);
stream.publish("mp4:h264livestream.f4v", "live");
}
public function onBWDone():void {}
}
}
\ No newline at end of file
package {
import flash.display.Sprite;
import flash.events.NetStatusEvent;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.text.TextField;
import flash.text.TextFormat;
[SWF(width="600", height="500", backgroundColor="#CCCCCC")]
public class AVCPlayback extends Sprite {
private var traceField:TextField;
private var video:Video;
private var connection:NetConnection;
private var stream:NetStream;
private var streamClient:Object;
public function AVCPlayback() {
generateDisplayObjects();
performOperations();
}
protected function generateDisplayObjects():void {
video = new Video(stage.stageWidth, stage.stageHeight);
addChild(video);
var defaultFormat:TextFormat = new TextFormat();
defaultFormat.font = "Arial";
defaultFormat.size = 24;
defaultFormat.color = 0xFFFFFF;
traceField = new TextField();
traceField.backgroundColor = 0x000000;
traceField.alpha = 0.7;
traceField.autoSize = "left";
traceField.background = true;
traceField.defaultTextFormat = defaultFormat;
addChild(traceField);
}
protected function performOperations():void {
streamClient = new Object();
streamClient.onBWDone = onBWDone;
connection = new NetConnection();
connection.client = streamClient;
connection.addEventListener(NetStatusEvent.NET_STATUS, monitorStatus);
connection.connect("rtmp://localhost/live");
}
protected function monitorStatus(e:NetStatusEvent):void {
traceField.appendText(e.info.code + "\n");
if(e.info.code == "NetConnection.Connect.Success"){
beginStreaming();
}
}
protected function beginStreaming():void {
stream = new NetStream(connection);
stream.addEventListener(NetStatusEvent.NET_STATUS, monitorStatus);
stream.play("mp4:h264livestream.f4v");
video.attachNetStream(stream);
}
public function onBWDone():void {}
}
}
\ No newline at end of file
File added
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
[SWF(width="600", height="500", backgroundColor="#CCCCCC")]
public class CheckPlaying extends Sprite {
private var face:MovieClip;
private var traceField:TextField;
public function CheckPlaying() {
generateDisplayObjects();
}
protected function generateDisplayObjects():void {
face = new AngryFace() as MovieClip;
face.x = stage.stageWidth/2;
face.y = stage.stageHeight/2;
face.stop();
face.addEventListener(MouseEvent.CLICK, toggleFacePlaying);
addChild(face);
var defaultFormat:TextFormat = new TextFormat();
defaultFormat.font = "Arial";
defaultFormat.size = 26;
defaultFormat.color = 0xFFFFFF;
traceField = new TextField();
traceField.backgroundColor = 0x000000;
traceField.alpha = 0.7;
traceField.autoSize = "left";
traceField.background = true;
traceField.defaultTextFormat = defaultFormat;
addChild(traceField);
stage.addEventListener(Event.ENTER_FRAME, checkPlaying);
}
protected function toggleFacePlaying(e:MouseEvent):void {
if(face.isPlaying){
face.stop();
}else{
face.play();
}
}
protected function checkPlaying(e:Event):void {
traceField.text = "MovieClip is playing? => " + face.isPlaying;
}
}
}
\ No newline at end of file
package {
import flash.display.Sprite;
[SWF(width="600", height="500", backgroundColor="#CCCCCC")]
public class CubicBezierCurve extends Sprite {
private var drawingHolder:Sprite;
public function CubicBezierCurve() {
generateDisplayObjects();
}
protected function generateDisplayObjects():void {
drawingHolder = new Sprite();
drawingHolder.graphics.moveTo(20, stage.stageHeight-20);
drawingHolder.graphics.lineStyle(5,0x000000);
drawingHolder.graphics.cubicCurveTo(50, 50, stage.stageWidth-50, 50, stage.stageWidth-20, stage.stageHeight-20);
addChild(drawingHolder);
}
}
}
\ No newline at end of file
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.media.Camera;
import flash.media.CameraPosition;
import flash.media.Video;
[SWF(backgroundColor="#000000")]
public class FrontCamera extends Sprite {
private var video:Video;
private var camera:Camera;
public function FrontCamera() {
super();
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
if(Camera.isSupported){
setupCamera();
}
}
private function setupCamera():void {
for(var i:int=0; i<Camera.names.length; ++i) {
camera = Camera.getCamera(String(i));
if (camera.position == CameraPosition.FRONT){
camera.setMode(stage.stageWidth, stage.stageHeight, 30);
camera.setQuality(60000, 80);
setupVideo();
break;
}
}
}
private function setupVideo():void {
video = new Video(stage.stageWidth, stage.stageHeight);
video.attachCamera(camera);
addChild(video);
}
}
}
\ No newline at end of file
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.SampleDataEvent;
import flash.filters.BlurFilter;
import flash.geom.Point;
import flash.media.Microphone;
import flash.media.SoundCodec;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.ByteArray;
[SWF(width="600", height="500", backgroundColor="#CCCCCC")]
public class G711Telephony extends Sprite {
private const SPECTRUM_COLOR:uint = 0xda0000;
private var traceField:TextField;
private var microphone:Microphone;
private var audioBlur:BlurFilter;
private var audioBitmapData:BitmapData;
private var audioBitmap:Bitmap;
private var soundDisplay:Sprite;
private var soundActivity:Sprite;
public function G711Telephony() {
generateTextFields();
spectrumSetup();
performOperations();
}
protected function generateTextFields():void {
var defaultFormat:TextFormat = new TextFormat();
defaultFormat.font = "Arial";
defaultFormat.size = 24;
defaultFormat.color = 0xFFFFFF;
traceField = new TextField();
traceField.width = stage.stageWidth;
traceField.height = stage.stageHeight;
traceField.backgroundColor = 0x000000;
traceField.alpha = 0.7;
traceField.multiline = true;
traceField.wordWrap = true;
traceField.background = true;
traceField.defaultTextFormat = defaultFormat;
addChild(traceField);
}
protected function spectrumSetup():void {
audioBitmapData = new BitmapData(stage.stageWidth, stage.stageWidth, true, 0x000000);
audioBitmap = new Bitmap(audioBitmapData);
audioBlur = new BlurFilter(2, 12, 2);
soundActivity = new Sprite();
soundDisplay = new Sprite();
soundActivity.addChild(soundDisplay);
soundActivity.addChild(audioBitmap);
addChild(soundActivity);
}
protected function performOperations():void {
if(Microphone.isSupported){
microphone = Microphone.getMicrophone(0);
microphone.codec = SoundCodec.PCMU;
microphone.addEventListener(SampleDataEvent.SAMPLE_DATA, microphoneDataSample);
}
}
protected function microphoneDataSample(e:SampleDataEvent):void {
var soundBytes:ByteArray = new ByteArray();
soundBytes = e.data;
traceField.text = "Microphone: " + e.target.name + "\n\n";
traceField.appendText("activityLevel: " + e.target.activityLevel + "\n");
traceField.appendText("bytesAvailable: " + soundBytes.bytesAvailable + "\n");
traceField.appendText("codec: " + e.target.codec);
drawSpectrum(e.data);
}
protected function drawSpectrum(d:ByteArray):void {
var ba:ByteArray = new ByteArray();
ba = d;
var a:Number = 0;
var n:Number = 0;
var i:uint = 0;
soundDisplay.graphics.clear();
soundDisplay.graphics.lineStyle(2, SPECTRUM_COLOR, 0.8, false);
soundDisplay.graphics.moveTo(0, (n/2)+(stage.stageHeight/2+100));
for(i=0; i<=ba.bytesAvailable; i++) {
a = ba.readFloat();
n = a*soundActivity.height;
soundDisplay.graphics.lineTo(i*(stage.stageWidth/ba.bytesAvailable), (n/2)+(stage.stageHeight/2+100));
}
soundDisplay.graphics.endFill();
audioBitmapData.draw(soundDisplay);
audioBitmapData.applyFilter(audioBitmapData, audioBitmapData.rect, new Point(0,0), audioBlur);
}
}
}
\ No newline at end of file
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.system.System;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
[SWF(width="600", height="500", backgroundColor="#CCCCCC")]
public class GCAdvice extends Sprite {
private var traceField:TextField;
private var stateName:String;
private var levelTimer:Timer;
private var msg:String;
public function GCAdvice() {
generateDisplayObjects();
performOperations();
}
protected function generateDisplayObjects():void {
var defaultFormat:TextFormat = new TextFormat();
defaultFormat.font = "Arial";
defaultFormat.size = 26;
defaultFormat.color = 0xFFFFFF;
traceField = new TextField();
traceField.backgroundColor = 0x000000;
traceField.alpha = 0.7;
traceField.width = stage.stageWidth;
traceField.height = stage.stageHeight;
traceField.wordWrap = true;
traceField.multiline = true;
traceField.background = true;
traceField.defaultTextFormat = defaultFormat;
addChild(traceField);
}
protected function performOperations():void {
stateName = "GamePlaying";
msg = "\n\nUser is playing the game and we do not want to be disruptive by advising the GC at this time...";
msg += "\n\nIf we were to try and invoke the GC while a user is interacting with our game, and while a number of CPU intensive processes are in play, it can actually freeze the game temporarily.";
msg += "\n\nThis could cause the user to fail at his task and is visually disruptive.";
stage.addEventListener(Event.ENTER_FRAME, monitorGameState);
levelTimer = new Timer(5000, 1);
levelTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timeUp);
levelTimer.start();
}
protected function monitorGameState(e:Event):void {
traceField.text = "Current State: " + stateName;
traceField.appendText(msg);
}
protected function timeUp(e:TimerEvent):void {
stateName = "LevelComplete";
System.pauseForGCIfCollectionImminent();
msg = "\n\nSystem.pauseForGCIfCollectionImminent() invoked!";
msg += "\n\nWe do this between levels to avoid any strange behavior such as display glitches due to the GC running.";
msg += "\n\nBy advising the GC to fire during a time of little user interaction and game engine processes - we reduce the risk of disruption considerably.";
msg += "\n\nConvenient!";
}
}
}
\ No newline at end of file
package {
import starling.display.Sprite;
import starling.display.Quad;
import starling.events.Event;
import starling.utils.deg2rad;
public class Game extends Sprite {
private var quad:Quad;
public function Game() {
this.addEventListener(Event.ADDED_TO_STAGE, onStageReady);
}
protected function onStageReady(e:Event):void {
quad = new Quad(300, 300);
quad.pivotX = 150;
quad.pivotY = 150;
quad.setVertexColor(0, 0x00ff18);
quad.setVertexColor(1, 0x2dcb3b);
quad.setVertexColor(2, 0x00ff18);
quad.setVertexColor(3, 0x2dcb3b);
quad.x = (stage.stageWidth/2);
quad.y = (stage.stageHeight/2);
this.addChild(quad);
this.addEventListener(Event.ENTER_FRAME, renderLoop);
}
protected function renderLoop(e:Event):void {
quad.rotation += 0.02;
}
}
}
\ No newline at end of file
package {
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.events.ProgressEvent;
[SWF(width="600", height="500", backgroundColor="#CCCCCC")]
public class HighRes extends Sprite {
private var imageLoader:Loader;
private var traceField:TextField;
public function HighRes() {
generateDisplayObjects();
}
protected function generateDisplayObjects():void {
imageLoader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imageProgress);
imageLoader.load(new URLRequest("assets/highres.jpg"));
addChild(imageLoader);
var defaultFormat:TextFormat = new TextFormat();
defaultFormat.font = "Arial";
defaultFormat.size = 22;
defaultFormat.color = 0xFFFFFF;
traceField = new TextField();
traceField.backgroundColor = 0x000000;
traceField.alpha = 0.6;
traceField.autoSize = "left";
traceField.background = true;
traceField.defaultTextFormat = defaultFormat;
addChild(traceField);
}
protected function imageProgress(e:ProgressEvent):void {
traceField.appendText(e.bytesLoaded + " / " + e.bytesTotal + " bytes loaded...\n");
}
protected function imageLoaded(e:Event):void {
imageLoader.height = stage.stageHeight;
imageLoader.scaleX = imageLoader.scaleY;
traceField.text = "Loaded image is " + e.target.width + " x " + e.target.height + " pixels =>\nThat's " + e.target.width*e.target.height + " total pixels!\n\n" + traceField.text;
}
}
}
\ No newline at end of file
package {
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.text.TextFormat;
[SWF(width="600", height="500", backgroundColor="#CCCCCC")]
public class JPEGXR extends Sprite {
private var imageLoader:Loader;
private var traceField:TextField;
private const JXR_PATH:String = "assets/JPEG-XR.jxr";
public function JPEGXR() {
generateDisplayObjects();
}
protected function generateDisplayObjects():void {
imageLoader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imageProgress);
imageLoader.load(new URLRequest(JXR_PATH));
addChild(imageLoader);
var defaultFormat:TextFormat = new TextFormat();
defaultFormat.font = "Arial";
defaultFormat.size = 22;
defaultFormat.color = 0xFFFFFF;
traceField = new TextField();
traceField.backgroundColor = 0x000000;
traceField.alpha = 0.6;
traceField.autoSize = "left";
traceField.background = true;
traceField.defaultTextFormat = defaultFormat;
addChild(traceField);
}
protected function imageProgress(e:ProgressEvent):void {
traceField.appendText(e.bytesLoaded + " / " + e.bytesTotal + " bytes loaded...\n");
}
protected function imageLoaded(e:Event):void {
imageLoader.height = stage.stageHeight;
imageLoader.scaleX = imageLoader.scaleY;
traceField.text = JXR_PATH + " Loaded!\n\n" + traceField.text;
}