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);
355 gst_rtsp_media_unprepare (priv->media);
356 g_object_unref (priv->media);
359 g_free (priv->server_ip);
360 g_mutex_clear (&priv->lock);
361 g_mutex_clear (&priv->send_lock);
363 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
367 gst_rtsp_client_get_property (GObject * object, guint propid,
368 GValue * value, GParamSpec * pspec)
370 GstRTSPClient *client = GST_RTSP_CLIENT (object);
373 case PROP_SESSION_POOL:
374 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
376 case PROP_MOUNT_POINTS:
377 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
380 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
385 gst_rtsp_client_set_property (GObject * object, guint propid,
386 const GValue * value, GParamSpec * pspec)
388 GstRTSPClient *client = GST_RTSP_CLIENT (object);
391 case PROP_SESSION_POOL:
392 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
394 case PROP_MOUNT_POINTS:
395 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
398 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
403 * gst_rtsp_client_new:
405 * Create a new #GstRTSPClient instance.
407 * Returns: a new #GstRTSPClient
410 gst_rtsp_client_new (void)
412 GstRTSPClient *result;
414 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
420 send_message (GstRTSPClient * client, GstRTSPSession * session,
421 GstRTSPMessage * message, gboolean close)
423 GstRTSPClientPrivate *priv = client->priv;
425 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
426 "GStreamer RTSP server");
428 /* remove any previous header */
429 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
431 /* add the new session header for new session ids */
433 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
434 gst_rtsp_session_get_header (session));
437 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
438 gst_rtsp_message_dump (message);
442 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
444 g_mutex_lock (&priv->send_lock);
446 priv->send_func (client, message, close, priv->send_data);
447 g_mutex_unlock (&priv->send_lock);
449 gst_rtsp_message_unset (message);
453 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
454 GstRTSPContext * ctx)
456 gst_rtsp_message_init_response (ctx->response, code,
457 gst_rtsp_status_as_text (code), ctx->request);
459 send_message (client, NULL, ctx->response, FALSE);
463 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
465 if (path1 == NULL || path2 == NULL)
468 if (strlen (path1) != len2)
471 if (strncmp (path1, path2, len2))
477 /* this function is called to initially find the media for the DESCRIBE request
478 * but is cached for when the same client (without breaking the connection) is
479 * doing a setup for the exact same url. */
480 static GstRTSPMedia *
481 find_media (GstRTSPClient * client, GstRTSPContext * ctx, gint * matched)
483 GstRTSPClientPrivate *priv = client->priv;
484 GstRTSPMediaFactory *factory;
489 if (!priv->mount_points)
490 goto no_mount_points;
492 path = ctx->uri->abspath;
494 /* find the longest matching factory for the uri first */
495 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
499 ctx->factory = factory;
501 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
502 goto no_factory_access;
504 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
510 path_len = strlen (path);
512 if (!paths_are_equal (priv->path, path, path_len)) {
513 GstRTSPThread *thread;
515 /* remove any previously cached values before we try to construct a new
521 gst_rtsp_media_unprepare (priv->media);
522 g_object_unref (priv->media);
526 /* prepare the media and add it to the pipeline */
527 if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
532 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
533 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
537 /* prepare the media */
538 if (!(gst_rtsp_media_prepare (media, thread)))
541 /* now keep track of the uri and the media */
542 priv->path = g_strndup (path, path_len);
545 /* we have seen this path before, used cached media */
548 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
551 g_object_unref (factory);
555 g_object_ref (media);
562 GST_ERROR ("client %p: no mount points configured", client);
563 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
568 GST_ERROR ("client %p: no factory for uri %s", client, path);
569 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
574 GST_ERROR ("client %p: not authorized to see factory uri %s", client, path);
579 GST_ERROR ("client %p: not authorized for factory uri %s", client, path);
584 GST_ERROR ("client %p: can't create media", client);
585 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
586 g_object_unref (factory);
592 GST_ERROR ("client %p: can't create thread", client);
593 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
594 g_object_unref (media);
596 g_object_unref (factory);
602 GST_ERROR ("client %p: can't prepare media", client);
603 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
604 g_object_unref (media);
606 g_object_unref (factory);
613 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
615 GstRTSPClientPrivate *priv = client->priv;
616 GstRTSPMessage message = { 0 };
621 gst_rtsp_message_init_data (&message, channel);
623 /* FIXME, need some sort of iovec RTSPMessage here */
624 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
627 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
629 g_mutex_lock (&priv->send_lock);
631 priv->send_func (client, &message, FALSE, priv->send_data);
632 g_mutex_unlock (&priv->send_lock);
634 gst_rtsp_message_steal_body (&message, &data, &usize);
635 gst_buffer_unmap (buffer, &map_info);
637 gst_rtsp_message_unset (&message);
643 link_transport (GstRTSPClient * client, GstRTSPSession * session,
644 GstRTSPStreamTransport * trans)
646 GstRTSPClientPrivate *priv = client->priv;
648 GST_DEBUG ("client %p: linking transport %p", client, trans);
650 gst_rtsp_stream_transport_set_callbacks (trans,
651 (GstRTSPSendFunc) do_send_data,
652 (GstRTSPSendFunc) do_send_data, client, NULL);
654 priv->transports = g_list_prepend (priv->transports, trans);
656 /* make sure our session can't expire */
657 gst_rtsp_session_prevent_expire (session);
661 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
662 GstRTSPStreamTransport * trans)
664 GstRTSPClientPrivate *priv = client->priv;
666 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
668 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
670 priv->transports = g_list_remove (priv->transports, trans);
672 /* our session can now expire */
673 gst_rtsp_session_allow_expire (session);
677 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
678 GstRTSPSessionMedia * sessmedia)
683 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
684 for (i = 0; i < n_streams; i++) {
685 GstRTSPStreamTransport *trans;
686 const GstRTSPTransport *tr;
688 /* get the transport, if there is no transport configured, skip this stream */
689 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
693 tr = gst_rtsp_stream_transport_get_transport (trans);
695 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
696 /* for TCP, unlink the stream from the TCP connection of the client */
697 unlink_transport (client, session, trans);
703 close_connection (GstRTSPClient * client)
705 GstRTSPClientPrivate *priv = client->priv;
706 const gchar *tunnelid;
708 GST_DEBUG ("client %p: closing connection", client);
710 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
711 g_mutex_lock (&tunnels_lock);
712 /* remove from tunnelids */
713 g_hash_table_remove (tunnels, tunnelid);
714 g_mutex_unlock (&tunnels_lock);
717 gst_rtsp_connection_close (priv->connection);
721 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
723 GstRTSPClientPrivate *priv = client->priv;
724 GstRTSPSession *session;
725 GstRTSPSessionMedia *sessmedia;
726 GstRTSPStatusCode code;
733 session = ctx->session;
738 path = ctx->uri->abspath;
740 /* get a handle to the configuration of the media in the session */
741 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
745 /* only aggregate control for now.. */
746 if (path[matched] != '\0')
749 ctx->sessmedia = sessmedia;
751 /* we emit the signal before closing the connection */
752 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
755 /* unlink the all TCP callbacks */
756 unlink_session_transports (client, session, sessmedia);
758 /* remove the session from the watched sessions */
759 client_unwatch_session (client, session);
761 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
763 /* unmanage the media in the session, returns false if all media session
765 if (!gst_rtsp_session_release_media (session, sessmedia)) {
766 /* remove the session */
767 gst_rtsp_session_pool_remove (priv->session_pool, session);
769 /* construct the response now */
770 code = GST_RTSP_STS_OK;
771 gst_rtsp_message_init_response (ctx->response, code,
772 gst_rtsp_status_as_text (code), ctx->request);
774 send_message (client, session, ctx->response, TRUE);
781 GST_ERROR ("client %p: no session", client);
782 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
787 GST_ERROR ("client %p: no uri supplied", client);
788 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
793 GST_ERROR ("client %p: no media for uri", client);
794 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
799 GST_ERROR ("client %p: no aggregate path %s", client, path);
800 send_generic_response (client,
801 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
807 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
811 res = gst_rtsp_params_set (client, ctx);
817 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
821 res = gst_rtsp_params_get (client, ctx);
827 handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
833 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
834 if (res != GST_RTSP_OK)
838 /* no body, keep-alive request */
839 send_generic_response (client, GST_RTSP_STS_OK, ctx);
841 /* there is a body, handle the params */
842 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
843 if (res != GST_RTSP_OK)
846 send_message (client, ctx->session, ctx->response, FALSE);
849 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
857 GST_ERROR ("client %p: bad request", client);
858 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
864 handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
870 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
871 if (res != GST_RTSP_OK)
875 /* no body, keep-alive request */
876 send_generic_response (client, GST_RTSP_STS_OK, ctx);
878 /* there is a body, handle the params */
879 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
880 if (res != GST_RTSP_OK)
883 send_message (client, ctx->session, ctx->response, FALSE);
886 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
894 GST_ERROR ("client %p: bad request", client);
895 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
901 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
903 GstRTSPSession *session;
904 GstRTSPSessionMedia *sessmedia;
905 GstRTSPStatusCode code;
906 GstRTSPState rtspstate;
910 if (!(session = ctx->session))
916 path = ctx->uri->abspath;
918 /* get a handle to the configuration of the media in the session */
919 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
923 if (path[matched] != '\0')
926 ctx->sessmedia = sessmedia;
928 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
929 /* the session state must be playing or recording */
930 if (rtspstate != GST_RTSP_STATE_PLAYING &&
931 rtspstate != GST_RTSP_STATE_RECORDING)
934 /* unlink the all TCP callbacks */
935 unlink_session_transports (client, session, sessmedia);
937 /* then pause sending */
938 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
940 /* construct the response now */
941 code = GST_RTSP_STS_OK;
942 gst_rtsp_message_init_response (ctx->response, code,
943 gst_rtsp_status_as_text (code), ctx->request);
945 send_message (client, session, ctx->response, FALSE);
947 /* the state is now READY */
948 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
950 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
957 GST_ERROR ("client %p: no seesion", client);
958 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
963 GST_ERROR ("client %p: no uri supplied", client);
964 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
969 GST_ERROR ("client %p: no media for uri", client);
970 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
975 GST_ERROR ("client %p: no aggregate path %s", client, path);
976 send_generic_response (client,
977 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
982 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
983 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
990 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
992 GstRTSPSession *session;
993 GstRTSPSessionMedia *sessmedia;
995 GstRTSPStatusCode code;
997 guint n_streams, i, infocount;
999 GstRTSPTimeRange *range;
1001 GstRTSPState rtspstate;
1002 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1006 if (!(session = ctx->session))
1012 path = ctx->uri->abspath;
1014 /* get a handle to the configuration of the media in the session */
1015 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1019 if (path[matched] != '\0')
1022 ctx->sessmedia = sessmedia;
1023 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1025 /* the session state must be playing or ready */
1026 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1027 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1030 /* parse the range header if we have one */
1031 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1032 if (res == GST_RTSP_OK) {
1033 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1034 /* we have a range, seek to the position */
1036 gst_rtsp_media_seek (media, range);
1037 gst_rtsp_range_free (range);
1041 /* grab RTPInfo from the payloaders now */
1042 rtpinfo = g_string_new ("");
1044 n_streams = gst_rtsp_media_n_streams (media);
1045 for (i = 0, infocount = 0; i < n_streams; i++) {
1046 GstRTSPStreamTransport *trans;
1047 GstRTSPStream *stream;
1048 const GstRTSPTransport *tr;
1052 /* get the transport, if there is no transport configured, skip this stream */
1053 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
1054 if (trans == NULL) {
1055 GST_INFO ("stream %d is not configured", i);
1058 tr = gst_rtsp_stream_transport_get_transport (trans);
1060 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1061 /* for TCP, link the stream to the TCP connection of the client */
1062 link_transport (client, session, trans);
1065 stream = gst_rtsp_stream_transport_get_stream (trans);
1066 if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
1068 g_string_append (rtpinfo, ", ");
1070 uristr = gst_rtsp_url_get_request_uri (ctx->uri);
1071 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
1072 uristr, i, seq, rtptime);
1077 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
1081 /* construct the response now */
1082 code = GST_RTSP_STS_OK;
1083 gst_rtsp_message_init_response (ctx->response, code,
1084 gst_rtsp_status_as_text (code), ctx->request);
1086 /* add the RTP-Info header */
1087 if (infocount > 0) {
1088 str = g_string_free (rtpinfo, FALSE);
1089 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO, str);
1091 g_string_free (rtpinfo, TRUE);
1095 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1097 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1099 send_message (client, session, ctx->response, FALSE);
1101 /* start playing after sending the request */
1102 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1104 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1106 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1113 GST_ERROR ("client %p: no session", client);
1114 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1119 GST_ERROR ("client %p: no uri supplied", client);
1120 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1125 GST_ERROR ("client %p: media not found", client);
1126 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1131 GST_ERROR ("client %p: no aggregate path %s", client, path);
1132 send_generic_response (client,
1133 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1138 GST_ERROR ("client %p: not PLAYING or READY", client);
1139 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1146 do_keepalive (GstRTSPSession * session)
1148 GST_INFO ("keep session %p alive", session);
1149 gst_rtsp_session_touch (session);
1152 /* parse @transport and return a valid transport in @tr. only transports
1153 * from @supported are returned. Returns FALSE if no valid transport
1156 parse_transport (const char *transport, GstRTSPLowerTrans supported,
1157 GstRTSPTransport * tr)
1164 gst_rtsp_transport_init (tr);
1166 GST_DEBUG ("parsing transports %s", transport);
1168 transports = g_strsplit (transport, ",", 0);
1170 /* loop through the transports, try to parse */
1171 for (i = 0; transports[i]; i++) {
1172 res = gst_rtsp_transport_parse (transports[i], tr);
1173 if (res != GST_RTSP_OK) {
1174 /* no valid transport, search some more */
1175 GST_WARNING ("could not parse transport %s", transports[i]);
1179 /* we have a transport, see if it's RTP/AVP */
1180 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1181 GST_WARNING ("invalid transport %s", transports[i]);
1185 if (!(tr->lower_transport & supported)) {
1186 GST_WARNING ("unsupported transport %s", transports[i]);
1190 /* we have a valid transport */
1191 GST_INFO ("found valid transport %s", transports[i]);
1196 gst_rtsp_transport_init (tr);
1198 g_strfreev (transports);
1204 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
1205 GstRTSPMessage * request)
1207 gchar *blocksize_str;
1208 gboolean ret = TRUE;
1210 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1211 &blocksize_str, 0) == GST_RTSP_OK) {
1215 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1216 if (end == blocksize_str) {
1217 GST_ERROR ("failed to parse blocksize");
1220 /* we don't want to change the mtu when this media
1221 * can be shared because it impacts other clients */
1222 if (gst_rtsp_media_is_shared (media))
1225 if (blocksize > G_MAXUINT)
1226 blocksize = G_MAXUINT;
1227 gst_rtsp_stream_set_mtu (stream, blocksize);
1234 default_configure_client_transport (GstRTSPClient * client,
1235 GstRTSPContext * ctx, GstRTSPTransport * ct)
1237 GstRTSPClientPrivate *priv = client->priv;
1239 /* we have a valid transport now, set the destination of the client. */
1240 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1241 gboolean use_client_settings;
1243 use_client_settings =
1244 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1246 if (ct->destination && use_client_settings) {
1247 GstRTSPAddress *addr;
1249 addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1250 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1255 gst_rtsp_address_free (addr);
1257 GstRTSPAddress *addr;
1258 GSocketFamily family;
1260 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1262 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1266 g_free (ct->destination);
1267 ct->destination = g_strdup (addr->address);
1268 ct->port.min = addr->port;
1269 ct->port.max = addr->port + addr->n_ports - 1;
1270 ct->ttl = addr->ttl;
1272 gst_rtsp_address_free (addr);
1277 url = gst_rtsp_connection_get_url (priv->connection);
1278 g_free (ct->destination);
1279 ct->destination = g_strdup (url->host);
1281 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1282 /* check if the client selected channels for TCP */
1283 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1284 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1294 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1299 static GstRTSPTransport *
1300 make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
1301 GstRTSPTransport * ct)
1303 GstRTSPTransport *st;
1305 GSocketFamily family;
1307 /* prepare the server transport */
1308 gst_rtsp_transport_new (&st);
1310 st->trans = ct->trans;
1311 st->profile = ct->profile;
1312 st->lower_transport = ct->lower_transport;
1314 addr = g_inet_address_new_from_string (ct->destination);
1317 GST_ERROR ("failed to get inet addr from client destination");
1318 family = G_SOCKET_FAMILY_IPV4;
1320 family = g_inet_address_get_family (addr);
1321 g_object_unref (addr);
1325 switch (st->lower_transport) {
1326 case GST_RTSP_LOWER_TRANS_UDP:
1327 st->client_port = ct->client_port;
1328 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
1330 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1331 st->port = ct->port;
1332 st->destination = g_strdup (ct->destination);
1335 case GST_RTSP_LOWER_TRANS_TCP:
1336 st->interleaved = ct->interleaved;
1341 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
1347 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
1349 GstRTSPClientPrivate *priv = client->priv;
1353 GstRTSPTransport *ct, *st;
1354 GstRTSPLowerTrans supported;
1355 GstRTSPStatusCode code;
1356 GstRTSPSession *session;
1357 GstRTSPStreamTransport *trans;
1359 GstRTSPSessionMedia *sessmedia;
1360 GstRTSPMedia *media;
1361 GstRTSPStream *stream;
1362 GstRTSPState rtspstate;
1363 GstRTSPClientClass *klass;
1364 gchar *path, *control;
1371 path = uri->abspath;
1373 /* parse the transport */
1375 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
1377 if (res != GST_RTSP_OK)
1380 /* we create the session after parsing stuff so that we don't make
1381 * a session for malformed requests */
1382 if (priv->session_pool == NULL)
1385 session = ctx->session;
1388 g_object_ref (session);
1389 /* get a handle to the configuration of the media in the session, this can
1390 * return NULL if this is a new url to manage in this session. */
1391 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1393 /* we need a new media configuration in this session */
1397 /* we have no session media, find one and manage it */
1398 if (sessmedia == NULL) {
1399 /* get a handle to the configuration of the media in the session */
1400 media = find_media (client, ctx, &matched);
1402 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1403 g_object_ref (media);
1405 /* no media, not found then */
1407 goto media_not_found;
1409 /* path is what matched. We can modify the parsed uri in place */
1410 path[matched] = '\0';
1411 /* control is remainder */
1412 control = &path[matched + 1];
1414 /* find the stream now using the control part */
1415 stream = gst_rtsp_media_find_stream (media, control);
1417 goto stream_not_found;
1419 /* now we have a uri identifying a valid media and stream */
1420 ctx->stream = stream;
1423 if (session == NULL) {
1424 /* create a session if this fails we probably reached our session limit or
1426 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1427 goto service_unavailable;
1429 /* make sure this client is closed when the session is closed */
1430 client_watch_session (client, session);
1432 /* signal new session */
1433 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1436 ctx->session = session;
1439 if (sessmedia == NULL) {
1440 /* manage the media in our session now, if not done already */
1441 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1442 /* if we stil have no media, error */
1443 if (sessmedia == NULL)
1444 goto sessmedia_unavailable;
1446 g_object_unref (media);
1449 ctx->sessmedia = sessmedia;
1451 /* set blocksize on this stream */
1452 if (!handle_blocksize (media, stream, ctx->request))
1453 goto invalid_blocksize;
1455 gst_rtsp_transport_new (&ct);
1457 /* our supported transports */
1458 supported = gst_rtsp_stream_get_protocols (stream);
1460 /* parse and find a usable supported transport */
1461 if (!parse_transport (transport, supported, ct))
1462 goto unsupported_transports;
1464 /* update the client transport */
1465 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1466 if (!klass->configure_client_transport (client, ctx, ct))
1467 goto unsupported_client_transport;
1469 /* set in the session media transport */
1470 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1472 /* configure keepalive for this transport */
1473 gst_rtsp_stream_transport_set_keepalive (trans,
1474 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1476 /* create and serialize the server transport */
1477 st = make_server_transport (client, ctx, ct);
1478 trans_str = gst_rtsp_transport_as_text (st);
1479 gst_rtsp_transport_free (st);
1481 /* construct the response now */
1482 code = GST_RTSP_STS_OK;
1483 gst_rtsp_message_init_response (ctx->response, code,
1484 gst_rtsp_status_as_text (code), ctx->request);
1486 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
1490 send_message (client, session, ctx->response, FALSE);
1492 /* update the state */
1493 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1494 switch (rtspstate) {
1495 case GST_RTSP_STATE_PLAYING:
1496 case GST_RTSP_STATE_RECORDING:
1497 case GST_RTSP_STATE_READY:
1498 /* no state change */
1501 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1504 g_object_unref (session);
1506 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
1513 GST_ERROR ("client %p: no uri", client);
1514 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1519 GST_ERROR ("client %p: no transport", client);
1520 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1525 GST_ERROR ("client %p: no session pool configured", client);
1526 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1531 GST_ERROR ("client %p: media '%s' not found", client, path);
1532 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1537 GST_ERROR ("client %p: stream '%s' not found", client, control);
1538 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1539 g_object_unref (media);
1542 service_unavailable:
1544 GST_ERROR ("client %p: can't create session", client);
1545 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1546 g_object_unref (media);
1549 sessmedia_unavailable:
1551 GST_ERROR ("client %p: can't create session media", client);
1552 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1553 g_object_unref (media);
1554 g_object_unref (session);
1559 GST_ERROR ("client %p: invalid blocksize", client);
1560 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1561 g_object_unref (session);
1564 unsupported_transports:
1566 GST_ERROR ("client %p: unsupported transports", client);
1567 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1568 gst_rtsp_transport_free (ct);
1569 g_object_unref (session);
1572 unsupported_client_transport:
1574 GST_ERROR ("client %p: unsupported client transport", client);
1575 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1576 gst_rtsp_transport_free (ct);
1577 g_object_unref (session);
1582 static GstSDPMessage *
1583 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1585 GstRTSPClientPrivate *priv = client->priv;
1590 gst_sdp_message_new (&sdp);
1592 /* some standard things first */
1593 gst_sdp_message_set_version (sdp, "0");
1600 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1603 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1604 gst_sdp_message_set_information (sdp, "rtsp-server");
1605 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1606 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1607 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1608 gst_sdp_message_add_attribute (sdp, "control", "*");
1610 info.is_ipv6 = priv->is_ipv6;
1611 info.server_ip = priv->server_ip;
1613 /* create an SDP for the media object */
1614 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1622 GST_ERROR ("client %p: could not create SDP", client);
1623 gst_sdp_message_free (sdp);
1628 /* for the describe we must generate an SDP */
1630 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
1635 gchar *str, *str_query, *content_base;
1636 GstRTSPMedia *media;
1637 GstRTSPClientClass *klass;
1639 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1644 /* check what kind of format is accepted, we don't really do anything with it
1645 * and always return SDP for now. */
1650 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
1652 if (res == GST_RTSP_ENOTIMPL)
1655 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1659 /* find the media object for the uri */
1660 if (!(media = find_media (client, ctx, NULL)))
1663 /* create an SDP for the media object on this client */
1664 if (!(sdp = klass->create_sdp (client, media)))
1667 g_object_unref (media);
1669 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
1670 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
1672 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
1675 /* content base for some clients that might screw up creating the setup uri */
1676 str = gst_rtsp_url_get_request_uri (ctx->uri);
1677 str_len = strlen (str);
1679 /* check for query part */
1680 if (ctx->uri->query != NULL) {
1681 str_query = g_strrstr (str, "?");
1683 str_len = strlen (str);
1686 /* check for trailing '/' and append one */
1687 if (str[str_len - 1] != '/') {
1688 content_base = g_malloc (str_len + 2);
1689 memcpy (content_base, str, str_len);
1690 content_base[str_len] = '/';
1691 content_base[str_len + 1] = '\0';
1697 GST_INFO ("adding content-base: %s", content_base);
1699 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE,
1701 g_free (content_base);
1703 /* add SDP to the response body */
1704 str = gst_sdp_message_as_text (sdp);
1705 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
1706 gst_sdp_message_free (sdp);
1708 send_message (client, ctx->session, ctx->response, FALSE);
1710 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1718 GST_ERROR ("client %p: no uri", client);
1719 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1724 GST_ERROR ("client %p: no media", client);
1725 /* error reply is already sent */
1730 GST_ERROR ("client %p: can't create SDP", client);
1731 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1732 g_object_unref (media);
1738 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
1740 GstRTSPMethod options;
1743 options = GST_RTSP_DESCRIBE |
1748 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1750 str = gst_rtsp_options_as_text (options);
1752 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
1753 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
1755 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
1758 send_message (client, ctx->session, ctx->response, FALSE);
1760 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1766 /* remove duplicate and trailing '/' */
1768 sanitize_uri (GstRTSPUrl * uri)
1772 gboolean have_slash, prev_slash;
1774 s = d = uri->abspath;
1775 len = strlen (uri->abspath);
1779 for (i = 0; i < len; i++) {
1780 have_slash = s[i] == '/';
1782 if (!have_slash || !prev_slash)
1784 prev_slash = have_slash;
1786 len = d - uri->abspath;
1787 /* don't remove the first slash if that's the only thing left */
1788 if (len > 1 && *(d - 1) == '/')
1794 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1796 GstRTSPClientPrivate *priv = client->priv;
1798 GST_INFO ("client %p: session %p finished", client, session);
1800 /* unlink all media managed in this session */
1801 client_unlink_session (client, session);
1803 /* remove the session */
1804 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1805 GST_INFO ("client %p: all sessions finalized, close the connection",
1807 close_connection (client);
1812 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1814 GstRTSPClientPrivate *priv = client->priv;
1815 GstRTSPMethod method;
1816 const gchar *uristr;
1817 GstRTSPUrl *uri = NULL;
1818 GstRTSPVersion version;
1820 GstRTSPSession *session = NULL;
1821 GstRTSPContext sctx = { NULL }, *ctx;
1822 GstRTSPMessage response = { 0 };
1825 if (!(ctx = gst_rtsp_context_get_current ())) {
1827 ctx->auth = priv->auth;
1828 gst_rtsp_context_push_current (ctx);
1831 ctx->conn = priv->connection;
1832 ctx->client = client;
1833 ctx->request = request;
1834 ctx->response = &response;
1836 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1837 gst_rtsp_message_dump (request);
1840 GST_INFO ("client %p: received a request", client);
1842 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1844 /* we can only handle 1.0 requests */
1845 if (version != GST_RTSP_VERSION_1_0)
1848 ctx->method = method;
1850 /* we always try to parse the url first */
1851 if (strcmp (uristr, "*") == 0) {
1852 /* special case where we have * as uri, keep uri = NULL */
1853 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1856 /* get the session if there is any */
1857 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1858 if (res == GST_RTSP_OK) {
1859 if (priv->session_pool == NULL)
1862 /* we had a session in the request, find it again */
1863 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1864 goto session_not_found;
1866 /* we add the session to the client list of watched sessions. When a session
1867 * disappears because it times out, we will be notified. If all sessions are
1868 * gone, we will close the connection */
1869 client_watch_session (client, session);
1872 /* sanitize the uri */
1876 ctx->session = session;
1878 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
1879 goto not_authorized;
1881 /* now see what is asked and dispatch to a dedicated handler */
1883 case GST_RTSP_OPTIONS:
1884 handle_options_request (client, ctx);
1886 case GST_RTSP_DESCRIBE:
1887 handle_describe_request (client, ctx);
1889 case GST_RTSP_SETUP:
1890 handle_setup_request (client, ctx);
1893 handle_play_request (client, ctx);
1895 case GST_RTSP_PAUSE:
1896 handle_pause_request (client, ctx);
1898 case GST_RTSP_TEARDOWN:
1899 handle_teardown_request (client, ctx);
1901 case GST_RTSP_SET_PARAMETER:
1902 handle_set_param_request (client, ctx);
1904 case GST_RTSP_GET_PARAMETER:
1905 handle_get_param_request (client, ctx);
1907 case GST_RTSP_ANNOUNCE:
1908 case GST_RTSP_RECORD:
1909 case GST_RTSP_REDIRECT:
1910 goto not_implemented;
1911 case GST_RTSP_INVALID:
1918 gst_rtsp_context_pop_current (ctx);
1920 g_object_unref (session);
1922 gst_rtsp_url_free (uri);
1928 GST_ERROR ("client %p: version %d not supported", client, version);
1929 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1935 GST_ERROR ("client %p: bad request", client);
1936 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1941 GST_ERROR ("client %p: no pool configured", client);
1942 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1947 GST_ERROR ("client %p: session not found", client);
1948 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1953 GST_ERROR ("client %p: not allowed", client);
1958 GST_ERROR ("client %p: method %d not implemented", client, method);
1959 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
1966 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
1968 GstRTSPClientPrivate *priv = client->priv;
1970 GstRTSPSession *session = NULL;
1971 GstRTSPContext sctx = { NULL }, *ctx;
1974 if (!(ctx = gst_rtsp_context_get_current ())) {
1976 ctx->auth = priv->auth;
1977 gst_rtsp_context_push_current (ctx);
1980 ctx->conn = priv->connection;
1981 ctx->client = client;
1982 ctx->request = NULL;
1984 ctx->method = GST_RTSP_INVALID;
1985 ctx->response = response;
1987 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1988 gst_rtsp_message_dump (response);
1991 GST_INFO ("client %p: received a response", client);
1993 /* get the session if there is any */
1995 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
1996 if (res == GST_RTSP_OK) {
1997 if (priv->session_pool == NULL)
2000 /* we had a session in the request, find it again */
2001 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2002 goto session_not_found;
2004 /* we add the session to the client list of watched sessions. When a session
2005 * disappears because it times out, we will be notified. If all sessions are
2006 * gone, we will close the connection */
2007 client_watch_session (client, session);
2010 ctx->session = session;
2012 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
2017 gst_rtsp_context_pop_current (ctx);
2019 g_object_unref (session);
2024 GST_ERROR ("client %p: no pool configured", client);
2029 GST_ERROR ("client %p: session not found", client);
2035 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2037 GstRTSPClientPrivate *priv = client->priv;
2046 /* find the stream for this message */
2047 res = gst_rtsp_message_parse_data (message, &channel);
2048 if (res != GST_RTSP_OK)
2051 gst_rtsp_message_steal_body (message, &data, &size);
2053 buffer = gst_buffer_new_wrapped (data, size);
2056 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2057 GstRTSPStreamTransport *trans;
2058 GstRTSPStream *stream;
2059 const GstRTSPTransport *tr;
2063 tr = gst_rtsp_stream_transport_get_transport (trans);
2064 stream = gst_rtsp_stream_transport_get_stream (trans);
2066 /* check for TCP transport */
2067 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2068 /* dispatch to the stream based on the channel number */
2069 if (tr->interleaved.min == channel) {
2070 gst_rtsp_stream_recv_rtp (stream, buffer);
2073 } else if (tr->interleaved.max == channel) {
2074 gst_rtsp_stream_recv_rtcp (stream, buffer);
2081 gst_buffer_unref (buffer);
2085 * gst_rtsp_client_set_session_pool:
2086 * @client: a #GstRTSPClient
2087 * @pool: a #GstRTSPSessionPool
2089 * Set @pool as the sessionpool for @client which it will use to find
2090 * or allocate sessions. the sessionpool is usually inherited from the server
2091 * that created the client but can be overridden later.
2094 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2095 GstRTSPSessionPool * pool)
2097 GstRTSPSessionPool *old;
2098 GstRTSPClientPrivate *priv;
2100 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2102 priv = client->priv;
2105 g_object_ref (pool);
2107 g_mutex_lock (&priv->lock);
2108 old = priv->session_pool;
2109 priv->session_pool = pool;
2110 g_mutex_unlock (&priv->lock);
2113 g_object_unref (old);
2117 * gst_rtsp_client_get_session_pool:
2118 * @client: a #GstRTSPClient
2120 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2122 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2124 GstRTSPSessionPool *
2125 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2127 GstRTSPClientPrivate *priv;
2128 GstRTSPSessionPool *result;
2130 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2132 priv = client->priv;
2134 g_mutex_lock (&priv->lock);
2135 if ((result = priv->session_pool))
2136 g_object_ref (result);
2137 g_mutex_unlock (&priv->lock);
2143 * gst_rtsp_client_set_mount_points:
2144 * @client: a #GstRTSPClient
2145 * @mounts: a #GstRTSPMountPoints
2147 * Set @mounts as the mount points for @client which it will use to map urls
2148 * to media streams. These mount points are usually inherited from the server that
2149 * created the client but can be overriden later.
2152 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2153 GstRTSPMountPoints * mounts)
2155 GstRTSPClientPrivate *priv;
2156 GstRTSPMountPoints *old;
2158 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2160 priv = client->priv;
2163 g_object_ref (mounts);
2165 g_mutex_lock (&priv->lock);
2166 old = priv->mount_points;
2167 priv->mount_points = mounts;
2168 g_mutex_unlock (&priv->lock);
2171 g_object_unref (old);
2175 * gst_rtsp_client_get_mount_points:
2176 * @client: a #GstRTSPClient
2178 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2180 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2182 GstRTSPMountPoints *
2183 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2185 GstRTSPClientPrivate *priv;
2186 GstRTSPMountPoints *result;
2188 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2190 priv = client->priv;
2192 g_mutex_lock (&priv->lock);
2193 if ((result = priv->mount_points))
2194 g_object_ref (result);
2195 g_mutex_unlock (&priv->lock);
2201 * gst_rtsp_client_set_auth:
2202 * @client: a #GstRTSPClient
2203 * @auth: a #GstRTSPAuth
2205 * configure @auth to be used as the authentication manager of @client.
2208 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2210 GstRTSPClientPrivate *priv;
2213 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2215 priv = client->priv;
2218 g_object_ref (auth);
2220 g_mutex_lock (&priv->lock);
2223 g_mutex_unlock (&priv->lock);
2226 g_object_unref (old);
2231 * gst_rtsp_client_get_auth:
2232 * @client: a #GstRTSPClient
2234 * Get the #GstRTSPAuth used as the authentication manager of @client.
2236 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2240 gst_rtsp_client_get_auth (GstRTSPClient * client)
2242 GstRTSPClientPrivate *priv;
2243 GstRTSPAuth *result;
2245 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2247 priv = client->priv;
2249 g_mutex_lock (&priv->lock);
2250 if ((result = priv->auth))
2251 g_object_ref (result);
2252 g_mutex_unlock (&priv->lock);
2258 * gst_rtsp_client_set_thread_pool:
2259 * @client: a #GstRTSPClient
2260 * @pool: a #GstRTSPThreadPool
2262 * configure @pool to be used as the thread pool of @client.
2265 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2266 GstRTSPThreadPool * pool)
2268 GstRTSPClientPrivate *priv;
2269 GstRTSPThreadPool *old;
2271 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2273 priv = client->priv;
2276 g_object_ref (pool);
2278 g_mutex_lock (&priv->lock);
2279 old = priv->thread_pool;
2280 priv->thread_pool = pool;
2281 g_mutex_unlock (&priv->lock);
2284 g_object_unref (old);
2288 * gst_rtsp_client_get_thread_pool:
2289 * @client: a #GstRTSPClient
2291 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2293 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2297 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2299 GstRTSPClientPrivate *priv;
2300 GstRTSPThreadPool *result;
2302 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2304 priv = client->priv;
2306 g_mutex_lock (&priv->lock);
2307 if ((result = priv->thread_pool))
2308 g_object_ref (result);
2309 g_mutex_unlock (&priv->lock);
2315 * gst_rtsp_client_set_connection:
2316 * @client: a #GstRTSPClient
2317 * @conn: (transfer full): a #GstRTSPConnection
2319 * Set the #GstRTSPConnection of @client. This function takes ownership of
2322 * Returns: %TRUE on success.
2325 gst_rtsp_client_set_connection (GstRTSPClient * client,
2326 GstRTSPConnection * conn)
2328 GstRTSPClientPrivate *priv;
2329 GSocket *read_socket;
2330 GSocketAddress *address;
2332 GError *error = NULL;
2334 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2335 g_return_val_if_fail (conn != NULL, FALSE);
2337 priv = client->priv;
2339 read_socket = gst_rtsp_connection_get_read_socket (conn);
2341 if (!(address = g_socket_get_local_address (read_socket, &error)))
2344 g_free (priv->server_ip);
2345 /* keep the original ip that the client connected to */
2346 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2347 GInetAddress *iaddr;
2349 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2351 /* socket might be ipv6 but adress still ipv4 */
2352 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2353 priv->server_ip = g_inet_address_to_string (iaddr);
2354 g_object_unref (address);
2356 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2357 priv->server_ip = g_strdup ("unknown");
2360 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2361 priv->server_ip, priv->is_ipv6);
2363 url = gst_rtsp_connection_get_url (conn);
2364 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2366 priv->connection = conn;
2373 GST_ERROR ("could not get local address %s", error->message);
2374 g_error_free (error);
2380 * gst_rtsp_client_get_connection:
2381 * @client: a #GstRTSPClient
2383 * Get the #GstRTSPConnection of @client.
2385 * Returns: (transfer none): the #GstRTSPConnection of @client.
2386 * The connection object returned remains valid until the client is freed.
2389 gst_rtsp_client_get_connection (GstRTSPClient * client)
2391 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2393 return client->priv->connection;
2397 * gst_rtsp_client_set_send_func:
2398 * @client: a #GstRTSPClient
2399 * @func: a #GstRTSPClientSendFunc
2400 * @user_data: user data passed to @func
2401 * @notify: called when @user_data is no longer in use
2403 * Set @func as the callback that will be called when a new message needs to be
2404 * sent to the client. @user_data is passed to @func and @notify is called when
2405 * @user_data is no longer in use.
2407 * By default, the client will send the messages on the #GstRTSPConnection that
2408 * was configured with gst_rtsp_client_attach() was called.
2411 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2412 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2414 GstRTSPClientPrivate *priv;
2415 GDestroyNotify old_notify;
2418 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2420 priv = client->priv;
2422 g_mutex_lock (&priv->send_lock);
2423 priv->send_func = func;
2424 old_notify = priv->send_notify;
2425 old_data = priv->send_data;
2426 priv->send_notify = notify;
2427 priv->send_data = user_data;
2428 g_mutex_unlock (&priv->send_lock);
2431 old_notify (old_data);
2435 * gst_rtsp_client_handle_message:
2436 * @client: a #GstRTSPClient
2437 * @message: an #GstRTSPMessage
2439 * Let the client handle @message.
2441 * Returns: a #GstRTSPResult.
2444 gst_rtsp_client_handle_message (GstRTSPClient * client,
2445 GstRTSPMessage * message)
2447 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2448 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2450 switch (message->type) {
2451 case GST_RTSP_MESSAGE_REQUEST:
2452 handle_request (client, message);
2454 case GST_RTSP_MESSAGE_RESPONSE:
2455 handle_response (client, message);
2457 case GST_RTSP_MESSAGE_DATA:
2458 handle_data (client, message);
2467 * gst_rtsp_client_send_message:
2468 * @client: a #GstRTSPClient
2469 * @session: a #GstRTSPSession to send the message to or %NULL
2470 * @message: The #GstRTSPMessage to send
2472 * Send a message message to the remote end. @message must be a
2473 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
2476 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
2477 GstRTSPMessage * message)
2479 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2480 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2481 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
2482 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
2484 send_message (client, session, message, FALSE);
2489 static GstRTSPResult
2490 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2491 gboolean close, gpointer user_data)
2493 GstRTSPClientPrivate *priv = client->priv;
2495 /* send the response and store the seq number so we can wait until it's
2496 * written to the client to close the connection */
2497 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2498 &priv->close_seq : NULL);
2501 static GstRTSPResult
2502 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2505 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2508 static GstRTSPResult
2509 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2511 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2512 GstRTSPClientPrivate *priv = client->priv;
2514 if (priv->close_seq && priv->close_seq == cseq) {
2515 priv->close_seq = 0;
2516 close_connection (client);
2522 static GstRTSPResult
2523 closed (GstRTSPWatch * watch, gpointer user_data)
2525 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2526 GstRTSPClientPrivate *priv = client->priv;
2527 const gchar *tunnelid;
2529 GST_INFO ("client %p: connection closed", client);
2531 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2532 g_mutex_lock (&tunnels_lock);
2533 /* remove from tunnelids */
2534 g_hash_table_remove (tunnels, tunnelid);
2535 g_mutex_unlock (&tunnels_lock);
2538 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2543 static GstRTSPResult
2544 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2546 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2549 str = gst_rtsp_strresult (result);
2550 GST_INFO ("client %p: received an error %s", client, str);
2556 static GstRTSPResult
2557 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2558 GstRTSPMessage * message, guint id, gpointer user_data)
2560 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2563 str = gst_rtsp_strresult (result);
2565 ("client %p: error when handling message %p with id %d: %s",
2566 client, message, id, str);
2573 remember_tunnel (GstRTSPClient * client)
2575 GstRTSPClientPrivate *priv = client->priv;
2576 const gchar *tunnelid;
2578 /* store client in the pending tunnels */
2579 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2580 if (tunnelid == NULL)
2583 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2585 /* we can't have two clients connecting with the same tunnelid */
2586 g_mutex_lock (&tunnels_lock);
2587 if (g_hash_table_lookup (tunnels, tunnelid))
2588 goto tunnel_existed;
2590 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2591 g_mutex_unlock (&tunnels_lock);
2598 GST_ERROR ("client %p: no tunnelid provided", client);
2603 g_mutex_unlock (&tunnels_lock);
2604 GST_ERROR ("client %p: tunnel session %s already existed", client,
2610 static GstRTSPStatusCode
2611 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2613 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2614 GstRTSPClientPrivate *priv = client->priv;
2616 GST_INFO ("client %p: tunnel start (connection %p)", client,
2619 if (!remember_tunnel (client))
2622 return GST_RTSP_STS_OK;
2627 GST_ERROR ("client %p: error starting tunnel", client);
2628 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2632 static GstRTSPResult
2633 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2635 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2636 GstRTSPClientPrivate *priv = client->priv;
2638 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2641 /* ignore error, it'll only be a problem when the client does a POST again */
2642 remember_tunnel (client);
2647 static GstRTSPResult
2648 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2650 const gchar *tunnelid;
2651 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2652 GstRTSPClientPrivate *priv = client->priv;
2653 GstRTSPClient *oclient;
2654 GstRTSPClientPrivate *opriv;
2656 GST_INFO ("client %p: tunnel complete", client);
2658 /* find previous tunnel */
2659 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2660 if (tunnelid == NULL)
2663 g_mutex_lock (&tunnels_lock);
2664 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2667 /* remove the old client from the table. ref before because removing it will
2668 * remove the ref to it. */
2669 g_object_ref (oclient);
2670 g_hash_table_remove (tunnels, tunnelid);
2672 opriv = oclient->priv;
2674 if (opriv->watch == NULL)
2676 g_mutex_unlock (&tunnels_lock);
2678 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2679 opriv->connection, priv->connection);
2681 /* merge the tunnels into the first client */
2682 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2683 gst_rtsp_watch_reset (opriv->watch);
2684 g_object_unref (oclient);
2691 GST_ERROR ("client %p: no tunnelid provided", client);
2692 return GST_RTSP_ERROR;
2696 g_mutex_unlock (&tunnels_lock);
2697 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2698 return GST_RTSP_ERROR;
2702 g_mutex_unlock (&tunnels_lock);
2703 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2704 g_object_unref (oclient);
2705 return GST_RTSP_ERROR;
2709 static GstRTSPWatchFuncs watch_funcs = {
2721 client_watch_notify (GstRTSPClient * client)
2723 GstRTSPClientPrivate *priv = client->priv;
2725 GST_INFO ("client %p: watch destroyed", client);
2727 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2728 g_object_unref (client);
2732 * gst_rtsp_client_attach:
2733 * @client: a #GstRTSPClient
2734 * @context: (allow-none): a #GMainContext
2736 * Attaches @client to @context. When the mainloop for @context is run, the
2737 * client will be dispatched. When @context is NULL, the default context will be
2740 * This function should be called when the client properties and urls are fully
2741 * configured and the client is ready to start.
2743 * Returns: the ID (greater than 0) for the source within the GMainContext.
2746 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2748 GstRTSPClientPrivate *priv;
2751 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2752 priv = client->priv;
2753 g_return_val_if_fail (priv->connection != NULL, 0);
2754 g_return_val_if_fail (priv->watch == NULL, 0);
2756 /* create watch for the connection and attach */
2757 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2758 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2759 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2760 (GDestroyNotify) gst_rtsp_watch_unref);
2762 /* FIXME make this configurable. We don't want to do this yet because it will
2763 * be superceeded by a cache object later */
2764 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2766 GST_INFO ("attaching to context %p", context);
2767 res = gst_rtsp_watch_attach (priv->watch, context);
2773 * gst_rtsp_client_session_filter:
2774 * @client: a #GstRTSPClient
2775 * @func: (scope call): a callback
2776 * @user_data: user data passed to @func
2778 * Call @func for each session managed by @client. The result value of @func
2779 * determines what happens to the session. @func will be called with @client
2780 * locked so no further actions on @client can be performed from @func.
2782 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
2785 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
2787 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
2788 * will also be added with an additional ref to the result #GList of this
2791 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
2792 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2793 * element in the #GList should be unreffed before the list is freed.
2796 gst_rtsp_client_session_filter (GstRTSPClient * client,
2797 GstRTSPClientSessionFilterFunc func, gpointer user_data)
2799 GstRTSPClientPrivate *priv;
2800 GList *result, *walk, *next;
2802 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2803 g_return_val_if_fail (func != NULL, NULL);
2805 priv = client->priv;
2809 g_mutex_lock (&priv->lock);
2810 for (walk = priv->sessions; walk; walk = next) {
2811 GstRTSPSession *sess = walk->data;
2813 next = g_list_next (walk);
2815 switch (func (client, sess, user_data)) {
2816 case GST_RTSP_FILTER_REMOVE:
2817 /* stop watching the session and pretent it went away */
2818 client_cleanup_session (client, sess);
2820 case GST_RTSP_FILTER_REF:
2821 result = g_list_prepend (result, g_object_ref (sess));
2823 case GST_RTSP_FILTER_KEEP:
2828 g_mutex_unlock (&priv->lock);