rtsp-media: Unblock all streams
[platform/upstream/gstreamer.git] / examples / test-appsrc.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include <gst/gst.h>
21
22 #include <gst/rtsp-server/rtsp-server.h>
23
24 typedef struct
25 {
26   gboolean white;
27   GstClockTime timestamp;
28 } MyContext;
29
30 /* called when we need to give data to appsrc */
31 static void
32 need_data (GstElement * appsrc, guint unused, MyContext * ctx)
33 {
34   GstBuffer *buffer;
35   guint size;
36   GstFlowReturn ret;
37
38   size = 385 * 288 * 2;
39
40   buffer = gst_buffer_new_allocate (NULL, size, NULL);
41
42   /* this makes the image black/white */
43   gst_buffer_memset (buffer, 0, ctx->white ? 0xff : 0x0, size);
44
45   ctx->white = !ctx->white;
46
47   /* increment the timestamp every 1/2 second */
48   GST_BUFFER_PTS (buffer) = ctx->timestamp;
49   GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale_int (1, GST_SECOND, 2);
50   ctx->timestamp += GST_BUFFER_DURATION (buffer);
51
52   g_signal_emit_by_name (appsrc, "push-buffer", buffer, &ret);
53   gst_buffer_unref (buffer);
54 }
55
56 /* called when a new media pipeline is constructed. We can query the
57  * pipeline and configure our appsrc */
58 static void
59 media_configure (GstRTSPMediaFactory * factory, GstRTSPMedia * media,
60     gpointer user_data)
61 {
62   GstElement *element, *appsrc;
63   MyContext *ctx;
64
65   /* get the element used for providing the streams of the media */
66   element = gst_rtsp_media_get_element (media);
67
68   /* get our appsrc, we named it 'mysrc' with the name property */
69   appsrc = gst_bin_get_by_name_recurse_up (GST_BIN (element), "mysrc");
70
71   /* this instructs appsrc that we will be dealing with timed buffer */
72   gst_util_set_object_arg (G_OBJECT (appsrc), "format", "time");
73   /* configure the caps of the video */
74   g_object_set (G_OBJECT (appsrc), "caps",
75       gst_caps_new_simple ("video/x-raw",
76           "format", G_TYPE_STRING, "RGB16",
77           "width", G_TYPE_INT, 384,
78           "height", G_TYPE_INT, 288,
79           "framerate", GST_TYPE_FRACTION, 0, 1, NULL), NULL);
80
81   ctx = g_new0 (MyContext, 1);
82   ctx->white = FALSE;
83   ctx->timestamp = 0;
84   /* make sure ther datais freed when the media is gone */
85   g_object_set_data_full (G_OBJECT (media), "my-extra-data", ctx,
86       (GDestroyNotify) g_free);
87
88   /* install the callback that will be called when a buffer is needed */
89   g_signal_connect (appsrc, "need-data", (GCallback) need_data, ctx);
90   gst_object_unref (appsrc);
91   gst_object_unref (element);
92 }
93
94 int
95 main (int argc, char *argv[])
96 {
97   GMainLoop *loop;
98   GstRTSPServer *server;
99   GstRTSPMountPoints *mounts;
100   GstRTSPMediaFactory *factory;
101
102   gst_init (&argc, &argv);
103
104   loop = g_main_loop_new (NULL, FALSE);
105
106   /* create a server instance */
107   server = gst_rtsp_server_new ();
108
109   /* get the mount points for this server, every server has a default object
110    * that be used to map uri mount points to media factories */
111   mounts = gst_rtsp_server_get_mount_points (server);
112
113   /* make a media factory for a test stream. The default media factory can use
114    * gst-launch syntax to create pipelines.
115    * any launch line works as long as it contains elements named pay%d. Each
116    * element with pay%d names will be a stream */
117   factory = gst_rtsp_media_factory_new ();
118   gst_rtsp_media_factory_set_launch (factory,
119       "( appsrc name=mysrc ! videoconvert ! x264enc ! rtph264pay name=pay0 pt=96 )");
120
121   /* notify when our media is ready, This is called whenever someone asks for
122    * the media and a new pipeline with our appsrc is created */
123   g_signal_connect (factory, "media-configure", (GCallback) media_configure,
124       NULL);
125
126   /* attach the test factory to the /test url */
127   gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
128
129   /* don't need the ref to the mounts anymore */
130   g_object_unref (mounts);
131
132   /* attach the server to the default maincontext */
133   gst_rtsp_server_attach (server, NULL);
134
135   /* start serving */
136   g_print ("stream ready at rtsp://127.0.0.1:8554/test\n");
137   g_main_loop_run (loop);
138
139   return 0;
140 }