/**
 * This package provides classes to handle video in Processing. The API is compatible with the built-in video library of Processing. 
 * GSVideo uses the multimedia toolkit GStreamer (http://www.gstreamer.net/)  through the gstreamer-java bindings by Wayne Meissener:
 * http://code.google.com/p/gstreamer-java/ 
 * @author Andres Colubri
 * @version 0.4.5
 *
 * Copyright (c) 2008 Andres Colubri
 *
 * This source 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 2 of the License, or
 * (at your option) any later version.
 * 
 * This code 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.
 * 
 * A copy of the GNU General Public License is available on the World
 * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also
 * obtain it by writing to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package codeanticode.gsvideo;

import java.net.URL;

import processing.core.*;
import org.gstreamer.*;

/**
 * This class contains some basic functions used by the rest of the classes in this library.
 */
public class GSVideo
{	
    public static void init()
    {
        if (INSTANCES_COUNT == 0) 
        {
		    if (OS() == MACOS)
			{
				setOSXPath();
			}
		    if (OS() == WINDOWS)
			{
				setWinPath();
			}			

		    if (!gstreamerBinPath.equals(""))
		    {
                System.setProperty("jna.library.path", gstreamerBinPath);
		    }
		    
		    if (OS() == WINDOWS)
		    {
	    	    JnaLibLoaderChain loader = JnaLibLoaderChain.getInstance();
		    }
		    
            String[] args = { "" };
            Gst.init("GSVideo", args);

		    if (!gstreamerPluginsPath.equals(""))
            {
        	    Registry reg = Registry.getDefault();
        	    boolean res;
        	    res = reg.scanPath(gstreamerPluginsPath);
        	    if (!res) System.err.println("Cannot load GStreamer plugins from " + gstreamerPluginsPath);
            }
        	
        }
        INSTANCES_COUNT++;	    
    }
	
    public static int OS() 
    {
        String os = System.getProperty("os.name").toUpperCase();
        if (os.indexOf("LINUX") != -1) 
        {
            return LINUX;
        }
        else if (os.indexOf("MAC") != -1) 
        {
            return MACOS;
        }
        else if (os.indexOf("WINDOWS") != -1) 
        {
            return WINDOWS;
        }	  
        else 
        {
            return UNKNOWN;
        }
    }

	public static void setOSXPath()
	{
		gstreamerBinPath = "/opt/local/lib:/usr/local/lib";
		gstreamerPluginsPath = "";
    }	

	public static void setWinPath()
	{
	    LibraryPath libPath = new LibraryPath();
		
	    String path = libPath.get();
	    
		gstreamerBinPath = path + "\\gstreamer\\base";
		gstreamerPluginsPath = path + "\\gstreamer\\plugins";
    }	

    public static final int LINUX = 0;
    public static final int MACOS = 1;
    public static final int WINDOWS = 2;
    public static final int UNKNOWN = -1;
    static long INSTANCES_COUNT = 0;
	
    static String gstreamerBinPath = "";
    static String gstreamerPluginsPath = "";
 }
