i created a pipe filesrc ! decodebin ! appsink and set the appsink to
dont emit signals and block when reaches a maximum number of buffers, my
problem is that with this behaviour after pausing the pipeline i cant
set it to playing again, the stream seens to be locked. Before setting
to play again if i perform a seek (to any position) them the playing
will occur. Im using a separated thread to pull the data from the
appsink, i dont now if this is a bug of appsink, i cant see what im
doing wrong (i used gst-debug=5 and the pipe seens to get blocked when i
set it back to playing state). I would apreciate if someone could help,
here is the source code:
<br>
<br>
<br>
<br>#include <stdio.h>
<br>#include <glib.h>
<br>#include <gst/gst.h>
<br>#include <gst/app/gstappsink.h>
<br>
<br>static const int SLEEP_TIME_US = 5000;
<br>static gboolean run_thread = TRUE;
<br>
<br>static gboolean bus_call (GstBus *bus, GstMessage *msg, gpointer data)
<br>{
<br> GMainLoop *loop = (GMainLoop *) data;
<br>
<br> switch (GST_MESSAGE_TYPE (msg))
<br> {
<br>
<br> case GST_MESSAGE_EOS:
<br> {
<br> g_print ("Fim da stream\n");
<br> g_main_loop_quit (loop);
<br> break;
<br> }
<br>
<br> case GST_MESSAGE_ERROR:
<br> {
<br> gchar *debug;
<br> GError *error;
<br>
<br> gst_message_parse_error (msg, &error, &debug);
<br> g_free (debug);
<br>
<br> g_printerr ("Erro: %s\n", error->message);
<br> g_error_free (error);
<br>
<br> g_main_loop_quit (loop);
<br>
<br> break;
<br> }
<br>
<br> default:
<br> g_print("Tipo da mensagem [%d], Nome da mensagem [%s]\n",
GST_MESSAGE_TYPE (msg), GST_MESSAGE_TYPE_NAME(msg));
<br> break;
<br> }
return TRUE;
<br>}
<br>
<br>// ------------------ START PADS - ELEMENTS GENERIC FUNCTIONS
--------------- //
<br>
<br>static int connect_pads(GstPad* src, GstPad* sink)
<br>{
<br> gchar* src_name = NULL;
<br> gchar* sink_name = NULL;
<br> if(gst_pad_link(src, sink) != GST_PAD_LINK_OK){
<br> src_name = gst_pad_get_name(src);
<br> sink_name = gst_pad_get_name(sink);
<br> g_debug("Error linking [%s] to [%s]", src_name, sink_name);
<br> g_free(src_name);
<br> g_free(sink_name);
<br> return FALSE;
<br> }
<br> return TRUE;
<br>}
<br>
<br>// ------------------ END PADS - ELEMENTS GENERIC FUNCTIONS
--------------- //
<br>
<br>// ------------------ START DECODEBIN FUNCTIONS --------------------- //
<br>static void callback_type_found(GstElement *decodebin, GstPad *srcpad,
gboolean last, gpointer output)
<br>{
<br> GstElement* output_element = (GstElement*)output;
<br> GstPad* sinkpad = NULL;
<br>
if(output_element == NULL) {
<br> g_debug("callback_type_found: Error casting gpointer to
GstElement");
<br> return;
<br> }
<br>
<br> sinkpad = gst_element_get_static_pad(output_element, "sink");
if(sinkpad == NULL) {
<br> g_debug("callback_type_found: Error getting output_element sink
pad");
<br> return;
<br> }
<br>
<br> if(!connect_pads(srcpad, sinkpad)) {
<br> g_debug("callback_type_found: Cannot connect decodebin "
<br> "srcpad to
output_element sinkpad");
<br> g_object_unref (sinkpad);
<br> return;
<br> }
<br>
<br> g_debug("callback_type_found: Ended successfully");
<br> g_object_unref(sinkpad);
<br>}
<br>
<br>static void build_decodebin(GstElement* pipeline, GstElement* src,
GstElement* output, const gchar* caps)
<br>{
<br> GstElement* decoder = gst_element_factory_make ("decodebin", "decoder");
<br> g_debug("Trying to use decodebin to find type and decode media!");
<br> if(!decoder) {
<br> g_debug("build_decodebin: Error creating decodebin");
<br> return;
<br> }
<br>
<br> if(caps != NULL) {
<br> GstCaps* caps_obj = gst_caps_from_string(caps);
<br> if(caps_obj == NULL){
<br> g_debug("build_decodebin: Error creating caps[%s]", caps);
<br> return;
<br> }
<br> g_object_set(G_OBJECT (decoder), "sink-caps", caps_obj, NULL);
<br> }
<br>
<br> if(!gst_bin_add(GST_BIN (pipeline), decoder)) {
<br> g_debug("build_decodebin: Error inserting the decodebin on the
pipeline");
<br> return;
<br> }
<br>
<br> if(!gst_element_link(src, decoder)) {
<br> g_debug("build_decodebin: Error linking the source element to
the decoder");
<br> return;
<br> }
<br>
g_signal_connect (decoder, "new-decoded-pad",
G_CALLBACK(callback_type_found), output);
<br>}
<br>
<br>// ------------------ END DECODEBIN FUNCTIONS --------------------- //
<br>
<br>
<br>static gpointer pull_func(gpointer data)
<br>{
<br> GstAppSink* appsink = (GstAppSink*) data;
<br> GstBuffer* buffer = NULL;
<br>
while(run_thread){
<br> g_usleep(SLEEP_TIME_US);
<br> if(appsink){
<br> buffer = gst_app_sink_pull_buffer(appsink);
<br> if(buffer){
<br> g_debug("appsink: buffer timestamp(%lli) size(%d)",
<br> GST_BUFFER_TIMESTAMP(buffer),
<br> GST_BUFFER_SIZE(buffer));
<br> gst_buffer_unref(buffer);
<br> }else{
<br> g_warning("NULL BUFFER PULLED !!");
<br> }
<br> }else{
<br> g_warning("NULL APPSINK !!!");
<br> }
<br> }
<br>
<br> return NULL;
<br>}
<br>
<br>gboolean resume_time(gpointer data)
<br>{
<br> /* IF I DO THIS THE PLAYING WORKS AND APPSINK GIVES ME BUFFERS
<br> gint64 cur;
<br> GstFormat format =
gst_format_get_by_nick(gst_format_get_name(GST_FORMAT_TIME));
<br> gst_element_query_position(GST_ELEMENT (data), &format, &cur);
<br>
<br> if (!gst_element_seek_simple(GST_ELEMENT(data),
<br> GST_FORMAT_TIME,
<br> GST_SEEK_FLAG_FLUSH |
GST_SEEK_FLAG_KEY_UNIT, cur)) {
<br> g_print ("Seek failed!\n");
<br> return FALSE;
<br> }*/
<br>
<br> g_debug("PLAY \n");
<br> gst_element_set_state(GST_ELEMENT(data), GST_STATE_PLAYING);
<br>
<br> return FALSE;
<br>}
<br>
<br>gboolean pause_time(gpointer data)
<br>{
<br> g_debug("PAUSE \n");
<br> gst_element_set_state (GST_ELEMENT(data), GST_STATE_PAUSED);
<br> return FALSE;
<br>}
<br>
<br>int main(int argc, char *argv[])
<br>{
<br> GstElement *pipeline = NULL;
<br> GstElement *source = NULL;
<br> GstElement *appsink = NULL;
<br> GstBus *bus = NULL;
<br> GMainLoop *loop = NULL;
<br> GThread * pull_thread = NULL;
<br>
<br> if(argc < 2){
<br> g_debug("Usage [%s] [uri]", argv[0]);
<br> return -1;
<br> }
<br> gst_init (&argc, &argv);
<br> loop = g_main_loop_new (NULL, FALSE);
<br>
pipeline = gst_pipeline_new ("pause-block-pipe");
<br> source = gst_element_factory_make ("filesrc", "sourceFile");
<br> g_object_set (G_OBJECT(source), "location", argv[1], NULL);
<br> appsink = gst_element_factory_make ("appsink", "asink");
<br>
<br> if (!pipeline || !source || !appsink) {
<br> g_printerr ("Erro na criacao de um dos elementos do pipe.\n");
<br> return -1;
<br> }
<br> bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
<br> gst_bus_add_watch (bus, bus_call, loop);
<br> gst_object_unref (bus);
<br>
<br> gst_bin_add_many (GST_BIN(pipeline), source, appsink, NULL);
<br> build_decodebin(pipeline, source, appsink, NULL);
<br>
<br> gst_app_sink_set_max_buffers((GstAppSink*)appsink, 10);
<br> g_object_set (G_OBJECT(appsink), "sync", FALSE, NULL);
<br>
<br> pull_thread = g_thread_create(pull_func, appsink, TRUE, NULL);
<br> if(!pull_thread){
<br> g_debug("Error creating thread!!");
<br> return -1;
<br> }
<br> gst_element_set_state (pipeline, GST_STATE_PLAYING);
<br> g_timeout_add_seconds(2, pause_time, pipeline);
<br> g_timeout_add_seconds(6, resume_time, pipeline);
<br>
<br> /* Iterate */
<br> g_print ("Executando...\n");
<br> g_main_loop_run (loop);
<br>
<br> /* Out of the main loop, clean up nicely */
<br> g_print ("Terminando...\n");
<br> run_thread = FALSE;
<br> gst_element_set_state (pipeline, GST_STATE_NULL);
<br>
<br> g_print ("Deletando pipeline...\n");
<br> gst_object_unref (GST_OBJECT (pipeline));
<br>
return 1;
}
<br><br><br clear="all"><br>-- <br>Paulo Leonardo Benatto, patito<br>"the fear of being free makes you proud of being a slave"<br>