Created
April 14, 2015 01:19
-
-
Save bitonic/627fb9c1a5d494372d1d to your computer and use it in GitHub Desktop.
loop video into v4l2loopback device
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <gst/gst.h> | |
| #include <glib.h> | |
| typedef struct { | |
| GMainLoop* loop; | |
| GstElement* pipeline; | |
| } LoopAndPipeline; | |
| static gboolean bus_call(GstBus *bus, GstMessage *msg, gpointer data) | |
| { | |
| LoopAndPipeline *loop_and_pipeline = (LoopAndPipeline*) data; | |
| GMainLoop *loop = loop_and_pipeline->loop; | |
| GstElement *pipeline = loop_and_pipeline->pipeline; | |
| switch (GST_MESSAGE_TYPE (msg)) { | |
| case GST_MESSAGE_EOS: | |
| g_print ("End of stream\n"); | |
| gst_element_set_state(pipeline, GST_STATE_READY); | |
| gst_element_set_state(pipeline, GST_STATE_PLAYING); | |
| break; | |
| 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; | |
| } | |
| static void on_pad_added (GstElement *element, GstPad *pad, gpointer data) | |
| { | |
| GstPad *sinkpad; | |
| GstElement *decoder = (GstElement *) data; | |
| /* We can now link this pad with the vorbis-decoder sink pad */ | |
| g_print ("Dynamic pad created, linking demuxer/decoder\n"); | |
| sinkpad = gst_element_get_static_pad (decoder, "sink"); | |
| gst_pad_link (pad, sinkpad); | |
| gst_object_unref (sinkpad); | |
| } | |
| int | |
| main (int argc, char *argv[]) | |
| { | |
| GMainLoop *loop; | |
| GstBus *bus; | |
| guint bus_watch_id; | |
| /* Initialisation */ | |
| gst_init (&argc, &argv); | |
| loop = g_main_loop_new (NULL, FALSE); | |
| /* Check input arguments */ | |
| if (argc != 3) { | |
| g_printerr ("Usage: %s <video> <v4l2loopback device>\n", argv[0]); | |
| return -1; | |
| } | |
| /* Create gstreamer elements */ | |
| GstElement *pipeline = gst_pipeline_new ("audio-player"); | |
| GstElement *source = gst_element_factory_make ("filesrc", NULL); | |
| GstElement *decode = gst_element_factory_make ("decodebin", NULL); | |
| GstElement *convert = gst_element_factory_make ("videoconvert", NULL); | |
| GstElement *sink = gst_element_factory_make ("v4l2sink", NULL); | |
| if (!pipeline || !source || !decode || !convert || !sink) { | |
| g_printerr ("One element could not be created. Exiting.\n"); | |
| return -1; | |
| } | |
| /* Set up the pipeline */ | |
| g_object_set (G_OBJECT (source), "location", argv[1], NULL); | |
| g_object_set(G_OBJECT (sink), "device", argv[2], NULL); | |
| /* we add a message handler */ | |
| LoopAndPipeline loop_and_pipeline; | |
| loop_and_pipeline.loop = loop; | |
| loop_and_pipeline.pipeline = pipeline; | |
| bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline)); | |
| bus_watch_id = gst_bus_add_watch (bus, bus_call, &main_loop_and_pipeline); | |
| gst_object_unref (bus); | |
| /* we add all elements into the pipeline */ | |
| gst_bin_add_many (GST_BIN (pipeline), source, decode, convert, sink, NULL); | |
| /* we link the elements together */ | |
| gst_element_link (source, decode); | |
| GstCaps *caps = gst_caps_new_simple ("video/x-raw", "format", G_TYPE_STRING, "YUY2", NULL); | |
| gst_element_link_filtered (convert, sink, caps); | |
| g_signal_connect (decode, "pad-added", G_CALLBACK (on_pad_added), convert); | |
| /* note that the demuxer will be linked to the decoder dynamically. | |
| The reason is that Ogg may contain various streams (for example | |
| audio and video). The source pad(s) will be created at run time, | |
| by the demuxer when it detects the amount and nature of streams. | |
| Therefore we connect a callback function which will be executed | |
| when the "pad-added" is emitted.*/ | |
| /* Set the pipeline to "playing" state*/ | |
| g_print ("Now playing: %s\n", argv[1]); | |
| gst_element_set_state (pipeline, GST_STATE_PLAYING); | |
| /* Iterate */ | |
| g_print ("Running...\n"); | |
| g_main_loop_run (loop); | |
| /* Out of the main loop, clean up nicely */ | |
| g_print ("Returned, stopping playback\n"); | |
| gst_element_set_state (pipeline, GST_STATE_NULL); | |
| g_print ("Deleting pipeline\n"); | |
| gst_object_unref (GST_OBJECT (pipeline)); | |
| g_source_remove (bus_watch_id); | |
| g_main_loop_unref (loop); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment