Guest User

Gstreamer test-appsrc

a guest
Mar 16th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.29 KB | None | 0 0
  1. #include <gst/gst.h>
  2. #include <gst/app/gstappsink.h>
  3. #include <gst/app/gstappsrc.h>
  4. #include <gst/rtsp-server/rtsp-server.h>
  5.  
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9.  
  10. typedef struct _App App;
  11. struct _App
  12. {
  13.     GstElement *videosink;
  14. };
  15. App s_app;
  16.  
  17. typedef struct {
  18.     App *glblapp;
  19.     GstClockTime timestamp;
  20. } Context;
  21.  
  22. const gchar *videocaps = "video/x-raw, format=(string)I420, width=(int)1280, height=(int)1024, interlace-mode=(string)progressive, pixel-aspect-ratio=(fraction)1/1, chroma-site=(string)mpeg2, colorimetry=(string)bt709, framerate=(fraction)10/1";
  23.  
  24. // RTSP server signal and event handler
  25. static void
  26. need_data (GstElement *appsrc, guint unused, Context *ctx)
  27. {
  28.     GstFlowReturn ret;
  29.     GstSample *sample = gst_app_sink_pull_sample (GST_APP_SINK(ctx->glblapp->videosink));
  30.     if (sample != NULL) {
  31.         GstBuffer *buffer = gst_sample_get_buffer(sample);
  32.         gst_sample_unref (sample);
  33.         GST_BUFFER_PTS(buffer) = ctx->timestamp;
  34.         GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale_int (1, GST_SECOND, 25);
  35.         ctx->timestamp += GST_BUFFER_DURATION (buffer);
  36.         g_signal_emit_by_name(appsrc, "push-buffer", buffer, &ret);
  37.     }
  38. }
  39.  
  40. static void
  41. media_configure (GstRTSPMediaFactory *factory, GstRTSPMedia *media, App *app)
  42. {
  43.     Context *ctx;
  44.     GstElement *pipeline;
  45.     GstElement *appsrc;
  46.     pipeline = gst_rtsp_media_get_element(media);
  47.     appsrc = gst_bin_get_by_name_recurse_up (GST_BIN (pipeline), "mysrc");
  48.     gst_rtsp_media_set_reusable(media, TRUE);
  49.     gst_util_set_object_arg (G_OBJECT (appsrc), "format", "time");
  50.     g_object_set (G_OBJECT (appsrc), "caps", gst_caps_from_string(videocaps), NULL);
  51.     g_object_set(G_OBJECT(appsrc), "max-bytes",
  52.         gst_app_src_get_max_bytes(GST_APP_SRC(appsrc)), NULL);
  53.     ctx = g_new0 (Context, 1);
  54.     ctx->glblapp = app;
  55.     ctx->timestamp = 0;
  56.     g_signal_connect (appsrc, "need-data", (GCallback) need_data, ctx);
  57. }
  58.  
  59. // Bus message handler
  60. gboolean
  61. bus_callback(GstBus *bus, GstMessage *msg, gpointer data)
  62. {
  63.     GstElement *pipeline = GST_ELEMENT(data);
  64.     switch (GST_MESSAGE_TYPE(msg)) {
  65.     case GST_MESSAGE_EOS:
  66.         if (!gst_element_seek(pipeline,
  67.             1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
  68.             GST_SEEK_TYPE_SET, 1000000000,
  69.             GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE)) {
  70.             g_message("Seek failed!");
  71.         }
  72.         break;
  73.     default:
  74.         break;
  75.     }
  76.     return TRUE;
  77. }
  78.  
  79. gint
  80. main (gint argc, gchar *argv[])
  81. {
  82.     App *app = &s_app;
  83.     GstBus *bus;
  84.     GstRTSPServer *server;
  85.     GstRTSPMediaFactory *factory;
  86.     GstRTSPMountPoints *mountpoints;
  87.  
  88.     gst_init (&argc, &argv);
  89.     GMainLoop *loop = g_main_loop_new (NULL, FALSE);
  90.  
  91.     // Playbin, setup and configuration
  92.     GstElement *playbin = gst_element_factory_make ("playbin", "play");
  93.     app->videosink = gst_element_factory_make ("appsink", "video_sink");
  94.     g_object_set (G_OBJECT (app->videosink), "emit-signals", FALSE, "sync", TRUE, NULL);
  95.     g_object_set (G_OBJECT (playbin), "video-sink", app->videosink, NULL);
  96.     gst_app_sink_set_drop(GST_APP_SINK (app->videosink), TRUE);
  97.     gst_app_sink_set_max_buffers(GST_APP_SINK (app->videosink), 1);  
  98.     bus = gst_pipeline_get_bus (GST_PIPELINE (playbin));
  99.     gst_bus_add_watch (bus, bus_callback, playbin);
  100.     g_object_set (G_OBJECT (playbin), "uri", "file:///dev/disk/mounted/sda1/tanguy/gst-rtsp-server/examples/small.mp4", NULL);
  101.     gst_element_set_state (playbin, GST_STATE_PLAYING);
  102.  
  103.     // RTSP server, setup and configuration
  104.     server = gst_rtsp_server_new();
  105.     mountpoints = gst_rtsp_server_get_mount_points(server);
  106.     factory = gst_rtsp_media_factory_new();
  107.     gst_rtsp_media_factory_set_shared(factory, TRUE);
  108.     gst_rtsp_media_factory_set_launch (factory,
  109.         "( appsrc name=mysrc ! rtph264pay name=pay0 pt=96 )");
  110.     g_signal_connect (factory, "media-configure", (GCallback) media_configure, app);
  111.     gst_rtsp_mount_points_add_factory (mountpoints, "/test", factory);
  112.     g_object_unref(mountpoints);
  113.     gst_rtsp_server_attach (server, NULL);
  114.     g_print("RTSP Server started...");
  115.     g_main_loop_run (loop);
  116.  
  117.     // Clean up
  118.     gst_element_set_state (playbin, GST_STATE_NULL);
  119.     gst_object_unref (bus);
  120.     return 0;
  121. }
Add Comment
Please, Sign In to add comment