Add test-record-auth example
[platform/upstream/gstreamer.git] / examples / test-record-auth.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3  * Copyright (C) 2015 Centricular Ltd
4  *     Author: Sebastian Dröge <sebastian@centricular.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #include <gst/gst.h>
23
24 #include <gst/rtsp-server/rtsp-server.h>
25
26 #define DEFAULT_RTSP_PORT "8554"
27
28 static char *port = (char *) DEFAULT_RTSP_PORT;
29
30 static GOptionEntry entries[] = {
31   {"port", 'p', 0, G_OPTION_ARG_STRING, &port,
32       "Port to listen on (default: " DEFAULT_RTSP_PORT ")", "PORT"},
33   {NULL}
34 };
35
36 int
37 main (int argc, char *argv[])
38 {
39   GMainLoop *loop;
40   GstRTSPServer *server;
41   GstRTSPMountPoints *mounts;
42   GstRTSPMediaFactory *factory;
43   GOptionContext *optctx;
44   GError *error = NULL;
45   GstRTSPAuth *auth;
46   GstRTSPToken *token;
47   gchar *basic;
48
49   optctx = g_option_context_new ("<launch line> - Test RTSP Server, Launch\n\n"
50       "Example: \"( decodebin name=depay0 ! autovideosink )\"");
51   g_option_context_add_main_entries (optctx, entries, NULL);
52   g_option_context_add_group (optctx, gst_init_get_option_group ());
53   if (!g_option_context_parse (optctx, &argc, &argv, &error)) {
54     g_printerr ("Error parsing options: %s\n", error->message);
55     return -1;
56   }
57
58   if (argc < 2) {
59     g_print ("%s\n", g_option_context_get_help (optctx, TRUE, NULL));
60     return 1;
61   }
62   g_option_context_free (optctx);
63
64   loop = g_main_loop_new (NULL, FALSE);
65
66   /* create a server instance */
67   server = gst_rtsp_server_new ();
68
69   /* get the mount points for this server, every server has a default object
70    * that be used to map uri mount points to media factories */
71   mounts = gst_rtsp_server_get_mount_points (server);
72
73   /* make a media factory for a test stream. The default media factory can use
74    * gst-launch syntax to create pipelines.
75    * any launch line works as long as it contains elements named depay%d. Each
76    * element with depay%d names will be a stream */
77   factory = gst_rtsp_media_factory_new ();
78   gst_rtsp_media_factory_set_transport_mode (factory,
79       GST_RTSP_TRANSPORT_MODE_RECORD);
80   gst_rtsp_media_factory_set_launch (factory, argv[1]);
81   gst_rtsp_media_factory_set_latency (factory, 2000);
82
83   /* allow user to access this resource */
84   gst_rtsp_media_factory_add_role (factory, "user",
85       GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
86       GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, TRUE, NULL);
87   /* Anonymous users can see but not construct, so get UNAUTHORIZED */
88   gst_rtsp_media_factory_add_role (factory, "anonymous",
89       GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
90       GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, FALSE, NULL);
91
92   /* attach the test factory to the /test url */
93   gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
94
95   /* don't need the ref to the mapper anymore */
96   g_object_unref (mounts);
97
98   /* Set up the auth for user account */
99   /* make a new authentication manager */
100   auth = gst_rtsp_auth_new ();
101
102   /* make default token - anonymous unauthenticated access */
103   token =
104       gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
105       "anonymous", NULL);
106   gst_rtsp_auth_set_default_token (auth, token);
107   gst_rtsp_token_unref (token);
108
109   /* make user token */
110   token =
111       gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
112       "user", NULL);
113   basic = gst_rtsp_auth_make_basic ("user", "password");
114   gst_rtsp_auth_add_basic (auth, basic, token);
115   g_free (basic);
116   gst_rtsp_token_unref (token);
117
118   /* set as the server authentication manager */
119   gst_rtsp_server_set_auth (server, auth);
120   g_object_unref (auth);
121
122   /* attach the server to the default maincontext */
123   gst_rtsp_server_attach (server, NULL);
124
125   /* start serving */
126   g_print ("stream ready at rtsp://127.0.0.1:%s/test\n", port);
127   g_main_loop_run (loop);
128
129   return 0;
130 }