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>
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.
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.
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.
23 * @short_description: A client connection state
24 * @see_also: #GstRTSPServer, #GstRTSPThreadPool
26 * The client object handles the connection with a client for as long as a TCP
29 * A #GstRTSPClient is created by #GstRTSPServer when a new connection is
30 * accepted and it inherits the #GstRTSPMountPoints, #GstRTSPSessionPool,
31 * #GstRTSPAuth and #GstRTSPThreadPool from the server.
33 * The client connection should be configured with the #GstRTSPConnection using
34 * gst_rtsp_client_set_connection() before it can be attached to a #GMainContext
35 * using gst_rtsp_client_attach(). From then on the client will handle requests
38 * Use gst_rtsp_client_session_filter() to iterate or modify all the
39 * #GstRTSPSession objects managed by the client object.
41 * Last reviewed on 2013-07-11 (1.0.0)
47 #include <gst/sdp/gstmikey.h>
48 #include <gst/rtsp/gstrtsp-enumtypes.h>
50 #include "rtsp-client.h"
52 #include "rtsp-params.h"
54 #define GST_RTSP_CLIENT_GET_PRIVATE(obj) \
55 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_CLIENT, GstRTSPClientPrivate))
65 * send_lock, lock, tunnels_lock
68 struct _GstRTSPClientPrivate
70 GMutex lock; /* protects everything else */
73 GstRTSPConnection *connection;
75 GMainContext *watch_context;
80 GstRTSPClientSendFunc send_func; /* protected by send_lock */
81 gpointer send_data; /* protected by send_lock */
82 GDestroyNotify send_notify; /* protected by send_lock */
84 GstRTSPSessionPool *session_pool;
85 gulong session_removed_id;
86 GstRTSPMountPoints *mount_points;
88 GstRTSPThreadPool *thread_pool;
90 /* used to cache the media in the last requested DESCRIBE so that
91 * we can pick it up in the next SETUP immediately */
95 GHashTable *transports;
97 guint sessions_cookie;
99 gboolean drop_backlog;
101 guint rtsp_ctrl_timeout_id;
102 guint rtsp_ctrl_timeout_cnt;
104 /* The version currently being used */
105 GstRTSPVersion version;
107 GHashTable *pipelined_requests; /* pipelined_request_id -> session_id */
108 GstRTSPTunnelState tstate;
111 static GMutex tunnels_lock;
112 static GHashTable *tunnels; /* protected by tunnels_lock */
114 /* FIXME make this configurable. We don't want to do this yet because it will
115 * be superceeded by a cache object later */
116 #define WATCH_BACKLOG_SIZE 100
118 #define DEFAULT_SESSION_POOL NULL
119 #define DEFAULT_MOUNT_POINTS NULL
120 #define DEFAULT_DROP_BACKLOG TRUE
122 #define RTSP_CTRL_CB_INTERVAL 1
123 #define RTSP_CTRL_TIMEOUT_VALUE 60
138 SIGNAL_PRE_OPTIONS_REQUEST,
139 SIGNAL_OPTIONS_REQUEST,
140 SIGNAL_PRE_DESCRIBE_REQUEST,
141 SIGNAL_DESCRIBE_REQUEST,
142 SIGNAL_PRE_SETUP_REQUEST,
143 SIGNAL_SETUP_REQUEST,
144 SIGNAL_PRE_PLAY_REQUEST,
146 SIGNAL_PRE_PAUSE_REQUEST,
147 SIGNAL_PAUSE_REQUEST,
148 SIGNAL_PRE_TEARDOWN_REQUEST,
149 SIGNAL_TEARDOWN_REQUEST,
150 SIGNAL_PRE_SET_PARAMETER_REQUEST,
151 SIGNAL_SET_PARAMETER_REQUEST,
152 SIGNAL_PRE_GET_PARAMETER_REQUEST,
153 SIGNAL_GET_PARAMETER_REQUEST,
154 SIGNAL_HANDLE_RESPONSE,
156 SIGNAL_PRE_ANNOUNCE_REQUEST,
157 SIGNAL_ANNOUNCE_REQUEST,
158 SIGNAL_PRE_RECORD_REQUEST,
159 SIGNAL_RECORD_REQUEST,
160 SIGNAL_CHECK_REQUIREMENTS,
164 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
165 #define GST_CAT_DEFAULT rtsp_client_debug
167 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
169 static void gst_rtsp_client_get_property (GObject * object, guint propid,
170 GValue * value, GParamSpec * pspec);
171 static void gst_rtsp_client_set_property (GObject * object, guint propid,
172 const GValue * value, GParamSpec * pspec);
173 static void gst_rtsp_client_finalize (GObject * obj);
175 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
176 static gboolean handle_sdp (GstRTSPClient * client, GstRTSPContext * ctx,
177 GstRTSPMedia * media, GstSDPMessage * sdp);
178 static gboolean default_configure_client_media (GstRTSPClient * client,
179 GstRTSPMedia * media, GstRTSPStream * stream, GstRTSPContext * ctx);
180 static gboolean default_configure_client_transport (GstRTSPClient * client,
181 GstRTSPContext * ctx, GstRTSPTransport * ct);
182 static GstRTSPResult default_params_set (GstRTSPClient * client,
183 GstRTSPContext * ctx);
184 static GstRTSPResult default_params_get (GstRTSPClient * client,
185 GstRTSPContext * ctx);
186 static gchar *default_make_path_from_uri (GstRTSPClient * client,
187 const GstRTSPUrl * uri);
188 static void client_session_removed (GstRTSPSessionPool * pool,
189 GstRTSPSession * session, GstRTSPClient * client);
190 static GstRTSPStatusCode default_pre_signal_handler (GstRTSPClient * client,
191 GstRTSPContext * ctx);
192 static gboolean pre_signal_accumulator (GSignalInvocationHint * ihint,
193 GValue * return_accu, const GValue * handler_return, gpointer data);
195 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
198 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
200 GObjectClass *gobject_class;
202 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
204 gobject_class = G_OBJECT_CLASS (klass);
206 gobject_class->get_property = gst_rtsp_client_get_property;
207 gobject_class->set_property = gst_rtsp_client_set_property;
208 gobject_class->finalize = gst_rtsp_client_finalize;
210 klass->create_sdp = create_sdp;
211 klass->handle_sdp = handle_sdp;
212 klass->configure_client_media = default_configure_client_media;
213 klass->configure_client_transport = default_configure_client_transport;
214 klass->params_set = default_params_set;
215 klass->params_get = default_params_get;
216 klass->make_path_from_uri = default_make_path_from_uri;
218 klass->pre_options_request = default_pre_signal_handler;
219 klass->pre_describe_request = default_pre_signal_handler;
220 klass->pre_setup_request = default_pre_signal_handler;
221 klass->pre_play_request = default_pre_signal_handler;
222 klass->pre_pause_request = default_pre_signal_handler;
223 klass->pre_teardown_request = default_pre_signal_handler;
224 klass->pre_set_parameter_request = default_pre_signal_handler;
225 klass->pre_get_parameter_request = default_pre_signal_handler;
226 klass->pre_announce_request = default_pre_signal_handler;
227 klass->pre_record_request = default_pre_signal_handler;
229 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
230 g_param_spec_object ("session-pool", "Session Pool",
231 "The session pool to use for client session",
232 GST_TYPE_RTSP_SESSION_POOL,
233 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
235 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
236 g_param_spec_object ("mount-points", "Mount Points",
237 "The mount points to use for client session",
238 GST_TYPE_RTSP_MOUNT_POINTS,
239 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
241 g_object_class_install_property (gobject_class, PROP_DROP_BACKLOG,
242 g_param_spec_boolean ("drop-backlog", "Drop Backlog",
243 "Drop data when the backlog queue is full",
244 DEFAULT_DROP_BACKLOG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
246 gst_rtsp_client_signals[SIGNAL_CLOSED] =
247 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
248 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
249 g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
251 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
252 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
253 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
254 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
257 * GstRTSPClient::pre-options-request:
258 * @client: a #GstRTSPClient
259 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
261 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
262 * otherwise an appropriate return code
266 gst_rtsp_client_signals[SIGNAL_PRE_OPTIONS_REQUEST] =
267 g_signal_new ("pre-options-request", G_TYPE_FROM_CLASS (klass),
268 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
269 pre_options_request), pre_signal_accumulator, NULL,
270 g_cclosure_marshal_generic, GST_TYPE_RTSP_STATUS_CODE, 1,
271 GST_TYPE_RTSP_CONTEXT);
274 * GstRTSPClient::options-request:
275 * @client: a #GstRTSPClient
276 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
278 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
279 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
280 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
281 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
282 GST_TYPE_RTSP_CONTEXT);
285 * GstRTSPClient::pre-describe-request:
286 * @client: a #GstRTSPClient
287 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
289 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
290 * otherwise an appropriate return code
294 gst_rtsp_client_signals[SIGNAL_PRE_DESCRIBE_REQUEST] =
295 g_signal_new ("pre-describe-request", G_TYPE_FROM_CLASS (klass),
296 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
297 pre_describe_request), pre_signal_accumulator, NULL,
298 g_cclosure_marshal_generic, GST_TYPE_RTSP_STATUS_CODE, 1,
299 GST_TYPE_RTSP_CONTEXT);
302 * GstRTSPClient::describe-request:
303 * @client: a #GstRTSPClient
304 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
306 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
307 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
308 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
309 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
310 GST_TYPE_RTSP_CONTEXT);
313 * GstRTSPClient::pre-setup-request:
314 * @client: a #GstRTSPClient
315 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
317 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
318 * otherwise an appropriate return code
322 gst_rtsp_client_signals[SIGNAL_PRE_SETUP_REQUEST] =
323 g_signal_new ("pre-setup-request", G_TYPE_FROM_CLASS (klass),
324 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
325 pre_setup_request), pre_signal_accumulator, NULL,
326 g_cclosure_marshal_generic, GST_TYPE_RTSP_STATUS_CODE, 1,
327 GST_TYPE_RTSP_CONTEXT);
330 * GstRTSPClient::setup-request:
331 * @client: a #GstRTSPClient
332 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
334 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
335 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
336 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
337 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
338 GST_TYPE_RTSP_CONTEXT);
341 * GstRTSPClient::pre-play-request:
342 * @client: a #GstRTSPClient
343 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
345 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
346 * otherwise an appropriate return code
350 gst_rtsp_client_signals[SIGNAL_PRE_PLAY_REQUEST] =
351 g_signal_new ("pre-play-request", G_TYPE_FROM_CLASS (klass),
352 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
353 pre_play_request), pre_signal_accumulator, NULL,
354 g_cclosure_marshal_generic, GST_TYPE_RTSP_STATUS_CODE, 1,
355 GST_TYPE_RTSP_CONTEXT);
358 * GstRTSPClient::play-request:
359 * @client: a #GstRTSPClient
360 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
362 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
363 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
364 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
365 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
366 GST_TYPE_RTSP_CONTEXT);
369 * GstRTSPClient::pre-pause-request:
370 * @client: a #GstRTSPClient
371 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
373 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
374 * otherwise an appropriate return code
378 gst_rtsp_client_signals[SIGNAL_PRE_PAUSE_REQUEST] =
379 g_signal_new ("pre-pause-request", G_TYPE_FROM_CLASS (klass),
380 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
381 pre_pause_request), pre_signal_accumulator, NULL,
382 g_cclosure_marshal_generic, GST_TYPE_RTSP_STATUS_CODE, 1,
383 GST_TYPE_RTSP_CONTEXT);
386 * GstRTSPClient::pause-request:
387 * @client: a #GstRTSPClient
388 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
390 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
391 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
392 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
393 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
394 GST_TYPE_RTSP_CONTEXT);
397 * GstRTSPClient::pre-teardown-request:
398 * @client: a #GstRTSPClient
399 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
401 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
402 * otherwise an appropriate return code
406 gst_rtsp_client_signals[SIGNAL_PRE_TEARDOWN_REQUEST] =
407 g_signal_new ("pre-teardown-request", G_TYPE_FROM_CLASS (klass),
408 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
409 pre_teardown_request), pre_signal_accumulator, NULL,
410 g_cclosure_marshal_generic, GST_TYPE_RTSP_STATUS_CODE, 1,
411 GST_TYPE_RTSP_CONTEXT);
414 * GstRTSPClient::teardown-request:
415 * @client: a #GstRTSPClient
416 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
418 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
419 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
420 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
421 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
422 GST_TYPE_RTSP_CONTEXT);
425 * GstRTSPClient::pre-set-parameter-request:
426 * @client: a #GstRTSPClient
427 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
429 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
430 * otherwise an appropriate return code
434 gst_rtsp_client_signals[SIGNAL_PRE_SET_PARAMETER_REQUEST] =
435 g_signal_new ("pre-set-parameter-request", G_TYPE_FROM_CLASS (klass),
436 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
437 pre_set_parameter_request), pre_signal_accumulator, NULL,
438 g_cclosure_marshal_generic,
439 GST_TYPE_RTSP_STATUS_CODE, 1, GST_TYPE_RTSP_CONTEXT);
442 * GstRTSPClient::set-parameter-request:
443 * @client: a #GstRTSPClient
444 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
446 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
447 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
448 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
449 set_parameter_request), NULL, NULL, g_cclosure_marshal_generic,
450 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
453 * GstRTSPClient::pre-get-parameter-request:
454 * @client: a #GstRTSPClient
455 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
457 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
458 * otherwise an appropriate return code
462 gst_rtsp_client_signals[SIGNAL_PRE_GET_PARAMETER_REQUEST] =
463 g_signal_new ("pre-get-parameter-request", G_TYPE_FROM_CLASS (klass),
464 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
465 pre_get_parameter_request), pre_signal_accumulator, NULL,
466 g_cclosure_marshal_generic, GST_TYPE_RTSP_STATUS_CODE, 1,
467 GST_TYPE_RTSP_CONTEXT);
470 * GstRTSPClient::get-parameter-request:
471 * @client: a #GstRTSPClient
472 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
474 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
475 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
476 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
477 get_parameter_request), NULL, NULL, g_cclosure_marshal_generic,
478 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
481 * GstRTSPClient::handle-response:
482 * @client: a #GstRTSPClient
483 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
485 gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE] =
486 g_signal_new ("handle-response", G_TYPE_FROM_CLASS (klass),
487 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
488 handle_response), NULL, NULL, g_cclosure_marshal_generic,
489 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
492 * GstRTSPClient::send-message:
493 * @client: The RTSP client
494 * @session: (type GstRtspServer.RTSPSession): The session
495 * @message: (type GstRtsp.RTSPMessage): The message
497 gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE] =
498 g_signal_new ("send-message", G_TYPE_FROM_CLASS (klass),
499 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
500 send_message), NULL, NULL, g_cclosure_marshal_generic,
501 G_TYPE_NONE, 2, GST_TYPE_RTSP_CONTEXT, G_TYPE_POINTER);
504 * GstRTSPClient::pre-announce-request:
505 * @client: a #GstRTSPClient
506 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
508 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
509 * otherwise an appropriate return code
513 gst_rtsp_client_signals[SIGNAL_PRE_ANNOUNCE_REQUEST] =
514 g_signal_new ("pre-announce-request", G_TYPE_FROM_CLASS (klass),
515 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
516 pre_announce_request), pre_signal_accumulator, NULL,
517 g_cclosure_marshal_generic, GST_TYPE_RTSP_STATUS_CODE, 1,
518 GST_TYPE_RTSP_CONTEXT);
521 * GstRTSPClient::announce-request:
522 * @client: a #GstRTSPClient
523 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
525 gst_rtsp_client_signals[SIGNAL_ANNOUNCE_REQUEST] =
526 g_signal_new ("announce-request", G_TYPE_FROM_CLASS (klass),
527 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, announce_request),
528 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
529 GST_TYPE_RTSP_CONTEXT);
532 * GstRTSPClient::pre-record-request:
533 * @client: a #GstRTSPClient
534 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
536 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
537 * otherwise an appropriate return code
541 gst_rtsp_client_signals[SIGNAL_PRE_RECORD_REQUEST] =
542 g_signal_new ("pre-record-request", G_TYPE_FROM_CLASS (klass),
543 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
544 pre_record_request), pre_signal_accumulator, NULL,
545 g_cclosure_marshal_generic, GST_TYPE_RTSP_STATUS_CODE, 1,
546 GST_TYPE_RTSP_CONTEXT);
549 * GstRTSPClient::record-request:
550 * @client: a #GstRTSPClient
551 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
553 gst_rtsp_client_signals[SIGNAL_RECORD_REQUEST] =
554 g_signal_new ("record-request", G_TYPE_FROM_CLASS (klass),
555 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, record_request),
556 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
557 GST_TYPE_RTSP_CONTEXT);
560 * GstRTSPClient::check-requirements:
561 * @client: a #GstRTSPClient
562 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
563 * @arr: a NULL-terminated array of strings
565 * Returns: a newly allocated string with comma-separated list of
566 * unsupported options. An empty string must be returned if
567 * all options are supported.
571 gst_rtsp_client_signals[SIGNAL_CHECK_REQUIREMENTS] =
572 g_signal_new ("check-requirements", G_TYPE_FROM_CLASS (klass),
573 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
574 check_requirements), NULL, NULL, g_cclosure_marshal_generic,
575 G_TYPE_STRING, 2, GST_TYPE_RTSP_CONTEXT, G_TYPE_STRV);
578 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
579 g_mutex_init (&tunnels_lock);
581 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
585 gst_rtsp_client_init (GstRTSPClient * client)
587 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
591 g_mutex_init (&priv->lock);
592 g_mutex_init (&priv->send_lock);
593 g_mutex_init (&priv->watch_lock);
595 priv->drop_backlog = DEFAULT_DROP_BACKLOG;
597 g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
599 priv->pipelined_requests = g_hash_table_new_full (g_str_hash,
600 g_str_equal, g_free, g_free);
601 priv->tstate = TUNNEL_STATE_UNKNOWN;
604 static GstRTSPFilterResult
605 filter_session_media (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
608 gboolean *closed = user_data;
611 gboolean is_all_udp = TRUE;
613 media = gst_rtsp_session_media_get_media (sessmedia);
614 n_streams = gst_rtsp_media_n_streams (media);
616 for (i = 0; i < n_streams; i++) {
617 GstRTSPStreamTransport *transport =
618 gst_rtsp_session_media_get_transport (sessmedia, i);
619 const GstRTSPTransport *rtsp_transport;
624 rtsp_transport = gst_rtsp_stream_transport_get_transport (transport);
626 && rtsp_transport->lower_transport != GST_RTSP_LOWER_TRANS_UDP
627 && rtsp_transport->lower_transport != GST_RTSP_LOWER_TRANS_UDP_MCAST) {
633 if (!is_all_udp || gst_rtsp_media_is_stop_on_disconnect (media)) {
634 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
635 return GST_RTSP_FILTER_REMOVE;
638 return GST_RTSP_FILTER_KEEP;
643 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
645 GstRTSPClientPrivate *priv = client->priv;
647 g_mutex_lock (&priv->lock);
648 /* check if we already know about this session */
649 if (g_list_find (priv->sessions, session) == NULL) {
650 GST_INFO ("watching session %p", session);
652 priv->sessions = g_list_prepend (priv->sessions, g_object_ref (session));
653 priv->sessions_cookie++;
655 /* connect removed session handler, it will be disconnected when the last
656 * session gets removed */
657 if (priv->session_removed_id == 0)
658 priv->session_removed_id = g_signal_connect_data (priv->session_pool,
659 "session-removed", G_CALLBACK (client_session_removed),
660 g_object_ref (client), (GClosureNotify) g_object_unref, 0);
662 g_mutex_unlock (&priv->lock);
667 /* should be called with lock */
669 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session,
672 GstRTSPClientPrivate *priv = client->priv;
674 GST_INFO ("client %p: unwatch session %p", client, session);
677 link = g_list_find (priv->sessions, session);
682 priv->sessions = g_list_delete_link (priv->sessions, link);
683 priv->sessions_cookie++;
685 /* if this was the last session, disconnect the handler.
686 * This will also drop the extra client ref */
687 if (!priv->sessions) {
688 g_signal_handler_disconnect (priv->session_pool, priv->session_removed_id);
689 priv->session_removed_id = 0;
692 if (!priv->drop_backlog) {
693 /* unlink all media managed in this session */
694 gst_rtsp_session_filter (session, filter_session_media, client);
697 /* remove the session */
698 g_object_unref (session);
701 static GstRTSPFilterResult
702 cleanup_session (GstRTSPClient * client, GstRTSPSession * sess,
705 gboolean *closed = user_data;
706 GstRTSPClientPrivate *priv = client->priv;
708 if (priv->drop_backlog) {
709 /* unlink all media managed in this session. This needs to happen
710 * without the client lock, so we really want to do it here. */
711 gst_rtsp_session_filter (sess, filter_session_media, user_data);
715 return GST_RTSP_FILTER_REMOVE;
717 return GST_RTSP_FILTER_KEEP;
721 clean_cached_media (GstRTSPClient * client, gboolean unprepare)
723 GstRTSPClientPrivate *priv = client->priv;
731 gst_rtsp_media_unprepare (priv->media);
732 g_object_unref (priv->media);
737 /* A client is finalized when the connection is broken */
739 gst_rtsp_client_finalize (GObject * obj)
741 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
742 GstRTSPClientPrivate *priv = client->priv;
744 GST_INFO ("finalize client %p", client);
747 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
748 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
751 g_source_destroy ((GSource *) priv->watch);
753 if (priv->watch_context)
754 g_main_context_unref (priv->watch_context);
756 /* all sessions should have been removed by now. We keep a ref to
757 * the client object for the session removed handler. The ref is
758 * dropped when the last session is removed from the list. */
759 g_assert (priv->sessions == NULL);
760 g_assert (priv->session_removed_id == 0);
762 g_hash_table_unref (priv->transports);
763 g_hash_table_unref (priv->pipelined_requests);
765 if (priv->connection)
766 gst_rtsp_connection_free (priv->connection);
767 if (priv->session_pool) {
768 g_object_unref (priv->session_pool);
770 if (priv->mount_points)
771 g_object_unref (priv->mount_points);
773 g_object_unref (priv->auth);
774 if (priv->thread_pool)
775 g_object_unref (priv->thread_pool);
777 clean_cached_media (client, TRUE);
779 g_free (priv->server_ip);
780 g_mutex_clear (&priv->lock);
781 g_mutex_clear (&priv->send_lock);
782 g_mutex_clear (&priv->watch_lock);
784 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
788 gst_rtsp_client_get_property (GObject * object, guint propid,
789 GValue * value, GParamSpec * pspec)
791 GstRTSPClient *client = GST_RTSP_CLIENT (object);
792 GstRTSPClientPrivate *priv = client->priv;
795 case PROP_SESSION_POOL:
796 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
798 case PROP_MOUNT_POINTS:
799 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
801 case PROP_DROP_BACKLOG:
802 g_value_set_boolean (value, priv->drop_backlog);
805 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
810 gst_rtsp_client_set_property (GObject * object, guint propid,
811 const GValue * value, GParamSpec * pspec)
813 GstRTSPClient *client = GST_RTSP_CLIENT (object);
814 GstRTSPClientPrivate *priv = client->priv;
817 case PROP_SESSION_POOL:
818 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
820 case PROP_MOUNT_POINTS:
821 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
823 case PROP_DROP_BACKLOG:
824 g_mutex_lock (&priv->lock);
825 priv->drop_backlog = g_value_get_boolean (value);
826 g_mutex_unlock (&priv->lock);
829 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
834 * gst_rtsp_client_new:
836 * Create a new #GstRTSPClient instance.
838 * Returns: (transfer full): a new #GstRTSPClient
841 gst_rtsp_client_new (void)
843 GstRTSPClient *result;
845 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
851 send_message (GstRTSPClient * client, GstRTSPContext * ctx,
852 GstRTSPMessage * message, gboolean close)
854 GstRTSPClientPrivate *priv = client->priv;
856 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
857 "GStreamer RTSP server");
859 /* remove any previous header */
860 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
862 /* add the new session header for new session ids */
864 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
865 gst_rtsp_session_get_header (ctx->session));
868 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
869 gst_rtsp_message_dump (message);
873 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
876 message->type_data.response.version =
877 ctx->request->type_data.request.version;
879 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE],
882 g_mutex_lock (&priv->send_lock);
884 priv->send_func (client, message, close, priv->send_data);
885 g_mutex_unlock (&priv->send_lock);
887 gst_rtsp_message_unset (message);
891 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
892 GstRTSPContext * ctx)
894 gst_rtsp_message_init_response (ctx->response, code,
895 gst_rtsp_status_as_text (code), ctx->request);
899 send_message (client, ctx, ctx->response, FALSE);
903 send_option_not_supported_response (GstRTSPClient * client,
904 GstRTSPContext * ctx, const gchar * unsupported_options)
906 GstRTSPStatusCode code = GST_RTSP_STS_OPTION_NOT_SUPPORTED;
908 gst_rtsp_message_init_response (ctx->response, code,
909 gst_rtsp_status_as_text (code), ctx->request);
911 if (unsupported_options != NULL) {
912 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_UNSUPPORTED,
913 unsupported_options);
918 send_message (client, ctx, ctx->response, FALSE);
922 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
924 if (path1 == NULL || path2 == NULL)
927 if (strlen (path1) != len2)
930 if (strncmp (path1, path2, len2))
936 /* this function is called to initially find the media for the DESCRIBE request
937 * but is cached for when the same client (without breaking the connection) is
938 * doing a setup for the exact same url. */
939 static GstRTSPMedia *
940 find_media (GstRTSPClient * client, GstRTSPContext * ctx, gchar * path,
943 GstRTSPClientPrivate *priv = client->priv;
944 GstRTSPMediaFactory *factory;
948 /* find the longest matching factory for the uri first */
949 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
953 ctx->factory = factory;
955 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
956 goto no_factory_access;
958 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
964 path_len = strlen (path);
966 if (!paths_are_equal (priv->path, path, path_len)) {
967 /* remove any previously cached values before we try to construct a new
969 clean_cached_media (client, TRUE);
971 /* prepare the media and add it to the pipeline */
972 if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
977 if (!(gst_rtsp_media_get_transport_mode (media) &
978 GST_RTSP_TRANSPORT_MODE_RECORD)) {
979 GstRTSPThread *thread;
981 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
982 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
986 /* prepare the media */
987 if (!gst_rtsp_media_prepare (media, thread))
991 /* now keep track of the uri and the media */
992 priv->path = g_strndup (path, path_len);
995 /* we have seen this path before, used cached media */
998 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
1001 g_object_unref (factory);
1002 ctx->factory = NULL;
1005 g_object_ref (media);
1012 GST_ERROR ("client %p: no factory for path %s", client, path);
1013 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1018 g_object_unref (factory);
1019 ctx->factory = NULL;
1020 GST_ERROR ("client %p: not authorized to see factory path %s", client,
1022 /* error reply is already sent */
1027 g_object_unref (factory);
1028 ctx->factory = NULL;
1029 GST_ERROR ("client %p: not authorized for factory path %s", client, path);
1030 /* error reply is already sent */
1035 GST_ERROR ("client %p: can't create media", client);
1036 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1037 g_object_unref (factory);
1038 ctx->factory = NULL;
1043 GST_ERROR ("client %p: can't create thread", client);
1044 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1045 g_object_unref (media);
1047 g_object_unref (factory);
1048 ctx->factory = NULL;
1053 GST_ERROR ("client %p: can't prepare media", client);
1054 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1055 g_object_unref (media);
1057 g_object_unref (factory);
1058 ctx->factory = NULL;
1064 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
1066 GstRTSPClientPrivate *priv = client->priv;
1067 GstRTSPMessage message = { 0 };
1068 gboolean ret = TRUE;
1069 GstMapInfo map_info;
1073 gst_rtsp_message_init_data (&message, channel);
1075 /* FIXME, need some sort of iovec RTSPMessage here */
1076 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
1079 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
1081 g_mutex_lock (&priv->send_lock);
1082 if (priv->send_func)
1083 ret = priv->send_func (client, &message, FALSE, priv->send_data);
1084 g_mutex_unlock (&priv->send_lock);
1086 gst_rtsp_message_steal_body (&message, &data, &usize);
1087 gst_buffer_unmap (buffer, &map_info);
1089 gst_rtsp_message_unset (&message);
1095 * gst_rtsp_client_close:
1096 * @client: a #GstRTSPClient
1098 * Close the connection of @client and remove all media it was managing.
1103 gst_rtsp_client_close (GstRTSPClient * client)
1105 GstRTSPClientPrivate *priv = client->priv;
1106 const gchar *tunnelid;
1108 GST_DEBUG ("client %p: closing connection", client);
1110 if (priv->connection) {
1111 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
1112 g_mutex_lock (&tunnels_lock);
1113 /* remove from tunnelids */
1114 g_hash_table_remove (tunnels, tunnelid);
1115 g_mutex_unlock (&tunnels_lock);
1117 gst_rtsp_connection_close (priv->connection);
1120 /* connection is now closed, destroy the watch which will also cause the
1121 * closed signal to be emitted */
1123 GST_DEBUG ("client %p: destroying watch", client);
1124 g_source_destroy ((GSource *) priv->watch);
1126 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
1127 g_main_context_unref (priv->watch_context);
1128 priv->watch_context = NULL;
1133 default_make_path_from_uri (GstRTSPClient * client, const GstRTSPUrl * uri)
1138 path = g_strconcat (uri->abspath, "?", uri->query, NULL);
1140 path = g_strdup (uri->abspath);
1145 /* Default signal handler function for all "pre-command" signals, like
1146 * pre-options-request. It just returns the RTSP return code 200.
1147 * Subclasses can override this to get another default behaviour.
1149 static GstRTSPStatusCode
1150 default_pre_signal_handler (GstRTSPClient * client, GstRTSPContext * ctx)
1152 GST_LOG_OBJECT (client, "returning GST_RTSP_STS_OK");
1153 return GST_RTSP_STS_OK;
1156 /* The pre-signal accumulator function checks the return value of the signal
1157 * handlers. If any of them returns an RTSP status code that does not start
1158 * with 2 it will return FALSE, no more signal handlers will be called, and
1159 * this last RTSP status code will be the result of the signal emission.
1162 pre_signal_accumulator (GSignalInvocationHint * ihint, GValue * return_accu,
1163 const GValue * handler_return, gpointer data)
1165 GstRTSPStatusCode handler_value = g_value_get_enum (handler_return);
1166 GstRTSPStatusCode accumulated_value = g_value_get_enum (return_accu);
1168 if (handler_value < 200 || handler_value > 299) {
1169 GST_DEBUG ("handler_value : %d, returning FALSE", handler_value);
1170 g_value_set_enum (return_accu, handler_value);
1174 /* the accumulated value is initiated to 0 by GLib. if current handler value is
1175 * bigger then use that instead
1177 * FIXME: Should we prioritize the 2xx codes in a smarter way?
1178 * Like, "201 Created" > "250 Low On Storage Space" > "200 OK"?
1180 if (handler_value > accumulated_value)
1181 g_value_set_enum (return_accu, handler_value);
1187 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
1189 GstRTSPClientPrivate *priv = client->priv;
1190 GstRTSPClientClass *klass;
1191 GstRTSPSession *session;
1192 GstRTSPSessionMedia *sessmedia;
1193 GstRTSPStatusCode code;
1196 gboolean keep_session;
1197 GstRTSPStatusCode sig_result;
1202 session = ctx->session;
1207 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1208 path = klass->make_path_from_uri (client, ctx->uri);
1210 /* get a handle to the configuration of the media in the session */
1211 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1215 /* only aggregate control for now.. */
1216 if (path[matched] != '\0')
1221 ctx->sessmedia = sessmedia;
1223 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_TEARDOWN_REQUEST],
1224 0, ctx, &sig_result);
1225 if (sig_result != GST_RTSP_STS_OK) {
1229 /* we emit the signal before closing the connection */
1230 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
1233 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
1235 /* unmanage the media in the session, returns false if all media session
1237 keep_session = gst_rtsp_session_release_media (session, sessmedia);
1239 /* construct the response now */
1240 code = GST_RTSP_STS_OK;
1241 gst_rtsp_message_init_response (ctx->response, code,
1242 gst_rtsp_status_as_text (code), ctx->request);
1244 send_message (client, ctx, ctx->response, TRUE);
1246 if (!keep_session) {
1247 /* remove the session */
1248 gst_rtsp_session_pool_remove (priv->session_pool, session);
1256 GST_ERROR ("client %p: no session", client);
1257 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1262 GST_ERROR ("client %p: no uri supplied", client);
1263 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1268 GST_ERROR ("client %p: no media for uri", client);
1269 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1275 GST_ERROR ("client %p: no aggregate path %s", client, path);
1276 send_generic_response (client,
1277 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1283 GST_ERROR ("client %p: pre signal returned error: %s", client,
1284 gst_rtsp_status_as_text (sig_result));
1285 send_generic_response (client, sig_result, ctx);
1290 static GstRTSPResult
1291 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
1295 res = gst_rtsp_params_set (client, ctx);
1300 static GstRTSPResult
1301 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
1305 res = gst_rtsp_params_get (client, ctx);
1311 handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
1316 GstRTSPStatusCode sig_result;
1318 g_signal_emit (client,
1319 gst_rtsp_client_signals[SIGNAL_PRE_GET_PARAMETER_REQUEST], 0, ctx,
1321 if (sig_result != GST_RTSP_STS_OK) {
1325 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
1326 if (res != GST_RTSP_OK)
1329 if (size == 0 || !data || strlen ((char *) data) == 0) {
1330 if (ctx->request->type_data.request.version >= GST_RTSP_VERSION_2_0) {
1331 GST_ERROR_OBJECT (client, "Using PLAY request for keep-alive is forbidden"
1336 /* no body (or only '\0'), keep-alive request */
1337 send_generic_response (client, GST_RTSP_STS_OK, ctx);
1339 /* there is a body, handle the params */
1340 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
1341 if (res != GST_RTSP_OK)
1344 send_message (client, ctx, ctx->response, FALSE);
1347 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
1355 GST_ERROR ("client %p: pre signal returned error: %s", client,
1356 gst_rtsp_status_as_text (sig_result));
1357 send_generic_response (client, sig_result, ctx);
1362 GST_ERROR ("client %p: bad request", client);
1363 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1369 handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
1374 GstRTSPStatusCode sig_result;
1376 g_signal_emit (client,
1377 gst_rtsp_client_signals[SIGNAL_PRE_SET_PARAMETER_REQUEST], 0, ctx,
1379 if (sig_result != GST_RTSP_STS_OK) {
1383 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
1384 if (res != GST_RTSP_OK)
1387 if (size == 0 || !data || strlen ((char *) data) == 0) {
1388 /* no body (or only '\0'), keep-alive request */
1389 send_generic_response (client, GST_RTSP_STS_OK, ctx);
1391 /* there is a body, handle the params */
1392 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
1393 if (res != GST_RTSP_OK)
1396 send_message (client, ctx, ctx->response, FALSE);
1399 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
1407 GST_ERROR ("client %p: pre signal returned error: %s", client,
1408 gst_rtsp_status_as_text (sig_result));
1409 send_generic_response (client, sig_result, ctx);
1414 GST_ERROR ("client %p: bad request", client);
1415 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1421 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
1423 GstRTSPSession *session;
1424 GstRTSPClientClass *klass;
1425 GstRTSPSessionMedia *sessmedia;
1426 GstRTSPMedia *media;
1427 GstRTSPStatusCode code;
1428 GstRTSPState rtspstate;
1431 GstRTSPStatusCode sig_result;
1434 if (!(session = ctx->session))
1440 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1441 path = klass->make_path_from_uri (client, ctx->uri);
1443 /* get a handle to the configuration of the media in the session */
1444 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1448 if (path[matched] != '\0')
1453 media = gst_rtsp_session_media_get_media (sessmedia);
1454 n = gst_rtsp_media_n_streams (media);
1455 for (i = 0; i < n; i++) {
1456 GstRTSPStream *stream = gst_rtsp_media_get_stream (media, i);
1458 if (gst_rtsp_stream_get_publish_clock_mode (stream) ==
1459 GST_RTSP_PUBLISH_CLOCK_MODE_CLOCK_AND_OFFSET)
1463 ctx->sessmedia = sessmedia;
1465 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_PAUSE_REQUEST], 0,
1467 if (sig_result != GST_RTSP_STS_OK) {
1471 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1472 /* the session state must be playing or recording */
1473 if (rtspstate != GST_RTSP_STATE_PLAYING &&
1474 rtspstate != GST_RTSP_STATE_RECORDING)
1477 /* then pause sending */
1478 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
1480 /* construct the response now */
1481 code = GST_RTSP_STS_OK;
1482 gst_rtsp_message_init_response (ctx->response, code,
1483 gst_rtsp_status_as_text (code), ctx->request);
1485 send_message (client, ctx, ctx->response, FALSE);
1487 /* the state is now READY */
1488 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1490 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
1497 GST_ERROR ("client %p: no session", client);
1498 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1503 GST_ERROR ("client %p: no uri supplied", client);
1504 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1509 GST_ERROR ("client %p: no media for uri", client);
1510 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1516 GST_ERROR ("client %p: no aggregate path %s", client, path);
1517 send_generic_response (client,
1518 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1524 GST_ERROR ("client %p: pre signal returned error: %s", client,
1525 gst_rtsp_status_as_text (sig_result));
1526 send_generic_response (client, sig_result, ctx);
1531 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
1532 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1538 GST_ERROR ("client %p: pausing not supported", client);
1539 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1544 /* convert @url and @path to a URL used as a content base for the factory
1545 * located at @path */
1547 make_base_url (GstRTSPClient * client, GstRTSPUrl * url, const gchar * path)
1553 /* check for trailing '/' and append one */
1554 trail = (path[strlen (path) - 1] != '/' ? "/" : "");
1559 tmp.abspath = g_strdup_printf ("%s%s", path, trail);
1561 result = gst_rtsp_url_get_request_uri (&tmp);
1562 g_free (tmp.abspath);
1568 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
1570 GstRTSPSession *session;
1571 GstRTSPClientClass *klass;
1572 GstRTSPSessionMedia *sessmedia;
1573 GstRTSPMedia *media;
1574 GstRTSPStatusCode code;
1577 GstRTSPTimeRange *range;
1579 GstRTSPState rtspstate;
1580 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1581 gchar *path, *rtpinfo;
1583 gchar *seek_style = NULL;
1584 GstRTSPStatusCode sig_result;
1585 GPtrArray *transports;
1587 if (!(session = ctx->session))
1590 if (!(uri = ctx->uri))
1593 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1594 path = klass->make_path_from_uri (client, uri);
1596 /* get a handle to the configuration of the media in the session */
1597 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1601 if (path[matched] != '\0')
1606 ctx->sessmedia = sessmedia;
1607 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1609 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_PLAY_REQUEST], 0,
1611 if (sig_result != GST_RTSP_STS_OK) {
1615 if (!(gst_rtsp_media_get_transport_mode (media) &
1616 GST_RTSP_TRANSPORT_MODE_PLAY))
1617 goto unsupported_mode;
1619 /* the session state must be playing or ready */
1620 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1621 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1624 /* update the pipeline */
1625 transports = gst_rtsp_session_media_get_transports (sessmedia);
1626 if (!gst_rtsp_media_complete_pipeline (media, transports)) {
1627 g_ptr_array_unref (transports);
1628 goto pipeline_error;
1630 g_ptr_array_unref (transports);
1632 /* in play we first unsuspend, media could be suspended from SDP or PAUSED */
1633 if (!gst_rtsp_media_unsuspend (media))
1634 goto unsuspend_failed;
1636 /* parse the range header if we have one */
1637 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1638 if (res == GST_RTSP_OK) {
1639 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1640 GstRTSPMediaStatus media_status;
1641 GstSeekFlags flags = 0;
1643 if (gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_SEEK_STYLE,
1645 if (g_strcmp0 (seek_style, "RAP") == 0)
1646 flags = GST_SEEK_FLAG_ACCURATE;
1647 else if (g_strcmp0 (seek_style, "CoRAP") == 0)
1648 flags = GST_SEEK_FLAG_KEY_UNIT;
1649 else if (g_strcmp0 (seek_style, "First-Prior") == 0)
1650 flags = GST_SEEK_FLAG_KEY_UNIT & GST_SEEK_FLAG_SNAP_BEFORE;
1651 else if (g_strcmp0 (seek_style, "Next") == 0)
1652 flags = GST_SEEK_FLAG_KEY_UNIT & GST_SEEK_FLAG_SNAP_AFTER;
1654 GST_FIXME_OBJECT (client, "Add support for seek style %s",
1658 /* we have a range, seek to the position */
1660 gst_rtsp_media_seek_full (media, range, flags);
1661 gst_rtsp_range_free (range);
1663 media_status = gst_rtsp_media_get_status (media);
1664 if (media_status == GST_RTSP_MEDIA_STATUS_ERROR)
1669 /* grab RTPInfo from the media now */
1670 rtpinfo = gst_rtsp_session_media_get_rtpinfo (sessmedia);
1672 /* construct the response now */
1673 code = GST_RTSP_STS_OK;
1674 gst_rtsp_message_init_response (ctx->response, code,
1675 gst_rtsp_status_as_text (code), ctx->request);
1677 /* add the RTP-Info header */
1679 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO,
1682 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_SEEK_STYLE,
1686 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1688 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1690 send_message (client, ctx, ctx->response, FALSE);
1692 /* start playing after sending the response */
1693 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1695 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1697 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1704 GST_ERROR ("client %p: no session", client);
1705 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1710 GST_ERROR ("client %p: no uri supplied", client);
1711 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1716 GST_ERROR ("client %p: media not found", client);
1717 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1722 GST_ERROR ("client %p: no aggregate path %s", client, path);
1723 send_generic_response (client,
1724 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1730 GST_ERROR ("client %p: pre signal returned error: %s", client,
1731 gst_rtsp_status_as_text (sig_result));
1732 send_generic_response (client, sig_result, ctx);
1737 GST_ERROR ("client %p: not PLAYING or READY", client);
1738 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1744 GST_ERROR ("client %p: failed to configure the pipeline", client);
1745 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1751 GST_ERROR ("client %p: unsuspend failed", client);
1752 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1757 GST_ERROR ("client %p: seek failed", client);
1758 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1763 GST_ERROR ("client %p: media does not support PLAY", client);
1764 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_ALLOWED, ctx);
1770 do_keepalive (GstRTSPSession * session)
1772 GST_INFO ("keep session %p alive", session);
1773 gst_rtsp_session_touch (session);
1776 /* parse @transport and return a valid transport in @tr. only transports
1777 * supported by @stream are returned. Returns FALSE if no valid transport
1780 parse_transport (const char *transport, GstRTSPStream * stream,
1781 GstRTSPTransport * tr)
1788 gst_rtsp_transport_init (tr);
1790 GST_DEBUG ("parsing transports %s", transport);
1792 transports = g_strsplit (transport, ",", 0);
1794 /* loop through the transports, try to parse */
1795 for (i = 0; transports[i]; i++) {
1796 res = gst_rtsp_transport_parse (transports[i], tr);
1797 if (res != GST_RTSP_OK) {
1798 /* no valid transport, search some more */
1799 GST_WARNING ("could not parse transport %s", transports[i]);
1803 /* we have a transport, see if it's supported */
1804 if (!gst_rtsp_stream_is_transport_supported (stream, tr)) {
1805 GST_WARNING ("unsupported transport %s", transports[i]);
1809 /* we have a valid transport */
1810 GST_INFO ("found valid transport %s", transports[i]);
1815 gst_rtsp_transport_init (tr);
1817 g_strfreev (transports);
1823 default_configure_client_media (GstRTSPClient * client, GstRTSPMedia * media,
1824 GstRTSPStream * stream, GstRTSPContext * ctx)
1826 GstRTSPMessage *request = ctx->request;
1827 gchar *blocksize_str;
1829 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1830 &blocksize_str, 0) == GST_RTSP_OK) {
1834 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1835 if (end == blocksize_str)
1838 /* we don't want to change the mtu when this media
1839 * can be shared because it impacts other clients */
1840 if (gst_rtsp_media_is_shared (media))
1843 if (blocksize > G_MAXUINT)
1844 blocksize = G_MAXUINT;
1846 gst_rtsp_stream_set_mtu (stream, blocksize);
1854 GST_ERROR_OBJECT (client, "failed to parse blocksize");
1855 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1861 default_configure_client_transport (GstRTSPClient * client,
1862 GstRTSPContext * ctx, GstRTSPTransport * ct)
1864 GstRTSPClientPrivate *priv = client->priv;
1866 /* we have a valid transport now, set the destination of the client. */
1867 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST ||
1868 ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP) {
1870 /* allocate UDP ports */
1871 GSocketFamily family;
1872 gboolean use_client_settings = FALSE;
1874 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1875 if ((ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) &&
1876 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS) &&
1877 (ct->destination != NULL))
1878 use_client_settings = TRUE;
1880 if (!gst_rtsp_stream_allocate_udp_sockets (ctx->stream, family, ct,
1881 use_client_settings))
1882 goto error_allocating_ports;
1884 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1885 GstRTSPAddress *addr = NULL;
1887 if (use_client_settings) {
1888 /* the address has been successfully allocated, let's check if it's
1889 * the one requested by the client */
1890 addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1891 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1896 g_free (ct->destination);
1897 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1900 ct->destination = g_strdup (addr->address);
1901 ct->port.min = addr->port;
1902 ct->port.max = addr->port + addr->n_ports - 1;
1903 ct->ttl = addr->ttl;
1906 gst_rtsp_address_free (addr);
1910 url = gst_rtsp_connection_get_url (priv->connection);
1911 g_free (ct->destination);
1912 ct->destination = g_strdup (url->host);
1917 url = gst_rtsp_connection_get_url (priv->connection);
1918 g_free (ct->destination);
1919 ct->destination = g_strdup (url->host);
1921 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1923 GSocketAddress *addr;
1925 sock = gst_rtsp_connection_get_read_socket (priv->connection);
1926 if ((addr = g_socket_get_remote_address (sock, NULL))) {
1927 /* our read port is the sender port of client */
1928 ct->client_port.min =
1929 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1930 g_object_unref (addr);
1932 if ((addr = g_socket_get_local_address (sock, NULL))) {
1933 ct->server_port.max =
1934 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1935 g_object_unref (addr);
1937 sock = gst_rtsp_connection_get_write_socket (priv->connection);
1938 if ((addr = g_socket_get_remote_address (sock, NULL))) {
1939 /* our write port is the receiver port of client */
1940 ct->client_port.max =
1941 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1942 g_object_unref (addr);
1944 if ((addr = g_socket_get_local_address (sock, NULL))) {
1945 ct->server_port.min =
1946 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1947 g_object_unref (addr);
1949 /* check if the client selected channels for TCP */
1950 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1951 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1959 error_allocating_ports:
1961 GST_ERROR_OBJECT (client, "Failed to allocate UDP ports");
1966 GST_ERROR_OBJECT (client, "Failed to acquire address for stream");
1971 static GstRTSPTransport *
1972 make_server_transport (GstRTSPClient * client, GstRTSPMedia * media,
1973 GstRTSPContext * ctx, GstRTSPTransport * ct)
1975 GstRTSPTransport *st;
1977 GSocketFamily family;
1979 /* prepare the server transport */
1980 gst_rtsp_transport_new (&st);
1982 st->trans = ct->trans;
1983 st->profile = ct->profile;
1984 st->lower_transport = ct->lower_transport;
1985 st->mode_play = ct->mode_play;
1986 st->mode_record = ct->mode_record;
1988 addr = g_inet_address_new_from_string (ct->destination);
1991 GST_ERROR ("failed to get inet addr from client destination");
1992 family = G_SOCKET_FAMILY_IPV4;
1994 family = g_inet_address_get_family (addr);
1995 g_object_unref (addr);
1999 switch (st->lower_transport) {
2000 case GST_RTSP_LOWER_TRANS_UDP:
2001 st->client_port = ct->client_port;
2002 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
2004 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
2005 st->port = ct->port;
2006 st->destination = g_strdup (ct->destination);
2009 case GST_RTSP_LOWER_TRANS_TCP:
2010 st->interleaved = ct->interleaved;
2011 st->client_port = ct->client_port;
2012 st->server_port = ct->server_port;
2017 if ((gst_rtsp_media_get_transport_mode (media) &
2018 GST_RTSP_TRANSPORT_MODE_PLAY))
2019 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
2025 rtsp_ctrl_timeout_cb (gpointer user_data)
2027 gboolean res = G_SOURCE_CONTINUE;
2028 GstRTSPClient *client = (GstRTSPClient *) user_data;
2029 GstRTSPClientPrivate *priv = client->priv;
2031 priv->rtsp_ctrl_timeout_cnt += RTSP_CTRL_CB_INTERVAL;
2033 if (priv->rtsp_ctrl_timeout_cnt > RTSP_CTRL_TIMEOUT_VALUE) {
2034 GST_DEBUG ("rtsp control session timeout id=%u expired, closing client.",
2035 priv->rtsp_ctrl_timeout_id);
2036 g_mutex_lock (&priv->lock);
2037 priv->rtsp_ctrl_timeout_id = 0;
2038 priv->rtsp_ctrl_timeout_cnt = 0;
2039 g_mutex_unlock (&priv->lock);
2040 gst_rtsp_client_close (client);
2042 res = G_SOURCE_REMOVE;
2049 rtsp_ctrl_timeout_remove (GstRTSPClientPrivate * priv)
2051 g_mutex_lock (&priv->lock);
2053 if (priv->rtsp_ctrl_timeout_id != 0) {
2054 g_source_destroy (g_main_context_find_source_by_id (priv->watch_context,
2055 priv->rtsp_ctrl_timeout_id));
2056 GST_DEBUG ("rtsp control session removed timeout id=%u.",
2057 priv->rtsp_ctrl_timeout_id);
2058 priv->rtsp_ctrl_timeout_id = 0;
2059 priv->rtsp_ctrl_timeout_cnt = 0;
2062 g_mutex_unlock (&priv->lock);
2066 stream_make_keymgmt (GstRTSPClient * client, const gchar * location,
2067 GstRTSPStream * stream)
2069 gchar *base64, *result = NULL;
2070 GstMIKEYMessage *mikey_msg;
2071 GstCaps *srtcpparams;
2072 GstElement *rtcp_encoder;
2073 gint srtcp_cipher, srtp_cipher;
2074 gint srtcp_auth, srtp_auth;
2076 GType ciphertype, authtype;
2077 GEnumClass *cipher_enum, *auth_enum;
2078 GEnumValue *srtcp_cipher_value, *srtp_cipher_value, *srtcp_auth_value,
2081 rtcp_encoder = gst_rtsp_stream_get_srtp_encoder (stream);
2086 ciphertype = g_type_from_name ("GstSrtpCipherType");
2087 authtype = g_type_from_name ("GstSrtpAuthType");
2089 cipher_enum = g_type_class_ref (ciphertype);
2090 auth_enum = g_type_class_ref (authtype);
2092 /* We need to bring the encoder to READY so that it generates its key */
2093 gst_element_set_state (rtcp_encoder, GST_STATE_READY);
2095 g_object_get (rtcp_encoder, "rtcp-cipher", &srtcp_cipher, "rtcp-auth",
2096 &srtcp_auth, "rtp-cipher", &srtp_cipher, "rtp-auth", &srtp_auth, "key",
2098 g_object_unref (rtcp_encoder);
2100 srtcp_cipher_value = g_enum_get_value (cipher_enum, srtcp_cipher);
2101 srtp_cipher_value = g_enum_get_value (cipher_enum, srtp_cipher);
2102 srtcp_auth_value = g_enum_get_value (auth_enum, srtcp_auth);
2103 srtp_auth_value = g_enum_get_value (auth_enum, srtp_auth);
2105 g_type_class_unref (cipher_enum);
2106 g_type_class_unref (auth_enum);
2108 srtcpparams = gst_caps_new_simple ("application/x-srtcp",
2109 "srtcp-cipher", G_TYPE_STRING, srtcp_cipher_value->value_nick,
2110 "srtcp-auth", G_TYPE_STRING, srtcp_auth_value->value_nick,
2111 "srtp-cipher", G_TYPE_STRING, srtp_cipher_value->value_nick,
2112 "srtp-auth", G_TYPE_STRING, srtp_auth_value->value_nick,
2113 "srtp-key", GST_TYPE_BUFFER, key, NULL);
2115 mikey_msg = gst_mikey_message_new_from_caps (srtcpparams);
2119 gst_rtsp_stream_get_ssrc (stream, &send_ssrc);
2120 gst_mikey_message_add_cs_srtp (mikey_msg, 0, send_ssrc, 0);
2122 base64 = gst_mikey_message_base64_encode (mikey_msg);
2123 gst_mikey_message_unref (mikey_msg);
2126 result = gst_sdp_make_keymgmt (location, base64);
2136 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
2138 GstRTSPClientPrivate *priv = client->priv;
2141 gchar *transport, *keymgmt;
2142 GstRTSPTransport *ct, *st;
2143 GstRTSPStatusCode code;
2144 GstRTSPSession *session;
2145 GstRTSPStreamTransport *trans;
2147 GstRTSPSessionMedia *sessmedia;
2148 GstRTSPMedia *media;
2149 GstRTSPStream *stream;
2150 GstRTSPState rtspstate;
2151 GstRTSPClientClass *klass;
2152 gchar *path, *control = NULL;
2154 gboolean new_session = FALSE;
2155 GstRTSPStatusCode sig_result;
2156 gchar *pipelined_request_id = NULL, *accept_range = NULL;
2162 klass = GST_RTSP_CLIENT_GET_CLASS (client);
2163 path = klass->make_path_from_uri (client, uri);
2165 /* parse the transport */
2167 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
2169 if (res != GST_RTSP_OK)
2172 /* Handle Pipelined-requests if using >= 2.0 */
2173 if (ctx->request->type_data.request.version >= GST_RTSP_VERSION_2_0)
2174 gst_rtsp_message_get_header (ctx->request,
2175 GST_RTSP_HDR_PIPELINED_REQUESTS, &pipelined_request_id, 0);
2177 /* we create the session after parsing stuff so that we don't make
2178 * a session for malformed requests */
2179 if (priv->session_pool == NULL)
2182 session = ctx->session;
2185 g_object_ref (session);
2186 /* get a handle to the configuration of the media in the session, this can
2187 * return NULL if this is a new url to manage in this session. */
2188 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
2190 /* we need a new media configuration in this session */
2194 /* we have no session media, find one and manage it */
2195 if (sessmedia == NULL) {
2196 /* get a handle to the configuration of the media in the session */
2197 media = find_media (client, ctx, path, &matched);
2198 /* need to suspend the media, if the protocol has changed */
2200 gst_rtsp_media_suspend (media);
2202 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
2203 g_object_ref (media);
2205 goto media_not_found;
2207 /* no media, not found then */
2209 goto media_not_found_no_reply;
2211 if (path[matched] == '\0') {
2212 if (gst_rtsp_media_n_streams (media) == 1) {
2213 stream = gst_rtsp_media_get_stream (media, 0);
2215 goto control_not_found;
2218 /* path is what matched. */
2219 path[matched] = '\0';
2220 /* control is remainder */
2221 control = &path[matched + 1];
2223 /* find the stream now using the control part */
2224 stream = gst_rtsp_media_find_stream (media, control);
2228 goto stream_not_found;
2230 /* now we have a uri identifying a valid media and stream */
2231 ctx->stream = stream;
2234 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_SETUP_REQUEST], 0,
2236 if (sig_result != GST_RTSP_STS_OK) {
2240 if (session == NULL) {
2241 /* create a session if this fails we probably reached our session limit or
2243 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
2244 goto service_unavailable;
2246 /* Pipelined requests should be cleared between sessions */
2247 g_hash_table_remove_all (priv->pipelined_requests);
2249 /* make sure this client is closed when the session is closed */
2250 client_watch_session (client, session);
2253 /* signal new session */
2254 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
2257 ctx->session = session;
2260 if (pipelined_request_id) {
2261 g_hash_table_insert (client->priv->pipelined_requests,
2262 g_strdup (pipelined_request_id),
2263 g_strdup (gst_rtsp_session_get_sessionid (session)));
2265 rtsp_ctrl_timeout_remove (priv);
2267 if (!klass->configure_client_media (client, media, stream, ctx))
2268 goto configure_media_failed_no_reply;
2270 gst_rtsp_transport_new (&ct);
2272 /* parse and find a usable supported transport */
2273 if (!parse_transport (transport, stream, ct))
2274 goto unsupported_transports;
2277 && !(gst_rtsp_media_get_transport_mode (media) &
2278 GST_RTSP_TRANSPORT_MODE_PLAY)) || (ct->mode_record
2279 && !(gst_rtsp_media_get_transport_mode (media) &
2280 GST_RTSP_TRANSPORT_MODE_RECORD)))
2281 goto unsupported_mode;
2283 /* parse the keymgmt */
2284 if (gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_KEYMGMT,
2285 &keymgmt, 0) == GST_RTSP_OK) {
2286 if (!gst_rtsp_stream_handle_keymgmt (ctx->stream, keymgmt))
2290 if (gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT_RANGES,
2291 &accept_range, 0) == GST_RTSP_OK) {
2292 GEnumValue *runit = NULL;
2294 gchar **valid_ranges;
2295 GEnumClass *runit_class = g_type_class_ref (GST_TYPE_RTSP_RANGE_UNIT);
2297 gst_rtsp_message_dump (ctx->request);
2298 valid_ranges = g_strsplit (accept_range, ",", -1);
2300 for (i = 0; valid_ranges[i]; i++) {
2301 gchar *range = valid_ranges[i];
2303 while (*range == ' ')
2306 runit = g_enum_get_value_by_nick (runit_class, range);
2310 g_strfreev (valid_ranges);
2311 g_type_class_unref (runit_class);
2314 goto unsupported_range_unit;
2317 if (sessmedia == NULL) {
2318 /* manage the media in our session now, if not done already */
2320 gst_rtsp_session_manage_media (session, path, g_object_ref (media));
2321 /* if we stil have no media, error */
2322 if (sessmedia == NULL)
2323 goto sessmedia_unavailable;
2325 /* don't cache media anymore */
2326 clean_cached_media (client, FALSE);
2329 ctx->sessmedia = sessmedia;
2331 /* update the client transport */
2332 if (!klass->configure_client_transport (client, ctx, ct))
2333 goto unsupported_client_transport;
2335 /* set in the session media transport */
2336 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
2340 /* configure the url used to set this transport, this we will use when
2341 * generating the response for the PLAY request */
2342 gst_rtsp_stream_transport_set_url (trans, uri);
2343 /* configure keepalive for this transport */
2344 gst_rtsp_stream_transport_set_keepalive (trans,
2345 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
2347 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2348 /* our callbacks to send data on this TCP connection */
2349 gst_rtsp_stream_transport_set_callbacks (trans,
2350 (GstRTSPSendFunc) do_send_data,
2351 (GstRTSPSendFunc) do_send_data, client, NULL);
2353 g_hash_table_insert (priv->transports,
2354 GINT_TO_POINTER (ct->interleaved.min), trans);
2355 g_object_ref (trans);
2356 g_hash_table_insert (priv->transports,
2357 GINT_TO_POINTER (ct->interleaved.max), trans);
2358 g_object_ref (trans);
2361 /* create and serialize the server transport */
2362 st = make_server_transport (client, media, ctx, ct);
2363 trans_str = gst_rtsp_transport_as_text (st);
2364 gst_rtsp_transport_free (st);
2366 /* construct the response now */
2367 code = GST_RTSP_STS_OK;
2368 gst_rtsp_message_init_response (ctx->response, code,
2369 gst_rtsp_status_as_text (code), ctx->request);
2371 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
2375 if (pipelined_request_id)
2376 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PIPELINED_REQUESTS,
2377 pipelined_request_id);
2379 if (ctx->request->type_data.request.version >= GST_RTSP_VERSION_2_0) {
2380 GstClockTimeDiff seekable = gst_rtsp_media_seekable (media);
2381 GString *media_properties = g_string_new (NULL);
2384 g_string_append (media_properties,
2385 "No-Seeking,Time-Progressing,Time-Duration=0.0");
2386 else if (seekable == 0)
2387 g_string_append (media_properties, "Beginning-Only");
2388 else if (seekable == G_MAXINT64)
2389 g_string_append (media_properties, "Random-Access");
2391 g_string_append_printf (media_properties,
2392 "Random-Access=%f, Unlimited, Immutable",
2393 (gdouble) seekable / GST_SECOND);
2395 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_MEDIA_PROPERTIES,
2396 g_string_free (media_properties, FALSE));
2397 /* TODO Check how Accept-Ranges should be filled */
2398 gst_rtsp_message_add_header (ctx->request, GST_RTSP_HDR_ACCEPT_RANGES,
2399 "npt, clock, smpte, clock");
2402 send_message (client, ctx, ctx->response, FALSE);
2404 /* update the state */
2405 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
2406 switch (rtspstate) {
2407 case GST_RTSP_STATE_PLAYING:
2408 case GST_RTSP_STATE_RECORDING:
2409 case GST_RTSP_STATE_READY:
2410 /* no state change */
2413 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
2416 g_object_unref (media);
2417 g_object_unref (session);
2420 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
2427 GST_ERROR ("client %p: no uri", client);
2428 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2433 GST_ERROR ("client %p: no transport", client);
2434 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
2439 GST_ERROR ("client %p: no session pool configured", client);
2440 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2443 media_not_found_no_reply:
2445 GST_ERROR ("client %p: media '%s' not found", client, path);
2446 /* error reply is already sent */
2447 goto cleanup_session;
2451 GST_ERROR ("client %p: media '%s' not found", client, path);
2452 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2453 goto cleanup_session;
2457 GST_ERROR ("client %p: no control in path '%s'", client, path);
2458 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2459 g_object_unref (media);
2460 goto cleanup_session;
2464 GST_ERROR ("client %p: stream '%s' not found", client,
2465 GST_STR_NULL (control));
2466 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2467 g_object_unref (media);
2468 goto cleanup_session;
2472 GST_ERROR ("client %p: pre signal returned error: %s", client,
2473 gst_rtsp_status_as_text (sig_result));
2474 send_generic_response (client, sig_result, ctx);
2475 g_object_unref (media);
2478 service_unavailable:
2480 GST_ERROR ("client %p: can't create session", client);
2481 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2482 g_object_unref (media);
2483 goto cleanup_session;
2485 sessmedia_unavailable:
2487 GST_ERROR ("client %p: can't create session media", client);
2488 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2489 goto cleanup_transport;
2491 configure_media_failed_no_reply:
2493 GST_ERROR ("client %p: configure_media failed", client);
2494 g_object_unref (media);
2495 /* error reply is already sent */
2496 goto cleanup_session;
2498 unsupported_transports:
2500 GST_ERROR ("client %p: unsupported transports", client);
2501 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
2502 goto cleanup_transport;
2504 unsupported_client_transport:
2506 GST_ERROR ("client %p: unsupported client transport", client);
2507 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
2508 goto cleanup_transport;
2512 GST_ERROR ("client %p: unsupported mode (media play: %d, media record: %d, "
2513 "mode play: %d, mode record: %d)", client,
2514 ! !(gst_rtsp_media_get_transport_mode (media) &
2515 GST_RTSP_TRANSPORT_MODE_PLAY),
2516 ! !(gst_rtsp_media_get_transport_mode (media) &
2517 GST_RTSP_TRANSPORT_MODE_RECORD), ct->mode_play, ct->mode_record);
2518 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
2519 goto cleanup_transport;
2521 unsupported_range_unit:
2523 GST_ERROR ("Client %p: does not support any range format we support",
2525 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
2526 goto cleanup_transport;
2530 GST_ERROR ("client %p: keymgmt error", client);
2531 send_generic_response (client, GST_RTSP_STS_KEY_MANAGEMENT_FAILURE, ctx);
2532 goto cleanup_transport;
2536 gst_rtsp_transport_free (ct);
2538 g_object_unref (media);
2541 gst_rtsp_session_pool_remove (priv->session_pool, session);
2543 g_object_unref (session);
2550 static GstSDPMessage *
2551 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
2553 GstRTSPClientPrivate *priv = client->priv;
2557 guint64 session_id_tmp;
2558 gchar session_id[21];
2560 gst_sdp_message_new (&sdp);
2562 /* some standard things first */
2563 gst_sdp_message_set_version (sdp, "0");
2570 session_id_tmp = (((guint64) g_random_int ()) << 32) | g_random_int ();
2571 g_snprintf (session_id, sizeof (session_id), "%" G_GUINT64_FORMAT,
2574 gst_sdp_message_set_origin (sdp, "-", session_id, "1", "IN", proto,
2577 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
2578 gst_sdp_message_set_information (sdp, "rtsp-server");
2579 gst_sdp_message_add_time (sdp, "0", "0", NULL);
2580 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
2581 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
2582 gst_sdp_message_add_attribute (sdp, "control", "*");
2584 info.is_ipv6 = priv->is_ipv6;
2585 info.server_ip = priv->server_ip;
2587 /* create an SDP for the media object */
2588 if (!gst_rtsp_media_setup_sdp (media, sdp, &info))
2596 GST_ERROR ("client %p: could not create SDP", client);
2597 gst_sdp_message_free (sdp);
2602 /* for the describe we must generate an SDP */
2604 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
2606 GstRTSPClientPrivate *priv = client->priv;
2611 GstRTSPMedia *media;
2612 GstRTSPClientClass *klass;
2613 GstRTSPStatusCode sig_result;
2615 klass = GST_RTSP_CLIENT_GET_CLASS (client);
2620 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_DESCRIBE_REQUEST],
2621 0, ctx, &sig_result);
2622 if (sig_result != GST_RTSP_STS_OK) {
2626 /* check what kind of format is accepted, we don't really do anything with it
2627 * and always return SDP for now. */
2632 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
2634 if (res == GST_RTSP_ENOTIMPL)
2637 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
2641 if (!priv->mount_points)
2642 goto no_mount_points;
2644 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
2647 /* find the media object for the uri */
2648 if (!(media = find_media (client, ctx, path, NULL)))
2651 if (!(gst_rtsp_media_get_transport_mode (media) &
2652 GST_RTSP_TRANSPORT_MODE_PLAY))
2653 goto unsupported_mode;
2655 /* create an SDP for the media object on this client */
2656 if (!(sdp = klass->create_sdp (client, media)))
2659 /* we suspend after the describe */
2660 gst_rtsp_media_suspend (media);
2661 g_object_unref (media);
2663 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2664 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2666 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
2669 /* content base for some clients that might screw up creating the setup uri */
2670 str = make_base_url (client, ctx->uri, path);
2673 GST_INFO ("adding content-base: %s", str);
2674 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE, str);
2676 /* add SDP to the response body */
2677 str = gst_sdp_message_as_text (sdp);
2678 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
2679 gst_sdp_message_free (sdp);
2681 send_message (client, ctx, ctx->response, FALSE);
2683 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
2691 GST_ERROR ("client %p: pre signal returned error: %s", client,
2692 gst_rtsp_status_as_text (sig_result));
2693 send_generic_response (client, sig_result, ctx);
2698 GST_ERROR ("client %p: no uri", client);
2699 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2704 GST_ERROR ("client %p: no mount points configured", client);
2705 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2710 GST_ERROR ("client %p: can't find path for url", client);
2711 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2716 GST_ERROR ("client %p: no media", client);
2718 /* error reply is already sent */
2723 GST_ERROR ("client %p: media does not support DESCRIBE", client);
2724 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_ALLOWED, ctx);
2726 g_object_unref (media);
2731 GST_ERROR ("client %p: can't create SDP", client);
2732 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2734 g_object_unref (media);
2740 handle_sdp (GstRTSPClient * client, GstRTSPContext * ctx, GstRTSPMedia * media,
2741 GstSDPMessage * sdp)
2743 GstRTSPClientPrivate *priv = client->priv;
2744 GstRTSPThread *thread;
2746 /* create an SDP for the media object */
2747 if (!gst_rtsp_media_handle_sdp (media, sdp))
2750 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
2751 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
2755 /* prepare the media */
2756 if (!gst_rtsp_media_prepare (media, thread))
2764 GST_ERROR ("client %p: could not handle SDP", client);
2769 GST_ERROR ("client %p: can't create thread", client);
2774 GST_ERROR ("client %p: can't prepare media", client);
2780 handle_announce_request (GstRTSPClient * client, GstRTSPContext * ctx)
2782 GstRTSPClientPrivate *priv = client->priv;
2783 GstRTSPClientClass *klass;
2786 GstRTSPMedia *media;
2787 gchar *path, *cont = NULL;
2790 GstRTSPStatusCode sig_result;
2793 klass = GST_RTSP_CLIENT_GET_CLASS (client);
2798 if (!priv->mount_points)
2799 goto no_mount_points;
2801 /* check if reply is SDP */
2802 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_CONTENT_TYPE, &cont,
2804 /* could not be set but since the request returned OK, we assume it
2805 * was SDP, else check it. */
2807 if (g_ascii_strcasecmp (cont, "application/sdp") != 0)
2808 goto wrong_content_type;
2811 /* get message body and parse as SDP */
2812 gst_rtsp_message_get_body (ctx->request, &data, &size);
2813 if (data == NULL || size == 0)
2816 GST_DEBUG ("client %p: parse SDP...", client);
2817 gst_sdp_message_new (&sdp);
2818 sres = gst_sdp_message_parse_buffer (data, size, sdp);
2819 if (sres != GST_SDP_OK)
2820 goto sdp_parse_failed;
2822 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
2825 /* find the media object for the uri */
2826 if (!(media = find_media (client, ctx, path, NULL)))
2831 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_ANNOUNCE_REQUEST],
2832 0, ctx, &sig_result);
2833 if (sig_result != GST_RTSP_STS_OK) {
2837 if (!(gst_rtsp_media_get_transport_mode (media) &
2838 GST_RTSP_TRANSPORT_MODE_RECORD))
2839 goto unsupported_mode;
2841 /* Tell client subclass about the media */
2842 if (!klass->handle_sdp (client, ctx, media, sdp))
2845 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2846 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2848 n_streams = gst_rtsp_media_n_streams (media);
2849 for (i = 0; i < n_streams; i++) {
2850 GstRTSPStream *stream = gst_rtsp_media_get_stream (media, i);
2852 g_strdup_printf ("rtsp://%s%s:8554/stream=%d", priv->server_ip, path,
2854 gchar *keymgmt = stream_make_keymgmt (client, location, stream);
2857 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_KEYMGMT,
2863 /* we suspend after the announce */
2864 gst_rtsp_media_suspend (media);
2865 g_object_unref (media);
2867 send_message (client, ctx, ctx->response, FALSE);
2869 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_ANNOUNCE_REQUEST],
2872 gst_sdp_message_free (sdp);
2878 GST_ERROR ("client %p: no uri", client);
2879 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2884 GST_ERROR ("client %p: no mount points configured", client);
2885 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2890 GST_ERROR ("client %p: can't find path for url", client);
2891 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2892 gst_sdp_message_free (sdp);
2897 GST_ERROR ("client %p: unknown content type", client);
2898 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2903 GST_ERROR ("client %p: can't find SDP message", client);
2904 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2909 GST_ERROR ("client %p: failed to parse SDP message", client);
2910 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2911 gst_sdp_message_free (sdp);
2916 GST_ERROR ("client %p: no media", client);
2918 /* error reply is already sent */
2919 gst_sdp_message_free (sdp);
2924 GST_ERROR ("client %p: pre signal returned error: %s", client,
2925 gst_rtsp_status_as_text (sig_result));
2926 send_generic_response (client, sig_result, ctx);
2927 gst_sdp_message_free (sdp);
2932 GST_ERROR ("client %p: media does not support ANNOUNCE", client);
2933 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_ALLOWED, ctx);
2935 g_object_unref (media);
2936 gst_sdp_message_free (sdp);
2941 GST_ERROR ("client %p: can't handle SDP", client);
2942 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_MEDIA_TYPE, ctx);
2944 g_object_unref (media);
2945 gst_sdp_message_free (sdp);
2951 handle_record_request (GstRTSPClient * client, GstRTSPContext * ctx)
2953 GstRTSPSession *session;
2954 GstRTSPClientClass *klass;
2955 GstRTSPSessionMedia *sessmedia;
2956 GstRTSPMedia *media;
2958 GstRTSPState rtspstate;
2961 GstRTSPStatusCode sig_result;
2962 GPtrArray *transports;
2964 if (!(session = ctx->session))
2967 if (!(uri = ctx->uri))
2970 klass = GST_RTSP_CLIENT_GET_CLASS (client);
2971 path = klass->make_path_from_uri (client, uri);
2973 /* get a handle to the configuration of the media in the session */
2974 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
2978 if (path[matched] != '\0')
2983 ctx->sessmedia = sessmedia;
2984 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
2986 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_RECORD_REQUEST], 0,
2988 if (sig_result != GST_RTSP_STS_OK) {
2992 if (!(gst_rtsp_media_get_transport_mode (media) &
2993 GST_RTSP_TRANSPORT_MODE_RECORD))
2994 goto unsupported_mode;
2996 /* the session state must be playing or ready */
2997 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
2998 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
3001 /* update the pipeline */
3002 transports = gst_rtsp_session_media_get_transports (sessmedia);
3003 if (!gst_rtsp_media_complete_pipeline (media, transports)) {
3004 g_ptr_array_unref (transports);
3005 goto pipeline_error;
3007 g_ptr_array_unref (transports);
3009 /* in record we first unsuspend, media could be suspended from SDP or PAUSED */
3010 if (!gst_rtsp_media_unsuspend (media))
3011 goto unsuspend_failed;
3013 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
3014 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
3016 send_message (client, ctx, ctx->response, FALSE);
3018 /* start playing after sending the response */
3019 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
3021 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
3023 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_RECORD_REQUEST], 0,
3031 GST_ERROR ("client %p: no session", client);
3032 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
3037 GST_ERROR ("client %p: no uri supplied", client);
3038 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
3043 GST_ERROR ("client %p: media not found", client);
3044 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
3049 GST_ERROR ("client %p: no aggregate path %s", client, path);
3050 send_generic_response (client,
3051 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
3057 GST_ERROR ("client %p: pre signal returned error: %s", client,
3058 gst_rtsp_status_as_text (sig_result));
3059 send_generic_response (client, sig_result, ctx);
3064 GST_ERROR ("client %p: media does not support RECORD", client);
3065 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_ALLOWED, ctx);
3070 GST_ERROR ("client %p: not PLAYING or READY", client);
3071 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
3077 GST_ERROR ("client %p: failed to configure the pipeline", client);
3078 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
3084 GST_ERROR ("client %p: unsuspend failed", client);
3085 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
3091 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx,
3092 GstRTSPVersion version)
3094 GstRTSPMethod options;
3096 GstRTSPStatusCode sig_result;
3098 options = GST_RTSP_DESCRIBE |
3103 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
3105 if (version < GST_RTSP_VERSION_2_0) {
3106 options |= GST_RTSP_RECORD;
3107 options |= GST_RTSP_ANNOUNCE;
3110 str = gst_rtsp_options_as_text (options);
3112 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
3113 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
3115 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
3118 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_OPTIONS_REQUEST], 0,
3120 if (sig_result != GST_RTSP_STS_OK) {
3124 send_message (client, ctx, ctx->response, FALSE);
3126 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
3134 GST_ERROR ("client %p: pre signal returned error: %s", client,
3135 gst_rtsp_status_as_text (sig_result));
3136 send_generic_response (client, sig_result, ctx);
3137 gst_rtsp_message_free (ctx->response);
3142 /* remove duplicate and trailing '/' */
3144 sanitize_uri (GstRTSPUrl * uri)
3148 gboolean have_slash, prev_slash;
3150 s = d = uri->abspath;
3151 len = strlen (uri->abspath);
3155 for (i = 0; i < len; i++) {
3156 have_slash = s[i] == '/';
3158 if (!have_slash || !prev_slash)
3160 prev_slash = have_slash;
3162 len = d - uri->abspath;
3163 /* don't remove the first slash if that's the only thing left */
3164 if (len > 1 && *(d - 1) == '/')
3169 /* is called when the session is removed from its session pool. */
3171 client_session_removed (GstRTSPSessionPool * pool, GstRTSPSession * session,
3172 GstRTSPClient * client)
3174 GstRTSPClientPrivate *priv = client->priv;
3176 GST_INFO ("client %p: session %p removed", client, session);
3178 g_mutex_lock (&priv->lock);
3179 if (priv->watch != NULL)
3180 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 0);
3181 client_unwatch_session (client, session, NULL);
3182 if (priv->watch != NULL)
3183 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
3184 g_mutex_unlock (&priv->lock);
3187 /* Check for Require headers. Returns TRUE if there are no Require headers,
3188 * otherwise lets the application decide which headers are supported.
3189 * By default all headers are unsupported.
3190 * If there are unsupported options, FALSE will be returned together with
3191 * a newly-allocated string of (comma-separated) unsupported options in
3192 * the unsupported_reqs variable.
3194 * There may be multiple Require headers, but we must send one single
3195 * Unsupported header with all the unsupported options as response. If
3196 * an incoming Require header contained a comma-separated list of options
3197 * GstRtspConnection will already have split that list up into multiple
3201 check_request_requirements (GstRTSPContext * ctx, gchar ** unsupported_reqs)
3204 GPtrArray *arr = NULL;
3205 GstRTSPMessage *msg = ctx->request;
3208 gchar *sig_result = NULL;
3209 gboolean result = TRUE;
3213 res = gst_rtsp_message_get_header (msg, GST_RTSP_HDR_REQUIRE, &reqs, i++);
3215 if (res == GST_RTSP_ENOTIMPL)
3219 arr = g_ptr_array_new_with_free_func ((GDestroyNotify) g_free);
3221 g_ptr_array_add (arr, g_strdup (reqs));
3225 /* if we don't have any Require headers at all, all is fine */
3229 /* otherwise we've now processed at all the Require headers */
3230 g_ptr_array_add (arr, NULL);
3232 g_signal_emit (ctx->client,
3233 gst_rtsp_client_signals[SIGNAL_CHECK_REQUIREMENTS], 0, ctx,
3234 (gchar **) arr->pdata, &sig_result);
3236 if (sig_result == NULL) {
3237 /* no supported options, just report all of the required ones as
3239 *unsupported_reqs = g_strjoinv (", ", (gchar **) arr->pdata);
3244 if (strlen (sig_result) == 0)
3245 g_free (sig_result);
3247 *unsupported_reqs = sig_result;
3252 g_ptr_array_unref (arr);
3257 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
3259 GstRTSPClientPrivate *priv = client->priv;
3260 GstRTSPMethod method;
3261 const gchar *uristr;
3262 GstRTSPUrl *uri = NULL;
3263 GstRTSPVersion version;
3265 GstRTSPSession *session = NULL;
3266 GstRTSPContext sctx = { NULL }, *ctx;
3267 GstRTSPMessage response = { 0 };
3268 gchar *unsupported_reqs = NULL;
3269 gchar *sessid = NULL, *pipelined_request_id = NULL;
3271 if (!(ctx = gst_rtsp_context_get_current ())) {
3273 ctx->auth = priv->auth;
3274 gst_rtsp_context_push_current (ctx);
3277 ctx->conn = priv->connection;
3278 ctx->client = client;
3279 ctx->request = request;
3280 ctx->response = &response;
3282 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
3283 gst_rtsp_message_dump (request);
3286 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
3288 GST_INFO ("client %p: received a request %s %s %s", client,
3289 gst_rtsp_method_as_text (method), uristr,
3290 gst_rtsp_version_as_text (version));
3292 /* we can only handle 1.0 requests */
3293 if (version != GST_RTSP_VERSION_1_0 && version != GST_RTSP_VERSION_2_0)
3296 ctx->method = method;
3298 /* we always try to parse the url first */
3299 if (strcmp (uristr, "*") == 0) {
3300 /* special case where we have * as uri, keep uri = NULL */
3301 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
3302 /* check if the uristr is an absolute path <=> scheme and host information
3306 scheme = g_uri_parse_scheme (uristr);
3307 if (scheme == NULL && g_str_has_prefix (uristr, "/")) {
3308 gchar *absolute_uristr = NULL;
3310 GST_WARNING_OBJECT (client, "request doesn't contain absolute url");
3311 if (priv->server_ip == NULL) {
3312 GST_WARNING_OBJECT (client, "host information missing");
3317 g_strdup_printf ("rtsp://%s%s", priv->server_ip, uristr);
3319 GST_DEBUG_OBJECT (client, "absolute url: %s", absolute_uristr);
3320 if (gst_rtsp_url_parse (absolute_uristr, &uri) != GST_RTSP_OK) {
3321 g_free (absolute_uristr);
3324 g_free (absolute_uristr);
3331 /* get the session if there is any */
3332 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_PIPELINED_REQUESTS,
3333 &pipelined_request_id, 0);
3334 if (res == GST_RTSP_OK) {
3335 sessid = g_hash_table_lookup (client->priv->pipelined_requests,
3336 pipelined_request_id);
3339 res = GST_RTSP_ERROR;
3342 if (res != GST_RTSP_OK)
3344 gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
3346 if (res == GST_RTSP_OK) {
3347 if (priv->session_pool == NULL)
3350 /* we had a session in the request, find it again */
3351 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
3352 goto session_not_found;
3354 /* we add the session to the client list of watched sessions. When a session
3355 * disappears because it times out, we will be notified. If all sessions are
3356 * gone, we will close the connection */
3357 client_watch_session (client, session);
3360 /* sanitize the uri */
3364 ctx->session = session;
3366 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
3367 goto not_authorized;
3369 /* handle any 'Require' headers */
3370 if (!check_request_requirements (ctx, &unsupported_reqs))
3371 goto unsupported_requirement;
3373 /* the backlog must be unlimited while processing requests.
3374 * the causes of this are two cases of deadlocks while streaming over TCP:
3376 * 1. consider the scenario where the media pipeline's streaming thread
3377 * is blocking in the appsink (taking the appsink's preroll lock) because
3378 * the backlog is full. when a PAUSE request is received by the RTSP
3379 * client thread then the the state of the session media ought to change
3380 * to PAUSED. while most elements in the pipeline can change state this
3381 * can never happen for the appsink since its preroll lock is taken by
3384 * 2. consider the scenario where the media pipeline's streaming thread
3385 * is blocking in the appsink new_sample callback (taking the send lock
3386 * in RTSP client) because the backlog is full. when e.g. a GET request
3387 * is received by the RTSP client thread then a response ought to be sent
3388 * but this can never happen since it requires taking the send lock
3389 * already taken by another thread.
3391 * the reason that the backlog is never emptied is that the source used
3392 * for dequeing messages from the backlog is never dispatched because it
3393 * is attached to the same mainloop as the source receving RTSP requests and
3394 * therefore run by the RTSP client thread which is alreayd blocking.
3396 * without significant changes the easiest way to cope with this is to
3397 * not block indefinitely when the backlog is full, but rather let the
3398 * backlog grow in size. this in effect means that there can not be any
3399 * upper boundary on its size.
3401 if (priv->watch != NULL)
3402 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 0);
3404 /* now see what is asked and dispatch to a dedicated handler */
3406 case GST_RTSP_OPTIONS:
3407 priv->version = version;
3408 handle_options_request (client, ctx, version);
3410 case GST_RTSP_DESCRIBE:
3411 handle_describe_request (client, ctx);
3413 case GST_RTSP_SETUP:
3414 handle_setup_request (client, ctx);
3417 handle_play_request (client, ctx);
3419 case GST_RTSP_PAUSE:
3420 handle_pause_request (client, ctx);
3422 case GST_RTSP_TEARDOWN:
3423 handle_teardown_request (client, ctx);
3425 case GST_RTSP_SET_PARAMETER:
3426 handle_set_param_request (client, ctx);
3428 case GST_RTSP_GET_PARAMETER:
3429 handle_get_param_request (client, ctx);
3431 case GST_RTSP_ANNOUNCE:
3432 if (version >= GST_RTSP_VERSION_2_0)
3433 goto invalid_command_for_version;
3434 handle_announce_request (client, ctx);
3436 case GST_RTSP_RECORD:
3437 if (version >= GST_RTSP_VERSION_2_0)
3438 goto invalid_command_for_version;
3439 handle_record_request (client, ctx);
3441 case GST_RTSP_REDIRECT:
3442 if (priv->watch != NULL)
3443 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
3444 goto not_implemented;
3445 case GST_RTSP_INVALID:
3447 if (priv->watch != NULL)
3448 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
3452 if (priv->watch != NULL)
3453 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
3457 gst_rtsp_context_pop_current (ctx);
3459 g_object_unref (session);
3461 gst_rtsp_url_free (uri);
3467 GST_ERROR ("client %p: version %d not supported", client, version);
3468 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
3472 invalid_command_for_version:
3474 if (priv->watch != NULL)
3475 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
3477 GST_ERROR ("client %p: invalid command for version", client);
3478 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
3483 GST_ERROR ("client %p: bad request", client);
3484 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
3489 GST_ERROR ("client %p: no pool configured", client);
3490 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
3495 GST_ERROR ("client %p: session not found", client);
3496 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
3501 GST_ERROR ("client %p: not allowed", client);
3502 /* error reply is already sent */
3505 unsupported_requirement:
3507 GST_ERROR ("client %p: Required option is not supported (%s)", client,
3509 send_option_not_supported_response (client, ctx, unsupported_reqs);
3510 g_free (unsupported_reqs);
3515 GST_ERROR ("client %p: method %d not implemented", client, method);
3516 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
3523 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
3525 GstRTSPClientPrivate *priv = client->priv;
3527 GstRTSPSession *session = NULL;
3528 GstRTSPContext sctx = { NULL }, *ctx;
3531 if (!(ctx = gst_rtsp_context_get_current ())) {
3533 ctx->auth = priv->auth;
3534 gst_rtsp_context_push_current (ctx);
3537 ctx->conn = priv->connection;
3538 ctx->client = client;
3539 ctx->request = NULL;
3541 ctx->method = GST_RTSP_INVALID;
3542 ctx->response = response;
3544 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
3545 gst_rtsp_message_dump (response);
3548 GST_INFO ("client %p: received a response", client);
3550 /* get the session if there is any */
3552 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
3553 if (res == GST_RTSP_OK) {
3554 if (priv->session_pool == NULL)
3557 /* we had a session in the request, find it again */
3558 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
3559 goto session_not_found;
3561 /* we add the session to the client list of watched sessions. When a session
3562 * disappears because it times out, we will be notified. If all sessions are
3563 * gone, we will close the connection */
3564 client_watch_session (client, session);
3567 ctx->session = session;
3569 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
3574 gst_rtsp_context_pop_current (ctx);
3576 g_object_unref (session);
3581 GST_ERROR ("client %p: no pool configured", client);
3586 GST_ERROR ("client %p: session not found", client);
3592 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
3594 GstRTSPClientPrivate *priv = client->priv;
3600 GstRTSPStreamTransport *trans;
3602 /* find the stream for this message */
3603 res = gst_rtsp_message_parse_data (message, &channel);
3604 if (res != GST_RTSP_OK)
3607 gst_rtsp_message_get_body (message, &data, &size);
3609 goto invalid_length;
3611 gst_rtsp_message_steal_body (message, &data, &size);
3613 /* Strip trailing \0 (which GstRTSPConnection adds) */
3616 buffer = gst_buffer_new_wrapped (data, size);
3619 g_hash_table_lookup (priv->transports, GINT_TO_POINTER ((gint) channel));
3621 GSocketAddress *addr;
3623 /* Only create the socket address once for the transport, we don't really
3624 * want to do that for every single packet.
3626 * The netaddress meta is later used by the RTP stack to know where
3627 * packets came from and allows us to match it again to a stream transport
3629 * In theory we could use the remote socket address of the RTSP connection
3630 * here, but this would fail with a custom configure_client_transport()
3634 g_object_get_data (G_OBJECT (trans), "rtsp-client.remote-addr"))) {
3635 const GstRTSPTransport *tr;
3636 GInetAddress *iaddr;
3638 tr = gst_rtsp_stream_transport_get_transport (trans);
3639 iaddr = g_inet_address_new_from_string (tr->destination);
3641 addr = g_inet_socket_address_new (iaddr, tr->client_port.min);
3642 g_object_unref (iaddr);
3643 g_object_set_data_full (G_OBJECT (trans), "rtsp-client.remote-addr",
3644 addr, (GDestroyNotify) g_object_unref);
3649 gst_buffer_add_net_address_meta (buffer, addr);
3652 /* dispatch to the stream based on the channel number */
3653 GST_LOG_OBJECT (client, "%u bytes of data on channel %u", size, channel);
3654 gst_rtsp_stream_transport_recv_data (trans, channel, buffer);
3656 GST_DEBUG_OBJECT (client, "received %u bytes of data for "
3657 "unknown channel %u", size, channel);
3658 gst_buffer_unref (buffer);
3666 GST_DEBUG ("client %p: Short message received, ignoring", client);
3672 * gst_rtsp_client_set_session_pool:
3673 * @client: a #GstRTSPClient
3674 * @pool: (transfer none) (nullable): a #GstRTSPSessionPool
3676 * Set @pool as the sessionpool for @client which it will use to find
3677 * or allocate sessions. the sessionpool is usually inherited from the server
3678 * that created the client but can be overridden later.
3681 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
3682 GstRTSPSessionPool * pool)
3684 GstRTSPSessionPool *old;
3685 GstRTSPClientPrivate *priv;
3687 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
3689 priv = client->priv;
3692 g_object_ref (pool);
3694 g_mutex_lock (&priv->lock);
3695 old = priv->session_pool;
3696 priv->session_pool = pool;
3698 if (priv->session_removed_id) {
3699 g_signal_handler_disconnect (old, priv->session_removed_id);
3700 priv->session_removed_id = 0;
3702 g_mutex_unlock (&priv->lock);
3704 /* FIXME, should remove all sessions from the old pool for this client */
3706 g_object_unref (old);
3710 * gst_rtsp_client_get_session_pool:
3711 * @client: a #GstRTSPClient
3713 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
3715 * Returns: (transfer full) (nullable): a #GstRTSPSessionPool, unref after usage.
3717 GstRTSPSessionPool *
3718 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
3720 GstRTSPClientPrivate *priv;
3721 GstRTSPSessionPool *result;
3723 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
3725 priv = client->priv;
3727 g_mutex_lock (&priv->lock);
3728 if ((result = priv->session_pool))
3729 g_object_ref (result);
3730 g_mutex_unlock (&priv->lock);
3736 * gst_rtsp_client_set_mount_points:
3737 * @client: a #GstRTSPClient
3738 * @mounts: (transfer none) (nullable): a #GstRTSPMountPoints
3740 * Set @mounts as the mount points for @client which it will use to map urls
3741 * to media streams. These mount points are usually inherited from the server that
3742 * created the client but can be overriden later.
3745 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
3746 GstRTSPMountPoints * mounts)
3748 GstRTSPClientPrivate *priv;
3749 GstRTSPMountPoints *old;
3751 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
3753 priv = client->priv;
3756 g_object_ref (mounts);
3758 g_mutex_lock (&priv->lock);
3759 old = priv->mount_points;
3760 priv->mount_points = mounts;
3761 g_mutex_unlock (&priv->lock);
3764 g_object_unref (old);
3768 * gst_rtsp_client_get_mount_points:
3769 * @client: a #GstRTSPClient
3771 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
3773 * Returns: (transfer full) (nullable): a #GstRTSPMountPoints, unref after usage.
3775 GstRTSPMountPoints *
3776 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
3778 GstRTSPClientPrivate *priv;
3779 GstRTSPMountPoints *result;
3781 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
3783 priv = client->priv;
3785 g_mutex_lock (&priv->lock);
3786 if ((result = priv->mount_points))
3787 g_object_ref (result);
3788 g_mutex_unlock (&priv->lock);
3794 * gst_rtsp_client_set_auth:
3795 * @client: a #GstRTSPClient
3796 * @auth: (transfer none) (nullable): a #GstRTSPAuth
3798 * configure @auth to be used as the authentication manager of @client.
3801 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
3803 GstRTSPClientPrivate *priv;
3806 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
3808 priv = client->priv;
3811 g_object_ref (auth);
3813 g_mutex_lock (&priv->lock);
3816 g_mutex_unlock (&priv->lock);
3819 g_object_unref (old);
3824 * gst_rtsp_client_get_auth:
3825 * @client: a #GstRTSPClient
3827 * Get the #GstRTSPAuth used as the authentication manager of @client.
3829 * Returns: (transfer full) (nullable): the #GstRTSPAuth of @client.
3830 * g_object_unref() after usage.
3833 gst_rtsp_client_get_auth (GstRTSPClient * client)
3835 GstRTSPClientPrivate *priv;
3836 GstRTSPAuth *result;
3838 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
3840 priv = client->priv;
3842 g_mutex_lock (&priv->lock);
3843 if ((result = priv->auth))
3844 g_object_ref (result);
3845 g_mutex_unlock (&priv->lock);
3851 * gst_rtsp_client_set_thread_pool:
3852 * @client: a #GstRTSPClient
3853 * @pool: (transfer none) (nullable): a #GstRTSPThreadPool
3855 * configure @pool to be used as the thread pool of @client.
3858 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
3859 GstRTSPThreadPool * pool)
3861 GstRTSPClientPrivate *priv;
3862 GstRTSPThreadPool *old;
3864 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
3866 priv = client->priv;
3869 g_object_ref (pool);
3871 g_mutex_lock (&priv->lock);
3872 old = priv->thread_pool;
3873 priv->thread_pool = pool;
3874 g_mutex_unlock (&priv->lock);
3877 g_object_unref (old);
3881 * gst_rtsp_client_get_thread_pool:
3882 * @client: a #GstRTSPClient
3884 * Get the #GstRTSPThreadPool used as the thread pool of @client.
3886 * Returns: (transfer full) (nullable): the #GstRTSPThreadPool of @client. g_object_unref() after
3890 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
3892 GstRTSPClientPrivate *priv;
3893 GstRTSPThreadPool *result;
3895 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
3897 priv = client->priv;
3899 g_mutex_lock (&priv->lock);
3900 if ((result = priv->thread_pool))
3901 g_object_ref (result);
3902 g_mutex_unlock (&priv->lock);
3908 * gst_rtsp_client_set_connection:
3909 * @client: a #GstRTSPClient
3910 * @conn: (transfer full): a #GstRTSPConnection
3912 * Set the #GstRTSPConnection of @client. This function takes ownership of
3915 * Returns: %TRUE on success.
3918 gst_rtsp_client_set_connection (GstRTSPClient * client,
3919 GstRTSPConnection * conn)
3921 GstRTSPClientPrivate *priv;
3922 GSocket *read_socket;
3923 GSocketAddress *address;
3925 GError *error = NULL;
3927 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
3928 g_return_val_if_fail (conn != NULL, FALSE);
3930 priv = client->priv;
3932 read_socket = gst_rtsp_connection_get_read_socket (conn);
3934 if (!(address = g_socket_get_local_address (read_socket, &error)))
3937 g_free (priv->server_ip);
3938 /* keep the original ip that the client connected to */
3939 if (G_IS_INET_SOCKET_ADDRESS (address)) {
3940 GInetAddress *iaddr;
3942 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
3944 /* socket might be ipv6 but adress still ipv4 */
3945 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
3946 priv->server_ip = g_inet_address_to_string (iaddr);
3947 g_object_unref (address);
3949 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
3950 priv->server_ip = g_strdup ("unknown");
3953 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
3954 priv->server_ip, priv->is_ipv6);
3956 url = gst_rtsp_connection_get_url (conn);
3957 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
3959 priv->connection = conn;
3966 GST_ERROR ("could not get local address %s", error->message);
3967 g_error_free (error);
3973 * gst_rtsp_client_get_connection:
3974 * @client: a #GstRTSPClient
3976 * Get the #GstRTSPConnection of @client.
3978 * Returns: (transfer none) (nullable): the #GstRTSPConnection of @client.
3979 * The connection object returned remains valid until the client is freed.
3982 gst_rtsp_client_get_connection (GstRTSPClient * client)
3984 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
3986 return client->priv->connection;
3990 * gst_rtsp_client_set_send_func:
3991 * @client: a #GstRTSPClient
3992 * @func: (scope notified): a #GstRTSPClientSendFunc
3993 * @user_data: (closure): user data passed to @func
3994 * @notify: (allow-none): called when @user_data is no longer in use
3996 * Set @func as the callback that will be called when a new message needs to be
3997 * sent to the client. @user_data is passed to @func and @notify is called when
3998 * @user_data is no longer in use.
4000 * By default, the client will send the messages on the #GstRTSPConnection that
4001 * was configured with gst_rtsp_client_attach() was called.
4004 gst_rtsp_client_set_send_func (GstRTSPClient * client,
4005 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
4007 GstRTSPClientPrivate *priv;
4008 GDestroyNotify old_notify;
4011 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
4013 priv = client->priv;
4015 g_mutex_lock (&priv->send_lock);
4016 priv->send_func = func;
4017 old_notify = priv->send_notify;
4018 old_data = priv->send_data;
4019 priv->send_notify = notify;
4020 priv->send_data = user_data;
4021 g_mutex_unlock (&priv->send_lock);
4024 old_notify (old_data);
4028 * gst_rtsp_client_handle_message:
4029 * @client: a #GstRTSPClient
4030 * @message: (transfer none): an #GstRTSPMessage
4032 * Let the client handle @message.
4034 * Returns: a #GstRTSPResult.
4037 gst_rtsp_client_handle_message (GstRTSPClient * client,
4038 GstRTSPMessage * message)
4040 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
4041 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
4043 switch (message->type) {
4044 case GST_RTSP_MESSAGE_REQUEST:
4045 handle_request (client, message);
4047 case GST_RTSP_MESSAGE_RESPONSE:
4048 handle_response (client, message);
4050 case GST_RTSP_MESSAGE_DATA:
4051 handle_data (client, message);
4060 * gst_rtsp_client_send_message:
4061 * @client: a #GstRTSPClient
4062 * @session: (allow-none) (transfer none): a #GstRTSPSession to send
4063 * the message to or %NULL
4064 * @message: (transfer none): The #GstRTSPMessage to send
4066 * Send a message message to the remote end. @message must be a
4067 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
4070 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
4071 GstRTSPMessage * message)
4073 GstRTSPContext sctx = { NULL }
4075 GstRTSPClientPrivate *priv;
4077 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
4078 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
4079 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
4080 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
4082 priv = client->priv;
4084 if (!(ctx = gst_rtsp_context_get_current ())) {
4086 ctx->auth = priv->auth;
4087 gst_rtsp_context_push_current (ctx);
4090 ctx->conn = priv->connection;
4091 ctx->client = client;
4092 ctx->session = session;
4094 send_message (client, ctx, message, FALSE);
4097 gst_rtsp_context_pop_current (ctx);
4103 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
4104 gboolean close, gpointer user_data)
4106 GstRTSPClientPrivate *priv = client->priv;
4114 /* send the response and store the seq number so we can wait until it's
4115 * written to the client to close the connection */
4117 gst_rtsp_watch_send_message (priv->watch, message,
4118 close ? &priv->close_seq : NULL);
4119 if (ret == GST_RTSP_OK)
4122 if (ret != GST_RTSP_ENOMEM)
4126 if (priv->drop_backlog)
4129 /* queue was full, wait for more space */
4130 GST_DEBUG_OBJECT (client, "waiting for backlog");
4131 ret = gst_rtsp_watch_wait_backlog (priv->watch, &time);
4132 GST_DEBUG_OBJECT (client, "Resend due to backlog full");
4133 } while (ret != GST_RTSP_EINTR);
4135 return ret == GST_RTSP_OK;
4140 GST_DEBUG_OBJECT (client, "got error %d", ret);
4141 return ret == GST_RTSP_OK;
4145 static GstRTSPResult
4146 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
4149 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
4152 static GstRTSPResult
4153 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
4155 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
4156 GstRTSPClientPrivate *priv = client->priv;
4158 if (priv->close_seq && priv->close_seq == cseq) {
4159 GST_INFO ("client %p: send close message", client);
4160 priv->close_seq = 0;
4161 gst_rtsp_client_close (client);
4167 static GstRTSPResult
4168 closed (GstRTSPWatch * watch, gpointer user_data)
4170 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
4171 GstRTSPClientPrivate *priv = client->priv;
4172 const gchar *tunnelid;
4174 GST_INFO ("client %p: connection closed", client);
4176 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
4177 g_mutex_lock (&tunnels_lock);
4178 /* remove from tunnelids */
4179 g_hash_table_remove (tunnels, tunnelid);
4180 g_mutex_unlock (&tunnels_lock);
4183 gst_rtsp_watch_set_flushing (watch, TRUE);
4184 g_mutex_lock (&priv->watch_lock);
4185 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
4186 g_mutex_unlock (&priv->watch_lock);
4191 static GstRTSPResult
4192 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
4194 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
4197 str = gst_rtsp_strresult (result);
4198 GST_INFO ("client %p: received an error %s", client, str);
4204 static GstRTSPResult
4205 error_full (GstRTSPWatch * watch, GstRTSPResult result,
4206 GstRTSPMessage * message, guint id, gpointer user_data)
4208 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
4211 str = gst_rtsp_strresult (result);
4213 ("client %p: error when handling message %p with id %d: %s",
4214 client, message, id, str);
4221 remember_tunnel (GstRTSPClient * client)
4223 GstRTSPClientPrivate *priv = client->priv;
4224 const gchar *tunnelid;
4226 /* store client in the pending tunnels */
4227 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
4228 if (tunnelid == NULL)
4231 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
4233 /* we can't have two clients connecting with the same tunnelid */
4234 g_mutex_lock (&tunnels_lock);
4235 if (g_hash_table_lookup (tunnels, tunnelid))
4236 goto tunnel_existed;
4238 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
4239 g_mutex_unlock (&tunnels_lock);
4246 GST_ERROR ("client %p: no tunnelid provided", client);
4251 g_mutex_unlock (&tunnels_lock);
4252 GST_ERROR ("client %p: tunnel session %s already existed", client,
4258 static GstRTSPResult
4259 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
4261 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
4262 GstRTSPClientPrivate *priv = client->priv;
4264 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
4267 /* ignore error, it'll only be a problem when the client does a POST again */
4268 remember_tunnel (client);
4273 static GstRTSPStatusCode
4274 handle_tunnel (GstRTSPClient * client)
4276 GstRTSPClientPrivate *priv = client->priv;
4277 GstRTSPClient *oclient;
4278 GstRTSPClientPrivate *opriv;
4279 const gchar *tunnelid;
4281 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
4282 if (tunnelid == NULL)
4285 /* check for previous tunnel */
4286 g_mutex_lock (&tunnels_lock);
4287 oclient = g_hash_table_lookup (tunnels, tunnelid);
4289 if (oclient == NULL) {
4290 /* no previous tunnel, remember tunnel */
4291 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
4292 g_mutex_unlock (&tunnels_lock);
4294 GST_INFO ("client %p: no previous tunnel found, remembering tunnel (%p)",
4295 client, priv->connection);
4297 /* merge both tunnels into the first client */
4298 /* remove the old client from the table. ref before because removing it will
4299 * remove the ref to it. */
4300 g_object_ref (oclient);
4301 g_hash_table_remove (tunnels, tunnelid);
4302 g_mutex_unlock (&tunnels_lock);
4304 opriv = oclient->priv;
4306 g_mutex_lock (&opriv->watch_lock);
4307 if (opriv->watch == NULL)
4309 if (opriv->tstate == priv->tstate)
4310 goto tunnel_duplicate_id;
4312 GST_INFO ("client %p: found previous tunnel %p (old %p, new %p)", client,
4313 oclient, opriv->connection, priv->connection);
4315 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
4316 gst_rtsp_watch_reset (priv->watch);
4317 gst_rtsp_watch_reset (opriv->watch);
4318 g_mutex_unlock (&opriv->watch_lock);
4319 g_object_unref (oclient);
4321 /* the old client owns the tunnel now, the new one will be freed */
4322 g_source_destroy ((GSource *) priv->watch);
4324 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
4327 return GST_RTSP_STS_OK;
4332 GST_ERROR ("client %p: no tunnelid provided", client);
4333 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
4337 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
4338 g_mutex_unlock (&opriv->watch_lock);
4339 g_object_unref (oclient);
4340 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
4342 tunnel_duplicate_id:
4344 GST_ERROR ("client %p: tunnel session %s was duplicate", client, tunnelid);
4345 g_mutex_unlock (&opriv->watch_lock);
4346 g_object_unref (oclient);
4347 return GST_RTSP_STS_BAD_REQUEST;
4351 static GstRTSPStatusCode
4352 tunnel_get (GstRTSPWatch * watch, gpointer user_data)
4354 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
4356 GST_INFO ("client %p: tunnel get (connection %p)", client,
4357 client->priv->connection);
4359 g_mutex_lock (&client->priv->lock);
4360 client->priv->tstate = TUNNEL_STATE_GET;
4361 g_mutex_unlock (&client->priv->lock);
4363 return handle_tunnel (client);
4366 static GstRTSPResult
4367 tunnel_post (GstRTSPWatch * watch, gpointer user_data)
4369 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
4371 GST_INFO ("client %p: tunnel post (connection %p)", client,
4372 client->priv->connection);
4374 g_mutex_lock (&client->priv->lock);
4375 client->priv->tstate = TUNNEL_STATE_POST;
4376 g_mutex_unlock (&client->priv->lock);
4378 if (handle_tunnel (client) != GST_RTSP_STS_OK)
4379 return GST_RTSP_ERROR;
4384 static GstRTSPResult
4385 tunnel_http_response (GstRTSPWatch * watch, GstRTSPMessage * request,
4386 GstRTSPMessage * response, gpointer user_data)
4388 GstRTSPClientClass *klass;
4390 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
4391 klass = GST_RTSP_CLIENT_GET_CLASS (client);
4393 if (klass->tunnel_http_response) {
4394 klass->tunnel_http_response (client, request, response);
4400 static GstRTSPWatchFuncs watch_funcs = {
4409 tunnel_http_response
4413 client_watch_notify (GstRTSPClient * client)
4415 GstRTSPClientPrivate *priv = client->priv;
4416 gboolean closed = TRUE;
4418 GST_INFO ("client %p: watch destroyed", client);
4420 /* remove all sessions if the media says so and so drop the extra client ref */
4421 rtsp_ctrl_timeout_remove (priv);
4422 gst_rtsp_client_session_filter (client, cleanup_session, &closed);
4424 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
4425 g_object_unref (client);
4429 * gst_rtsp_client_attach:
4430 * @client: a #GstRTSPClient
4431 * @context: (allow-none): a #GMainContext
4433 * Attaches @client to @context. When the mainloop for @context is run, the
4434 * client will be dispatched. When @context is %NULL, the default context will be
4437 * This function should be called when the client properties and urls are fully
4438 * configured and the client is ready to start.
4440 * Returns: the ID (greater than 0) for the source within the GMainContext.
4443 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
4445 GstRTSPClientPrivate *priv;
4449 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
4450 priv = client->priv;
4451 g_return_val_if_fail (priv->connection != NULL, 0);
4452 g_return_val_if_fail (priv->watch == NULL, 0);
4454 /* make sure noone will free the context before the watch is destroyed */
4455 priv->watch_context = g_main_context_ref (context);
4457 /* create watch for the connection and attach */
4458 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
4459 g_object_ref (client), (GDestroyNotify) client_watch_notify);
4460 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
4461 (GDestroyNotify) gst_rtsp_watch_unref);
4463 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
4465 GST_INFO ("client %p: attaching to context %p", client, context);
4466 res = gst_rtsp_watch_attach (priv->watch, context);
4468 /* Setting up a timeout for the RTSP control channel until a session
4469 * is up where it is handling timeouts. */
4470 rtsp_ctrl_timeout_remove (priv); /* removing old if any */
4471 g_mutex_lock (&priv->lock);
4473 timer_src = g_timeout_source_new_seconds (RTSP_CTRL_CB_INTERVAL);
4474 g_source_set_callback (timer_src, rtsp_ctrl_timeout_cb, client, NULL);
4475 priv->rtsp_ctrl_timeout_id = g_source_attach (timer_src, priv->watch_context);
4476 g_source_unref (timer_src);
4477 GST_DEBUG ("rtsp control setting up session timeout id=%u.",
4478 priv->rtsp_ctrl_timeout_id);
4480 g_mutex_unlock (&priv->lock);
4486 * gst_rtsp_client_session_filter:
4487 * @client: a #GstRTSPClient
4488 * @func: (scope call) (allow-none): a callback
4489 * @user_data: user data passed to @func
4491 * Call @func for each session managed by @client. The result value of @func
4492 * determines what happens to the session. @func will be called with @client
4493 * locked so no further actions on @client can be performed from @func.
4495 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
4498 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
4500 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
4501 * will also be added with an additional ref to the result #GList of this
4504 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each session.
4506 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
4507 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
4508 * element in the #GList should be unreffed before the list is freed.
4511 gst_rtsp_client_session_filter (GstRTSPClient * client,
4512 GstRTSPClientSessionFilterFunc func, gpointer user_data)
4514 GstRTSPClientPrivate *priv;
4515 GList *result, *walk, *next;
4516 GHashTable *visited;
4519 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
4521 priv = client->priv;
4525 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
4527 g_mutex_lock (&priv->lock);
4529 cookie = priv->sessions_cookie;
4530 for (walk = priv->sessions; walk; walk = next) {
4531 GstRTSPSession *sess = walk->data;
4532 GstRTSPFilterResult res;
4535 next = g_list_next (walk);
4538 /* only visit each session once */
4539 if (g_hash_table_contains (visited, sess))
4542 g_hash_table_add (visited, g_object_ref (sess));
4543 g_mutex_unlock (&priv->lock);
4545 res = func (client, sess, user_data);
4547 g_mutex_lock (&priv->lock);
4549 res = GST_RTSP_FILTER_REF;
4551 changed = (cookie != priv->sessions_cookie);
4554 case GST_RTSP_FILTER_REMOVE:
4555 /* stop watching the session and pretend it went away, if the list was
4556 * changed, we can't use the current list position, try to see if we
4557 * still have the session */
4558 client_unwatch_session (client, sess, changed ? NULL : walk);
4559 cookie = priv->sessions_cookie;
4561 case GST_RTSP_FILTER_REF:
4562 result = g_list_prepend (result, g_object_ref (sess));
4564 case GST_RTSP_FILTER_KEEP:
4571 g_mutex_unlock (&priv->lock);
4574 g_hash_table_unref (visited);