2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
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.
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.
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.
21 #include <gst/app/app.h>
23 #include <gst/rtsp-server/rtsp-server.h>
27 GstElement *generator_pipe;
28 GstElement *vid_appsink;
29 GstElement *vid_appsrc;
30 GstElement *aud_appsink;
31 GstElement *aud_appsrc;
34 /* called when we need to give data to an appsrc */
36 need_data (GstElement * appsrc, guint unused, MyContext * ctx)
41 if (appsrc == ctx->vid_appsrc)
42 sample = gst_app_sink_pull_sample (GST_APP_SINK (ctx->vid_appsink));
44 sample = gst_app_sink_pull_sample (GST_APP_SINK (ctx->aud_appsink));
47 GstBuffer *buffer = gst_sample_get_buffer (sample);
48 GstSegment *seg = gst_sample_get_segment (sample);
49 GstClockTime pts, dts;
51 /* Convert the PTS/DTS to running time so they start from 0 */
52 pts = GST_BUFFER_PTS (buffer);
53 if (GST_CLOCK_TIME_IS_VALID (pts))
54 pts = gst_segment_to_running_time (seg, GST_FORMAT_TIME, pts);
56 dts = GST_BUFFER_DTS (buffer);
57 if (GST_CLOCK_TIME_IS_VALID (dts))
58 dts = gst_segment_to_running_time (seg, GST_FORMAT_TIME, dts);
61 /* Make writable so we can adjust the timestamps */
62 buffer = gst_buffer_copy (buffer);
63 GST_BUFFER_PTS (buffer) = pts;
64 GST_BUFFER_DTS (buffer) = dts;
65 g_signal_emit_by_name (appsrc, "push-buffer", buffer, &ret);
66 gst_buffer_unref (buffer);
69 /* we don't need the appsink sample anymore */
70 gst_sample_unref (sample);
75 ctx_free (MyContext * ctx)
77 gst_element_set_state (ctx->generator_pipe, GST_STATE_NULL);
79 gst_object_unref (ctx->generator_pipe);
80 gst_object_unref (ctx->vid_appsrc);
81 gst_object_unref (ctx->vid_appsink);
82 gst_object_unref (ctx->aud_appsrc);
83 gst_object_unref (ctx->aud_appsink);
88 /* called when a new media pipeline is constructed. We can query the
89 * pipeline and configure our appsrc */
91 media_configure (GstRTSPMediaFactory * factory, GstRTSPMedia * media,
94 GstElement *element, *appsrc, *appsink;
98 ctx = g_new0 (MyContext, 1);
99 /* This pipeline generates H264 video and PCM audio. The appsinks are kept small so that if delivery is slow,
100 * encoded buffers are dropped as needed. There's slightly more buffers (32) allowed for audio */
101 ctx->generator_pipe =
103 ("videotestsrc is-live=true ! x264enc speed-preset=superfast tune=zerolatency ! h264parse ! appsink name=vid max-buffers=1 drop=true "
104 "audiotestsrc is-live=true ! appsink name=aud max-buffers=32 drop=true",
107 /* make sure the data is freed when the media is gone */
108 g_object_set_data_full (G_OBJECT (media), "rtsp-extra-data", ctx,
109 (GDestroyNotify) ctx_free);
111 /* get the element (bin) used for providing the streams of the media */
112 element = gst_rtsp_media_get_element (media);
114 /* Find the 2 app sources (video / audio), and configure them, connect to the
115 * signals to request data */
116 /* configure the caps of the video */
117 caps = gst_caps_new_simple ("video/x-h264",
118 "stream-format", G_TYPE_STRING, "byte-stream",
119 "alignment", G_TYPE_STRING, "au",
120 "width", G_TYPE_INT, 384, "height", G_TYPE_INT, 288,
121 "framerate", GST_TYPE_FRACTION, 15, 1, NULL);
122 ctx->vid_appsrc = appsrc =
123 gst_bin_get_by_name_recurse_up (GST_BIN (element), "videosrc");
124 ctx->vid_appsink = appsink =
125 gst_bin_get_by_name (GST_BIN (ctx->generator_pipe), "vid");
126 gst_util_set_object_arg (G_OBJECT (appsrc), "format", "time");
127 g_object_set (G_OBJECT (appsrc), "caps", caps, NULL);
128 g_object_set (G_OBJECT (appsink), "caps", caps, NULL);
129 /* install the callback that will be called when a buffer is needed */
130 g_signal_connect (appsrc, "need-data", (GCallback) need_data, ctx);
131 gst_caps_unref (caps);
133 caps = gst_caps_new_simple ("audio/x-raw", "format", G_TYPE_STRING, "S24BE",
134 "layout", G_TYPE_STRING, "interleaved", "rate", G_TYPE_INT, 48000,
135 "channels", G_TYPE_INT, 2, NULL);
136 ctx->aud_appsrc = appsrc =
137 gst_bin_get_by_name_recurse_up (GST_BIN (element), "audiosrc");
138 ctx->aud_appsink = appsink =
139 gst_bin_get_by_name (GST_BIN (ctx->generator_pipe), "aud");
140 gst_util_set_object_arg (G_OBJECT (appsrc), "format", "time");
141 g_object_set (G_OBJECT (appsrc), "caps", caps, NULL);
142 g_object_set (G_OBJECT (appsink), "caps", caps, NULL);
143 g_signal_connect (appsrc, "need-data", (GCallback) need_data, ctx);
144 gst_caps_unref (caps);
146 gst_element_set_state (ctx->generator_pipe, GST_STATE_PLAYING);
147 gst_object_unref (element);
151 main (int argc, char *argv[])
154 GstRTSPServer *server;
155 GstRTSPMountPoints *mounts;
156 GstRTSPMediaFactory *factory;
158 gst_init (&argc, &argv);
160 loop = g_main_loop_new (NULL, FALSE);
162 /* create a server instance */
163 server = gst_rtsp_server_new ();
165 /* get the mount points for this server, every server has a default object
166 * that be used to map uri mount points to media factories */
167 mounts = gst_rtsp_server_get_mount_points (server);
169 /* make a media factory for a test stream. The default media factory can use
170 * gst-launch syntax to create pipelines.
171 * any launch line works as long as it contains elements named pay%d. Each
172 * element with pay%d names will be a stream */
173 factory = gst_rtsp_media_factory_new ();
174 gst_rtsp_media_factory_set_launch (factory,
175 "( appsrc name=videosrc ! h264parse ! rtph264pay name=pay0 pt=96 "
176 " appsrc name=audiosrc ! audioconvert ! rtpL24pay name=pay1 pt=97 )");
178 /* notify when our media is ready, This is called whenever someone asks for
179 * the media and a new pipeline with our appsrc is created */
180 g_signal_connect (factory, "media-configure", (GCallback) media_configure,
183 /* attach the test factory to the /test url */
184 gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
186 /* don't need the ref to the mounts anymore */
187 g_object_unref (mounts);
189 /* attach the server to the default maincontext */
190 gst_rtsp_server_attach (server, NULL);
193 g_print ("stream ready at rtsp://127.0.0.1:8554/test\n");
194 g_main_loop_run (loop);