0a3891668534d89feebbab16a9b2b7592d4908da
[platform/upstream/gstreamer.git] / examples / test-cgroups.c
1 /* GStreamer
2  * Copyright (C) 2013 Wim Taymans <wim.taymans at gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /* Runs a pipeline and clasifies the media pipelines based on the
21  * authenticated user.
22  *
23  * This test requires 2 cpu cgroups to exist named 'user' and 'admin'.
24  * The rtsp server should have permission to add its threads to the
25  * cgroups.
26  *
27  *   sudo cgcreate -t uid:gid -g cpu:/user
28  *   sudo cgcreate -t uid:gid -g cpu:/admin
29  *
30  * With -t you can give the user and group access to the task file to
31  * write the thread ids. The user running the server can be used.
32  *
33  * Then you would want to change the cpu shares assigned to each group:
34  *
35  *   sudo cgset -r cpu.shares=100 user
36  *   sudo cgset -r cpu.shares=1024 admin
37  *
38  * Then start clients for 'user' until the stream is degraded because of
39  * lack of CPU. Then start a client for 'admin' and check that the stream
40  * is not degraded.
41  */
42
43 #include <libcgroup.h>
44
45 #include <gst/gst.h>
46 #include <gst/rtsp-server/rtsp-server.h>
47
48 typedef struct _GstRTSPCGroupPool GstRTSPCGroupPool;
49 typedef struct _GstRTSPCGroupPoolClass GstRTSPCGroupPoolClass;
50
51 #define GST_TYPE_RTSP_CGROUP_POOL              (gst_rtsp_cgroup_pool_get_type ())
52 #define GST_IS_RTSP_CGROUP_POOL(obj)           (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_RTSP_CGROUP_POOL))
53 #define GST_IS_RTSP_CGROUP_POOL_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_RTSP_CGROUP_POOL))
54 #define GST_RTSP_CGROUP_POOL_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_RTSP_CGROUP_POOL, GstRTSPCGroupPoolClass))
55 #define GST_RTSP_CGROUP_POOL(obj)              (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_RTSP_CGROUP_POOL, GstRTSPCGroupPool))
56 #define GST_RTSP_CGROUP_POOL_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_RTSP_CGROUP_POOL, GstRTSPCGroupPoolClass))
57 #define GST_RTSP_CGROUP_POOL_CAST(obj)         ((GstRTSPCGroupPool*)(obj))
58 #define GST_RTSP_CGROUP_POOL_CLASS_CAST(klass) ((GstRTSPCGroupPoolClass*)(klass))
59
60 struct _GstRTSPCGroupPool
61 {
62   GstRTSPThreadPool parent;
63
64   struct cgroup *user;
65   struct cgroup *admin;
66 };
67
68 struct _GstRTSPCGroupPoolClass
69 {
70   GstRTSPThreadPoolClass parent_class;
71 };
72
73 static GQuark thread_cgroup;
74
75 static void gst_rtsp_cgroup_pool_finalize (GObject * obj);
76
77 static void default_thread_enter (GstRTSPThreadPool * pool,
78     GstRTSPThread * thread);
79 static void default_configure_thread (GstRTSPThreadPool * pool,
80     GstRTSPThread * thread, GstRTSPContext * ctx);
81
82 G_DEFINE_TYPE (GstRTSPCGroupPool, gst_rtsp_cgroup_pool,
83     GST_TYPE_RTSP_THREAD_POOL);
84
85 static void
86 gst_rtsp_cgroup_pool_class_init (GstRTSPCGroupPoolClass * klass)
87 {
88   GObjectClass *gobject_class;
89   GstRTSPThreadPoolClass *tpool_class;
90
91   gobject_class = G_OBJECT_CLASS (klass);
92   tpool_class = GST_RTSP_THREAD_POOL_CLASS (klass);
93
94   gobject_class->finalize = gst_rtsp_cgroup_pool_finalize;
95
96   tpool_class->configure_thread = default_configure_thread;
97   tpool_class->thread_enter = default_thread_enter;
98
99   thread_cgroup = g_quark_from_string ("cgroup.pool.thread.cgroup");
100
101   cgroup_init ();
102 }
103
104 static void
105 gst_rtsp_cgroup_pool_init (GstRTSPCGroupPool * pool)
106 {
107   pool->user = cgroup_new_cgroup ("user");
108   g_assert (cgroup_add_controller (pool->user, "cpu") != NULL);
109   pool->admin = cgroup_new_cgroup ("admin");
110   g_assert (cgroup_add_controller (pool->admin, "cpu") != NULL);
111 }
112
113 static void
114 gst_rtsp_cgroup_pool_finalize (GObject * obj)
115 {
116   GstRTSPCGroupPool *pool = GST_RTSP_CGROUP_POOL (obj);
117
118   GST_INFO ("finalize pool %p", pool);
119
120   cgroup_free (&pool->user);
121   cgroup_free (&pool->admin);
122
123   G_OBJECT_CLASS (gst_rtsp_cgroup_pool_parent_class)->finalize (obj);
124 }
125
126 static void
127 default_thread_enter (GstRTSPThreadPool * pool, GstRTSPThread * thread)
128 {
129   struct cgroup *cgroup;
130
131   cgroup = gst_mini_object_get_qdata (GST_MINI_OBJECT (thread), thread_cgroup);
132   if (cgroup) {
133     gint res = 0;
134
135     res = cgroup_attach_task (cgroup);
136
137     if (res != 0)
138       GST_ERROR ("error: %d (%s)", res, cgroup_strerror (res));
139   }
140 }
141
142 static void
143 default_configure_thread (GstRTSPThreadPool * pool,
144     GstRTSPThread * thread, GstRTSPContext * ctx)
145 {
146   GstRTSPCGroupPool *cpool = GST_RTSP_CGROUP_POOL (pool);
147   const gchar *cls;
148   struct cgroup *cgroup;
149
150   if (ctx->token)
151     cls = gst_rtsp_token_get_string (ctx->token, "cgroup.pool.media.class");
152   else
153     cls = NULL;
154
155   GST_DEBUG ("manage cgroup %s", cls);
156
157   if (!g_strcmp0 (cls, "admin"))
158     cgroup = cpool->admin;
159   else
160     cgroup = cpool->user;
161
162   /* attach the cgroup to the thread */
163   gst_mini_object_set_qdata (GST_MINI_OBJECT (thread), thread_cgroup,
164       cgroup, NULL);
165 }
166
167 static gboolean
168 timeout (GstRTSPServer * server)
169 {
170   GstRTSPSessionPool *pool;
171
172   pool = gst_rtsp_server_get_session_pool (server);
173   gst_rtsp_session_pool_cleanup (pool);
174   g_object_unref (pool);
175
176   return TRUE;
177 }
178
179 int
180 main (int argc, char *argv[])
181 {
182   GMainLoop *loop;
183   GstRTSPServer *server;
184   GstRTSPMountPoints *mounts;
185   GstRTSPMediaFactory *factory;
186   GstRTSPAuth *auth;
187   GstRTSPToken *token;
188   gchar *basic;
189   GstRTSPThreadPool *thread_pool;
190
191   gst_init (&argc, &argv);
192
193   loop = g_main_loop_new (NULL, FALSE);
194
195   /* create a server instance */
196   server = gst_rtsp_server_new ();
197
198   /* get the mounts for this server, every server has a default mapper object
199    * that be used to map uri mount points to media factories */
200   mounts = gst_rtsp_server_get_mount_points (server);
201
202   /* make a media factory for a test stream. The default media factory can use
203    * gst-launch syntax to create pipelines. 
204    * any launch line works as long as it contains elements named pay%d. Each
205    * element with pay%d names will be a stream */
206   factory = gst_rtsp_media_factory_new ();
207   gst_rtsp_media_factory_set_launch (factory, "( "
208       "videotestsrc ! video/x-raw,width=640,height=480,framerate=50/1 ! "
209       "x264enc ! rtph264pay name=pay0 pt=96 "
210       "audiotestsrc ! audio/x-raw,rate=8000 ! "
211       "alawenc ! rtppcmapay name=pay1 pt=97 " ")");
212   /* attach the test factory to the /test url */
213   gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
214
215   /* allow user and admin to access this resource */
216   gst_rtsp_media_factory_add_role (factory, "user",
217       "media.factory.access", G_TYPE_BOOLEAN, TRUE,
218       "media.factory.construct", G_TYPE_BOOLEAN, TRUE, NULL);
219   gst_rtsp_media_factory_add_role (factory, "admin",
220       "media.factory.access", G_TYPE_BOOLEAN, TRUE,
221       "media.factory.construct", G_TYPE_BOOLEAN, TRUE, NULL);
222
223   /* don't need the ref to the mapper anymore */
224   g_object_unref (mounts);
225
226   /* make a new authentication manager */
227   auth = gst_rtsp_auth_new ();
228
229   /* make user token */
230   token = gst_rtsp_token_new ("cgroup.pool.media.class", G_TYPE_STRING, "user",
231       "media.factory.role", G_TYPE_STRING, "user", NULL);
232   basic = gst_rtsp_auth_make_basic ("user", "password");
233   gst_rtsp_auth_add_basic (auth, basic, token);
234   g_free (basic);
235   gst_rtsp_token_unref (token);
236
237   /* make admin token */
238   token = gst_rtsp_token_new ("cgroup.pool.media.class", G_TYPE_STRING, "admin",
239       "media.factory.role", G_TYPE_STRING, "admin", NULL);
240   basic = gst_rtsp_auth_make_basic ("admin", "power");
241   gst_rtsp_auth_add_basic (auth, basic, token);
242   g_free (basic);
243   gst_rtsp_token_unref (token);
244
245   /* set as the server authentication manager */
246   gst_rtsp_server_set_auth (server, auth);
247   g_object_unref (auth);
248
249   thread_pool = g_object_new (GST_TYPE_RTSP_CGROUP_POOL, NULL);
250   gst_rtsp_server_set_thread_pool (server, thread_pool);
251   g_object_unref (thread_pool);
252
253   /* attach the server to the default maincontext */
254   if (gst_rtsp_server_attach (server, NULL) == 0)
255     goto failed;
256
257   g_timeout_add_seconds (2, (GSourceFunc) timeout, server);
258
259   /* start serving */
260   g_print ("stream with user:password ready at rtsp://127.0.0.1:8554/test\n");
261   g_print ("stream with admin:power ready at rtsp://127.0.0.1:8554/test\n");
262   g_main_loop_run (loop);
263
264   return 0;
265
266   /* ERRORS */
267 failed:
268   {
269     g_print ("failed to attach the server\n");
270     return -1;
271   }
272 }