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);
55 /* called when a new media pipeline is constructed. We can query the
56 * pipeline and configure our appsrc */
58 media_configure (GstRTSPMediaFactory * factory, GstRTSPMedia * media,
61 GstElement *element, *appsrc;
64 /* get the element used for providing the streams of the media */
65 element = gst_rtsp_media_get_element (media);
67 /* get our appsrc, we named it 'mysrc' with the name property */
68 appsrc = gst_bin_get_by_name_recurse_up (GST_BIN (element), "mysrc");
70 /* this instructs appsrc that we will be dealing with timed buffer */
71 gst_util_set_object_arg (G_OBJECT (appsrc), "format", "time");
72 /* configure the caps of the video */
73 g_object_set (G_OBJECT (appsrc), "caps",
74 gst_caps_new_simple ("video/x-raw",
75 "format", G_TYPE_STRING, "RGB16",
76 "width", G_TYPE_INT, 384,
77 "height", G_TYPE_INT, 288,
78 "framerate", GST_TYPE_FRACTION, 0, 1, NULL), NULL);
80 ctx = g_new0 (MyContext, 1);
83 /* make sure ther datais freed when the media is gone */
84 g_object_set_data_full (G_OBJECT (media), "my-extra-data", ctx,
85 (GDestroyNotify) g_free);
87 /* install the callback that will be called when a buffer is needed */
88 g_signal_connect (appsrc, "need-data", (GCallback) need_data, ctx);
89 gst_object_unref (appsrc);
90 gst_object_unref (element);
94 main (int argc, char *argv[])
97 GstRTSPServer *server;
98 GstRTSPMountPoints *mounts;
99 GstRTSPMediaFactory *factory;
101 gst_init (&argc, &argv);
103 loop = g_main_loop_new (NULL, FALSE);
105 /* create a server instance */
106 server = gst_rtsp_server_new ();
108 /* get the mount points for this server, every server has a default object
109 * that be used to map uri mount points to media factories */
110 mounts = gst_rtsp_server_get_mount_points (server);
112 /* make a media factory for a test stream. The default media factory can use
113 * gst-launch syntax to create pipelines.
114 * any launch line works as long as it contains elements named pay%d. Each
115 * element with pay%d names will be a stream */
116 factory = gst_rtsp_media_factory_new ();
117 gst_rtsp_media_factory_set_launch (factory,
118 "( appsrc name=mysrc ! videoconvert ! x264enc ! rtph264pay name=pay0 pt=96 )");
120 /* notify when our media is ready, This is called whenever someone asks for
121 * the media and a new pipeline with our appsrc is created */
122 g_signal_connect (factory, "media-configure", (GCallback) media_configure,
125 /* attach the test factory to the /test url */
126 gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
128 /* don't need the ref to the mounts anymore */
129 g_object_unref (mounts);
131 /* attach the server to the default maincontext */
132 gst_rtsp_server_attach (server, NULL);
135 g_print ("stream ready at rtsp://127.0.0.1:8554/test\n");
136 g_main_loop_run (loop);