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,
571 GST_ERROR ("client %p: not authorized for factory path %s", client, path);
576 GST_ERROR ("client %p: can't create media", client);
577 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
578 g_object_unref (factory);
584 GST_ERROR ("client %p: can't create thread", client);
585 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
586 g_object_unref (media);
588 g_object_unref (factory);
594 GST_ERROR ("client %p: can't prepare media", client);
595 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
596 g_object_unref (media);
598 g_object_unref (factory);
605 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
607 GstRTSPClientPrivate *priv = client->priv;
608 GstRTSPMessage message = { 0 };
613 gst_rtsp_message_init_data (&message, channel);
615 /* FIXME, need some sort of iovec RTSPMessage here */
616 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
619 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
621 g_mutex_lock (&priv->send_lock);
623 priv->send_func (client, &message, FALSE, priv->send_data);
624 g_mutex_unlock (&priv->send_lock);
626 gst_rtsp_message_steal_body (&message, &data, &usize);
627 gst_buffer_unmap (buffer, &map_info);
629 gst_rtsp_message_unset (&message);
635 link_transport (GstRTSPClient * client, GstRTSPSession * session,
636 GstRTSPStreamTransport * trans)
638 GstRTSPClientPrivate *priv = client->priv;
640 GST_DEBUG ("client %p: linking transport %p", client, trans);
642 gst_rtsp_stream_transport_set_callbacks (trans,
643 (GstRTSPSendFunc) do_send_data,
644 (GstRTSPSendFunc) do_send_data, client, NULL);
646 priv->transports = g_list_prepend (priv->transports, trans);
648 /* make sure our session can't expire */
649 gst_rtsp_session_prevent_expire (session);
653 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
654 GstRTSPStreamTransport * trans)
656 GstRTSPClientPrivate *priv = client->priv;
658 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
660 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
662 priv->transports = g_list_remove (priv->transports, trans);
664 /* our session can now expire */
665 gst_rtsp_session_allow_expire (session);
669 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
670 GstRTSPSessionMedia * sessmedia)
675 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
676 for (i = 0; i < n_streams; i++) {
677 GstRTSPStreamTransport *trans;
678 const GstRTSPTransport *tr;
680 /* get the transport, if there is no transport configured, skip this stream */
681 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
685 tr = gst_rtsp_stream_transport_get_transport (trans);
687 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
688 /* for TCP, unlink the stream from the TCP connection of the client */
689 unlink_transport (client, session, trans);
695 close_connection (GstRTSPClient * client)
697 GstRTSPClientPrivate *priv = client->priv;
698 const gchar *tunnelid;
700 GST_DEBUG ("client %p: closing connection", client);
702 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
703 g_mutex_lock (&tunnels_lock);
704 /* remove from tunnelids */
705 g_hash_table_remove (tunnels, tunnelid);
706 g_mutex_unlock (&tunnels_lock);
709 gst_rtsp_connection_close (priv->connection);
713 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
715 GstRTSPClientPrivate *priv = client->priv;
716 GstRTSPSession *session;
717 GstRTSPSessionMedia *sessmedia;
718 GstRTSPStatusCode code;
725 session = ctx->session;
730 path = ctx->uri->abspath;
732 /* get a handle to the configuration of the media in the session */
733 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
737 /* only aggregate control for now.. */
738 if (path[matched] != '\0')
741 ctx->sessmedia = sessmedia;
743 /* we emit the signal before closing the connection */
744 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
747 /* unlink the all TCP callbacks */
748 unlink_session_transports (client, session, sessmedia);
750 /* remove the session from the watched sessions */
751 client_unwatch_session (client, session);
753 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
755 /* unmanage the media in the session, returns false if all media session
757 if (!gst_rtsp_session_release_media (session, sessmedia)) {
758 /* remove the session */
759 gst_rtsp_session_pool_remove (priv->session_pool, session);
761 /* construct the response now */
762 code = GST_RTSP_STS_OK;
763 gst_rtsp_message_init_response (ctx->response, code,
764 gst_rtsp_status_as_text (code), ctx->request);
766 send_message (client, session, ctx->response, TRUE);
773 GST_ERROR ("client %p: no session", client);
774 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
779 GST_ERROR ("client %p: no uri supplied", client);
780 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
785 GST_ERROR ("client %p: no media for uri", client);
786 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
791 GST_ERROR ("client %p: no aggregate path %s", client, path);
792 send_generic_response (client,
793 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
799 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
803 res = gst_rtsp_params_set (client, ctx);
809 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
813 res = gst_rtsp_params_get (client, ctx);
819 handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
825 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
826 if (res != GST_RTSP_OK)
830 /* no body, keep-alive request */
831 send_generic_response (client, GST_RTSP_STS_OK, ctx);
833 /* there is a body, handle the params */
834 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
835 if (res != GST_RTSP_OK)
838 send_message (client, ctx->session, ctx->response, FALSE);
841 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
849 GST_ERROR ("client %p: bad request", client);
850 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
856 handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
862 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
863 if (res != GST_RTSP_OK)
867 /* no body, keep-alive request */
868 send_generic_response (client, GST_RTSP_STS_OK, ctx);
870 /* there is a body, handle the params */
871 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
872 if (res != GST_RTSP_OK)
875 send_message (client, ctx->session, ctx->response, FALSE);
878 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
886 GST_ERROR ("client %p: bad request", client);
887 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
893 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
895 GstRTSPSession *session;
896 GstRTSPSessionMedia *sessmedia;
897 GstRTSPStatusCode code;
898 GstRTSPState rtspstate;
902 if (!(session = ctx->session))
908 path = ctx->uri->abspath;
910 /* get a handle to the configuration of the media in the session */
911 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
915 if (path[matched] != '\0')
918 ctx->sessmedia = sessmedia;
920 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
921 /* the session state must be playing or recording */
922 if (rtspstate != GST_RTSP_STATE_PLAYING &&
923 rtspstate != GST_RTSP_STATE_RECORDING)
926 /* unlink the all TCP callbacks */
927 unlink_session_transports (client, session, sessmedia);
929 /* then pause sending */
930 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
932 /* construct the response now */
933 code = GST_RTSP_STS_OK;
934 gst_rtsp_message_init_response (ctx->response, code,
935 gst_rtsp_status_as_text (code), ctx->request);
937 send_message (client, session, ctx->response, FALSE);
939 /* the state is now READY */
940 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
942 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
949 GST_ERROR ("client %p: no seesion", client);
950 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
955 GST_ERROR ("client %p: no uri supplied", client);
956 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
961 GST_ERROR ("client %p: no media for uri", client);
962 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
967 GST_ERROR ("client %p: no aggregate path %s", client, path);
968 send_generic_response (client,
969 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
974 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
975 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
982 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
984 GstRTSPSession *session;
985 GstRTSPSessionMedia *sessmedia;
987 GstRTSPStatusCode code;
989 guint n_streams, i, infocount;
991 GstRTSPTimeRange *range;
993 GstRTSPState rtspstate;
994 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
998 if (!(session = ctx->session))
1004 path = ctx->uri->abspath;
1006 /* get a handle to the configuration of the media in the session */
1007 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1011 if (path[matched] != '\0')
1014 ctx->sessmedia = sessmedia;
1015 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1017 /* the session state must be playing or ready */
1018 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1019 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1022 /* parse the range header if we have one */
1023 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1024 if (res == GST_RTSP_OK) {
1025 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1026 /* we have a range, seek to the position */
1028 gst_rtsp_media_seek (media, range);
1029 gst_rtsp_range_free (range);
1033 /* grab RTPInfo from the payloaders now */
1034 rtpinfo = g_string_new ("");
1036 n_streams = gst_rtsp_media_n_streams (media);
1037 for (i = 0, infocount = 0; i < n_streams; i++) {
1038 GstRTSPStreamTransport *trans;
1039 GstRTSPStream *stream;
1040 const GstRTSPTransport *tr;
1044 /* get the transport, if there is no transport configured, skip this stream */
1045 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
1046 if (trans == NULL) {
1047 GST_INFO ("stream %d is not configured", i);
1050 tr = gst_rtsp_stream_transport_get_transport (trans);
1052 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1053 /* for TCP, link the stream to the TCP connection of the client */
1054 link_transport (client, session, trans);
1057 stream = gst_rtsp_stream_transport_get_stream (trans);
1058 if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
1060 g_string_append (rtpinfo, ", ");
1062 uristr = gst_rtsp_url_get_request_uri (ctx->uri);
1063 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
1064 uristr, i, seq, rtptime);
1069 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
1073 /* construct the response now */
1074 code = GST_RTSP_STS_OK;
1075 gst_rtsp_message_init_response (ctx->response, code,
1076 gst_rtsp_status_as_text (code), ctx->request);
1078 /* add the RTP-Info header */
1079 if (infocount > 0) {
1080 str = g_string_free (rtpinfo, FALSE);
1081 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO, str);
1083 g_string_free (rtpinfo, TRUE);
1087 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1089 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1091 send_message (client, session, ctx->response, FALSE);
1093 /* start playing after sending the request */
1094 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1096 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1098 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1105 GST_ERROR ("client %p: no session", client);
1106 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1111 GST_ERROR ("client %p: no uri supplied", client);
1112 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1117 GST_ERROR ("client %p: media not found", client);
1118 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1123 GST_ERROR ("client %p: no aggregate path %s", client, path);
1124 send_generic_response (client,
1125 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1130 GST_ERROR ("client %p: not PLAYING or READY", client);
1131 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1138 do_keepalive (GstRTSPSession * session)
1140 GST_INFO ("keep session %p alive", session);
1141 gst_rtsp_session_touch (session);
1144 /* parse @transport and return a valid transport in @tr. only transports
1145 * from @supported are returned. Returns FALSE if no valid transport
1148 parse_transport (const char *transport, GstRTSPLowerTrans supported,
1149 GstRTSPTransport * tr)
1156 gst_rtsp_transport_init (tr);
1158 GST_DEBUG ("parsing transports %s", transport);
1160 transports = g_strsplit (transport, ",", 0);
1162 /* loop through the transports, try to parse */
1163 for (i = 0; transports[i]; i++) {
1164 res = gst_rtsp_transport_parse (transports[i], tr);
1165 if (res != GST_RTSP_OK) {
1166 /* no valid transport, search some more */
1167 GST_WARNING ("could not parse transport %s", transports[i]);
1171 /* we have a transport, see if it's RTP/AVP */
1172 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1173 GST_WARNING ("invalid transport %s", transports[i]);
1177 if (!(tr->lower_transport & supported)) {
1178 GST_WARNING ("unsupported transport %s", transports[i]);
1182 /* we have a valid transport */
1183 GST_INFO ("found valid transport %s", transports[i]);
1188 gst_rtsp_transport_init (tr);
1190 g_strfreev (transports);
1196 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
1197 GstRTSPMessage * request)
1199 gchar *blocksize_str;
1200 gboolean ret = TRUE;
1202 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1203 &blocksize_str, 0) == GST_RTSP_OK) {
1207 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1208 if (end == blocksize_str) {
1209 GST_ERROR ("failed to parse blocksize");
1212 /* we don't want to change the mtu when this media
1213 * can be shared because it impacts other clients */
1214 if (gst_rtsp_media_is_shared (media))
1217 if (blocksize > G_MAXUINT)
1218 blocksize = G_MAXUINT;
1219 gst_rtsp_stream_set_mtu (stream, blocksize);
1226 default_configure_client_transport (GstRTSPClient * client,
1227 GstRTSPContext * ctx, GstRTSPTransport * ct)
1229 GstRTSPClientPrivate *priv = client->priv;
1231 /* we have a valid transport now, set the destination of the client. */
1232 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1233 gboolean use_client_settings;
1235 use_client_settings =
1236 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1238 if (ct->destination && use_client_settings) {
1239 GstRTSPAddress *addr;
1241 addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1242 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1247 gst_rtsp_address_free (addr);
1249 GstRTSPAddress *addr;
1250 GSocketFamily family;
1252 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1254 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1258 g_free (ct->destination);
1259 ct->destination = g_strdup (addr->address);
1260 ct->port.min = addr->port;
1261 ct->port.max = addr->port + addr->n_ports - 1;
1262 ct->ttl = addr->ttl;
1264 gst_rtsp_address_free (addr);
1269 url = gst_rtsp_connection_get_url (priv->connection);
1270 g_free (ct->destination);
1271 ct->destination = g_strdup (url->host);
1273 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1274 /* check if the client selected channels for TCP */
1275 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1276 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1286 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1291 static GstRTSPTransport *
1292 make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
1293 GstRTSPTransport * ct)
1295 GstRTSPTransport *st;
1297 GSocketFamily family;
1299 /* prepare the server transport */
1300 gst_rtsp_transport_new (&st);
1302 st->trans = ct->trans;
1303 st->profile = ct->profile;
1304 st->lower_transport = ct->lower_transport;
1306 addr = g_inet_address_new_from_string (ct->destination);
1309 GST_ERROR ("failed to get inet addr from client destination");
1310 family = G_SOCKET_FAMILY_IPV4;
1312 family = g_inet_address_get_family (addr);
1313 g_object_unref (addr);
1317 switch (st->lower_transport) {
1318 case GST_RTSP_LOWER_TRANS_UDP:
1319 st->client_port = ct->client_port;
1320 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
1322 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1323 st->port = ct->port;
1324 st->destination = g_strdup (ct->destination);
1327 case GST_RTSP_LOWER_TRANS_TCP:
1328 st->interleaved = ct->interleaved;
1333 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
1339 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
1341 GstRTSPClientPrivate *priv = client->priv;
1345 GstRTSPTransport *ct, *st;
1346 GstRTSPLowerTrans supported;
1347 GstRTSPStatusCode code;
1348 GstRTSPSession *session;
1349 GstRTSPStreamTransport *trans;
1351 GstRTSPSessionMedia *sessmedia;
1352 GstRTSPMedia *media;
1353 GstRTSPStream *stream;
1354 GstRTSPState rtspstate;
1355 GstRTSPClientClass *klass;
1356 gchar *path, *control;
1363 path = uri->abspath;
1365 /* parse the transport */
1367 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
1369 if (res != GST_RTSP_OK)
1372 /* we create the session after parsing stuff so that we don't make
1373 * a session for malformed requests */
1374 if (priv->session_pool == NULL)
1377 session = ctx->session;
1380 g_object_ref (session);
1381 /* get a handle to the configuration of the media in the session, this can
1382 * return NULL if this is a new url to manage in this session. */
1383 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1385 /* we need a new media configuration in this session */
1389 /* we have no session media, find one and manage it */
1390 if (sessmedia == NULL) {
1391 /* get a handle to the configuration of the media in the session */
1392 media = find_media (client, ctx, path, &matched);
1394 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1395 g_object_ref (media);
1397 /* no media, not found then */
1399 goto media_not_found;
1401 /* path is what matched. We can modify the parsed uri in place */
1402 path[matched] = '\0';
1403 /* control is remainder */
1404 control = &path[matched + 1];
1406 /* find the stream now using the control part */
1407 stream = gst_rtsp_media_find_stream (media, control);
1409 goto stream_not_found;
1411 /* now we have a uri identifying a valid media and stream */
1412 ctx->stream = stream;
1415 if (session == NULL) {
1416 /* create a session if this fails we probably reached our session limit or
1418 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1419 goto service_unavailable;
1421 /* make sure this client is closed when the session is closed */
1422 client_watch_session (client, session);
1424 /* signal new session */
1425 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1428 ctx->session = session;
1431 if (sessmedia == NULL) {
1432 /* manage the media in our session now, if not done already */
1433 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1434 /* if we stil have no media, error */
1435 if (sessmedia == NULL)
1436 goto sessmedia_unavailable;
1438 g_object_unref (media);
1441 ctx->sessmedia = sessmedia;
1443 /* set blocksize on this stream */
1444 if (!handle_blocksize (media, stream, ctx->request))
1445 goto invalid_blocksize;
1447 gst_rtsp_transport_new (&ct);
1449 /* our supported transports */
1450 supported = gst_rtsp_stream_get_protocols (stream);
1452 /* parse and find a usable supported transport */
1453 if (!parse_transport (transport, supported, ct))
1454 goto unsupported_transports;
1456 /* update the client transport */
1457 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1458 if (!klass->configure_client_transport (client, ctx, ct))
1459 goto unsupported_client_transport;
1461 /* set in the session media transport */
1462 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1464 /* configure keepalive for this transport */
1465 gst_rtsp_stream_transport_set_keepalive (trans,
1466 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1468 /* create and serialize the server transport */
1469 st = make_server_transport (client, ctx, ct);
1470 trans_str = gst_rtsp_transport_as_text (st);
1471 gst_rtsp_transport_free (st);
1473 /* construct the response now */
1474 code = GST_RTSP_STS_OK;
1475 gst_rtsp_message_init_response (ctx->response, code,
1476 gst_rtsp_status_as_text (code), ctx->request);
1478 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
1482 send_message (client, session, ctx->response, FALSE);
1484 /* update the state */
1485 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1486 switch (rtspstate) {
1487 case GST_RTSP_STATE_PLAYING:
1488 case GST_RTSP_STATE_RECORDING:
1489 case GST_RTSP_STATE_READY:
1490 /* no state change */
1493 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1496 g_object_unref (session);
1498 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
1505 GST_ERROR ("client %p: no uri", client);
1506 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1511 GST_ERROR ("client %p: no transport", client);
1512 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1517 GST_ERROR ("client %p: no session pool configured", client);
1518 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1523 GST_ERROR ("client %p: media '%s' not found", client, path);
1524 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1529 GST_ERROR ("client %p: stream '%s' not found", client, control);
1530 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1531 g_object_unref (media);
1534 service_unavailable:
1536 GST_ERROR ("client %p: can't create session", client);
1537 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1538 g_object_unref (media);
1541 sessmedia_unavailable:
1543 GST_ERROR ("client %p: can't create session media", client);
1544 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1545 g_object_unref (media);
1546 g_object_unref (session);
1551 GST_ERROR ("client %p: invalid blocksize", client);
1552 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1553 g_object_unref (session);
1556 unsupported_transports:
1558 GST_ERROR ("client %p: unsupported transports", client);
1559 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1560 gst_rtsp_transport_free (ct);
1561 g_object_unref (session);
1564 unsupported_client_transport:
1566 GST_ERROR ("client %p: unsupported client transport", client);
1567 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1568 gst_rtsp_transport_free (ct);
1569 g_object_unref (session);
1574 static GstSDPMessage *
1575 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1577 GstRTSPClientPrivate *priv = client->priv;
1582 gst_sdp_message_new (&sdp);
1584 /* some standard things first */
1585 gst_sdp_message_set_version (sdp, "0");
1592 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1595 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1596 gst_sdp_message_set_information (sdp, "rtsp-server");
1597 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1598 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1599 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1600 gst_sdp_message_add_attribute (sdp, "control", "*");
1602 info.is_ipv6 = priv->is_ipv6;
1603 info.server_ip = priv->server_ip;
1605 /* create an SDP for the media object */
1606 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1614 GST_ERROR ("client %p: could not create SDP", client);
1615 gst_sdp_message_free (sdp);
1620 /* for the describe we must generate an SDP */
1622 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
1624 GstRTSPClientPrivate *priv = client->priv;
1628 gchar *path, *str, *str_query, *content_base;
1629 GstRTSPMedia *media;
1630 GstRTSPClientClass *klass;
1632 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1637 /* check what kind of format is accepted, we don't really do anything with it
1638 * and always return SDP for now. */
1643 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
1645 if (res == GST_RTSP_ENOTIMPL)
1648 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1652 if (!priv->mount_points)
1653 goto no_mount_points;
1655 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
1658 /* find the media object for the uri */
1659 if (!(media = find_media (client, ctx, path, NULL)))
1664 /* create an SDP for the media object on this client */
1665 if (!(sdp = klass->create_sdp (client, media)))
1668 g_object_unref (media);
1670 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
1671 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
1673 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
1676 /* content base for some clients that might screw up creating the setup uri */
1677 str = gst_rtsp_url_get_request_uri (ctx->uri);
1678 str_len = strlen (str);
1680 /* check for query part */
1681 if (ctx->uri->query != NULL) {
1682 str_query = g_strrstr (str, "?");
1684 str_len = strlen (str);
1687 /* check for trailing '/' and append one */
1688 if (str[str_len - 1] != '/') {
1689 content_base = g_malloc (str_len + 2);
1690 memcpy (content_base, str, str_len);
1691 content_base[str_len] = '/';
1692 content_base[str_len + 1] = '\0';
1698 GST_INFO ("adding content-base: %s", content_base);
1700 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE,
1702 g_free (content_base);
1704 /* add SDP to the response body */
1705 str = gst_sdp_message_as_text (sdp);
1706 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
1707 gst_sdp_message_free (sdp);
1709 send_message (client, ctx->session, ctx->response, FALSE);
1711 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1719 GST_ERROR ("client %p: no uri", client);
1720 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1725 GST_ERROR ("client %p: no mount points configured", client);
1726 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1731 GST_ERROR ("client %p: can't find path for url", client);
1732 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1737 GST_ERROR ("client %p: no media", client);
1739 /* error reply is already sent */
1744 GST_ERROR ("client %p: can't create SDP", client);
1745 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1746 g_object_unref (media);
1752 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
1754 GstRTSPMethod options;
1757 options = GST_RTSP_DESCRIBE |
1762 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1764 str = gst_rtsp_options_as_text (options);
1766 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
1767 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
1769 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
1772 send_message (client, ctx->session, ctx->response, FALSE);
1774 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1780 /* remove duplicate and trailing '/' */
1782 sanitize_uri (GstRTSPUrl * uri)
1786 gboolean have_slash, prev_slash;
1788 s = d = uri->abspath;
1789 len = strlen (uri->abspath);
1793 for (i = 0; i < len; i++) {
1794 have_slash = s[i] == '/';
1796 if (!have_slash || !prev_slash)
1798 prev_slash = have_slash;
1800 len = d - uri->abspath;
1801 /* don't remove the first slash if that's the only thing left */
1802 if (len > 1 && *(d - 1) == '/')
1808 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1810 GstRTSPClientPrivate *priv = client->priv;
1812 GST_INFO ("client %p: session %p finished", client, session);
1814 /* unlink all media managed in this session */
1815 client_unlink_session (client, session);
1817 /* remove the session */
1818 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1819 GST_INFO ("client %p: all sessions finalized, close the connection",
1821 close_connection (client);
1826 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1828 GstRTSPClientPrivate *priv = client->priv;
1829 GstRTSPMethod method;
1830 const gchar *uristr;
1831 GstRTSPUrl *uri = NULL;
1832 GstRTSPVersion version;
1834 GstRTSPSession *session = NULL;
1835 GstRTSPContext sctx = { NULL }, *ctx;
1836 GstRTSPMessage response = { 0 };
1839 if (!(ctx = gst_rtsp_context_get_current ())) {
1841 ctx->auth = priv->auth;
1842 gst_rtsp_context_push_current (ctx);
1845 ctx->conn = priv->connection;
1846 ctx->client = client;
1847 ctx->request = request;
1848 ctx->response = &response;
1850 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1851 gst_rtsp_message_dump (request);
1854 GST_INFO ("client %p: received a request", client);
1856 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1858 /* we can only handle 1.0 requests */
1859 if (version != GST_RTSP_VERSION_1_0)
1862 ctx->method = method;
1864 /* we always try to parse the url first */
1865 if (strcmp (uristr, "*") == 0) {
1866 /* special case where we have * as uri, keep uri = NULL */
1867 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1870 /* get the session if there is any */
1871 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1872 if (res == GST_RTSP_OK) {
1873 if (priv->session_pool == NULL)
1876 /* we had a session in the request, find it again */
1877 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1878 goto session_not_found;
1880 /* we add the session to the client list of watched sessions. When a session
1881 * disappears because it times out, we will be notified. If all sessions are
1882 * gone, we will close the connection */
1883 client_watch_session (client, session);
1886 /* sanitize the uri */
1890 ctx->session = session;
1892 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
1893 goto not_authorized;
1895 /* now see what is asked and dispatch to a dedicated handler */
1897 case GST_RTSP_OPTIONS:
1898 handle_options_request (client, ctx);
1900 case GST_RTSP_DESCRIBE:
1901 handle_describe_request (client, ctx);
1903 case GST_RTSP_SETUP:
1904 handle_setup_request (client, ctx);
1907 handle_play_request (client, ctx);
1909 case GST_RTSP_PAUSE:
1910 handle_pause_request (client, ctx);
1912 case GST_RTSP_TEARDOWN:
1913 handle_teardown_request (client, ctx);
1915 case GST_RTSP_SET_PARAMETER:
1916 handle_set_param_request (client, ctx);
1918 case GST_RTSP_GET_PARAMETER:
1919 handle_get_param_request (client, ctx);
1921 case GST_RTSP_ANNOUNCE:
1922 case GST_RTSP_RECORD:
1923 case GST_RTSP_REDIRECT:
1924 goto not_implemented;
1925 case GST_RTSP_INVALID:
1932 gst_rtsp_context_pop_current (ctx);
1934 g_object_unref (session);
1936 gst_rtsp_url_free (uri);
1942 GST_ERROR ("client %p: version %d not supported", client, version);
1943 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1949 GST_ERROR ("client %p: bad request", client);
1950 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1955 GST_ERROR ("client %p: no pool configured", client);
1956 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1961 GST_ERROR ("client %p: session not found", client);
1962 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1967 GST_ERROR ("client %p: not allowed", client);
1972 GST_ERROR ("client %p: method %d not implemented", client, method);
1973 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
1980 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
1982 GstRTSPClientPrivate *priv = client->priv;
1984 GstRTSPSession *session = NULL;
1985 GstRTSPContext sctx = { NULL }, *ctx;
1988 if (!(ctx = gst_rtsp_context_get_current ())) {
1990 ctx->auth = priv->auth;
1991 gst_rtsp_context_push_current (ctx);
1994 ctx->conn = priv->connection;
1995 ctx->client = client;
1996 ctx->request = NULL;
1998 ctx->method = GST_RTSP_INVALID;
1999 ctx->response = response;
2001 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2002 gst_rtsp_message_dump (response);
2005 GST_INFO ("client %p: received a response", client);
2007 /* get the session if there is any */
2009 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
2010 if (res == GST_RTSP_OK) {
2011 if (priv->session_pool == NULL)
2014 /* we had a session in the request, find it again */
2015 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2016 goto session_not_found;
2018 /* we add the session to the client list of watched sessions. When a session
2019 * disappears because it times out, we will be notified. If all sessions are
2020 * gone, we will close the connection */
2021 client_watch_session (client, session);
2024 ctx->session = session;
2026 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
2031 gst_rtsp_context_pop_current (ctx);
2033 g_object_unref (session);
2038 GST_ERROR ("client %p: no pool configured", client);
2043 GST_ERROR ("client %p: session not found", client);
2049 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2051 GstRTSPClientPrivate *priv = client->priv;
2060 /* find the stream for this message */
2061 res = gst_rtsp_message_parse_data (message, &channel);
2062 if (res != GST_RTSP_OK)
2065 gst_rtsp_message_steal_body (message, &data, &size);
2067 buffer = gst_buffer_new_wrapped (data, size);
2070 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2071 GstRTSPStreamTransport *trans;
2072 GstRTSPStream *stream;
2073 const GstRTSPTransport *tr;
2077 tr = gst_rtsp_stream_transport_get_transport (trans);
2078 stream = gst_rtsp_stream_transport_get_stream (trans);
2080 /* check for TCP transport */
2081 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2082 /* dispatch to the stream based on the channel number */
2083 if (tr->interleaved.min == channel) {
2084 gst_rtsp_stream_recv_rtp (stream, buffer);
2087 } else if (tr->interleaved.max == channel) {
2088 gst_rtsp_stream_recv_rtcp (stream, buffer);
2095 gst_buffer_unref (buffer);
2099 * gst_rtsp_client_set_session_pool:
2100 * @client: a #GstRTSPClient
2101 * @pool: a #GstRTSPSessionPool
2103 * Set @pool as the sessionpool for @client which it will use to find
2104 * or allocate sessions. the sessionpool is usually inherited from the server
2105 * that created the client but can be overridden later.
2108 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2109 GstRTSPSessionPool * pool)
2111 GstRTSPSessionPool *old;
2112 GstRTSPClientPrivate *priv;
2114 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2116 priv = client->priv;
2119 g_object_ref (pool);
2121 g_mutex_lock (&priv->lock);
2122 old = priv->session_pool;
2123 priv->session_pool = pool;
2124 g_mutex_unlock (&priv->lock);
2127 g_object_unref (old);
2131 * gst_rtsp_client_get_session_pool:
2132 * @client: a #GstRTSPClient
2134 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2136 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2138 GstRTSPSessionPool *
2139 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2141 GstRTSPClientPrivate *priv;
2142 GstRTSPSessionPool *result;
2144 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2146 priv = client->priv;
2148 g_mutex_lock (&priv->lock);
2149 if ((result = priv->session_pool))
2150 g_object_ref (result);
2151 g_mutex_unlock (&priv->lock);
2157 * gst_rtsp_client_set_mount_points:
2158 * @client: a #GstRTSPClient
2159 * @mounts: a #GstRTSPMountPoints
2161 * Set @mounts as the mount points for @client which it will use to map urls
2162 * to media streams. These mount points are usually inherited from the server that
2163 * created the client but can be overriden later.
2166 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2167 GstRTSPMountPoints * mounts)
2169 GstRTSPClientPrivate *priv;
2170 GstRTSPMountPoints *old;
2172 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2174 priv = client->priv;
2177 g_object_ref (mounts);
2179 g_mutex_lock (&priv->lock);
2180 old = priv->mount_points;
2181 priv->mount_points = mounts;
2182 g_mutex_unlock (&priv->lock);
2185 g_object_unref (old);
2189 * gst_rtsp_client_get_mount_points:
2190 * @client: a #GstRTSPClient
2192 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2194 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2196 GstRTSPMountPoints *
2197 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2199 GstRTSPClientPrivate *priv;
2200 GstRTSPMountPoints *result;
2202 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2204 priv = client->priv;
2206 g_mutex_lock (&priv->lock);
2207 if ((result = priv->mount_points))
2208 g_object_ref (result);
2209 g_mutex_unlock (&priv->lock);
2215 * gst_rtsp_client_set_auth:
2216 * @client: a #GstRTSPClient
2217 * @auth: a #GstRTSPAuth
2219 * configure @auth to be used as the authentication manager of @client.
2222 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2224 GstRTSPClientPrivate *priv;
2227 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2229 priv = client->priv;
2232 g_object_ref (auth);
2234 g_mutex_lock (&priv->lock);
2237 g_mutex_unlock (&priv->lock);
2240 g_object_unref (old);
2245 * gst_rtsp_client_get_auth:
2246 * @client: a #GstRTSPClient
2248 * Get the #GstRTSPAuth used as the authentication manager of @client.
2250 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2254 gst_rtsp_client_get_auth (GstRTSPClient * client)
2256 GstRTSPClientPrivate *priv;
2257 GstRTSPAuth *result;
2259 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2261 priv = client->priv;
2263 g_mutex_lock (&priv->lock);
2264 if ((result = priv->auth))
2265 g_object_ref (result);
2266 g_mutex_unlock (&priv->lock);
2272 * gst_rtsp_client_set_thread_pool:
2273 * @client: a #GstRTSPClient
2274 * @pool: a #GstRTSPThreadPool
2276 * configure @pool to be used as the thread pool of @client.
2279 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2280 GstRTSPThreadPool * pool)
2282 GstRTSPClientPrivate *priv;
2283 GstRTSPThreadPool *old;
2285 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2287 priv = client->priv;
2290 g_object_ref (pool);
2292 g_mutex_lock (&priv->lock);
2293 old = priv->thread_pool;
2294 priv->thread_pool = pool;
2295 g_mutex_unlock (&priv->lock);
2298 g_object_unref (old);
2302 * gst_rtsp_client_get_thread_pool:
2303 * @client: a #GstRTSPClient
2305 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2307 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2311 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2313 GstRTSPClientPrivate *priv;
2314 GstRTSPThreadPool *result;
2316 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2318 priv = client->priv;
2320 g_mutex_lock (&priv->lock);
2321 if ((result = priv->thread_pool))
2322 g_object_ref (result);
2323 g_mutex_unlock (&priv->lock);
2329 * gst_rtsp_client_set_connection:
2330 * @client: a #GstRTSPClient
2331 * @conn: (transfer full): a #GstRTSPConnection
2333 * Set the #GstRTSPConnection of @client. This function takes ownership of
2336 * Returns: %TRUE on success.
2339 gst_rtsp_client_set_connection (GstRTSPClient * client,
2340 GstRTSPConnection * conn)
2342 GstRTSPClientPrivate *priv;
2343 GSocket *read_socket;
2344 GSocketAddress *address;
2346 GError *error = NULL;
2348 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2349 g_return_val_if_fail (conn != NULL, FALSE);
2351 priv = client->priv;
2353 read_socket = gst_rtsp_connection_get_read_socket (conn);
2355 if (!(address = g_socket_get_local_address (read_socket, &error)))
2358 g_free (priv->server_ip);
2359 /* keep the original ip that the client connected to */
2360 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2361 GInetAddress *iaddr;
2363 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2365 /* socket might be ipv6 but adress still ipv4 */
2366 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2367 priv->server_ip = g_inet_address_to_string (iaddr);
2368 g_object_unref (address);
2370 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2371 priv->server_ip = g_strdup ("unknown");
2374 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2375 priv->server_ip, priv->is_ipv6);
2377 url = gst_rtsp_connection_get_url (conn);
2378 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2380 priv->connection = conn;
2387 GST_ERROR ("could not get local address %s", error->message);
2388 g_error_free (error);
2394 * gst_rtsp_client_get_connection:
2395 * @client: a #GstRTSPClient
2397 * Get the #GstRTSPConnection of @client.
2399 * Returns: (transfer none): the #GstRTSPConnection of @client.
2400 * The connection object returned remains valid until the client is freed.
2403 gst_rtsp_client_get_connection (GstRTSPClient * client)
2405 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2407 return client->priv->connection;
2411 * gst_rtsp_client_set_send_func:
2412 * @client: a #GstRTSPClient
2413 * @func: a #GstRTSPClientSendFunc
2414 * @user_data: user data passed to @func
2415 * @notify: called when @user_data is no longer in use
2417 * Set @func as the callback that will be called when a new message needs to be
2418 * sent to the client. @user_data is passed to @func and @notify is called when
2419 * @user_data is no longer in use.
2421 * By default, the client will send the messages on the #GstRTSPConnection that
2422 * was configured with gst_rtsp_client_attach() was called.
2425 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2426 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2428 GstRTSPClientPrivate *priv;
2429 GDestroyNotify old_notify;
2432 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2434 priv = client->priv;
2436 g_mutex_lock (&priv->send_lock);
2437 priv->send_func = func;
2438 old_notify = priv->send_notify;
2439 old_data = priv->send_data;
2440 priv->send_notify = notify;
2441 priv->send_data = user_data;
2442 g_mutex_unlock (&priv->send_lock);
2445 old_notify (old_data);
2449 * gst_rtsp_client_handle_message:
2450 * @client: a #GstRTSPClient
2451 * @message: an #GstRTSPMessage
2453 * Let the client handle @message.
2455 * Returns: a #GstRTSPResult.
2458 gst_rtsp_client_handle_message (GstRTSPClient * client,
2459 GstRTSPMessage * message)
2461 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2462 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2464 switch (message->type) {
2465 case GST_RTSP_MESSAGE_REQUEST:
2466 handle_request (client, message);
2468 case GST_RTSP_MESSAGE_RESPONSE:
2469 handle_response (client, message);
2471 case GST_RTSP_MESSAGE_DATA:
2472 handle_data (client, message);
2481 * gst_rtsp_client_send_message:
2482 * @client: a #GstRTSPClient
2483 * @session: a #GstRTSPSession to send the message to or %NULL
2484 * @message: The #GstRTSPMessage to send
2486 * Send a message message to the remote end. @message must be a
2487 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
2490 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
2491 GstRTSPMessage * message)
2493 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2494 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2495 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
2496 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
2498 send_message (client, session, message, FALSE);
2503 static GstRTSPResult
2504 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2505 gboolean close, gpointer user_data)
2507 GstRTSPClientPrivate *priv = client->priv;
2509 /* send the response and store the seq number so we can wait until it's
2510 * written to the client to close the connection */
2511 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2512 &priv->close_seq : NULL);
2515 static GstRTSPResult
2516 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2519 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2522 static GstRTSPResult
2523 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2525 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2526 GstRTSPClientPrivate *priv = client->priv;
2528 if (priv->close_seq && priv->close_seq == cseq) {
2529 priv->close_seq = 0;
2530 close_connection (client);
2536 static GstRTSPResult
2537 closed (GstRTSPWatch * watch, gpointer user_data)
2539 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2540 GstRTSPClientPrivate *priv = client->priv;
2541 const gchar *tunnelid;
2543 GST_INFO ("client %p: connection closed", client);
2545 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2546 g_mutex_lock (&tunnels_lock);
2547 /* remove from tunnelids */
2548 g_hash_table_remove (tunnels, tunnelid);
2549 g_mutex_unlock (&tunnels_lock);
2552 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2557 static GstRTSPResult
2558 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2560 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2563 str = gst_rtsp_strresult (result);
2564 GST_INFO ("client %p: received an error %s", client, str);
2570 static GstRTSPResult
2571 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2572 GstRTSPMessage * message, guint id, gpointer user_data)
2574 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2577 str = gst_rtsp_strresult (result);
2579 ("client %p: error when handling message %p with id %d: %s",
2580 client, message, id, str);
2587 remember_tunnel (GstRTSPClient * client)
2589 GstRTSPClientPrivate *priv = client->priv;
2590 const gchar *tunnelid;
2592 /* store client in the pending tunnels */
2593 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2594 if (tunnelid == NULL)
2597 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2599 /* we can't have two clients connecting with the same tunnelid */
2600 g_mutex_lock (&tunnels_lock);
2601 if (g_hash_table_lookup (tunnels, tunnelid))
2602 goto tunnel_existed;
2604 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2605 g_mutex_unlock (&tunnels_lock);
2612 GST_ERROR ("client %p: no tunnelid provided", client);
2617 g_mutex_unlock (&tunnels_lock);
2618 GST_ERROR ("client %p: tunnel session %s already existed", client,
2624 static GstRTSPStatusCode
2625 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2627 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2628 GstRTSPClientPrivate *priv = client->priv;
2630 GST_INFO ("client %p: tunnel start (connection %p)", client,
2633 if (!remember_tunnel (client))
2636 return GST_RTSP_STS_OK;
2641 GST_ERROR ("client %p: error starting tunnel", client);
2642 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2646 static GstRTSPResult
2647 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2649 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2650 GstRTSPClientPrivate *priv = client->priv;
2652 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2655 /* ignore error, it'll only be a problem when the client does a POST again */
2656 remember_tunnel (client);
2661 static GstRTSPResult
2662 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2664 const gchar *tunnelid;
2665 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2666 GstRTSPClientPrivate *priv = client->priv;
2667 GstRTSPClient *oclient;
2668 GstRTSPClientPrivate *opriv;
2670 GST_INFO ("client %p: tunnel complete", client);
2672 /* find previous tunnel */
2673 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2674 if (tunnelid == NULL)
2677 g_mutex_lock (&tunnels_lock);
2678 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2681 /* remove the old client from the table. ref before because removing it will
2682 * remove the ref to it. */
2683 g_object_ref (oclient);
2684 g_hash_table_remove (tunnels, tunnelid);
2686 opriv = oclient->priv;
2688 if (opriv->watch == NULL)
2690 g_mutex_unlock (&tunnels_lock);
2692 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2693 opriv->connection, priv->connection);
2695 /* merge the tunnels into the first client */
2696 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2697 gst_rtsp_watch_reset (opriv->watch);
2698 g_object_unref (oclient);
2705 GST_ERROR ("client %p: no tunnelid provided", client);
2706 return GST_RTSP_ERROR;
2710 g_mutex_unlock (&tunnels_lock);
2711 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2712 return GST_RTSP_ERROR;
2716 g_mutex_unlock (&tunnels_lock);
2717 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2718 g_object_unref (oclient);
2719 return GST_RTSP_ERROR;
2723 static GstRTSPWatchFuncs watch_funcs = {
2735 client_watch_notify (GstRTSPClient * client)
2737 GstRTSPClientPrivate *priv = client->priv;
2739 GST_INFO ("client %p: watch destroyed", client);
2741 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2742 g_object_unref (client);
2746 * gst_rtsp_client_attach:
2747 * @client: a #GstRTSPClient
2748 * @context: (allow-none): a #GMainContext
2750 * Attaches @client to @context. When the mainloop for @context is run, the
2751 * client will be dispatched. When @context is NULL, the default context will be
2754 * This function should be called when the client properties and urls are fully
2755 * configured and the client is ready to start.
2757 * Returns: the ID (greater than 0) for the source within the GMainContext.
2760 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2762 GstRTSPClientPrivate *priv;
2765 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2766 priv = client->priv;
2767 g_return_val_if_fail (priv->connection != NULL, 0);
2768 g_return_val_if_fail (priv->watch == NULL, 0);
2770 /* create watch for the connection and attach */
2771 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2772 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2773 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2774 (GDestroyNotify) gst_rtsp_watch_unref);
2776 /* FIXME make this configurable. We don't want to do this yet because it will
2777 * be superceeded by a cache object later */
2778 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2780 GST_INFO ("attaching to context %p", context);
2781 res = gst_rtsp_watch_attach (priv->watch, context);
2787 * gst_rtsp_client_session_filter:
2788 * @client: a #GstRTSPClient
2789 * @func: (scope call): a callback
2790 * @user_data: user data passed to @func
2792 * Call @func for each session managed by @client. The result value of @func
2793 * determines what happens to the session. @func will be called with @client
2794 * locked so no further actions on @client can be performed from @func.
2796 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
2799 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
2801 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
2802 * will also be added with an additional ref to the result #GList of this
2805 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
2806 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2807 * element in the #GList should be unreffed before the list is freed.
2810 gst_rtsp_client_session_filter (GstRTSPClient * client,
2811 GstRTSPClientSessionFilterFunc func, gpointer user_data)
2813 GstRTSPClientPrivate *priv;
2814 GList *result, *walk, *next;
2816 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2817 g_return_val_if_fail (func != NULL, NULL);
2819 priv = client->priv;
2823 g_mutex_lock (&priv->lock);
2824 for (walk = priv->sessions; walk; walk = next) {
2825 GstRTSPSession *sess = walk->data;
2827 next = g_list_next (walk);
2829 switch (func (client, sess, user_data)) {
2830 case GST_RTSP_FILTER_REMOVE:
2831 /* stop watching the session and pretent it went away */
2832 client_cleanup_session (client, sess);
2834 case GST_RTSP_FILTER_REF:
2835 result = g_list_prepend (result, g_object_ref (sess));
2837 case GST_RTSP_FILTER_KEEP:
2842 g_mutex_unlock (&priv->lock);