/***********************************************************************
 *
 *   Copyright (C) 2007 Free Software Foundation, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 *
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
**************************************************************************/

#include "ming_utils.h"

#include <stdlib.h>
#include <stdio.h>
#include <ming.h>

#define OUTPUT_VERSION 7
#define OUTPUT_FILENAME "easyvideo2.swf"

void add_stream(SWFMovie mo, int x, int y, int xpos, int ypos);

void
add_stream(SWFMovie mo, int x, int y, int xpos, int ypos)
{
	SWFVideoStream vs1;
	SWFVideoStream vs2;
	SWFDisplayItem it1;
	SWFDisplayItem it2;

	SWFAction ac;
	char action[2048];

	vs1 = newSWFVideoStream();
	vs2 = newSWFVideoStream();
	
	SWFVideoStream_setDimension(vs1, x, y);
	SWFVideoStream_setDimension(vs2, x, y);
	
	it1 = SWFMovie_add(mo, vs1);
	it2 = SWFMovie_add(mo, vs2);
	
	SWFDisplayItem_setName(it1, "video1");
	SWFDisplayItem_setName(it2, "video2");

	SWFDisplayItem_moveTo(it1, xpos, ypos);
	SWFDisplayItem_moveTo(it2, xpos, ypos + 100);

	ac = newSWFAction("if (url1.length || url2.length) {"
	        "stop();\
	        nc1 = new NetConnection();\
	        nc2 = new NetConnection();\
	        nc1.connect(null);\
	        nc2.connect(null);\
	        stream1 = new NetStream(nc1);\
	        stream2 = new NetStream(nc2);\
	        video1.attachVideo(stream1);\
            video2.attachVideo(stream2);\
	        stream1.setBufferTime(10);\
            stream2.setBufferTime(10);\
            stream1URL = _root.url1;\
            stream2URL = _root.url2;\
	        stream2.play(stream1URL);\
	        stream1.play(stream2URL);"
	        
	    "}");

	SWFMovie_add(mo, ac);
}

int
main(int argc, char** argv)
{
	SWFMovie mo;
    SWFAction traceAc;

	/*********************************************
	 *
	 * Initialization
	 *
	 *********************************************/

	Ming_init();
        Ming_useSWFVersion (OUTPUT_VERSION);
	Ming_setScale(20.0); /* so we talk pixels */
 
	mo = newSWFMovie();
        SWFMovie_setDimension (mo, 320, 500); 
        //SWFMovie_setBackground (mo, 80, 148, 80); 


	/* Add the stream */

    traceAc = newSWFAction("trace('pass the parameters url1 and url2 to use');"
                           "trace('url1: ' + url1);"
                           "trace('url2: ' + url2);"
                           "if (url1 == undefined && url2 == undefined) { stop(); }"
                           );

	SWFMovie_add(mo, traceAc);
	
	add_stream(mo, 280, 160, 20, 20);

	SWFMovie_nextFrame(mo); /* showFrame */

	puts("Saving " OUTPUT_FILENAME );

	SWFMovie_save(mo, OUTPUT_FILENAME);

	return 0;
}
