• How to protect your SWF files by loading them from Wowza Media Server

    The following is some code contributed by user Roger Littin in our forums. From his post:

    I was looking for a method to stop my SWFs and images from being stolen or decompiled and came across a bit of code for loading them as a byte array instead of using the normal Loader. As Wowza can send a byte array to the flash client I went about making a simple module that will load the swf and then send it to the flash client. The flash client uses the loadBytes method of the Loader class to load the byteArray.

    Using this method, the swf will only exist on the client machine as an object in the memory and not as a file in the browser cache.

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import com.wowza.wms.amf.*;
    import com.wowza.wms.client.*;
    import com.wowza.wms.module.*;
    import com.wowza.wms.request.*;
    public class ModuleSwfLoader extends ModuleBase {
    	public void loadSwf(IClient client, RequestFunction function, AMFDataList params) {
    		String swfName = "";
    		byte[] fileBytes = null;
    		if (getParam(params, PARAM1).getType() == AMFData.DATA_TYPE_STRING)
    			swfName  = params.getString(PARAM1);
    		try {
    			if (!swfName .isEmpty()) {
    				File swf = new File(swfName);
    				if (swf.exists() && swf.canRead() && swf.isFile() && swf.length() < Integer.MAX_VALUE) {
    						InputStream in = new FileInputStream(swf);
    						int length = (int) swf.length();
    						fileBytes = new byte[length];
    						int offset = 0;
    				        int numRead = 0;
    				        while (offset < fileBytes.length && (numRead=in.read(fileBytes, offset, fileBytes.length-offset)) >= 0) {
    				            offset += numRead;
    				        if (offset < fileBytes.length) {
    				            throw new IOException("Could not completely read file "+swf.getName());
    		} catch (FileNotFoundException e) {
    		} catch (IOException e) {
    		sendResult(client, params, AMFDataByteArray.wrap(fileBytes));
    On the Flash side, you still need a SWF to put into your page but it just needs to be a simple loader class that makes a net connection to the wowza media server and then loads the page.

    AS3 Code:
    package {
        import flash.display.Loader;
        import flash.display.MovieClip;
        import flash.display.Sprite;
        import flash.events.Event;
        import flash.events.NetStatusEvent;
        import flash.net.NetConnection;
        import flash.net.Responder;
        import flash.utils.ByteArray;
    	public class SwfLoader extends Sprite
    		public var mc:MovieClip;
    		private var nc:NetConnection = new NetConnection();
    		private var animationLoader:Loader = new Loader();
    		public function SwfLoader()
    			nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
    		private function onNetStatus(event:NetStatusEvent):void {
    			trace (event.info.code);
    			if (event.info.code == "NetConnection.Connect.Success") {
    				nc.call("loadSwf", new Responder(onLoadSwf), "myPrivateFlashApp.swf");
    		private function onLoadSwf(swfBytes:ByteArray):void {
    			animationLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete_Loader_Section);
                if(swfBytes.length != 0)
    		private function onComplete_Loader_Section(event:Event):void {
    			mc = MovieClip(event.target.content);
    			animationLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete_Loader_Section);
    The main Flash app time line, etc., can be accessed through the mc var.


    Some content that is loaded may fail if it requires access to the stage. An example is the simplevideostreaming.swf that comes with wowza. In order for this to work, you need to add an event listener to the swf that you are loading to check that it has been added to the stage before interacting with it.

    Modify the as3 code at the end of simplevideostreaming.fla to add this event listener.
    if (stage == null)
    	addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    function onAddedToStage(event:Event = null):void
    	// init code that requires stage
    	var h264Capable:Boolean = testVersion(9, 0, 115, 0);
    	playerVersion.text = (h264Capable?"H.264 Ready (":"No H.264 (")+Capabilities.version+")";
    	if (!h264Capable)
    		playerVersion.textColor = 0xee0000;
    	stage.align = "TL";
    	stage.scaleMode = "noScale";
    I am not sure about as2 code to do the same thing. Perhaps someone else can chip in.


    If you're having problems or want to discuss this article, post in our forum.