2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3 * Copyright (C) 2018 Jan Schmidt <jan at centricular.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 /* This example disconnects any clients and exits 10 seconds
22 * after the first client connects */
26 #include <gst/rtsp-server/rtsp-server.h>
28 guint exit_timeout_id = 0;
30 /* define this if you want the resource to only be available when using
31 * user/password as the password */
34 /* define this if you want the server to use TLS (it will also need WITH_AUTH
38 /* this timeout is periodically run to clean up the expired sessions from the
39 * pool. This needs to be run explicitly currently but might be done
40 * automatically as part of the mainloop. */
42 timeout (GstRTSPServer * server)
44 GstRTSPSessionPool *pool;
46 pool = gst_rtsp_server_get_session_pool (server);
47 gst_rtsp_session_pool_cleanup (pool);
48 g_object_unref (pool);
53 static GstRTSPFilterResult
54 client_filter (GstRTSPServer * server, GstRTSPClient * client,
57 /* Simple filter that shuts down all clients. */
58 return GST_RTSP_FILTER_REMOVE;
61 /* Timeout that runs 10 seconds after the first client connects and triggers
62 * the shutdown of the server */
64 shutdown_timeout (GstRTSPServer * server)
66 GstRTSPMountPoints *mounts;
67 g_print ("Time for everyone to go. Removing mount point\n");
68 /* Remove the mount point to prevent new clients connecting */
69 mounts = gst_rtsp_server_get_mount_points (server);
70 gst_rtsp_mount_points_remove_factory (mounts, "/test");
71 g_object_unref (mounts);
73 /* Filter existing clients and remove them */
74 g_print ("Disconnecting existing clients\n");
75 gst_rtsp_server_client_filter (server, client_filter, NULL);
80 client_connected (GstRTSPServer * server, GstRTSPClient * client)
82 if (exit_timeout_id == 0) {
83 g_print ("First Client connected. Disconnecting everyone in 10 seconds\n");
85 g_timeout_add_seconds (10, (GSourceFunc) shutdown_timeout, server);
90 main (int argc, char *argv[])
93 GstRTSPServer *server;
94 GstRTSPMountPoints *mounts;
95 GstRTSPMediaFactory *factory;
100 GstRTSPPermissions *permissions;
103 GTlsCertificate *cert;
104 GError *error = NULL;
107 gst_init (&argc, &argv);
109 loop = g_main_loop_new (NULL, FALSE);
111 /* create a server instance */
112 server = gst_rtsp_server_new ();
115 /* make a new authentication manager. it can be added to control access to all
116 * the factories on the server or on individual factories. */
117 auth = gst_rtsp_auth_new ();
119 cert = g_tls_certificate_new_from_pem ("-----BEGIN CERTIFICATE-----"
120 "MIICJjCCAY+gAwIBAgIBBzANBgkqhkiG9w0BAQUFADCBhjETMBEGCgmSJomT8ixk"
121 "ARkWA0NPTTEXMBUGCgmSJomT8ixkARkWB0VYQU1QTEUxHjAcBgNVBAsTFUNlcnRp"
122 "ZmljYXRlIEF1dGhvcml0eTEXMBUGA1UEAxMOY2EuZXhhbXBsZS5jb20xHTAbBgkq"
123 "hkiG9w0BCQEWDmNhQGV4YW1wbGUuY29tMB4XDTExMDExNzE5NDcxN1oXDTIxMDEx"
124 "NDE5NDcxN1owSzETMBEGCgmSJomT8ixkARkWA0NPTTEXMBUGCgmSJomT8ixkARkW"
125 "B0VYQU1QTEUxGzAZBgNVBAMTEnNlcnZlci5leGFtcGxlLmNvbTBcMA0GCSqGSIb3"
126 "DQEBAQUAA0sAMEgCQQDYScTxk55XBmbDM9zzwO+grVySE4rudWuzH2PpObIonqbf"
127 "hRoAalKVluG9jvbHI81eXxCdSObv1KBP1sbN5RzpAgMBAAGjIjAgMAkGA1UdEwQC"
128 "MAAwEwYDVR0lBAwwCgYIKwYBBQUHAwEwDQYJKoZIhvcNAQEFBQADgYEAYx6fMqT1"
129 "Gvo0jq88E8mc+bmp4LfXD4wJ7KxYeadQxt75HFRpj4FhFO3DOpVRFgzHlOEo3Fwk"
130 "PZOKjvkT0cbcoEq5whLH25dHoQxGoVQgFyAP5s+7Vp5AlHh8Y/vAoXeEVyy/RCIH"
131 "QkhUlAflfDMcrrYjsmwoOPSjhx6Mm/AopX4="
132 "-----END CERTIFICATE-----"
133 "-----BEGIN PRIVATE KEY-----"
134 "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEA2EnE8ZOeVwZmwzPc"
135 "88DvoK1ckhOK7nVrsx9j6TmyKJ6m34UaAGpSlZbhvY72xyPNXl8QnUjm79SgT9bG"
136 "zeUc6QIDAQABAkBRFJZ32VbqWMP9OVwDJLiwC01AlYLnka0mIQZbT/2xq9dUc9GW"
137 "U3kiVw4lL8v/+sPjtTPCYYdzHHOyDen6znVhAiEA9qJT7BtQvRxCvGrAhr9MS022"
138 "tTdPbW829BoUtIeH64cCIQDggG5i48v7HPacPBIH1RaSVhXl8qHCpQD3qrIw3FMw"
139 "DwIga8PqH5Sf5sHedy2+CiK0V4MRfoU4c3zQ6kArI+bEgSkCIQCLA1vXBiE31B5s"
140 "bdHoYa1BXebfZVd+1Hd95IfEM5mbRwIgSkDuQwV55BBlvWph3U8wVIMIb4GStaH8"
141 "W535W8UBbEg=" "-----END PRIVATE KEY-----", -1, &error);
143 g_printerr ("failed to parse PEM: %s\n", error->message);
146 gst_rtsp_auth_set_tls_certificate (auth, cert);
147 g_object_unref (cert);
150 /* make user token */
152 gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
154 basic = gst_rtsp_auth_make_basic ("user", "password");
155 gst_rtsp_auth_add_basic (auth, basic, token);
157 gst_rtsp_token_unref (token);
159 /* configure in the server */
160 gst_rtsp_server_set_auth (server, auth);
163 /* get the mount points for this server, every server has a default object
164 * that be used to map uri mount points to media factories */
165 mounts = gst_rtsp_server_get_mount_points (server);
167 /* make a media factory for a test stream. The default media factory can use
168 * gst-launch syntax to create pipelines.
169 * any launch line works as long as it contains elements named pay%d. Each
170 * element with pay%d names will be a stream */
171 factory = gst_rtsp_media_factory_new ();
172 gst_rtsp_media_factory_set_launch (factory, "( "
173 "videotestsrc ! video/x-raw,width=352,height=288,framerate=15/1 ! "
174 "x264enc ! rtph264pay name=pay0 pt=96 "
175 "audiotestsrc ! audio/x-raw,rate=8000 ! "
176 "alawenc ! rtppcmapay name=pay1 pt=97 " ")");
178 /* add permissions for the user media role */
179 permissions = gst_rtsp_permissions_new ();
180 gst_rtsp_permissions_add_role (permissions, "user",
181 GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
182 GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, TRUE, NULL);
183 gst_rtsp_media_factory_set_permissions (factory, permissions);
184 gst_rtsp_permissions_unref (permissions);
186 gst_rtsp_media_factory_set_profiles (factory, GST_RTSP_PROFILE_SAVP);
190 /* attach the test factory to the /test url */
191 gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
193 /* don't need the ref to the mapper anymore */
194 g_object_unref (mounts);
196 /* attach the server to the default maincontext */
197 if (gst_rtsp_server_attach (server, NULL) == 0)
200 g_signal_connect (server, "client-connected", (GCallback) client_connected,
203 /* add a timeout for the session cleanup */
204 g_timeout_add_seconds (2, (GSourceFunc) timeout, server);
206 /* start serving, this never stops */
208 g_print ("stream ready at rtsps://127.0.0.1:8554/test\n");
210 g_print ("stream ready at rtsp://127.0.0.1:8554/test\n");
212 g_main_loop_run (loop);
219 g_print ("failed to attach the server\n");