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_get_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);
1500 /* construct the response now */
1501 code = GST_RTSP_STS_OK;
1502 gst_rtsp_message_init_response (ctx->response, code,
1503 gst_rtsp_status_as_text (code), ctx->request);
1505 send_message (client, ctx, ctx->response, TRUE);
1507 if (!keep_session) {
1508 /* remove the session */
1509 gst_rtsp_session_pool_remove (priv->session_pool, session);
1512 gst_rtsp_media_unlock (media);
1513 g_object_unref (media);
1515 /* remove all transports that were present in the session media which we just
1516 * unmanaged from the priv->transports array, so we do not try to handle data
1517 * on channels that were just closed */
1518 cleanup_transports (client, session_media_transports);
1519 g_ptr_array_unref (session_media_transports);
1526 GST_ERROR ("client %p: no session", client);
1527 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1532 GST_ERROR ("client %p: no uri supplied", client);
1533 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1538 GST_ERROR ("client %p: no media for uri", client);
1539 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1545 GST_ERROR ("client %p: no aggregate path %s", client, path);
1546 send_generic_response (client,
1547 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1553 GST_ERROR ("client %p: pre signal returned error: %s", client,
1554 gst_rtsp_status_as_text (sig_result));
1555 send_generic_response (client, sig_result, ctx);
1556 gst_rtsp_media_unlock (media);
1557 g_object_unref (media);
1562 static GstRTSPResult
1563 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
1567 res = gst_rtsp_params_set (client, ctx);
1572 static GstRTSPResult
1573 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
1577 res = gst_rtsp_params_get (client, ctx);
1583 default_handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
1588 GstRTSPStatusCode sig_result;
1590 g_signal_emit (client,
1591 gst_rtsp_client_signals[SIGNAL_PRE_GET_PARAMETER_REQUEST], 0, ctx,
1593 if (sig_result != GST_RTSP_STS_OK) {
1597 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
1598 if (res != GST_RTSP_OK)
1601 if (size == 0 || !data || strlen ((char *) data) == 0) {
1602 if (ctx->request->type_data.request.version >= GST_RTSP_VERSION_2_0) {
1603 GST_ERROR_OBJECT (client, "Using PLAY request for keep-alive is forbidden"
1608 /* no body (or only '\0'), keep-alive request */
1609 send_generic_response (client, GST_RTSP_STS_OK, ctx);
1611 /* there is a body, handle the params */
1612 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
1613 if (res != GST_RTSP_OK)
1616 send_message (client, ctx, ctx->response, FALSE);
1619 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
1627 GST_ERROR ("client %p: pre signal returned error: %s", client,
1628 gst_rtsp_status_as_text (sig_result));
1629 send_generic_response (client, sig_result, ctx);
1634 GST_ERROR ("client %p: bad request", client);
1635 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1641 default_handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
1646 GstRTSPStatusCode sig_result;
1648 g_signal_emit (client,
1649 gst_rtsp_client_signals[SIGNAL_PRE_SET_PARAMETER_REQUEST], 0, ctx,
1651 if (sig_result != GST_RTSP_STS_OK) {
1655 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
1656 if (res != GST_RTSP_OK)
1659 if (size == 0 || !data || strlen ((char *) data) == 0) {
1660 /* no body (or only '\0'), keep-alive request */
1661 send_generic_response (client, GST_RTSP_STS_OK, ctx);
1663 /* there is a body, handle the params */
1664 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
1665 if (res != GST_RTSP_OK)
1668 send_message (client, ctx, ctx->response, FALSE);
1671 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
1679 GST_ERROR ("client %p: pre signal returned error: %s", client,
1680 gst_rtsp_status_as_text (sig_result));
1681 send_generic_response (client, sig_result, ctx);
1686 GST_ERROR ("client %p: bad request", client);
1687 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1693 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
1695 GstRTSPSession *session;
1696 GstRTSPClientClass *klass;
1697 GstRTSPSessionMedia *sessmedia;
1698 GstRTSPMedia *media;
1699 GstRTSPStatusCode code;
1700 GstRTSPState rtspstate;
1703 GstRTSPStatusCode sig_result;
1706 if (!(session = ctx->session))
1712 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1713 path = klass->make_path_from_uri (client, ctx->uri);
1715 /* get a handle to the configuration of the media in the session */
1716 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1720 if (path[matched] != '\0')
1725 media = gst_rtsp_session_media_get_media (sessmedia);
1726 g_object_ref (media);
1727 gst_rtsp_media_lock (media);
1728 n = gst_rtsp_media_n_streams (media);
1729 for (i = 0; i < n; i++) {
1730 GstRTSPStream *stream = gst_rtsp_media_get_stream (media, i);
1732 if (gst_rtsp_stream_get_publish_clock_mode (stream) ==
1733 GST_RTSP_PUBLISH_CLOCK_MODE_CLOCK_AND_OFFSET)
1737 ctx->sessmedia = sessmedia;
1739 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_PAUSE_REQUEST], 0,
1741 if (sig_result != GST_RTSP_STS_OK) {
1745 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1746 /* the session state must be playing or recording */
1747 if (rtspstate != GST_RTSP_STATE_PLAYING &&
1748 rtspstate != GST_RTSP_STATE_RECORDING)
1751 /* then pause sending */
1752 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
1754 /* construct the response now */
1755 code = GST_RTSP_STS_OK;
1756 gst_rtsp_message_init_response (ctx->response, code,
1757 gst_rtsp_status_as_text (code), ctx->request);
1759 send_message (client, ctx, ctx->response, FALSE);
1761 /* the state is now READY */
1762 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1764 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
1766 gst_rtsp_media_unlock (media);
1767 g_object_unref (media);
1774 GST_ERROR ("client %p: no session", client);
1775 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1780 GST_ERROR ("client %p: no uri supplied", client);
1781 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1786 GST_ERROR ("client %p: no media for uri", client);
1787 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1793 GST_ERROR ("client %p: no aggregate path %s", client, path);
1794 send_generic_response (client,
1795 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1801 GST_ERROR ("client %p: pre signal returned error: %s", client,
1802 gst_rtsp_status_as_text (sig_result));
1803 send_generic_response (client, sig_result, ctx);
1804 gst_rtsp_media_unlock (media);
1805 g_object_unref (media);
1810 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
1811 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1813 gst_rtsp_media_unlock (media);
1814 g_object_unref (media);
1819 GST_ERROR ("client %p: pausing not supported", client);
1820 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1821 gst_rtsp_media_unlock (media);
1822 g_object_unref (media);
1827 /* convert @url and @path to a URL used as a content base for the factory
1828 * located at @path */
1830 make_base_url (GstRTSPClient * client, GstRTSPUrl * url, const gchar * path)
1836 /* check for trailing '/' and append one */
1837 trail = (path[strlen (path) - 1] != '/' ? "/" : "");
1842 tmp.abspath = g_strdup_printf ("%s%s", path, trail);
1844 result = gst_rtsp_url_get_request_uri (&tmp);
1845 g_free (tmp.abspath);
1850 /* Check if the given header of type double is present and, if so,
1851 * put it's value in the supplied variable.
1853 static GstRTSPStatusCode
1854 parse_header_value_double (GstRTSPClient * client, GstRTSPContext * ctx,
1855 GstRTSPHeaderField header, gboolean * present, gdouble * value)
1861 res = gst_rtsp_message_get_header (ctx->request, header, &str, 0);
1862 if (res == GST_RTSP_OK) {
1863 *value = g_ascii_strtod (str, &end);
1865 goto parse_header_failed;
1867 GST_DEBUG ("client %p: got '%s', value %f", client,
1868 gst_rtsp_header_as_text (header), *value);
1874 return GST_RTSP_STS_OK;
1876 parse_header_failed:
1878 GST_ERROR ("client %p: failed parsing '%s' header", client,
1879 gst_rtsp_header_as_text (header));
1880 return GST_RTSP_STS_BAD_REQUEST;
1884 /* Parse scale and speed headers, if present, and set the rate to
1885 * (rate * scale * speed) */
1886 static GstRTSPStatusCode
1887 parse_scale_and_speed (GstRTSPClient * client, GstRTSPContext * ctx,
1888 gboolean * scale_present, gboolean * speed_present, gdouble * rate,
1889 GstSeekFlags * flags)
1891 gdouble scale = 1.0;
1892 gdouble speed = 1.0;
1893 GstRTSPStatusCode status;
1895 GST_DEBUG ("got rate %f", *rate);
1897 status = parse_header_value_double (client, ctx, GST_RTSP_HDR_SCALE,
1898 scale_present, &scale);
1899 if (status != GST_RTSP_STS_OK)
1902 if (*scale_present) {
1903 GST_DEBUG ("got Scale %f", scale);
1905 goto bad_scale_value;
1908 if (ABS (scale) != 1.0)
1909 *flags |= GST_SEEK_FLAG_TRICKMODE;
1912 GST_DEBUG ("rate after parsing Scale %f", *rate);
1914 status = parse_header_value_double (client, ctx, GST_RTSP_HDR_SPEED,
1915 speed_present, &speed);
1916 if (status != GST_RTSP_STS_OK)
1919 if (*speed_present) {
1920 GST_DEBUG ("got Speed %f", speed);
1922 goto bad_speed_value;
1926 GST_DEBUG ("rate after parsing Speed %f", *rate);
1932 GST_ERROR ("client %p: bad 'Scale' header value (%f)", client, scale);
1933 return GST_RTSP_STS_BAD_REQUEST;
1937 GST_ERROR ("client %p: bad 'Speed' header value (%f)", client, speed);
1938 return GST_RTSP_STS_BAD_REQUEST;
1942 static GstRTSPStatusCode
1943 setup_play_mode (GstRTSPClient * client, GstRTSPContext * ctx,
1944 GstRTSPRangeUnit * unit, gboolean * scale_present, gboolean * speed_present)
1948 GstRTSPTimeRange *range = NULL;
1950 GstSeekFlags flags = GST_SEEK_FLAG_NONE;
1951 GstRTSPClientClass *klass = GST_RTSP_CLIENT_GET_CLASS (client);
1952 GstRTSPStatusCode rtsp_status_code;
1953 GstClockTime trickmode_interval = 0;
1954 gboolean enable_rate_control = TRUE;
1956 /* parse the range header if we have one */
1957 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1958 if (res == GST_RTSP_OK) {
1959 gchar *seek_style = NULL;
1961 res = gst_rtsp_range_parse (str, &range);
1962 if (res != GST_RTSP_OK)
1963 goto parse_range_failed;
1965 *unit = range->unit;
1967 /* parse seek style header, if present */
1968 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_SEEK_STYLE,
1971 if (res == GST_RTSP_OK) {
1972 if (g_strcmp0 (seek_style, "RAP") == 0)
1973 flags = GST_SEEK_FLAG_ACCURATE;
1974 else if (g_strcmp0 (seek_style, "CoRAP") == 0)
1975 flags = GST_SEEK_FLAG_KEY_UNIT;
1976 else if (g_strcmp0 (seek_style, "First-Prior") == 0)
1977 flags = GST_SEEK_FLAG_KEY_UNIT & GST_SEEK_FLAG_SNAP_BEFORE;
1978 else if (g_strcmp0 (seek_style, "Next") == 0)
1979 flags = GST_SEEK_FLAG_KEY_UNIT & GST_SEEK_FLAG_SNAP_AFTER;
1981 GST_FIXME_OBJECT (client, "Add support for seek style %s", seek_style);
1982 } else if (range->min.type == GST_RTSP_TIME_END) {
1983 flags = GST_SEEK_FLAG_ACCURATE;
1985 flags = GST_SEEK_FLAG_KEY_UNIT;
1989 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_SEEK_STYLE,
1992 flags = GST_SEEK_FLAG_ACCURATE;
1995 /* check for scale and/or speed headers
1996 * we will set the seek rate to (speed * scale) and let the media decide
1997 * the resulting scale and speed. in the response we will use rate and applied
1998 * rate from the resulting segment as values for the speed and scale headers
2000 rtsp_status_code = parse_scale_and_speed (client, ctx, scale_present,
2001 speed_present, &rate, &flags);
2002 if (rtsp_status_code != GST_RTSP_STS_OK)
2003 goto scale_speed_failed;
2005 /* give the application a chance to tweak range, flags, or rate */
2006 if (klass->adjust_play_mode != NULL) {
2008 klass->adjust_play_mode (client, ctx, &range, &flags, &rate,
2009 &trickmode_interval, &enable_rate_control);
2010 if (rtsp_status_code != GST_RTSP_STS_OK)
2011 goto adjust_play_mode_failed;
2014 gst_rtsp_media_set_rate_control (ctx->media, enable_rate_control);
2016 /* now do the seek with the seek options */
2017 gst_rtsp_media_seek_trickmode (ctx->media, range, flags, rate,
2018 trickmode_interval);
2020 gst_rtsp_range_free (range);
2022 if (gst_rtsp_media_get_status (ctx->media) == GST_RTSP_MEDIA_STATUS_ERROR)
2025 return GST_RTSP_STS_OK;
2029 GST_ERROR ("client %p: failed parsing range header", client);
2030 return GST_RTSP_STS_BAD_REQUEST;
2035 gst_rtsp_range_free (range);
2036 GST_ERROR ("client %p: failed parsing Scale or Speed headers", client);
2037 return rtsp_status_code;
2039 adjust_play_mode_failed:
2041 GST_ERROR ("client %p: sub class returned bad code (%d)", client,
2044 gst_rtsp_range_free (range);
2045 return rtsp_status_code;
2049 GST_ERROR ("client %p: seek failed", client);
2050 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2055 default_handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
2057 GstRTSPSession *session;
2058 GstRTSPClientClass *klass;
2059 GstRTSPSessionMedia *sessmedia;
2060 GstRTSPMedia *media;
2061 GstRTSPStatusCode code;
2064 GstRTSPState rtspstate;
2065 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
2066 gchar *path, *rtpinfo = NULL;
2068 GstRTSPStatusCode sig_result;
2069 GPtrArray *transports;
2070 gboolean scale_present;
2071 gboolean speed_present;
2073 gdouble applied_rate;
2075 if (!(session = ctx->session))
2078 if (!(uri = ctx->uri))
2081 klass = GST_RTSP_CLIENT_GET_CLASS (client);
2082 path = klass->make_path_from_uri (client, uri);
2084 /* get a handle to the configuration of the media in the session */
2085 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
2089 if (path[matched] != '\0')
2094 ctx->sessmedia = sessmedia;
2095 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
2097 g_object_ref (media);
2098 gst_rtsp_media_lock (media);
2100 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_PLAY_REQUEST], 0,
2102 if (sig_result != GST_RTSP_STS_OK) {
2106 if (!(gst_rtsp_media_get_transport_mode (media) &
2107 GST_RTSP_TRANSPORT_MODE_PLAY))
2108 goto unsupported_mode;
2110 /* the session state must be playing or ready */
2111 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
2112 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
2115 /* update the pipeline */
2116 transports = gst_rtsp_session_media_get_transports (sessmedia);
2117 if (!gst_rtsp_media_complete_pipeline (media, transports)) {
2118 g_ptr_array_unref (transports);
2119 goto pipeline_error;
2121 g_ptr_array_unref (transports);
2123 /* in play we first unsuspend, media could be suspended from SDP or PAUSED */
2124 if (!gst_rtsp_media_unsuspend (media))
2125 goto unsuspend_failed;
2127 code = setup_play_mode (client, ctx, &unit, &scale_present, &speed_present);
2128 if (code != GST_RTSP_STS_OK)
2131 /* grab RTPInfo from the media now */
2132 if (gst_rtsp_media_has_completed_sender (media) &&
2133 !(rtpinfo = gst_rtsp_session_media_get_rtpinfo (sessmedia)))
2134 goto rtp_info_error;
2136 /* construct the response now */
2137 code = GST_RTSP_STS_OK;
2138 gst_rtsp_message_init_response (ctx->response, code,
2139 gst_rtsp_status_as_text (code), ctx->request);
2141 /* add the RTP-Info header */
2143 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO,
2147 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
2149 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
2151 if (gst_rtsp_media_has_completed_sender (media)) {
2152 /* the scale and speed headers must always be added if they were present in
2153 * the request. however, even if they were not, we still add them if
2154 * applied_rate or rate deviate from the "normal", i.e. 1.0 */
2155 if (!gst_rtsp_media_get_rates (media, &rate, &applied_rate))
2156 goto get_rates_error;
2157 g_assert (rate != 0 && applied_rate != 0);
2159 if (scale_present || applied_rate != 1.0)
2160 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_SCALE,
2161 g_strdup_printf ("%1.3f", applied_rate));
2163 if (speed_present || rate != 1.0)
2164 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_SPEED,
2165 g_strdup_printf ("%1.3f", rate));
2168 if (klass->adjust_play_response) {
2169 code = klass->adjust_play_response (client, ctx);
2170 if (code != GST_RTSP_STS_OK)
2171 goto adjust_play_response_failed;
2174 send_message (client, ctx, ctx->response, FALSE);
2176 /* start playing after sending the response */
2177 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
2179 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
2181 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
2183 gst_rtsp_media_unlock (media);
2184 g_object_unref (media);
2191 GST_ERROR ("client %p: no session", client);
2192 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2197 GST_ERROR ("client %p: no uri supplied", client);
2198 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2203 GST_ERROR ("client %p: media not found", client);
2204 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2209 GST_ERROR ("client %p: no aggregate path %s", client, path);
2210 send_generic_response (client,
2211 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
2217 GST_ERROR ("client %p: pre signal returned error: %s", client,
2218 gst_rtsp_status_as_text (sig_result));
2219 send_generic_response (client, sig_result, ctx);
2220 gst_rtsp_media_unlock (media);
2221 g_object_unref (media);
2226 GST_ERROR ("client %p: not PLAYING or READY", client);
2227 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
2229 gst_rtsp_media_unlock (media);
2230 g_object_unref (media);
2235 GST_ERROR ("client %p: failed to configure the pipeline", client);
2236 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
2238 gst_rtsp_media_unlock (media);
2239 g_object_unref (media);
2244 GST_ERROR ("client %p: unsuspend failed", client);
2245 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2246 gst_rtsp_media_unlock (media);
2247 g_object_unref (media);
2252 GST_ERROR ("client %p: seek failed", client);
2253 send_generic_response (client, code, ctx);
2254 gst_rtsp_media_unlock (media);
2255 g_object_unref (media);
2260 GST_ERROR ("client %p: media does not support PLAY", client);
2261 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_ALLOWED, ctx);
2262 gst_rtsp_media_unlock (media);
2263 g_object_unref (media);
2268 GST_ERROR ("client %p: failed obtaining rate and applied_rate", client);
2269 send_generic_response (client, GST_RTSP_STS_INTERNAL_SERVER_ERROR, ctx);
2270 gst_rtsp_media_unlock (media);
2271 g_object_unref (media);
2274 adjust_play_response_failed:
2276 GST_ERROR ("client %p: failed to adjust play response", client);
2277 send_generic_response (client, code, ctx);
2278 gst_rtsp_media_unlock (media);
2279 g_object_unref (media);
2284 GST_ERROR ("client %p: failed to add RTP-Info", client);
2285 send_generic_response (client, GST_RTSP_STS_INTERNAL_SERVER_ERROR, ctx);
2286 gst_rtsp_media_unlock (media);
2287 g_object_unref (media);
2293 do_keepalive (GstRTSPSession * session)
2295 GST_INFO ("keep session %p alive", session);
2296 gst_rtsp_session_touch (session);
2299 /* parse @transport and return a valid transport in @tr. only transports
2300 * supported by @stream are returned. Returns FALSE if no valid transport
2303 parse_transport (const char *transport, GstRTSPStream * stream,
2304 GstRTSPTransport * tr)
2311 gst_rtsp_transport_init (tr);
2313 GST_DEBUG ("parsing transports %s", transport);
2315 transports = g_strsplit (transport, ",", 0);
2317 /* loop through the transports, try to parse */
2318 for (i = 0; transports[i]; i++) {
2319 g_strstrip (transports[i]);
2320 res = gst_rtsp_transport_parse (transports[i], tr);
2321 if (res != GST_RTSP_OK) {
2322 /* no valid transport, search some more */
2323 GST_WARNING ("could not parse transport %s", transports[i]);
2327 /* we have a transport, see if it's supported */
2328 if (!gst_rtsp_stream_is_transport_supported (stream, tr)) {
2329 GST_WARNING ("unsupported transport %s", transports[i]);
2333 /* we have a valid transport */
2334 GST_INFO ("found valid transport %s", transports[i]);
2339 gst_rtsp_transport_init (tr);
2341 g_strfreev (transports);
2347 default_configure_client_media (GstRTSPClient * client, GstRTSPMedia * media,
2348 GstRTSPStream * stream, GstRTSPContext * ctx)
2350 GstRTSPMessage *request = ctx->request;
2351 gchar *blocksize_str;
2353 if (!gst_rtsp_stream_is_sender (stream))
2356 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
2357 &blocksize_str, 0) == GST_RTSP_OK) {
2361 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
2362 if (end == blocksize_str)
2365 /* we don't want to change the mtu when this media
2366 * can be shared because it impacts other clients */
2367 if (gst_rtsp_media_is_shared (media))
2370 if (blocksize > G_MAXUINT)
2371 blocksize = G_MAXUINT;
2373 gst_rtsp_stream_set_mtu (stream, blocksize);
2381 GST_ERROR_OBJECT (client, "failed to parse blocksize");
2382 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2388 default_configure_client_transport (GstRTSPClient * client,
2389 GstRTSPContext * ctx, GstRTSPTransport * ct)
2391 GstRTSPClientPrivate *priv = client->priv;
2393 /* we have a valid transport now, set the destination of the client. */
2394 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST ||
2395 ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP) {
2396 /* allocate UDP ports */
2397 GSocketFamily family;
2398 gboolean use_client_settings = FALSE;
2400 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
2402 if ((ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) &&
2403 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS) &&
2404 (ct->destination != NULL)) {
2406 if (!gst_rtsp_stream_verify_mcast_ttl (ctx->stream, ct->ttl))
2409 use_client_settings = TRUE;
2412 /* We need to allocate the sockets for both families before starting
2413 * multiudpsink, otherwise multiudpsink won't accept new clients with
2414 * a different family.
2416 /* FIXME: could be more adequately solved by making it possible
2417 * to set a socket on multiudpsink after it has already been started */
2418 if (!gst_rtsp_stream_allocate_udp_sockets (ctx->stream,
2419 G_SOCKET_FAMILY_IPV4, ct, use_client_settings)
2420 && family == G_SOCKET_FAMILY_IPV4)
2421 goto error_allocating_ports;
2423 if (!gst_rtsp_stream_allocate_udp_sockets (ctx->stream,
2424 G_SOCKET_FAMILY_IPV6, ct, use_client_settings)
2425 && family == G_SOCKET_FAMILY_IPV6)
2426 goto error_allocating_ports;
2428 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2429 if (use_client_settings) {
2430 /* FIXME: the address has been successfully allocated, however, in
2431 * the use_client_settings case we need to verify that the allocated
2432 * address is the one requested by the client and if this address is
2433 * an allowed destination. Verifying this via the address pool in not
2434 * the proper way as the address pool should only be used for choosing
2435 * the server-selected address/port pairs. */
2436 GSocket *rtp_socket;
2440 gst_rtsp_stream_get_rtp_multicast_socket (ctx->stream, family);
2441 if (rtp_socket == NULL)
2443 ttl = g_socket_get_multicast_ttl (rtp_socket);
2444 g_object_unref (rtp_socket);
2445 if (ct->ttl < ttl) {
2446 /* use the maximum ttl that is requested by multicast clients */
2447 GST_DEBUG ("requested ttl %u, but keeping ttl %u", ct->ttl, ttl);
2452 GstRTSPAddress *addr = NULL;
2454 g_free (ct->destination);
2455 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
2458 ct->destination = g_strdup (addr->address);
2459 ct->port.min = addr->port;
2460 ct->port.max = addr->port + addr->n_ports - 1;
2461 ct->ttl = addr->ttl;
2462 gst_rtsp_address_free (addr);
2465 if (!gst_rtsp_stream_add_multicast_client_address (ctx->stream,
2466 ct->destination, ct->port.min, ct->port.max, family))
2467 goto error_mcast_transport;
2472 url = gst_rtsp_connection_get_url (priv->connection);
2473 g_free (ct->destination);
2474 ct->destination = g_strdup (url->host);
2479 url = gst_rtsp_connection_get_url (priv->connection);
2480 g_free (ct->destination);
2481 ct->destination = g_strdup (url->host);
2483 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
2485 GSocketAddress *addr;
2487 sock = gst_rtsp_connection_get_read_socket (priv->connection);
2488 if ((addr = g_socket_get_remote_address (sock, NULL))) {
2489 /* our read port is the sender port of client */
2490 ct->client_port.min =
2491 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
2492 g_object_unref (addr);
2494 if ((addr = g_socket_get_local_address (sock, NULL))) {
2495 ct->server_port.max =
2496 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
2497 g_object_unref (addr);
2499 sock = gst_rtsp_connection_get_write_socket (priv->connection);
2500 if ((addr = g_socket_get_remote_address (sock, NULL))) {
2501 /* our write port is the receiver port of client */
2502 ct->client_port.max =
2503 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
2504 g_object_unref (addr);
2506 if ((addr = g_socket_get_local_address (sock, NULL))) {
2507 ct->server_port.min =
2508 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
2509 g_object_unref (addr);
2511 /* check if the client selected channels for TCP */
2512 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
2513 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
2516 /* alloc new channels if they are already taken */
2517 while (g_hash_table_contains (priv->transports,
2518 GINT_TO_POINTER (ct->interleaved.min))
2519 || g_hash_table_contains (priv->transports,
2520 GINT_TO_POINTER (ct->interleaved.max))) {
2521 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
2523 if (ct->interleaved.max > 255)
2524 goto error_allocating_channels;
2533 GST_ERROR_OBJECT (client,
2534 "Failed to allocate UDP ports: invalid ttl value");
2537 error_allocating_ports:
2539 GST_ERROR_OBJECT (client, "Failed to allocate UDP ports");
2544 GST_ERROR_OBJECT (client, "Failed to acquire address for stream");
2549 GST_ERROR_OBJECT (client, "Failed to get UDP socket");
2552 error_mcast_transport:
2554 GST_ERROR_OBJECT (client, "Failed to add multicast client transport");
2557 error_allocating_channels:
2559 GST_ERROR_OBJECT (client, "Failed to allocate interleaved channels");
2564 static GstRTSPTransport *
2565 make_server_transport (GstRTSPClient * client, GstRTSPMedia * media,
2566 GstRTSPContext * ctx, GstRTSPTransport * ct)
2568 GstRTSPTransport *st;
2570 GSocketFamily family;
2572 /* prepare the server transport */
2573 gst_rtsp_transport_new (&st);
2575 st->trans = ct->trans;
2576 st->profile = ct->profile;
2577 st->lower_transport = ct->lower_transport;
2578 st->mode_play = ct->mode_play;
2579 st->mode_record = ct->mode_record;
2581 addr = g_inet_address_new_from_string (ct->destination);
2584 GST_ERROR ("failed to get inet addr from client destination");
2585 family = G_SOCKET_FAMILY_IPV4;
2587 family = g_inet_address_get_family (addr);
2588 g_object_unref (addr);
2592 switch (st->lower_transport) {
2593 case GST_RTSP_LOWER_TRANS_UDP:
2594 st->client_port = ct->client_port;
2595 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
2597 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
2598 st->port = ct->port;
2599 st->destination = g_strdup (ct->destination);
2602 case GST_RTSP_LOWER_TRANS_TCP:
2603 st->interleaved = ct->interleaved;
2604 st->client_port = ct->client_port;
2605 st->server_port = ct->server_port;
2610 if ((gst_rtsp_media_get_transport_mode (media) &
2611 GST_RTSP_TRANSPORT_MODE_PLAY))
2612 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
2618 rtsp_ctrl_timeout_remove_unlocked (GstRTSPClientPrivate * priv)
2620 if (priv->rtsp_ctrl_timeout != NULL) {
2621 GST_DEBUG ("rtsp control session removed timeout %p.",
2622 priv->rtsp_ctrl_timeout);
2623 g_source_destroy (priv->rtsp_ctrl_timeout);
2624 g_source_unref (priv->rtsp_ctrl_timeout);
2625 priv->rtsp_ctrl_timeout = NULL;
2626 priv->rtsp_ctrl_timeout_cnt = 0;
2631 rtsp_ctrl_timeout_remove (GstRTSPClient * client)
2633 g_mutex_lock (&client->priv->lock);
2634 rtsp_ctrl_timeout_remove_unlocked (client->priv);
2635 g_mutex_unlock (&client->priv->lock);
2639 rtsp_ctrl_timeout_destroy_notify (gpointer user_data)
2641 GWeakRef *client_weak_ref = (GWeakRef *) user_data;
2643 g_weak_ref_clear (client_weak_ref);
2644 g_free (client_weak_ref);
2648 rtsp_ctrl_timeout_cb (gpointer user_data)
2650 gboolean res = G_SOURCE_CONTINUE;
2651 GstRTSPClientPrivate *priv;
2652 GWeakRef *client_weak_ref = (GWeakRef *) user_data;
2653 GstRTSPClient *client = (GstRTSPClient *) g_weak_ref_get (client_weak_ref);
2655 if (client == NULL) {
2656 return G_SOURCE_REMOVE;
2659 priv = client->priv;
2660 g_mutex_lock (&priv->lock);
2661 priv->rtsp_ctrl_timeout_cnt += RTSP_CTRL_CB_INTERVAL;
2663 if ((priv->rtsp_ctrl_timeout_cnt > RTSP_CTRL_TIMEOUT_VALUE)
2664 || (priv->had_session
2665 && priv->rtsp_ctrl_timeout_cnt > priv->post_session_timeout)) {
2666 GST_DEBUG ("rtsp control session timeout %p expired, closing client.",
2667 priv->rtsp_ctrl_timeout);
2668 rtsp_ctrl_timeout_remove_unlocked (client->priv);
2670 res = G_SOURCE_REMOVE;
2673 g_mutex_unlock (&priv->lock);
2675 if (res == G_SOURCE_REMOVE) {
2676 gst_rtsp_client_close (client);
2679 g_object_unref (client);
2685 stream_make_keymgmt (GstRTSPClient * client, const gchar * location,
2686 GstRTSPStream * stream)
2688 gchar *base64, *result = NULL;
2689 GstMIKEYMessage *mikey_msg;
2690 GstCaps *srtcpparams;
2691 GstElement *rtcp_encoder;
2692 gint srtcp_cipher, srtp_cipher;
2693 gint srtcp_auth, srtp_auth;
2695 GType ciphertype, authtype;
2696 GEnumClass *cipher_enum, *auth_enum;
2697 GEnumValue *srtcp_cipher_value, *srtp_cipher_value, *srtcp_auth_value,
2700 rtcp_encoder = gst_rtsp_stream_get_srtp_encoder (stream);
2705 ciphertype = g_type_from_name ("GstSrtpCipherType");
2706 authtype = g_type_from_name ("GstSrtpAuthType");
2708 cipher_enum = g_type_class_ref (ciphertype);
2709 auth_enum = g_type_class_ref (authtype);
2711 /* We need to bring the encoder to READY so that it generates its key */
2712 gst_element_set_state (rtcp_encoder, GST_STATE_READY);
2714 g_object_get (rtcp_encoder, "rtcp-cipher", &srtcp_cipher, "rtcp-auth",
2715 &srtcp_auth, "rtp-cipher", &srtp_cipher, "rtp-auth", &srtp_auth, "key",
2717 g_object_unref (rtcp_encoder);
2719 srtcp_cipher_value = g_enum_get_value (cipher_enum, srtcp_cipher);
2720 srtp_cipher_value = g_enum_get_value (cipher_enum, srtp_cipher);
2721 srtcp_auth_value = g_enum_get_value (auth_enum, srtcp_auth);
2722 srtp_auth_value = g_enum_get_value (auth_enum, srtp_auth);
2724 g_type_class_unref (cipher_enum);
2725 g_type_class_unref (auth_enum);
2727 srtcpparams = gst_caps_new_simple ("application/x-srtcp",
2728 "srtcp-cipher", G_TYPE_STRING, srtcp_cipher_value->value_nick,
2729 "srtcp-auth", G_TYPE_STRING, srtcp_auth_value->value_nick,
2730 "srtp-cipher", G_TYPE_STRING, srtp_cipher_value->value_nick,
2731 "srtp-auth", G_TYPE_STRING, srtp_auth_value->value_nick,
2732 "srtp-key", GST_TYPE_BUFFER, key, NULL);
2734 mikey_msg = gst_mikey_message_new_from_caps (srtcpparams);
2738 gst_rtsp_stream_get_ssrc (stream, &send_ssrc);
2739 gst_mikey_message_add_cs_srtp (mikey_msg, 0, send_ssrc, 0);
2741 base64 = gst_mikey_message_base64_encode (mikey_msg);
2742 gst_mikey_message_unref (mikey_msg);
2745 result = gst_sdp_make_keymgmt (location, base64);
2755 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
2757 GstRTSPClientPrivate *priv = client->priv;
2760 gchar *transport, *keymgmt;
2761 GstRTSPTransport *ct, *st;
2762 GstRTSPStatusCode code;
2763 GstRTSPSession *session;
2764 GstRTSPStreamTransport *trans;
2766 GstRTSPSessionMedia *sessmedia;
2767 GstRTSPMedia *media;
2768 GstRTSPStream *stream;
2769 GstRTSPState rtspstate;
2770 GstRTSPClientClass *klass;
2771 gchar *path, *control = NULL;
2773 gboolean new_session = FALSE;
2774 GstRTSPStatusCode sig_result;
2775 gchar *pipelined_request_id = NULL, *accept_range = NULL;
2781 klass = GST_RTSP_CLIENT_GET_CLASS (client);
2782 path = klass->make_path_from_uri (client, uri);
2784 /* parse the transport */
2786 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
2788 if (res != GST_RTSP_OK)
2791 /* Handle Pipelined-requests if using >= 2.0 */
2792 if (ctx->request->type_data.request.version >= GST_RTSP_VERSION_2_0)
2793 gst_rtsp_message_get_header (ctx->request,
2794 GST_RTSP_HDR_PIPELINED_REQUESTS, &pipelined_request_id, 0);
2796 /* we create the session after parsing stuff so that we don't make
2797 * a session for malformed requests */
2798 if (priv->session_pool == NULL)
2801 session = ctx->session;
2804 g_object_ref (session);
2805 /* get a handle to the configuration of the media in the session, this can
2806 * return NULL if this is a new url to manage in this session. */
2807 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
2809 /* we need a new media configuration in this session */
2813 /* we have no session media, find one and manage it */
2814 if (sessmedia == NULL) {
2815 /* get a handle to the configuration of the media in the session */
2816 media = find_media (client, ctx, path, &matched);
2817 /* need to suspend the media, if the protocol has changed */
2818 if (media != NULL) {
2819 gst_rtsp_media_lock (media);
2820 gst_rtsp_media_suspend (media);
2823 if ((media = gst_rtsp_session_media_get_media (sessmedia))) {
2824 g_object_ref (media);
2825 gst_rtsp_media_lock (media);
2827 goto media_not_found;
2830 /* no media, not found then */
2832 goto media_not_found_no_reply;
2834 if (path[matched] == '\0') {
2835 if (gst_rtsp_media_n_streams (media) == 1) {
2836 stream = gst_rtsp_media_get_stream (media, 0);
2838 goto control_not_found;
2841 /* path is what matched. */
2842 gchar *newpath = g_strndup (path, matched);
2843 /* control is remainder */
2844 if (matched == 1 && path[0] == '/')
2845 control = g_strdup (&path[1]);
2847 control = g_strdup (&path[matched + 1]);
2852 /* find the stream now using the control part */
2853 stream = gst_rtsp_media_find_stream (media, control);
2857 goto stream_not_found;
2859 /* now we have a uri identifying a valid media and stream */
2860 ctx->stream = stream;
2863 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_SETUP_REQUEST], 0,
2865 if (sig_result != GST_RTSP_STS_OK) {
2869 if (session == NULL) {
2870 /* create a session if this fails we probably reached our session limit or
2872 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
2873 goto service_unavailable;
2875 /* Pipelined requests should be cleared between sessions */
2876 g_hash_table_remove_all (priv->pipelined_requests);
2878 /* make sure this client is closed when the session is closed */
2879 client_watch_session (client, session);
2882 /* signal new session */
2883 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
2886 ctx->session = session;
2889 if (pipelined_request_id) {
2890 g_hash_table_insert (client->priv->pipelined_requests,
2891 g_strdup (pipelined_request_id),
2892 g_strdup (gst_rtsp_session_get_sessionid (session)));
2894 /* Remember that we had at least one session in the past */
2895 priv->had_session = TRUE;
2896 rtsp_ctrl_timeout_remove (client);
2898 if (!klass->configure_client_media (client, media, stream, ctx))
2899 goto configure_media_failed_no_reply;
2901 gst_rtsp_transport_new (&ct);
2903 /* parse and find a usable supported transport */
2904 if (!parse_transport (transport, stream, ct))
2905 goto unsupported_transports;
2908 && !(gst_rtsp_media_get_transport_mode (media) &
2909 GST_RTSP_TRANSPORT_MODE_PLAY)) || (ct->mode_record
2910 && !(gst_rtsp_media_get_transport_mode (media) &
2911 GST_RTSP_TRANSPORT_MODE_RECORD)))
2912 goto unsupported_mode;
2914 /* parse the keymgmt */
2915 if (gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_KEYMGMT,
2916 &keymgmt, 0) == GST_RTSP_OK) {
2917 if (!gst_rtsp_stream_handle_keymgmt (ctx->stream, keymgmt))
2921 if (gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT_RANGES,
2922 &accept_range, 0) == GST_RTSP_OK) {
2923 GEnumValue *runit = NULL;
2925 gchar **valid_ranges;
2926 GEnumClass *runit_class = g_type_class_ref (GST_TYPE_RTSP_RANGE_UNIT);
2928 gst_rtsp_message_dump (ctx->request);
2929 valid_ranges = g_strsplit (accept_range, ",", -1);
2931 for (i = 0; valid_ranges[i]; i++) {
2932 gchar *range = valid_ranges[i];
2934 while (*range == ' ')
2937 runit = g_enum_get_value_by_nick (runit_class, range);
2941 g_strfreev (valid_ranges);
2942 g_type_class_unref (runit_class);
2945 goto unsupported_range_unit;
2948 if (sessmedia == NULL) {
2949 /* manage the media in our session now, if not done already */
2951 gst_rtsp_session_manage_media (session, path, g_object_ref (media));
2952 /* if we stil have no media, error */
2953 if (sessmedia == NULL)
2954 goto sessmedia_unavailable;
2956 /* don't cache media anymore */
2957 clean_cached_media (client, FALSE);
2960 ctx->sessmedia = sessmedia;
2962 /* update the client transport */
2963 if (!klass->configure_client_transport (client, ctx, ct))
2964 goto unsupported_client_transport;
2966 /* set in the session media transport */
2967 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
2971 /* configure the url used to set this transport, this we will use when
2972 * generating the response for the PLAY request */
2973 gst_rtsp_stream_transport_set_url (trans, uri);
2974 /* configure keepalive for this transport */
2975 gst_rtsp_stream_transport_set_keepalive (trans,
2976 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
2978 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2979 /* our callbacks to send data on this TCP connection */
2980 gst_rtsp_stream_transport_set_callbacks (trans,
2981 (GstRTSPSendFunc) do_send_data,
2982 (GstRTSPSendFunc) do_send_data, client, NULL);
2983 gst_rtsp_stream_transport_set_list_callbacks (trans,
2984 (GstRTSPSendListFunc) do_send_data_list,
2985 (GstRTSPSendListFunc) do_send_data_list, client, NULL);
2987 gst_rtsp_stream_transport_set_back_pressure_callback (trans,
2988 (GstRTSPBackPressureFunc) do_check_back_pressure, client, NULL);
2990 g_hash_table_insert (priv->transports,
2991 GINT_TO_POINTER (ct->interleaved.min), trans);
2992 g_object_ref (trans);
2993 g_hash_table_insert (priv->transports,
2994 GINT_TO_POINTER (ct->interleaved.max), trans);
2995 g_object_ref (trans);
2996 add_data_seq (client, ct->interleaved.min);
2997 add_data_seq (client, ct->interleaved.max);
3000 /* create and serialize the server transport */
3001 st = make_server_transport (client, media, ctx, ct);
3002 trans_str = gst_rtsp_transport_as_text (st);
3004 /* FIXME-WFD : Temporarily force to set profile string */
3005 trans_str = g_strjoinv ("RTP/AVP/UDP", g_strsplit (trans_str, "RTP/AVP", -1));
3007 gst_rtsp_transport_free (st);
3009 /* construct the response now */
3010 code = GST_RTSP_STS_OK;
3011 gst_rtsp_message_init_response (ctx->response, code,
3012 gst_rtsp_status_as_text (code), ctx->request);
3014 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
3018 if (pipelined_request_id)
3019 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PIPELINED_REQUESTS,
3020 pipelined_request_id);
3022 if (ctx->request->type_data.request.version >= GST_RTSP_VERSION_2_0) {
3023 GstClockTimeDiff seekable = gst_rtsp_media_seekable (media);
3024 GString *media_properties = g_string_new (NULL);
3027 g_string_append (media_properties,
3028 "No-Seeking,Time-Progressing,Time-Duration=0.0");
3029 else if (seekable == 0)
3030 g_string_append (media_properties, "Beginning-Only");
3031 else if (seekable == G_MAXINT64)
3032 g_string_append (media_properties, "Random-Access");
3034 g_string_append_printf (media_properties,
3035 "Random-Access=%f, Unlimited, Immutable",
3036 (gdouble) seekable / GST_SECOND);
3038 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_MEDIA_PROPERTIES,
3039 media_properties->str);
3040 g_string_free (media_properties, TRUE);
3041 /* TODO Check how Accept-Ranges should be filled */
3042 gst_rtsp_message_add_header (ctx->request, GST_RTSP_HDR_ACCEPT_RANGES,
3043 "npt, clock, smpte, clock");
3046 send_message (client, ctx, ctx->response, FALSE);
3048 /* update the state */
3049 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
3050 switch (rtspstate) {
3051 case GST_RTSP_STATE_PLAYING:
3052 case GST_RTSP_STATE_RECORDING:
3053 case GST_RTSP_STATE_READY:
3054 /* no state change */
3057 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
3061 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
3063 gst_rtsp_media_unlock (media);
3064 g_object_unref (media);
3065 g_object_unref (session);
3074 GST_ERROR ("client %p: no uri", client);
3075 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
3080 GST_ERROR ("client %p: no transport", client);
3081 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
3086 GST_ERROR ("client %p: no session pool configured", client);
3087 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
3090 media_not_found_no_reply:
3092 GST_ERROR ("client %p: media '%s' not found", client, path);
3093 /* error reply is already sent */
3094 goto cleanup_session;
3098 GST_ERROR ("client %p: media '%s' not found", client, path);
3099 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
3100 goto cleanup_session;
3104 GST_ERROR ("client %p: no control in path '%s'", client, path);
3105 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
3106 gst_rtsp_media_unlock (media);
3107 g_object_unref (media);
3108 goto cleanup_session;
3112 GST_ERROR ("client %p: stream '%s' not found", client,
3113 GST_STR_NULL (control));
3114 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
3115 gst_rtsp_media_unlock (media);
3116 g_object_unref (media);
3117 goto cleanup_session;
3121 GST_ERROR ("client %p: pre signal returned error: %s", client,
3122 gst_rtsp_status_as_text (sig_result));
3123 send_generic_response (client, sig_result, ctx);
3124 gst_rtsp_media_unlock (media);
3125 g_object_unref (media);
3128 service_unavailable:
3130 GST_ERROR ("client %p: can't create session", client);
3131 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
3132 gst_rtsp_media_unlock (media);
3133 g_object_unref (media);
3134 goto cleanup_session;
3136 sessmedia_unavailable:
3138 GST_ERROR ("client %p: can't create session media", client);
3139 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
3140 goto cleanup_transport;
3142 configure_media_failed_no_reply:
3144 GST_ERROR ("client %p: configure_media failed", client);
3145 gst_rtsp_media_unlock (media);
3146 g_object_unref (media);
3147 /* error reply is already sent */
3148 goto cleanup_session;
3150 unsupported_transports:
3152 GST_ERROR ("client %p: unsupported transports", client);
3153 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
3154 goto cleanup_transport;
3156 unsupported_client_transport:
3158 GST_ERROR ("client %p: unsupported client transport", client);
3159 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
3160 goto cleanup_transport;
3164 GST_ERROR ("client %p: unsupported mode (media play: %d, media record: %d, "
3165 "mode play: %d, mode record: %d)", client,
3166 ! !(gst_rtsp_media_get_transport_mode (media) &
3167 GST_RTSP_TRANSPORT_MODE_PLAY),
3168 ! !(gst_rtsp_media_get_transport_mode (media) &
3169 GST_RTSP_TRANSPORT_MODE_RECORD), ct->mode_play, ct->mode_record);
3170 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
3171 goto cleanup_transport;
3173 unsupported_range_unit:
3175 GST_ERROR ("Client %p: does not support any range format we support",
3177 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
3178 goto cleanup_transport;
3182 GST_ERROR ("client %p: keymgmt error", client);
3183 send_generic_response (client, GST_RTSP_STS_KEY_MANAGEMENT_FAILURE, ctx);
3184 goto cleanup_transport;
3188 gst_rtsp_transport_free (ct);
3190 gst_rtsp_media_unlock (media);
3191 g_object_unref (media);
3195 gst_rtsp_session_pool_remove (priv->session_pool, session);
3197 g_object_unref (session);
3205 static GstSDPMessage *
3206 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
3208 GstRTSPClientPrivate *priv = client->priv;
3212 guint64 session_id_tmp;
3213 gchar session_id[21];
3215 gst_sdp_message_new (&sdp);
3217 /* some standard things first */
3218 gst_sdp_message_set_version (sdp, "0");
3225 session_id_tmp = (((guint64) g_random_int ()) << 32) | g_random_int ();
3226 g_snprintf (session_id, sizeof (session_id), "%" G_GUINT64_FORMAT,
3229 gst_sdp_message_set_origin (sdp, "-", session_id, "1", "IN", proto,
3232 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
3233 gst_sdp_message_set_information (sdp, "rtsp-server");
3234 gst_sdp_message_add_time (sdp, "0", "0", NULL);
3235 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
3236 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
3237 gst_sdp_message_add_attribute (sdp, "control", "*");
3239 info.is_ipv6 = priv->is_ipv6;
3240 info.server_ip = priv->server_ip;
3242 /* create an SDP for the media object */
3243 if (!gst_rtsp_media_setup_sdp (media, sdp, &info))
3251 GST_ERROR ("client %p: could not create SDP", client);
3252 gst_sdp_message_free (sdp);
3257 /* for the describe we must generate an SDP */
3259 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
3261 GstRTSPClientPrivate *priv = client->priv;
3266 GstRTSPMedia *media;
3267 GstRTSPClientClass *klass;
3268 GstRTSPStatusCode sig_result;
3270 klass = GST_RTSP_CLIENT_GET_CLASS (client);
3275 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_DESCRIBE_REQUEST],
3276 0, ctx, &sig_result);
3277 if (sig_result != GST_RTSP_STS_OK) {
3281 /* check what kind of format is accepted, we don't really do anything with it
3282 * and always return SDP for now. */
3287 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
3289 if (res == GST_RTSP_ENOTIMPL)
3292 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
3296 if (!priv->mount_points)
3297 goto no_mount_points;
3299 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
3302 /* find the media object for the uri */
3303 if (!(media = find_media (client, ctx, path, NULL)))
3306 gst_rtsp_media_lock (media);
3308 if (!(gst_rtsp_media_get_transport_mode (media) &
3309 GST_RTSP_TRANSPORT_MODE_PLAY))
3310 goto unsupported_mode;
3312 /* create an SDP for the media object on this client */
3313 if (!(sdp = klass->create_sdp (client, media)))
3316 /* we suspend after the describe */
3317 gst_rtsp_media_suspend (media);
3319 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
3320 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
3322 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
3325 /* content base for some clients that might screw up creating the setup uri */
3326 str = make_base_url (client, ctx->uri, path);
3329 GST_INFO ("adding content-base: %s", str);
3330 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE, str);
3332 /* add SDP to the response body */
3333 str = gst_sdp_message_as_text (sdp);
3334 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
3335 gst_sdp_message_free (sdp);
3337 send_message (client, ctx, ctx->response, FALSE);
3339 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
3342 gst_rtsp_media_unlock (media);
3343 g_object_unref (media);
3350 GST_ERROR ("client %p: pre signal returned error: %s", client,
3351 gst_rtsp_status_as_text (sig_result));
3352 send_generic_response (client, sig_result, ctx);
3357 GST_ERROR ("client %p: no uri", client);
3358 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
3363 GST_ERROR ("client %p: no mount points configured", client);
3364 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
3369 GST_ERROR ("client %p: can't find path for url", client);
3370 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
3375 GST_ERROR ("client %p: no media", client);
3377 /* error reply is already sent */
3382 GST_ERROR ("client %p: media does not support DESCRIBE", client);
3383 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_ALLOWED, ctx);
3385 gst_rtsp_media_unlock (media);
3386 g_object_unref (media);
3391 GST_ERROR ("client %p: can't create SDP", client);
3392 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
3394 gst_rtsp_media_unlock (media);
3395 g_object_unref (media);
3401 handle_sdp (GstRTSPClient * client, GstRTSPContext * ctx, GstRTSPMedia * media,
3402 GstSDPMessage * sdp)
3404 GstRTSPClientPrivate *priv = client->priv;
3405 GstRTSPThread *thread;
3407 /* create an SDP for the media object */
3408 if (!gst_rtsp_media_handle_sdp (media, sdp))
3411 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
3412 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
3416 /* prepare the media */
3417 if (!gst_rtsp_media_prepare (media, thread))
3425 GST_ERROR ("client %p: could not handle SDP", client);
3430 GST_ERROR ("client %p: can't create thread", client);
3435 GST_ERROR ("client %p: can't prepare media", client);
3441 handle_announce_request (GstRTSPClient * client, GstRTSPContext * ctx)
3443 GstRTSPClientPrivate *priv = client->priv;
3444 GstRTSPClientClass *klass;
3447 GstRTSPMedia *media;
3448 gchar *path, *cont = NULL;
3451 GstRTSPStatusCode sig_result;
3454 klass = GST_RTSP_CLIENT_GET_CLASS (client);
3459 if (!priv->mount_points)
3460 goto no_mount_points;
3462 /* check if reply is SDP */
3463 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_CONTENT_TYPE, &cont,
3465 /* could not be set but since the request returned OK, we assume it
3466 * was SDP, else check it. */
3468 if (g_ascii_strcasecmp (cont, "application/sdp") != 0)
3469 goto wrong_content_type;
3472 /* get message body and parse as SDP */
3473 gst_rtsp_message_get_body (ctx->request, &data, &size);
3474 if (data == NULL || size == 0)
3477 GST_DEBUG ("client %p: parse SDP...", client);
3478 gst_sdp_message_new (&sdp);
3479 sres = gst_sdp_message_parse_buffer (data, size, sdp);
3480 if (sres != GST_SDP_OK)
3481 goto sdp_parse_failed;
3483 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
3486 /* find the media object for the uri */
3487 if (!(media = find_media (client, ctx, path, NULL)))
3491 gst_rtsp_media_lock (media);
3493 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_ANNOUNCE_REQUEST],
3494 0, ctx, &sig_result);
3495 if (sig_result != GST_RTSP_STS_OK) {
3499 if (!(gst_rtsp_media_get_transport_mode (media) &
3500 GST_RTSP_TRANSPORT_MODE_RECORD))
3501 goto unsupported_mode;
3503 /* Tell client subclass about the media */
3504 if (!klass->handle_sdp (client, ctx, media, sdp))
3507 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
3508 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
3510 n_streams = gst_rtsp_media_n_streams (media);
3511 for (i = 0; i < n_streams; i++) {
3512 GstRTSPStream *stream = gst_rtsp_media_get_stream (media, i);
3513 gchar *uri, *location, *keymgmt;
3515 uri = gst_rtsp_url_get_request_uri (ctx->uri);
3516 location = g_strdup_printf ("%s/stream=%d", uri, i);
3517 keymgmt = stream_make_keymgmt (client, location, stream);
3520 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_KEYMGMT,
3527 /* we suspend after the announce */
3528 gst_rtsp_media_suspend (media);
3530 send_message (client, ctx, ctx->response, FALSE);
3532 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_ANNOUNCE_REQUEST],
3535 gst_sdp_message_free (sdp);
3537 gst_rtsp_media_unlock (media);
3538 g_object_unref (media);
3544 GST_ERROR ("client %p: no uri", client);
3545 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
3550 GST_ERROR ("client %p: no mount points configured", client);
3551 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
3556 GST_ERROR ("client %p: can't find path for url", client);
3557 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
3558 gst_sdp_message_free (sdp);
3563 GST_ERROR ("client %p: unknown content type", client);
3564 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
3569 GST_ERROR ("client %p: can't find SDP message", client);
3570 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
3575 GST_ERROR ("client %p: failed to parse SDP message", client);
3576 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
3577 gst_sdp_message_free (sdp);
3582 GST_ERROR ("client %p: no media", client);
3584 /* error reply is already sent */
3585 gst_sdp_message_free (sdp);
3590 GST_ERROR ("client %p: pre signal returned error: %s", client,
3591 gst_rtsp_status_as_text (sig_result));
3592 send_generic_response (client, sig_result, ctx);
3593 gst_sdp_message_free (sdp);
3594 gst_rtsp_media_unlock (media);
3595 g_object_unref (media);
3600 GST_ERROR ("client %p: media does not support ANNOUNCE", client);
3601 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_ALLOWED, ctx);
3603 gst_rtsp_media_unlock (media);
3604 g_object_unref (media);
3605 gst_sdp_message_free (sdp);
3610 GST_ERROR ("client %p: can't handle SDP", client);
3611 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_MEDIA_TYPE, ctx);
3613 gst_rtsp_media_unlock (media);
3614 g_object_unref (media);
3615 gst_sdp_message_free (sdp);
3621 handle_record_request (GstRTSPClient * client, GstRTSPContext * ctx)
3623 GstRTSPSession *session;
3624 GstRTSPClientClass *klass;
3625 GstRTSPSessionMedia *sessmedia;
3626 GstRTSPMedia *media;
3628 GstRTSPState rtspstate;
3631 GstRTSPStatusCode sig_result;
3632 GPtrArray *transports;
3634 if (!(session = ctx->session))
3637 if (!(uri = ctx->uri))
3640 klass = GST_RTSP_CLIENT_GET_CLASS (client);
3641 path = klass->make_path_from_uri (client, uri);
3643 /* get a handle to the configuration of the media in the session */
3644 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
3648 if (path[matched] != '\0')
3653 ctx->sessmedia = sessmedia;
3654 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
3656 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_RECORD_REQUEST], 0,
3658 if (sig_result != GST_RTSP_STS_OK) {
3662 if (!(gst_rtsp_media_get_transport_mode (media) &
3663 GST_RTSP_TRANSPORT_MODE_RECORD))
3664 goto unsupported_mode;
3666 /* the session state must be playing or ready */
3667 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
3668 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
3671 /* update the pipeline */
3672 transports = gst_rtsp_session_media_get_transports (sessmedia);
3673 if (!gst_rtsp_media_complete_pipeline (media, transports)) {
3674 g_ptr_array_unref (transports);
3675 goto pipeline_error;
3677 g_ptr_array_unref (transports);
3679 /* in record we first unsuspend, media could be suspended from SDP or PAUSED */
3680 if (!gst_rtsp_media_unsuspend (media))
3681 goto unsuspend_failed;
3683 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
3684 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
3686 send_message (client, ctx, ctx->response, FALSE);
3688 /* start playing after sending the response */
3689 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
3691 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
3693 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_RECORD_REQUEST], 0,
3701 GST_ERROR ("client %p: no session", client);
3702 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
3707 GST_ERROR ("client %p: no uri supplied", client);
3708 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
3713 GST_ERROR ("client %p: media not found", client);
3714 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
3719 GST_ERROR ("client %p: no aggregate path %s", client, path);
3720 send_generic_response (client,
3721 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
3727 GST_ERROR ("client %p: pre signal returned error: %s", client,
3728 gst_rtsp_status_as_text (sig_result));
3729 send_generic_response (client, sig_result, ctx);
3734 GST_ERROR ("client %p: media does not support RECORD", client);
3735 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_ALLOWED, ctx);
3740 GST_ERROR ("client %p: not PLAYING or READY", client);
3741 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
3747 GST_ERROR ("client %p: failed to configure the pipeline", client);
3748 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
3754 GST_ERROR ("client %p: unsuspend failed", client);
3755 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
3761 default_handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx,
3762 GstRTSPVersion version)
3764 GstRTSPMethod options;
3766 GstRTSPStatusCode sig_result;
3768 options = GST_RTSP_DESCRIBE |
3773 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
3775 if (version < GST_RTSP_VERSION_2_0) {
3776 options |= GST_RTSP_RECORD;
3777 options |= GST_RTSP_ANNOUNCE;
3780 str = gst_rtsp_options_as_text (options);
3782 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
3783 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
3785 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
3788 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_OPTIONS_REQUEST], 0,
3790 if (sig_result != GST_RTSP_STS_OK) {
3794 send_message (client, ctx, ctx->response, FALSE);
3796 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
3804 GST_ERROR ("client %p: pre signal returned error: %s", client,
3805 gst_rtsp_status_as_text (sig_result));
3806 send_generic_response (client, sig_result, ctx);
3807 gst_rtsp_message_free (ctx->response);
3812 /* remove duplicate and trailing '/' */
3814 sanitize_uri (GstRTSPUrl * uri)
3818 gboolean have_slash, prev_slash;
3820 s = d = uri->abspath;
3821 len = strlen (uri->abspath);
3825 for (i = 0; i < len; i++) {
3826 have_slash = s[i] == '/';
3828 if (!have_slash || !prev_slash)
3830 prev_slash = have_slash;
3832 len = d - uri->abspath;
3833 /* don't remove the first slash if that's the only thing left */
3834 if (len > 1 && *(d - 1) == '/')
3839 /* is called when the session is removed from its session pool. */
3841 client_session_removed (GstRTSPSessionPool * pool, GstRTSPSession * session,
3842 GstRTSPClient * client)
3844 GstRTSPClientPrivate *priv = client->priv;
3847 GST_INFO ("client %p: session %p removed", client, session);
3849 g_mutex_lock (&priv->lock);
3850 client_unwatch_session (client, session, NULL);
3852 if (!priv->sessions && priv->rtsp_ctrl_timeout == NULL) {
3853 if (priv->post_session_timeout > 0) {
3854 GWeakRef *client_weak_ref = g_new (GWeakRef, 1);
3855 timer_src = g_timeout_source_new_seconds (RTSP_CTRL_CB_INTERVAL);
3857 g_weak_ref_init (client_weak_ref, client);
3858 g_source_set_callback (timer_src, rtsp_ctrl_timeout_cb, client_weak_ref,
3859 rtsp_ctrl_timeout_destroy_notify);
3860 priv->rtsp_ctrl_timeout_cnt = 0;
3861 g_source_attach (timer_src, priv->watch_context);
3862 priv->rtsp_ctrl_timeout = timer_src;
3863 GST_DEBUG ("rtsp control setting up connection timeout %p.",
3864 priv->rtsp_ctrl_timeout);
3865 g_mutex_unlock (&priv->lock);
3866 } else if (priv->post_session_timeout == 0) {
3867 g_mutex_unlock (&priv->lock);
3868 gst_rtsp_client_close (client);
3870 g_mutex_unlock (&priv->lock);
3873 g_mutex_unlock (&priv->lock);
3877 /* Check for Require headers. Returns TRUE if there are no Require headers,
3878 * otherwise lets the application decide which headers are supported.
3879 * By default all headers are unsupported.
3880 * If there are unsupported options, FALSE will be returned together with
3881 * a newly-allocated string of (comma-separated) unsupported options in
3882 * the unsupported_reqs variable.
3884 * There may be multiple Require headers, but we must send one single
3885 * Unsupported header with all the unsupported options as response. If
3886 * an incoming Require header contained a comma-separated list of options
3887 * GstRtspConnection will already have split that list up into multiple
3891 check_request_requirements (GstRTSPContext * ctx, gchar ** unsupported_reqs)
3894 GPtrArray *arr = NULL;
3895 GstRTSPMessage *msg = ctx->request;
3898 gchar *sig_result = NULL;
3899 gboolean result = TRUE;
3903 res = gst_rtsp_message_get_header (msg, GST_RTSP_HDR_REQUIRE, &reqs, i++);
3905 if (res == GST_RTSP_ENOTIMPL)
3909 arr = g_ptr_array_new_with_free_func ((GDestroyNotify) g_free);
3911 g_ptr_array_add (arr, g_strdup (reqs));
3915 /* if we don't have any Require headers at all, all is fine */
3919 /* otherwise we've now processed at all the Require headers */
3920 g_ptr_array_add (arr, NULL);
3922 g_signal_emit (ctx->client,
3923 gst_rtsp_client_signals[SIGNAL_CHECK_REQUIREMENTS], 0, ctx,
3924 (gchar **) arr->pdata, &sig_result);
3926 if (sig_result == NULL) {
3927 /* no supported options, just report all of the required ones as
3929 *unsupported_reqs = g_strjoinv (", ", (gchar **) arr->pdata);
3934 if (strlen (sig_result) == 0)
3935 g_free (sig_result);
3937 *unsupported_reqs = sig_result;
3942 g_ptr_array_unref (arr);
3947 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
3949 GstRTSPClientPrivate *priv = client->priv;
3950 GstRTSPMethod method;
3951 const gchar *uristr;
3952 GstRTSPUrl *uri = NULL;
3953 GstRTSPVersion version;
3955 GstRTSPSession *session = NULL;
3956 GstRTSPContext sctx = { NULL }, *ctx;
3957 GstRTSPMessage response = { 0 };
3958 gchar *unsupported_reqs = NULL;
3959 gchar *sessid = NULL, *pipelined_request_id = NULL;
3960 GstRTSPClientClass *klass;
3962 klass = GST_RTSP_CLIENT_GET_CLASS (client);
3963 if (!(ctx = gst_rtsp_context_get_current ())) {
3965 ctx->auth = priv->auth;
3966 gst_rtsp_context_push_current (ctx);
3969 ctx->conn = priv->connection;
3970 ctx->client = client;
3971 ctx->request = request;
3972 ctx->response = &response;
3974 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
3975 gst_rtsp_message_dump (request);
3978 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
3980 GST_INFO ("client %p: received a request %s %s %s", client,
3981 gst_rtsp_method_as_text (method), uristr,
3982 gst_rtsp_version_as_text (version));
3984 /* we can only handle 1.0 requests */
3985 if (version != GST_RTSP_VERSION_1_0 && version != GST_RTSP_VERSION_2_0)
3988 ctx->method = method;
3990 /* we always try to parse the url first */
3991 if (strcmp (uristr, "*") == 0) {
3992 /* special case where we have * as uri, keep uri = NULL */
3993 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
3994 /* check if the uristr is an absolute path <=> scheme and host information
3998 scheme = g_uri_parse_scheme (uristr);
3999 if (scheme == NULL && g_str_has_prefix (uristr, "/")) {
4000 gchar *absolute_uristr = NULL;
4002 GST_WARNING_OBJECT (client, "request doesn't contain absolute url");
4003 if (priv->server_ip == NULL) {
4004 GST_WARNING_OBJECT (client, "host information missing");
4009 g_strdup_printf ("rtsp://%s%s", priv->server_ip, uristr);
4011 GST_DEBUG_OBJECT (client, "absolute url: %s", absolute_uristr);
4012 if (gst_rtsp_url_parse (absolute_uristr, &uri) != GST_RTSP_OK) {
4013 g_free (absolute_uristr);
4016 g_free (absolute_uristr);
4023 /* get the session if there is any */
4024 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_PIPELINED_REQUESTS,
4025 &pipelined_request_id, 0);
4026 if (res == GST_RTSP_OK) {
4027 sessid = g_hash_table_lookup (client->priv->pipelined_requests,
4028 pipelined_request_id);
4031 res = GST_RTSP_ERROR;
4034 if (res != GST_RTSP_OK)
4036 gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
4038 if (res == GST_RTSP_OK) {
4039 if (priv->session_pool == NULL)
4042 /* we had a session in the request, find it again */
4043 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
4044 goto session_not_found;
4046 /* we add the session to the client list of watched sessions. When a session
4047 * disappears because it times out, we will be notified. If all sessions are
4048 * gone, we will close the connection */
4049 client_watch_session (client, session);
4052 /* sanitize the uri */
4056 ctx->session = session;
4058 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
4059 goto not_authorized;
4061 /* handle any 'Require' headers */
4062 if (!check_request_requirements (ctx, &unsupported_reqs))
4063 goto unsupported_requirement;
4065 /* now see what is asked and dispatch to a dedicated handler */
4067 case GST_RTSP_OPTIONS:
4068 priv->version = version;
4069 klass->handle_options_request (client, ctx, version);
4071 case GST_RTSP_DESCRIBE:
4072 handle_describe_request (client, ctx);
4074 case GST_RTSP_SETUP:
4075 handle_setup_request (client, ctx);
4078 klass->handle_play_request (client, ctx);
4080 case GST_RTSP_PAUSE:
4081 handle_pause_request (client, ctx);
4083 case GST_RTSP_TEARDOWN:
4084 handle_teardown_request (client, ctx);
4086 case GST_RTSP_SET_PARAMETER:
4087 klass->handle_set_param_request (client, ctx);
4089 case GST_RTSP_GET_PARAMETER:
4090 klass->handle_get_param_request (client, ctx);
4092 case GST_RTSP_ANNOUNCE:
4093 if (version >= GST_RTSP_VERSION_2_0)
4094 goto invalid_command_for_version;
4095 handle_announce_request (client, ctx);
4097 case GST_RTSP_RECORD:
4098 if (version >= GST_RTSP_VERSION_2_0)
4099 goto invalid_command_for_version;
4100 handle_record_request (client, ctx);
4102 case GST_RTSP_REDIRECT:
4103 goto not_implemented;
4104 case GST_RTSP_INVALID:
4111 gst_rtsp_context_pop_current (ctx);
4113 g_object_unref (session);
4115 gst_rtsp_url_free (uri);
4121 GST_ERROR ("client %p: version %d not supported", client, version);
4122 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
4126 invalid_command_for_version:
4128 GST_ERROR ("client %p: invalid command for version", client);
4129 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
4134 GST_ERROR ("client %p: bad request", client);
4135 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
4140 GST_ERROR ("client %p: no pool configured", client);
4141 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
4146 GST_ERROR ("client %p: session not found", client);
4147 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
4152 GST_ERROR ("client %p: not allowed", client);
4153 /* error reply is already sent */
4156 unsupported_requirement:
4158 GST_ERROR ("client %p: Required option is not supported (%s)", client,
4160 send_option_not_supported_response (client, ctx, unsupported_reqs);
4161 g_free (unsupported_reqs);
4166 GST_ERROR ("client %p: method %d not implemented", client, method);
4167 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
4174 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
4176 GstRTSPClientPrivate *priv = client->priv;
4178 GstRTSPSession *session = NULL;
4179 GstRTSPContext sctx = { NULL }, *ctx;
4182 if (!(ctx = gst_rtsp_context_get_current ())) {
4184 ctx->auth = priv->auth;
4185 gst_rtsp_context_push_current (ctx);
4188 ctx->conn = priv->connection;
4189 ctx->client = client;
4190 ctx->request = NULL;
4192 ctx->method = GST_RTSP_INVALID;
4193 ctx->response = response;
4195 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
4196 gst_rtsp_message_dump (response);
4199 GST_INFO ("client %p: received a response", client);
4201 /* get the session if there is any */
4203 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
4204 if (res == GST_RTSP_OK) {
4205 if (priv->session_pool == NULL)
4208 /* we had a session in the request, find it again */
4209 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
4210 goto session_not_found;
4212 /* we add the session to the client list of watched sessions. When a session
4213 * disappears because it times out, we will be notified. If all sessions are
4214 * gone, we will close the connection */
4215 client_watch_session (client, session);
4218 ctx->session = session;
4220 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
4225 gst_rtsp_context_pop_current (ctx);
4227 g_object_unref (session);
4232 GST_ERROR ("client %p: no pool configured", client);
4237 GST_ERROR ("client %p: session not found", client);
4243 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
4245 GstRTSPClientPrivate *priv = client->priv;
4251 GstRTSPStreamTransport *trans;
4253 /* find the stream for this message */
4254 res = gst_rtsp_message_parse_data (message, &channel);
4255 if (res != GST_RTSP_OK)
4258 gst_rtsp_message_get_body (message, &data, &size);
4260 goto invalid_length;
4262 gst_rtsp_message_steal_body (message, &data, &size);
4264 /* Strip trailing \0 (which GstRTSPConnection adds) */
4267 buffer = gst_buffer_new_wrapped (data, size);
4270 g_hash_table_lookup (priv->transports, GINT_TO_POINTER ((gint) channel));
4272 GSocketAddress *addr;
4274 /* Only create the socket address once for the transport, we don't really
4275 * want to do that for every single packet.
4277 * The netaddress meta is later used by the RTP stack to know where
4278 * packets came from and allows us to match it again to a stream transport
4280 * In theory we could use the remote socket address of the RTSP connection
4281 * here, but this would fail with a custom configure_client_transport()
4285 g_object_get_data (G_OBJECT (trans), "rtsp-client.remote-addr"))) {
4286 const GstRTSPTransport *tr;
4287 GInetAddress *iaddr;
4289 tr = gst_rtsp_stream_transport_get_transport (trans);
4290 iaddr = g_inet_address_new_from_string (tr->destination);
4292 addr = g_inet_socket_address_new (iaddr, tr->client_port.min);
4293 g_object_unref (iaddr);
4294 g_object_set_data_full (G_OBJECT (trans), "rtsp-client.remote-addr",
4295 addr, (GDestroyNotify) g_object_unref);
4300 gst_buffer_add_net_address_meta (buffer, addr);
4303 /* dispatch to the stream based on the channel number */
4304 GST_LOG_OBJECT (client, "%u bytes of data on channel %u", size, channel);
4305 gst_rtsp_stream_transport_recv_data (trans, channel, buffer);
4307 GST_DEBUG_OBJECT (client, "received %u bytes of data for "
4308 "unknown channel %u", size, channel);
4309 gst_buffer_unref (buffer);
4317 GST_DEBUG ("client %p: Short message received, ignoring", client);
4323 * gst_rtsp_client_set_session_pool:
4324 * @client: a #GstRTSPClient
4325 * @pool: (transfer none) (nullable): a #GstRTSPSessionPool
4327 * Set @pool as the sessionpool for @client which it will use to find
4328 * or allocate sessions. the sessionpool is usually inherited from the server
4329 * that created the client but can be overridden later.
4332 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
4333 GstRTSPSessionPool * pool)
4335 GstRTSPSessionPool *old;
4336 GstRTSPClientPrivate *priv;
4338 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
4340 priv = client->priv;
4343 g_object_ref (pool);
4345 g_mutex_lock (&priv->lock);
4346 old = priv->session_pool;
4347 priv->session_pool = pool;
4349 if (priv->session_removed_id) {
4350 g_signal_handler_disconnect (old, priv->session_removed_id);
4351 priv->session_removed_id = 0;
4353 g_mutex_unlock (&priv->lock);
4355 /* FIXME, should remove all sessions from the old pool for this client */
4357 g_object_unref (old);
4361 * gst_rtsp_client_get_session_pool:
4362 * @client: a #GstRTSPClient
4364 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
4366 * Returns: (transfer full) (nullable): a #GstRTSPSessionPool, unref after usage.
4368 GstRTSPSessionPool *
4369 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
4371 GstRTSPClientPrivate *priv;
4372 GstRTSPSessionPool *result;
4374 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
4376 priv = client->priv;
4378 g_mutex_lock (&priv->lock);
4379 if ((result = priv->session_pool))
4380 g_object_ref (result);
4381 g_mutex_unlock (&priv->lock);
4387 * gst_rtsp_client_set_mount_points:
4388 * @client: a #GstRTSPClient
4389 * @mounts: (transfer none) (nullable): a #GstRTSPMountPoints
4391 * Set @mounts as the mount points for @client which it will use to map urls
4392 * to media streams. These mount points are usually inherited from the server that
4393 * created the client but can be overriden later.
4396 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
4397 GstRTSPMountPoints * mounts)
4399 GstRTSPClientPrivate *priv;
4400 GstRTSPMountPoints *old;
4402 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
4404 priv = client->priv;
4407 g_object_ref (mounts);
4409 g_mutex_lock (&priv->lock);
4410 old = priv->mount_points;
4411 priv->mount_points = mounts;
4412 g_mutex_unlock (&priv->lock);
4415 g_object_unref (old);
4419 * gst_rtsp_client_get_mount_points:
4420 * @client: a #GstRTSPClient
4422 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
4424 * Returns: (transfer full) (nullable): a #GstRTSPMountPoints, unref after usage.
4426 GstRTSPMountPoints *
4427 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
4429 GstRTSPClientPrivate *priv;
4430 GstRTSPMountPoints *result;
4432 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
4434 priv = client->priv;
4436 g_mutex_lock (&priv->lock);
4437 if ((result = priv->mount_points))
4438 g_object_ref (result);
4439 g_mutex_unlock (&priv->lock);
4445 * gst_rtsp_client_set_content_length_limit:
4446 * @client: a #GstRTSPClient
4447 * @limit: Content-Length limit
4449 * Configure @client to use the specified Content-Length limit.
4451 * Define an appropriate request size limit and reject requests exceeding the
4452 * limit with response status 413 Request Entity Too Large
4457 gst_rtsp_client_set_content_length_limit (GstRTSPClient * client, guint limit)
4459 GstRTSPClientPrivate *priv;
4461 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
4463 priv = client->priv;
4464 g_mutex_lock (&priv->lock);
4465 priv->content_length_limit = limit;
4466 g_mutex_unlock (&priv->lock);
4470 * gst_rtsp_client_get_content_length_limit:
4471 * @client: a #GstRTSPClient
4473 * Get the Content-Length limit of @client.
4475 * Returns: the Content-Length limit.
4480 gst_rtsp_client_get_content_length_limit (GstRTSPClient * client)
4482 GstRTSPClientPrivate *priv;
4483 glong content_length_limit;
4485 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), -1);
4486 priv = client->priv;
4488 g_mutex_lock (&priv->lock);
4489 content_length_limit = priv->content_length_limit;
4490 g_mutex_unlock (&priv->lock);
4492 return content_length_limit;
4496 * gst_rtsp_client_set_auth:
4497 * @client: a #GstRTSPClient
4498 * @auth: (transfer none) (nullable): a #GstRTSPAuth
4500 * configure @auth to be used as the authentication manager of @client.
4503 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
4505 GstRTSPClientPrivate *priv;
4508 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
4510 priv = client->priv;
4513 g_object_ref (auth);
4515 g_mutex_lock (&priv->lock);
4518 g_mutex_unlock (&priv->lock);
4521 g_object_unref (old);
4526 * gst_rtsp_client_get_auth:
4527 * @client: a #GstRTSPClient
4529 * Get the #GstRTSPAuth used as the authentication manager of @client.
4531 * Returns: (transfer full) (nullable): the #GstRTSPAuth of @client.
4532 * g_object_unref() after usage.
4535 gst_rtsp_client_get_auth (GstRTSPClient * client)
4537 GstRTSPClientPrivate *priv;
4538 GstRTSPAuth *result;
4540 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
4542 priv = client->priv;
4544 g_mutex_lock (&priv->lock);
4545 if ((result = priv->auth))
4546 g_object_ref (result);
4547 g_mutex_unlock (&priv->lock);
4553 * gst_rtsp_client_set_thread_pool:
4554 * @client: a #GstRTSPClient
4555 * @pool: (transfer none) (nullable): a #GstRTSPThreadPool
4557 * configure @pool to be used as the thread pool of @client.
4560 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
4561 GstRTSPThreadPool * pool)
4563 GstRTSPClientPrivate *priv;
4564 GstRTSPThreadPool *old;
4566 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
4568 priv = client->priv;
4571 g_object_ref (pool);
4573 g_mutex_lock (&priv->lock);
4574 old = priv->thread_pool;
4575 priv->thread_pool = pool;
4576 g_mutex_unlock (&priv->lock);
4579 g_object_unref (old);
4583 * gst_rtsp_client_get_thread_pool:
4584 * @client: a #GstRTSPClient
4586 * Get the #GstRTSPThreadPool used as the thread pool of @client.
4588 * Returns: (transfer full) (nullable): the #GstRTSPThreadPool of @client. g_object_unref() after
4592 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
4594 GstRTSPClientPrivate *priv;
4595 GstRTSPThreadPool *result;
4597 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
4599 priv = client->priv;
4601 g_mutex_lock (&priv->lock);
4602 if ((result = priv->thread_pool))
4603 g_object_ref (result);
4604 g_mutex_unlock (&priv->lock);
4610 * gst_rtsp_client_set_connection:
4611 * @client: a #GstRTSPClient
4612 * @conn: (transfer full): a #GstRTSPConnection
4614 * Set the #GstRTSPConnection of @client. This function takes ownership of
4617 * Returns: %TRUE on success.
4620 gst_rtsp_client_set_connection (GstRTSPClient * client,
4621 GstRTSPConnection * conn)
4623 GstRTSPClientPrivate *priv;
4624 GSocket *read_socket;
4625 GSocketAddress *address;
4627 GError *error = NULL;
4629 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
4630 g_return_val_if_fail (conn != NULL, FALSE);
4632 priv = client->priv;
4634 gst_rtsp_connection_set_content_length_limit (conn,
4635 priv->content_length_limit);
4636 read_socket = gst_rtsp_connection_get_read_socket (conn);
4638 if (!(address = g_socket_get_local_address (read_socket, &error)))
4641 g_free (priv->server_ip);
4642 /* keep the original ip that the client connected to */
4643 if (G_IS_INET_SOCKET_ADDRESS (address)) {
4644 GInetAddress *iaddr;
4646 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
4648 /* socket might be ipv6 but adress still ipv4 */
4649 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
4650 priv->server_ip = g_inet_address_to_string (iaddr);
4651 g_object_unref (address);
4653 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
4654 priv->server_ip = g_strdup ("unknown");
4655 g_object_unref (address);
4658 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
4659 priv->server_ip, priv->is_ipv6);
4661 url = gst_rtsp_connection_get_url (conn);
4662 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
4664 priv->connection = conn;
4671 GST_ERROR ("could not get local address %s", error->message);
4672 g_error_free (error);
4678 * gst_rtsp_client_get_connection:
4679 * @client: a #GstRTSPClient
4681 * Get the #GstRTSPConnection of @client.
4683 * Returns: (transfer none) (nullable): the #GstRTSPConnection of @client.
4684 * The connection object returned remains valid until the client is freed.
4687 gst_rtsp_client_get_connection (GstRTSPClient * client)
4689 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
4691 return client->priv->connection;
4695 * gst_rtsp_client_set_send_func:
4696 * @client: a #GstRTSPClient
4697 * @func: (scope notified): a #GstRTSPClientSendFunc
4698 * @user_data: (closure): user data passed to @func
4699 * @notify: (allow-none): called when @user_data is no longer in use
4701 * Set @func as the callback that will be called when a new message needs to be
4702 * sent to the client. @user_data is passed to @func and @notify is called when
4703 * @user_data is no longer in use.
4705 * By default, the client will send the messages on the #GstRTSPConnection that
4706 * was configured with gst_rtsp_client_attach() was called.
4708 * It is only allowed to set either a `send_func` or a `send_messages_func`
4709 * but not both at the same time.
4712 gst_rtsp_client_set_send_func (GstRTSPClient * client,
4713 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
4715 GstRTSPClientPrivate *priv;
4716 GDestroyNotify old_notify;
4719 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
4721 priv = client->priv;
4723 g_mutex_lock (&priv->send_lock);
4724 g_assert (func == NULL || priv->send_messages_func == NULL);
4725 priv->send_func = func;
4726 old_notify = priv->send_notify;
4727 old_data = priv->send_data;
4728 priv->send_notify = notify;
4729 priv->send_data = user_data;
4730 g_mutex_unlock (&priv->send_lock);
4733 old_notify (old_data);
4737 * gst_rtsp_client_set_send_messages_func:
4738 * @client: a #GstRTSPClient
4739 * @func: (scope notified): a #GstRTSPClientSendMessagesFunc
4740 * @user_data: (closure): user data passed to @func
4741 * @notify: (allow-none): called when @user_data is no longer in use
4743 * Set @func as the callback that will be called when new messages needs to be
4744 * sent to the client. @user_data is passed to @func and @notify is called when
4745 * @user_data is no longer in use.
4747 * By default, the client will send the messages on the #GstRTSPConnection that
4748 * was configured with gst_rtsp_client_attach() was called.
4750 * It is only allowed to set either a `send_func` or a `send_messages_func`
4751 * but not both at the same time.
4756 gst_rtsp_client_set_send_messages_func (GstRTSPClient * client,
4757 GstRTSPClientSendMessagesFunc func, gpointer user_data,
4758 GDestroyNotify notify)
4760 GstRTSPClientPrivate *priv;
4761 GDestroyNotify old_notify;
4764 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
4766 priv = client->priv;
4768 g_mutex_lock (&priv->send_lock);
4769 g_assert (func == NULL || priv->send_func == NULL);
4770 priv->send_messages_func = func;
4771 old_notify = priv->send_messages_notify;
4772 old_data = priv->send_messages_data;
4773 priv->send_messages_notify = notify;
4774 priv->send_messages_data = user_data;
4775 g_mutex_unlock (&priv->send_lock);
4778 old_notify (old_data);
4782 * gst_rtsp_client_handle_message:
4783 * @client: a #GstRTSPClient
4784 * @message: (transfer none): an #GstRTSPMessage
4786 * Let the client handle @message.
4788 * Returns: a #GstRTSPResult.
4791 gst_rtsp_client_handle_message (GstRTSPClient * client,
4792 GstRTSPMessage * message)
4794 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
4795 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
4797 switch (message->type) {
4798 case GST_RTSP_MESSAGE_REQUEST:
4799 handle_request (client, message);
4801 case GST_RTSP_MESSAGE_RESPONSE:
4802 handle_response (client, message);
4804 case GST_RTSP_MESSAGE_DATA:
4805 handle_data (client, message);
4814 * gst_rtsp_client_send_message:
4815 * @client: a #GstRTSPClient
4816 * @session: (allow-none) (transfer none): a #GstRTSPSession to send
4817 * the message to or %NULL
4818 * @message: (transfer none): The #GstRTSPMessage to send
4820 * Send a message message to the remote end. @message must be a
4821 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
4824 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
4825 GstRTSPMessage * message)
4827 GstRTSPContext sctx = { NULL }
4829 GstRTSPClientPrivate *priv;
4831 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
4832 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
4833 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
4834 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
4836 priv = client->priv;
4838 if (!(ctx = gst_rtsp_context_get_current ())) {
4840 ctx->auth = priv->auth;
4841 gst_rtsp_context_push_current (ctx);
4844 ctx->conn = priv->connection;
4845 ctx->client = client;
4846 ctx->session = session;
4848 send_message (client, ctx, message, FALSE);
4851 gst_rtsp_context_pop_current (ctx);
4857 * gst_rtsp_client_get_stream_transport:
4859 * This is useful when providing a send function through
4860 * gst_rtsp_client_set_send_func() when doing RTSP over TCP:
4861 * the send function must call gst_rtsp_stream_transport_message_sent ()
4862 * on the appropriate transport when data has been received for streaming
4865 * Returns: (transfer none) (nullable): the #GstRTSPStreamTransport associated with @channel.
4869 GstRTSPStreamTransport *
4870 gst_rtsp_client_get_stream_transport (GstRTSPClient * self, guint8 channel)
4872 return g_hash_table_lookup (self->priv->transports,
4873 GINT_TO_POINTER ((gint) channel));
4877 do_send_messages (GstRTSPClient * client, GstRTSPMessage * messages,
4878 guint n_messages, gboolean close, gpointer user_data)
4880 GstRTSPClientPrivate *priv = client->priv;
4885 /* send the message */
4887 GST_INFO ("client %p: sending close message", client);
4889 ret = gst_rtsp_watch_send_messages (priv->watch, messages, n_messages, &id);
4890 if (ret != GST_RTSP_OK)
4893 for (i = 0; i < n_messages; i++) {
4894 if (gst_rtsp_message_get_type (&messages[i]) == GST_RTSP_MESSAGE_DATA) {
4898 /* We assume that all data messages in the list are for the
4900 r = gst_rtsp_message_parse_data (&messages[i], &channel);
4901 if (r != GST_RTSP_OK) {
4906 /* check if the message has been queued for transmission in watch */
4908 /* store the seq number so we can wait until it has been sent */
4909 GST_DEBUG_OBJECT (client, "wait for message %d, channel %d", id,
4911 set_data_seq (client, channel, id);
4913 GstRTSPStreamTransport *trans;
4916 g_hash_table_lookup (priv->transports,
4917 GINT_TO_POINTER ((gint) channel));
4919 GST_DEBUG_OBJECT (client, "emit 'message-sent' signal");
4920 g_mutex_unlock (&priv->send_lock);
4921 gst_rtsp_stream_transport_message_sent (trans);
4922 g_mutex_lock (&priv->send_lock);
4929 return ret == GST_RTSP_OK;
4934 GST_DEBUG_OBJECT (client, "got error %d", ret);
4939 static GstRTSPResult
4940 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
4943 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
4946 static GstRTSPResult
4947 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
4949 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
4950 GstRTSPClientPrivate *priv = client->priv;
4951 GstRTSPStreamTransport *trans = NULL;
4954 g_mutex_lock (&priv->send_lock);
4956 if (get_data_channel (client, cseq, &channel)) {
4957 trans = g_hash_table_lookup (priv->transports, GINT_TO_POINTER (channel));
4958 set_data_seq (client, channel, 0);
4960 g_mutex_unlock (&priv->send_lock);
4963 GST_DEBUG_OBJECT (client, "emit 'message-sent' signal");
4964 gst_rtsp_stream_transport_message_sent (trans);
4970 static GstRTSPResult
4971 closed (GstRTSPWatch * watch, gpointer user_data)
4973 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
4974 GstRTSPClientPrivate *priv = client->priv;
4975 const gchar *tunnelid;
4977 GST_INFO ("client %p: connection closed", client);
4979 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
4980 g_mutex_lock (&tunnels_lock);
4981 /* remove from tunnelids */
4982 g_hash_table_remove (tunnels, tunnelid);
4983 g_mutex_unlock (&tunnels_lock);
4986 gst_rtsp_watch_set_flushing (watch, TRUE);
4987 g_mutex_lock (&priv->watch_lock);
4988 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
4989 gst_rtsp_client_set_send_messages_func (client, NULL, NULL, NULL);
4990 g_mutex_unlock (&priv->watch_lock);
4995 static GstRTSPResult
4996 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
4998 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
5001 str = gst_rtsp_strresult (result);
5002 GST_INFO ("client %p: received an error %s", client, str);
5008 static GstRTSPResult
5009 error_full (GstRTSPWatch * watch, GstRTSPResult result,
5010 GstRTSPMessage * message, guint id, gpointer user_data)
5012 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
5014 GstRTSPContext sctx = { NULL }, *ctx;
5015 GstRTSPClientPrivate *priv;
5016 GstRTSPMessage response = { 0 };
5017 priv = client->priv;
5019 if (!(ctx = gst_rtsp_context_get_current ())) {
5021 ctx->auth = priv->auth;
5022 gst_rtsp_context_push_current (ctx);
5025 ctx->conn = priv->connection;
5026 ctx->client = client;
5027 ctx->request = message;
5028 ctx->method = GST_RTSP_INVALID;
5029 ctx->response = &response;
5031 /* only return error response if it is a request */
5032 if (!message || message->type != GST_RTSP_MESSAGE_REQUEST)
5035 if (result == GST_RTSP_ENOMEM) {
5036 send_generic_response (client, GST_RTSP_STS_REQUEST_ENTITY_TOO_LARGE, ctx);
5039 if (result == GST_RTSP_EPARSE) {
5040 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
5046 gst_rtsp_context_pop_current (ctx);
5047 str = gst_rtsp_strresult (result);
5049 ("client %p: error when handling message %p with id %d: %s",
5050 client, message, id, str);
5057 remember_tunnel (GstRTSPClient * client)
5059 GstRTSPClientPrivate *priv = client->priv;
5060 const gchar *tunnelid;
5062 /* store client in the pending tunnels */
5063 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
5064 if (tunnelid == NULL)
5067 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
5069 /* we can't have two clients connecting with the same tunnelid */
5070 g_mutex_lock (&tunnels_lock);
5071 if (g_hash_table_lookup (tunnels, tunnelid))
5072 goto tunnel_existed;
5074 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
5075 g_mutex_unlock (&tunnels_lock);
5082 GST_ERROR ("client %p: no tunnelid provided", client);
5087 g_mutex_unlock (&tunnels_lock);
5088 GST_ERROR ("client %p: tunnel session %s already existed", client,
5094 static GstRTSPResult
5095 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
5097 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
5098 GstRTSPClientPrivate *priv = client->priv;
5100 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
5103 /* ignore error, it'll only be a problem when the client does a POST again */
5104 remember_tunnel (client);
5109 static GstRTSPStatusCode
5110 handle_tunnel (GstRTSPClient * client)
5112 GstRTSPClientPrivate *priv = client->priv;
5113 GstRTSPClient *oclient;
5114 GstRTSPClientPrivate *opriv;
5115 const gchar *tunnelid;
5117 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
5118 if (tunnelid == NULL)
5121 /* check for previous tunnel */
5122 g_mutex_lock (&tunnels_lock);
5123 oclient = g_hash_table_lookup (tunnels, tunnelid);
5125 if (oclient == NULL) {
5126 /* no previous tunnel, remember tunnel */
5127 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
5128 g_mutex_unlock (&tunnels_lock);
5130 GST_INFO ("client %p: no previous tunnel found, remembering tunnel (%p)",
5131 client, priv->connection);
5133 /* merge both tunnels into the first client */
5134 /* remove the old client from the table. ref before because removing it will
5135 * remove the ref to it. */
5136 g_object_ref (oclient);
5137 g_hash_table_remove (tunnels, tunnelid);
5138 g_mutex_unlock (&tunnels_lock);
5140 opriv = oclient->priv;
5142 g_mutex_lock (&opriv->watch_lock);
5143 if (opriv->watch == NULL)
5145 if (opriv->tstate == priv->tstate)
5146 goto tunnel_duplicate_id;
5148 GST_INFO ("client %p: found previous tunnel %p (old %p, new %p)", client,
5149 oclient, opriv->connection, priv->connection);
5151 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
5152 gst_rtsp_watch_reset (priv->watch);
5153 gst_rtsp_watch_reset (opriv->watch);
5154 g_mutex_unlock (&opriv->watch_lock);
5155 g_object_unref (oclient);
5157 /* the old client owns the tunnel now, the new one will be freed */
5158 g_source_destroy ((GSource *) priv->watch);
5160 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
5161 gst_rtsp_client_set_send_messages_func (client, NULL, NULL, NULL);
5162 rtsp_ctrl_timeout_remove (client);
5165 return GST_RTSP_STS_OK;
5170 GST_ERROR ("client %p: no tunnelid provided", client);
5171 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
5175 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
5176 g_mutex_unlock (&opriv->watch_lock);
5177 g_object_unref (oclient);
5178 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
5180 tunnel_duplicate_id:
5182 GST_ERROR ("client %p: tunnel session %s was duplicate", client, tunnelid);
5183 g_mutex_unlock (&opriv->watch_lock);
5184 g_object_unref (oclient);
5185 return GST_RTSP_STS_BAD_REQUEST;
5189 static GstRTSPStatusCode
5190 tunnel_get (GstRTSPWatch * watch, gpointer user_data)
5192 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
5194 GST_INFO ("client %p: tunnel get (connection %p)", client,
5195 client->priv->connection);
5197 g_mutex_lock (&client->priv->lock);
5198 client->priv->tstate = TUNNEL_STATE_GET;
5199 g_mutex_unlock (&client->priv->lock);
5201 return handle_tunnel (client);
5204 static GstRTSPResult
5205 tunnel_post (GstRTSPWatch * watch, gpointer user_data)
5207 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
5209 GST_INFO ("client %p: tunnel post (connection %p)", client,
5210 client->priv->connection);
5212 g_mutex_lock (&client->priv->lock);
5213 client->priv->tstate = TUNNEL_STATE_POST;
5214 g_mutex_unlock (&client->priv->lock);
5216 if (handle_tunnel (client) != GST_RTSP_STS_OK)
5217 return GST_RTSP_ERROR;
5222 static GstRTSPResult
5223 tunnel_http_response (GstRTSPWatch * watch, GstRTSPMessage * request,
5224 GstRTSPMessage * response, gpointer user_data)
5226 GstRTSPClientClass *klass;
5228 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
5229 klass = GST_RTSP_CLIENT_GET_CLASS (client);
5231 if (klass->tunnel_http_response) {
5232 klass->tunnel_http_response (client, request, response);
5238 static GstRTSPWatchFuncs watch_funcs = {
5247 tunnel_http_response
5251 client_watch_notify (GstRTSPClient * client)
5253 GstRTSPClientPrivate *priv = client->priv;
5254 gboolean closed = TRUE;
5256 GST_INFO ("client %p: watch destroyed", client);
5258 /* remove all sessions if the media says so and so drop the extra client ref */
5259 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
5260 gst_rtsp_client_set_send_messages_func (client, NULL, NULL, NULL);
5261 rtsp_ctrl_timeout_remove (client);
5262 gst_rtsp_client_session_filter (client, cleanup_session, &closed);
5265 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
5266 g_object_unref (client);
5270 * gst_rtsp_client_attach:
5271 * @client: a #GstRTSPClient
5272 * @context: (allow-none): a #GMainContext
5274 * Attaches @client to @context. When the mainloop for @context is run, the
5275 * client will be dispatched. When @context is %NULL, the default context will be
5278 * This function should be called when the client properties and urls are fully
5279 * configured and the client is ready to start.
5281 * Returns: the ID (greater than 0) for the source within the GMainContext.
5284 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
5286 GstRTSPClientPrivate *priv;
5289 GWeakRef *client_weak_ref = g_new (GWeakRef, 1);
5291 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
5292 priv = client->priv;
5293 g_return_val_if_fail (priv->connection != NULL, 0);
5294 g_return_val_if_fail (priv->watch == NULL, 0);
5295 g_return_val_if_fail (priv->watch_context == NULL, 0);
5297 /* make sure noone will free the context before the watch is destroyed */
5298 priv->watch_context = g_main_context_ref (context);
5300 /* create watch for the connection and attach */
5301 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
5302 g_object_ref (client), (GDestroyNotify) client_watch_notify);
5303 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
5304 gst_rtsp_client_set_send_messages_func (client, do_send_messages, priv->watch,
5305 (GDestroyNotify) gst_rtsp_watch_unref);
5307 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
5309 GST_INFO ("client %p: attaching to context %p", client, context);
5310 res = gst_rtsp_watch_attach (priv->watch, context);
5312 /* Setting up a timeout for the RTSP control channel until a session
5313 * is up where it is handling timeouts. */
5314 g_mutex_lock (&priv->lock);
5316 /* remove old timeout if any */
5317 rtsp_ctrl_timeout_remove_unlocked (client->priv);
5319 timer_src = g_timeout_source_new_seconds (RTSP_CTRL_CB_INTERVAL);
5320 g_weak_ref_init (client_weak_ref, client);
5321 g_source_set_callback (timer_src, rtsp_ctrl_timeout_cb, client_weak_ref,
5322 rtsp_ctrl_timeout_destroy_notify);
5323 g_source_attach (timer_src, priv->watch_context);
5324 priv->rtsp_ctrl_timeout = timer_src;
5325 GST_DEBUG ("rtsp control setting up session timeout %p.",
5326 priv->rtsp_ctrl_timeout);
5328 g_mutex_unlock (&priv->lock);
5334 * gst_rtsp_client_session_filter:
5335 * @client: a #GstRTSPClient
5336 * @func: (scope call) (allow-none): a callback
5337 * @user_data: user data passed to @func
5339 * Call @func for each session managed by @client. The result value of @func
5340 * determines what happens to the session. @func will be called with @client
5341 * locked so no further actions on @client can be performed from @func.
5343 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
5346 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
5348 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
5349 * will also be added with an additional ref to the result #GList of this
5352 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each session.
5354 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
5355 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
5356 * element in the #GList should be unreffed before the list is freed.
5359 gst_rtsp_client_session_filter (GstRTSPClient * client,
5360 GstRTSPClientSessionFilterFunc func, gpointer user_data)
5362 GstRTSPClientPrivate *priv;
5363 GList *result, *walk, *next;
5364 GHashTable *visited;
5367 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
5369 priv = client->priv;
5373 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
5375 g_mutex_lock (&priv->lock);
5377 cookie = priv->sessions_cookie;
5378 for (walk = priv->sessions; walk; walk = next) {
5379 GstRTSPSession *sess = walk->data;
5380 GstRTSPFilterResult res;
5383 next = g_list_next (walk);
5386 /* only visit each session once */
5387 if (g_hash_table_contains (visited, sess))
5390 g_hash_table_add (visited, g_object_ref (sess));
5391 g_mutex_unlock (&priv->lock);
5393 res = func (client, sess, user_data);
5395 g_mutex_lock (&priv->lock);
5397 res = GST_RTSP_FILTER_REF;
5399 changed = (cookie != priv->sessions_cookie);
5402 case GST_RTSP_FILTER_REMOVE:
5403 /* stop watching the session and pretend it went away, if the list was
5404 * changed, we can't use the current list position, try to see if we
5405 * still have the session */
5406 client_unwatch_session (client, sess, changed ? NULL : walk);
5407 cookie = priv->sessions_cookie;
5409 case GST_RTSP_FILTER_REF:
5410 result = g_list_prepend (result, g_object_ref (sess));
5412 case GST_RTSP_FILTER_KEEP:
5419 g_mutex_unlock (&priv->lock);
5422 g_hash_table_unref (visited);
5428 * gst_rtsp_client_set_watch_flushing:
5429 * @client: a #GstRTSPClient
5430 * @val: a boolean value
5432 * sets watch flushing to @val on watch to accet/ignore new messages.
5435 gst_rtsp_client_set_watch_flushing (GstRTSPClient * client, gboolean val)
5437 GstRTSPClientPrivate *priv = NULL;
5438 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
5440 priv = gst_rtsp_client_get_instance_private (client);
5442 /* make sure we unblock/block the backlog and accept/don't accept new messages on the watch */
5443 if (priv->watch != NULL) {
5444 GST_INFO ("Set watch flushing as %d", val);
5445 gst_rtsp_watch_set_flushing (priv->watch, val);