2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * @short_description: A client connection state
22 * @see_also: #GstRTSPServer, #GstRTSPThreadPool
24 * The client object handles the connection with a client for as long as a TCP
27 * A #GstRTSPClient is created by #GstRTSPServer when a new connection is
28 * accepted and it inherits the #GstRTSPMountPoints, #GstRTSPSessionPool,
29 * #GstRTSPAuth and #GstRTSPThreadPool from the server.
31 * The client connection should be configured with the #GstRTSPConnection using
32 * gst_rtsp_client_set_connection() before it can be attached to a #GMainContext
33 * using gst_rtsp_client_attach(). From then on the client will handle requests
36 * Use gst_rtsp_client_session_filter() to iterate or modify all the
37 * #GstRTSPSession objects managed by the client object.
39 * Last reviewed on 2013-07-11 (1.0.0)
45 #include <gst/sdp/gstmikey.h>
47 #include "rtsp-client.h"
49 #include "rtsp-params.h"
51 #define GST_RTSP_CLIENT_GET_PRIVATE(obj) \
52 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_CLIENT, GstRTSPClientPrivate))
55 * send_lock, lock, tunnels_lock
58 struct _GstRTSPClientPrivate
60 GMutex lock; /* protects everything else */
62 GstRTSPConnection *connection;
64 GMainContext *watch_context;
69 GstRTSPClientSendFunc send_func; /* protected by send_lock */
70 gpointer send_data; /* protected by send_lock */
71 GDestroyNotify send_notify; /* protected by send_lock */
73 GstRTSPSessionPool *session_pool;
74 GstRTSPMountPoints *mount_points;
76 GstRTSPThreadPool *thread_pool;
78 /* used to cache the media in the last requested DESCRIBE so that
79 * we can pick it up in the next SETUP immediately */
86 gboolean drop_backlog;
89 static GMutex tunnels_lock;
90 static GHashTable *tunnels; /* protected by tunnels_lock */
92 #define DEFAULT_SESSION_POOL NULL
93 #define DEFAULT_MOUNT_POINTS NULL
94 #define DEFAULT_DROP_BACKLOG TRUE
109 SIGNAL_OPTIONS_REQUEST,
110 SIGNAL_DESCRIBE_REQUEST,
111 SIGNAL_SETUP_REQUEST,
113 SIGNAL_PAUSE_REQUEST,
114 SIGNAL_TEARDOWN_REQUEST,
115 SIGNAL_SET_PARAMETER_REQUEST,
116 SIGNAL_GET_PARAMETER_REQUEST,
117 SIGNAL_HANDLE_RESPONSE,
122 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
123 #define GST_CAT_DEFAULT rtsp_client_debug
125 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
127 static void gst_rtsp_client_get_property (GObject * object, guint propid,
128 GValue * value, GParamSpec * pspec);
129 static void gst_rtsp_client_set_property (GObject * object, guint propid,
130 const GValue * value, GParamSpec * pspec);
131 static void gst_rtsp_client_finalize (GObject * obj);
133 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
134 static void client_session_finalized (GstRTSPClient * client,
135 GstRTSPSession * session);
136 static void unlink_session_transports (GstRTSPClient * client,
137 GstRTSPSession * session, GstRTSPSessionMedia * sessmedia);
138 static gboolean default_configure_client_media (GstRTSPClient * client,
139 GstRTSPMedia * media, GstRTSPStream * stream, GstRTSPContext * ctx);
140 static gboolean default_configure_client_transport (GstRTSPClient * client,
141 GstRTSPContext * ctx, GstRTSPTransport * ct);
142 static GstRTSPResult default_params_set (GstRTSPClient * client,
143 GstRTSPContext * ctx);
144 static GstRTSPResult default_params_get (GstRTSPClient * client,
145 GstRTSPContext * ctx);
146 static gchar *default_make_path_from_uri (GstRTSPClient * client,
147 const GstRTSPUrl * uri);
149 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
152 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
154 GObjectClass *gobject_class;
156 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
158 gobject_class = G_OBJECT_CLASS (klass);
160 gobject_class->get_property = gst_rtsp_client_get_property;
161 gobject_class->set_property = gst_rtsp_client_set_property;
162 gobject_class->finalize = gst_rtsp_client_finalize;
164 klass->create_sdp = create_sdp;
165 klass->configure_client_media = default_configure_client_media;
166 klass->configure_client_transport = default_configure_client_transport;
167 klass->params_set = default_params_set;
168 klass->params_get = default_params_get;
169 klass->make_path_from_uri = default_make_path_from_uri;
171 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
172 g_param_spec_object ("session-pool", "Session Pool",
173 "The session pool to use for client session",
174 GST_TYPE_RTSP_SESSION_POOL,
175 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
177 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
178 g_param_spec_object ("mount-points", "Mount Points",
179 "The mount points to use for client session",
180 GST_TYPE_RTSP_MOUNT_POINTS,
181 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183 g_object_class_install_property (gobject_class, PROP_DROP_BACKLOG,
184 g_param_spec_boolean ("drop-backlog", "Drop Backlog",
185 "Drop data when the backlog queue is full",
186 DEFAULT_DROP_BACKLOG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188 gst_rtsp_client_signals[SIGNAL_CLOSED] =
189 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
190 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
191 g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
193 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
194 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
195 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
196 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
198 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
199 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
200 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
201 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
202 GST_TYPE_RTSP_CONTEXT);
204 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
205 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
206 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
207 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
208 GST_TYPE_RTSP_CONTEXT);
210 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
211 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
212 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
213 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
214 GST_TYPE_RTSP_CONTEXT);
216 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
217 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
218 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
219 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
220 GST_TYPE_RTSP_CONTEXT);
222 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
223 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
224 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
225 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
226 GST_TYPE_RTSP_CONTEXT);
228 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
229 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
230 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
231 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
232 GST_TYPE_RTSP_CONTEXT);
234 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
235 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
236 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
237 set_parameter_request), NULL, NULL, g_cclosure_marshal_generic,
238 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
240 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
241 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
242 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
243 get_parameter_request), NULL, NULL, g_cclosure_marshal_generic,
244 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
246 gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE] =
247 g_signal_new ("handle-response", G_TYPE_FROM_CLASS (klass),
248 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
249 handle_response), NULL, NULL, g_cclosure_marshal_generic,
250 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
253 * GstRTSPClient::send-message:
254 * @client: The RTSP client
255 * @session: (type GstRtspServer.RTSPSession): The session
256 * @message: (type GstRtsp.RTSPMessage): The message
258 gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE] =
259 g_signal_new ("send-message", G_TYPE_FROM_CLASS (klass),
260 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
261 G_TYPE_NONE, 2, GST_TYPE_RTSP_CONTEXT, G_TYPE_POINTER);
264 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
265 g_mutex_init (&tunnels_lock);
267 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
271 gst_rtsp_client_init (GstRTSPClient * client)
273 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
277 g_mutex_init (&priv->lock);
278 g_mutex_init (&priv->send_lock);
280 priv->drop_backlog = DEFAULT_DROP_BACKLOG;
283 static GstRTSPFilterResult
284 filter_session (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
287 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
289 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
290 unlink_session_transports (client, sess, sessmedia);
292 /* unmanage the media in the session */
293 return GST_RTSP_FILTER_REMOVE;
297 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
299 /* unlink all media managed in this session */
300 gst_rtsp_session_filter (session, filter_session, client);
304 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
306 GstRTSPClientPrivate *priv = client->priv;
309 for (walk = priv->sessions; walk; walk = g_list_next (walk)) {
310 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
312 /* we already know about this session */
313 if (msession == session)
317 GST_INFO ("watching session %p", session);
319 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
321 priv->sessions = g_list_prepend (priv->sessions, session);
325 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session)
327 GstRTSPClientPrivate *priv = client->priv;
329 GST_INFO ("unwatching session %p", session);
331 g_object_weak_unref (G_OBJECT (session),
332 (GWeakNotify) client_session_finalized, client);
333 priv->sessions = g_list_remove (priv->sessions, session);
337 client_cleanup_session (GstRTSPClient * client, GstRTSPSession * session)
339 g_object_weak_unref (G_OBJECT (session),
340 (GWeakNotify) client_session_finalized, client);
341 client_unlink_session (client, session);
345 client_cleanup_sessions (GstRTSPClient * client)
347 GstRTSPClientPrivate *priv = client->priv;
350 /* remove weak-ref from sessions */
351 for (sessions = priv->sessions; sessions; sessions = g_list_next (sessions)) {
352 client_cleanup_session (client, (GstRTSPSession *) sessions->data);
354 g_list_free (priv->sessions);
355 priv->sessions = NULL;
358 /* A client is finalized when the connection is broken */
360 gst_rtsp_client_finalize (GObject * obj)
362 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
363 GstRTSPClientPrivate *priv = client->priv;
365 GST_INFO ("finalize client %p", client);
368 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
369 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
372 g_source_destroy ((GSource *) priv->watch);
374 if (priv->watch_context)
375 g_main_context_unref (priv->watch_context);
377 client_cleanup_sessions (client);
379 if (priv->connection)
380 gst_rtsp_connection_free (priv->connection);
381 if (priv->session_pool)
382 g_object_unref (priv->session_pool);
383 if (priv->mount_points)
384 g_object_unref (priv->mount_points);
386 g_object_unref (priv->auth);
387 if (priv->thread_pool)
388 g_object_unref (priv->thread_pool);
393 gst_rtsp_media_unprepare (priv->media);
394 g_object_unref (priv->media);
397 g_free (priv->server_ip);
398 g_mutex_clear (&priv->lock);
399 g_mutex_clear (&priv->send_lock);
401 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
405 gst_rtsp_client_get_property (GObject * object, guint propid,
406 GValue * value, GParamSpec * pspec)
408 GstRTSPClient *client = GST_RTSP_CLIENT (object);
409 GstRTSPClientPrivate *priv = client->priv;
412 case PROP_SESSION_POOL:
413 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
415 case PROP_MOUNT_POINTS:
416 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
418 case PROP_DROP_BACKLOG:
419 g_value_set_boolean (value, priv->drop_backlog);
422 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
427 gst_rtsp_client_set_property (GObject * object, guint propid,
428 const GValue * value, GParamSpec * pspec)
430 GstRTSPClient *client = GST_RTSP_CLIENT (object);
431 GstRTSPClientPrivate *priv = client->priv;
434 case PROP_SESSION_POOL:
435 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
437 case PROP_MOUNT_POINTS:
438 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
440 case PROP_DROP_BACKLOG:
441 g_mutex_lock (&priv->lock);
442 priv->drop_backlog = g_value_get_boolean (value);
443 g_mutex_unlock (&priv->lock);
446 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
451 * gst_rtsp_client_new:
453 * Create a new #GstRTSPClient instance.
455 * Returns: (transfer full): a new #GstRTSPClient
458 gst_rtsp_client_new (void)
460 GstRTSPClient *result;
462 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
468 send_message (GstRTSPClient * client, GstRTSPContext * ctx,
469 GstRTSPMessage * message, gboolean close)
471 GstRTSPClientPrivate *priv = client->priv;
473 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
474 "GStreamer RTSP server");
476 /* remove any previous header */
477 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
479 /* add the new session header for new session ids */
481 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
482 gst_rtsp_session_get_header (ctx->session));
485 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
486 gst_rtsp_message_dump (message);
490 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
492 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE],
495 g_mutex_lock (&priv->send_lock);
497 priv->send_func (client, message, close, priv->send_data);
498 g_mutex_unlock (&priv->send_lock);
500 gst_rtsp_message_unset (message);
504 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
505 GstRTSPContext * ctx)
507 gst_rtsp_message_init_response (ctx->response, code,
508 gst_rtsp_status_as_text (code), ctx->request);
512 send_message (client, ctx, ctx->response, FALSE);
516 send_option_not_supported_response (GstRTSPClient * client,
517 GstRTSPContext * ctx, const gchar * unsupported_options)
519 GstRTSPStatusCode code = GST_RTSP_STS_OPTION_NOT_SUPPORTED;
521 gst_rtsp_message_init_response (ctx->response, code,
522 gst_rtsp_status_as_text (code), ctx->request);
524 if (unsupported_options != NULL) {
525 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_UNSUPPORTED,
526 unsupported_options);
531 send_message (client, ctx, ctx->response, FALSE);
535 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
537 if (path1 == NULL || path2 == NULL)
540 if (strlen (path1) != len2)
543 if (strncmp (path1, path2, len2))
549 /* this function is called to initially find the media for the DESCRIBE request
550 * but is cached for when the same client (without breaking the connection) is
551 * doing a setup for the exact same url. */
552 static GstRTSPMedia *
553 find_media (GstRTSPClient * client, GstRTSPContext * ctx, gchar * path,
556 GstRTSPClientPrivate *priv = client->priv;
557 GstRTSPMediaFactory *factory;
561 /* find the longest matching factory for the uri first */
562 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
566 ctx->factory = factory;
568 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
569 goto no_factory_access;
571 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
577 path_len = strlen (path);
579 if (!paths_are_equal (priv->path, path, path_len)) {
580 GstRTSPThread *thread;
582 /* remove any previously cached values before we try to construct a new
588 gst_rtsp_media_unprepare (priv->media);
589 g_object_unref (priv->media);
593 /* prepare the media and add it to the pipeline */
594 if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
599 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
600 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
604 /* prepare the media */
605 if (!(gst_rtsp_media_prepare (media, thread)))
608 /* now keep track of the uri and the media */
609 priv->path = g_strndup (path, path_len);
612 /* we have seen this path before, used cached media */
615 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
618 g_object_unref (factory);
622 g_object_ref (media);
629 GST_ERROR ("client %p: no factory for path %s", client, path);
630 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
635 GST_ERROR ("client %p: not authorized to see factory path %s", client,
637 /* error reply is already sent */
642 GST_ERROR ("client %p: not authorized for factory path %s", client, path);
643 /* error reply is already sent */
648 GST_ERROR ("client %p: can't create media", client);
649 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
650 g_object_unref (factory);
656 GST_ERROR ("client %p: can't create thread", client);
657 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
658 g_object_unref (media);
660 g_object_unref (factory);
666 GST_ERROR ("client %p: can't prepare media", client);
667 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
668 g_object_unref (media);
670 g_object_unref (factory);
677 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
679 GstRTSPClientPrivate *priv = client->priv;
680 GstRTSPMessage message = { 0 };
685 gst_rtsp_message_init_data (&message, channel);
687 /* FIXME, need some sort of iovec RTSPMessage here */
688 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
691 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
693 g_mutex_lock (&priv->send_lock);
695 priv->send_func (client, &message, FALSE, priv->send_data);
696 g_mutex_unlock (&priv->send_lock);
698 gst_rtsp_message_steal_body (&message, &data, &usize);
699 gst_buffer_unmap (buffer, &map_info);
701 gst_rtsp_message_unset (&message);
707 link_transport (GstRTSPClient * client, GstRTSPSession * session,
708 GstRTSPStreamTransport * trans)
710 GstRTSPClientPrivate *priv = client->priv;
712 GST_DEBUG ("client %p: linking transport %p", client, trans);
714 gst_rtsp_stream_transport_set_callbacks (trans,
715 (GstRTSPSendFunc) do_send_data,
716 (GstRTSPSendFunc) do_send_data, client, NULL);
718 priv->transports = g_list_prepend (priv->transports, trans);
720 /* make sure our session can't expire */
721 gst_rtsp_session_prevent_expire (session);
725 link_session_transports (GstRTSPClient * client, GstRTSPSession * session,
726 GstRTSPSessionMedia * sessmedia)
731 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
732 for (i = 0; i < n_streams; i++) {
733 GstRTSPStreamTransport *trans;
734 const GstRTSPTransport *tr;
736 /* get the transport, if there is no transport configured, skip this stream */
737 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
741 tr = gst_rtsp_stream_transport_get_transport (trans);
743 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
744 /* for TCP, link the stream to the TCP connection of the client */
745 link_transport (client, session, trans);
751 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
752 GstRTSPStreamTransport * trans)
754 GstRTSPClientPrivate *priv = client->priv;
756 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
758 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
760 priv->transports = g_list_remove (priv->transports, trans);
762 /* our session can now expire */
763 gst_rtsp_session_allow_expire (session);
767 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
768 GstRTSPSessionMedia * sessmedia)
773 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
774 for (i = 0; i < n_streams; i++) {
775 GstRTSPStreamTransport *trans;
776 const GstRTSPTransport *tr;
778 /* get the transport, if there is no transport configured, skip this stream */
779 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
783 tr = gst_rtsp_stream_transport_get_transport (trans);
785 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
786 /* for TCP, unlink the stream from the TCP connection of the client */
787 unlink_transport (client, session, trans);
793 close_connection (GstRTSPClient * client)
795 GstRTSPClientPrivate *priv = client->priv;
796 const gchar *tunnelid;
798 GST_DEBUG ("client %p: closing connection", client);
800 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
801 g_mutex_lock (&tunnels_lock);
802 /* remove from tunnelids */
803 g_hash_table_remove (tunnels, tunnelid);
804 g_mutex_unlock (&tunnels_lock);
807 gst_rtsp_connection_close (priv->connection);
809 /* connection is now closed, destroy the watch which will also cause the
810 * closed signal to be emitted */
812 GST_DEBUG ("client %p: destroying watch", client);
813 g_source_destroy ((GSource *) priv->watch);
815 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
820 default_make_path_from_uri (GstRTSPClient * client, const GstRTSPUrl * uri)
825 path = g_strconcat (uri->abspath, "?", uri->query, NULL);
827 path = g_strdup (uri->abspath);
833 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
835 GstRTSPClientPrivate *priv = client->priv;
836 GstRTSPClientClass *klass;
837 GstRTSPSession *session;
838 GstRTSPSessionMedia *sessmedia;
839 GstRTSPStatusCode code;
846 session = ctx->session;
851 klass = GST_RTSP_CLIENT_GET_CLASS (client);
852 path = klass->make_path_from_uri (client, ctx->uri);
854 /* get a handle to the configuration of the media in the session */
855 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
859 /* only aggregate control for now.. */
860 if (path[matched] != '\0')
865 ctx->sessmedia = sessmedia;
867 /* we emit the signal before closing the connection */
868 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
871 /* make sure we unblock the backlog and don't accept new messages
873 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
875 /* unlink the all TCP callbacks */
876 unlink_session_transports (client, session, sessmedia);
878 /* remove the session from the watched sessions */
879 client_unwatch_session (client, session);
881 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
883 /* allow messages again so that we can send the reply */
884 gst_rtsp_watch_set_flushing (priv->watch, FALSE);
886 /* unmanage the media in the session, returns false if all media session
888 if (!gst_rtsp_session_release_media (session, sessmedia)) {
889 /* remove the session */
890 gst_rtsp_session_pool_remove (priv->session_pool, session);
892 /* construct the response now */
893 code = GST_RTSP_STS_OK;
894 gst_rtsp_message_init_response (ctx->response, code,
895 gst_rtsp_status_as_text (code), ctx->request);
897 send_message (client, ctx, ctx->response, TRUE);
904 GST_ERROR ("client %p: no session", client);
905 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
910 GST_ERROR ("client %p: no uri supplied", client);
911 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
916 GST_ERROR ("client %p: no media for uri", client);
917 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
923 GST_ERROR ("client %p: no aggregate path %s", client, path);
924 send_generic_response (client,
925 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
932 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
936 res = gst_rtsp_params_set (client, ctx);
942 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
946 res = gst_rtsp_params_get (client, ctx);
952 handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
958 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
959 if (res != GST_RTSP_OK)
963 /* no body, keep-alive request */
964 send_generic_response (client, GST_RTSP_STS_OK, ctx);
966 /* there is a body, handle the params */
967 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
968 if (res != GST_RTSP_OK)
971 send_message (client, ctx, ctx->response, FALSE);
974 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
982 GST_ERROR ("client %p: bad request", client);
983 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
989 handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
995 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
996 if (res != GST_RTSP_OK)
1000 /* no body, keep-alive request */
1001 send_generic_response (client, GST_RTSP_STS_OK, ctx);
1003 /* there is a body, handle the params */
1004 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
1005 if (res != GST_RTSP_OK)
1008 send_message (client, ctx, ctx->response, FALSE);
1011 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
1019 GST_ERROR ("client %p: bad request", client);
1020 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1026 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
1028 GstRTSPSession *session;
1029 GstRTSPClientClass *klass;
1030 GstRTSPSessionMedia *sessmedia;
1031 GstRTSPStatusCode code;
1032 GstRTSPState rtspstate;
1036 if (!(session = ctx->session))
1042 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1043 path = klass->make_path_from_uri (client, ctx->uri);
1045 /* get a handle to the configuration of the media in the session */
1046 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1050 if (path[matched] != '\0')
1055 ctx->sessmedia = sessmedia;
1057 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1058 /* the session state must be playing or recording */
1059 if (rtspstate != GST_RTSP_STATE_PLAYING &&
1060 rtspstate != GST_RTSP_STATE_RECORDING)
1063 /* unlink the all TCP callbacks */
1064 unlink_session_transports (client, session, sessmedia);
1066 /* then pause sending */
1067 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
1069 /* construct the response now */
1070 code = GST_RTSP_STS_OK;
1071 gst_rtsp_message_init_response (ctx->response, code,
1072 gst_rtsp_status_as_text (code), ctx->request);
1074 send_message (client, ctx, ctx->response, FALSE);
1076 /* the state is now READY */
1077 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1079 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
1086 GST_ERROR ("client %p: no seesion", client);
1087 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1092 GST_ERROR ("client %p: no uri supplied", client);
1093 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1098 GST_ERROR ("client %p: no media for uri", client);
1099 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1105 GST_ERROR ("client %p: no aggregate path %s", client, path);
1106 send_generic_response (client,
1107 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1113 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
1114 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1120 /* convert @url and @path to a URL used as a content base for the factory
1121 * located at @path */
1123 make_base_url (GstRTSPClient * client, GstRTSPUrl * url, const gchar * path)
1129 /* check for trailing '/' and append one */
1130 trail = (path[strlen (path) - 1] != '/' ? "/" : "");
1135 tmp.abspath = g_strdup_printf ("%s%s", path, trail);
1137 result = gst_rtsp_url_get_request_uri (&tmp);
1138 g_free (tmp.abspath);
1144 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
1146 GstRTSPSession *session;
1147 GstRTSPClientClass *klass;
1148 GstRTSPSessionMedia *sessmedia;
1149 GstRTSPMedia *media;
1150 GstRTSPStatusCode code;
1153 GstRTSPTimeRange *range;
1155 GstRTSPState rtspstate;
1156 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1157 gchar *path, *rtpinfo;
1160 if (!(session = ctx->session))
1163 if (!(uri = ctx->uri))
1166 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1167 path = klass->make_path_from_uri (client, uri);
1169 /* get a handle to the configuration of the media in the session */
1170 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1174 if (path[matched] != '\0')
1179 ctx->sessmedia = sessmedia;
1180 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1182 /* the session state must be playing or ready */
1183 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1184 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1187 /* in play we first unsuspend, media could be suspended from SDP or PAUSED */
1188 if (!gst_rtsp_media_unsuspend (media))
1189 goto unsuspend_failed;
1191 /* parse the range header if we have one */
1192 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1193 if (res == GST_RTSP_OK) {
1194 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1195 /* we have a range, seek to the position */
1197 gst_rtsp_media_seek (media, range);
1198 gst_rtsp_range_free (range);
1202 /* link the all TCP callbacks */
1203 link_session_transports (client, session, sessmedia);
1205 /* grab RTPInfo from the media now */
1206 rtpinfo = gst_rtsp_session_media_get_rtpinfo (sessmedia);
1208 /* construct the response now */
1209 code = GST_RTSP_STS_OK;
1210 gst_rtsp_message_init_response (ctx->response, code,
1211 gst_rtsp_status_as_text (code), ctx->request);
1213 /* add the RTP-Info header */
1215 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO,
1219 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1221 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1223 send_message (client, ctx, ctx->response, FALSE);
1225 /* start playing after sending the response */
1226 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1228 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1230 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1237 GST_ERROR ("client %p: no session", client);
1238 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1243 GST_ERROR ("client %p: no uri supplied", client);
1244 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1249 GST_ERROR ("client %p: media not found", client);
1250 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1255 GST_ERROR ("client %p: no aggregate path %s", client, path);
1256 send_generic_response (client,
1257 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1263 GST_ERROR ("client %p: not PLAYING or READY", client);
1264 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1270 GST_ERROR ("client %p: unsuspend failed", client);
1271 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1277 do_keepalive (GstRTSPSession * session)
1279 GST_INFO ("keep session %p alive", session);
1280 gst_rtsp_session_touch (session);
1283 /* parse @transport and return a valid transport in @tr. only transports
1284 * supported by @stream are returned. Returns FALSE if no valid transport
1287 parse_transport (const char *transport, GstRTSPStream * stream,
1288 GstRTSPTransport * tr)
1295 gst_rtsp_transport_init (tr);
1297 GST_DEBUG ("parsing transports %s", transport);
1299 transports = g_strsplit (transport, ",", 0);
1301 /* loop through the transports, try to parse */
1302 for (i = 0; transports[i]; i++) {
1303 res = gst_rtsp_transport_parse (transports[i], tr);
1304 if (res != GST_RTSP_OK) {
1305 /* no valid transport, search some more */
1306 GST_WARNING ("could not parse transport %s", transports[i]);
1310 /* we have a transport, see if it's supported */
1311 if (!gst_rtsp_stream_is_transport_supported (stream, tr)) {
1312 GST_WARNING ("unsupported transport %s", transports[i]);
1316 /* we have a valid transport */
1317 GST_INFO ("found valid transport %s", transports[i]);
1322 gst_rtsp_transport_init (tr);
1324 g_strfreev (transports);
1330 default_configure_client_media (GstRTSPClient * client, GstRTSPMedia * media,
1331 GstRTSPStream * stream, GstRTSPContext * ctx)
1333 GstRTSPMessage *request = ctx->request;
1334 gchar *blocksize_str;
1336 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1337 &blocksize_str, 0) == GST_RTSP_OK) {
1341 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1342 if (end == blocksize_str)
1345 /* we don't want to change the mtu when this media
1346 * can be shared because it impacts other clients */
1347 if (gst_rtsp_media_is_shared (media))
1350 if (blocksize > G_MAXUINT)
1351 blocksize = G_MAXUINT;
1353 gst_rtsp_stream_set_mtu (stream, blocksize);
1361 GST_ERROR_OBJECT (client, "failed to parse blocksize");
1362 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1368 default_configure_client_transport (GstRTSPClient * client,
1369 GstRTSPContext * ctx, GstRTSPTransport * ct)
1371 GstRTSPClientPrivate *priv = client->priv;
1373 /* we have a valid transport now, set the destination of the client. */
1374 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1375 gboolean use_client_settings;
1377 use_client_settings =
1378 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1380 if (ct->destination && use_client_settings) {
1381 GstRTSPAddress *addr;
1383 addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1384 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1389 gst_rtsp_address_free (addr);
1391 GstRTSPAddress *addr;
1392 GSocketFamily family;
1394 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1396 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1400 g_free (ct->destination);
1401 ct->destination = g_strdup (addr->address);
1402 ct->port.min = addr->port;
1403 ct->port.max = addr->port + addr->n_ports - 1;
1404 ct->ttl = addr->ttl;
1406 gst_rtsp_address_free (addr);
1411 url = gst_rtsp_connection_get_url (priv->connection);
1412 g_free (ct->destination);
1413 ct->destination = g_strdup (url->host);
1415 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1417 GSocketAddress *addr;
1419 sock = gst_rtsp_connection_get_read_socket (priv->connection);
1420 if ((addr = g_socket_get_remote_address (sock, NULL))) {
1421 /* our read port is the sender port of client */
1422 ct->client_port.min =
1423 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1424 g_object_unref (addr);
1426 if ((addr = g_socket_get_local_address (sock, NULL))) {
1427 ct->server_port.max =
1428 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1429 g_object_unref (addr);
1431 sock = gst_rtsp_connection_get_write_socket (priv->connection);
1432 if ((addr = g_socket_get_remote_address (sock, NULL))) {
1433 /* our write port is the receiver port of client */
1434 ct->client_port.max =
1435 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1436 g_object_unref (addr);
1438 if ((addr = g_socket_get_local_address (sock, NULL))) {
1439 ct->server_port.min =
1440 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1441 g_object_unref (addr);
1443 /* check if the client selected channels for TCP */
1444 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1445 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1455 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1460 static GstRTSPTransport *
1461 make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
1462 GstRTSPTransport * ct)
1464 GstRTSPTransport *st;
1466 GSocketFamily family;
1468 /* prepare the server transport */
1469 gst_rtsp_transport_new (&st);
1471 st->trans = ct->trans;
1472 st->profile = ct->profile;
1473 st->lower_transport = ct->lower_transport;
1475 addr = g_inet_address_new_from_string (ct->destination);
1478 GST_ERROR ("failed to get inet addr from client destination");
1479 family = G_SOCKET_FAMILY_IPV4;
1481 family = g_inet_address_get_family (addr);
1482 g_object_unref (addr);
1486 switch (st->lower_transport) {
1487 case GST_RTSP_LOWER_TRANS_UDP:
1488 st->client_port = ct->client_port;
1489 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
1491 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1492 st->port = ct->port;
1493 st->destination = g_strdup (ct->destination);
1496 case GST_RTSP_LOWER_TRANS_TCP:
1497 st->interleaved = ct->interleaved;
1498 st->client_port = ct->client_port;
1499 st->server_port = ct->server_port;
1504 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
1509 #define AES_128_KEY_LEN 16
1510 #define AES_256_KEY_LEN 32
1512 #define HMAC_32_KEY_LEN 4
1513 #define HMAC_80_KEY_LEN 10
1516 mikey_apply_policy (GstCaps * caps, GstMIKEYMessage * msg, guint8 policy)
1518 const gchar *srtp_cipher;
1519 const gchar *srtp_auth;
1520 const GstMIKEYPayload *sp;
1523 /* loop over Security policy until we find one containing policy */
1525 if ((sp = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, i)) == NULL)
1528 if (((GstMIKEYPayloadSP *) sp)->policy == policy)
1532 /* the default ciphers */
1533 srtp_cipher = "aes-128-icm";
1534 srtp_auth = "hmac-sha1-80";
1536 /* now override the defaults with what is in the Security Policy */
1540 /* collect all the params and go over them */
1541 len = gst_mikey_payload_sp_get_n_params (sp);
1542 for (i = 0; i < len; i++) {
1543 const GstMIKEYPayloadSPParam *param =
1544 gst_mikey_payload_sp_get_param (sp, i);
1546 switch (param->type) {
1547 case GST_MIKEY_SP_SRTP_ENC_ALG:
1548 switch (param->val[0]) {
1550 srtp_cipher = "null";
1554 srtp_cipher = "aes-128-icm";
1560 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
1561 switch (param->val[0]) {
1562 case AES_128_KEY_LEN:
1563 srtp_cipher = "aes-128-icm";
1565 case AES_256_KEY_LEN:
1566 srtp_cipher = "aes-256-icm";
1572 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1573 switch (param->val[0]) {
1579 srtp_auth = "hmac-sha1-80";
1585 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
1586 switch (param->val[0]) {
1587 case HMAC_32_KEY_LEN:
1588 srtp_auth = "hmac-sha1-32";
1590 case HMAC_80_KEY_LEN:
1591 srtp_auth = "hmac-sha1-80";
1597 case GST_MIKEY_SP_SRTP_SRTP_ENC:
1599 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
1606 /* now configure the SRTP parameters */
1607 gst_caps_set_simple (caps,
1608 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
1609 "srtp-auth", G_TYPE_STRING, srtp_auth,
1610 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
1611 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
1617 handle_mikey_data (GstRTSPClient * client, GstRTSPContext * ctx,
1618 guint8 * data, gsize size)
1620 GstMIKEYMessage *msg;
1622 GstCaps *caps = NULL;
1623 GstMIKEYPayloadKEMAC *kemac;
1624 const GstMIKEYPayloadKeyData *pkd;
1627 /* the MIKEY message contains a CSB or crypto session bundle. It is a
1628 * set of Crypto Sessions protected with the same master key.
1629 * In the context of SRTP, an RTP and its RTCP stream is part of a
1631 if ((msg = gst_mikey_message_new_from_data (data, size, NULL, NULL)) == NULL)
1634 /* we can only handle SRTP crypto sessions for now */
1635 if (msg->map_type != GST_MIKEY_MAP_TYPE_SRTP)
1636 goto invalid_map_type;
1638 /* get the number of crypto sessions. This maps SSRC to its
1639 * security parameters */
1640 n_cs = gst_mikey_message_get_n_cs (msg);
1642 goto no_crypto_sessions;
1644 /* we also need keys */
1645 if (!(kemac = (GstMIKEYPayloadKEMAC *) gst_mikey_message_find_payload
1646 (msg, GST_MIKEY_PT_KEMAC, 0)))
1649 /* we don't support encrypted keys */
1650 if (kemac->enc_alg != GST_MIKEY_ENC_NULL
1651 || kemac->mac_alg != GST_MIKEY_MAC_NULL)
1652 goto unsupported_encryption;
1654 /* get Key data sub-payload */
1655 pkd = (const GstMIKEYPayloadKeyData *)
1656 gst_mikey_payload_kemac_get_sub (&kemac->pt, 0);
1659 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
1662 /* go over all crypto sessions and create the security policy for each
1664 for (i = 0; i < n_cs; i++) {
1665 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
1667 caps = gst_caps_new_simple ("application/x-srtp",
1668 "ssrc", G_TYPE_UINT, map->ssrc,
1669 "roc", G_TYPE_UINT, map->roc, "srtp-key", GST_TYPE_BUFFER, key, NULL);
1670 mikey_apply_policy (caps, msg, map->policy);
1672 gst_rtsp_stream_update_crypto (ctx->stream, map->ssrc, caps);
1673 gst_caps_unref (caps);
1675 gst_mikey_message_free (msg);
1682 GST_DEBUG_OBJECT (client, "failed to parse MIKEY message");
1687 GST_DEBUG_OBJECT (client, "invalid map type %d", msg->map_type);
1688 goto cleanup_message;
1692 GST_DEBUG_OBJECT (client, "no crypto sessions");
1693 goto cleanup_message;
1697 GST_DEBUG_OBJECT (client, "no keys found");
1698 goto cleanup_message;
1700 unsupported_encryption:
1702 GST_DEBUG_OBJECT (client, "unsupported key encryption");
1703 goto cleanup_message;
1707 gst_mikey_message_free (msg);
1712 #define IS_STRIP_CHAR(c) (g_ascii_isspace ((guchar)(c)) || ((c) == '\"'))
1715 strip_chars (gchar * str)
1722 if (!IS_STRIP_CHAR (str[len]))
1726 for (s = str; *s && IS_STRIP_CHAR (*s); s++);
1727 memmove (str, s, len + 1);
1730 /* KeyMgmt = "KeyMgmt" ":" key-mgmt-spec 0*("," key-mgmt-spec)
1731 * key-mgmt-spec = "prot" "=" KMPID ";" ["uri" "=" %x22 URI %x22 ";"]
1734 handle_keymgmt (GstRTSPClient * client, GstRTSPContext * ctx, gchar * keymgmt)
1739 specs = g_strsplit (keymgmt, ",", 0);
1740 for (i = 0; specs[i]; i++) {
1743 split = g_strsplit (specs[i], ";", 0);
1744 for (j = 0; split[j]; j++) {
1745 g_strstrip (split[j]);
1746 if (g_str_has_prefix (split[j], "prot=")) {
1747 g_strstrip (split[j] + 5);
1748 if (!g_str_equal (split[j] + 5, "mikey"))
1750 GST_DEBUG ("found mikey");
1751 } else if (g_str_has_prefix (split[j], "uri=")) {
1752 strip_chars (split[j] + 4);
1753 GST_DEBUG ("found uri '%s'", split[j] + 4);
1754 } else if (g_str_has_prefix (split[j], "data=")) {
1757 strip_chars (split[j] + 5);
1758 GST_DEBUG ("found data '%s'", split[j] + 5);
1759 data = g_base64_decode_inplace (split[j] + 5, &size);
1760 handle_mikey_data (client, ctx, data, size);
1768 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
1770 GstRTSPClientPrivate *priv = client->priv;
1773 gchar *transport, *keymgmt;
1774 GstRTSPTransport *ct, *st;
1775 GstRTSPStatusCode code;
1776 GstRTSPSession *session;
1777 GstRTSPStreamTransport *trans;
1779 GstRTSPSessionMedia *sessmedia;
1780 GstRTSPMedia *media;
1781 GstRTSPStream *stream;
1782 GstRTSPState rtspstate;
1783 GstRTSPClientClass *klass;
1784 gchar *path, *control;
1791 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1792 path = klass->make_path_from_uri (client, uri);
1794 /* parse the transport */
1796 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
1798 if (res != GST_RTSP_OK)
1801 /* we create the session after parsing stuff so that we don't make
1802 * a session for malformed requests */
1803 if (priv->session_pool == NULL)
1806 session = ctx->session;
1809 g_object_ref (session);
1810 /* get a handle to the configuration of the media in the session, this can
1811 * return NULL if this is a new url to manage in this session. */
1812 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1814 /* we need a new media configuration in this session */
1818 /* we have no session media, find one and manage it */
1819 if (sessmedia == NULL) {
1820 /* get a handle to the configuration of the media in the session */
1821 media = find_media (client, ctx, path, &matched);
1823 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1824 g_object_ref (media);
1826 goto media_not_found;
1828 /* no media, not found then */
1830 goto media_not_found_no_reply;
1832 if (path[matched] == '\0')
1833 goto control_not_found;
1835 /* path is what matched. */
1836 path[matched] = '\0';
1837 /* control is remainder */
1838 control = &path[matched + 1];
1840 /* find the stream now using the control part */
1841 stream = gst_rtsp_media_find_stream (media, control);
1843 goto stream_not_found;
1845 /* now we have a uri identifying a valid media and stream */
1846 ctx->stream = stream;
1849 if (session == NULL) {
1850 /* create a session if this fails we probably reached our session limit or
1852 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1853 goto service_unavailable;
1855 /* make sure this client is closed when the session is closed */
1856 client_watch_session (client, session);
1858 /* signal new session */
1859 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1862 ctx->session = session;
1865 if (sessmedia == NULL) {
1866 /* manage the media in our session now, if not done already */
1867 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1868 /* if we stil have no media, error */
1869 if (sessmedia == NULL)
1870 goto sessmedia_unavailable;
1872 g_object_unref (media);
1875 ctx->sessmedia = sessmedia;
1877 if (!klass->configure_client_media (client, media, stream, ctx))
1878 goto configure_media_failed_no_reply;
1880 gst_rtsp_transport_new (&ct);
1882 /* parse and find a usable supported transport */
1883 if (!parse_transport (transport, stream, ct))
1884 goto unsupported_transports;
1886 /* update the client transport */
1887 if (!klass->configure_client_transport (client, ctx, ct))
1888 goto unsupported_client_transport;
1890 /* parse the keymgmt */
1891 if (gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_KEYMGMT,
1892 &keymgmt, 0) == GST_RTSP_OK) {
1893 if (!handle_keymgmt (client, ctx, keymgmt))
1897 /* set in the session media transport */
1898 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1900 /* configure the url used to set this transport, this we will use when
1901 * generating the response for the PLAY request */
1902 gst_rtsp_stream_transport_set_url (trans, uri);
1904 /* configure keepalive for this transport */
1905 gst_rtsp_stream_transport_set_keepalive (trans,
1906 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1908 /* create and serialize the server transport */
1909 st = make_server_transport (client, ctx, ct);
1910 trans_str = gst_rtsp_transport_as_text (st);
1911 gst_rtsp_transport_free (st);
1913 /* construct the response now */
1914 code = GST_RTSP_STS_OK;
1915 gst_rtsp_message_init_response (ctx->response, code,
1916 gst_rtsp_status_as_text (code), ctx->request);
1918 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
1922 send_message (client, ctx, ctx->response, FALSE);
1924 /* update the state */
1925 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1926 switch (rtspstate) {
1927 case GST_RTSP_STATE_PLAYING:
1928 case GST_RTSP_STATE_RECORDING:
1929 case GST_RTSP_STATE_READY:
1930 /* no state change */
1933 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1936 g_object_unref (session);
1939 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
1946 GST_ERROR ("client %p: no uri", client);
1947 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1952 GST_ERROR ("client %p: no transport", client);
1953 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1958 GST_ERROR ("client %p: no session pool configured", client);
1959 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1962 media_not_found_no_reply:
1964 GST_ERROR ("client %p: media '%s' not found", client, path);
1965 /* error reply is already sent */
1970 GST_ERROR ("client %p: media '%s' not found", client, path);
1971 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1976 GST_ERROR ("client %p: no control in path '%s'", client, path);
1977 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1978 g_object_unref (media);
1983 GST_ERROR ("client %p: stream '%s' not found", client, control);
1984 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1985 g_object_unref (media);
1988 service_unavailable:
1990 GST_ERROR ("client %p: can't create session", client);
1991 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1992 g_object_unref (media);
1995 sessmedia_unavailable:
1997 GST_ERROR ("client %p: can't create session media", client);
1998 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1999 g_object_unref (media);
2000 goto cleanup_session;
2002 configure_media_failed_no_reply:
2004 GST_ERROR ("client %p: configure_media failed", client);
2005 /* error reply is already sent */
2006 goto cleanup_session;
2008 unsupported_transports:
2010 GST_ERROR ("client %p: unsupported transports", client);
2011 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
2012 goto cleanup_transport;
2014 unsupported_client_transport:
2016 GST_ERROR ("client %p: unsupported client transport", client);
2017 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
2018 goto cleanup_transport;
2022 GST_ERROR ("client %p: keymgmt error", client);
2023 send_generic_response (client, GST_RTSP_STS_KEY_MANAGEMENT_FAILURE, ctx);
2024 goto cleanup_transport;
2028 gst_rtsp_transport_free (ct);
2030 g_object_unref (session);
2037 static GstSDPMessage *
2038 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
2040 GstRTSPClientPrivate *priv = client->priv;
2045 gst_sdp_message_new (&sdp);
2047 /* some standard things first */
2048 gst_sdp_message_set_version (sdp, "0");
2055 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
2058 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
2059 gst_sdp_message_set_information (sdp, "rtsp-server");
2060 gst_sdp_message_add_time (sdp, "0", "0", NULL);
2061 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
2062 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
2063 gst_sdp_message_add_attribute (sdp, "control", "*");
2065 info.is_ipv6 = priv->is_ipv6;
2066 info.server_ip = priv->server_ip;
2068 /* create an SDP for the media object */
2069 if (!gst_rtsp_media_setup_sdp (media, sdp, &info))
2077 GST_ERROR ("client %p: could not create SDP", client);
2078 gst_sdp_message_free (sdp);
2083 /* for the describe we must generate an SDP */
2085 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
2087 GstRTSPClientPrivate *priv = client->priv;
2092 GstRTSPMedia *media;
2093 GstRTSPClientClass *klass;
2095 klass = GST_RTSP_CLIENT_GET_CLASS (client);
2100 /* check what kind of format is accepted, we don't really do anything with it
2101 * and always return SDP for now. */
2106 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
2108 if (res == GST_RTSP_ENOTIMPL)
2111 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
2115 if (!priv->mount_points)
2116 goto no_mount_points;
2118 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
2121 /* find the media object for the uri */
2122 if (!(media = find_media (client, ctx, path, NULL)))
2125 /* create an SDP for the media object on this client */
2126 if (!(sdp = klass->create_sdp (client, media)))
2129 /* we suspend after the describe */
2130 gst_rtsp_media_suspend (media);
2131 g_object_unref (media);
2133 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2134 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2136 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
2139 /* content base for some clients that might screw up creating the setup uri */
2140 str = make_base_url (client, ctx->uri, path);
2143 GST_INFO ("adding content-base: %s", str);
2144 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE, str);
2146 /* add SDP to the response body */
2147 str = gst_sdp_message_as_text (sdp);
2148 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
2149 gst_sdp_message_free (sdp);
2151 send_message (client, ctx, ctx->response, FALSE);
2153 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
2161 GST_ERROR ("client %p: no uri", client);
2162 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2167 GST_ERROR ("client %p: no mount points configured", client);
2168 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2173 GST_ERROR ("client %p: can't find path for url", client);
2174 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2179 GST_ERROR ("client %p: no media", client);
2181 /* error reply is already sent */
2186 GST_ERROR ("client %p: can't create SDP", client);
2187 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2189 g_object_unref (media);
2195 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
2197 GstRTSPMethod options;
2200 options = GST_RTSP_DESCRIBE |
2205 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
2207 str = gst_rtsp_options_as_text (options);
2209 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2210 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2212 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
2215 send_message (client, ctx, ctx->response, FALSE);
2217 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
2223 /* remove duplicate and trailing '/' */
2225 sanitize_uri (GstRTSPUrl * uri)
2229 gboolean have_slash, prev_slash;
2231 s = d = uri->abspath;
2232 len = strlen (uri->abspath);
2236 for (i = 0; i < len; i++) {
2237 have_slash = s[i] == '/';
2239 if (!have_slash || !prev_slash)
2241 prev_slash = have_slash;
2243 len = d - uri->abspath;
2244 /* don't remove the first slash if that's the only thing left */
2245 if (len > 1 && *(d - 1) == '/')
2251 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
2253 GstRTSPClientPrivate *priv = client->priv;
2255 GST_INFO ("client %p: session %p finished", client, session);
2257 /* unlink all media managed in this session */
2258 client_unlink_session (client, session);
2260 /* remove the session */
2261 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
2262 GST_INFO ("client %p: all sessions finalized, close the connection",
2264 close_connection (client);
2268 /* Returns TRUE if there are no Require headers, otherwise returns FALSE
2269 * and also returns a newly-allocated string of (comma-separated) unsupported
2270 * options in the unsupported_reqs variable .
2272 * There may be multiple Require headers, but we must send one single
2273 * Unsupported header with all the unsupported options as response. If
2274 * an incoming Require header contained a comma-separated list of options
2275 * GstRtspConnection will already have split that list up into multiple
2278 * TODO: allow the application to decide what features are supported
2281 check_request_requirements (GstRTSPMessage * msg, gchar ** unsupported_reqs)
2284 GPtrArray *arr = NULL;
2290 res = gst_rtsp_message_get_header (msg, GST_RTSP_HDR_REQUIRE, &reqs, i++);
2292 if (res == GST_RTSP_ENOTIMPL)
2296 arr = g_ptr_array_new_with_free_func ((GDestroyNotify) g_free);
2298 g_ptr_array_add (arr, g_strdup (reqs));
2302 /* if we don't have any Require headers at all, all is fine */
2306 /* otherwise we've now processed at all the Require headers */
2307 g_ptr_array_add (arr, NULL);
2309 /* for now we don't commit to supporting anything, so will just report
2310 * all of the required options as unsupported */
2311 *unsupported_reqs = g_strjoinv (", ", (gchar **) arr->pdata);
2313 g_ptr_array_unref (arr);
2318 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
2320 GstRTSPClientPrivate *priv = client->priv;
2321 GstRTSPMethod method;
2322 const gchar *uristr;
2323 GstRTSPUrl *uri = NULL;
2324 GstRTSPVersion version;
2326 GstRTSPSession *session = NULL;
2327 GstRTSPContext sctx = { NULL }, *ctx;
2328 GstRTSPMessage response = { 0 };
2329 gchar *unsupported_reqs = NULL;
2332 if (!(ctx = gst_rtsp_context_get_current ())) {
2334 ctx->auth = priv->auth;
2335 gst_rtsp_context_push_current (ctx);
2338 ctx->conn = priv->connection;
2339 ctx->client = client;
2340 ctx->request = request;
2341 ctx->response = &response;
2343 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2344 gst_rtsp_message_dump (request);
2347 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
2349 GST_INFO ("client %p: received a request %s %s %s", client,
2350 gst_rtsp_method_as_text (method), uristr,
2351 gst_rtsp_version_as_text (version));
2353 /* we can only handle 1.0 requests */
2354 if (version != GST_RTSP_VERSION_1_0)
2357 ctx->method = method;
2359 /* we always try to parse the url first */
2360 if (strcmp (uristr, "*") == 0) {
2361 /* special case where we have * as uri, keep uri = NULL */
2362 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
2363 /* check if the uristr is an absolute path <=> scheme and host information
2367 scheme = g_uri_parse_scheme (uristr);
2368 if (scheme == NULL && g_str_has_prefix (uristr, "/")) {
2369 gchar *absolute_uristr = NULL;
2371 GST_WARNING_OBJECT (client, "request doesn't contain absolute url");
2372 if (priv->server_ip == NULL) {
2373 GST_WARNING_OBJECT (client, "host information missing");
2378 g_strdup_printf ("rtsp://%s%s", priv->server_ip, uristr);
2380 GST_DEBUG_OBJECT (client, "absolute url: %s", absolute_uristr);
2381 if (gst_rtsp_url_parse (absolute_uristr, &uri) != GST_RTSP_OK) {
2382 g_free (absolute_uristr);
2385 g_free (absolute_uristr);
2392 /* get the session if there is any */
2393 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
2394 if (res == GST_RTSP_OK) {
2395 if (priv->session_pool == NULL)
2398 /* we had a session in the request, find it again */
2399 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2400 goto session_not_found;
2402 /* we add the session to the client list of watched sessions. When a session
2403 * disappears because it times out, we will be notified. If all sessions are
2404 * gone, we will close the connection */
2405 client_watch_session (client, session);
2408 /* sanitize the uri */
2412 ctx->session = session;
2414 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
2415 goto not_authorized;
2417 /* handle any 'Require' headers */
2418 if (!check_request_requirements (ctx->request, &unsupported_reqs))
2419 goto unsupported_requirement;
2421 /* now see what is asked and dispatch to a dedicated handler */
2423 case GST_RTSP_OPTIONS:
2424 handle_options_request (client, ctx);
2426 case GST_RTSP_DESCRIBE:
2427 handle_describe_request (client, ctx);
2429 case GST_RTSP_SETUP:
2430 handle_setup_request (client, ctx);
2433 handle_play_request (client, ctx);
2435 case GST_RTSP_PAUSE:
2436 handle_pause_request (client, ctx);
2438 case GST_RTSP_TEARDOWN:
2439 handle_teardown_request (client, ctx);
2441 case GST_RTSP_SET_PARAMETER:
2442 handle_set_param_request (client, ctx);
2444 case GST_RTSP_GET_PARAMETER:
2445 handle_get_param_request (client, ctx);
2447 case GST_RTSP_ANNOUNCE:
2448 case GST_RTSP_RECORD:
2449 case GST_RTSP_REDIRECT:
2450 goto not_implemented;
2451 case GST_RTSP_INVALID:
2458 gst_rtsp_context_pop_current (ctx);
2460 g_object_unref (session);
2462 gst_rtsp_url_free (uri);
2468 GST_ERROR ("client %p: version %d not supported", client, version);
2469 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
2475 GST_ERROR ("client %p: bad request", client);
2476 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2481 GST_ERROR ("client %p: no pool configured", client);
2482 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2487 GST_ERROR ("client %p: session not found", client);
2488 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2493 GST_ERROR ("client %p: not allowed", client);
2494 /* error reply is already sent */
2497 unsupported_requirement:
2499 GST_ERROR ("client %p: Required option is not supported (%s)", client,
2501 send_option_not_supported_response (client, ctx, unsupported_reqs);
2502 g_free (unsupported_reqs);
2507 GST_ERROR ("client %p: method %d not implemented", client, method);
2508 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
2515 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
2517 GstRTSPClientPrivate *priv = client->priv;
2519 GstRTSPSession *session = NULL;
2520 GstRTSPContext sctx = { NULL }, *ctx;
2523 if (!(ctx = gst_rtsp_context_get_current ())) {
2525 ctx->auth = priv->auth;
2526 gst_rtsp_context_push_current (ctx);
2529 ctx->conn = priv->connection;
2530 ctx->client = client;
2531 ctx->request = NULL;
2533 ctx->method = GST_RTSP_INVALID;
2534 ctx->response = response;
2536 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2537 gst_rtsp_message_dump (response);
2540 GST_INFO ("client %p: received a response", client);
2542 /* get the session if there is any */
2544 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
2545 if (res == GST_RTSP_OK) {
2546 if (priv->session_pool == NULL)
2549 /* we had a session in the request, find it again */
2550 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2551 goto session_not_found;
2553 /* we add the session to the client list of watched sessions. When a session
2554 * disappears because it times out, we will be notified. If all sessions are
2555 * gone, we will close the connection */
2556 client_watch_session (client, session);
2559 ctx->session = session;
2561 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
2566 gst_rtsp_context_pop_current (ctx);
2568 g_object_unref (session);
2573 GST_ERROR ("client %p: no pool configured", client);
2578 GST_ERROR ("client %p: session not found", client);
2584 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2586 GstRTSPClientPrivate *priv = client->priv;
2595 /* find the stream for this message */
2596 res = gst_rtsp_message_parse_data (message, &channel);
2597 if (res != GST_RTSP_OK)
2600 gst_rtsp_message_steal_body (message, &data, &size);
2602 buffer = gst_buffer_new_wrapped (data, size);
2605 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2606 GstRTSPStreamTransport *trans;
2607 GstRTSPStream *stream;
2608 const GstRTSPTransport *tr;
2612 tr = gst_rtsp_stream_transport_get_transport (trans);
2613 stream = gst_rtsp_stream_transport_get_stream (trans);
2615 /* check for TCP transport */
2616 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2617 /* dispatch to the stream based on the channel number */
2618 if (tr->interleaved.min == channel) {
2619 gst_rtsp_stream_recv_rtp (stream, buffer);
2622 } else if (tr->interleaved.max == channel) {
2623 gst_rtsp_stream_recv_rtcp (stream, buffer);
2630 gst_buffer_unref (buffer);
2634 * gst_rtsp_client_set_session_pool:
2635 * @client: a #GstRTSPClient
2636 * @pool: (transfer none): a #GstRTSPSessionPool
2638 * Set @pool as the sessionpool for @client which it will use to find
2639 * or allocate sessions. the sessionpool is usually inherited from the server
2640 * that created the client but can be overridden later.
2643 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2644 GstRTSPSessionPool * pool)
2646 GstRTSPSessionPool *old;
2647 GstRTSPClientPrivate *priv;
2649 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2651 priv = client->priv;
2654 g_object_ref (pool);
2656 g_mutex_lock (&priv->lock);
2657 old = priv->session_pool;
2658 priv->session_pool = pool;
2659 g_mutex_unlock (&priv->lock);
2662 g_object_unref (old);
2666 * gst_rtsp_client_get_session_pool:
2667 * @client: a #GstRTSPClient
2669 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2671 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2673 GstRTSPSessionPool *
2674 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2676 GstRTSPClientPrivate *priv;
2677 GstRTSPSessionPool *result;
2679 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2681 priv = client->priv;
2683 g_mutex_lock (&priv->lock);
2684 if ((result = priv->session_pool))
2685 g_object_ref (result);
2686 g_mutex_unlock (&priv->lock);
2692 * gst_rtsp_client_set_mount_points:
2693 * @client: a #GstRTSPClient
2694 * @mounts: (transfer none): a #GstRTSPMountPoints
2696 * Set @mounts as the mount points for @client which it will use to map urls
2697 * to media streams. These mount points are usually inherited from the server that
2698 * created the client but can be overriden later.
2701 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2702 GstRTSPMountPoints * mounts)
2704 GstRTSPClientPrivate *priv;
2705 GstRTSPMountPoints *old;
2707 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2709 priv = client->priv;
2712 g_object_ref (mounts);
2714 g_mutex_lock (&priv->lock);
2715 old = priv->mount_points;
2716 priv->mount_points = mounts;
2717 g_mutex_unlock (&priv->lock);
2720 g_object_unref (old);
2724 * gst_rtsp_client_get_mount_points:
2725 * @client: a #GstRTSPClient
2727 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2729 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2731 GstRTSPMountPoints *
2732 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2734 GstRTSPClientPrivate *priv;
2735 GstRTSPMountPoints *result;
2737 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2739 priv = client->priv;
2741 g_mutex_lock (&priv->lock);
2742 if ((result = priv->mount_points))
2743 g_object_ref (result);
2744 g_mutex_unlock (&priv->lock);
2750 * gst_rtsp_client_set_auth:
2751 * @client: a #GstRTSPClient
2752 * @auth: (transfer none): a #GstRTSPAuth
2754 * configure @auth to be used as the authentication manager of @client.
2757 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2759 GstRTSPClientPrivate *priv;
2762 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2764 priv = client->priv;
2767 g_object_ref (auth);
2769 g_mutex_lock (&priv->lock);
2772 g_mutex_unlock (&priv->lock);
2775 g_object_unref (old);
2780 * gst_rtsp_client_get_auth:
2781 * @client: a #GstRTSPClient
2783 * Get the #GstRTSPAuth used as the authentication manager of @client.
2785 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2789 gst_rtsp_client_get_auth (GstRTSPClient * client)
2791 GstRTSPClientPrivate *priv;
2792 GstRTSPAuth *result;
2794 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2796 priv = client->priv;
2798 g_mutex_lock (&priv->lock);
2799 if ((result = priv->auth))
2800 g_object_ref (result);
2801 g_mutex_unlock (&priv->lock);
2807 * gst_rtsp_client_set_thread_pool:
2808 * @client: a #GstRTSPClient
2809 * @pool: (transfer none): a #GstRTSPThreadPool
2811 * configure @pool to be used as the thread pool of @client.
2814 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2815 GstRTSPThreadPool * pool)
2817 GstRTSPClientPrivate *priv;
2818 GstRTSPThreadPool *old;
2820 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2822 priv = client->priv;
2825 g_object_ref (pool);
2827 g_mutex_lock (&priv->lock);
2828 old = priv->thread_pool;
2829 priv->thread_pool = pool;
2830 g_mutex_unlock (&priv->lock);
2833 g_object_unref (old);
2837 * gst_rtsp_client_get_thread_pool:
2838 * @client: a #GstRTSPClient
2840 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2842 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2846 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2848 GstRTSPClientPrivate *priv;
2849 GstRTSPThreadPool *result;
2851 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2853 priv = client->priv;
2855 g_mutex_lock (&priv->lock);
2856 if ((result = priv->thread_pool))
2857 g_object_ref (result);
2858 g_mutex_unlock (&priv->lock);
2864 * gst_rtsp_client_set_connection:
2865 * @client: a #GstRTSPClient
2866 * @conn: (transfer full): a #GstRTSPConnection
2868 * Set the #GstRTSPConnection of @client. This function takes ownership of
2871 * Returns: %TRUE on success.
2874 gst_rtsp_client_set_connection (GstRTSPClient * client,
2875 GstRTSPConnection * conn)
2877 GstRTSPClientPrivate *priv;
2878 GSocket *read_socket;
2879 GSocketAddress *address;
2881 GError *error = NULL;
2883 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2884 g_return_val_if_fail (conn != NULL, FALSE);
2886 priv = client->priv;
2888 read_socket = gst_rtsp_connection_get_read_socket (conn);
2890 if (!(address = g_socket_get_local_address (read_socket, &error)))
2893 g_free (priv->server_ip);
2894 /* keep the original ip that the client connected to */
2895 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2896 GInetAddress *iaddr;
2898 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2900 /* socket might be ipv6 but adress still ipv4 */
2901 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2902 priv->server_ip = g_inet_address_to_string (iaddr);
2903 g_object_unref (address);
2905 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2906 priv->server_ip = g_strdup ("unknown");
2909 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2910 priv->server_ip, priv->is_ipv6);
2912 url = gst_rtsp_connection_get_url (conn);
2913 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2915 priv->connection = conn;
2922 GST_ERROR ("could not get local address %s", error->message);
2923 g_error_free (error);
2929 * gst_rtsp_client_get_connection:
2930 * @client: a #GstRTSPClient
2932 * Get the #GstRTSPConnection of @client.
2934 * Returns: (transfer none): the #GstRTSPConnection of @client.
2935 * The connection object returned remains valid until the client is freed.
2938 gst_rtsp_client_get_connection (GstRTSPClient * client)
2940 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2942 return client->priv->connection;
2946 * gst_rtsp_client_set_send_func:
2947 * @client: a #GstRTSPClient
2948 * @func: (scope notified): a #GstRTSPClientSendFunc
2949 * @user_data: (closure): user data passed to @func
2950 * @notify: (allow-none): called when @user_data is no longer in use
2952 * Set @func as the callback that will be called when a new message needs to be
2953 * sent to the client. @user_data is passed to @func and @notify is called when
2954 * @user_data is no longer in use.
2956 * By default, the client will send the messages on the #GstRTSPConnection that
2957 * was configured with gst_rtsp_client_attach() was called.
2960 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2961 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2963 GstRTSPClientPrivate *priv;
2964 GDestroyNotify old_notify;
2967 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2969 priv = client->priv;
2971 g_mutex_lock (&priv->send_lock);
2972 priv->send_func = func;
2973 old_notify = priv->send_notify;
2974 old_data = priv->send_data;
2975 priv->send_notify = notify;
2976 priv->send_data = user_data;
2977 g_mutex_unlock (&priv->send_lock);
2980 old_notify (old_data);
2984 * gst_rtsp_client_handle_message:
2985 * @client: a #GstRTSPClient
2986 * @message: (transfer none): an #GstRTSPMessage
2988 * Let the client handle @message.
2990 * Returns: a #GstRTSPResult.
2993 gst_rtsp_client_handle_message (GstRTSPClient * client,
2994 GstRTSPMessage * message)
2996 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2997 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2999 switch (message->type) {
3000 case GST_RTSP_MESSAGE_REQUEST:
3001 handle_request (client, message);
3003 case GST_RTSP_MESSAGE_RESPONSE:
3004 handle_response (client, message);
3006 case GST_RTSP_MESSAGE_DATA:
3007 handle_data (client, message);
3016 * gst_rtsp_client_send_message:
3017 * @client: a #GstRTSPClient
3018 * @session: (allow-none) (transfer none): a #GstRTSPSession to send
3019 * the message to or %NULL
3020 * @message: (transfer none): The #GstRTSPMessage to send
3022 * Send a message message to the remote end. @message must be a
3023 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
3026 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
3027 GstRTSPMessage * message)
3029 GstRTSPContext sctx = { NULL }
3031 GstRTSPClientPrivate *priv;
3033 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
3034 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3035 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
3036 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
3038 priv = client->priv;
3040 if (!(ctx = gst_rtsp_context_get_current ())) {
3042 ctx->auth = priv->auth;
3043 gst_rtsp_context_push_current (ctx);
3046 ctx->conn = priv->connection;
3047 ctx->client = client;
3048 ctx->session = session;
3050 send_message (client, ctx, message, FALSE);
3053 gst_rtsp_context_pop_current (ctx);
3058 static GstRTSPResult
3059 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
3060 gboolean close, gpointer user_data)
3062 GstRTSPClientPrivate *priv = client->priv;
3070 /* send the response and store the seq number so we can wait until it's
3071 * written to the client to close the connection */
3073 gst_rtsp_watch_send_message (priv->watch, message,
3074 close ? &priv->close_seq : NULL);
3075 if (ret == GST_RTSP_OK)
3078 if (ret != GST_RTSP_ENOMEM)
3082 if (priv->drop_backlog)
3085 /* queue was full, wait for more space */
3086 GST_DEBUG_OBJECT (client, "waiting for backlog");
3087 ret = gst_rtsp_watch_wait_backlog (priv->watch, &time);
3088 GST_DEBUG_OBJECT (client, "Resend due to backlog full");
3089 } while (ret != GST_RTSP_EINTR);
3096 GST_DEBUG_OBJECT (client, "got error %d", ret);
3101 static GstRTSPResult
3102 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
3105 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
3108 static GstRTSPResult
3109 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
3111 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3112 GstRTSPClientPrivate *priv = client->priv;
3114 if (priv->close_seq && priv->close_seq == cseq) {
3115 priv->close_seq = 0;
3116 close_connection (client);
3122 static GstRTSPResult
3123 closed (GstRTSPWatch * watch, gpointer user_data)
3125 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3126 GstRTSPClientPrivate *priv = client->priv;
3127 const gchar *tunnelid;
3129 GST_INFO ("client %p: connection closed", client);
3131 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
3132 g_mutex_lock (&tunnels_lock);
3133 /* remove from tunnelids */
3134 g_hash_table_remove (tunnels, tunnelid);
3135 g_mutex_unlock (&tunnels_lock);
3138 gst_rtsp_watch_set_flushing (watch, TRUE);
3139 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
3144 static GstRTSPResult
3145 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
3147 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3150 str = gst_rtsp_strresult (result);
3151 GST_INFO ("client %p: received an error %s", client, str);
3157 static GstRTSPResult
3158 error_full (GstRTSPWatch * watch, GstRTSPResult result,
3159 GstRTSPMessage * message, guint id, gpointer user_data)
3161 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3164 str = gst_rtsp_strresult (result);
3166 ("client %p: error when handling message %p with id %d: %s",
3167 client, message, id, str);
3174 remember_tunnel (GstRTSPClient * client)
3176 GstRTSPClientPrivate *priv = client->priv;
3177 const gchar *tunnelid;
3179 /* store client in the pending tunnels */
3180 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
3181 if (tunnelid == NULL)
3184 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
3186 /* we can't have two clients connecting with the same tunnelid */
3187 g_mutex_lock (&tunnels_lock);
3188 if (g_hash_table_lookup (tunnels, tunnelid))
3189 goto tunnel_existed;
3191 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
3192 g_mutex_unlock (&tunnels_lock);
3199 GST_ERROR ("client %p: no tunnelid provided", client);
3204 g_mutex_unlock (&tunnels_lock);
3205 GST_ERROR ("client %p: tunnel session %s already existed", client,
3211 static GstRTSPResult
3212 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
3214 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3215 GstRTSPClientPrivate *priv = client->priv;
3217 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
3220 /* ignore error, it'll only be a problem when the client does a POST again */
3221 remember_tunnel (client);
3227 handle_tunnel (GstRTSPClient * client)
3229 GstRTSPClientPrivate *priv = client->priv;
3230 GstRTSPClient *oclient;
3231 GstRTSPClientPrivate *opriv;
3232 const gchar *tunnelid;
3234 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
3235 if (tunnelid == NULL)
3238 /* check for previous tunnel */
3239 g_mutex_lock (&tunnels_lock);
3240 oclient = g_hash_table_lookup (tunnels, tunnelid);
3242 if (oclient == NULL) {
3243 /* no previous tunnel, remember tunnel */
3244 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
3245 g_mutex_unlock (&tunnels_lock);
3247 GST_INFO ("client %p: no previous tunnel found, remembering tunnel (%p)",
3248 client, priv->connection);
3250 /* merge both tunnels into the first client */
3251 /* remove the old client from the table. ref before because removing it will
3252 * remove the ref to it. */
3253 g_object_ref (oclient);
3254 g_hash_table_remove (tunnels, tunnelid);
3255 g_mutex_unlock (&tunnels_lock);
3257 opriv = oclient->priv;
3259 if (opriv->watch == NULL)
3262 GST_INFO ("client %p: found previous tunnel %p (old %p, new %p)", client,
3263 oclient, opriv->connection, priv->connection);
3265 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
3266 gst_rtsp_watch_reset (priv->watch);
3267 gst_rtsp_watch_reset (opriv->watch);
3268 g_object_unref (oclient);
3270 /* the old client owns the tunnel now, the new one will be freed */
3271 g_source_destroy ((GSource *) priv->watch);
3273 g_main_context_unref (priv->watch_context);
3274 priv->watch_context = NULL;
3275 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
3283 GST_ERROR ("client %p: no tunnelid provided", client);
3288 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
3289 g_object_unref (oclient);
3294 static GstRTSPStatusCode
3295 tunnel_get (GstRTSPWatch * watch, gpointer user_data)
3297 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3299 GST_INFO ("client %p: tunnel get (connection %p)", client,
3300 client->priv->connection);
3302 if (!handle_tunnel (client)) {
3303 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
3306 return GST_RTSP_STS_OK;
3309 static GstRTSPResult
3310 tunnel_post (GstRTSPWatch * watch, gpointer user_data)
3312 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3314 GST_INFO ("client %p: tunnel post (connection %p)", client,
3315 client->priv->connection);
3317 if (!handle_tunnel (client)) {
3318 return GST_RTSP_ERROR;
3324 static GstRTSPResult
3325 tunnel_http_response (GstRTSPWatch * watch, GstRTSPMessage * request,
3326 GstRTSPMessage * response, gpointer user_data)
3328 GstRTSPClientClass *klass;
3330 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3331 klass = GST_RTSP_CLIENT_GET_CLASS (client);
3333 if (klass->tunnel_http_response) {
3334 klass->tunnel_http_response (client, request, response);
3340 static GstRTSPWatchFuncs watch_funcs = {
3349 tunnel_http_response
3353 client_watch_notify (GstRTSPClient * client)
3355 GstRTSPClientPrivate *priv = client->priv;
3357 GST_INFO ("client %p: watch destroyed", client);
3359 g_main_context_unref (priv->watch_context);
3360 priv->watch_context = NULL;
3361 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
3362 g_object_unref (client);
3366 * gst_rtsp_client_attach:
3367 * @client: a #GstRTSPClient
3368 * @context: (allow-none): a #GMainContext
3370 * Attaches @client to @context. When the mainloop for @context is run, the
3371 * client will be dispatched. When @context is %NULL, the default context will be
3374 * This function should be called when the client properties and urls are fully
3375 * configured and the client is ready to start.
3377 * Returns: the ID (greater than 0) for the source within the GMainContext.
3380 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
3382 GstRTSPClientPrivate *priv;
3385 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
3386 priv = client->priv;
3387 g_return_val_if_fail (priv->connection != NULL, 0);
3388 g_return_val_if_fail (priv->watch == NULL, 0);
3390 /* make sure noone will free the context before the watch is destroyed */
3391 priv->watch_context = g_main_context_ref (context);
3393 /* create watch for the connection and attach */
3394 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
3395 g_object_ref (client), (GDestroyNotify) client_watch_notify);
3396 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
3397 (GDestroyNotify) gst_rtsp_watch_unref);
3399 /* FIXME make this configurable. We don't want to do this yet because it will
3400 * be superceeded by a cache object later */
3401 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
3403 GST_INFO ("attaching to context %p", context);
3404 res = gst_rtsp_watch_attach (priv->watch, context);
3410 * gst_rtsp_client_session_filter:
3411 * @client: a #GstRTSPClient
3412 * @func: (scope call) (allow-none): a callback
3413 * @user_data: user data passed to @func
3415 * Call @func for each session managed by @client. The result value of @func
3416 * determines what happens to the session. @func will be called with @client
3417 * locked so no further actions on @client can be performed from @func.
3419 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
3422 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
3424 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
3425 * will also be added with an additional ref to the result #GList of this
3428 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each session.
3430 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
3431 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3432 * element in the #GList should be unreffed before the list is freed.
3435 gst_rtsp_client_session_filter (GstRTSPClient * client,
3436 GstRTSPClientSessionFilterFunc func, gpointer user_data)
3438 GstRTSPClientPrivate *priv;
3439 GList *result, *walk, *next;
3441 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
3443 priv = client->priv;
3447 g_mutex_lock (&priv->lock);
3448 for (walk = priv->sessions; walk; walk = next) {
3449 GstRTSPSession *sess = walk->data;
3450 GstRTSPFilterResult res;
3452 next = g_list_next (walk);
3455 res = func (client, sess, user_data);
3457 res = GST_RTSP_FILTER_REF;
3460 case GST_RTSP_FILTER_REMOVE:
3461 /* stop watching the session and pretent it went away */
3462 client_cleanup_session (client, sess);
3464 case GST_RTSP_FILTER_REF:
3465 result = g_list_prepend (result, g_object_ref (sess));
3467 case GST_RTSP_FILTER_KEEP:
3472 g_mutex_unlock (&priv->lock);