2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * @short_description: A client connection state
22 * @see_also: #GstRTSPServer, #GstRTSPThreadPool
24 * The client object handles the connection with a client for as long as a TCP
27 * A #GstRTSPClient is created by #GstRTSPServer when a new connection is
28 * accepted and it inherits the #GstRTSPMountPoints, #GstRTSPSessionPool,
29 * #GstRTSPAuth and #GstRTSPThreadPool from the server.
31 * The client connection should be configured with the #GstRTSPConnection using
32 * gst_rtsp_client_set_connection() before it can be attached to a #GMainContext
33 * using gst_rtsp_client_attach(). From then on the client will handle requests
36 * Use gst_rtsp_client_session_filter() to iterate or modify all the
37 * #GstRTSPSession objects managed by the client object.
39 * Last reviewed on 2013-07-11 (1.0.0)
45 #include <gst/sdp/gstmikey.h>
47 #include "rtsp-client.h"
49 #include "rtsp-params.h"
51 #define GST_RTSP_CLIENT_GET_PRIVATE(obj) \
52 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_CLIENT, GstRTSPClientPrivate))
55 * send_lock, lock, tunnels_lock
58 struct _GstRTSPClientPrivate
60 GMutex lock; /* protects everything else */
62 GstRTSPConnection *connection;
64 GMainContext *watch_context;
69 GstRTSPClientSendFunc send_func; /* protected by send_lock */
70 gpointer send_data; /* protected by send_lock */
71 GDestroyNotify send_notify; /* protected by send_lock */
73 GstRTSPSessionPool *session_pool;
74 gulong session_removed_id;
75 GstRTSPMountPoints *mount_points;
77 GstRTSPThreadPool *thread_pool;
79 /* used to cache the media in the last requested DESCRIBE so that
80 * we can pick it up in the next SETUP immediately */
87 gboolean drop_backlog;
90 static GMutex tunnels_lock;
91 static GHashTable *tunnels; /* protected by tunnels_lock */
93 #define DEFAULT_SESSION_POOL NULL
94 #define DEFAULT_MOUNT_POINTS NULL
95 #define DEFAULT_DROP_BACKLOG TRUE
110 SIGNAL_OPTIONS_REQUEST,
111 SIGNAL_DESCRIBE_REQUEST,
112 SIGNAL_SETUP_REQUEST,
114 SIGNAL_PAUSE_REQUEST,
115 SIGNAL_TEARDOWN_REQUEST,
116 SIGNAL_SET_PARAMETER_REQUEST,
117 SIGNAL_GET_PARAMETER_REQUEST,
118 SIGNAL_HANDLE_RESPONSE,
123 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
124 #define GST_CAT_DEFAULT rtsp_client_debug
126 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
128 static void gst_rtsp_client_get_property (GObject * object, guint propid,
129 GValue * value, GParamSpec * pspec);
130 static void gst_rtsp_client_set_property (GObject * object, guint propid,
131 const GValue * value, GParamSpec * pspec);
132 static void gst_rtsp_client_finalize (GObject * obj);
134 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
135 static void unlink_session_transports (GstRTSPClient * client,
136 GstRTSPSession * session, GstRTSPSessionMedia * sessmedia);
137 static gboolean default_configure_client_media (GstRTSPClient * client,
138 GstRTSPMedia * media, GstRTSPStream * stream, GstRTSPContext * ctx);
139 static gboolean default_configure_client_transport (GstRTSPClient * client,
140 GstRTSPContext * ctx, GstRTSPTransport * ct);
141 static GstRTSPResult default_params_set (GstRTSPClient * client,
142 GstRTSPContext * ctx);
143 static GstRTSPResult default_params_get (GstRTSPClient * client,
144 GstRTSPContext * ctx);
145 static gchar *default_make_path_from_uri (GstRTSPClient * client,
146 const GstRTSPUrl * uri);
148 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
151 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
153 GObjectClass *gobject_class;
155 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
157 gobject_class = G_OBJECT_CLASS (klass);
159 gobject_class->get_property = gst_rtsp_client_get_property;
160 gobject_class->set_property = gst_rtsp_client_set_property;
161 gobject_class->finalize = gst_rtsp_client_finalize;
163 klass->create_sdp = create_sdp;
164 klass->configure_client_media = default_configure_client_media;
165 klass->configure_client_transport = default_configure_client_transport;
166 klass->params_set = default_params_set;
167 klass->params_get = default_params_get;
168 klass->make_path_from_uri = default_make_path_from_uri;
170 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
171 g_param_spec_object ("session-pool", "Session Pool",
172 "The session pool to use for client session",
173 GST_TYPE_RTSP_SESSION_POOL,
174 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
176 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
177 g_param_spec_object ("mount-points", "Mount Points",
178 "The mount points to use for client session",
179 GST_TYPE_RTSP_MOUNT_POINTS,
180 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
182 g_object_class_install_property (gobject_class, PROP_DROP_BACKLOG,
183 g_param_spec_boolean ("drop-backlog", "Drop Backlog",
184 "Drop data when the backlog queue is full",
185 DEFAULT_DROP_BACKLOG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
187 gst_rtsp_client_signals[SIGNAL_CLOSED] =
188 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
189 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
190 g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
192 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
193 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
194 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
195 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
197 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
198 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
199 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
200 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
201 GST_TYPE_RTSP_CONTEXT);
203 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
204 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
205 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
206 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
207 GST_TYPE_RTSP_CONTEXT);
209 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
210 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
211 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
212 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
213 GST_TYPE_RTSP_CONTEXT);
215 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
216 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
217 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
218 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
219 GST_TYPE_RTSP_CONTEXT);
221 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
222 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
223 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
224 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
225 GST_TYPE_RTSP_CONTEXT);
227 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
228 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
229 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
230 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
231 GST_TYPE_RTSP_CONTEXT);
233 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
234 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
235 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
236 set_parameter_request), NULL, NULL, g_cclosure_marshal_generic,
237 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
239 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
240 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
241 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
242 get_parameter_request), NULL, NULL, g_cclosure_marshal_generic,
243 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
245 gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE] =
246 g_signal_new ("handle-response", G_TYPE_FROM_CLASS (klass),
247 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
248 handle_response), NULL, NULL, g_cclosure_marshal_generic,
249 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
252 * GstRTSPClient::send-message:
253 * @client: The RTSP client
254 * @session: (type GstRtspServer.RTSPSession): The session
255 * @message: (type GstRtsp.RTSPMessage): The message
257 gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE] =
258 g_signal_new ("send-message", G_TYPE_FROM_CLASS (klass),
259 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
260 G_TYPE_NONE, 2, GST_TYPE_RTSP_CONTEXT, G_TYPE_POINTER);
263 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
264 g_mutex_init (&tunnels_lock);
266 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
270 gst_rtsp_client_init (GstRTSPClient * client)
272 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
276 g_mutex_init (&priv->lock);
277 g_mutex_init (&priv->send_lock);
279 priv->drop_backlog = DEFAULT_DROP_BACKLOG;
282 static GstRTSPFilterResult
283 filter_session_media (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
286 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
288 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
289 unlink_session_transports (client, sess, sessmedia);
291 /* unmanage the media in the session */
292 return GST_RTSP_FILTER_REMOVE;
296 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
298 GstRTSPClientPrivate *priv = client->priv;
300 g_mutex_lock (&priv->lock);
301 /* check if we already know about this session */
302 if (g_list_find (priv->sessions, session) == NULL) {
303 GST_INFO ("watching session %p", session);
304 priv->sessions = g_list_prepend (priv->sessions, g_object_ref (session));
306 g_mutex_unlock (&priv->lock);
311 /* should be called with lock */
313 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session,
316 GstRTSPClientPrivate *priv = client->priv;
318 GST_INFO ("client %p: unwatch session %p", client, session);
321 link = g_list_find (priv->sessions, session);
325 priv->sessions = g_list_delete_link (priv->sessions, link);
327 /* unlink all media managed in this session */
328 gst_rtsp_session_filter (session, filter_session_media, client);
330 /* remove the session */
331 g_object_unref (session);
334 static GstRTSPFilterResult
335 cleanup_session (GstRTSPClient * client, GstRTSPSession * sess,
338 return GST_RTSP_FILTER_REMOVE;
341 /* A client is finalized when the connection is broken */
343 gst_rtsp_client_finalize (GObject * obj)
345 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
346 GstRTSPClientPrivate *priv = client->priv;
348 GST_INFO ("finalize client %p", client);
351 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
352 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
355 g_source_destroy ((GSource *) priv->watch);
357 if (priv->watch_context)
358 g_main_context_unref (priv->watch_context);
360 gst_rtsp_client_session_filter (client, cleanup_session, NULL);
362 if (priv->connection)
363 gst_rtsp_connection_free (priv->connection);
364 if (priv->session_pool) {
365 g_signal_handler_disconnect (priv->session_pool, priv->session_removed_id);
366 g_object_unref (priv->session_pool);
368 if (priv->mount_points)
369 g_object_unref (priv->mount_points);
371 g_object_unref (priv->auth);
372 if (priv->thread_pool)
373 g_object_unref (priv->thread_pool);
378 gst_rtsp_media_unprepare (priv->media);
379 g_object_unref (priv->media);
382 g_free (priv->server_ip);
383 g_mutex_clear (&priv->lock);
384 g_mutex_clear (&priv->send_lock);
386 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
390 gst_rtsp_client_get_property (GObject * object, guint propid,
391 GValue * value, GParamSpec * pspec)
393 GstRTSPClient *client = GST_RTSP_CLIENT (object);
394 GstRTSPClientPrivate *priv = client->priv;
397 case PROP_SESSION_POOL:
398 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
400 case PROP_MOUNT_POINTS:
401 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
403 case PROP_DROP_BACKLOG:
404 g_value_set_boolean (value, priv->drop_backlog);
407 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
412 gst_rtsp_client_set_property (GObject * object, guint propid,
413 const GValue * value, GParamSpec * pspec)
415 GstRTSPClient *client = GST_RTSP_CLIENT (object);
416 GstRTSPClientPrivate *priv = client->priv;
419 case PROP_SESSION_POOL:
420 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
422 case PROP_MOUNT_POINTS:
423 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
425 case PROP_DROP_BACKLOG:
426 g_mutex_lock (&priv->lock);
427 priv->drop_backlog = g_value_get_boolean (value);
428 g_mutex_unlock (&priv->lock);
431 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
436 * gst_rtsp_client_new:
438 * Create a new #GstRTSPClient instance.
440 * Returns: (transfer full): a new #GstRTSPClient
443 gst_rtsp_client_new (void)
445 GstRTSPClient *result;
447 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
453 send_message (GstRTSPClient * client, GstRTSPContext * ctx,
454 GstRTSPMessage * message, gboolean close)
456 GstRTSPClientPrivate *priv = client->priv;
458 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
459 "GStreamer RTSP server");
461 /* remove any previous header */
462 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
464 /* add the new session header for new session ids */
466 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
467 gst_rtsp_session_get_header (ctx->session));
470 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
471 gst_rtsp_message_dump (message);
475 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
477 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE],
480 g_mutex_lock (&priv->send_lock);
482 priv->send_func (client, message, close, priv->send_data);
483 g_mutex_unlock (&priv->send_lock);
485 gst_rtsp_message_unset (message);
489 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
490 GstRTSPContext * ctx)
492 gst_rtsp_message_init_response (ctx->response, code,
493 gst_rtsp_status_as_text (code), ctx->request);
497 send_message (client, ctx, ctx->response, FALSE);
501 send_option_not_supported_response (GstRTSPClient * client,
502 GstRTSPContext * ctx, const gchar * unsupported_options)
504 GstRTSPStatusCode code = GST_RTSP_STS_OPTION_NOT_SUPPORTED;
506 gst_rtsp_message_init_response (ctx->response, code,
507 gst_rtsp_status_as_text (code), ctx->request);
509 if (unsupported_options != NULL) {
510 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_UNSUPPORTED,
511 unsupported_options);
516 send_message (client, ctx, ctx->response, FALSE);
520 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
522 if (path1 == NULL || path2 == NULL)
525 if (strlen (path1) != len2)
528 if (strncmp (path1, path2, len2))
534 /* this function is called to initially find the media for the DESCRIBE request
535 * but is cached for when the same client (without breaking the connection) is
536 * doing a setup for the exact same url. */
537 static GstRTSPMedia *
538 find_media (GstRTSPClient * client, GstRTSPContext * ctx, gchar * path,
541 GstRTSPClientPrivate *priv = client->priv;
542 GstRTSPMediaFactory *factory;
546 /* find the longest matching factory for the uri first */
547 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
551 ctx->factory = factory;
553 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
554 goto no_factory_access;
556 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
562 path_len = strlen (path);
564 if (!paths_are_equal (priv->path, path, path_len)) {
565 GstRTSPThread *thread;
567 /* remove any previously cached values before we try to construct a new
573 gst_rtsp_media_unprepare (priv->media);
574 g_object_unref (priv->media);
578 /* prepare the media and add it to the pipeline */
579 if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
584 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
585 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
589 /* prepare the media */
590 if (!(gst_rtsp_media_prepare (media, thread)))
593 /* now keep track of the uri and the media */
594 priv->path = g_strndup (path, path_len);
597 /* we have seen this path before, used cached media */
600 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
603 g_object_unref (factory);
607 g_object_ref (media);
614 GST_ERROR ("client %p: no factory for path %s", client, path);
615 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
620 GST_ERROR ("client %p: not authorized to see factory path %s", client,
622 /* error reply is already sent */
627 GST_ERROR ("client %p: not authorized for factory path %s", client, path);
628 /* error reply is already sent */
633 GST_ERROR ("client %p: can't create media", client);
634 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
635 g_object_unref (factory);
641 GST_ERROR ("client %p: can't create thread", client);
642 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
643 g_object_unref (media);
645 g_object_unref (factory);
651 GST_ERROR ("client %p: can't prepare media", client);
652 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
653 g_object_unref (media);
655 g_object_unref (factory);
662 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
664 GstRTSPClientPrivate *priv = client->priv;
665 GstRTSPMessage message = { 0 };
670 gst_rtsp_message_init_data (&message, channel);
672 /* FIXME, need some sort of iovec RTSPMessage here */
673 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
676 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
678 g_mutex_lock (&priv->send_lock);
680 priv->send_func (client, &message, FALSE, priv->send_data);
681 g_mutex_unlock (&priv->send_lock);
683 gst_rtsp_message_steal_body (&message, &data, &usize);
684 gst_buffer_unmap (buffer, &map_info);
686 gst_rtsp_message_unset (&message);
692 link_transport (GstRTSPClient * client, GstRTSPSession * session,
693 GstRTSPStreamTransport * trans)
695 GstRTSPClientPrivate *priv = client->priv;
697 GST_DEBUG ("client %p: linking transport %p", client, trans);
699 gst_rtsp_stream_transport_set_callbacks (trans,
700 (GstRTSPSendFunc) do_send_data,
701 (GstRTSPSendFunc) do_send_data, client, NULL);
703 priv->transports = g_list_prepend (priv->transports, trans);
705 /* make sure our session can't expire */
706 gst_rtsp_session_prevent_expire (session);
710 link_session_transports (GstRTSPClient * client, GstRTSPSession * session,
711 GstRTSPSessionMedia * sessmedia)
716 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
717 for (i = 0; i < n_streams; i++) {
718 GstRTSPStreamTransport *trans;
719 const GstRTSPTransport *tr;
721 /* get the transport, if there is no transport configured, skip this stream */
722 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
726 tr = gst_rtsp_stream_transport_get_transport (trans);
728 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
729 /* for TCP, link the stream to the TCP connection of the client */
730 link_transport (client, session, trans);
736 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
737 GstRTSPStreamTransport * trans)
739 GstRTSPClientPrivate *priv = client->priv;
741 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
743 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
745 priv->transports = g_list_remove (priv->transports, trans);
747 /* our session can now expire */
748 gst_rtsp_session_allow_expire (session);
752 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
753 GstRTSPSessionMedia * sessmedia)
758 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
759 for (i = 0; i < n_streams; i++) {
760 GstRTSPStreamTransport *trans;
761 const GstRTSPTransport *tr;
763 /* get the transport, if there is no transport configured, skip this stream */
764 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
768 tr = gst_rtsp_stream_transport_get_transport (trans);
770 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
771 /* for TCP, unlink the stream from the TCP connection of the client */
772 unlink_transport (client, session, trans);
778 close_connection (GstRTSPClient * client)
780 GstRTSPClientPrivate *priv = client->priv;
781 const gchar *tunnelid;
783 GST_DEBUG ("client %p: closing connection", client);
785 if (priv->connection) {
786 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
787 g_mutex_lock (&tunnels_lock);
788 /* remove from tunnelids */
789 g_hash_table_remove (tunnels, tunnelid);
790 g_mutex_unlock (&tunnels_lock);
792 gst_rtsp_connection_close (priv->connection);
795 /* connection is now closed, destroy the watch which will also cause the
796 * closed signal to be emitted */
798 GST_DEBUG ("client %p: destroying watch", client);
799 g_source_destroy ((GSource *) priv->watch);
801 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
806 default_make_path_from_uri (GstRTSPClient * client, const GstRTSPUrl * uri)
811 path = g_strconcat (uri->abspath, "?", uri->query, NULL);
813 path = g_strdup (uri->abspath);
819 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
821 GstRTSPClientPrivate *priv = client->priv;
822 GstRTSPClientClass *klass;
823 GstRTSPSession *session;
824 GstRTSPSessionMedia *sessmedia;
825 GstRTSPStatusCode code;
828 gboolean keep_session;
833 session = ctx->session;
838 klass = GST_RTSP_CLIENT_GET_CLASS (client);
839 path = klass->make_path_from_uri (client, ctx->uri);
841 /* get a handle to the configuration of the media in the session */
842 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
846 /* only aggregate control for now.. */
847 if (path[matched] != '\0')
852 ctx->sessmedia = sessmedia;
854 /* we emit the signal before closing the connection */
855 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
858 /* make sure we unblock the backlog and don't accept new messages
860 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
862 /* unlink the all TCP callbacks */
863 unlink_session_transports (client, session, sessmedia);
865 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
867 /* allow messages again so that we can send the reply */
868 gst_rtsp_watch_set_flushing (priv->watch, FALSE);
870 /* unmanage the media in the session, returns false if all media session
872 keep_session = gst_rtsp_session_release_media (session, sessmedia);
874 /* construct the response now */
875 code = GST_RTSP_STS_OK;
876 gst_rtsp_message_init_response (ctx->response, code,
877 gst_rtsp_status_as_text (code), ctx->request);
879 send_message (client, ctx, ctx->response, TRUE);
882 /* remove the session */
883 gst_rtsp_session_pool_remove (priv->session_pool, session);
891 GST_ERROR ("client %p: no session", client);
892 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
897 GST_ERROR ("client %p: no uri supplied", client);
898 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
903 GST_ERROR ("client %p: no media for uri", client);
904 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
910 GST_ERROR ("client %p: no aggregate path %s", client, path);
911 send_generic_response (client,
912 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
919 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
923 res = gst_rtsp_params_set (client, ctx);
929 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
933 res = gst_rtsp_params_get (client, ctx);
939 handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
945 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
946 if (res != GST_RTSP_OK)
950 /* no body, keep-alive request */
951 send_generic_response (client, GST_RTSP_STS_OK, ctx);
953 /* there is a body, handle the params */
954 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
955 if (res != GST_RTSP_OK)
958 send_message (client, ctx, ctx->response, FALSE);
961 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
969 GST_ERROR ("client %p: bad request", client);
970 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
976 handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
982 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
983 if (res != GST_RTSP_OK)
987 /* no body, keep-alive request */
988 send_generic_response (client, GST_RTSP_STS_OK, ctx);
990 /* there is a body, handle the params */
991 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
992 if (res != GST_RTSP_OK)
995 send_message (client, ctx, ctx->response, FALSE);
998 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
1006 GST_ERROR ("client %p: bad request", client);
1007 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1013 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
1015 GstRTSPSession *session;
1016 GstRTSPClientClass *klass;
1017 GstRTSPSessionMedia *sessmedia;
1018 GstRTSPStatusCode code;
1019 GstRTSPState rtspstate;
1023 if (!(session = ctx->session))
1029 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1030 path = klass->make_path_from_uri (client, ctx->uri);
1032 /* get a handle to the configuration of the media in the session */
1033 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1037 if (path[matched] != '\0')
1042 ctx->sessmedia = sessmedia;
1044 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1045 /* the session state must be playing or recording */
1046 if (rtspstate != GST_RTSP_STATE_PLAYING &&
1047 rtspstate != GST_RTSP_STATE_RECORDING)
1050 /* unlink the all TCP callbacks */
1051 unlink_session_transports (client, session, sessmedia);
1053 /* then pause sending */
1054 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
1056 /* construct the response now */
1057 code = GST_RTSP_STS_OK;
1058 gst_rtsp_message_init_response (ctx->response, code,
1059 gst_rtsp_status_as_text (code), ctx->request);
1061 send_message (client, ctx, ctx->response, FALSE);
1063 /* the state is now READY */
1064 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1066 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
1073 GST_ERROR ("client %p: no seesion", client);
1074 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1079 GST_ERROR ("client %p: no uri supplied", client);
1080 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1085 GST_ERROR ("client %p: no media for uri", client);
1086 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1092 GST_ERROR ("client %p: no aggregate path %s", client, path);
1093 send_generic_response (client,
1094 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1100 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
1101 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1107 /* convert @url and @path to a URL used as a content base for the factory
1108 * located at @path */
1110 make_base_url (GstRTSPClient * client, GstRTSPUrl * url, const gchar * path)
1116 /* check for trailing '/' and append one */
1117 trail = (path[strlen (path) - 1] != '/' ? "/" : "");
1122 tmp.abspath = g_strdup_printf ("%s%s", path, trail);
1124 result = gst_rtsp_url_get_request_uri (&tmp);
1125 g_free (tmp.abspath);
1131 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
1133 GstRTSPSession *session;
1134 GstRTSPClientClass *klass;
1135 GstRTSPSessionMedia *sessmedia;
1136 GstRTSPMedia *media;
1137 GstRTSPStatusCode code;
1140 GstRTSPTimeRange *range;
1142 GstRTSPState rtspstate;
1143 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1144 gchar *path, *rtpinfo;
1147 if (!(session = ctx->session))
1150 if (!(uri = ctx->uri))
1153 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1154 path = klass->make_path_from_uri (client, uri);
1156 /* get a handle to the configuration of the media in the session */
1157 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1161 if (path[matched] != '\0')
1166 ctx->sessmedia = sessmedia;
1167 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1169 /* the session state must be playing or ready */
1170 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1171 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1174 /* in play we first unsuspend, media could be suspended from SDP or PAUSED */
1175 if (!gst_rtsp_media_unsuspend (media))
1176 goto unsuspend_failed;
1178 /* parse the range header if we have one */
1179 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1180 if (res == GST_RTSP_OK) {
1181 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1182 /* we have a range, seek to the position */
1184 gst_rtsp_media_seek (media, range);
1185 gst_rtsp_range_free (range);
1189 /* link the all TCP callbacks */
1190 link_session_transports (client, session, sessmedia);
1192 /* grab RTPInfo from the media now */
1193 rtpinfo = gst_rtsp_session_media_get_rtpinfo (sessmedia);
1195 /* construct the response now */
1196 code = GST_RTSP_STS_OK;
1197 gst_rtsp_message_init_response (ctx->response, code,
1198 gst_rtsp_status_as_text (code), ctx->request);
1200 /* add the RTP-Info header */
1202 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO,
1206 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1208 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1210 send_message (client, ctx, ctx->response, FALSE);
1212 /* start playing after sending the response */
1213 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1215 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1217 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1224 GST_ERROR ("client %p: no session", client);
1225 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1230 GST_ERROR ("client %p: no uri supplied", client);
1231 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1236 GST_ERROR ("client %p: media not found", client);
1237 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1242 GST_ERROR ("client %p: no aggregate path %s", client, path);
1243 send_generic_response (client,
1244 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1250 GST_ERROR ("client %p: not PLAYING or READY", client);
1251 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1257 GST_ERROR ("client %p: unsuspend failed", client);
1258 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1264 do_keepalive (GstRTSPSession * session)
1266 GST_INFO ("keep session %p alive", session);
1267 gst_rtsp_session_touch (session);
1270 /* parse @transport and return a valid transport in @tr. only transports
1271 * supported by @stream are returned. Returns FALSE if no valid transport
1274 parse_transport (const char *transport, GstRTSPStream * stream,
1275 GstRTSPTransport * tr)
1282 gst_rtsp_transport_init (tr);
1284 GST_DEBUG ("parsing transports %s", transport);
1286 transports = g_strsplit (transport, ",", 0);
1288 /* loop through the transports, try to parse */
1289 for (i = 0; transports[i]; i++) {
1290 res = gst_rtsp_transport_parse (transports[i], tr);
1291 if (res != GST_RTSP_OK) {
1292 /* no valid transport, search some more */
1293 GST_WARNING ("could not parse transport %s", transports[i]);
1297 /* we have a transport, see if it's supported */
1298 if (!gst_rtsp_stream_is_transport_supported (stream, tr)) {
1299 GST_WARNING ("unsupported transport %s", transports[i]);
1303 /* we have a valid transport */
1304 GST_INFO ("found valid transport %s", transports[i]);
1309 gst_rtsp_transport_init (tr);
1311 g_strfreev (transports);
1317 default_configure_client_media (GstRTSPClient * client, GstRTSPMedia * media,
1318 GstRTSPStream * stream, GstRTSPContext * ctx)
1320 GstRTSPMessage *request = ctx->request;
1321 gchar *blocksize_str;
1323 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1324 &blocksize_str, 0) == GST_RTSP_OK) {
1328 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1329 if (end == blocksize_str)
1332 /* we don't want to change the mtu when this media
1333 * can be shared because it impacts other clients */
1334 if (gst_rtsp_media_is_shared (media))
1337 if (blocksize > G_MAXUINT)
1338 blocksize = G_MAXUINT;
1340 gst_rtsp_stream_set_mtu (stream, blocksize);
1348 GST_ERROR_OBJECT (client, "failed to parse blocksize");
1349 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1355 default_configure_client_transport (GstRTSPClient * client,
1356 GstRTSPContext * ctx, GstRTSPTransport * ct)
1358 GstRTSPClientPrivate *priv = client->priv;
1360 /* we have a valid transport now, set the destination of the client. */
1361 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1362 gboolean use_client_settings;
1364 use_client_settings =
1365 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1367 if (ct->destination && use_client_settings) {
1368 GstRTSPAddress *addr;
1370 addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1371 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1376 gst_rtsp_address_free (addr);
1378 GstRTSPAddress *addr;
1379 GSocketFamily family;
1381 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1383 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1387 g_free (ct->destination);
1388 ct->destination = g_strdup (addr->address);
1389 ct->port.min = addr->port;
1390 ct->port.max = addr->port + addr->n_ports - 1;
1391 ct->ttl = addr->ttl;
1393 gst_rtsp_address_free (addr);
1398 url = gst_rtsp_connection_get_url (priv->connection);
1399 g_free (ct->destination);
1400 ct->destination = g_strdup (url->host);
1402 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1404 GSocketAddress *addr;
1406 sock = gst_rtsp_connection_get_read_socket (priv->connection);
1407 if ((addr = g_socket_get_remote_address (sock, NULL))) {
1408 /* our read port is the sender port of client */
1409 ct->client_port.min =
1410 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1411 g_object_unref (addr);
1413 if ((addr = g_socket_get_local_address (sock, NULL))) {
1414 ct->server_port.max =
1415 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1416 g_object_unref (addr);
1418 sock = gst_rtsp_connection_get_write_socket (priv->connection);
1419 if ((addr = g_socket_get_remote_address (sock, NULL))) {
1420 /* our write port is the receiver port of client */
1421 ct->client_port.max =
1422 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1423 g_object_unref (addr);
1425 if ((addr = g_socket_get_local_address (sock, NULL))) {
1426 ct->server_port.min =
1427 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1428 g_object_unref (addr);
1430 /* check if the client selected channels for TCP */
1431 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1432 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1442 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1447 static GstRTSPTransport *
1448 make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
1449 GstRTSPTransport * ct)
1451 GstRTSPTransport *st;
1453 GSocketFamily family;
1455 /* prepare the server transport */
1456 gst_rtsp_transport_new (&st);
1458 st->trans = ct->trans;
1459 st->profile = ct->profile;
1460 st->lower_transport = ct->lower_transport;
1462 addr = g_inet_address_new_from_string (ct->destination);
1465 GST_ERROR ("failed to get inet addr from client destination");
1466 family = G_SOCKET_FAMILY_IPV4;
1468 family = g_inet_address_get_family (addr);
1469 g_object_unref (addr);
1473 switch (st->lower_transport) {
1474 case GST_RTSP_LOWER_TRANS_UDP:
1475 st->client_port = ct->client_port;
1476 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
1478 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1479 st->port = ct->port;
1480 st->destination = g_strdup (ct->destination);
1483 case GST_RTSP_LOWER_TRANS_TCP:
1484 st->interleaved = ct->interleaved;
1485 st->client_port = ct->client_port;
1486 st->server_port = ct->server_port;
1491 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
1496 #define AES_128_KEY_LEN 16
1497 #define AES_256_KEY_LEN 32
1499 #define HMAC_32_KEY_LEN 4
1500 #define HMAC_80_KEY_LEN 10
1503 mikey_apply_policy (GstCaps * caps, GstMIKEYMessage * msg, guint8 policy)
1505 const gchar *srtp_cipher;
1506 const gchar *srtp_auth;
1507 const GstMIKEYPayload *sp;
1510 /* loop over Security policy until we find one containing policy */
1512 if ((sp = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, i)) == NULL)
1515 if (((GstMIKEYPayloadSP *) sp)->policy == policy)
1519 /* the default ciphers */
1520 srtp_cipher = "aes-128-icm";
1521 srtp_auth = "hmac-sha1-80";
1523 /* now override the defaults with what is in the Security Policy */
1527 /* collect all the params and go over them */
1528 len = gst_mikey_payload_sp_get_n_params (sp);
1529 for (i = 0; i < len; i++) {
1530 const GstMIKEYPayloadSPParam *param =
1531 gst_mikey_payload_sp_get_param (sp, i);
1533 switch (param->type) {
1534 case GST_MIKEY_SP_SRTP_ENC_ALG:
1535 switch (param->val[0]) {
1537 srtp_cipher = "null";
1541 srtp_cipher = "aes-128-icm";
1547 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
1548 switch (param->val[0]) {
1549 case AES_128_KEY_LEN:
1550 srtp_cipher = "aes-128-icm";
1552 case AES_256_KEY_LEN:
1553 srtp_cipher = "aes-256-icm";
1559 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1560 switch (param->val[0]) {
1566 srtp_auth = "hmac-sha1-80";
1572 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
1573 switch (param->val[0]) {
1574 case HMAC_32_KEY_LEN:
1575 srtp_auth = "hmac-sha1-32";
1577 case HMAC_80_KEY_LEN:
1578 srtp_auth = "hmac-sha1-80";
1584 case GST_MIKEY_SP_SRTP_SRTP_ENC:
1586 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
1593 /* now configure the SRTP parameters */
1594 gst_caps_set_simple (caps,
1595 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
1596 "srtp-auth", G_TYPE_STRING, srtp_auth,
1597 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
1598 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
1604 handle_mikey_data (GstRTSPClient * client, GstRTSPContext * ctx,
1605 guint8 * data, gsize size)
1607 GstMIKEYMessage *msg;
1609 GstCaps *caps = NULL;
1610 GstMIKEYPayloadKEMAC *kemac;
1611 const GstMIKEYPayloadKeyData *pkd;
1614 /* the MIKEY message contains a CSB or crypto session bundle. It is a
1615 * set of Crypto Sessions protected with the same master key.
1616 * In the context of SRTP, an RTP and its RTCP stream is part of a
1618 if ((msg = gst_mikey_message_new_from_data (data, size, NULL, NULL)) == NULL)
1621 /* we can only handle SRTP crypto sessions for now */
1622 if (msg->map_type != GST_MIKEY_MAP_TYPE_SRTP)
1623 goto invalid_map_type;
1625 /* get the number of crypto sessions. This maps SSRC to its
1626 * security parameters */
1627 n_cs = gst_mikey_message_get_n_cs (msg);
1629 goto no_crypto_sessions;
1631 /* we also need keys */
1632 if (!(kemac = (GstMIKEYPayloadKEMAC *) gst_mikey_message_find_payload
1633 (msg, GST_MIKEY_PT_KEMAC, 0)))
1636 /* we don't support encrypted keys */
1637 if (kemac->enc_alg != GST_MIKEY_ENC_NULL
1638 || kemac->mac_alg != GST_MIKEY_MAC_NULL)
1639 goto unsupported_encryption;
1641 /* get Key data sub-payload */
1642 pkd = (const GstMIKEYPayloadKeyData *)
1643 gst_mikey_payload_kemac_get_sub (&kemac->pt, 0);
1646 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
1649 /* go over all crypto sessions and create the security policy for each
1651 for (i = 0; i < n_cs; i++) {
1652 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
1654 caps = gst_caps_new_simple ("application/x-srtp",
1655 "ssrc", G_TYPE_UINT, map->ssrc,
1656 "roc", G_TYPE_UINT, map->roc, "srtp-key", GST_TYPE_BUFFER, key, NULL);
1657 mikey_apply_policy (caps, msg, map->policy);
1659 gst_rtsp_stream_update_crypto (ctx->stream, map->ssrc, caps);
1660 gst_caps_unref (caps);
1662 gst_mikey_message_unref (msg);
1669 GST_DEBUG_OBJECT (client, "failed to parse MIKEY message");
1674 GST_DEBUG_OBJECT (client, "invalid map type %d", msg->map_type);
1675 goto cleanup_message;
1679 GST_DEBUG_OBJECT (client, "no crypto sessions");
1680 goto cleanup_message;
1684 GST_DEBUG_OBJECT (client, "no keys found");
1685 goto cleanup_message;
1687 unsupported_encryption:
1689 GST_DEBUG_OBJECT (client, "unsupported key encryption");
1690 goto cleanup_message;
1694 gst_mikey_message_unref (msg);
1699 #define IS_STRIP_CHAR(c) (g_ascii_isspace ((guchar)(c)) || ((c) == '\"'))
1702 strip_chars (gchar * str)
1709 if (!IS_STRIP_CHAR (str[len]))
1713 for (s = str; *s && IS_STRIP_CHAR (*s); s++);
1714 memmove (str, s, len + 1);
1717 /* KeyMgmt = "KeyMgmt" ":" key-mgmt-spec 0*("," key-mgmt-spec)
1718 * key-mgmt-spec = "prot" "=" KMPID ";" ["uri" "=" %x22 URI %x22 ";"]
1721 handle_keymgmt (GstRTSPClient * client, GstRTSPContext * ctx, gchar * keymgmt)
1726 specs = g_strsplit (keymgmt, ",", 0);
1727 for (i = 0; specs[i]; i++) {
1730 split = g_strsplit (specs[i], ";", 0);
1731 for (j = 0; split[j]; j++) {
1732 g_strstrip (split[j]);
1733 if (g_str_has_prefix (split[j], "prot=")) {
1734 g_strstrip (split[j] + 5);
1735 if (!g_str_equal (split[j] + 5, "mikey"))
1737 GST_DEBUG ("found mikey");
1738 } else if (g_str_has_prefix (split[j], "uri=")) {
1739 strip_chars (split[j] + 4);
1740 GST_DEBUG ("found uri '%s'", split[j] + 4);
1741 } else if (g_str_has_prefix (split[j], "data=")) {
1744 strip_chars (split[j] + 5);
1745 GST_DEBUG ("found data '%s'", split[j] + 5);
1746 data = g_base64_decode_inplace (split[j] + 5, &size);
1747 handle_mikey_data (client, ctx, data, size);
1755 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
1757 GstRTSPClientPrivate *priv = client->priv;
1760 gchar *transport, *keymgmt;
1761 GstRTSPTransport *ct, *st;
1762 GstRTSPStatusCode code;
1763 GstRTSPSession *session;
1764 GstRTSPStreamTransport *trans;
1766 GstRTSPSessionMedia *sessmedia;
1767 GstRTSPMedia *media;
1768 GstRTSPStream *stream;
1769 GstRTSPState rtspstate;
1770 GstRTSPClientClass *klass;
1771 gchar *path, *control;
1773 gboolean new_session = FALSE;
1779 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1780 path = klass->make_path_from_uri (client, uri);
1782 /* parse the transport */
1784 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
1786 if (res != GST_RTSP_OK)
1789 /* we create the session after parsing stuff so that we don't make
1790 * a session for malformed requests */
1791 if (priv->session_pool == NULL)
1794 session = ctx->session;
1797 g_object_ref (session);
1798 /* get a handle to the configuration of the media in the session, this can
1799 * return NULL if this is a new url to manage in this session. */
1800 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1802 /* we need a new media configuration in this session */
1806 /* we have no session media, find one and manage it */
1807 if (sessmedia == NULL) {
1808 /* get a handle to the configuration of the media in the session */
1809 media = find_media (client, ctx, path, &matched);
1811 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1812 g_object_ref (media);
1814 goto media_not_found;
1816 /* no media, not found then */
1818 goto media_not_found_no_reply;
1820 if (path[matched] == '\0')
1821 goto control_not_found;
1823 /* path is what matched. */
1824 path[matched] = '\0';
1825 /* control is remainder */
1826 control = &path[matched + 1];
1828 /* find the stream now using the control part */
1829 stream = gst_rtsp_media_find_stream (media, control);
1831 goto stream_not_found;
1833 /* now we have a uri identifying a valid media and stream */
1834 ctx->stream = stream;
1837 if (session == NULL) {
1838 /* create a session if this fails we probably reached our session limit or
1840 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1841 goto service_unavailable;
1843 /* make sure this client is closed when the session is closed */
1844 client_watch_session (client, session);
1847 /* signal new session */
1848 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1851 ctx->session = session;
1854 if (!klass->configure_client_media (client, media, stream, ctx))
1855 goto configure_media_failed_no_reply;
1857 gst_rtsp_transport_new (&ct);
1859 /* parse and find a usable supported transport */
1860 if (!parse_transport (transport, stream, ct))
1861 goto unsupported_transports;
1863 /* update the client transport */
1864 if (!klass->configure_client_transport (client, ctx, ct))
1865 goto unsupported_client_transport;
1867 /* parse the keymgmt */
1868 if (gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_KEYMGMT,
1869 &keymgmt, 0) == GST_RTSP_OK) {
1870 if (!handle_keymgmt (client, ctx, keymgmt))
1874 if (sessmedia == NULL) {
1875 /* manage the media in our session now, if not done already */
1876 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1877 /* if we stil have no media, error */
1878 if (sessmedia == NULL)
1879 goto sessmedia_unavailable;
1881 g_object_unref (media);
1884 ctx->sessmedia = sessmedia;
1886 /* set in the session media transport */
1887 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1889 /* configure the url used to set this transport, this we will use when
1890 * generating the response for the PLAY request */
1891 gst_rtsp_stream_transport_set_url (trans, uri);
1893 /* configure keepalive for this transport */
1894 gst_rtsp_stream_transport_set_keepalive (trans,
1895 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1897 /* create and serialize the server transport */
1898 st = make_server_transport (client, ctx, ct);
1899 trans_str = gst_rtsp_transport_as_text (st);
1900 gst_rtsp_transport_free (st);
1902 /* construct the response now */
1903 code = GST_RTSP_STS_OK;
1904 gst_rtsp_message_init_response (ctx->response, code,
1905 gst_rtsp_status_as_text (code), ctx->request);
1907 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
1911 send_message (client, ctx, ctx->response, FALSE);
1913 /* update the state */
1914 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1915 switch (rtspstate) {
1916 case GST_RTSP_STATE_PLAYING:
1917 case GST_RTSP_STATE_RECORDING:
1918 case GST_RTSP_STATE_READY:
1919 /* no state change */
1922 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1925 g_object_unref (session);
1928 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
1935 GST_ERROR ("client %p: no uri", client);
1936 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1941 GST_ERROR ("client %p: no transport", client);
1942 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1947 GST_ERROR ("client %p: no session pool configured", client);
1948 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1951 media_not_found_no_reply:
1953 GST_ERROR ("client %p: media '%s' not found", client, path);
1954 /* error reply is already sent */
1959 GST_ERROR ("client %p: media '%s' not found", client, path);
1960 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1965 GST_ERROR ("client %p: no control in path '%s'", client, path);
1966 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1967 g_object_unref (media);
1972 GST_ERROR ("client %p: stream '%s' not found", client, control);
1973 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1974 g_object_unref (media);
1977 service_unavailable:
1979 GST_ERROR ("client %p: can't create session", client);
1980 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1981 g_object_unref (media);
1984 sessmedia_unavailable:
1986 GST_ERROR ("client %p: can't create session media", client);
1987 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1988 g_object_unref (media);
1989 goto cleanup_session;
1991 configure_media_failed_no_reply:
1993 GST_ERROR ("client %p: configure_media failed", client);
1994 /* error reply is already sent */
1995 goto cleanup_session;
1997 unsupported_transports:
1999 GST_ERROR ("client %p: unsupported transports", client);
2000 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
2001 goto cleanup_transport;
2003 unsupported_client_transport:
2005 GST_ERROR ("client %p: unsupported client transport", client);
2006 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
2007 goto cleanup_transport;
2011 GST_ERROR ("client %p: keymgmt error", client);
2012 send_generic_response (client, GST_RTSP_STS_KEY_MANAGEMENT_FAILURE, ctx);
2013 goto cleanup_transport;
2017 gst_rtsp_transport_free (ct);
2020 gst_rtsp_session_pool_remove (priv->session_pool, session);
2021 g_object_unref (session);
2028 static GstSDPMessage *
2029 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
2031 GstRTSPClientPrivate *priv = client->priv;
2036 gst_sdp_message_new (&sdp);
2038 /* some standard things first */
2039 gst_sdp_message_set_version (sdp, "0");
2046 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
2049 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
2050 gst_sdp_message_set_information (sdp, "rtsp-server");
2051 gst_sdp_message_add_time (sdp, "0", "0", NULL);
2052 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
2053 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
2054 gst_sdp_message_add_attribute (sdp, "control", "*");
2056 info.is_ipv6 = priv->is_ipv6;
2057 info.server_ip = priv->server_ip;
2059 /* create an SDP for the media object */
2060 if (!gst_rtsp_media_setup_sdp (media, sdp, &info))
2068 GST_ERROR ("client %p: could not create SDP", client);
2069 gst_sdp_message_free (sdp);
2074 /* for the describe we must generate an SDP */
2076 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
2078 GstRTSPClientPrivate *priv = client->priv;
2083 GstRTSPMedia *media;
2084 GstRTSPClientClass *klass;
2086 klass = GST_RTSP_CLIENT_GET_CLASS (client);
2091 /* check what kind of format is accepted, we don't really do anything with it
2092 * and always return SDP for now. */
2097 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
2099 if (res == GST_RTSP_ENOTIMPL)
2102 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
2106 if (!priv->mount_points)
2107 goto no_mount_points;
2109 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
2112 /* find the media object for the uri */
2113 if (!(media = find_media (client, ctx, path, NULL)))
2116 /* create an SDP for the media object on this client */
2117 if (!(sdp = klass->create_sdp (client, media)))
2120 /* we suspend after the describe */
2121 gst_rtsp_media_suspend (media);
2122 g_object_unref (media);
2124 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2125 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2127 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
2130 /* content base for some clients that might screw up creating the setup uri */
2131 str = make_base_url (client, ctx->uri, path);
2134 GST_INFO ("adding content-base: %s", str);
2135 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE, str);
2137 /* add SDP to the response body */
2138 str = gst_sdp_message_as_text (sdp);
2139 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
2140 gst_sdp_message_free (sdp);
2142 send_message (client, ctx, ctx->response, FALSE);
2144 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
2152 GST_ERROR ("client %p: no uri", client);
2153 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2158 GST_ERROR ("client %p: no mount points configured", client);
2159 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2164 GST_ERROR ("client %p: can't find path for url", client);
2165 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2170 GST_ERROR ("client %p: no media", client);
2172 /* error reply is already sent */
2177 GST_ERROR ("client %p: can't create SDP", client);
2178 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2180 g_object_unref (media);
2186 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
2188 GstRTSPMethod options;
2191 options = GST_RTSP_DESCRIBE |
2196 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
2198 str = gst_rtsp_options_as_text (options);
2200 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2201 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2203 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
2206 send_message (client, ctx, ctx->response, FALSE);
2208 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
2214 /* remove duplicate and trailing '/' */
2216 sanitize_uri (GstRTSPUrl * uri)
2220 gboolean have_slash, prev_slash;
2222 s = d = uri->abspath;
2223 len = strlen (uri->abspath);
2227 for (i = 0; i < len; i++) {
2228 have_slash = s[i] == '/';
2230 if (!have_slash || !prev_slash)
2232 prev_slash = have_slash;
2234 len = d - uri->abspath;
2235 /* don't remove the first slash if that's the only thing left */
2236 if (len > 1 && *(d - 1) == '/')
2241 /* is called when the session is removed from its session pool. */
2243 client_session_removed (GstRTSPSessionPool * pool, GstRTSPSession * session,
2244 GstRTSPClient * client)
2246 GstRTSPClientPrivate *priv = client->priv;
2248 GST_INFO ("client %p: session %p removed", client, session);
2250 g_mutex_lock (&priv->lock);
2251 client_unwatch_session (client, session, NULL);
2252 g_mutex_unlock (&priv->lock);
2255 /* Returns TRUE if there are no Require headers, otherwise returns FALSE
2256 * and also returns a newly-allocated string of (comma-separated) unsupported
2257 * options in the unsupported_reqs variable .
2259 * There may be multiple Require headers, but we must send one single
2260 * Unsupported header with all the unsupported options as response. If
2261 * an incoming Require header contained a comma-separated list of options
2262 * GstRtspConnection will already have split that list up into multiple
2265 * TODO: allow the application to decide what features are supported
2268 check_request_requirements (GstRTSPMessage * msg, gchar ** unsupported_reqs)
2271 GPtrArray *arr = NULL;
2277 res = gst_rtsp_message_get_header (msg, GST_RTSP_HDR_REQUIRE, &reqs, i++);
2279 if (res == GST_RTSP_ENOTIMPL)
2283 arr = g_ptr_array_new_with_free_func ((GDestroyNotify) g_free);
2285 g_ptr_array_add (arr, g_strdup (reqs));
2289 /* if we don't have any Require headers at all, all is fine */
2293 /* otherwise we've now processed at all the Require headers */
2294 g_ptr_array_add (arr, NULL);
2296 /* for now we don't commit to supporting anything, so will just report
2297 * all of the required options as unsupported */
2298 *unsupported_reqs = g_strjoinv (", ", (gchar **) arr->pdata);
2300 g_ptr_array_unref (arr);
2305 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
2307 GstRTSPClientPrivate *priv = client->priv;
2308 GstRTSPMethod method;
2309 const gchar *uristr;
2310 GstRTSPUrl *uri = NULL;
2311 GstRTSPVersion version;
2313 GstRTSPSession *session = NULL;
2314 GstRTSPContext sctx = { NULL }, *ctx;
2315 GstRTSPMessage response = { 0 };
2316 gchar *unsupported_reqs = NULL;
2319 if (!(ctx = gst_rtsp_context_get_current ())) {
2321 ctx->auth = priv->auth;
2322 gst_rtsp_context_push_current (ctx);
2325 ctx->conn = priv->connection;
2326 ctx->client = client;
2327 ctx->request = request;
2328 ctx->response = &response;
2330 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2331 gst_rtsp_message_dump (request);
2334 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
2336 GST_INFO ("client %p: received a request %s %s %s", client,
2337 gst_rtsp_method_as_text (method), uristr,
2338 gst_rtsp_version_as_text (version));
2340 /* we can only handle 1.0 requests */
2341 if (version != GST_RTSP_VERSION_1_0)
2344 ctx->method = method;
2346 /* we always try to parse the url first */
2347 if (strcmp (uristr, "*") == 0) {
2348 /* special case where we have * as uri, keep uri = NULL */
2349 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
2350 /* check if the uristr is an absolute path <=> scheme and host information
2354 scheme = g_uri_parse_scheme (uristr);
2355 if (scheme == NULL && g_str_has_prefix (uristr, "/")) {
2356 gchar *absolute_uristr = NULL;
2358 GST_WARNING_OBJECT (client, "request doesn't contain absolute url");
2359 if (priv->server_ip == NULL) {
2360 GST_WARNING_OBJECT (client, "host information missing");
2365 g_strdup_printf ("rtsp://%s%s", priv->server_ip, uristr);
2367 GST_DEBUG_OBJECT (client, "absolute url: %s", absolute_uristr);
2368 if (gst_rtsp_url_parse (absolute_uristr, &uri) != GST_RTSP_OK) {
2369 g_free (absolute_uristr);
2372 g_free (absolute_uristr);
2379 /* get the session if there is any */
2380 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
2381 if (res == GST_RTSP_OK) {
2382 if (priv->session_pool == NULL)
2385 /* we had a session in the request, find it again */
2386 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2387 goto session_not_found;
2389 /* we add the session to the client list of watched sessions. When a session
2390 * disappears because it times out, we will be notified. If all sessions are
2391 * gone, we will close the connection */
2392 client_watch_session (client, session);
2395 /* sanitize the uri */
2399 ctx->session = session;
2401 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
2402 goto not_authorized;
2404 /* handle any 'Require' headers */
2405 if (!check_request_requirements (ctx->request, &unsupported_reqs))
2406 goto unsupported_requirement;
2408 /* now see what is asked and dispatch to a dedicated handler */
2410 case GST_RTSP_OPTIONS:
2411 handle_options_request (client, ctx);
2413 case GST_RTSP_DESCRIBE:
2414 handle_describe_request (client, ctx);
2416 case GST_RTSP_SETUP:
2417 handle_setup_request (client, ctx);
2420 handle_play_request (client, ctx);
2422 case GST_RTSP_PAUSE:
2423 handle_pause_request (client, ctx);
2425 case GST_RTSP_TEARDOWN:
2426 handle_teardown_request (client, ctx);
2428 case GST_RTSP_SET_PARAMETER:
2429 handle_set_param_request (client, ctx);
2431 case GST_RTSP_GET_PARAMETER:
2432 handle_get_param_request (client, ctx);
2434 case GST_RTSP_ANNOUNCE:
2435 case GST_RTSP_RECORD:
2436 case GST_RTSP_REDIRECT:
2437 goto not_implemented;
2438 case GST_RTSP_INVALID:
2445 gst_rtsp_context_pop_current (ctx);
2447 g_object_unref (session);
2449 gst_rtsp_url_free (uri);
2455 GST_ERROR ("client %p: version %d not supported", client, version);
2456 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
2462 GST_ERROR ("client %p: bad request", client);
2463 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2468 GST_ERROR ("client %p: no pool configured", client);
2469 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2474 GST_ERROR ("client %p: session not found", client);
2475 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2480 GST_ERROR ("client %p: not allowed", client);
2481 /* error reply is already sent */
2484 unsupported_requirement:
2486 GST_ERROR ("client %p: Required option is not supported (%s)", client,
2488 send_option_not_supported_response (client, ctx, unsupported_reqs);
2489 g_free (unsupported_reqs);
2494 GST_ERROR ("client %p: method %d not implemented", client, method);
2495 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
2502 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
2504 GstRTSPClientPrivate *priv = client->priv;
2506 GstRTSPSession *session = NULL;
2507 GstRTSPContext sctx = { NULL }, *ctx;
2510 if (!(ctx = gst_rtsp_context_get_current ())) {
2512 ctx->auth = priv->auth;
2513 gst_rtsp_context_push_current (ctx);
2516 ctx->conn = priv->connection;
2517 ctx->client = client;
2518 ctx->request = NULL;
2520 ctx->method = GST_RTSP_INVALID;
2521 ctx->response = response;
2523 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2524 gst_rtsp_message_dump (response);
2527 GST_INFO ("client %p: received a response", client);
2529 /* get the session if there is any */
2531 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
2532 if (res == GST_RTSP_OK) {
2533 if (priv->session_pool == NULL)
2536 /* we had a session in the request, find it again */
2537 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2538 goto session_not_found;
2540 /* we add the session to the client list of watched sessions. When a session
2541 * disappears because it times out, we will be notified. If all sessions are
2542 * gone, we will close the connection */
2543 client_watch_session (client, session);
2546 ctx->session = session;
2548 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
2553 gst_rtsp_context_pop_current (ctx);
2555 g_object_unref (session);
2560 GST_ERROR ("client %p: no pool configured", client);
2565 GST_ERROR ("client %p: session not found", client);
2571 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2573 GstRTSPClientPrivate *priv = client->priv;
2582 /* find the stream for this message */
2583 res = gst_rtsp_message_parse_data (message, &channel);
2584 if (res != GST_RTSP_OK)
2587 gst_rtsp_message_steal_body (message, &data, &size);
2589 buffer = gst_buffer_new_wrapped (data, size);
2592 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2593 GstRTSPStreamTransport *trans;
2594 GstRTSPStream *stream;
2595 const GstRTSPTransport *tr;
2599 tr = gst_rtsp_stream_transport_get_transport (trans);
2600 stream = gst_rtsp_stream_transport_get_stream (trans);
2602 /* check for TCP transport */
2603 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2604 /* dispatch to the stream based on the channel number */
2605 if (tr->interleaved.min == channel) {
2606 gst_rtsp_stream_recv_rtp (stream, buffer);
2609 } else if (tr->interleaved.max == channel) {
2610 gst_rtsp_stream_recv_rtcp (stream, buffer);
2617 gst_buffer_unref (buffer);
2621 * gst_rtsp_client_set_session_pool:
2622 * @client: a #GstRTSPClient
2623 * @pool: (transfer none): a #GstRTSPSessionPool
2625 * Set @pool as the sessionpool for @client which it will use to find
2626 * or allocate sessions. the sessionpool is usually inherited from the server
2627 * that created the client but can be overridden later.
2630 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2631 GstRTSPSessionPool * pool)
2633 GstRTSPSessionPool *old;
2634 GstRTSPClientPrivate *priv;
2636 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2638 priv = client->priv;
2641 g_object_ref (pool);
2643 g_mutex_lock (&priv->lock);
2644 old = priv->session_pool;
2645 priv->session_pool = pool;
2647 if (priv->session_removed_id)
2648 g_signal_handler_disconnect (old, priv->session_removed_id);
2650 priv->session_removed_id = g_signal_connect (pool, "session-removed",
2651 G_CALLBACK (client_session_removed), client);
2653 priv->session_removed_id = 0;
2654 g_mutex_unlock (&priv->lock);
2656 /* FIXME, should remove all sessions from the old pool for this client */
2658 g_object_unref (old);
2662 * gst_rtsp_client_get_session_pool:
2663 * @client: a #GstRTSPClient
2665 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2667 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2669 GstRTSPSessionPool *
2670 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2672 GstRTSPClientPrivate *priv;
2673 GstRTSPSessionPool *result;
2675 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2677 priv = client->priv;
2679 g_mutex_lock (&priv->lock);
2680 if ((result = priv->session_pool))
2681 g_object_ref (result);
2682 g_mutex_unlock (&priv->lock);
2688 * gst_rtsp_client_set_mount_points:
2689 * @client: a #GstRTSPClient
2690 * @mounts: (transfer none): a #GstRTSPMountPoints
2692 * Set @mounts as the mount points for @client which it will use to map urls
2693 * to media streams. These mount points are usually inherited from the server that
2694 * created the client but can be overriden later.
2697 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2698 GstRTSPMountPoints * mounts)
2700 GstRTSPClientPrivate *priv;
2701 GstRTSPMountPoints *old;
2703 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2705 priv = client->priv;
2708 g_object_ref (mounts);
2710 g_mutex_lock (&priv->lock);
2711 old = priv->mount_points;
2712 priv->mount_points = mounts;
2713 g_mutex_unlock (&priv->lock);
2716 g_object_unref (old);
2720 * gst_rtsp_client_get_mount_points:
2721 * @client: a #GstRTSPClient
2723 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2725 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2727 GstRTSPMountPoints *
2728 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2730 GstRTSPClientPrivate *priv;
2731 GstRTSPMountPoints *result;
2733 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2735 priv = client->priv;
2737 g_mutex_lock (&priv->lock);
2738 if ((result = priv->mount_points))
2739 g_object_ref (result);
2740 g_mutex_unlock (&priv->lock);
2746 * gst_rtsp_client_set_auth:
2747 * @client: a #GstRTSPClient
2748 * @auth: (transfer none): a #GstRTSPAuth
2750 * configure @auth to be used as the authentication manager of @client.
2753 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2755 GstRTSPClientPrivate *priv;
2758 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2760 priv = client->priv;
2763 g_object_ref (auth);
2765 g_mutex_lock (&priv->lock);
2768 g_mutex_unlock (&priv->lock);
2771 g_object_unref (old);
2776 * gst_rtsp_client_get_auth:
2777 * @client: a #GstRTSPClient
2779 * Get the #GstRTSPAuth used as the authentication manager of @client.
2781 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2785 gst_rtsp_client_get_auth (GstRTSPClient * client)
2787 GstRTSPClientPrivate *priv;
2788 GstRTSPAuth *result;
2790 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2792 priv = client->priv;
2794 g_mutex_lock (&priv->lock);
2795 if ((result = priv->auth))
2796 g_object_ref (result);
2797 g_mutex_unlock (&priv->lock);
2803 * gst_rtsp_client_set_thread_pool:
2804 * @client: a #GstRTSPClient
2805 * @pool: (transfer none): a #GstRTSPThreadPool
2807 * configure @pool to be used as the thread pool of @client.
2810 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2811 GstRTSPThreadPool * pool)
2813 GstRTSPClientPrivate *priv;
2814 GstRTSPThreadPool *old;
2816 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2818 priv = client->priv;
2821 g_object_ref (pool);
2823 g_mutex_lock (&priv->lock);
2824 old = priv->thread_pool;
2825 priv->thread_pool = pool;
2826 g_mutex_unlock (&priv->lock);
2829 g_object_unref (old);
2833 * gst_rtsp_client_get_thread_pool:
2834 * @client: a #GstRTSPClient
2836 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2838 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2842 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2844 GstRTSPClientPrivate *priv;
2845 GstRTSPThreadPool *result;
2847 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2849 priv = client->priv;
2851 g_mutex_lock (&priv->lock);
2852 if ((result = priv->thread_pool))
2853 g_object_ref (result);
2854 g_mutex_unlock (&priv->lock);
2860 * gst_rtsp_client_set_connection:
2861 * @client: a #GstRTSPClient
2862 * @conn: (transfer full): a #GstRTSPConnection
2864 * Set the #GstRTSPConnection of @client. This function takes ownership of
2867 * Returns: %TRUE on success.
2870 gst_rtsp_client_set_connection (GstRTSPClient * client,
2871 GstRTSPConnection * conn)
2873 GstRTSPClientPrivate *priv;
2874 GSocket *read_socket;
2875 GSocketAddress *address;
2877 GError *error = NULL;
2879 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2880 g_return_val_if_fail (conn != NULL, FALSE);
2882 priv = client->priv;
2884 read_socket = gst_rtsp_connection_get_read_socket (conn);
2886 if (!(address = g_socket_get_local_address (read_socket, &error)))
2889 g_free (priv->server_ip);
2890 /* keep the original ip that the client connected to */
2891 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2892 GInetAddress *iaddr;
2894 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2896 /* socket might be ipv6 but adress still ipv4 */
2897 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2898 priv->server_ip = g_inet_address_to_string (iaddr);
2899 g_object_unref (address);
2901 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2902 priv->server_ip = g_strdup ("unknown");
2905 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2906 priv->server_ip, priv->is_ipv6);
2908 url = gst_rtsp_connection_get_url (conn);
2909 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2911 priv->connection = conn;
2918 GST_ERROR ("could not get local address %s", error->message);
2919 g_error_free (error);
2925 * gst_rtsp_client_get_connection:
2926 * @client: a #GstRTSPClient
2928 * Get the #GstRTSPConnection of @client.
2930 * Returns: (transfer none): the #GstRTSPConnection of @client.
2931 * The connection object returned remains valid until the client is freed.
2934 gst_rtsp_client_get_connection (GstRTSPClient * client)
2936 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2938 return client->priv->connection;
2942 * gst_rtsp_client_set_send_func:
2943 * @client: a #GstRTSPClient
2944 * @func: (scope notified): a #GstRTSPClientSendFunc
2945 * @user_data: (closure): user data passed to @func
2946 * @notify: (allow-none): called when @user_data is no longer in use
2948 * Set @func as the callback that will be called when a new message needs to be
2949 * sent to the client. @user_data is passed to @func and @notify is called when
2950 * @user_data is no longer in use.
2952 * By default, the client will send the messages on the #GstRTSPConnection that
2953 * was configured with gst_rtsp_client_attach() was called.
2956 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2957 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2959 GstRTSPClientPrivate *priv;
2960 GDestroyNotify old_notify;
2963 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2965 priv = client->priv;
2967 g_mutex_lock (&priv->send_lock);
2968 priv->send_func = func;
2969 old_notify = priv->send_notify;
2970 old_data = priv->send_data;
2971 priv->send_notify = notify;
2972 priv->send_data = user_data;
2973 g_mutex_unlock (&priv->send_lock);
2976 old_notify (old_data);
2980 * gst_rtsp_client_handle_message:
2981 * @client: a #GstRTSPClient
2982 * @message: (transfer none): an #GstRTSPMessage
2984 * Let the client handle @message.
2986 * Returns: a #GstRTSPResult.
2989 gst_rtsp_client_handle_message (GstRTSPClient * client,
2990 GstRTSPMessage * message)
2992 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2993 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2995 switch (message->type) {
2996 case GST_RTSP_MESSAGE_REQUEST:
2997 handle_request (client, message);
2999 case GST_RTSP_MESSAGE_RESPONSE:
3000 handle_response (client, message);
3002 case GST_RTSP_MESSAGE_DATA:
3003 handle_data (client, message);
3012 * gst_rtsp_client_send_message:
3013 * @client: a #GstRTSPClient
3014 * @session: (allow-none) (transfer none): a #GstRTSPSession to send
3015 * the message to or %NULL
3016 * @message: (transfer none): The #GstRTSPMessage to send
3018 * Send a message message to the remote end. @message must be a
3019 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
3022 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
3023 GstRTSPMessage * message)
3025 GstRTSPContext sctx = { NULL }
3027 GstRTSPClientPrivate *priv;
3029 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
3030 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3031 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
3032 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
3034 priv = client->priv;
3036 if (!(ctx = gst_rtsp_context_get_current ())) {
3038 ctx->auth = priv->auth;
3039 gst_rtsp_context_push_current (ctx);
3042 ctx->conn = priv->connection;
3043 ctx->client = client;
3044 ctx->session = session;
3046 send_message (client, ctx, message, FALSE);
3049 gst_rtsp_context_pop_current (ctx);
3054 static GstRTSPResult
3055 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
3056 gboolean close, gpointer user_data)
3058 GstRTSPClientPrivate *priv = client->priv;
3066 /* send the response and store the seq number so we can wait until it's
3067 * written to the client to close the connection */
3069 gst_rtsp_watch_send_message (priv->watch, message,
3070 close ? &priv->close_seq : NULL);
3071 if (ret == GST_RTSP_OK)
3074 if (ret != GST_RTSP_ENOMEM)
3078 if (priv->drop_backlog)
3081 /* queue was full, wait for more space */
3082 GST_DEBUG_OBJECT (client, "waiting for backlog");
3083 ret = gst_rtsp_watch_wait_backlog (priv->watch, &time);
3084 GST_DEBUG_OBJECT (client, "Resend due to backlog full");
3085 } while (ret != GST_RTSP_EINTR);
3092 GST_DEBUG_OBJECT (client, "got error %d", ret);
3097 static GstRTSPResult
3098 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
3101 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
3104 static GstRTSPResult
3105 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
3107 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3108 GstRTSPClientPrivate *priv = client->priv;
3110 if (priv->close_seq && priv->close_seq == cseq) {
3111 GST_INFO ("client %p: send close message", client);
3112 priv->close_seq = 0;
3113 close_connection (client);
3119 static GstRTSPResult
3120 closed (GstRTSPWatch * watch, gpointer user_data)
3122 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3123 GstRTSPClientPrivate *priv = client->priv;
3124 const gchar *tunnelid;
3126 GST_INFO ("client %p: connection closed", client);
3128 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
3129 g_mutex_lock (&tunnels_lock);
3130 /* remove from tunnelids */
3131 g_hash_table_remove (tunnels, tunnelid);
3132 g_mutex_unlock (&tunnels_lock);
3135 gst_rtsp_watch_set_flushing (watch, TRUE);
3136 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
3141 static GstRTSPResult
3142 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
3144 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3147 str = gst_rtsp_strresult (result);
3148 GST_INFO ("client %p: received an error %s", client, str);
3154 static GstRTSPResult
3155 error_full (GstRTSPWatch * watch, GstRTSPResult result,
3156 GstRTSPMessage * message, guint id, gpointer user_data)
3158 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3161 str = gst_rtsp_strresult (result);
3163 ("client %p: error when handling message %p with id %d: %s",
3164 client, message, id, str);
3171 remember_tunnel (GstRTSPClient * client)
3173 GstRTSPClientPrivate *priv = client->priv;
3174 const gchar *tunnelid;
3176 /* store client in the pending tunnels */
3177 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
3178 if (tunnelid == NULL)
3181 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
3183 /* we can't have two clients connecting with the same tunnelid */
3184 g_mutex_lock (&tunnels_lock);
3185 if (g_hash_table_lookup (tunnels, tunnelid))
3186 goto tunnel_existed;
3188 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
3189 g_mutex_unlock (&tunnels_lock);
3196 GST_ERROR ("client %p: no tunnelid provided", client);
3201 g_mutex_unlock (&tunnels_lock);
3202 GST_ERROR ("client %p: tunnel session %s already existed", client,
3208 static GstRTSPResult
3209 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
3211 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3212 GstRTSPClientPrivate *priv = client->priv;
3214 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
3217 /* ignore error, it'll only be a problem when the client does a POST again */
3218 remember_tunnel (client);
3224 handle_tunnel (GstRTSPClient * client)
3226 GstRTSPClientPrivate *priv = client->priv;
3227 GstRTSPClient *oclient;
3228 GstRTSPClientPrivate *opriv;
3229 const gchar *tunnelid;
3231 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
3232 if (tunnelid == NULL)
3235 /* check for previous tunnel */
3236 g_mutex_lock (&tunnels_lock);
3237 oclient = g_hash_table_lookup (tunnels, tunnelid);
3239 if (oclient == NULL) {
3240 /* no previous tunnel, remember tunnel */
3241 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
3242 g_mutex_unlock (&tunnels_lock);
3244 GST_INFO ("client %p: no previous tunnel found, remembering tunnel (%p)",
3245 client, priv->connection);
3247 /* merge both tunnels into the first client */
3248 /* remove the old client from the table. ref before because removing it will
3249 * remove the ref to it. */
3250 g_object_ref (oclient);
3251 g_hash_table_remove (tunnels, tunnelid);
3252 g_mutex_unlock (&tunnels_lock);
3254 opriv = oclient->priv;
3256 if (opriv->watch == NULL)
3259 GST_INFO ("client %p: found previous tunnel %p (old %p, new %p)", client,
3260 oclient, opriv->connection, priv->connection);
3262 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
3263 gst_rtsp_watch_reset (priv->watch);
3264 gst_rtsp_watch_reset (opriv->watch);
3265 g_object_unref (oclient);
3267 /* the old client owns the tunnel now, the new one will be freed */
3268 g_source_destroy ((GSource *) priv->watch);
3270 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
3278 GST_ERROR ("client %p: no tunnelid provided", client);
3283 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
3284 g_object_unref (oclient);
3289 static GstRTSPStatusCode
3290 tunnel_get (GstRTSPWatch * watch, gpointer user_data)
3292 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3294 GST_INFO ("client %p: tunnel get (connection %p)", client,
3295 client->priv->connection);
3297 if (!handle_tunnel (client)) {
3298 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
3301 return GST_RTSP_STS_OK;
3304 static GstRTSPResult
3305 tunnel_post (GstRTSPWatch * watch, gpointer user_data)
3307 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3309 GST_INFO ("client %p: tunnel post (connection %p)", client,
3310 client->priv->connection);
3312 if (!handle_tunnel (client)) {
3313 return GST_RTSP_ERROR;
3319 static GstRTSPResult
3320 tunnel_http_response (GstRTSPWatch * watch, GstRTSPMessage * request,
3321 GstRTSPMessage * response, gpointer user_data)
3323 GstRTSPClientClass *klass;
3325 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3326 klass = GST_RTSP_CLIENT_GET_CLASS (client);
3328 if (klass->tunnel_http_response) {
3329 klass->tunnel_http_response (client, request, response);
3335 static GstRTSPWatchFuncs watch_funcs = {
3344 tunnel_http_response
3348 client_watch_notify (GstRTSPClient * client)
3350 GstRTSPClientPrivate *priv = client->priv;
3352 GST_INFO ("client %p: watch destroyed", client);
3354 g_main_context_unref (priv->watch_context);
3355 priv->watch_context = NULL;
3356 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
3357 g_object_unref (client);
3361 * gst_rtsp_client_attach:
3362 * @client: a #GstRTSPClient
3363 * @context: (allow-none): a #GMainContext
3365 * Attaches @client to @context. When the mainloop for @context is run, the
3366 * client will be dispatched. When @context is %NULL, the default context will be
3369 * This function should be called when the client properties and urls are fully
3370 * configured and the client is ready to start.
3372 * Returns: the ID (greater than 0) for the source within the GMainContext.
3375 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
3377 GstRTSPClientPrivate *priv;
3380 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
3381 priv = client->priv;
3382 g_return_val_if_fail (priv->connection != NULL, 0);
3383 g_return_val_if_fail (priv->watch == NULL, 0);
3385 /* make sure noone will free the context before the watch is destroyed */
3386 priv->watch_context = g_main_context_ref (context);
3388 /* create watch for the connection and attach */
3389 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
3390 g_object_ref (client), (GDestroyNotify) client_watch_notify);
3391 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
3392 (GDestroyNotify) gst_rtsp_watch_unref);
3394 /* FIXME make this configurable. We don't want to do this yet because it will
3395 * be superceeded by a cache object later */
3396 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
3398 GST_INFO ("client %p: attaching to context %p", client, context);
3399 res = gst_rtsp_watch_attach (priv->watch, context);
3405 * gst_rtsp_client_session_filter:
3406 * @client: a #GstRTSPClient
3407 * @func: (scope call) (allow-none): a callback
3408 * @user_data: user data passed to @func
3410 * Call @func for each session managed by @client. The result value of @func
3411 * determines what happens to the session. @func will be called with @client
3412 * locked so no further actions on @client can be performed from @func.
3414 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
3417 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
3419 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
3420 * will also be added with an additional ref to the result #GList of this
3423 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each session.
3425 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
3426 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3427 * element in the #GList should be unreffed before the list is freed.
3430 gst_rtsp_client_session_filter (GstRTSPClient * client,
3431 GstRTSPClientSessionFilterFunc func, gpointer user_data)
3433 GstRTSPClientPrivate *priv;
3434 GList *result, *walk, *next;
3436 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
3438 priv = client->priv;
3442 g_mutex_lock (&priv->lock);
3443 for (walk = priv->sessions; walk; walk = next) {
3444 GstRTSPSession *sess = walk->data;
3445 GstRTSPFilterResult res;
3447 next = g_list_next (walk);
3450 res = func (client, sess, user_data);
3452 res = GST_RTSP_FILTER_REF;
3455 case GST_RTSP_FILTER_REMOVE:
3456 /* stop watching the session and pretent it went away */
3457 client_unwatch_session (client, sess, walk);
3459 case GST_RTSP_FILTER_REF:
3460 result = g_list_prepend (result, g_object_ref (sess));
3462 case GST_RTSP_FILTER_KEEP:
3467 g_mutex_unlock (&priv->lock);