rtsp-client: Add unit test of SETUP for RTSP/RTP/TCP
[platform/upstream/gstreamer.git] / examples / test-video-disconnect.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3  * Copyright (C) 2018 Jan Schmidt <jan at centricular.com>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 /* This example disconnects any clients and exits 10 seconds
22  * after the first client connects */
23
24 #include <gst/gst.h>
25
26 #include <gst/rtsp-server/rtsp-server.h>
27
28 guint exit_timeout_id = 0;
29
30 /* define this if you want the resource to only be available when using
31  * user/password as the password */
32 #undef WITH_AUTH
33
34 /* define this if you want the server to use TLS (it will also need WITH_AUTH
35  * to be defined) */
36 #undef WITH_TLS
37
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. */
41 static gboolean
42 timeout (GstRTSPServer * server)
43 {
44   GstRTSPSessionPool *pool;
45
46   pool = gst_rtsp_server_get_session_pool (server);
47   gst_rtsp_session_pool_cleanup (pool);
48   g_object_unref (pool);
49
50   return TRUE;
51 }
52
53 static GstRTSPFilterResult
54 client_filter (GstRTSPServer * server, GstRTSPClient * client,
55     gpointer user_data)
56 {
57   /* Simple filter that shuts down all clients. */
58   return GST_RTSP_FILTER_REMOVE;
59 }
60
61 /* Timeout that runs 10 seconds after the first client connects and triggers
62  * the shutdown of the server */
63 static gboolean
64 shutdown_timeout (GstRTSPServer * server)
65 {
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);
72
73   /* Filter existing clients and remove them */
74   g_print ("Disconnecting existing clients\n");
75   gst_rtsp_server_client_filter (server, client_filter, NULL);
76   return FALSE;
77 }
78
79 static void
80 client_connected (GstRTSPServer * server, GstRTSPClient * client)
81 {
82   if (exit_timeout_id == 0) {
83     g_print ("First Client connected. Disconnecting everyone in 10 seconds\n");
84     exit_timeout_id =
85         g_timeout_add_seconds (10, (GSourceFunc) shutdown_timeout, server);
86   }
87 }
88
89 int
90 main (int argc, char *argv[])
91 {
92   GMainLoop *loop;
93   GstRTSPServer *server;
94   GstRTSPMountPoints *mounts;
95   GstRTSPMediaFactory *factory;
96 #ifdef WITH_AUTH
97   GstRTSPAuth *auth;
98   GstRTSPToken *token;
99   gchar *basic;
100   GstRTSPPermissions *permissions;
101 #endif
102 #ifdef WITH_TLS
103   GTlsCertificate *cert;
104   GError *error = NULL;
105 #endif
106
107   gst_init (&argc, &argv);
108
109   loop = g_main_loop_new (NULL, FALSE);
110
111   /* create a server instance */
112   server = gst_rtsp_server_new ();
113
114 #ifdef WITH_AUTH
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 ();
118 #ifdef WITH_TLS
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);
142   if (cert == NULL) {
143     g_printerr ("failed to parse PEM: %s\n", error->message);
144     return -1;
145   }
146   gst_rtsp_auth_set_tls_certificate (auth, cert);
147   g_object_unref (cert);
148 #endif
149
150   /* make user token */
151   token =
152       gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
153       "user", NULL);
154   basic = gst_rtsp_auth_make_basic ("user", "password");
155   gst_rtsp_auth_add_basic (auth, basic, token);
156   g_free (basic);
157   gst_rtsp_token_unref (token);
158
159   /* configure in the server */
160   gst_rtsp_server_set_auth (server, auth);
161 #endif
162
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);
166
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 " ")");
177 #ifdef WITH_AUTH
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);
185 #ifdef WITH_TLS
186   gst_rtsp_media_factory_set_profiles (factory, GST_RTSP_PROFILE_SAVP);
187 #endif
188 #endif
189
190   /* attach the test factory to the /test url */
191   gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
192
193   /* don't need the ref to the mapper anymore */
194   g_object_unref (mounts);
195
196   /* attach the server to the default maincontext */
197   if (gst_rtsp_server_attach (server, NULL) == 0)
198     goto failed;
199
200   g_signal_connect (server, "client-connected", (GCallback) client_connected,
201       NULL);
202
203   /* add a timeout for the session cleanup */
204   g_timeout_add_seconds (2, (GSourceFunc) timeout, server);
205
206   /* start serving, this never stops */
207 #ifdef WITH_TLS
208   g_print ("stream ready at rtsps://127.0.0.1:8554/test\n");
209 #else
210   g_print ("stream ready at rtsp://127.0.0.1:8554/test\n");
211 #endif
212   g_main_loop_run (loop);
213
214   return 0;
215
216   /* ERRORS */
217 failed:
218   {
219     g_print ("failed to attach the server\n");
220     return -1;
221   }
222 }