Showing posts with label gstreamermm. Show all posts
Showing posts with label gstreamermm. Show all posts

Saturday, July 16, 2016

Generic C++ GObject signals wrapper

Recently I've discovered, that connecting to signals in gstreamermm can be really inconvenient. The problem doesn't exist in the other mm libraries, because most of the classes and their signals are wrapped.
But GStreamer allows to create user-defined elements, so it's actually impossible to wrap everything in gstreamermm (for now library supports wrappers for gstreamer plugins core and base).

Currently, if you want to connect to a signal in gstreamermm, you have two options:
  1. Using pure C API:

  2. auto typefind = Gst::ElementFactory::create_element("typefind");
    
    g_signal_connect (typefind->gobj(), 
                      "have-type", 
                      G_CALLBACK(cb_typefind_havetype), 
                     (gpointer *)typefind->gobj());
    
    static void cb_typefind_havetype (GstTypeFindElement *typefind,
                                      guint               probability,
                                      GstCaps            *caps,
                                      gpointer            user_data)
    {
      // callback implementation
    }
    Well, it's not very bad. But... you have to use C structures in the callback instead of C++ wrappers.

  3. Using gstreamermm API
  4. As I mentioned, gstreamermm provide wrappers for core and base plugins, so some of the elements (and their signals) are already wrapped in the library:
    auto typefind = Gst::TypeFindElement::create();
    typefind->signal_have_type().connect(
        [] (guint probability, const Glib::RefPtr& caps)
        {
          // callback implementation
        });
    
However, many plugins are not wrapped (and they never will), so usually you need to either write wrapper for element which you wanted to use (and then, maintain this wrapper as well), or use pure C API.
Moreover, I'm going to remove plugin API in the next release [1], so user won't be able to use gstreamermm API even for the base and core plugins. I was wondering, if it would be possible to write generic wrapper for the GObject signals. So... there you are! The solution is not perfect yet, and I haven't tested it so much, but so far it works fine with few plugins and signals.

namespace Glib
{  
  template <typename T>
  static constexpr T wrap (T v, bool=true)
  {
    return v;
  }

  template <typename T>
  static constexpr T unwrap (T v, bool=true)
  {
    return v;
  }

  template<typename T>
  using unwrapped_t = decltype(unwrap(*((typename std::remove_reference<T>::type*)nullptr)));

  template<typename T>
  constexpr T return_helper()
  {
    typedef unwrapped_t<T> Ret;
    return Ret();
  }

  template<>
  constexpr void return_helper()
  {
    return void();
  }
}


template<typename>
class signal_callback;

template<typename Ret, typename ...T>
class signal_callback<Ret(T...)>
{
  template<typename ...Args>
  static auto callback(void* self, Args ...v)
  {
    using Glib::wrap;
    typedef sigc::slot< void, decltype(wrap(v))... > SlotType;

    void* data = std::get<sizeof...(Args)-1>(std::tuple<Args...>(v...));

    // Do not try to call a signal on a disassociated wrapper.
    if(dynamic_cast<Glib::Object*>(Glib::ObjectBase::_get_current_wrapper((GObject*) self)))
      {
	try
	  {
	    if(sigc::slot_base *const slot = Glib::SignalProxyNormal::data_to_slot(data))
	      {
		(*static_cast<SlotType*>(slot))(wrap(std::forward<Args>(v), true)...);
	      }
	  }
	catch(...)
	  {
	    Glib::exception_handlers_invoke();
	  }
      }

    return Glib::return_helper<Ret>();
  }

public:
  auto operator()(const std::string& signal_name, const Glib::RefPtr<Glib::Object>& obj)
  {
    using Glib::unwrap;
    static std::map<std::pair<GType, std::string>, Glib::SignalProxyInfo> signal_infos;

    auto key = std::make_pair(G_TYPE_FROM_INSTANCE (obj->gobj()), signal_name);
    if (signal_infos.find(key) == signal_infos.end())
      {
	signal_infos[key] = {
	  signal_name.c_str(),
	  (GCallback) &callback<Glib::unwrapped_t<T>..., void*>,
	  (GCallback) &callback<Glib::unwrapped_t<T>..., void*>
	};
      }

    return Glib::SignalProxy<Ret, T... >(obj.operator->(), &signal_infos[key]);
  }
};


auto typefind = Gst::ElementFactory::create_element("typefind"),
signal_callback<void(guint, const Glib::RefPtr<Gst::Caps>&)> signal_wrapper;

signal_wrapper("have-type", typefind).connect(
  [&ready, &cv] (guint probability, const Glib::RefPtr<Gst::Caps>& caps) {
    std::cout << "have-type: probability=" << probability << std::endl;
    Gst::Structure structure = caps->get_structure(0);
    const Glib::ustring mime_type = structure.get_name();
    std::cout << "have-type: mime_type=" << mime_type << std::endl;

    structure.foreach([] (const Glib::QueryQuark& id, const Glib::ValueBase& value) {
      const Glib::ustring str_id = id;
      gchar* str_value = g_strdup_value_contents(value.gobj());
      std::cout << "Structure field: id=" << str_id << ", value=" << str_value << std::endl;
      g_free(str_value);		      
      return true;
  });
});

