/* 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/gstenumtypes.h>
#include <gst/gstinterface.h>
#include <gst/gstutils.h>
#include <gstreamermm/bus.h>
#include <gstreamermm/caps.h>
#include <gstreamermm/elementfactory.h>
#include <gstreamermm/event.h>
#include <gstreamermm/index.h>
#include <gstreamermm/pad.h>
#include <gstreamermm/padtemplate.h>
#include <gstreamermm/query.h>
#include <gstreamermm/taglist.h>
#include <gstreamermm/iterator.h>

_PINCLUDE(gstreamermm/private/object_p.h)

namespace Gst
{

void ElementInterfaced_WeakNotify_gstreamermm_callback(void* data, GObject* where_the_object_was)
{
  delete (Gst::Element*) data;
}

namespace Enums
{

Glib::ustring get_name(State s)
{
  return gst_element_state_get_name(GstState(s));
}

Glib::ustring get_name(StateChangeReturn s)
{
  return gst_element_state_change_return_get_name(GstStateChangeReturn(s));
}

} //namespace Enums

Glib::RefPtr<Gst::Element> Element::link(const Glib::RefPtr<Gst::Element>& dest)
{
  const bool result = gst_element_link(gobj(), dest->gobj());

  if(result)
    return dest;
  else
    throw std::runtime_error("failed to link: " + get_name() + "->" + dest->get_name());
}

bool Element::query_position(Format& format) const
{
  return gst_element_query_position(const_cast<GstElement*>(gobj()), reinterpret_cast<GstFormat*>(&format), NULL);
}

bool Element::query_duration(Format& format) const
{
  return gst_element_query_duration(const_cast<GstElement*>(gobj()), reinterpret_cast<GstFormat*>(&format), NULL);
}

// This method is written manually because an extra ref is necessary. See
// gst_element_send_event() docs
bool Element::send_event(const Glib::RefPtr<Gst::Event>& event)
{
  event->reference();
  return gst_element_send_event(gobj(), event->gobj());
}

} //namespace Gst
