/***************************************************************************
 *   Copyright (C) 2009 by Fraxinas   *
 *   andreas.frisch@multimedia-labs.de   *
 *                                                                         *
 *   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 2 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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <gst/gst.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <linux/dvb/dmx.h>
#include <linux/dvb/video.h>
#include <fcntl.h>
#include <string.h>
#include <glib.h>
// #include <mcheck.h>


typedef struct _App App;

struct _App
{
  GstElement *playbin;
  GstElement *audiobin;

  GMainLoop *loop;
};

App s_app;

static gboolean
bus_message (GstBus * bus, GstMessage * message, App * app)
{
  GST_DEBUG ("got message %s",
      gst_message_type_get_name (GST_MESSAGE_TYPE (message)));

  switch (GST_MESSAGE_TYPE (message)) {
    case GST_MESSAGE_ERROR:
    {
      GError *gerror;
      gchar *debug;

      gst_message_parse_error (message, &gerror, &debug);
      gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
      g_error_free (gerror);
      g_free (debug);

      g_main_loop_quit (app->loop);
      break;
    }
    case GST_MESSAGE_WARNING:
    {
      GError *gerror;
      gchar *debug;

      gst_message_parse_warning (message, &gerror, &debug);
      gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
      g_error_free (gerror);
      g_free (debug);

//       g_main_loop_quit (app->loop);
      break;
    }
    case GST_MESSAGE_EOS:
      g_message ("received EOS");
      g_main_loop_quit (app->loop);
      break;
    default:
      break;
  }
  return TRUE;
}

void gstGhostpadHasCAPS(GstPad *pad, GParamSpec * unused, gpointer user_data)
{
  	printf("gstGhostpadHasCAPS**************\n");
	GstCaps *caps;

	g_object_get (G_OBJECT (pad), "caps", &caps, NULL);
	
	printf("gstGhostpadHasCAPS:: AUDIO signal::caps = %s\n", gst_caps_to_string(caps));
}

int
main (int argc, char *argv[])
{
	App *app = &s_app;
	GstBus *bus;
		const gchar *nano_str;
		guint major, minor, micro, nano;
		int flags;
	gst_init (&argc, &argv);
	
	if (argc < 2) {
		g_print ("usage: %s <uri>\n", argv[0]);
		return -1;
	}

	gst_version (&major, &minor, &micro, &nano);
	
	if (nano == 1)
		nano_str = "(CVS)";
	else if (nano == 2)
		nano_str = "(Prerelease)";
	else
		nano_str = "";
	
	printf ("leakseek.c is linked against GStreamer %d.%d.%d %s for dreambox 8000\n",
		major, minor, micro, nano_str);
		
	/* create a mainloop to get messages */
	app->loop = g_main_loop_new (NULL, TRUE);
	
	app->playbin = gst_element_factory_make ("playbin2", NULL);
	g_assert (app->playbin);
	
	app->audiobin = gst_bin_new("universal_audiobin");

	GstElement *universal_capsfilter = gst_element_factory_make("capsfilter", "universal_capsfilter");
	
	GstCaps *universal_caps = gst_caps_new_any();
	
	g_object_set (G_OBJECT (universal_capsfilter), "caps", universal_caps, NULL);
	gst_caps_unref(universal_caps);
	
	GstElement *dvb_fakesink = gst_element_factory_make("fakesink", "dvbaudiosink_fakesink");
	
	gst_bin_add_many(GST_BIN(app->audiobin), universal_capsfilter, dvb_fakesink, NULL);
	gst_element_link(universal_capsfilter, dvb_fakesink);
	
	GstPad *universal_audiosinkpad = gst_ghost_pad_new("sink", gst_element_get_pad(universal_capsfilter, "sink"));

	gst_element_add_pad (app->audiobin, universal_audiosinkpad);
	g_object_set (G_OBJECT (app->playbin), "audio-sink", app->audiobin, NULL);

	bus = gst_pipeline_get_bus (GST_PIPELINE (app->playbin));
	
	/* add watch for messages */
	gst_bus_add_watch (bus, (GstBusFunc) bus_message, app);

	g_signal_connect (G_OBJECT (universal_audiosinkpad), "notify::caps", G_CALLBACK (gstGhostpadHasCAPS), 0);

	/* set to read from appsrc */
	g_object_set (app->playbin, "uri", argv[1], NULL);
	
	/* go to playing and wait in a mainloop. */
// 	mtrace();
	gst_element_set_state (app->playbin, GST_STATE_PLAYING);
	
	/* this mainloop is stopped when we receive an error or EOS */
	g_main_loop_run (app->loop);
	
	g_message ("stopping");
	
	gst_element_set_state (app->playbin, GST_STATE_NULL);
	
	gst_object_unref (bus);
	g_main_loop_unref (app->loop);
// 	muntrace();
	return 0;
}

