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 "rtsp-client.h"
47 #include "rtsp-params.h"
49 #define GST_RTSP_CLIENT_GET_PRIVATE(obj) \
50 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_CLIENT, GstRTSPClientPrivate))
53 * send_lock, lock, tunnels_lock
56 struct _GstRTSPClientPrivate
58 GMutex lock; /* protects everything else */
60 GstRTSPConnection *connection;
66 GstRTSPClientSendFunc send_func; /* protected by send_lock */
67 gpointer send_data; /* protected by send_lock */
68 GDestroyNotify send_notify; /* protected by send_lock */
70 GstRTSPSessionPool *session_pool;
71 GstRTSPMountPoints *mount_points;
73 GstRTSPThreadPool *thread_pool;
75 /* used to cache the media in the last requested DESCRIBE so that
76 * we can pick it up in the next SETUP immediately */
84 static GMutex tunnels_lock;
85 static GHashTable *tunnels; /* protected by tunnels_lock */
87 #define DEFAULT_SESSION_POOL NULL
88 #define DEFAULT_MOUNT_POINTS NULL
102 SIGNAL_OPTIONS_REQUEST,
103 SIGNAL_DESCRIBE_REQUEST,
104 SIGNAL_SETUP_REQUEST,
106 SIGNAL_PAUSE_REQUEST,
107 SIGNAL_TEARDOWN_REQUEST,
108 SIGNAL_SET_PARAMETER_REQUEST,
109 SIGNAL_GET_PARAMETER_REQUEST,
110 SIGNAL_HANDLE_RESPONSE,
114 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
115 #define GST_CAT_DEFAULT rtsp_client_debug
117 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
119 static void gst_rtsp_client_get_property (GObject * object, guint propid,
120 GValue * value, GParamSpec * pspec);
121 static void gst_rtsp_client_set_property (GObject * object, guint propid,
122 const GValue * value, GParamSpec * pspec);
123 static void gst_rtsp_client_finalize (GObject * obj);
125 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
126 static void client_session_finalized (GstRTSPClient * client,
127 GstRTSPSession * session);
128 static void unlink_session_transports (GstRTSPClient * client,
129 GstRTSPSession * session, GstRTSPSessionMedia * sessmedia);
130 static gboolean default_configure_client_transport (GstRTSPClient * client,
131 GstRTSPContext * ctx, GstRTSPTransport * ct);
132 static GstRTSPResult default_params_set (GstRTSPClient * client,
133 GstRTSPContext * ctx);
134 static GstRTSPResult default_params_get (GstRTSPClient * client,
135 GstRTSPContext * ctx);
137 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
140 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
142 GObjectClass *gobject_class;
144 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
146 gobject_class = G_OBJECT_CLASS (klass);
148 gobject_class->get_property = gst_rtsp_client_get_property;
149 gobject_class->set_property = gst_rtsp_client_set_property;
150 gobject_class->finalize = gst_rtsp_client_finalize;
152 klass->create_sdp = create_sdp;
153 klass->configure_client_transport = default_configure_client_transport;
154 klass->params_set = default_params_set;
155 klass->params_get = default_params_get;
157 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
158 g_param_spec_object ("session-pool", "Session Pool",
159 "The session pool to use for client session",
160 GST_TYPE_RTSP_SESSION_POOL,
161 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
163 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
164 g_param_spec_object ("mount-points", "Mount Points",
165 "The mount points to use for client session",
166 GST_TYPE_RTSP_MOUNT_POINTS,
167 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
169 gst_rtsp_client_signals[SIGNAL_CLOSED] =
170 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
171 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
172 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
174 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
175 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
176 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
177 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
179 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
180 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
181 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
182 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
185 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
186 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
187 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
188 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
191 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
192 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
193 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
194 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
197 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
198 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
199 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
200 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
203 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
204 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
205 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
206 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
209 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
210 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
211 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
212 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
215 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
216 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
217 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
218 set_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
219 G_TYPE_NONE, 1, G_TYPE_POINTER);
221 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
222 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
223 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
224 get_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
225 G_TYPE_NONE, 1, G_TYPE_POINTER);
227 gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE] =
228 g_signal_new ("handle-response", G_TYPE_FROM_CLASS (klass),
229 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
230 handle_response), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
231 G_TYPE_NONE, 1, G_TYPE_POINTER);
234 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
235 g_mutex_init (&tunnels_lock);
237 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
241 gst_rtsp_client_init (GstRTSPClient * client)
243 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
247 g_mutex_init (&priv->lock);
248 g_mutex_init (&priv->send_lock);
252 static GstRTSPFilterResult
253 filter_session (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
256 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
258 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
259 unlink_session_transports (client, sess, sessmedia);
261 /* unmanage the media in the session */
262 return GST_RTSP_FILTER_REMOVE;
266 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
268 /* unlink all media managed in this session */
269 gst_rtsp_session_filter (session, filter_session, client);
273 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
275 GstRTSPClientPrivate *priv = client->priv;
278 for (walk = priv->sessions; walk; walk = g_list_next (walk)) {
279 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
281 /* we already know about this session */
282 if (msession == session)
286 GST_INFO ("watching session %p", session);
288 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
290 priv->sessions = g_list_prepend (priv->sessions, session);
294 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session)
296 GstRTSPClientPrivate *priv = client->priv;
298 GST_INFO ("unwatching session %p", session);
300 g_object_weak_unref (G_OBJECT (session),
301 (GWeakNotify) client_session_finalized, client);
302 priv->sessions = g_list_remove (priv->sessions, session);
306 client_cleanup_session (GstRTSPClient * client, GstRTSPSession * session)
308 g_object_weak_unref (G_OBJECT (session),
309 (GWeakNotify) client_session_finalized, client);
310 client_unlink_session (client, session);
314 client_cleanup_sessions (GstRTSPClient * client)
316 GstRTSPClientPrivate *priv = client->priv;
319 /* remove weak-ref from sessions */
320 for (sessions = priv->sessions; sessions; sessions = g_list_next (sessions)) {
321 client_cleanup_session (client, (GstRTSPSession *) sessions->data);
323 g_list_free (priv->sessions);
324 priv->sessions = NULL;
327 /* A client is finalized when the connection is broken */
329 gst_rtsp_client_finalize (GObject * obj)
331 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
332 GstRTSPClientPrivate *priv = client->priv;
334 GST_INFO ("finalize client %p", client);
336 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
339 g_source_destroy ((GSource *) priv->watch);
341 client_cleanup_sessions (client);
343 if (priv->connection)
344 gst_rtsp_connection_free (priv->connection);
345 if (priv->session_pool)
346 g_object_unref (priv->session_pool);
347 if (priv->mount_points)
348 g_object_unref (priv->mount_points);
350 g_object_unref (priv->auth);
351 if (priv->thread_pool)
352 g_object_unref (priv->thread_pool);
357 gst_rtsp_media_unprepare (priv->media);
358 g_object_unref (priv->media);
361 g_free (priv->server_ip);
362 g_mutex_clear (&priv->lock);
363 g_mutex_clear (&priv->send_lock);
365 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
369 gst_rtsp_client_get_property (GObject * object, guint propid,
370 GValue * value, GParamSpec * pspec)
372 GstRTSPClient *client = GST_RTSP_CLIENT (object);
375 case PROP_SESSION_POOL:
376 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
378 case PROP_MOUNT_POINTS:
379 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
382 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
387 gst_rtsp_client_set_property (GObject * object, guint propid,
388 const GValue * value, GParamSpec * pspec)
390 GstRTSPClient *client = GST_RTSP_CLIENT (object);
393 case PROP_SESSION_POOL:
394 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
396 case PROP_MOUNT_POINTS:
397 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
400 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
405 * gst_rtsp_client_new:
407 * Create a new #GstRTSPClient instance.
409 * Returns: a new #GstRTSPClient
412 gst_rtsp_client_new (void)
414 GstRTSPClient *result;
416 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
422 send_message (GstRTSPClient * client, GstRTSPSession * session,
423 GstRTSPMessage * message, gboolean close)
425 GstRTSPClientPrivate *priv = client->priv;
427 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
428 "GStreamer RTSP server");
430 /* remove any previous header */
431 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
433 /* add the new session header for new session ids */
435 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
436 gst_rtsp_session_get_header (session));
439 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
440 gst_rtsp_message_dump (message);
444 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
446 g_mutex_lock (&priv->send_lock);
448 priv->send_func (client, message, close, priv->send_data);
449 g_mutex_unlock (&priv->send_lock);
451 gst_rtsp_message_unset (message);
455 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
456 GstRTSPContext * ctx)
458 gst_rtsp_message_init_response (ctx->response, code,
459 gst_rtsp_status_as_text (code), ctx->request);
461 send_message (client, NULL, ctx->response, FALSE);
465 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
467 if (path1 == NULL || path2 == NULL)
470 if (strlen (path1) != len2)
473 if (strncmp (path1, path2, len2))
479 /* this function is called to initially find the media for the DESCRIBE request
480 * but is cached for when the same client (without breaking the connection) is
481 * doing a setup for the exact same url. */
482 static GstRTSPMedia *
483 find_media (GstRTSPClient * client, GstRTSPContext * ctx, gint * matched)
485 GstRTSPClientPrivate *priv = client->priv;
486 GstRTSPMediaFactory *factory;
491 if (!priv->mount_points)
492 goto no_mount_points;
494 path = ctx->uri->abspath;
496 /* find the longest matching factory for the uri first */
497 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
501 ctx->factory = factory;
503 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
504 goto no_factory_access;
506 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
512 path_len = strlen (path);
514 if (!paths_are_equal (priv->path, path, path_len)) {
515 GstRTSPThread *thread;
517 /* remove any previously cached values before we try to construct a new
523 gst_rtsp_media_unprepare (priv->media);
524 g_object_unref (priv->media);
528 /* prepare the media and add it to the pipeline */
529 if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
534 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
535 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
539 /* prepare the media */
540 if (!(gst_rtsp_media_prepare (media, thread)))
543 /* now keep track of the uri and the media */
544 priv->path = g_strndup (path, path_len);
547 /* we have seen this path before, used cached media */
550 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
553 g_object_unref (factory);
557 g_object_ref (media);
564 GST_ERROR ("client %p: no mount points configured", client);
565 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
570 GST_ERROR ("client %p: no factory for uri %s", client, path);
571 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
576 GST_ERROR ("client %p: not authorized to see factory uri %s", client, path);
581 GST_ERROR ("client %p: not authorized for factory uri %s", client, path);
586 GST_ERROR ("client %p: can't create media", client);
587 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
588 g_object_unref (factory);
594 GST_ERROR ("client %p: can't create thread", client);
595 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
596 g_object_unref (media);
598 g_object_unref (factory);
604 GST_ERROR ("client %p: can't prepare media", client);
605 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
606 g_object_unref (media);
608 g_object_unref (factory);
615 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
617 GstRTSPClientPrivate *priv = client->priv;
618 GstRTSPMessage message = { 0 };
623 gst_rtsp_message_init_data (&message, channel);
625 /* FIXME, need some sort of iovec RTSPMessage here */
626 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
629 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
631 g_mutex_lock (&priv->send_lock);
633 priv->send_func (client, &message, FALSE, priv->send_data);
634 g_mutex_unlock (&priv->send_lock);
636 gst_rtsp_message_steal_body (&message, &data, &usize);
637 gst_buffer_unmap (buffer, &map_info);
639 gst_rtsp_message_unset (&message);
645 link_transport (GstRTSPClient * client, GstRTSPSession * session,
646 GstRTSPStreamTransport * trans)
648 GstRTSPClientPrivate *priv = client->priv;
650 GST_DEBUG ("client %p: linking transport %p", client, trans);
652 gst_rtsp_stream_transport_set_callbacks (trans,
653 (GstRTSPSendFunc) do_send_data,
654 (GstRTSPSendFunc) do_send_data, client, NULL);
656 priv->transports = g_list_prepend (priv->transports, trans);
658 /* make sure our session can't expire */
659 gst_rtsp_session_prevent_expire (session);
663 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
664 GstRTSPStreamTransport * trans)
666 GstRTSPClientPrivate *priv = client->priv;
668 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
670 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
672 priv->transports = g_list_remove (priv->transports, trans);
674 /* our session can now expire */
675 gst_rtsp_session_allow_expire (session);
679 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
680 GstRTSPSessionMedia * sessmedia)
685 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
686 for (i = 0; i < n_streams; i++) {
687 GstRTSPStreamTransport *trans;
688 const GstRTSPTransport *tr;
690 /* get the transport, if there is no transport configured, skip this stream */
691 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
695 tr = gst_rtsp_stream_transport_get_transport (trans);
697 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
698 /* for TCP, unlink the stream from the TCP connection of the client */
699 unlink_transport (client, session, trans);
705 close_connection (GstRTSPClient * client)
707 GstRTSPClientPrivate *priv = client->priv;
708 const gchar *tunnelid;
710 GST_DEBUG ("client %p: closing connection", client);
712 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
713 g_mutex_lock (&tunnels_lock);
714 /* remove from tunnelids */
715 g_hash_table_remove (tunnels, tunnelid);
716 g_mutex_unlock (&tunnels_lock);
719 gst_rtsp_connection_close (priv->connection);
723 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
725 GstRTSPClientPrivate *priv = client->priv;
726 GstRTSPSession *session;
727 GstRTSPSessionMedia *sessmedia;
728 GstRTSPStatusCode code;
735 session = ctx->session;
740 path = ctx->uri->abspath;
742 /* get a handle to the configuration of the media in the session */
743 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
747 /* only aggregate control for now.. */
748 if (path[matched] != '\0')
751 ctx->sessmedia = sessmedia;
753 /* we emit the signal before closing the connection */
754 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
757 /* unlink the all TCP callbacks */
758 unlink_session_transports (client, session, sessmedia);
760 /* remove the session from the watched sessions */
761 client_unwatch_session (client, session);
763 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
765 /* unmanage the media in the session, returns false if all media session
767 if (!gst_rtsp_session_release_media (session, sessmedia)) {
768 /* remove the session */
769 gst_rtsp_session_pool_remove (priv->session_pool, session);
771 /* construct the response now */
772 code = GST_RTSP_STS_OK;
773 gst_rtsp_message_init_response (ctx->response, code,
774 gst_rtsp_status_as_text (code), ctx->request);
776 send_message (client, session, ctx->response, TRUE);
783 GST_ERROR ("client %p: no session", client);
784 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
789 GST_ERROR ("client %p: no uri supplied", client);
790 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
795 GST_ERROR ("client %p: no media for uri", client);
796 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
801 GST_ERROR ("client %p: no aggregate path %s", client, path);
802 send_generic_response (client,
803 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
809 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
813 res = gst_rtsp_params_set (client, ctx);
819 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
823 res = gst_rtsp_params_get (client, ctx);
829 handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
835 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
836 if (res != GST_RTSP_OK)
840 /* no body, keep-alive request */
841 send_generic_response (client, GST_RTSP_STS_OK, ctx);
843 /* there is a body, handle the params */
844 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
845 if (res != GST_RTSP_OK)
848 send_message (client, ctx->session, ctx->response, FALSE);
851 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
859 GST_ERROR ("client %p: bad request", client);
860 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
866 handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
872 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
873 if (res != GST_RTSP_OK)
877 /* no body, keep-alive request */
878 send_generic_response (client, GST_RTSP_STS_OK, ctx);
880 /* there is a body, handle the params */
881 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
882 if (res != GST_RTSP_OK)
885 send_message (client, ctx->session, ctx->response, FALSE);
888 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
896 GST_ERROR ("client %p: bad request", client);
897 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
903 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
905 GstRTSPSession *session;
906 GstRTSPSessionMedia *sessmedia;
907 GstRTSPStatusCode code;
908 GstRTSPState rtspstate;
912 if (!(session = ctx->session))
918 path = ctx->uri->abspath;
920 /* get a handle to the configuration of the media in the session */
921 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
925 if (path[matched] != '\0')
928 ctx->sessmedia = sessmedia;
930 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
931 /* the session state must be playing or recording */
932 if (rtspstate != GST_RTSP_STATE_PLAYING &&
933 rtspstate != GST_RTSP_STATE_RECORDING)
936 /* unlink the all TCP callbacks */
937 unlink_session_transports (client, session, sessmedia);
939 /* then pause sending */
940 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
942 /* construct the response now */
943 code = GST_RTSP_STS_OK;
944 gst_rtsp_message_init_response (ctx->response, code,
945 gst_rtsp_status_as_text (code), ctx->request);
947 send_message (client, session, ctx->response, FALSE);
949 /* the state is now READY */
950 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
952 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
959 GST_ERROR ("client %p: no seesion", client);
960 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
965 GST_ERROR ("client %p: no uri supplied", client);
966 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
971 GST_ERROR ("client %p: no media for uri", client);
972 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
977 GST_ERROR ("client %p: no aggregate path %s", client, path);
978 send_generic_response (client,
979 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
984 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
985 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
992 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
994 GstRTSPSession *session;
995 GstRTSPSessionMedia *sessmedia;
997 GstRTSPStatusCode code;
999 guint n_streams, i, infocount;
1001 GstRTSPTimeRange *range;
1003 GstRTSPState rtspstate;
1004 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1008 if (!(session = ctx->session))
1014 path = ctx->uri->abspath;
1016 /* get a handle to the configuration of the media in the session */
1017 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1021 if (path[matched] != '\0')
1024 ctx->sessmedia = sessmedia;
1025 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1027 /* the session state must be playing or ready */
1028 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1029 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1032 /* parse the range header if we have one */
1033 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1034 if (res == GST_RTSP_OK) {
1035 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1036 /* we have a range, seek to the position */
1038 gst_rtsp_media_seek (media, range);
1039 gst_rtsp_range_free (range);
1043 /* grab RTPInfo from the payloaders now */
1044 rtpinfo = g_string_new ("");
1046 n_streams = gst_rtsp_media_n_streams (media);
1047 for (i = 0, infocount = 0; i < n_streams; i++) {
1048 GstRTSPStreamTransport *trans;
1049 GstRTSPStream *stream;
1050 const GstRTSPTransport *tr;
1054 /* get the transport, if there is no transport configured, skip this stream */
1055 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
1056 if (trans == NULL) {
1057 GST_INFO ("stream %d is not configured", i);
1060 tr = gst_rtsp_stream_transport_get_transport (trans);
1062 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1063 /* for TCP, link the stream to the TCP connection of the client */
1064 link_transport (client, session, trans);
1067 stream = gst_rtsp_stream_transport_get_stream (trans);
1068 if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
1070 g_string_append (rtpinfo, ", ");
1072 uristr = gst_rtsp_url_get_request_uri (ctx->uri);
1073 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
1074 uristr, i, seq, rtptime);
1079 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
1083 /* construct the response now */
1084 code = GST_RTSP_STS_OK;
1085 gst_rtsp_message_init_response (ctx->response, code,
1086 gst_rtsp_status_as_text (code), ctx->request);
1088 /* add the RTP-Info header */
1089 if (infocount > 0) {
1090 str = g_string_free (rtpinfo, FALSE);
1091 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO, str);
1093 g_string_free (rtpinfo, TRUE);
1097 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1099 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1101 send_message (client, session, ctx->response, FALSE);
1103 /* start playing after sending the request */
1104 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1106 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1108 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1115 GST_ERROR ("client %p: no session", client);
1116 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1121 GST_ERROR ("client %p: no uri supplied", client);
1122 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1127 GST_ERROR ("client %p: media not found", client);
1128 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1133 GST_ERROR ("client %p: no aggregate path %s", client, path);
1134 send_generic_response (client,
1135 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1140 GST_ERROR ("client %p: not PLAYING or READY", client);
1141 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1148 do_keepalive (GstRTSPSession * session)
1150 GST_INFO ("keep session %p alive", session);
1151 gst_rtsp_session_touch (session);
1154 /* parse @transport and return a valid transport in @tr. only transports
1155 * from @supported are returned. Returns FALSE if no valid transport
1158 parse_transport (const char *transport, GstRTSPLowerTrans supported,
1159 GstRTSPTransport * tr)
1166 gst_rtsp_transport_init (tr);
1168 GST_DEBUG ("parsing transports %s", transport);
1170 transports = g_strsplit (transport, ",", 0);
1172 /* loop through the transports, try to parse */
1173 for (i = 0; transports[i]; i++) {
1174 res = gst_rtsp_transport_parse (transports[i], tr);
1175 if (res != GST_RTSP_OK) {
1176 /* no valid transport, search some more */
1177 GST_WARNING ("could not parse transport %s", transports[i]);
1181 /* we have a transport, see if it's RTP/AVP */
1182 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1183 GST_WARNING ("invalid transport %s", transports[i]);
1187 if (!(tr->lower_transport & supported)) {
1188 GST_WARNING ("unsupported transport %s", transports[i]);
1192 /* we have a valid transport */
1193 GST_INFO ("found valid transport %s", transports[i]);
1198 gst_rtsp_transport_init (tr);
1200 g_strfreev (transports);
1206 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
1207 GstRTSPMessage * request)
1209 gchar *blocksize_str;
1210 gboolean ret = TRUE;
1212 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1213 &blocksize_str, 0) == GST_RTSP_OK) {
1217 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1218 if (end == blocksize_str) {
1219 GST_ERROR ("failed to parse blocksize");
1222 /* we don't want to change the mtu when this media
1223 * can be shared because it impacts other clients */
1224 if (gst_rtsp_media_is_shared (media))
1227 if (blocksize > G_MAXUINT)
1228 blocksize = G_MAXUINT;
1229 gst_rtsp_stream_set_mtu (stream, blocksize);
1236 default_configure_client_transport (GstRTSPClient * client,
1237 GstRTSPContext * ctx, GstRTSPTransport * ct)
1239 GstRTSPClientPrivate *priv = client->priv;
1241 /* we have a valid transport now, set the destination of the client. */
1242 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1243 gboolean use_client_settings;
1245 use_client_settings =
1246 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1248 if (ct->destination && use_client_settings) {
1249 GstRTSPAddress *addr;
1251 addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1252 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1257 gst_rtsp_address_free (addr);
1259 GstRTSPAddress *addr;
1260 GSocketFamily family;
1262 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1264 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1268 g_free (ct->destination);
1269 ct->destination = g_strdup (addr->address);
1270 ct->port.min = addr->port;
1271 ct->port.max = addr->port + addr->n_ports - 1;
1272 ct->ttl = addr->ttl;
1274 gst_rtsp_address_free (addr);
1279 url = gst_rtsp_connection_get_url (priv->connection);
1280 g_free (ct->destination);
1281 ct->destination = g_strdup (url->host);
1283 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1284 /* check if the client selected channels for TCP */
1285 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1286 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1296 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1301 static GstRTSPTransport *
1302 make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
1303 GstRTSPTransport * ct)
1305 GstRTSPTransport *st;
1307 GSocketFamily family;
1309 /* prepare the server transport */
1310 gst_rtsp_transport_new (&st);
1312 st->trans = ct->trans;
1313 st->profile = ct->profile;
1314 st->lower_transport = ct->lower_transport;
1316 addr = g_inet_address_new_from_string (ct->destination);
1319 GST_ERROR ("failed to get inet addr from client destination");
1320 family = G_SOCKET_FAMILY_IPV4;
1322 family = g_inet_address_get_family (addr);
1323 g_object_unref (addr);
1327 switch (st->lower_transport) {
1328 case GST_RTSP_LOWER_TRANS_UDP:
1329 st->client_port = ct->client_port;
1330 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
1332 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1333 st->port = ct->port;
1334 st->destination = g_strdup (ct->destination);
1337 case GST_RTSP_LOWER_TRANS_TCP:
1338 st->interleaved = ct->interleaved;
1343 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
1349 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
1351 GstRTSPClientPrivate *priv = client->priv;
1355 GstRTSPTransport *ct, *st;
1356 GstRTSPLowerTrans supported;
1357 GstRTSPStatusCode code;
1358 GstRTSPSession *session;
1359 GstRTSPStreamTransport *trans;
1361 GstRTSPSessionMedia *sessmedia;
1362 GstRTSPMedia *media;
1363 GstRTSPStream *stream;
1364 GstRTSPState rtspstate;
1365 GstRTSPClientClass *klass;
1366 gchar *path, *control;
1373 path = uri->abspath;
1375 /* parse the transport */
1377 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
1379 if (res != GST_RTSP_OK)
1382 /* we create the session after parsing stuff so that we don't make
1383 * a session for malformed requests */
1384 if (priv->session_pool == NULL)
1387 session = ctx->session;
1390 g_object_ref (session);
1391 /* get a handle to the configuration of the media in the session, this can
1392 * return NULL if this is a new url to manage in this session. */
1393 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1395 /* we need a new media configuration in this session */
1399 /* we have no session media, find one and manage it */
1400 if (sessmedia == NULL) {
1401 /* get a handle to the configuration of the media in the session */
1402 media = find_media (client, ctx, &matched);
1404 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1405 g_object_ref (media);
1407 /* no media, not found then */
1409 goto media_not_found;
1411 /* path is what matched. We can modify the parsed uri in place */
1412 path[matched] = '\0';
1413 /* control is remainder */
1414 control = &path[matched + 1];
1416 /* find the stream now using the control part */
1417 stream = gst_rtsp_media_find_stream (media, control);
1419 goto stream_not_found;
1421 /* now we have a uri identifying a valid media and stream */
1422 ctx->stream = stream;
1425 if (session == NULL) {
1426 /* create a session if this fails we probably reached our session limit or
1428 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1429 goto service_unavailable;
1431 /* make sure this client is closed when the session is closed */
1432 client_watch_session (client, session);
1434 /* signal new session */
1435 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1438 ctx->session = session;
1441 if (sessmedia == NULL) {
1442 /* manage the media in our session now, if not done already */
1443 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1444 /* if we stil have no media, error */
1445 if (sessmedia == NULL)
1446 goto sessmedia_unavailable;
1448 g_object_unref (media);
1451 ctx->sessmedia = sessmedia;
1453 /* set blocksize on this stream */
1454 if (!handle_blocksize (media, stream, ctx->request))
1455 goto invalid_blocksize;
1457 gst_rtsp_transport_new (&ct);
1459 /* our supported transports */
1460 supported = gst_rtsp_stream_get_protocols (stream);
1462 /* parse and find a usable supported transport */
1463 if (!parse_transport (transport, supported, ct))
1464 goto unsupported_transports;
1466 /* update the client transport */
1467 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1468 if (!klass->configure_client_transport (client, ctx, ct))
1469 goto unsupported_client_transport;
1471 /* set in the session media transport */
1472 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1474 /* configure keepalive for this transport */
1475 gst_rtsp_stream_transport_set_keepalive (trans,
1476 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1478 /* create and serialize the server transport */
1479 st = make_server_transport (client, ctx, ct);
1480 trans_str = gst_rtsp_transport_as_text (st);
1481 gst_rtsp_transport_free (st);
1483 /* construct the response now */
1484 code = GST_RTSP_STS_OK;
1485 gst_rtsp_message_init_response (ctx->response, code,
1486 gst_rtsp_status_as_text (code), ctx->request);
1488 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
1492 send_message (client, session, ctx->response, FALSE);
1494 /* update the state */
1495 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1496 switch (rtspstate) {
1497 case GST_RTSP_STATE_PLAYING:
1498 case GST_RTSP_STATE_RECORDING:
1499 case GST_RTSP_STATE_READY:
1500 /* no state change */
1503 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1506 g_object_unref (session);
1508 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
1515 GST_ERROR ("client %p: no uri", client);
1516 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1521 GST_ERROR ("client %p: no transport", client);
1522 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1527 GST_ERROR ("client %p: no session pool configured", client);
1528 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1533 GST_ERROR ("client %p: media '%s' not found", client, path);
1534 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1539 GST_ERROR ("client %p: stream '%s' not found", client, control);
1540 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1541 g_object_unref (media);
1544 service_unavailable:
1546 GST_ERROR ("client %p: can't create session", client);
1547 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1548 g_object_unref (media);
1551 sessmedia_unavailable:
1553 GST_ERROR ("client %p: can't create session media", client);
1554 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1555 g_object_unref (media);
1556 g_object_unref (session);
1561 GST_ERROR ("client %p: invalid blocksize", client);
1562 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1563 g_object_unref (session);
1566 unsupported_transports:
1568 GST_ERROR ("client %p: unsupported transports", client);
1569 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1570 gst_rtsp_transport_free (ct);
1571 g_object_unref (session);
1574 unsupported_client_transport:
1576 GST_ERROR ("client %p: unsupported client transport", client);
1577 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1578 gst_rtsp_transport_free (ct);
1579 g_object_unref (session);
1584 static GstSDPMessage *
1585 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1587 GstRTSPClientPrivate *priv = client->priv;
1592 gst_sdp_message_new (&sdp);
1594 /* some standard things first */
1595 gst_sdp_message_set_version (sdp, "0");
1602 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1605 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1606 gst_sdp_message_set_information (sdp, "rtsp-server");
1607 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1608 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1609 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1610 gst_sdp_message_add_attribute (sdp, "control", "*");
1612 info.is_ipv6 = priv->is_ipv6;
1613 info.server_ip = priv->server_ip;
1615 /* create an SDP for the media object */
1616 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1624 GST_ERROR ("client %p: could not create SDP", client);
1625 gst_sdp_message_free (sdp);
1630 /* for the describe we must generate an SDP */
1632 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
1637 gchar *str, *str_query, *content_base;
1638 GstRTSPMedia *media;
1639 GstRTSPClientClass *klass;
1641 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1646 /* check what kind of format is accepted, we don't really do anything with it
1647 * and always return SDP for now. */
1652 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
1654 if (res == GST_RTSP_ENOTIMPL)
1657 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1661 /* find the media object for the uri */
1662 if (!(media = find_media (client, ctx, NULL)))
1665 /* create an SDP for the media object on this client */
1666 if (!(sdp = klass->create_sdp (client, media)))
1669 g_object_unref (media);
1671 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
1672 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
1674 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
1677 /* content base for some clients that might screw up creating the setup uri */
1678 str = gst_rtsp_url_get_request_uri (ctx->uri);
1679 str_len = strlen (str);
1681 /* check for query part */
1682 if (ctx->uri->query != NULL) {
1683 str_query = g_strrstr (str, "?");
1685 str_len = strlen (str);
1688 /* check for trailing '/' and append one */
1689 if (str[str_len - 1] != '/') {
1690 content_base = g_malloc (str_len + 2);
1691 memcpy (content_base, str, str_len);
1692 content_base[str_len] = '/';
1693 content_base[str_len + 1] = '\0';
1699 GST_INFO ("adding content-base: %s", content_base);
1701 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE,
1703 g_free (content_base);
1705 /* add SDP to the response body */
1706 str = gst_sdp_message_as_text (sdp);
1707 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
1708 gst_sdp_message_free (sdp);
1710 send_message (client, ctx->session, ctx->response, FALSE);
1712 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1720 GST_ERROR ("client %p: no uri", client);
1721 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1726 GST_ERROR ("client %p: no media", client);
1727 /* error reply is already sent */
1732 GST_ERROR ("client %p: can't create SDP", client);
1733 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1734 g_object_unref (media);
1740 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
1742 GstRTSPMethod options;
1745 options = GST_RTSP_DESCRIBE |
1750 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1752 str = gst_rtsp_options_as_text (options);
1754 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
1755 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
1757 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
1760 send_message (client, ctx->session, ctx->response, FALSE);
1762 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1768 /* remove duplicate and trailing '/' */
1770 sanitize_uri (GstRTSPUrl * uri)
1774 gboolean have_slash, prev_slash;
1776 s = d = uri->abspath;
1777 len = strlen (uri->abspath);
1781 for (i = 0; i < len; i++) {
1782 have_slash = s[i] == '/';
1784 if (!have_slash || !prev_slash)
1786 prev_slash = have_slash;
1788 len = d - uri->abspath;
1789 /* don't remove the first slash if that's the only thing left */
1790 if (len > 1 && *(d - 1) == '/')
1796 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1798 GstRTSPClientPrivate *priv = client->priv;
1800 GST_INFO ("client %p: session %p finished", client, session);
1802 /* unlink all media managed in this session */
1803 client_unlink_session (client, session);
1805 /* remove the session */
1806 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1807 GST_INFO ("client %p: all sessions finalized, close the connection",
1809 close_connection (client);
1814 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1816 GstRTSPClientPrivate *priv = client->priv;
1817 GstRTSPMethod method;
1818 const gchar *uristr;
1819 GstRTSPUrl *uri = NULL;
1820 GstRTSPVersion version;
1822 GstRTSPSession *session = NULL;
1823 GstRTSPContext sctx = { NULL }, *ctx;
1824 GstRTSPMessage response = { 0 };
1827 if (!(ctx = gst_rtsp_context_get_current ())) {
1829 ctx->auth = priv->auth;
1830 gst_rtsp_context_push_current (ctx);
1833 ctx->conn = priv->connection;
1834 ctx->client = client;
1835 ctx->request = request;
1836 ctx->response = &response;
1838 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1839 gst_rtsp_message_dump (request);
1842 GST_INFO ("client %p: received a request", client);
1844 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1846 /* we can only handle 1.0 requests */
1847 if (version != GST_RTSP_VERSION_1_0)
1850 ctx->method = method;
1852 /* we always try to parse the url first */
1853 if (strcmp (uristr, "*") == 0) {
1854 /* special case where we have * as uri, keep uri = NULL */
1855 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1858 /* get the session if there is any */
1859 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1860 if (res == GST_RTSP_OK) {
1861 if (priv->session_pool == NULL)
1864 /* we had a session in the request, find it again */
1865 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1866 goto session_not_found;
1868 /* we add the session to the client list of watched sessions. When a session
1869 * disappears because it times out, we will be notified. If all sessions are
1870 * gone, we will close the connection */
1871 client_watch_session (client, session);
1874 /* sanitize the uri */
1878 ctx->session = session;
1880 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
1881 goto not_authorized;
1883 /* now see what is asked and dispatch to a dedicated handler */
1885 case GST_RTSP_OPTIONS:
1886 handle_options_request (client, ctx);
1888 case GST_RTSP_DESCRIBE:
1889 handle_describe_request (client, ctx);
1891 case GST_RTSP_SETUP:
1892 handle_setup_request (client, ctx);
1895 handle_play_request (client, ctx);
1897 case GST_RTSP_PAUSE:
1898 handle_pause_request (client, ctx);
1900 case GST_RTSP_TEARDOWN:
1901 handle_teardown_request (client, ctx);
1903 case GST_RTSP_SET_PARAMETER:
1904 handle_set_param_request (client, ctx);
1906 case GST_RTSP_GET_PARAMETER:
1907 handle_get_param_request (client, ctx);
1909 case GST_RTSP_ANNOUNCE:
1910 case GST_RTSP_RECORD:
1911 case GST_RTSP_REDIRECT:
1912 goto not_implemented;
1913 case GST_RTSP_INVALID:
1920 gst_rtsp_context_pop_current (ctx);
1922 g_object_unref (session);
1924 gst_rtsp_url_free (uri);
1930 GST_ERROR ("client %p: version %d not supported", client, version);
1931 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1937 GST_ERROR ("client %p: bad request", client);
1938 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1943 GST_ERROR ("client %p: no pool configured", client);
1944 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1949 GST_ERROR ("client %p: session not found", client);
1950 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1955 GST_ERROR ("client %p: not allowed", client);
1960 GST_ERROR ("client %p: method %d not implemented", client, method);
1961 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
1968 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
1970 GstRTSPClientPrivate *priv = client->priv;
1972 GstRTSPSession *session = NULL;
1973 GstRTSPContext sctx = { NULL }, *ctx;
1976 if (!(ctx = gst_rtsp_context_get_current ())) {
1978 ctx->auth = priv->auth;
1979 gst_rtsp_context_push_current (ctx);
1982 ctx->conn = priv->connection;
1983 ctx->client = client;
1984 ctx->request = NULL;
1986 ctx->method = GST_RTSP_INVALID;
1987 ctx->response = response;
1989 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1990 gst_rtsp_message_dump (response);
1993 GST_INFO ("client %p: received a response", client);
1995 /* get the session if there is any */
1997 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
1998 if (res == GST_RTSP_OK) {
1999 if (priv->session_pool == NULL)
2002 /* we had a session in the request, find it again */
2003 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2004 goto session_not_found;
2006 /* we add the session to the client list of watched sessions. When a session
2007 * disappears because it times out, we will be notified. If all sessions are
2008 * gone, we will close the connection */
2009 client_watch_session (client, session);
2012 ctx->session = session;
2014 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
2019 gst_rtsp_context_pop_current (ctx);
2021 g_object_unref (session);
2026 GST_ERROR ("client %p: no pool configured", client);
2031 GST_ERROR ("client %p: session not found", client);
2037 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2039 GstRTSPClientPrivate *priv = client->priv;
2048 /* find the stream for this message */
2049 res = gst_rtsp_message_parse_data (message, &channel);
2050 if (res != GST_RTSP_OK)
2053 gst_rtsp_message_steal_body (message, &data, &size);
2055 buffer = gst_buffer_new_wrapped (data, size);
2058 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2059 GstRTSPStreamTransport *trans;
2060 GstRTSPStream *stream;
2061 const GstRTSPTransport *tr;
2065 tr = gst_rtsp_stream_transport_get_transport (trans);
2066 stream = gst_rtsp_stream_transport_get_stream (trans);
2068 /* check for TCP transport */
2069 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2070 /* dispatch to the stream based on the channel number */
2071 if (tr->interleaved.min == channel) {
2072 gst_rtsp_stream_recv_rtp (stream, buffer);
2075 } else if (tr->interleaved.max == channel) {
2076 gst_rtsp_stream_recv_rtcp (stream, buffer);
2083 gst_buffer_unref (buffer);
2087 * gst_rtsp_client_set_session_pool:
2088 * @client: a #GstRTSPClient
2089 * @pool: a #GstRTSPSessionPool
2091 * Set @pool as the sessionpool for @client which it will use to find
2092 * or allocate sessions. the sessionpool is usually inherited from the server
2093 * that created the client but can be overridden later.
2096 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2097 GstRTSPSessionPool * pool)
2099 GstRTSPSessionPool *old;
2100 GstRTSPClientPrivate *priv;
2102 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2104 priv = client->priv;
2107 g_object_ref (pool);
2109 g_mutex_lock (&priv->lock);
2110 old = priv->session_pool;
2111 priv->session_pool = pool;
2112 g_mutex_unlock (&priv->lock);
2115 g_object_unref (old);
2119 * gst_rtsp_client_get_session_pool:
2120 * @client: a #GstRTSPClient
2122 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2124 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2126 GstRTSPSessionPool *
2127 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2129 GstRTSPClientPrivate *priv;
2130 GstRTSPSessionPool *result;
2132 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2134 priv = client->priv;
2136 g_mutex_lock (&priv->lock);
2137 if ((result = priv->session_pool))
2138 g_object_ref (result);
2139 g_mutex_unlock (&priv->lock);
2145 * gst_rtsp_client_set_mount_points:
2146 * @client: a #GstRTSPClient
2147 * @mounts: a #GstRTSPMountPoints
2149 * Set @mounts as the mount points for @client which it will use to map urls
2150 * to media streams. These mount points are usually inherited from the server that
2151 * created the client but can be overriden later.
2154 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2155 GstRTSPMountPoints * mounts)
2157 GstRTSPClientPrivate *priv;
2158 GstRTSPMountPoints *old;
2160 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2162 priv = client->priv;
2165 g_object_ref (mounts);
2167 g_mutex_lock (&priv->lock);
2168 old = priv->mount_points;
2169 priv->mount_points = mounts;
2170 g_mutex_unlock (&priv->lock);
2173 g_object_unref (old);
2177 * gst_rtsp_client_get_mount_points:
2178 * @client: a #GstRTSPClient
2180 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2182 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2184 GstRTSPMountPoints *
2185 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2187 GstRTSPClientPrivate *priv;
2188 GstRTSPMountPoints *result;
2190 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2192 priv = client->priv;
2194 g_mutex_lock (&priv->lock);
2195 if ((result = priv->mount_points))
2196 g_object_ref (result);
2197 g_mutex_unlock (&priv->lock);
2203 * gst_rtsp_client_set_auth:
2204 * @client: a #GstRTSPClient
2205 * @auth: a #GstRTSPAuth
2207 * configure @auth to be used as the authentication manager of @client.
2210 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2212 GstRTSPClientPrivate *priv;
2215 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2217 priv = client->priv;
2220 g_object_ref (auth);
2222 g_mutex_lock (&priv->lock);
2225 g_mutex_unlock (&priv->lock);
2228 g_object_unref (old);
2233 * gst_rtsp_client_get_auth:
2234 * @client: a #GstRTSPClient
2236 * Get the #GstRTSPAuth used as the authentication manager of @client.
2238 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2242 gst_rtsp_client_get_auth (GstRTSPClient * client)
2244 GstRTSPClientPrivate *priv;
2245 GstRTSPAuth *result;
2247 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2249 priv = client->priv;
2251 g_mutex_lock (&priv->lock);
2252 if ((result = priv->auth))
2253 g_object_ref (result);
2254 g_mutex_unlock (&priv->lock);
2260 * gst_rtsp_client_set_thread_pool:
2261 * @client: a #GstRTSPClient
2262 * @pool: a #GstRTSPThreadPool
2264 * configure @pool to be used as the thread pool of @client.
2267 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2268 GstRTSPThreadPool * pool)
2270 GstRTSPClientPrivate *priv;
2271 GstRTSPThreadPool *old;
2273 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2275 priv = client->priv;
2278 g_object_ref (pool);
2280 g_mutex_lock (&priv->lock);
2281 old = priv->thread_pool;
2282 priv->thread_pool = pool;
2283 g_mutex_unlock (&priv->lock);
2286 g_object_unref (old);
2290 * gst_rtsp_client_get_thread_pool:
2291 * @client: a #GstRTSPClient
2293 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2295 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2299 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2301 GstRTSPClientPrivate *priv;
2302 GstRTSPThreadPool *result;
2304 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2306 priv = client->priv;
2308 g_mutex_lock (&priv->lock);
2309 if ((result = priv->thread_pool))
2310 g_object_ref (result);
2311 g_mutex_unlock (&priv->lock);
2317 * gst_rtsp_client_set_connection:
2318 * @client: a #GstRTSPClient
2319 * @conn: (transfer full): a #GstRTSPConnection
2321 * Set the #GstRTSPConnection of @client. This function takes ownership of
2324 * Returns: %TRUE on success.
2327 gst_rtsp_client_set_connection (GstRTSPClient * client,
2328 GstRTSPConnection * conn)
2330 GstRTSPClientPrivate *priv;
2331 GSocket *read_socket;
2332 GSocketAddress *address;
2334 GError *error = NULL;
2336 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2337 g_return_val_if_fail (conn != NULL, FALSE);
2339 priv = client->priv;
2341 read_socket = gst_rtsp_connection_get_read_socket (conn);
2343 if (!(address = g_socket_get_local_address (read_socket, &error)))
2346 g_free (priv->server_ip);
2347 /* keep the original ip that the client connected to */
2348 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2349 GInetAddress *iaddr;
2351 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2353 /* socket might be ipv6 but adress still ipv4 */
2354 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2355 priv->server_ip = g_inet_address_to_string (iaddr);
2356 g_object_unref (address);
2358 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2359 priv->server_ip = g_strdup ("unknown");
2362 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2363 priv->server_ip, priv->is_ipv6);
2365 url = gst_rtsp_connection_get_url (conn);
2366 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2368 priv->connection = conn;
2375 GST_ERROR ("could not get local address %s", error->message);
2376 g_error_free (error);
2382 * gst_rtsp_client_get_connection:
2383 * @client: a #GstRTSPClient
2385 * Get the #GstRTSPConnection of @client.
2387 * Returns: (transfer none): the #GstRTSPConnection of @client.
2388 * The connection object returned remains valid until the client is freed.
2391 gst_rtsp_client_get_connection (GstRTSPClient * client)
2393 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2395 return client->priv->connection;
2399 * gst_rtsp_client_set_send_func:
2400 * @client: a #GstRTSPClient
2401 * @func: a #GstRTSPClientSendFunc
2402 * @user_data: user data passed to @func
2403 * @notify: called when @user_data is no longer in use
2405 * Set @func as the callback that will be called when a new message needs to be
2406 * sent to the client. @user_data is passed to @func and @notify is called when
2407 * @user_data is no longer in use.
2409 * By default, the client will send the messages on the #GstRTSPConnection that
2410 * was configured with gst_rtsp_client_attach() was called.
2413 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2414 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2416 GstRTSPClientPrivate *priv;
2417 GDestroyNotify old_notify;
2420 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2422 priv = client->priv;
2424 g_mutex_lock (&priv->send_lock);
2425 priv->send_func = func;
2426 old_notify = priv->send_notify;
2427 old_data = priv->send_data;
2428 priv->send_notify = notify;
2429 priv->send_data = user_data;
2430 g_mutex_unlock (&priv->send_lock);
2433 old_notify (old_data);
2437 * gst_rtsp_client_handle_message:
2438 * @client: a #GstRTSPClient
2439 * @message: an #GstRTSPMessage
2441 * Let the client handle @message.
2443 * Returns: a #GstRTSPResult.
2446 gst_rtsp_client_handle_message (GstRTSPClient * client,
2447 GstRTSPMessage * message)
2449 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2450 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2452 switch (message->type) {
2453 case GST_RTSP_MESSAGE_REQUEST:
2454 handle_request (client, message);
2456 case GST_RTSP_MESSAGE_RESPONSE:
2457 handle_response (client, message);
2459 case GST_RTSP_MESSAGE_DATA:
2460 handle_data (client, message);
2469 * gst_rtsp_client_send_message:
2470 * @client: a #GstRTSPClient
2471 * @session: a #GstRTSPSession to send the message to or %NULL
2472 * @message: The #GstRTSPMessage to send
2474 * Send a message message to the remote end. @message must be a
2475 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
2478 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
2479 GstRTSPMessage * message)
2481 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2482 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2483 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
2484 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
2486 send_message (client, session, message, FALSE);
2491 static GstRTSPResult
2492 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2493 gboolean close, gpointer user_data)
2495 GstRTSPClientPrivate *priv = client->priv;
2497 /* send the response and store the seq number so we can wait until it's
2498 * written to the client to close the connection */
2499 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2500 &priv->close_seq : NULL);
2503 static GstRTSPResult
2504 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2507 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2510 static GstRTSPResult
2511 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2513 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2514 GstRTSPClientPrivate *priv = client->priv;
2516 if (priv->close_seq && priv->close_seq == cseq) {
2517 priv->close_seq = 0;
2518 close_connection (client);
2524 static GstRTSPResult
2525 closed (GstRTSPWatch * watch, gpointer user_data)
2527 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2528 GstRTSPClientPrivate *priv = client->priv;
2529 const gchar *tunnelid;
2531 GST_INFO ("client %p: connection closed", client);
2533 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2534 g_mutex_lock (&tunnels_lock);
2535 /* remove from tunnelids */
2536 g_hash_table_remove (tunnels, tunnelid);
2537 g_mutex_unlock (&tunnels_lock);
2540 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2545 static GstRTSPResult
2546 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2548 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2551 str = gst_rtsp_strresult (result);
2552 GST_INFO ("client %p: received an error %s", client, str);
2558 static GstRTSPResult
2559 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2560 GstRTSPMessage * message, guint id, gpointer user_data)
2562 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2565 str = gst_rtsp_strresult (result);
2567 ("client %p: error when handling message %p with id %d: %s",
2568 client, message, id, str);
2575 remember_tunnel (GstRTSPClient * client)
2577 GstRTSPClientPrivate *priv = client->priv;
2578 const gchar *tunnelid;
2580 /* store client in the pending tunnels */
2581 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2582 if (tunnelid == NULL)
2585 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2587 /* we can't have two clients connecting with the same tunnelid */
2588 g_mutex_lock (&tunnels_lock);
2589 if (g_hash_table_lookup (tunnels, tunnelid))
2590 goto tunnel_existed;
2592 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2593 g_mutex_unlock (&tunnels_lock);
2600 GST_ERROR ("client %p: no tunnelid provided", client);
2605 g_mutex_unlock (&tunnels_lock);
2606 GST_ERROR ("client %p: tunnel session %s already existed", client,
2612 static GstRTSPStatusCode
2613 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2615 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2616 GstRTSPClientPrivate *priv = client->priv;
2618 GST_INFO ("client %p: tunnel start (connection %p)", client,
2621 if (!remember_tunnel (client))
2624 return GST_RTSP_STS_OK;
2629 GST_ERROR ("client %p: error starting tunnel", client);
2630 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2634 static GstRTSPResult
2635 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2637 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2638 GstRTSPClientPrivate *priv = client->priv;
2640 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2643 /* ignore error, it'll only be a problem when the client does a POST again */
2644 remember_tunnel (client);
2649 static GstRTSPResult
2650 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2652 const gchar *tunnelid;
2653 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2654 GstRTSPClientPrivate *priv = client->priv;
2655 GstRTSPClient *oclient;
2656 GstRTSPClientPrivate *opriv;
2658 GST_INFO ("client %p: tunnel complete", client);
2660 /* find previous tunnel */
2661 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2662 if (tunnelid == NULL)
2665 g_mutex_lock (&tunnels_lock);
2666 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2669 /* remove the old client from the table. ref before because removing it will
2670 * remove the ref to it. */
2671 g_object_ref (oclient);
2672 g_hash_table_remove (tunnels, tunnelid);
2674 opriv = oclient->priv;
2676 if (opriv->watch == NULL)
2678 g_mutex_unlock (&tunnels_lock);
2680 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2681 opriv->connection, priv->connection);
2683 /* merge the tunnels into the first client */
2684 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2685 gst_rtsp_watch_reset (opriv->watch);
2686 g_object_unref (oclient);
2693 GST_ERROR ("client %p: no tunnelid provided", client);
2694 return GST_RTSP_ERROR;
2698 g_mutex_unlock (&tunnels_lock);
2699 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2700 return GST_RTSP_ERROR;
2704 g_mutex_unlock (&tunnels_lock);
2705 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2706 g_object_unref (oclient);
2707 return GST_RTSP_ERROR;
2711 static GstRTSPWatchFuncs watch_funcs = {
2723 client_watch_notify (GstRTSPClient * client)
2725 GstRTSPClientPrivate *priv = client->priv;
2727 GST_INFO ("client %p: watch destroyed", client);
2729 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2730 g_object_unref (client);
2734 * gst_rtsp_client_attach:
2735 * @client: a #GstRTSPClient
2736 * @context: (allow-none): a #GMainContext
2738 * Attaches @client to @context. When the mainloop for @context is run, the
2739 * client will be dispatched. When @context is NULL, the default context will be
2742 * This function should be called when the client properties and urls are fully
2743 * configured and the client is ready to start.
2745 * Returns: the ID (greater than 0) for the source within the GMainContext.
2748 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2750 GstRTSPClientPrivate *priv;
2753 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2754 priv = client->priv;
2755 g_return_val_if_fail (priv->connection != NULL, 0);
2756 g_return_val_if_fail (priv->watch == NULL, 0);
2758 /* create watch for the connection and attach */
2759 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2760 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2761 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2762 (GDestroyNotify) gst_rtsp_watch_unref);
2764 /* FIXME make this configurable. We don't want to do this yet because it will
2765 * be superceeded by a cache object later */
2766 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2768 GST_INFO ("attaching to context %p", context);
2769 res = gst_rtsp_watch_attach (priv->watch, context);
2775 * gst_rtsp_client_session_filter:
2776 * @client: a #GstRTSPClient
2777 * @func: (scope call): a callback
2778 * @user_data: user data passed to @func
2780 * Call @func for each session managed by @client. The result value of @func
2781 * determines what happens to the session. @func will be called with @client
2782 * locked so no further actions on @client can be performed from @func.
2784 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
2787 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
2789 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
2790 * will also be added with an additional ref to the result #GList of this
2793 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
2794 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2795 * element in the #GList should be unreffed before the list is freed.
2798 gst_rtsp_client_session_filter (GstRTSPClient * client,
2799 GstRTSPClientSessionFilterFunc func, gpointer user_data)
2801 GstRTSPClientPrivate *priv;
2802 GList *result, *walk, *next;
2804 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2805 g_return_val_if_fail (func != NULL, NULL);
2807 priv = client->priv;
2811 g_mutex_lock (&priv->lock);
2812 for (walk = priv->sessions; walk; walk = next) {
2813 GstRTSPSession *sess = walk->data;
2815 next = g_list_next (walk);
2817 switch (func (client, sess, user_data)) {
2818 case GST_RTSP_FILTER_REMOVE:
2819 /* stop watching the session and pretent it went away */
2820 client_cleanup_session (client, sess);
2822 case GST_RTSP_FILTER_REF:
2823 result = g_list_prepend (result, g_object_ref (sess));
2825 case GST_RTSP_FILTER_KEEP:
2830 g_mutex_unlock (&priv->lock);