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 GstRTSPMountPoints *mount_points;
76 GstRTSPThreadPool *thread_pool;
78 /* used to cache the media in the last requested DESCRIBE so that
79 * we can pick it up in the next SETUP immediately */
86 gboolean drop_backlog;
89 static GMutex tunnels_lock;
90 static GHashTable *tunnels; /* protected by tunnels_lock */
92 #define DEFAULT_SESSION_POOL NULL
93 #define DEFAULT_MOUNT_POINTS NULL
94 #define DEFAULT_DROP_BACKLOG TRUE
109 SIGNAL_OPTIONS_REQUEST,
110 SIGNAL_DESCRIBE_REQUEST,
111 SIGNAL_SETUP_REQUEST,
113 SIGNAL_PAUSE_REQUEST,
114 SIGNAL_TEARDOWN_REQUEST,
115 SIGNAL_SET_PARAMETER_REQUEST,
116 SIGNAL_GET_PARAMETER_REQUEST,
117 SIGNAL_HANDLE_RESPONSE,
122 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
123 #define GST_CAT_DEFAULT rtsp_client_debug
125 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
127 static void gst_rtsp_client_get_property (GObject * object, guint propid,
128 GValue * value, GParamSpec * pspec);
129 static void gst_rtsp_client_set_property (GObject * object, guint propid,
130 const GValue * value, GParamSpec * pspec);
131 static void gst_rtsp_client_finalize (GObject * obj);
133 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
134 static void client_session_finalized (GstRTSPClient * client,
135 GstRTSPSession * session);
136 static void unlink_session_transports (GstRTSPClient * client,
137 GstRTSPSession * session, GstRTSPSessionMedia * sessmedia);
138 static gboolean default_configure_client_media (GstRTSPClient * client,
139 GstRTSPMedia * media, GstRTSPStream * stream, GstRTSPContext * ctx);
140 static gboolean default_configure_client_transport (GstRTSPClient * client,
141 GstRTSPContext * ctx, GstRTSPTransport * ct);
142 static GstRTSPResult default_params_set (GstRTSPClient * client,
143 GstRTSPContext * ctx);
144 static GstRTSPResult default_params_get (GstRTSPClient * client,
145 GstRTSPContext * ctx);
146 static gchar *default_make_path_from_uri (GstRTSPClient * client,
147 const GstRTSPUrl * uri);
149 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
152 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
154 GObjectClass *gobject_class;
156 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
158 gobject_class = G_OBJECT_CLASS (klass);
160 gobject_class->get_property = gst_rtsp_client_get_property;
161 gobject_class->set_property = gst_rtsp_client_set_property;
162 gobject_class->finalize = gst_rtsp_client_finalize;
164 klass->create_sdp = create_sdp;
165 klass->configure_client_media = default_configure_client_media;
166 klass->configure_client_transport = default_configure_client_transport;
167 klass->params_set = default_params_set;
168 klass->params_get = default_params_get;
169 klass->make_path_from_uri = default_make_path_from_uri;
171 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
172 g_param_spec_object ("session-pool", "Session Pool",
173 "The session pool to use for client session",
174 GST_TYPE_RTSP_SESSION_POOL,
175 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
177 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
178 g_param_spec_object ("mount-points", "Mount Points",
179 "The mount points to use for client session",
180 GST_TYPE_RTSP_MOUNT_POINTS,
181 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183 g_object_class_install_property (gobject_class, PROP_DROP_BACKLOG,
184 g_param_spec_boolean ("drop-backlog", "Drop Backlog",
185 "Drop data when the backlog queue is full",
186 DEFAULT_DROP_BACKLOG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188 gst_rtsp_client_signals[SIGNAL_CLOSED] =
189 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
190 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
191 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
193 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
194 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
195 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
196 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
198 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
199 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
200 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
201 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
204 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
205 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
206 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
207 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
210 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
211 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
212 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
213 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
216 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
217 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
218 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
219 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
222 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
223 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
224 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
225 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
228 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
229 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
230 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
231 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
234 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
235 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
236 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
237 set_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
238 G_TYPE_NONE, 1, G_TYPE_POINTER);
240 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
241 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
242 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
243 get_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
244 G_TYPE_NONE, 1, G_TYPE_POINTER);
246 gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE] =
247 g_signal_new ("handle-response", G_TYPE_FROM_CLASS (klass),
248 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
249 handle_response), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
250 G_TYPE_NONE, 1, G_TYPE_POINTER);
252 gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE] =
253 g_signal_new ("send-message", G_TYPE_FROM_CLASS (klass),
254 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
255 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER);
258 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
259 g_mutex_init (&tunnels_lock);
261 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
265 gst_rtsp_client_init (GstRTSPClient * client)
267 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
271 g_mutex_init (&priv->lock);
272 g_mutex_init (&priv->send_lock);
274 priv->drop_backlog = DEFAULT_DROP_BACKLOG;
277 static GstRTSPFilterResult
278 filter_session (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
281 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
283 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
284 unlink_session_transports (client, sess, sessmedia);
286 /* unmanage the media in the session */
287 return GST_RTSP_FILTER_REMOVE;
291 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
293 /* unlink all media managed in this session */
294 gst_rtsp_session_filter (session, filter_session, client);
298 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
300 GstRTSPClientPrivate *priv = client->priv;
303 for (walk = priv->sessions; walk; walk = g_list_next (walk)) {
304 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
306 /* we already know about this session */
307 if (msession == session)
311 GST_INFO ("watching session %p", session);
313 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
315 priv->sessions = g_list_prepend (priv->sessions, session);
319 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session)
321 GstRTSPClientPrivate *priv = client->priv;
323 GST_INFO ("unwatching session %p", session);
325 g_object_weak_unref (G_OBJECT (session),
326 (GWeakNotify) client_session_finalized, client);
327 priv->sessions = g_list_remove (priv->sessions, session);
331 client_cleanup_session (GstRTSPClient * client, GstRTSPSession * session)
333 g_object_weak_unref (G_OBJECT (session),
334 (GWeakNotify) client_session_finalized, client);
335 client_unlink_session (client, session);
339 client_cleanup_sessions (GstRTSPClient * client)
341 GstRTSPClientPrivate *priv = client->priv;
344 /* remove weak-ref from sessions */
345 for (sessions = priv->sessions; sessions; sessions = g_list_next (sessions)) {
346 client_cleanup_session (client, (GstRTSPSession *) sessions->data);
348 g_list_free (priv->sessions);
349 priv->sessions = NULL;
352 /* A client is finalized when the connection is broken */
354 gst_rtsp_client_finalize (GObject * obj)
356 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
357 GstRTSPClientPrivate *priv = client->priv;
359 GST_INFO ("finalize client %p", client);
362 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
363 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
366 g_source_destroy ((GSource *) priv->watch);
368 if (priv->watch_context)
369 g_main_context_unref (priv->watch_context);
371 client_cleanup_sessions (client);
373 if (priv->connection)
374 gst_rtsp_connection_free (priv->connection);
375 if (priv->session_pool)
376 g_object_unref (priv->session_pool);
377 if (priv->mount_points)
378 g_object_unref (priv->mount_points);
380 g_object_unref (priv->auth);
381 if (priv->thread_pool)
382 g_object_unref (priv->thread_pool);
387 gst_rtsp_media_unprepare (priv->media);
388 g_object_unref (priv->media);
391 g_free (priv->server_ip);
392 g_mutex_clear (&priv->lock);
393 g_mutex_clear (&priv->send_lock);
395 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
399 gst_rtsp_client_get_property (GObject * object, guint propid,
400 GValue * value, GParamSpec * pspec)
402 GstRTSPClient *client = GST_RTSP_CLIENT (object);
403 GstRTSPClientPrivate *priv = client->priv;
406 case PROP_SESSION_POOL:
407 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
409 case PROP_MOUNT_POINTS:
410 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
412 case PROP_DROP_BACKLOG:
413 g_value_set_boolean (value, priv->drop_backlog);
416 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
421 gst_rtsp_client_set_property (GObject * object, guint propid,
422 const GValue * value, GParamSpec * pspec)
424 GstRTSPClient *client = GST_RTSP_CLIENT (object);
425 GstRTSPClientPrivate *priv = client->priv;
428 case PROP_SESSION_POOL:
429 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
431 case PROP_MOUNT_POINTS:
432 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
434 case PROP_DROP_BACKLOG:
435 g_mutex_lock (&priv->lock);
436 priv->drop_backlog = g_value_get_boolean (value);
437 g_mutex_unlock (&priv->lock);
440 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
445 * gst_rtsp_client_new:
447 * Create a new #GstRTSPClient instance.
449 * Returns: (transfer full): a new #GstRTSPClient
452 gst_rtsp_client_new (void)
454 GstRTSPClient *result;
456 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
462 send_message (GstRTSPClient * client, GstRTSPContext * ctx,
463 GstRTSPMessage * message, gboolean close)
465 GstRTSPClientPrivate *priv = client->priv;
467 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
468 "GStreamer RTSP server");
470 /* remove any previous header */
471 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
473 /* add the new session header for new session ids */
475 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
476 gst_rtsp_session_get_header (ctx->session));
479 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
480 gst_rtsp_message_dump (message);
484 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
486 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE],
489 g_mutex_lock (&priv->send_lock);
491 priv->send_func (client, message, close, priv->send_data);
492 g_mutex_unlock (&priv->send_lock);
494 gst_rtsp_message_unset (message);
498 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
499 GstRTSPContext * ctx)
501 gst_rtsp_message_init_response (ctx->response, code,
502 gst_rtsp_status_as_text (code), ctx->request);
506 send_message (client, ctx, ctx->response, FALSE);
510 send_option_not_supported_response (GstRTSPClient * client,
511 GstRTSPContext * ctx, const gchar * unsupported_options)
513 GstRTSPStatusCode code = GST_RTSP_STS_OPTION_NOT_SUPPORTED;
515 gst_rtsp_message_init_response (ctx->response, code,
516 gst_rtsp_status_as_text (code), ctx->request);
518 if (unsupported_options != NULL) {
519 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_UNSUPPORTED,
520 unsupported_options);
525 send_message (client, ctx, ctx->response, FALSE);
529 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
531 if (path1 == NULL || path2 == NULL)
534 if (strlen (path1) != len2)
537 if (strncmp (path1, path2, len2))
543 /* this function is called to initially find the media for the DESCRIBE request
544 * but is cached for when the same client (without breaking the connection) is
545 * doing a setup for the exact same url. */
546 static GstRTSPMedia *
547 find_media (GstRTSPClient * client, GstRTSPContext * ctx, gchar * path,
550 GstRTSPClientPrivate *priv = client->priv;
551 GstRTSPMediaFactory *factory;
555 /* find the longest matching factory for the uri first */
556 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
560 ctx->factory = factory;
562 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
563 goto no_factory_access;
565 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
571 path_len = strlen (path);
573 if (!paths_are_equal (priv->path, path, path_len)) {
574 GstRTSPThread *thread;
576 /* remove any previously cached values before we try to construct a new
582 gst_rtsp_media_unprepare (priv->media);
583 g_object_unref (priv->media);
587 /* prepare the media and add it to the pipeline */
588 if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
593 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
594 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
598 /* prepare the media */
599 if (!(gst_rtsp_media_prepare (media, thread)))
602 /* now keep track of the uri and the media */
603 priv->path = g_strndup (path, path_len);
606 /* we have seen this path before, used cached media */
609 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
612 g_object_unref (factory);
616 g_object_ref (media);
623 GST_ERROR ("client %p: no factory for path %s", client, path);
624 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
629 GST_ERROR ("client %p: not authorized to see factory path %s", client,
631 /* error reply is already sent */
636 GST_ERROR ("client %p: not authorized for factory path %s", client, path);
637 /* error reply is already sent */
642 GST_ERROR ("client %p: can't create media", client);
643 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
644 g_object_unref (factory);
650 GST_ERROR ("client %p: can't create thread", client);
651 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
652 g_object_unref (media);
654 g_object_unref (factory);
660 GST_ERROR ("client %p: can't prepare media", client);
661 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
662 g_object_unref (media);
664 g_object_unref (factory);
671 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
673 GstRTSPClientPrivate *priv = client->priv;
674 GstRTSPMessage message = { 0 };
679 gst_rtsp_message_init_data (&message, channel);
681 /* FIXME, need some sort of iovec RTSPMessage here */
682 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
685 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
687 g_mutex_lock (&priv->send_lock);
689 priv->send_func (client, &message, FALSE, priv->send_data);
690 g_mutex_unlock (&priv->send_lock);
692 gst_rtsp_message_steal_body (&message, &data, &usize);
693 gst_buffer_unmap (buffer, &map_info);
695 gst_rtsp_message_unset (&message);
701 link_transport (GstRTSPClient * client, GstRTSPSession * session,
702 GstRTSPStreamTransport * trans)
704 GstRTSPClientPrivate *priv = client->priv;
706 GST_DEBUG ("client %p: linking transport %p", client, trans);
708 gst_rtsp_stream_transport_set_callbacks (trans,
709 (GstRTSPSendFunc) do_send_data,
710 (GstRTSPSendFunc) do_send_data, client, NULL);
712 priv->transports = g_list_prepend (priv->transports, trans);
714 /* make sure our session can't expire */
715 gst_rtsp_session_prevent_expire (session);
719 link_session_transports (GstRTSPClient * client, GstRTSPSession * session,
720 GstRTSPSessionMedia * sessmedia)
725 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
726 for (i = 0; i < n_streams; i++) {
727 GstRTSPStreamTransport *trans;
728 const GstRTSPTransport *tr;
730 /* get the transport, if there is no transport configured, skip this stream */
731 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
735 tr = gst_rtsp_stream_transport_get_transport (trans);
737 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
738 /* for TCP, link the stream to the TCP connection of the client */
739 link_transport (client, session, trans);
745 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
746 GstRTSPStreamTransport * trans)
748 GstRTSPClientPrivate *priv = client->priv;
750 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
752 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
754 priv->transports = g_list_remove (priv->transports, trans);
756 /* our session can now expire */
757 gst_rtsp_session_allow_expire (session);
761 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
762 GstRTSPSessionMedia * sessmedia)
767 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
768 for (i = 0; i < n_streams; i++) {
769 GstRTSPStreamTransport *trans;
770 const GstRTSPTransport *tr;
772 /* get the transport, if there is no transport configured, skip this stream */
773 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
777 tr = gst_rtsp_stream_transport_get_transport (trans);
779 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
780 /* for TCP, unlink the stream from the TCP connection of the client */
781 unlink_transport (client, session, trans);
787 close_connection (GstRTSPClient * client)
789 GstRTSPClientPrivate *priv = client->priv;
790 const gchar *tunnelid;
792 GST_DEBUG ("client %p: closing connection", client);
794 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
795 g_mutex_lock (&tunnels_lock);
796 /* remove from tunnelids */
797 g_hash_table_remove (tunnels, tunnelid);
798 g_mutex_unlock (&tunnels_lock);
801 gst_rtsp_connection_close (priv->connection);
803 /* connection is now closed, destroy the watch which will also cause the
804 * closed signal to be emitted */
806 GST_DEBUG ("client %p: destroying watch", client);
807 g_source_destroy ((GSource *) priv->watch);
809 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
814 default_make_path_from_uri (GstRTSPClient * client, const GstRTSPUrl * uri)
819 path = g_strconcat (uri->abspath, "?", uri->query, NULL);
821 path = g_strdup (uri->abspath);
827 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
829 GstRTSPClientPrivate *priv = client->priv;
830 GstRTSPClientClass *klass;
831 GstRTSPSession *session;
832 GstRTSPSessionMedia *sessmedia;
833 GstRTSPStatusCode code;
840 session = ctx->session;
845 klass = GST_RTSP_CLIENT_GET_CLASS (client);
846 path = klass->make_path_from_uri (client, ctx->uri);
848 /* get a handle to the configuration of the media in the session */
849 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
853 /* only aggregate control for now.. */
854 if (path[matched] != '\0')
859 ctx->sessmedia = sessmedia;
861 /* we emit the signal before closing the connection */
862 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
865 /* make sure we unblock the backlog and don't accept new messages
867 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
869 /* unlink the all TCP callbacks */
870 unlink_session_transports (client, session, sessmedia);
872 /* remove the session from the watched sessions */
873 client_unwatch_session (client, session);
875 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
877 /* allow messages again so that we can send the reply */
878 gst_rtsp_watch_set_flushing (priv->watch, FALSE);
880 /* unmanage the media in the session, returns false if all media session
882 if (!gst_rtsp_session_release_media (session, sessmedia)) {
883 /* remove the session */
884 gst_rtsp_session_pool_remove (priv->session_pool, session);
886 /* construct the response now */
887 code = GST_RTSP_STS_OK;
888 gst_rtsp_message_init_response (ctx->response, code,
889 gst_rtsp_status_as_text (code), ctx->request);
891 send_message (client, ctx, ctx->response, TRUE);
898 GST_ERROR ("client %p: no session", client);
899 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
904 GST_ERROR ("client %p: no uri supplied", client);
905 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
910 GST_ERROR ("client %p: no media for uri", client);
911 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
917 GST_ERROR ("client %p: no aggregate path %s", client, path);
918 send_generic_response (client,
919 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
926 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
930 res = gst_rtsp_params_set (client, ctx);
936 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
940 res = gst_rtsp_params_get (client, ctx);
946 handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
952 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
953 if (res != GST_RTSP_OK)
957 /* no body, keep-alive request */
958 send_generic_response (client, GST_RTSP_STS_OK, ctx);
960 /* there is a body, handle the params */
961 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
962 if (res != GST_RTSP_OK)
965 send_message (client, ctx, ctx->response, FALSE);
968 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
976 GST_ERROR ("client %p: bad request", client);
977 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
983 handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
989 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
990 if (res != GST_RTSP_OK)
994 /* no body, keep-alive request */
995 send_generic_response (client, GST_RTSP_STS_OK, ctx);
997 /* there is a body, handle the params */
998 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
999 if (res != GST_RTSP_OK)
1002 send_message (client, ctx, ctx->response, FALSE);
1005 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
1013 GST_ERROR ("client %p: bad request", client);
1014 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1020 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
1022 GstRTSPSession *session;
1023 GstRTSPClientClass *klass;
1024 GstRTSPSessionMedia *sessmedia;
1025 GstRTSPStatusCode code;
1026 GstRTSPState rtspstate;
1030 if (!(session = ctx->session))
1036 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1037 path = klass->make_path_from_uri (client, ctx->uri);
1039 /* get a handle to the configuration of the media in the session */
1040 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1044 if (path[matched] != '\0')
1049 ctx->sessmedia = sessmedia;
1051 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1052 /* the session state must be playing or recording */
1053 if (rtspstate != GST_RTSP_STATE_PLAYING &&
1054 rtspstate != GST_RTSP_STATE_RECORDING)
1057 /* unlink the all TCP callbacks */
1058 unlink_session_transports (client, session, sessmedia);
1060 /* then pause sending */
1061 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
1063 /* construct the response now */
1064 code = GST_RTSP_STS_OK;
1065 gst_rtsp_message_init_response (ctx->response, code,
1066 gst_rtsp_status_as_text (code), ctx->request);
1068 send_message (client, ctx, ctx->response, FALSE);
1070 /* the state is now READY */
1071 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1073 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
1080 GST_ERROR ("client %p: no seesion", client);
1081 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1086 GST_ERROR ("client %p: no uri supplied", client);
1087 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1092 GST_ERROR ("client %p: no media for uri", client);
1093 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1099 GST_ERROR ("client %p: no aggregate path %s", client, path);
1100 send_generic_response (client,
1101 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1107 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
1108 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1114 /* convert @url and @path to a URL used as a content base for the factory
1115 * located at @path */
1117 make_base_url (GstRTSPClient * client, GstRTSPUrl * url, const gchar * path)
1123 /* check for trailing '/' and append one */
1124 trail = (path[strlen (path) - 1] != '/' ? "/" : "");
1129 tmp.abspath = g_strdup_printf ("%s%s", path, trail);
1131 result = gst_rtsp_url_get_request_uri (&tmp);
1132 g_free (tmp.abspath);
1138 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
1140 GstRTSPSession *session;
1141 GstRTSPClientClass *klass;
1142 GstRTSPSessionMedia *sessmedia;
1143 GstRTSPMedia *media;
1144 GstRTSPStatusCode code;
1147 GstRTSPTimeRange *range;
1149 GstRTSPState rtspstate;
1150 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1151 gchar *path, *rtpinfo;
1154 if (!(session = ctx->session))
1157 if (!(uri = ctx->uri))
1160 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1161 path = klass->make_path_from_uri (client, uri);
1163 /* get a handle to the configuration of the media in the session */
1164 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1168 if (path[matched] != '\0')
1173 ctx->sessmedia = sessmedia;
1174 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1176 /* the session state must be playing or ready */
1177 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1178 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1181 /* in play we first unsuspend, media could be suspended from SDP or PAUSED */
1182 if (!gst_rtsp_media_unsuspend (media))
1183 goto unsuspend_failed;
1185 /* parse the range header if we have one */
1186 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1187 if (res == GST_RTSP_OK) {
1188 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1189 /* we have a range, seek to the position */
1191 gst_rtsp_media_seek (media, range);
1192 gst_rtsp_range_free (range);
1196 /* link the all TCP callbacks */
1197 link_session_transports (client, session, sessmedia);
1199 /* grab RTPInfo from the media now */
1200 rtpinfo = gst_rtsp_session_media_get_rtpinfo (sessmedia);
1202 /* construct the response now */
1203 code = GST_RTSP_STS_OK;
1204 gst_rtsp_message_init_response (ctx->response, code,
1205 gst_rtsp_status_as_text (code), ctx->request);
1207 /* add the RTP-Info header */
1209 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO,
1213 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1215 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1217 send_message (client, ctx, ctx->response, FALSE);
1219 /* start playing after sending the response */
1220 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1222 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1224 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1231 GST_ERROR ("client %p: no session", client);
1232 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1237 GST_ERROR ("client %p: no uri supplied", client);
1238 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1243 GST_ERROR ("client %p: media not found", client);
1244 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1249 GST_ERROR ("client %p: no aggregate path %s", client, path);
1250 send_generic_response (client,
1251 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1257 GST_ERROR ("client %p: not PLAYING or READY", client);
1258 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1264 GST_ERROR ("client %p: unsuspend failed", client);
1265 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1271 do_keepalive (GstRTSPSession * session)
1273 GST_INFO ("keep session %p alive", session);
1274 gst_rtsp_session_touch (session);
1277 /* parse @transport and return a valid transport in @tr. only transports
1278 * supported by @stream are returned. Returns FALSE if no valid transport
1281 parse_transport (const char *transport, GstRTSPStream * stream,
1282 GstRTSPTransport * tr)
1289 gst_rtsp_transport_init (tr);
1291 GST_DEBUG ("parsing transports %s", transport);
1293 transports = g_strsplit (transport, ",", 0);
1295 /* loop through the transports, try to parse */
1296 for (i = 0; transports[i]; i++) {
1297 res = gst_rtsp_transport_parse (transports[i], tr);
1298 if (res != GST_RTSP_OK) {
1299 /* no valid transport, search some more */
1300 GST_WARNING ("could not parse transport %s", transports[i]);
1304 /* we have a transport, see if it's supported */
1305 if (!gst_rtsp_stream_is_transport_supported (stream, tr)) {
1306 GST_WARNING ("unsupported transport %s", transports[i]);
1310 /* we have a valid transport */
1311 GST_INFO ("found valid transport %s", transports[i]);
1316 gst_rtsp_transport_init (tr);
1318 g_strfreev (transports);
1324 default_configure_client_media (GstRTSPClient * client, GstRTSPMedia * media,
1325 GstRTSPStream * stream, GstRTSPContext * ctx)
1327 GstRTSPMessage *request = ctx->request;
1328 gchar *blocksize_str;
1330 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1331 &blocksize_str, 0) == GST_RTSP_OK) {
1335 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1336 if (end == blocksize_str)
1339 /* we don't want to change the mtu when this media
1340 * can be shared because it impacts other clients */
1341 if (gst_rtsp_media_is_shared (media))
1344 if (blocksize > G_MAXUINT)
1345 blocksize = G_MAXUINT;
1347 gst_rtsp_stream_set_mtu (stream, blocksize);
1355 GST_ERROR_OBJECT (client, "failed to parse blocksize");
1356 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1362 default_configure_client_transport (GstRTSPClient * client,
1363 GstRTSPContext * ctx, GstRTSPTransport * ct)
1365 GstRTSPClientPrivate *priv = client->priv;
1367 /* we have a valid transport now, set the destination of the client. */
1368 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1369 gboolean use_client_settings;
1371 use_client_settings =
1372 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1374 if (ct->destination && use_client_settings) {
1375 GstRTSPAddress *addr;
1377 addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1378 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1383 gst_rtsp_address_free (addr);
1385 GstRTSPAddress *addr;
1386 GSocketFamily family;
1388 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1390 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1394 g_free (ct->destination);
1395 ct->destination = g_strdup (addr->address);
1396 ct->port.min = addr->port;
1397 ct->port.max = addr->port + addr->n_ports - 1;
1398 ct->ttl = addr->ttl;
1400 gst_rtsp_address_free (addr);
1405 url = gst_rtsp_connection_get_url (priv->connection);
1406 g_free (ct->destination);
1407 ct->destination = g_strdup (url->host);
1409 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1411 GSocketAddress *addr;
1413 sock = gst_rtsp_connection_get_read_socket (priv->connection);
1414 if ((addr = g_socket_get_remote_address (sock, NULL))) {
1415 /* our read port is the sender port of client */
1416 ct->client_port.min =
1417 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1418 g_object_unref (addr);
1420 if ((addr = g_socket_get_local_address (sock, NULL))) {
1421 ct->server_port.max =
1422 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1423 g_object_unref (addr);
1425 sock = gst_rtsp_connection_get_write_socket (priv->connection);
1426 if ((addr = g_socket_get_remote_address (sock, NULL))) {
1427 /* our write port is the receiver port of client */
1428 ct->client_port.max =
1429 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1430 g_object_unref (addr);
1432 if ((addr = g_socket_get_local_address (sock, NULL))) {
1433 ct->server_port.min =
1434 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1435 g_object_unref (addr);
1437 /* check if the client selected channels for TCP */
1438 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1439 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1449 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1454 static GstRTSPTransport *
1455 make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
1456 GstRTSPTransport * ct)
1458 GstRTSPTransport *st;
1460 GSocketFamily family;
1462 /* prepare the server transport */
1463 gst_rtsp_transport_new (&st);
1465 st->trans = ct->trans;
1466 st->profile = ct->profile;
1467 st->lower_transport = ct->lower_transport;
1469 addr = g_inet_address_new_from_string (ct->destination);
1472 GST_ERROR ("failed to get inet addr from client destination");
1473 family = G_SOCKET_FAMILY_IPV4;
1475 family = g_inet_address_get_family (addr);
1476 g_object_unref (addr);
1480 switch (st->lower_transport) {
1481 case GST_RTSP_LOWER_TRANS_UDP:
1482 st->client_port = ct->client_port;
1483 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
1485 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1486 st->port = ct->port;
1487 st->destination = g_strdup (ct->destination);
1490 case GST_RTSP_LOWER_TRANS_TCP:
1491 st->interleaved = ct->interleaved;
1492 st->client_port = ct->client_port;
1493 st->server_port = ct->server_port;
1498 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
1503 #define AES_128_KEY_LEN 16
1504 #define AES_256_KEY_LEN 32
1506 #define HMAC_32_KEY_LEN 4
1507 #define HMAC_80_KEY_LEN 10
1510 mikey_apply_policy (GstCaps * caps, GstMIKEYMessage * msg, guint8 policy)
1512 const gchar *srtp_cipher;
1513 const gchar *srtp_auth;
1514 const GstMIKEYPayload *sp;
1517 /* loop over Security policy until we find one containing policy */
1519 if ((sp = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, i)) == NULL)
1522 if (((GstMIKEYPayloadSP *) sp)->policy == policy)
1526 /* the default ciphers */
1527 srtp_cipher = "aes-128-icm";
1528 srtp_auth = "hmac-sha1-80";
1530 /* now override the defaults with what is in the Security Policy */
1534 /* collect all the params and go over them */
1535 len = gst_mikey_payload_sp_get_n_params (sp);
1536 for (i = 0; i < len; i++) {
1537 const GstMIKEYPayloadSPParam *param =
1538 gst_mikey_payload_sp_get_param (sp, i);
1540 switch (param->type) {
1541 case GST_MIKEY_SP_SRTP_ENC_ALG:
1542 switch (param->val[0]) {
1544 srtp_cipher = "null";
1548 srtp_cipher = "aes-128-icm";
1554 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
1555 switch (param->val[0]) {
1556 case AES_128_KEY_LEN:
1557 srtp_cipher = "aes-128-icm";
1559 case AES_256_KEY_LEN:
1560 srtp_cipher = "aes-256-icm";
1566 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1567 switch (param->val[0]) {
1573 srtp_auth = "hmac-sha1-80";
1579 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
1580 switch (param->val[0]) {
1581 case HMAC_32_KEY_LEN:
1582 srtp_auth = "hmac-sha1-32";
1584 case HMAC_80_KEY_LEN:
1585 srtp_auth = "hmac-sha1-80";
1591 case GST_MIKEY_SP_SRTP_SRTP_ENC:
1593 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
1600 /* now configure the SRTP parameters */
1601 gst_caps_set_simple (caps,
1602 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
1603 "srtp-auth", G_TYPE_STRING, srtp_auth,
1604 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
1605 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
1611 handle_mikey_data (GstRTSPClient * client, GstRTSPContext * ctx,
1612 guint8 * data, gsize size)
1614 GstMIKEYMessage *msg;
1616 GstCaps *caps = NULL;
1617 GstMIKEYPayloadKEMAC *kemac;
1618 const GstMIKEYPayloadKeyData *pkd;
1621 /* the MIKEY message contains a CSB or crypto session bundle. It is a
1622 * set of Crypto Sessions protected with the same master key.
1623 * In the context of SRTP, an RTP and its RTCP stream is part of a
1625 if ((msg = gst_mikey_message_new_from_data (data, size, NULL, NULL)) == NULL)
1628 /* we can only handle SRTP crypto sessions for now */
1629 if (msg->map_type != GST_MIKEY_MAP_TYPE_SRTP)
1630 goto invalid_map_type;
1632 /* get the number of crypto sessions. This maps SSRC to its
1633 * security parameters */
1634 n_cs = gst_mikey_message_get_n_cs (msg);
1636 goto no_crypto_sessions;
1638 /* we also need keys */
1639 if (!(kemac = (GstMIKEYPayloadKEMAC *) gst_mikey_message_find_payload
1640 (msg, GST_MIKEY_PT_KEMAC, 0)))
1643 /* we don't support encrypted keys */
1644 if (kemac->enc_alg != GST_MIKEY_ENC_NULL
1645 || kemac->mac_alg != GST_MIKEY_MAC_NULL)
1646 goto unsupported_encryption;
1648 /* get Key data sub-payload */
1649 pkd = (const GstMIKEYPayloadKeyData *)
1650 gst_mikey_payload_kemac_get_sub (&kemac->pt, 0);
1653 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
1656 /* go over all crypto sessions and create the security policy for each
1658 for (i = 0; i < n_cs; i++) {
1659 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
1661 caps = gst_caps_new_simple ("application/x-srtp",
1662 "ssrc", G_TYPE_UINT, map->ssrc,
1663 "roc", G_TYPE_UINT, map->roc, "srtp-key", GST_TYPE_BUFFER, key, NULL);
1664 mikey_apply_policy (caps, msg, map->policy);
1666 gst_rtsp_stream_update_crypto (ctx->stream, map->ssrc, caps);
1667 gst_caps_unref (caps);
1669 gst_mikey_message_free (msg);
1676 GST_DEBUG_OBJECT (client, "failed to parse MIKEY message");
1681 GST_DEBUG_OBJECT (client, "invalid map type %d", msg->map_type);
1682 goto cleanup_message;
1686 GST_DEBUG_OBJECT (client, "no crypto sessions");
1687 goto cleanup_message;
1691 GST_DEBUG_OBJECT (client, "no keys found");
1692 goto cleanup_message;
1694 unsupported_encryption:
1696 GST_DEBUG_OBJECT (client, "unsupported key encryption");
1697 goto cleanup_message;
1701 gst_mikey_message_free (msg);
1706 #define IS_STRIP_CHAR(c) (g_ascii_isspace ((guchar)(c)) || ((c) == '\"'))
1709 strip_chars (gchar * str)
1716 if (!IS_STRIP_CHAR (str[len]))
1720 for (s = str; *s && IS_STRIP_CHAR (*s); s++);
1721 memmove (str, s, len + 1);
1725 * KeyMgmt = "KeyMgmt" ":" key-mgmt-spec 0*("," key-mgmt-spec)
1726 * key-mgmt-spec = "prot" "=" KMPID ";" ["uri" "=" %x22 URI %x22 ";"]
1729 handle_keymgmt (GstRTSPClient * client, GstRTSPContext * ctx, gchar * keymgmt)
1734 specs = g_strsplit (keymgmt, ",", 0);
1735 for (i = 0; specs[i]; i++) {
1738 split = g_strsplit (specs[i], ";", 0);
1739 for (j = 0; split[j]; j++) {
1740 g_strstrip (split[j]);
1741 if (g_str_has_prefix (split[j], "prot=")) {
1742 g_strstrip (split[j] + 5);
1743 if (!g_str_equal (split[j] + 5, "mikey"))
1745 GST_DEBUG ("found mikey");
1746 } else if (g_str_has_prefix (split[j], "uri=")) {
1747 strip_chars (split[j] + 4);
1748 GST_DEBUG ("found uri '%s'", split[j] + 4);
1749 } else if (g_str_has_prefix (split[j], "data=")) {
1752 strip_chars (split[j] + 5);
1753 GST_DEBUG ("found data '%s'", split[j] + 5);
1754 data = g_base64_decode_inplace (split[j] + 5, &size);
1755 handle_mikey_data (client, ctx, data, size);
1763 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
1765 GstRTSPClientPrivate *priv = client->priv;
1768 gchar *transport, *keymgmt;
1769 GstRTSPTransport *ct, *st;
1770 GstRTSPStatusCode code;
1771 GstRTSPSession *session;
1772 GstRTSPStreamTransport *trans;
1774 GstRTSPSessionMedia *sessmedia;
1775 GstRTSPMedia *media;
1776 GstRTSPStream *stream;
1777 GstRTSPState rtspstate;
1778 GstRTSPClientClass *klass;
1779 gchar *path, *control;
1786 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1787 path = klass->make_path_from_uri (client, uri);
1789 /* parse the transport */
1791 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
1793 if (res != GST_RTSP_OK)
1796 /* we create the session after parsing stuff so that we don't make
1797 * a session for malformed requests */
1798 if (priv->session_pool == NULL)
1801 session = ctx->session;
1804 g_object_ref (session);
1805 /* get a handle to the configuration of the media in the session, this can
1806 * return NULL if this is a new url to manage in this session. */
1807 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1809 /* we need a new media configuration in this session */
1813 /* we have no session media, find one and manage it */
1814 if (sessmedia == NULL) {
1815 /* get a handle to the configuration of the media in the session */
1816 media = find_media (client, ctx, path, &matched);
1818 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1819 g_object_ref (media);
1821 goto media_not_found;
1823 /* no media, not found then */
1825 goto media_not_found_no_reply;
1827 if (path[matched] == '\0')
1828 goto control_not_found;
1830 /* path is what matched. */
1831 path[matched] = '\0';
1832 /* control is remainder */
1833 control = &path[matched + 1];
1835 /* find the stream now using the control part */
1836 stream = gst_rtsp_media_find_stream (media, control);
1838 goto stream_not_found;
1840 /* now we have a uri identifying a valid media and stream */
1841 ctx->stream = stream;
1844 if (session == NULL) {
1845 /* create a session if this fails we probably reached our session limit or
1847 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1848 goto service_unavailable;
1850 /* make sure this client is closed when the session is closed */
1851 client_watch_session (client, session);
1853 /* signal new session */
1854 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1857 ctx->session = session;
1860 if (sessmedia == NULL) {
1861 /* manage the media in our session now, if not done already */
1862 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1863 /* if we stil have no media, error */
1864 if (sessmedia == NULL)
1865 goto sessmedia_unavailable;
1867 g_object_unref (media);
1870 ctx->sessmedia = sessmedia;
1872 if (!klass->configure_client_media (client, media, stream, ctx))
1873 goto configure_media_failed_no_reply;
1875 gst_rtsp_transport_new (&ct);
1877 /* parse and find a usable supported transport */
1878 if (!parse_transport (transport, stream, ct))
1879 goto unsupported_transports;
1881 /* update the client transport */
1882 if (!klass->configure_client_transport (client, ctx, ct))
1883 goto unsupported_client_transport;
1885 /* parse the keymgmt */
1886 if (gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_KEYMGMT,
1887 &keymgmt, 0) == GST_RTSP_OK) {
1888 if (!handle_keymgmt (client, ctx, keymgmt))
1892 /* set in the session media transport */
1893 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1895 /* configure the url used to set this transport, this we will use when
1896 * generating the response for the PLAY request */
1897 gst_rtsp_stream_transport_set_url (trans, uri);
1899 /* configure keepalive for this transport */
1900 gst_rtsp_stream_transport_set_keepalive (trans,
1901 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1903 /* create and serialize the server transport */
1904 st = make_server_transport (client, ctx, ct);
1905 trans_str = gst_rtsp_transport_as_text (st);
1906 gst_rtsp_transport_free (st);
1908 /* construct the response now */
1909 code = GST_RTSP_STS_OK;
1910 gst_rtsp_message_init_response (ctx->response, code,
1911 gst_rtsp_status_as_text (code), ctx->request);
1913 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
1917 send_message (client, ctx, ctx->response, FALSE);
1919 /* update the state */
1920 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1921 switch (rtspstate) {
1922 case GST_RTSP_STATE_PLAYING:
1923 case GST_RTSP_STATE_RECORDING:
1924 case GST_RTSP_STATE_READY:
1925 /* no state change */
1928 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1931 g_object_unref (session);
1934 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
1941 GST_ERROR ("client %p: no uri", client);
1942 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1947 GST_ERROR ("client %p: no transport", client);
1948 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1953 GST_ERROR ("client %p: no session pool configured", client);
1954 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1957 media_not_found_no_reply:
1959 GST_ERROR ("client %p: media '%s' not found", client, path);
1960 /* error reply is already sent */
1965 GST_ERROR ("client %p: media '%s' not found", client, path);
1966 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1971 GST_ERROR ("client %p: no control in path '%s'", client, path);
1972 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1973 g_object_unref (media);
1978 GST_ERROR ("client %p: stream '%s' not found", client, control);
1979 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1980 g_object_unref (media);
1983 service_unavailable:
1985 GST_ERROR ("client %p: can't create session", client);
1986 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1987 g_object_unref (media);
1990 sessmedia_unavailable:
1992 GST_ERROR ("client %p: can't create session media", client);
1993 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1994 g_object_unref (media);
1995 goto cleanup_session;
1997 configure_media_failed_no_reply:
1999 GST_ERROR ("client %p: configure_media failed", client);
2000 /* error reply is already sent */
2001 goto cleanup_session;
2003 unsupported_transports:
2005 GST_ERROR ("client %p: unsupported transports", client);
2006 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
2007 goto cleanup_transport;
2009 unsupported_client_transport:
2011 GST_ERROR ("client %p: unsupported client transport", client);
2012 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
2013 goto cleanup_transport;
2017 GST_ERROR ("client %p: keymgmt error", client);
2018 send_generic_response (client, GST_RTSP_STS_KEY_MANAGEMENT_FAILURE, ctx);
2019 goto cleanup_transport;
2023 gst_rtsp_transport_free (ct);
2025 g_object_unref (session);
2032 static GstSDPMessage *
2033 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
2035 GstRTSPClientPrivate *priv = client->priv;
2040 gst_sdp_message_new (&sdp);
2042 /* some standard things first */
2043 gst_sdp_message_set_version (sdp, "0");
2050 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
2053 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
2054 gst_sdp_message_set_information (sdp, "rtsp-server");
2055 gst_sdp_message_add_time (sdp, "0", "0", NULL);
2056 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
2057 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
2058 gst_sdp_message_add_attribute (sdp, "control", "*");
2060 info.is_ipv6 = priv->is_ipv6;
2061 info.server_ip = priv->server_ip;
2063 /* create an SDP for the media object */
2064 if (!gst_rtsp_media_setup_sdp (media, sdp, &info))
2072 GST_ERROR ("client %p: could not create SDP", client);
2073 gst_sdp_message_free (sdp);
2078 /* for the describe we must generate an SDP */
2080 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
2082 GstRTSPClientPrivate *priv = client->priv;
2087 GstRTSPMedia *media;
2088 GstRTSPClientClass *klass;
2090 klass = GST_RTSP_CLIENT_GET_CLASS (client);
2095 /* check what kind of format is accepted, we don't really do anything with it
2096 * and always return SDP for now. */
2101 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
2103 if (res == GST_RTSP_ENOTIMPL)
2106 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
2110 if (!priv->mount_points)
2111 goto no_mount_points;
2113 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
2116 /* find the media object for the uri */
2117 if (!(media = find_media (client, ctx, path, NULL)))
2120 /* create an SDP for the media object on this client */
2121 if (!(sdp = klass->create_sdp (client, media)))
2124 /* we suspend after the describe */
2125 gst_rtsp_media_suspend (media);
2126 g_object_unref (media);
2128 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2129 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2131 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
2134 /* content base for some clients that might screw up creating the setup uri */
2135 str = make_base_url (client, ctx->uri, path);
2138 GST_INFO ("adding content-base: %s", str);
2139 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE, str);
2141 /* add SDP to the response body */
2142 str = gst_sdp_message_as_text (sdp);
2143 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
2144 gst_sdp_message_free (sdp);
2146 send_message (client, ctx, ctx->response, FALSE);
2148 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
2156 GST_ERROR ("client %p: no uri", client);
2157 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2162 GST_ERROR ("client %p: no mount points configured", client);
2163 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2168 GST_ERROR ("client %p: can't find path for url", client);
2169 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2174 GST_ERROR ("client %p: no media", client);
2176 /* error reply is already sent */
2181 GST_ERROR ("client %p: can't create SDP", client);
2182 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2184 g_object_unref (media);
2190 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
2192 GstRTSPMethod options;
2195 options = GST_RTSP_DESCRIBE |
2200 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
2202 str = gst_rtsp_options_as_text (options);
2204 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2205 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2207 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
2210 send_message (client, ctx, ctx->response, FALSE);
2212 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
2218 /* remove duplicate and trailing '/' */
2220 sanitize_uri (GstRTSPUrl * uri)
2224 gboolean have_slash, prev_slash;
2226 s = d = uri->abspath;
2227 len = strlen (uri->abspath);
2231 for (i = 0; i < len; i++) {
2232 have_slash = s[i] == '/';
2234 if (!have_slash || !prev_slash)
2236 prev_slash = have_slash;
2238 len = d - uri->abspath;
2239 /* don't remove the first slash if that's the only thing left */
2240 if (len > 1 && *(d - 1) == '/')
2246 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
2248 GstRTSPClientPrivate *priv = client->priv;
2250 GST_INFO ("client %p: session %p finished", client, session);
2252 /* unlink all media managed in this session */
2253 client_unlink_session (client, session);
2255 /* remove the session */
2256 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
2257 GST_INFO ("client %p: all sessions finalized, close the connection",
2259 close_connection (client);
2263 /* Returns TRUE if there are no Require headers, otherwise returns FALSE
2264 * and also returns a newly-allocated string of (comma-separated) unsupported
2265 * options in the unsupported_reqs variable .
2267 * There may be multiple Require headers, but we must send one single
2268 * Unsupported header with all the unsupported options as response. If
2269 * an incoming Require header contained a comma-separated list of options
2270 * GstRtspConnection will already have split that list up into multiple
2273 * TODO: allow the application to decide what features are supported
2276 check_request_requirements (GstRTSPMessage * msg, gchar ** unsupported_reqs)
2279 GPtrArray *arr = NULL;
2285 res = gst_rtsp_message_get_header (msg, GST_RTSP_HDR_REQUIRE, &reqs, i++);
2287 if (res == GST_RTSP_ENOTIMPL)
2291 arr = g_ptr_array_new_with_free_func ((GDestroyNotify) g_free);
2293 g_ptr_array_add (arr, g_strdup (reqs));
2297 /* if we don't have any Require headers at all, all is fine */
2301 /* otherwise we've now processed at all the Require headers */
2302 g_ptr_array_add (arr, NULL);
2304 /* for now we don't commit to supporting anything, so will just report
2305 * all of the required options as unsupported */
2306 *unsupported_reqs = g_strjoinv (", ", (gchar **) arr->pdata);
2308 g_ptr_array_unref (arr);
2313 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
2315 GstRTSPClientPrivate *priv = client->priv;
2316 GstRTSPMethod method;
2317 const gchar *uristr;
2318 GstRTSPUrl *uri = NULL;
2319 GstRTSPVersion version;
2321 GstRTSPSession *session = NULL;
2322 GstRTSPContext sctx = { NULL }, *ctx;
2323 GstRTSPMessage response = { 0 };
2324 gchar *unsupported_reqs = NULL;
2327 if (!(ctx = gst_rtsp_context_get_current ())) {
2329 ctx->auth = priv->auth;
2330 gst_rtsp_context_push_current (ctx);
2333 ctx->conn = priv->connection;
2334 ctx->client = client;
2335 ctx->request = request;
2336 ctx->response = &response;
2338 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2339 gst_rtsp_message_dump (request);
2342 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
2344 GST_INFO ("client %p: received a request %s %s %s", client,
2345 gst_rtsp_method_as_text (method), uristr,
2346 gst_rtsp_version_as_text (version));
2348 /* we can only handle 1.0 requests */
2349 if (version != GST_RTSP_VERSION_1_0)
2352 ctx->method = method;
2354 /* we always try to parse the url first */
2355 if (strcmp (uristr, "*") == 0) {
2356 /* special case where we have * as uri, keep uri = NULL */
2357 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
2358 /* check if the uristr is an absolute path <=> scheme and host information
2362 scheme = g_uri_parse_scheme (uristr);
2363 if (scheme == NULL && g_str_has_prefix (uristr, "/")) {
2364 gchar *absolute_uristr = NULL;
2366 GST_WARNING_OBJECT (client, "request doesn't contain absolute url");
2367 if (priv->server_ip == NULL) {
2368 GST_WARNING_OBJECT (client, "host information missing");
2373 g_strdup_printf ("rtsp://%s%s", priv->server_ip, uristr);
2375 GST_DEBUG_OBJECT (client, "absolute url: %s", absolute_uristr);
2376 if (gst_rtsp_url_parse (absolute_uristr, &uri) != GST_RTSP_OK) {
2377 g_free (absolute_uristr);
2380 g_free (absolute_uristr);
2387 /* get the session if there is any */
2388 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
2389 if (res == GST_RTSP_OK) {
2390 if (priv->session_pool == NULL)
2393 /* we had a session in the request, find it again */
2394 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2395 goto session_not_found;
2397 /* we add the session to the client list of watched sessions. When a session
2398 * disappears because it times out, we will be notified. If all sessions are
2399 * gone, we will close the connection */
2400 client_watch_session (client, session);
2403 /* sanitize the uri */
2407 ctx->session = session;
2409 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
2410 goto not_authorized;
2412 /* handle any 'Require' headers */
2413 if (!check_request_requirements (ctx->request, &unsupported_reqs))
2414 goto unsupported_requirement;
2416 /* now see what is asked and dispatch to a dedicated handler */
2418 case GST_RTSP_OPTIONS:
2419 handle_options_request (client, ctx);
2421 case GST_RTSP_DESCRIBE:
2422 handle_describe_request (client, ctx);
2424 case GST_RTSP_SETUP:
2425 handle_setup_request (client, ctx);
2428 handle_play_request (client, ctx);
2430 case GST_RTSP_PAUSE:
2431 handle_pause_request (client, ctx);
2433 case GST_RTSP_TEARDOWN:
2434 handle_teardown_request (client, ctx);
2436 case GST_RTSP_SET_PARAMETER:
2437 handle_set_param_request (client, ctx);
2439 case GST_RTSP_GET_PARAMETER:
2440 handle_get_param_request (client, ctx);
2442 case GST_RTSP_ANNOUNCE:
2443 case GST_RTSP_RECORD:
2444 case GST_RTSP_REDIRECT:
2445 goto not_implemented;
2446 case GST_RTSP_INVALID:
2453 gst_rtsp_context_pop_current (ctx);
2455 g_object_unref (session);
2457 gst_rtsp_url_free (uri);
2463 GST_ERROR ("client %p: version %d not supported", client, version);
2464 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
2470 GST_ERROR ("client %p: bad request", client);
2471 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2476 GST_ERROR ("client %p: no pool configured", client);
2477 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2482 GST_ERROR ("client %p: session not found", client);
2483 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2488 GST_ERROR ("client %p: not allowed", client);
2489 /* error reply is already sent */
2492 unsupported_requirement:
2494 GST_ERROR ("client %p: Required option is not supported (%s)", client,
2496 send_option_not_supported_response (client, ctx, unsupported_reqs);
2497 g_free (unsupported_reqs);
2502 GST_ERROR ("client %p: method %d not implemented", client, method);
2503 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
2510 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
2512 GstRTSPClientPrivate *priv = client->priv;
2514 GstRTSPSession *session = NULL;
2515 GstRTSPContext sctx = { NULL }, *ctx;
2518 if (!(ctx = gst_rtsp_context_get_current ())) {
2520 ctx->auth = priv->auth;
2521 gst_rtsp_context_push_current (ctx);
2524 ctx->conn = priv->connection;
2525 ctx->client = client;
2526 ctx->request = NULL;
2528 ctx->method = GST_RTSP_INVALID;
2529 ctx->response = response;
2531 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2532 gst_rtsp_message_dump (response);
2535 GST_INFO ("client %p: received a response", client);
2537 /* get the session if there is any */
2539 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
2540 if (res == GST_RTSP_OK) {
2541 if (priv->session_pool == NULL)
2544 /* we had a session in the request, find it again */
2545 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2546 goto session_not_found;
2548 /* we add the session to the client list of watched sessions. When a session
2549 * disappears because it times out, we will be notified. If all sessions are
2550 * gone, we will close the connection */
2551 client_watch_session (client, session);
2554 ctx->session = session;
2556 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
2561 gst_rtsp_context_pop_current (ctx);
2563 g_object_unref (session);
2568 GST_ERROR ("client %p: no pool configured", client);
2573 GST_ERROR ("client %p: session not found", client);
2579 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2581 GstRTSPClientPrivate *priv = client->priv;
2590 /* find the stream for this message */
2591 res = gst_rtsp_message_parse_data (message, &channel);
2592 if (res != GST_RTSP_OK)
2595 gst_rtsp_message_steal_body (message, &data, &size);
2597 buffer = gst_buffer_new_wrapped (data, size);
2600 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2601 GstRTSPStreamTransport *trans;
2602 GstRTSPStream *stream;
2603 const GstRTSPTransport *tr;
2607 tr = gst_rtsp_stream_transport_get_transport (trans);
2608 stream = gst_rtsp_stream_transport_get_stream (trans);
2610 /* check for TCP transport */
2611 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2612 /* dispatch to the stream based on the channel number */
2613 if (tr->interleaved.min == channel) {
2614 gst_rtsp_stream_recv_rtp (stream, buffer);
2617 } else if (tr->interleaved.max == channel) {
2618 gst_rtsp_stream_recv_rtcp (stream, buffer);
2625 gst_buffer_unref (buffer);
2629 * gst_rtsp_client_set_session_pool:
2630 * @client: a #GstRTSPClient
2631 * @pool: (transfer none): a #GstRTSPSessionPool
2633 * Set @pool as the sessionpool for @client which it will use to find
2634 * or allocate sessions. the sessionpool is usually inherited from the server
2635 * that created the client but can be overridden later.
2638 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2639 GstRTSPSessionPool * pool)
2641 GstRTSPSessionPool *old;
2642 GstRTSPClientPrivate *priv;
2644 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2646 priv = client->priv;
2649 g_object_ref (pool);
2651 g_mutex_lock (&priv->lock);
2652 old = priv->session_pool;
2653 priv->session_pool = pool;
2654 g_mutex_unlock (&priv->lock);
2657 g_object_unref (old);
2661 * gst_rtsp_client_get_session_pool:
2662 * @client: a #GstRTSPClient
2664 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2666 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2668 GstRTSPSessionPool *
2669 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2671 GstRTSPClientPrivate *priv;
2672 GstRTSPSessionPool *result;
2674 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2676 priv = client->priv;
2678 g_mutex_lock (&priv->lock);
2679 if ((result = priv->session_pool))
2680 g_object_ref (result);
2681 g_mutex_unlock (&priv->lock);
2687 * gst_rtsp_client_set_mount_points:
2688 * @client: a #GstRTSPClient
2689 * @mounts: (transfer none): a #GstRTSPMountPoints
2691 * Set @mounts as the mount points for @client which it will use to map urls
2692 * to media streams. These mount points are usually inherited from the server that
2693 * created the client but can be overriden later.
2696 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2697 GstRTSPMountPoints * mounts)
2699 GstRTSPClientPrivate *priv;
2700 GstRTSPMountPoints *old;
2702 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2704 priv = client->priv;
2707 g_object_ref (mounts);
2709 g_mutex_lock (&priv->lock);
2710 old = priv->mount_points;
2711 priv->mount_points = mounts;
2712 g_mutex_unlock (&priv->lock);
2715 g_object_unref (old);
2719 * gst_rtsp_client_get_mount_points:
2720 * @client: a #GstRTSPClient
2722 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2724 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2726 GstRTSPMountPoints *
2727 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2729 GstRTSPClientPrivate *priv;
2730 GstRTSPMountPoints *result;
2732 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2734 priv = client->priv;
2736 g_mutex_lock (&priv->lock);
2737 if ((result = priv->mount_points))
2738 g_object_ref (result);
2739 g_mutex_unlock (&priv->lock);
2745 * gst_rtsp_client_set_auth:
2746 * @client: a #GstRTSPClient
2747 * @auth: (transfer none): a #GstRTSPAuth
2749 * configure @auth to be used as the authentication manager of @client.
2752 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2754 GstRTSPClientPrivate *priv;
2757 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2759 priv = client->priv;
2762 g_object_ref (auth);
2764 g_mutex_lock (&priv->lock);
2767 g_mutex_unlock (&priv->lock);
2770 g_object_unref (old);
2775 * gst_rtsp_client_get_auth:
2776 * @client: a #GstRTSPClient
2778 * Get the #GstRTSPAuth used as the authentication manager of @client.
2780 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2784 gst_rtsp_client_get_auth (GstRTSPClient * client)
2786 GstRTSPClientPrivate *priv;
2787 GstRTSPAuth *result;
2789 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2791 priv = client->priv;
2793 g_mutex_lock (&priv->lock);
2794 if ((result = priv->auth))
2795 g_object_ref (result);
2796 g_mutex_unlock (&priv->lock);
2802 * gst_rtsp_client_set_thread_pool:
2803 * @client: a #GstRTSPClient
2804 * @pool: (transfer none): a #GstRTSPThreadPool
2806 * configure @pool to be used as the thread pool of @client.
2809 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2810 GstRTSPThreadPool * pool)
2812 GstRTSPClientPrivate *priv;
2813 GstRTSPThreadPool *old;
2815 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2817 priv = client->priv;
2820 g_object_ref (pool);
2822 g_mutex_lock (&priv->lock);
2823 old = priv->thread_pool;
2824 priv->thread_pool = pool;
2825 g_mutex_unlock (&priv->lock);
2828 g_object_unref (old);
2832 * gst_rtsp_client_get_thread_pool:
2833 * @client: a #GstRTSPClient
2835 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2837 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2841 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2843 GstRTSPClientPrivate *priv;
2844 GstRTSPThreadPool *result;
2846 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2848 priv = client->priv;
2850 g_mutex_lock (&priv->lock);
2851 if ((result = priv->thread_pool))
2852 g_object_ref (result);
2853 g_mutex_unlock (&priv->lock);
2859 * gst_rtsp_client_set_connection:
2860 * @client: a #GstRTSPClient
2861 * @conn: (transfer full): a #GstRTSPConnection
2863 * Set the #GstRTSPConnection of @client. This function takes ownership of
2866 * Returns: %TRUE on success.
2869 gst_rtsp_client_set_connection (GstRTSPClient * client,
2870 GstRTSPConnection * conn)
2872 GstRTSPClientPrivate *priv;
2873 GSocket *read_socket;
2874 GSocketAddress *address;
2876 GError *error = NULL;
2878 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2879 g_return_val_if_fail (conn != NULL, FALSE);
2881 priv = client->priv;
2883 read_socket = gst_rtsp_connection_get_read_socket (conn);
2885 if (!(address = g_socket_get_local_address (read_socket, &error)))
2888 g_free (priv->server_ip);
2889 /* keep the original ip that the client connected to */
2890 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2891 GInetAddress *iaddr;
2893 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2895 /* socket might be ipv6 but adress still ipv4 */
2896 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2897 priv->server_ip = g_inet_address_to_string (iaddr);
2898 g_object_unref (address);
2900 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2901 priv->server_ip = g_strdup ("unknown");
2904 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2905 priv->server_ip, priv->is_ipv6);
2907 url = gst_rtsp_connection_get_url (conn);
2908 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2910 priv->connection = conn;
2917 GST_ERROR ("could not get local address %s", error->message);
2918 g_error_free (error);
2924 * gst_rtsp_client_get_connection:
2925 * @client: a #GstRTSPClient
2927 * Get the #GstRTSPConnection of @client.
2929 * Returns: (transfer none): the #GstRTSPConnection of @client.
2930 * The connection object returned remains valid until the client is freed.
2933 gst_rtsp_client_get_connection (GstRTSPClient * client)
2935 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2937 return client->priv->connection;
2941 * gst_rtsp_client_set_send_func:
2942 * @client: a #GstRTSPClient
2943 * @func: (scope notified): a #GstRTSPClientSendFunc
2944 * @user_data: (closure): user data passed to @func
2945 * @notify: (allow-none): called when @user_data is no longer in use
2947 * Set @func as the callback that will be called when a new message needs to be
2948 * sent to the client. @user_data is passed to @func and @notify is called when
2949 * @user_data is no longer in use.
2951 * By default, the client will send the messages on the #GstRTSPConnection that
2952 * was configured with gst_rtsp_client_attach() was called.
2955 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2956 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2958 GstRTSPClientPrivate *priv;
2959 GDestroyNotify old_notify;
2962 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2964 priv = client->priv;
2966 g_mutex_lock (&priv->send_lock);
2967 priv->send_func = func;
2968 old_notify = priv->send_notify;
2969 old_data = priv->send_data;
2970 priv->send_notify = notify;
2971 priv->send_data = user_data;
2972 g_mutex_unlock (&priv->send_lock);
2975 old_notify (old_data);
2979 * gst_rtsp_client_handle_message:
2980 * @client: a #GstRTSPClient
2981 * @message: (transfer none): an #GstRTSPMessage
2983 * Let the client handle @message.
2985 * Returns: a #GstRTSPResult.
2988 gst_rtsp_client_handle_message (GstRTSPClient * client,
2989 GstRTSPMessage * message)
2991 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2992 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2994 switch (message->type) {
2995 case GST_RTSP_MESSAGE_REQUEST:
2996 handle_request (client, message);
2998 case GST_RTSP_MESSAGE_RESPONSE:
2999 handle_response (client, message);
3001 case GST_RTSP_MESSAGE_DATA:
3002 handle_data (client, message);
3011 * gst_rtsp_client_send_message:
3012 * @client: a #GstRTSPClient
3013 * @session: (transfer none): a #GstRTSPSession to send the message to or %NULL
3014 * @message: (transfer none): The #GstRTSPMessage to send
3016 * Send a message message to the remote end. @message must be a
3017 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
3020 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
3021 GstRTSPMessage * message)
3023 GstRTSPContext sctx = { NULL }
3025 GstRTSPClientPrivate *priv;
3027 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
3028 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3029 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
3030 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
3032 priv = client->priv;
3034 if (!(ctx = gst_rtsp_context_get_current ())) {
3036 ctx->auth = priv->auth;
3037 gst_rtsp_context_push_current (ctx);
3040 ctx->conn = priv->connection;
3041 ctx->client = client;
3042 ctx->session = session;
3044 send_message (client, ctx, message, FALSE);
3047 gst_rtsp_context_pop_current (ctx);
3052 static GstRTSPResult
3053 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
3054 gboolean close, gpointer user_data)
3056 GstRTSPClientPrivate *priv = client->priv;
3064 /* send the response and store the seq number so we can wait until it's
3065 * written to the client to close the connection */
3067 gst_rtsp_watch_send_message (priv->watch, message,
3068 close ? &priv->close_seq : NULL);
3069 if (ret == GST_RTSP_OK)
3072 if (ret != GST_RTSP_ENOMEM)
3076 if (priv->drop_backlog)
3079 /* queue was full, wait for more space */
3080 GST_DEBUG_OBJECT (client, "waiting for backlog");
3081 ret = gst_rtsp_watch_wait_backlog (priv->watch, &time);
3082 GST_DEBUG_OBJECT (client, "Resend due to backlog full");
3083 } while (ret != GST_RTSP_EINTR);
3090 GST_DEBUG_OBJECT (client, "got error %d", ret);
3095 static GstRTSPResult
3096 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
3099 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
3102 static GstRTSPResult
3103 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
3105 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3106 GstRTSPClientPrivate *priv = client->priv;
3108 if (priv->close_seq && priv->close_seq == cseq) {
3109 priv->close_seq = 0;
3110 close_connection (client);
3116 static GstRTSPResult
3117 closed (GstRTSPWatch * watch, gpointer user_data)
3119 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3120 GstRTSPClientPrivate *priv = client->priv;
3121 const gchar *tunnelid;
3123 GST_INFO ("client %p: connection closed", client);
3125 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
3126 g_mutex_lock (&tunnels_lock);
3127 /* remove from tunnelids */
3128 g_hash_table_remove (tunnels, tunnelid);
3129 g_mutex_unlock (&tunnels_lock);
3132 gst_rtsp_watch_set_flushing (watch, TRUE);
3133 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
3138 static GstRTSPResult
3139 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
3141 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3144 str = gst_rtsp_strresult (result);
3145 GST_INFO ("client %p: received an error %s", client, str);
3151 static GstRTSPResult
3152 error_full (GstRTSPWatch * watch, GstRTSPResult result,
3153 GstRTSPMessage * message, guint id, gpointer user_data)
3155 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3158 str = gst_rtsp_strresult (result);
3160 ("client %p: error when handling message %p with id %d: %s",
3161 client, message, id, str);
3168 remember_tunnel (GstRTSPClient * client)
3170 GstRTSPClientPrivate *priv = client->priv;
3171 const gchar *tunnelid;
3173 /* store client in the pending tunnels */
3174 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
3175 if (tunnelid == NULL)
3178 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
3180 /* we can't have two clients connecting with the same tunnelid */
3181 g_mutex_lock (&tunnels_lock);
3182 if (g_hash_table_lookup (tunnels, tunnelid))
3183 goto tunnel_existed;
3185 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
3186 g_mutex_unlock (&tunnels_lock);
3193 GST_ERROR ("client %p: no tunnelid provided", client);
3198 g_mutex_unlock (&tunnels_lock);
3199 GST_ERROR ("client %p: tunnel session %s already existed", client,
3205 static GstRTSPResult
3206 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
3208 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3209 GstRTSPClientPrivate *priv = client->priv;
3211 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
3214 /* ignore error, it'll only be a problem when the client does a POST again */
3215 remember_tunnel (client);
3221 handle_tunnel (GstRTSPClient * client)
3223 GstRTSPClientPrivate *priv = client->priv;
3224 GstRTSPClient *oclient;
3225 GstRTSPClientPrivate *opriv;
3226 const gchar *tunnelid;
3228 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
3229 if (tunnelid == NULL)
3232 /* check for previous tunnel */
3233 g_mutex_lock (&tunnels_lock);
3234 oclient = g_hash_table_lookup (tunnels, tunnelid);
3236 if (oclient == NULL) {
3237 /* no previous tunnel, remember tunnel */
3238 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
3239 g_mutex_unlock (&tunnels_lock);
3241 GST_INFO ("client %p: no previous tunnel found, remembering tunnel (%p)",
3242 client, priv->connection);
3244 /* merge both tunnels into the first client */
3245 /* remove the old client from the table. ref before because removing it will
3246 * remove the ref to it. */
3247 g_object_ref (oclient);
3248 g_hash_table_remove (tunnels, tunnelid);
3249 g_mutex_unlock (&tunnels_lock);
3251 opriv = oclient->priv;
3253 if (opriv->watch == NULL)
3256 GST_INFO ("client %p: found previous tunnel %p (old %p, new %p)", client,
3257 oclient, opriv->connection, priv->connection);
3259 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
3260 gst_rtsp_watch_reset (priv->watch);
3261 gst_rtsp_watch_reset (opriv->watch);
3262 g_object_unref (oclient);
3264 /* the old client owns the tunnel now, the new one will be freed */
3265 g_source_destroy ((GSource *) priv->watch);
3267 g_main_context_unref (priv->watch_context);
3268 priv->watch_context = NULL;
3269 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
3277 GST_ERROR ("client %p: no tunnelid provided", client);
3282 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
3283 g_object_unref (oclient);
3288 static GstRTSPStatusCode
3289 tunnel_get (GstRTSPWatch * watch, gpointer user_data)
3291 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3293 GST_INFO ("client %p: tunnel get (connection %p)", client,
3294 client->priv->connection);
3296 if (!handle_tunnel (client)) {
3297 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
3300 return GST_RTSP_STS_OK;
3303 static GstRTSPResult
3304 tunnel_post (GstRTSPWatch * watch, gpointer user_data)
3306 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3308 GST_INFO ("client %p: tunnel post (connection %p)", client,
3309 client->priv->connection);
3311 if (!handle_tunnel (client)) {
3312 return GST_RTSP_ERROR;
3318 static GstRTSPResult
3319 tunnel_http_response (GstRTSPWatch * watch, GstRTSPMessage * request,
3320 GstRTSPMessage * response, gpointer user_data)
3322 GstRTSPClientClass *klass;
3324 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3325 klass = GST_RTSP_CLIENT_GET_CLASS (client);
3327 if (klass->tunnel_http_response) {
3328 klass->tunnel_http_response (client, request, response);
3334 static GstRTSPWatchFuncs watch_funcs = {
3343 tunnel_http_response
3347 client_watch_notify (GstRTSPClient * client)
3349 GstRTSPClientPrivate *priv = client->priv;
3351 GST_INFO ("client %p: watch destroyed", client);
3353 g_main_context_unref (priv->watch_context);
3354 priv->watch_context = NULL;
3355 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
3356 g_object_unref (client);
3360 * gst_rtsp_client_attach:
3361 * @client: a #GstRTSPClient
3362 * @context: (allow-none): a #GMainContext
3364 * Attaches @client to @context. When the mainloop for @context is run, the
3365 * client will be dispatched. When @context is %NULL, the default context will be
3368 * This function should be called when the client properties and urls are fully
3369 * configured and the client is ready to start.
3371 * Returns: the ID (greater than 0) for the source within the GMainContext.
3374 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
3376 GstRTSPClientPrivate *priv;
3379 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
3380 priv = client->priv;
3381 g_return_val_if_fail (priv->connection != NULL, 0);
3382 g_return_val_if_fail (priv->watch == NULL, 0);
3384 /* make sure noone will free the context before the watch is destroyed */
3385 priv->watch_context = g_main_context_ref (context);
3387 /* create watch for the connection and attach */
3388 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
3389 g_object_ref (client), (GDestroyNotify) client_watch_notify);
3390 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
3391 (GDestroyNotify) gst_rtsp_watch_unref);
3393 /* FIXME make this configurable. We don't want to do this yet because it will
3394 * be superceeded by a cache object later */
3395 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
3397 GST_INFO ("attaching to context %p", context);
3398 res = gst_rtsp_watch_attach (priv->watch, context);
3404 * gst_rtsp_client_session_filter:
3405 * @client: a #GstRTSPClient
3406 * @func: (scope call) (allow-none): a callback
3407 * @user_data: user data passed to @func
3409 * Call @func for each session managed by @client. The result value of @func
3410 * determines what happens to the session. @func will be called with @client
3411 * locked so no further actions on @client can be performed from @func.
3413 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
3416 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
3418 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
3419 * will also be added with an additional ref to the result #GList of this
3422 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each session.
3424 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
3425 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3426 * element in the #GList should be unreffed before the list is freed.
3429 gst_rtsp_client_session_filter (GstRTSPClient * client,
3430 GstRTSPClientSessionFilterFunc func, gpointer user_data)
3432 GstRTSPClientPrivate *priv;
3433 GList *result, *walk, *next;
3435 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
3437 priv = client->priv;
3441 g_mutex_lock (&priv->lock);
3442 for (walk = priv->sessions; walk; walk = next) {
3443 GstRTSPSession *sess = walk->data;
3444 GstRTSPFilterResult res;
3446 next = g_list_next (walk);
3449 res = func (client, sess, user_data);
3451 res = GST_RTSP_FILTER_REF;
3454 case GST_RTSP_FILTER_REMOVE:
3455 /* stop watching the session and pretent it went away */
3456 client_cleanup_session (client, sess);
3458 case GST_RTSP_FILTER_REF:
3459 result = g_list_prepend (result, g_object_ref (sess));
3461 case GST_RTSP_FILTER_KEEP:
3466 g_mutex_unlock (&priv->lock);