/***************************************************************************
 *   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.             *
 ***************************************************************************/

// gcc -Wall -g `pkg-config gstreamer-0.10 --cflags --libs` bug642274_universal_audiobin_ver2.c -o bug642274_universal_audiobin_ver2

#include <gst/gst.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <glib.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, App *app)
{
  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));

  GstPad *dvbaudiosinkpad = gst_element_get_static_pad(gst_bin_get_by_name(GST_BIN(app->audiobin), "universal_dvbaudiosink"), "sink");
  GstCaps *dvbaudiosinkcaps;
  dvbaudiosinkcaps = gst_caps_copy(gst_pad_get_pad_template_caps (dvbaudiosinkpad));

  int ret = gst_caps_is_subset(caps, dvbaudiosinkcaps);

  printf("gst_caps_is_subset of dvbaudiosink's caps= %i\n", ret);

  if ( ret == 1 )
  {
	  int r = gst_ghost_pad_set_target (GST_GHOST_PAD(pad), dvbaudiosinkpad);
	  printf("setting ghostpad target to dvbaudiosinkpad@%p returns %i\n", dvbaudiosinkpad, r);
  }
  else
  {
// 				GstPad *decodebinpad = gst_element_get_static_pad(gst_bin_get_by_name(GST_BIN(this->m_gst_audiobin), "decodebin"), "sink");
	  GstPad *universalpad = gst_element_get_static_pad(gst_bin_get_by_name(GST_BIN(app->audiobin), "universal_capsfilter"), "sink");
	  int r = gst_ghost_pad_set_target (GST_GHOST_PAD(pad), universalpad);
	  printf("setting ghostpad target to alsasink returns %i\n", r);
  }
}

int
main (int argc, char *argv[])
{
  App *app = &s_app;
  GstBus *bus;
  const gchar *nano_str;
  guint major, minor, micro, nano;

  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->audiobin = gst_bin_new ("universal_audiobin");

  GstElement *alsasink = gst_element_factory_make ("alsasink", "alsasink");
  GstElement *universal_capsfilter = gst_element_factory_make ("capsfilter", "universal_capsfilter");  
  gst_bin_add_many (GST_BIN (app->audiobin), universal_capsfilter, alsasink, NULL);
  gst_element_link (universal_capsfilter, alsasink);
  GstCaps *universal_caps = gst_caps_new_simple("audio/x-raw-int", "endianness", G_TYPE_INT, 4321, "depth", G_TYPE_INT, 16, "width", G_TYPE_INT, 16, "channels", G_TYPE_INT, 2, NULL);

  GstCaps *dvb_caps;
  GstElement *dvb_audiosink = gst_element_factory_make ("dvbaudiosink", "universal_dvbaudiosink");
  if (dvb_audiosink != NULL)
  {
    gst_bin_add (GST_BIN (app->audiobin), dvb_audiosink);
    dvb_caps = gst_caps_copy(gst_pad_get_pad_template_caps (gst_element_get_static_pad(dvb_audiosink, "sink")));
  }
  else
  {
    GstElement *dvb_fakesink = gst_element_factory_make ("fakesink", "universal_dvbaudiosink");
    dvb_caps = gst_caps_from_string("audio/mpeg; audio/x-ac3; audio/x-dts");
    GstElement *dvb_capsfilter = gst_element_factory_make("capsfilter", "dvbaudiosink_capsfilter");
    g_object_set (G_OBJECT (dvb_capsfilter), "caps", dvb_caps, NULL);
    gst_bin_add_many (GST_BIN (app->audiobin), dvb_capsfilter, dvb_fakesink,  NULL);
    gst_element_link (dvb_capsfilter, dvb_fakesink);
  }  

  GstCaps *bin_caps = gst_caps_copy(dvb_caps);
  gst_caps_merge(bin_caps,gst_caps_copy(universal_caps));
  
  GstPad *universal_audiosinkpad = gst_ghost_pad_new_no_target_from_template("sink", gst_pad_template_new("sink", GST_PAD_SINK, GST_PAD_ALWAYS, bin_caps));
//   GstPad *universal_audiosinkpad = gst_ghost_pad_new ("sink", gst_element_get_pad (universal_capsfilter, "sink"));
//   g_object_set (G_OBJECT (universal_audiosinkpad), "caps", universal_caps, NULL);
  printf("caps for universal audiobin:= %s\n", gst_caps_to_string(gst_pad_get_pad_template_caps(universal_audiosinkpad)));
  gst_caps_unref (universal_caps);
  gst_caps_unref (dvb_caps);

  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), app);

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