GstAppSrc and GstRTSPServer

rezils niko.867 at gmail.com
Tue Apr 21 14:55:41 PDT 2015


I'm sorry, I've just seen your post. 
It's been a while since I worked on this, anyway here is what I used at that
time:

#include <stdio.h>
#include <stdlib.h>
#include <gst/gst.h>
#include <gst/app/gstappsrc.h>
#include <gst/rtsp-server/rtsp-server.h>

typedef struct {
    GstElement* pipeline;
    GstAppSrc* src;
    GMainLoop* loop;
    guint sourceid;
    FILE* file;
} gst_app_t;

gst_app_t gst_app;
guint8* imageData;
gint imageSize;

gst_app_t* app = &gst_app;
GstBus* bus;
GstStateChangeReturn state_ret;

GstRTSPServer* server;
GstRTSPMediaMapping* mapping;
GstRTSPMediaFactory* factory;

GstRTSPUrl* url;
GstRTSPMedia* media;

static gboolean read_data (gst_app_t* app);

int main(int argc, char* argv[])
{
    // Open a JPEG image
    app->file = fopen ("asd.JPG", "r");
    g_assert (app->file);

    // Get image size
    fseek (app->file, 0L, SEEK_END);
    imageSize = ftell (app->file);
    fseek (app->file, 0L, SEEK_SET);

    // Read image data
    imageData = (guint8*)g_malloc (imageSize);
    g_assert (imageData);
    fread (imageData, 1, imageSize, app->file);

    // Init gstreamer
    gst_init (&argc, &argv);

    // Create the main loop
    app->loop = g_main_loop_new (NULL, FALSE);

    // Create a server instance 
    server = gst_rtsp_server_new();
    gst_rtsp_server_set_address (server, "0.0.0.0");
    gst_rtsp_server_set_service (server, "8554");

    // Create a factory
    factory = gst_rtsp_media_factory_new();
    gst_rtsp_media_factory_set_shared (factory, TRUE);
    gst_rtsp_media_factory_set_eos_shutdown (factory, TRUE);   
    
    // Set launch pipeline
    // (Here I provided two of them, choose between JPEG, MPEG4 and H264
encoding as you prefer)
//     gst_rtsp_media_factory_set_launch (factory, "( appsrc is-live=true
do-timestamp=true name=mysource ! decodebin ! jpegenc ! rtpjpegpay name=pay0
)"); 
//     gst_rtsp_media_factory_set_launch (factory, "( appsrc is-live=true
do-timestamp=true name=mysource ! decodebin ! ffenc_mpeg4 ! rtpmp4vpay
send-config=true pt=96 name=pay0 )"); 
    gst_rtsp_media_factory_set_launch (factory, "( appsrc name=mysource
is-live=true do-timestamp=true ! jpegparse ! decodebin ! x264enc
tune=zerolatency ! rtph264pay name=pay0 )");

    // Construct a custom media to retrieve the app pipeline
    url = (GstRTSPUrl*) malloc (sizeof (GstRTSPUrl));
    gst_rtsp_url_parse ("rtsp://0.0.0.0:8554/media", &url);
    media = gst_rtsp_media_factory_construct (factory, url);
    gst_rtsp_media_set_reusable (media, TRUE);
    media->is_live = TRUE;
    app->pipeline = media->pipeline;
    g_assert (app->pipeline);    
       
    // Get the GstAppSrc pointer
    app->src = (GstAppSrc*) gst_bin_get_by_name (GST_BIN (app->pipeline),
"mysource");
    g_assert (app->src);
    g_assert (GST_IS_APP_SRC (app->src));  

    // Get media mapping and attach the test factory to the /media url
    mapping = gst_rtsp_server_get_media_mapping (server);
    gst_rtsp_media_mapping_add_factory (mapping, "/media", factory);    
    g_object_unref (mapping);       // Don't need the ref to the mapper
anymore 

    // Attach the server to the default maincontext 
    gst_rtsp_server_attach (server, NULL);

    // Add a timeout event
    g_timeout_add (100, (GSourceFunc) read_data, app);

    // Run main loop
    printf ("Running main loop\n");
    g_main_loop_run (app->loop);

    return 0;
}

static gboolean read_data (gst_app_t* app) {
    GstBuffer* buffer;
    guint8* ptr;
    GstFlowReturn ret;
    
    if (media->active == 0) {               
        media = gst_rtsp_media_factory_construct (factory, url);
        gst_rtsp_media_set_reusable (media, TRUE);
        media->is_live = TRUE;
        app->pipeline = media->pipeline;
        g_assert (app->pipeline);  
        
        app->src = (GstAppSrc*) gst_bin_get_by_name (GST_BIN
(app->pipeline), "mysource");
    }
    
    // Allocate a buffer for gstreamer
    ptr = (guint8*) g_malloc (imageSize);    
    g_assert (ptr);

    // Copy image data to the gstreamer buffer
    memcpy ((void*) ptr, (void*) imageData, imageSize);

    // Set up the gstreamer buffer
    buffer = gst_buffer_new();
    GST_BUFFER_MALLOCDATA (buffer) = ptr;
    GST_BUFFER_SIZE (buffer) = imageSize;
    GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);

    // Push the buffer
    ret = gst_app_src_push_buffer (app->src, buffer);

    if (ret !=  GST_FLOW_OK) {
        g_debug ("gst_app_src_push_buffer returned %d for %d bytes \n", ret,
imageSize);
        return FALSE;
    }

    return TRUE;
}



--
View this message in context: http://gstreamer-devel.966125.n4.nabble.com/GstAppSrc-and-GstRTSPServer-tp4656822p4671648.html
Sent from the GStreamer-devel mailing list archive at Nabble.com.


More information about the gstreamer-devel mailing list