2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3 * Copyright (C) 2015 Centricular Ltd
4 * Author: Sebastian Dröge <sebastian@centricular.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * @short_description: A client connection state
24 * @see_also: #GstRTSPServer, #GstRTSPThreadPool
26 * The client object handles the connection with a client for as long as a TCP
29 * A #GstRTSPClient is created by #GstRTSPServer when a new connection is
30 * accepted and it inherits the #GstRTSPMountPoints, #GstRTSPSessionPool,
31 * #GstRTSPAuth and #GstRTSPThreadPool from the server.
33 * The client connection should be configured with the #GstRTSPConnection using
34 * gst_rtsp_client_set_connection() before it can be attached to a #GMainContext
35 * using gst_rtsp_client_attach(). From then on the client will handle requests
38 * Use gst_rtsp_client_session_filter() to iterate or modify all the
39 * #GstRTSPSession objects managed by the client object.
41 * Last reviewed on 2013-07-11 (1.0.0)
47 #include <gst/sdp/gstmikey.h>
48 #include <gst/rtsp/gstrtsp-enumtypes.h>
50 #include "rtsp-client.h"
52 #include "rtsp-params.h"
54 #define GST_RTSP_CLIENT_GET_PRIVATE(obj) \
55 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_CLIENT, GstRTSPClientPrivate))
58 * send_lock, lock, tunnels_lock
61 struct _GstRTSPClientPrivate
63 GMutex lock; /* protects everything else */
66 GstRTSPConnection *connection;
68 GMainContext *watch_context;
73 GstRTSPClientSendFunc send_func; /* protected by send_lock */
74 gpointer send_data; /* protected by send_lock */
75 GDestroyNotify send_notify; /* protected by send_lock */
77 GstRTSPSessionPool *session_pool;
78 gulong session_removed_id;
79 GstRTSPMountPoints *mount_points;
81 GstRTSPThreadPool *thread_pool;
83 /* used to cache the media in the last requested DESCRIBE so that
84 * we can pick it up in the next SETUP immediately */
88 GHashTable *transports;
90 guint sessions_cookie;
92 gboolean drop_backlog;
94 guint rtsp_ctrl_timeout_id;
95 guint rtsp_ctrl_timeout_cnt;
97 /* The version currently being used */
98 GstRTSPVersion version;
100 GHashTable *pipelined_requests; /* pipelined_request_id -> session_id */
103 static GMutex tunnels_lock;
104 static GHashTable *tunnels; /* protected by tunnels_lock */
106 /* FIXME make this configurable. We don't want to do this yet because it will
107 * be superceeded by a cache object later */
108 #define WATCH_BACKLOG_SIZE 100
110 #define DEFAULT_SESSION_POOL NULL
111 #define DEFAULT_MOUNT_POINTS NULL
112 #define DEFAULT_DROP_BACKLOG TRUE
114 #define RTSP_CTRL_CB_INTERVAL 1
115 #define RTSP_CTRL_TIMEOUT_VALUE 60
130 SIGNAL_PRE_OPTIONS_REQUEST,
131 SIGNAL_OPTIONS_REQUEST,
132 SIGNAL_PRE_DESCRIBE_REQUEST,
133 SIGNAL_DESCRIBE_REQUEST,
134 SIGNAL_PRE_SETUP_REQUEST,
135 SIGNAL_SETUP_REQUEST,
136 SIGNAL_PRE_PLAY_REQUEST,
138 SIGNAL_PRE_PAUSE_REQUEST,
139 SIGNAL_PAUSE_REQUEST,
140 SIGNAL_PRE_TEARDOWN_REQUEST,
141 SIGNAL_TEARDOWN_REQUEST,
142 SIGNAL_PRE_SET_PARAMETER_REQUEST,
143 SIGNAL_SET_PARAMETER_REQUEST,
144 SIGNAL_PRE_GET_PARAMETER_REQUEST,
145 SIGNAL_GET_PARAMETER_REQUEST,
146 SIGNAL_HANDLE_RESPONSE,
148 SIGNAL_PRE_ANNOUNCE_REQUEST,
149 SIGNAL_ANNOUNCE_REQUEST,
150 SIGNAL_PRE_RECORD_REQUEST,
151 SIGNAL_RECORD_REQUEST,
152 SIGNAL_CHECK_REQUIREMENTS,
156 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
157 #define GST_CAT_DEFAULT rtsp_client_debug
159 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
161 static void gst_rtsp_client_get_property (GObject * object, guint propid,
162 GValue * value, GParamSpec * pspec);
163 static void gst_rtsp_client_set_property (GObject * object, guint propid,
164 const GValue * value, GParamSpec * pspec);
165 static void gst_rtsp_client_finalize (GObject * obj);
167 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
168 static gboolean handle_sdp (GstRTSPClient * client, GstRTSPContext * ctx,
169 GstRTSPMedia * media, GstSDPMessage * sdp);
170 static gboolean default_configure_client_media (GstRTSPClient * client,
171 GstRTSPMedia * media, GstRTSPStream * stream, GstRTSPContext * ctx);
172 static gboolean default_configure_client_transport (GstRTSPClient * client,
173 GstRTSPContext * ctx, GstRTSPTransport * ct);
174 static GstRTSPResult default_params_set (GstRTSPClient * client,
175 GstRTSPContext * ctx);
176 static GstRTSPResult default_params_get (GstRTSPClient * client,
177 GstRTSPContext * ctx);
178 static gchar *default_make_path_from_uri (GstRTSPClient * client,
179 const GstRTSPUrl * uri);
180 static void client_session_removed (GstRTSPSessionPool * pool,
181 GstRTSPSession * session, GstRTSPClient * client);
182 static GstRTSPStatusCode default_pre_signal_handler (GstRTSPClient * client,
183 GstRTSPContext * ctx);
184 static gboolean pre_signal_accumulator (GSignalInvocationHint * ihint,
185 GValue * return_accu, const GValue * handler_return, gpointer data);
187 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
190 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
192 GObjectClass *gobject_class;
194 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
196 gobject_class = G_OBJECT_CLASS (klass);
198 gobject_class->get_property = gst_rtsp_client_get_property;
199 gobject_class->set_property = gst_rtsp_client_set_property;
200 gobject_class->finalize = gst_rtsp_client_finalize;
202 klass->create_sdp = create_sdp;
203 klass->handle_sdp = handle_sdp;
204 klass->configure_client_media = default_configure_client_media;
205 klass->configure_client_transport = default_configure_client_transport;
206 klass->params_set = default_params_set;
207 klass->params_get = default_params_get;
208 klass->make_path_from_uri = default_make_path_from_uri;
210 klass->pre_options_request = default_pre_signal_handler;
211 klass->pre_describe_request = default_pre_signal_handler;
212 klass->pre_setup_request = default_pre_signal_handler;
213 klass->pre_play_request = default_pre_signal_handler;
214 klass->pre_pause_request = default_pre_signal_handler;
215 klass->pre_teardown_request = default_pre_signal_handler;
216 klass->pre_set_parameter_request = default_pre_signal_handler;
217 klass->pre_get_parameter_request = default_pre_signal_handler;
218 klass->pre_announce_request = default_pre_signal_handler;
219 klass->pre_record_request = default_pre_signal_handler;
221 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
222 g_param_spec_object ("session-pool", "Session Pool",
223 "The session pool to use for client session",
224 GST_TYPE_RTSP_SESSION_POOL,
225 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
228 g_param_spec_object ("mount-points", "Mount Points",
229 "The mount points to use for client session",
230 GST_TYPE_RTSP_MOUNT_POINTS,
231 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
233 g_object_class_install_property (gobject_class, PROP_DROP_BACKLOG,
234 g_param_spec_boolean ("drop-backlog", "Drop Backlog",
235 "Drop data when the backlog queue is full",
236 DEFAULT_DROP_BACKLOG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
238 gst_rtsp_client_signals[SIGNAL_CLOSED] =
239 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
240 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
241 g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
243 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
244 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
245 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
246 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
249 * GstRTSPClient::pre-options-request:
250 * @client: a #GstRTSPClient
251 * @ctx: a #GstRTSPContext
253 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
254 * otherwise an appropriate return code
258 gst_rtsp_client_signals[SIGNAL_PRE_OPTIONS_REQUEST] =
259 g_signal_new ("pre-options-request", G_TYPE_FROM_CLASS (klass),
260 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
261 pre_options_request), pre_signal_accumulator, NULL,
262 g_cclosure_marshal_generic, GST_TYPE_RTSP_STATUS_CODE, 1,
263 GST_TYPE_RTSP_CONTEXT);
265 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
266 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
267 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
268 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
269 GST_TYPE_RTSP_CONTEXT);
272 * GstRTSPClient::pre-describe-request:
273 * @client: a #GstRTSPClient
274 * @ctx: a #GstRTSPContext
276 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
277 * otherwise an appropriate return code
281 gst_rtsp_client_signals[SIGNAL_PRE_DESCRIBE_REQUEST] =
282 g_signal_new ("pre-describe-request", G_TYPE_FROM_CLASS (klass),
283 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
284 pre_describe_request), pre_signal_accumulator, NULL,
285 g_cclosure_marshal_generic, GST_TYPE_RTSP_STATUS_CODE, 1,
286 GST_TYPE_RTSP_CONTEXT);
288 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
289 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
290 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
291 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
292 GST_TYPE_RTSP_CONTEXT);
295 * GstRTSPClient::pre-setup-request:
296 * @client: a #GstRTSPClient
297 * @ctx: a #GstRTSPContext
299 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
300 * otherwise an appropriate return code
304 gst_rtsp_client_signals[SIGNAL_PRE_SETUP_REQUEST] =
305 g_signal_new ("pre-setup-request", G_TYPE_FROM_CLASS (klass),
306 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
307 pre_setup_request), pre_signal_accumulator, NULL,
308 g_cclosure_marshal_generic, GST_TYPE_RTSP_STATUS_CODE, 1,
309 GST_TYPE_RTSP_CONTEXT);
311 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
312 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
313 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
314 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
315 GST_TYPE_RTSP_CONTEXT);
318 * GstRTSPClient::pre-play-request:
319 * @client: a #GstRTSPClient
320 * @ctx: a #GstRTSPContext
322 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
323 * otherwise an appropriate return code
327 gst_rtsp_client_signals[SIGNAL_PRE_PLAY_REQUEST] =
328 g_signal_new ("pre-play-request", G_TYPE_FROM_CLASS (klass),
329 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
330 pre_play_request), pre_signal_accumulator, NULL,
331 g_cclosure_marshal_generic, GST_TYPE_RTSP_STATUS_CODE, 1,
332 GST_TYPE_RTSP_CONTEXT);
334 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
335 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
336 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
337 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
338 GST_TYPE_RTSP_CONTEXT);
341 * GstRTSPClient::pre-pause-request:
342 * @client: a #GstRTSPClient
343 * @ctx: a #GstRTSPContext
345 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
346 * otherwise an appropriate return code
350 gst_rtsp_client_signals[SIGNAL_PRE_PAUSE_REQUEST] =
351 g_signal_new ("pre-pause-request", G_TYPE_FROM_CLASS (klass),
352 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
353 pre_pause_request), pre_signal_accumulator, NULL,
354 g_cclosure_marshal_generic, GST_TYPE_RTSP_STATUS_CODE, 1,
355 GST_TYPE_RTSP_CONTEXT);
357 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
358 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
359 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
360 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
361 GST_TYPE_RTSP_CONTEXT);
364 * GstRTSPClient::pre-teardown-request:
365 * @client: a #GstRTSPClient
366 * @ctx: a #GstRTSPContext
368 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
369 * otherwise an appropriate return code
373 gst_rtsp_client_signals[SIGNAL_PRE_TEARDOWN_REQUEST] =
374 g_signal_new ("pre-teardown-request", G_TYPE_FROM_CLASS (klass),
375 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
376 pre_teardown_request), pre_signal_accumulator, NULL,
377 g_cclosure_marshal_generic, GST_TYPE_RTSP_STATUS_CODE, 1,
378 GST_TYPE_RTSP_CONTEXT);
380 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
381 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
382 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
383 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
384 GST_TYPE_RTSP_CONTEXT);
387 * GstRTSPClient::pre-set-parameter-request:
388 * @client: a #GstRTSPClient
389 * @ctx: a #GstRTSPContext
391 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
392 * otherwise an appropriate return code
396 gst_rtsp_client_signals[SIGNAL_PRE_SET_PARAMETER_REQUEST] =
397 g_signal_new ("pre-set-parameter-request", G_TYPE_FROM_CLASS (klass),
398 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
399 pre_set_parameter_request), pre_signal_accumulator, NULL,
400 g_cclosure_marshal_generic,
401 GST_TYPE_RTSP_STATUS_CODE, 1, GST_TYPE_RTSP_CONTEXT);
403 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
404 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
405 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
406 set_parameter_request), NULL, NULL, g_cclosure_marshal_generic,
407 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
410 * GstRTSPClient::pre-get-parameter-request:
411 * @client: a #GstRTSPClient
412 * @ctx: 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_GET_PARAMETER_REQUEST] =
420 g_signal_new ("pre-get-parameter-request", G_TYPE_FROM_CLASS (klass),
421 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
422 pre_get_parameter_request), pre_signal_accumulator, NULL,
423 g_cclosure_marshal_generic, GST_TYPE_RTSP_STATUS_CODE, 1,
424 GST_TYPE_RTSP_CONTEXT);
426 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
427 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
428 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
429 get_parameter_request), NULL, NULL, g_cclosure_marshal_generic,
430 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
432 gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE] =
433 g_signal_new ("handle-response", G_TYPE_FROM_CLASS (klass),
434 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
435 handle_response), NULL, NULL, g_cclosure_marshal_generic,
436 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
439 * GstRTSPClient::send-message:
440 * @client: The RTSP client
441 * @session: (type GstRtspServer.RTSPSession): The session
442 * @message: (type GstRtsp.RTSPMessage): The message
444 gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE] =
445 g_signal_new ("send-message", G_TYPE_FROM_CLASS (klass),
446 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
447 send_message), NULL, NULL, g_cclosure_marshal_generic,
448 G_TYPE_NONE, 2, GST_TYPE_RTSP_CONTEXT, G_TYPE_POINTER);
451 * GstRTSPClient::pre-announce-request:
452 * @client: a #GstRTSPClient
453 * @ctx: a #GstRTSPContext
455 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
456 * otherwise an appropriate return code
460 gst_rtsp_client_signals[SIGNAL_PRE_ANNOUNCE_REQUEST] =
461 g_signal_new ("pre-announce-request", G_TYPE_FROM_CLASS (klass),
462 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
463 pre_announce_request), pre_signal_accumulator, NULL,
464 g_cclosure_marshal_generic, GST_TYPE_RTSP_STATUS_CODE, 1,
465 GST_TYPE_RTSP_CONTEXT);
467 gst_rtsp_client_signals[SIGNAL_ANNOUNCE_REQUEST] =
468 g_signal_new ("announce-request", G_TYPE_FROM_CLASS (klass),
469 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, announce_request),
470 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
471 GST_TYPE_RTSP_CONTEXT);
474 * GstRTSPClient::pre-record-request:
475 * @client: a #GstRTSPClient
476 * @ctx: a #GstRTSPContext
478 * Returns: a #GstRTSPStatusCode, GST_RTSP_STS_OK in case of success,
479 * otherwise an appropriate return code
483 gst_rtsp_client_signals[SIGNAL_PRE_RECORD_REQUEST] =
484 g_signal_new ("pre-record-request", G_TYPE_FROM_CLASS (klass),
485 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
486 pre_record_request), pre_signal_accumulator, NULL,
487 g_cclosure_marshal_generic, GST_TYPE_RTSP_STATUS_CODE, 1,
488 GST_TYPE_RTSP_CONTEXT);
490 gst_rtsp_client_signals[SIGNAL_RECORD_REQUEST] =
491 g_signal_new ("record-request", G_TYPE_FROM_CLASS (klass),
492 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, record_request),
493 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
494 GST_TYPE_RTSP_CONTEXT);
497 * GstRTSPClient::check-requirements:
498 * @client: a #GstRTSPClient
499 * @ctx: a #GstRTSPContext
500 * @arr: a NULL-terminated array of strings
502 * Returns: a newly allocated string with comma-separated list of
503 * unsupported options. An empty string must be returned if
504 * all options are supported.
508 gst_rtsp_client_signals[SIGNAL_CHECK_REQUIREMENTS] =
509 g_signal_new ("check-requirements", G_TYPE_FROM_CLASS (klass),
510 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
511 check_requirements), NULL, NULL, g_cclosure_marshal_generic,
512 G_TYPE_STRING, 2, GST_TYPE_RTSP_CONTEXT, G_TYPE_STRV);
515 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
516 g_mutex_init (&tunnels_lock);
518 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
522 gst_rtsp_client_init (GstRTSPClient * client)
524 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
528 g_mutex_init (&priv->lock);
529 g_mutex_init (&priv->send_lock);
530 g_mutex_init (&priv->watch_lock);
532 priv->drop_backlog = DEFAULT_DROP_BACKLOG;
534 g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
536 priv->pipelined_requests = g_hash_table_new_full (g_str_hash,
537 g_str_equal, g_free, g_free);
540 static GstRTSPFilterResult
541 filter_session_media (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
544 gboolean *closed = user_data;
547 gboolean is_all_udp = TRUE;
549 media = gst_rtsp_session_media_get_media (sessmedia);
550 n_streams = gst_rtsp_media_n_streams (media);
552 for (i = 0; i < n_streams; i++) {
553 GstRTSPStreamTransport *transport =
554 gst_rtsp_session_media_get_transport (sessmedia, i);
555 const GstRTSPTransport *rtsp_transport;
560 rtsp_transport = gst_rtsp_stream_transport_get_transport (transport);
562 && rtsp_transport->lower_transport != GST_RTSP_LOWER_TRANS_UDP
563 && rtsp_transport->lower_transport != GST_RTSP_LOWER_TRANS_UDP_MCAST) {
569 if (!is_all_udp || gst_rtsp_media_is_stop_on_disconnect (media)) {
570 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
571 return GST_RTSP_FILTER_REMOVE;
574 return GST_RTSP_FILTER_KEEP;
579 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
581 GstRTSPClientPrivate *priv = client->priv;
583 g_mutex_lock (&priv->lock);
584 /* check if we already know about this session */
585 if (g_list_find (priv->sessions, session) == NULL) {
586 GST_INFO ("watching session %p", session);
588 priv->sessions = g_list_prepend (priv->sessions, g_object_ref (session));
589 priv->sessions_cookie++;
591 /* connect removed session handler, it will be disconnected when the last
592 * session gets removed */
593 if (priv->session_removed_id == 0)
594 priv->session_removed_id = g_signal_connect_data (priv->session_pool,
595 "session-removed", G_CALLBACK (client_session_removed),
596 g_object_ref (client), (GClosureNotify) g_object_unref, 0);
598 g_mutex_unlock (&priv->lock);
603 /* should be called with lock */
605 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session,
608 GstRTSPClientPrivate *priv = client->priv;
610 GST_INFO ("client %p: unwatch session %p", client, session);
613 link = g_list_find (priv->sessions, session);
618 priv->sessions = g_list_delete_link (priv->sessions, link);
619 priv->sessions_cookie++;
621 /* if this was the last session, disconnect the handler.
622 * This will also drop the extra client ref */
623 if (!priv->sessions) {
624 g_signal_handler_disconnect (priv->session_pool, priv->session_removed_id);
625 priv->session_removed_id = 0;
628 if (!priv->drop_backlog) {
629 /* unlink all media managed in this session */
630 gst_rtsp_session_filter (session, filter_session_media, client);
633 /* remove the session */
634 g_object_unref (session);
637 static GstRTSPFilterResult
638 cleanup_session (GstRTSPClient * client, GstRTSPSession * sess,
641 gboolean *closed = user_data;
642 GstRTSPClientPrivate *priv = client->priv;
644 if (priv->drop_backlog) {
645 /* unlink all media managed in this session. This needs to happen
646 * without the client lock, so we really want to do it here. */
647 gst_rtsp_session_filter (sess, filter_session_media, user_data);
651 return GST_RTSP_FILTER_REMOVE;
653 return GST_RTSP_FILTER_KEEP;
657 clean_cached_media (GstRTSPClient * client, gboolean unprepare)
659 GstRTSPClientPrivate *priv = client->priv;
667 gst_rtsp_media_unprepare (priv->media);
668 g_object_unref (priv->media);
673 /* A client is finalized when the connection is broken */
675 gst_rtsp_client_finalize (GObject * obj)
677 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
678 GstRTSPClientPrivate *priv = client->priv;
680 GST_INFO ("finalize client %p", client);
683 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
684 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
687 g_source_destroy ((GSource *) priv->watch);
689 if (priv->watch_context)
690 g_main_context_unref (priv->watch_context);
692 /* all sessions should have been removed by now. We keep a ref to
693 * the client object for the session removed handler. The ref is
694 * dropped when the last session is removed from the list. */
695 g_assert (priv->sessions == NULL);
696 g_assert (priv->session_removed_id == 0);
698 g_hash_table_unref (priv->transports);
700 if (priv->connection)
701 gst_rtsp_connection_free (priv->connection);
702 if (priv->session_pool) {
703 g_object_unref (priv->session_pool);
705 if (priv->mount_points)
706 g_object_unref (priv->mount_points);
708 g_object_unref (priv->auth);
709 if (priv->thread_pool)
710 g_object_unref (priv->thread_pool);
712 clean_cached_media (client, TRUE);
714 g_free (priv->server_ip);
715 g_mutex_clear (&priv->lock);
716 g_mutex_clear (&priv->send_lock);
717 g_mutex_clear (&priv->watch_lock);
719 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
723 gst_rtsp_client_get_property (GObject * object, guint propid,
724 GValue * value, GParamSpec * pspec)
726 GstRTSPClient *client = GST_RTSP_CLIENT (object);
727 GstRTSPClientPrivate *priv = client->priv;
730 case PROP_SESSION_POOL:
731 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
733 case PROP_MOUNT_POINTS:
734 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
736 case PROP_DROP_BACKLOG:
737 g_value_set_boolean (value, priv->drop_backlog);
740 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
745 gst_rtsp_client_set_property (GObject * object, guint propid,
746 const GValue * value, GParamSpec * pspec)
748 GstRTSPClient *client = GST_RTSP_CLIENT (object);
749 GstRTSPClientPrivate *priv = client->priv;
752 case PROP_SESSION_POOL:
753 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
755 case PROP_MOUNT_POINTS:
756 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
758 case PROP_DROP_BACKLOG:
759 g_mutex_lock (&priv->lock);
760 priv->drop_backlog = g_value_get_boolean (value);
761 g_mutex_unlock (&priv->lock);
764 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
769 * gst_rtsp_client_new:
771 * Create a new #GstRTSPClient instance.
773 * Returns: (transfer full): a new #GstRTSPClient
776 gst_rtsp_client_new (void)
778 GstRTSPClient *result;
780 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
786 send_message (GstRTSPClient * client, GstRTSPContext * ctx,
787 GstRTSPMessage * message, gboolean close)
789 GstRTSPClientPrivate *priv = client->priv;
791 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
792 "GStreamer RTSP server");
794 /* remove any previous header */
795 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
797 /* add the new session header for new session ids */
799 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
800 gst_rtsp_session_get_header (ctx->session));
803 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
804 gst_rtsp_message_dump (message);
808 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
811 message->type_data.response.version =
812 ctx->request->type_data.request.version;
814 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE],
817 g_mutex_lock (&priv->send_lock);
819 priv->send_func (client, message, close, priv->send_data);
820 g_mutex_unlock (&priv->send_lock);
822 gst_rtsp_message_unset (message);
826 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
827 GstRTSPContext * ctx)
829 gst_rtsp_message_init_response (ctx->response, code,
830 gst_rtsp_status_as_text (code), ctx->request);
834 send_message (client, ctx, ctx->response, FALSE);
838 send_option_not_supported_response (GstRTSPClient * client,
839 GstRTSPContext * ctx, const gchar * unsupported_options)
841 GstRTSPStatusCode code = GST_RTSP_STS_OPTION_NOT_SUPPORTED;
843 gst_rtsp_message_init_response (ctx->response, code,
844 gst_rtsp_status_as_text (code), ctx->request);
846 if (unsupported_options != NULL) {
847 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_UNSUPPORTED,
848 unsupported_options);
853 send_message (client, ctx, ctx->response, FALSE);
857 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
859 if (path1 == NULL || path2 == NULL)
862 if (strlen (path1) != len2)
865 if (strncmp (path1, path2, len2))
871 /* this function is called to initially find the media for the DESCRIBE request
872 * but is cached for when the same client (without breaking the connection) is
873 * doing a setup for the exact same url. */
874 static GstRTSPMedia *
875 find_media (GstRTSPClient * client, GstRTSPContext * ctx, gchar * path,
878 GstRTSPClientPrivate *priv = client->priv;
879 GstRTSPMediaFactory *factory;
883 /* find the longest matching factory for the uri first */
884 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
888 ctx->factory = factory;
890 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
891 goto no_factory_access;
893 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
899 path_len = strlen (path);
901 if (!paths_are_equal (priv->path, path, path_len)) {
902 /* remove any previously cached values before we try to construct a new
904 clean_cached_media (client, TRUE);
906 /* prepare the media and add it to the pipeline */
907 if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
912 if (!(gst_rtsp_media_get_transport_mode (media) &
913 GST_RTSP_TRANSPORT_MODE_RECORD)) {
914 GstRTSPThread *thread;
916 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
917 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
921 /* prepare the media */
922 if (!gst_rtsp_media_prepare (media, thread))
926 /* now keep track of the uri and the media */
927 priv->path = g_strndup (path, path_len);
930 /* we have seen this path before, used cached media */
933 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
936 g_object_unref (factory);
940 g_object_ref (media);
947 GST_ERROR ("client %p: no factory for path %s", client, path);
948 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
953 g_object_unref (factory);
955 GST_ERROR ("client %p: not authorized to see factory path %s", client,
957 /* error reply is already sent */
962 g_object_unref (factory);
964 GST_ERROR ("client %p: not authorized for factory path %s", client, path);
965 /* error reply is already sent */
970 GST_ERROR ("client %p: can't create media", client);
971 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
972 g_object_unref (factory);
978 GST_ERROR ("client %p: can't create thread", client);
979 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
980 g_object_unref (media);
982 g_object_unref (factory);
988 GST_ERROR ("client %p: can't prepare media", client);
989 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
990 g_object_unref (media);
992 g_object_unref (factory);
999 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
1001 GstRTSPClientPrivate *priv = client->priv;
1002 GstRTSPMessage message = { 0 };
1003 GstRTSPResult res = GST_RTSP_OK;
1004 GstMapInfo map_info;
1008 gst_rtsp_message_init_data (&message, channel);
1010 /* FIXME, need some sort of iovec RTSPMessage here */
1011 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
1014 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
1016 g_mutex_lock (&priv->send_lock);
1017 if (priv->send_func)
1018 res = priv->send_func (client, &message, FALSE, priv->send_data);
1019 g_mutex_unlock (&priv->send_lock);
1021 gst_rtsp_message_steal_body (&message, &data, &usize);
1022 gst_buffer_unmap (buffer, &map_info);
1024 gst_rtsp_message_unset (&message);
1026 return res == GST_RTSP_OK;
1030 * gst_rtsp_client_close:
1031 * @client: a #GstRTSPClient
1033 * Close the connection of @client and remove all media it was managing.
1038 gst_rtsp_client_close (GstRTSPClient * client)
1040 GstRTSPClientPrivate *priv = client->priv;
1041 const gchar *tunnelid;
1043 GST_DEBUG ("client %p: closing connection", client);
1045 if (priv->connection) {
1046 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
1047 g_mutex_lock (&tunnels_lock);
1048 /* remove from tunnelids */
1049 g_hash_table_remove (tunnels, tunnelid);
1050 g_mutex_unlock (&tunnels_lock);
1052 gst_rtsp_connection_close (priv->connection);
1055 /* connection is now closed, destroy the watch which will also cause the
1056 * closed signal to be emitted */
1058 GST_DEBUG ("client %p: destroying watch", client);
1059 g_source_destroy ((GSource *) priv->watch);
1061 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
1062 g_main_context_unref (priv->watch_context);
1063 priv->watch_context = NULL;
1068 default_make_path_from_uri (GstRTSPClient * client, const GstRTSPUrl * uri)
1073 path = g_strconcat (uri->abspath, "?", uri->query, NULL);
1075 path = g_strdup (uri->abspath);
1080 /* Default signal handler function for all "pre-command" signals, like
1081 * pre-options-request. It just returns the RTSP return code 200.
1082 * Subclasses can override this to get another default behaviour.
1084 static GstRTSPStatusCode
1085 default_pre_signal_handler (GstRTSPClient * client, GstRTSPContext * ctx)
1087 GST_LOG_OBJECT (client, "returning GST_RTSP_STS_OK");
1088 return GST_RTSP_STS_OK;
1091 /* The pre-signal accumulator function checks the return value of the signal
1092 * handlers. If any of them returns an RTSP status code that does not start
1093 * with 2 it will return FALSE, no more signal handlers will be called, and
1094 * this last RTSP status code will be the result of the signal emission.
1097 pre_signal_accumulator (GSignalInvocationHint * ihint, GValue * return_accu,
1098 const GValue * handler_return, gpointer data)
1100 GstRTSPStatusCode handler_value = g_value_get_enum (handler_return);
1101 GstRTSPStatusCode accumulated_value = g_value_get_enum (return_accu);
1103 if (handler_value < 200 || handler_value > 299) {
1104 GST_DEBUG ("handler_value : %d, returning FALSE", handler_value);
1105 g_value_set_enum (return_accu, handler_value);
1109 /* the accumulated value is initiated to 0 by GLib. if current handler value is
1110 * bigger then use that instead
1112 * FIXME: Should we prioritize the 2xx codes in a smarter way?
1113 * Like, "201 Created" > "250 Low On Storage Space" > "200 OK"?
1115 if (handler_value > accumulated_value)
1116 g_value_set_enum (return_accu, handler_value);
1122 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
1124 GstRTSPClientPrivate *priv = client->priv;
1125 GstRTSPClientClass *klass;
1126 GstRTSPSession *session;
1127 GstRTSPSessionMedia *sessmedia;
1128 GstRTSPStatusCode code;
1131 gboolean keep_session;
1132 GstRTSPStatusCode sig_result;
1137 session = ctx->session;
1142 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1143 path = klass->make_path_from_uri (client, ctx->uri);
1145 /* get a handle to the configuration of the media in the session */
1146 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1150 /* only aggregate control for now.. */
1151 if (path[matched] != '\0')
1156 ctx->sessmedia = sessmedia;
1158 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_TEARDOWN_REQUEST],
1159 0, ctx, &sig_result);
1160 if (sig_result != GST_RTSP_STS_OK) {
1164 /* we emit the signal before closing the connection */
1165 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
1168 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
1170 /* unmanage the media in the session, returns false if all media session
1172 keep_session = gst_rtsp_session_release_media (session, sessmedia);
1174 /* construct the response now */
1175 code = GST_RTSP_STS_OK;
1176 gst_rtsp_message_init_response (ctx->response, code,
1177 gst_rtsp_status_as_text (code), ctx->request);
1179 send_message (client, ctx, ctx->response, TRUE);
1181 if (!keep_session) {
1182 /* remove the session */
1183 gst_rtsp_session_pool_remove (priv->session_pool, session);
1191 GST_ERROR ("client %p: no session", client);
1192 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1197 GST_ERROR ("client %p: no uri supplied", client);
1198 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1203 GST_ERROR ("client %p: no media for uri", client);
1204 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1210 GST_ERROR ("client %p: no aggregate path %s", client, path);
1211 send_generic_response (client,
1212 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1218 GST_ERROR ("client %p: pre signal returned error: %s", client,
1219 gst_rtsp_status_as_text (sig_result));
1220 send_generic_response (client, sig_result, ctx);
1225 static GstRTSPResult
1226 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
1230 res = gst_rtsp_params_set (client, ctx);
1235 static GstRTSPResult
1236 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
1240 res = gst_rtsp_params_get (client, ctx);
1246 handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
1251 GstRTSPStatusCode sig_result;
1253 g_signal_emit (client,
1254 gst_rtsp_client_signals[SIGNAL_PRE_GET_PARAMETER_REQUEST], 0, ctx,
1256 if (sig_result != GST_RTSP_STS_OK) {
1260 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
1261 if (res != GST_RTSP_OK)
1264 if (size == 0 || !data || strlen ((char *) data) == 0) {
1265 if (ctx->request->type_data.request.version >= GST_RTSP_VERSION_2_0) {
1266 GST_ERROR_OBJECT (client, "Using PLAY request for keep-alive is forbidden"
1271 /* no body (or only '\0'), keep-alive request */
1272 send_generic_response (client, GST_RTSP_STS_OK, ctx);
1274 /* there is a body, handle the params */
1275 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
1276 if (res != GST_RTSP_OK)
1279 send_message (client, ctx, ctx->response, FALSE);
1282 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
1290 GST_ERROR ("client %p: pre signal returned error: %s", client,
1291 gst_rtsp_status_as_text (sig_result));
1292 send_generic_response (client, sig_result, ctx);
1297 GST_ERROR ("client %p: bad request", client);
1298 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1304 handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
1309 GstRTSPStatusCode sig_result;
1311 g_signal_emit (client,
1312 gst_rtsp_client_signals[SIGNAL_PRE_SET_PARAMETER_REQUEST], 0, ctx,
1314 if (sig_result != GST_RTSP_STS_OK) {
1318 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
1319 if (res != GST_RTSP_OK)
1322 if (size == 0 || !data || strlen ((char *) data) == 0) {
1323 /* no body (or only '\0'), keep-alive request */
1324 send_generic_response (client, GST_RTSP_STS_OK, ctx);
1326 /* there is a body, handle the params */
1327 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
1328 if (res != GST_RTSP_OK)
1331 send_message (client, ctx, ctx->response, FALSE);
1334 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
1342 GST_ERROR ("client %p: pre signal returned error: %s", client,
1343 gst_rtsp_status_as_text (sig_result));
1344 send_generic_response (client, sig_result, ctx);
1349 GST_ERROR ("client %p: bad request", client);
1350 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1356 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
1358 GstRTSPSession *session;
1359 GstRTSPClientClass *klass;
1360 GstRTSPSessionMedia *sessmedia;
1361 GstRTSPMedia *media;
1362 GstRTSPStatusCode code;
1363 GstRTSPState rtspstate;
1366 GstRTSPStatusCode sig_result;
1369 if (!(session = ctx->session))
1375 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1376 path = klass->make_path_from_uri (client, ctx->uri);
1378 /* get a handle to the configuration of the media in the session */
1379 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1383 if (path[matched] != '\0')
1388 media = gst_rtsp_session_media_get_media (sessmedia);
1389 n = gst_rtsp_media_n_streams (media);
1390 for (i = 0; i < n; i++) {
1391 GstRTSPStream *stream = gst_rtsp_media_get_stream (media, i);
1393 if (gst_rtsp_stream_get_publish_clock_mode (stream) ==
1394 GST_RTSP_PUBLISH_CLOCK_MODE_CLOCK_AND_OFFSET)
1398 ctx->sessmedia = sessmedia;
1400 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_PAUSE_REQUEST], 0,
1402 if (sig_result != GST_RTSP_STS_OK) {
1406 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1407 /* the session state must be playing or recording */
1408 if (rtspstate != GST_RTSP_STATE_PLAYING &&
1409 rtspstate != GST_RTSP_STATE_RECORDING)
1412 /* then pause sending */
1413 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
1415 /* construct the response now */
1416 code = GST_RTSP_STS_OK;
1417 gst_rtsp_message_init_response (ctx->response, code,
1418 gst_rtsp_status_as_text (code), ctx->request);
1420 send_message (client, ctx, ctx->response, FALSE);
1422 /* the state is now READY */
1423 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1425 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
1432 GST_ERROR ("client %p: no session", client);
1433 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1438 GST_ERROR ("client %p: no uri supplied", client);
1439 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1444 GST_ERROR ("client %p: no media for uri", client);
1445 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1451 GST_ERROR ("client %p: no aggregate path %s", client, path);
1452 send_generic_response (client,
1453 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1459 GST_ERROR ("client %p: pre signal returned error: %s", client,
1460 gst_rtsp_status_as_text (sig_result));
1461 send_generic_response (client, sig_result, ctx);
1466 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
1467 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1473 GST_ERROR ("client %p: pausing not supported", client);
1474 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1479 /* convert @url and @path to a URL used as a content base for the factory
1480 * located at @path */
1482 make_base_url (GstRTSPClient * client, GstRTSPUrl * url, const gchar * path)
1488 /* check for trailing '/' and append one */
1489 trail = (path[strlen (path) - 1] != '/' ? "/" : "");
1494 tmp.abspath = g_strdup_printf ("%s%s", path, trail);
1496 result = gst_rtsp_url_get_request_uri (&tmp);
1497 g_free (tmp.abspath);
1503 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
1505 GstRTSPSession *session;
1506 GstRTSPClientClass *klass;
1507 GstRTSPSessionMedia *sessmedia;
1508 GstRTSPMedia *media;
1509 GstRTSPStatusCode code;
1512 GstRTSPTimeRange *range;
1514 GstRTSPState rtspstate;
1515 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1516 gchar *path, *rtpinfo;
1518 gchar *seek_style = NULL;
1519 GstRTSPStatusCode sig_result;
1521 if (!(session = ctx->session))
1524 if (!(uri = ctx->uri))
1527 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1528 path = klass->make_path_from_uri (client, uri);
1530 /* get a handle to the configuration of the media in the session */
1531 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1535 if (path[matched] != '\0')
1540 ctx->sessmedia = sessmedia;
1541 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1543 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_PLAY_REQUEST], 0,
1545 if (sig_result != GST_RTSP_STS_OK) {
1549 if (!(gst_rtsp_media_get_transport_mode (media) &
1550 GST_RTSP_TRANSPORT_MODE_PLAY))
1551 goto unsupported_mode;
1553 /* the session state must be playing or ready */
1554 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1555 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1558 /* in play we first unsuspend, media could be suspended from SDP or PAUSED */
1559 if (!gst_rtsp_media_unsuspend (media))
1560 goto unsuspend_failed;
1562 /* parse the range header if we have one */
1563 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1564 if (res == GST_RTSP_OK) {
1565 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1566 GstRTSPMediaStatus media_status;
1567 GstSeekFlags flags = 0;
1569 if (gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_SEEK_STYLE,
1571 if (g_strcmp0 (seek_style, "RAP") == 0)
1572 flags = GST_SEEK_FLAG_ACCURATE;
1573 else if (g_strcmp0 (seek_style, "CoRAP") == 0)
1574 flags = GST_SEEK_FLAG_KEY_UNIT;
1575 else if (g_strcmp0 (seek_style, "First-Prior") == 0)
1576 flags = GST_SEEK_FLAG_KEY_UNIT & GST_SEEK_FLAG_SNAP_BEFORE;
1577 else if (g_strcmp0 (seek_style, "Next") == 0)
1578 flags = GST_SEEK_FLAG_KEY_UNIT & GST_SEEK_FLAG_SNAP_AFTER;
1580 GST_FIXME_OBJECT (client, "Add support for seek style %s",
1584 /* we have a range, seek to the position */
1586 gst_rtsp_media_seek_full (media, range, flags);
1587 gst_rtsp_range_free (range);
1589 media_status = gst_rtsp_media_get_status (media);
1590 if (media_status == GST_RTSP_MEDIA_STATUS_ERROR)
1595 /* grab RTPInfo from the media now */
1596 rtpinfo = gst_rtsp_session_media_get_rtpinfo (sessmedia);
1598 /* construct the response now */
1599 code = GST_RTSP_STS_OK;
1600 gst_rtsp_message_init_response (ctx->response, code,
1601 gst_rtsp_status_as_text (code), ctx->request);
1603 /* add the RTP-Info header */
1605 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO,
1608 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_SEEK_STYLE,
1612 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1614 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1616 send_message (client, ctx, ctx->response, FALSE);
1618 /* start playing after sending the response */
1619 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1621 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1623 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1630 GST_ERROR ("client %p: no session", client);
1631 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1636 GST_ERROR ("client %p: no uri supplied", client);
1637 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1642 GST_ERROR ("client %p: media not found", client);
1643 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1648 GST_ERROR ("client %p: no aggregate path %s", client, path);
1649 send_generic_response (client,
1650 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1656 GST_ERROR ("client %p: pre signal returned error: %s", client,
1657 gst_rtsp_status_as_text (sig_result));
1658 send_generic_response (client, sig_result, ctx);
1663 GST_ERROR ("client %p: not PLAYING or READY", client);
1664 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1670 GST_ERROR ("client %p: unsuspend failed", client);
1671 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1676 GST_ERROR ("client %p: seek failed", client);
1677 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1682 GST_ERROR ("client %p: media does not support PLAY", client);
1683 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_ALLOWED, ctx);
1689 do_keepalive (GstRTSPSession * session)
1691 GST_INFO ("keep session %p alive", session);
1692 gst_rtsp_session_touch (session);
1695 /* parse @transport and return a valid transport in @tr. only transports
1696 * supported by @stream are returned. Returns FALSE if no valid transport
1699 parse_transport (const char *transport, GstRTSPStream * stream,
1700 GstRTSPTransport * tr)
1707 gst_rtsp_transport_init (tr);
1709 GST_DEBUG ("parsing transports %s", transport);
1711 transports = g_strsplit (transport, ",", 0);
1713 /* loop through the transports, try to parse */
1714 for (i = 0; transports[i]; i++) {
1715 res = gst_rtsp_transport_parse (transports[i], tr);
1716 if (res != GST_RTSP_OK) {
1717 /* no valid transport, search some more */
1718 GST_WARNING ("could not parse transport %s", transports[i]);
1722 /* we have a transport, see if it's supported */
1723 if (!gst_rtsp_stream_is_transport_supported (stream, tr)) {
1724 GST_WARNING ("unsupported transport %s", transports[i]);
1728 /* we have a valid transport */
1729 GST_INFO ("found valid transport %s", transports[i]);
1734 gst_rtsp_transport_init (tr);
1736 g_strfreev (transports);
1742 default_configure_client_media (GstRTSPClient * client, GstRTSPMedia * media,
1743 GstRTSPStream * stream, GstRTSPContext * ctx)
1745 GstRTSPMessage *request = ctx->request;
1746 gchar *blocksize_str;
1748 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1749 &blocksize_str, 0) == GST_RTSP_OK) {
1753 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1754 if (end == blocksize_str)
1757 /* we don't want to change the mtu when this media
1758 * can be shared because it impacts other clients */
1759 if (gst_rtsp_media_is_shared (media))
1762 if (blocksize > G_MAXUINT)
1763 blocksize = G_MAXUINT;
1765 gst_rtsp_stream_set_mtu (stream, blocksize);
1773 GST_ERROR_OBJECT (client, "failed to parse blocksize");
1774 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1780 default_configure_client_transport (GstRTSPClient * client,
1781 GstRTSPContext * ctx, GstRTSPTransport * ct)
1783 GstRTSPClientPrivate *priv = client->priv;
1785 /* we have a valid transport now, set the destination of the client. */
1786 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1787 gboolean use_client_settings;
1789 use_client_settings =
1790 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1792 if (ct->destination && use_client_settings) {
1793 GstRTSPAddress *addr;
1795 addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1796 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1801 gst_rtsp_address_free (addr);
1803 GstRTSPAddress *addr;
1804 GSocketFamily family;
1806 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1808 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1812 g_free (ct->destination);
1813 ct->destination = g_strdup (addr->address);
1814 ct->port.min = addr->port;
1815 ct->port.max = addr->port + addr->n_ports - 1;
1816 ct->ttl = addr->ttl;
1818 gst_rtsp_address_free (addr);
1823 url = gst_rtsp_connection_get_url (priv->connection);
1824 g_free (ct->destination);
1825 ct->destination = g_strdup (url->host);
1827 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1829 GSocketAddress *addr;
1831 sock = gst_rtsp_connection_get_read_socket (priv->connection);
1832 if ((addr = g_socket_get_remote_address (sock, NULL))) {
1833 /* our read port is the sender port of client */
1834 ct->client_port.min =
1835 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1836 g_object_unref (addr);
1838 if ((addr = g_socket_get_local_address (sock, NULL))) {
1839 ct->server_port.max =
1840 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1841 g_object_unref (addr);
1843 sock = gst_rtsp_connection_get_write_socket (priv->connection);
1844 if ((addr = g_socket_get_remote_address (sock, NULL))) {
1845 /* our write port is the receiver port of client */
1846 ct->client_port.max =
1847 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1848 g_object_unref (addr);
1850 if ((addr = g_socket_get_local_address (sock, NULL))) {
1851 ct->server_port.min =
1852 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1853 g_object_unref (addr);
1855 /* check if the client selected channels for TCP */
1856 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1857 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1867 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1872 static GstRTSPTransport *
1873 make_server_transport (GstRTSPClient * client, GstRTSPMedia * media,
1874 GstRTSPContext * ctx, GstRTSPTransport * ct)
1876 GstRTSPTransport *st;
1878 GSocketFamily family;
1880 /* prepare the server transport */
1881 gst_rtsp_transport_new (&st);
1883 st->trans = ct->trans;
1884 st->profile = ct->profile;
1885 st->lower_transport = ct->lower_transport;
1886 st->mode_play = ct->mode_play;
1887 st->mode_record = ct->mode_record;
1889 addr = g_inet_address_new_from_string (ct->destination);
1892 GST_ERROR ("failed to get inet addr from client destination");
1893 family = G_SOCKET_FAMILY_IPV4;
1895 family = g_inet_address_get_family (addr);
1896 g_object_unref (addr);
1900 switch (st->lower_transport) {
1901 case GST_RTSP_LOWER_TRANS_UDP:
1902 st->client_port = ct->client_port;
1903 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
1905 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1906 st->port = ct->port;
1907 st->destination = g_strdup (ct->destination);
1910 case GST_RTSP_LOWER_TRANS_TCP:
1911 st->interleaved = ct->interleaved;
1912 st->client_port = ct->client_port;
1913 st->server_port = ct->server_port;
1918 if ((gst_rtsp_media_get_transport_mode (media) &
1919 GST_RTSP_TRANSPORT_MODE_PLAY))
1920 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
1925 #define AES_128_KEY_LEN 16
1926 #define AES_256_KEY_LEN 32
1928 #define HMAC_32_KEY_LEN 4
1929 #define HMAC_80_KEY_LEN 10
1932 mikey_apply_policy (GstCaps * caps, GstMIKEYMessage * msg, guint8 policy)
1934 const gchar *srtp_cipher;
1935 const gchar *srtp_auth;
1936 const GstMIKEYPayload *sp;
1939 /* loop over Security policy until we find one containing policy */
1941 if ((sp = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, i)) == NULL)
1944 if (((GstMIKEYPayloadSP *) sp)->policy == policy)
1948 /* the default ciphers */
1949 srtp_cipher = "aes-128-icm";
1950 srtp_auth = "hmac-sha1-80";
1952 /* now override the defaults with what is in the Security Policy */
1956 /* collect all the params and go over them */
1957 len = gst_mikey_payload_sp_get_n_params (sp);
1958 for (i = 0; i < len; i++) {
1959 const GstMIKEYPayloadSPParam *param =
1960 gst_mikey_payload_sp_get_param (sp, i);
1962 switch (param->type) {
1963 case GST_MIKEY_SP_SRTP_ENC_ALG:
1964 switch (param->val[0]) {
1966 srtp_cipher = "null";
1970 srtp_cipher = "aes-128-icm";
1976 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
1977 switch (param->val[0]) {
1978 case AES_128_KEY_LEN:
1979 srtp_cipher = "aes-128-icm";
1981 case AES_256_KEY_LEN:
1982 srtp_cipher = "aes-256-icm";
1988 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1989 switch (param->val[0]) {
1995 srtp_auth = "hmac-sha1-80";
2001 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
2002 switch (param->val[0]) {
2003 case HMAC_32_KEY_LEN:
2004 srtp_auth = "hmac-sha1-32";
2006 case HMAC_80_KEY_LEN:
2007 srtp_auth = "hmac-sha1-80";
2013 case GST_MIKEY_SP_SRTP_SRTP_ENC:
2015 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
2022 /* now configure the SRTP parameters */
2023 gst_caps_set_simple (caps,
2024 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
2025 "srtp-auth", G_TYPE_STRING, srtp_auth,
2026 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
2027 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
2033 handle_mikey_data (GstRTSPClient * client, GstRTSPContext * ctx,
2034 guint8 * data, gsize size)
2036 GstMIKEYMessage *msg;
2038 GstCaps *caps = NULL;
2039 GstMIKEYPayloadKEMAC *kemac;
2040 const GstMIKEYPayloadKeyData *pkd;
2043 /* the MIKEY message contains a CSB or crypto session bundle. It is a
2044 * set of Crypto Sessions protected with the same master key.
2045 * In the context of SRTP, an RTP and its RTCP stream is part of a
2047 if ((msg = gst_mikey_message_new_from_data (data, size, NULL, NULL)) == NULL)
2050 /* we can only handle SRTP crypto sessions for now */
2051 if (msg->map_type != GST_MIKEY_MAP_TYPE_SRTP)
2052 goto invalid_map_type;
2054 /* get the number of crypto sessions. This maps SSRC to its
2055 * security parameters */
2056 n_cs = gst_mikey_message_get_n_cs (msg);
2058 goto no_crypto_sessions;
2060 /* we also need keys */
2061 if (!(kemac = (GstMIKEYPayloadKEMAC *) gst_mikey_message_find_payload
2062 (msg, GST_MIKEY_PT_KEMAC, 0)))
2065 /* we don't support encrypted keys */
2066 if (kemac->enc_alg != GST_MIKEY_ENC_NULL
2067 || kemac->mac_alg != GST_MIKEY_MAC_NULL)
2068 goto unsupported_encryption;
2070 /* get Key data sub-payload */
2071 pkd = (const GstMIKEYPayloadKeyData *)
2072 gst_mikey_payload_kemac_get_sub (&kemac->pt, 0);
2075 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
2078 /* go over all crypto sessions and create the security policy for each
2080 for (i = 0; i < n_cs; i++) {
2081 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
2083 caps = gst_caps_new_simple ("application/x-srtp",
2084 "ssrc", G_TYPE_UINT, map->ssrc,
2085 "roc", G_TYPE_UINT, map->roc, "srtp-key", GST_TYPE_BUFFER, key, NULL);
2086 mikey_apply_policy (caps, msg, map->policy);
2088 gst_rtsp_stream_update_crypto (ctx->stream, map->ssrc, caps);
2089 gst_caps_unref (caps);
2091 gst_mikey_message_unref (msg);
2092 gst_buffer_unref (key);
2099 GST_DEBUG_OBJECT (client, "failed to parse MIKEY message");
2104 GST_DEBUG_OBJECT (client, "invalid map type %d", msg->map_type);
2105 goto cleanup_message;
2109 GST_DEBUG_OBJECT (client, "no crypto sessions");
2110 goto cleanup_message;
2114 GST_DEBUG_OBJECT (client, "no keys found");
2115 goto cleanup_message;
2117 unsupported_encryption:
2119 GST_DEBUG_OBJECT (client, "unsupported key encryption");
2120 goto cleanup_message;
2124 gst_mikey_message_unref (msg);
2129 #define IS_STRIP_CHAR(c) (g_ascii_isspace ((guchar)(c)) || ((c) == '\"'))
2132 strip_chars (gchar * str)
2139 if (!IS_STRIP_CHAR (str[len]))
2143 for (s = str; *s && IS_STRIP_CHAR (*s); s++);
2144 memmove (str, s, len + 1);
2147 /* KeyMgmt = "KeyMgmt" ":" key-mgmt-spec 0*("," key-mgmt-spec)
2148 * key-mgmt-spec = "prot" "=" KMPID ";" ["uri" "=" %x22 URI %x22 ";"]
2151 handle_keymgmt (GstRTSPClient * client, GstRTSPContext * ctx, gchar * keymgmt)
2156 specs = g_strsplit (keymgmt, ",", 0);
2157 for (i = 0; specs[i]; i++) {
2160 split = g_strsplit (specs[i], ";", 0);
2161 for (j = 0; split[j]; j++) {
2162 g_strstrip (split[j]);
2163 if (g_str_has_prefix (split[j], "prot=")) {
2164 g_strstrip (split[j] + 5);
2165 if (!g_str_equal (split[j] + 5, "mikey"))
2167 GST_DEBUG ("found mikey");
2168 } else if (g_str_has_prefix (split[j], "uri=")) {
2169 strip_chars (split[j] + 4);
2170 GST_DEBUG ("found uri '%s'", split[j] + 4);
2171 } else if (g_str_has_prefix (split[j], "data=")) {
2174 strip_chars (split[j] + 5);
2175 GST_DEBUG ("found data '%s'", split[j] + 5);
2176 data = g_base64_decode_inplace (split[j] + 5, &size);
2177 handle_mikey_data (client, ctx, data, size);
2187 rtsp_ctrl_timeout_cb (gpointer user_data)
2189 gboolean res = G_SOURCE_CONTINUE;
2190 GstRTSPClient *client = (GstRTSPClient *) user_data;
2191 GstRTSPClientPrivate *priv = client->priv;
2193 priv->rtsp_ctrl_timeout_cnt += RTSP_CTRL_CB_INTERVAL;
2195 if (priv->rtsp_ctrl_timeout_cnt > RTSP_CTRL_TIMEOUT_VALUE) {
2196 GST_DEBUG ("rtsp control session timeout id=%u expired, closing client.",
2197 priv->rtsp_ctrl_timeout_id);
2198 g_mutex_lock (&priv->lock);
2199 priv->rtsp_ctrl_timeout_id = 0;
2200 priv->rtsp_ctrl_timeout_cnt = 0;
2201 g_mutex_unlock (&priv->lock);
2202 gst_rtsp_client_close (client);
2204 res = G_SOURCE_REMOVE;
2211 rtsp_ctrl_timeout_remove (GstRTSPClientPrivate * priv)
2213 g_mutex_lock (&priv->lock);
2215 if (priv->rtsp_ctrl_timeout_id != 0) {
2216 g_source_destroy (g_main_context_find_source_by_id (priv->watch_context,
2217 priv->rtsp_ctrl_timeout_id));
2218 GST_DEBUG ("rtsp control session removed timeout id=%u.",
2219 priv->rtsp_ctrl_timeout_id);
2220 priv->rtsp_ctrl_timeout_id = 0;
2221 priv->rtsp_ctrl_timeout_cnt = 0;
2224 g_mutex_unlock (&priv->lock);
2228 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
2230 GstRTSPClientPrivate *priv = client->priv;
2233 gchar *transport, *keymgmt;
2234 GstRTSPTransport *ct, *st;
2235 GstRTSPStatusCode code;
2236 GstRTSPSession *session;
2237 GstRTSPStreamTransport *trans;
2239 GstRTSPSessionMedia *sessmedia;
2240 GstRTSPMedia *media;
2241 GstRTSPStream *stream;
2242 GstRTSPState rtspstate;
2243 GstRTSPClientClass *klass;
2244 gchar *path, *control = NULL;
2246 gboolean new_session = FALSE;
2247 GstRTSPStatusCode sig_result;
2248 gchar *pipelined_request_id = NULL, *accept_range = NULL;
2254 klass = GST_RTSP_CLIENT_GET_CLASS (client);
2255 path = klass->make_path_from_uri (client, uri);
2257 /* parse the transport */
2259 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
2261 if (res != GST_RTSP_OK)
2264 /* Handle Pipelined-requests if using >= 2.0 */
2265 if (ctx->request->type_data.request.version >= GST_RTSP_VERSION_2_0)
2266 gst_rtsp_message_get_header (ctx->request,
2267 GST_RTSP_HDR_PIPELINED_REQUESTS, &pipelined_request_id, 0);
2269 /* we create the session after parsing stuff so that we don't make
2270 * a session for malformed requests */
2271 if (priv->session_pool == NULL)
2274 session = ctx->session;
2277 g_object_ref (session);
2278 /* get a handle to the configuration of the media in the session, this can
2279 * return NULL if this is a new url to manage in this session. */
2280 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
2282 /* we need a new media configuration in this session */
2286 /* we have no session media, find one and manage it */
2287 if (sessmedia == NULL) {
2288 /* get a handle to the configuration of the media in the session */
2289 media = find_media (client, ctx, path, &matched);
2290 /* need to suspend the media, if the protocol has changed */
2292 gst_rtsp_media_suspend (media);
2294 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
2295 g_object_ref (media);
2297 goto media_not_found;
2299 /* no media, not found then */
2301 goto media_not_found_no_reply;
2303 if (path[matched] == '\0') {
2304 if (gst_rtsp_media_n_streams (media) == 1) {
2305 stream = gst_rtsp_media_get_stream (media, 0);
2307 goto control_not_found;
2310 /* path is what matched. */
2311 path[matched] = '\0';
2312 /* control is remainder */
2313 control = &path[matched + 1];
2315 /* find the stream now using the control part */
2316 stream = gst_rtsp_media_find_stream (media, control);
2320 goto stream_not_found;
2322 /* now we have a uri identifying a valid media and stream */
2323 ctx->stream = stream;
2326 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_SETUP_REQUEST], 0,
2328 if (sig_result != GST_RTSP_STS_OK) {
2332 if (session == NULL) {
2333 /* create a session if this fails we probably reached our session limit or
2335 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
2336 goto service_unavailable;
2338 /* Pipelined requests should be cleared between sessions */
2339 g_hash_table_remove_all (priv->pipelined_requests);
2341 /* make sure this client is closed when the session is closed */
2342 client_watch_session (client, session);
2345 /* signal new session */
2346 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
2349 ctx->session = session;
2352 if (pipelined_request_id) {
2353 g_hash_table_insert (client->priv->pipelined_requests,
2354 g_strdup (pipelined_request_id),
2355 g_strdup (gst_rtsp_session_get_sessionid (session)));
2357 rtsp_ctrl_timeout_remove (priv);
2359 if (!klass->configure_client_media (client, media, stream, ctx))
2360 goto configure_media_failed_no_reply;
2362 gst_rtsp_transport_new (&ct);
2364 /* parse and find a usable supported transport */
2365 if (!parse_transport (transport, stream, ct))
2366 goto unsupported_transports;
2369 && !(gst_rtsp_media_get_transport_mode (media) &
2370 GST_RTSP_TRANSPORT_MODE_PLAY)) || (ct->mode_record
2371 && !(gst_rtsp_media_get_transport_mode (media) &
2372 GST_RTSP_TRANSPORT_MODE_RECORD)))
2373 goto unsupported_mode;
2375 /* parse the keymgmt */
2376 if (gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_KEYMGMT,
2377 &keymgmt, 0) == GST_RTSP_OK) {
2378 if (!handle_keymgmt (client, ctx, keymgmt))
2382 if (gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT_RANGES,
2383 &accept_range, 0) == GST_RTSP_OK) {
2384 GEnumValue *runit = NULL;
2386 gchar **valid_ranges;
2387 GEnumClass *runit_class = g_type_class_ref (GST_TYPE_RTSP_RANGE_UNIT);
2389 gst_rtsp_message_dump (ctx->request);
2390 valid_ranges = g_strsplit (accept_range, ",", -1);
2392 for (i = 0; valid_ranges[i]; i++) {
2393 gchar *range = valid_ranges[i];
2395 while (*range == ' ')
2398 runit = g_enum_get_value_by_nick (runit_class, range);
2402 g_strfreev (valid_ranges);
2403 g_type_class_unref (runit_class);
2406 goto unsupported_range_unit;
2409 if (sessmedia == NULL) {
2410 /* manage the media in our session now, if not done already */
2412 gst_rtsp_session_manage_media (session, path, g_object_ref (media));
2413 /* if we stil have no media, error */
2414 if (sessmedia == NULL)
2415 goto sessmedia_unavailable;
2417 /* don't cache media anymore */
2418 clean_cached_media (client, FALSE);
2421 ctx->sessmedia = sessmedia;
2423 /* update the client transport */
2424 if (!klass->configure_client_transport (client, ctx, ct))
2425 goto unsupported_client_transport;
2427 /* set in the session media transport */
2428 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
2432 /* configure the url used to set this transport, this we will use when
2433 * generating the response for the PLAY request */
2434 gst_rtsp_stream_transport_set_url (trans, uri);
2435 /* configure keepalive for this transport */
2436 gst_rtsp_stream_transport_set_keepalive (trans,
2437 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
2439 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2440 /* our callbacks to send data on this TCP connection */
2441 gst_rtsp_stream_transport_set_callbacks (trans,
2442 (GstRTSPSendFunc) do_send_data,
2443 (GstRTSPSendFunc) do_send_data, client, NULL);
2445 g_hash_table_insert (priv->transports,
2446 GINT_TO_POINTER (ct->interleaved.min), trans);
2447 g_object_ref (trans);
2448 g_hash_table_insert (priv->transports,
2449 GINT_TO_POINTER (ct->interleaved.max), trans);
2450 g_object_ref (trans);
2453 /* create and serialize the server transport */
2454 st = make_server_transport (client, media, ctx, ct);
2455 trans_str = gst_rtsp_transport_as_text (st);
2456 gst_rtsp_transport_free (st);
2458 /* construct the response now */
2459 code = GST_RTSP_STS_OK;
2460 gst_rtsp_message_init_response (ctx->response, code,
2461 gst_rtsp_status_as_text (code), ctx->request);
2463 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
2467 if (pipelined_request_id)
2468 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PIPELINED_REQUESTS,
2469 pipelined_request_id);
2471 if (ctx->request->type_data.request.version >= GST_RTSP_VERSION_2_0) {
2472 GstClockTimeDiff seekable = gst_rtsp_media_seekable (media);
2473 GString *media_properties = g_string_new (NULL);
2476 g_string_append (media_properties,
2477 "No-Seeking,Time-Progressing,Time-Duration=0.0");
2478 else if (seekable == 0)
2479 g_string_append (media_properties, "Beginning-Only");
2480 else if (seekable == G_MAXINT64)
2481 g_string_append (media_properties, "Random-Access");
2483 g_string_append_printf (media_properties,
2484 "Random-Access=%f, Unlimited, Immutable",
2485 (gdouble) seekable / GST_SECOND);
2487 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_MEDIA_PROPERTIES,
2488 g_string_free (media_properties, FALSE));
2489 /* TODO Check how Accept-Ranges should be filled */
2490 gst_rtsp_message_add_header (ctx->request, GST_RTSP_HDR_ACCEPT_RANGES,
2491 "npt, clock, smpte, clock");
2494 send_message (client, ctx, ctx->response, FALSE);
2496 /* update the state */
2497 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
2498 switch (rtspstate) {
2499 case GST_RTSP_STATE_PLAYING:
2500 case GST_RTSP_STATE_RECORDING:
2501 case GST_RTSP_STATE_READY:
2502 /* no state change */
2505 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
2508 g_object_unref (media);
2509 g_object_unref (session);
2512 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
2519 GST_ERROR ("client %p: no uri", client);
2520 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2525 GST_ERROR ("client %p: no transport", client);
2526 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
2531 GST_ERROR ("client %p: no session pool configured", client);
2532 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2535 media_not_found_no_reply:
2537 GST_ERROR ("client %p: media '%s' not found", client, path);
2538 /* error reply is already sent */
2539 goto cleanup_session;
2543 GST_ERROR ("client %p: media '%s' not found", client, path);
2544 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2545 goto cleanup_session;
2549 GST_ERROR ("client %p: no control in path '%s'", client, path);
2550 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2551 g_object_unref (media);
2552 goto cleanup_session;
2556 GST_ERROR ("client %p: stream '%s' not found", client,
2557 GST_STR_NULL (control));
2558 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2559 g_object_unref (media);
2560 goto cleanup_session;
2564 GST_ERROR ("client %p: pre signal returned error: %s", client,
2565 gst_rtsp_status_as_text (sig_result));
2566 send_generic_response (client, sig_result, ctx);
2567 g_object_unref (media);
2570 service_unavailable:
2572 GST_ERROR ("client %p: can't create session", client);
2573 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2574 g_object_unref (media);
2575 goto cleanup_session;
2577 sessmedia_unavailable:
2579 GST_ERROR ("client %p: can't create session media", client);
2580 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2581 goto cleanup_transport;
2583 configure_media_failed_no_reply:
2585 GST_ERROR ("client %p: configure_media failed", client);
2586 g_object_unref (media);
2587 /* error reply is already sent */
2588 goto cleanup_session;
2590 unsupported_transports:
2592 GST_ERROR ("client %p: unsupported transports", client);
2593 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
2594 goto cleanup_transport;
2596 unsupported_client_transport:
2598 GST_ERROR ("client %p: unsupported client transport", client);
2599 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
2600 goto cleanup_transport;
2604 GST_ERROR ("client %p: unsupported mode (media play: %d, media record: %d, "
2605 "mode play: %d, mode record: %d)", client,
2606 ! !(gst_rtsp_media_get_transport_mode (media) &
2607 GST_RTSP_TRANSPORT_MODE_PLAY),
2608 ! !(gst_rtsp_media_get_transport_mode (media) &
2609 GST_RTSP_TRANSPORT_MODE_RECORD), ct->mode_play, ct->mode_record);
2610 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
2611 goto cleanup_transport;
2613 unsupported_range_unit:
2615 GST_ERROR ("Client %p: does not support any range format we support",
2617 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
2618 goto cleanup_transport;
2622 GST_ERROR ("client %p: keymgmt error", client);
2623 send_generic_response (client, GST_RTSP_STS_KEY_MANAGEMENT_FAILURE, ctx);
2624 goto cleanup_transport;
2628 gst_rtsp_transport_free (ct);
2630 g_object_unref (media);
2633 gst_rtsp_session_pool_remove (priv->session_pool, session);
2635 g_object_unref (session);
2642 static GstSDPMessage *
2643 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
2645 GstRTSPClientPrivate *priv = client->priv;
2649 guint64 session_id_tmp;
2650 gchar session_id[21];
2652 gst_sdp_message_new (&sdp);
2654 /* some standard things first */
2655 gst_sdp_message_set_version (sdp, "0");
2662 session_id_tmp = (((guint64) g_random_int ()) << 32) | g_random_int ();
2663 g_snprintf (session_id, sizeof (session_id), "%" G_GUINT64_FORMAT,
2666 gst_sdp_message_set_origin (sdp, "-", session_id, "1", "IN", proto,
2669 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
2670 gst_sdp_message_set_information (sdp, "rtsp-server");
2671 gst_sdp_message_add_time (sdp, "0", "0", NULL);
2672 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
2673 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
2674 gst_sdp_message_add_attribute (sdp, "control", "*");
2676 info.is_ipv6 = priv->is_ipv6;
2677 info.server_ip = priv->server_ip;
2679 /* create an SDP for the media object */
2680 if (!gst_rtsp_media_setup_sdp (media, sdp, &info))
2688 GST_ERROR ("client %p: could not create SDP", client);
2689 gst_sdp_message_free (sdp);
2694 /* for the describe we must generate an SDP */
2696 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
2698 GstRTSPClientPrivate *priv = client->priv;
2703 GstRTSPMedia *media;
2704 GstRTSPClientClass *klass;
2705 GstRTSPStatusCode sig_result;
2707 klass = GST_RTSP_CLIENT_GET_CLASS (client);
2712 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_DESCRIBE_REQUEST],
2713 0, ctx, &sig_result);
2714 if (sig_result != GST_RTSP_STS_OK) {
2718 /* check what kind of format is accepted, we don't really do anything with it
2719 * and always return SDP for now. */
2724 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
2726 if (res == GST_RTSP_ENOTIMPL)
2729 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
2733 if (!priv->mount_points)
2734 goto no_mount_points;
2736 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
2739 /* find the media object for the uri */
2740 if (!(media = find_media (client, ctx, path, NULL)))
2743 if (!(gst_rtsp_media_get_transport_mode (media) &
2744 GST_RTSP_TRANSPORT_MODE_PLAY))
2745 goto unsupported_mode;
2747 /* create an SDP for the media object on this client */
2748 if (!(sdp = klass->create_sdp (client, media)))
2751 /* we suspend after the describe */
2752 gst_rtsp_media_suspend (media);
2753 g_object_unref (media);
2755 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2756 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2758 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
2761 /* content base for some clients that might screw up creating the setup uri */
2762 str = make_base_url (client, ctx->uri, path);
2765 GST_INFO ("adding content-base: %s", str);
2766 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE, str);
2768 /* add SDP to the response body */
2769 str = gst_sdp_message_as_text (sdp);
2770 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
2771 gst_sdp_message_free (sdp);
2773 send_message (client, ctx, ctx->response, FALSE);
2775 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
2783 GST_ERROR ("client %p: pre signal returned error: %s", client,
2784 gst_rtsp_status_as_text (sig_result));
2785 send_generic_response (client, sig_result, ctx);
2790 GST_ERROR ("client %p: no uri", client);
2791 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2796 GST_ERROR ("client %p: no mount points configured", client);
2797 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2802 GST_ERROR ("client %p: can't find path for url", client);
2803 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2808 GST_ERROR ("client %p: no media", client);
2810 /* error reply is already sent */
2815 GST_ERROR ("client %p: media does not support DESCRIBE", client);
2816 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_ALLOWED, ctx);
2818 g_object_unref (media);
2823 GST_ERROR ("client %p: can't create SDP", client);
2824 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2826 g_object_unref (media);
2832 handle_sdp (GstRTSPClient * client, GstRTSPContext * ctx, GstRTSPMedia * media,
2833 GstSDPMessage * sdp)
2835 GstRTSPClientPrivate *priv = client->priv;
2836 GstRTSPThread *thread;
2838 /* create an SDP for the media object */
2839 if (!gst_rtsp_media_handle_sdp (media, sdp))
2842 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
2843 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
2847 /* prepare the media */
2848 if (!gst_rtsp_media_prepare (media, thread))
2856 GST_ERROR ("client %p: could not handle SDP", client);
2861 GST_ERROR ("client %p: can't create thread", client);
2866 GST_ERROR ("client %p: can't prepare media", client);
2872 handle_announce_request (GstRTSPClient * client, GstRTSPContext * ctx)
2874 GstRTSPClientPrivate *priv = client->priv;
2875 GstRTSPClientClass *klass;
2878 GstRTSPMedia *media;
2879 gchar *path, *cont = NULL;
2882 GstRTSPStatusCode sig_result;
2884 klass = GST_RTSP_CLIENT_GET_CLASS (client);
2889 if (!priv->mount_points)
2890 goto no_mount_points;
2892 /* check if reply is SDP */
2893 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_CONTENT_TYPE, &cont,
2895 /* could not be set but since the request returned OK, we assume it
2896 * was SDP, else check it. */
2898 if (g_ascii_strcasecmp (cont, "application/sdp") != 0)
2899 goto wrong_content_type;
2902 /* get message body and parse as SDP */
2903 gst_rtsp_message_get_body (ctx->request, &data, &size);
2904 if (data == NULL || size == 0)
2907 GST_DEBUG ("client %p: parse SDP...", client);
2908 gst_sdp_message_new (&sdp);
2909 sres = gst_sdp_message_parse_buffer (data, size, sdp);
2910 if (sres != GST_SDP_OK)
2911 goto sdp_parse_failed;
2913 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
2916 /* find the media object for the uri */
2917 if (!(media = find_media (client, ctx, path, NULL)))
2922 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_ANNOUNCE_REQUEST],
2923 0, ctx, &sig_result);
2924 if (sig_result != GST_RTSP_STS_OK) {
2928 if (!(gst_rtsp_media_get_transport_mode (media) &
2929 GST_RTSP_TRANSPORT_MODE_RECORD))
2930 goto unsupported_mode;
2932 /* Tell client subclass about the media */
2933 if (!klass->handle_sdp (client, ctx, media, sdp))
2936 /* we suspend after the announce */
2937 gst_rtsp_media_suspend (media);
2938 g_object_unref (media);
2940 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2941 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2943 send_message (client, ctx, ctx->response, FALSE);
2945 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_ANNOUNCE_REQUEST],
2948 gst_sdp_message_free (sdp);
2954 GST_ERROR ("client %p: no uri", client);
2955 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2960 GST_ERROR ("client %p: no mount points configured", client);
2961 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2966 GST_ERROR ("client %p: can't find path for url", client);
2967 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2968 gst_sdp_message_free (sdp);
2973 GST_ERROR ("client %p: unknown content type", client);
2974 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2979 GST_ERROR ("client %p: can't find SDP message", client);
2980 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2985 GST_ERROR ("client %p: failed to parse SDP message", client);
2986 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2987 gst_sdp_message_free (sdp);
2992 GST_ERROR ("client %p: no media", client);
2994 /* error reply is already sent */
2995 gst_sdp_message_free (sdp);
3000 GST_ERROR ("client %p: pre signal returned error: %s", client,
3001 gst_rtsp_status_as_text (sig_result));
3002 send_generic_response (client, sig_result, ctx);
3003 gst_sdp_message_free (sdp);
3008 GST_ERROR ("client %p: media does not support ANNOUNCE", client);
3009 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_ALLOWED, ctx);
3011 g_object_unref (media);
3012 gst_sdp_message_free (sdp);
3017 GST_ERROR ("client %p: can't handle SDP", client);
3018 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_MEDIA_TYPE, ctx);
3020 g_object_unref (media);
3021 gst_sdp_message_free (sdp);
3027 handle_record_request (GstRTSPClient * client, GstRTSPContext * ctx)
3029 GstRTSPSession *session;
3030 GstRTSPClientClass *klass;
3031 GstRTSPSessionMedia *sessmedia;
3032 GstRTSPMedia *media;
3034 GstRTSPState rtspstate;
3037 GstRTSPStatusCode sig_result;
3039 if (!(session = ctx->session))
3042 if (!(uri = ctx->uri))
3045 klass = GST_RTSP_CLIENT_GET_CLASS (client);
3046 path = klass->make_path_from_uri (client, uri);
3048 /* get a handle to the configuration of the media in the session */
3049 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
3053 if (path[matched] != '\0')
3058 ctx->sessmedia = sessmedia;
3059 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
3061 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_RECORD_REQUEST], 0,
3063 if (sig_result != GST_RTSP_STS_OK) {
3067 if (!(gst_rtsp_media_get_transport_mode (media) &
3068 GST_RTSP_TRANSPORT_MODE_RECORD))
3069 goto unsupported_mode;
3071 /* the session state must be playing or ready */
3072 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
3073 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
3076 /* in play we first unsuspend, media could be suspended from SDP or PAUSED */
3077 if (!gst_rtsp_media_unsuspend (media))
3078 goto unsuspend_failed;
3080 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
3081 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
3083 send_message (client, ctx, ctx->response, FALSE);
3085 /* start playing after sending the response */
3086 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
3088 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
3090 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_RECORD_REQUEST], 0,
3098 GST_ERROR ("client %p: no session", client);
3099 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
3104 GST_ERROR ("client %p: no uri supplied", client);
3105 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
3110 GST_ERROR ("client %p: media not found", client);
3111 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
3116 GST_ERROR ("client %p: no aggregate path %s", client, path);
3117 send_generic_response (client,
3118 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
3124 GST_ERROR ("client %p: pre signal returned error: %s", client,
3125 gst_rtsp_status_as_text (sig_result));
3126 send_generic_response (client, sig_result, ctx);
3131 GST_ERROR ("client %p: media does not support RECORD", client);
3132 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_ALLOWED, ctx);
3137 GST_ERROR ("client %p: not PLAYING or READY", client);
3138 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
3144 GST_ERROR ("client %p: unsuspend failed", client);
3145 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
3151 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx,
3152 GstRTSPVersion version)
3154 GstRTSPMethod options;
3156 GstRTSPStatusCode sig_result;
3158 options = GST_RTSP_DESCRIBE |
3163 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
3165 if (version < GST_RTSP_VERSION_2_0) {
3166 options |= GST_RTSP_RECORD;
3167 options |= GST_RTSP_ANNOUNCE;
3170 str = gst_rtsp_options_as_text (options);
3172 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
3173 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
3175 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
3178 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PRE_OPTIONS_REQUEST], 0,
3180 if (sig_result != GST_RTSP_STS_OK) {
3184 send_message (client, ctx, ctx->response, FALSE);
3186 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
3194 GST_ERROR ("client %p: pre signal returned error: %s", client,
3195 gst_rtsp_status_as_text (sig_result));
3196 send_generic_response (client, sig_result, ctx);
3197 gst_rtsp_message_free (ctx->response);
3202 /* remove duplicate and trailing '/' */
3204 sanitize_uri (GstRTSPUrl * uri)
3208 gboolean have_slash, prev_slash;
3210 s = d = uri->abspath;
3211 len = strlen (uri->abspath);
3215 for (i = 0; i < len; i++) {
3216 have_slash = s[i] == '/';
3218 if (!have_slash || !prev_slash)
3220 prev_slash = have_slash;
3222 len = d - uri->abspath;
3223 /* don't remove the first slash if that's the only thing left */
3224 if (len > 1 && *(d - 1) == '/')
3229 /* is called when the session is removed from its session pool. */
3231 client_session_removed (GstRTSPSessionPool * pool, GstRTSPSession * session,
3232 GstRTSPClient * client)
3234 GstRTSPClientPrivate *priv = client->priv;
3236 GST_INFO ("client %p: session %p removed", client, session);
3238 g_mutex_lock (&priv->lock);
3239 if (priv->watch != NULL)
3240 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 0);
3241 client_unwatch_session (client, session, NULL);
3242 if (priv->watch != NULL)
3243 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
3244 g_mutex_unlock (&priv->lock);
3247 /* Check for Require headers. Returns TRUE if there are no Require headers,
3248 * otherwise lets the application decide which headers are supported.
3249 * By default all headers are unsupported.
3250 * If there are unsupported options, FALSE will be returned together with
3251 * a newly-allocated string of (comma-separated) unsupported options in
3252 * the unsupported_reqs variable.
3254 * There may be multiple Require headers, but we must send one single
3255 * Unsupported header with all the unsupported options as response. If
3256 * an incoming Require header contained a comma-separated list of options
3257 * GstRtspConnection will already have split that list up into multiple
3261 check_request_requirements (GstRTSPContext * ctx, gchar ** unsupported_reqs)
3264 GPtrArray *arr = NULL;
3265 GstRTSPMessage *msg = ctx->request;
3268 gchar *sig_result = NULL;
3269 gboolean result = TRUE;
3273 res = gst_rtsp_message_get_header (msg, GST_RTSP_HDR_REQUIRE, &reqs, i++);
3275 if (res == GST_RTSP_ENOTIMPL)
3279 arr = g_ptr_array_new_with_free_func ((GDestroyNotify) g_free);
3281 g_ptr_array_add (arr, g_strdup (reqs));
3285 /* if we don't have any Require headers at all, all is fine */
3289 /* otherwise we've now processed at all the Require headers */
3290 g_ptr_array_add (arr, NULL);
3292 g_signal_emit (ctx->client,
3293 gst_rtsp_client_signals[SIGNAL_CHECK_REQUIREMENTS], 0, ctx,
3294 (gchar **) arr->pdata, &sig_result);
3296 if (sig_result == NULL) {
3297 /* no supported options, just report all of the required ones as
3299 *unsupported_reqs = g_strjoinv (", ", (gchar **) arr->pdata);
3304 if (strlen (sig_result) == 0)
3305 g_free (sig_result);
3307 *unsupported_reqs = sig_result;
3312 g_ptr_array_unref (arr);
3317 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
3319 GstRTSPClientPrivate *priv = client->priv;
3320 GstRTSPMethod method;
3321 const gchar *uristr;
3322 GstRTSPUrl *uri = NULL;
3323 GstRTSPVersion version;
3325 GstRTSPSession *session = NULL;
3326 GstRTSPContext sctx = { NULL }, *ctx;
3327 GstRTSPMessage response = { 0 };
3328 gchar *unsupported_reqs = NULL;
3329 gchar *sessid = NULL, *pipelined_request_id = NULL;
3331 if (!(ctx = gst_rtsp_context_get_current ())) {
3333 ctx->auth = priv->auth;
3334 gst_rtsp_context_push_current (ctx);
3337 ctx->conn = priv->connection;
3338 ctx->client = client;
3339 ctx->request = request;
3340 ctx->response = &response;
3342 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
3343 gst_rtsp_message_dump (request);
3346 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
3348 GST_INFO ("client %p: received a request %s %s %s", client,
3349 gst_rtsp_method_as_text (method), uristr,
3350 gst_rtsp_version_as_text (version));
3352 /* we can only handle 1.0 requests */
3353 if (version != GST_RTSP_VERSION_1_0 && version != GST_RTSP_VERSION_2_0)
3356 ctx->method = method;
3358 /* we always try to parse the url first */
3359 if (strcmp (uristr, "*") == 0) {
3360 /* special case where we have * as uri, keep uri = NULL */
3361 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
3362 /* check if the uristr is an absolute path <=> scheme and host information
3366 scheme = g_uri_parse_scheme (uristr);
3367 if (scheme == NULL && g_str_has_prefix (uristr, "/")) {
3368 gchar *absolute_uristr = NULL;
3370 GST_WARNING_OBJECT (client, "request doesn't contain absolute url");
3371 if (priv->server_ip == NULL) {
3372 GST_WARNING_OBJECT (client, "host information missing");
3377 g_strdup_printf ("rtsp://%s%s", priv->server_ip, uristr);
3379 GST_DEBUG_OBJECT (client, "absolute url: %s", absolute_uristr);
3380 if (gst_rtsp_url_parse (absolute_uristr, &uri) != GST_RTSP_OK) {
3381 g_free (absolute_uristr);
3384 g_free (absolute_uristr);
3391 /* get the session if there is any */
3392 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_PIPELINED_REQUESTS,
3393 &pipelined_request_id, 0);
3394 if (res == GST_RTSP_OK) {
3395 sessid = g_hash_table_lookup (client->priv->pipelined_requests,
3396 pipelined_request_id);
3399 res = GST_RTSP_ERROR;
3402 if (res != GST_RTSP_OK)
3404 gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
3406 if (res == GST_RTSP_OK) {
3407 if (priv->session_pool == NULL)
3410 /* we had a session in the request, find it again */
3411 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
3412 goto session_not_found;
3414 /* we add the session to the client list of watched sessions. When a session
3415 * disappears because it times out, we will be notified. If all sessions are
3416 * gone, we will close the connection */
3417 client_watch_session (client, session);
3420 /* sanitize the uri */
3424 ctx->session = session;
3426 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
3427 goto not_authorized;
3429 /* handle any 'Require' headers */
3430 if (!check_request_requirements (ctx, &unsupported_reqs))
3431 goto unsupported_requirement;
3433 /* the backlog must be unlimited while processing requests.
3434 * the causes of this are two cases of deadlocks while streaming over TCP:
3436 * 1. consider the scenario where the media pipeline's streaming thread
3437 * is blocking in the appsink (taking the appsink's preroll lock) because
3438 * the backlog is full. when a PAUSE request is received by the RTSP
3439 * client thread then the the state of the session media ought to change
3440 * to PAUSED. while most elements in the pipeline can change state this
3441 * can never happen for the appsink since its preroll lock is taken by
3444 * 2. consider the scenario where the media pipeline's streaming thread
3445 * is blocking in the appsink new_sample callback (taking the send lock
3446 * in RTSP client) because the backlog is full. when e.g. a GET request
3447 * is received by the RTSP client thread then a response ought to be sent
3448 * but this can never happen since it requires taking the send lock
3449 * already taken by another thread.
3451 * the reason that the backlog is never emptied is that the source used
3452 * for dequeing messages from the backlog is never dispatched because it
3453 * is attached to the same mainloop as the source receving RTSP requests and
3454 * therefore run by the RTSP client thread which is alreayd blocking.
3456 * without significant changes the easiest way to cope with this is to
3457 * not block indefinitely when the backlog is full, but rather let the
3458 * backlog grow in size. this in effect means that there can not be any
3459 * upper boundary on its size.
3461 if (priv->watch != NULL)
3462 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 0);
3464 /* now see what is asked and dispatch to a dedicated handler */
3466 case GST_RTSP_OPTIONS:
3467 priv->version = version;
3468 handle_options_request (client, ctx, version);
3470 case GST_RTSP_DESCRIBE:
3471 handle_describe_request (client, ctx);
3473 case GST_RTSP_SETUP:
3474 handle_setup_request (client, ctx);
3477 handle_play_request (client, ctx);
3479 case GST_RTSP_PAUSE:
3480 handle_pause_request (client, ctx);
3482 case GST_RTSP_TEARDOWN:
3483 handle_teardown_request (client, ctx);
3485 case GST_RTSP_SET_PARAMETER:
3486 handle_set_param_request (client, ctx);
3488 case GST_RTSP_GET_PARAMETER:
3489 handle_get_param_request (client, ctx);
3491 case GST_RTSP_ANNOUNCE:
3492 if (version >= GST_RTSP_VERSION_2_0)
3493 goto invalid_command_for_version;
3494 handle_announce_request (client, ctx);
3496 case GST_RTSP_RECORD:
3497 if (version >= GST_RTSP_VERSION_2_0)
3498 goto invalid_command_for_version;
3499 handle_record_request (client, ctx);
3501 case GST_RTSP_REDIRECT:
3502 if (priv->watch != NULL)
3503 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
3504 goto not_implemented;
3505 case GST_RTSP_INVALID:
3507 if (priv->watch != NULL)
3508 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
3512 if (priv->watch != NULL)
3513 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
3517 gst_rtsp_context_pop_current (ctx);
3519 g_object_unref (session);
3521 gst_rtsp_url_free (uri);
3527 GST_ERROR ("client %p: version %d not supported", client, version);
3528 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
3532 invalid_command_for_version:
3534 if (priv->watch != NULL)
3535 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
3537 GST_ERROR ("client %p: invalid command for version", client);
3538 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
3543 GST_ERROR ("client %p: bad request", client);
3544 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
3549 GST_ERROR ("client %p: no pool configured", client);
3550 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
3555 GST_ERROR ("client %p: session not found", client);
3556 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
3561 GST_ERROR ("client %p: not allowed", client);
3562 /* error reply is already sent */
3565 unsupported_requirement:
3567 GST_ERROR ("client %p: Required option is not supported (%s)", client,
3569 send_option_not_supported_response (client, ctx, unsupported_reqs);
3570 g_free (unsupported_reqs);
3575 GST_ERROR ("client %p: method %d not implemented", client, method);
3576 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
3583 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
3585 GstRTSPClientPrivate *priv = client->priv;
3587 GstRTSPSession *session = NULL;
3588 GstRTSPContext sctx = { NULL }, *ctx;
3591 if (!(ctx = gst_rtsp_context_get_current ())) {
3593 ctx->auth = priv->auth;
3594 gst_rtsp_context_push_current (ctx);
3597 ctx->conn = priv->connection;
3598 ctx->client = client;
3599 ctx->request = NULL;
3601 ctx->method = GST_RTSP_INVALID;
3602 ctx->response = response;
3604 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
3605 gst_rtsp_message_dump (response);
3608 GST_INFO ("client %p: received a response", client);
3610 /* get the session if there is any */
3612 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
3613 if (res == GST_RTSP_OK) {
3614 if (priv->session_pool == NULL)
3617 /* we had a session in the request, find it again */
3618 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
3619 goto session_not_found;
3621 /* we add the session to the client list of watched sessions. When a session
3622 * disappears because it times out, we will be notified. If all sessions are
3623 * gone, we will close the connection */
3624 client_watch_session (client, session);
3627 ctx->session = session;
3629 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
3634 gst_rtsp_context_pop_current (ctx);
3636 g_object_unref (session);
3641 GST_ERROR ("client %p: no pool configured", client);
3646 GST_ERROR ("client %p: session not found", client);
3652 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
3654 GstRTSPClientPrivate *priv = client->priv;
3660 GstRTSPStreamTransport *trans;
3662 /* find the stream for this message */
3663 res = gst_rtsp_message_parse_data (message, &channel);
3664 if (res != GST_RTSP_OK)
3667 gst_rtsp_message_get_body (message, &data, &size);
3669 goto invalid_length;
3671 gst_rtsp_message_steal_body (message, &data, &size);
3673 /* Strip trailing \0 (which GstRTSPConnection adds) */
3676 buffer = gst_buffer_new_wrapped (data, size);
3679 g_hash_table_lookup (priv->transports, GINT_TO_POINTER ((gint) channel));
3681 /* dispatch to the stream based on the channel number */
3682 GST_LOG_OBJECT (client, "%u bytes of data on channel %u", size, channel);
3683 gst_rtsp_stream_transport_recv_data (trans, channel, buffer);
3685 GST_DEBUG_OBJECT (client, "received %u bytes of data for "
3686 "unknown channel %u", size, channel);
3687 gst_buffer_unref (buffer);
3695 GST_DEBUG ("client %p: Short message received, ignoring", client);
3701 * gst_rtsp_client_set_session_pool:
3702 * @client: a #GstRTSPClient
3703 * @pool: (transfer none): a #GstRTSPSessionPool
3705 * Set @pool as the sessionpool for @client which it will use to find
3706 * or allocate sessions. the sessionpool is usually inherited from the server
3707 * that created the client but can be overridden later.
3710 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
3711 GstRTSPSessionPool * pool)
3713 GstRTSPSessionPool *old;
3714 GstRTSPClientPrivate *priv;
3716 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
3718 priv = client->priv;
3721 g_object_ref (pool);
3723 g_mutex_lock (&priv->lock);
3724 old = priv->session_pool;
3725 priv->session_pool = pool;
3727 if (priv->session_removed_id) {
3728 g_signal_handler_disconnect (old, priv->session_removed_id);
3729 priv->session_removed_id = 0;
3731 g_mutex_unlock (&priv->lock);
3733 /* FIXME, should remove all sessions from the old pool for this client */
3735 g_object_unref (old);
3739 * gst_rtsp_client_get_session_pool:
3740 * @client: a #GstRTSPClient
3742 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
3744 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
3746 GstRTSPSessionPool *
3747 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
3749 GstRTSPClientPrivate *priv;
3750 GstRTSPSessionPool *result;
3752 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
3754 priv = client->priv;
3756 g_mutex_lock (&priv->lock);
3757 if ((result = priv->session_pool))
3758 g_object_ref (result);
3759 g_mutex_unlock (&priv->lock);
3765 * gst_rtsp_client_set_mount_points:
3766 * @client: a #GstRTSPClient
3767 * @mounts: (transfer none): a #GstRTSPMountPoints
3769 * Set @mounts as the mount points for @client which it will use to map urls
3770 * to media streams. These mount points are usually inherited from the server that
3771 * created the client but can be overriden later.
3774 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
3775 GstRTSPMountPoints * mounts)
3777 GstRTSPClientPrivate *priv;
3778 GstRTSPMountPoints *old;
3780 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
3782 priv = client->priv;
3785 g_object_ref (mounts);
3787 g_mutex_lock (&priv->lock);
3788 old = priv->mount_points;
3789 priv->mount_points = mounts;
3790 g_mutex_unlock (&priv->lock);
3793 g_object_unref (old);
3797 * gst_rtsp_client_get_mount_points:
3798 * @client: a #GstRTSPClient
3800 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
3802 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
3804 GstRTSPMountPoints *
3805 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
3807 GstRTSPClientPrivate *priv;
3808 GstRTSPMountPoints *result;
3810 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
3812 priv = client->priv;
3814 g_mutex_lock (&priv->lock);
3815 if ((result = priv->mount_points))
3816 g_object_ref (result);
3817 g_mutex_unlock (&priv->lock);
3823 * gst_rtsp_client_set_auth:
3824 * @client: a #GstRTSPClient
3825 * @auth: (transfer none): a #GstRTSPAuth
3827 * configure @auth to be used as the authentication manager of @client.
3830 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
3832 GstRTSPClientPrivate *priv;
3835 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
3837 priv = client->priv;
3840 g_object_ref (auth);
3842 g_mutex_lock (&priv->lock);
3845 g_mutex_unlock (&priv->lock);
3848 g_object_unref (old);
3853 * gst_rtsp_client_get_auth:
3854 * @client: a #GstRTSPClient
3856 * Get the #GstRTSPAuth used as the authentication manager of @client.
3858 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
3862 gst_rtsp_client_get_auth (GstRTSPClient * client)
3864 GstRTSPClientPrivate *priv;
3865 GstRTSPAuth *result;
3867 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
3869 priv = client->priv;
3871 g_mutex_lock (&priv->lock);
3872 if ((result = priv->auth))
3873 g_object_ref (result);
3874 g_mutex_unlock (&priv->lock);
3880 * gst_rtsp_client_set_thread_pool:
3881 * @client: a #GstRTSPClient
3882 * @pool: (transfer none): a #GstRTSPThreadPool
3884 * configure @pool to be used as the thread pool of @client.
3887 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
3888 GstRTSPThreadPool * pool)
3890 GstRTSPClientPrivate *priv;
3891 GstRTSPThreadPool *old;
3893 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
3895 priv = client->priv;
3898 g_object_ref (pool);
3900 g_mutex_lock (&priv->lock);
3901 old = priv->thread_pool;
3902 priv->thread_pool = pool;
3903 g_mutex_unlock (&priv->lock);
3906 g_object_unref (old);
3910 * gst_rtsp_client_get_thread_pool:
3911 * @client: a #GstRTSPClient
3913 * Get the #GstRTSPThreadPool used as the thread pool of @client.
3915 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
3919 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
3921 GstRTSPClientPrivate *priv;
3922 GstRTSPThreadPool *result;
3924 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
3926 priv = client->priv;
3928 g_mutex_lock (&priv->lock);
3929 if ((result = priv->thread_pool))
3930 g_object_ref (result);
3931 g_mutex_unlock (&priv->lock);
3937 * gst_rtsp_client_set_connection:
3938 * @client: a #GstRTSPClient
3939 * @conn: (transfer full): a #GstRTSPConnection
3941 * Set the #GstRTSPConnection of @client. This function takes ownership of
3944 * Returns: %TRUE on success.
3947 gst_rtsp_client_set_connection (GstRTSPClient * client,
3948 GstRTSPConnection * conn)
3950 GstRTSPClientPrivate *priv;
3951 GSocket *read_socket;
3952 GSocketAddress *address;
3954 GError *error = NULL;
3956 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
3957 g_return_val_if_fail (conn != NULL, FALSE);
3959 priv = client->priv;
3961 read_socket = gst_rtsp_connection_get_read_socket (conn);
3963 if (!(address = g_socket_get_local_address (read_socket, &error)))
3966 g_free (priv->server_ip);
3967 /* keep the original ip that the client connected to */
3968 if (G_IS_INET_SOCKET_ADDRESS (address)) {
3969 GInetAddress *iaddr;
3971 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
3973 /* socket might be ipv6 but adress still ipv4 */
3974 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
3975 priv->server_ip = g_inet_address_to_string (iaddr);
3976 g_object_unref (address);
3978 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
3979 priv->server_ip = g_strdup ("unknown");
3982 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
3983 priv->server_ip, priv->is_ipv6);
3985 url = gst_rtsp_connection_get_url (conn);
3986 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
3988 priv->connection = conn;
3995 GST_ERROR ("could not get local address %s", error->message);
3996 g_error_free (error);
4002 * gst_rtsp_client_get_connection:
4003 * @client: a #GstRTSPClient
4005 * Get the #GstRTSPConnection of @client.
4007 * Returns: (transfer none): the #GstRTSPConnection of @client.
4008 * The connection object returned remains valid until the client is freed.
4011 gst_rtsp_client_get_connection (GstRTSPClient * client)
4013 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
4015 return client->priv->connection;
4019 * gst_rtsp_client_set_send_func:
4020 * @client: a #GstRTSPClient
4021 * @func: (scope notified): a #GstRTSPClientSendFunc
4022 * @user_data: (closure): user data passed to @func
4023 * @notify: (allow-none): called when @user_data is no longer in use
4025 * Set @func as the callback that will be called when a new message needs to be
4026 * sent to the client. @user_data is passed to @func and @notify is called when
4027 * @user_data is no longer in use.
4029 * By default, the client will send the messages on the #GstRTSPConnection that
4030 * was configured with gst_rtsp_client_attach() was called.
4033 gst_rtsp_client_set_send_func (GstRTSPClient * client,
4034 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
4036 GstRTSPClientPrivate *priv;
4037 GDestroyNotify old_notify;
4040 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
4042 priv = client->priv;
4044 g_mutex_lock (&priv->send_lock);
4045 priv->send_func = func;
4046 old_notify = priv->send_notify;
4047 old_data = priv->send_data;
4048 priv->send_notify = notify;
4049 priv->send_data = user_data;
4050 g_mutex_unlock (&priv->send_lock);
4053 old_notify (old_data);
4057 * gst_rtsp_client_handle_message:
4058 * @client: a #GstRTSPClient
4059 * @message: (transfer none): an #GstRTSPMessage
4061 * Let the client handle @message.
4063 * Returns: a #GstRTSPResult.
4066 gst_rtsp_client_handle_message (GstRTSPClient * client,
4067 GstRTSPMessage * message)
4069 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
4070 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
4072 switch (message->type) {
4073 case GST_RTSP_MESSAGE_REQUEST:
4074 handle_request (client, message);
4076 case GST_RTSP_MESSAGE_RESPONSE:
4077 handle_response (client, message);
4079 case GST_RTSP_MESSAGE_DATA:
4080 handle_data (client, message);
4089 * gst_rtsp_client_send_message:
4090 * @client: a #GstRTSPClient
4091 * @session: (allow-none) (transfer none): a #GstRTSPSession to send
4092 * the message to or %NULL
4093 * @message: (transfer none): The #GstRTSPMessage to send
4095 * Send a message message to the remote end. @message must be a
4096 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
4099 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
4100 GstRTSPMessage * message)
4102 GstRTSPContext sctx = { NULL }
4104 GstRTSPClientPrivate *priv;
4106 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
4107 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
4108 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
4109 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
4111 priv = client->priv;
4113 if (!(ctx = gst_rtsp_context_get_current ())) {
4115 ctx->auth = priv->auth;
4116 gst_rtsp_context_push_current (ctx);
4119 ctx->conn = priv->connection;
4120 ctx->client = client;
4121 ctx->session = session;
4123 send_message (client, ctx, message, FALSE);
4126 gst_rtsp_context_pop_current (ctx);
4132 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
4133 gboolean close, gpointer user_data)
4135 GstRTSPClientPrivate *priv = client->priv;
4143 /* send the response and store the seq number so we can wait until it's
4144 * written to the client to close the connection */
4146 gst_rtsp_watch_send_message (priv->watch, message,
4147 close ? &priv->close_seq : NULL);
4148 if (ret == GST_RTSP_OK)
4151 if (ret != GST_RTSP_ENOMEM)
4155 if (priv->drop_backlog)
4158 /* queue was full, wait for more space */
4159 GST_DEBUG_OBJECT (client, "waiting for backlog");
4160 ret = gst_rtsp_watch_wait_backlog (priv->watch, &time);
4161 GST_DEBUG_OBJECT (client, "Resend due to backlog full");
4162 } while (ret != GST_RTSP_EINTR);
4164 return ret == GST_RTSP_OK;
4169 GST_DEBUG_OBJECT (client, "got error %d", ret);
4170 return ret == GST_RTSP_OK;
4174 static GstRTSPResult
4175 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
4178 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
4181 static GstRTSPResult
4182 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
4184 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
4185 GstRTSPClientPrivate *priv = client->priv;
4187 if (priv->close_seq && priv->close_seq == cseq) {
4188 GST_INFO ("client %p: send close message", client);
4189 priv->close_seq = 0;
4190 gst_rtsp_client_close (client);
4196 static GstRTSPResult
4197 closed (GstRTSPWatch * watch, gpointer user_data)
4199 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
4200 GstRTSPClientPrivate *priv = client->priv;
4201 const gchar *tunnelid;
4203 GST_INFO ("client %p: connection closed", client);
4205 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
4206 g_mutex_lock (&tunnels_lock);
4207 /* remove from tunnelids */
4208 g_hash_table_remove (tunnels, tunnelid);
4209 g_mutex_unlock (&tunnels_lock);
4212 gst_rtsp_watch_set_flushing (watch, TRUE);
4213 g_mutex_lock (&priv->watch_lock);
4214 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
4215 g_mutex_unlock (&priv->watch_lock);
4220 static GstRTSPResult
4221 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
4223 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
4226 str = gst_rtsp_strresult (result);
4227 GST_INFO ("client %p: received an error %s", client, str);
4233 static GstRTSPResult
4234 error_full (GstRTSPWatch * watch, GstRTSPResult result,
4235 GstRTSPMessage * message, guint id, gpointer user_data)
4237 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
4240 str = gst_rtsp_strresult (result);
4242 ("client %p: error when handling message %p with id %d: %s",
4243 client, message, id, str);
4250 remember_tunnel (GstRTSPClient * client)
4252 GstRTSPClientPrivate *priv = client->priv;
4253 const gchar *tunnelid;
4255 /* store client in the pending tunnels */
4256 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
4257 if (tunnelid == NULL)
4260 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
4262 /* we can't have two clients connecting with the same tunnelid */
4263 g_mutex_lock (&tunnels_lock);
4264 if (g_hash_table_lookup (tunnels, tunnelid))
4265 goto tunnel_existed;
4267 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
4268 g_mutex_unlock (&tunnels_lock);
4275 GST_ERROR ("client %p: no tunnelid provided", client);
4280 g_mutex_unlock (&tunnels_lock);
4281 GST_ERROR ("client %p: tunnel session %s already existed", client,
4287 static GstRTSPResult
4288 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
4290 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
4291 GstRTSPClientPrivate *priv = client->priv;
4293 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
4296 /* ignore error, it'll only be a problem when the client does a POST again */
4297 remember_tunnel (client);
4303 handle_tunnel (GstRTSPClient * client)
4305 GstRTSPClientPrivate *priv = client->priv;
4306 GstRTSPClient *oclient;
4307 GstRTSPClientPrivate *opriv;
4308 const gchar *tunnelid;
4310 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
4311 if (tunnelid == NULL)
4314 /* check for previous tunnel */
4315 g_mutex_lock (&tunnels_lock);
4316 oclient = g_hash_table_lookup (tunnels, tunnelid);
4318 if (oclient == NULL) {
4319 /* no previous tunnel, remember tunnel */
4320 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
4321 g_mutex_unlock (&tunnels_lock);
4323 GST_INFO ("client %p: no previous tunnel found, remembering tunnel (%p)",
4324 client, priv->connection);
4326 /* merge both tunnels into the first client */
4327 /* remove the old client from the table. ref before because removing it will
4328 * remove the ref to it. */
4329 g_object_ref (oclient);
4330 g_hash_table_remove (tunnels, tunnelid);
4331 g_mutex_unlock (&tunnels_lock);
4333 opriv = oclient->priv;
4335 g_mutex_lock (&opriv->watch_lock);
4336 if (opriv->watch == NULL)
4339 GST_INFO ("client %p: found previous tunnel %p (old %p, new %p)", client,
4340 oclient, opriv->connection, priv->connection);
4342 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
4343 gst_rtsp_watch_reset (priv->watch);
4344 gst_rtsp_watch_reset (opriv->watch);
4345 g_mutex_unlock (&opriv->watch_lock);
4346 g_object_unref (oclient);
4348 /* the old client owns the tunnel now, the new one will be freed */
4349 g_source_destroy ((GSource *) priv->watch);
4351 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
4359 GST_ERROR ("client %p: no tunnelid provided", client);
4364 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
4365 g_mutex_unlock (&opriv->watch_lock);
4366 g_object_unref (oclient);
4371 static GstRTSPStatusCode
4372 tunnel_get (GstRTSPWatch * watch, gpointer user_data)
4374 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
4376 GST_INFO ("client %p: tunnel get (connection %p)", client,
4377 client->priv->connection);
4379 if (!handle_tunnel (client)) {
4380 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
4383 return GST_RTSP_STS_OK;
4386 static GstRTSPResult
4387 tunnel_post (GstRTSPWatch * watch, gpointer user_data)
4389 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
4391 GST_INFO ("client %p: tunnel post (connection %p)", client,
4392 client->priv->connection);
4394 if (!handle_tunnel (client)) {
4395 return GST_RTSP_ERROR;
4401 static GstRTSPResult
4402 tunnel_http_response (GstRTSPWatch * watch, GstRTSPMessage * request,
4403 GstRTSPMessage * response, gpointer user_data)
4405 GstRTSPClientClass *klass;
4407 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
4408 klass = GST_RTSP_CLIENT_GET_CLASS (client);
4410 if (klass->tunnel_http_response) {
4411 klass->tunnel_http_response (client, request, response);
4417 static GstRTSPWatchFuncs watch_funcs = {
4426 tunnel_http_response
4430 client_watch_notify (GstRTSPClient * client)
4432 GstRTSPClientPrivate *priv = client->priv;
4433 gboolean closed = TRUE;
4435 GST_INFO ("client %p: watch destroyed", client);
4437 /* remove all sessions if the media says so and so drop the extra client ref */
4438 rtsp_ctrl_timeout_remove (priv);
4439 gst_rtsp_client_session_filter (client, cleanup_session, &closed);
4441 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
4442 g_object_unref (client);
4446 * gst_rtsp_client_attach:
4447 * @client: a #GstRTSPClient
4448 * @context: (allow-none): a #GMainContext
4450 * Attaches @client to @context. When the mainloop for @context is run, the
4451 * client will be dispatched. When @context is %NULL, the default context will be
4454 * This function should be called when the client properties and urls are fully
4455 * configured and the client is ready to start.
4457 * Returns: the ID (greater than 0) for the source within the GMainContext.
4460 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
4462 GstRTSPClientPrivate *priv;
4466 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
4467 priv = client->priv;
4468 g_return_val_if_fail (priv->connection != NULL, 0);
4469 g_return_val_if_fail (priv->watch == NULL, 0);
4471 /* make sure noone will free the context before the watch is destroyed */
4472 priv->watch_context = g_main_context_ref (context);
4474 /* create watch for the connection and attach */
4475 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
4476 g_object_ref (client), (GDestroyNotify) client_watch_notify);
4477 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
4478 (GDestroyNotify) gst_rtsp_watch_unref);
4480 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
4482 GST_INFO ("client %p: attaching to context %p", client, context);
4483 res = gst_rtsp_watch_attach (priv->watch, context);
4485 /* Setting up a timeout for the RTSP control channel until a session
4486 * is up where it is handling timeouts. */
4487 rtsp_ctrl_timeout_remove (priv); /* removing old if any */
4488 g_mutex_lock (&priv->lock);
4490 timer_src = g_timeout_source_new_seconds (RTSP_CTRL_CB_INTERVAL);
4491 g_source_set_callback (timer_src, rtsp_ctrl_timeout_cb, client, NULL);
4492 priv->rtsp_ctrl_timeout_id = g_source_attach (timer_src, priv->watch_context);
4493 g_source_unref (timer_src);
4494 GST_DEBUG ("rtsp control setting up session timeout id=%u.",
4495 priv->rtsp_ctrl_timeout_id);
4497 g_mutex_unlock (&priv->lock);
4503 * gst_rtsp_client_session_filter:
4504 * @client: a #GstRTSPClient
4505 * @func: (scope call) (allow-none): a callback
4506 * @user_data: user data passed to @func
4508 * Call @func for each session managed by @client. The result value of @func
4509 * determines what happens to the session. @func will be called with @client
4510 * locked so no further actions on @client can be performed from @func.
4512 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
4515 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
4517 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
4518 * will also be added with an additional ref to the result #GList of this
4521 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each session.
4523 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
4524 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
4525 * element in the #GList should be unreffed before the list is freed.
4528 gst_rtsp_client_session_filter (GstRTSPClient * client,
4529 GstRTSPClientSessionFilterFunc func, gpointer user_data)
4531 GstRTSPClientPrivate *priv;
4532 GList *result, *walk, *next;
4533 GHashTable *visited;
4536 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
4538 priv = client->priv;
4542 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
4544 g_mutex_lock (&priv->lock);
4546 cookie = priv->sessions_cookie;
4547 for (walk = priv->sessions; walk; walk = next) {
4548 GstRTSPSession *sess = walk->data;
4549 GstRTSPFilterResult res;
4552 next = g_list_next (walk);
4555 /* only visit each session once */
4556 if (g_hash_table_contains (visited, sess))
4559 g_hash_table_add (visited, g_object_ref (sess));
4560 g_mutex_unlock (&priv->lock);
4562 res = func (client, sess, user_data);
4564 g_mutex_lock (&priv->lock);
4566 res = GST_RTSP_FILTER_REF;
4568 changed = (cookie != priv->sessions_cookie);
4571 case GST_RTSP_FILTER_REMOVE:
4572 /* stop watching the session and pretend it went away, if the list was
4573 * changed, we can't use the current list position, try to see if we
4574 * still have the session */
4575 client_unwatch_session (client, sess, changed ? NULL : walk);
4576 cookie = priv->sessions_cookie;
4578 case GST_RTSP_FILTER_REF:
4579 result = g_list_prepend (result, g_object_ref (sess));
4581 case GST_RTSP_FILTER_KEEP:
4588 g_mutex_unlock (&priv->lock);
4591 g_hash_table_unref (visited);