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,
113 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
114 #define GST_CAT_DEFAULT rtsp_client_debug
116 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
118 static void gst_rtsp_client_get_property (GObject * object, guint propid,
119 GValue * value, GParamSpec * pspec);
120 static void gst_rtsp_client_set_property (GObject * object, guint propid,
121 const GValue * value, GParamSpec * pspec);
122 static void gst_rtsp_client_finalize (GObject * obj);
124 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
125 static void client_session_finalized (GstRTSPClient * client,
126 GstRTSPSession * session);
127 static void unlink_session_transports (GstRTSPClient * client,
128 GstRTSPSession * session, GstRTSPSessionMedia * sessmedia);
129 static gboolean default_configure_client_transport (GstRTSPClient * client,
130 GstRTSPContext * ctx, GstRTSPTransport * ct);
131 static GstRTSPResult default_params_set (GstRTSPClient * client,
132 GstRTSPContext * ctx);
133 static GstRTSPResult default_params_get (GstRTSPClient * client,
134 GstRTSPContext * ctx);
136 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
139 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
141 GObjectClass *gobject_class;
143 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
145 gobject_class = G_OBJECT_CLASS (klass);
147 gobject_class->get_property = gst_rtsp_client_get_property;
148 gobject_class->set_property = gst_rtsp_client_set_property;
149 gobject_class->finalize = gst_rtsp_client_finalize;
151 klass->create_sdp = create_sdp;
152 klass->configure_client_transport = default_configure_client_transport;
153 klass->params_set = default_params_set;
154 klass->params_get = default_params_get;
156 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
157 g_param_spec_object ("session-pool", "Session Pool",
158 "The session pool to use for client session",
159 GST_TYPE_RTSP_SESSION_POOL,
160 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
162 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
163 g_param_spec_object ("mount-points", "Mount Points",
164 "The mount points to use for client session",
165 GST_TYPE_RTSP_MOUNT_POINTS,
166 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
168 gst_rtsp_client_signals[SIGNAL_CLOSED] =
169 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
170 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
171 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
173 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
174 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
175 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
176 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
178 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
179 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
180 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
181 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
184 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
185 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
186 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
187 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
190 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
191 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
192 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
193 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
196 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
197 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
198 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
199 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
202 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
203 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
204 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
205 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
208 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
209 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
210 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
211 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
214 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
215 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
216 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
217 set_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
218 G_TYPE_NONE, 1, G_TYPE_POINTER);
220 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
221 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
222 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
223 get_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
224 G_TYPE_NONE, 1, G_TYPE_POINTER);
227 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
228 g_mutex_init (&tunnels_lock);
230 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
234 gst_rtsp_client_init (GstRTSPClient * client)
236 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
240 g_mutex_init (&priv->lock);
241 g_mutex_init (&priv->send_lock);
245 static GstRTSPFilterResult
246 filter_session (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
249 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
251 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
252 unlink_session_transports (client, sess, sessmedia);
254 /* unmanage the media in the session */
255 return GST_RTSP_FILTER_REMOVE;
259 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
261 /* unlink all media managed in this session */
262 gst_rtsp_session_filter (session, filter_session, client);
266 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
268 GstRTSPClientPrivate *priv = client->priv;
271 for (walk = priv->sessions; walk; walk = g_list_next (walk)) {
272 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
274 /* we already know about this session */
275 if (msession == session)
279 GST_INFO ("watching session %p", session);
281 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
283 priv->sessions = g_list_prepend (priv->sessions, session);
287 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session)
289 GstRTSPClientPrivate *priv = client->priv;
291 GST_INFO ("unwatching session %p", session);
293 g_object_weak_unref (G_OBJECT (session),
294 (GWeakNotify) client_session_finalized, client);
295 priv->sessions = g_list_remove (priv->sessions, session);
299 client_cleanup_session (GstRTSPClient * client, GstRTSPSession * session)
301 g_object_weak_unref (G_OBJECT (session),
302 (GWeakNotify) client_session_finalized, client);
303 client_unlink_session (client, session);
307 client_cleanup_sessions (GstRTSPClient * client)
309 GstRTSPClientPrivate *priv = client->priv;
312 /* remove weak-ref from sessions */
313 for (sessions = priv->sessions; sessions; sessions = g_list_next (sessions)) {
314 client_cleanup_session (client, (GstRTSPSession *) sessions->data);
316 g_list_free (priv->sessions);
317 priv->sessions = NULL;
320 /* A client is finalized when the connection is broken */
322 gst_rtsp_client_finalize (GObject * obj)
324 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
325 GstRTSPClientPrivate *priv = client->priv;
327 GST_INFO ("finalize client %p", client);
329 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
332 g_source_destroy ((GSource *) priv->watch);
334 client_cleanup_sessions (client);
336 if (priv->connection)
337 gst_rtsp_connection_free (priv->connection);
338 if (priv->session_pool)
339 g_object_unref (priv->session_pool);
340 if (priv->mount_points)
341 g_object_unref (priv->mount_points);
343 g_object_unref (priv->auth);
348 gst_rtsp_media_unprepare (priv->media);
349 g_object_unref (priv->media);
352 g_free (priv->server_ip);
353 g_mutex_clear (&priv->lock);
354 g_mutex_clear (&priv->send_lock);
356 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
360 gst_rtsp_client_get_property (GObject * object, guint propid,
361 GValue * value, GParamSpec * pspec)
363 GstRTSPClient *client = GST_RTSP_CLIENT (object);
366 case PROP_SESSION_POOL:
367 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
369 case PROP_MOUNT_POINTS:
370 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
373 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
378 gst_rtsp_client_set_property (GObject * object, guint propid,
379 const GValue * value, GParamSpec * pspec)
381 GstRTSPClient *client = GST_RTSP_CLIENT (object);
384 case PROP_SESSION_POOL:
385 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
387 case PROP_MOUNT_POINTS:
388 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
391 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
396 * gst_rtsp_client_new:
398 * Create a new #GstRTSPClient instance.
400 * Returns: a new #GstRTSPClient
403 gst_rtsp_client_new (void)
405 GstRTSPClient *result;
407 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
413 send_message (GstRTSPClient * client, GstRTSPSession * session,
414 GstRTSPMessage * message, gboolean close)
416 GstRTSPClientPrivate *priv = client->priv;
418 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
419 "GStreamer RTSP server");
421 /* remove any previous header */
422 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
424 /* add the new session header for new session ids */
426 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
427 gst_rtsp_session_get_header (session));
430 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
431 gst_rtsp_message_dump (message);
435 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
437 g_mutex_lock (&priv->send_lock);
439 priv->send_func (client, message, close, priv->send_data);
440 g_mutex_unlock (&priv->send_lock);
442 gst_rtsp_message_unset (message);
446 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
447 GstRTSPContext * ctx)
449 gst_rtsp_message_init_response (ctx->response, code,
450 gst_rtsp_status_as_text (code), ctx->request);
452 send_message (client, NULL, ctx->response, FALSE);
456 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
458 if (path1 == NULL || path2 == NULL)
461 if (strlen (path1) != len2)
464 if (strncmp (path1, path2, len2))
470 /* this function is called to initially find the media for the DESCRIBE request
471 * but is cached for when the same client (without breaking the connection) is
472 * doing a setup for the exact same url. */
473 static GstRTSPMedia *
474 find_media (GstRTSPClient * client, GstRTSPContext * ctx, gint * matched)
476 GstRTSPClientPrivate *priv = client->priv;
477 GstRTSPMediaFactory *factory;
482 if (!priv->mount_points)
483 goto no_mount_points;
485 path = ctx->uri->abspath;
487 /* find the longest matching factory for the uri first */
488 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
492 ctx->factory = factory;
494 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
495 goto no_factory_access;
497 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
503 path_len = strlen (path);
505 if (!paths_are_equal (priv->path, path, path_len)) {
506 GstRTSPThread *thread;
508 /* remove any previously cached values before we try to construct a new
514 gst_rtsp_media_unprepare (priv->media);
515 g_object_unref (priv->media);
519 /* prepare the media and add it to the pipeline */
520 if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
525 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
526 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
530 /* prepare the media */
531 if (!(gst_rtsp_media_prepare (media, thread)))
534 /* now keep track of the uri and the media */
535 priv->path = g_strndup (path, path_len);
538 /* we have seen this path before, used cached media */
541 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
544 g_object_unref (factory);
548 g_object_ref (media);
555 GST_ERROR ("client %p: no mount points configured", client);
556 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
561 GST_ERROR ("client %p: no factory for uri %s", client, path);
562 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
567 GST_ERROR ("client %p: not authorized to see factory uri %s", client, path);
572 GST_ERROR ("client %p: not authorized for factory uri %s", client, path);
577 GST_ERROR ("client %p: can't create media", client);
578 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
579 g_object_unref (factory);
585 GST_ERROR ("client %p: can't create thread", client);
586 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
587 g_object_unref (media);
589 g_object_unref (factory);
595 GST_ERROR ("client %p: can't prepare media", client);
596 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
597 g_object_unref (media);
599 g_object_unref (factory);
606 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
608 GstRTSPClientPrivate *priv = client->priv;
609 GstRTSPMessage message = { 0 };
614 gst_rtsp_message_init_data (&message, channel);
616 /* FIXME, need some sort of iovec RTSPMessage here */
617 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
620 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
622 g_mutex_lock (&priv->send_lock);
624 priv->send_func (client, &message, FALSE, priv->send_data);
625 g_mutex_unlock (&priv->send_lock);
627 gst_rtsp_message_steal_body (&message, &data, &usize);
628 gst_buffer_unmap (buffer, &map_info);
630 gst_rtsp_message_unset (&message);
636 link_transport (GstRTSPClient * client, GstRTSPSession * session,
637 GstRTSPStreamTransport * trans)
639 GstRTSPClientPrivate *priv = client->priv;
641 GST_DEBUG ("client %p: linking transport %p", client, trans);
643 gst_rtsp_stream_transport_set_callbacks (trans,
644 (GstRTSPSendFunc) do_send_data,
645 (GstRTSPSendFunc) do_send_data, client, NULL);
647 priv->transports = g_list_prepend (priv->transports, trans);
649 /* make sure our session can't expire */
650 gst_rtsp_session_prevent_expire (session);
654 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
655 GstRTSPStreamTransport * trans)
657 GstRTSPClientPrivate *priv = client->priv;
659 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
661 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
663 priv->transports = g_list_remove (priv->transports, trans);
665 /* our session can now expire */
666 gst_rtsp_session_allow_expire (session);
670 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
671 GstRTSPSessionMedia * sessmedia)
676 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
677 for (i = 0; i < n_streams; i++) {
678 GstRTSPStreamTransport *trans;
679 const GstRTSPTransport *tr;
681 /* get the transport, if there is no transport configured, skip this stream */
682 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
686 tr = gst_rtsp_stream_transport_get_transport (trans);
688 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
689 /* for TCP, unlink the stream from the TCP connection of the client */
690 unlink_transport (client, session, trans);
696 close_connection (GstRTSPClient * client)
698 GstRTSPClientPrivate *priv = client->priv;
699 const gchar *tunnelid;
701 GST_DEBUG ("client %p: closing connection", client);
703 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
704 g_mutex_lock (&tunnels_lock);
705 /* remove from tunnelids */
706 g_hash_table_remove (tunnels, tunnelid);
707 g_mutex_unlock (&tunnels_lock);
710 gst_rtsp_connection_close (priv->connection);
714 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
716 GstRTSPClientPrivate *priv = client->priv;
717 GstRTSPSession *session;
718 GstRTSPSessionMedia *sessmedia;
719 GstRTSPStatusCode code;
726 session = ctx->session;
731 path = ctx->uri->abspath;
733 /* get a handle to the configuration of the media in the session */
734 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
738 /* only aggregate control for now.. */
739 if (path[matched] != '\0')
742 ctx->sessmedia = sessmedia;
744 /* we emit the signal before closing the connection */
745 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
748 /* unlink the all TCP callbacks */
749 unlink_session_transports (client, session, sessmedia);
751 /* remove the session from the watched sessions */
752 client_unwatch_session (client, session);
754 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
756 /* unmanage the media in the session, returns false if all media session
758 if (!gst_rtsp_session_release_media (session, sessmedia)) {
759 /* remove the session */
760 gst_rtsp_session_pool_remove (priv->session_pool, session);
762 /* construct the response now */
763 code = GST_RTSP_STS_OK;
764 gst_rtsp_message_init_response (ctx->response, code,
765 gst_rtsp_status_as_text (code), ctx->request);
767 send_message (client, session, ctx->response, TRUE);
774 GST_ERROR ("client %p: no session", client);
775 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
780 GST_ERROR ("client %p: no uri supplied", client);
781 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
786 GST_ERROR ("client %p: no media for uri", client);
787 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
792 GST_ERROR ("client %p: no aggregate path %s", client, path);
793 send_generic_response (client,
794 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
800 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
804 res = gst_rtsp_params_set (client, ctx);
810 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
814 res = gst_rtsp_params_get (client, ctx);
820 handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
826 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
827 if (res != GST_RTSP_OK)
831 /* no body, keep-alive request */
832 send_generic_response (client, GST_RTSP_STS_OK, ctx);
834 /* there is a body, handle the params */
835 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
836 if (res != GST_RTSP_OK)
839 send_message (client, ctx->session, ctx->response, FALSE);
842 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
850 GST_ERROR ("client %p: bad request", client);
851 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
857 handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
863 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
864 if (res != GST_RTSP_OK)
868 /* no body, keep-alive request */
869 send_generic_response (client, GST_RTSP_STS_OK, ctx);
871 /* there is a body, handle the params */
872 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
873 if (res != GST_RTSP_OK)
876 send_message (client, ctx->session, ctx->response, FALSE);
879 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
887 GST_ERROR ("client %p: bad request", client);
888 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
894 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
896 GstRTSPSession *session;
897 GstRTSPSessionMedia *sessmedia;
898 GstRTSPStatusCode code;
899 GstRTSPState rtspstate;
903 if (!(session = ctx->session))
909 path = ctx->uri->abspath;
911 /* get a handle to the configuration of the media in the session */
912 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
916 if (path[matched] != '\0')
919 ctx->sessmedia = sessmedia;
921 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
922 /* the session state must be playing or recording */
923 if (rtspstate != GST_RTSP_STATE_PLAYING &&
924 rtspstate != GST_RTSP_STATE_RECORDING)
927 /* unlink the all TCP callbacks */
928 unlink_session_transports (client, session, sessmedia);
930 /* then pause sending */
931 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
933 /* construct the response now */
934 code = GST_RTSP_STS_OK;
935 gst_rtsp_message_init_response (ctx->response, code,
936 gst_rtsp_status_as_text (code), ctx->request);
938 send_message (client, session, ctx->response, FALSE);
940 /* the state is now READY */
941 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
943 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
950 GST_ERROR ("client %p: no seesion", client);
951 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
956 GST_ERROR ("client %p: no uri supplied", client);
957 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
962 GST_ERROR ("client %p: no media for uri", client);
963 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
968 GST_ERROR ("client %p: no aggregate path %s", client, path);
969 send_generic_response (client,
970 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
975 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
976 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
983 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
985 GstRTSPSession *session;
986 GstRTSPSessionMedia *sessmedia;
988 GstRTSPStatusCode code;
990 guint n_streams, i, infocount;
992 GstRTSPTimeRange *range;
994 GstRTSPState rtspstate;
995 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
999 if (!(session = ctx->session))
1005 path = ctx->uri->abspath;
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')
1015 ctx->sessmedia = sessmedia;
1016 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1018 /* the session state must be playing or ready */
1019 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1020 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1023 /* parse the range header if we have one */
1024 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1025 if (res == GST_RTSP_OK) {
1026 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1027 /* we have a range, seek to the position */
1029 gst_rtsp_media_seek (media, range);
1030 gst_rtsp_range_free (range);
1034 /* grab RTPInfo from the payloaders now */
1035 rtpinfo = g_string_new ("");
1037 n_streams = gst_rtsp_media_n_streams (media);
1038 for (i = 0, infocount = 0; i < n_streams; i++) {
1039 GstRTSPStreamTransport *trans;
1040 GstRTSPStream *stream;
1041 const GstRTSPTransport *tr;
1045 /* get the transport, if there is no transport configured, skip this stream */
1046 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
1047 if (trans == NULL) {
1048 GST_INFO ("stream %d is not configured", i);
1051 tr = gst_rtsp_stream_transport_get_transport (trans);
1053 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1054 /* for TCP, link the stream to the TCP connection of the client */
1055 link_transport (client, session, trans);
1058 stream = gst_rtsp_stream_transport_get_stream (trans);
1059 if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
1061 g_string_append (rtpinfo, ", ");
1063 uristr = gst_rtsp_url_get_request_uri (ctx->uri);
1064 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
1065 uristr, i, seq, rtptime);
1070 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
1074 /* construct the response now */
1075 code = GST_RTSP_STS_OK;
1076 gst_rtsp_message_init_response (ctx->response, code,
1077 gst_rtsp_status_as_text (code), ctx->request);
1079 /* add the RTP-Info header */
1080 if (infocount > 0) {
1081 str = g_string_free (rtpinfo, FALSE);
1082 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO, str);
1084 g_string_free (rtpinfo, TRUE);
1088 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1090 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1092 send_message (client, session, ctx->response, FALSE);
1094 /* start playing after sending the request */
1095 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1097 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1099 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1106 GST_ERROR ("client %p: no session", client);
1107 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1112 GST_ERROR ("client %p: no uri supplied", client);
1113 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1118 GST_ERROR ("client %p: media not found", client);
1119 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1124 GST_ERROR ("client %p: no aggregate path %s", client, path);
1125 send_generic_response (client,
1126 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1131 GST_ERROR ("client %p: not PLAYING or READY", client);
1132 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1139 do_keepalive (GstRTSPSession * session)
1141 GST_INFO ("keep session %p alive", session);
1142 gst_rtsp_session_touch (session);
1145 /* parse @transport and return a valid transport in @tr. only transports
1146 * from @supported are returned. Returns FALSE if no valid transport
1149 parse_transport (const char *transport, GstRTSPLowerTrans supported,
1150 GstRTSPTransport * tr)
1157 gst_rtsp_transport_init (tr);
1159 GST_DEBUG ("parsing transports %s", transport);
1161 transports = g_strsplit (transport, ",", 0);
1163 /* loop through the transports, try to parse */
1164 for (i = 0; transports[i]; i++) {
1165 res = gst_rtsp_transport_parse (transports[i], tr);
1166 if (res != GST_RTSP_OK) {
1167 /* no valid transport, search some more */
1168 GST_WARNING ("could not parse transport %s", transports[i]);
1172 /* we have a transport, see if it's RTP/AVP */
1173 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1174 GST_WARNING ("invalid transport %s", transports[i]);
1178 if (!(tr->lower_transport & supported)) {
1179 GST_WARNING ("unsupported transport %s", transports[i]);
1183 /* we have a valid transport */
1184 GST_INFO ("found valid transport %s", transports[i]);
1189 gst_rtsp_transport_init (tr);
1191 g_strfreev (transports);
1197 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
1198 GstRTSPMessage * request)
1200 gchar *blocksize_str;
1201 gboolean ret = TRUE;
1203 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1204 &blocksize_str, 0) == GST_RTSP_OK) {
1208 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1209 if (end == blocksize_str) {
1210 GST_ERROR ("failed to parse blocksize");
1213 /* we don't want to change the mtu when this media
1214 * can be shared because it impacts other clients */
1215 if (gst_rtsp_media_is_shared (media))
1218 if (blocksize > G_MAXUINT)
1219 blocksize = G_MAXUINT;
1220 gst_rtsp_stream_set_mtu (stream, blocksize);
1227 default_configure_client_transport (GstRTSPClient * client,
1228 GstRTSPContext * ctx, GstRTSPTransport * ct)
1230 GstRTSPClientPrivate *priv = client->priv;
1232 /* we have a valid transport now, set the destination of the client. */
1233 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1234 gboolean use_client_settings;
1236 use_client_settings =
1237 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1239 if (ct->destination && use_client_settings) {
1240 GstRTSPAddress *addr;
1242 addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1243 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1248 gst_rtsp_address_free (addr);
1250 GstRTSPAddress *addr;
1251 GSocketFamily family;
1253 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1255 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1259 g_free (ct->destination);
1260 ct->destination = g_strdup (addr->address);
1261 ct->port.min = addr->port;
1262 ct->port.max = addr->port + addr->n_ports - 1;
1263 ct->ttl = addr->ttl;
1265 gst_rtsp_address_free (addr);
1270 url = gst_rtsp_connection_get_url (priv->connection);
1271 g_free (ct->destination);
1272 ct->destination = g_strdup (url->host);
1274 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1275 /* check if the client selected channels for TCP */
1276 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1277 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1287 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1292 static GstRTSPTransport *
1293 make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
1294 GstRTSPTransport * ct)
1296 GstRTSPTransport *st;
1298 GSocketFamily family;
1300 /* prepare the server transport */
1301 gst_rtsp_transport_new (&st);
1303 st->trans = ct->trans;
1304 st->profile = ct->profile;
1305 st->lower_transport = ct->lower_transport;
1307 addr = g_inet_address_new_from_string (ct->destination);
1310 GST_ERROR ("failed to get inet addr from client destination");
1311 family = G_SOCKET_FAMILY_IPV4;
1313 family = g_inet_address_get_family (addr);
1314 g_object_unref (addr);
1318 switch (st->lower_transport) {
1319 case GST_RTSP_LOWER_TRANS_UDP:
1320 st->client_port = ct->client_port;
1321 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
1323 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1324 st->port = ct->port;
1325 st->destination = g_strdup (ct->destination);
1328 case GST_RTSP_LOWER_TRANS_TCP:
1329 st->interleaved = ct->interleaved;
1334 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
1340 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
1342 GstRTSPClientPrivate *priv = client->priv;
1346 GstRTSPTransport *ct, *st;
1347 GstRTSPLowerTrans supported;
1348 GstRTSPStatusCode code;
1349 GstRTSPSession *session;
1350 GstRTSPStreamTransport *trans;
1352 GstRTSPSessionMedia *sessmedia;
1353 GstRTSPMedia *media;
1354 GstRTSPStream *stream;
1355 GstRTSPState rtspstate;
1356 GstRTSPClientClass *klass;
1357 gchar *path, *control;
1364 path = uri->abspath;
1366 /* parse the transport */
1368 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
1370 if (res != GST_RTSP_OK)
1373 /* we create the session after parsing stuff so that we don't make
1374 * a session for malformed requests */
1375 if (priv->session_pool == NULL)
1378 session = ctx->session;
1381 g_object_ref (session);
1382 /* get a handle to the configuration of the media in the session, this can
1383 * return NULL if this is a new url to manage in this session. */
1384 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1386 /* we need a new media configuration in this session */
1390 /* we have no session media, find one and manage it */
1391 if (sessmedia == NULL) {
1392 /* get a handle to the configuration of the media in the session */
1393 media = find_media (client, ctx, &matched);
1395 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1396 g_object_ref (media);
1398 /* no media, not found then */
1400 goto media_not_found;
1402 /* path is what matched. We can modify the parsed uri in place */
1403 path[matched] = '\0';
1404 /* control is remainder */
1405 control = &path[matched + 1];
1407 /* find the stream now using the control part */
1408 stream = gst_rtsp_media_find_stream (media, control);
1410 goto stream_not_found;
1412 /* now we have a uri identifying a valid media and stream */
1413 ctx->stream = stream;
1416 if (session == NULL) {
1417 /* create a session if this fails we probably reached our session limit or
1419 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1420 goto service_unavailable;
1422 /* make sure this client is closed when the session is closed */
1423 client_watch_session (client, session);
1425 /* signal new session */
1426 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1429 ctx->session = session;
1432 if (sessmedia == NULL) {
1433 /* manage the media in our session now, if not done already */
1434 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1435 /* if we stil have no media, error */
1436 if (sessmedia == NULL)
1437 goto sessmedia_unavailable;
1439 g_object_unref (media);
1442 ctx->sessmedia = sessmedia;
1444 /* set blocksize on this stream */
1445 if (!handle_blocksize (media, stream, ctx->request))
1446 goto invalid_blocksize;
1448 gst_rtsp_transport_new (&ct);
1450 /* our supported transports */
1451 supported = gst_rtsp_stream_get_protocols (stream);
1453 /* parse and find a usable supported transport */
1454 if (!parse_transport (transport, supported, ct))
1455 goto unsupported_transports;
1457 /* update the client transport */
1458 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1459 if (!klass->configure_client_transport (client, ctx, ct))
1460 goto unsupported_client_transport;
1462 /* set in the session media transport */
1463 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1465 /* configure keepalive for this transport */
1466 gst_rtsp_stream_transport_set_keepalive (trans,
1467 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1469 /* create and serialize the server transport */
1470 st = make_server_transport (client, ctx, ct);
1471 trans_str = gst_rtsp_transport_as_text (st);
1472 gst_rtsp_transport_free (st);
1474 /* construct the response now */
1475 code = GST_RTSP_STS_OK;
1476 gst_rtsp_message_init_response (ctx->response, code,
1477 gst_rtsp_status_as_text (code), ctx->request);
1479 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
1483 send_message (client, session, ctx->response, FALSE);
1485 /* update the state */
1486 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1487 switch (rtspstate) {
1488 case GST_RTSP_STATE_PLAYING:
1489 case GST_RTSP_STATE_RECORDING:
1490 case GST_RTSP_STATE_READY:
1491 /* no state change */
1494 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1497 g_object_unref (session);
1499 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
1506 GST_ERROR ("client %p: no uri", client);
1507 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1512 GST_ERROR ("client %p: no transport", client);
1513 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1518 GST_ERROR ("client %p: no session pool configured", client);
1519 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1524 GST_ERROR ("client %p: media '%s' not found", client, path);
1525 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1530 GST_ERROR ("client %p: stream '%s' not found", client, control);
1531 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1532 g_object_unref (media);
1535 service_unavailable:
1537 GST_ERROR ("client %p: can't create session", client);
1538 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1539 g_object_unref (media);
1542 sessmedia_unavailable:
1544 GST_ERROR ("client %p: can't create session media", client);
1545 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1546 g_object_unref (media);
1547 g_object_unref (session);
1552 GST_ERROR ("client %p: invalid blocksize", client);
1553 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1554 g_object_unref (session);
1557 unsupported_transports:
1559 GST_ERROR ("client %p: unsupported transports", client);
1560 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1561 gst_rtsp_transport_free (ct);
1562 g_object_unref (session);
1565 unsupported_client_transport:
1567 GST_ERROR ("client %p: unsupported client transport", client);
1568 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1569 gst_rtsp_transport_free (ct);
1570 g_object_unref (session);
1575 static GstSDPMessage *
1576 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1578 GstRTSPClientPrivate *priv = client->priv;
1583 gst_sdp_message_new (&sdp);
1585 /* some standard things first */
1586 gst_sdp_message_set_version (sdp, "0");
1593 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1596 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1597 gst_sdp_message_set_information (sdp, "rtsp-server");
1598 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1599 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1600 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1601 gst_sdp_message_add_attribute (sdp, "control", "*");
1603 info.is_ipv6 = priv->is_ipv6;
1604 info.server_ip = priv->server_ip;
1606 /* create an SDP for the media object */
1607 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1615 GST_ERROR ("client %p: could not create SDP", client);
1616 gst_sdp_message_free (sdp);
1621 /* for the describe we must generate an SDP */
1623 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
1628 gchar *str, *content_base;
1629 GstRTSPMedia *media;
1630 GstRTSPClientClass *klass;
1632 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1637 /* check what kind of format is accepted, we don't really do anything with it
1638 * and always return SDP for now. */
1643 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
1645 if (res == GST_RTSP_ENOTIMPL)
1648 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1652 /* find the media object for the uri */
1653 if (!(media = find_media (client, ctx, NULL)))
1656 /* create an SDP for the media object on this client */
1657 if (!(sdp = klass->create_sdp (client, media)))
1660 g_object_unref (media);
1662 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
1663 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
1665 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
1668 /* content base for some clients that might screw up creating the setup uri */
1669 str = gst_rtsp_url_get_request_uri (ctx->uri);
1670 str_len = strlen (str);
1672 /* check for trailing '/' and append one */
1673 if (str[str_len - 1] != '/') {
1674 content_base = g_malloc (str_len + 2);
1675 memcpy (content_base, str, str_len);
1676 content_base[str_len] = '/';
1677 content_base[str_len + 1] = '\0';
1683 GST_INFO ("adding content-base: %s", content_base);
1685 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE,
1687 g_free (content_base);
1689 /* add SDP to the response body */
1690 str = gst_sdp_message_as_text (sdp);
1691 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
1692 gst_sdp_message_free (sdp);
1694 send_message (client, ctx->session, ctx->response, FALSE);
1696 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1704 GST_ERROR ("client %p: no uri", client);
1705 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1710 GST_ERROR ("client %p: no media", client);
1711 /* error reply is already sent */
1716 GST_ERROR ("client %p: can't create SDP", client);
1717 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1718 g_object_unref (media);
1724 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
1726 GstRTSPMethod options;
1729 options = GST_RTSP_DESCRIBE |
1734 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1736 str = gst_rtsp_options_as_text (options);
1738 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
1739 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
1741 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
1744 send_message (client, ctx->session, ctx->response, FALSE);
1746 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1752 /* remove duplicate and trailing '/' */
1754 sanitize_uri (GstRTSPUrl * uri)
1758 gboolean have_slash, prev_slash;
1760 s = d = uri->abspath;
1761 len = strlen (uri->abspath);
1765 for (i = 0; i < len; i++) {
1766 have_slash = s[i] == '/';
1768 if (!have_slash || !prev_slash)
1770 prev_slash = have_slash;
1772 len = d - uri->abspath;
1773 /* don't remove the first slash if that's the only thing left */
1774 if (len > 1 && *(d - 1) == '/')
1780 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1782 GstRTSPClientPrivate *priv = client->priv;
1784 GST_INFO ("client %p: session %p finished", client, session);
1786 /* unlink all media managed in this session */
1787 client_unlink_session (client, session);
1789 /* remove the session */
1790 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1791 GST_INFO ("client %p: all sessions finalized, close the connection",
1793 close_connection (client);
1798 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1800 GstRTSPClientPrivate *priv = client->priv;
1801 GstRTSPMethod method;
1802 const gchar *uristr;
1803 GstRTSPUrl *uri = NULL;
1804 GstRTSPVersion version;
1806 GstRTSPSession *session = NULL;
1807 GstRTSPContext sctx = { NULL }, *ctx;
1808 GstRTSPMessage response = { 0 };
1811 if (!(ctx = gst_rtsp_context_get_current ())) {
1813 ctx->auth = priv->auth;
1814 gst_rtsp_context_push_current (ctx);
1817 ctx->conn = priv->connection;
1818 ctx->client = client;
1819 ctx->request = request;
1820 ctx->response = &response;
1822 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1823 gst_rtsp_message_dump (request);
1826 GST_INFO ("client %p: received a request", client);
1828 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1830 /* we can only handle 1.0 requests */
1831 if (version != GST_RTSP_VERSION_1_0)
1834 ctx->method = method;
1836 /* we always try to parse the url first */
1837 if (strcmp (uristr, "*") == 0) {
1838 /* special case where we have * as uri, keep uri = NULL */
1839 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1842 /* get the session if there is any */
1843 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1844 if (res == GST_RTSP_OK) {
1845 if (priv->session_pool == NULL)
1848 /* we had a session in the request, find it again */
1849 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1850 goto session_not_found;
1852 /* we add the session to the client list of watched sessions. When a session
1853 * disappears because it times out, we will be notified. If all sessions are
1854 * gone, we will close the connection */
1855 client_watch_session (client, session);
1858 /* sanitize the uri */
1862 ctx->session = session;
1864 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
1865 goto not_authorized;
1867 /* now see what is asked and dispatch to a dedicated handler */
1869 case GST_RTSP_OPTIONS:
1870 handle_options_request (client, ctx);
1872 case GST_RTSP_DESCRIBE:
1873 handle_describe_request (client, ctx);
1875 case GST_RTSP_SETUP:
1876 handle_setup_request (client, ctx);
1879 handle_play_request (client, ctx);
1881 case GST_RTSP_PAUSE:
1882 handle_pause_request (client, ctx);
1884 case GST_RTSP_TEARDOWN:
1885 handle_teardown_request (client, ctx);
1887 case GST_RTSP_SET_PARAMETER:
1888 handle_set_param_request (client, ctx);
1890 case GST_RTSP_GET_PARAMETER:
1891 handle_get_param_request (client, ctx);
1893 case GST_RTSP_ANNOUNCE:
1894 case GST_RTSP_RECORD:
1895 case GST_RTSP_REDIRECT:
1896 goto not_implemented;
1897 case GST_RTSP_INVALID:
1904 gst_rtsp_context_pop_current (ctx);
1906 g_object_unref (session);
1908 gst_rtsp_url_free (uri);
1914 GST_ERROR ("client %p: version %d not supported", client, version);
1915 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1921 GST_ERROR ("client %p: bad request", client);
1922 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1927 GST_ERROR ("client %p: no pool configured", client);
1928 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1933 GST_ERROR ("client %p: session not found", client);
1934 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1939 GST_ERROR ("client %p: not allowed", client);
1944 GST_ERROR ("client %p: method %d not implemented", client, method);
1945 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
1951 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1953 GstRTSPClientPrivate *priv = client->priv;
1962 /* find the stream for this message */
1963 res = gst_rtsp_message_parse_data (message, &channel);
1964 if (res != GST_RTSP_OK)
1967 gst_rtsp_message_steal_body (message, &data, &size);
1969 buffer = gst_buffer_new_wrapped (data, size);
1972 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1973 GstRTSPStreamTransport *trans;
1974 GstRTSPStream *stream;
1975 const GstRTSPTransport *tr;
1979 tr = gst_rtsp_stream_transport_get_transport (trans);
1980 stream = gst_rtsp_stream_transport_get_stream (trans);
1982 /* check for TCP transport */
1983 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1984 /* dispatch to the stream based on the channel number */
1985 if (tr->interleaved.min == channel) {
1986 gst_rtsp_stream_recv_rtp (stream, buffer);
1989 } else if (tr->interleaved.max == channel) {
1990 gst_rtsp_stream_recv_rtcp (stream, buffer);
1997 gst_buffer_unref (buffer);
2001 * gst_rtsp_client_set_session_pool:
2002 * @client: a #GstRTSPClient
2003 * @pool: a #GstRTSPSessionPool
2005 * Set @pool as the sessionpool for @client which it will use to find
2006 * or allocate sessions. the sessionpool is usually inherited from the server
2007 * that created the client but can be overridden later.
2010 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2011 GstRTSPSessionPool * pool)
2013 GstRTSPSessionPool *old;
2014 GstRTSPClientPrivate *priv;
2016 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2018 priv = client->priv;
2021 g_object_ref (pool);
2023 g_mutex_lock (&priv->lock);
2024 old = priv->session_pool;
2025 priv->session_pool = pool;
2026 g_mutex_unlock (&priv->lock);
2029 g_object_unref (old);
2033 * gst_rtsp_client_get_session_pool:
2034 * @client: a #GstRTSPClient
2036 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2038 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2040 GstRTSPSessionPool *
2041 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2043 GstRTSPClientPrivate *priv;
2044 GstRTSPSessionPool *result;
2046 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2048 priv = client->priv;
2050 g_mutex_lock (&priv->lock);
2051 if ((result = priv->session_pool))
2052 g_object_ref (result);
2053 g_mutex_unlock (&priv->lock);
2059 * gst_rtsp_client_set_mount_points:
2060 * @client: a #GstRTSPClient
2061 * @mounts: a #GstRTSPMountPoints
2063 * Set @mounts as the mount points for @client which it will use to map urls
2064 * to media streams. These mount points are usually inherited from the server that
2065 * created the client but can be overriden later.
2068 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2069 GstRTSPMountPoints * mounts)
2071 GstRTSPClientPrivate *priv;
2072 GstRTSPMountPoints *old;
2074 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2076 priv = client->priv;
2079 g_object_ref (mounts);
2081 g_mutex_lock (&priv->lock);
2082 old = priv->mount_points;
2083 priv->mount_points = mounts;
2084 g_mutex_unlock (&priv->lock);
2087 g_object_unref (old);
2091 * gst_rtsp_client_get_mount_points:
2092 * @client: a #GstRTSPClient
2094 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2096 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2098 GstRTSPMountPoints *
2099 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2101 GstRTSPClientPrivate *priv;
2102 GstRTSPMountPoints *result;
2104 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2106 priv = client->priv;
2108 g_mutex_lock (&priv->lock);
2109 if ((result = priv->mount_points))
2110 g_object_ref (result);
2111 g_mutex_unlock (&priv->lock);
2117 * gst_rtsp_client_set_auth:
2118 * @client: a #GstRTSPClient
2119 * @auth: a #GstRTSPAuth
2121 * configure @auth to be used as the authentication manager of @client.
2124 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2126 GstRTSPClientPrivate *priv;
2129 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2131 priv = client->priv;
2134 g_object_ref (auth);
2136 g_mutex_lock (&priv->lock);
2139 g_mutex_unlock (&priv->lock);
2142 g_object_unref (old);
2147 * gst_rtsp_client_get_auth:
2148 * @client: a #GstRTSPClient
2150 * Get the #GstRTSPAuth used as the authentication manager of @client.
2152 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2156 gst_rtsp_client_get_auth (GstRTSPClient * client)
2158 GstRTSPClientPrivate *priv;
2159 GstRTSPAuth *result;
2161 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2163 priv = client->priv;
2165 g_mutex_lock (&priv->lock);
2166 if ((result = priv->auth))
2167 g_object_ref (result);
2168 g_mutex_unlock (&priv->lock);
2174 * gst_rtsp_client_set_thread_pool:
2175 * @client: a #GstRTSPClient
2176 * @pool: a #GstRTSPThreadPool
2178 * configure @pool to be used as the thread pool of @client.
2181 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2182 GstRTSPThreadPool * pool)
2184 GstRTSPClientPrivate *priv;
2185 GstRTSPThreadPool *old;
2187 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2189 priv = client->priv;
2192 g_object_ref (pool);
2194 g_mutex_lock (&priv->lock);
2195 old = priv->thread_pool;
2196 priv->thread_pool = pool;
2197 g_mutex_unlock (&priv->lock);
2200 g_object_unref (old);
2204 * gst_rtsp_client_get_thread_pool:
2205 * @client: a #GstRTSPClient
2207 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2209 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2213 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2215 GstRTSPClientPrivate *priv;
2216 GstRTSPThreadPool *result;
2218 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2220 priv = client->priv;
2222 g_mutex_lock (&priv->lock);
2223 if ((result = priv->thread_pool))
2224 g_object_ref (result);
2225 g_mutex_unlock (&priv->lock);
2231 * gst_rtsp_client_set_connection:
2232 * @client: a #GstRTSPClient
2233 * @conn: (transfer full): a #GstRTSPConnection
2235 * Set the #GstRTSPConnection of @client. This function takes ownership of
2238 * Returns: %TRUE on success.
2241 gst_rtsp_client_set_connection (GstRTSPClient * client,
2242 GstRTSPConnection * conn)
2244 GstRTSPClientPrivate *priv;
2245 GSocket *read_socket;
2246 GSocketAddress *address;
2248 GError *error = NULL;
2250 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2251 g_return_val_if_fail (conn != NULL, FALSE);
2253 priv = client->priv;
2255 read_socket = gst_rtsp_connection_get_read_socket (conn);
2257 if (!(address = g_socket_get_local_address (read_socket, &error)))
2260 g_free (priv->server_ip);
2261 /* keep the original ip that the client connected to */
2262 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2263 GInetAddress *iaddr;
2265 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2267 /* socket might be ipv6 but adress still ipv4 */
2268 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2269 priv->server_ip = g_inet_address_to_string (iaddr);
2270 g_object_unref (address);
2272 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2273 priv->server_ip = g_strdup ("unknown");
2276 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2277 priv->server_ip, priv->is_ipv6);
2279 url = gst_rtsp_connection_get_url (conn);
2280 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2282 priv->connection = conn;
2289 GST_ERROR ("could not get local address %s", error->message);
2290 g_error_free (error);
2296 * gst_rtsp_client_get_connection:
2297 * @client: a #GstRTSPClient
2299 * Get the #GstRTSPConnection of @client.
2301 * Returns: (transfer none): the #GstRTSPConnection of @client.
2302 * The connection object returned remains valid until the client is freed.
2305 gst_rtsp_client_get_connection (GstRTSPClient * client)
2307 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2309 return client->priv->connection;
2313 * gst_rtsp_client_set_send_func:
2314 * @client: a #GstRTSPClient
2315 * @func: a #GstRTSPClientSendFunc
2316 * @user_data: user data passed to @func
2317 * @notify: called when @user_data is no longer in use
2319 * Set @func as the callback that will be called when a new message needs to be
2320 * sent to the client. @user_data is passed to @func and @notify is called when
2321 * @user_data is no longer in use.
2323 * By default, the client will send the messages on the #GstRTSPConnection that
2324 * was configured with gst_rtsp_client_attach() was called.
2327 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2328 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2330 GstRTSPClientPrivate *priv;
2331 GDestroyNotify old_notify;
2334 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2336 priv = client->priv;
2338 g_mutex_lock (&priv->send_lock);
2339 priv->send_func = func;
2340 old_notify = priv->send_notify;
2341 old_data = priv->send_data;
2342 priv->send_notify = notify;
2343 priv->send_data = user_data;
2344 g_mutex_unlock (&priv->send_lock);
2347 old_notify (old_data);
2351 * gst_rtsp_client_handle_message:
2352 * @client: a #GstRTSPClient
2353 * @message: an #GstRTSPMessage
2355 * Let the client handle @message.
2357 * Returns: a #GstRTSPResult.
2360 gst_rtsp_client_handle_message (GstRTSPClient * client,
2361 GstRTSPMessage * message)
2363 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2364 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2366 switch (message->type) {
2367 case GST_RTSP_MESSAGE_REQUEST:
2368 handle_request (client, message);
2370 case GST_RTSP_MESSAGE_RESPONSE:
2372 case GST_RTSP_MESSAGE_DATA:
2373 handle_data (client, message);
2382 * gst_rtsp_client_send_message:
2383 * @client: a #GstRTSPClient
2384 * @session: a #GstRTSPSession to send the message to or %NULL
2385 * @message: The #GstRTSPMessage to send
2387 * Send a message message to the remote end. @message must be a
2388 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
2391 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
2392 GstRTSPMessage * message)
2394 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2395 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2396 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
2397 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
2399 send_message (client, session, message, FALSE);
2404 static GstRTSPResult
2405 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2406 gboolean close, gpointer user_data)
2408 GstRTSPClientPrivate *priv = client->priv;
2410 /* send the response and store the seq number so we can wait until it's
2411 * written to the client to close the connection */
2412 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2413 &priv->close_seq : NULL);
2416 static GstRTSPResult
2417 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2420 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2423 static GstRTSPResult
2424 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2426 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2427 GstRTSPClientPrivate *priv = client->priv;
2429 if (priv->close_seq && priv->close_seq == cseq) {
2430 priv->close_seq = 0;
2431 close_connection (client);
2437 static GstRTSPResult
2438 closed (GstRTSPWatch * watch, gpointer user_data)
2440 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2441 GstRTSPClientPrivate *priv = client->priv;
2442 const gchar *tunnelid;
2444 GST_INFO ("client %p: connection closed", client);
2446 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2447 g_mutex_lock (&tunnels_lock);
2448 /* remove from tunnelids */
2449 g_hash_table_remove (tunnels, tunnelid);
2450 g_mutex_unlock (&tunnels_lock);
2453 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2458 static GstRTSPResult
2459 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2461 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2464 str = gst_rtsp_strresult (result);
2465 GST_INFO ("client %p: received an error %s", client, str);
2471 static GstRTSPResult
2472 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2473 GstRTSPMessage * message, guint id, gpointer user_data)
2475 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2478 str = gst_rtsp_strresult (result);
2480 ("client %p: error when handling message %p with id %d: %s",
2481 client, message, id, str);
2488 remember_tunnel (GstRTSPClient * client)
2490 GstRTSPClientPrivate *priv = client->priv;
2491 const gchar *tunnelid;
2493 /* store client in the pending tunnels */
2494 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2495 if (tunnelid == NULL)
2498 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2500 /* we can't have two clients connecting with the same tunnelid */
2501 g_mutex_lock (&tunnels_lock);
2502 if (g_hash_table_lookup (tunnels, tunnelid))
2503 goto tunnel_existed;
2505 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2506 g_mutex_unlock (&tunnels_lock);
2513 GST_ERROR ("client %p: no tunnelid provided", client);
2518 g_mutex_unlock (&tunnels_lock);
2519 GST_ERROR ("client %p: tunnel session %s already existed", client,
2525 static GstRTSPStatusCode
2526 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2528 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2529 GstRTSPClientPrivate *priv = client->priv;
2531 GST_INFO ("client %p: tunnel start (connection %p)", client,
2534 if (!remember_tunnel (client))
2537 return GST_RTSP_STS_OK;
2542 GST_ERROR ("client %p: error starting tunnel", client);
2543 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2547 static GstRTSPResult
2548 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2550 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2551 GstRTSPClientPrivate *priv = client->priv;
2553 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2556 /* ignore error, it'll only be a problem when the client does a POST again */
2557 remember_tunnel (client);
2562 static GstRTSPResult
2563 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2565 const gchar *tunnelid;
2566 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2567 GstRTSPClientPrivate *priv = client->priv;
2568 GstRTSPClient *oclient;
2569 GstRTSPClientPrivate *opriv;
2571 GST_INFO ("client %p: tunnel complete", client);
2573 /* find previous tunnel */
2574 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2575 if (tunnelid == NULL)
2578 g_mutex_lock (&tunnels_lock);
2579 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2582 /* remove the old client from the table. ref before because removing it will
2583 * remove the ref to it. */
2584 g_object_ref (oclient);
2585 g_hash_table_remove (tunnels, tunnelid);
2587 opriv = oclient->priv;
2589 if (opriv->watch == NULL)
2591 g_mutex_unlock (&tunnels_lock);
2593 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2594 opriv->connection, priv->connection);
2596 /* merge the tunnels into the first client */
2597 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2598 gst_rtsp_watch_reset (opriv->watch);
2599 g_object_unref (oclient);
2606 GST_ERROR ("client %p: no tunnelid provided", client);
2607 return GST_RTSP_ERROR;
2611 g_mutex_unlock (&tunnels_lock);
2612 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2613 return GST_RTSP_ERROR;
2617 g_mutex_unlock (&tunnels_lock);
2618 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2619 g_object_unref (oclient);
2620 return GST_RTSP_ERROR;
2624 static GstRTSPWatchFuncs watch_funcs = {
2636 client_watch_notify (GstRTSPClient * client)
2638 GstRTSPClientPrivate *priv = client->priv;
2640 GST_INFO ("client %p: watch destroyed", client);
2642 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2643 g_object_unref (client);
2647 * gst_rtsp_client_attach:
2648 * @client: a #GstRTSPClient
2649 * @context: (allow-none): a #GMainContext
2651 * Attaches @client to @context. When the mainloop for @context is run, the
2652 * client will be dispatched. When @context is NULL, the default context will be
2655 * This function should be called when the client properties and urls are fully
2656 * configured and the client is ready to start.
2658 * Returns: the ID (greater than 0) for the source within the GMainContext.
2661 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2663 GstRTSPClientPrivate *priv;
2666 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2667 priv = client->priv;
2668 g_return_val_if_fail (priv->connection != NULL, 0);
2669 g_return_val_if_fail (priv->watch == NULL, 0);
2671 /* create watch for the connection and attach */
2672 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2673 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2674 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2675 (GDestroyNotify) gst_rtsp_watch_unref);
2677 /* FIXME make this configurable. We don't want to do this yet because it will
2678 * be superceeded by a cache object later */
2679 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2681 GST_INFO ("attaching to context %p", context);
2682 res = gst_rtsp_watch_attach (priv->watch, context);
2688 * gst_rtsp_client_session_filter:
2689 * @client: a #GstRTSPClient
2690 * @func: (scope call): a callback
2691 * @user_data: user data passed to @func
2693 * Call @func for each session managed by @client. The result value of @func
2694 * determines what happens to the session. @func will be called with @client
2695 * locked so no further actions on @client can be performed from @func.
2697 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
2700 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
2702 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
2703 * will also be added with an additional ref to the result #GList of this
2706 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
2707 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2708 * element in the #GList should be unreffed before the list is freed.
2711 gst_rtsp_client_session_filter (GstRTSPClient * client,
2712 GstRTSPClientSessionFilterFunc func, gpointer user_data)
2714 GstRTSPClientPrivate *priv;
2715 GList *result, *walk, *next;
2717 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2718 g_return_val_if_fail (func != NULL, NULL);
2720 priv = client->priv;
2724 g_mutex_lock (&priv->lock);
2725 for (walk = priv->sessions; walk; walk = next) {
2726 GstRTSPSession *sess = walk->data;
2728 next = g_list_next (walk);
2730 switch (func (client, sess, user_data)) {
2731 case GST_RTSP_FILTER_REMOVE:
2732 /* stop watching the session and pretent it went away */
2733 client_cleanup_session (client, sess);
2735 case GST_RTSP_FILTER_REF:
2736 result = g_list_prepend (result, g_object_ref (sess));
2738 case GST_RTSP_FILTER_KEEP:
2743 g_mutex_unlock (&priv->lock);