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);
54 /* signal callback when the media is prepared for streaming. We can get the
55 * session manager for each of the streams and connect to some signals. */
57 media_prepared_cb (GstRTSPMedia * media)
61 n_streams = gst_rtsp_media_n_streams (media);
63 GST_INFO ("media %p is prepared and has %u streams", media, n_streams);
65 for (i = 0; i < n_streams; i++) {
66 GstRTSPStream *stream;
69 stream = gst_rtsp_media_get_stream (media, i);
73 session = gst_rtsp_stream_get_rtpsession (stream);
74 GST_INFO ("watching session %p on stream %u", session, i);
76 g_signal_connect (session, "on-ssrc-active",
77 (GCallback) on_ssrc_active, media);
82 media_configure_cb (GstRTSPMediaFactory * factory, GstRTSPMedia * media)
84 /* connect our prepared signal so that we can see when this media is
85 * prepared for streaming */
86 g_signal_connect (media, "prepared", (GCallback) media_prepared_cb, factory);
90 main (int argc, char *argv[])
93 GstRTSPServer *server;
94 GstRTSPMountPoints *mounts;
95 GstRTSPMediaFactory *factory;
96 GOptionContext *optctx;
100 optctx = g_option_context_new ("<filename.mp4> - Test RTSP Server, MP4");
101 g_option_context_add_main_entries (optctx, entries, NULL);
102 g_option_context_add_group (optctx, gst_init_get_option_group ());
103 if (!g_option_context_parse (optctx, &argc, &argv, &error)) {
104 g_printerr ("Error parsing options: %s\n", error->message);
107 g_option_context_free (optctx);
109 loop = g_main_loop_new (NULL, FALSE);
111 /* create a server instance */
112 server = gst_rtsp_server_new ();
113 g_object_set (server, "service", port, NULL);
115 /* get the mount points for this server, every server has a default object
116 * that be used to map uri mount points to media factories */
117 mounts = gst_rtsp_server_get_mount_points (server);
119 str = g_strdup_printf ("( "
120 "filesrc location=%s ! qtdemux name=d "
121 "d. ! queue ! rtph264pay pt=96 name=pay0 "
122 "d. ! queue ! rtpmp4apay pt=97 name=pay1 " ")", argv[1]);
124 /* make a media factory for a test stream. The default media factory can use
125 * gst-launch syntax to create pipelines.
126 * any launch line works as long as it contains elements named pay%d. Each
127 * element with pay%d names will be a stream */
128 factory = gst_rtsp_media_factory_new ();
129 gst_rtsp_media_factory_set_launch (factory, str);
130 g_signal_connect (factory, "media-configure", (GCallback) media_configure_cb,
134 /* attach the test factory to the /test url */
135 gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
137 /* don't need the ref to the mapper anymore */
138 g_object_unref (mounts);
140 /* attach the server to the default maincontext */
141 gst_rtsp_server_attach (server, NULL);
144 g_print ("stream ready at rtsp://127.0.0.1:%s/test\n", port);
145 g_main_loop_run (loop);