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)
50 #include <gst/sdp/gstmikey.h>
51 #include <gst/rtsp/gstrtsp-enumtypes.h>
53 #include "rtsp-client.h"
55 #include "rtsp-params.h"
56 #include "rtsp-server-internal.h"
66 * send_lock, lock, tunnels_lock
69 struct _GstRTSPClientPrivate
71 GMutex lock; /* protects everything else */
74 GstRTSPConnection *connection;
76 GMainContext *watch_context;
80 /* protected by send_lock */
81 GstRTSPClientSendFunc send_func;
83 GDestroyNotify send_notify;
84 GstRTSPClientSendMessagesFunc send_messages_func;
85 gpointer send_messages_data;
86 GDestroyNotify send_messages_notify;
89 GstRTSPSessionPool *session_pool;
90 gulong session_removed_id;
91 GstRTSPMountPoints *mount_points;
93 GstRTSPThreadPool *thread_pool;
95 /* used to cache the media in the last requested DESCRIBE so that
96 * we can pick it up in the next SETUP immediately */
100 GHashTable *transports;
102 guint sessions_cookie;
104 gboolean drop_backlog;
105 gint post_session_timeout;
107 guint content_length_limit;
109 gboolean had_session;
110 GSource *rtsp_ctrl_timeout;
111 guint rtsp_ctrl_timeout_cnt;
113 /* The version currently being used */
114 GstRTSPVersion version;
116 GHashTable *pipelined_requests; /* pipelined_request_id -> session_id */
117 GstRTSPTunnelState tstate;
126 static GMutex tunnels_lock;
127 static GHashTable *tunnels; /* protected by tunnels_lock */
129 #define WATCH_BACKLOG_SIZE 100
131 #define DEFAULT_SESSION_POOL NULL
132 #define DEFAULT_MOUNT_POINTS NULL
133 #define DEFAULT_DROP_BACKLOG TRUE
134 #define DEFAULT_POST_SESSION_TIMEOUT -1
136 #define RTSP_CTRL_CB_INTERVAL 1
137 #define RTSP_CTRL_TIMEOUT_VALUE 60
145 PROP_POST_SESSION_TIMEOUT,
153 SIGNAL_PRE_OPTIONS_REQUEST,
154 SIGNAL_OPTIONS_REQUEST,
155 SIGNAL_PRE_DESCRIBE_REQUEST,
156 SIGNAL_DESCRIBE_REQUEST,
157 SIGNAL_PRE_SETUP_REQUEST,
158 SIGNAL_SETUP_REQUEST,
159 SIGNAL_PRE_PLAY_REQUEST,
161 SIGNAL_PRE_PAUSE_REQUEST,
162 SIGNAL_PAUSE_REQUEST,
163 SIGNAL_PRE_TEARDOWN_REQUEST,
164 SIGNAL_TEARDOWN_REQUEST,
165 SIGNAL_PRE_SET_PARAMETER_REQUEST,
166 SIGNAL_SET_PARAMETER_REQUEST,
167 SIGNAL_PRE_GET_PARAMETER_REQUEST,
168 SIGNAL_GET_PARAMETER_REQUEST,
169 SIGNAL_HANDLE_RESPONSE,
171 SIGNAL_PRE_ANNOUNCE_REQUEST,
172 SIGNAL_ANNOUNCE_REQUEST,
173 SIGNAL_PRE_RECORD_REQUEST,
174 SIGNAL_RECORD_REQUEST,
175 SIGNAL_CHECK_REQUIREMENTS,
179 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
180 #define GST_CAT_DEFAULT rtsp_client_debug
182 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
184 static void gst_rtsp_client_get_property (GObject * object, guint propid,
185 GValue * value, GParamSpec * pspec);
186 static void gst_rtsp_client_set_property (GObject * object, guint propid,
187 const GValue * value, GParamSpec * pspec);
188 static void gst_rtsp_client_finalize (GObject * obj);
190 static void rtsp_ctrl_timeout_remove (GstRTSPClient * client);
192 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
193 static gboolean handle_sdp (GstRTSPClient * client, GstRTSPContext * ctx,
194 GstRTSPMedia * media, GstSDPMessage * sdp);
195 static gboolean default_configure_client_media (GstRTSPClient * client,
196 GstRTSPMedia * media, GstRTSPStream * stream, GstRTSPContext * ctx);
197 static gboolean default_configure_client_transport (GstRTSPClient * client,
198 GstRTSPContext * ctx, GstRTSPTransport * ct);
199 static GstRTSPResult default_params_set (GstRTSPClient * client,
200 GstRTSPContext * ctx);
201 static GstRTSPResult default_params_get (GstRTSPClient * client,
202 GstRTSPContext * ctx);
203 static gchar *default_make_path_from_uri (GstRTSPClient * client,
204 const GstRTSPUrl * uri);
205 static gboolean default_handle_options_request (GstRTSPClient * client,
206 GstRTSPContext * ctx, GstRTSPVersion version);
207 static gboolean default_handle_set_param_request (GstRTSPClient * client,
208 GstRTSPContext * ctx);
209 static gboolean default_handle_get_param_request (GstRTSPClient * client,
210 GstRTSPContext * ctx);
211 static gboolean default_handle_play_request (GstRTSPClient * client,
212 GstRTSPContext * ctx);
214 static void client_session_removed (GstRTSPSessionPool * pool,
215 GstRTSPSession * session, GstRTSPClient * client);
216 static GstRTSPStatusCode default_pre_signal_handler (GstRTSPClient * client,
217 GstRTSPContext * ctx);
218 static gboolean pre_signal_accumulator (GSignalInvocationHint * ihint,
219 GValue * return_accu, const GValue * handler_return, gpointer data);
220 gboolean gst_rtsp_media_has_completed_sender (GstRTSPMedia * media);
222 G_DEFINE_TYPE_WITH_PRIVATE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
225 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
227 GObjectClass *gobject_class;
229 gobject_class = G_OBJECT_CLASS (klass);
231 gobject_class->get_property = gst_rtsp_client_get_property;
232 gobject_class->set_property = gst_rtsp_client_set_property;
233 gobject_class->finalize = gst_rtsp_client_finalize;
235 klass->create_sdp = create_sdp;
236 klass->handle_sdp = handle_sdp;
237 klass->configure_client_media = default_configure_client_media;
238 klass->configure_client_transport = default_configure_client_transport;
239 klass->params_set = default_params_set;
240 klass->params_get = default_params_get;
241 klass->make_path_from_uri = default_make_path_from_uri;
242 klass->handle_options_request = default_handle_options_request;
243 klass->handle_set_param_request = default_handle_set_param_request;
244 klass->handle_get_param_request = default_handle_get_param_request;
245 klass->handle_play_request = default_handle_play_request;
247 klass->pre_options_request = default_pre_signal_handler;
248 klass->pre_describe_request = default_pre_signal_handler;
249 klass->pre_setup_request = default_pre_signal_handler;
250 klass->pre_play_request = default_pre_signal_handler;
251 klass->pre_pause_request = default_pre_signal_handler;
252 klass->pre_teardown_request = default_pre_signal_handler;
253 klass->pre_set_parameter_request = default_pre_signal_handler;
254 klass->pre_get_parameter_request = default_pre_signal_handler;
255 klass->pre_announce_request = default_pre_signal_handler;
256 klass->pre_record_request = default_pre_signal_handler;
258 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
259 g_param_spec_object ("session-pool", "Session Pool",
260 "The session pool to use for client session",
261 GST_TYPE_RTSP_SESSION_POOL,
262 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
264 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
265 g_param_spec_object ("mount-points", "Mount Points",
266 "The mount points to use for client session",
267 GST_TYPE_RTSP_MOUNT_POINTS,
268 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
270 g_object_class_install_property (gobject_class, PROP_DROP_BACKLOG,
271 g_param_spec_boolean ("drop-backlog", "Drop Backlog",
272 "Drop data when the backlog queue is full",
273 DEFAULT_DROP_BACKLOG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
276 * GstRTSPClient::post-session-timeout:
278 * An extra tcp timeout ( > 0) after session timeout, in seconds.
279 * The tcp connection will be kept alive until this timeout happens to give
280 * the client a possibility to reuse the connection.
281 * 0 means that the connection will be closed immediately after the session
284 * Default value is -1 seconds, meaning that we let the system close
289 g_object_class_install_property (gobject_class, PROP_POST_SESSION_TIMEOUT,
290 g_param_spec_int ("post-session-timeout", "Post Session Timeout",
291 "An extra TCP connection timeout after session timeout", G_MININT,
292 G_MAXINT, DEFAULT_POST_SESSION_TIMEOUT,
293 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
295 gst_rtsp_client_signals[SIGNAL_CLOSED] =
296 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
297 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL, NULL,
298 G_TYPE_NONE, 0, G_TYPE_NONE);
300 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
301 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
302 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL, NULL,
303 G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
306 * GstRTSPClient::pre-options-request:
307 * @client: a #GstRTSPClient
308 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
310 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
311 * otherwise an appropriate return code
315 gst_rtsp_client_signals[SIGNAL_PRE_OPTIONS_REQUEST] =
316 g_signal_new ("pre-options-request", G_TYPE_FROM_CLASS (klass),
317 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
318 pre_options_request), pre_signal_accumulator, NULL, NULL,
319 GST_TYPE_RTSP_STATUS_CODE, 1, GST_TYPE_RTSP_CONTEXT);
322 * GstRTSPClient::options-request:
323 * @client: a #GstRTSPClient
324 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
326 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
327 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
328 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
329 NULL, NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
332 * GstRTSPClient::pre-describe-request:
333 * @client: a #GstRTSPClient
334 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
336 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
337 * otherwise an appropriate return code
341 gst_rtsp_client_signals[SIGNAL_PRE_DESCRIBE_REQUEST] =
342 g_signal_new ("pre-describe-request", G_TYPE_FROM_CLASS (klass),
343 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
344 pre_describe_request), pre_signal_accumulator, NULL, NULL,
345 GST_TYPE_RTSP_STATUS_CODE, 1, GST_TYPE_RTSP_CONTEXT);
348 * GstRTSPClient::describe-request:
349 * @client: a #GstRTSPClient
350 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
352 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
353 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
354 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
355 NULL, NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
358 * GstRTSPClient::pre-setup-request:
359 * @client: a #GstRTSPClient
360 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
362 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
363 * otherwise an appropriate return code
367 gst_rtsp_client_signals[SIGNAL_PRE_SETUP_REQUEST] =
368 g_signal_new ("pre-setup-request", G_TYPE_FROM_CLASS (klass),
369 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
370 pre_setup_request), pre_signal_accumulator, NULL, NULL,
371 GST_TYPE_RTSP_STATUS_CODE, 1, GST_TYPE_RTSP_CONTEXT);
374 * GstRTSPClient::setup-request:
375 * @client: a #GstRTSPClient
376 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
378 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
379 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
380 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
381 NULL, NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
384 * GstRTSPClient::pre-play-request:
385 * @client: a #GstRTSPClient
386 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
388 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
389 * otherwise an appropriate return code
393 gst_rtsp_client_signals[SIGNAL_PRE_PLAY_REQUEST] =
394 g_signal_new ("pre-play-request", G_TYPE_FROM_CLASS (klass),
395 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
396 pre_play_request), pre_signal_accumulator, NULL,
397 NULL, GST_TYPE_RTSP_STATUS_CODE, 1, GST_TYPE_RTSP_CONTEXT);
400 * GstRTSPClient::play-request:
401 * @client: a #GstRTSPClient
402 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
404 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
405 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
406 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
407 NULL, NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
410 * GstRTSPClient::pre-pause-request:
411 * @client: a #GstRTSPClient
412 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
414 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
415 * otherwise an appropriate return code
419 gst_rtsp_client_signals[SIGNAL_PRE_PAUSE_REQUEST] =
420 g_signal_new ("pre-pause-request", G_TYPE_FROM_CLASS (klass),
421 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
422 pre_pause_request), pre_signal_accumulator, NULL, NULL,
423 GST_TYPE_RTSP_STATUS_CODE, 1, GST_TYPE_RTSP_CONTEXT);
426 * GstRTSPClient::pause-request:
427 * @client: a #GstRTSPClient
428 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
430 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
431 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
432 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
433 NULL, NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
436 * GstRTSPClient::pre-teardown-request:
437 * @client: a #GstRTSPClient
438 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
440 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
441 * otherwise an appropriate return code
445 gst_rtsp_client_signals[SIGNAL_PRE_TEARDOWN_REQUEST] =
446 g_signal_new ("pre-teardown-request", G_TYPE_FROM_CLASS (klass),
447 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
448 pre_teardown_request), pre_signal_accumulator, NULL, NULL,
449 GST_TYPE_RTSP_STATUS_CODE, 1, GST_TYPE_RTSP_CONTEXT);
452 * GstRTSPClient::teardown-request:
453 * @client: a #GstRTSPClient
454 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
456 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
457 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
458 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
459 NULL, NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
462 * GstRTSPClient::pre-set-parameter-request:
463 * @client: a #GstRTSPClient
464 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
466 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
467 * otherwise an appropriate return code
471 gst_rtsp_client_signals[SIGNAL_PRE_SET_PARAMETER_REQUEST] =
472 g_signal_new ("pre-set-parameter-request", G_TYPE_FROM_CLASS (klass),
473 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
474 pre_set_parameter_request), pre_signal_accumulator, NULL, NULL,
475 GST_TYPE_RTSP_STATUS_CODE, 1, GST_TYPE_RTSP_CONTEXT);
478 * GstRTSPClient::set-parameter-request:
479 * @client: a #GstRTSPClient
480 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
482 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
483 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
484 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
485 set_parameter_request), NULL, NULL, NULL,
486 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
489 * GstRTSPClient::pre-get-parameter-request:
490 * @client: a #GstRTSPClient
491 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
493 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
494 * otherwise an appropriate return code
498 gst_rtsp_client_signals[SIGNAL_PRE_GET_PARAMETER_REQUEST] =
499 g_signal_new ("pre-get-parameter-request", G_TYPE_FROM_CLASS (klass),
500 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
501 pre_get_parameter_request), pre_signal_accumulator, NULL, NULL,
502 GST_TYPE_RTSP_STATUS_CODE, 1, GST_TYPE_RTSP_CONTEXT);
505 * GstRTSPClient::get-parameter-request:
506 * @client: a #GstRTSPClient
507 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
509 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
510 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
511 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
512 get_parameter_request), NULL, NULL, NULL,
513 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
516 * GstRTSPClient::handle-response:
517 * @client: a #GstRTSPClient
518 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
520 gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE] =
521 g_signal_new ("handle-response", G_TYPE_FROM_CLASS (klass),
522 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
523 handle_response), NULL, NULL, NULL,
524 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
527 * GstRTSPClient::send-message:
528 * @client: The RTSP client
529 * @session: (type GstRtspServer.RTSPSession): The session
530 * @message: (type GstRtsp.RTSPMessage): The message
532 gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE] =
533 g_signal_new ("send-message", G_TYPE_FROM_CLASS (klass),
534 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
535 send_message), NULL, NULL, NULL,
536 G_TYPE_NONE, 2, GST_TYPE_RTSP_CONTEXT, G_TYPE_POINTER);
539 * GstRTSPClient::pre-announce-request:
540 * @client: a #GstRTSPClient
541 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
543 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
544 * otherwise an appropriate return code
548 gst_rtsp_client_signals[SIGNAL_PRE_ANNOUNCE_REQUEST] =
549 g_signal_new ("pre-announce-request", G_TYPE_FROM_CLASS (klass),
550 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
551 pre_announce_request), pre_signal_accumulator, NULL, NULL,
552 GST_TYPE_RTSP_STATUS_CODE, 1, GST_TYPE_RTSP_CONTEXT);
555 * GstRTSPClient::announce-request:
556 * @client: a #GstRTSPClient
557 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
559 gst_rtsp_client_signals[SIGNAL_ANNOUNCE_REQUEST] =
560 g_signal_new ("announce-request", G_TYPE_FROM_CLASS (klass),
561 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, announce_request),
562 NULL, NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
565 * GstRTSPClient::pre-record-request:
566 * @client: a #GstRTSPClient
567 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
569 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
570 * otherwise an appropriate return code
574 gst_rtsp_client_signals[SIGNAL_PRE_RECORD_REQUEST] =
575 g_signal_new ("pre-record-request", G_TYPE_FROM_CLASS (klass),
576 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
577 pre_record_request), pre_signal_accumulator, NULL, NULL,
578 GST_TYPE_RTSP_STATUS_CODE, 1, GST_TYPE_RTSP_CONTEXT);
581 * GstRTSPClient::record-request:
582 * @client: a #GstRTSPClient
583 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
585 gst_rtsp_client_signals[SIGNAL_RECORD_REQUEST] =
586 g_signal_new ("record-request", G_TYPE_FROM_CLASS (klass),
587 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, record_request),
588 NULL, NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
591 * GstRTSPClient::check-requirements:
592 * @client: a #GstRTSPClient
593 * @ctx: (type GstRtspServer.RTSPContext): a #GstRTSPContext
594 * @arr: a NULL-terminated array of strings
596 * Returns: a newly allocated string with comma-separated list of
597 * unsupported options. An empty string must be returned if
598 * all options are supported.
602 gst_rtsp_client_signals[SIGNAL_CHECK_REQUIREMENTS] =
603 g_signal_new ("check-requirements", G_TYPE_FROM_CLASS (klass),
604 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
605 check_requirements), NULL, NULL, NULL,
606 G_TYPE_STRING, 2, GST_TYPE_RTSP_CONTEXT, G_TYPE_STRV);
609 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
610 g_mutex_init (&tunnels_lock);
612 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
616 gst_rtsp_client_init (GstRTSPClient * client)
618 GstRTSPClientPrivate *priv = gst_rtsp_client_get_instance_private (client);
622 g_mutex_init (&priv->lock);
623 g_mutex_init (&priv->send_lock);
624 g_mutex_init (&priv->watch_lock);
625 priv->data_seqs = g_array_new (FALSE, FALSE, sizeof (DataSeq));
626 priv->drop_backlog = DEFAULT_DROP_BACKLOG;
627 priv->post_session_timeout = DEFAULT_POST_SESSION_TIMEOUT;
629 g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
631 priv->pipelined_requests = g_hash_table_new_full (g_str_hash,
632 g_str_equal, g_free, g_free);
633 priv->tstate = TUNNEL_STATE_UNKNOWN;
634 priv->content_length_limit = G_MAXUINT;
637 static GstRTSPFilterResult
638 filter_session_media (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
641 gboolean *closed = user_data;
644 gboolean is_all_udp = TRUE;
646 media = gst_rtsp_session_media_get_media (sessmedia);
647 n_streams = gst_rtsp_media_n_streams (media);
649 for (i = 0; i < n_streams; i++) {
650 GstRTSPStreamTransport *transport =
651 gst_rtsp_session_media_get_transport (sessmedia, i);
652 const GstRTSPTransport *rtsp_transport;
657 rtsp_transport = gst_rtsp_stream_transport_get_transport (transport);
659 && rtsp_transport->lower_transport != GST_RTSP_LOWER_TRANS_UDP
660 && rtsp_transport->lower_transport != GST_RTSP_LOWER_TRANS_UDP_MCAST) {
666 if (!is_all_udp || gst_rtsp_media_is_stop_on_disconnect (media)) {
667 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
668 return GST_RTSP_FILTER_REMOVE;
671 return GST_RTSP_FILTER_KEEP;
676 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
678 GstRTSPClientPrivate *priv = client->priv;
680 g_mutex_lock (&priv->lock);
681 /* check if we already know about this session */
682 if (g_list_find (priv->sessions, session) == NULL) {
683 GST_INFO ("watching session %p", session);
685 priv->sessions = g_list_prepend (priv->sessions, g_object_ref (session));
686 priv->sessions_cookie++;
688 /* connect removed session handler, it will be disconnected when the last
689 * session gets removed */
690 if (priv->session_removed_id == 0)
691 priv->session_removed_id = g_signal_connect_data (priv->session_pool,
692 "session-removed", G_CALLBACK (client_session_removed),
693 g_object_ref (client), (GClosureNotify) g_object_unref, 0);
695 g_mutex_unlock (&priv->lock);
700 /* should be called with lock */
702 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session,
705 GstRTSPClientPrivate *priv = client->priv;
707 GST_INFO ("client %p: unwatch session %p", client, session);
710 link = g_list_find (priv->sessions, session);
715 priv->sessions = g_list_delete_link (priv->sessions, link);
716 priv->sessions_cookie++;
718 /* if this was the last session, disconnect the handler.
719 * This will also drop the extra client ref */
720 if (!priv->sessions) {
721 g_signal_handler_disconnect (priv->session_pool, priv->session_removed_id);
722 priv->session_removed_id = 0;
725 if (!priv->drop_backlog) {
726 /* unlink all media managed in this session */
727 gst_rtsp_session_filter (session, filter_session_media, client);
730 /* remove the session */
731 g_object_unref (session);
734 static GstRTSPFilterResult
735 cleanup_session (GstRTSPClient * client, GstRTSPSession * sess,
738 gboolean *closed = user_data;
739 GstRTSPClientPrivate *priv = client->priv;
741 if (priv->drop_backlog) {
742 /* unlink all media managed in this session. This needs to happen
743 * without the client lock, so we really want to do it here. */
744 gst_rtsp_session_filter (sess, filter_session_media, user_data);
748 return GST_RTSP_FILTER_REMOVE;
750 return GST_RTSP_FILTER_KEEP;
754 clean_cached_media (GstRTSPClient * client, gboolean unprepare)
756 GstRTSPClientPrivate *priv = client->priv;
764 gst_rtsp_media_unprepare (priv->media);
765 g_object_unref (priv->media);
770 /* A client is finalized when the connection is broken */
772 gst_rtsp_client_finalize (GObject * obj)
774 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
775 GstRTSPClientPrivate *priv = client->priv;
777 GST_INFO ("finalize client %p", client);
779 /* the watch and related state should be cleared before finalize
780 * as the watch actually holds a strong reference to the client */
781 g_assert (priv->watch == NULL);
782 g_assert (priv->rtsp_ctrl_timeout == NULL);
784 if (priv->watch_context) {
785 g_main_context_unref (priv->watch_context);
786 priv->watch_context = NULL;
789 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
790 gst_rtsp_client_set_send_messages_func (client, NULL, NULL, NULL);
792 /* all sessions should have been removed by now. We keep a ref to
793 * the client object for the session removed handler. The ref is
794 * dropped when the last session is removed from the list. */
795 g_assert (priv->sessions == NULL);
796 g_assert (priv->session_removed_id == 0);
798 g_array_unref (priv->data_seqs);
799 g_hash_table_unref (priv->transports);
800 g_hash_table_unref (priv->pipelined_requests);
802 if (priv->connection)
803 gst_rtsp_connection_free (priv->connection);
804 if (priv->session_pool) {
805 g_object_unref (priv->session_pool);
807 if (priv->mount_points)
808 g_object_unref (priv->mount_points);
810 g_object_unref (priv->auth);
811 if (priv->thread_pool)
812 g_object_unref (priv->thread_pool);
814 clean_cached_media (client, TRUE);
816 g_free (priv->server_ip);
817 g_mutex_clear (&priv->lock);
818 g_mutex_clear (&priv->send_lock);
819 g_mutex_clear (&priv->watch_lock);
821 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
825 gst_rtsp_client_get_property (GObject * object, guint propid,
826 GValue * value, GParamSpec * pspec)
828 GstRTSPClient *client = GST_RTSP_CLIENT (object);
829 GstRTSPClientPrivate *priv = client->priv;
832 case PROP_SESSION_POOL:
833 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
835 case PROP_MOUNT_POINTS:
836 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
838 case PROP_DROP_BACKLOG:
839 g_value_set_boolean (value, priv->drop_backlog);
841 case PROP_POST_SESSION_TIMEOUT:
842 g_value_set_int (value, priv->post_session_timeout);
845 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
850 gst_rtsp_client_set_property (GObject * object, guint propid,
851 const GValue * value, GParamSpec * pspec)
853 GstRTSPClient *client = GST_RTSP_CLIENT (object);
854 GstRTSPClientPrivate *priv = client->priv;
857 case PROP_SESSION_POOL:
858 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
860 case PROP_MOUNT_POINTS:
861 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
863 case PROP_DROP_BACKLOG:
864 g_mutex_lock (&priv->lock);
865 priv->drop_backlog = g_value_get_boolean (value);
866 g_mutex_unlock (&priv->lock);
868 case PROP_POST_SESSION_TIMEOUT:
869 g_mutex_lock (&priv->lock);
870 priv->post_session_timeout = g_value_get_int (value);
871 g_mutex_unlock (&priv->lock);
874 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
879 * gst_rtsp_client_new:
881 * Create a new #GstRTSPClient instance.
883 * Returns: (transfer full): a new #GstRTSPClient
886 gst_rtsp_client_new (void)
888 GstRTSPClient *result;
890 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
896 send_message (GstRTSPClient * client, GstRTSPContext * ctx,
897 GstRTSPMessage * message, gboolean close)
899 GstRTSPClientPrivate *priv = client->priv;
901 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
902 "GStreamer RTSP server");
904 /* remove any previous header */
905 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
907 /* add the new session header for new session ids */
909 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
910 gst_rtsp_session_get_header (ctx->session));
913 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
914 gst_rtsp_message_dump (message);
918 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
921 message->type_data.response.version =
922 ctx->request->type_data.request.version;
924 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE],
927 g_mutex_lock (&priv->send_lock);
928 if (priv->send_messages_func) {
929 priv->send_messages_func (client, message, 1, close, priv->send_data);
930 } else if (priv->send_func) {
931 priv->send_func (client, message, close, priv->send_data);
933 g_mutex_unlock (&priv->send_lock);
935 gst_rtsp_message_unset (message);
939 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
940 GstRTSPContext * ctx)
942 gst_rtsp_message_init_response (ctx->response, code,
943 gst_rtsp_status_as_text (code), ctx->request);
947 send_message (client, ctx, ctx->response, FALSE);
951 send_option_not_supported_response (GstRTSPClient * client,
952 GstRTSPContext * ctx, const gchar * unsupported_options)
954 GstRTSPStatusCode code = GST_RTSP_STS_OPTION_NOT_SUPPORTED;
956 gst_rtsp_message_init_response (ctx->response, code,
957 gst_rtsp_status_as_text (code), ctx->request);
959 if (unsupported_options != NULL) {
960 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_UNSUPPORTED,
961 unsupported_options);
966 send_message (client, ctx, ctx->response, FALSE);
970 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
972 if (path1 == NULL || path2 == NULL)
975 if (strlen (path1) != len2)
978 if (strncmp (path1, path2, len2))
984 /* this function is called to initially find the media for the DESCRIBE request
985 * but is cached for when the same client (without breaking the connection) is
986 * doing a setup for the exact same url. */
987 static GstRTSPMedia *
988 find_media (GstRTSPClient * client, GstRTSPContext * ctx, gchar * path,
991 GstRTSPClientPrivate *priv = client->priv;
992 GstRTSPMediaFactory *factory;
996 /* find the longest matching factory for the uri first */
997 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
1001 ctx->factory = factory;
1003 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
1004 goto no_factory_access;
1006 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
1007 goto not_authorized;
1010 path_len = *matched;
1012 path_len = strlen (path);
1014 if (!paths_are_equal (priv->path, path, path_len)) {
1015 /* remove any previously cached values before we try to construct a new
1017 clean_cached_media (client, TRUE);
1019 /* prepare the media and add it to the pipeline */
1020 if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
1025 if (!(gst_rtsp_media_get_transport_mode (media) &
1026 GST_RTSP_TRANSPORT_MODE_RECORD)) {
1027 GstRTSPThread *thread;
1029 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
1030 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
1034 /* prepare the media */
1035 if (!gst_rtsp_media_prepare (media, thread))
1039 /* now keep track of the uri and the media */
1040 priv->path = g_strndup (path, path_len);
1041 priv->media = media;
1043 /* we have seen this path before, used cached media */
1044 media = priv->media;
1046 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
1049 g_object_unref (factory);
1050 ctx->factory = NULL;
1053 g_object_ref (media);
1060 GST_ERROR ("client %p: no factory for path %s", client, path);
1061 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1066 g_object_unref (factory);
1067 ctx->factory = NULL;
1068 GST_ERROR ("client %p: not authorized to see factory path %s", client,
1070 /* error reply is already sent */
1075 g_object_unref (factory);
1076 ctx->factory = NULL;
1077 GST_ERROR ("client %p: not authorized for factory path %s", client, path);
1078 /* error reply is already sent */
1083 GST_ERROR ("client %p: can't create media", client);
1084 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1085 g_object_unref (factory);
1086 ctx->factory = NULL;
1091 GST_ERROR ("client %p: can't create thread", client);
1092 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1093 g_object_unref (media);
1095 g_object_unref (factory);
1096 ctx->factory = NULL;
1101 GST_ERROR ("client %p: can't prepare media", client);
1102 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1103 g_object_unref (media);
1105 g_object_unref (factory);
1106 ctx->factory = NULL;
1111 static inline DataSeq *
1112 get_data_seq_element (GstRTSPClient * client, guint8 channel)
1114 GstRTSPClientPrivate *priv = client->priv;
1115 GArray *data_seqs = priv->data_seqs;
1118 while (i < data_seqs->len) {
1119 DataSeq *data_seq = &g_array_index (data_seqs, DataSeq, i);
1120 if (data_seq->channel == channel)
1129 add_data_seq (GstRTSPClient * client, guint8 channel)
1131 GstRTSPClientPrivate *priv = client->priv;
1132 DataSeq data_seq = {.channel = channel,.seq = 0 };
1134 if (get_data_seq_element (client, channel) == NULL)
1135 g_array_append_val (priv->data_seqs, data_seq);
1139 set_data_seq (GstRTSPClient * client, guint8 channel, guint seq)
1143 data_seq = get_data_seq_element (client, channel);
1144 g_assert_nonnull (data_seq);
1145 data_seq->seq = seq;
1149 get_data_seq (GstRTSPClient * client, guint8 channel)
1153 data_seq = get_data_seq_element (client, channel);
1154 g_assert_nonnull (data_seq);
1155 return data_seq->seq;
1159 get_data_channel (GstRTSPClient * client, guint seq, guint8 * channel)
1161 GstRTSPClientPrivate *priv = client->priv;
1162 GArray *data_seqs = priv->data_seqs;
1165 while (i < data_seqs->len) {
1166 DataSeq *data_seq = &g_array_index (data_seqs, DataSeq, i);
1167 if (data_seq->seq == seq) {
1168 *channel = data_seq->channel;
1178 do_close (gpointer user_data)
1180 GstRTSPClient *client = user_data;
1182 gst_rtsp_client_close (client);
1184 return G_SOURCE_REMOVE;
1188 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
1190 GstRTSPClientPrivate *priv = client->priv;
1191 GstRTSPMessage message = { 0 };
1192 gboolean ret = TRUE;
1194 gst_rtsp_message_init_data (&message, channel);
1196 gst_rtsp_message_set_body_buffer (&message, buffer);
1198 g_mutex_lock (&priv->send_lock);
1199 if (get_data_seq (client, channel) != 0) {
1200 GST_WARNING ("already a queued data message for channel %d", channel);
1201 g_mutex_unlock (&priv->send_lock);
1204 if (priv->send_messages_func) {
1206 priv->send_messages_func (client, &message, 1, FALSE, priv->send_data);
1207 } else if (priv->send_func) {
1208 ret = priv->send_func (client, &message, FALSE, priv->send_data);
1210 g_mutex_unlock (&priv->send_lock);
1212 gst_rtsp_message_unset (&message);
1217 /* close in watch context */
1218 idle_src = g_idle_source_new ();
1219 g_source_set_callback (idle_src, do_close, client, NULL);
1220 g_source_attach (idle_src, priv->watch_context);
1221 g_source_unref (idle_src);
1228 do_check_back_pressure (guint8 channel, GstRTSPClient * client)
1230 return get_data_seq (client, channel) != 0;
1234 do_send_data_list (GstBufferList * buffer_list, guint8 channel,
1235 GstRTSPClient * client)
1237 GstRTSPClientPrivate *priv = client->priv;
1238 gboolean ret = TRUE;
1239 guint i, n = gst_buffer_list_length (buffer_list);
1240 GstRTSPMessage *messages;
1242 g_mutex_lock (&priv->send_lock);
1243 if (get_data_seq (client, channel) != 0) {
1244 GST_WARNING ("already a queued data message for channel %d", channel);
1245 g_mutex_unlock (&priv->send_lock);
1249 messages = g_newa (GstRTSPMessage, n);
1250 memset (messages, 0, sizeof (GstRTSPMessage) * n);
1251 for (i = 0; i < n; i++) {
1252 GstBuffer *buffer = gst_buffer_list_get (buffer_list, i);
1253 gst_rtsp_message_init_data (&messages[i], channel);
1254 gst_rtsp_message_set_body_buffer (&messages[i], buffer);
1257 if (priv->send_messages_func) {
1259 priv->send_messages_func (client, messages, n, FALSE, priv->send_data);
1260 } else if (priv->send_func) {
1261 for (i = 0; i < n; i++) {
1262 ret = priv->send_func (client, &messages[i], FALSE, priv->send_data);
1267 g_mutex_unlock (&priv->send_lock);
1269 for (i = 0; i < n; i++) {
1270 gst_rtsp_message_unset (&messages[i]);
1276 /* close in watch context */
1277 idle_src = g_idle_source_new ();
1278 g_source_set_callback (idle_src, do_close, client, NULL);
1279 g_source_attach (idle_src, priv->watch_context);
1280 g_source_unref (idle_src);
1287 * gst_rtsp_client_close:
1288 * @client: a #GstRTSPClient
1290 * Close the connection of @client and remove all media it was managing.
1295 gst_rtsp_client_close (GstRTSPClient * client)
1297 GstRTSPClientPrivate *priv = client->priv;
1298 const gchar *tunnelid;
1300 GST_DEBUG ("client %p: closing connection", client);
1302 g_mutex_lock (&priv->watch_lock);
1304 /* Work around the lack of thread safety of gst_rtsp_connection_close */
1306 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
1309 if (priv->connection) {
1310 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
1311 g_mutex_lock (&tunnels_lock);
1312 /* remove from tunnelids */
1313 g_hash_table_remove (tunnels, tunnelid);
1314 g_mutex_unlock (&tunnels_lock);
1316 gst_rtsp_connection_flush (priv->connection, TRUE);
1317 gst_rtsp_connection_close (priv->connection);
1321 GST_DEBUG ("client %p: destroying watch", client);
1322 g_source_destroy ((GSource *) priv->watch);
1324 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
1325 gst_rtsp_client_set_send_messages_func (client, NULL, NULL, NULL);
1326 rtsp_ctrl_timeout_remove (client);
1329 g_mutex_unlock (&priv->watch_lock);
1333 default_make_path_from_uri (GstRTSPClient * client, const GstRTSPUrl * uri)
1338 path = g_strconcat (uri->abspath, "?", uri->query, NULL);
1340 /* normalize rtsp://<IP>:<PORT> to rtsp://<IP>:<PORT>/ */
1341 path = g_strdup (uri->abspath[0] ? uri->abspath : "/");
1347 /* Default signal handler function for all "pre-command" signals, like
1348 * pre-options-request. It just returns the RTSP return code 200.
1349 * Subclasses can override this to get another default behaviour.
1351 static GstRTSPStatusCode
1352 default_pre_signal_handler (GstRTSPClient * client, GstRTSPContext * ctx)
1354 GST_LOG_OBJECT (client, "returning GST_RTSP_STS_OK");
1355 return GST_RTSP_STS_OK;
1358 /* The pre-signal accumulator function checks the return value of the signal
1359 * handlers. If any of them returns an RTSP status code that does not start
1360 * with 2 it will return FALSE, no more signal handlers will be called, and
1361 * this last RTSP status code will be the result of the signal emission.
1364 pre_signal_accumulator (GSignalInvocationHint * ihint, GValue * return_accu,
1365 const GValue * handler_return, gpointer data)
1367 GstRTSPStatusCode handler_value = g_value_get_enum (handler_return);
1368 GstRTSPStatusCode accumulated_value = g_value_get_enum (return_accu);
1370 if (handler_value < 200 || handler_value > 299) {
1371 GST_DEBUG ("handler_value : %d, returning FALSE", handler_value);
1372 g_value_set_enum (return_accu, handler_value);
1376 /* the accumulated value is initiated to 0 by GLib. if current handler value is
1377 * bigger then use that instead
1379 * FIXME: Should we prioritize the 2xx codes in a smarter way?
1380 * Like, "201 Created" > "250 Low On Storage Space" > "200 OK"?
1382 if (handler_value > accumulated_value)
1383 g_value_set_enum (return_accu, handler_value);
1388 /* The cleanup_transports function is called from handle_teardown_request() to
1389 * remove any stream transports from the newly closed session that were added to
1390 * priv->transports in handle_setup_request(). This is done to avoid forwarding
1391 * data from the client on a channel that we just closed.
1394 cleanup_transports (GstRTSPClient * client, GPtrArray * transports)
1396 GstRTSPClientPrivate *priv = client->priv;
1397 GstRTSPStreamTransport *stream_transport;
1398 const GstRTSPTransport *rtsp_transport;
1401 GST_LOG_OBJECT (client, "potentially removing %u transports",
1404 for (i = 0; i < transports->len; i++) {
1405 stream_transport = g_ptr_array_index (transports, i);
1406 if (stream_transport == NULL) {
1407 GST_LOG_OBJECT (client, "stream transport %u is NULL, continue", i);
1411 rtsp_transport = gst_rtsp_stream_transport_get_transport (stream_transport);
1412 if (rtsp_transport == NULL) {
1413 GST_LOG_OBJECT (client, "RTSP transport %u is NULL, continue", i);
1417 /* priv->transport only stores transports where RTP is tunneled over RTSP */
1418 if (rtsp_transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1419 if (!g_hash_table_remove (priv->transports,
1420 GINT_TO_POINTER (rtsp_transport->interleaved.min))) {
1421 GST_WARNING_OBJECT (client,
1422 "failed removing transport with key '%d' from priv->transports",
1423 rtsp_transport->interleaved.min);
1425 if (!g_hash_table_remove (priv->transports,
1426 GINT_TO_POINTER (rtsp_transport->interleaved.max))) {
1427 GST_WARNING_OBJECT (client,
1428 "failed removing transport with key '%d' from priv->transports",
1429 rtsp_transport->interleaved.max);
1432 GST_LOG_OBJECT (client, "transport %u not RTP/RTSP, skip it", i);
1438 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
1440 GstRTSPClientPrivate *priv = client->priv;
1441 GstRTSPClientClass *klass;
1442 GstRTSPSession *session;
1443 GstRTSPSessionMedia *sessmedia;
1444 GstRTSPMedia *media;
1445 GstRTSPStatusCode code;
1448 gboolean keep_session;
1449 GstRTSPStatusCode sig_result;
1450 GPtrArray *session_media_transports;
1455 session = ctx->session;
1460 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1461 path = klass->make_path_from_uri (client, ctx->uri);
1463 /* get a handle to the configuration of the media in the session */
1464 sessmedia = gst_rtsp_session_dup_media (session, path, &matched);
1468 /* only aggregate control for now.. */
1469 if (path[matched] != '\0')
1474 ctx->sessmedia = sessmedia;
1476 media = gst_rtsp_session_media_get_media (sessmedia);
1477 g_object_ref (media);
1478 gst_rtsp_media_lock (media);
1480 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_TEARDOWN_REQUEST],
1481 0, ctx, &sig_result);
1482 if (sig_result != GST_RTSP_STS_OK) {
1486 /* get a reference to the transports in the session media so we can clean up
1487 * our priv->transports before returning */
1488 session_media_transports = gst_rtsp_session_media_get_transports (sessmedia);
1490 /* we emit the signal before closing the connection */
1491 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
1494 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
1496 /* unmanage the media in the session, returns false if all media session
1498 keep_session = gst_rtsp_session_release_media (session, sessmedia);
1499 g_object_unref (sessmedia);
1501 /* construct the response now */
1502 code = GST_RTSP_STS_OK;
1503 gst_rtsp_message_init_response (ctx->response, code,
1504 gst_rtsp_status_as_text (code), ctx->request);
1506 send_message (client, ctx, ctx->response, TRUE);
1508 if (!keep_session) {
1509 /* remove the session */
1510 gst_rtsp_session_pool_remove (priv->session_pool, session);
1513 gst_rtsp_media_unlock (media);
1514 g_object_unref (media);
1516 /* remove all transports that were present in the session media which we just
1517 * unmanaged from the priv->transports array, so we do not try to handle data
1518 * on channels that were just closed */
1519 cleanup_transports (client, session_media_transports);
1520 g_ptr_array_unref (session_media_transports);
1527 GST_ERROR ("client %p: no session", client);
1528 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1533 GST_ERROR ("client %p: no uri supplied", client);
1534 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1539 GST_ERROR ("client %p: no media for uri", client);
1540 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1546 GST_ERROR ("client %p: no aggregate path %s", client, path);
1547 send_generic_response (client,
1548 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1550 g_object_unref (sessmedia);
1555 GST_ERROR ("client %p: pre signal returned error: %s", client,
1556 gst_rtsp_status_as_text (sig_result));
1557 send_generic_response (client, sig_result, ctx);
1558 gst_rtsp_media_unlock (media);
1559 g_object_unref (media);
1560 g_object_unref (sessmedia);
1565 static GstRTSPResult
1566 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
1570 res = gst_rtsp_params_set (client, ctx);
1575 static GstRTSPResult
1576 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
1580 res = gst_rtsp_params_get (client, ctx);
1586 default_handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
1591 GstRTSPStatusCode sig_result;
1593 g_signal_emit (client,
1594 gst_rtsp_client_signals[SIGNAL_PRE_GET_PARAMETER_REQUEST], 0, ctx,
1596 if (sig_result != GST_RTSP_STS_OK) {
1600 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
1601 if (res != GST_RTSP_OK)
1604 if (size == 0 || !data || strlen ((char *) data) == 0) {
1605 if (ctx->request->type_data.request.version >= GST_RTSP_VERSION_2_0) {
1606 GST_ERROR_OBJECT (client, "Using PLAY request for keep-alive is forbidden"
1611 /* no body (or only '\0'), keep-alive request */
1612 send_generic_response (client, GST_RTSP_STS_OK, ctx);
1614 /* there is a body, handle the params */
1615 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
1616 if (res != GST_RTSP_OK)
1619 send_message (client, ctx, ctx->response, FALSE);
1622 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
1630 GST_ERROR ("client %p: pre signal returned error: %s", client,
1631 gst_rtsp_status_as_text (sig_result));
1632 send_generic_response (client, sig_result, ctx);
1637 GST_ERROR ("client %p: bad request", client);
1638 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1644 default_handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
1649 GstRTSPStatusCode sig_result;
1651 g_signal_emit (client,
1652 gst_rtsp_client_signals[SIGNAL_PRE_SET_PARAMETER_REQUEST], 0, ctx,
1654 if (sig_result != GST_RTSP_STS_OK) {
1658 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
1659 if (res != GST_RTSP_OK)
1662 if (size == 0 || !data || strlen ((char *) data) == 0) {
1663 /* no body (or only '\0'), keep-alive request */
1664 send_generic_response (client, GST_RTSP_STS_OK, ctx);
1666 /* there is a body, handle the params */
1667 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
1668 if (res != GST_RTSP_OK)
1671 send_message (client, ctx, ctx->response, FALSE);
1674 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
1682 GST_ERROR ("client %p: pre signal returned error: %s", client,
1683 gst_rtsp_status_as_text (sig_result));
1684 send_generic_response (client, sig_result, ctx);
1689 GST_ERROR ("client %p: bad request", client);
1690 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1696 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
1698 GstRTSPSession *session;
1699 GstRTSPClientClass *klass;
1700 GstRTSPSessionMedia *sessmedia;
1701 GstRTSPMedia *media;
1702 GstRTSPStatusCode code;
1703 GstRTSPState rtspstate;
1706 GstRTSPStatusCode sig_result;
1709 if (!(session = ctx->session))
1715 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1716 path = klass->make_path_from_uri (client, ctx->uri);
1718 /* get a handle to the configuration of the media in the session */
1719 sessmedia = gst_rtsp_session_dup_media (session, path, &matched);
1723 if (path[matched] != '\0')
1728 media = gst_rtsp_session_media_get_media (sessmedia);
1729 g_object_ref (media);
1730 gst_rtsp_media_lock (media);
1731 n = gst_rtsp_media_n_streams (media);
1732 for (i = 0; i < n; i++) {
1733 GstRTSPStream *stream = gst_rtsp_media_get_stream (media, i);
1735 if (gst_rtsp_stream_get_publish_clock_mode (stream) ==
1736 GST_RTSP_PUBLISH_CLOCK_MODE_CLOCK_AND_OFFSET)
1740 ctx->sessmedia = sessmedia;
1742 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_PAUSE_REQUEST], 0,
1744 if (sig_result != GST_RTSP_STS_OK) {
1748 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1749 /* the session state must be playing or recording */
1750 if (rtspstate != GST_RTSP_STATE_PLAYING &&
1751 rtspstate != GST_RTSP_STATE_RECORDING)
1754 /* then pause sending */
1755 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
1757 /* construct the response now */
1758 code = GST_RTSP_STS_OK;
1759 gst_rtsp_message_init_response (ctx->response, code,
1760 gst_rtsp_status_as_text (code), ctx->request);
1762 send_message (client, ctx, ctx->response, FALSE);
1764 /* the state is now READY */
1765 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1766 g_object_unref (sessmedia);
1768 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
1770 gst_rtsp_media_unlock (media);
1771 g_object_unref (media);
1778 GST_ERROR ("client %p: no session", client);
1779 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1784 GST_ERROR ("client %p: no uri supplied", client);
1785 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1790 GST_ERROR ("client %p: no media for uri", client);
1791 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1797 GST_ERROR ("client %p: no aggregate path %s", client, path);
1798 send_generic_response (client,
1799 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1800 g_object_unref (sessmedia);
1806 GST_ERROR ("client %p: pre signal returned error: %s", client,
1807 gst_rtsp_status_as_text (sig_result));
1808 send_generic_response (client, sig_result, ctx);
1809 gst_rtsp_media_unlock (media);
1810 g_object_unref (sessmedia);
1811 g_object_unref (media);
1816 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
1817 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1819 gst_rtsp_media_unlock (media);
1820 g_object_unref (sessmedia);
1821 g_object_unref (media);
1826 GST_ERROR ("client %p: pausing not supported", client);
1827 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1828 gst_rtsp_media_unlock (media);
1829 g_object_unref (sessmedia);
1830 g_object_unref (media);
1835 /* convert @url and @path to a URL used as a content base for the factory
1836 * located at @path */
1838 make_base_url (GstRTSPClient * client, GstRTSPUrl * url, const gchar * path)
1844 /* check for trailing '/' and append one */
1845 trail = (path[strlen (path) - 1] != '/' ? "/" : "");
1850 tmp.abspath = g_strdup_printf ("%s%s", path, trail);
1852 result = gst_rtsp_url_get_request_uri (&tmp);
1853 g_free (tmp.abspath);
1858 /* Check if the given header of type double is present and, if so,
1859 * put it's value in the supplied variable.
1861 static GstRTSPStatusCode
1862 parse_header_value_double (GstRTSPClient * client, GstRTSPContext * ctx,
1863 GstRTSPHeaderField header, gboolean * present, gdouble * value)
1869 res = gst_rtsp_message_get_header (ctx->request, header, &str, 0);
1870 if (res == GST_RTSP_OK) {
1871 *value = g_ascii_strtod (str, &end);
1873 goto parse_header_failed;
1875 GST_DEBUG ("client %p: got '%s', value %f", client,
1876 gst_rtsp_header_as_text (header), *value);
1882 return GST_RTSP_STS_OK;
1884 parse_header_failed:
1886 GST_ERROR ("client %p: failed parsing '%s' header", client,
1887 gst_rtsp_header_as_text (header));
1888 return GST_RTSP_STS_BAD_REQUEST;
1892 /* Parse scale and speed headers, if present, and set the rate to
1893 * (rate * scale * speed) */
1894 static GstRTSPStatusCode
1895 parse_scale_and_speed (GstRTSPClient * client, GstRTSPContext * ctx,
1896 gboolean * scale_present, gboolean * speed_present, gdouble * rate,
1897 GstSeekFlags * flags)
1899 gdouble scale = 1.0;
1900 gdouble speed = 1.0;
1901 GstRTSPStatusCode status;
1903 GST_DEBUG ("got rate %f", *rate);
1905 status = parse_header_value_double (client, ctx, GST_RTSP_HDR_SCALE,
1906 scale_present, &scale);
1907 if (status != GST_RTSP_STS_OK)
1910 if (*scale_present) {
1911 GST_DEBUG ("got Scale %f", scale);
1913 goto bad_scale_value;
1916 if (ABS (scale) != 1.0)
1917 *flags |= GST_SEEK_FLAG_TRICKMODE;
1920 GST_DEBUG ("rate after parsing Scale %f", *rate);
1922 status = parse_header_value_double (client, ctx, GST_RTSP_HDR_SPEED,
1923 speed_present, &speed);
1924 if (status != GST_RTSP_STS_OK)
1927 if (*speed_present) {
1928 GST_DEBUG ("got Speed %f", speed);
1930 goto bad_speed_value;
1934 GST_DEBUG ("rate after parsing Speed %f", *rate);
1940 GST_ERROR ("client %p: bad 'Scale' header value (%f)", client, scale);
1941 return GST_RTSP_STS_BAD_REQUEST;
1945 GST_ERROR ("client %p: bad 'Speed' header value (%f)", client, speed);
1946 return GST_RTSP_STS_BAD_REQUEST;
1950 static GstRTSPStatusCode
1951 setup_play_mode (GstRTSPClient * client, GstRTSPContext * ctx,
1952 GstRTSPRangeUnit * unit, gboolean * scale_present, gboolean * speed_present)
1956 GstRTSPTimeRange *range = NULL;
1958 GstSeekFlags flags = GST_SEEK_FLAG_NONE;
1959 GstRTSPClientClass *klass = GST_RTSP_CLIENT_GET_CLASS (client);
1960 GstRTSPStatusCode rtsp_status_code;
1961 GstClockTime trickmode_interval = 0;
1962 gboolean enable_rate_control = TRUE;
1964 /* parse the range header if we have one */
1965 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1966 if (res == GST_RTSP_OK) {
1967 gchar *seek_style = NULL;
1969 res = gst_rtsp_range_parse (str, &range);
1970 if (res != GST_RTSP_OK)
1971 goto parse_range_failed;
1973 *unit = range->unit;
1975 /* parse seek style header, if present */
1976 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_SEEK_STYLE,
1979 if (res == GST_RTSP_OK) {
1980 if (g_strcmp0 (seek_style, "RAP") == 0)
1981 flags = GST_SEEK_FLAG_ACCURATE;
1982 else if (g_strcmp0 (seek_style, "CoRAP") == 0)
1983 flags = GST_SEEK_FLAG_KEY_UNIT;
1984 else if (g_strcmp0 (seek_style, "First-Prior") == 0)
1985 flags = GST_SEEK_FLAG_KEY_UNIT & GST_SEEK_FLAG_SNAP_BEFORE;
1986 else if (g_strcmp0 (seek_style, "Next") == 0)
1987 flags = GST_SEEK_FLAG_KEY_UNIT & GST_SEEK_FLAG_SNAP_AFTER;
1989 GST_FIXME_OBJECT (client, "Add support for seek style %s", seek_style);
1990 } else if (range->min.type == GST_RTSP_TIME_END) {
1991 flags = GST_SEEK_FLAG_ACCURATE;
1993 flags = GST_SEEK_FLAG_KEY_UNIT;
1997 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_SEEK_STYLE,
2000 flags = GST_SEEK_FLAG_ACCURATE;
2003 /* check for scale and/or speed headers
2004 * we will set the seek rate to (speed * scale) and let the media decide
2005 * the resulting scale and speed. in the response we will use rate and applied
2006 * rate from the resulting segment as values for the speed and scale headers
2008 rtsp_status_code = parse_scale_and_speed (client, ctx, scale_present,
2009 speed_present, &rate, &flags);
2010 if (rtsp_status_code != GST_RTSP_STS_OK)
2011 goto scale_speed_failed;
2013 /* give the application a chance to tweak range, flags, or rate */
2014 if (klass->adjust_play_mode != NULL) {
2016 klass->adjust_play_mode (client, ctx, &range, &flags, &rate,
2017 &trickmode_interval, &enable_rate_control);
2018 if (rtsp_status_code != GST_RTSP_STS_OK)
2019 goto adjust_play_mode_failed;
2022 gst_rtsp_media_set_rate_control (ctx->media, enable_rate_control);
2024 /* now do the seek with the seek options */
2025 gst_rtsp_media_seek_trickmode (ctx->media, range, flags, rate,
2026 trickmode_interval);
2028 gst_rtsp_range_free (range);
2030 if (gst_rtsp_media_get_status (ctx->media) == GST_RTSP_MEDIA_STATUS_ERROR)
2033 return GST_RTSP_STS_OK;
2037 GST_ERROR ("client %p: failed parsing range header", client);
2038 return GST_RTSP_STS_BAD_REQUEST;
2043 gst_rtsp_range_free (range);
2044 GST_ERROR ("client %p: failed parsing Scale or Speed headers", client);
2045 return rtsp_status_code;
2047 adjust_play_mode_failed:
2049 GST_ERROR ("client %p: sub class returned bad code (%d)", client,
2052 gst_rtsp_range_free (range);
2053 return rtsp_status_code;
2057 GST_ERROR ("client %p: seek failed", client);
2058 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2063 default_handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
2065 GstRTSPSession *session;
2066 GstRTSPClientClass *klass;
2067 GstRTSPSessionMedia *sessmedia;
2068 GstRTSPMedia *media;
2069 GstRTSPStatusCode code;
2072 GstRTSPState rtspstate;
2073 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
2074 gchar *path, *rtpinfo = NULL;
2076 GstRTSPStatusCode sig_result;
2077 GPtrArray *transports;
2078 gboolean scale_present;
2079 gboolean speed_present;
2081 gdouble applied_rate;
2083 if (!(session = ctx->session))
2086 if (!(uri = ctx->uri))
2089 klass = GST_RTSP_CLIENT_GET_CLASS (client);
2090 path = klass->make_path_from_uri (client, uri);
2092 /* get a handle to the configuration of the media in the session */
2093 sessmedia = gst_rtsp_session_dup_media (session, path, &matched);
2097 if (path[matched] != '\0')
2102 ctx->sessmedia = sessmedia;
2103 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
2105 g_object_ref (media);
2106 gst_rtsp_media_lock (media);
2108 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_PLAY_REQUEST], 0,
2110 if (sig_result != GST_RTSP_STS_OK) {
2114 if (!(gst_rtsp_media_get_transport_mode (media) &
2115 GST_RTSP_TRANSPORT_MODE_PLAY))
2116 goto unsupported_mode;
2118 /* the session state must be playing or ready */
2119 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
2120 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
2123 /* update the pipeline */
2124 transports = gst_rtsp_session_media_get_transports (sessmedia);
2125 if (!gst_rtsp_media_complete_pipeline (media, transports)) {
2126 g_ptr_array_unref (transports);
2127 goto pipeline_error;
2129 g_ptr_array_unref (transports);
2131 /* in play we first unsuspend, media could be suspended from SDP or PAUSED */
2132 if (!gst_rtsp_media_unsuspend (media))
2133 goto unsuspend_failed;
2135 code = setup_play_mode (client, ctx, &unit, &scale_present, &speed_present);
2136 if (code != GST_RTSP_STS_OK)
2139 /* grab RTPInfo from the media now */
2140 if (gst_rtsp_media_has_completed_sender (media) &&
2141 !(rtpinfo = gst_rtsp_session_media_get_rtpinfo (sessmedia)))
2142 goto rtp_info_error;
2144 /* construct the response now */
2145 code = GST_RTSP_STS_OK;
2146 gst_rtsp_message_init_response (ctx->response, code,
2147 gst_rtsp_status_as_text (code), ctx->request);
2149 /* add the RTP-Info header */
2151 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO,
2155 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
2157 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
2159 if (gst_rtsp_media_has_completed_sender (media)) {
2160 /* the scale and speed headers must always be added if they were present in
2161 * the request. however, even if they were not, we still add them if
2162 * applied_rate or rate deviate from the "normal", i.e. 1.0 */
2163 if (!gst_rtsp_media_get_rates (media, &rate, &applied_rate))
2164 goto get_rates_error;
2165 g_assert (rate != 0 && applied_rate != 0);
2167 if (scale_present || applied_rate != 1.0)
2168 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_SCALE,
2169 g_strdup_printf ("%1.3f", applied_rate));
2171 if (speed_present || rate != 1.0)
2172 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_SPEED,
2173 g_strdup_printf ("%1.3f", rate));
2176 if (klass->adjust_play_response) {
2177 code = klass->adjust_play_response (client, ctx);
2178 if (code != GST_RTSP_STS_OK)
2179 goto adjust_play_response_failed;
2182 send_message (client, ctx, ctx->response, FALSE);
2184 /* start playing after sending the response */
2185 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
2187 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
2188 g_object_unref (sessmedia);
2190 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
2192 gst_rtsp_media_unlock (media);
2193 g_object_unref (media);
2200 GST_ERROR ("client %p: no session", client);
2201 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2206 GST_ERROR ("client %p: no uri supplied", client);
2207 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2212 GST_ERROR ("client %p: media not found", client);
2213 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2218 GST_ERROR ("client %p: no aggregate path %s", client, path);
2219 send_generic_response (client,
2220 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
2221 g_object_unref (sessmedia);
2227 GST_ERROR ("client %p: pre signal returned error: %s", client,
2228 gst_rtsp_status_as_text (sig_result));
2229 send_generic_response (client, sig_result, ctx);
2230 gst_rtsp_media_unlock (media);
2231 g_object_unref (media);
2232 g_object_unref (sessmedia);
2237 GST_ERROR ("client %p: not PLAYING or READY", client);
2238 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
2240 gst_rtsp_media_unlock (media);
2241 g_object_unref (media);
2242 g_object_unref (sessmedia);
2247 GST_ERROR ("client %p: failed to configure the pipeline", client);
2248 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
2250 gst_rtsp_media_unlock (media);
2251 g_object_unref (media);
2252 g_object_unref (sessmedia);
2257 GST_ERROR ("client %p: unsuspend failed", client);
2258 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2259 gst_rtsp_media_unlock (media);
2260 g_object_unref (media);
2261 g_object_unref (sessmedia);
2266 GST_ERROR ("client %p: seek failed", client);
2267 send_generic_response (client, code, ctx);
2268 gst_rtsp_media_unlock (media);
2269 g_object_unref (media);
2270 g_object_unref (sessmedia);
2275 GST_ERROR ("client %p: media does not support PLAY", client);
2276 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_ALLOWED, ctx);
2277 gst_rtsp_media_unlock (media);
2278 g_object_unref (media);
2279 g_object_unref (sessmedia);
2284 GST_ERROR ("client %p: failed obtaining rate and applied_rate", client);
2285 send_generic_response (client, GST_RTSP_STS_INTERNAL_SERVER_ERROR, ctx);
2286 gst_rtsp_media_unlock (media);
2287 g_object_unref (media);
2288 g_object_unref (sessmedia);
2291 adjust_play_response_failed:
2293 GST_ERROR ("client %p: failed to adjust play response", client);
2294 send_generic_response (client, code, ctx);
2295 gst_rtsp_media_unlock (media);
2296 g_object_unref (media);
2297 g_object_unref (sessmedia);
2302 GST_ERROR ("client %p: failed to add RTP-Info", client);
2303 send_generic_response (client, GST_RTSP_STS_INTERNAL_SERVER_ERROR, ctx);
2304 gst_rtsp_media_unlock (media);
2305 g_object_unref (media);
2306 g_object_unref (sessmedia);
2312 do_keepalive (GstRTSPSession * session)
2314 GST_INFO ("keep session %p alive", session);
2315 gst_rtsp_session_touch (session);
2318 /* parse @transport and return a valid transport in @tr. only transports
2319 * supported by @stream are returned. Returns FALSE if no valid transport
2322 parse_transport (const char *transport, GstRTSPStream * stream,
2323 GstRTSPTransport * tr)
2330 gst_rtsp_transport_init (tr);
2332 GST_DEBUG ("parsing transports %s", transport);
2334 transports = g_strsplit (transport, ",", 0);
2336 /* loop through the transports, try to parse */
2337 for (i = 0; transports[i]; i++) {
2338 g_strstrip (transports[i]);
2339 res = gst_rtsp_transport_parse (transports[i], tr);
2340 if (res != GST_RTSP_OK) {
2341 /* no valid transport, search some more */
2342 GST_WARNING ("could not parse transport %s", transports[i]);
2346 /* we have a transport, see if it's supported */
2347 if (!gst_rtsp_stream_is_transport_supported (stream, tr)) {
2348 GST_WARNING ("unsupported transport %s", transports[i]);
2352 /* we have a valid transport */
2353 GST_INFO ("found valid transport %s", transports[i]);
2358 gst_rtsp_transport_init (tr);
2360 g_strfreev (transports);
2366 default_configure_client_media (GstRTSPClient * client, GstRTSPMedia * media,
2367 GstRTSPStream * stream, GstRTSPContext * ctx)
2369 GstRTSPMessage *request = ctx->request;
2370 gchar *blocksize_str;
2372 if (!gst_rtsp_stream_is_sender (stream))
2375 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
2376 &blocksize_str, 0) == GST_RTSP_OK) {
2380 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
2381 if (end == blocksize_str)
2384 /* we don't want to change the mtu when this media
2385 * can be shared because it impacts other clients */
2386 if (gst_rtsp_media_is_shared (media))
2389 if (blocksize > G_MAXUINT)
2390 blocksize = G_MAXUINT;
2392 gst_rtsp_stream_set_mtu (stream, blocksize);
2400 GST_ERROR_OBJECT (client, "failed to parse blocksize");
2401 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2407 default_configure_client_transport (GstRTSPClient * client,
2408 GstRTSPContext * ctx, GstRTSPTransport * ct)
2410 GstRTSPClientPrivate *priv = client->priv;
2412 /* we have a valid transport now, set the destination of the client. */
2413 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST ||
2414 ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP) {
2415 /* allocate UDP ports */
2416 GSocketFamily family;
2417 gboolean use_client_settings = FALSE;
2419 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
2421 if ((ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) &&
2422 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS) &&
2423 (ct->destination != NULL)) {
2425 if (!gst_rtsp_stream_verify_mcast_ttl (ctx->stream, ct->ttl))
2428 use_client_settings = TRUE;
2431 /* We need to allocate the sockets for both families before starting
2432 * multiudpsink, otherwise multiudpsink won't accept new clients with
2433 * a different family.
2435 /* FIXME: could be more adequately solved by making it possible
2436 * to set a socket on multiudpsink after it has already been started */
2437 if (!gst_rtsp_stream_allocate_udp_sockets (ctx->stream,
2438 G_SOCKET_FAMILY_IPV4, ct, use_client_settings)
2439 && family == G_SOCKET_FAMILY_IPV4)
2440 goto error_allocating_ports;
2442 if (!gst_rtsp_stream_allocate_udp_sockets (ctx->stream,
2443 G_SOCKET_FAMILY_IPV6, ct, use_client_settings)
2444 && family == G_SOCKET_FAMILY_IPV6)
2445 goto error_allocating_ports;
2447 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2448 if (use_client_settings) {
2449 /* FIXME: the address has been successfully allocated, however, in
2450 * the use_client_settings case we need to verify that the allocated
2451 * address is the one requested by the client and if this address is
2452 * an allowed destination. Verifying this via the address pool in not
2453 * the proper way as the address pool should only be used for choosing
2454 * the server-selected address/port pairs. */
2455 GSocket *rtp_socket;
2459 gst_rtsp_stream_get_rtp_multicast_socket (ctx->stream, family);
2460 if (rtp_socket == NULL)
2462 ttl = g_socket_get_multicast_ttl (rtp_socket);
2463 g_object_unref (rtp_socket);
2464 if (ct->ttl < ttl) {
2465 /* use the maximum ttl that is requested by multicast clients */
2466 GST_DEBUG ("requested ttl %u, but keeping ttl %u", ct->ttl, ttl);
2471 GstRTSPAddress *addr = NULL;
2473 g_free (ct->destination);
2474 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
2477 ct->destination = g_strdup (addr->address);
2478 ct->port.min = addr->port;
2479 ct->port.max = addr->port + addr->n_ports - 1;
2480 ct->ttl = addr->ttl;
2481 gst_rtsp_address_free (addr);
2484 if (!gst_rtsp_stream_add_multicast_client_address (ctx->stream,
2485 ct->destination, ct->port.min, ct->port.max, family))
2486 goto error_mcast_transport;
2491 url = gst_rtsp_connection_get_url (priv->connection);
2492 g_free (ct->destination);
2493 ct->destination = g_strdup (url->host);
2498 url = gst_rtsp_connection_get_url (priv->connection);
2499 g_free (ct->destination);
2500 ct->destination = g_strdup (url->host);
2502 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
2504 GSocketAddress *addr;
2506 sock = gst_rtsp_connection_get_read_socket (priv->connection);
2507 if ((addr = g_socket_get_remote_address (sock, NULL))) {
2508 /* our read port is the sender port of client */
2509 ct->client_port.min =
2510 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
2511 g_object_unref (addr);
2513 if ((addr = g_socket_get_local_address (sock, NULL))) {
2514 ct->server_port.max =
2515 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
2516 g_object_unref (addr);
2518 sock = gst_rtsp_connection_get_write_socket (priv->connection);
2519 if ((addr = g_socket_get_remote_address (sock, NULL))) {
2520 /* our write port is the receiver port of client */
2521 ct->client_port.max =
2522 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
2523 g_object_unref (addr);
2525 if ((addr = g_socket_get_local_address (sock, NULL))) {
2526 ct->server_port.min =
2527 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
2528 g_object_unref (addr);
2530 /* check if the client selected channels for TCP */
2531 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
2532 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
2535 /* alloc new channels if they are already taken */
2536 while (g_hash_table_contains (priv->transports,
2537 GINT_TO_POINTER (ct->interleaved.min))
2538 || g_hash_table_contains (priv->transports,
2539 GINT_TO_POINTER (ct->interleaved.max))) {
2540 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
2542 if (ct->interleaved.max > 255)
2543 goto error_allocating_channels;
2552 GST_ERROR_OBJECT (client,
2553 "Failed to allocate UDP ports: invalid ttl value");
2556 error_allocating_ports:
2558 GST_ERROR_OBJECT (client, "Failed to allocate UDP ports");
2563 GST_ERROR_OBJECT (client, "Failed to acquire address for stream");
2568 GST_ERROR_OBJECT (client, "Failed to get UDP socket");
2571 error_mcast_transport:
2573 GST_ERROR_OBJECT (client, "Failed to add multicast client transport");
2576 error_allocating_channels:
2578 GST_ERROR_OBJECT (client, "Failed to allocate interleaved channels");
2583 static GstRTSPTransport *
2584 make_server_transport (GstRTSPClient * client, GstRTSPMedia * media,
2585 GstRTSPContext * ctx, GstRTSPTransport * ct)
2587 GstRTSPTransport *st;
2589 GSocketFamily family;
2591 /* prepare the server transport */
2592 gst_rtsp_transport_new (&st);
2594 st->trans = ct->trans;
2595 st->profile = ct->profile;
2596 st->lower_transport = ct->lower_transport;
2597 st->mode_play = ct->mode_play;
2598 st->mode_record = ct->mode_record;
2600 addr = g_inet_address_new_from_string (ct->destination);
2603 GST_ERROR ("failed to get inet addr from client destination");
2604 family = G_SOCKET_FAMILY_IPV4;
2606 family = g_inet_address_get_family (addr);
2607 g_object_unref (addr);
2611 switch (st->lower_transport) {
2612 case GST_RTSP_LOWER_TRANS_UDP:
2613 st->client_port = ct->client_port;
2614 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
2616 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
2617 st->port = ct->port;
2618 st->destination = g_strdup (ct->destination);
2621 case GST_RTSP_LOWER_TRANS_TCP:
2622 st->interleaved = ct->interleaved;
2623 st->client_port = ct->client_port;
2624 st->server_port = ct->server_port;
2629 if ((gst_rtsp_media_get_transport_mode (media) &
2630 GST_RTSP_TRANSPORT_MODE_PLAY))
2631 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
2637 rtsp_ctrl_timeout_remove_unlocked (GstRTSPClientPrivate * priv)
2639 if (priv->rtsp_ctrl_timeout != NULL) {
2640 GST_DEBUG ("rtsp control session removed timeout %p.",
2641 priv->rtsp_ctrl_timeout);
2642 g_source_destroy (priv->rtsp_ctrl_timeout);
2643 g_source_unref (priv->rtsp_ctrl_timeout);
2644 priv->rtsp_ctrl_timeout = NULL;
2645 priv->rtsp_ctrl_timeout_cnt = 0;
2650 rtsp_ctrl_timeout_remove (GstRTSPClient * client)
2652 g_mutex_lock (&client->priv->lock);
2653 rtsp_ctrl_timeout_remove_unlocked (client->priv);
2654 g_mutex_unlock (&client->priv->lock);
2658 rtsp_ctrl_timeout_destroy_notify (gpointer user_data)
2660 GWeakRef *client_weak_ref = (GWeakRef *) user_data;
2662 g_weak_ref_clear (client_weak_ref);
2663 g_free (client_weak_ref);
2667 rtsp_ctrl_timeout_cb (gpointer user_data)
2669 gboolean res = G_SOURCE_CONTINUE;
2670 GstRTSPClientPrivate *priv;
2671 GWeakRef *client_weak_ref = (GWeakRef *) user_data;
2672 GstRTSPClient *client = (GstRTSPClient *) g_weak_ref_get (client_weak_ref);
2674 if (client == NULL) {
2675 return G_SOURCE_REMOVE;
2678 priv = client->priv;
2679 g_mutex_lock (&priv->lock);
2680 priv->rtsp_ctrl_timeout_cnt += RTSP_CTRL_CB_INTERVAL;
2682 if ((priv->rtsp_ctrl_timeout_cnt > RTSP_CTRL_TIMEOUT_VALUE)
2683 || (priv->had_session
2684 && priv->rtsp_ctrl_timeout_cnt > priv->post_session_timeout)) {
2685 GST_DEBUG ("rtsp control session timeout %p expired, closing client.",
2686 priv->rtsp_ctrl_timeout);
2687 rtsp_ctrl_timeout_remove_unlocked (client->priv);
2689 res = G_SOURCE_REMOVE;
2692 g_mutex_unlock (&priv->lock);
2694 if (res == G_SOURCE_REMOVE) {
2695 gst_rtsp_client_close (client);
2698 g_object_unref (client);
2704 stream_make_keymgmt (GstRTSPClient * client, const gchar * location,
2705 GstRTSPStream * stream)
2707 gchar *base64, *result = NULL;
2708 GstMIKEYMessage *mikey_msg;
2709 GstCaps *srtcpparams;
2710 GstElement *rtcp_encoder;
2711 gint srtcp_cipher, srtp_cipher;
2712 gint srtcp_auth, srtp_auth;
2714 GType ciphertype, authtype;
2715 GEnumClass *cipher_enum, *auth_enum;
2716 GEnumValue *srtcp_cipher_value, *srtp_cipher_value, *srtcp_auth_value,
2719 rtcp_encoder = gst_rtsp_stream_get_srtp_encoder (stream);
2724 ciphertype = g_type_from_name ("GstSrtpCipherType");
2725 authtype = g_type_from_name ("GstSrtpAuthType");
2727 cipher_enum = g_type_class_ref (ciphertype);
2728 auth_enum = g_type_class_ref (authtype);
2730 /* We need to bring the encoder to READY so that it generates its key */
2731 gst_element_set_state (rtcp_encoder, GST_STATE_READY);
2733 g_object_get (rtcp_encoder, "rtcp-cipher", &srtcp_cipher, "rtcp-auth",
2734 &srtcp_auth, "rtp-cipher", &srtp_cipher, "rtp-auth", &srtp_auth, "key",
2736 g_object_unref (rtcp_encoder);
2738 srtcp_cipher_value = g_enum_get_value (cipher_enum, srtcp_cipher);
2739 srtp_cipher_value = g_enum_get_value (cipher_enum, srtp_cipher);
2740 srtcp_auth_value = g_enum_get_value (auth_enum, srtcp_auth);
2741 srtp_auth_value = g_enum_get_value (auth_enum, srtp_auth);
2743 g_type_class_unref (cipher_enum);
2744 g_type_class_unref (auth_enum);
2746 srtcpparams = gst_caps_new_simple ("application/x-srtcp",
2747 "srtcp-cipher", G_TYPE_STRING, srtcp_cipher_value->value_nick,
2748 "srtcp-auth", G_TYPE_STRING, srtcp_auth_value->value_nick,
2749 "srtp-cipher", G_TYPE_STRING, srtp_cipher_value->value_nick,
2750 "srtp-auth", G_TYPE_STRING, srtp_auth_value->value_nick,
2751 "srtp-key", GST_TYPE_BUFFER, key, NULL);
2753 mikey_msg = gst_mikey_message_new_from_caps (srtcpparams);
2757 gst_rtsp_stream_get_ssrc (stream, &send_ssrc);
2758 gst_mikey_message_add_cs_srtp (mikey_msg, 0, send_ssrc, 0);
2760 base64 = gst_mikey_message_base64_encode (mikey_msg);
2761 gst_mikey_message_unref (mikey_msg);
2764 result = gst_sdp_make_keymgmt (location, base64);
2774 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
2776 GstRTSPClientPrivate *priv = client->priv;
2779 gchar *transport, *keymgmt;
2780 GstRTSPTransport *ct, *st;
2781 GstRTSPStatusCode code;
2782 GstRTSPSession *session;
2783 GstRTSPStreamTransport *trans;
2785 GstRTSPSessionMedia *sessmedia;
2786 GstRTSPMedia *media;
2787 GstRTSPStream *stream;
2788 GstRTSPState rtspstate;
2789 GstRTSPClientClass *klass;
2790 gchar *path, *control = NULL;
2792 gboolean new_session = FALSE;
2793 GstRTSPStatusCode sig_result;
2794 gchar *pipelined_request_id = NULL, *accept_range = NULL;
2800 klass = GST_RTSP_CLIENT_GET_CLASS (client);
2801 path = klass->make_path_from_uri (client, uri);
2803 /* parse the transport */
2805 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
2807 if (res != GST_RTSP_OK)
2810 /* Handle Pipelined-requests if using >= 2.0 */
2811 if (ctx->request->type_data.request.version >= GST_RTSP_VERSION_2_0)
2812 gst_rtsp_message_get_header (ctx->request,
2813 GST_RTSP_HDR_PIPELINED_REQUESTS, &pipelined_request_id, 0);
2815 /* we create the session after parsing stuff so that we don't make
2816 * a session for malformed requests */
2817 if (priv->session_pool == NULL)
2820 session = ctx->session;
2823 g_object_ref (session);
2824 /* get a handle to the configuration of the media in the session, this can
2825 * return NULL if this is a new url to manage in this session. */
2826 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
2828 /* we need a new media configuration in this session */
2832 /* we have no session media, find one and manage it */
2833 if (sessmedia == NULL) {
2834 /* get a handle to the configuration of the media in the session */
2835 media = find_media (client, ctx, path, &matched);
2836 /* need to suspend the media, if the protocol has changed */
2837 if (media != NULL) {
2838 gst_rtsp_media_lock (media);
2839 gst_rtsp_media_suspend (media);
2842 if ((media = gst_rtsp_session_media_get_media (sessmedia))) {
2843 g_object_ref (media);
2844 gst_rtsp_media_lock (media);
2846 goto media_not_found;
2849 /* no media, not found then */
2851 goto media_not_found_no_reply;
2853 if (path[matched] == '\0') {
2854 if (gst_rtsp_media_n_streams (media) == 1) {
2855 stream = gst_rtsp_media_get_stream (media, 0);
2857 goto control_not_found;
2860 /* path is what matched. */
2861 gchar *newpath = g_strndup (path, matched);
2862 /* control is remainder */
2863 if (matched == 1 && path[0] == '/')
2864 control = g_strdup (&path[1]);
2866 control = g_strdup (&path[matched + 1]);
2871 /* find the stream now using the control part */
2872 stream = gst_rtsp_media_find_stream (media, control);
2876 goto stream_not_found;
2878 /* now we have a uri identifying a valid media and stream */
2879 ctx->stream = stream;
2882 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_SETUP_REQUEST], 0,
2884 if (sig_result != GST_RTSP_STS_OK) {
2888 if (session == NULL) {
2889 /* create a session if this fails we probably reached our session limit or
2891 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
2892 goto service_unavailable;
2894 /* Pipelined requests should be cleared between sessions */
2895 g_hash_table_remove_all (priv->pipelined_requests);
2897 /* make sure this client is closed when the session is closed */
2898 client_watch_session (client, session);
2901 /* signal new session */
2902 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
2905 ctx->session = session;
2908 if (pipelined_request_id) {
2909 g_hash_table_insert (client->priv->pipelined_requests,
2910 g_strdup (pipelined_request_id),
2911 g_strdup (gst_rtsp_session_get_sessionid (session)));
2913 /* Remember that we had at least one session in the past */
2914 priv->had_session = TRUE;
2915 rtsp_ctrl_timeout_remove (client);
2917 if (!klass->configure_client_media (client, media, stream, ctx))
2918 goto configure_media_failed_no_reply;
2920 gst_rtsp_transport_new (&ct);
2922 /* parse and find a usable supported transport */
2923 if (!parse_transport (transport, stream, ct))
2924 goto unsupported_transports;
2927 && !(gst_rtsp_media_get_transport_mode (media) &
2928 GST_RTSP_TRANSPORT_MODE_PLAY)) || (ct->mode_record
2929 && !(gst_rtsp_media_get_transport_mode (media) &
2930 GST_RTSP_TRANSPORT_MODE_RECORD)))
2931 goto unsupported_mode;
2933 /* parse the keymgmt */
2934 if (gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_KEYMGMT,
2935 &keymgmt, 0) == GST_RTSP_OK) {
2936 if (!gst_rtsp_stream_handle_keymgmt (ctx->stream, keymgmt))
2940 if (gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT_RANGES,
2941 &accept_range, 0) == GST_RTSP_OK) {
2942 GEnumValue *runit = NULL;
2944 gchar **valid_ranges;
2945 GEnumClass *runit_class = g_type_class_ref (GST_TYPE_RTSP_RANGE_UNIT);
2947 gst_rtsp_message_dump (ctx->request);
2948 valid_ranges = g_strsplit (accept_range, ",", -1);
2950 for (i = 0; valid_ranges[i]; i++) {
2951 gchar *range = valid_ranges[i];
2953 while (*range == ' ')
2956 runit = g_enum_get_value_by_nick (runit_class, range);
2960 g_strfreev (valid_ranges);
2961 g_type_class_unref (runit_class);
2964 goto unsupported_range_unit;
2967 if (sessmedia == NULL) {
2968 /* manage the media in our session now, if not done already */
2970 gst_rtsp_session_manage_media (session, path, g_object_ref (media));
2971 /* if we stil have no media, error */
2972 if (sessmedia == NULL)
2973 goto sessmedia_unavailable;
2975 /* don't cache media anymore */
2976 clean_cached_media (client, FALSE);
2979 ctx->sessmedia = sessmedia;
2981 /* update the client transport */
2982 if (!klass->configure_client_transport (client, ctx, ct))
2983 goto unsupported_client_transport;
2985 /* set in the session media transport */
2986 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
2990 /* configure the url used to set this transport, this we will use when
2991 * generating the response for the PLAY request */
2992 gst_rtsp_stream_transport_set_url (trans, uri);
2993 /* configure keepalive for this transport */
2994 gst_rtsp_stream_transport_set_keepalive (trans,
2995 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
2997 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2998 /* our callbacks to send data on this TCP connection */
2999 gst_rtsp_stream_transport_set_callbacks (trans,
3000 (GstRTSPSendFunc) do_send_data,
3001 (GstRTSPSendFunc) do_send_data, client, NULL);
3002 gst_rtsp_stream_transport_set_list_callbacks (trans,
3003 (GstRTSPSendListFunc) do_send_data_list,
3004 (GstRTSPSendListFunc) do_send_data_list, client, NULL);
3006 gst_rtsp_stream_transport_set_back_pressure_callback (trans,
3007 (GstRTSPBackPressureFunc) do_check_back_pressure, client, NULL);
3009 g_hash_table_insert (priv->transports,
3010 GINT_TO_POINTER (ct->interleaved.min), trans);
3011 g_object_ref (trans);
3012 g_hash_table_insert (priv->transports,
3013 GINT_TO_POINTER (ct->interleaved.max), trans);
3014 g_object_ref (trans);
3015 add_data_seq (client, ct->interleaved.min);
3016 add_data_seq (client, ct->interleaved.max);
3019 /* create and serialize the server transport */
3020 st = make_server_transport (client, media, ctx, ct);
3021 trans_str = gst_rtsp_transport_as_text (st);
3023 /* FIXME-WFD : Temporarily force to set profile string */
3024 trans_str = g_strjoinv ("RTP/AVP/UDP", g_strsplit (trans_str, "RTP/AVP", -1));
3026 gst_rtsp_transport_free (st);
3028 /* construct the response now */
3029 code = GST_RTSP_STS_OK;
3030 gst_rtsp_message_init_response (ctx->response, code,
3031 gst_rtsp_status_as_text (code), ctx->request);
3033 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
3037 if (pipelined_request_id)
3038 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PIPELINED_REQUESTS,
3039 pipelined_request_id);
3041 if (ctx->request->type_data.request.version >= GST_RTSP_VERSION_2_0) {
3042 GstClockTimeDiff seekable = gst_rtsp_media_seekable (media);
3043 GString *media_properties = g_string_new (NULL);
3046 g_string_append (media_properties,
3047 "No-Seeking,Time-Progressing,Time-Duration=0.0");
3048 else if (seekable == 0)
3049 g_string_append (media_properties, "Beginning-Only");
3050 else if (seekable == G_MAXINT64)
3051 g_string_append (media_properties, "Random-Access");
3053 g_string_append_printf (media_properties,
3054 "Random-Access=%f, Unlimited, Immutable",
3055 (gdouble) seekable / GST_SECOND);
3057 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_MEDIA_PROPERTIES,
3058 media_properties->str);
3059 g_string_free (media_properties, TRUE);
3060 /* TODO Check how Accept-Ranges should be filled */
3061 gst_rtsp_message_add_header (ctx->request, GST_RTSP_HDR_ACCEPT_RANGES,
3062 "npt, clock, smpte, clock");
3065 send_message (client, ctx, ctx->response, FALSE);
3067 /* update the state */
3068 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
3069 switch (rtspstate) {
3070 case GST_RTSP_STATE_PLAYING:
3071 case GST_RTSP_STATE_RECORDING:
3072 case GST_RTSP_STATE_READY:
3073 /* no state change */
3076 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
3080 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
3082 gst_rtsp_media_unlock (media);
3083 g_object_unref (media);
3084 g_object_unref (session);
3093 GST_ERROR ("client %p: no uri", client);
3094 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
3099 GST_ERROR ("client %p: no transport", client);
3100 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
3105 GST_ERROR ("client %p: no session pool configured", client);
3106 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
3109 media_not_found_no_reply:
3111 GST_ERROR ("client %p: media '%s' not found", client, path);
3112 /* error reply is already sent */
3113 goto cleanup_session;
3117 GST_ERROR ("client %p: media '%s' not found", client, path);
3118 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
3119 goto cleanup_session;
3123 GST_ERROR ("client %p: no control in path '%s'", client, path);
3124 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
3125 gst_rtsp_media_unlock (media);
3126 g_object_unref (media);
3127 goto cleanup_session;
3131 GST_ERROR ("client %p: stream '%s' not found", client,
3132 GST_STR_NULL (control));
3133 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
3134 gst_rtsp_media_unlock (media);
3135 g_object_unref (media);
3136 goto cleanup_session;
3140 GST_ERROR ("client %p: pre signal returned error: %s", client,
3141 gst_rtsp_status_as_text (sig_result));
3142 send_generic_response (client, sig_result, ctx);
3143 gst_rtsp_media_unlock (media);
3144 g_object_unref (media);
3147 service_unavailable:
3149 GST_ERROR ("client %p: can't create session", client);
3150 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
3151 gst_rtsp_media_unlock (media);
3152 g_object_unref (media);
3153 goto cleanup_session;
3155 sessmedia_unavailable:
3157 GST_ERROR ("client %p: can't create session media", client);
3158 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
3159 goto cleanup_transport;
3161 configure_media_failed_no_reply:
3163 GST_ERROR ("client %p: configure_media failed", client);
3164 gst_rtsp_media_unlock (media);
3165 g_object_unref (media);
3166 /* error reply is already sent */
3167 goto cleanup_session;
3169 unsupported_transports:
3171 GST_ERROR ("client %p: unsupported transports", client);
3172 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
3173 goto cleanup_transport;
3175 unsupported_client_transport:
3177 GST_ERROR ("client %p: unsupported client transport", client);
3178 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
3179 goto cleanup_transport;
3183 GST_ERROR ("client %p: unsupported mode (media play: %d, media record: %d, "
3184 "mode play: %d, mode record: %d)", client,
3185 ! !(gst_rtsp_media_get_transport_mode (media) &
3186 GST_RTSP_TRANSPORT_MODE_PLAY),
3187 ! !(gst_rtsp_media_get_transport_mode (media) &
3188 GST_RTSP_TRANSPORT_MODE_RECORD), ct->mode_play, ct->mode_record);
3189 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
3190 goto cleanup_transport;
3192 unsupported_range_unit:
3194 GST_ERROR ("Client %p: does not support any range format we support",
3196 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
3197 goto cleanup_transport;
3201 GST_ERROR ("client %p: keymgmt error", client);
3202 send_generic_response (client, GST_RTSP_STS_KEY_MANAGEMENT_FAILURE, ctx);
3203 goto cleanup_transport;
3207 gst_rtsp_transport_free (ct);
3209 gst_rtsp_media_unlock (media);
3210 g_object_unref (media);
3214 gst_rtsp_session_pool_remove (priv->session_pool, session);
3216 g_object_unref (session);
3224 static GstSDPMessage *
3225 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
3227 GstRTSPClientPrivate *priv = client->priv;
3231 guint64 session_id_tmp;
3232 gchar session_id[21];
3234 gst_sdp_message_new (&sdp);
3236 /* some standard things first */
3237 gst_sdp_message_set_version (sdp, "0");
3244 session_id_tmp = (((guint64) g_random_int ()) << 32) | g_random_int ();
3245 g_snprintf (session_id, sizeof (session_id), "%" G_GUINT64_FORMAT,
3248 gst_sdp_message_set_origin (sdp, "-", session_id, "1", "IN", proto,
3251 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
3252 gst_sdp_message_set_information (sdp, "rtsp-server");
3253 gst_sdp_message_add_time (sdp, "0", "0", NULL);
3254 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
3255 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
3256 gst_sdp_message_add_attribute (sdp, "control", "*");
3258 info.is_ipv6 = priv->is_ipv6;
3259 info.server_ip = priv->server_ip;
3261 /* create an SDP for the media object */
3262 if (!gst_rtsp_media_setup_sdp (media, sdp, &info))
3270 GST_ERROR ("client %p: could not create SDP", client);
3271 gst_sdp_message_free (sdp);
3276 /* for the describe we must generate an SDP */
3278 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
3280 GstRTSPClientPrivate *priv = client->priv;
3285 GstRTSPMedia *media;
3286 GstRTSPClientClass *klass;
3287 GstRTSPStatusCode sig_result;
3289 klass = GST_RTSP_CLIENT_GET_CLASS (client);
3294 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_DESCRIBE_REQUEST],
3295 0, ctx, &sig_result);
3296 if (sig_result != GST_RTSP_STS_OK) {
3300 /* check what kind of format is accepted, we don't really do anything with it
3301 * and always return SDP for now. */
3306 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
3308 if (res == GST_RTSP_ENOTIMPL)
3311 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
3315 if (!priv->mount_points)
3316 goto no_mount_points;
3318 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
3321 /* find the media object for the uri */
3322 if (!(media = find_media (client, ctx, path, NULL)))
3325 gst_rtsp_media_lock (media);
3327 if (!(gst_rtsp_media_get_transport_mode (media) &
3328 GST_RTSP_TRANSPORT_MODE_PLAY))
3329 goto unsupported_mode;
3331 /* create an SDP for the media object on this client */
3332 if (!(sdp = klass->create_sdp (client, media)))
3335 /* we suspend after the describe */
3336 gst_rtsp_media_suspend (media);
3338 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
3339 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
3341 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
3344 /* content base for some clients that might screw up creating the setup uri */
3345 str = make_base_url (client, ctx->uri, path);
3348 GST_INFO ("adding content-base: %s", str);
3349 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE, str);
3351 /* add SDP to the response body */
3352 str = gst_sdp_message_as_text (sdp);
3353 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
3354 gst_sdp_message_free (sdp);
3356 send_message (client, ctx, ctx->response, FALSE);
3358 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
3361 gst_rtsp_media_unlock (media);
3362 g_object_unref (media);
3369 GST_ERROR ("client %p: pre signal returned error: %s", client,
3370 gst_rtsp_status_as_text (sig_result));
3371 send_generic_response (client, sig_result, ctx);
3376 GST_ERROR ("client %p: no uri", client);
3377 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
3382 GST_ERROR ("client %p: no mount points configured", client);
3383 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
3388 GST_ERROR ("client %p: can't find path for url", client);
3389 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
3394 GST_ERROR ("client %p: no media", client);
3396 /* error reply is already sent */
3401 GST_ERROR ("client %p: media does not support DESCRIBE", client);
3402 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_ALLOWED, ctx);
3404 gst_rtsp_media_unlock (media);
3405 g_object_unref (media);
3410 GST_ERROR ("client %p: can't create SDP", client);
3411 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
3413 gst_rtsp_media_unlock (media);
3414 g_object_unref (media);
3420 handle_sdp (GstRTSPClient * client, GstRTSPContext * ctx, GstRTSPMedia * media,
3421 GstSDPMessage * sdp)
3423 GstRTSPClientPrivate *priv = client->priv;
3424 GstRTSPThread *thread;
3426 /* create an SDP for the media object */
3427 if (!gst_rtsp_media_handle_sdp (media, sdp))
3430 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
3431 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
3435 /* prepare the media */
3436 if (!gst_rtsp_media_prepare (media, thread))
3444 GST_ERROR ("client %p: could not handle SDP", client);
3449 GST_ERROR ("client %p: can't create thread", client);
3454 GST_ERROR ("client %p: can't prepare media", client);
3460 handle_announce_request (GstRTSPClient * client, GstRTSPContext * ctx)
3462 GstRTSPClientPrivate *priv = client->priv;
3463 GstRTSPClientClass *klass;
3466 GstRTSPMedia *media;
3467 gchar *path, *cont = NULL;
3470 GstRTSPStatusCode sig_result;
3473 klass = GST_RTSP_CLIENT_GET_CLASS (client);
3478 if (!priv->mount_points)
3479 goto no_mount_points;
3481 /* check if reply is SDP */
3482 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_CONTENT_TYPE, &cont,
3484 /* could not be set but since the request returned OK, we assume it
3485 * was SDP, else check it. */
3487 if (g_ascii_strcasecmp (cont, "application/sdp") != 0)
3488 goto wrong_content_type;
3491 /* get message body and parse as SDP */
3492 gst_rtsp_message_get_body (ctx->request, &data, &size);
3493 if (data == NULL || size == 0)
3496 GST_DEBUG ("client %p: parse SDP...", client);
3497 gst_sdp_message_new (&sdp);
3498 sres = gst_sdp_message_parse_buffer (data, size, sdp);
3499 if (sres != GST_SDP_OK)
3500 goto sdp_parse_failed;
3502 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
3505 /* find the media object for the uri */
3506 if (!(media = find_media (client, ctx, path, NULL)))
3510 gst_rtsp_media_lock (media);
3512 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_ANNOUNCE_REQUEST],
3513 0, ctx, &sig_result);
3514 if (sig_result != GST_RTSP_STS_OK) {
3518 if (!(gst_rtsp_media_get_transport_mode (media) &
3519 GST_RTSP_TRANSPORT_MODE_RECORD))
3520 goto unsupported_mode;
3522 /* Tell client subclass about the media */
3523 if (!klass->handle_sdp (client, ctx, media, sdp))
3526 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
3527 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
3529 n_streams = gst_rtsp_media_n_streams (media);
3530 for (i = 0; i < n_streams; i++) {
3531 GstRTSPStream *stream = gst_rtsp_media_get_stream (media, i);
3532 gchar *uri, *location, *keymgmt;
3534 uri = gst_rtsp_url_get_request_uri (ctx->uri);
3535 location = g_strdup_printf ("%s/stream=%d", uri, i);
3536 keymgmt = stream_make_keymgmt (client, location, stream);
3539 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_KEYMGMT,
3546 /* we suspend after the announce */
3547 gst_rtsp_media_suspend (media);
3549 send_message (client, ctx, ctx->response, FALSE);
3551 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_ANNOUNCE_REQUEST],
3554 gst_sdp_message_free (sdp);
3556 gst_rtsp_media_unlock (media);
3557 g_object_unref (media);
3563 GST_ERROR ("client %p: no uri", client);
3564 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
3569 GST_ERROR ("client %p: no mount points configured", client);
3570 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
3575 GST_ERROR ("client %p: can't find path for url", client);
3576 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
3577 gst_sdp_message_free (sdp);
3582 GST_ERROR ("client %p: unknown content type", client);
3583 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
3588 GST_ERROR ("client %p: can't find SDP message", client);
3589 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
3594 GST_ERROR ("client %p: failed to parse SDP message", client);
3595 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
3596 gst_sdp_message_free (sdp);
3601 GST_ERROR ("client %p: no media", client);
3603 /* error reply is already sent */
3604 gst_sdp_message_free (sdp);
3609 GST_ERROR ("client %p: pre signal returned error: %s", client,
3610 gst_rtsp_status_as_text (sig_result));
3611 send_generic_response (client, sig_result, ctx);
3612 gst_sdp_message_free (sdp);
3613 gst_rtsp_media_unlock (media);
3614 g_object_unref (media);
3619 GST_ERROR ("client %p: media does not support ANNOUNCE", client);
3620 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_ALLOWED, ctx);
3622 gst_rtsp_media_unlock (media);
3623 g_object_unref (media);
3624 gst_sdp_message_free (sdp);
3629 GST_ERROR ("client %p: can't handle SDP", client);
3630 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_MEDIA_TYPE, ctx);
3632 gst_rtsp_media_unlock (media);
3633 g_object_unref (media);
3634 gst_sdp_message_free (sdp);
3640 handle_record_request (GstRTSPClient * client, GstRTSPContext * ctx)
3642 GstRTSPSession *session;
3643 GstRTSPClientClass *klass;
3644 GstRTSPSessionMedia *sessmedia;
3645 GstRTSPMedia *media;
3647 GstRTSPState rtspstate;
3650 GstRTSPStatusCode sig_result;
3651 GPtrArray *transports;
3653 if (!(session = ctx->session))
3656 if (!(uri = ctx->uri))
3659 klass = GST_RTSP_CLIENT_GET_CLASS (client);
3660 path = klass->make_path_from_uri (client, uri);
3662 /* get a handle to the configuration of the media in the session */
3663 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
3667 if (path[matched] != '\0')
3672 ctx->sessmedia = sessmedia;
3673 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
3675 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_RECORD_REQUEST], 0,
3677 if (sig_result != GST_RTSP_STS_OK) {
3681 if (!(gst_rtsp_media_get_transport_mode (media) &
3682 GST_RTSP_TRANSPORT_MODE_RECORD))
3683 goto unsupported_mode;
3685 /* the session state must be playing or ready */
3686 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
3687 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
3690 /* update the pipeline */
3691 transports = gst_rtsp_session_media_get_transports (sessmedia);
3692 if (!gst_rtsp_media_complete_pipeline (media, transports)) {
3693 g_ptr_array_unref (transports);
3694 goto pipeline_error;
3696 g_ptr_array_unref (transports);
3698 /* in record we first unsuspend, media could be suspended from SDP or PAUSED */
3699 if (!gst_rtsp_media_unsuspend (media))
3700 goto unsuspend_failed;
3702 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
3703 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
3705 send_message (client, ctx, ctx->response, FALSE);
3707 /* start playing after sending the response */
3708 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
3710 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
3712 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_RECORD_REQUEST], 0,
3720 GST_ERROR ("client %p: no session", client);
3721 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
3726 GST_ERROR ("client %p: no uri supplied", client);
3727 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
3732 GST_ERROR ("client %p: media not found", client);
3733 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
3738 GST_ERROR ("client %p: no aggregate path %s", client, path);
3739 send_generic_response (client,
3740 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
3746 GST_ERROR ("client %p: pre signal returned error: %s", client,
3747 gst_rtsp_status_as_text (sig_result));
3748 send_generic_response (client, sig_result, ctx);
3753 GST_ERROR ("client %p: media does not support RECORD", client);
3754 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_ALLOWED, ctx);
3759 GST_ERROR ("client %p: not PLAYING or READY", client);
3760 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
3766 GST_ERROR ("client %p: failed to configure the pipeline", client);
3767 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
3773 GST_ERROR ("client %p: unsuspend failed", client);
3774 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
3780 default_handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx,
3781 GstRTSPVersion version)
3783 GstRTSPMethod options;
3785 GstRTSPStatusCode sig_result;
3787 options = GST_RTSP_DESCRIBE |
3792 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
3794 if (version < GST_RTSP_VERSION_2_0) {
3795 options |= GST_RTSP_RECORD;
3796 options |= GST_RTSP_ANNOUNCE;
3799 str = gst_rtsp_options_as_text (options);
3801 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
3802 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
3804 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
3807 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_OPTIONS_REQUEST], 0,
3809 if (sig_result != GST_RTSP_STS_OK) {
3813 send_message (client, ctx, ctx->response, FALSE);
3815 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
3823 GST_ERROR ("client %p: pre signal returned error: %s", client,
3824 gst_rtsp_status_as_text (sig_result));
3825 send_generic_response (client, sig_result, ctx);
3826 gst_rtsp_message_free (ctx->response);
3831 /* remove duplicate and trailing '/' */
3833 sanitize_uri (GstRTSPUrl * uri)
3837 gboolean have_slash, prev_slash;
3839 s = d = uri->abspath;
3840 len = strlen (uri->abspath);
3844 for (i = 0; i < len; i++) {
3845 have_slash = s[i] == '/';
3847 if (!have_slash || !prev_slash)
3849 prev_slash = have_slash;
3851 len = d - uri->abspath;
3852 /* don't remove the first slash if that's the only thing left */
3853 if (len > 1 && *(d - 1) == '/')
3858 /* is called when the session is removed from its session pool. */
3860 client_session_removed (GstRTSPSessionPool * pool, GstRTSPSession * session,
3861 GstRTSPClient * client)
3863 GstRTSPClientPrivate *priv = client->priv;
3866 GST_INFO ("client %p: session %p removed", client, session);
3868 g_mutex_lock (&priv->lock);
3869 client_unwatch_session (client, session, NULL);
3871 if (!priv->sessions && priv->rtsp_ctrl_timeout == NULL) {
3872 if (priv->post_session_timeout > 0) {
3873 GWeakRef *client_weak_ref = g_new (GWeakRef, 1);
3874 timer_src = g_timeout_source_new_seconds (RTSP_CTRL_CB_INTERVAL);
3876 g_weak_ref_init (client_weak_ref, client);
3877 g_source_set_callback (timer_src, rtsp_ctrl_timeout_cb, client_weak_ref,
3878 rtsp_ctrl_timeout_destroy_notify);
3879 priv->rtsp_ctrl_timeout_cnt = 0;
3880 g_source_attach (timer_src, priv->watch_context);
3881 priv->rtsp_ctrl_timeout = timer_src;
3882 GST_DEBUG ("rtsp control setting up connection timeout %p.",
3883 priv->rtsp_ctrl_timeout);
3884 g_mutex_unlock (&priv->lock);
3885 } else if (priv->post_session_timeout == 0) {
3886 g_mutex_unlock (&priv->lock);
3887 gst_rtsp_client_close (client);
3889 g_mutex_unlock (&priv->lock);
3892 g_mutex_unlock (&priv->lock);
3896 /* Check for Require headers. Returns TRUE if there are no Require headers,
3897 * otherwise lets the application decide which headers are supported.
3898 * By default all headers are unsupported.
3899 * If there are unsupported options, FALSE will be returned together with
3900 * a newly-allocated string of (comma-separated) unsupported options in
3901 * the unsupported_reqs variable.
3903 * There may be multiple Require headers, but we must send one single
3904 * Unsupported header with all the unsupported options as response. If
3905 * an incoming Require header contained a comma-separated list of options
3906 * GstRtspConnection will already have split that list up into multiple
3910 check_request_requirements (GstRTSPContext * ctx, gchar ** unsupported_reqs)
3913 GPtrArray *arr = NULL;
3914 GstRTSPMessage *msg = ctx->request;
3917 gchar *sig_result = NULL;
3918 gboolean result = TRUE;
3922 res = gst_rtsp_message_get_header (msg, GST_RTSP_HDR_REQUIRE, &reqs, i++);
3924 if (res == GST_RTSP_ENOTIMPL)
3928 arr = g_ptr_array_new_with_free_func ((GDestroyNotify) g_free);
3930 g_ptr_array_add (arr, g_strdup (reqs));
3934 /* if we don't have any Require headers at all, all is fine */
3938 /* otherwise we've now processed at all the Require headers */
3939 g_ptr_array_add (arr, NULL);
3941 g_signal_emit (ctx->client,
3942 gst_rtsp_client_signals[SIGNAL_CHECK_REQUIREMENTS], 0, ctx,
3943 (gchar **) arr->pdata, &sig_result);
3945 if (sig_result == NULL) {
3946 /* no supported options, just report all of the required ones as
3948 *unsupported_reqs = g_strjoinv (", ", (gchar **) arr->pdata);
3953 if (strlen (sig_result) == 0)
3954 g_free (sig_result);
3956 *unsupported_reqs = sig_result;
3961 g_ptr_array_unref (arr);
3966 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
3968 GstRTSPClientPrivate *priv = client->priv;
3969 GstRTSPMethod method;
3970 const gchar *uristr;
3971 GstRTSPUrl *uri = NULL;
3972 GstRTSPVersion version;
3974 GstRTSPSession *session = NULL;
3975 GstRTSPContext sctx = { NULL }, *ctx;
3976 GstRTSPMessage response = { 0 };
3977 gchar *unsupported_reqs = NULL;
3978 gchar *sessid = NULL, *pipelined_request_id = NULL;
3979 GstRTSPClientClass *klass;
3981 klass = GST_RTSP_CLIENT_GET_CLASS (client);
3982 if (!(ctx = gst_rtsp_context_get_current ())) {
3984 ctx->auth = priv->auth;
3985 gst_rtsp_context_push_current (ctx);
3988 ctx->conn = priv->connection;
3989 ctx->client = client;
3990 ctx->request = request;
3991 ctx->response = &response;
3993 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
3994 gst_rtsp_message_dump (request);
3997 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
3999 GST_INFO ("client %p: received a request %s %s %s", client,
4000 gst_rtsp_method_as_text (method), uristr,
4001 gst_rtsp_version_as_text (version));
4003 /* we can only handle 1.0 requests */
4004 if (version != GST_RTSP_VERSION_1_0 && version != GST_RTSP_VERSION_2_0)
4007 ctx->method = method;
4009 /* we always try to parse the url first */
4010 if (strcmp (uristr, "*") == 0) {
4011 /* special case where we have * as uri, keep uri = NULL */
4012 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
4013 /* check if the uristr is an absolute path <=> scheme and host information
4017 scheme = g_uri_parse_scheme (uristr);
4018 if (scheme == NULL && g_str_has_prefix (uristr, "/")) {
4019 gchar *absolute_uristr = NULL;
4021 GST_WARNING_OBJECT (client, "request doesn't contain absolute url");
4022 if (priv->server_ip == NULL) {
4023 GST_WARNING_OBJECT (client, "host information missing");
4028 g_strdup_printf ("rtsp://%s%s", priv->server_ip, uristr);
4030 GST_DEBUG_OBJECT (client, "absolute url: %s", absolute_uristr);
4031 if (gst_rtsp_url_parse (absolute_uristr, &uri) != GST_RTSP_OK) {
4032 g_free (absolute_uristr);
4035 g_free (absolute_uristr);
4042 /* get the session if there is any */
4043 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_PIPELINED_REQUESTS,
4044 &pipelined_request_id, 0);
4045 if (res == GST_RTSP_OK) {
4046 sessid = g_hash_table_lookup (client->priv->pipelined_requests,
4047 pipelined_request_id);
4050 res = GST_RTSP_ERROR;
4053 if (res != GST_RTSP_OK)
4055 gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
4057 if (res == GST_RTSP_OK) {
4058 if (priv->session_pool == NULL)
4061 /* we had a session in the request, find it again */
4062 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
4063 goto session_not_found;
4065 /* we add the session to the client list of watched sessions. When a session
4066 * disappears because it times out, we will be notified. If all sessions are
4067 * gone, we will close the connection */
4068 client_watch_session (client, session);
4071 /* sanitize the uri */
4075 ctx->session = session;
4077 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
4078 goto not_authorized;
4080 /* handle any 'Require' headers */
4081 if (!check_request_requirements (ctx, &unsupported_reqs))
4082 goto unsupported_requirement;
4084 /* now see what is asked and dispatch to a dedicated handler */
4086 case GST_RTSP_OPTIONS:
4087 priv->version = version;
4088 klass->handle_options_request (client, ctx, version);
4090 case GST_RTSP_DESCRIBE:
4091 handle_describe_request (client, ctx);
4093 case GST_RTSP_SETUP:
4094 handle_setup_request (client, ctx);
4097 klass->handle_play_request (client, ctx);
4099 case GST_RTSP_PAUSE:
4100 handle_pause_request (client, ctx);
4102 case GST_RTSP_TEARDOWN:
4103 handle_teardown_request (client, ctx);
4105 case GST_RTSP_SET_PARAMETER:
4106 klass->handle_set_param_request (client, ctx);
4108 case GST_RTSP_GET_PARAMETER:
4109 klass->handle_get_param_request (client, ctx);
4111 case GST_RTSP_ANNOUNCE:
4112 if (version >= GST_RTSP_VERSION_2_0)
4113 goto invalid_command_for_version;
4114 handle_announce_request (client, ctx);
4116 case GST_RTSP_RECORD:
4117 if (version >= GST_RTSP_VERSION_2_0)
4118 goto invalid_command_for_version;
4119 handle_record_request (client, ctx);
4121 case GST_RTSP_REDIRECT:
4122 goto not_implemented;
4123 case GST_RTSP_INVALID:
4130 gst_rtsp_context_pop_current (ctx);
4132 g_object_unref (session);
4134 gst_rtsp_url_free (uri);
4140 GST_ERROR ("client %p: version %d not supported", client, version);
4141 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
4145 invalid_command_for_version:
4147 GST_ERROR ("client %p: invalid command for version", client);
4148 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
4153 GST_ERROR ("client %p: bad request", client);
4154 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
4159 GST_ERROR ("client %p: no pool configured", client);
4160 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
4165 GST_ERROR ("client %p: session not found", client);
4166 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
4171 GST_ERROR ("client %p: not allowed", client);
4172 /* error reply is already sent */
4175 unsupported_requirement:
4177 GST_ERROR ("client %p: Required option is not supported (%s)", client,
4179 send_option_not_supported_response (client, ctx, unsupported_reqs);
4180 g_free (unsupported_reqs);
4185 GST_ERROR ("client %p: method %d not implemented", client, method);
4186 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
4193 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
4195 GstRTSPClientPrivate *priv = client->priv;
4197 GstRTSPSession *session = NULL;
4198 GstRTSPContext sctx = { NULL }, *ctx;
4201 if (!(ctx = gst_rtsp_context_get_current ())) {
4203 ctx->auth = priv->auth;
4204 gst_rtsp_context_push_current (ctx);
4207 ctx->conn = priv->connection;
4208 ctx->client = client;
4209 ctx->request = NULL;
4211 ctx->method = GST_RTSP_INVALID;
4212 ctx->response = response;
4214 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
4215 gst_rtsp_message_dump (response);
4218 GST_INFO ("client %p: received a response", client);
4220 /* get the session if there is any */
4222 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
4223 if (res == GST_RTSP_OK) {
4224 if (priv->session_pool == NULL)
4227 /* we had a session in the request, find it again */
4228 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
4229 goto session_not_found;
4231 /* we add the session to the client list of watched sessions. When a session
4232 * disappears because it times out, we will be notified. If all sessions are
4233 * gone, we will close the connection */
4234 client_watch_session (client, session);
4237 ctx->session = session;
4239 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
4244 gst_rtsp_context_pop_current (ctx);
4246 g_object_unref (session);
4251 GST_ERROR ("client %p: no pool configured", client);
4256 GST_ERROR ("client %p: session not found", client);
4262 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
4264 GstRTSPClientPrivate *priv = client->priv;
4270 GstRTSPStreamTransport *trans;
4272 /* find the stream for this message */
4273 res = gst_rtsp_message_parse_data (message, &channel);
4274 if (res != GST_RTSP_OK)
4277 gst_rtsp_message_get_body (message, &data, &size);
4279 goto invalid_length;
4281 gst_rtsp_message_steal_body (message, &data, &size);
4283 /* Strip trailing \0 (which GstRTSPConnection adds) */
4286 buffer = gst_buffer_new_wrapped (data, size);
4289 g_hash_table_lookup (priv->transports, GINT_TO_POINTER ((gint) channel));
4291 GSocketAddress *addr;
4293 /* Only create the socket address once for the transport, we don't really
4294 * want to do that for every single packet.
4296 * The netaddress meta is later used by the RTP stack to know where
4297 * packets came from and allows us to match it again to a stream transport
4299 * In theory we could use the remote socket address of the RTSP connection
4300 * here, but this would fail with a custom configure_client_transport()
4304 g_object_get_data (G_OBJECT (trans), "rtsp-client.remote-addr"))) {
4305 const GstRTSPTransport *tr;
4306 GInetAddress *iaddr;
4308 tr = gst_rtsp_stream_transport_get_transport (trans);
4309 iaddr = g_inet_address_new_from_string (tr->destination);
4311 addr = g_inet_socket_address_new (iaddr, tr->client_port.min);
4312 g_object_unref (iaddr);
4313 g_object_set_data_full (G_OBJECT (trans), "rtsp-client.remote-addr",
4314 addr, (GDestroyNotify) g_object_unref);
4319 gst_buffer_add_net_address_meta (buffer, addr);
4322 /* dispatch to the stream based on the channel number */
4323 GST_LOG_OBJECT (client, "%u bytes of data on channel %u", size, channel);
4324 gst_rtsp_stream_transport_recv_data (trans, channel, buffer);
4326 GST_DEBUG_OBJECT (client, "received %u bytes of data for "
4327 "unknown channel %u", size, channel);
4328 gst_buffer_unref (buffer);
4336 GST_DEBUG ("client %p: Short message received, ignoring", client);
4342 * gst_rtsp_client_set_session_pool:
4343 * @client: a #GstRTSPClient
4344 * @pool: (transfer none) (nullable): a #GstRTSPSessionPool
4346 * Set @pool as the sessionpool for @client which it will use to find
4347 * or allocate sessions. the sessionpool is usually inherited from the server
4348 * that created the client but can be overridden later.
4351 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
4352 GstRTSPSessionPool * pool)
4354 GstRTSPSessionPool *old;
4355 GstRTSPClientPrivate *priv;
4357 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
4359 priv = client->priv;
4362 g_object_ref (pool);
4364 g_mutex_lock (&priv->lock);
4365 old = priv->session_pool;
4366 priv->session_pool = pool;
4368 if (priv->session_removed_id) {
4369 g_signal_handler_disconnect (old, priv->session_removed_id);
4370 priv->session_removed_id = 0;
4372 g_mutex_unlock (&priv->lock);
4374 /* FIXME, should remove all sessions from the old pool for this client */
4376 g_object_unref (old);
4380 * gst_rtsp_client_get_session_pool:
4381 * @client: a #GstRTSPClient
4383 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
4385 * Returns: (transfer full) (nullable): a #GstRTSPSessionPool, unref after usage.
4387 GstRTSPSessionPool *
4388 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
4390 GstRTSPClientPrivate *priv;
4391 GstRTSPSessionPool *result;
4393 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
4395 priv = client->priv;
4397 g_mutex_lock (&priv->lock);
4398 if ((result = priv->session_pool))
4399 g_object_ref (result);
4400 g_mutex_unlock (&priv->lock);
4406 * gst_rtsp_client_set_mount_points:
4407 * @client: a #GstRTSPClient
4408 * @mounts: (transfer none) (nullable): a #GstRTSPMountPoints
4410 * Set @mounts as the mount points for @client which it will use to map urls
4411 * to media streams. These mount points are usually inherited from the server that
4412 * created the client but can be overriden later.
4415 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
4416 GstRTSPMountPoints * mounts)
4418 GstRTSPClientPrivate *priv;
4419 GstRTSPMountPoints *old;
4421 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
4423 priv = client->priv;
4426 g_object_ref (mounts);
4428 g_mutex_lock (&priv->lock);
4429 old = priv->mount_points;
4430 priv->mount_points = mounts;
4431 g_mutex_unlock (&priv->lock);
4434 g_object_unref (old);
4438 * gst_rtsp_client_get_mount_points:
4439 * @client: a #GstRTSPClient
4441 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
4443 * Returns: (transfer full) (nullable): a #GstRTSPMountPoints, unref after usage.
4445 GstRTSPMountPoints *
4446 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
4448 GstRTSPClientPrivate *priv;
4449 GstRTSPMountPoints *result;
4451 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
4453 priv = client->priv;
4455 g_mutex_lock (&priv->lock);
4456 if ((result = priv->mount_points))
4457 g_object_ref (result);
4458 g_mutex_unlock (&priv->lock);
4464 * gst_rtsp_client_set_content_length_limit:
4465 * @client: a #GstRTSPClient
4466 * @limit: Content-Length limit
4468 * Configure @client to use the specified Content-Length limit.
4470 * Define an appropriate request size limit and reject requests exceeding the
4471 * limit with response status 413 Request Entity Too Large
4476 gst_rtsp_client_set_content_length_limit (GstRTSPClient * client, guint limit)
4478 GstRTSPClientPrivate *priv;
4480 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
4482 priv = client->priv;
4483 g_mutex_lock (&priv->lock);
4484 priv->content_length_limit = limit;
4485 g_mutex_unlock (&priv->lock);
4489 * gst_rtsp_client_get_content_length_limit:
4490 * @client: a #GstRTSPClient
4492 * Get the Content-Length limit of @client.
4494 * Returns: the Content-Length limit.
4499 gst_rtsp_client_get_content_length_limit (GstRTSPClient * client)
4501 GstRTSPClientPrivate *priv;
4502 glong content_length_limit;
4504 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), -1);
4505 priv = client->priv;
4507 g_mutex_lock (&priv->lock);
4508 content_length_limit = priv->content_length_limit;
4509 g_mutex_unlock (&priv->lock);
4511 return content_length_limit;
4515 * gst_rtsp_client_set_auth:
4516 * @client: a #GstRTSPClient
4517 * @auth: (transfer none) (nullable): a #GstRTSPAuth
4519 * configure @auth to be used as the authentication manager of @client.
4522 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
4524 GstRTSPClientPrivate *priv;
4527 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
4529 priv = client->priv;
4532 g_object_ref (auth);
4534 g_mutex_lock (&priv->lock);
4537 g_mutex_unlock (&priv->lock);
4540 g_object_unref (old);
4545 * gst_rtsp_client_get_auth:
4546 * @client: a #GstRTSPClient
4548 * Get the #GstRTSPAuth used as the authentication manager of @client.
4550 * Returns: (transfer full) (nullable): the #GstRTSPAuth of @client.
4551 * g_object_unref() after usage.
4554 gst_rtsp_client_get_auth (GstRTSPClient * client)
4556 GstRTSPClientPrivate *priv;
4557 GstRTSPAuth *result;
4559 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
4561 priv = client->priv;
4563 g_mutex_lock (&priv->lock);
4564 if ((result = priv->auth))
4565 g_object_ref (result);
4566 g_mutex_unlock (&priv->lock);
4572 * gst_rtsp_client_set_thread_pool:
4573 * @client: a #GstRTSPClient
4574 * @pool: (transfer none) (nullable): a #GstRTSPThreadPool
4576 * configure @pool to be used as the thread pool of @client.
4579 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
4580 GstRTSPThreadPool * pool)
4582 GstRTSPClientPrivate *priv;
4583 GstRTSPThreadPool *old;
4585 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
4587 priv = client->priv;
4590 g_object_ref (pool);
4592 g_mutex_lock (&priv->lock);
4593 old = priv->thread_pool;
4594 priv->thread_pool = pool;
4595 g_mutex_unlock (&priv->lock);
4598 g_object_unref (old);
4602 * gst_rtsp_client_get_thread_pool:
4603 * @client: a #GstRTSPClient
4605 * Get the #GstRTSPThreadPool used as the thread pool of @client.
4607 * Returns: (transfer full) (nullable): the #GstRTSPThreadPool of @client. g_object_unref() after
4611 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
4613 GstRTSPClientPrivate *priv;
4614 GstRTSPThreadPool *result;
4616 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
4618 priv = client->priv;
4620 g_mutex_lock (&priv->lock);
4621 if ((result = priv->thread_pool))
4622 g_object_ref (result);
4623 g_mutex_unlock (&priv->lock);
4629 * gst_rtsp_client_set_connection:
4630 * @client: a #GstRTSPClient
4631 * @conn: (transfer full): a #GstRTSPConnection
4633 * Set the #GstRTSPConnection of @client. This function takes ownership of
4636 * Returns: %TRUE on success.
4639 gst_rtsp_client_set_connection (GstRTSPClient * client,
4640 GstRTSPConnection * conn)
4642 GstRTSPClientPrivate *priv;
4643 GSocket *read_socket;
4644 GSocketAddress *address;
4646 GError *error = NULL;
4648 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
4649 g_return_val_if_fail (conn != NULL, FALSE);
4651 priv = client->priv;
4653 gst_rtsp_connection_set_content_length_limit (conn,
4654 priv->content_length_limit);
4655 read_socket = gst_rtsp_connection_get_read_socket (conn);
4657 if (!(address = g_socket_get_local_address (read_socket, &error)))
4660 g_free (priv->server_ip);
4661 /* keep the original ip that the client connected to */
4662 if (G_IS_INET_SOCKET_ADDRESS (address)) {
4663 GInetAddress *iaddr;
4665 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
4667 /* socket might be ipv6 but adress still ipv4 */
4668 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
4669 priv->server_ip = g_inet_address_to_string (iaddr);
4670 g_object_unref (address);
4672 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
4673 priv->server_ip = g_strdup ("unknown");
4674 g_object_unref (address);
4677 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
4678 priv->server_ip, priv->is_ipv6);
4680 url = gst_rtsp_connection_get_url (conn);
4681 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
4683 priv->connection = conn;
4690 GST_ERROR ("could not get local address %s", error->message);
4691 g_error_free (error);
4697 * gst_rtsp_client_get_connection:
4698 * @client: a #GstRTSPClient
4700 * Get the #GstRTSPConnection of @client.
4702 * Returns: (transfer none) (nullable): the #GstRTSPConnection of @client.
4703 * The connection object returned remains valid until the client is freed.
4706 gst_rtsp_client_get_connection (GstRTSPClient * client)
4708 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
4710 return client->priv->connection;
4714 * gst_rtsp_client_set_send_func:
4715 * @client: a #GstRTSPClient
4716 * @func: (scope notified): a #GstRTSPClientSendFunc
4717 * @user_data: (closure): user data passed to @func
4718 * @notify: (allow-none): called when @user_data is no longer in use
4720 * Set @func as the callback that will be called when a new message needs to be
4721 * sent to the client. @user_data is passed to @func and @notify is called when
4722 * @user_data is no longer in use.
4724 * By default, the client will send the messages on the #GstRTSPConnection that
4725 * was configured with gst_rtsp_client_attach() was called.
4727 * It is only allowed to set either a `send_func` or a `send_messages_func`
4728 * but not both at the same time.
4731 gst_rtsp_client_set_send_func (GstRTSPClient * client,
4732 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
4734 GstRTSPClientPrivate *priv;
4735 GDestroyNotify old_notify;
4738 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
4740 priv = client->priv;
4742 g_mutex_lock (&priv->send_lock);
4743 g_assert (func == NULL || priv->send_messages_func == NULL);
4744 priv->send_func = func;
4745 old_notify = priv->send_notify;
4746 old_data = priv->send_data;
4747 priv->send_notify = notify;
4748 priv->send_data = user_data;
4749 g_mutex_unlock (&priv->send_lock);
4752 old_notify (old_data);
4756 * gst_rtsp_client_set_send_messages_func:
4757 * @client: a #GstRTSPClient
4758 * @func: (scope notified): a #GstRTSPClientSendMessagesFunc
4759 * @user_data: (closure): user data passed to @func
4760 * @notify: (allow-none): called when @user_data is no longer in use
4762 * Set @func as the callback that will be called when new messages needs to be
4763 * sent to the client. @user_data is passed to @func and @notify is called when
4764 * @user_data is no longer in use.
4766 * By default, the client will send the messages on the #GstRTSPConnection that
4767 * was configured with gst_rtsp_client_attach() was called.
4769 * It is only allowed to set either a `send_func` or a `send_messages_func`
4770 * but not both at the same time.
4775 gst_rtsp_client_set_send_messages_func (GstRTSPClient * client,
4776 GstRTSPClientSendMessagesFunc func, gpointer user_data,
4777 GDestroyNotify notify)
4779 GstRTSPClientPrivate *priv;
4780 GDestroyNotify old_notify;
4783 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
4785 priv = client->priv;
4787 g_mutex_lock (&priv->send_lock);
4788 g_assert (func == NULL || priv->send_func == NULL);
4789 priv->send_messages_func = func;
4790 old_notify = priv->send_messages_notify;
4791 old_data = priv->send_messages_data;
4792 priv->send_messages_notify = notify;
4793 priv->send_messages_data = user_data;
4794 g_mutex_unlock (&priv->send_lock);
4797 old_notify (old_data);
4801 * gst_rtsp_client_handle_message:
4802 * @client: a #GstRTSPClient
4803 * @message: (transfer none): an #GstRTSPMessage
4805 * Let the client handle @message.
4807 * Returns: a #GstRTSPResult.
4810 gst_rtsp_client_handle_message (GstRTSPClient * client,
4811 GstRTSPMessage * message)
4813 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
4814 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
4816 switch (message->type) {
4817 case GST_RTSP_MESSAGE_REQUEST:
4818 handle_request (client, message);
4820 case GST_RTSP_MESSAGE_RESPONSE:
4821 handle_response (client, message);
4823 case GST_RTSP_MESSAGE_DATA:
4824 handle_data (client, message);
4833 * gst_rtsp_client_send_message:
4834 * @client: a #GstRTSPClient
4835 * @session: (allow-none) (transfer none): a #GstRTSPSession to send
4836 * the message to or %NULL
4837 * @message: (transfer none): The #GstRTSPMessage to send
4839 * Send a message message to the remote end. @message must be a
4840 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
4843 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
4844 GstRTSPMessage * message)
4846 GstRTSPContext sctx = { NULL }
4848 GstRTSPClientPrivate *priv;
4850 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
4851 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
4852 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
4853 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
4855 priv = client->priv;
4857 if (!(ctx = gst_rtsp_context_get_current ())) {
4859 ctx->auth = priv->auth;
4860 gst_rtsp_context_push_current (ctx);
4863 ctx->conn = priv->connection;
4864 ctx->client = client;
4865 ctx->session = session;
4867 send_message (client, ctx, message, FALSE);
4870 gst_rtsp_context_pop_current (ctx);
4876 * gst_rtsp_client_get_stream_transport:
4878 * This is useful when providing a send function through
4879 * gst_rtsp_client_set_send_func() when doing RTSP over TCP:
4880 * the send function must call gst_rtsp_stream_transport_message_sent ()
4881 * on the appropriate transport when data has been received for streaming
4884 * Returns: (transfer none) (nullable): the #GstRTSPStreamTransport associated with @channel.
4888 GstRTSPStreamTransport *
4889 gst_rtsp_client_get_stream_transport (GstRTSPClient * self, guint8 channel)
4891 return g_hash_table_lookup (self->priv->transports,
4892 GINT_TO_POINTER ((gint) channel));
4896 do_send_messages (GstRTSPClient * client, GstRTSPMessage * messages,
4897 guint n_messages, gboolean close, gpointer user_data)
4899 GstRTSPClientPrivate *priv = client->priv;
4904 /* send the message */
4906 GST_INFO ("client %p: sending close message", client);
4908 ret = gst_rtsp_watch_send_messages (priv->watch, messages, n_messages, &id);
4909 if (ret != GST_RTSP_OK)
4912 for (i = 0; i < n_messages; i++) {
4913 if (gst_rtsp_message_get_type (&messages[i]) == GST_RTSP_MESSAGE_DATA) {
4917 /* We assume that all data messages in the list are for the
4919 r = gst_rtsp_message_parse_data (&messages[i], &channel);
4920 if (r != GST_RTSP_OK) {
4925 /* check if the message has been queued for transmission in watch */
4927 /* store the seq number so we can wait until it has been sent */
4928 GST_DEBUG_OBJECT (client, "wait for message %d, channel %d", id,
4930 set_data_seq (client, channel, id);
4932 GstRTSPStreamTransport *trans;
4935 g_hash_table_lookup (priv->transports,
4936 GINT_TO_POINTER ((gint) channel));
4938 GST_DEBUG_OBJECT (client, "emit 'message-sent' signal");
4939 g_mutex_unlock (&priv->send_lock);
4940 gst_rtsp_stream_transport_message_sent (trans);
4941 g_mutex_lock (&priv->send_lock);
4948 return ret == GST_RTSP_OK;
4953 GST_DEBUG_OBJECT (client, "got error %d", ret);
4958 static GstRTSPResult
4959 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
4962 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
4965 static GstRTSPResult
4966 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
4968 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
4969 GstRTSPClientPrivate *priv = client->priv;
4970 GstRTSPStreamTransport *trans = NULL;
4973 g_mutex_lock (&priv->send_lock);
4975 if (get_data_channel (client, cseq, &channel)) {
4976 trans = g_hash_table_lookup (priv->transports, GINT_TO_POINTER (channel));
4977 set_data_seq (client, channel, 0);
4979 g_mutex_unlock (&priv->send_lock);
4982 GST_DEBUG_OBJECT (client, "emit 'message-sent' signal");
4983 gst_rtsp_stream_transport_message_sent (trans);
4989 static GstRTSPResult
4990 closed (GstRTSPWatch * watch, gpointer user_data)
4992 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
4993 GstRTSPClientPrivate *priv = client->priv;
4994 const gchar *tunnelid;
4996 GST_INFO ("client %p: connection closed", client);
4998 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
4999 g_mutex_lock (&tunnels_lock);
5000 /* remove from tunnelids */
5001 g_hash_table_remove (tunnels, tunnelid);
5002 g_mutex_unlock (&tunnels_lock);
5005 gst_rtsp_watch_set_flushing (watch, TRUE);
5006 g_mutex_lock (&priv->watch_lock);
5007 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
5008 gst_rtsp_client_set_send_messages_func (client, NULL, NULL, NULL);
5009 g_mutex_unlock (&priv->watch_lock);
5014 static GstRTSPResult
5015 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
5017 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
5020 str = gst_rtsp_strresult (result);
5021 GST_INFO ("client %p: received an error %s", client, str);
5027 static GstRTSPResult
5028 error_full (GstRTSPWatch * watch, GstRTSPResult result,
5029 GstRTSPMessage * message, guint id, gpointer user_data)
5031 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
5033 GstRTSPContext sctx = { NULL }, *ctx;
5034 GstRTSPClientPrivate *priv;
5035 GstRTSPMessage response = { 0 };
5036 priv = client->priv;
5038 if (!(ctx = gst_rtsp_context_get_current ())) {
5040 ctx->auth = priv->auth;
5041 gst_rtsp_context_push_current (ctx);
5044 ctx->conn = priv->connection;
5045 ctx->client = client;
5046 ctx->request = message;
5047 ctx->method = GST_RTSP_INVALID;
5048 ctx->response = &response;
5050 /* only return error response if it is a request */
5051 if (!message || message->type != GST_RTSP_MESSAGE_REQUEST)
5054 if (result == GST_RTSP_ENOMEM) {
5055 send_generic_response (client, GST_RTSP_STS_REQUEST_ENTITY_TOO_LARGE, ctx);
5058 if (result == GST_RTSP_EPARSE) {
5059 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
5065 gst_rtsp_context_pop_current (ctx);
5066 str = gst_rtsp_strresult (result);
5068 ("client %p: error when handling message %p with id %d: %s",
5069 client, message, id, str);
5076 remember_tunnel (GstRTSPClient * client)
5078 GstRTSPClientPrivate *priv = client->priv;
5079 const gchar *tunnelid;
5081 /* store client in the pending tunnels */
5082 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
5083 if (tunnelid == NULL)
5086 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
5088 /* we can't have two clients connecting with the same tunnelid */
5089 g_mutex_lock (&tunnels_lock);
5090 if (g_hash_table_lookup (tunnels, tunnelid))
5091 goto tunnel_existed;
5093 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
5094 g_mutex_unlock (&tunnels_lock);
5101 GST_ERROR ("client %p: no tunnelid provided", client);
5106 g_mutex_unlock (&tunnels_lock);
5107 GST_ERROR ("client %p: tunnel session %s already existed", client,
5113 static GstRTSPResult
5114 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
5116 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
5117 GstRTSPClientPrivate *priv = client->priv;
5119 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
5122 /* ignore error, it'll only be a problem when the client does a POST again */
5123 remember_tunnel (client);
5128 static GstRTSPStatusCode
5129 handle_tunnel (GstRTSPClient * client)
5131 GstRTSPClientPrivate *priv = client->priv;
5132 GstRTSPClient *oclient;
5133 GstRTSPClientPrivate *opriv;
5134 const gchar *tunnelid;
5136 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
5137 if (tunnelid == NULL)
5140 /* check for previous tunnel */
5141 g_mutex_lock (&tunnels_lock);
5142 oclient = g_hash_table_lookup (tunnels, tunnelid);
5144 if (oclient == NULL) {
5145 /* no previous tunnel, remember tunnel */
5146 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
5147 g_mutex_unlock (&tunnels_lock);
5149 GST_INFO ("client %p: no previous tunnel found, remembering tunnel (%p)",
5150 client, priv->connection);
5152 /* merge both tunnels into the first client */
5153 /* remove the old client from the table. ref before because removing it will
5154 * remove the ref to it. */
5155 g_object_ref (oclient);
5156 g_hash_table_remove (tunnels, tunnelid);
5157 g_mutex_unlock (&tunnels_lock);
5159 opriv = oclient->priv;
5161 g_mutex_lock (&opriv->watch_lock);
5162 if (opriv->watch == NULL)
5164 if (opriv->tstate == priv->tstate)
5165 goto tunnel_duplicate_id;
5167 GST_INFO ("client %p: found previous tunnel %p (old %p, new %p)", client,
5168 oclient, opriv->connection, priv->connection);
5170 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
5171 gst_rtsp_watch_reset (priv->watch);
5172 gst_rtsp_watch_reset (opriv->watch);
5173 g_mutex_unlock (&opriv->watch_lock);
5174 g_object_unref (oclient);
5176 /* the old client owns the tunnel now, the new one will be freed */
5177 g_source_destroy ((GSource *) priv->watch);
5179 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
5180 gst_rtsp_client_set_send_messages_func (client, NULL, NULL, NULL);
5181 rtsp_ctrl_timeout_remove (client);
5184 return GST_RTSP_STS_OK;
5189 GST_ERROR ("client %p: no tunnelid provided", client);
5190 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
5194 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
5195 g_mutex_unlock (&opriv->watch_lock);
5196 g_object_unref (oclient);
5197 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
5199 tunnel_duplicate_id:
5201 GST_ERROR ("client %p: tunnel session %s was duplicate", client, tunnelid);
5202 g_mutex_unlock (&opriv->watch_lock);
5203 g_object_unref (oclient);
5204 return GST_RTSP_STS_BAD_REQUEST;
5208 static GstRTSPStatusCode
5209 tunnel_get (GstRTSPWatch * watch, gpointer user_data)
5211 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
5213 GST_INFO ("client %p: tunnel get (connection %p)", client,
5214 client->priv->connection);
5216 g_mutex_lock (&client->priv->lock);
5217 client->priv->tstate = TUNNEL_STATE_GET;
5218 g_mutex_unlock (&client->priv->lock);
5220 return handle_tunnel (client);
5223 static GstRTSPResult
5224 tunnel_post (GstRTSPWatch * watch, gpointer user_data)
5226 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
5228 GST_INFO ("client %p: tunnel post (connection %p)", client,
5229 client->priv->connection);
5231 g_mutex_lock (&client->priv->lock);
5232 client->priv->tstate = TUNNEL_STATE_POST;
5233 g_mutex_unlock (&client->priv->lock);
5235 if (handle_tunnel (client) != GST_RTSP_STS_OK)
5236 return GST_RTSP_ERROR;
5241 static GstRTSPResult
5242 tunnel_http_response (GstRTSPWatch * watch, GstRTSPMessage * request,
5243 GstRTSPMessage * response, gpointer user_data)
5245 GstRTSPClientClass *klass;
5247 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
5248 klass = GST_RTSP_CLIENT_GET_CLASS (client);
5250 if (klass->tunnel_http_response) {
5251 klass->tunnel_http_response (client, request, response);
5257 static GstRTSPWatchFuncs watch_funcs = {
5266 tunnel_http_response
5270 client_watch_notify (GstRTSPClient * client)
5272 GstRTSPClientPrivate *priv = client->priv;
5273 gboolean closed = TRUE;
5275 GST_INFO ("client %p: watch destroyed", client);
5277 /* remove all sessions if the media says so and so drop the extra client ref */
5278 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
5279 gst_rtsp_client_set_send_messages_func (client, NULL, NULL, NULL);
5280 rtsp_ctrl_timeout_remove (client);
5281 gst_rtsp_client_session_filter (client, cleanup_session, &closed);
5284 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
5285 g_object_unref (client);
5289 * gst_rtsp_client_attach:
5290 * @client: a #GstRTSPClient
5291 * @context: (allow-none): a #GMainContext
5293 * Attaches @client to @context. When the mainloop for @context is run, the
5294 * client will be dispatched. When @context is %NULL, the default context will be
5297 * This function should be called when the client properties and urls are fully
5298 * configured and the client is ready to start.
5300 * Returns: the ID (greater than 0) for the source within the GMainContext.
5303 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
5305 GstRTSPClientPrivate *priv;
5308 GWeakRef *client_weak_ref = g_new (GWeakRef, 1);
5310 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
5311 priv = client->priv;
5312 g_return_val_if_fail (priv->connection != NULL, 0);
5313 g_return_val_if_fail (priv->watch == NULL, 0);
5314 g_return_val_if_fail (priv->watch_context == NULL, 0);
5316 /* make sure noone will free the context before the watch is destroyed */
5317 priv->watch_context = g_main_context_ref (context);
5319 /* create watch for the connection and attach */
5320 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
5321 g_object_ref (client), (GDestroyNotify) client_watch_notify);
5322 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
5323 gst_rtsp_client_set_send_messages_func (client, do_send_messages, priv->watch,
5324 (GDestroyNotify) gst_rtsp_watch_unref);
5326 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
5328 GST_INFO ("client %p: attaching to context %p", client, context);
5329 res = gst_rtsp_watch_attach (priv->watch, context);
5331 /* Setting up a timeout for the RTSP control channel until a session
5332 * is up where it is handling timeouts. */
5333 g_mutex_lock (&priv->lock);
5335 /* remove old timeout if any */
5336 rtsp_ctrl_timeout_remove_unlocked (client->priv);
5338 timer_src = g_timeout_source_new_seconds (RTSP_CTRL_CB_INTERVAL);
5339 g_weak_ref_init (client_weak_ref, client);
5340 g_source_set_callback (timer_src, rtsp_ctrl_timeout_cb, client_weak_ref,
5341 rtsp_ctrl_timeout_destroy_notify);
5342 g_source_attach (timer_src, priv->watch_context);
5343 priv->rtsp_ctrl_timeout = timer_src;
5344 GST_DEBUG ("rtsp control setting up session timeout %p.",
5345 priv->rtsp_ctrl_timeout);
5347 g_mutex_unlock (&priv->lock);
5353 * gst_rtsp_client_session_filter:
5354 * @client: a #GstRTSPClient
5355 * @func: (scope call) (allow-none): a callback
5356 * @user_data: user data passed to @func
5358 * Call @func for each session managed by @client. The result value of @func
5359 * determines what happens to the session. @func will be called with @client
5360 * locked so no further actions on @client can be performed from @func.
5362 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
5365 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
5367 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
5368 * will also be added with an additional ref to the result #GList of this
5371 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each session.
5373 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
5374 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
5375 * element in the #GList should be unreffed before the list is freed.
5378 gst_rtsp_client_session_filter (GstRTSPClient * client,
5379 GstRTSPClientSessionFilterFunc func, gpointer user_data)
5381 GstRTSPClientPrivate *priv;
5382 GList *result, *walk, *next;
5383 GHashTable *visited;
5386 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
5388 priv = client->priv;
5392 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
5394 g_mutex_lock (&priv->lock);
5396 cookie = priv->sessions_cookie;
5397 for (walk = priv->sessions; walk; walk = next) {
5398 GstRTSPSession *sess = walk->data;
5399 GstRTSPFilterResult res;
5402 next = g_list_next (walk);
5405 /* only visit each session once */
5406 if (g_hash_table_contains (visited, sess))
5409 g_hash_table_add (visited, g_object_ref (sess));
5410 g_mutex_unlock (&priv->lock);
5412 res = func (client, sess, user_data);
5414 g_mutex_lock (&priv->lock);
5416 res = GST_RTSP_FILTER_REF;
5418 changed = (cookie != priv->sessions_cookie);
5421 case GST_RTSP_FILTER_REMOVE:
5422 /* stop watching the session and pretend it went away, if the list was
5423 * changed, we can't use the current list position, try to see if we
5424 * still have the session */
5425 client_unwatch_session (client, sess, changed ? NULL : walk);
5426 cookie = priv->sessions_cookie;
5428 case GST_RTSP_FILTER_REF:
5429 result = g_list_prepend (result, g_object_ref (sess));
5431 case GST_RTSP_FILTER_KEEP:
5438 g_mutex_unlock (&priv->lock);
5441 g_hash_table_unref (visited);
5447 * gst_rtsp_client_set_watch_flushing:
5448 * @client: a #GstRTSPClient
5449 * @val: a boolean value
5451 * sets watch flushing to @val on watch to accet/ignore new messages.
5454 gst_rtsp_client_set_watch_flushing (GstRTSPClient * client, gboolean val)
5456 GstRTSPClientPrivate *priv = NULL;
5457 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
5459 priv = gst_rtsp_client_get_instance_private (client);
5461 /* make sure we unblock/block the backlog and accept/don't accept new messages on the watch */
5462 if (priv->watch != NULL) {
5463 GST_INFO ("Set watch flushing as %d", val);
5464 gst_rtsp_watch_set_flushing (priv->watch, val);