2 * Copyright (C) 2013 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.
20 /* Runs a pipeline and clasifies the media pipelines based on the
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
27 * sudo cgcreate -t uid:gid -g cpu:/user
28 * sudo cgcreate -t uid:gid -g cpu:/admin
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.
33 * Then you would want to change the cpu shares assigned to each group:
35 * sudo cgset -r cpu.shares=100 user
36 * sudo cgset -r cpu.shares=1024 admin
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
43 #include <libcgroup.h>
46 #include <gst/rtsp-server/rtsp-server.h>
48 typedef struct _GstRTSPCGroupPool GstRTSPCGroupPool;
49 typedef struct _GstRTSPCGroupPoolClass GstRTSPCGroupPoolClass;
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))
60 struct _GstRTSPCGroupPool
62 GstRTSPThreadPool parent;
68 struct _GstRTSPCGroupPoolClass
70 GstRTSPThreadPoolClass parent_class;
73 static GQuark thread_cgroup;
75 static void gst_rtsp_cgroup_pool_finalize (GObject * obj);
77 static void default_thread_enter (GstRTSPThreadPool * pool,
78 GstRTSPThread * thread);
79 static void default_configure_thread (GstRTSPThreadPool * pool,
80 GstRTSPThread * thread, GstRTSPContext * ctx);
82 static GType gst_rtsp_cgroup_pool_get_type (void);
84 G_DEFINE_TYPE (GstRTSPCGroupPool, gst_rtsp_cgroup_pool,
85 GST_TYPE_RTSP_THREAD_POOL);
88 gst_rtsp_cgroup_pool_class_init (GstRTSPCGroupPoolClass * klass)
90 GObjectClass *gobject_class;
91 GstRTSPThreadPoolClass *tpool_class;
93 gobject_class = G_OBJECT_CLASS (klass);
94 tpool_class = GST_RTSP_THREAD_POOL_CLASS (klass);
96 gobject_class->finalize = gst_rtsp_cgroup_pool_finalize;
98 tpool_class->configure_thread = default_configure_thread;
99 tpool_class->thread_enter = default_thread_enter;
101 thread_cgroup = g_quark_from_string ("cgroup.pool.thread.cgroup");
107 gst_rtsp_cgroup_pool_init (GstRTSPCGroupPool * pool)
109 pool->user = cgroup_new_cgroup ("user");
110 if (cgroup_add_controller (pool->user, "cpu") == NULL)
111 g_error ("Failed to add cpu controller to user cgroup");
112 pool->admin = cgroup_new_cgroup ("admin");
113 if (cgroup_add_controller (pool->admin, "cpu") == NULL)
114 g_error ("Failed to add cpu controller to admin cgroup");
118 gst_rtsp_cgroup_pool_finalize (GObject * obj)
120 GstRTSPCGroupPool *pool = GST_RTSP_CGROUP_POOL (obj);
122 GST_INFO ("finalize pool %p", pool);
124 cgroup_free (&pool->user);
125 cgroup_free (&pool->admin);
127 G_OBJECT_CLASS (gst_rtsp_cgroup_pool_parent_class)->finalize (obj);
131 default_thread_enter (GstRTSPThreadPool * pool, GstRTSPThread * thread)
133 struct cgroup *cgroup;
135 cgroup = gst_mini_object_get_qdata (GST_MINI_OBJECT (thread), thread_cgroup);
139 res = cgroup_attach_task (cgroup);
142 GST_ERROR ("error: %d (%s)", res, cgroup_strerror (res));
147 default_configure_thread (GstRTSPThreadPool * pool,
148 GstRTSPThread * thread, GstRTSPContext * ctx)
150 GstRTSPCGroupPool *cpool = GST_RTSP_CGROUP_POOL (pool);
152 struct cgroup *cgroup;
155 cls = gst_rtsp_token_get_string (ctx->token, "cgroup.pool.media.class");
159 GST_DEBUG ("manage cgroup %s", cls);
161 if (!g_strcmp0 (cls, "admin"))
162 cgroup = cpool->admin;
164 cgroup = cpool->user;
166 /* attach the cgroup to the thread */
167 gst_mini_object_set_qdata (GST_MINI_OBJECT (thread), thread_cgroup,
172 timeout (GstRTSPServer * server)
174 GstRTSPSessionPool *pool;
176 pool = gst_rtsp_server_get_session_pool (server);
177 gst_rtsp_session_pool_cleanup (pool);
178 g_object_unref (pool);
184 main (int argc, char *argv[])
187 GstRTSPServer *server;
188 GstRTSPMountPoints *mounts;
189 GstRTSPMediaFactory *factory;
193 GstRTSPThreadPool *thread_pool;
195 gst_init (&argc, &argv);
197 loop = g_main_loop_new (NULL, FALSE);
199 /* create a server instance */
200 server = gst_rtsp_server_new ();
202 /* get the mounts for this server, every server has a default mapper object
203 * that be used to map uri mount points to media factories */
204 mounts = gst_rtsp_server_get_mount_points (server);
206 /* make a media factory for a test stream. The default media factory can use
207 * gst-launch syntax to create pipelines.
208 * any launch line works as long as it contains elements named pay%d. Each
209 * element with pay%d names will be a stream */
210 factory = gst_rtsp_media_factory_new ();
211 gst_rtsp_media_factory_set_launch (factory, "( "
212 "videotestsrc ! video/x-raw,width=640,height=480,framerate=50/1 ! "
213 "x264enc ! rtph264pay name=pay0 pt=96 "
214 "audiotestsrc ! audio/x-raw,rate=8000 ! "
215 "alawenc ! rtppcmapay name=pay1 pt=97 " ")");
216 /* attach the test factory to the /test url */
217 gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
219 /* allow user and admin to access this resource */
220 gst_rtsp_media_factory_add_role (factory, "user",
221 "media.factory.access", G_TYPE_BOOLEAN, TRUE,
222 "media.factory.construct", G_TYPE_BOOLEAN, TRUE, NULL);
223 gst_rtsp_media_factory_add_role (factory, "admin",
224 "media.factory.access", G_TYPE_BOOLEAN, TRUE,
225 "media.factory.construct", G_TYPE_BOOLEAN, TRUE, NULL);
227 /* don't need the ref to the mapper anymore */
228 g_object_unref (mounts);
230 /* make a new authentication manager */
231 auth = gst_rtsp_auth_new ();
233 /* make user token */
234 token = gst_rtsp_token_new ("cgroup.pool.media.class", G_TYPE_STRING, "user",
235 "media.factory.role", G_TYPE_STRING, "user", NULL);
236 basic = gst_rtsp_auth_make_basic ("user", "password");
237 gst_rtsp_auth_add_basic (auth, basic, token);
239 gst_rtsp_token_unref (token);
241 /* make admin token */
242 token = gst_rtsp_token_new ("cgroup.pool.media.class", G_TYPE_STRING, "admin",
243 "media.factory.role", G_TYPE_STRING, "admin", NULL);
244 basic = gst_rtsp_auth_make_basic ("admin", "power");
245 gst_rtsp_auth_add_basic (auth, basic, token);
247 gst_rtsp_token_unref (token);
249 /* set as the server authentication manager */
250 gst_rtsp_server_set_auth (server, auth);
251 g_object_unref (auth);
253 thread_pool = g_object_new (GST_TYPE_RTSP_CGROUP_POOL, NULL);
254 gst_rtsp_server_set_thread_pool (server, thread_pool);
255 g_object_unref (thread_pool);
257 /* attach the server to the default maincontext */
258 if (gst_rtsp_server_attach (server, NULL) == 0)
261 g_timeout_add_seconds (2, (GSourceFunc) timeout, server);
264 g_print ("stream with user:password ready at rtsp://127.0.0.1:8554/test\n");
265 g_print ("stream with admin:power ready at rtsp://127.0.0.1:8554/test\n");
266 g_main_loop_run (loop);
273 g_print ("failed to attach the server\n");