Tips for manipulating video BitmapData

Posted: 26/10/09

An image of Tips for manipulating video BitmapData

A fairly short post, but one I believe is useful to anyone messing around with BitmapData from a video. So a few thoughts:

Is your FMS application set-up to allow access to the stream?

Flash Media Server 3 and better allow client access to the raw video data stream.

Flash Media Server won't allow Flash Player access to the BitmapData of video streamed over RTMP/E by default. The runtime is written to treat this content as ‘secured' and therefore cannot be manipulated. The workaround is to modify the application.xml for your FMS app to explicitly permit this; but if you can't do this, you won't be able to draw from the data in the video.

For example:

<Application>

<Client>

<Access>

<VideoSampleAccess enabled="true">/</VideoSampleAccess>

</Access>

<Bandwidth>

<!-- Specified in bytes/sec -->

<ServerToClient>2500000</ServerToClient>

<!-- Specified in bytes/sec -->

<ClientToServer>2500000</ClientToServer>

</Bandwidth>

</Client>

</Application>

You can also adjust the property with server-side ActionScript, bearing in mind that any value set for this property in the application.xml will be overriden. If you're using a CDN, this also may already be configured for you; if not you can should be able to configure some server-side code yourself.

Does modifying VideoSampleAccess have any other side effects?

Not that I'm aware of. As far as I've seen, RTMPE content is still delivered as encrypted bytes to the client, the client decrypts and is then allowed to access the raw video data. All other security features, such as SWF Verification, continue to be enforced.

Do you need to load a policy file?

You need to load a policy file (see my post on creating crossdomain.xml files) for any video you're playing over HTTP (or progressive download), or the Flash Player will throw a security sandbox exception, which will just appear as a silent fail in a player that doesn't have a content debugger. You do this by setting the checkPolicyFile property to true on your NetStream instance (before you start loading!).

var myNetStream:NetStream = new NetStream( myNetConnection );

myNetStream.checkPolicyFile = true;

myNetStream.play( http://mydomain.com/video.flv );

Is the content buffered?

Even with all of the correct configurations, if you try to invoke a BitmapData.draw() method on a video that isn't loaded, or when the NetStream object is null, you can still end up with a security sandbox exception. This usually occurs when a drawing routine is being called at a fixed interval, and is the case for both streamed and HTTP solutions. An easy workaround is to either wait until the NetStream.Buffer.Full NetStatus is dispatched before instigating any draw routine, or simply wrap your BitmapData.draw() calls with a try…catch statement.

try

{

data = new BitmapData( _width, _height, true, 0xFFFFFF );

data.draw( _videoContent, new Matrix( 1, 0, 0, 1, - myBitmap.x, - myBitmap.y ), null, null, new Rectangle( 0, 0, _width, _height ) );

myBitmap.bitmapData = data;

}

catch( e:Error ){}

Keywords for this post: flash, video, fms, bitmapdata, rtmpe, security, stream, progressive, netstream, videoSampleAccess