Full source of the code can be found on the github [2].
As you see, you still have to know the type of the callback, but at least you can use gstreamermm C++ classes.
There is couple of things to do in this code, like getting last parameter from the list in more efficient way than through the tuple, etc.
I don't feel it is stable enough to integrate it with gstreamermm, but probably in the future I'll do that. Also, we could even internally use it in the glibmm to reduce amount of generated code.

Links
[1] https://bugzilla.gnome.org/show_bug.cgi?id=755395
[2] https://gist.github.com/loganek/7833089caff73ff2e8b1f076c8f7910e

Friday, August 29, 2014

gstreamermm on Windows & Visual Studio

Hi everyone!
Recently me and my friends have ported quite big project from Linux to Windows, and had to build it in Visual Studio environment. (Un)fortunatelly, we're strongly dependent on gstreamermm project, so I had to prepare .vcxproj files, which allows us to compile gstreamermm in Microsoft's IDE.
I spent one evening on that, and that's it! I've commited Visual Studio project file for building gstreamermm project, and also property sheet (.props), which contains all necessary dependencies for gstreamermm-based projects.
I've tested it in Debug/Release configuration, both platforms (Win32 and x64), and have used Visual Studio 2010 (project file probably can be upgraded to VS2013, but I didn't test it yet).
You can find it in the nearest gstreamermm release (currently I'm working on 1.2.4 version, it should be finished in a next month), but I've already prepared unofficial release for you [1].
Go ahead, download, use, and file  a bugs on a bugzilla [2] ;)

Installer
If I have a time, I will prepare windows installer for sure, and upload it with official v1.2.4 release. If not, maybe someone else will pick up the gauntlet, and do one-click installer for gstreamermm community - if you'd like to help, just let me know, I'll help you so far as it is possible.

Dependencies
gstreamermm depends on many other libraries, so you need to install gtkmm package (can be found here [3]), or build necessary yourself from sources (that's way which I have chosen).

Links
[1] https://dl.dropboxusercontent.com/u/14229925/gstreamermm-1.0.10-msvc-support.tar.gz
[2] https://bugzilla.gnome.org/enter_bug.cgi?product=gstreamermm
[3] http://ftp.gnome.org/pub/gnome/binaries/

Sunday, August 3, 2014

gstreamermm 1.0 finally released!

Yeah, after more than year, I finally released gstreamermm [1] 1.0 version - it might be downloaded from the website [2]. I'm really sorry people, who have waited for this release, I had not so much time to do it. Ok. I had time, I didn't it earlier because of my laziness. GUADEC finally motivated me to do this release, and I'm promis, I'll work on gstreamermm project regular, and also official releases will be done as soon as it will be possible.

Changes
The most important change, compared to previous version, is supported gstreamer 1.0.
Moreover, possibility for writing your own plugin was added (thanks peper0 [3]).
I also used gtest [4] for writing testcases.

Future plans
  • There is a lot of bugs, which I'll for sure fix in the next stable version. I'm going to write much more test cases (my aim - cover every single function). Feel free to use this library, file a bugs and send patches on a bugzilla [5]!
  • I'm also going to wrap new things from 1.2 version, and later also from 1.4 version.
Packaging - HELP!
I'm looking for help with releasing Ubuntu/Fedora packages. I'd never done it, and it would be nice, if somebody help me, or even do it instead of me :)

Sunday, July 13, 2014

GstCreator - introduction


I'd like to introduce to you my OpenSource project - GstCreator [1]. It's some kind of IDE for GStreamer framework [2]. User can rapidly prototype GStreamer pipeline, and (in the future, not implemented yet) generate C or C++ file with code.
However, user can peek buffers, events and queries on every single pad.

Toolchain
Entire project is written in C++. I'm using Qt library [3](maybe in the future it will be replaced by Gtk+ [4], will see). CMake [5] is my build system. I don't use GStreamer directly, but gstreamermm library [6], which is C++ wrapper for GStreamer framework. It's because of two reasons: 
  • don't like mix 2 languages in one project (even if syntax of both languages is very similar)
  • I'm currently maintainer of gstreamermm project, and use this library by myself helps me find and eliminate a lot of bugs.
Development plan
GstCreator is not ready, ah, it's quite long long way to make first release of this application. But I'm of good cheer.
The list of most important features, which should be implemented in the nearest future:
  • linking to a `sometimes` pads
  • improved `ProbesWatcher` module by showing more informations about buffers, events, and queries, and adding possibility for filtering data
  • save & load project
List above shows only the most important tasks, but there is a lot of minor bugs, which should be implemented ASAP. There is a lot ideas in my head, how to improve this tool, made it more useful and powerful ;)
I hope, some of you will enjoy this application, maybe there is also somebody, who'd like to help me in development ;)
I'm also waiting for your ideas connected to this project. Maybe some features are especially useful, but I didn't notice it;)

I will inform you here about my progress, new features ;)

Quiz
And at the end, short (and easy) quiz: What film is played on a screenshoot shown below?

GstCreator during playing udp stream

Links 
[1] https://github.com/loganek/gstcreator
[2] http://gstreamer.freedesktop.org/
[3] http://qt-project.org/
[4] http://www.gtk.org/
[5] http://cmake.org/
[6] https://git.gnome.org/browse/gstreamermm/