2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * @short_description: A client connection state
22 * @see_also: #GstRTSPServer, #GstRTSPThreadPool
24 * The client object handles the connection with a client for as long as a TCP
27 * A #GstRTSPClient is created by #GstRTSPServer when a new connection is
28 * accepted and it inherits the #GstRTSPMountPoints, #GstRTSPSessionPool,
29 * #GstRTSPAuth and #GstRTSPThreadPool from the server.
31 * The client connection should be configured with the #GstRTSPConnection using
32 * gst_rtsp_client_set_connection() before it can be attached to a #GMainContext
33 * using gst_rtsp_client_attach(). From then on the client will handle requests
36 * Use gst_rtsp_client_session_filter() to iterate or modify all the
37 * #GstRTSPSession objects managed by the client object.
39 * Last reviewed on 2013-07-11 (1.0.0)
45 #include <gst/sdp/gstmikey.h>
47 #include "rtsp-client.h"
49 #include "rtsp-params.h"
51 #define GST_RTSP_CLIENT_GET_PRIVATE(obj) \
52 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_CLIENT, GstRTSPClientPrivate))
55 * send_lock, lock, tunnels_lock
58 struct _GstRTSPClientPrivate
60 GMutex lock; /* protects everything else */
62 GstRTSPConnection *connection;
64 GMainContext *watch_context;
69 GstRTSPClientSendFunc send_func; /* protected by send_lock */
70 gpointer send_data; /* protected by send_lock */
71 GDestroyNotify send_notify; /* protected by send_lock */
73 GstRTSPSessionPool *session_pool;
74 gulong session_removed_id;
75 GstRTSPMountPoints *mount_points;
77 GstRTSPThreadPool *thread_pool;
79 /* used to cache the media in the last requested DESCRIBE so that
80 * we can pick it up in the next SETUP immediately */
87 gboolean drop_backlog;
90 static GMutex tunnels_lock;
91 static GHashTable *tunnels; /* protected by tunnels_lock */
93 #define DEFAULT_SESSION_POOL NULL
94 #define DEFAULT_MOUNT_POINTS NULL
95 #define DEFAULT_DROP_BACKLOG TRUE
110 SIGNAL_OPTIONS_REQUEST,
111 SIGNAL_DESCRIBE_REQUEST,
112 SIGNAL_SETUP_REQUEST,
114 SIGNAL_PAUSE_REQUEST,
115 SIGNAL_TEARDOWN_REQUEST,
116 SIGNAL_SET_PARAMETER_REQUEST,
117 SIGNAL_GET_PARAMETER_REQUEST,
118 SIGNAL_HANDLE_RESPONSE,
123 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
124 #define GST_CAT_DEFAULT rtsp_client_debug
126 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
128 static void gst_rtsp_client_get_property (GObject * object, guint propid,
129 GValue * value, GParamSpec * pspec);
130 static void gst_rtsp_client_set_property (GObject * object, guint propid,
131 const GValue * value, GParamSpec * pspec);
132 static void gst_rtsp_client_finalize (GObject * obj);
134 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
135 static void unlink_session_transports (GstRTSPClient * client,
136 GstRTSPSession * session, GstRTSPSessionMedia * sessmedia);
137 static gboolean default_configure_client_media (GstRTSPClient * client,
138 GstRTSPMedia * media, GstRTSPStream * stream, GstRTSPContext * ctx);
139 static gboolean default_configure_client_transport (GstRTSPClient * client,
140 GstRTSPContext * ctx, GstRTSPTransport * ct);
141 static GstRTSPResult default_params_set (GstRTSPClient * client,
142 GstRTSPContext * ctx);
143 static GstRTSPResult default_params_get (GstRTSPClient * client,
144 GstRTSPContext * ctx);
145 static gchar *default_make_path_from_uri (GstRTSPClient * client,
146 const GstRTSPUrl * uri);
148 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
151 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
153 GObjectClass *gobject_class;
155 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
157 gobject_class = G_OBJECT_CLASS (klass);
159 gobject_class->get_property = gst_rtsp_client_get_property;
160 gobject_class->set_property = gst_rtsp_client_set_property;
161 gobject_class->finalize = gst_rtsp_client_finalize;
163 klass->create_sdp = create_sdp;
164 klass->configure_client_media = default_configure_client_media;
165 klass->configure_client_transport = default_configure_client_transport;
166 klass->params_set = default_params_set;
167 klass->params_get = default_params_get;
168 klass->make_path_from_uri = default_make_path_from_uri;
170 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
171 g_param_spec_object ("session-pool", "Session Pool",
172 "The session pool to use for client session",
173 GST_TYPE_RTSP_SESSION_POOL,
174 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
176 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
177 g_param_spec_object ("mount-points", "Mount Points",
178 "The mount points to use for client session",
179 GST_TYPE_RTSP_MOUNT_POINTS,
180 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
182 g_object_class_install_property (gobject_class, PROP_DROP_BACKLOG,
183 g_param_spec_boolean ("drop-backlog", "Drop Backlog",
184 "Drop data when the backlog queue is full",
185 DEFAULT_DROP_BACKLOG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
187 gst_rtsp_client_signals[SIGNAL_CLOSED] =
188 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
189 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
190 g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
192 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
193 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
194 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
195 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
197 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
198 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
199 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
200 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
201 GST_TYPE_RTSP_CONTEXT);
203 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
204 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
205 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
206 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
207 GST_TYPE_RTSP_CONTEXT);
209 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
210 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
211 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
212 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
213 GST_TYPE_RTSP_CONTEXT);
215 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
216 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
217 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
218 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
219 GST_TYPE_RTSP_CONTEXT);
221 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
222 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
223 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
224 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
225 GST_TYPE_RTSP_CONTEXT);
227 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
228 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
229 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
230 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
231 GST_TYPE_RTSP_CONTEXT);
233 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
234 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
235 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
236 set_parameter_request), NULL, NULL, g_cclosure_marshal_generic,
237 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
239 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
240 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
241 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
242 get_parameter_request), NULL, NULL, g_cclosure_marshal_generic,
243 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
245 gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE] =
246 g_signal_new ("handle-response", G_TYPE_FROM_CLASS (klass),
247 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
248 handle_response), NULL, NULL, g_cclosure_marshal_generic,
249 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
252 * GstRTSPClient::send-message:
253 * @client: The RTSP client
254 * @session: (type GstRtspServer.RTSPSession): The session
255 * @message: (type GstRtsp.RTSPMessage): The message
257 gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE] =
258 g_signal_new ("send-message", G_TYPE_FROM_CLASS (klass),
259 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
260 G_TYPE_NONE, 2, GST_TYPE_RTSP_CONTEXT, G_TYPE_POINTER);
263 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
264 g_mutex_init (&tunnels_lock);
266 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
270 gst_rtsp_client_init (GstRTSPClient * client)
272 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
276 g_mutex_init (&priv->lock);
277 g_mutex_init (&priv->send_lock);
279 priv->drop_backlog = DEFAULT_DROP_BACKLOG;
282 static GstRTSPFilterResult
283 filter_session_media (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
286 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
288 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
289 unlink_session_transports (client, sess, sessmedia);
291 /* unmanage the media in the session */
292 return GST_RTSP_FILTER_REMOVE;
296 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
298 GstRTSPClientPrivate *priv = client->priv;
300 g_mutex_lock (&priv->lock);
301 /* check if we already know about this session */
302 if (g_list_find (priv->sessions, session) == NULL) {
303 GST_INFO ("watching session %p", session);
304 priv->sessions = g_list_prepend (priv->sessions, g_object_ref (session));
306 g_mutex_unlock (&priv->lock);
311 /* should be called with lock */
313 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session,
316 GstRTSPClientPrivate *priv = client->priv;
318 GST_INFO ("client %p: unwatch session %p", client, session);
321 link = g_list_find (priv->sessions, session);
325 priv->sessions = g_list_delete_link (priv->sessions, link);
327 /* unlink all media managed in this session */
328 gst_rtsp_session_filter (session, filter_session_media, client);
330 /* remove the session */
331 g_object_unref (session);
334 static GstRTSPFilterResult
335 cleanup_session (GstRTSPClient * client, GstRTSPSession * sess,
338 return GST_RTSP_FILTER_REMOVE;
341 /* A client is finalized when the connection is broken */
343 gst_rtsp_client_finalize (GObject * obj)
345 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
346 GstRTSPClientPrivate *priv = client->priv;
348 GST_INFO ("finalize client %p", client);
351 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
352 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
355 g_source_destroy ((GSource *) priv->watch);
357 if (priv->watch_context)
358 g_main_context_unref (priv->watch_context);
360 gst_rtsp_client_session_filter (client, cleanup_session, NULL);
362 if (priv->connection)
363 gst_rtsp_connection_free (priv->connection);
364 if (priv->session_pool) {
365 g_signal_handler_disconnect (priv->session_pool, priv->session_removed_id);
366 g_object_unref (priv->session_pool);
368 if (priv->mount_points)
369 g_object_unref (priv->mount_points);
371 g_object_unref (priv->auth);
372 if (priv->thread_pool)
373 g_object_unref (priv->thread_pool);
378 gst_rtsp_media_unprepare (priv->media);
379 g_object_unref (priv->media);
382 g_free (priv->server_ip);
383 g_mutex_clear (&priv->lock);
384 g_mutex_clear (&priv->send_lock);
386 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
390 gst_rtsp_client_get_property (GObject * object, guint propid,
391 GValue * value, GParamSpec * pspec)
393 GstRTSPClient *client = GST_RTSP_CLIENT (object);
394 GstRTSPClientPrivate *priv = client->priv;
397 case PROP_SESSION_POOL:
398 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
400 case PROP_MOUNT_POINTS:
401 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
403 case PROP_DROP_BACKLOG:
404 g_value_set_boolean (value, priv->drop_backlog);
407 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
412 gst_rtsp_client_set_property (GObject * object, guint propid,
413 const GValue * value, GParamSpec * pspec)
415 GstRTSPClient *client = GST_RTSP_CLIENT (object);
416 GstRTSPClientPrivate *priv = client->priv;
419 case PROP_SESSION_POOL:
420 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
422 case PROP_MOUNT_POINTS:
423 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
425 case PROP_DROP_BACKLOG:
426 g_mutex_lock (&priv->lock);
427 priv->drop_backlog = g_value_get_boolean (value);
428 g_mutex_unlock (&priv->lock);
431 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
436 * gst_rtsp_client_new:
438 * Create a new #GstRTSPClient instance.
440 * Returns: (transfer full): a new #GstRTSPClient
443 gst_rtsp_client_new (void)
445 GstRTSPClient *result;
447 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
453 send_message (GstRTSPClient * client, GstRTSPContext * ctx,
454 GstRTSPMessage * message, gboolean close)
456 GstRTSPClientPrivate *priv = client->priv;
458 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
459 "GStreamer RTSP server");
461 /* remove any previous header */
462 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
464 /* add the new session header for new session ids */
466 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
467 gst_rtsp_session_get_header (ctx->session));
470 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
471 gst_rtsp_message_dump (message);
475 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
477 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE],
480 g_mutex_lock (&priv->send_lock);
482 priv->send_func (client, message, close, priv->send_data);
483 g_mutex_unlock (&priv->send_lock);
485 gst_rtsp_message_unset (message);
489 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
490 GstRTSPContext * ctx)
492 gst_rtsp_message_init_response (ctx->response, code,
493 gst_rtsp_status_as_text (code), ctx->request);
497 send_message (client, ctx, ctx->response, FALSE);
501 send_option_not_supported_response (GstRTSPClient * client,
502 GstRTSPContext * ctx, const gchar * unsupported_options)
504 GstRTSPStatusCode code = GST_RTSP_STS_OPTION_NOT_SUPPORTED;
506 gst_rtsp_message_init_response (ctx->response, code,
507 gst_rtsp_status_as_text (code), ctx->request);
509 if (unsupported_options != NULL) {
510 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_UNSUPPORTED,
511 unsupported_options);
516 send_message (client, ctx, ctx->response, FALSE);
520 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
522 if (path1 == NULL || path2 == NULL)
525 if (strlen (path1) != len2)
528 if (strncmp (path1, path2, len2))
534 /* this function is called to initially find the media for the DESCRIBE request
535 * but is cached for when the same client (without breaking the connection) is
536 * doing a setup for the exact same url. */
537 static GstRTSPMedia *
538 find_media (GstRTSPClient * client, GstRTSPContext * ctx, gchar * path,
541 GstRTSPClientPrivate *priv = client->priv;
542 GstRTSPMediaFactory *factory;
546 /* find the longest matching factory for the uri first */
547 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
551 ctx->factory = factory;
553 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
554 goto no_factory_access;
556 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
562 path_len = strlen (path);
564 if (!paths_are_equal (priv->path, path, path_len)) {
565 GstRTSPThread *thread;
567 /* remove any previously cached values before we try to construct a new
573 gst_rtsp_media_unprepare (priv->media);
574 g_object_unref (priv->media);
578 /* prepare the media and add it to the pipeline */
579 if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
584 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
585 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
589 /* prepare the media */
590 if (!(gst_rtsp_media_prepare (media, thread)))
593 /* now keep track of the uri and the media */
594 priv->path = g_strndup (path, path_len);
597 /* we have seen this path before, used cached media */
600 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
603 g_object_unref (factory);
607 g_object_ref (media);
614 GST_ERROR ("client %p: no factory for path %s", client, path);
615 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
620 GST_ERROR ("client %p: not authorized to see factory path %s", client,
622 /* error reply is already sent */
627 GST_ERROR ("client %p: not authorized for factory path %s", client, path);
628 /* error reply is already sent */
633 GST_ERROR ("client %p: can't create media", client);
634 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
635 g_object_unref (factory);
641 GST_ERROR ("client %p: can't create thread", client);
642 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
643 g_object_unref (media);
645 g_object_unref (factory);
651 GST_ERROR ("client %p: can't prepare media", client);
652 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
653 g_object_unref (media);
655 g_object_unref (factory);
662 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
664 GstRTSPClientPrivate *priv = client->priv;
665 GstRTSPMessage message = { 0 };
670 gst_rtsp_message_init_data (&message, channel);
672 /* FIXME, need some sort of iovec RTSPMessage here */
673 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
676 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
678 g_mutex_lock (&priv->send_lock);
680 priv->send_func (client, &message, FALSE, priv->send_data);
681 g_mutex_unlock (&priv->send_lock);
683 gst_rtsp_message_steal_body (&message, &data, &usize);
684 gst_buffer_unmap (buffer, &map_info);
686 gst_rtsp_message_unset (&message);
692 link_transport (GstRTSPClient * client, GstRTSPSession * session,
693 GstRTSPStreamTransport * trans)
695 GstRTSPClientPrivate *priv = client->priv;
697 GST_DEBUG ("client %p: linking transport %p", client, trans);
699 gst_rtsp_stream_transport_set_callbacks (trans,
700 (GstRTSPSendFunc) do_send_data,
701 (GstRTSPSendFunc) do_send_data, client, NULL);
703 priv->transports = g_list_prepend (priv->transports, trans);
705 /* make sure our session can't expire */
706 gst_rtsp_session_prevent_expire (session);
710 link_session_transports (GstRTSPClient * client, GstRTSPSession * session,
711 GstRTSPSessionMedia * sessmedia)
716 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
717 for (i = 0; i < n_streams; i++) {
718 GstRTSPStreamTransport *trans;
719 const GstRTSPTransport *tr;
721 /* get the transport, if there is no transport configured, skip this stream */
722 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
726 tr = gst_rtsp_stream_transport_get_transport (trans);
728 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
729 /* for TCP, link the stream to the TCP connection of the client */
730 link_transport (client, session, trans);
736 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
737 GstRTSPStreamTransport * trans)
739 GstRTSPClientPrivate *priv = client->priv;
741 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
743 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
745 priv->transports = g_list_remove (priv->transports, trans);
747 /* our session can now expire */
748 gst_rtsp_session_allow_expire (session);
752 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
753 GstRTSPSessionMedia * sessmedia)
758 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
759 for (i = 0; i < n_streams; i++) {
760 GstRTSPStreamTransport *trans;
761 const GstRTSPTransport *tr;
763 /* get the transport, if there is no transport configured, skip this stream */
764 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
768 tr = gst_rtsp_stream_transport_get_transport (trans);
770 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
771 /* for TCP, unlink the stream from the TCP connection of the client */
772 unlink_transport (client, session, trans);
778 close_connection (GstRTSPClient * client)
780 GstRTSPClientPrivate *priv = client->priv;
781 const gchar *tunnelid;
783 GST_DEBUG ("client %p: closing connection", client);
785 if (priv->connection) {
786 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
787 g_mutex_lock (&tunnels_lock);
788 /* remove from tunnelids */
789 g_hash_table_remove (tunnels, tunnelid);
790 g_mutex_unlock (&tunnels_lock);
792 gst_rtsp_connection_close (priv->connection);
795 /* connection is now closed, destroy the watch which will also cause the
796 * closed signal to be emitted */
798 GST_DEBUG ("client %p: destroying watch", client);
799 g_source_destroy ((GSource *) priv->watch);
801 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
806 default_make_path_from_uri (GstRTSPClient * client, const GstRTSPUrl * uri)
811 path = g_strconcat (uri->abspath, "?", uri->query, NULL);
813 path = g_strdup (uri->abspath);
819 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
821 GstRTSPClientPrivate *priv = client->priv;
822 GstRTSPClientClass *klass;
823 GstRTSPSession *session;
824 GstRTSPSessionMedia *sessmedia;
825 GstRTSPStatusCode code;
828 gboolean keep_session;
833 session = ctx->session;
838 klass = GST_RTSP_CLIENT_GET_CLASS (client);
839 path = klass->make_path_from_uri (client, ctx->uri);
841 /* get a handle to the configuration of the media in the session */
842 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
846 /* only aggregate control for now.. */
847 if (path[matched] != '\0')
852 ctx->sessmedia = sessmedia;
854 /* we emit the signal before closing the connection */
855 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
858 /* make sure we unblock the backlog and don't accept new messages
860 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
862 /* unlink the all TCP callbacks */
863 unlink_session_transports (client, session, sessmedia);
865 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
867 /* allow messages again so that we can send the reply */
868 gst_rtsp_watch_set_flushing (priv->watch, FALSE);
870 /* unmanage the media in the session, returns false if all media session
872 keep_session = gst_rtsp_session_release_media (session, sessmedia);
874 /* construct the response now */
875 code = GST_RTSP_STS_OK;
876 gst_rtsp_message_init_response (ctx->response, code,
877 gst_rtsp_status_as_text (code), ctx->request);
879 send_message (client, ctx, ctx->response, TRUE);
882 /* remove the session */
883 gst_rtsp_session_pool_remove (priv->session_pool, session);
891 GST_ERROR ("client %p: no session", client);
892 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
897 GST_ERROR ("client %p: no uri supplied", client);
898 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
903 GST_ERROR ("client %p: no media for uri", client);
904 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
910 GST_ERROR ("client %p: no aggregate path %s", client, path);
911 send_generic_response (client,
912 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
919 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
923 res = gst_rtsp_params_set (client, ctx);
929 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
933 res = gst_rtsp_params_get (client, ctx);
939 handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
945 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
946 if (res != GST_RTSP_OK)
950 /* no body, keep-alive request */
951 send_generic_response (client, GST_RTSP_STS_OK, ctx);
953 /* there is a body, handle the params */
954 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
955 if (res != GST_RTSP_OK)
958 send_message (client, ctx, ctx->response, FALSE);
961 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
969 GST_ERROR ("client %p: bad request", client);
970 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
976 handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
982 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
983 if (res != GST_RTSP_OK)
987 /* no body, keep-alive request */
988 send_generic_response (client, GST_RTSP_STS_OK, ctx);
990 /* there is a body, handle the params */
991 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
992 if (res != GST_RTSP_OK)
995 send_message (client, ctx, ctx->response, FALSE);
998 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
1006 GST_ERROR ("client %p: bad request", client);
1007 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1013 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
1015 GstRTSPSession *session;
1016 GstRTSPClientClass *klass;
1017 GstRTSPSessionMedia *sessmedia;
1018 GstRTSPStatusCode code;
1019 GstRTSPState rtspstate;
1023 if (!(session = ctx->session))
1029 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1030 path = klass->make_path_from_uri (client, ctx->uri);
1032 /* get a handle to the configuration of the media in the session */
1033 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1037 if (path[matched] != '\0')
1042 ctx->sessmedia = sessmedia;
1044 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1045 /* the session state must be playing or recording */
1046 if (rtspstate != GST_RTSP_STATE_PLAYING &&
1047 rtspstate != GST_RTSP_STATE_RECORDING)
1050 /* unlink the all TCP callbacks */
1051 unlink_session_transports (client, session, sessmedia);
1053 /* then pause sending */
1054 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
1056 /* construct the response now */
1057 code = GST_RTSP_STS_OK;
1058 gst_rtsp_message_init_response (ctx->response, code,
1059 gst_rtsp_status_as_text (code), ctx->request);
1061 send_message (client, ctx, ctx->response, FALSE);
1063 /* the state is now READY */
1064 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1066 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
1073 GST_ERROR ("client %p: no seesion", client);
1074 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1079 GST_ERROR ("client %p: no uri supplied", client);
1080 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1085 GST_ERROR ("client %p: no media for uri", client);
1086 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1092 GST_ERROR ("client %p: no aggregate path %s", client, path);
1093 send_generic_response (client,
1094 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1100 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
1101 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1107 /* convert @url and @path to a URL used as a content base for the factory
1108 * located at @path */
1110 make_base_url (GstRTSPClient * client, GstRTSPUrl * url, const gchar * path)
1116 /* check for trailing '/' and append one */
1117 trail = (path[strlen (path) - 1] != '/' ? "/" : "");
1122 tmp.abspath = g_strdup_printf ("%s%s", path, trail);
1124 result = gst_rtsp_url_get_request_uri (&tmp);
1125 g_free (tmp.abspath);
1131 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
1133 GstRTSPSession *session;
1134 GstRTSPClientClass *klass;
1135 GstRTSPSessionMedia *sessmedia;
1136 GstRTSPMedia *media;
1137 GstRTSPStatusCode code;
1140 GstRTSPTimeRange *range;
1142 GstRTSPState rtspstate;
1143 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1144 gchar *path, *rtpinfo;
1147 if (!(session = ctx->session))
1150 if (!(uri = ctx->uri))
1153 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1154 path = klass->make_path_from_uri (client, uri);
1156 /* get a handle to the configuration of the media in the session */
1157 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1161 if (path[matched] != '\0')
1166 ctx->sessmedia = sessmedia;
1167 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1169 /* the session state must be playing or ready */
1170 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1171 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1174 /* in play we first unsuspend, media could be suspended from SDP or PAUSED */
1175 if (!gst_rtsp_media_unsuspend (media))
1176 goto unsuspend_failed;
1178 /* parse the range header if we have one */
1179 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1180 if (res == GST_RTSP_OK) {
1181 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1182 /* we have a range, seek to the position */
1184 gst_rtsp_media_seek (media, range);
1185 gst_rtsp_range_free (range);
1189 /* link the all TCP callbacks */
1190 link_session_transports (client, session, sessmedia);
1192 /* grab RTPInfo from the media now */
1193 rtpinfo = gst_rtsp_session_media_get_rtpinfo (sessmedia);
1195 /* construct the response now */
1196 code = GST_RTSP_STS_OK;
1197 gst_rtsp_message_init_response (ctx->response, code,
1198 gst_rtsp_status_as_text (code), ctx->request);
1200 /* add the RTP-Info header */
1202 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO,
1206 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1208 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1210 send_message (client, ctx, ctx->response, FALSE);
1212 /* start playing after sending the response */
1213 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1215 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1217 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1224 GST_ERROR ("client %p: no session", client);
1225 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1230 GST_ERROR ("client %p: no uri supplied", client);
1231 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1236 GST_ERROR ("client %p: media not found", client);
1237 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1242 GST_ERROR ("client %p: no aggregate path %s", client, path);
1243 send_generic_response (client,
1244 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1250 GST_ERROR ("client %p: not PLAYING or READY", client);
1251 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1257 GST_ERROR ("client %p: unsuspend failed", client);
1258 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1264 do_keepalive (GstRTSPSession * session)
1266 GST_INFO ("keep session %p alive", session);
1267 gst_rtsp_session_touch (session);
1270 /* parse @transport and return a valid transport in @tr. only transports
1271 * supported by @stream are returned. Returns FALSE if no valid transport
1274 parse_transport (const char *transport, GstRTSPStream * stream,
1275 GstRTSPTransport * tr)
1282 gst_rtsp_transport_init (tr);
1284 GST_DEBUG ("parsing transports %s", transport);
1286 transports = g_strsplit (transport, ",", 0);
1288 /* loop through the transports, try to parse */
1289 for (i = 0; transports[i]; i++) {
1290 res = gst_rtsp_transport_parse (transports[i], tr);
1291 if (res != GST_RTSP_OK) {
1292 /* no valid transport, search some more */
1293 GST_WARNING ("could not parse transport %s", transports[i]);
1297 /* we have a transport, see if it's supported */
1298 if (!gst_rtsp_stream_is_transport_supported (stream, tr)) {
1299 GST_WARNING ("unsupported transport %s", transports[i]);
1303 /* we have a valid transport */
1304 GST_INFO ("found valid transport %s", transports[i]);
1309 gst_rtsp_transport_init (tr);
1311 g_strfreev (transports);
1317 default_configure_client_media (GstRTSPClient * client, GstRTSPMedia * media,
1318 GstRTSPStream * stream, GstRTSPContext * ctx)
1320 GstRTSPMessage *request = ctx->request;
1321 gchar *blocksize_str;
1323 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1324 &blocksize_str, 0) == GST_RTSP_OK) {
1328 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1329 if (end == blocksize_str)
1332 /* we don't want to change the mtu when this media
1333 * can be shared because it impacts other clients */
1334 if (gst_rtsp_media_is_shared (media))
1337 if (blocksize > G_MAXUINT)
1338 blocksize = G_MAXUINT;
1340 gst_rtsp_stream_set_mtu (stream, blocksize);
1348 GST_ERROR_OBJECT (client, "failed to parse blocksize");
1349 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1355 default_configure_client_transport (GstRTSPClient * client,
1356 GstRTSPContext * ctx, GstRTSPTransport * ct)
1358 GstRTSPClientPrivate *priv = client->priv;
1360 /* we have a valid transport now, set the destination of the client. */
1361 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1362 gboolean use_client_settings;
1364 use_client_settings =
1365 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1367 if (ct->destination && use_client_settings) {
1368 GstRTSPAddress *addr;
1370 addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1371 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1376 gst_rtsp_address_free (addr);
1378 GstRTSPAddress *addr;
1379 GSocketFamily family;
1381 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1383 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1387 g_free (ct->destination);
1388 ct->destination = g_strdup (addr->address);
1389 ct->port.min = addr->port;
1390 ct->port.max = addr->port + addr->n_ports - 1;
1391 ct->ttl = addr->ttl;
1393 gst_rtsp_address_free (addr);
1398 url = gst_rtsp_connection_get_url (priv->connection);
1399 g_free (ct->destination);
1400 ct->destination = g_strdup (url->host);
1402 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1404 GSocketAddress *addr;
1406 sock = gst_rtsp_connection_get_read_socket (priv->connection);
1407 if ((addr = g_socket_get_remote_address (sock, NULL))) {
1408 /* our read port is the sender port of client */
1409 ct->client_port.min =
1410 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1411 g_object_unref (addr);
1413 if ((addr = g_socket_get_local_address (sock, NULL))) {
1414 ct->server_port.max =
1415 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1416 g_object_unref (addr);
1418 sock = gst_rtsp_connection_get_write_socket (priv->connection);
1419 if ((addr = g_socket_get_remote_address (sock, NULL))) {
1420 /* our write port is the receiver port of client */
1421 ct->client_port.max =
1422 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1423 g_object_unref (addr);
1425 if ((addr = g_socket_get_local_address (sock, NULL))) {
1426 ct->server_port.min =
1427 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1428 g_object_unref (addr);
1430 /* check if the client selected channels for TCP */
1431 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1432 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1442 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1447 static GstRTSPTransport *
1448 make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
1449 GstRTSPTransport * ct)
1451 GstRTSPTransport *st;
1453 GSocketFamily family;
1455 /* prepare the server transport */
1456 gst_rtsp_transport_new (&st);
1458 st->trans = ct->trans;
1459 st->profile = ct->profile;
1460 st->lower_transport = ct->lower_transport;
1462 addr = g_inet_address_new_from_string (ct->destination);
1465 GST_ERROR ("failed to get inet addr from client destination");
1466 family = G_SOCKET_FAMILY_IPV4;
1468 family = g_inet_address_get_family (addr);
1469 g_object_unref (addr);
1473 switch (st->lower_transport) {
1474 case GST_RTSP_LOWER_TRANS_UDP:
1475 st->client_port = ct->client_port;
1476 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
1478 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1479 st->port = ct->port;
1480 st->destination = g_strdup (ct->destination);
1483 case GST_RTSP_LOWER_TRANS_TCP:
1484 st->interleaved = ct->interleaved;
1485 st->client_port = ct->client_port;
1486 st->server_port = ct->server_port;
1491 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
1496 #define AES_128_KEY_LEN 16
1497 #define AES_256_KEY_LEN 32
1499 #define HMAC_32_KEY_LEN 4
1500 #define HMAC_80_KEY_LEN 10
1503 mikey_apply_policy (GstCaps * caps, GstMIKEYMessage * msg, guint8 policy)
1505 const gchar *srtp_cipher;
1506 const gchar *srtp_auth;
1507 const GstMIKEYPayload *sp;
1510 /* loop over Security policy until we find one containing policy */
1512 if ((sp = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, i)) == NULL)
1515 if (((GstMIKEYPayloadSP *) sp)->policy == policy)
1519 /* the default ciphers */
1520 srtp_cipher = "aes-128-icm";
1521 srtp_auth = "hmac-sha1-80";
1523 /* now override the defaults with what is in the Security Policy */
1527 /* collect all the params and go over them */
1528 len = gst_mikey_payload_sp_get_n_params (sp);
1529 for (i = 0; i < len; i++) {
1530 const GstMIKEYPayloadSPParam *param =
1531 gst_mikey_payload_sp_get_param (sp, i);
1533 switch (param->type) {
1534 case GST_MIKEY_SP_SRTP_ENC_ALG:
1535 switch (param->val[0]) {
1537 srtp_cipher = "null";
1541 srtp_cipher = "aes-128-icm";
1547 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
1548 switch (param->val[0]) {
1549 case AES_128_KEY_LEN:
1550 srtp_cipher = "aes-128-icm";
1552 case AES_256_KEY_LEN:
1553 srtp_cipher = "aes-256-icm";
1559 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1560 switch (param->val[0]) {
1566 srtp_auth = "hmac-sha1-80";
1572 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
1573 switch (param->val[0]) {
1574 case HMAC_32_KEY_LEN:
1575 srtp_auth = "hmac-sha1-32";
1577 case HMAC_80_KEY_LEN:
1578 srtp_auth = "hmac-sha1-80";
1584 case GST_MIKEY_SP_SRTP_SRTP_ENC:
1586 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
1593 /* now configure the SRTP parameters */
1594 gst_caps_set_simple (caps,
1595 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
1596 "srtp-auth", G_TYPE_STRING, srtp_auth,
1597 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
1598 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
1604 handle_mikey_data (GstRTSPClient * client, GstRTSPContext * ctx,
1605 guint8 * data, gsize size)
1607 GstMIKEYMessage *msg;
1609 GstCaps *caps = NULL;
1610 GstMIKEYPayloadKEMAC *kemac;
1611 const GstMIKEYPayloadKeyData *pkd;
1614 /* the MIKEY message contains a CSB or crypto session bundle. It is a
1615 * set of Crypto Sessions protected with the same master key.
1616 * In the context of SRTP, an RTP and its RTCP stream is part of a
1618 if ((msg = gst_mikey_message_new_from_data (data, size, NULL, NULL)) == NULL)
1621 /* we can only handle SRTP crypto sessions for now */
1622 if (msg->map_type != GST_MIKEY_MAP_TYPE_SRTP)
1623 goto invalid_map_type;
1625 /* get the number of crypto sessions. This maps SSRC to its
1626 * security parameters */
1627 n_cs = gst_mikey_message_get_n_cs (msg);
1629 goto no_crypto_sessions;
1631 /* we also need keys */
1632 if (!(kemac = (GstMIKEYPayloadKEMAC *) gst_mikey_message_find_payload
1633 (msg, GST_MIKEY_PT_KEMAC, 0)))
1636 /* we don't support encrypted keys */
1637 if (kemac->enc_alg != GST_MIKEY_ENC_NULL
1638 || kemac->mac_alg != GST_MIKEY_MAC_NULL)
1639 goto unsupported_encryption;
1641 /* get Key data sub-payload */
1642 pkd = (const GstMIKEYPayloadKeyData *)
1643 gst_mikey_payload_kemac_get_sub (&kemac->pt, 0);
1646 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
1649 /* go over all crypto sessions and create the security policy for each
1651 for (i = 0; i < n_cs; i++) {
1652 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
1654 caps = gst_caps_new_simple ("application/x-srtp",
1655 "ssrc", G_TYPE_UINT, map->ssrc,
1656 "roc", G_TYPE_UINT, map->roc, "srtp-key", GST_TYPE_BUFFER, key, NULL);
1657 mikey_apply_policy (caps, msg, map->policy);
1659 gst_rtsp_stream_update_crypto (ctx->stream, map->ssrc, caps);
1660 gst_caps_unref (caps);
1662 gst_mikey_message_unref (msg);
1669 GST_DEBUG_OBJECT (client, "failed to parse MIKEY message");
1674 GST_DEBUG_OBJECT (client, "invalid map type %d", msg->map_type);
1675 goto cleanup_message;
1679 GST_DEBUG_OBJECT (client, "no crypto sessions");
1680 goto cleanup_message;
1684 GST_DEBUG_OBJECT (client, "no keys found");
1685 goto cleanup_message;
1687 unsupported_encryption:
1689 GST_DEBUG_OBJECT (client, "unsupported key encryption");
1690 goto cleanup_message;
1694 gst_mikey_message_unref (msg);
1699 #define IS_STRIP_CHAR(c) (g_ascii_isspace ((guchar)(c)) || ((c) == '\"'))
1702 strip_chars (gchar * str)
1709 if (!IS_STRIP_CHAR (str[len]))
1713 for (s = str; *s && IS_STRIP_CHAR (*s); s++);
1714 memmove (str, s, len + 1);
1717 /* KeyMgmt = "KeyMgmt" ":" key-mgmt-spec 0*("," key-mgmt-spec)
1718 * key-mgmt-spec = "prot" "=" KMPID ";" ["uri" "=" %x22 URI %x22 ";"]
1721 handle_keymgmt (GstRTSPClient * client, GstRTSPContext * ctx, gchar * keymgmt)
1726 specs = g_strsplit (keymgmt, ",", 0);
1727 for (i = 0; specs[i]; i++) {
1730 split = g_strsplit (specs[i], ";", 0);
1731 for (j = 0; split[j]; j++) {
1732 g_strstrip (split[j]);
1733 if (g_str_has_prefix (split[j], "prot=")) {
1734 g_strstrip (split[j] + 5);
1735 if (!g_str_equal (split[j] + 5, "mikey"))
1737 GST_DEBUG ("found mikey");
1738 } else if (g_str_has_prefix (split[j], "uri=")) {
1739 strip_chars (split[j] + 4);
1740 GST_DEBUG ("found uri '%s'", split[j] + 4);
1741 } else if (g_str_has_prefix (split[j], "data=")) {
1744 strip_chars (split[j] + 5);
1745 GST_DEBUG ("found data '%s'", split[j] + 5);
1746 data = g_base64_decode_inplace (split[j] + 5, &size);
1747 handle_mikey_data (client, ctx, data, size);
1755 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
1757 GstRTSPClientPrivate *priv = client->priv;
1760 gchar *transport, *keymgmt;
1761 GstRTSPTransport *ct, *st;
1762 GstRTSPStatusCode code;
1763 GstRTSPSession *session;
1764 GstRTSPStreamTransport *trans;
1766 GstRTSPSessionMedia *sessmedia;
1767 GstRTSPMedia *media;
1768 GstRTSPStream *stream;
1769 GstRTSPState rtspstate;
1770 GstRTSPClientClass *klass;
1771 gchar *path, *control;
1778 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1779 path = klass->make_path_from_uri (client, uri);
1781 /* parse the transport */
1783 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
1785 if (res != GST_RTSP_OK)
1788 /* we create the session after parsing stuff so that we don't make
1789 * a session for malformed requests */
1790 if (priv->session_pool == NULL)
1793 session = ctx->session;
1796 g_object_ref (session);
1797 /* get a handle to the configuration of the media in the session, this can
1798 * return NULL if this is a new url to manage in this session. */
1799 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1801 /* we need a new media configuration in this session */
1805 /* we have no session media, find one and manage it */
1806 if (sessmedia == NULL) {
1807 /* get a handle to the configuration of the media in the session */
1808 media = find_media (client, ctx, path, &matched);
1810 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1811 g_object_ref (media);
1813 goto media_not_found;
1815 /* no media, not found then */
1817 goto media_not_found_no_reply;
1819 if (path[matched] == '\0')
1820 goto control_not_found;
1822 /* path is what matched. */
1823 path[matched] = '\0';
1824 /* control is remainder */
1825 control = &path[matched + 1];
1827 /* find the stream now using the control part */
1828 stream = gst_rtsp_media_find_stream (media, control);
1830 goto stream_not_found;
1832 /* now we have a uri identifying a valid media and stream */
1833 ctx->stream = stream;
1836 if (session == NULL) {
1837 /* create a session if this fails we probably reached our session limit or
1839 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1840 goto service_unavailable;
1842 /* make sure this client is closed when the session is closed */
1843 client_watch_session (client, session);
1845 /* signal new session */
1846 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1849 ctx->session = session;
1852 if (sessmedia == NULL) {
1853 /* manage the media in our session now, if not done already */
1854 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1855 /* if we stil have no media, error */
1856 if (sessmedia == NULL)
1857 goto sessmedia_unavailable;
1859 g_object_unref (media);
1862 ctx->sessmedia = sessmedia;
1864 if (!klass->configure_client_media (client, media, stream, ctx))
1865 goto configure_media_failed_no_reply;
1867 gst_rtsp_transport_new (&ct);
1869 /* parse and find a usable supported transport */
1870 if (!parse_transport (transport, stream, ct))
1871 goto unsupported_transports;
1873 /* update the client transport */
1874 if (!klass->configure_client_transport (client, ctx, ct))
1875 goto unsupported_client_transport;
1877 /* parse the keymgmt */
1878 if (gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_KEYMGMT,
1879 &keymgmt, 0) == GST_RTSP_OK) {
1880 if (!handle_keymgmt (client, ctx, keymgmt))
1884 /* set in the session media transport */
1885 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1887 /* configure the url used to set this transport, this we will use when
1888 * generating the response for the PLAY request */
1889 gst_rtsp_stream_transport_set_url (trans, uri);
1891 /* configure keepalive for this transport */
1892 gst_rtsp_stream_transport_set_keepalive (trans,
1893 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1895 /* create and serialize the server transport */
1896 st = make_server_transport (client, ctx, ct);
1897 trans_str = gst_rtsp_transport_as_text (st);
1898 gst_rtsp_transport_free (st);
1900 /* construct the response now */
1901 code = GST_RTSP_STS_OK;
1902 gst_rtsp_message_init_response (ctx->response, code,
1903 gst_rtsp_status_as_text (code), ctx->request);
1905 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
1909 send_message (client, ctx, ctx->response, FALSE);
1911 /* update the state */
1912 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1913 switch (rtspstate) {
1914 case GST_RTSP_STATE_PLAYING:
1915 case GST_RTSP_STATE_RECORDING:
1916 case GST_RTSP_STATE_READY:
1917 /* no state change */
1920 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1923 g_object_unref (session);
1926 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
1933 GST_ERROR ("client %p: no uri", client);
1934 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1939 GST_ERROR ("client %p: no transport", client);
1940 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1945 GST_ERROR ("client %p: no session pool configured", client);
1946 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1949 media_not_found_no_reply:
1951 GST_ERROR ("client %p: media '%s' not found", client, path);
1952 /* error reply is already sent */
1957 GST_ERROR ("client %p: media '%s' not found", client, path);
1958 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1963 GST_ERROR ("client %p: no control in path '%s'", client, path);
1964 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1965 g_object_unref (media);
1970 GST_ERROR ("client %p: stream '%s' not found", client, control);
1971 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1972 g_object_unref (media);
1975 service_unavailable:
1977 GST_ERROR ("client %p: can't create session", client);
1978 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1979 g_object_unref (media);
1982 sessmedia_unavailable:
1984 GST_ERROR ("client %p: can't create session media", client);
1985 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1986 g_object_unref (media);
1987 goto cleanup_session;
1989 configure_media_failed_no_reply:
1991 GST_ERROR ("client %p: configure_media failed", client);
1992 /* error reply is already sent */
1993 goto cleanup_session;
1995 unsupported_transports:
1997 GST_ERROR ("client %p: unsupported transports", client);
1998 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1999 goto cleanup_transport;
2001 unsupported_client_transport:
2003 GST_ERROR ("client %p: unsupported client transport", client);
2004 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
2005 goto cleanup_transport;
2009 GST_ERROR ("client %p: keymgmt error", client);
2010 send_generic_response (client, GST_RTSP_STS_KEY_MANAGEMENT_FAILURE, ctx);
2011 goto cleanup_transport;
2015 gst_rtsp_transport_free (ct);
2017 g_object_unref (session);
2024 static GstSDPMessage *
2025 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
2027 GstRTSPClientPrivate *priv = client->priv;
2032 gst_sdp_message_new (&sdp);
2034 /* some standard things first */
2035 gst_sdp_message_set_version (sdp, "0");
2042 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
2045 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
2046 gst_sdp_message_set_information (sdp, "rtsp-server");
2047 gst_sdp_message_add_time (sdp, "0", "0", NULL);
2048 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
2049 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
2050 gst_sdp_message_add_attribute (sdp, "control", "*");
2052 info.is_ipv6 = priv->is_ipv6;
2053 info.server_ip = priv->server_ip;
2055 /* create an SDP for the media object */
2056 if (!gst_rtsp_media_setup_sdp (media, sdp, &info))
2064 GST_ERROR ("client %p: could not create SDP", client);
2065 gst_sdp_message_free (sdp);
2070 /* for the describe we must generate an SDP */
2072 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
2074 GstRTSPClientPrivate *priv = client->priv;
2079 GstRTSPMedia *media;
2080 GstRTSPClientClass *klass;
2082 klass = GST_RTSP_CLIENT_GET_CLASS (client);
2087 /* check what kind of format is accepted, we don't really do anything with it
2088 * and always return SDP for now. */
2093 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
2095 if (res == GST_RTSP_ENOTIMPL)
2098 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
2102 if (!priv->mount_points)
2103 goto no_mount_points;
2105 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
2108 /* find the media object for the uri */
2109 if (!(media = find_media (client, ctx, path, NULL)))
2112 /* create an SDP for the media object on this client */
2113 if (!(sdp = klass->create_sdp (client, media)))
2116 /* we suspend after the describe */
2117 gst_rtsp_media_suspend (media);
2118 g_object_unref (media);
2120 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2121 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2123 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
2126 /* content base for some clients that might screw up creating the setup uri */
2127 str = make_base_url (client, ctx->uri, path);
2130 GST_INFO ("adding content-base: %s", str);
2131 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE, str);
2133 /* add SDP to the response body */
2134 str = gst_sdp_message_as_text (sdp);
2135 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
2136 gst_sdp_message_free (sdp);
2138 send_message (client, ctx, ctx->response, FALSE);
2140 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
2148 GST_ERROR ("client %p: no uri", client);
2149 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2154 GST_ERROR ("client %p: no mount points configured", client);
2155 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2160 GST_ERROR ("client %p: can't find path for url", client);
2161 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2166 GST_ERROR ("client %p: no media", client);
2168 /* error reply is already sent */
2173 GST_ERROR ("client %p: can't create SDP", client);
2174 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2176 g_object_unref (media);
2182 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
2184 GstRTSPMethod options;
2187 options = GST_RTSP_DESCRIBE |
2192 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
2194 str = gst_rtsp_options_as_text (options);
2196 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2197 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2199 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
2202 send_message (client, ctx, ctx->response, FALSE);
2204 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
2210 /* remove duplicate and trailing '/' */
2212 sanitize_uri (GstRTSPUrl * uri)
2216 gboolean have_slash, prev_slash;
2218 s = d = uri->abspath;
2219 len = strlen (uri->abspath);
2223 for (i = 0; i < len; i++) {
2224 have_slash = s[i] == '/';
2226 if (!have_slash || !prev_slash)
2228 prev_slash = have_slash;
2230 len = d - uri->abspath;
2231 /* don't remove the first slash if that's the only thing left */
2232 if (len > 1 && *(d - 1) == '/')
2237 /* is called when the session is removed from its session pool. */
2239 client_session_removed (GstRTSPSessionPool * pool, GstRTSPSession * session,
2240 GstRTSPClient * client)
2242 GstRTSPClientPrivate *priv = client->priv;
2244 GST_INFO ("client %p: session %p removed", client, session);
2246 g_mutex_lock (&priv->lock);
2247 client_unwatch_session (client, session, NULL);
2248 g_mutex_unlock (&priv->lock);
2251 /* Returns TRUE if there are no Require headers, otherwise returns FALSE
2252 * and also returns a newly-allocated string of (comma-separated) unsupported
2253 * options in the unsupported_reqs variable .
2255 * There may be multiple Require headers, but we must send one single
2256 * Unsupported header with all the unsupported options as response. If
2257 * an incoming Require header contained a comma-separated list of options
2258 * GstRtspConnection will already have split that list up into multiple
2261 * TODO: allow the application to decide what features are supported
2264 check_request_requirements (GstRTSPMessage * msg, gchar ** unsupported_reqs)
2267 GPtrArray *arr = NULL;
2273 res = gst_rtsp_message_get_header (msg, GST_RTSP_HDR_REQUIRE, &reqs, i++);
2275 if (res == GST_RTSP_ENOTIMPL)
2279 arr = g_ptr_array_new_with_free_func ((GDestroyNotify) g_free);
2281 g_ptr_array_add (arr, g_strdup (reqs));
2285 /* if we don't have any Require headers at all, all is fine */
2289 /* otherwise we've now processed at all the Require headers */
2290 g_ptr_array_add (arr, NULL);
2292 /* for now we don't commit to supporting anything, so will just report
2293 * all of the required options as unsupported */
2294 *unsupported_reqs = g_strjoinv (", ", (gchar **) arr->pdata);
2296 g_ptr_array_unref (arr);
2301 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
2303 GstRTSPClientPrivate *priv = client->priv;
2304 GstRTSPMethod method;
2305 const gchar *uristr;
2306 GstRTSPUrl *uri = NULL;
2307 GstRTSPVersion version;
2309 GstRTSPSession *session = NULL;
2310 GstRTSPContext sctx = { NULL }, *ctx;
2311 GstRTSPMessage response = { 0 };
2312 gchar *unsupported_reqs = NULL;
2315 if (!(ctx = gst_rtsp_context_get_current ())) {
2317 ctx->auth = priv->auth;
2318 gst_rtsp_context_push_current (ctx);
2321 ctx->conn = priv->connection;
2322 ctx->client = client;
2323 ctx->request = request;
2324 ctx->response = &response;
2326 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2327 gst_rtsp_message_dump (request);
2330 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
2332 GST_INFO ("client %p: received a request %s %s %s", client,
2333 gst_rtsp_method_as_text (method), uristr,
2334 gst_rtsp_version_as_text (version));
2336 /* we can only handle 1.0 requests */
2337 if (version != GST_RTSP_VERSION_1_0)
2340 ctx->method = method;
2342 /* we always try to parse the url first */
2343 if (strcmp (uristr, "*") == 0) {
2344 /* special case where we have * as uri, keep uri = NULL */
2345 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
2346 /* check if the uristr is an absolute path <=> scheme and host information
2350 scheme = g_uri_parse_scheme (uristr);
2351 if (scheme == NULL && g_str_has_prefix (uristr, "/")) {
2352 gchar *absolute_uristr = NULL;
2354 GST_WARNING_OBJECT (client, "request doesn't contain absolute url");
2355 if (priv->server_ip == NULL) {
2356 GST_WARNING_OBJECT (client, "host information missing");
2361 g_strdup_printf ("rtsp://%s%s", priv->server_ip, uristr);
2363 GST_DEBUG_OBJECT (client, "absolute url: %s", absolute_uristr);
2364 if (gst_rtsp_url_parse (absolute_uristr, &uri) != GST_RTSP_OK) {
2365 g_free (absolute_uristr);
2368 g_free (absolute_uristr);
2375 /* get the session if there is any */
2376 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
2377 if (res == GST_RTSP_OK) {
2378 if (priv->session_pool == NULL)
2381 /* we had a session in the request, find it again */
2382 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2383 goto session_not_found;
2385 /* we add the session to the client list of watched sessions. When a session
2386 * disappears because it times out, we will be notified. If all sessions are
2387 * gone, we will close the connection */
2388 client_watch_session (client, session);
2391 /* sanitize the uri */
2395 ctx->session = session;
2397 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
2398 goto not_authorized;
2400 /* handle any 'Require' headers */
2401 if (!check_request_requirements (ctx->request, &unsupported_reqs))
2402 goto unsupported_requirement;
2404 /* now see what is asked and dispatch to a dedicated handler */
2406 case GST_RTSP_OPTIONS:
2407 handle_options_request (client, ctx);
2409 case GST_RTSP_DESCRIBE:
2410 handle_describe_request (client, ctx);
2412 case GST_RTSP_SETUP:
2413 handle_setup_request (client, ctx);
2416 handle_play_request (client, ctx);
2418 case GST_RTSP_PAUSE:
2419 handle_pause_request (client, ctx);
2421 case GST_RTSP_TEARDOWN:
2422 handle_teardown_request (client, ctx);
2424 case GST_RTSP_SET_PARAMETER:
2425 handle_set_param_request (client, ctx);
2427 case GST_RTSP_GET_PARAMETER:
2428 handle_get_param_request (client, ctx);
2430 case GST_RTSP_ANNOUNCE:
2431 case GST_RTSP_RECORD:
2432 case GST_RTSP_REDIRECT:
2433 goto not_implemented;
2434 case GST_RTSP_INVALID:
2441 gst_rtsp_context_pop_current (ctx);
2443 g_object_unref (session);
2445 gst_rtsp_url_free (uri);
2451 GST_ERROR ("client %p: version %d not supported", client, version);
2452 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
2458 GST_ERROR ("client %p: bad request", client);
2459 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2464 GST_ERROR ("client %p: no pool configured", client);
2465 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2470 GST_ERROR ("client %p: session not found", client);
2471 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2476 GST_ERROR ("client %p: not allowed", client);
2477 /* error reply is already sent */
2480 unsupported_requirement:
2482 GST_ERROR ("client %p: Required option is not supported (%s)", client,
2484 send_option_not_supported_response (client, ctx, unsupported_reqs);
2485 g_free (unsupported_reqs);
2490 GST_ERROR ("client %p: method %d not implemented", client, method);
2491 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
2498 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
2500 GstRTSPClientPrivate *priv = client->priv;
2502 GstRTSPSession *session = NULL;
2503 GstRTSPContext sctx = { NULL }, *ctx;
2506 if (!(ctx = gst_rtsp_context_get_current ())) {
2508 ctx->auth = priv->auth;
2509 gst_rtsp_context_push_current (ctx);
2512 ctx->conn = priv->connection;
2513 ctx->client = client;
2514 ctx->request = NULL;
2516 ctx->method = GST_RTSP_INVALID;
2517 ctx->response = response;
2519 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2520 gst_rtsp_message_dump (response);
2523 GST_INFO ("client %p: received a response", client);
2525 /* get the session if there is any */
2527 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
2528 if (res == GST_RTSP_OK) {
2529 if (priv->session_pool == NULL)
2532 /* we had a session in the request, find it again */
2533 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2534 goto session_not_found;
2536 /* we add the session to the client list of watched sessions. When a session
2537 * disappears because it times out, we will be notified. If all sessions are
2538 * gone, we will close the connection */
2539 client_watch_session (client, session);
2542 ctx->session = session;
2544 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
2549 gst_rtsp_context_pop_current (ctx);
2551 g_object_unref (session);
2556 GST_ERROR ("client %p: no pool configured", client);
2561 GST_ERROR ("client %p: session not found", client);
2567 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2569 GstRTSPClientPrivate *priv = client->priv;
2578 /* find the stream for this message */
2579 res = gst_rtsp_message_parse_data (message, &channel);
2580 if (res != GST_RTSP_OK)
2583 gst_rtsp_message_steal_body (message, &data, &size);
2585 buffer = gst_buffer_new_wrapped (data, size);
2588 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2589 GstRTSPStreamTransport *trans;
2590 GstRTSPStream *stream;
2591 const GstRTSPTransport *tr;
2595 tr = gst_rtsp_stream_transport_get_transport (trans);
2596 stream = gst_rtsp_stream_transport_get_stream (trans);
2598 /* check for TCP transport */
2599 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2600 /* dispatch to the stream based on the channel number */
2601 if (tr->interleaved.min == channel) {
2602 gst_rtsp_stream_recv_rtp (stream, buffer);
2605 } else if (tr->interleaved.max == channel) {
2606 gst_rtsp_stream_recv_rtcp (stream, buffer);
2613 gst_buffer_unref (buffer);
2617 * gst_rtsp_client_set_session_pool:
2618 * @client: a #GstRTSPClient
2619 * @pool: (transfer none): a #GstRTSPSessionPool
2621 * Set @pool as the sessionpool for @client which it will use to find
2622 * or allocate sessions. the sessionpool is usually inherited from the server
2623 * that created the client but can be overridden later.
2626 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2627 GstRTSPSessionPool * pool)
2629 GstRTSPSessionPool *old;
2630 GstRTSPClientPrivate *priv;
2632 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2634 priv = client->priv;
2637 g_object_ref (pool);
2639 g_mutex_lock (&priv->lock);
2640 old = priv->session_pool;
2641 priv->session_pool = pool;
2643 if (priv->session_removed_id)
2644 g_signal_handler_disconnect (old, priv->session_removed_id);
2646 priv->session_removed_id = g_signal_connect (pool, "session-removed",
2647 G_CALLBACK (client_session_removed), client);
2649 priv->session_removed_id = 0;
2650 g_mutex_unlock (&priv->lock);
2652 /* FIXME, should remove all sessions from the old pool for this client */
2654 g_object_unref (old);
2658 * gst_rtsp_client_get_session_pool:
2659 * @client: a #GstRTSPClient
2661 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2663 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2665 GstRTSPSessionPool *
2666 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2668 GstRTSPClientPrivate *priv;
2669 GstRTSPSessionPool *result;
2671 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2673 priv = client->priv;
2675 g_mutex_lock (&priv->lock);
2676 if ((result = priv->session_pool))
2677 g_object_ref (result);
2678 g_mutex_unlock (&priv->lock);
2684 * gst_rtsp_client_set_mount_points:
2685 * @client: a #GstRTSPClient
2686 * @mounts: (transfer none): a #GstRTSPMountPoints
2688 * Set @mounts as the mount points for @client which it will use to map urls
2689 * to media streams. These mount points are usually inherited from the server that
2690 * created the client but can be overriden later.
2693 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2694 GstRTSPMountPoints * mounts)
2696 GstRTSPClientPrivate *priv;
2697 GstRTSPMountPoints *old;
2699 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2701 priv = client->priv;
2704 g_object_ref (mounts);
2706 g_mutex_lock (&priv->lock);
2707 old = priv->mount_points;
2708 priv->mount_points = mounts;
2709 g_mutex_unlock (&priv->lock);
2712 g_object_unref (old);
2716 * gst_rtsp_client_get_mount_points:
2717 * @client: a #GstRTSPClient
2719 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2721 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2723 GstRTSPMountPoints *
2724 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2726 GstRTSPClientPrivate *priv;
2727 GstRTSPMountPoints *result;
2729 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2731 priv = client->priv;
2733 g_mutex_lock (&priv->lock);
2734 if ((result = priv->mount_points))
2735 g_object_ref (result);
2736 g_mutex_unlock (&priv->lock);
2742 * gst_rtsp_client_set_auth:
2743 * @client: a #GstRTSPClient
2744 * @auth: (transfer none): a #GstRTSPAuth
2746 * configure @auth to be used as the authentication manager of @client.
2749 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2751 GstRTSPClientPrivate *priv;
2754 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2756 priv = client->priv;
2759 g_object_ref (auth);
2761 g_mutex_lock (&priv->lock);
2764 g_mutex_unlock (&priv->lock);
2767 g_object_unref (old);
2772 * gst_rtsp_client_get_auth:
2773 * @client: a #GstRTSPClient
2775 * Get the #GstRTSPAuth used as the authentication manager of @client.
2777 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2781 gst_rtsp_client_get_auth (GstRTSPClient * client)
2783 GstRTSPClientPrivate *priv;
2784 GstRTSPAuth *result;
2786 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2788 priv = client->priv;
2790 g_mutex_lock (&priv->lock);
2791 if ((result = priv->auth))
2792 g_object_ref (result);
2793 g_mutex_unlock (&priv->lock);
2799 * gst_rtsp_client_set_thread_pool:
2800 * @client: a #GstRTSPClient
2801 * @pool: (transfer none): a #GstRTSPThreadPool
2803 * configure @pool to be used as the thread pool of @client.
2806 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2807 GstRTSPThreadPool * pool)
2809 GstRTSPClientPrivate *priv;
2810 GstRTSPThreadPool *old;
2812 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2814 priv = client->priv;
2817 g_object_ref (pool);
2819 g_mutex_lock (&priv->lock);
2820 old = priv->thread_pool;
2821 priv->thread_pool = pool;
2822 g_mutex_unlock (&priv->lock);
2825 g_object_unref (old);
2829 * gst_rtsp_client_get_thread_pool:
2830 * @client: a #GstRTSPClient
2832 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2834 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2838 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2840 GstRTSPClientPrivate *priv;
2841 GstRTSPThreadPool *result;
2843 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2845 priv = client->priv;
2847 g_mutex_lock (&priv->lock);
2848 if ((result = priv->thread_pool))
2849 g_object_ref (result);
2850 g_mutex_unlock (&priv->lock);
2856 * gst_rtsp_client_set_connection:
2857 * @client: a #GstRTSPClient
2858 * @conn: (transfer full): a #GstRTSPConnection
2860 * Set the #GstRTSPConnection of @client. This function takes ownership of
2863 * Returns: %TRUE on success.
2866 gst_rtsp_client_set_connection (GstRTSPClient * client,
2867 GstRTSPConnection * conn)
2869 GstRTSPClientPrivate *priv;
2870 GSocket *read_socket;
2871 GSocketAddress *address;
2873 GError *error = NULL;
2875 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2876 g_return_val_if_fail (conn != NULL, FALSE);
2878 priv = client->priv;
2880 read_socket = gst_rtsp_connection_get_read_socket (conn);
2882 if (!(address = g_socket_get_local_address (read_socket, &error)))
2885 g_free (priv->server_ip);
2886 /* keep the original ip that the client connected to */
2887 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2888 GInetAddress *iaddr;
2890 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2892 /* socket might be ipv6 but adress still ipv4 */
2893 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2894 priv->server_ip = g_inet_address_to_string (iaddr);
2895 g_object_unref (address);
2897 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2898 priv->server_ip = g_strdup ("unknown");
2901 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2902 priv->server_ip, priv->is_ipv6);
2904 url = gst_rtsp_connection_get_url (conn);
2905 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2907 priv->connection = conn;
2914 GST_ERROR ("could not get local address %s", error->message);
2915 g_error_free (error);
2921 * gst_rtsp_client_get_connection:
2922 * @client: a #GstRTSPClient
2924 * Get the #GstRTSPConnection of @client.
2926 * Returns: (transfer none): the #GstRTSPConnection of @client.
2927 * The connection object returned remains valid until the client is freed.
2930 gst_rtsp_client_get_connection (GstRTSPClient * client)
2932 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2934 return client->priv->connection;
2938 * gst_rtsp_client_set_send_func:
2939 * @client: a #GstRTSPClient
2940 * @func: (scope notified): a #GstRTSPClientSendFunc
2941 * @user_data: (closure): user data passed to @func
2942 * @notify: (allow-none): called when @user_data is no longer in use
2944 * Set @func as the callback that will be called when a new message needs to be
2945 * sent to the client. @user_data is passed to @func and @notify is called when
2946 * @user_data is no longer in use.
2948 * By default, the client will send the messages on the #GstRTSPConnection that
2949 * was configured with gst_rtsp_client_attach() was called.
2952 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2953 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2955 GstRTSPClientPrivate *priv;
2956 GDestroyNotify old_notify;
2959 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2961 priv = client->priv;
2963 g_mutex_lock (&priv->send_lock);
2964 priv->send_func = func;
2965 old_notify = priv->send_notify;
2966 old_data = priv->send_data;
2967 priv->send_notify = notify;
2968 priv->send_data = user_data;
2969 g_mutex_unlock (&priv->send_lock);
2972 old_notify (old_data);
2976 * gst_rtsp_client_handle_message:
2977 * @client: a #GstRTSPClient
2978 * @message: (transfer none): an #GstRTSPMessage
2980 * Let the client handle @message.
2982 * Returns: a #GstRTSPResult.
2985 gst_rtsp_client_handle_message (GstRTSPClient * client,
2986 GstRTSPMessage * message)
2988 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2989 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2991 switch (message->type) {
2992 case GST_RTSP_MESSAGE_REQUEST:
2993 handle_request (client, message);
2995 case GST_RTSP_MESSAGE_RESPONSE:
2996 handle_response (client, message);
2998 case GST_RTSP_MESSAGE_DATA:
2999 handle_data (client, message);
3008 * gst_rtsp_client_send_message:
3009 * @client: a #GstRTSPClient
3010 * @session: (allow-none) (transfer none): a #GstRTSPSession to send
3011 * the message to or %NULL
3012 * @message: (transfer none): The #GstRTSPMessage to send
3014 * Send a message message to the remote end. @message must be a
3015 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
3018 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
3019 GstRTSPMessage * message)
3021 GstRTSPContext sctx = { NULL }
3023 GstRTSPClientPrivate *priv;
3025 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
3026 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3027 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
3028 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
3030 priv = client->priv;
3032 if (!(ctx = gst_rtsp_context_get_current ())) {
3034 ctx->auth = priv->auth;
3035 gst_rtsp_context_push_current (ctx);
3038 ctx->conn = priv->connection;
3039 ctx->client = client;
3040 ctx->session = session;
3042 send_message (client, ctx, message, FALSE);
3045 gst_rtsp_context_pop_current (ctx);
3050 static GstRTSPResult
3051 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
3052 gboolean close, gpointer user_data)
3054 GstRTSPClientPrivate *priv = client->priv;
3062 /* send the response and store the seq number so we can wait until it's
3063 * written to the client to close the connection */
3065 gst_rtsp_watch_send_message (priv->watch, message,
3066 close ? &priv->close_seq : NULL);
3067 if (ret == GST_RTSP_OK)
3070 if (ret != GST_RTSP_ENOMEM)
3074 if (priv->drop_backlog)
3077 /* queue was full, wait for more space */
3078 GST_DEBUG_OBJECT (client, "waiting for backlog");
3079 ret = gst_rtsp_watch_wait_backlog (priv->watch, &time);
3080 GST_DEBUG_OBJECT (client, "Resend due to backlog full");
3081 } while (ret != GST_RTSP_EINTR);
3088 GST_DEBUG_OBJECT (client, "got error %d", ret);
3093 static GstRTSPResult
3094 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
3097 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
3100 static GstRTSPResult
3101 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
3103 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3104 GstRTSPClientPrivate *priv = client->priv;
3106 if (priv->close_seq && priv->close_seq == cseq) {
3107 GST_INFO ("client %p: send close message", client);
3108 priv->close_seq = 0;
3109 close_connection (client);
3115 static GstRTSPResult
3116 closed (GstRTSPWatch * watch, gpointer user_data)
3118 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3119 GstRTSPClientPrivate *priv = client->priv;
3120 const gchar *tunnelid;
3122 GST_INFO ("client %p: connection closed", client);
3124 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
3125 g_mutex_lock (&tunnels_lock);
3126 /* remove from tunnelids */
3127 g_hash_table_remove (tunnels, tunnelid);
3128 g_mutex_unlock (&tunnels_lock);
3131 gst_rtsp_watch_set_flushing (watch, TRUE);
3132 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
3137 static GstRTSPResult
3138 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
3140 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3143 str = gst_rtsp_strresult (result);
3144 GST_INFO ("client %p: received an error %s", client, str);
3150 static GstRTSPResult
3151 error_full (GstRTSPWatch * watch, GstRTSPResult result,
3152 GstRTSPMessage * message, guint id, gpointer user_data)
3154 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3157 str = gst_rtsp_strresult (result);
3159 ("client %p: error when handling message %p with id %d: %s",
3160 client, message, id, str);
3167 remember_tunnel (GstRTSPClient * client)
3169 GstRTSPClientPrivate *priv = client->priv;
3170 const gchar *tunnelid;
3172 /* store client in the pending tunnels */
3173 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
3174 if (tunnelid == NULL)
3177 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
3179 /* we can't have two clients connecting with the same tunnelid */
3180 g_mutex_lock (&tunnels_lock);
3181 if (g_hash_table_lookup (tunnels, tunnelid))
3182 goto tunnel_existed;
3184 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
3185 g_mutex_unlock (&tunnels_lock);
3192 GST_ERROR ("client %p: no tunnelid provided", client);
3197 g_mutex_unlock (&tunnels_lock);
3198 GST_ERROR ("client %p: tunnel session %s already existed", client,
3204 static GstRTSPResult
3205 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
3207 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3208 GstRTSPClientPrivate *priv = client->priv;
3210 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
3213 /* ignore error, it'll only be a problem when the client does a POST again */
3214 remember_tunnel (client);
3220 handle_tunnel (GstRTSPClient * client)
3222 GstRTSPClientPrivate *priv = client->priv;
3223 GstRTSPClient *oclient;
3224 GstRTSPClientPrivate *opriv;
3225 const gchar *tunnelid;
3227 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
3228 if (tunnelid == NULL)
3231 /* check for previous tunnel */
3232 g_mutex_lock (&tunnels_lock);
3233 oclient = g_hash_table_lookup (tunnels, tunnelid);
3235 if (oclient == NULL) {
3236 /* no previous tunnel, remember tunnel */
3237 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
3238 g_mutex_unlock (&tunnels_lock);
3240 GST_INFO ("client %p: no previous tunnel found, remembering tunnel (%p)",
3241 client, priv->connection);
3243 /* merge both tunnels into the first client */
3244 /* remove the old client from the table. ref before because removing it will
3245 * remove the ref to it. */
3246 g_object_ref (oclient);
3247 g_hash_table_remove (tunnels, tunnelid);
3248 g_mutex_unlock (&tunnels_lock);
3250 opriv = oclient->priv;
3252 if (opriv->watch == NULL)
3255 GST_INFO ("client %p: found previous tunnel %p (old %p, new %p)", client,
3256 oclient, opriv->connection, priv->connection);
3258 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
3259 gst_rtsp_watch_reset (priv->watch);
3260 gst_rtsp_watch_reset (opriv->watch);
3261 g_object_unref (oclient);
3263 /* the old client owns the tunnel now, the new one will be freed */
3264 g_source_destroy ((GSource *) priv->watch);
3266 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
3274 GST_ERROR ("client %p: no tunnelid provided", client);
3279 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
3280 g_object_unref (oclient);
3285 static GstRTSPStatusCode
3286 tunnel_get (GstRTSPWatch * watch, gpointer user_data)
3288 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3290 GST_INFO ("client %p: tunnel get (connection %p)", client,
3291 client->priv->connection);
3293 if (!handle_tunnel (client)) {
3294 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
3297 return GST_RTSP_STS_OK;
3300 static GstRTSPResult
3301 tunnel_post (GstRTSPWatch * watch, gpointer user_data)
3303 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3305 GST_INFO ("client %p: tunnel post (connection %p)", client,
3306 client->priv->connection);
3308 if (!handle_tunnel (client)) {
3309 return GST_RTSP_ERROR;
3315 static GstRTSPResult
3316 tunnel_http_response (GstRTSPWatch * watch, GstRTSPMessage * request,
3317 GstRTSPMessage * response, gpointer user_data)
3319 GstRTSPClientClass *klass;
3321 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3322 klass = GST_RTSP_CLIENT_GET_CLASS (client);
3324 if (klass->tunnel_http_response) {
3325 klass->tunnel_http_response (client, request, response);
3331 static GstRTSPWatchFuncs watch_funcs = {
3340 tunnel_http_response
3344 client_watch_notify (GstRTSPClient * client)
3346 GstRTSPClientPrivate *priv = client->priv;
3348 GST_INFO ("client %p: watch destroyed", client);
3350 g_main_context_unref (priv->watch_context);
3351 priv->watch_context = NULL;
3352 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
3353 g_object_unref (client);
3357 * gst_rtsp_client_attach:
3358 * @client: a #GstRTSPClient
3359 * @context: (allow-none): a #GMainContext
3361 * Attaches @client to @context. When the mainloop for @context is run, the
3362 * client will be dispatched. When @context is %NULL, the default context will be
3365 * This function should be called when the client properties and urls are fully
3366 * configured and the client is ready to start.
3368 * Returns: the ID (greater than 0) for the source within the GMainContext.
3371 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
3373 GstRTSPClientPrivate *priv;
3376 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
3377 priv = client->priv;
3378 g_return_val_if_fail (priv->connection != NULL, 0);
3379 g_return_val_if_fail (priv->watch == NULL, 0);
3381 /* make sure noone will free the context before the watch is destroyed */
3382 priv->watch_context = g_main_context_ref (context);
3384 /* create watch for the connection and attach */
3385 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
3386 g_object_ref (client), (GDestroyNotify) client_watch_notify);
3387 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
3388 (GDestroyNotify) gst_rtsp_watch_unref);
3390 /* FIXME make this configurable. We don't want to do this yet because it will
3391 * be superceeded by a cache object later */
3392 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
3394 GST_INFO ("client %p: attaching to context %p", client, context);
3395 res = gst_rtsp_watch_attach (priv->watch, context);
3401 * gst_rtsp_client_session_filter:
3402 * @client: a #GstRTSPClient
3403 * @func: (scope call) (allow-none): a callback
3404 * @user_data: user data passed to @func
3406 * Call @func for each session managed by @client. The result value of @func
3407 * determines what happens to the session. @func will be called with @client
3408 * locked so no further actions on @client can be performed from @func.
3410 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
3413 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
3415 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
3416 * will also be added with an additional ref to the result #GList of this
3419 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each session.
3421 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
3422 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3423 * element in the #GList should be unreffed before the list is freed.
3426 gst_rtsp_client_session_filter (GstRTSPClient * client,
3427 GstRTSPClientSessionFilterFunc func, gpointer user_data)
3429 GstRTSPClientPrivate *priv;
3430 GList *result, *walk, *next;
3432 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
3434 priv = client->priv;
3438 g_mutex_lock (&priv->lock);
3439 for (walk = priv->sessions; walk; walk = next) {
3440 GstRTSPSession *sess = walk->data;
3441 GstRTSPFilterResult res;
3443 next = g_list_next (walk);
3446 res = func (client, sess, user_data);
3448 res = GST_RTSP_FILTER_REF;
3451 case GST_RTSP_FILTER_REMOVE:
3452 /* stop watching the session and pretent it went away */
3453 client_unwatch_session (client, sess, walk);
3455 case GST_RTSP_FILTER_REF:
3456 result = g_list_prepend (result, g_object_ref (sess));
3458 case GST_RTSP_FILTER_KEEP:
3463 g_mutex_unlock (&priv->lock);