summaryrefslogtreecommitdiff
path: root/examples/test-cgroups.c
blob: 600fa3c16f843bc6c5e8f0df24689e271ed0f40d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* GStreamer
 * Copyright (C) 2013 Wim Taymans <wim.taymans at gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/* Runs a pipeline and clasifies the media pipelines based on the
 * authenticated user.
 *
 * This test requires 2 cpu cgroups to exist named 'user' and 'admin'.
 * The rtsp server should have permission to add its threads to the
 * cgroups.
 *
 *   sudo cgcreate -t uid:gid -g cpu:/user
 *   sudo cgcreate -t uid:gid -g cpu:/admin
 *
 * With -t you can give the user and group access to the task file to
 * write the thread ids. The user running the server can be used.
 *
 * Then you would want to change the cpu shares assigned to each group:
 *
 *   sudo cgset -r cpu.shares=100 user
 *   sudo cgset -r cpu.shares=1024 admin
 *
 * Then start clients for 'user' until the stream is degraded because of
 * lack of CPU. Then start a client for 'admin' and check that the stream
 * is not degraded.
 */

#include <libcgroup.h>

#include <gst/gst.h>
#include <gst/rtsp-server/rtsp-server.h>

typedef struct _GstRTSPCGroupPool GstRTSPCGroupPool;
typedef struct _GstRTSPCGroupPoolClass GstRTSPCGroupPoolClass;

#define GST_TYPE_RTSP_CGROUP_POOL              (gst_rtsp_cgroup_pool_get_type ())
#define GST_IS_RTSP_CGROUP_POOL(obj)           (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_RTSP_CGROUP_POOL))
#define GST_IS_RTSP_CGROUP_POOL_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_RTSP_CGROUP_POOL))
#define GST_RTSP_CGROUP_POOL_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_RTSP_CGROUP_POOL, GstRTSPCGroupPoolClass))
#define GST_RTSP_CGROUP_POOL(obj)              (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_RTSP_CGROUP_POOL, GstRTSPCGroupPool))
#define GST_RTSP_CGROUP_POOL_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_RTSP_CGROUP_POOL, GstRTSPCGroupPoolClass))
#define GST_RTSP_CGROUP_POOL_CAST(obj)         ((GstRTSPCGroupPool*)(obj))
#define GST_RTSP_CGROUP_POOL_CLASS_CAST(klass) ((GstRTSPCGroupPoolClass*)(klass))

struct _GstRTSPCGroupPool
{
  GstRTSPThreadPool parent;

  struct cgroup *user;
  struct cgroup *admin;
};

struct _GstRTSPCGroupPoolClass
{
  GstRTSPThreadPoolClass parent_class;
};

static GQuark thread_cgroup;

static void gst_rtsp_cgroup_pool_finalize (GObject * obj);

static void default_thread_enter (GstRTSPThreadPool * pool,
    GstRTSPThread * thread);
static void default_configure_thread (GstRTSPThreadPool * pool,
    GstRTSPThread * thread, GstRTSPContext * ctx);

static GType gst_rtsp_cgroup_pool_get_type (void);

G_DEFINE_TYPE (GstRTSPCGroupPool, gst_rtsp_cgroup_pool,
    GST_TYPE_RTSP_THREAD_POOL);

static void
gst_rtsp_cgroup_pool_class_init (GstRTSPCGroupPoolClass * klass)
{
  GObjectClass *gobject_class;
  GstRTSPThreadPoolClass *tpool_class;

  gobject_class = G_OBJECT_CLASS (klass);
  tpool_class = GST_RTSP_THREAD_POOL_CLASS (klass);

  gobject_class->finalize = gst_rtsp_cgroup_pool_finalize;

  tpool_class->configure_thread = default_configure_thread;
  tpool_class->thread_enter = default_thread_enter;

  thread_cgroup = g_quark_from_string ("cgroup.pool.thread.cgroup");

  cgroup_init ();
}

static void
gst_rtsp_cgroup_pool_init (GstRTSPCGroupPool * pool)
{
  pool->user = cgroup_new_cgroup ("user");
  if (cgroup_add_controller (pool->user, "cpu") == NULL)
    g_error ("Failed to add cpu controller to user cgroup");
  pool->admin = cgroup_new_cgroup ("admin");
  if (cgroup_add_controller (pool->admin, "cpu") == NULL)
    g_error ("Failed to add cpu controller to admin cgroup");
}

static void
gst_rtsp_cgroup_pool_finalize (GObject * obj)
{
  GstRTSPCGroupPool *pool = GST_RTSP_CGROUP_POOL (obj);

  GST_INFO ("finalize pool %p", pool);

  cgroup_free (&pool->user);
  cgroup_free (&pool->admin);

  G_OBJECT_CLASS (gst_rtsp_cgroup_pool_parent_class)->finalize (obj);
}

static void
default_thread_enter (GstRTSPThreadPool * pool, GstRTSPThread * thread)
{
  struct cgroup *cgroup;

  cgroup = gst_mini_object_get_qdata (GST_MINI_OBJECT (thread), thread_cgroup);
  if (cgroup) {
    gint res = 0;

    res = cgroup_attach_task (cgroup);

    if (res != 0)
      GST_ERROR ("error: %d (%s)", res, cgroup_strerror (res));
  }
}

