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>
24 #define DEFAULT_RTSP_PORT "8554"
26 static char *port = (char *) DEFAULT_RTSP_PORT;
28 static GOptionEntry entries[] = {
29 {"port", 'p', 0, G_OPTION_ARG_STRING, &port,
30 "Port to listen on (default: " DEFAULT_RTSP_PORT ")", "PORT"},
34 /* called when a stream has received an RTCP packet from the client */
36 on_ssrc_active (GObject * session, GObject * source, GstRTSPMedia * media)
40 GST_INFO ("source %p in session %p is active", source, session);
42 g_object_get (source, "stats", &stats, NULL);
46 sstr = gst_structure_to_string (stats);
47 g_print ("structure: %s\n", sstr);
50 gst_structure_free (stats);
55 on_sender_ssrc_active (GObject * session, GObject * source,
60 GST_INFO ("source %p in session %p is active", source, session);
62 g_object_get (source, "stats", &stats, NULL);
66 sstr = gst_structure_to_string (stats);
67 g_print ("Sender stats:\nstructure: %s\n", sstr);
70 gst_structure_free (stats);
74 /* signal callback when the media is prepared for streaming. We can get the
75 * session manager for each of the streams and connect to some signals. */
77 media_prepared_cb (GstRTSPMedia * media)
81 n_streams = gst_rtsp_media_n_streams (media);
83 GST_INFO ("media %p is prepared and has %u streams", media, n_streams);
85 for (i = 0; i < n_streams; i++) {
86 GstRTSPStream *stream;
89 stream = gst_rtsp_media_get_stream (media, i);
93 session = gst_rtsp_stream_get_rtpsession (stream);
94 GST_INFO ("watching session %p on stream %u", session, i);
96 g_signal_connect (session, "on-ssrc-active",
97 (GCallback) on_ssrc_active, media);
98 g_signal_connect (session, "on-sender-ssrc-active",
99 (GCallback) on_sender_ssrc_active, media);
104 media_configure_cb (GstRTSPMediaFactory * factory, GstRTSPMedia * media)
106 /* connect our prepared signal so that we can see when this media is
107 * prepared for streaming */
108 g_signal_connect (media, "prepared", (GCallback) media_prepared_cb, factory);
112 main (int argc, char *argv[])
115 GstRTSPServer *server;
116 GstRTSPMountPoints *mounts;
117 GstRTSPMediaFactory *factory;
118 GOptionContext *optctx;
119 GError *error = NULL;
122 optctx = g_option_context_new ("<filename.mp4> - Test RTSP Server, MP4");
123 g_option_context_add_main_entries (optctx, entries, NULL);
124 g_option_context_add_group (optctx, gst_init_get_option_group ());
125 if (!g_option_context_parse (optctx, &argc, &argv, &error)) {
126 g_printerr ("Error parsing options: %s\n", error->message);
127 g_option_context_free (optctx);
128 g_clear_error (&error);
133 g_print ("%s\n", g_option_context_get_help (optctx, TRUE, NULL));
136 g_option_context_free (optctx);
138 loop = g_main_loop_new (NULL, FALSE);
140 /* create a server instance */
141 server = gst_rtsp_server_new ();
142 g_object_set (server, "service", port, NULL);
144 /* get the mount points for this server, every server has a default object
145 * that be used to map uri mount points to media factories */
146 mounts = gst_rtsp_server_get_mount_points (server);
148 str = g_strdup_printf ("( "
149 "filesrc location=\"%s\" ! qtdemux name=d "
150 "d. ! queue ! rtph264pay pt=96 name=pay0 "
151 "d. ! queue ! rtpmp4apay pt=97 name=pay1 " ")", argv[1]);
153 /* make a media factory for a test stream. The default media factory can use
154 * gst-launch syntax to create pipelines.
155 * any launch line works as long as it contains elements named pay%d. Each
156 * element with pay%d names will be a stream */
157 factory = gst_rtsp_media_factory_new ();
158 gst_rtsp_media_factory_set_launch (factory, str);
159 g_signal_connect (factory, "media-configure", (GCallback) media_configure_cb,
163 /* attach the test factory to the /test url */
164 gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
166 /* don't need the ref to the mapper anymore */
167 g_object_unref (mounts);
169 /* attach the server to the default maincontext */
170 gst_rtsp_server_attach (server, NULL);
173 g_print ("stream ready at rtsp://127.0.0.1:%s/test\n", port);
174 g_main_loop_run (loop);