shithub: h264bsd

ref: 350337b6b4cdb22fb93aff5c58e9e24c2416d1d0
dir: /flex/test/h264test.mxml/

View raw version
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx">
    <fx:Script>
        <![CDATA[
            import h264bsd.CroppingInfo;
            import h264bsd.Decoder;
            
            import mx.core.UIComponent;
            
            private function loadFile():void {
                var fileRef:FileReference = new FileReference();
                fileRef.addEventListener(Event.SELECT, onFileSelected);
                fileRef.browse();
                
                function onFileSelected(e:Event):void {
                    e.target.addEventListener(Event.COMPLETE, onFileComplete);
                    e.target.load();
                }
                
                function onFileComplete(e:Event):void {
                    loopH264Data(e.target.data as ByteArray);
                }
            }
            
            private function loopH264Data(data:ByteArray):void {
                if(data == null) return;
                
                var decoder:Decoder = new Decoder();
                
                var renderTarget:Bitmap = new Bitmap();
                var renderComponent:UIComponent = new UIComponent();
                renderComponent.addChild(renderTarget);
                main.addElement(renderComponent);
                
                decoder.queueInput(data);
                loopBody();
                
                function loopBody():void {
                    var result:int = decoder.decode();
                    
                    switch(result)
                    {
                        case Decoder.NO_INPUT:
                            data.position = 0;
                            decoder.queueInput(data);
                            setTimeout(loopBody, 0);
                            break;
                        case Decoder.HDRS_RDY:
                            var cinfo:CroppingInfo = decoder.getCroppingInfo();
                            renderTarget.bitmapData = new BitmapData(cinfo.width, cinfo.height);
                            setTimeout(loopBody, 0);
                            break;
                        case Decoder.PIC_RDY:
                            decoder.drawNextOutputPicture(renderTarget.bitmapData);
                            ++frameCount;
                            setTimeout(loopBody, 0);
                            break;
                        case Decoder.RDY:
                            loopBody();
                            break;
                        default:
                            throw new Error("An error has occured in the decoder");
                            break;
                    }
                }

                var lastUpdateTime:int = getTimer();
                var lastFrameCount:int = 0;
                var frameCount:int = 0;
                setInterval(updateStatus, 1000);

                function updateStatus():void {
                    var updateTime:int = getTimer();
                    var interval:int = updateTime - lastUpdateTime;
                    lastUpdateTime = updateTime;

                    var frameCountSinceLast:int = frameCount - lastFrameCount;
                    lastFrameCount = frameCount;

                    var frameRate:Number = frameCountSinceLast * 1000 / interval;

                    statusLabel.text = "FPS: " + frameRate.toFixed(2);
                }
            }
        ]]>
    </fx:Script>
    
    <s:VGroup id="main">
        <s:HGroup id="statusPanel" verticalAlign="middle">
            <s:Button label="Load File" click="loadFile()"/>
            <s:Label id="statusLabel"/>
        </s:HGroup>
    </s:VGroup>
</s:Application>