Just a little thank you

rob_ewbank robewbank at gmail.com
Wed Feb 5 07:56:17 PST 2014


I have been working on a project using Gstreamer and whilst building on Linux
is a very good, powerful framework. Porting is a tricky situation. 

Anyways, the guys on this forum who make GStreamer have been very helpful
and responsive. I thought I'd give a little something back. Just a small
part of my code I find useful for working with GStreamer in C++. It allows
building elements within a C++ class you inherit from. Simplifying the
implementation process. 

Inherit off this function, build a pipeline using the functions available.
There should be an element for input and an element for the output.
Everything else is using C in gstreamer, but this allows encapsulation of
parts. 

When you use it from an external point of view, control it via the shared
object (control_and_status) that is passed into the block. An object you
need to create. Any object / variable in that will be available to anything
that passed it in.

 Log() can be replaced by std::cout.

_______________ HEADER
________________________________________________________

#pragma once
#include <gst/gst.h>
#include <controlandstatus.h>

class CustomElementBase
{
public:
  CustomElementBase(ContrlAndStat control_and_status);
  virtual ~CustomElementBase(){}
  virtual GstElement * GetElement();

  virtual GstElement * CreateElementAndAddToBin(std::string element_type,
std::string name = "");
  virtual void CreatePipeline();
  virtual void SetOutputElement(GstElement * element, std::string
source_name = "src");
  virtual void SetInputElement(GstElement * element, std::string sink_name =
"sink");

  // These functions need to be overriden in the derived class
  virtual std::string NameOfCustomElement() = 0;
  virtual bool CreateElement() = 0;
protected:
  ContrlAndStat _control_and_status;
  GstElement * _element_bin;

  boost::signals2::scoped_connection
_scoped_connection_update_from_controls;
};


_________________________ CPP
_________________________________________________

#include "customelementbase.h"

//=====================================================================================================
/// Constructor, this is the base class for custom elements, it abstracts
//=====================================================================================================
CustomElementBase::CustomElementBase(ContrlAndStat control_and_status):
  _control_and_status(control_and_status),
  _element_bin(NULL)
{

}

//=====================================================================================================
/// Returns the element
//=====================================================================================================
GstElement * CustomElementBase::GetElement()
{
  if (_element_bin)
  {
    return _element_bin;
  }
  else
  {
    if (CreateElement())
    {
      return _element_bin;
    }
    else
    {
      Log() << "Cannot get " << NameOfCustomElement() << " pipeline" <<
std::endl;
      return NULL;
    }
  }
}


//=====================================================================================================
/// Creates the pipeline
//=====================================================================================================
void CustomElementBase::CreatePipeline()
{
  /* Create gstreamer elements */
  _element_bin = gst_bin_new(NameOfCustomElement().c_str());

  if(!_element_bin)
  {
    Log() << "Bin could  " << NameOfCustomElement() << "  not be created" <<
std::endl;
    throw 0;
  }
}


//=====================================================================================================
/// Creates an element and adds it to the bin, throws if cannot
//=====================================================================================================
GstElement * CustomElementBase::CreateElementAndAddToBin(std::string
element_type, std::string name)
{
  GstElement * element_to_create = NULL;
  if (name != "")
  {
    element_to_create  = gst_element_factory_make (element_type.c_str(),
name.c_str());
  }
  else
  {
    element_to_create  = gst_element_factory_make (element_type.c_str(),
NULL);
  }

  if(!element_to_create)
  {
    Log() << element_type << " could not be created in " <<
NameOfCustomElement() << std::endl;
    throw 0;
  }
  Log() << element_type << " Created successfully in " <<
NameOfCustomElement() << std::endl;
  gst_bin_add(GST_BIN (_element_bin), element_to_create);
  return element_to_create;
}



//=====================================================================================================
/// Sets the source for this bin
//=====================================================================================================
void CustomElementBase::SetOutputElement(GstElement * element, std::string
source_name)
{
  GstPad *source_pad = gst_element_get_static_pad (element, "src");
  if (!gst_element_add_pad (_element_bin, gst_ghost_pad_new
(source_name.c_str(), source_pad)))
  {
    Log() << "Could not attach output pad in " << NameOfCustomElement()  <<
std::endl;
  }
  gst_object_unref (GST_OBJECT (source_pad));
}


//=====================================================================================================
/// Sets the sink for this bin
//=====================================================================================================
void CustomElementBase::SetInputElement(GstElement * element, std::string
sink_name)
{
  GstPad *sink_pad = gst_element_get_static_pad(element, "sink");
  if (!gst_element_add_pad (_element_bin, gst_ghost_pad_new
(sink_name.c_str(), sink_pad)))
  {
    Log() << "Could not attach input pad in"  << NameOfCustomElement() <<
std::endl;
  }

  gst_object_unref (GST_OBJECT (sink_pad));
}







--
View this message in context: http://gstreamer-devel.966125.n4.nabble.com/Just-a-little-thank-you-tp4665114.html
Sent from the GStreamer-devel mailing list archive at Nabble.com.


More information about the gstreamer-devel mailing list