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;
68 GstRTSPClientSendFunc send_func; /* protected by send_lock */
69 gpointer send_data; /* protected by send_lock */
70 GDestroyNotify send_notify; /* protected by send_lock */
72 GstRTSPSessionPool *session_pool;
73 GstRTSPMountPoints *mount_points;
75 GstRTSPThreadPool *thread_pool;
77 /* used to cache the media in the last requested DESCRIBE so that
78 * we can pick it up in the next SETUP immediately */
85 gboolean drop_backlog;
88 static GMutex tunnels_lock;
89 static GHashTable *tunnels; /* protected by tunnels_lock */
91 #define DEFAULT_SESSION_POOL NULL
92 #define DEFAULT_MOUNT_POINTS NULL
93 #define DEFAULT_DROP_BACKLOG TRUE
108 SIGNAL_OPTIONS_REQUEST,
109 SIGNAL_DESCRIBE_REQUEST,
110 SIGNAL_SETUP_REQUEST,
112 SIGNAL_PAUSE_REQUEST,
113 SIGNAL_TEARDOWN_REQUEST,
114 SIGNAL_SET_PARAMETER_REQUEST,
115 SIGNAL_GET_PARAMETER_REQUEST,
116 SIGNAL_HANDLE_RESPONSE,
121 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
122 #define GST_CAT_DEFAULT rtsp_client_debug
124 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
126 static void gst_rtsp_client_get_property (GObject * object, guint propid,
127 GValue * value, GParamSpec * pspec);
128 static void gst_rtsp_client_set_property (GObject * object, guint propid,
129 const GValue * value, GParamSpec * pspec);
130 static void gst_rtsp_client_finalize (GObject * obj);
132 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
133 static void client_session_finalized (GstRTSPClient * client,
134 GstRTSPSession * session);
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_VOID__VOID, 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_VOID__OBJECT, 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_VOID__POINTER, G_TYPE_NONE, 1,
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_VOID__POINTER, G_TYPE_NONE, 1,
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_VOID__POINTER, G_TYPE_NONE, 1,
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_VOID__POINTER, G_TYPE_NONE, 1,
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_VOID__POINTER, G_TYPE_NONE, 1,
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_VOID__POINTER, G_TYPE_NONE, 1,
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_VOID__POINTER,
237 G_TYPE_NONE, 1, G_TYPE_POINTER);
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_VOID__POINTER,
243 G_TYPE_NONE, 1, G_TYPE_POINTER);
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_VOID__POINTER,
249 G_TYPE_NONE, 1, G_TYPE_POINTER);
251 gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE] =
252 g_signal_new ("send-message", G_TYPE_FROM_CLASS (klass),
253 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__POINTER,
254 G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER);
257 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
258 g_mutex_init (&tunnels_lock);
260 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
264 gst_rtsp_client_init (GstRTSPClient * client)
266 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
270 g_mutex_init (&priv->lock);
271 g_mutex_init (&priv->send_lock);
273 priv->drop_backlog = DEFAULT_DROP_BACKLOG;
276 static GstRTSPFilterResult
277 filter_session (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
280 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
282 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
283 unlink_session_transports (client, sess, sessmedia);
285 /* unmanage the media in the session */
286 return GST_RTSP_FILTER_REMOVE;
290 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
292 /* unlink all media managed in this session */
293 gst_rtsp_session_filter (session, filter_session, client);
297 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
299 GstRTSPClientPrivate *priv = client->priv;
302 for (walk = priv->sessions; walk; walk = g_list_next (walk)) {
303 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
305 /* we already know about this session */
306 if (msession == session)
310 GST_INFO ("watching session %p", session);
312 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
314 priv->sessions = g_list_prepend (priv->sessions, session);
318 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session)
320 GstRTSPClientPrivate *priv = client->priv;
322 GST_INFO ("unwatching session %p", session);
324 g_object_weak_unref (G_OBJECT (session),
325 (GWeakNotify) client_session_finalized, client);
326 priv->sessions = g_list_remove (priv->sessions, session);
330 client_cleanup_session (GstRTSPClient * client, GstRTSPSession * session)
332 g_object_weak_unref (G_OBJECT (session),
333 (GWeakNotify) client_session_finalized, client);
334 client_unlink_session (client, session);
338 client_cleanup_sessions (GstRTSPClient * client)
340 GstRTSPClientPrivate *priv = client->priv;
343 /* remove weak-ref from sessions */
344 for (sessions = priv->sessions; sessions; sessions = g_list_next (sessions)) {
345 client_cleanup_session (client, (GstRTSPSession *) sessions->data);
347 g_list_free (priv->sessions);
348 priv->sessions = NULL;
351 /* A client is finalized when the connection is broken */
353 gst_rtsp_client_finalize (GObject * obj)
355 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
356 GstRTSPClientPrivate *priv = client->priv;
358 GST_INFO ("finalize client %p", client);
361 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
362 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
365 g_source_destroy ((GSource *) priv->watch);
367 client_cleanup_sessions (client);
369 if (priv->connection)
370 gst_rtsp_connection_free (priv->connection);
371 if (priv->session_pool)
372 g_object_unref (priv->session_pool);
373 if (priv->mount_points)
374 g_object_unref (priv->mount_points);
376 g_object_unref (priv->auth);
377 if (priv->thread_pool)
378 g_object_unref (priv->thread_pool);
383 gst_rtsp_media_unprepare (priv->media);
384 g_object_unref (priv->media);
387 g_free (priv->server_ip);
388 g_mutex_clear (&priv->lock);
389 g_mutex_clear (&priv->send_lock);
391 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
395 gst_rtsp_client_get_property (GObject * object, guint propid,
396 GValue * value, GParamSpec * pspec)
398 GstRTSPClient *client = GST_RTSP_CLIENT (object);
399 GstRTSPClientPrivate *priv = client->priv;
402 case PROP_SESSION_POOL:
403 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
405 case PROP_MOUNT_POINTS:
406 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
408 case PROP_DROP_BACKLOG:
409 g_value_set_boolean (value, priv->drop_backlog);
412 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
417 gst_rtsp_client_set_property (GObject * object, guint propid,
418 const GValue * value, GParamSpec * pspec)
420 GstRTSPClient *client = GST_RTSP_CLIENT (object);
421 GstRTSPClientPrivate *priv = client->priv;
424 case PROP_SESSION_POOL:
425 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
427 case PROP_MOUNT_POINTS:
428 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
430 case PROP_DROP_BACKLOG:
431 g_mutex_lock (&priv->lock);
432 priv->drop_backlog = g_value_get_boolean (value);
433 g_mutex_unlock (&priv->lock);
436 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
441 * gst_rtsp_client_new:
443 * Create a new #GstRTSPClient instance.
445 * Returns: (transfer full): a new #GstRTSPClient
448 gst_rtsp_client_new (void)
450 GstRTSPClient *result;
452 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
458 send_message (GstRTSPClient * client, GstRTSPContext * ctx,
459 GstRTSPMessage * message, gboolean close)
461 GstRTSPClientPrivate *priv = client->priv;
463 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
464 "GStreamer RTSP server");
466 /* remove any previous header */
467 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
469 /* add the new session header for new session ids */
471 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
472 gst_rtsp_session_get_header (ctx->session));
475 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
476 gst_rtsp_message_dump (message);
480 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
482 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE],
485 g_mutex_lock (&priv->send_lock);
487 priv->send_func (client, message, close, priv->send_data);
488 g_mutex_unlock (&priv->send_lock);
490 gst_rtsp_message_unset (message);
494 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
495 GstRTSPContext * ctx)
497 gst_rtsp_message_init_response (ctx->response, code,
498 gst_rtsp_status_as_text (code), ctx->request);
502 send_message (client, ctx, ctx->response, FALSE);
506 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
508 if (path1 == NULL || path2 == NULL)
511 if (strlen (path1) != len2)
514 if (strncmp (path1, path2, len2))
520 /* this function is called to initially find the media for the DESCRIBE request
521 * but is cached for when the same client (without breaking the connection) is
522 * doing a setup for the exact same url. */
523 static GstRTSPMedia *
524 find_media (GstRTSPClient * client, GstRTSPContext * ctx, gchar * path,
527 GstRTSPClientPrivate *priv = client->priv;
528 GstRTSPMediaFactory *factory;
532 /* find the longest matching factory for the uri first */
533 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
537 ctx->factory = factory;
539 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
540 goto no_factory_access;
542 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
548 path_len = strlen (path);
550 if (!paths_are_equal (priv->path, path, path_len)) {
551 GstRTSPThread *thread;
553 /* remove any previously cached values before we try to construct a new
559 gst_rtsp_media_unprepare (priv->media);
560 g_object_unref (priv->media);
564 /* prepare the media and add it to the pipeline */
565 if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
570 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
571 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
575 /* prepare the media */
576 if (!(gst_rtsp_media_prepare (media, thread)))
579 /* now keep track of the uri and the media */
580 priv->path = g_strndup (path, path_len);
583 /* we have seen this path before, used cached media */
586 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
589 g_object_unref (factory);
593 g_object_ref (media);
600 GST_ERROR ("client %p: no factory for path %s", client, path);
601 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
606 GST_ERROR ("client %p: not authorized to see factory path %s", client,
608 /* error reply is already sent */
613 GST_ERROR ("client %p: not authorized for factory path %s", client, path);
614 /* error reply is already sent */
619 GST_ERROR ("client %p: can't create media", client);
620 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
621 g_object_unref (factory);
627 GST_ERROR ("client %p: can't create thread", client);
628 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
629 g_object_unref (media);
631 g_object_unref (factory);
637 GST_ERROR ("client %p: can't prepare media", client);
638 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
639 g_object_unref (media);
641 g_object_unref (factory);
648 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
650 GstRTSPClientPrivate *priv = client->priv;
651 GstRTSPMessage message = { 0 };
656 gst_rtsp_message_init_data (&message, channel);
658 /* FIXME, need some sort of iovec RTSPMessage here */
659 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
662 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
664 g_mutex_lock (&priv->send_lock);
666 priv->send_func (client, &message, FALSE, priv->send_data);
667 g_mutex_unlock (&priv->send_lock);
669 gst_rtsp_message_steal_body (&message, &data, &usize);
670 gst_buffer_unmap (buffer, &map_info);
672 gst_rtsp_message_unset (&message);
678 link_transport (GstRTSPClient * client, GstRTSPSession * session,
679 GstRTSPStreamTransport * trans)
681 GstRTSPClientPrivate *priv = client->priv;
683 GST_DEBUG ("client %p: linking transport %p", client, trans);
685 gst_rtsp_stream_transport_set_callbacks (trans,
686 (GstRTSPSendFunc) do_send_data,
687 (GstRTSPSendFunc) do_send_data, client, NULL);
689 priv->transports = g_list_prepend (priv->transports, trans);
691 /* make sure our session can't expire */
692 gst_rtsp_session_prevent_expire (session);
696 link_session_transports (GstRTSPClient * client, GstRTSPSession * session,
697 GstRTSPSessionMedia * sessmedia)
702 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
703 for (i = 0; i < n_streams; i++) {
704 GstRTSPStreamTransport *trans;
705 const GstRTSPTransport *tr;
707 /* get the transport, if there is no transport configured, skip this stream */
708 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
712 tr = gst_rtsp_stream_transport_get_transport (trans);
714 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
715 /* for TCP, link the stream to the TCP connection of the client */
716 link_transport (client, session, trans);
722 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
723 GstRTSPStreamTransport * trans)
725 GstRTSPClientPrivate *priv = client->priv;
727 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
729 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
731 priv->transports = g_list_remove (priv->transports, trans);
733 /* our session can now expire */
734 gst_rtsp_session_allow_expire (session);
738 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
739 GstRTSPSessionMedia * sessmedia)
744 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
745 for (i = 0; i < n_streams; i++) {
746 GstRTSPStreamTransport *trans;
747 const GstRTSPTransport *tr;
749 /* get the transport, if there is no transport configured, skip this stream */
750 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
754 tr = gst_rtsp_stream_transport_get_transport (trans);
756 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
757 /* for TCP, unlink the stream from the TCP connection of the client */
758 unlink_transport (client, session, trans);
764 close_connection (GstRTSPClient * client)
766 GstRTSPClientPrivate *priv = client->priv;
767 const gchar *tunnelid;
769 GST_DEBUG ("client %p: closing connection", client);
771 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
772 g_mutex_lock (&tunnels_lock);
773 /* remove from tunnelids */
774 g_hash_table_remove (tunnels, tunnelid);
775 g_mutex_unlock (&tunnels_lock);
778 gst_rtsp_connection_close (priv->connection);
782 default_make_path_from_uri (GstRTSPClient * client, const GstRTSPUrl * uri)
787 path = g_strconcat (uri->abspath, "?", uri->query, NULL);
789 path = g_strdup (uri->abspath);
795 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
797 GstRTSPClientPrivate *priv = client->priv;
798 GstRTSPClientClass *klass;
799 GstRTSPSession *session;
800 GstRTSPSessionMedia *sessmedia;
801 GstRTSPStatusCode code;
808 session = ctx->session;
813 klass = GST_RTSP_CLIENT_GET_CLASS (client);
814 path = klass->make_path_from_uri (client, ctx->uri);
816 /* get a handle to the configuration of the media in the session */
817 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
821 /* only aggregate control for now.. */
822 if (path[matched] != '\0')
827 ctx->sessmedia = sessmedia;
829 /* we emit the signal before closing the connection */
830 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
833 /* make sure we unblock the backlog and don't accept new messages
835 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
837 /* unlink the all TCP callbacks */
838 unlink_session_transports (client, session, sessmedia);
840 /* remove the session from the watched sessions */
841 client_unwatch_session (client, session);
843 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
845 /* allow messages again so that we can send the reply */
846 gst_rtsp_watch_set_flushing (priv->watch, FALSE);
848 /* unmanage the media in the session, returns false if all media session
850 if (!gst_rtsp_session_release_media (session, sessmedia)) {
851 /* remove the session */
852 gst_rtsp_session_pool_remove (priv->session_pool, session);
854 /* construct the response now */
855 code = GST_RTSP_STS_OK;
856 gst_rtsp_message_init_response (ctx->response, code,
857 gst_rtsp_status_as_text (code), ctx->request);
859 send_message (client, ctx, ctx->response, TRUE);
866 GST_ERROR ("client %p: no session", client);
867 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
872 GST_ERROR ("client %p: no uri supplied", client);
873 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
878 GST_ERROR ("client %p: no media for uri", client);
879 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
885 GST_ERROR ("client %p: no aggregate path %s", client, path);
886 send_generic_response (client,
887 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
894 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
898 res = gst_rtsp_params_set (client, ctx);
904 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
908 res = gst_rtsp_params_get (client, ctx);
914 handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
920 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
921 if (res != GST_RTSP_OK)
925 /* no body, keep-alive request */
926 send_generic_response (client, GST_RTSP_STS_OK, ctx);
928 /* there is a body, handle the params */
929 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
930 if (res != GST_RTSP_OK)
933 send_message (client, ctx, ctx->response, FALSE);
936 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
944 GST_ERROR ("client %p: bad request", client);
945 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
951 handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
957 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
958 if (res != GST_RTSP_OK)
962 /* no body, keep-alive request */
963 send_generic_response (client, GST_RTSP_STS_OK, ctx);
965 /* there is a body, handle the params */
966 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
967 if (res != GST_RTSP_OK)
970 send_message (client, ctx, ctx->response, FALSE);
973 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
981 GST_ERROR ("client %p: bad request", client);
982 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
988 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
990 GstRTSPSession *session;
991 GstRTSPClientClass *klass;
992 GstRTSPSessionMedia *sessmedia;
993 GstRTSPStatusCode code;
994 GstRTSPState rtspstate;
998 if (!(session = ctx->session))
1004 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1005 path = klass->make_path_from_uri (client, ctx->uri);
1007 /* get a handle to the configuration of the media in the session */
1008 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1012 if (path[matched] != '\0')
1017 ctx->sessmedia = sessmedia;
1019 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1020 /* the session state must be playing or recording */
1021 if (rtspstate != GST_RTSP_STATE_PLAYING &&
1022 rtspstate != GST_RTSP_STATE_RECORDING)
1025 /* unlink the all TCP callbacks */
1026 unlink_session_transports (client, session, sessmedia);
1028 /* then pause sending */
1029 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
1031 /* construct the response now */
1032 code = GST_RTSP_STS_OK;
1033 gst_rtsp_message_init_response (ctx->response, code,
1034 gst_rtsp_status_as_text (code), ctx->request);
1036 send_message (client, ctx, ctx->response, FALSE);
1038 /* the state is now READY */
1039 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1041 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
1048 GST_ERROR ("client %p: no seesion", client);
1049 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1054 GST_ERROR ("client %p: no uri supplied", client);
1055 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1060 GST_ERROR ("client %p: no media for uri", client);
1061 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1067 GST_ERROR ("client %p: no aggregate path %s", client, path);
1068 send_generic_response (client,
1069 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1075 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
1076 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1082 /* convert @url and @path to a URL used as a content base for the factory
1083 * located at @path */
1085 make_base_url (GstRTSPClient * client, GstRTSPUrl * url, const gchar * path)
1091 /* check for trailing '/' and append one */
1092 trail = (path[strlen (path) - 1] != '/' ? "/" : "");
1097 tmp.abspath = g_strdup_printf ("%s%s", path, trail);
1099 result = gst_rtsp_url_get_request_uri (&tmp);
1100 g_free (tmp.abspath);
1106 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
1108 GstRTSPSession *session;
1109 GstRTSPClientClass *klass;
1110 GstRTSPSessionMedia *sessmedia;
1111 GstRTSPMedia *media;
1112 GstRTSPStatusCode code;
1115 GstRTSPTimeRange *range;
1117 GstRTSPState rtspstate;
1118 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1119 gchar *path, *rtpinfo;
1122 if (!(session = ctx->session))
1125 if (!(uri = ctx->uri))
1128 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1129 path = klass->make_path_from_uri (client, uri);
1131 /* get a handle to the configuration of the media in the session */
1132 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1136 if (path[matched] != '\0')
1141 ctx->sessmedia = sessmedia;
1142 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1144 /* the session state must be playing or ready */
1145 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1146 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1149 /* in play we first unsuspend, media could be suspended from SDP or PAUSED */
1150 if (!gst_rtsp_media_unsuspend (media))
1151 goto unsuspend_failed;
1153 /* parse the range header if we have one */
1154 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1155 if (res == GST_RTSP_OK) {
1156 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1157 /* we have a range, seek to the position */
1159 gst_rtsp_media_seek (media, range);
1160 gst_rtsp_range_free (range);
1164 /* link the all TCP callbacks */
1165 link_session_transports (client, session, sessmedia);
1167 /* grab RTPInfo from the media now */
1168 rtpinfo = gst_rtsp_session_media_get_rtpinfo (sessmedia);
1170 /* construct the response now */
1171 code = GST_RTSP_STS_OK;
1172 gst_rtsp_message_init_response (ctx->response, code,
1173 gst_rtsp_status_as_text (code), ctx->request);
1175 /* add the RTP-Info header */
1177 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO,
1181 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1183 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1185 send_message (client, ctx, ctx->response, FALSE);
1187 /* start playing after sending the response */
1188 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1190 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1192 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1199 GST_ERROR ("client %p: no session", client);
1200 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1205 GST_ERROR ("client %p: no uri supplied", client);
1206 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1211 GST_ERROR ("client %p: media not found", client);
1212 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1217 GST_ERROR ("client %p: no aggregate path %s", client, path);
1218 send_generic_response (client,
1219 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1225 GST_ERROR ("client %p: not PLAYING or READY", client);
1226 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1232 GST_ERROR ("client %p: unsuspend failed", client);
1233 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1239 do_keepalive (GstRTSPSession * session)
1241 GST_INFO ("keep session %p alive", session);
1242 gst_rtsp_session_touch (session);
1245 /* parse @transport and return a valid transport in @tr. only transports
1246 * supported by @stream are returned. Returns FALSE if no valid transport
1249 parse_transport (const char *transport, GstRTSPStream * stream,
1250 GstRTSPTransport * tr)
1257 gst_rtsp_transport_init (tr);
1259 GST_DEBUG ("parsing transports %s", transport);
1261 transports = g_strsplit (transport, ",", 0);
1263 /* loop through the transports, try to parse */
1264 for (i = 0; transports[i]; i++) {
1265 res = gst_rtsp_transport_parse (transports[i], tr);
1266 if (res != GST_RTSP_OK) {
1267 /* no valid transport, search some more */
1268 GST_WARNING ("could not parse transport %s", transports[i]);
1272 /* we have a transport, see if it's supported */
1273 if (!gst_rtsp_stream_is_transport_supported (stream, tr)) {
1274 GST_WARNING ("unsupported transport %s", transports[i]);
1278 /* we have a valid transport */
1279 GST_INFO ("found valid transport %s", transports[i]);
1284 gst_rtsp_transport_init (tr);
1286 g_strfreev (transports);
1292 default_configure_client_media (GstRTSPClient * client, GstRTSPMedia * media,
1293 GstRTSPStream * stream, GstRTSPContext * ctx)
1295 GstRTSPMessage *request = ctx->request;
1296 gchar *blocksize_str;
1298 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1299 &blocksize_str, 0) == GST_RTSP_OK) {
1303 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1304 if (end == blocksize_str)
1307 /* we don't want to change the mtu when this media
1308 * can be shared because it impacts other clients */
1309 if (gst_rtsp_media_is_shared (media))
1312 if (blocksize > G_MAXUINT)
1313 blocksize = G_MAXUINT;
1315 gst_rtsp_stream_set_mtu (stream, blocksize);
1323 GST_ERROR_OBJECT (client, "failed to parse blocksize");
1324 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1330 default_configure_client_transport (GstRTSPClient * client,
1331 GstRTSPContext * ctx, GstRTSPTransport * ct)
1333 GstRTSPClientPrivate *priv = client->priv;
1335 /* we have a valid transport now, set the destination of the client. */
1336 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1337 gboolean use_client_settings;
1339 use_client_settings =
1340 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1342 if (ct->destination && use_client_settings) {
1343 GstRTSPAddress *addr;
1345 addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1346 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1351 gst_rtsp_address_free (addr);
1353 GstRTSPAddress *addr;
1354 GSocketFamily family;
1356 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1358 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1362 g_free (ct->destination);
1363 ct->destination = g_strdup (addr->address);
1364 ct->port.min = addr->port;
1365 ct->port.max = addr->port + addr->n_ports - 1;
1366 ct->ttl = addr->ttl;
1368 gst_rtsp_address_free (addr);
1373 url = gst_rtsp_connection_get_url (priv->connection);
1374 g_free (ct->destination);
1375 ct->destination = g_strdup (url->host);
1377 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1378 /* check if the client selected channels for TCP */
1379 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1380 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1390 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1395 static GstRTSPTransport *
1396 make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
1397 GstRTSPTransport * ct)
1399 GstRTSPTransport *st;
1401 GSocketFamily family;
1403 /* prepare the server transport */
1404 gst_rtsp_transport_new (&st);
1406 st->trans = ct->trans;
1407 st->profile = ct->profile;
1408 st->lower_transport = ct->lower_transport;
1410 addr = g_inet_address_new_from_string (ct->destination);
1413 GST_ERROR ("failed to get inet addr from client destination");
1414 family = G_SOCKET_FAMILY_IPV4;
1416 family = g_inet_address_get_family (addr);
1417 g_object_unref (addr);
1421 switch (st->lower_transport) {
1422 case GST_RTSP_LOWER_TRANS_UDP:
1423 st->client_port = ct->client_port;
1424 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
1426 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1427 st->port = ct->port;
1428 st->destination = g_strdup (ct->destination);
1431 case GST_RTSP_LOWER_TRANS_TCP:
1432 st->interleaved = ct->interleaved;
1437 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
1443 mikey_apply_policy (GstCaps * caps, GstMIKEYMessage * msg, guint8 policy)
1445 const gchar *srtp_cipher;
1446 const gchar *srtp_auth;
1447 const GstMIKEYPayload *sp;
1450 /* loop over Security policy until we find one containing policy */
1452 if ((sp = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, i)) == NULL)
1455 if (((GstMIKEYPayloadSP *) sp)->policy == policy)
1459 /* the default ciphers */
1460 srtp_cipher = "aes-128-icm";
1461 srtp_auth = "hmac-sha1-80";
1463 /* now override the defaults with what is in the Security Policy */
1467 /* collect all the params and go over them */
1468 len = gst_mikey_payload_sp_get_n_params (sp);
1469 for (i = 0; i < len; i++) {
1470 const GstMIKEYPayloadSPParam *param =
1471 gst_mikey_payload_sp_get_param (sp, i);
1473 switch (param->type) {
1474 case GST_MIKEY_SP_SRTP_ENC_ALG:
1475 switch (param->val[0]) {
1477 srtp_cipher = "null";
1481 srtp_cipher = "aes-128-icm";
1487 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1488 switch (param->val[0]) {
1494 srtp_auth = "hmac-sha1-80";
1500 case GST_MIKEY_SP_SRTP_SRTP_ENC:
1502 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
1509 /* now configure the SRTP parameters */
1510 gst_caps_set_simple (caps,
1511 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
1512 "srtp-auth", G_TYPE_STRING, srtp_auth,
1513 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
1514 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
1520 handle_mikey_data (GstRTSPClient * client, GstRTSPContext * ctx,
1521 guint8 * data, gsize size)
1523 GstMIKEYMessage *msg;
1525 GstCaps *caps = NULL;
1526 GstMIKEYPayloadKEMAC *kemac;
1527 const GstMIKEYPayloadKeyData *pkd;
1530 /* the MIKEY message contains a CSB or crypto session bundle. It is a
1531 * set of Crypto Sessions protected with the same master key.
1532 * In the context of SRTP, an RTP and its RTCP stream is part of a
1534 if ((msg = gst_mikey_message_new_from_data (data, size, NULL, NULL)) == NULL)
1537 /* we can only handle SRTP crypto sessions for now */
1538 if (msg->map_type != GST_MIKEY_MAP_TYPE_SRTP)
1539 goto invalid_map_type;
1541 /* get the number of crypto sessions. This maps SSRC to its
1542 * security parameters */
1543 n_cs = gst_mikey_message_get_n_cs (msg);
1545 goto no_crypto_sessions;
1547 /* we also need keys */
1548 if (!(kemac = (GstMIKEYPayloadKEMAC *) gst_mikey_message_find_payload
1549 (msg, GST_MIKEY_PT_KEMAC, 0)))
1552 /* we don't support encrypted keys */
1553 if (kemac->enc_alg != GST_MIKEY_ENC_NULL
1554 || kemac->mac_alg != GST_MIKEY_MAC_NULL)
1555 goto unsupported_encryption;
1557 /* get Key data sub-payload */
1558 pkd = (const GstMIKEYPayloadKeyData *)
1559 gst_mikey_payload_kemac_get_sub (&kemac->pt, 0);
1562 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
1565 /* go over all crypto sessions and create the security policy for each
1567 for (i = 0; i < n_cs; i++) {
1568 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
1570 caps = gst_caps_new_simple ("application/x-srtp",
1571 "ssrc", G_TYPE_UINT, map->ssrc,
1572 "roc", G_TYPE_UINT, map->roc, "srtp-key", GST_TYPE_BUFFER, key, NULL);
1573 mikey_apply_policy (caps, msg, map->policy);
1575 gst_rtsp_stream_update_crypto (ctx->stream, map->ssrc, caps);
1576 gst_caps_unref (caps);
1578 gst_mikey_message_free (msg);
1585 GST_DEBUG_OBJECT (client, "failed to parse MIKEY message");
1590 GST_DEBUG_OBJECT (client, "invalid map type %d", msg->map_type);
1591 goto cleanup_message;
1595 GST_DEBUG_OBJECT (client, "no crypto sessions");
1596 goto cleanup_message;
1600 GST_DEBUG_OBJECT (client, "no keys found");
1601 goto cleanup_message;
1603 unsupported_encryption:
1605 GST_DEBUG_OBJECT (client, "unsupported key encryption");
1606 goto cleanup_message;
1610 gst_mikey_message_free (msg);
1615 #define IS_STRIP_CHAR(c) (g_ascii_isspace ((guchar)(c)) || ((c) == '\"'))
1618 strip_chars (gchar * str)
1625 if (!IS_STRIP_CHAR (str[len]))
1629 for (s = str; *s && IS_STRIP_CHAR (*s); s++);
1630 memmove (str, s, len + 1);
1634 * KeyMgmt = "KeyMgmt" ":" key-mgmt-spec 0*("," key-mgmt-spec)
1635 * key-mgmt-spec = "prot" "=" KMPID ";" ["uri" "=" %x22 URI %x22 ";"]
1638 handle_keymgmt (GstRTSPClient * client, GstRTSPContext * ctx, gchar * keymgmt)
1643 specs = g_strsplit (keymgmt, ",", 0);
1644 for (i = 0; specs[i]; i++) {
1647 split = g_strsplit (specs[i], ";", 0);
1648 for (j = 0; split[j]; j++) {
1649 g_strstrip (split[j]);
1650 if (g_str_has_prefix (split[j], "prot=")) {
1651 g_strstrip (split[j] + 5);
1652 if (!g_str_equal (split[j] + 5, "mikey"))
1654 GST_DEBUG ("found mikey");
1655 } else if (g_str_has_prefix (split[j], "uri=")) {
1656 strip_chars (split[j] + 4);
1657 GST_DEBUG ("found uri '%s'", split[j] + 4);
1658 } else if (g_str_has_prefix (split[j], "data=")) {
1661 strip_chars (split[j] + 5);
1662 GST_DEBUG ("found data '%s'", split[j] + 5);
1663 data = g_base64_decode_inplace (split[j] + 5, &size);
1664 handle_mikey_data (client, ctx, data, size);
1672 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
1674 GstRTSPClientPrivate *priv = client->priv;
1677 gchar *transport, *keymgmt;
1678 GstRTSPTransport *ct, *st;
1679 GstRTSPStatusCode code;
1680 GstRTSPSession *session;
1681 GstRTSPStreamTransport *trans;
1683 GstRTSPSessionMedia *sessmedia;
1684 GstRTSPMedia *media;
1685 GstRTSPStream *stream;
1686 GstRTSPState rtspstate;
1687 GstRTSPClientClass *klass;
1688 gchar *path, *control;
1695 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1696 path = klass->make_path_from_uri (client, uri);
1698 /* parse the transport */
1700 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
1702 if (res != GST_RTSP_OK)
1705 /* we create the session after parsing stuff so that we don't make
1706 * a session for malformed requests */
1707 if (priv->session_pool == NULL)
1710 session = ctx->session;
1713 g_object_ref (session);
1714 /* get a handle to the configuration of the media in the session, this can
1715 * return NULL if this is a new url to manage in this session. */
1716 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1718 /* we need a new media configuration in this session */
1722 /* we have no session media, find one and manage it */
1723 if (sessmedia == NULL) {
1724 /* get a handle to the configuration of the media in the session */
1725 media = find_media (client, ctx, path, &matched);
1727 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1728 g_object_ref (media);
1730 goto media_not_found;
1732 /* no media, not found then */
1734 goto media_not_found_no_reply;
1736 if (path[matched] == '\0')
1737 goto control_not_found;
1739 /* path is what matched. */
1740 path[matched] = '\0';
1741 /* control is remainder */
1742 control = &path[matched + 1];
1744 /* find the stream now using the control part */
1745 stream = gst_rtsp_media_find_stream (media, control);
1747 goto stream_not_found;
1749 /* now we have a uri identifying a valid media and stream */
1750 ctx->stream = stream;
1753 if (session == NULL) {
1754 /* create a session if this fails we probably reached our session limit or
1756 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1757 goto service_unavailable;
1759 /* make sure this client is closed when the session is closed */
1760 client_watch_session (client, session);
1762 /* signal new session */
1763 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1766 ctx->session = session;
1769 if (sessmedia == NULL) {
1770 /* manage the media in our session now, if not done already */
1771 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1772 /* if we stil have no media, error */
1773 if (sessmedia == NULL)
1774 goto sessmedia_unavailable;
1776 g_object_unref (media);
1779 ctx->sessmedia = sessmedia;
1781 if (!klass->configure_client_media (client, media, stream, ctx))
1782 goto configure_media_failed_no_reply;
1784 gst_rtsp_transport_new (&ct);
1786 /* parse and find a usable supported transport */
1787 if (!parse_transport (transport, stream, ct))
1788 goto unsupported_transports;
1790 /* update the client transport */
1791 if (!klass->configure_client_transport (client, ctx, ct))
1792 goto unsupported_client_transport;
1794 /* parse the keymgmt */
1795 if (gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_KEYMGMT,
1796 &keymgmt, 0) == GST_RTSP_OK) {
1797 if (!handle_keymgmt (client, ctx, keymgmt))
1801 /* set in the session media transport */
1802 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1804 /* configure the url used to set this transport, this we will use when
1805 * generating the response for the PLAY request */
1806 gst_rtsp_stream_transport_set_url (trans, uri);
1808 /* configure keepalive for this transport */
1809 gst_rtsp_stream_transport_set_keepalive (trans,
1810 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1812 /* create and serialize the server transport */
1813 st = make_server_transport (client, ctx, ct);
1814 trans_str = gst_rtsp_transport_as_text (st);
1815 gst_rtsp_transport_free (st);
1817 /* construct the response now */
1818 code = GST_RTSP_STS_OK;
1819 gst_rtsp_message_init_response (ctx->response, code,
1820 gst_rtsp_status_as_text (code), ctx->request);
1822 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
1826 send_message (client, ctx, ctx->response, FALSE);
1828 /* update the state */
1829 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1830 switch (rtspstate) {
1831 case GST_RTSP_STATE_PLAYING:
1832 case GST_RTSP_STATE_RECORDING:
1833 case GST_RTSP_STATE_READY:
1834 /* no state change */
1837 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1840 g_object_unref (session);
1843 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
1850 GST_ERROR ("client %p: no uri", client);
1851 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1856 GST_ERROR ("client %p: no transport", client);
1857 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1862 GST_ERROR ("client %p: no session pool configured", client);
1863 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1866 media_not_found_no_reply:
1868 GST_ERROR ("client %p: media '%s' not found", client, path);
1869 /* error reply is already sent */
1874 GST_ERROR ("client %p: media '%s' not found", client, path);
1875 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1880 GST_ERROR ("client %p: no control in path '%s'", client, path);
1881 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1882 g_object_unref (media);
1887 GST_ERROR ("client %p: stream '%s' not found", client, control);
1888 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1889 g_object_unref (media);
1892 service_unavailable:
1894 GST_ERROR ("client %p: can't create session", client);
1895 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1896 g_object_unref (media);
1899 sessmedia_unavailable:
1901 GST_ERROR ("client %p: can't create session media", client);
1902 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1903 g_object_unref (media);
1904 goto cleanup_session;
1906 configure_media_failed_no_reply:
1908 GST_ERROR ("client %p: configure_media failed", client);
1909 /* error reply is already sent */
1910 goto cleanup_session;
1912 unsupported_transports:
1914 GST_ERROR ("client %p: unsupported transports", client);
1915 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1916 goto cleanup_transport;
1918 unsupported_client_transport:
1920 GST_ERROR ("client %p: unsupported client transport", client);
1921 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1922 goto cleanup_transport;
1926 GST_ERROR ("client %p: keymgmt error", client);
1927 send_generic_response (client, GST_RTSP_STS_KEY_MANAGEMENT_FAILURE, ctx);
1928 goto cleanup_transport;
1932 gst_rtsp_transport_free (ct);
1934 g_object_unref (session);
1941 static GstSDPMessage *
1942 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1944 GstRTSPClientPrivate *priv = client->priv;
1949 gst_sdp_message_new (&sdp);
1951 /* some standard things first */
1952 gst_sdp_message_set_version (sdp, "0");
1959 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1962 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1963 gst_sdp_message_set_information (sdp, "rtsp-server");
1964 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1965 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1966 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1967 gst_sdp_message_add_attribute (sdp, "control", "*");
1969 info.is_ipv6 = priv->is_ipv6;
1970 info.server_ip = priv->server_ip;
1972 /* create an SDP for the media object */
1973 if (!gst_rtsp_media_setup_sdp (media, sdp, &info))
1981 GST_ERROR ("client %p: could not create SDP", client);
1982 gst_sdp_message_free (sdp);
1987 /* for the describe we must generate an SDP */
1989 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
1991 GstRTSPClientPrivate *priv = client->priv;
1996 GstRTSPMedia *media;
1997 GstRTSPClientClass *klass;
1999 klass = GST_RTSP_CLIENT_GET_CLASS (client);
2004 /* check what kind of format is accepted, we don't really do anything with it
2005 * and always return SDP for now. */
2010 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
2012 if (res == GST_RTSP_ENOTIMPL)
2015 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
2019 if (!priv->mount_points)
2020 goto no_mount_points;
2022 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
2025 /* find the media object for the uri */
2026 if (!(media = find_media (client, ctx, path, NULL)))
2029 /* create an SDP for the media object on this client */
2030 if (!(sdp = klass->create_sdp (client, media)))
2033 /* we suspend after the describe */
2034 gst_rtsp_media_suspend (media);
2035 g_object_unref (media);
2037 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2038 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2040 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
2043 /* content base for some clients that might screw up creating the setup uri */
2044 str = make_base_url (client, ctx->uri, path);
2047 GST_INFO ("adding content-base: %s", str);
2048 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE, str);
2050 /* add SDP to the response body */
2051 str = gst_sdp_message_as_text (sdp);
2052 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
2053 gst_sdp_message_free (sdp);
2055 send_message (client, ctx, ctx->response, FALSE);
2057 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
2065 GST_ERROR ("client %p: no uri", client);
2066 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2071 GST_ERROR ("client %p: no mount points configured", client);
2072 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2077 GST_ERROR ("client %p: can't find path for url", client);
2078 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2083 GST_ERROR ("client %p: no media", client);
2085 /* error reply is already sent */
2090 GST_ERROR ("client %p: can't create SDP", client);
2091 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2093 g_object_unref (media);
2099 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
2101 GstRTSPMethod options;
2104 options = GST_RTSP_DESCRIBE |
2109 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
2111 str = gst_rtsp_options_as_text (options);
2113 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2114 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2116 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
2119 send_message (client, ctx, ctx->response, FALSE);
2121 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
2127 /* remove duplicate and trailing '/' */
2129 sanitize_uri (GstRTSPUrl * uri)
2133 gboolean have_slash, prev_slash;
2135 s = d = uri->abspath;
2136 len = strlen (uri->abspath);
2140 for (i = 0; i < len; i++) {
2141 have_slash = s[i] == '/';
2143 if (!have_slash || !prev_slash)
2145 prev_slash = have_slash;
2147 len = d - uri->abspath;
2148 /* don't remove the first slash if that's the only thing left */
2149 if (len > 1 && *(d - 1) == '/')
2155 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
2157 GstRTSPClientPrivate *priv = client->priv;
2159 GST_INFO ("client %p: session %p finished", client, session);
2161 /* unlink all media managed in this session */
2162 client_unlink_session (client, session);
2164 /* remove the session */
2165 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
2166 GST_INFO ("client %p: all sessions finalized, close the connection",
2168 close_connection (client);
2173 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
2175 GstRTSPClientPrivate *priv = client->priv;
2176 GstRTSPMethod method;
2177 const gchar *uristr;
2178 GstRTSPUrl *uri = NULL;
2179 GstRTSPVersion version;
2181 GstRTSPSession *session = NULL;
2182 GstRTSPContext sctx = { NULL }, *ctx;
2183 GstRTSPMessage response = { 0 };
2186 if (!(ctx = gst_rtsp_context_get_current ())) {
2188 ctx->auth = priv->auth;
2189 gst_rtsp_context_push_current (ctx);
2192 ctx->conn = priv->connection;
2193 ctx->client = client;
2194 ctx->request = request;
2195 ctx->response = &response;
2197 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2198 gst_rtsp_message_dump (request);
2201 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
2203 GST_INFO ("client %p: received a request %s %s %s", client,
2204 gst_rtsp_method_as_text (method), uristr,
2205 gst_rtsp_version_as_text (version));
2207 /* we can only handle 1.0 requests */
2208 if (version != GST_RTSP_VERSION_1_0)
2211 ctx->method = method;
2213 /* we always try to parse the url first */
2214 if (strcmp (uristr, "*") == 0) {
2215 /* special case where we have * as uri, keep uri = NULL */
2216 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
2217 /* check if the uristr is an absolute path <=> scheme and host information
2221 scheme = g_uri_parse_scheme (uristr);
2222 if (scheme == NULL && g_str_has_prefix (uristr, "/")) {
2223 gchar *absolute_uristr = NULL;
2225 GST_WARNING_OBJECT (client, "request doesn't contain absolute url");
2226 if (priv->server_ip == NULL) {
2227 GST_WARNING_OBJECT (client, "host information missing");
2232 g_strdup_printf ("rtsp://%s%s", priv->server_ip, uristr);
2234 GST_DEBUG_OBJECT (client, "absolute url: %s", absolute_uristr);
2235 if (gst_rtsp_url_parse (absolute_uristr, &uri) != GST_RTSP_OK) {
2236 g_free (absolute_uristr);
2239 g_free (absolute_uristr);
2246 /* get the session if there is any */
2247 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
2248 if (res == GST_RTSP_OK) {
2249 if (priv->session_pool == NULL)
2252 /* we had a session in the request, find it again */
2253 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2254 goto session_not_found;
2256 /* we add the session to the client list of watched sessions. When a session
2257 * disappears because it times out, we will be notified. If all sessions are
2258 * gone, we will close the connection */
2259 client_watch_session (client, session);
2262 /* sanitize the uri */
2266 ctx->session = session;
2268 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
2269 goto not_authorized;
2271 /* now see what is asked and dispatch to a dedicated handler */
2273 case GST_RTSP_OPTIONS:
2274 handle_options_request (client, ctx);
2276 case GST_RTSP_DESCRIBE:
2277 handle_describe_request (client, ctx);
2279 case GST_RTSP_SETUP:
2280 handle_setup_request (client, ctx);
2283 handle_play_request (client, ctx);
2285 case GST_RTSP_PAUSE:
2286 handle_pause_request (client, ctx);
2288 case GST_RTSP_TEARDOWN:
2289 handle_teardown_request (client, ctx);
2291 case GST_RTSP_SET_PARAMETER:
2292 handle_set_param_request (client, ctx);
2294 case GST_RTSP_GET_PARAMETER:
2295 handle_get_param_request (client, ctx);
2297 case GST_RTSP_ANNOUNCE:
2298 case GST_RTSP_RECORD:
2299 case GST_RTSP_REDIRECT:
2300 goto not_implemented;
2301 case GST_RTSP_INVALID:
2308 gst_rtsp_context_pop_current (ctx);
2310 g_object_unref (session);
2312 gst_rtsp_url_free (uri);
2318 GST_ERROR ("client %p: version %d not supported", client, version);
2319 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
2325 GST_ERROR ("client %p: bad request", client);
2326 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2331 GST_ERROR ("client %p: no pool configured", client);
2332 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2337 GST_ERROR ("client %p: session not found", client);
2338 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2343 GST_ERROR ("client %p: not allowed", client);
2344 /* error reply is already sent */
2349 GST_ERROR ("client %p: method %d not implemented", client, method);
2350 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
2357 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
2359 GstRTSPClientPrivate *priv = client->priv;
2361 GstRTSPSession *session = NULL;
2362 GstRTSPContext sctx = { NULL }, *ctx;
2365 if (!(ctx = gst_rtsp_context_get_current ())) {
2367 ctx->auth = priv->auth;
2368 gst_rtsp_context_push_current (ctx);
2371 ctx->conn = priv->connection;
2372 ctx->client = client;
2373 ctx->request = NULL;
2375 ctx->method = GST_RTSP_INVALID;
2376 ctx->response = response;
2378 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2379 gst_rtsp_message_dump (response);
2382 GST_INFO ("client %p: received a response", client);
2384 /* get the session if there is any */
2386 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
2387 if (res == GST_RTSP_OK) {
2388 if (priv->session_pool == NULL)
2391 /* we had a session in the request, find it again */
2392 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2393 goto session_not_found;
2395 /* we add the session to the client list of watched sessions. When a session
2396 * disappears because it times out, we will be notified. If all sessions are
2397 * gone, we will close the connection */
2398 client_watch_session (client, session);
2401 ctx->session = session;
2403 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
2408 gst_rtsp_context_pop_current (ctx);
2410 g_object_unref (session);
2415 GST_ERROR ("client %p: no pool configured", client);
2420 GST_ERROR ("client %p: session not found", client);
2426 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2428 GstRTSPClientPrivate *priv = client->priv;
2437 /* find the stream for this message */
2438 res = gst_rtsp_message_parse_data (message, &channel);
2439 if (res != GST_RTSP_OK)
2442 gst_rtsp_message_steal_body (message, &data, &size);
2444 buffer = gst_buffer_new_wrapped (data, size);
2447 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2448 GstRTSPStreamTransport *trans;
2449 GstRTSPStream *stream;
2450 const GstRTSPTransport *tr;
2454 tr = gst_rtsp_stream_transport_get_transport (trans);
2455 stream = gst_rtsp_stream_transport_get_stream (trans);
2457 /* check for TCP transport */
2458 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2459 /* dispatch to the stream based on the channel number */
2460 if (tr->interleaved.min == channel) {
2461 gst_rtsp_stream_recv_rtp (stream, buffer);
2464 } else if (tr->interleaved.max == channel) {
2465 gst_rtsp_stream_recv_rtcp (stream, buffer);
2472 gst_buffer_unref (buffer);
2476 * gst_rtsp_client_set_session_pool:
2477 * @client: a #GstRTSPClient
2478 * @pool: (transfer none): a #GstRTSPSessionPool
2480 * Set @pool as the sessionpool for @client which it will use to find
2481 * or allocate sessions. the sessionpool is usually inherited from the server
2482 * that created the client but can be overridden later.
2485 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2486 GstRTSPSessionPool * pool)
2488 GstRTSPSessionPool *old;
2489 GstRTSPClientPrivate *priv;
2491 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2493 priv = client->priv;
2496 g_object_ref (pool);
2498 g_mutex_lock (&priv->lock);
2499 old = priv->session_pool;
2500 priv->session_pool = pool;
2501 g_mutex_unlock (&priv->lock);
2504 g_object_unref (old);
2508 * gst_rtsp_client_get_session_pool:
2509 * @client: a #GstRTSPClient
2511 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2513 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2515 GstRTSPSessionPool *
2516 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2518 GstRTSPClientPrivate *priv;
2519 GstRTSPSessionPool *result;
2521 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2523 priv = client->priv;
2525 g_mutex_lock (&priv->lock);
2526 if ((result = priv->session_pool))
2527 g_object_ref (result);
2528 g_mutex_unlock (&priv->lock);
2534 * gst_rtsp_client_set_mount_points:
2535 * @client: a #GstRTSPClient
2536 * @mounts: (transfer none): a #GstRTSPMountPoints
2538 * Set @mounts as the mount points for @client which it will use to map urls
2539 * to media streams. These mount points are usually inherited from the server that
2540 * created the client but can be overriden later.
2543 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2544 GstRTSPMountPoints * mounts)
2546 GstRTSPClientPrivate *priv;
2547 GstRTSPMountPoints *old;
2549 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2551 priv = client->priv;
2554 g_object_ref (mounts);
2556 g_mutex_lock (&priv->lock);
2557 old = priv->mount_points;
2558 priv->mount_points = mounts;
2559 g_mutex_unlock (&priv->lock);
2562 g_object_unref (old);
2566 * gst_rtsp_client_get_mount_points:
2567 * @client: a #GstRTSPClient
2569 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2571 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2573 GstRTSPMountPoints *
2574 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2576 GstRTSPClientPrivate *priv;
2577 GstRTSPMountPoints *result;
2579 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2581 priv = client->priv;
2583 g_mutex_lock (&priv->lock);
2584 if ((result = priv->mount_points))
2585 g_object_ref (result);
2586 g_mutex_unlock (&priv->lock);
2592 * gst_rtsp_client_set_auth:
2593 * @client: a #GstRTSPClient
2594 * @auth: (transfer none): a #GstRTSPAuth
2596 * configure @auth to be used as the authentication manager of @client.
2599 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2601 GstRTSPClientPrivate *priv;
2604 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2606 priv = client->priv;
2609 g_object_ref (auth);
2611 g_mutex_lock (&priv->lock);
2614 g_mutex_unlock (&priv->lock);
2617 g_object_unref (old);
2622 * gst_rtsp_client_get_auth:
2623 * @client: a #GstRTSPClient
2625 * Get the #GstRTSPAuth used as the authentication manager of @client.
2627 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2631 gst_rtsp_client_get_auth (GstRTSPClient * client)
2633 GstRTSPClientPrivate *priv;
2634 GstRTSPAuth *result;
2636 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2638 priv = client->priv;
2640 g_mutex_lock (&priv->lock);
2641 if ((result = priv->auth))
2642 g_object_ref (result);
2643 g_mutex_unlock (&priv->lock);
2649 * gst_rtsp_client_set_thread_pool:
2650 * @client: a #GstRTSPClient
2651 * @pool: (transfer none): a #GstRTSPThreadPool
2653 * configure @pool to be used as the thread pool of @client.
2656 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2657 GstRTSPThreadPool * pool)
2659 GstRTSPClientPrivate *priv;
2660 GstRTSPThreadPool *old;
2662 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2664 priv = client->priv;
2667 g_object_ref (pool);
2669 g_mutex_lock (&priv->lock);
2670 old = priv->thread_pool;
2671 priv->thread_pool = pool;
2672 g_mutex_unlock (&priv->lock);
2675 g_object_unref (old);
2679 * gst_rtsp_client_get_thread_pool:
2680 * @client: a #GstRTSPClient
2682 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2684 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2688 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2690 GstRTSPClientPrivate *priv;
2691 GstRTSPThreadPool *result;
2693 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2695 priv = client->priv;
2697 g_mutex_lock (&priv->lock);
2698 if ((result = priv->thread_pool))
2699 g_object_ref (result);
2700 g_mutex_unlock (&priv->lock);
2706 * gst_rtsp_client_set_connection:
2707 * @client: a #GstRTSPClient
2708 * @conn: (transfer full): a #GstRTSPConnection
2710 * Set the #GstRTSPConnection of @client. This function takes ownership of
2713 * Returns: %TRUE on success.
2716 gst_rtsp_client_set_connection (GstRTSPClient * client,
2717 GstRTSPConnection * conn)
2719 GstRTSPClientPrivate *priv;
2720 GSocket *read_socket;
2721 GSocketAddress *address;
2723 GError *error = NULL;
2725 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2726 g_return_val_if_fail (conn != NULL, FALSE);
2728 priv = client->priv;
2730 read_socket = gst_rtsp_connection_get_read_socket (conn);
2732 if (!(address = g_socket_get_local_address (read_socket, &error)))
2735 g_free (priv->server_ip);
2736 /* keep the original ip that the client connected to */
2737 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2738 GInetAddress *iaddr;
2740 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2742 /* socket might be ipv6 but adress still ipv4 */
2743 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2744 priv->server_ip = g_inet_address_to_string (iaddr);
2745 g_object_unref (address);
2747 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2748 priv->server_ip = g_strdup ("unknown");
2751 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2752 priv->server_ip, priv->is_ipv6);
2754 url = gst_rtsp_connection_get_url (conn);
2755 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2757 priv->connection = conn;
2764 GST_ERROR ("could not get local address %s", error->message);
2765 g_error_free (error);
2771 * gst_rtsp_client_get_connection:
2772 * @client: a #GstRTSPClient
2774 * Get the #GstRTSPConnection of @client.
2776 * Returns: (transfer none): the #GstRTSPConnection of @client.
2777 * The connection object returned remains valid until the client is freed.
2780 gst_rtsp_client_get_connection (GstRTSPClient * client)
2782 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2784 return client->priv->connection;
2788 * gst_rtsp_client_set_send_func:
2789 * @client: a #GstRTSPClient
2790 * @func: (scope notified): a #GstRTSPClientSendFunc
2791 * @user_data: (closure): user data passed to @func
2792 * @notify: (allow-none): called when @user_data is no longer in use
2794 * Set @func as the callback that will be called when a new message needs to be
2795 * sent to the client. @user_data is passed to @func and @notify is called when
2796 * @user_data is no longer in use.
2798 * By default, the client will send the messages on the #GstRTSPConnection that
2799 * was configured with gst_rtsp_client_attach() was called.
2802 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2803 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2805 GstRTSPClientPrivate *priv;
2806 GDestroyNotify old_notify;
2809 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2811 priv = client->priv;
2813 g_mutex_lock (&priv->send_lock);
2814 priv->send_func = func;
2815 old_notify = priv->send_notify;
2816 old_data = priv->send_data;
2817 priv->send_notify = notify;
2818 priv->send_data = user_data;
2819 g_mutex_unlock (&priv->send_lock);
2822 old_notify (old_data);
2826 * gst_rtsp_client_handle_message:
2827 * @client: a #GstRTSPClient
2828 * @message: (transfer none): an #GstRTSPMessage
2830 * Let the client handle @message.
2832 * Returns: a #GstRTSPResult.
2835 gst_rtsp_client_handle_message (GstRTSPClient * client,
2836 GstRTSPMessage * message)
2838 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2839 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2841 switch (message->type) {
2842 case GST_RTSP_MESSAGE_REQUEST:
2843 handle_request (client, message);
2845 case GST_RTSP_MESSAGE_RESPONSE:
2846 handle_response (client, message);
2848 case GST_RTSP_MESSAGE_DATA:
2849 handle_data (client, message);
2858 * gst_rtsp_client_send_message:
2859 * @client: a #GstRTSPClient
2860 * @session: (transfer none): a #GstRTSPSession to send the message to or %NULL
2861 * @message: (transfer none): The #GstRTSPMessage to send
2863 * Send a message message to the remote end. @message must be a
2864 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
2867 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
2868 GstRTSPMessage * message)
2870 GstRTSPContext sctx = { NULL }
2872 GstRTSPClientPrivate *priv;
2874 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2875 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2876 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
2877 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
2879 priv = client->priv;
2881 if (!(ctx = gst_rtsp_context_get_current ())) {
2883 ctx->auth = priv->auth;
2884 gst_rtsp_context_push_current (ctx);
2887 ctx->conn = priv->connection;
2888 ctx->client = client;
2889 ctx->session = session;
2891 send_message (client, ctx, message, FALSE);
2894 gst_rtsp_context_pop_current (ctx);
2899 static GstRTSPResult
2900 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2901 gboolean close, gpointer user_data)
2903 GstRTSPClientPrivate *priv = client->priv;
2911 /* send the response and store the seq number so we can wait until it's
2912 * written to the client to close the connection */
2914 gst_rtsp_watch_send_message (priv->watch, message,
2915 close ? &priv->close_seq : NULL);
2916 if (ret == GST_RTSP_OK)
2919 if (ret != GST_RTSP_ENOMEM)
2923 if (priv->drop_backlog)
2926 /* queue was full, wait for more space */
2927 GST_DEBUG_OBJECT (client, "waiting for backlog");
2928 ret = gst_rtsp_watch_wait_backlog (priv->watch, &time);
2929 GST_DEBUG_OBJECT (client, "Resend due to backlog full");
2930 } while (ret != GST_RTSP_EINTR);
2937 GST_DEBUG_OBJECT (client, "got error %d", ret);
2942 static GstRTSPResult
2943 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2946 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2949 static GstRTSPResult
2950 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2952 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2953 GstRTSPClientPrivate *priv = client->priv;
2955 if (priv->close_seq && priv->close_seq == cseq) {
2956 priv->close_seq = 0;
2957 close_connection (client);
2963 static GstRTSPResult
2964 closed (GstRTSPWatch * watch, gpointer user_data)
2966 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2967 GstRTSPClientPrivate *priv = client->priv;
2968 const gchar *tunnelid;
2970 GST_INFO ("client %p: connection closed", client);
2972 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2973 g_mutex_lock (&tunnels_lock);
2974 /* remove from tunnelids */
2975 g_hash_table_remove (tunnels, tunnelid);
2976 g_mutex_unlock (&tunnels_lock);
2979 gst_rtsp_watch_set_flushing (watch, TRUE);
2980 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2985 static GstRTSPResult
2986 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2988 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2991 str = gst_rtsp_strresult (result);
2992 GST_INFO ("client %p: received an error %s", client, str);
2998 static GstRTSPResult
2999 error_full (GstRTSPWatch * watch, GstRTSPResult result,
3000 GstRTSPMessage * message, guint id, gpointer user_data)
3002 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3005 str = gst_rtsp_strresult (result);
3007 ("client %p: error when handling message %p with id %d: %s",
3008 client, message, id, str);
3015 remember_tunnel (GstRTSPClient * client)
3017 GstRTSPClientPrivate *priv = client->priv;
3018 const gchar *tunnelid;
3020 /* store client in the pending tunnels */
3021 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
3022 if (tunnelid == NULL)
3025 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
3027 /* we can't have two clients connecting with the same tunnelid */
3028 g_mutex_lock (&tunnels_lock);
3029 if (g_hash_table_lookup (tunnels, tunnelid))
3030 goto tunnel_existed;
3032 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
3033 g_mutex_unlock (&tunnels_lock);
3040 GST_ERROR ("client %p: no tunnelid provided", client);
3045 g_mutex_unlock (&tunnels_lock);
3046 GST_ERROR ("client %p: tunnel session %s already existed", client,
3052 static GstRTSPResult
3053 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
3055 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3056 GstRTSPClientPrivate *priv = client->priv;
3058 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
3061 /* ignore error, it'll only be a problem when the client does a POST again */
3062 remember_tunnel (client);
3068 handle_tunnel (GstRTSPClient * client)
3070 GstRTSPClientPrivate *priv = client->priv;
3071 GstRTSPClient *oclient;
3072 GstRTSPClientPrivate *opriv;
3073 const gchar *tunnelid;
3075 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
3076 if (tunnelid == NULL)
3079 /* check for previous tunnel */
3080 g_mutex_lock (&tunnels_lock);
3081 oclient = g_hash_table_lookup (tunnels, tunnelid);
3083 if (oclient == NULL) {
3084 /* no previous tunnel, remember tunnel */
3085 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
3086 g_mutex_unlock (&tunnels_lock);
3088 GST_INFO ("client %p: no previous tunnel found, remembering tunnel (%p)",
3089 client, priv->connection);
3091 /* merge both tunnels into the first client */
3092 /* remove the old client from the table. ref before because removing it will
3093 * remove the ref to it. */
3094 g_object_ref (oclient);
3095 g_hash_table_remove (tunnels, tunnelid);
3096 g_mutex_unlock (&tunnels_lock);
3098 opriv = oclient->priv;
3100 if (opriv->watch == NULL)
3103 GST_INFO ("client %p: found previous tunnel %p (old %p, new %p)", client,
3104 oclient, opriv->connection, priv->connection);
3106 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
3107 gst_rtsp_watch_reset (priv->watch);
3108 gst_rtsp_watch_reset (opriv->watch);
3109 g_object_unref (oclient);
3111 /* the old client owns the tunnel now, the new one will be freed */
3112 g_source_destroy ((GSource *) priv->watch);
3114 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
3122 GST_ERROR ("client %p: no tunnelid provided", client);
3127 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
3128 g_object_unref (oclient);
3133 static GstRTSPStatusCode
3134 tunnel_get (GstRTSPWatch * watch, gpointer user_data)
3136 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3138 GST_INFO ("client %p: tunnel get (connection %p)", client,
3139 client->priv->connection);
3141 if (!handle_tunnel (client)) {
3142 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
3145 return GST_RTSP_STS_OK;
3148 static GstRTSPResult
3149 tunnel_post (GstRTSPWatch * watch, gpointer user_data)
3151 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3153 GST_INFO ("client %p: tunnel post (connection %p)", client,
3154 client->priv->connection);
3156 if (!handle_tunnel (client)) {
3157 return GST_RTSP_ERROR;
3163 static GstRTSPResult
3164 tunnel_http_response (GstRTSPWatch * watch, GstRTSPMessage * request,
3165 GstRTSPMessage * response, gpointer user_data)
3167 GstRTSPClientClass *klass;
3169 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3170 klass = GST_RTSP_CLIENT_GET_CLASS (client);
3172 if (klass->tunnel_http_response) {
3173 klass->tunnel_http_response (client, request, response);
3179 static GstRTSPWatchFuncs watch_funcs = {
3188 tunnel_http_response
3192 client_watch_notify (GstRTSPClient * client)
3194 GstRTSPClientPrivate *priv = client->priv;
3196 GST_INFO ("client %p: watch destroyed", client);
3198 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
3199 g_object_unref (client);
3203 * gst_rtsp_client_attach:
3204 * @client: a #GstRTSPClient
3205 * @context: (allow-none): a #GMainContext
3207 * Attaches @client to @context. When the mainloop for @context is run, the
3208 * client will be dispatched. When @context is %NULL, the default context will be
3211 * This function should be called when the client properties and urls are fully
3212 * configured and the client is ready to start.
3214 * Returns: the ID (greater than 0) for the source within the GMainContext.
3217 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
3219 GstRTSPClientPrivate *priv;
3222 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
3223 priv = client->priv;
3224 g_return_val_if_fail (priv->connection != NULL, 0);
3225 g_return_val_if_fail (priv->watch == NULL, 0);
3227 /* create watch for the connection and attach */
3228 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
3229 g_object_ref (client), (GDestroyNotify) client_watch_notify);
3230 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
3231 (GDestroyNotify) gst_rtsp_watch_unref);
3233 /* FIXME make this configurable. We don't want to do this yet because it will
3234 * be superceeded by a cache object later */
3235 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
3237 GST_INFO ("attaching to context %p", context);
3238 res = gst_rtsp_watch_attach (priv->watch, context);
3244 * gst_rtsp_client_session_filter:
3245 * @client: a #GstRTSPClient
3246 * @func: (scope call) (allow-none): a callback
3247 * @user_data: user data passed to @func
3249 * Call @func for each session managed by @client. The result value of @func
3250 * determines what happens to the session. @func will be called with @client
3251 * locked so no further actions on @client can be performed from @func.
3253 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
3256 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
3258 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
3259 * will also be added with an additional ref to the result #GList of this
3262 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each session.
3264 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
3265 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3266 * element in the #GList should be unreffed before the list is freed.
3269 gst_rtsp_client_session_filter (GstRTSPClient * client,
3270 GstRTSPClientSessionFilterFunc func, gpointer user_data)
3272 GstRTSPClientPrivate *priv;
3273 GList *result, *walk, *next;
3275 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
3277 priv = client->priv;
3281 g_mutex_lock (&priv->lock);
3282 for (walk = priv->sessions; walk; walk = next) {
3283 GstRTSPSession *sess = walk->data;
3284 GstRTSPFilterResult res;
3286 next = g_list_next (walk);
3289 res = func (client, sess, user_data);
3291 res = GST_RTSP_FILTER_REF;
3294 case GST_RTSP_FILTER_REMOVE:
3295 /* stop watching the session and pretent it went away */
3296 client_cleanup_session (client, sess);
3298 case GST_RTSP_FILTER_REF:
3299 result = g_list_prepend (result, g_object_ref (sess));
3301 case GST_RTSP_FILTER_KEEP:
3306 g_mutex_unlock (&priv->lock);