static void
default_configure_thread (GstRTSPThreadPool * pool,
    GstRTSPThread * thread, GstRTSPContext * ctx)
{
  GstRTSPCGroupPool *cpool = GST_RTSP_CGROUP_POOL (pool);
  const gchar *cls;
  struct cgroup *cgroup;

  if (ctx->token)
    cls = gst_rtsp_token_get_string (ctx->token, "cgroup.pool.media.class");
  else
    cls = NULL;

  GST_DEBUG ("manage cgroup %s", cls);

  if (!g_strcmp0 (cls, "admin"))
    cgroup = cpool->admin;
  else
    cgroup = cpool->user;

  /* attach the cgroup to the thread */
  gst_mini_object_set_qdata (GST_MINI_OBJECT (thread), thread_cgroup,
      cgroup, NULL);
}

static gboolean
timeout (GstRTSPServer * server)
{
  GstRTSPSessionPool *pool;

  pool = gst_rtsp_server_get_session_pool (server);
  gst_rtsp_session_pool_cleanup (pool);
  g_object_unref (pool);

  return TRUE;
}

int
main (int argc, char *argv[])
{
  GMainLoop *loop;
  GstRTSPServer *server;
  GstRTSPMountPoints *mounts;
  GstRTSPMediaFactory *factory;
  GstRTSPAuth *auth;
  GstRTSPToken *token;
  gchar *basic;
  GstRTSPThreadPool *thread_pool;

  gst_init (&argc, &argv);

  loop = g_main_loop_new (NULL, FALSE);

  /* create a server instance */
  server = gst_rtsp_server_new ();

  /* get the mounts for this server, every server has a default mapper object
   * that be used to map uri mount points to media factories */
  mounts = gst_rtsp_server_get_mount_points (server);

  /* make a media factory for a test stream. The default media factory can use
   * gst-launch syntax to create pipelines. 
   * any launch line works as long as it contains elements named pay%d. Each
   * element with pay%d names will be a stream */
  factory = gst_rtsp_media_factory_new ();
  gst_rtsp_media_factory_set_launch (factory, "( "
      "videotestsrc ! video/x-raw,width=640,height=480,framerate=50/1 ! "
      "x264enc ! rtph264pay name=pay0 pt=96 "
      "audiotestsrc ! audio/x-raw,rate=8000 ! "
      "alawenc ! rtppcmapay name=pay1 pt=97 " ")");
  /* attach the test factory to the /test url */
  gst_rtsp_mount_points_add_factory (mounts, "/test", factory);

  /* allow user and admin to access this resource */
  gst_rtsp_media_factory_add_role (factory, "user",
      "media.factory.access", G_TYPE_BOOLEAN, TRUE,
      "media.factory.construct", G_TYPE_BOOLEAN, TRUE, NULL);
  gst_rtsp_media_factory_add_role (factory, "admin",
      "media.factory.access", G_TYPE_BOOLEAN, TRUE,
      "media.factory.construct", G_TYPE_BOOLEAN, TRUE, NULL);

  /* don't need the ref to the mapper anymore */
  g_object_unref (mounts);

  /* make a new authentication manager */
  auth = gst_rtsp_auth_new ();

  /* make user token */
  token = gst_rtsp_token_new ("cgroup.pool.media.class", G_TYPE_STRING, "user",
      "media.factory.role", G_TYPE_STRING, "user", NULL);
  basic = gst_rtsp_auth_make_basic ("user", "password");
  gst_rtsp_auth_add_basic (auth, basic, token);
  g_free (basic);
  gst_rtsp_token_unref (token);

  /* make admin token */
  token = gst_rtsp_token_new ("cgroup.pool.media.class", G_TYPE_STRING, "admin",
      "media.factory.role", G_TYPE_STRING, "admin", NULL);
  basic = gst_rtsp_auth_make_basic ("admin", "power");
  gst_rtsp_auth_add_basic (auth, basic, token);
  g_free (basic);
  gst_rtsp_token_unref (token);

  /* set as the server authentication manager */
  gst_rtsp_server_set_auth (server, auth);
  g_object_unref (auth);

  thread_pool = g_object_new (GST_TYPE_RTSP_CGROUP_POOL, NULL);
  gst_rtsp_server_set_thread_pool (server, thread_pool);
  g_object_unref (thread_pool);

  /* attach the server to the default maincontext */
  if (gst_rtsp_server_attach (server, NULL) == 0)
    goto failed;

  g_timeout_add_seconds (2, (GSourceFunc) timeout, server);

  /* start serving */
  g_print ("stream with user:password ready at rtsp://127.0.0.1:8554/test\n");
  g_print ("stream with admin:power ready at rtsp://127.0.0.1:8554/test\n");
  g_main_loop_run (loop);

  return 0;

  /* ERRORS */
failed:
  {
    g_print ("failed to attach the server\n");
    return -1;
  }
}