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