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 static gchar *htdigest_path = NULL;
25 static gchar *realm = NULL;
27 static GOptionEntry entries[] = {
28 {"htdigest-path", 'h', 0, G_OPTION_ARG_STRING, &htdigest_path,
29 "Path to an htdigest file to parse (default: None)", "PATH"},
30 {"realm", 'r', 0, G_OPTION_ARG_STRING, &realm,
31 "Authentication realm (default: None)", "REALM"},
37 remove_func (GstRTSPSessionPool * pool, GstRTSPSession * session,
38 GstRTSPServer * server)
40 return GST_RTSP_FILTER_REMOVE;
44 remove_sessions (GstRTSPServer * server)
46 GstRTSPSessionPool *pool;
48 g_print ("removing all sessions\n");
49 pool = gst_rtsp_server_get_session_pool (server);
50 gst_rtsp_session_pool_filter (pool,
51 (GstRTSPSessionPoolFilterFunc) remove_func, server);
52 g_object_unref (pool);
58 timeout (GstRTSPServer * server)
60 GstRTSPSessionPool *pool;
62 pool = gst_rtsp_server_get_session_pool (server);
63 gst_rtsp_session_pool_cleanup (pool);
64 g_object_unref (pool);
70 main (int argc, char *argv[])
73 GstRTSPServer *server;
74 GstRTSPMountPoints *mounts;
75 GstRTSPMediaFactory *factory;
78 GOptionContext *optctx;
81 optctx = g_option_context_new (NULL);
82 g_option_context_add_main_entries (optctx, entries, NULL);
83 g_option_context_add_group (optctx, gst_init_get_option_group ());
84 if (!g_option_context_parse (optctx, &argc, &argv, &error)) {
85 g_printerr ("Error parsing options: %s\n", error->message);
86 g_option_context_free (optctx);
87 g_clear_error (&error);
90 g_option_context_free (optctx);
92 loop = g_main_loop_new (NULL, FALSE);
94 /* create a server instance */
95 server = gst_rtsp_server_new ();
97 /* get the mounts for this server, every server has a default mapper object
98 * that be used to map uri mount points to media factories */
99 mounts = gst_rtsp_server_get_mount_points (server);
102 /* make a media factory for a test stream. The default media factory can use
103 * gst-launch syntax to create pipelines.
104 * any launch line works as long as it contains elements named pay%d. Each
105 * element with pay%d names will be a stream */
106 factory = gst_rtsp_media_factory_new ();
107 gst_rtsp_media_factory_set_launch (factory, "( "
108 "videotestsrc ! video/x-raw,width=352,height=288,framerate=15/1 ! "
109 "x264enc ! rtph264pay name=pay0 pt=96 "
110 "audiotestsrc ! audio/x-raw,rate=8000 ! "
111 "alawenc ! rtppcmapay name=pay1 pt=97 " ")");
112 /* attach the test factory to the /test url */
113 gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
115 /* allow user and admin to access this resource */
116 gst_rtsp_media_factory_add_role (factory, "user",
117 GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
118 GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, TRUE, NULL);
119 gst_rtsp_media_factory_add_role (factory, "admin",
120 GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
121 GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, TRUE, NULL);
122 /* admin2 can look at the media but not construct so he gets a
123 * 401 Unauthorized */
124 gst_rtsp_media_factory_add_role (factory, "admin2",
125 GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
126 GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, FALSE, NULL);
127 /* Anonymous user can do the same things as admin2 on this resource */
128 gst_rtsp_media_factory_add_role (factory, "anonymous",
129 GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
130 GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, FALSE, NULL);
132 /* make another factory */
133 factory = gst_rtsp_media_factory_new ();
134 gst_rtsp_media_factory_set_launch (factory, "( "
135 "videotestsrc ! video/x-raw,width=352,height=288,framerate=30/1 ! "
136 "x264enc ! rtph264pay name=pay0 pt=96 )");
137 /* attach the test factory to the /test url */
138 gst_rtsp_mount_points_add_factory (mounts, "/test2", factory);
140 /* allow admin2 to access this resource */
141 /* user and admin have no permissions so they can't even see the
142 * media and get a 404 Not Found */
143 gst_rtsp_media_factory_add_role (factory, "admin2",
144 GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
145 GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, TRUE, NULL);
147 /* don't need the ref to the mapper anymore */
148 g_object_unref (mounts);
150 /* make a new authentication manager */
151 auth = gst_rtsp_auth_new ();
152 gst_rtsp_auth_set_supported_methods (auth, GST_RTSP_AUTH_DIGEST);
154 /* make default token, it has no permissions */
156 gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
158 gst_rtsp_auth_set_default_token (auth, token);
159 gst_rtsp_token_unref (token);
161 /* make user token */
163 gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
165 gst_rtsp_auth_add_digest (auth, "user", "password", token);
166 gst_rtsp_token_unref (token);
170 gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
173 if (!gst_rtsp_auth_parse_htdigest (auth, htdigest_path, token)) {
174 g_printerr ("Could not parse htdigest at %s\n", htdigest_path);
175 gst_rtsp_token_unref (token);
179 gst_rtsp_token_unref (token);
183 gst_rtsp_auth_set_realm (auth, realm);
185 /* make admin token */
187 gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
189 gst_rtsp_auth_add_digest (auth, "admin", "power", token);
190 gst_rtsp_token_unref (token);
192 /* make admin2 token */
194 gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
196 gst_rtsp_auth_add_digest (auth, "admin2", "power2", token);
197 gst_rtsp_token_unref (token);
199 /* set as the server authentication manager */
200 gst_rtsp_server_set_auth (server, auth);
201 g_object_unref (auth);
203 /* attach the server to the default maincontext */
204 if (gst_rtsp_server_attach (server, NULL) == 0)
207 g_timeout_add_seconds (2, (GSourceFunc) timeout, server);
208 g_timeout_add_seconds (10, (GSourceFunc) remove_sessions, server);
211 g_print ("stream with user:password ready at rtsp://127.0.0.1:8554/test\n");
212 g_print ("stream with admin:power ready at rtsp://127.0.0.1:8554/test\n");
213 g_print ("stream with admin2:power2 ready at rtsp://127.0.0.1:8554/test2\n");
216 g_print ("stream with htdigest users ready at rtsp://127.0.0.1:8554/test\n");
218 g_main_loop_run (loop);
225 g_print ("failed to attach the server\n");