/* gstreamermm - a C++ wrapper for gstreamer
 *
 * Copyright 2008 The gstreamermm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <gst/gst.h>
#include <gstreamermm/taskpool.h>
_PINCLUDE(gstreamermm/private/object_p.h)

namespace
{

extern "C"
{

static void Task_Task_gstreamermm_callback(void* data)
{
  Gst::Task::SlotTask* the_slot = static_cast<Gst::Task::SlotTask*>(data);

  try
  {
    (*the_slot)();
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }
}

static void Task_Enter_gstreamermm_callback(GstTask* task, GThread* thread, gpointer user_data)
{
  Gst::Task::SlotEnter* the_slot = static_cast<Gst::Task::SlotEnter*>(user_data);

  try
  {
    (*the_slot)(Glib::wrap(task, true), Glib::Threads::wrap(thread));
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }
}

static void Task_Leave_gstreamermm_callback(GstTask *task, GThread *thread, gpointer user_data)
{
  Gst::Task::SlotLeave* the_slot = static_cast<Gst::Task::SlotLeave*>(user_data);
  try
  {
    (*the_slot)(Glib::wrap(task, true), Glib::Threads::wrap(thread));
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }
}

static void Task_Callbacks_gstreamermm_callback_destroy(void* data)
{
  delete static_cast<Gst::Task::BiSlot*>(data);
}

} // extern "C"

} // anonymous namespace

namespace Gst
{

Task::Task(const SlotTask& task_slot)
:
  _CONSTRUCT()
{
  // Create a copy of the slot.  A pointer to this copy will be passed through
  // the call back's data parameter.  It will be destroyed with the
  // std::auto_ptr<>.
  m_slot.reset(new SlotTask(task_slot));

  // These lines are taken verbatim from gst_task_create() after calling
  // g_object_new().
  gobj()->func = &Task_Task_gstreamermm_callback;
  gobj()->user_data = m_slot.get();
}

void Task::set_thread_slots(const SlotEnter& enter_slot,
  const SlotLeave& leave_slot)
{
  gst_task_set_enter_callback(gobj(), &Task_Enter_gstreamermm_callback, new SlotEnter(enter_slot), &Task_Callbacks_gstreamermm_callback_destroy);
  gst_task_set_leave_callback(gobj(), &Task_Leave_gstreamermm_callback, new SlotEnter(leave_slot), &Task_Callbacks_gstreamermm_callback_destroy);
}

} //namespace Gst
