2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * @short_description: A client connection state
22 * @see_also: #GstRTSPServer, #GstRTSPThreadPool
24 * The client object handles the connection with a client for as long as a TCP
27 * A #GstRTSPClient is created by #GstRTSPServer when a new connection is
28 * accepted and it inherits the #GstRTSPMountPoints, #GstRTSPSessionPool,
29 * #GstRTSPAuth and #GstRTSPThreadPool from the server.
31 * The client connection should be configured with the #GstRTSPConnection using
32 * gst_rtsp_client_set_connection() before it can be attached to a #GMainContext
33 * using gst_rtsp_client_attach(). From then on the client will handle requests
36 * Use gst_rtsp_client_session_filter() to iterate or modify all the
37 * #GstRTSPSession objects managed by the client object.
39 * Last reviewed on 2013-07-11 (1.0.0)
45 #include "rtsp-client.h"
47 #include "rtsp-params.h"
49 #define GST_RTSP_CLIENT_GET_PRIVATE(obj) \
50 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_CLIENT, GstRTSPClientPrivate))
53 * send_lock, lock, tunnels_lock
56 struct _GstRTSPClientPrivate
58 GMutex lock; /* protects everything else */
60 GstRTSPConnection *connection;
66 GstRTSPClientSendFunc send_func; /* protected by send_lock */
67 gpointer send_data; /* protected by send_lock */
68 GDestroyNotify send_notify; /* protected by send_lock */
70 GstRTSPSessionPool *session_pool;
71 GstRTSPMountPoints *mount_points;
73 GstRTSPThreadPool *thread_pool;
75 /* used to cache the media in the last requested DESCRIBE so that
76 * we can pick it up in the next SETUP immediately */
84 static GMutex tunnels_lock;
85 static GHashTable *tunnels; /* protected by tunnels_lock */
87 #define DEFAULT_SESSION_POOL NULL
88 #define DEFAULT_MOUNT_POINTS NULL
102 SIGNAL_OPTIONS_REQUEST,
103 SIGNAL_DESCRIBE_REQUEST,
104 SIGNAL_SETUP_REQUEST,
106 SIGNAL_PAUSE_REQUEST,
107 SIGNAL_TEARDOWN_REQUEST,
108 SIGNAL_SET_PARAMETER_REQUEST,
109 SIGNAL_GET_PARAMETER_REQUEST,
110 SIGNAL_HANDLE_RESPONSE,
114 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
115 #define GST_CAT_DEFAULT rtsp_client_debug
117 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
119 static void gst_rtsp_client_get_property (GObject * object, guint propid,
120 GValue * value, GParamSpec * pspec);
121 static void gst_rtsp_client_set_property (GObject * object, guint propid,
122 const GValue * value, GParamSpec * pspec);
123 static void gst_rtsp_client_finalize (GObject * obj);
125 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
126 static void client_session_finalized (GstRTSPClient * client,
127 GstRTSPSession * session);
128 static void unlink_session_transports (GstRTSPClient * client,
129 GstRTSPSession * session, GstRTSPSessionMedia * sessmedia);
130 static gboolean default_configure_client_transport (GstRTSPClient * client,
131 GstRTSPContext * ctx, GstRTSPTransport * ct);
132 static GstRTSPResult default_params_set (GstRTSPClient * client,
133 GstRTSPContext * ctx);
134 static GstRTSPResult default_params_get (GstRTSPClient * client,
135 GstRTSPContext * ctx);
137 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
140 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
142 GObjectClass *gobject_class;
144 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
146 gobject_class = G_OBJECT_CLASS (klass);
148 gobject_class->get_property = gst_rtsp_client_get_property;
149 gobject_class->set_property = gst_rtsp_client_set_property;
150 gobject_class->finalize = gst_rtsp_client_finalize;
152 klass->create_sdp = create_sdp;
153 klass->configure_client_transport = default_configure_client_transport;
154 klass->params_set = default_params_set;
155 klass->params_get = default_params_get;
157 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
158 g_param_spec_object ("session-pool", "Session Pool",
159 "The session pool to use for client session",
160 GST_TYPE_RTSP_SESSION_POOL,
161 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
163 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
164 g_param_spec_object ("mount-points", "Mount Points",
165 "The mount points to use for client session",
166 GST_TYPE_RTSP_MOUNT_POINTS,
167 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
169 gst_rtsp_client_signals[SIGNAL_CLOSED] =
170 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
171 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
172 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
174 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
175 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
176 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
177 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
179 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
180 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
181 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
182 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
185 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
186 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
187 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
188 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
191 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
192 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
193 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
194 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
197 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
198 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
199 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
200 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
203 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
204 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
205 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
206 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
209 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
210 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
211 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
212 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
215 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
216 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
217 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
218 set_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
219 G_TYPE_NONE, 1, G_TYPE_POINTER);
221 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
222 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
223 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
224 get_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
225 G_TYPE_NONE, 1, G_TYPE_POINTER);
227 gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE] =
228 g_signal_new ("handle-response", G_TYPE_FROM_CLASS (klass),
229 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
230 handle_response), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
231 G_TYPE_NONE, 1, G_TYPE_POINTER);
234 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
235 g_mutex_init (&tunnels_lock);
237 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
241 gst_rtsp_client_init (GstRTSPClient * client)
243 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
247 g_mutex_init (&priv->lock);
248 g_mutex_init (&priv->send_lock);
252 static GstRTSPFilterResult
253 filter_session (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
256 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
258 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
259 unlink_session_transports (client, sess, sessmedia);
261 /* unmanage the media in the session */
262 return GST_RTSP_FILTER_REMOVE;
266 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
268 /* unlink all media managed in this session */
269 gst_rtsp_session_filter (session, filter_session, client);
273 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
275 GstRTSPClientPrivate *priv = client->priv;
278 for (walk = priv->sessions; walk; walk = g_list_next (walk)) {
279 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
281 /* we already know about this session */
282 if (msession == session)
286 GST_INFO ("watching session %p", session);
288 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
290 priv->sessions = g_list_prepend (priv->sessions, session);
294 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session)
296 GstRTSPClientPrivate *priv = client->priv;
298 GST_INFO ("unwatching session %p", session);
300 g_object_weak_unref (G_OBJECT (session),
301 (GWeakNotify) client_session_finalized, client);
302 priv->sessions = g_list_remove (priv->sessions, session);
306 client_cleanup_session (GstRTSPClient * client, GstRTSPSession * session)
308 g_object_weak_unref (G_OBJECT (session),
309 (GWeakNotify) client_session_finalized, client);
310 client_unlink_session (client, session);
314 client_cleanup_sessions (GstRTSPClient * client)
316 GstRTSPClientPrivate *priv = client->priv;
319 /* remove weak-ref from sessions */
320 for (sessions = priv->sessions; sessions; sessions = g_list_next (sessions)) {
321 client_cleanup_session (client, (GstRTSPSession *) sessions->data);
323 g_list_free (priv->sessions);
324 priv->sessions = NULL;
327 /* A client is finalized when the connection is broken */
329 gst_rtsp_client_finalize (GObject * obj)
331 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
332 GstRTSPClientPrivate *priv = client->priv;
334 GST_INFO ("finalize client %p", client);
336 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
339 g_source_destroy ((GSource *) priv->watch);
341 client_cleanup_sessions (client);
343 if (priv->connection)
344 gst_rtsp_connection_free (priv->connection);
345 if (priv->session_pool)
346 g_object_unref (priv->session_pool);
347 if (priv->mount_points)
348 g_object_unref (priv->mount_points);
350 g_object_unref (priv->auth);
351 if (priv->thread_pool)
352 g_object_unref (priv->thread_pool);
357 gst_rtsp_media_unprepare (priv->media);
358 g_object_unref (priv->media);
361 g_free (priv->server_ip);
362 g_mutex_clear (&priv->lock);
363 g_mutex_clear (&priv->send_lock);
365 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
369 gst_rtsp_client_get_property (GObject * object, guint propid,
370 GValue * value, GParamSpec * pspec)
372 GstRTSPClient *client = GST_RTSP_CLIENT (object);
375 case PROP_SESSION_POOL:
376 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
378 case PROP_MOUNT_POINTS:
379 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
382 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
387 gst_rtsp_client_set_property (GObject * object, guint propid,
388 const GValue * value, GParamSpec * pspec)
390 GstRTSPClient *client = GST_RTSP_CLIENT (object);
393 case PROP_SESSION_POOL:
394 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
396 case PROP_MOUNT_POINTS:
397 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
400 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
405 * gst_rtsp_client_new:
407 * Create a new #GstRTSPClient instance.
409 * Returns: a new #GstRTSPClient
412 gst_rtsp_client_new (void)
414 GstRTSPClient *result;
416 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
422 send_message (GstRTSPClient * client, GstRTSPSession * session,
423 GstRTSPMessage * message, gboolean close)
425 GstRTSPClientPrivate *priv = client->priv;
427 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
428 "GStreamer RTSP server");
430 /* remove any previous header */
431 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
433 /* add the new session header for new session ids */
435 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
436 gst_rtsp_session_get_header (session));
439 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
440 gst_rtsp_message_dump (message);
444 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
446 g_mutex_lock (&priv->send_lock);
448 priv->send_func (client, message, close, priv->send_data);
449 g_mutex_unlock (&priv->send_lock);
451 gst_rtsp_message_unset (message);
455 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
456 GstRTSPContext * ctx)
458 gst_rtsp_message_init_response (ctx->response, code,
459 gst_rtsp_status_as_text (code), ctx->request);
461 send_message (client, NULL, ctx->response, FALSE);
465 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
467 if (path1 == NULL || path2 == NULL)
470 if (strlen (path1) != len2)
473 if (strncmp (path1, path2, len2))
479 /* this function is called to initially find the media for the DESCRIBE request
480 * but is cached for when the same client (without breaking the connection) is
481 * doing a setup for the exact same url. */
482 static GstRTSPMedia *
483 find_media (GstRTSPClient * client, GstRTSPContext * ctx, gchar * path,
486 GstRTSPClientPrivate *priv = client->priv;
487 GstRTSPMediaFactory *factory;
491 /* find the longest matching factory for the uri first */
492 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
496 ctx->factory = factory;
498 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
499 goto no_factory_access;
501 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
507 path_len = strlen (path);
509 if (!paths_are_equal (priv->path, path, path_len)) {
510 GstRTSPThread *thread;
512 /* remove any previously cached values before we try to construct a new
518 gst_rtsp_media_unprepare (priv->media);
519 g_object_unref (priv->media);
523 /* prepare the media and add it to the pipeline */
524 if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
529 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
530 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
534 /* prepare the media */
535 if (!(gst_rtsp_media_prepare (media, thread)))
538 /* now keep track of the uri and the media */
539 priv->path = g_strndup (path, path_len);
542 /* we have seen this path before, used cached media */
545 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
548 g_object_unref (factory);
552 g_object_ref (media);
559 GST_ERROR ("client %p: no factory for path %s", client, path);
560 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
565 GST_ERROR ("client %p: not authorized to see factory path %s", client,
567 /* error reply is already sent */
572 GST_ERROR ("client %p: not authorized for factory path %s", client, path);
573 /* error reply is already sent */
578 GST_ERROR ("client %p: can't create media", client);
579 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
580 g_object_unref (factory);
586 GST_ERROR ("client %p: can't create thread", client);
587 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
588 g_object_unref (media);
590 g_object_unref (factory);
596 GST_ERROR ("client %p: can't prepare media", client);
597 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
598 g_object_unref (media);
600 g_object_unref (factory);
607 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
609 GstRTSPClientPrivate *priv = client->priv;
610 GstRTSPMessage message = { 0 };
615 gst_rtsp_message_init_data (&message, channel);
617 /* FIXME, need some sort of iovec RTSPMessage here */
618 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
621 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
623 g_mutex_lock (&priv->send_lock);
625 priv->send_func (client, &message, FALSE, priv->send_data);
626 g_mutex_unlock (&priv->send_lock);
628 gst_rtsp_message_steal_body (&message, &data, &usize);
629 gst_buffer_unmap (buffer, &map_info);
631 gst_rtsp_message_unset (&message);
637 link_transport (GstRTSPClient * client, GstRTSPSession * session,
638 GstRTSPStreamTransport * trans)
640 GstRTSPClientPrivate *priv = client->priv;
642 GST_DEBUG ("client %p: linking transport %p", client, trans);
644 gst_rtsp_stream_transport_set_callbacks (trans,
645 (GstRTSPSendFunc) do_send_data,
646 (GstRTSPSendFunc) do_send_data, client, NULL);
648 priv->transports = g_list_prepend (priv->transports, trans);
650 /* make sure our session can't expire */
651 gst_rtsp_session_prevent_expire (session);
655 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
656 GstRTSPStreamTransport * trans)
658 GstRTSPClientPrivate *priv = client->priv;
660 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
662 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
664 priv->transports = g_list_remove (priv->transports, trans);
666 /* our session can now expire */
667 gst_rtsp_session_allow_expire (session);
671 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
672 GstRTSPSessionMedia * sessmedia)
677 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
678 for (i = 0; i < n_streams; i++) {
679 GstRTSPStreamTransport *trans;
680 const GstRTSPTransport *tr;
682 /* get the transport, if there is no transport configured, skip this stream */
683 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
687 tr = gst_rtsp_stream_transport_get_transport (trans);
689 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
690 /* for TCP, unlink the stream from the TCP connection of the client */
691 unlink_transport (client, session, trans);
697 close_connection (GstRTSPClient * client)
699 GstRTSPClientPrivate *priv = client->priv;
700 const gchar *tunnelid;
702 GST_DEBUG ("client %p: closing connection", client);
704 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
705 g_mutex_lock (&tunnels_lock);
706 /* remove from tunnelids */
707 g_hash_table_remove (tunnels, tunnelid);
708 g_mutex_unlock (&tunnels_lock);
711 gst_rtsp_connection_close (priv->connection);
715 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
717 GstRTSPClientPrivate *priv = client->priv;
718 GstRTSPSession *session;
719 GstRTSPSessionMedia *sessmedia;
720 GstRTSPStatusCode code;
727 session = ctx->session;
732 path = ctx->uri->abspath;
734 /* get a handle to the configuration of the media in the session */
735 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
739 /* only aggregate control for now.. */
740 if (path[matched] != '\0')
743 ctx->sessmedia = sessmedia;
745 /* we emit the signal before closing the connection */
746 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
749 /* unlink the all TCP callbacks */
750 unlink_session_transports (client, session, sessmedia);
752 /* remove the session from the watched sessions */
753 client_unwatch_session (client, session);
755 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
757 /* unmanage the media in the session, returns false if all media session
759 if (!gst_rtsp_session_release_media (session, sessmedia)) {
760 /* remove the session */
761 gst_rtsp_session_pool_remove (priv->session_pool, session);
763 /* construct the response now */
764 code = GST_RTSP_STS_OK;
765 gst_rtsp_message_init_response (ctx->response, code,
766 gst_rtsp_status_as_text (code), ctx->request);
768 send_message (client, session, ctx->response, TRUE);
775 GST_ERROR ("client %p: no session", client);
776 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
781 GST_ERROR ("client %p: no uri supplied", client);
782 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
787 GST_ERROR ("client %p: no media for uri", client);
788 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
793 GST_ERROR ("client %p: no aggregate path %s", client, path);
794 send_generic_response (client,
795 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
801 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
805 res = gst_rtsp_params_set (client, ctx);
811 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
815 res = gst_rtsp_params_get (client, ctx);
821 handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
827 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
828 if (res != GST_RTSP_OK)
832 /* no body, keep-alive request */
833 send_generic_response (client, GST_RTSP_STS_OK, ctx);
835 /* there is a body, handle the params */
836 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
837 if (res != GST_RTSP_OK)
840 send_message (client, ctx->session, ctx->response, FALSE);
843 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
851 GST_ERROR ("client %p: bad request", client);
852 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
858 handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
864 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
865 if (res != GST_RTSP_OK)
869 /* no body, keep-alive request */
870 send_generic_response (client, GST_RTSP_STS_OK, ctx);
872 /* there is a body, handle the params */
873 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
874 if (res != GST_RTSP_OK)
877 send_message (client, ctx->session, ctx->response, FALSE);
880 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
888 GST_ERROR ("client %p: bad request", client);
889 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
895 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
897 GstRTSPSession *session;
898 GstRTSPSessionMedia *sessmedia;
899 GstRTSPStatusCode code;
900 GstRTSPState rtspstate;
904 if (!(session = ctx->session))
910 path = ctx->uri->abspath;
912 /* get a handle to the configuration of the media in the session */
913 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
917 if (path[matched] != '\0')
920 ctx->sessmedia = sessmedia;
922 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
923 /* the session state must be playing or recording */
924 if (rtspstate != GST_RTSP_STATE_PLAYING &&
925 rtspstate != GST_RTSP_STATE_RECORDING)
928 /* unlink the all TCP callbacks */
929 unlink_session_transports (client, session, sessmedia);
931 /* then pause sending */
932 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
934 /* construct the response now */
935 code = GST_RTSP_STS_OK;
936 gst_rtsp_message_init_response (ctx->response, code,
937 gst_rtsp_status_as_text (code), ctx->request);
939 send_message (client, session, ctx->response, FALSE);
941 /* the state is now READY */
942 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
944 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
951 GST_ERROR ("client %p: no seesion", client);
952 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
957 GST_ERROR ("client %p: no uri supplied", client);
958 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
963 GST_ERROR ("client %p: no media for uri", client);
964 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
969 GST_ERROR ("client %p: no aggregate path %s", client, path);
970 send_generic_response (client,
971 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
976 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
977 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
983 /* convert @url and @path to a URL used as a content base for the factory
984 * located at @path */
986 make_base_url (GstRTSPClient * client, GstRTSPUrl * url, gchar * path)
989 gchar *result, *trail;
991 /* check for trailing '/' and append one */
992 trail = (path[strlen (path) - 1] != '/' ? "/" : "");
997 tmp.abspath = g_strdup_printf ("%s%s", path, trail);
999 result = gst_rtsp_url_get_request_uri (&tmp);
1000 g_free (tmp.abspath);
1006 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
1008 GstRTSPSession *session;
1009 GstRTSPSessionMedia *sessmedia;
1010 GstRTSPMedia *media;
1011 GstRTSPStatusCode code;
1014 guint n_streams, i, infocount;
1015 gchar *str, *base_url;
1016 GstRTSPTimeRange *range;
1018 GstRTSPState rtspstate;
1019 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1023 if (!(session = ctx->session))
1026 if (!(uri = ctx->uri))
1029 path = uri->abspath;
1031 /* get a handle to the configuration of the media in the session */
1032 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1036 if (path[matched] != '\0')
1039 ctx->sessmedia = sessmedia;
1040 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1042 /* the session state must be playing or ready */
1043 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1044 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1047 /* parse the range header if we have one */
1048 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1049 if (res == GST_RTSP_OK) {
1050 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1051 /* we have a range, seek to the position */
1053 gst_rtsp_media_seek (media, range);
1054 gst_rtsp_range_free (range);
1058 /* grab RTPInfo from the payloaders now */
1059 rtpinfo = g_string_new ("");
1061 base_url = make_base_url (client, uri, path);
1063 n_streams = gst_rtsp_media_n_streams (media);
1064 for (i = 0, infocount = 0; i < n_streams; i++) {
1065 GstRTSPStreamTransport *trans;
1066 GstRTSPStream *stream;
1067 const GstRTSPTransport *tr;
1070 /* get the transport, if there is no transport configured, skip this stream */
1071 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
1072 if (trans == NULL) {
1073 GST_INFO ("stream %d is not configured", i);
1076 tr = gst_rtsp_stream_transport_get_transport (trans);
1078 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1079 /* for TCP, link the stream to the TCP connection of the client */
1080 link_transport (client, session, trans);
1083 stream = gst_rtsp_stream_transport_get_stream (trans);
1084 if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
1088 g_string_append (rtpinfo, ", ");
1090 control = gst_rtsp_stream_get_control (stream);
1091 g_string_append_printf (rtpinfo, "url=%s%s;seq=%u;rtptime=%u",
1092 base_url, control, seq, rtptime);
1097 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
1102 /* construct the response now */
1103 code = GST_RTSP_STS_OK;
1104 gst_rtsp_message_init_response (ctx->response, code,
1105 gst_rtsp_status_as_text (code), ctx->request);
1107 /* add the RTP-Info header */
1108 if (infocount > 0) {
1109 str = g_string_free (rtpinfo, FALSE);
1110 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO, str);
1112 g_string_free (rtpinfo, TRUE);
1116 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1118 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1120 send_message (client, session, ctx->response, FALSE);
1122 /* start playing after sending the request */
1123 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1125 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1127 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1134 GST_ERROR ("client %p: no session", client);
1135 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1140 GST_ERROR ("client %p: no uri supplied", client);
1141 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1146 GST_ERROR ("client %p: media not found", client);
1147 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1152 GST_ERROR ("client %p: no aggregate path %s", client, path);
1153 send_generic_response (client,
1154 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1159 GST_ERROR ("client %p: not PLAYING or READY", client);
1160 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1167 do_keepalive (GstRTSPSession * session)
1169 GST_INFO ("keep session %p alive", session);
1170 gst_rtsp_session_touch (session);
1173 /* parse @transport and return a valid transport in @tr. only transports
1174 * from @supported are returned. Returns FALSE if no valid transport
1177 parse_transport (const char *transport, GstRTSPLowerTrans supported,
1178 GstRTSPTransport * tr)
1185 gst_rtsp_transport_init (tr);
1187 GST_DEBUG ("parsing transports %s", transport);
1189 transports = g_strsplit (transport, ",", 0);
1191 /* loop through the transports, try to parse */
1192 for (i = 0; transports[i]; i++) {
1193 res = gst_rtsp_transport_parse (transports[i], tr);
1194 if (res != GST_RTSP_OK) {
1195 /* no valid transport, search some more */
1196 GST_WARNING ("could not parse transport %s", transports[i]);
1200 /* we have a transport, see if it's RTP/AVP */
1201 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1202 GST_WARNING ("invalid transport %s", transports[i]);
1206 if (!(tr->lower_transport & supported)) {
1207 GST_WARNING ("unsupported transport %s", transports[i]);
1211 /* we have a valid transport */
1212 GST_INFO ("found valid transport %s", transports[i]);
1217 gst_rtsp_transport_init (tr);
1219 g_strfreev (transports);
1225 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
1226 GstRTSPMessage * request)
1228 gchar *blocksize_str;
1229 gboolean ret = TRUE;
1231 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1232 &blocksize_str, 0) == GST_RTSP_OK) {
1236 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1237 if (end == blocksize_str) {
1238 GST_ERROR ("failed to parse blocksize");
1241 /* we don't want to change the mtu when this media
1242 * can be shared because it impacts other clients */
1243 if (gst_rtsp_media_is_shared (media))
1246 if (blocksize > G_MAXUINT)
1247 blocksize = G_MAXUINT;
1248 gst_rtsp_stream_set_mtu (stream, blocksize);
1255 default_configure_client_transport (GstRTSPClient * client,
1256 GstRTSPContext * ctx, GstRTSPTransport * ct)
1258 GstRTSPClientPrivate *priv = client->priv;
1260 /* we have a valid transport now, set the destination of the client. */
1261 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1262 gboolean use_client_settings;
1264 use_client_settings =
1265 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1267 if (ct->destination && use_client_settings) {
1268 GstRTSPAddress *addr;
1270 addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1271 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1276 gst_rtsp_address_free (addr);
1278 GstRTSPAddress *addr;
1279 GSocketFamily family;
1281 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1283 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1287 g_free (ct->destination);
1288 ct->destination = g_strdup (addr->address);
1289 ct->port.min = addr->port;
1290 ct->port.max = addr->port + addr->n_ports - 1;
1291 ct->ttl = addr->ttl;
1293 gst_rtsp_address_free (addr);
1298 url = gst_rtsp_connection_get_url (priv->connection);
1299 g_free (ct->destination);
1300 ct->destination = g_strdup (url->host);
1302 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1303 /* check if the client selected channels for TCP */
1304 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1305 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1315 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1320 static GstRTSPTransport *
1321 make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
1322 GstRTSPTransport * ct)
1324 GstRTSPTransport *st;
1326 GSocketFamily family;
1328 /* prepare the server transport */
1329 gst_rtsp_transport_new (&st);
1331 st->trans = ct->trans;
1332 st->profile = ct->profile;
1333 st->lower_transport = ct->lower_transport;
1335 addr = g_inet_address_new_from_string (ct->destination);
1338 GST_ERROR ("failed to get inet addr from client destination");
1339 family = G_SOCKET_FAMILY_IPV4;
1341 family = g_inet_address_get_family (addr);
1342 g_object_unref (addr);
1346 switch (st->lower_transport) {
1347 case GST_RTSP_LOWER_TRANS_UDP:
1348 st->client_port = ct->client_port;
1349 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
1351 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1352 st->port = ct->port;
1353 st->destination = g_strdup (ct->destination);
1356 case GST_RTSP_LOWER_TRANS_TCP:
1357 st->interleaved = ct->interleaved;
1362 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
1368 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
1370 GstRTSPClientPrivate *priv = client->priv;
1374 GstRTSPTransport *ct, *st;
1375 GstRTSPLowerTrans supported;
1376 GstRTSPStatusCode code;
1377 GstRTSPSession *session;
1378 GstRTSPStreamTransport *trans;
1380 GstRTSPSessionMedia *sessmedia;
1381 GstRTSPMedia *media;
1382 GstRTSPStream *stream;
1383 GstRTSPState rtspstate;
1384 GstRTSPClientClass *klass;
1385 gchar *path, *control;
1392 path = uri->abspath;
1394 /* parse the transport */
1396 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
1398 if (res != GST_RTSP_OK)
1401 /* we create the session after parsing stuff so that we don't make
1402 * a session for malformed requests */
1403 if (priv->session_pool == NULL)
1406 session = ctx->session;
1409 g_object_ref (session);
1410 /* get a handle to the configuration of the media in the session, this can
1411 * return NULL if this is a new url to manage in this session. */
1412 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1414 /* we need a new media configuration in this session */
1418 /* we have no session media, find one and manage it */
1419 if (sessmedia == NULL) {
1420 /* get a handle to the configuration of the media in the session */
1421 media = find_media (client, ctx, path, &matched);
1423 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1424 g_object_ref (media);
1426 goto media_not_found;
1428 /* no media, not found then */
1430 goto media_not_found_no_reply;
1432 if (path[matched] == '\0')
1433 goto control_not_found;
1435 /* path is what matched. We can modify the parsed uri in place */
1436 path[matched] = '\0';
1437 /* control is remainder */
1438 control = &path[matched + 1];
1440 /* find the stream now using the control part */
1441 stream = gst_rtsp_media_find_stream (media, control);
1443 goto stream_not_found;
1445 /* now we have a uri identifying a valid media and stream */
1446 ctx->stream = stream;
1449 if (session == NULL) {
1450 /* create a session if this fails we probably reached our session limit or
1452 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1453 goto service_unavailable;
1455 /* make sure this client is closed when the session is closed */
1456 client_watch_session (client, session);
1458 /* signal new session */
1459 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1462 ctx->session = session;
1465 if (sessmedia == NULL) {
1466 /* manage the media in our session now, if not done already */
1467 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1468 /* if we stil have no media, error */
1469 if (sessmedia == NULL)
1470 goto sessmedia_unavailable;
1472 g_object_unref (media);
1475 ctx->sessmedia = sessmedia;
1477 /* set blocksize on this stream */
1478 if (!handle_blocksize (media, stream, ctx->request))
1479 goto invalid_blocksize;
1481 gst_rtsp_transport_new (&ct);
1483 /* our supported transports */
1484 supported = gst_rtsp_stream_get_protocols (stream);
1486 /* parse and find a usable supported transport */
1487 if (!parse_transport (transport, supported, ct))
1488 goto unsupported_transports;
1490 /* update the client transport */
1491 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1492 if (!klass->configure_client_transport (client, ctx, ct))
1493 goto unsupported_client_transport;
1495 /* set in the session media transport */
1496 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1498 /* configure keepalive for this transport */
1499 gst_rtsp_stream_transport_set_keepalive (trans,
1500 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1502 /* create and serialize the server transport */
1503 st = make_server_transport (client, ctx, ct);
1504 trans_str = gst_rtsp_transport_as_text (st);
1505 gst_rtsp_transport_free (st);
1507 /* construct the response now */
1508 code = GST_RTSP_STS_OK;
1509 gst_rtsp_message_init_response (ctx->response, code,
1510 gst_rtsp_status_as_text (code), ctx->request);
1512 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
1516 send_message (client, session, ctx->response, FALSE);
1518 /* update the state */
1519 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1520 switch (rtspstate) {
1521 case GST_RTSP_STATE_PLAYING:
1522 case GST_RTSP_STATE_RECORDING:
1523 case GST_RTSP_STATE_READY:
1524 /* no state change */
1527 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1530 g_object_unref (session);
1532 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
1539 GST_ERROR ("client %p: no uri", client);
1540 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1545 GST_ERROR ("client %p: no transport", client);
1546 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1551 GST_ERROR ("client %p: no session pool configured", client);
1552 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1555 media_not_found_no_reply:
1557 GST_ERROR ("client %p: media '%s' not found", client, path);
1558 /* error reply is already sent */
1563 GST_ERROR ("client %p: media '%s' not found", client, path);
1564 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1569 GST_ERROR ("client %p: no control in path '%s'", client, path);
1570 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1571 g_object_unref (media);
1576 GST_ERROR ("client %p: stream '%s' not found", client, control);
1577 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1578 g_object_unref (media);
1581 service_unavailable:
1583 GST_ERROR ("client %p: can't create session", client);
1584 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1585 g_object_unref (media);
1588 sessmedia_unavailable:
1590 GST_ERROR ("client %p: can't create session media", client);
1591 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1592 g_object_unref (media);
1593 g_object_unref (session);
1598 GST_ERROR ("client %p: invalid blocksize", client);
1599 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1600 g_object_unref (session);
1603 unsupported_transports:
1605 GST_ERROR ("client %p: unsupported transports", client);
1606 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1607 gst_rtsp_transport_free (ct);
1608 g_object_unref (session);
1611 unsupported_client_transport:
1613 GST_ERROR ("client %p: unsupported client transport", client);
1614 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1615 gst_rtsp_transport_free (ct);
1616 g_object_unref (session);
1621 static GstSDPMessage *
1622 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1624 GstRTSPClientPrivate *priv = client->priv;
1629 gst_sdp_message_new (&sdp);
1631 /* some standard things first */
1632 gst_sdp_message_set_version (sdp, "0");
1639 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1642 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1643 gst_sdp_message_set_information (sdp, "rtsp-server");
1644 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1645 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1646 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1647 gst_sdp_message_add_attribute (sdp, "control", "*");
1649 info.is_ipv6 = priv->is_ipv6;
1650 info.server_ip = priv->server_ip;
1652 /* create an SDP for the media object */
1653 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1661 GST_ERROR ("client %p: could not create SDP", client);
1662 gst_sdp_message_free (sdp);
1667 /* for the describe we must generate an SDP */
1669 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
1671 GstRTSPClientPrivate *priv = client->priv;
1676 GstRTSPMedia *media;
1677 GstRTSPClientClass *klass;
1679 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1684 /* check what kind of format is accepted, we don't really do anything with it
1685 * and always return SDP for now. */
1690 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
1692 if (res == GST_RTSP_ENOTIMPL)
1695 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1699 if (!priv->mount_points)
1700 goto no_mount_points;
1702 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
1705 /* find the media object for the uri */
1706 if (!(media = find_media (client, ctx, path, NULL)))
1709 /* create an SDP for the media object on this client */
1710 if (!(sdp = klass->create_sdp (client, media)))
1713 g_object_unref (media);
1715 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
1716 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
1718 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
1721 /* content base for some clients that might screw up creating the setup uri */
1722 str = make_base_url (client, ctx->uri, path);
1725 GST_INFO ("adding content-base: %s", str);
1726 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE, str);
1728 /* add SDP to the response body */
1729 str = gst_sdp_message_as_text (sdp);
1730 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
1731 gst_sdp_message_free (sdp);
1733 send_message (client, ctx->session, ctx->response, FALSE);
1735 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1743 GST_ERROR ("client %p: no uri", client);
1744 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1749 GST_ERROR ("client %p: no mount points configured", client);
1750 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1755 GST_ERROR ("client %p: can't find path for url", client);
1756 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1761 GST_ERROR ("client %p: no media", client);
1763 /* error reply is already sent */
1768 GST_ERROR ("client %p: can't create SDP", client);
1769 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1771 g_object_unref (media);
1777 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
1779 GstRTSPMethod options;
1782 options = GST_RTSP_DESCRIBE |
1787 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1789 str = gst_rtsp_options_as_text (options);
1791 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
1792 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
1794 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
1797 send_message (client, ctx->session, ctx->response, FALSE);
1799 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1805 /* remove duplicate and trailing '/' */
1807 sanitize_uri (GstRTSPUrl * uri)
1811 gboolean have_slash, prev_slash;
1813 s = d = uri->abspath;
1814 len = strlen (uri->abspath);
1818 for (i = 0; i < len; i++) {
1819 have_slash = s[i] == '/';
1821 if (!have_slash || !prev_slash)
1823 prev_slash = have_slash;
1825 len = d - uri->abspath;
1826 /* don't remove the first slash if that's the only thing left */
1827 if (len > 1 && *(d - 1) == '/')
1833 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1835 GstRTSPClientPrivate *priv = client->priv;
1837 GST_INFO ("client %p: session %p finished", client, session);
1839 /* unlink all media managed in this session */
1840 client_unlink_session (client, session);
1842 /* remove the session */
1843 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1844 GST_INFO ("client %p: all sessions finalized, close the connection",
1846 close_connection (client);
1851 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1853 GstRTSPClientPrivate *priv = client->priv;
1854 GstRTSPMethod method;
1855 const gchar *uristr;
1856 GstRTSPUrl *uri = NULL;
1857 GstRTSPVersion version;
1859 GstRTSPSession *session = NULL;
1860 GstRTSPContext sctx = { NULL }, *ctx;
1861 GstRTSPMessage response = { 0 };
1864 if (!(ctx = gst_rtsp_context_get_current ())) {
1866 ctx->auth = priv->auth;
1867 gst_rtsp_context_push_current (ctx);
1870 ctx->conn = priv->connection;
1871 ctx->client = client;
1872 ctx->request = request;
1873 ctx->response = &response;
1875 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1876 gst_rtsp_message_dump (request);
1879 GST_INFO ("client %p: received a request", client);
1881 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1883 /* we can only handle 1.0 requests */
1884 if (version != GST_RTSP_VERSION_1_0)
1887 ctx->method = method;
1889 /* we always try to parse the url first */
1890 if (strcmp (uristr, "*") == 0) {
1891 /* special case where we have * as uri, keep uri = NULL */
1892 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1895 /* get the session if there is any */
1896 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1897 if (res == GST_RTSP_OK) {
1898 if (priv->session_pool == NULL)
1901 /* we had a session in the request, find it again */
1902 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1903 goto session_not_found;
1905 /* we add the session to the client list of watched sessions. When a session
1906 * disappears because it times out, we will be notified. If all sessions are
1907 * gone, we will close the connection */
1908 client_watch_session (client, session);
1911 /* sanitize the uri */
1915 ctx->session = session;
1917 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
1918 goto not_authorized;
1920 /* now see what is asked and dispatch to a dedicated handler */
1922 case GST_RTSP_OPTIONS:
1923 handle_options_request (client, ctx);
1925 case GST_RTSP_DESCRIBE:
1926 handle_describe_request (client, ctx);
1928 case GST_RTSP_SETUP:
1929 handle_setup_request (client, ctx);
1932 handle_play_request (client, ctx);
1934 case GST_RTSP_PAUSE:
1935 handle_pause_request (client, ctx);
1937 case GST_RTSP_TEARDOWN:
1938 handle_teardown_request (client, ctx);
1940 case GST_RTSP_SET_PARAMETER:
1941 handle_set_param_request (client, ctx);
1943 case GST_RTSP_GET_PARAMETER:
1944 handle_get_param_request (client, ctx);
1946 case GST_RTSP_ANNOUNCE:
1947 case GST_RTSP_RECORD:
1948 case GST_RTSP_REDIRECT:
1949 goto not_implemented;
1950 case GST_RTSP_INVALID:
1957 gst_rtsp_context_pop_current (ctx);
1959 g_object_unref (session);
1961 gst_rtsp_url_free (uri);
1967 GST_ERROR ("client %p: version %d not supported", client, version);
1968 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1974 GST_ERROR ("client %p: bad request", client);
1975 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1980 GST_ERROR ("client %p: no pool configured", client);
1981 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1986 GST_ERROR ("client %p: session not found", client);
1987 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1992 GST_ERROR ("client %p: not allowed", client);
1993 /* error reply is already sent */
1998 GST_ERROR ("client %p: method %d not implemented", client, method);
1999 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
2006 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
2008 GstRTSPClientPrivate *priv = client->priv;
2010 GstRTSPSession *session = NULL;
2011 GstRTSPContext sctx = { NULL }, *ctx;
2014 if (!(ctx = gst_rtsp_context_get_current ())) {
2016 ctx->auth = priv->auth;
2017 gst_rtsp_context_push_current (ctx);
2020 ctx->conn = priv->connection;
2021 ctx->client = client;
2022 ctx->request = NULL;
2024 ctx->method = GST_RTSP_INVALID;
2025 ctx->response = response;
2027 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2028 gst_rtsp_message_dump (response);
2031 GST_INFO ("client %p: received a response", client);
2033 /* get the session if there is any */
2035 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
2036 if (res == GST_RTSP_OK) {
2037 if (priv->session_pool == NULL)
2040 /* we had a session in the request, find it again */
2041 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2042 goto session_not_found;
2044 /* we add the session to the client list of watched sessions. When a session
2045 * disappears because it times out, we will be notified. If all sessions are
2046 * gone, we will close the connection */
2047 client_watch_session (client, session);
2050 ctx->session = session;
2052 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
2057 gst_rtsp_context_pop_current (ctx);
2059 g_object_unref (session);
2064 GST_ERROR ("client %p: no pool configured", client);
2069 GST_ERROR ("client %p: session not found", client);
2075 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2077 GstRTSPClientPrivate *priv = client->priv;
2086 /* find the stream for this message */
2087 res = gst_rtsp_message_parse_data (message, &channel);
2088 if (res != GST_RTSP_OK)
2091 gst_rtsp_message_steal_body (message, &data, &size);
2093 buffer = gst_buffer_new_wrapped (data, size);
2096 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2097 GstRTSPStreamTransport *trans;
2098 GstRTSPStream *stream;
2099 const GstRTSPTransport *tr;
2103 tr = gst_rtsp_stream_transport_get_transport (trans);
2104 stream = gst_rtsp_stream_transport_get_stream (trans);
2106 /* check for TCP transport */
2107 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2108 /* dispatch to the stream based on the channel number */
2109 if (tr->interleaved.min == channel) {
2110 gst_rtsp_stream_recv_rtp (stream, buffer);
2113 } else if (tr->interleaved.max == channel) {
2114 gst_rtsp_stream_recv_rtcp (stream, buffer);
2121 gst_buffer_unref (buffer);
2125 * gst_rtsp_client_set_session_pool:
2126 * @client: a #GstRTSPClient
2127 * @pool: a #GstRTSPSessionPool
2129 * Set @pool as the sessionpool for @client which it will use to find
2130 * or allocate sessions. the sessionpool is usually inherited from the server
2131 * that created the client but can be overridden later.
2134 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2135 GstRTSPSessionPool * pool)
2137 GstRTSPSessionPool *old;
2138 GstRTSPClientPrivate *priv;
2140 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2142 priv = client->priv;
2145 g_object_ref (pool);
2147 g_mutex_lock (&priv->lock);
2148 old = priv->session_pool;
2149 priv->session_pool = pool;
2150 g_mutex_unlock (&priv->lock);
2153 g_object_unref (old);
2157 * gst_rtsp_client_get_session_pool:
2158 * @client: a #GstRTSPClient
2160 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2162 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2164 GstRTSPSessionPool *
2165 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2167 GstRTSPClientPrivate *priv;
2168 GstRTSPSessionPool *result;
2170 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2172 priv = client->priv;
2174 g_mutex_lock (&priv->lock);
2175 if ((result = priv->session_pool))
2176 g_object_ref (result);
2177 g_mutex_unlock (&priv->lock);
2183 * gst_rtsp_client_set_mount_points:
2184 * @client: a #GstRTSPClient
2185 * @mounts: a #GstRTSPMountPoints
2187 * Set @mounts as the mount points for @client which it will use to map urls
2188 * to media streams. These mount points are usually inherited from the server that
2189 * created the client but can be overriden later.
2192 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2193 GstRTSPMountPoints * mounts)
2195 GstRTSPClientPrivate *priv;
2196 GstRTSPMountPoints *old;
2198 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2200 priv = client->priv;
2203 g_object_ref (mounts);
2205 g_mutex_lock (&priv->lock);
2206 old = priv->mount_points;
2207 priv->mount_points = mounts;
2208 g_mutex_unlock (&priv->lock);
2211 g_object_unref (old);
2215 * gst_rtsp_client_get_mount_points:
2216 * @client: a #GstRTSPClient
2218 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2220 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2222 GstRTSPMountPoints *
2223 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2225 GstRTSPClientPrivate *priv;
2226 GstRTSPMountPoints *result;
2228 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2230 priv = client->priv;
2232 g_mutex_lock (&priv->lock);
2233 if ((result = priv->mount_points))
2234 g_object_ref (result);
2235 g_mutex_unlock (&priv->lock);
2241 * gst_rtsp_client_set_auth:
2242 * @client: a #GstRTSPClient
2243 * @auth: a #GstRTSPAuth
2245 * configure @auth to be used as the authentication manager of @client.
2248 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2250 GstRTSPClientPrivate *priv;
2253 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2255 priv = client->priv;
2258 g_object_ref (auth);
2260 g_mutex_lock (&priv->lock);
2263 g_mutex_unlock (&priv->lock);
2266 g_object_unref (old);
2271 * gst_rtsp_client_get_auth:
2272 * @client: a #GstRTSPClient
2274 * Get the #GstRTSPAuth used as the authentication manager of @client.
2276 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2280 gst_rtsp_client_get_auth (GstRTSPClient * client)
2282 GstRTSPClientPrivate *priv;
2283 GstRTSPAuth *result;
2285 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2287 priv = client->priv;
2289 g_mutex_lock (&priv->lock);
2290 if ((result = priv->auth))
2291 g_object_ref (result);
2292 g_mutex_unlock (&priv->lock);
2298 * gst_rtsp_client_set_thread_pool:
2299 * @client: a #GstRTSPClient
2300 * @pool: a #GstRTSPThreadPool
2302 * configure @pool to be used as the thread pool of @client.
2305 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2306 GstRTSPThreadPool * pool)
2308 GstRTSPClientPrivate *priv;
2309 GstRTSPThreadPool *old;
2311 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2313 priv = client->priv;
2316 g_object_ref (pool);
2318 g_mutex_lock (&priv->lock);
2319 old = priv->thread_pool;
2320 priv->thread_pool = pool;
2321 g_mutex_unlock (&priv->lock);
2324 g_object_unref (old);
2328 * gst_rtsp_client_get_thread_pool:
2329 * @client: a #GstRTSPClient
2331 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2333 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2337 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2339 GstRTSPClientPrivate *priv;
2340 GstRTSPThreadPool *result;
2342 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2344 priv = client->priv;
2346 g_mutex_lock (&priv->lock);
2347 if ((result = priv->thread_pool))
2348 g_object_ref (result);
2349 g_mutex_unlock (&priv->lock);
2355 * gst_rtsp_client_set_connection:
2356 * @client: a #GstRTSPClient
2357 * @conn: (transfer full): a #GstRTSPConnection
2359 * Set the #GstRTSPConnection of @client. This function takes ownership of
2362 * Returns: %TRUE on success.
2365 gst_rtsp_client_set_connection (GstRTSPClient * client,
2366 GstRTSPConnection * conn)
2368 GstRTSPClientPrivate *priv;
2369 GSocket *read_socket;
2370 GSocketAddress *address;
2372 GError *error = NULL;
2374 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2375 g_return_val_if_fail (conn != NULL, FALSE);
2377 priv = client->priv;
2379 read_socket = gst_rtsp_connection_get_read_socket (conn);
2381 if (!(address = g_socket_get_local_address (read_socket, &error)))
2384 g_free (priv->server_ip);
2385 /* keep the original ip that the client connected to */
2386 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2387 GInetAddress *iaddr;
2389 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2391 /* socket might be ipv6 but adress still ipv4 */
2392 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2393 priv->server_ip = g_inet_address_to_string (iaddr);
2394 g_object_unref (address);
2396 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2397 priv->server_ip = g_strdup ("unknown");
2400 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2401 priv->server_ip, priv->is_ipv6);
2403 url = gst_rtsp_connection_get_url (conn);
2404 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2406 priv->connection = conn;
2413 GST_ERROR ("could not get local address %s", error->message);
2414 g_error_free (error);
2420 * gst_rtsp_client_get_connection:
2421 * @client: a #GstRTSPClient
2423 * Get the #GstRTSPConnection of @client.
2425 * Returns: (transfer none): the #GstRTSPConnection of @client.
2426 * The connection object returned remains valid until the client is freed.
2429 gst_rtsp_client_get_connection (GstRTSPClient * client)
2431 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2433 return client->priv->connection;
2437 * gst_rtsp_client_set_send_func:
2438 * @client: a #GstRTSPClient
2439 * @func: a #GstRTSPClientSendFunc
2440 * @user_data: user data passed to @func
2441 * @notify: called when @user_data is no longer in use
2443 * Set @func as the callback that will be called when a new message needs to be
2444 * sent to the client. @user_data is passed to @func and @notify is called when
2445 * @user_data is no longer in use.
2447 * By default, the client will send the messages on the #GstRTSPConnection that
2448 * was configured with gst_rtsp_client_attach() was called.
2451 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2452 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2454 GstRTSPClientPrivate *priv;
2455 GDestroyNotify old_notify;
2458 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2460 priv = client->priv;
2462 g_mutex_lock (&priv->send_lock);
2463 priv->send_func = func;
2464 old_notify = priv->send_notify;
2465 old_data = priv->send_data;
2466 priv->send_notify = notify;
2467 priv->send_data = user_data;
2468 g_mutex_unlock (&priv->send_lock);
2471 old_notify (old_data);
2475 * gst_rtsp_client_handle_message:
2476 * @client: a #GstRTSPClient
2477 * @message: an #GstRTSPMessage
2479 * Let the client handle @message.
2481 * Returns: a #GstRTSPResult.
2484 gst_rtsp_client_handle_message (GstRTSPClient * client,
2485 GstRTSPMessage * message)
2487 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2488 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2490 switch (message->type) {
2491 case GST_RTSP_MESSAGE_REQUEST:
2492 handle_request (client, message);
2494 case GST_RTSP_MESSAGE_RESPONSE:
2495 handle_response (client, message);
2497 case GST_RTSP_MESSAGE_DATA:
2498 handle_data (client, message);
2507 * gst_rtsp_client_send_message:
2508 * @client: a #GstRTSPClient
2509 * @session: a #GstRTSPSession to send the message to or %NULL
2510 * @message: The #GstRTSPMessage to send
2512 * Send a message message to the remote end. @message must be a
2513 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
2516 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
2517 GstRTSPMessage * message)
2519 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2520 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2521 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
2522 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
2524 send_message (client, session, message, FALSE);
2529 static GstRTSPResult
2530 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2531 gboolean close, gpointer user_data)
2533 GstRTSPClientPrivate *priv = client->priv;
2535 /* send the response and store the seq number so we can wait until it's
2536 * written to the client to close the connection */
2537 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2538 &priv->close_seq : NULL);
2541 static GstRTSPResult
2542 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2545 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2548 static GstRTSPResult
2549 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2551 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2552 GstRTSPClientPrivate *priv = client->priv;
2554 if (priv->close_seq && priv->close_seq == cseq) {
2555 priv->close_seq = 0;
2556 close_connection (client);
2562 static GstRTSPResult
2563 closed (GstRTSPWatch * watch, gpointer user_data)
2565 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2566 GstRTSPClientPrivate *priv = client->priv;
2567 const gchar *tunnelid;
2569 GST_INFO ("client %p: connection closed", client);
2571 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2572 g_mutex_lock (&tunnels_lock);
2573 /* remove from tunnelids */
2574 g_hash_table_remove (tunnels, tunnelid);
2575 g_mutex_unlock (&tunnels_lock);
2578 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2583 static GstRTSPResult
2584 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2586 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2589 str = gst_rtsp_strresult (result);
2590 GST_INFO ("client %p: received an error %s", client, str);
2596 static GstRTSPResult
2597 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2598 GstRTSPMessage * message, guint id, gpointer user_data)
2600 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2603 str = gst_rtsp_strresult (result);
2605 ("client %p: error when handling message %p with id %d: %s",
2606 client, message, id, str);
2613 remember_tunnel (GstRTSPClient * client)
2615 GstRTSPClientPrivate *priv = client->priv;
2616 const gchar *tunnelid;
2618 /* store client in the pending tunnels */
2619 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2620 if (tunnelid == NULL)
2623 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2625 /* we can't have two clients connecting with the same tunnelid */
2626 g_mutex_lock (&tunnels_lock);
2627 if (g_hash_table_lookup (tunnels, tunnelid))
2628 goto tunnel_existed;
2630 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2631 g_mutex_unlock (&tunnels_lock);
2638 GST_ERROR ("client %p: no tunnelid provided", client);
2643 g_mutex_unlock (&tunnels_lock);
2644 GST_ERROR ("client %p: tunnel session %s already existed", client,
2650 static GstRTSPStatusCode
2651 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2653 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2654 GstRTSPClientPrivate *priv = client->priv;
2656 GST_INFO ("client %p: tunnel start (connection %p)", client,
2659 if (!remember_tunnel (client))
2662 return GST_RTSP_STS_OK;
2667 GST_ERROR ("client %p: error starting tunnel", client);
2668 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2672 static GstRTSPResult
2673 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2675 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2676 GstRTSPClientPrivate *priv = client->priv;
2678 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2681 /* ignore error, it'll only be a problem when the client does a POST again */
2682 remember_tunnel (client);
2687 static GstRTSPResult
2688 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2690 const gchar *tunnelid;
2691 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2692 GstRTSPClientPrivate *priv = client->priv;
2693 GstRTSPClient *oclient;
2694 GstRTSPClientPrivate *opriv;
2696 GST_INFO ("client %p: tunnel complete", client);
2698 /* find previous tunnel */
2699 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2700 if (tunnelid == NULL)
2703 g_mutex_lock (&tunnels_lock);
2704 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2707 /* remove the old client from the table. ref before because removing it will
2708 * remove the ref to it. */
2709 g_object_ref (oclient);
2710 g_hash_table_remove (tunnels, tunnelid);
2712 opriv = oclient->priv;
2714 if (opriv->watch == NULL)
2716 g_mutex_unlock (&tunnels_lock);
2718 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2719 opriv->connection, priv->connection);
2721 /* merge the tunnels into the first client */
2722 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2723 gst_rtsp_watch_reset (opriv->watch);
2724 g_object_unref (oclient);
2731 GST_ERROR ("client %p: no tunnelid provided", client);
2732 return GST_RTSP_ERROR;
2736 g_mutex_unlock (&tunnels_lock);
2737 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2738 return GST_RTSP_ERROR;
2742 g_mutex_unlock (&tunnels_lock);
2743 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2744 g_object_unref (oclient);
2745 return GST_RTSP_ERROR;
2749 static GstRTSPWatchFuncs watch_funcs = {
2761 client_watch_notify (GstRTSPClient * client)
2763 GstRTSPClientPrivate *priv = client->priv;
2765 GST_INFO ("client %p: watch destroyed", client);
2767 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2768 g_object_unref (client);
2772 * gst_rtsp_client_attach:
2773 * @client: a #GstRTSPClient
2774 * @context: (allow-none): a #GMainContext
2776 * Attaches @client to @context. When the mainloop for @context is run, the
2777 * client will be dispatched. When @context is NULL, the default context will be
2780 * This function should be called when the client properties and urls are fully
2781 * configured and the client is ready to start.
2783 * Returns: the ID (greater than 0) for the source within the GMainContext.
2786 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2788 GstRTSPClientPrivate *priv;
2791 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2792 priv = client->priv;
2793 g_return_val_if_fail (priv->connection != NULL, 0);
2794 g_return_val_if_fail (priv->watch == NULL, 0);
2796 /* create watch for the connection and attach */
2797 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2798 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2799 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2800 (GDestroyNotify) gst_rtsp_watch_unref);
2802 /* FIXME make this configurable. We don't want to do this yet because it will
2803 * be superceeded by a cache object later */
2804 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2806 GST_INFO ("attaching to context %p", context);
2807 res = gst_rtsp_watch_attach (priv->watch, context);
2813 * gst_rtsp_client_session_filter:
2814 * @client: a #GstRTSPClient
2815 * @func: (scope call): a callback
2816 * @user_data: user data passed to @func
2818 * Call @func for each session managed by @client. The result value of @func
2819 * determines what happens to the session. @func will be called with @client
2820 * locked so no further actions on @client can be performed from @func.
2822 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
2825 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
2827 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
2828 * will also be added with an additional ref to the result #GList of this
2831 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
2832 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2833 * element in the #GList should be unreffed before the list is freed.
2836 gst_rtsp_client_session_filter (GstRTSPClient * client,
2837 GstRTSPClientSessionFilterFunc func, gpointer user_data)
2839 GstRTSPClientPrivate *priv;
2840 GList *result, *walk, *next;
2842 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2843 g_return_val_if_fail (func != NULL, NULL);
2845 priv = client->priv;
2849 g_mutex_lock (&priv->lock);
2850 for (walk = priv->sessions; walk; walk = next) {
2851 GstRTSPSession *sess = walk->data;
2853 next = g_list_next (walk);
2855 switch (func (client, sess, user_data)) {
2856 case GST_RTSP_FILTER_REMOVE:
2857 /* stop watching the session and pretent it went away */
2858 client_cleanup_session (client, sess);
2860 case GST_RTSP_FILTER_REF:
2861 result = g_list_prepend (result, g_object_ref (sess));
2863 case GST_RTSP_FILTER_KEEP:
2868 g_mutex_unlock (&priv->lock);