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 this if you want the resource to only be available when using
25 * user/password as the password */
28 /* define this if you want the server to use TLS (it will also need WITH_AUTH
32 /* this timeout is periodically run to clean up the expired sessions from the
33 * pool. This needs to be run explicitly currently but might be done
34 * automatically as part of the mainloop. */
36 timeout (GstRTSPServer * server, gboolean ignored)
38 GstRTSPSessionPool *pool;
40 pool = gst_rtsp_server_get_session_pool (server);
41 gst_rtsp_session_pool_cleanup (pool);
42 g_object_unref (pool);
48 main (int argc, char *argv[])
51 GstRTSPServer *server;
52 GstRTSPMountPoints *mounts;
53 GstRTSPMediaFactory *factory;
58 GstRTSPPermissions *permissions;
61 GTlsCertificate *cert;
64 gst_init (&argc, &argv);
66 loop = g_main_loop_new (NULL, FALSE);
68 /* create a server instance */
69 server = gst_rtsp_server_new ();
72 /* make a new authentication manager. it can be added to control access to all
73 * the factories on the server or on individual factories. */
74 auth = gst_rtsp_auth_new ();
76 cert = g_tls_certificate_new_from_pem ("-----BEGIN CERTIFICATE-----"
77 "MIICJjCCAY+gAwIBAgIBBzANBgkqhkiG9w0BAQUFADCBhjETMBEGCgmSJomT8ixk"
78 "ARkWA0NPTTEXMBUGCgmSJomT8ixkARkWB0VYQU1QTEUxHjAcBgNVBAsTFUNlcnRp"
79 "ZmljYXRlIEF1dGhvcml0eTEXMBUGA1UEAxMOY2EuZXhhbX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bdHoYa1BXebfZVd+1Hd95IfEM5mbRwIgSkDuQwV55BBlvWph3U8wVIMIb4GStaH8"
98 "W535W8UBbEg=" "-----END PRIVATE KEY-----", -1, NULL);
99 gst_rtsp_auth_set_tls_certificate (auth, cert);
100 g_object_unref (cert);
103 /* make user token */
105 gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
107 basic = gst_rtsp_auth_make_basic ("user", "password");
108 gst_rtsp_auth_add_basic (auth, basic, token);
110 gst_rtsp_token_unref (token);
112 /* configure in the server */
113 gst_rtsp_server_set_auth (server, auth);
116 /* get the mount points for this server, every server has a default object
117 * that be used to map uri mount points to media factories */
118 mounts = gst_rtsp_server_get_mount_points (server);
120 /* make a media factory for a test stream. The default media factory can use
121 * gst-launch syntax to create pipelines.
122 * any launch line works as long as it contains elements named pay%d. Each
123 * element with pay%d names will be a stream */
124 factory = gst_rtsp_media_factory_new ();
125 gst_rtsp_media_factory_set_launch (factory, "( "
126 "videotestsrc ! video/x-raw,width=352,height=288,framerate=15/1 ! "
127 "x264enc ! rtph264pay name=pay0 pt=96 "
128 "audiotestsrc ! audio/x-raw,rate=8000 ! "
129 "alawenc ! rtppcmapay name=pay1 pt=97 " ")");
131 /* add permissions for the user media role */
132 permissions = gst_rtsp_permissions_new ();
133 gst_rtsp_permissions_add_role (permissions, "user",
134 GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
135 GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, TRUE, NULL);
136 gst_rtsp_media_factory_set_permissions (factory, permissions);
137 gst_rtsp_permissions_unref (permissions);
140 /* attach the test factory to the /test url */
141 gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
143 /* don't need the ref to the mapper anymore */
144 g_object_unref (mounts);
146 /* attach the server to the default maincontext */
147 if (gst_rtsp_server_attach (server, NULL) == 0)
150 /* add a timeout for the session cleanup */
151 g_timeout_add_seconds (2, (GSourceFunc) timeout, server);
153 /* start serving, this never stops */
155 g_print ("stream ready at rtsps://127.0.0.1:8554/test\n");
157 g_print ("stream ready at rtsp://127.0.0.1:8554/test\n");
159 g_main_loop_run (loop);
166 g_print ("failed to attach the server\n");