How to play audio memory buffer to gstreamer without file?

wonderjason wonder741106 at gmail.com
Mon May 22 08:22:00 UTC 2017


When I have buffer queue ( buffer pointer and buffer size ), I want to use
them to playback. 
So, I don't have duration related information. 
How to play audio memory buffer to gstreamer without file? 

I am able play one audio file to pipeline as below: 

static void feed_data (GstElement * appsrc, guint size, App_local * app) 
{   
        GstBuffer *buffer;  guint len;  GstFlowReturn ret;   
        buffer = gst_buffer_new ();   
        if (app->offset >= app->length) {	/* we are EOS, send end-of-stream
*/	
                g_signal_emit_by_name (app->appsrc, "end-of-stream", &ret);
return;   
        }   
        
        /* read any amount of data, we are allowed to return less if we are
EOS */   
        len = CHUNK_SIZE;   
        if (app->offset + len > app->length)	
                len = app->length - app->offset;   

        GST_BUFFER_DATA (buffer) = app->data + app->offset;   
        GST_BUFFER_SIZE (buffer) = len;   
        GST_DEBUG ("feed buffer %p, offset %" G_GUINT64_FORMAT "-%u",
buffer,	 app->offset, len);   
        g_signal_emit_by_name (app->appsrc, "push-buffer", buffer, &ret);   
        gst_buffer_unref (buffer);  app->offset += len;  return; 
} 

static gboolean seek_data (GstElement * appsrc, guint64 position, App_local
* app) 
{   
        GST_DEBUG ("seek to offset %" G_GUINT64_FORMAT, position);   
        app->offset = position;  return TRUE; 
} 

cb_found_source_local (GObject * object, GObject * orig, GParamSpec * pspec,
App_local * app) 
{ 
        /* get a handle to the appsrc */ 
        g_object_get (orig, pspec->name, &app->appsrc, NULL); 

        g_object_set (app->appsrc, "size", (gint64) app->length, NULL); 
        gst_util_set_object_arg (G_OBJECT (app->appsrc), "stream-type",
"seekable"); 
        
        /* configure the appsrc, we will push a buffer to appsrc when it
needs more data */   
        g_signal_connect (app->appsrc, "need-data", G_CALLBACK (feed_data),
app);   

        g_signal_connect (app->appsrc, "seek-data", G_CALLBACK (seek_data),
app); 

        pthread_mutex_lock(&GstGlobalStateLocalFile); 
        app->SrcCreated= 1; 
        pthread_mutex_unlock(&GstGlobalStateLocalFile); 
} 

void *CreateGstPipeLocal(void *argv) 
{ 
        char *file_path = ""; 
        App_local *app = &s_app_local; 
        GError *error = NULL; 
        GstBus *bus; 
        memset (&s_app_local, 0, sizeof (s_app_local)); 
        app->offset = 0; 
        app->buffering = 0; 
        app->SrcCreated= 0; 
        app->isPlaying =0; 

        file_path = (char*)argv; 

        app->file = g_mapped_file_new (file_path, FALSE, &error); 
        if (error) {	
                printf ("failed to open file: %s\n", error->message);	
                g_error_free (error);	

                pthread_mutex_lock(&GstGlobalStateLocalFile); 
                memset(errorMSG_local,0,sizeof(errorMSG_local)); 
                strncpy(errorMSG_local,"Failed to open
file",sizeof(errorMSG_local)); 
                errNumber_local = FILE_MESSAGE_ERROR; 
                pthread_mutex_unlock(&GstGlobalStateLocalFile); 
                
                return NULL;   
        } 
        app->length = g_mapped_file_get_length (app->file); 
        app->data = (guint8 *) g_mapped_file_get_contents (app->file); 
        app->offset = 0; 

        /* create a mainloop to get messages */ 
        app->loop = g_main_loop_new (NULL, TRUE); 
        app->playbin = gst_element_factory_make ("playbin2", NULL); 
        g_assert (app->playbin); 

        /* add watch for messages */ 
        bus = gst_element_get_bus (app->playbin); 
        gst_bus_add_signal_watch (bus); 
        g_signal_connect (bus, "message", G_CALLBACK (cb_message_local),
app); 

        g_object_set (app->playbin, "uri", "appsrc://", NULL); 
        /* get notification when the source is created so that we get a
handle to it 
        * and can configure it */ 
        g_signal_connect (app->playbin, "deep-notify::source",
(GCallback)cb_found_source_local, app); 

        /* go to playing and wait in a mainloop. */ 
        gst_element_set_state (app->playbin, GST_STATE_PAUSED); 

                g_isPlaying_local = 0; 
        /* this mainloop is stopped when we receive an error or EOS */ 
        g_main_loop_run (app->loop); 
        if (app->playbin) { 
                //printf("%s %d Exit local GMain\n", __FUNCTION__,
__LINE__); 
                gst_element_set_state (app->playbin, GST_STATE_NULL); 
                //printf("%s %d Exit local GMain\n", __FUNCTION__,
__LINE__); 
        }else 
                printf("%d NULL playbin\n", __LINE__); 

        if (app->file !=NULL) 
                        g_mapped_file_unref (app->file); 

        gst_object_unref (bus); 

        g_main_loop_unref (app->loop); 
        memset (&s_app_local, 0, sizeof (s_app_local)); 
        g_isPlaying_local = 0; 

        printf("GstPipeLocal END\n"); 
        
        return NULL; 
} 



--
View this message in context: http://gstreamer-devel.966125.n4.nabble.com/How-to-play-audio-memory-buffer-to-gstreamer-without-file-tp4683063.html
Sent from the GStreamer-devel mailing list archive at Nabble.com.


More information about the gstreamer-devel mailing list