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 /* we already know about this session */
301 if (g_list_find (priv->sessions, session) != NULL)
304 GST_INFO ("watching session %p", session);
305 priv->sessions = g_list_prepend (priv->sessions, g_object_ref (session));
309 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session,
312 GstRTSPClientPrivate *priv = client->priv;
314 GST_INFO ("client %p: unwatch session %p", client, session);
317 link = g_list_find (priv->sessions, session);
322 /* unlink all media managed in this session */
323 gst_rtsp_session_filter (session, filter_session_media, client);
325 /* remove the session */
326 priv->sessions = g_list_delete_link (priv->sessions, link);
327 g_object_unref (session);
330 static GstRTSPFilterResult
331 cleanup_session (GstRTSPClient * client, GstRTSPSession * sess,
334 return GST_RTSP_FILTER_REMOVE;
337 /* A client is finalized when the connection is broken */
339 gst_rtsp_client_finalize (GObject * obj)
341 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
342 GstRTSPClientPrivate *priv = client->priv;
344 GST_INFO ("finalize client %p", client);
347 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
348 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
351 g_source_destroy ((GSource *) priv->watch);
353 if (priv->watch_context)
354 g_main_context_unref (priv->watch_context);
356 gst_rtsp_client_session_filter (client, cleanup_session, NULL);
358 if (priv->connection)
359 gst_rtsp_connection_free (priv->connection);
360 if (priv->session_pool) {
361 g_signal_handler_disconnect (priv->session_pool, priv->session_removed_id);
362 g_object_unref (priv->session_pool);
364 if (priv->mount_points)
365 g_object_unref (priv->mount_points);
367 g_object_unref (priv->auth);
368 if (priv->thread_pool)
369 g_object_unref (priv->thread_pool);
374 gst_rtsp_media_unprepare (priv->media);
375 g_object_unref (priv->media);
378 g_free (priv->server_ip);
379 g_mutex_clear (&priv->lock);
380 g_mutex_clear (&priv->send_lock);
382 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
386 gst_rtsp_client_get_property (GObject * object, guint propid,
387 GValue * value, GParamSpec * pspec)
389 GstRTSPClient *client = GST_RTSP_CLIENT (object);
390 GstRTSPClientPrivate *priv = client->priv;
393 case PROP_SESSION_POOL:
394 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
396 case PROP_MOUNT_POINTS:
397 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
399 case PROP_DROP_BACKLOG:
400 g_value_set_boolean (value, priv->drop_backlog);
403 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
408 gst_rtsp_client_set_property (GObject * object, guint propid,
409 const GValue * value, GParamSpec * pspec)
411 GstRTSPClient *client = GST_RTSP_CLIENT (object);
412 GstRTSPClientPrivate *priv = client->priv;
415 case PROP_SESSION_POOL:
416 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
418 case PROP_MOUNT_POINTS:
419 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
421 case PROP_DROP_BACKLOG:
422 g_mutex_lock (&priv->lock);
423 priv->drop_backlog = g_value_get_boolean (value);
424 g_mutex_unlock (&priv->lock);
427 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
432 * gst_rtsp_client_new:
434 * Create a new #GstRTSPClient instance.
436 * Returns: (transfer full): a new #GstRTSPClient
439 gst_rtsp_client_new (void)
441 GstRTSPClient *result;
443 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
449 send_message (GstRTSPClient * client, GstRTSPContext * ctx,
450 GstRTSPMessage * message, gboolean close)
452 GstRTSPClientPrivate *priv = client->priv;
454 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
455 "GStreamer RTSP server");
457 /* remove any previous header */
458 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
460 /* add the new session header for new session ids */
462 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
463 gst_rtsp_session_get_header (ctx->session));
466 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
467 gst_rtsp_message_dump (message);
471 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
473 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE],
476 g_mutex_lock (&priv->send_lock);
478 priv->send_func (client, message, close, priv->send_data);
479 g_mutex_unlock (&priv->send_lock);
481 gst_rtsp_message_unset (message);
485 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
486 GstRTSPContext * ctx)
488 gst_rtsp_message_init_response (ctx->response, code,
489 gst_rtsp_status_as_text (code), ctx->request);
493 send_message (client, ctx, ctx->response, FALSE);
497 send_option_not_supported_response (GstRTSPClient * client,
498 GstRTSPContext * ctx, const gchar * unsupported_options)
500 GstRTSPStatusCode code = GST_RTSP_STS_OPTION_NOT_SUPPORTED;
502 gst_rtsp_message_init_response (ctx->response, code,
503 gst_rtsp_status_as_text (code), ctx->request);
505 if (unsupported_options != NULL) {
506 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_UNSUPPORTED,
507 unsupported_options);
512 send_message (client, ctx, ctx->response, FALSE);
516 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
518 if (path1 == NULL || path2 == NULL)
521 if (strlen (path1) != len2)
524 if (strncmp (path1, path2, len2))
530 /* this function is called to initially find the media for the DESCRIBE request
531 * but is cached for when the same client (without breaking the connection) is
532 * doing a setup for the exact same url. */
533 static GstRTSPMedia *
534 find_media (GstRTSPClient * client, GstRTSPContext * ctx, gchar * path,
537 GstRTSPClientPrivate *priv = client->priv;
538 GstRTSPMediaFactory *factory;
542 /* find the longest matching factory for the uri first */
543 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
547 ctx->factory = factory;
549 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
550 goto no_factory_access;
552 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
558 path_len = strlen (path);
560 if (!paths_are_equal (priv->path, path, path_len)) {
561 GstRTSPThread *thread;
563 /* remove any previously cached values before we try to construct a new
569 gst_rtsp_media_unprepare (priv->media);
570 g_object_unref (priv->media);
574 /* prepare the media and add it to the pipeline */
575 if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
580 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
581 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
585 /* prepare the media */
586 if (!(gst_rtsp_media_prepare (media, thread)))
589 /* now keep track of the uri and the media */
590 priv->path = g_strndup (path, path_len);
593 /* we have seen this path before, used cached media */
596 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
599 g_object_unref (factory);
603 g_object_ref (media);
610 GST_ERROR ("client %p: no factory for path %s", client, path);
611 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
616 GST_ERROR ("client %p: not authorized to see factory path %s", client,
618 /* error reply is already sent */
623 GST_ERROR ("client %p: not authorized for factory path %s", client, path);
624 /* error reply is already sent */
629 GST_ERROR ("client %p: can't create media", client);
630 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
631 g_object_unref (factory);
637 GST_ERROR ("client %p: can't create thread", client);
638 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
639 g_object_unref (media);
641 g_object_unref (factory);
647 GST_ERROR ("client %p: can't prepare media", client);
648 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
649 g_object_unref (media);
651 g_object_unref (factory);
658 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
660 GstRTSPClientPrivate *priv = client->priv;
661 GstRTSPMessage message = { 0 };
666 gst_rtsp_message_init_data (&message, channel);
668 /* FIXME, need some sort of iovec RTSPMessage here */
669 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
672 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
674 g_mutex_lock (&priv->send_lock);
676 priv->send_func (client, &message, FALSE, priv->send_data);
677 g_mutex_unlock (&priv->send_lock);
679 gst_rtsp_message_steal_body (&message, &data, &usize);
680 gst_buffer_unmap (buffer, &map_info);
682 gst_rtsp_message_unset (&message);
688 link_transport (GstRTSPClient * client, GstRTSPSession * session,
689 GstRTSPStreamTransport * trans)
691 GstRTSPClientPrivate *priv = client->priv;
693 GST_DEBUG ("client %p: linking transport %p", client, trans);
695 gst_rtsp_stream_transport_set_callbacks (trans,
696 (GstRTSPSendFunc) do_send_data,
697 (GstRTSPSendFunc) do_send_data, client, NULL);
699 priv->transports = g_list_prepend (priv->transports, trans);
701 /* make sure our session can't expire */
702 gst_rtsp_session_prevent_expire (session);
706 link_session_transports (GstRTSPClient * client, GstRTSPSession * session,
707 GstRTSPSessionMedia * sessmedia)
712 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
713 for (i = 0; i < n_streams; i++) {
714 GstRTSPStreamTransport *trans;
715 const GstRTSPTransport *tr;
717 /* get the transport, if there is no transport configured, skip this stream */
718 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
722 tr = gst_rtsp_stream_transport_get_transport (trans);
724 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
725 /* for TCP, link the stream to the TCP connection of the client */
726 link_transport (client, session, trans);
732 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
733 GstRTSPStreamTransport * trans)
735 GstRTSPClientPrivate *priv = client->priv;
737 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
739 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
741 priv->transports = g_list_remove (priv->transports, trans);
743 /* our session can now expire */
744 gst_rtsp_session_allow_expire (session);
748 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
749 GstRTSPSessionMedia * sessmedia)
754 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
755 for (i = 0; i < n_streams; i++) {
756 GstRTSPStreamTransport *trans;
757 const GstRTSPTransport *tr;
759 /* get the transport, if there is no transport configured, skip this stream */
760 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
764 tr = gst_rtsp_stream_transport_get_transport (trans);
766 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
767 /* for TCP, unlink the stream from the TCP connection of the client */
768 unlink_transport (client, session, trans);
774 close_connection (GstRTSPClient * client)
776 GstRTSPClientPrivate *priv = client->priv;
777 const gchar *tunnelid;
779 GST_DEBUG ("client %p: closing connection", client);
781 if (priv->connection) {
782 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
783 g_mutex_lock (&tunnels_lock);
784 /* remove from tunnelids */
785 g_hash_table_remove (tunnels, tunnelid);
786 g_mutex_unlock (&tunnels_lock);
788 gst_rtsp_connection_close (priv->connection);
791 /* connection is now closed, destroy the watch which will also cause the
792 * closed signal to be emitted */
794 GST_DEBUG ("client %p: destroying watch", client);
795 g_source_destroy ((GSource *) priv->watch);
797 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
802 default_make_path_from_uri (GstRTSPClient * client, const GstRTSPUrl * uri)
807 path = g_strconcat (uri->abspath, "?", uri->query, NULL);
809 path = g_strdup (uri->abspath);
815 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
817 GstRTSPClientPrivate *priv = client->priv;
818 GstRTSPClientClass *klass;
819 GstRTSPSession *session;
820 GstRTSPSessionMedia *sessmedia;
821 GstRTSPStatusCode code;
824 gboolean keep_session;
829 session = ctx->session;
834 klass = GST_RTSP_CLIENT_GET_CLASS (client);
835 path = klass->make_path_from_uri (client, ctx->uri);
837 /* get a handle to the configuration of the media in the session */
838 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
842 /* only aggregate control for now.. */
843 if (path[matched] != '\0')
848 ctx->sessmedia = sessmedia;
850 /* we emit the signal before closing the connection */
851 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
854 /* make sure we unblock the backlog and don't accept new messages
856 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
858 /* unlink the all TCP callbacks */
859 unlink_session_transports (client, session, sessmedia);
861 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
863 /* allow messages again so that we can send the reply */
864 gst_rtsp_watch_set_flushing (priv->watch, FALSE);
866 /* unmanage the media in the session, returns false if all media session
868 keep_session = gst_rtsp_session_release_media (session, sessmedia);
870 /* construct the response now */
871 code = GST_RTSP_STS_OK;
872 gst_rtsp_message_init_response (ctx->response, code,
873 gst_rtsp_status_as_text (code), ctx->request);
875 send_message (client, ctx, ctx->response, TRUE);
878 /* remove the session */
879 gst_rtsp_session_pool_remove (priv->session_pool, session);
887 GST_ERROR ("client %p: no session", client);
888 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
893 GST_ERROR ("client %p: no uri supplied", client);
894 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
899 GST_ERROR ("client %p: no media for uri", client);
900 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
906 GST_ERROR ("client %p: no aggregate path %s", client, path);
907 send_generic_response (client,
908 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
915 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
919 res = gst_rtsp_params_set (client, ctx);
925 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
929 res = gst_rtsp_params_get (client, ctx);
935 handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
941 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
942 if (res != GST_RTSP_OK)
946 /* no body, keep-alive request */
947 send_generic_response (client, GST_RTSP_STS_OK, ctx);
949 /* there is a body, handle the params */
950 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
951 if (res != GST_RTSP_OK)
954 send_message (client, ctx, ctx->response, FALSE);
957 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
965 GST_ERROR ("client %p: bad request", client);
966 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
972 handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
978 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
979 if (res != GST_RTSP_OK)
983 /* no body, keep-alive request */
984 send_generic_response (client, GST_RTSP_STS_OK, ctx);
986 /* there is a body, handle the params */
987 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
988 if (res != GST_RTSP_OK)
991 send_message (client, ctx, ctx->response, FALSE);
994 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
1002 GST_ERROR ("client %p: bad request", client);
1003 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1009 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
1011 GstRTSPSession *session;
1012 GstRTSPClientClass *klass;
1013 GstRTSPSessionMedia *sessmedia;
1014 GstRTSPStatusCode code;
1015 GstRTSPState rtspstate;
1019 if (!(session = ctx->session))
1025 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1026 path = klass->make_path_from_uri (client, ctx->uri);
1028 /* get a handle to the configuration of the media in the session */
1029 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1033 if (path[matched] != '\0')
1038 ctx->sessmedia = sessmedia;
1040 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1041 /* the session state must be playing or recording */
1042 if (rtspstate != GST_RTSP_STATE_PLAYING &&
1043 rtspstate != GST_RTSP_STATE_RECORDING)
1046 /* unlink the all TCP callbacks */
1047 unlink_session_transports (client, session, sessmedia);
1049 /* then pause sending */
1050 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
1052 /* construct the response now */
1053 code = GST_RTSP_STS_OK;
1054 gst_rtsp_message_init_response (ctx->response, code,
1055 gst_rtsp_status_as_text (code), ctx->request);
1057 send_message (client, ctx, ctx->response, FALSE);
1059 /* the state is now READY */
1060 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1062 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
1069 GST_ERROR ("client %p: no seesion", client);
1070 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1075 GST_ERROR ("client %p: no uri supplied", client);
1076 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1081 GST_ERROR ("client %p: no media for uri", client);
1082 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1088 GST_ERROR ("client %p: no aggregate path %s", client, path);
1089 send_generic_response (client,
1090 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1096 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
1097 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1103 /* convert @url and @path to a URL used as a content base for the factory
1104 * located at @path */
1106 make_base_url (GstRTSPClient * client, GstRTSPUrl * url, const gchar * path)
1112 /* check for trailing '/' and append one */
1113 trail = (path[strlen (path) - 1] != '/' ? "/" : "");
1118 tmp.abspath = g_strdup_printf ("%s%s", path, trail);
1120 result = gst_rtsp_url_get_request_uri (&tmp);
1121 g_free (tmp.abspath);
1127 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
1129 GstRTSPSession *session;
1130 GstRTSPClientClass *klass;
1131 GstRTSPSessionMedia *sessmedia;
1132 GstRTSPMedia *media;
1133 GstRTSPStatusCode code;
1136 GstRTSPTimeRange *range;
1138 GstRTSPState rtspstate;
1139 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1140 gchar *path, *rtpinfo;
1143 if (!(session = ctx->session))
1146 if (!(uri = ctx->uri))
1149 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1150 path = klass->make_path_from_uri (client, uri);
1152 /* get a handle to the configuration of the media in the session */
1153 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1157 if (path[matched] != '\0')
1162 ctx->sessmedia = sessmedia;
1163 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1165 /* the session state must be playing or ready */
1166 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1167 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1170 /* in play we first unsuspend, media could be suspended from SDP or PAUSED */
1171 if (!gst_rtsp_media_unsuspend (media))
1172 goto unsuspend_failed;
1174 /* parse the range header if we have one */
1175 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1176 if (res == GST_RTSP_OK) {
1177 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1178 /* we have a range, seek to the position */
1180 gst_rtsp_media_seek (media, range);
1181 gst_rtsp_range_free (range);
1185 /* link the all TCP callbacks */
1186 link_session_transports (client, session, sessmedia);
1188 /* grab RTPInfo from the media now */
1189 rtpinfo = gst_rtsp_session_media_get_rtpinfo (sessmedia);
1191 /* construct the response now */
1192 code = GST_RTSP_STS_OK;
1193 gst_rtsp_message_init_response (ctx->response, code,
1194 gst_rtsp_status_as_text (code), ctx->request);
1196 /* add the RTP-Info header */
1198 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO,
1202 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1204 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1206 send_message (client, ctx, ctx->response, FALSE);
1208 /* start playing after sending the response */
1209 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1211 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1213 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1220 GST_ERROR ("client %p: no session", client);
1221 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1226 GST_ERROR ("client %p: no uri supplied", client);
1227 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1232 GST_ERROR ("client %p: media not found", client);
1233 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1238 GST_ERROR ("client %p: no aggregate path %s", client, path);
1239 send_generic_response (client,
1240 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1246 GST_ERROR ("client %p: not PLAYING or READY", client);
1247 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1253 GST_ERROR ("client %p: unsuspend failed", client);
1254 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1260 do_keepalive (GstRTSPSession * session)
1262 GST_INFO ("keep session %p alive", session);
1263 gst_rtsp_session_touch (session);
1266 /* parse @transport and return a valid transport in @tr. only transports
1267 * supported by @stream are returned. Returns FALSE if no valid transport
1270 parse_transport (const char *transport, GstRTSPStream * stream,
1271 GstRTSPTransport * tr)
1278 gst_rtsp_transport_init (tr);
1280 GST_DEBUG ("parsing transports %s", transport);
1282 transports = g_strsplit (transport, ",", 0);
1284 /* loop through the transports, try to parse */
1285 for (i = 0; transports[i]; i++) {
1286 res = gst_rtsp_transport_parse (transports[i], tr);
1287 if (res != GST_RTSP_OK) {
1288 /* no valid transport, search some more */
1289 GST_WARNING ("could not parse transport %s", transports[i]);
1293 /* we have a transport, see if it's supported */
1294 if (!gst_rtsp_stream_is_transport_supported (stream, tr)) {
1295 GST_WARNING ("unsupported transport %s", transports[i]);
1299 /* we have a valid transport */
1300 GST_INFO ("found valid transport %s", transports[i]);
1305 gst_rtsp_transport_init (tr);
1307 g_strfreev (transports);
1313 default_configure_client_media (GstRTSPClient * client, GstRTSPMedia * media,
1314 GstRTSPStream * stream, GstRTSPContext * ctx)
1316 GstRTSPMessage *request = ctx->request;
1317 gchar *blocksize_str;
1319 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1320 &blocksize_str, 0) == GST_RTSP_OK) {
1324 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1325 if (end == blocksize_str)
1328 /* we don't want to change the mtu when this media
1329 * can be shared because it impacts other clients */
1330 if (gst_rtsp_media_is_shared (media))
1333 if (blocksize > G_MAXUINT)
1334 blocksize = G_MAXUINT;
1336 gst_rtsp_stream_set_mtu (stream, blocksize);
1344 GST_ERROR_OBJECT (client, "failed to parse blocksize");
1345 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1351 default_configure_client_transport (GstRTSPClient * client,
1352 GstRTSPContext * ctx, GstRTSPTransport * ct)
1354 GstRTSPClientPrivate *priv = client->priv;
1356 /* we have a valid transport now, set the destination of the client. */
1357 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1358 gboolean use_client_settings;
1360 use_client_settings =
1361 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1363 if (ct->destination && use_client_settings) {
1364 GstRTSPAddress *addr;
1366 addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1367 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1372 gst_rtsp_address_free (addr);
1374 GstRTSPAddress *addr;
1375 GSocketFamily family;
1377 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1379 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1383 g_free (ct->destination);
1384 ct->destination = g_strdup (addr->address);
1385 ct->port.min = addr->port;
1386 ct->port.max = addr->port + addr->n_ports - 1;
1387 ct->ttl = addr->ttl;
1389 gst_rtsp_address_free (addr);
1394 url = gst_rtsp_connection_get_url (priv->connection);
1395 g_free (ct->destination);
1396 ct->destination = g_strdup (url->host);
1398 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1400 GSocketAddress *addr;
1402 sock = gst_rtsp_connection_get_read_socket (priv->connection);
1403 if ((addr = g_socket_get_remote_address (sock, NULL))) {
1404 /* our read port is the sender port of client */
1405 ct->client_port.min =
1406 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1407 g_object_unref (addr);
1409 if ((addr = g_socket_get_local_address (sock, NULL))) {
1410 ct->server_port.max =
1411 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1412 g_object_unref (addr);
1414 sock = gst_rtsp_connection_get_write_socket (priv->connection);
1415 if ((addr = g_socket_get_remote_address (sock, NULL))) {
1416 /* our write port is the receiver port of client */
1417 ct->client_port.max =
1418 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1419 g_object_unref (addr);
1421 if ((addr = g_socket_get_local_address (sock, NULL))) {
1422 ct->server_port.min =
1423 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1424 g_object_unref (addr);
1426 /* check if the client selected channels for TCP */
1427 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1428 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1438 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1443 static GstRTSPTransport *
1444 make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
1445 GstRTSPTransport * ct)
1447 GstRTSPTransport *st;
1449 GSocketFamily family;
1451 /* prepare the server transport */
1452 gst_rtsp_transport_new (&st);
1454 st->trans = ct->trans;
1455 st->profile = ct->profile;
1456 st->lower_transport = ct->lower_transport;
1458 addr = g_inet_address_new_from_string (ct->destination);
1461 GST_ERROR ("failed to get inet addr from client destination");
1462 family = G_SOCKET_FAMILY_IPV4;
1464 family = g_inet_address_get_family (addr);
1465 g_object_unref (addr);
1469 switch (st->lower_transport) {
1470 case GST_RTSP_LOWER_TRANS_UDP:
1471 st->client_port = ct->client_port;
1472 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
1474 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1475 st->port = ct->port;
1476 st->destination = g_strdup (ct->destination);
1479 case GST_RTSP_LOWER_TRANS_TCP:
1480 st->interleaved = ct->interleaved;
1481 st->client_port = ct->client_port;
1482 st->server_port = ct->server_port;
1487 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
1492 #define AES_128_KEY_LEN 16
1493 #define AES_256_KEY_LEN 32
1495 #define HMAC_32_KEY_LEN 4
1496 #define HMAC_80_KEY_LEN 10
1499 mikey_apply_policy (GstCaps * caps, GstMIKEYMessage * msg, guint8 policy)
1501 const gchar *srtp_cipher;
1502 const gchar *srtp_auth;
1503 const GstMIKEYPayload *sp;
1506 /* loop over Security policy until we find one containing policy */
1508 if ((sp = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, i)) == NULL)
1511 if (((GstMIKEYPayloadSP *) sp)->policy == policy)
1515 /* the default ciphers */
1516 srtp_cipher = "aes-128-icm";
1517 srtp_auth = "hmac-sha1-80";
1519 /* now override the defaults with what is in the Security Policy */
1523 /* collect all the params and go over them */
1524 len = gst_mikey_payload_sp_get_n_params (sp);
1525 for (i = 0; i < len; i++) {
1526 const GstMIKEYPayloadSPParam *param =
1527 gst_mikey_payload_sp_get_param (sp, i);
1529 switch (param->type) {
1530 case GST_MIKEY_SP_SRTP_ENC_ALG:
1531 switch (param->val[0]) {
1533 srtp_cipher = "null";
1537 srtp_cipher = "aes-128-icm";
1543 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
1544 switch (param->val[0]) {
1545 case AES_128_KEY_LEN:
1546 srtp_cipher = "aes-128-icm";
1548 case AES_256_KEY_LEN:
1549 srtp_cipher = "aes-256-icm";
1555 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1556 switch (param->val[0]) {
1562 srtp_auth = "hmac-sha1-80";
1568 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
1569 switch (param->val[0]) {
1570 case HMAC_32_KEY_LEN:
1571 srtp_auth = "hmac-sha1-32";
1573 case HMAC_80_KEY_LEN:
1574 srtp_auth = "hmac-sha1-80";
1580 case GST_MIKEY_SP_SRTP_SRTP_ENC:
1582 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
1589 /* now configure the SRTP parameters */
1590 gst_caps_set_simple (caps,
1591 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
1592 "srtp-auth", G_TYPE_STRING, srtp_auth,
1593 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
1594 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
1600 handle_mikey_data (GstRTSPClient * client, GstRTSPContext * ctx,
1601 guint8 * data, gsize size)
1603 GstMIKEYMessage *msg;
1605 GstCaps *caps = NULL;
1606 GstMIKEYPayloadKEMAC *kemac;
1607 const GstMIKEYPayloadKeyData *pkd;
1610 /* the MIKEY message contains a CSB or crypto session bundle. It is a
1611 * set of Crypto Sessions protected with the same master key.
1612 * In the context of SRTP, an RTP and its RTCP stream is part of a
1614 if ((msg = gst_mikey_message_new_from_data (data, size, NULL, NULL)) == NULL)
1617 /* we can only handle SRTP crypto sessions for now */
1618 if (msg->map_type != GST_MIKEY_MAP_TYPE_SRTP)
1619 goto invalid_map_type;
1621 /* get the number of crypto sessions. This maps SSRC to its
1622 * security parameters */
1623 n_cs = gst_mikey_message_get_n_cs (msg);
1625 goto no_crypto_sessions;
1627 /* we also need keys */
1628 if (!(kemac = (GstMIKEYPayloadKEMAC *) gst_mikey_message_find_payload
1629 (msg, GST_MIKEY_PT_KEMAC, 0)))
1632 /* we don't support encrypted keys */
1633 if (kemac->enc_alg != GST_MIKEY_ENC_NULL
1634 || kemac->mac_alg != GST_MIKEY_MAC_NULL)
1635 goto unsupported_encryption;
1637 /* get Key data sub-payload */
1638 pkd = (const GstMIKEYPayloadKeyData *)
1639 gst_mikey_payload_kemac_get_sub (&kemac->pt, 0);
1642 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
1645 /* go over all crypto sessions and create the security policy for each
1647 for (i = 0; i < n_cs; i++) {
1648 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
1650 caps = gst_caps_new_simple ("application/x-srtp",
1651 "ssrc", G_TYPE_UINT, map->ssrc,
1652 "roc", G_TYPE_UINT, map->roc, "srtp-key", GST_TYPE_BUFFER, key, NULL);
1653 mikey_apply_policy (caps, msg, map->policy);
1655 gst_rtsp_stream_update_crypto (ctx->stream, map->ssrc, caps);
1656 gst_caps_unref (caps);
1658 gst_mikey_message_free (msg);
1665 GST_DEBUG_OBJECT (client, "failed to parse MIKEY message");
1670 GST_DEBUG_OBJECT (client, "invalid map type %d", msg->map_type);
1671 goto cleanup_message;
1675 GST_DEBUG_OBJECT (client, "no crypto sessions");
1676 goto cleanup_message;
1680 GST_DEBUG_OBJECT (client, "no keys found");
1681 goto cleanup_message;
1683 unsupported_encryption:
1685 GST_DEBUG_OBJECT (client, "unsupported key encryption");
1686 goto cleanup_message;
1690 gst_mikey_message_free (msg);
1695 #define IS_STRIP_CHAR(c) (g_ascii_isspace ((guchar)(c)) || ((c) == '\"'))
1698 strip_chars (gchar * str)
1705 if (!IS_STRIP_CHAR (str[len]))
1709 for (s = str; *s && IS_STRIP_CHAR (*s); s++);
1710 memmove (str, s, len + 1);
1713 /* KeyMgmt = "KeyMgmt" ":" key-mgmt-spec 0*("," key-mgmt-spec)
1714 * key-mgmt-spec = "prot" "=" KMPID ";" ["uri" "=" %x22 URI %x22 ";"]
1717 handle_keymgmt (GstRTSPClient * client, GstRTSPContext * ctx, gchar * keymgmt)
1722 specs = g_strsplit (keymgmt, ",", 0);
1723 for (i = 0; specs[i]; i++) {
1726 split = g_strsplit (specs[i], ";", 0);
1727 for (j = 0; split[j]; j++) {
1728 g_strstrip (split[j]);
1729 if (g_str_has_prefix (split[j], "prot=")) {
1730 g_strstrip (split[j] + 5);
1731 if (!g_str_equal (split[j] + 5, "mikey"))
1733 GST_DEBUG ("found mikey");
1734 } else if (g_str_has_prefix (split[j], "uri=")) {
1735 strip_chars (split[j] + 4);
1736 GST_DEBUG ("found uri '%s'", split[j] + 4);
1737 } else if (g_str_has_prefix (split[j], "data=")) {
1740 strip_chars (split[j] + 5);
1741 GST_DEBUG ("found data '%s'", split[j] + 5);
1742 data = g_base64_decode_inplace (split[j] + 5, &size);
1743 handle_mikey_data (client, ctx, data, size);
1751 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
1753 GstRTSPClientPrivate *priv = client->priv;
1756 gchar *transport, *keymgmt;
1757 GstRTSPTransport *ct, *st;
1758 GstRTSPStatusCode code;
1759 GstRTSPSession *session;
1760 GstRTSPStreamTransport *trans;
1762 GstRTSPSessionMedia *sessmedia;
1763 GstRTSPMedia *media;
1764 GstRTSPStream *stream;
1765 GstRTSPState rtspstate;
1766 GstRTSPClientClass *klass;
1767 gchar *path, *control;
1774 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1775 path = klass->make_path_from_uri (client, uri);
1777 /* parse the transport */
1779 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
1781 if (res != GST_RTSP_OK)
1784 /* we create the session after parsing stuff so that we don't make
1785 * a session for malformed requests */
1786 if (priv->session_pool == NULL)
1789 session = ctx->session;
1792 g_object_ref (session);
1793 /* get a handle to the configuration of the media in the session, this can
1794 * return NULL if this is a new url to manage in this session. */
1795 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1797 /* we need a new media configuration in this session */
1801 /* we have no session media, find one and manage it */
1802 if (sessmedia == NULL) {
1803 /* get a handle to the configuration of the media in the session */
1804 media = find_media (client, ctx, path, &matched);
1806 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1807 g_object_ref (media);
1809 goto media_not_found;
1811 /* no media, not found then */
1813 goto media_not_found_no_reply;
1815 if (path[matched] == '\0')
1816 goto control_not_found;
1818 /* path is what matched. */
1819 path[matched] = '\0';
1820 /* control is remainder */
1821 control = &path[matched + 1];
1823 /* find the stream now using the control part */
1824 stream = gst_rtsp_media_find_stream (media, control);
1826 goto stream_not_found;
1828 /* now we have a uri identifying a valid media and stream */
1829 ctx->stream = stream;
1832 if (session == NULL) {
1833 /* create a session if this fails we probably reached our session limit or
1835 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1836 goto service_unavailable;
1838 /* make sure this client is closed when the session is closed */
1839 client_watch_session (client, session);
1841 /* signal new session */
1842 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1845 ctx->session = session;
1848 if (sessmedia == NULL) {
1849 /* manage the media in our session now, if not done already */
1850 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1851 /* if we stil have no media, error */
1852 if (sessmedia == NULL)
1853 goto sessmedia_unavailable;
1855 g_object_unref (media);
1858 ctx->sessmedia = sessmedia;
1860 if (!klass->configure_client_media (client, media, stream, ctx))
1861 goto configure_media_failed_no_reply;
1863 gst_rtsp_transport_new (&ct);
1865 /* parse and find a usable supported transport */
1866 if (!parse_transport (transport, stream, ct))
1867 goto unsupported_transports;
1869 /* update the client transport */
1870 if (!klass->configure_client_transport (client, ctx, ct))
1871 goto unsupported_client_transport;
1873 /* parse the keymgmt */
1874 if (gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_KEYMGMT,
1875 &keymgmt, 0) == GST_RTSP_OK) {
1876 if (!handle_keymgmt (client, ctx, keymgmt))
1880 /* set in the session media transport */
1881 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1883 /* configure the url used to set this transport, this we will use when
1884 * generating the response for the PLAY request */
1885 gst_rtsp_stream_transport_set_url (trans, uri);
1887 /* configure keepalive for this transport */
1888 gst_rtsp_stream_transport_set_keepalive (trans,
1889 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1891 /* create and serialize the server transport */
1892 st = make_server_transport (client, ctx, ct);
1893 trans_str = gst_rtsp_transport_as_text (st);
1894 gst_rtsp_transport_free (st);
1896 /* construct the response now */
1897 code = GST_RTSP_STS_OK;
1898 gst_rtsp_message_init_response (ctx->response, code,
1899 gst_rtsp_status_as_text (code), ctx->request);
1901 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
1905 send_message (client, ctx, ctx->response, FALSE);
1907 /* update the state */
1908 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1909 switch (rtspstate) {
1910 case GST_RTSP_STATE_PLAYING:
1911 case GST_RTSP_STATE_RECORDING:
1912 case GST_RTSP_STATE_READY:
1913 /* no state change */
1916 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1919 g_object_unref (session);
1922 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
1929 GST_ERROR ("client %p: no uri", client);
1930 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1935 GST_ERROR ("client %p: no transport", client);
1936 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1941 GST_ERROR ("client %p: no session pool configured", client);
1942 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1945 media_not_found_no_reply:
1947 GST_ERROR ("client %p: media '%s' not found", client, path);
1948 /* error reply is already sent */
1953 GST_ERROR ("client %p: media '%s' not found", client, path);
1954 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1959 GST_ERROR ("client %p: no control in path '%s'", client, path);
1960 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1961 g_object_unref (media);
1966 GST_ERROR ("client %p: stream '%s' not found", client, control);
1967 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1968 g_object_unref (media);
1971 service_unavailable:
1973 GST_ERROR ("client %p: can't create session", client);
1974 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1975 g_object_unref (media);
1978 sessmedia_unavailable:
1980 GST_ERROR ("client %p: can't create session media", client);
1981 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1982 g_object_unref (media);
1983 goto cleanup_session;
1985 configure_media_failed_no_reply:
1987 GST_ERROR ("client %p: configure_media failed", client);
1988 /* error reply is already sent */
1989 goto cleanup_session;
1991 unsupported_transports:
1993 GST_ERROR ("client %p: unsupported transports", client);
1994 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1995 goto cleanup_transport;
1997 unsupported_client_transport:
1999 GST_ERROR ("client %p: unsupported client transport", client);
2000 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
2001 goto cleanup_transport;
2005 GST_ERROR ("client %p: keymgmt error", client);
2006 send_generic_response (client, GST_RTSP_STS_KEY_MANAGEMENT_FAILURE, ctx);
2007 goto cleanup_transport;
2011 gst_rtsp_transport_free (ct);
2013 g_object_unref (session);
2020 static GstSDPMessage *
2021 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
2023 GstRTSPClientPrivate *priv = client->priv;
2028 gst_sdp_message_new (&sdp);
2030 /* some standard things first */
2031 gst_sdp_message_set_version (sdp, "0");
2038 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
2041 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
2042 gst_sdp_message_set_information (sdp, "rtsp-server");
2043 gst_sdp_message_add_time (sdp, "0", "0", NULL);
2044 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
2045 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
2046 gst_sdp_message_add_attribute (sdp, "control", "*");
2048 info.is_ipv6 = priv->is_ipv6;
2049 info.server_ip = priv->server_ip;
2051 /* create an SDP for the media object */
2052 if (!gst_rtsp_media_setup_sdp (media, sdp, &info))
2060 GST_ERROR ("client %p: could not create SDP", client);
2061 gst_sdp_message_free (sdp);
2066 /* for the describe we must generate an SDP */
2068 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
2070 GstRTSPClientPrivate *priv = client->priv;
2075 GstRTSPMedia *media;
2076 GstRTSPClientClass *klass;
2078 klass = GST_RTSP_CLIENT_GET_CLASS (client);
2083 /* check what kind of format is accepted, we don't really do anything with it
2084 * and always return SDP for now. */
2089 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
2091 if (res == GST_RTSP_ENOTIMPL)
2094 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
2098 if (!priv->mount_points)
2099 goto no_mount_points;
2101 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
2104 /* find the media object for the uri */
2105 if (!(media = find_media (client, ctx, path, NULL)))
2108 /* create an SDP for the media object on this client */
2109 if (!(sdp = klass->create_sdp (client, media)))
2112 /* we suspend after the describe */
2113 gst_rtsp_media_suspend (media);
2114 g_object_unref (media);
2116 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2117 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2119 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
2122 /* content base for some clients that might screw up creating the setup uri */
2123 str = make_base_url (client, ctx->uri, path);
2126 GST_INFO ("adding content-base: %s", str);
2127 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE, str);
2129 /* add SDP to the response body */
2130 str = gst_sdp_message_as_text (sdp);
2131 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
2132 gst_sdp_message_free (sdp);
2134 send_message (client, ctx, ctx->response, FALSE);
2136 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
2144 GST_ERROR ("client %p: no uri", client);
2145 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2150 GST_ERROR ("client %p: no mount points configured", client);
2151 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2156 GST_ERROR ("client %p: can't find path for url", client);
2157 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2162 GST_ERROR ("client %p: no media", client);
2164 /* error reply is already sent */
2169 GST_ERROR ("client %p: can't create SDP", client);
2170 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2172 g_object_unref (media);
2178 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
2180 GstRTSPMethod options;
2183 options = GST_RTSP_DESCRIBE |
2188 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
2190 str = gst_rtsp_options_as_text (options);
2192 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2193 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2195 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
2198 send_message (client, ctx, ctx->response, FALSE);
2200 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
2206 /* remove duplicate and trailing '/' */
2208 sanitize_uri (GstRTSPUrl * uri)
2212 gboolean have_slash, prev_slash;
2214 s = d = uri->abspath;
2215 len = strlen (uri->abspath);
2219 for (i = 0; i < len; i++) {
2220 have_slash = s[i] == '/';
2222 if (!have_slash || !prev_slash)
2224 prev_slash = have_slash;
2226 len = d - uri->abspath;
2227 /* don't remove the first slash if that's the only thing left */
2228 if (len > 1 && *(d - 1) == '/')
2233 /* is called when the session is removed from its session pool. */
2235 client_session_removed (GstRTSPSessionPool * pool, GstRTSPSession * session,
2236 GstRTSPClient * client)
2238 GST_INFO ("client %p: session %p removed", client, session);
2239 client_unwatch_session (client, session, NULL);
2242 /* Returns TRUE if there are no Require headers, otherwise returns FALSE
2243 * and also returns a newly-allocated string of (comma-separated) unsupported
2244 * options in the unsupported_reqs variable .
2246 * There may be multiple Require headers, but we must send one single
2247 * Unsupported header with all the unsupported options as response. If
2248 * an incoming Require header contained a comma-separated list of options
2249 * GstRtspConnection will already have split that list up into multiple
2252 * TODO: allow the application to decide what features are supported
2255 check_request_requirements (GstRTSPMessage * msg, gchar ** unsupported_reqs)
2258 GPtrArray *arr = NULL;
2264 res = gst_rtsp_message_get_header (msg, GST_RTSP_HDR_REQUIRE, &reqs, i++);
2266 if (res == GST_RTSP_ENOTIMPL)
2270 arr = g_ptr_array_new_with_free_func ((GDestroyNotify) g_free);
2272 g_ptr_array_add (arr, g_strdup (reqs));
2276 /* if we don't have any Require headers at all, all is fine */
2280 /* otherwise we've now processed at all the Require headers */
2281 g_ptr_array_add (arr, NULL);
2283 /* for now we don't commit to supporting anything, so will just report
2284 * all of the required options as unsupported */
2285 *unsupported_reqs = g_strjoinv (", ", (gchar **) arr->pdata);
2287 g_ptr_array_unref (arr);
2292 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
2294 GstRTSPClientPrivate *priv = client->priv;
2295 GstRTSPMethod method;
2296 const gchar *uristr;
2297 GstRTSPUrl *uri = NULL;
2298 GstRTSPVersion version;
2300 GstRTSPSession *session = NULL;
2301 GstRTSPContext sctx = { NULL }, *ctx;
2302 GstRTSPMessage response = { 0 };
2303 gchar *unsupported_reqs = NULL;
2306 if (!(ctx = gst_rtsp_context_get_current ())) {
2308 ctx->auth = priv->auth;
2309 gst_rtsp_context_push_current (ctx);
2312 ctx->conn = priv->connection;
2313 ctx->client = client;
2314 ctx->request = request;
2315 ctx->response = &response;
2317 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2318 gst_rtsp_message_dump (request);
2321 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
2323 GST_INFO ("client %p: received a request %s %s %s", client,
2324 gst_rtsp_method_as_text (method), uristr,
2325 gst_rtsp_version_as_text (version));
2327 /* we can only handle 1.0 requests */
2328 if (version != GST_RTSP_VERSION_1_0)
2331 ctx->method = method;
2333 /* we always try to parse the url first */
2334 if (strcmp (uristr, "*") == 0) {
2335 /* special case where we have * as uri, keep uri = NULL */
2336 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
2337 /* check if the uristr is an absolute path <=> scheme and host information
2341 scheme = g_uri_parse_scheme (uristr);
2342 if (scheme == NULL && g_str_has_prefix (uristr, "/")) {
2343 gchar *absolute_uristr = NULL;
2345 GST_WARNING_OBJECT (client, "request doesn't contain absolute url");
2346 if (priv->server_ip == NULL) {
2347 GST_WARNING_OBJECT (client, "host information missing");
2352 g_strdup_printf ("rtsp://%s%s", priv->server_ip, uristr);
2354 GST_DEBUG_OBJECT (client, "absolute url: %s", absolute_uristr);
2355 if (gst_rtsp_url_parse (absolute_uristr, &uri) != GST_RTSP_OK) {
2356 g_free (absolute_uristr);
2359 g_free (absolute_uristr);
2366 /* get the session if there is any */
2367 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
2368 if (res == GST_RTSP_OK) {
2369 if (priv->session_pool == NULL)
2372 /* we had a session in the request, find it again */
2373 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2374 goto session_not_found;
2376 /* we add the session to the client list of watched sessions. When a session
2377 * disappears because it times out, we will be notified. If all sessions are
2378 * gone, we will close the connection */
2379 client_watch_session (client, session);
2382 /* sanitize the uri */
2386 ctx->session = session;
2388 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
2389 goto not_authorized;
2391 /* handle any 'Require' headers */
2392 if (!check_request_requirements (ctx->request, &unsupported_reqs))
2393 goto unsupported_requirement;
2395 /* now see what is asked and dispatch to a dedicated handler */
2397 case GST_RTSP_OPTIONS:
2398 handle_options_request (client, ctx);
2400 case GST_RTSP_DESCRIBE:
2401 handle_describe_request (client, ctx);
2403 case GST_RTSP_SETUP:
2404 handle_setup_request (client, ctx);
2407 handle_play_request (client, ctx);
2409 case GST_RTSP_PAUSE:
2410 handle_pause_request (client, ctx);
2412 case GST_RTSP_TEARDOWN:
2413 handle_teardown_request (client, ctx);
2415 case GST_RTSP_SET_PARAMETER:
2416 handle_set_param_request (client, ctx);
2418 case GST_RTSP_GET_PARAMETER:
2419 handle_get_param_request (client, ctx);
2421 case GST_RTSP_ANNOUNCE:
2422 case GST_RTSP_RECORD:
2423 case GST_RTSP_REDIRECT:
2424 goto not_implemented;
2425 case GST_RTSP_INVALID:
2432 gst_rtsp_context_pop_current (ctx);
2434 g_object_unref (session);
2436 gst_rtsp_url_free (uri);
2442 GST_ERROR ("client %p: version %d not supported", client, version);
2443 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
2449 GST_ERROR ("client %p: bad request", client);
2450 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2455 GST_ERROR ("client %p: no pool configured", client);
2456 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2461 GST_ERROR ("client %p: session not found", client);
2462 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2467 GST_ERROR ("client %p: not allowed", client);
2468 /* error reply is already sent */
2471 unsupported_requirement:
2473 GST_ERROR ("client %p: Required option is not supported (%s)", client,
2475 send_option_not_supported_response (client, ctx, unsupported_reqs);
2476 g_free (unsupported_reqs);
2481 GST_ERROR ("client %p: method %d not implemented", client, method);
2482 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
2489 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
2491 GstRTSPClientPrivate *priv = client->priv;
2493 GstRTSPSession *session = NULL;
2494 GstRTSPContext sctx = { NULL }, *ctx;
2497 if (!(ctx = gst_rtsp_context_get_current ())) {
2499 ctx->auth = priv->auth;
2500 gst_rtsp_context_push_current (ctx);
2503 ctx->conn = priv->connection;
2504 ctx->client = client;
2505 ctx->request = NULL;
2507 ctx->method = GST_RTSP_INVALID;
2508 ctx->response = response;
2510 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2511 gst_rtsp_message_dump (response);
2514 GST_INFO ("client %p: received a response", client);
2516 /* get the session if there is any */
2518 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
2519 if (res == GST_RTSP_OK) {
2520 if (priv->session_pool == NULL)
2523 /* we had a session in the request, find it again */
2524 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2525 goto session_not_found;
2527 /* we add the session to the client list of watched sessions. When a session
2528 * disappears because it times out, we will be notified. If all sessions are
2529 * gone, we will close the connection */
2530 client_watch_session (client, session);
2533 ctx->session = session;
2535 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
2540 gst_rtsp_context_pop_current (ctx);
2542 g_object_unref (session);
2547 GST_ERROR ("client %p: no pool configured", client);
2552 GST_ERROR ("client %p: session not found", client);
2558 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2560 GstRTSPClientPrivate *priv = client->priv;
2569 /* find the stream for this message */
2570 res = gst_rtsp_message_parse_data (message, &channel);
2571 if (res != GST_RTSP_OK)
2574 gst_rtsp_message_steal_body (message, &data, &size);
2576 buffer = gst_buffer_new_wrapped (data, size);
2579 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2580 GstRTSPStreamTransport *trans;
2581 GstRTSPStream *stream;
2582 const GstRTSPTransport *tr;
2586 tr = gst_rtsp_stream_transport_get_transport (trans);
2587 stream = gst_rtsp_stream_transport_get_stream (trans);
2589 /* check for TCP transport */
2590 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2591 /* dispatch to the stream based on the channel number */
2592 if (tr->interleaved.min == channel) {
2593 gst_rtsp_stream_recv_rtp (stream, buffer);
2596 } else if (tr->interleaved.max == channel) {
2597 gst_rtsp_stream_recv_rtcp (stream, buffer);
2604 gst_buffer_unref (buffer);
2608 * gst_rtsp_client_set_session_pool:
2609 * @client: a #GstRTSPClient
2610 * @pool: (transfer none): a #GstRTSPSessionPool
2612 * Set @pool as the sessionpool for @client which it will use to find
2613 * or allocate sessions. the sessionpool is usually inherited from the server
2614 * that created the client but can be overridden later.
2617 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2618 GstRTSPSessionPool * pool)
2620 GstRTSPSessionPool *old;
2621 GstRTSPClientPrivate *priv;
2623 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2625 priv = client->priv;
2628 g_object_ref (pool);
2630 g_mutex_lock (&priv->lock);
2631 old = priv->session_pool;
2632 priv->session_pool = pool;
2634 if (priv->session_removed_id)
2635 g_signal_handler_disconnect (old, priv->session_removed_id);
2637 priv->session_removed_id = g_signal_connect (pool, "session-removed",
2638 G_CALLBACK (client_session_removed), client);
2640 priv->session_removed_id = 0;
2641 g_mutex_unlock (&priv->lock);
2643 /* FIXME, should remove all sessions from the old pool for this client */
2645 g_object_unref (old);
2649 * gst_rtsp_client_get_session_pool:
2650 * @client: a #GstRTSPClient
2652 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2654 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2656 GstRTSPSessionPool *
2657 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2659 GstRTSPClientPrivate *priv;
2660 GstRTSPSessionPool *result;
2662 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2664 priv = client->priv;
2666 g_mutex_lock (&priv->lock);
2667 if ((result = priv->session_pool))
2668 g_object_ref (result);
2669 g_mutex_unlock (&priv->lock);
2675 * gst_rtsp_client_set_mount_points:
2676 * @client: a #GstRTSPClient
2677 * @mounts: (transfer none): a #GstRTSPMountPoints
2679 * Set @mounts as the mount points for @client which it will use to map urls
2680 * to media streams. These mount points are usually inherited from the server that
2681 * created the client but can be overriden later.
2684 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2685 GstRTSPMountPoints * mounts)
2687 GstRTSPClientPrivate *priv;
2688 GstRTSPMountPoints *old;
2690 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2692 priv = client->priv;
2695 g_object_ref (mounts);
2697 g_mutex_lock (&priv->lock);
2698 old = priv->mount_points;
2699 priv->mount_points = mounts;
2700 g_mutex_unlock (&priv->lock);
2703 g_object_unref (old);
2707 * gst_rtsp_client_get_mount_points:
2708 * @client: a #GstRTSPClient
2710 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2712 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2714 GstRTSPMountPoints *
2715 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2717 GstRTSPClientPrivate *priv;
2718 GstRTSPMountPoints *result;
2720 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2722 priv = client->priv;
2724 g_mutex_lock (&priv->lock);
2725 if ((result = priv->mount_points))
2726 g_object_ref (result);
2727 g_mutex_unlock (&priv->lock);
2733 * gst_rtsp_client_set_auth:
2734 * @client: a #GstRTSPClient
2735 * @auth: (transfer none): a #GstRTSPAuth
2737 * configure @auth to be used as the authentication manager of @client.
2740 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2742 GstRTSPClientPrivate *priv;
2745 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2747 priv = client->priv;
2750 g_object_ref (auth);
2752 g_mutex_lock (&priv->lock);
2755 g_mutex_unlock (&priv->lock);
2758 g_object_unref (old);
2763 * gst_rtsp_client_get_auth:
2764 * @client: a #GstRTSPClient
2766 * Get the #GstRTSPAuth used as the authentication manager of @client.
2768 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2772 gst_rtsp_client_get_auth (GstRTSPClient * client)
2774 GstRTSPClientPrivate *priv;
2775 GstRTSPAuth *result;
2777 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2779 priv = client->priv;
2781 g_mutex_lock (&priv->lock);
2782 if ((result = priv->auth))
2783 g_object_ref (result);
2784 g_mutex_unlock (&priv->lock);
2790 * gst_rtsp_client_set_thread_pool:
2791 * @client: a #GstRTSPClient
2792 * @pool: (transfer none): a #GstRTSPThreadPool
2794 * configure @pool to be used as the thread pool of @client.
2797 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2798 GstRTSPThreadPool * pool)
2800 GstRTSPClientPrivate *priv;
2801 GstRTSPThreadPool *old;
2803 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2805 priv = client->priv;
2808 g_object_ref (pool);
2810 g_mutex_lock (&priv->lock);
2811 old = priv->thread_pool;
2812 priv->thread_pool = pool;
2813 g_mutex_unlock (&priv->lock);
2816 g_object_unref (old);
2820 * gst_rtsp_client_get_thread_pool:
2821 * @client: a #GstRTSPClient
2823 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2825 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2829 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2831 GstRTSPClientPrivate *priv;
2832 GstRTSPThreadPool *result;
2834 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2836 priv = client->priv;
2838 g_mutex_lock (&priv->lock);
2839 if ((result = priv->thread_pool))
2840 g_object_ref (result);
2841 g_mutex_unlock (&priv->lock);
2847 * gst_rtsp_client_set_connection:
2848 * @client: a #GstRTSPClient
2849 * @conn: (transfer full): a #GstRTSPConnection
2851 * Set the #GstRTSPConnection of @client. This function takes ownership of
2854 * Returns: %TRUE on success.
2857 gst_rtsp_client_set_connection (GstRTSPClient * client,
2858 GstRTSPConnection * conn)
2860 GstRTSPClientPrivate *priv;
2861 GSocket *read_socket;
2862 GSocketAddress *address;
2864 GError *error = NULL;
2866 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2867 g_return_val_if_fail (conn != NULL, FALSE);
2869 priv = client->priv;
2871 read_socket = gst_rtsp_connection_get_read_socket (conn);
2873 if (!(address = g_socket_get_local_address (read_socket, &error)))
2876 g_free (priv->server_ip);
2877 /* keep the original ip that the client connected to */
2878 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2879 GInetAddress *iaddr;
2881 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2883 /* socket might be ipv6 but adress still ipv4 */
2884 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2885 priv->server_ip = g_inet_address_to_string (iaddr);
2886 g_object_unref (address);
2888 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2889 priv->server_ip = g_strdup ("unknown");
2892 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2893 priv->server_ip, priv->is_ipv6);
2895 url = gst_rtsp_connection_get_url (conn);
2896 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2898 priv->connection = conn;
2905 GST_ERROR ("could not get local address %s", error->message);
2906 g_error_free (error);
2912 * gst_rtsp_client_get_connection:
2913 * @client: a #GstRTSPClient
2915 * Get the #GstRTSPConnection of @client.
2917 * Returns: (transfer none): the #GstRTSPConnection of @client.
2918 * The connection object returned remains valid until the client is freed.
2921 gst_rtsp_client_get_connection (GstRTSPClient * client)
2923 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2925 return client->priv->connection;
2929 * gst_rtsp_client_set_send_func:
2930 * @client: a #GstRTSPClient
2931 * @func: (scope notified): a #GstRTSPClientSendFunc
2932 * @user_data: (closure): user data passed to @func
2933 * @notify: (allow-none): called when @user_data is no longer in use
2935 * Set @func as the callback that will be called when a new message needs to be
2936 * sent to the client. @user_data is passed to @func and @notify is called when
2937 * @user_data is no longer in use.
2939 * By default, the client will send the messages on the #GstRTSPConnection that
2940 * was configured with gst_rtsp_client_attach() was called.
2943 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2944 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2946 GstRTSPClientPrivate *priv;
2947 GDestroyNotify old_notify;
2950 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2952 priv = client->priv;
2954 g_mutex_lock (&priv->send_lock);
2955 priv->send_func = func;
2956 old_notify = priv->send_notify;
2957 old_data = priv->send_data;
2958 priv->send_notify = notify;
2959 priv->send_data = user_data;
2960 g_mutex_unlock (&priv->send_lock);
2963 old_notify (old_data);
2967 * gst_rtsp_client_handle_message:
2968 * @client: a #GstRTSPClient
2969 * @message: (transfer none): an #GstRTSPMessage
2971 * Let the client handle @message.
2973 * Returns: a #GstRTSPResult.
2976 gst_rtsp_client_handle_message (GstRTSPClient * client,
2977 GstRTSPMessage * message)
2979 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2980 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2982 switch (message->type) {
2983 case GST_RTSP_MESSAGE_REQUEST:
2984 handle_request (client, message);
2986 case GST_RTSP_MESSAGE_RESPONSE:
2987 handle_response (client, message);
2989 case GST_RTSP_MESSAGE_DATA:
2990 handle_data (client, message);
2999 * gst_rtsp_client_send_message:
3000 * @client: a #GstRTSPClient
3001 * @session: (allow-none) (transfer none): a #GstRTSPSession to send
3002 * the message to or %NULL
3003 * @message: (transfer none): The #GstRTSPMessage to send
3005 * Send a message message to the remote end. @message must be a
3006 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
3009 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
3010 GstRTSPMessage * message)
3012 GstRTSPContext sctx = { NULL }
3014 GstRTSPClientPrivate *priv;
3016 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
3017 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3018 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
3019 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
3021 priv = client->priv;
3023 if (!(ctx = gst_rtsp_context_get_current ())) {
3025 ctx->auth = priv->auth;
3026 gst_rtsp_context_push_current (ctx);
3029 ctx->conn = priv->connection;
3030 ctx->client = client;
3031 ctx->session = session;
3033 send_message (client, ctx, message, FALSE);
3036 gst_rtsp_context_pop_current (ctx);
3041 static GstRTSPResult
3042 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
3043 gboolean close, gpointer user_data)
3045 GstRTSPClientPrivate *priv = client->priv;
3053 /* send the response and store the seq number so we can wait until it's
3054 * written to the client to close the connection */
3056 gst_rtsp_watch_send_message (priv->watch, message,
3057 close ? &priv->close_seq : NULL);
3058 if (ret == GST_RTSP_OK)
3061 if (ret != GST_RTSP_ENOMEM)
3065 if (priv->drop_backlog)
3068 /* queue was full, wait for more space */
3069 GST_DEBUG_OBJECT (client, "waiting for backlog");
3070 ret = gst_rtsp_watch_wait_backlog (priv->watch, &time);
3071 GST_DEBUG_OBJECT (client, "Resend due to backlog full");
3072 } while (ret != GST_RTSP_EINTR);
3079 GST_DEBUG_OBJECT (client, "got error %d", ret);
3084 static GstRTSPResult
3085 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
3088 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
3091 static GstRTSPResult
3092 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
3094 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3095 GstRTSPClientPrivate *priv = client->priv;
3097 if (priv->close_seq && priv->close_seq == cseq) {
3098 GST_INFO ("client %p: send close message", client);
3099 priv->close_seq = 0;
3100 close_connection (client);
3106 static GstRTSPResult
3107 closed (GstRTSPWatch * watch, gpointer user_data)
3109 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3110 GstRTSPClientPrivate *priv = client->priv;
3111 const gchar *tunnelid;
3113 GST_INFO ("client %p: connection closed", client);
3115 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
3116 g_mutex_lock (&tunnels_lock);
3117 /* remove from tunnelids */
3118 g_hash_table_remove (tunnels, tunnelid);
3119 g_mutex_unlock (&tunnels_lock);
3122 gst_rtsp_watch_set_flushing (watch, TRUE);
3123 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
3128 static GstRTSPResult
3129 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
3131 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3134 str = gst_rtsp_strresult (result);
3135 GST_INFO ("client %p: received an error %s", client, str);
3141 static GstRTSPResult
3142 error_full (GstRTSPWatch * watch, GstRTSPResult result,
3143 GstRTSPMessage * message, guint id, gpointer user_data)
3145 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3148 str = gst_rtsp_strresult (result);
3150 ("client %p: error when handling message %p with id %d: %s",
3151 client, message, id, str);
3158 remember_tunnel (GstRTSPClient * client)
3160 GstRTSPClientPrivate *priv = client->priv;
3161 const gchar *tunnelid;
3163 /* store client in the pending tunnels */
3164 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
3165 if (tunnelid == NULL)
3168 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
3170 /* we can't have two clients connecting with the same tunnelid */
3171 g_mutex_lock (&tunnels_lock);
3172 if (g_hash_table_lookup (tunnels, tunnelid))
3173 goto tunnel_existed;
3175 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
3176 g_mutex_unlock (&tunnels_lock);
3183 GST_ERROR ("client %p: no tunnelid provided", client);
3188 g_mutex_unlock (&tunnels_lock);
3189 GST_ERROR ("client %p: tunnel session %s already existed", client,
3195 static GstRTSPResult
3196 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
3198 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3199 GstRTSPClientPrivate *priv = client->priv;
3201 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
3204 /* ignore error, it'll only be a problem when the client does a POST again */
3205 remember_tunnel (client);
3211 handle_tunnel (GstRTSPClient * client)
3213 GstRTSPClientPrivate *priv = client->priv;
3214 GstRTSPClient *oclient;
3215 GstRTSPClientPrivate *opriv;
3216 const gchar *tunnelid;
3218 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
3219 if (tunnelid == NULL)
3222 /* check for previous tunnel */
3223 g_mutex_lock (&tunnels_lock);
3224 oclient = g_hash_table_lookup (tunnels, tunnelid);
3226 if (oclient == NULL) {
3227 /* no previous tunnel, remember tunnel */
3228 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
3229 g_mutex_unlock (&tunnels_lock);
3231 GST_INFO ("client %p: no previous tunnel found, remembering tunnel (%p)",
3232 client, priv->connection);
3234 /* merge both tunnels into the first client */
3235 /* remove the old client from the table. ref before because removing it will
3236 * remove the ref to it. */
3237 g_object_ref (oclient);
3238 g_hash_table_remove (tunnels, tunnelid);
3239 g_mutex_unlock (&tunnels_lock);
3241 opriv = oclient->priv;
3243 if (opriv->watch == NULL)
3246 GST_INFO ("client %p: found previous tunnel %p (old %p, new %p)", client,
3247 oclient, opriv->connection, priv->connection);
3249 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
3250 gst_rtsp_watch_reset (priv->watch);
3251 gst_rtsp_watch_reset (opriv->watch);
3252 g_object_unref (oclient);
3254 /* the old client owns the tunnel now, the new one will be freed */
3255 g_source_destroy ((GSource *) priv->watch);
3257 g_main_context_unref (priv->watch_context);
3258 priv->watch_context = NULL;
3259 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
3267 GST_ERROR ("client %p: no tunnelid provided", client);
3272 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
3273 g_object_unref (oclient);
3278 static GstRTSPStatusCode
3279 tunnel_get (GstRTSPWatch * watch, gpointer user_data)
3281 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3283 GST_INFO ("client %p: tunnel get (connection %p)", client,
3284 client->priv->connection);
3286 if (!handle_tunnel (client)) {
3287 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
3290 return GST_RTSP_STS_OK;
3293 static GstRTSPResult
3294 tunnel_post (GstRTSPWatch * watch, gpointer user_data)
3296 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3298 GST_INFO ("client %p: tunnel post (connection %p)", client,
3299 client->priv->connection);
3301 if (!handle_tunnel (client)) {
3302 return GST_RTSP_ERROR;
3308 static GstRTSPResult
3309 tunnel_http_response (GstRTSPWatch * watch, GstRTSPMessage * request,
3310 GstRTSPMessage * response, gpointer user_data)
3312 GstRTSPClientClass *klass;
3314 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3315 klass = GST_RTSP_CLIENT_GET_CLASS (client);
3317 if (klass->tunnel_http_response) {
3318 klass->tunnel_http_response (client, request, response);
3324 static GstRTSPWatchFuncs watch_funcs = {
3333 tunnel_http_response
3337 client_watch_notify (GstRTSPClient * client)
3339 GstRTSPClientPrivate *priv = client->priv;
3341 GST_INFO ("client %p: watch destroyed", client);
3343 g_main_context_unref (priv->watch_context);
3344 priv->watch_context = NULL;
3345 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
3346 g_object_unref (client);
3350 * gst_rtsp_client_attach:
3351 * @client: a #GstRTSPClient
3352 * @context: (allow-none): a #GMainContext
3354 * Attaches @client to @context. When the mainloop for @context is run, the
3355 * client will be dispatched. When @context is %NULL, the default context will be
3358 * This function should be called when the client properties and urls are fully
3359 * configured and the client is ready to start.
3361 * Returns: the ID (greater than 0) for the source within the GMainContext.
3364 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
3366 GstRTSPClientPrivate *priv;
3369 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
3370 priv = client->priv;
3371 g_return_val_if_fail (priv->connection != NULL, 0);
3372 g_return_val_if_fail (priv->watch == NULL, 0);
3374 /* make sure noone will free the context before the watch is destroyed */
3375 priv->watch_context = g_main_context_ref (context);
3377 /* create watch for the connection and attach */
3378 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
3379 g_object_ref (client), (GDestroyNotify) client_watch_notify);
3380 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
3381 (GDestroyNotify) gst_rtsp_watch_unref);
3383 /* FIXME make this configurable. We don't want to do this yet because it will
3384 * be superceeded by a cache object later */
3385 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
3387 GST_INFO ("attaching to context %p", context);
3388 res = gst_rtsp_watch_attach (priv->watch, context);
3394 * gst_rtsp_client_session_filter:
3395 * @client: a #GstRTSPClient
3396 * @func: (scope call) (allow-none): a callback
3397 * @user_data: user data passed to @func
3399 * Call @func for each session managed by @client. The result value of @func
3400 * determines what happens to the session. @func will be called with @client
3401 * locked so no further actions on @client can be performed from @func.
3403 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
3406 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
3408 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
3409 * will also be added with an additional ref to the result #GList of this
3412 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each session.
3414 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
3415 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3416 * element in the #GList should be unreffed before the list is freed.
3419 gst_rtsp_client_session_filter (GstRTSPClient * client,
3420 GstRTSPClientSessionFilterFunc func, gpointer user_data)
3422 GstRTSPClientPrivate *priv;
3423 GList *result, *walk, *next;
3425 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
3427 priv = client->priv;
3431 g_mutex_lock (&priv->lock);
3432 for (walk = priv->sessions; walk; walk = next) {
3433 GstRTSPSession *sess = walk->data;
3434 GstRTSPFilterResult res;
3436 next = g_list_next (walk);
3439 res = func (client, sess, user_data);
3441 res = GST_RTSP_FILTER_REF;
3444 case GST_RTSP_FILTER_REMOVE:
3445 /* stop watching the session and pretent it went away */
3446 client_unwatch_session (client, sess, walk);
3448 case GST_RTSP_FILTER_REF:
3449 result = g_list_prepend (result, g_object_ref (sess));
3451 case GST_RTSP_FILTER_KEEP:
3456 g_mutex_unlock (&priv->lock);