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, *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 trailing '/' and append one */
1680 if (str[str_len - 1] != '/') {
1681 content_base = g_malloc (str_len + 2);
1682 memcpy (content_base, str, str_len);
1683 content_base[str_len] = '/';
1684 content_base[str_len + 1] = '\0';
1690 GST_INFO ("adding content-base: %s", content_base);
1692 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE,
1694 g_free (content_base);
1696 /* add SDP to the response body */
1697 str = gst_sdp_message_as_text (sdp);
1698 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
1699 gst_sdp_message_free (sdp);
1701 send_message (client, ctx->session, ctx->response, FALSE);
1703 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1711 GST_ERROR ("client %p: no uri", client);
1712 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1717 GST_ERROR ("client %p: no media", client);
1718 /* error reply is already sent */
1723 GST_ERROR ("client %p: can't create SDP", client);
1724 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1725 g_object_unref (media);
1731 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
1733 GstRTSPMethod options;
1736 options = GST_RTSP_DESCRIBE |
1741 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1743 str = gst_rtsp_options_as_text (options);
1745 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
1746 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
1748 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
1751 send_message (client, ctx->session, ctx->response, FALSE);
1753 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1759 /* remove duplicate and trailing '/' */
1761 sanitize_uri (GstRTSPUrl * uri)
1765 gboolean have_slash, prev_slash;
1767 s = d = uri->abspath;
1768 len = strlen (uri->abspath);
1772 for (i = 0; i < len; i++) {
1773 have_slash = s[i] == '/';
1775 if (!have_slash || !prev_slash)
1777 prev_slash = have_slash;
1779 len = d - uri->abspath;
1780 /* don't remove the first slash if that's the only thing left */
1781 if (len > 1 && *(d - 1) == '/')
1787 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1789 GstRTSPClientPrivate *priv = client->priv;
1791 GST_INFO ("client %p: session %p finished", client, session);
1793 /* unlink all media managed in this session */
1794 client_unlink_session (client, session);
1796 /* remove the session */
1797 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1798 GST_INFO ("client %p: all sessions finalized, close the connection",
1800 close_connection (client);
1805 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1807 GstRTSPClientPrivate *priv = client->priv;
1808 GstRTSPMethod method;
1809 const gchar *uristr;
1810 GstRTSPUrl *uri = NULL;
1811 GstRTSPVersion version;
1813 GstRTSPSession *session = NULL;
1814 GstRTSPContext sctx = { NULL }, *ctx;
1815 GstRTSPMessage response = { 0 };
1818 if (!(ctx = gst_rtsp_context_get_current ())) {
1820 ctx->auth = priv->auth;
1821 gst_rtsp_context_push_current (ctx);
1824 ctx->conn = priv->connection;
1825 ctx->client = client;
1826 ctx->request = request;
1827 ctx->response = &response;
1829 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1830 gst_rtsp_message_dump (request);
1833 GST_INFO ("client %p: received a request", client);
1835 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1837 /* we can only handle 1.0 requests */
1838 if (version != GST_RTSP_VERSION_1_0)
1841 ctx->method = method;
1843 /* we always try to parse the url first */
1844 if (strcmp (uristr, "*") == 0) {
1845 /* special case where we have * as uri, keep uri = NULL */
1846 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1849 /* get the session if there is any */
1850 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1851 if (res == GST_RTSP_OK) {
1852 if (priv->session_pool == NULL)
1855 /* we had a session in the request, find it again */
1856 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1857 goto session_not_found;
1859 /* we add the session to the client list of watched sessions. When a session
1860 * disappears because it times out, we will be notified. If all sessions are
1861 * gone, we will close the connection */
1862 client_watch_session (client, session);
1865 /* sanitize the uri */
1869 ctx->session = session;
1871 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
1872 goto not_authorized;
1874 /* now see what is asked and dispatch to a dedicated handler */
1876 case GST_RTSP_OPTIONS:
1877 handle_options_request (client, ctx);
1879 case GST_RTSP_DESCRIBE:
1880 handle_describe_request (client, ctx);
1882 case GST_RTSP_SETUP:
1883 handle_setup_request (client, ctx);
1886 handle_play_request (client, ctx);
1888 case GST_RTSP_PAUSE:
1889 handle_pause_request (client, ctx);
1891 case GST_RTSP_TEARDOWN:
1892 handle_teardown_request (client, ctx);
1894 case GST_RTSP_SET_PARAMETER:
1895 handle_set_param_request (client, ctx);
1897 case GST_RTSP_GET_PARAMETER:
1898 handle_get_param_request (client, ctx);
1900 case GST_RTSP_ANNOUNCE:
1901 case GST_RTSP_RECORD:
1902 case GST_RTSP_REDIRECT:
1903 goto not_implemented;
1904 case GST_RTSP_INVALID:
1911 gst_rtsp_context_pop_current (ctx);
1913 g_object_unref (session);
1915 gst_rtsp_url_free (uri);
1921 GST_ERROR ("client %p: version %d not supported", client, version);
1922 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1928 GST_ERROR ("client %p: bad request", client);
1929 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1934 GST_ERROR ("client %p: no pool configured", client);
1935 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1940 GST_ERROR ("client %p: session not found", client);
1941 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1946 GST_ERROR ("client %p: not allowed", client);
1951 GST_ERROR ("client %p: method %d not implemented", client, method);
1952 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
1959 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
1961 GstRTSPClientPrivate *priv = client->priv;
1963 GstRTSPSession *session = NULL;
1964 GstRTSPContext sctx = { NULL }, *ctx;
1967 if (!(ctx = gst_rtsp_context_get_current ())) {
1969 ctx->auth = priv->auth;
1970 gst_rtsp_context_push_current (ctx);
1973 ctx->conn = priv->connection;
1974 ctx->client = client;
1975 ctx->request = NULL;
1977 ctx->method = GST_RTSP_INVALID;
1978 ctx->response = response;
1980 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1981 gst_rtsp_message_dump (response);
1984 GST_INFO ("client %p: received a response", client);
1986 /* get the session if there is any */
1988 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
1989 if (res == GST_RTSP_OK) {
1990 if (priv->session_pool == NULL)
1993 /* we had a session in the request, find it again */
1994 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1995 goto session_not_found;
1997 /* we add the session to the client list of watched sessions. When a session
1998 * disappears because it times out, we will be notified. If all sessions are
1999 * gone, we will close the connection */
2000 client_watch_session (client, session);
2003 ctx->session = session;
2005 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
2010 gst_rtsp_context_pop_current (ctx);
2012 g_object_unref (session);
2017 GST_ERROR ("client %p: no pool configured", client);
2022 GST_ERROR ("client %p: session not found", client);
2028 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2030 GstRTSPClientPrivate *priv = client->priv;
2039 /* find the stream for this message */
2040 res = gst_rtsp_message_parse_data (message, &channel);
2041 if (res != GST_RTSP_OK)
2044 gst_rtsp_message_steal_body (message, &data, &size);
2046 buffer = gst_buffer_new_wrapped (data, size);
2049 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2050 GstRTSPStreamTransport *trans;
2051 GstRTSPStream *stream;
2052 const GstRTSPTransport *tr;
2056 tr = gst_rtsp_stream_transport_get_transport (trans);
2057 stream = gst_rtsp_stream_transport_get_stream (trans);
2059 /* check for TCP transport */
2060 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2061 /* dispatch to the stream based on the channel number */
2062 if (tr->interleaved.min == channel) {
2063 gst_rtsp_stream_recv_rtp (stream, buffer);
2066 } else if (tr->interleaved.max == channel) {
2067 gst_rtsp_stream_recv_rtcp (stream, buffer);
2074 gst_buffer_unref (buffer);
2078 * gst_rtsp_client_set_session_pool:
2079 * @client: a #GstRTSPClient
2080 * @pool: a #GstRTSPSessionPool
2082 * Set @pool as the sessionpool for @client which it will use to find
2083 * or allocate sessions. the sessionpool is usually inherited from the server
2084 * that created the client but can be overridden later.
2087 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2088 GstRTSPSessionPool * pool)
2090 GstRTSPSessionPool *old;
2091 GstRTSPClientPrivate *priv;
2093 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2095 priv = client->priv;
2098 g_object_ref (pool);
2100 g_mutex_lock (&priv->lock);
2101 old = priv->session_pool;
2102 priv->session_pool = pool;
2103 g_mutex_unlock (&priv->lock);
2106 g_object_unref (old);
2110 * gst_rtsp_client_get_session_pool:
2111 * @client: a #GstRTSPClient
2113 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2115 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2117 GstRTSPSessionPool *
2118 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2120 GstRTSPClientPrivate *priv;
2121 GstRTSPSessionPool *result;
2123 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2125 priv = client->priv;
2127 g_mutex_lock (&priv->lock);
2128 if ((result = priv->session_pool))
2129 g_object_ref (result);
2130 g_mutex_unlock (&priv->lock);
2136 * gst_rtsp_client_set_mount_points:
2137 * @client: a #GstRTSPClient
2138 * @mounts: a #GstRTSPMountPoints
2140 * Set @mounts as the mount points for @client which it will use to map urls
2141 * to media streams. These mount points are usually inherited from the server that
2142 * created the client but can be overriden later.
2145 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2146 GstRTSPMountPoints * mounts)
2148 GstRTSPClientPrivate *priv;
2149 GstRTSPMountPoints *old;
2151 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2153 priv = client->priv;
2156 g_object_ref (mounts);
2158 g_mutex_lock (&priv->lock);
2159 old = priv->mount_points;
2160 priv->mount_points = mounts;
2161 g_mutex_unlock (&priv->lock);
2164 g_object_unref (old);
2168 * gst_rtsp_client_get_mount_points:
2169 * @client: a #GstRTSPClient
2171 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2173 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2175 GstRTSPMountPoints *
2176 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2178 GstRTSPClientPrivate *priv;
2179 GstRTSPMountPoints *result;
2181 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2183 priv = client->priv;
2185 g_mutex_lock (&priv->lock);
2186 if ((result = priv->mount_points))
2187 g_object_ref (result);
2188 g_mutex_unlock (&priv->lock);
2194 * gst_rtsp_client_set_auth:
2195 * @client: a #GstRTSPClient
2196 * @auth: a #GstRTSPAuth
2198 * configure @auth to be used as the authentication manager of @client.
2201 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2203 GstRTSPClientPrivate *priv;
2206 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2208 priv = client->priv;
2211 g_object_ref (auth);
2213 g_mutex_lock (&priv->lock);
2216 g_mutex_unlock (&priv->lock);
2219 g_object_unref (old);
2224 * gst_rtsp_client_get_auth:
2225 * @client: a #GstRTSPClient
2227 * Get the #GstRTSPAuth used as the authentication manager of @client.
2229 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2233 gst_rtsp_client_get_auth (GstRTSPClient * client)
2235 GstRTSPClientPrivate *priv;
2236 GstRTSPAuth *result;
2238 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2240 priv = client->priv;
2242 g_mutex_lock (&priv->lock);
2243 if ((result = priv->auth))
2244 g_object_ref (result);
2245 g_mutex_unlock (&priv->lock);
2251 * gst_rtsp_client_set_thread_pool:
2252 * @client: a #GstRTSPClient
2253 * @pool: a #GstRTSPThreadPool
2255 * configure @pool to be used as the thread pool of @client.
2258 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2259 GstRTSPThreadPool * pool)
2261 GstRTSPClientPrivate *priv;
2262 GstRTSPThreadPool *old;
2264 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2266 priv = client->priv;
2269 g_object_ref (pool);
2271 g_mutex_lock (&priv->lock);
2272 old = priv->thread_pool;
2273 priv->thread_pool = pool;
2274 g_mutex_unlock (&priv->lock);
2277 g_object_unref (old);
2281 * gst_rtsp_client_get_thread_pool:
2282 * @client: a #GstRTSPClient
2284 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2286 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2290 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2292 GstRTSPClientPrivate *priv;
2293 GstRTSPThreadPool *result;
2295 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2297 priv = client->priv;
2299 g_mutex_lock (&priv->lock);
2300 if ((result = priv->thread_pool))
2301 g_object_ref (result);
2302 g_mutex_unlock (&priv->lock);
2308 * gst_rtsp_client_set_connection:
2309 * @client: a #GstRTSPClient
2310 * @conn: (transfer full): a #GstRTSPConnection
2312 * Set the #GstRTSPConnection of @client. This function takes ownership of
2315 * Returns: %TRUE on success.
2318 gst_rtsp_client_set_connection (GstRTSPClient * client,
2319 GstRTSPConnection * conn)
2321 GstRTSPClientPrivate *priv;
2322 GSocket *read_socket;
2323 GSocketAddress *address;
2325 GError *error = NULL;
2327 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2328 g_return_val_if_fail (conn != NULL, FALSE);
2330 priv = client->priv;
2332 read_socket = gst_rtsp_connection_get_read_socket (conn);
2334 if (!(address = g_socket_get_local_address (read_socket, &error)))
2337 g_free (priv->server_ip);
2338 /* keep the original ip that the client connected to */
2339 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2340 GInetAddress *iaddr;
2342 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2344 /* socket might be ipv6 but adress still ipv4 */
2345 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2346 priv->server_ip = g_inet_address_to_string (iaddr);
2347 g_object_unref (address);
2349 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2350 priv->server_ip = g_strdup ("unknown");
2353 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2354 priv->server_ip, priv->is_ipv6);
2356 url = gst_rtsp_connection_get_url (conn);
2357 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2359 priv->connection = conn;
2366 GST_ERROR ("could not get local address %s", error->message);
2367 g_error_free (error);
2373 * gst_rtsp_client_get_connection:
2374 * @client: a #GstRTSPClient
2376 * Get the #GstRTSPConnection of @client.
2378 * Returns: (transfer none): the #GstRTSPConnection of @client.
2379 * The connection object returned remains valid until the client is freed.
2382 gst_rtsp_client_get_connection (GstRTSPClient * client)
2384 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2386 return client->priv->connection;
2390 * gst_rtsp_client_set_send_func:
2391 * @client: a #GstRTSPClient
2392 * @func: a #GstRTSPClientSendFunc
2393 * @user_data: user data passed to @func
2394 * @notify: called when @user_data is no longer in use
2396 * Set @func as the callback that will be called when a new message needs to be
2397 * sent to the client. @user_data is passed to @func and @notify is called when
2398 * @user_data is no longer in use.
2400 * By default, the client will send the messages on the #GstRTSPConnection that
2401 * was configured with gst_rtsp_client_attach() was called.
2404 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2405 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2407 GstRTSPClientPrivate *priv;
2408 GDestroyNotify old_notify;
2411 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2413 priv = client->priv;
2415 g_mutex_lock (&priv->send_lock);
2416 priv->send_func = func;
2417 old_notify = priv->send_notify;
2418 old_data = priv->send_data;
2419 priv->send_notify = notify;
2420 priv->send_data = user_data;
2421 g_mutex_unlock (&priv->send_lock);
2424 old_notify (old_data);
2428 * gst_rtsp_client_handle_message:
2429 * @client: a #GstRTSPClient
2430 * @message: an #GstRTSPMessage
2432 * Let the client handle @message.
2434 * Returns: a #GstRTSPResult.
2437 gst_rtsp_client_handle_message (GstRTSPClient * client,
2438 GstRTSPMessage * message)
2440 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2441 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2443 switch (message->type) {
2444 case GST_RTSP_MESSAGE_REQUEST:
2445 handle_request (client, message);
2447 case GST_RTSP_MESSAGE_RESPONSE:
2448 handle_response (client, message);
2450 case GST_RTSP_MESSAGE_DATA:
2451 handle_data (client, message);
2460 * gst_rtsp_client_send_message:
2461 * @client: a #GstRTSPClient
2462 * @session: a #GstRTSPSession to send the message to or %NULL
2463 * @message: The #GstRTSPMessage to send
2465 * Send a message message to the remote end. @message must be a
2466 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
2469 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
2470 GstRTSPMessage * message)
2472 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2473 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2474 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
2475 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
2477 send_message (client, session, message, FALSE);
2482 static GstRTSPResult
2483 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2484 gboolean close, gpointer user_data)
2486 GstRTSPClientPrivate *priv = client->priv;
2488 /* send the response and store the seq number so we can wait until it's
2489 * written to the client to close the connection */
2490 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2491 &priv->close_seq : NULL);
2494 static GstRTSPResult
2495 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2498 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2501 static GstRTSPResult
2502 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2504 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2505 GstRTSPClientPrivate *priv = client->priv;
2507 if (priv->close_seq && priv->close_seq == cseq) {
2508 priv->close_seq = 0;
2509 close_connection (client);
2515 static GstRTSPResult
2516 closed (GstRTSPWatch * watch, gpointer user_data)
2518 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2519 GstRTSPClientPrivate *priv = client->priv;
2520 const gchar *tunnelid;
2522 GST_INFO ("client %p: connection closed", client);
2524 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2525 g_mutex_lock (&tunnels_lock);
2526 /* remove from tunnelids */
2527 g_hash_table_remove (tunnels, tunnelid);
2528 g_mutex_unlock (&tunnels_lock);
2531 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2536 static GstRTSPResult
2537 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2539 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2542 str = gst_rtsp_strresult (result);
2543 GST_INFO ("client %p: received an error %s", client, str);
2549 static GstRTSPResult
2550 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2551 GstRTSPMessage * message, guint id, gpointer user_data)
2553 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2556 str = gst_rtsp_strresult (result);
2558 ("client %p: error when handling message %p with id %d: %s",
2559 client, message, id, str);
2566 remember_tunnel (GstRTSPClient * client)
2568 GstRTSPClientPrivate *priv = client->priv;
2569 const gchar *tunnelid;
2571 /* store client in the pending tunnels */
2572 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2573 if (tunnelid == NULL)
2576 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2578 /* we can't have two clients connecting with the same tunnelid */
2579 g_mutex_lock (&tunnels_lock);
2580 if (g_hash_table_lookup (tunnels, tunnelid))
2581 goto tunnel_existed;
2583 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2584 g_mutex_unlock (&tunnels_lock);
2591 GST_ERROR ("client %p: no tunnelid provided", client);
2596 g_mutex_unlock (&tunnels_lock);
2597 GST_ERROR ("client %p: tunnel session %s already existed", client,
2603 static GstRTSPStatusCode
2604 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2606 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2607 GstRTSPClientPrivate *priv = client->priv;
2609 GST_INFO ("client %p: tunnel start (connection %p)", client,
2612 if (!remember_tunnel (client))
2615 return GST_RTSP_STS_OK;
2620 GST_ERROR ("client %p: error starting tunnel", client);
2621 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2625 static GstRTSPResult
2626 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2628 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2629 GstRTSPClientPrivate *priv = client->priv;
2631 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2634 /* ignore error, it'll only be a problem when the client does a POST again */
2635 remember_tunnel (client);
2640 static GstRTSPResult
2641 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2643 const gchar *tunnelid;
2644 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2645 GstRTSPClientPrivate *priv = client->priv;
2646 GstRTSPClient *oclient;
2647 GstRTSPClientPrivate *opriv;
2649 GST_INFO ("client %p: tunnel complete", client);
2651 /* find previous tunnel */
2652 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2653 if (tunnelid == NULL)
2656 g_mutex_lock (&tunnels_lock);
2657 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2660 /* remove the old client from the table. ref before because removing it will
2661 * remove the ref to it. */
2662 g_object_ref (oclient);
2663 g_hash_table_remove (tunnels, tunnelid);
2665 opriv = oclient->priv;
2667 if (opriv->watch == NULL)
2669 g_mutex_unlock (&tunnels_lock);
2671 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2672 opriv->connection, priv->connection);
2674 /* merge the tunnels into the first client */
2675 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2676 gst_rtsp_watch_reset (opriv->watch);
2677 g_object_unref (oclient);
2684 GST_ERROR ("client %p: no tunnelid provided", client);
2685 return GST_RTSP_ERROR;
2689 g_mutex_unlock (&tunnels_lock);
2690 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2691 return GST_RTSP_ERROR;
2695 g_mutex_unlock (&tunnels_lock);
2696 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2697 g_object_unref (oclient);
2698 return GST_RTSP_ERROR;
2702 static GstRTSPWatchFuncs watch_funcs = {
2714 client_watch_notify (GstRTSPClient * client)
2716 GstRTSPClientPrivate *priv = client->priv;
2718 GST_INFO ("client %p: watch destroyed", client);
2720 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2721 g_object_unref (client);
2725 * gst_rtsp_client_attach:
2726 * @client: a #GstRTSPClient
2727 * @context: (allow-none): a #GMainContext
2729 * Attaches @client to @context. When the mainloop for @context is run, the
2730 * client will be dispatched. When @context is NULL, the default context will be
2733 * This function should be called when the client properties and urls are fully
2734 * configured and the client is ready to start.
2736 * Returns: the ID (greater than 0) for the source within the GMainContext.
2739 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2741 GstRTSPClientPrivate *priv;
2744 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2745 priv = client->priv;
2746 g_return_val_if_fail (priv->connection != NULL, 0);
2747 g_return_val_if_fail (priv->watch == NULL, 0);
2749 /* create watch for the connection and attach */
2750 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2751 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2752 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2753 (GDestroyNotify) gst_rtsp_watch_unref);
2755 /* FIXME make this configurable. We don't want to do this yet because it will
2756 * be superceeded by a cache object later */
2757 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2759 GST_INFO ("attaching to context %p", context);
2760 res = gst_rtsp_watch_attach (priv->watch, context);
2766 * gst_rtsp_client_session_filter:
2767 * @client: a #GstRTSPClient
2768 * @func: (scope call): a callback
2769 * @user_data: user data passed to @func
2771 * Call @func for each session managed by @client. The result value of @func
2772 * determines what happens to the session. @func will be called with @client
2773 * locked so no further actions on @client can be performed from @func.
2775 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
2778 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
2780 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
2781 * will also be added with an additional ref to the result #GList of this
2784 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
2785 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2786 * element in the #GList should be unreffed before the list is freed.
2789 gst_rtsp_client_session_filter (GstRTSPClient * client,
2790 GstRTSPClientSessionFilterFunc func, gpointer user_data)
2792 GstRTSPClientPrivate *priv;
2793 GList *result, *walk, *next;
2795 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2796 g_return_val_if_fail (func != NULL, NULL);
2798 priv = client->priv;
2802 g_mutex_lock (&priv->lock);
2803 for (walk = priv->sessions; walk; walk = next) {
2804 GstRTSPSession *sess = walk->data;
2806 next = g_list_next (walk);
2808 switch (func (client, sess, user_data)) {
2809 case GST_RTSP_FILTER_REMOVE:
2810 /* stop watching the session and pretent it went away */
2811 client_cleanup_session (client, sess);
2813 case GST_RTSP_FILTER_REF:
2814 result = g_list_prepend (result, g_object_ref (sess));
2816 case GST_RTSP_FILTER_KEEP:
2821 g_mutex_unlock (&priv->lock);