Hello. I have a small problem. I don't know how to write a program that takes a photo using a webcam. I know how to do that using gst-launch-1.0, though. gst-launch-1.0 -e v4l2src device=/dev/video0 num-buffers=1 ! videoconvert ! pngenc ! filesink location=sample.png sync=true This works. A photo of me is successfully saved as sample.png. What I'd like to do is to write GStreamer code equivalent to the command above. Here is my best try. #include <gst/gst.h> int main(int argc, char **argv) { // initialize return value int status; // initialize GStreamer gst_init(&argc, &argv); // create pipeline and other elements to use GstElement *pipeline = gst_pipeline_new("pipeline"); GstElement *source = gst_element_factory_make("v4l2src", "source"); GstElement *converter = gst_element_factory_make("videoconvert", "converter"); GstElement *encoder = gst_element_factory_make("pngenc", "encoder"); GstElement *file = gst_element_factory_make("filesink", "file"); // add all elements to the pipeline and connect them gst_bin_add_many(GST_BIN(pipeline), source, converter, encoder, file, NULL); gst_element_link_many(source, converter, encoder, file, NULL); // set the properties g_object_set(source, "device", "/dev/video0", "num-buffers", 1, NULL); g_object_set(file, "location", "sample.png", "sync", 1, NULL); // play gst_element_set_state(pipeline, GST_STATE_PLAYING); // wait for EOS GstBus *bus = gst_element_get_bus(pipeline); GstMessage *msg = gst_bus_timed_pop(bus, GST_CLOCK_TIME_NONE); // determine return value using msg switch (GST_MESSAGE_TYPE(msg)) { case GST_MESSAGE_ERROR: status = -1; break; case GST_MESSAGE_EOS: status = 0; break; default: status = 1; break; } // clean up gst_element_set_state(pipeline, GST_STATE_NULL); if (msg != NULL) gst_message_unref(msg); gst_object_unref(bus); gst_object_unref(pipeline); // exit from the program and return status return status; } The problem with this code is that really creates sample.png, but the file is corrupted and cannot be opened. Why the code above doesn't work as intended and what do I need to change to make it work the way I want? Thank you for reading this. Have a nice day. _______________________________________________ gstreamer-devel mailing list [hidden email] https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel |
Hi,
You're missing a loop around the bus message fetching, the first message is not the one you want. Olivier On October 20, 2018 11:57:30 a.m. EDT, "Радомир Хаџић" <[hidden email]> wrote: >Hello. > >I have a small problem. I don't know how to write a program that takes >a >photo using a webcam. I know how to do that using *gst-launch-1.0*, >though. >*gst-launch-1.0 -e v4l2src device=/dev/video0 num-buffers=1 ! >videoconvert >! pngenc ! filesink location=sample.png sync=true* >This works. A photo of me is successfully saved as *sample.png*. > >What I'd like to do is to write GStreamer code equivalent to the >command >above. Here is my best try. > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >*#include <gst/gst.h>int main(int argc, char **argv){ // initialize >return value int status; // initialize GStreamer >gst_init(&argc, >&argv); // create pipeline and other elements to use GstElement >*pipeline = gst_pipeline_new("pipeline"); GstElement *source = >gst_element_factory_make("v4l2src", "source"); GstElement *converter >= >gst_element_factory_make("videoconvert", "converter"); GstElement >*encoder = gst_element_factory_make("pngenc", "encoder"); GstElement >*file = gst_element_factory_make("filesink", "file"); // add all >elements to the pipeline and connect them >gst_bin_add_many(GST_BIN(pipeline), source, converter, encoder, file, >NULL); gst_element_link_many(source, converter, encoder, file, >NULL); >// set the properties g_object_set(source, "device", "/dev/video0", >"num-buffers", 1, NULL); g_object_set(file, "location", >"sample.png", >"sync", 1, NULL); // play gst_element_set_state(pipeline, >GST_STATE_PLAYING); // wait for EOS GstBus *bus = >gst_element_get_bus(pipeline); GstMessage *msg = >gst_bus_timed_pop(bus, >GST_CLOCK_TIME_NONE); // determine return value using msg switch >(GST_MESSAGE_TYPE(msg)) { case GST_MESSAGE_ERROR: >status = -1; break; case GST_MESSAGE_EOS: >status = 0; break; default: status = 1; > break; } // clean up gst_element_set_state(pipeline, >GST_STATE_NULL); if (msg != NULL) gst_message_unref(msg); >gst_object_unref(bus); gst_object_unref(pipeline); // exit from >the >program and return status return status;}* >The problem with this code is that really creates *sample.png*, but the >file is corrupted and cannot be opened. Why the code above doesn't work >as >intended and what do I need to change to make it work the way I want? > >Thank you for reading this. Have a nice day. -- Olivier Crête [hidden email] _______________________________________________ gstreamer-devel mailing list [hidden email] https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel |
Free forum by Nabble | Edit this page |