/***************************************************************************
 *   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 *videosink;
  GstElement *audiosink;
  GstElement *subsink;

  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;
}

static gboolean do_seek (gpointer data)
{
	App *app = &s_app;
	gint64 pos;
	gfloat rate = 1.0;
	GstFormat fmt = GST_FORMAT_TIME;
	GstSeekFlags flags = 0;
	g_print ("[do_seek]");
	flags |= GST_SEEK_FLAG_FLUSH;
	flags |= GST_SEEK_FLAG_ACCURATE;
/* 	flags |= GST_SEEK_FLAG_KEY_UNIT;
 	flags |= GST_SEEK_FLAG_SEGMENT;*/
	gst_element_query_position((app->playbin), &fmt, &pos);

	pos += (10 * GST_SECOND);

	gst_element_seek ((app->playbin), rate, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
		GST_SEEK_TYPE_SET, pos,
		GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE);

	return FALSE;
}

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 ("%s is linked against GStreamer %d.%d.%d %s for dreambox 8000\n", argv[0], 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->videosink = gst_element_factory_make ("dvbvideosink", "videosink");
	app->audiosink = gst_element_factory_make ("dvbaudiosink", "audiosink");

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

	/* set to read from appsrc */
	g_object_set (app->playbin, "uri", argv[1], NULL);
	flags = 0x07; // ( == GST_PLAY_FLAG_VIDEO | GST_PLAY_FLAG_AUDIO | GST_PLAY_FLAG_TEXT )
	g_object_set (G_OBJECT (app->playbin), "flags", flags, NULL);
 	g_object_set (G_OBJECT (app->playbin), "audio-sink", app->audiosink, NULL);
 	g_object_set (G_OBJECT (app->playbin), "video-sink", app->videosink, NULL);

	/* go to playing and wait in a mainloop. */
// 	mtrace();
	gst_element_set_state (app->playbin, GST_STATE_PLAYING);

  	g_timeout_add (2000, do_seek, NULL);

	/* 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;
}

