Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <gst/gst.h>
- #include <stdio.h>
- #include <string.h>
- #include <gst/app/gstappsink.h>
- GMainLoop *loop;
- GstElement *pipeline;
- GstElement *uridecodebin;
- GstElement *appsink;
- GstPad *sinkpad;
- GThread *loop_thread;
- static gboolean _gst_on_bus_message (GstBus * bus, GstMessage * message, void * data)
- {
- switch (GST_MESSAGE_TYPE (message))
- {
- case GST_MESSAGE_STATE_CHANGED:
- {
- gchar *name;
- GstState old_state, new_state;
- gst_message_parse_state_changed (message, &old_state, &new_state, NULL);
- name = gst_object_get_name (message->src);
- printf("state change: %d -> %d (object %s)\r\n", old_state, new_state, name);
- g_free (name);
- break;
- }
- case GST_MESSAGE_EOS:
- {
- printf("Finished playback\n");
- break;
- }
- case GST_MESSAGE_ERROR:
- {
- GError *err = NULL;
- gchar *dbg_info = NULL;
- gst_message_parse_error (message, &err, &dbg_info);
- printf("BUSMSG ERROR from element %s: %s\r\n", GST_OBJECT_NAME (message->src), err->message);
- printf("Debugging info: %s\r\n", (dbg_info) ? dbg_info : "none");
- g_error_free (err);
- g_free (dbg_info);
- break;
- }
- case GST_MESSAGE_BUFFERING:
- printf ("GST_MESSAGE_BUFFERING\r\n");
- break;
- default:
- printf ("other bus msg %d %s\r\n", GST_MESSAGE_TYPE (message), gst_message_type_get_name(GST_MESSAGE_TYPE (message)));
- break;
- }
- return TRUE;
- }
- static GstFlowReturn _gst_on_new_sample (GstElement * elt, void * data)
- {
- GstSample *sample;
- printf ("on_new_sample\r\n");
- sample = gst_app_sink_pull_sample (GST_APP_SINK (elt));
- gst_sample_unref (sample);
- return GST_FLOW_OK;
- }
- static gboolean _gst_on_pad_added (GstElement *gstdecodebin, GstPad *pad, gpointer user_data)
- {
- GstCaps *caps;
- gchar *caps_str;
- gst_object_ref (pad);
- caps = gst_pad_query_caps (pad, NULL);
- caps_str = gst_caps_to_string (caps);
- printf ("pad added, caps: %s\r\n", caps_str);
- g_free (caps_str);
- gst_caps_unref (caps);
- appsink = gst_element_factory_make ("appsink", NULL);
- gst_bin_add (GST_BIN(pipeline), appsink);
- g_signal_connect (appsink, "new-sample", G_CALLBACK (_gst_on_new_sample), NULL);
- g_object_set (G_OBJECT (appsink), "max-buffers", 1, "sync", FALSE, "emit-signals", TRUE, NULL);
- sinkpad = gst_element_get_compatible_pad (appsink, pad, NULL);
- if (gst_pad_link (pad,sinkpad) == GST_PAD_LINK_OK)
- {
- printf ("link src->sinkpad ok\r\n");
- }
- else
- {
- printf ("link src->sinkpad FAIL\r\n");
- }
- return TRUE;
- }
- static gpointer _gst_loop_thread_func (gpointer data)
- {
- g_main_loop_run (loop);
- return NULL;
- }
- static void _gst_setup (char *uri)
- {
- GstBus *bus;
- loop = g_main_loop_new (NULL, FALSE);
- loop_thread = g_thread_new ("myloop", _gst_loop_thread_func, NULL);
- pipeline = gst_pipeline_new ("mypipeline");
- uridecodebin = gst_element_factory_make ("uridecodebin", "myuridecodebin");
- gst_bin_add (GST_BIN(pipeline), uridecodebin);
- // If I create the appsink here, it works
- //appsink = gst_element_factory_make ("appsink", NULL);
- //gst_bin_add (GST_BIN(pipeline), appsink);
- bus = gst_element_get_bus (pipeline);
- gst_bus_add_watch (bus, (GstBusFunc) _gst_on_bus_message, NULL);
- gst_object_unref (bus);
- g_signal_connect (uridecodebin, "pad-added", G_CALLBACK (_gst_on_pad_added), NULL);
- g_object_set (G_OBJECT(uridecodebin), "uri", uri, NULL);
- g_object_set (G_OBJECT (uridecodebin), "caps", gst_caps_from_string ("audio/mpeg, mpegversion = (int) { 1, 2 };"), NULL);
- }
- int main (int argc, char *argv[])
- {
- gst_init (&argc, &argv);
- _gst_setup (argv[1]);
- sleep (2);
- printf ("\r\ngoing to PAUSED now\r\n");
- gst_element_set_state (pipeline, GST_STATE_PAUSED);
- sleep (2);
- printf ("\r\ngoing to PLAYING now\r\n");
- gst_element_set_state (pipeline, GST_STATE_PLAYING);
- sleep(10000);
- return 0;
- }
Add Comment
Please, Sign In to add comment