/* 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 <stdexcept>
#include <gst/gst.h>

_PINCLUDE(gstreamermm/private/miniobject_p.h)

namespace Gst
{

Glib::RefPtr<Gst::Buffer> Buffer::copy() const
{
  return Glib::wrap(gst_buffer_copy(gobj()), false);
}

Glib::RefPtr<Gst::Buffer> Buffer::create(guint size)
{
  return Glib::wrap(gst_buffer_new_allocate(NULL, size, NULL));
}

Glib::RefPtr<Buffer> Buffer::create_writable()
{
  return Glib::RefPtr<Buffer>::cast_static(MiniObject::create_writable());
}

guint64 Buffer::offset_none()
{
  return GST_BUFFER_OFFSET_NONE;
}

void Buffer::insert_memory(guint idx, Glib::RefPtr<Gst::Memory>& mem)
{
  GstMemory *mem_gobj = mem->gobj();
  mem->reference();
  mem.reset();
  gst_buffer_insert_memory(gobj(), idx, mem_gobj);
}

void Buffer::prepend_memory(Glib::RefPtr<Gst::Memory>& mem)
{
  insert_memory(0, mem);
}

void Buffer::append_memory(Glib::RefPtr<Gst::Memory>& mem)
{
  insert_memory(-1, mem);
}

void Buffer::replace_memory_range(guint idx, gint length, Glib::RefPtr<Gst::Memory>& mem)
{
  GstMemory *mem_gobj = mem->gobj();
  mem->reference();
  mem.reset();
  gst_buffer_replace_memory_range(gobj(), idx, length, mem_gobj);
}

void Buffer::replace_memory(guint idx, Glib::RefPtr<Gst::Memory>& mem)
{
  replace_memory_range(idx, 1, mem);
}

void Buffer::replace_all_memory(Glib::RefPtr<Gst::Memory>& mem)
{
  replace_memory_range(0, -1, mem);
}

} // namespace Gst
