/* 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/audio/gstaudioclock.h>

_PINCLUDE(gstreamermm/private/systemclock_p.h)

static GstClockTime AudioClock_GetTime_gstreamermm_callback(GstClock* clock, void* data)
{
  Gst::AudioClock::SlotGetTime* the_slot = static_cast<Gst::AudioClock::SlotGetTime*>(data);

  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
  #endif //GLIBMM_EXCEPTIONS_ENABLED
    return (*the_slot)(Glib::wrap(clock, true));
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }
  #endif //GLIBMM_EXCEPTIONS_ENABLED

  return 0; // arbitrary value
}

namespace Gst
{

AudioClock::AudioClock(const Glib::ustring& name, const SlotGetTime& time_slot)
  : _CONSTRUCT("name", name.c_str())
{
  slot = new SlotGetTime(time_slot);
  _slot_set(true);

  //The following lines are taken verbatim from gst_audio_clock_new() after the
  //call to g_object_new() because it seems that bug #545782 will not be
  //accepted:
  gobj()->func = &AudioClock_GetTime_gstreamermm_callback;
  gobj()->user_data = &slot;
}

bool AudioClock::_slot_set(bool mark_set) const
{
  static bool slot_set = false;

  if (mark_set)
    slot_set = true;

  return slot_set;
}


AudioClock::~AudioClock()
{
  if (_slot_set())
    delete slot;
}

} //namespace Gst
