Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- GStreamer example
- Compiles with : gcc `pkg-config --cflags --libs gstreamer-0.10` -o gst_example gst_example.c
- Detailed explanation and example footprint was shown here...
- http://ishikawa-vision.org/~kyamagu/cgi-bin/moin.cgi/VideoTexture/
- */
- #include <gst/gst.h>
- #include <glib.h>
- // if you require uncomment
- //#define USB_CAM
- //#define IEEE1394
- /* callback prototype */
- static gbooleanbus_handler(GstBus *bus, GstMessage *msg, gpointer data);
- /* main */
- int main(int argc, char *argv[])
- {
- GMainLoop *loop;
- GstElement *pipeline, *source, *sink;
- GstBus *bus;
- /** (1) Initialisation **/
- gst_init(&argc, &argv);
- /** (2) Objects instantiation **/
- loop = g_main_loop_new(NULL, FALSE);
- /* Create gstreamer elements */
- pipeline = gst_pipeline_new("testpipeline");
- #if defined(USB_CAM)
- //g_object_set(v4l2sink, "device", "/dev/video1", NULL);
- source = gst_element_factory_make("v4l2src", "videosrc"); /* use usb cam */
- #elif defined(IEEE1394)
- source = gst_element_factory_make("dc1394src", "videosrc"); /* use IEEE1394 cam */
- #else
- source = gst_element_factory_make("videotestsrc", "videosrc");
- #endif
- sink = gst_element_factory_make("fakesink", "fakesink");
- if (!pipeline || !source || !sink) {
- g_printerr("One element could not be created. Exiting.\n");
- return -1;
- }
- /** (3) Set up the pipeline **/
- /* we set the sync property of fakesink to TRUE */
- g_object_set(G_OBJECT (sink), "sync", TRUE, NULL);
- /* we add a message handler */
- bus = gst_pipeline_get_bus(GST_PIPELINE (pipeline));
- gst_bus_add_watch(bus, bus_handler, loop);
- gst_object_unref(bus);
- /* we add all elements into the pipeline */
- gst_bin_add_many(GST_BIN (pipeline), source, sink, NULL);
- /* we link the elements together */
- gst_element_link(source, sink);
- /** (4) Main loop **/
- /* Set the pipeline to "playing" state */
- gst_element_set_state(pipeline, GST_STATE_PLAYING);
- g_main_loop_run(loop);
- /* Out of the main loop, clean up nicely */
- // delete the memory allocated to the main loop
- g_main_loop_unref(loop);
- // When you don't need the element anymore, you need to unref it using gst_object_unref ().
- // This decreases the reference count for the element by 1. An element has a refcount of 1 when
- // it gets created. An element gets destroyed completely when the refcount is decreased to 0.
- // ref :- https://www.cnblogs.com/fellow1988/p/12892254.html
- //
- gst_object_unref(GST_OBJECT (source));
- gst_object_unref(GST_OBJECT (sink));
- // same for the pipeline memory
- gst_element_set_state(pipeline, GST_STATE_NULL);
- gst_object_unref(GST_OBJECT (pipeline));
- gst_deinit();
- return 0;
- }
- /* callback implementation */
- static gboolean bus_handler(GstBus *bus, GstMessage *msg, gpointer data)
- {
- GMainLoop *loop = (GMainLoop *) data;
- switch (GST_MESSAGE_TYPE (msg)) {
- case GST_MESSAGE_ERROR: {
- gchar *debug;
- GError *error;
- gst_message_parse_error(msg, &error, &debug);
- g_free (debug);
- g_printerr("Error: %s\n", error->message);
- g_error_free (error);
- g_main_loop_quit(loop);
- break;
- }
- default:
- break;
- }
- return TRUE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement