Fixed whitespace and formatting in util.SimpleFlvWriter

This commit is contained in:
Nathan Dinsmore 2014-06-24 12:52:47 -04:00
parent 3ca6d42232
commit 9ae4614595

View file

@ -35,9 +35,9 @@
var myWriter:SimpleFlvWriter = SimpleFlvWriter.getInstance();
myWriter.createFile(myFile, 320,240, 30, 120);
myWriter.saveFrame( myBitmapData1 );
myWriter.saveFrame( myBitmapData2 );
myWriter.saveFrame( myBitmapData3 ); // etc.
myWriter.saveFrame(myBitmapData1);
myWriter.saveFrame(myBitmapData2);
myWriter.saveFrame(myBitmapData3); // etc.
*/
package util {
@ -61,19 +61,16 @@ public class SimpleFlvWriter {
private var bmp:BitmapData;
public static function getInstance():SimpleFlvWriter
{
if(SimpleFlvWriter._instance == null)
public static function getInstance():SimpleFlvWriter {
if(SimpleFlvWriter._instance == null) {
SimpleFlvWriter._instance = new SimpleFlvWriter(new SingletonEnforcer());
}
return SimpleFlvWriter._instance;
}
public function SimpleFlvWriter(singletonEnforcer:SingletonEnforcer)
{
}
public function SimpleFlvWriter(singletonEnforcer:SingletonEnforcer) {}
public function createFile(bytes:ByteArray, pWidth:int, pHeight:int, pFramesPerSecond:Number, pDurationInSeconds:Number=0):void
{
public function createFile(bytes:ByteArray, pWidth:int, pHeight:int, pFramesPerSecond:Number, pDurationInSeconds:Number=0):void {
/*
Parameters:
@ -93,23 +90,21 @@ public class SimpleFlvWriter {
fs = bytes;
// create header
fs.writeBytes( header() );
fs.writeBytes(header());
// create metadata tag
fs.writeUnsignedInt( previousTagSize );
fs.writeBytes( flvTagOnMetaData() );
fs.writeUnsignedInt(previousTagSize);
fs.writeBytes(flvTagOnMetaData());
}
public function saveFrame(pBitmapData:BitmapData):void
{
public function saveFrame(pBitmapData:BitmapData):void {
// bitmap dimensions should of course match parameters passed to createFile()
bmp = pBitmapData;
fs.writeUnsignedInt( previousTagSize );
fs.writeBytes( flvTagVideo() );
fs.writeUnsignedInt(previousTagSize);
fs.writeBytes(flvTagVideo());
}
private function header():ByteArray
{
private function header():ByteArray {
var ba:ByteArray = new ByteArray();
ba.writeByte(0x46) // 'F'
ba.writeByte(0x4C) // 'L'
@ -120,28 +115,26 @@ public class SimpleFlvWriter {
return ba;
}
private function flvTagVideo():ByteArray
{
private function flvTagVideo():ByteArray {
var tag:ByteArray = new ByteArray();
var dat:ByteArray = videoData();
var timeStamp:uint = uint(1000/frameRate * iteration++);
// tag 'header'
tag.writeByte( 0x09 ); // tagType = video
tag.writeByte(0x09); // tagType = video
writeUI24(tag, dat.length); // data size
writeUI24(tag, timeStamp); // timestamp in ms
tag.writeByte(0); // timestamp extended, not using ***
writeUI24(tag, 0); // streamID always 0
// videodata
tag.writeBytes( dat );
tag.writeBytes(dat);
previousTagSize = tag.length;
return tag;
}
private function videoData():ByteArray
{
private function videoData():ByteArray {
var v:ByteArray = new ByteArray;
// VIDEODATA 'header'
@ -163,55 +156,50 @@ public class SimpleFlvWriter {
var xRemainder:int = frameWidth % blockWidth;
if (xRemainder > 0) xMax += 1;
for (var y1:int = 0; y1 < yMax; y1++)
{
for (var x1:int = 0; x1 < xMax; x1++)
{
for (var y1:int = 0; y1 < yMax; y1++) {
for (var x1:int = 0; x1 < xMax; x1++) {
// create block
var block:ByteArray = new ByteArray();
var yLimit:int = blockHeight;
if (yRemainder > 0 && y1 + 1 == yMax) yLimit = yRemainder;
for (var y2:int = 0; y2 < yLimit; y2++)
{
for (var y2:int = 0; y2 < yLimit; y2++) {
var xLimit:int = blockWidth;
if (xRemainder > 0 && x1 + 1 == xMax) xLimit = xRemainder;
for (var x2:int = 0; x2 < xLimit; x2++)
{
for (var x2:int = 0; x2 < xLimit; x2++) {
var px:int = (x1 * blockWidth) + x2;
var py:int = frameHeight - ((y1 * blockHeight) + y2); // (flv's save from bottom to top)
var p:uint = bmp.getPixel(px, py);
block.writeByte( p & 0xff ); // blue
block.writeByte( p >> 8 & 0xff ); // green
block.writeByte( p >> 16 ); // red
block.writeByte(p & 0xff); // blue
block.writeByte(p >> 8 & 0xff); // green
block.writeByte(p >> 16); // red
}
}
block.compress();
writeUI16(v, block.length); // write block length (UI16)
v.writeBytes( block ); // write block
v.writeBytes(block); // write block
}
}
return v;
}
private function flvTagOnMetaData():ByteArray
{
private function flvTagOnMetaData():ByteArray {
var tag:ByteArray = new ByteArray();
var dat:ByteArray = metaData();
// tag 'header'
tag.writeByte( 18 ); // tagType = script data
tag.writeByte(18); // tagType = script data
writeUI24(tag, dat.length); // data size
writeUI24(tag, 0); // timestamp should be 0 for onMetaData tag
tag.writeByte(0); // timestamp extended
writeUI24(tag, 0); // streamID always 0
// data tag
tag.writeBytes( dat );
tag.writeBytes(dat);
previousTagSize = tag.length;
return tag;
@ -228,7 +216,7 @@ public class SimpleFlvWriter {
// ObjectName (type SCRIPTDATASTRING):
writeUI16(b, "onMetaData".length); // StringLength
b.writeUTFBytes( "onMetaData" ); // StringData
b.writeUTFBytes("onMetaData"); // StringData
// ObjectData (type SCRIPTDATAVALUE):
@ -282,8 +270,7 @@ public class SimpleFlvWriter {
return b;
}
private function writeUI24(stream:*, p:uint):void
{
private function writeUI24(stream:*, p:uint):void {
var byte1:int = p >> 16;
var byte2:int = p >> 8 & 0xff;
var byte3:int = p & 0xff;
@ -292,14 +279,12 @@ public class SimpleFlvWriter {
stream.writeByte(byte3);
}
private function writeUI16(stream:*, p:uint):void
{
stream.writeByte( p >> 8 )
stream.writeByte( p & 0xff );
private function writeUI16(stream:*, p:uint):void {
stream.writeByte(p >> 8);
stream.writeByte(p & 0xff);
}
private function writeUI4_12(stream:*, p1:uint, p2:uint):void
{
private function writeUI4_12(stream:*, p1:uint, p2:uint):void {
// writes a 4-bit value followed by a 12-bit value in two sequential bytes
var byte1a:int = p1 << 4;
@ -310,8 +295,8 @@ public class SimpleFlvWriter {
stream.writeByte(byte1);
stream.writeByte(byte2);
}
}
}
}}
class SingletonEnforcer {}