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.
22 #include <gst/rtsp-server/rtsp-server.h>
27 GstClockTime timestamp;
30 /* called when we need to give data to appsrc */
32 need_data (GstElement * appsrc, guint unused, MyContext * ctx)
40 buffer = gst_buffer_new_allocate (NULL, size, NULL);
42 /* this makes the image black/white */
43 gst_buffer_memset (buffer, 0, ctx->white ? 0xff : 0x0, size);
45 ctx->white = !ctx->white;
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);
52 g_signal_emit_by_name (appsrc, "push-buffer", buffer, &ret);
53 gst_buffer_unref (buffer);
56 /* called when a new media pipeline is constructed. We can query the
57 * pipeline and configure our appsrc */
59 media_configure (GstRTSPMediaFactory * factory, GstRTSPMedia * media,
62 GstElement *element, *appsrc;
65 /* get the element used for providing the streams of the media */
66 element = gst_rtsp_media_get_element (media);
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");
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);
81 ctx = g_new0 (MyContext, 1);
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);
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);
95 main (int argc, char *argv[])
98 GstRTSPServer *server;
99 GstRTSPMountPoints *mounts;
100 GstRTSPMediaFactory *factory;
102 gst_init (&argc, &argv);
104 loop = g_main_loop_new (NULL, FALSE);
106 /* create a server instance */
107 server = gst_rtsp_server_new ();
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);
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 )");
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,
126 /* attach the test factory to the /test url */
127 gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
129 /* don't need the ref to the mounts anymore */
130 g_object_unref (mounts);
132 /* attach the server to the default maincontext */
133 gst_rtsp_server_attach (server, NULL);
136 g_print ("stream ready at rtsp://127.0.0.1:8554/test\n");
137 g_main_loop_run (loop);