/* gstreamermm - a C++ wrapper for gstreamer
 *
 * Copyright 2008-2009 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 <gstreamermm/buffer.h>
_PINCLUDE(gstreamermm/private/miniobject_p.h)

extern "C"
{

static GstBufferListItem BufferList_Foreach_gstreamermm_callback(GstBuffer** buffer, guint group, guint idx, void* data)
{
  Gst::BufferList::SlotForeach* the_slot =
    static_cast<Gst::BufferList::SlotForeach*>(data);

  try
  {
    // First wrap the original buffer.
    Glib::RefPtr<Gst::Buffer> temp = Gst::wrap(*buffer, true);

    // Call the slot which can modify the buffer (ie. set it to a new one or
    // clear it altogether).
    GstBufferListItem const result =
      static_cast<GstBufferListItem>((*the_slot)(temp, group, idx));

    // Set the reference to the buffer according to how the temp buffer has
    // been altered.
    if(!temp)
    {
      // Clear the reference to the original buffer also.
      *buffer = 0;
    }
    else if(*buffer != temp->gobj())
    {
      // The temp buffer has been set to a new one so make the reference point
      // to it and take an extra reference for when the temp wrapper is
      // destroyed (I think that's right).
      *buffer = temp->gobj();
      temp->reference();
    }

    return result;
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }

  return GST_BUFFER_LIST_CONTINUE;
}

static GstBuffer* BufferListIterator_Process_gstreamermm_callback(GstBuffer* buffer, void* data)
{
  Gst::BufferListIterator::SlotProcess* the_slot =
    static_cast<Gst::BufferListIterator::SlotProcess*>(data);

  try
  {
    Glib::RefPtr<Gst::Buffer> result = (*the_slot)(Gst::wrap(buffer, true));

    if(result)
      return result->gobj_copy();
    else
      return 0;
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }

  return 0;
}

} // extern "C"


namespace Gst
{

bool BufferList::is_writable() const
{
  return static_cast<bool>(gst_buffer_list_is_writable(gobj()));
}

Glib::RefPtr<BufferList> BufferList::create_writable()
{
  return Gst::wrap(gst_buffer_list_make_writable(gobj()));
}

guint BufferList::n_groups() const
{
  return gst_buffer_list_n_groups(const_cast<GstBufferList*>(gobj()));
}

void BufferList::foreach(const SlotForeach& slot)
{
  gst_buffer_list_foreach(gobj(), &BufferList_Foreach_gstreamermm_callback,
    const_cast<SlotForeach*>(&slot));
}

void BufferList::iterate(BufferListIterator& result)
{
  result.set_gobject(gst_buffer_list_iterate(gobj()));
}

BufferListIterator::BufferListIterator()
: gobject_(0),
  take_ownership(false)
{
}

BufferListIterator::BufferListIterator(GstBufferListIterator* castitem,
  bool take_ownership)
: gobject_(castitem),
  take_ownership(take_ownership)
{
}

void BufferListIterator::set_gobject(GstBufferListIterator* castitem,
  bool take_ownership)
{
  if(gobject_ && take_ownership)
    gst_buffer_list_iterator_free(gobject_);

  gobject_ = castitem;
  this->take_ownership = take_ownership;
}

BufferListIterator::~BufferListIterator()
{
  if(take_ownership && gobject_)
    gst_buffer_list_iterator_free(gobject_);
}

Glib::RefPtr<Gst::Buffer> BufferListIterator::process(const SlotProcess& slot)
{
  return Gst::wrap(gst_buffer_list_iterator_do(gobj(),
    &BufferListIterator_Process_gstreamermm_callback,
    const_cast<SlotProcess*>(&slot)));
}

} //namespace Gst
