After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 794481 - Callbacks from gstreamer's device monitor do not seem to work
Callbacks from gstreamer's device monitor do not seem to work
Status: RESOLVED NOTABUG
Product: GStreamer
Classification: Platform
Component: gstreamer (core)
unspecified
Other Linux
: Normal normal
: git master
Assigned To: GStreamer Maintainers
GStreamer Maintainers
Depends on:
Blocks:
 
 
Reported: 2018-03-19 09:50 UTC by Rion
Modified: 2018-03-19 16:21 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Rion 2018-03-19 09:50:31 UTC
Hi

I just tried the example code from https://gstreamer.freedesktop.org/data/doc/gstreamer/head/gstreamer/html/gstreamer-GstDeviceMonitor.html

and forged a small application to test the callbacks.

#include <gst/gst.h>
#include <glib.h>

static gboolean
my_bus_func (GstBus * bus, GstMessage * message, gpointer user_data)
{
   GstDevice *device;
   gchar *name;

   switch (GST_MESSAGE_TYPE (message)) {
     case GST_MESSAGE_DEVICE_ADDED:
       gst_message_parse_device_added (message, &device);
       name = gst_device_get_display_name (device);
       g_print("Device added: %s\n", name);
       g_free (name);
       gst_object_unref (device);
       break;
     case GST_MESSAGE_DEVICE_REMOVED:
       gst_message_parse_device_removed (message, &device);
       name = gst_device_get_display_name (device);
       g_print("Device removed: %s\n", name);
       g_free (name);
       gst_object_unref (device);
       break;
     default:
       break;
   }

   return G_SOURCE_CONTINUE;
}

GstDeviceMonitor *
setup_raw_video_source_device_monitor (void) {
   GstDeviceMonitor *monitor;
   GstBus *bus;
   GstCaps *caps;

   monitor = gst_device_monitor_new ();

   bus = gst_device_monitor_get_bus (monitor);
   gst_bus_add_watch (bus, my_bus_func, NULL);
   gst_object_unref (bus);

   gst_device_monitor_add_filter (monitor, "Audio/Sink", NULL);
    gst_device_monitor_add_filter (monitor, "Audio/Source", NULL);

   caps = gst_caps_new_empty_simple ("video/x-raw");
   gst_device_monitor_add_filter (monitor, "Video/Source", caps);
   gst_caps_unref (caps);

   gst_device_monitor_start (monitor);

   return monitor;
}

int main(int argc, char **argv)
{
    gst_init(&argc, &argv);
    GMainContext *mainContext = g_main_context_new();
    GMainLoop *mainLoop = g_main_loop_new(mainContext, FALSE);

    setup_raw_video_source_device_monitor();

    g_main_loop_run(mainLoop);
}


I compiled it like this
gcc test.c $(pkg-config --cflags glib-2.0) $(pkg-config --cflags gstreamer-1.0) $(pkg-config --libs glib-2.0) $(pkg-config --libs gstreamer-1.0)

But after start it just prints nothing. I tried to attach and detach audio usb devices but nothing. 
If instead of monitoring I try gst_device_monitor_get_devices(), it returns correct list with my devices.

It's Gentoo Linux system here and gstreamer 1.12.4
Comment 1 Olivier Crête 2018-03-19 15:34:21 UTC
The problem is that you have nothing running the default GMainContext. So in your code, just remove the g_main_context_new() call and pass "NULL" as the first argument to g_main_loop_new()
Comment 2 Rion 2018-03-19 16:21:17 UTC
Thanks. It worked. glib programming is something new for me.

I also figured out I have to combine both gst_device_monitor_get_devices() and gst_device_monitor_start() to keep my internal devices list in actual state. Previously I thought gst_device_monitor_start would be enough, but it doesn't generate events for already connected devices.