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_media (GstRTSPClient * client,
131 GstRTSPMedia * media, GstRTSPStream * stream, GstRTSPContext * ctx);
132 static gboolean default_configure_client_transport (GstRTSPClient * client,
133 GstRTSPContext * ctx, GstRTSPTransport * ct);
134 static GstRTSPResult default_params_set (GstRTSPClient * client,
135 GstRTSPContext * ctx);
136 static GstRTSPResult default_params_get (GstRTSPClient * client,
137 GstRTSPContext * ctx);
138 static gchar *default_make_path_from_uri (GstRTSPClient * client,
139 const GstRTSPUrl * uri);
141 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
144 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
146 GObjectClass *gobject_class;
148 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
150 gobject_class = G_OBJECT_CLASS (klass);
152 gobject_class->get_property = gst_rtsp_client_get_property;
153 gobject_class->set_property = gst_rtsp_client_set_property;
154 gobject_class->finalize = gst_rtsp_client_finalize;
156 klass->create_sdp = create_sdp;
157 klass->configure_client_media = default_configure_client_media;
158 klass->configure_client_transport = default_configure_client_transport;
159 klass->params_set = default_params_set;
160 klass->params_get = default_params_get;
161 klass->make_path_from_uri = default_make_path_from_uri;
163 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
164 g_param_spec_object ("session-pool", "Session Pool",
165 "The session pool to use for client session",
166 GST_TYPE_RTSP_SESSION_POOL,
167 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
169 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
170 g_param_spec_object ("mount-points", "Mount Points",
171 "The mount points to use for client session",
172 GST_TYPE_RTSP_MOUNT_POINTS,
173 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
175 gst_rtsp_client_signals[SIGNAL_CLOSED] =
176 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
177 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
178 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
180 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
181 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
182 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
183 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
185 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
186 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
187 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
188 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
191 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
192 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
193 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
194 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
197 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
198 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
199 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
200 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
203 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
204 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
205 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
206 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
209 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
210 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
211 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
212 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
215 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
216 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
217 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
218 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
221 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
222 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
223 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
224 set_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
225 G_TYPE_NONE, 1, G_TYPE_POINTER);
227 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
228 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
229 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
230 get_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
231 G_TYPE_NONE, 1, G_TYPE_POINTER);
233 gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE] =
234 g_signal_new ("handle-response", G_TYPE_FROM_CLASS (klass),
235 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
236 handle_response), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
237 G_TYPE_NONE, 1, G_TYPE_POINTER);
240 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
241 g_mutex_init (&tunnels_lock);
243 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
247 gst_rtsp_client_init (GstRTSPClient * client)
249 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
253 g_mutex_init (&priv->lock);
254 g_mutex_init (&priv->send_lock);
258 static GstRTSPFilterResult
259 filter_session (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
262 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
264 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
265 unlink_session_transports (client, sess, sessmedia);
267 /* unmanage the media in the session */
268 return GST_RTSP_FILTER_REMOVE;
272 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
274 /* unlink all media managed in this session */
275 gst_rtsp_session_filter (session, filter_session, client);
279 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
281 GstRTSPClientPrivate *priv = client->priv;
284 for (walk = priv->sessions; walk; walk = g_list_next (walk)) {
285 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
287 /* we already know about this session */
288 if (msession == session)
292 GST_INFO ("watching session %p", session);
294 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
296 priv->sessions = g_list_prepend (priv->sessions, session);
300 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session)
302 GstRTSPClientPrivate *priv = client->priv;
304 GST_INFO ("unwatching session %p", session);
306 g_object_weak_unref (G_OBJECT (session),
307 (GWeakNotify) client_session_finalized, client);
308 priv->sessions = g_list_remove (priv->sessions, session);
312 client_cleanup_session (GstRTSPClient * client, GstRTSPSession * session)
314 g_object_weak_unref (G_OBJECT (session),
315 (GWeakNotify) client_session_finalized, client);
316 client_unlink_session (client, session);
320 client_cleanup_sessions (GstRTSPClient * client)
322 GstRTSPClientPrivate *priv = client->priv;
325 /* remove weak-ref from sessions */
326 for (sessions = priv->sessions; sessions; sessions = g_list_next (sessions)) {
327 client_cleanup_session (client, (GstRTSPSession *) sessions->data);
329 g_list_free (priv->sessions);
330 priv->sessions = NULL;
333 /* A client is finalized when the connection is broken */
335 gst_rtsp_client_finalize (GObject * obj)
337 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
338 GstRTSPClientPrivate *priv = client->priv;
340 GST_INFO ("finalize client %p", client);
342 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
345 g_source_destroy ((GSource *) priv->watch);
347 client_cleanup_sessions (client);
349 if (priv->connection)
350 gst_rtsp_connection_free (priv->connection);
351 if (priv->session_pool)
352 g_object_unref (priv->session_pool);
353 if (priv->mount_points)
354 g_object_unref (priv->mount_points);
356 g_object_unref (priv->auth);
357 if (priv->thread_pool)
358 g_object_unref (priv->thread_pool);
363 gst_rtsp_media_unprepare (priv->media);
364 g_object_unref (priv->media);
367 g_free (priv->server_ip);
368 g_mutex_clear (&priv->lock);
369 g_mutex_clear (&priv->send_lock);
371 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
375 gst_rtsp_client_get_property (GObject * object, guint propid,
376 GValue * value, GParamSpec * pspec)
378 GstRTSPClient *client = GST_RTSP_CLIENT (object);
381 case PROP_SESSION_POOL:
382 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
384 case PROP_MOUNT_POINTS:
385 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
388 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
393 gst_rtsp_client_set_property (GObject * object, guint propid,
394 const GValue * value, GParamSpec * pspec)
396 GstRTSPClient *client = GST_RTSP_CLIENT (object);
399 case PROP_SESSION_POOL:
400 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
402 case PROP_MOUNT_POINTS:
403 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
406 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
411 * gst_rtsp_client_new:
413 * Create a new #GstRTSPClient instance.
415 * Returns: a new #GstRTSPClient
418 gst_rtsp_client_new (void)
420 GstRTSPClient *result;
422 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
428 send_message (GstRTSPClient * client, GstRTSPSession * session,
429 GstRTSPMessage * message, gboolean close)
431 GstRTSPClientPrivate *priv = client->priv;
433 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
434 "GStreamer RTSP server");
436 /* remove any previous header */
437 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
439 /* add the new session header for new session ids */
441 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
442 gst_rtsp_session_get_header (session));
445 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
446 gst_rtsp_message_dump (message);
450 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
452 g_mutex_lock (&priv->send_lock);
454 priv->send_func (client, message, close, priv->send_data);
455 g_mutex_unlock (&priv->send_lock);
457 gst_rtsp_message_unset (message);
461 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
462 GstRTSPContext * ctx)
464 gst_rtsp_message_init_response (ctx->response, code,
465 gst_rtsp_status_as_text (code), ctx->request);
467 send_message (client, NULL, ctx->response, FALSE);
471 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
473 if (path1 == NULL || path2 == NULL)
476 if (strlen (path1) != len2)
479 if (strncmp (path1, path2, len2))
485 /* this function is called to initially find the media for the DESCRIBE request
486 * but is cached for when the same client (without breaking the connection) is
487 * doing a setup for the exact same url. */
488 static GstRTSPMedia *
489 find_media (GstRTSPClient * client, GstRTSPContext * ctx, gchar * path,
492 GstRTSPClientPrivate *priv = client->priv;
493 GstRTSPMediaFactory *factory;
497 /* find the longest matching factory for the uri first */
498 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
502 ctx->factory = factory;
504 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
505 goto no_factory_access;
507 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
513 path_len = strlen (path);
515 if (!paths_are_equal (priv->path, path, path_len)) {
516 GstRTSPThread *thread;
518 /* remove any previously cached values before we try to construct a new
524 gst_rtsp_media_unprepare (priv->media);
525 g_object_unref (priv->media);
529 /* prepare the media and add it to the pipeline */
530 if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
535 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
536 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
540 /* prepare the media */
541 if (!(gst_rtsp_media_prepare (media, thread)))
544 /* now keep track of the uri and the media */
545 priv->path = g_strndup (path, path_len);
548 /* we have seen this path before, used cached media */
551 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
554 g_object_unref (factory);
558 g_object_ref (media);
565 GST_ERROR ("client %p: no factory for path %s", client, path);
566 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
571 GST_ERROR ("client %p: not authorized to see factory path %s", client,
573 /* error reply is already sent */
578 GST_ERROR ("client %p: not authorized for factory path %s", client, path);
579 /* error reply is already sent */
584 GST_ERROR ("client %p: can't create media", client);
585 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
586 g_object_unref (factory);
592 GST_ERROR ("client %p: can't create thread", client);
593 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
594 g_object_unref (media);
596 g_object_unref (factory);
602 GST_ERROR ("client %p: can't prepare media", client);
603 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
604 g_object_unref (media);
606 g_object_unref (factory);
613 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
615 GstRTSPClientPrivate *priv = client->priv;
616 GstRTSPMessage message = { 0 };
621 gst_rtsp_message_init_data (&message, channel);
623 /* FIXME, need some sort of iovec RTSPMessage here */
624 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
627 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
629 g_mutex_lock (&priv->send_lock);
631 priv->send_func (client, &message, FALSE, priv->send_data);
632 g_mutex_unlock (&priv->send_lock);
634 gst_rtsp_message_steal_body (&message, &data, &usize);
635 gst_buffer_unmap (buffer, &map_info);
637 gst_rtsp_message_unset (&message);
643 link_transport (GstRTSPClient * client, GstRTSPSession * session,
644 GstRTSPStreamTransport * trans)
646 GstRTSPClientPrivate *priv = client->priv;
648 GST_DEBUG ("client %p: linking transport %p", client, trans);
650 gst_rtsp_stream_transport_set_callbacks (trans,
651 (GstRTSPSendFunc) do_send_data,
652 (GstRTSPSendFunc) do_send_data, client, NULL);
654 priv->transports = g_list_prepend (priv->transports, trans);
656 /* make sure our session can't expire */
657 gst_rtsp_session_prevent_expire (session);
661 link_session_transports (GstRTSPClient * client, GstRTSPSession * session,
662 GstRTSPSessionMedia * sessmedia)
667 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
668 for (i = 0; i < n_streams; i++) {
669 GstRTSPStreamTransport *trans;
670 const GstRTSPTransport *tr;
672 /* get the transport, if there is no transport configured, skip this stream */
673 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
677 tr = gst_rtsp_stream_transport_get_transport (trans);
679 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
680 /* for TCP, link the stream to the TCP connection of the client */
681 link_transport (client, session, trans);
687 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
688 GstRTSPStreamTransport * trans)
690 GstRTSPClientPrivate *priv = client->priv;
692 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
694 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
696 priv->transports = g_list_remove (priv->transports, trans);
698 /* our session can now expire */
699 gst_rtsp_session_allow_expire (session);
703 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
704 GstRTSPSessionMedia * sessmedia)
709 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
710 for (i = 0; i < n_streams; i++) {
711 GstRTSPStreamTransport *trans;
712 const GstRTSPTransport *tr;
714 /* get the transport, if there is no transport configured, skip this stream */
715 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
719 tr = gst_rtsp_stream_transport_get_transport (trans);
721 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
722 /* for TCP, unlink the stream from the TCP connection of the client */
723 unlink_transport (client, session, trans);
729 close_connection (GstRTSPClient * client)
731 GstRTSPClientPrivate *priv = client->priv;
732 const gchar *tunnelid;
734 GST_DEBUG ("client %p: closing connection", client);
736 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
737 g_mutex_lock (&tunnels_lock);
738 /* remove from tunnelids */
739 g_hash_table_remove (tunnels, tunnelid);
740 g_mutex_unlock (&tunnels_lock);
743 gst_rtsp_connection_close (priv->connection);
747 default_make_path_from_uri (GstRTSPClient * client, const GstRTSPUrl * uri)
752 path = g_strconcat (uri->abspath, "?", uri->query, NULL);
754 path = g_strdup (uri->abspath);
760 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
762 GstRTSPClientPrivate *priv = client->priv;
763 GstRTSPClientClass *klass;
764 GstRTSPSession *session;
765 GstRTSPSessionMedia *sessmedia;
766 GstRTSPStatusCode code;
773 session = ctx->session;
778 klass = GST_RTSP_CLIENT_GET_CLASS (client);
779 path = klass->make_path_from_uri (client, ctx->uri);
781 /* get a handle to the configuration of the media in the session */
782 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
786 /* only aggregate control for now.. */
787 if (path[matched] != '\0')
792 ctx->sessmedia = sessmedia;
794 /* we emit the signal before closing the connection */
795 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
798 /* unlink the all TCP callbacks */
799 unlink_session_transports (client, session, sessmedia);
801 /* remove the session from the watched sessions */
802 client_unwatch_session (client, session);
804 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
806 /* unmanage the media in the session, returns false if all media session
808 if (!gst_rtsp_session_release_media (session, sessmedia)) {
809 /* remove the session */
810 gst_rtsp_session_pool_remove (priv->session_pool, session);
812 /* construct the response now */
813 code = GST_RTSP_STS_OK;
814 gst_rtsp_message_init_response (ctx->response, code,
815 gst_rtsp_status_as_text (code), ctx->request);
817 send_message (client, session, ctx->response, TRUE);
824 GST_ERROR ("client %p: no session", client);
825 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
830 GST_ERROR ("client %p: no uri supplied", client);
831 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
836 GST_ERROR ("client %p: no media for uri", client);
837 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
843 GST_ERROR ("client %p: no aggregate path %s", client, path);
844 send_generic_response (client,
845 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
852 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
856 res = gst_rtsp_params_set (client, ctx);
862 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
866 res = gst_rtsp_params_get (client, ctx);
872 handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
878 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
879 if (res != GST_RTSP_OK)
883 /* no body, keep-alive request */
884 send_generic_response (client, GST_RTSP_STS_OK, ctx);
886 /* there is a body, handle the params */
887 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
888 if (res != GST_RTSP_OK)
891 send_message (client, ctx->session, ctx->response, FALSE);
894 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
902 GST_ERROR ("client %p: bad request", client);
903 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
909 handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
915 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
916 if (res != GST_RTSP_OK)
920 /* no body, keep-alive request */
921 send_generic_response (client, GST_RTSP_STS_OK, ctx);
923 /* there is a body, handle the params */
924 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
925 if (res != GST_RTSP_OK)
928 send_message (client, ctx->session, ctx->response, FALSE);
931 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
939 GST_ERROR ("client %p: bad request", client);
940 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
946 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
948 GstRTSPSession *session;
949 GstRTSPClientClass *klass;
950 GstRTSPSessionMedia *sessmedia;
951 GstRTSPStatusCode code;
952 GstRTSPState rtspstate;
956 if (!(session = ctx->session))
962 klass = GST_RTSP_CLIENT_GET_CLASS (client);
963 path = klass->make_path_from_uri (client, ctx->uri);
965 /* get a handle to the configuration of the media in the session */
966 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
970 if (path[matched] != '\0')
975 ctx->sessmedia = sessmedia;
977 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
978 /* the session state must be playing or recording */
979 if (rtspstate != GST_RTSP_STATE_PLAYING &&
980 rtspstate != GST_RTSP_STATE_RECORDING)
983 /* unlink the all TCP callbacks */
984 unlink_session_transports (client, session, sessmedia);
986 /* then pause sending */
987 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
989 /* construct the response now */
990 code = GST_RTSP_STS_OK;
991 gst_rtsp_message_init_response (ctx->response, code,
992 gst_rtsp_status_as_text (code), ctx->request);
994 send_message (client, session, ctx->response, FALSE);
996 /* the state is now READY */
997 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
999 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
1006 GST_ERROR ("client %p: no seesion", client);
1007 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1012 GST_ERROR ("client %p: no uri supplied", client);
1013 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1018 GST_ERROR ("client %p: no media for uri", client);
1019 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1025 GST_ERROR ("client %p: no aggregate path %s", client, path);
1026 send_generic_response (client,
1027 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1033 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
1034 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1040 /* convert @url and @path to a URL used as a content base for the factory
1041 * located at @path */
1043 make_base_url (GstRTSPClient * client, GstRTSPUrl * url, gchar * path)
1046 gchar *result, *trail;
1048 /* check for trailing '/' and append one */
1049 trail = (path[strlen (path) - 1] != '/' ? "/" : "");
1054 tmp.abspath = g_strdup_printf ("%s%s", path, trail);
1056 result = gst_rtsp_url_get_request_uri (&tmp);
1057 g_free (tmp.abspath);
1063 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
1065 GstRTSPSession *session;
1066 GstRTSPClientClass *klass;
1067 GstRTSPSessionMedia *sessmedia;
1068 GstRTSPMedia *media;
1069 GstRTSPStatusCode code;
1072 GstRTSPTimeRange *range;
1074 GstRTSPState rtspstate;
1075 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1076 gchar *path, *rtpinfo;
1079 if (!(session = ctx->session))
1082 if (!(uri = ctx->uri))
1085 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1086 path = klass->make_path_from_uri (client, uri);
1088 /* get a handle to the configuration of the media in the session */
1089 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1093 if (path[matched] != '\0')
1098 ctx->sessmedia = sessmedia;
1099 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1101 /* the session state must be playing or ready */
1102 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1103 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1106 /* in play we first unsuspend, media could be suspended from SDP or PAUSED */
1107 if (!gst_rtsp_media_unsuspend (media))
1108 goto unsuspend_failed;
1110 /* parse the range header if we have one */
1111 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1112 if (res == GST_RTSP_OK) {
1113 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1114 /* we have a range, seek to the position */
1116 gst_rtsp_media_seek (media, range);
1117 gst_rtsp_range_free (range);
1121 /* link the all TCP callbacks */
1122 link_session_transports (client, session, sessmedia);
1124 /* grab RTPInfo from the media now */
1125 rtpinfo = gst_rtsp_session_media_get_rtpinfo (sessmedia);
1127 /* construct the response now */
1128 code = GST_RTSP_STS_OK;
1129 gst_rtsp_message_init_response (ctx->response, code,
1130 gst_rtsp_status_as_text (code), ctx->request);
1132 /* add the RTP-Info header */
1134 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO,
1138 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1140 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1142 send_message (client, session, ctx->response, FALSE);
1144 /* start playing after sending the request */
1145 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1147 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1149 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1156 GST_ERROR ("client %p: no session", client);
1157 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1162 GST_ERROR ("client %p: no uri supplied", client);
1163 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1168 GST_ERROR ("client %p: media not found", client);
1169 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1174 GST_ERROR ("client %p: no aggregate path %s", client, path);
1175 send_generic_response (client,
1176 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1182 GST_ERROR ("client %p: not PLAYING or READY", client);
1183 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1189 GST_ERROR ("client %p: unsuspend failed", client);
1190 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1196 do_keepalive (GstRTSPSession * session)
1198 GST_INFO ("keep session %p alive", session);
1199 gst_rtsp_session_touch (session);
1202 /* parse @transport and return a valid transport in @tr. only transports
1203 * from @supported are returned. Returns FALSE if no valid transport
1206 parse_transport (const char *transport, GstRTSPLowerTrans supported,
1207 GstRTSPTransport * tr)
1214 gst_rtsp_transport_init (tr);
1216 GST_DEBUG ("parsing transports %s", transport);
1218 transports = g_strsplit (transport, ",", 0);
1220 /* loop through the transports, try to parse */
1221 for (i = 0; transports[i]; i++) {
1222 res = gst_rtsp_transport_parse (transports[i], tr);
1223 if (res != GST_RTSP_OK) {
1224 /* no valid transport, search some more */
1225 GST_WARNING ("could not parse transport %s", transports[i]);
1229 /* we have a transport, see if it's RTP/AVP */
1230 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1231 GST_WARNING ("invalid transport %s", transports[i]);
1235 if (!(tr->lower_transport & supported)) {
1236 GST_WARNING ("unsupported transport %s", transports[i]);
1240 /* we have a valid transport */
1241 GST_INFO ("found valid transport %s", transports[i]);
1246 gst_rtsp_transport_init (tr);
1248 g_strfreev (transports);
1254 default_configure_client_media (GstRTSPClient * client, GstRTSPMedia * media,
1255 GstRTSPStream * stream, GstRTSPContext * ctx)
1257 GstRTSPMessage *request = ctx->request;
1258 gchar *blocksize_str;
1260 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1261 &blocksize_str, 0) == GST_RTSP_OK) {
1265 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1266 if (end == blocksize_str)
1269 /* we don't want to change the mtu when this media
1270 * can be shared because it impacts other clients */
1271 if (gst_rtsp_media_is_shared (media))
1274 if (blocksize > G_MAXUINT)
1275 blocksize = G_MAXUINT;
1277 gst_rtsp_stream_set_mtu (stream, blocksize);
1285 GST_ERROR_OBJECT (client, "failed to parse blocksize");
1286 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1292 default_configure_client_transport (GstRTSPClient * client,
1293 GstRTSPContext * ctx, GstRTSPTransport * ct)
1295 GstRTSPClientPrivate *priv = client->priv;
1297 /* we have a valid transport now, set the destination of the client. */
1298 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1299 gboolean use_client_settings;
1301 use_client_settings =
1302 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1304 if (ct->destination && use_client_settings) {
1305 GstRTSPAddress *addr;
1307 addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1308 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1313 gst_rtsp_address_free (addr);
1315 GstRTSPAddress *addr;
1316 GSocketFamily family;
1318 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1320 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1324 g_free (ct->destination);
1325 ct->destination = g_strdup (addr->address);
1326 ct->port.min = addr->port;
1327 ct->port.max = addr->port + addr->n_ports - 1;
1328 ct->ttl = addr->ttl;
1330 gst_rtsp_address_free (addr);
1335 url = gst_rtsp_connection_get_url (priv->connection);
1336 g_free (ct->destination);
1337 ct->destination = g_strdup (url->host);
1339 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1340 /* check if the client selected channels for TCP */
1341 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1342 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1352 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1357 static GstRTSPTransport *
1358 make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
1359 GstRTSPTransport * ct)
1361 GstRTSPTransport *st;
1363 GSocketFamily family;
1365 /* prepare the server transport */
1366 gst_rtsp_transport_new (&st);
1368 st->trans = ct->trans;
1369 st->profile = ct->profile;
1370 st->lower_transport = ct->lower_transport;
1372 addr = g_inet_address_new_from_string (ct->destination);
1375 GST_ERROR ("failed to get inet addr from client destination");
1376 family = G_SOCKET_FAMILY_IPV4;
1378 family = g_inet_address_get_family (addr);
1379 g_object_unref (addr);
1383 switch (st->lower_transport) {
1384 case GST_RTSP_LOWER_TRANS_UDP:
1385 st->client_port = ct->client_port;
1386 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
1388 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1389 st->port = ct->port;
1390 st->destination = g_strdup (ct->destination);
1393 case GST_RTSP_LOWER_TRANS_TCP:
1394 st->interleaved = ct->interleaved;
1399 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
1405 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
1407 GstRTSPClientPrivate *priv = client->priv;
1411 GstRTSPTransport *ct, *st;
1412 GstRTSPLowerTrans supported;
1413 GstRTSPStatusCode code;
1414 GstRTSPSession *session;
1415 GstRTSPStreamTransport *trans;
1417 GstRTSPSessionMedia *sessmedia;
1418 GstRTSPMedia *media;
1419 GstRTSPStream *stream;
1420 GstRTSPState rtspstate;
1421 GstRTSPClientClass *klass;
1422 gchar *path, *control;
1429 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1430 path = klass->make_path_from_uri (client, uri);
1432 /* parse the transport */
1434 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
1436 if (res != GST_RTSP_OK)
1439 /* we create the session after parsing stuff so that we don't make
1440 * a session for malformed requests */
1441 if (priv->session_pool == NULL)
1444 session = ctx->session;
1447 g_object_ref (session);
1448 /* get a handle to the configuration of the media in the session, this can
1449 * return NULL if this is a new url to manage in this session. */
1450 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1452 /* we need a new media configuration in this session */
1456 /* we have no session media, find one and manage it */
1457 if (sessmedia == NULL) {
1458 /* get a handle to the configuration of the media in the session */
1459 media = find_media (client, ctx, path, &matched);
1461 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1462 g_object_ref (media);
1464 goto media_not_found;
1466 /* no media, not found then */
1468 goto media_not_found_no_reply;
1470 if (path[matched] == '\0')
1471 goto control_not_found;
1473 /* path is what matched. */
1474 path[matched] = '\0';
1475 /* control is remainder */
1476 control = &path[matched + 1];
1478 /* find the stream now using the control part */
1479 stream = gst_rtsp_media_find_stream (media, control);
1481 goto stream_not_found;
1483 /* now we have a uri identifying a valid media and stream */
1484 ctx->stream = stream;
1487 if (session == NULL) {
1488 /* create a session if this fails we probably reached our session limit or
1490 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1491 goto service_unavailable;
1493 /* make sure this client is closed when the session is closed */
1494 client_watch_session (client, session);
1496 /* signal new session */
1497 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1500 ctx->session = session;
1503 if (sessmedia == NULL) {
1504 /* manage the media in our session now, if not done already */
1505 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1506 /* if we stil have no media, error */
1507 if (sessmedia == NULL)
1508 goto sessmedia_unavailable;
1510 g_object_unref (media);
1513 ctx->sessmedia = sessmedia;
1515 if (!klass->configure_client_media (client, media, stream, ctx))
1516 goto configure_media_failed_no_reply;
1518 gst_rtsp_transport_new (&ct);
1520 /* our supported transports */
1521 supported = gst_rtsp_stream_get_protocols (stream);
1523 /* parse and find a usable supported transport */
1524 if (!parse_transport (transport, supported, ct))
1525 goto unsupported_transports;
1527 /* update the client transport */
1528 if (!klass->configure_client_transport (client, ctx, ct))
1529 goto unsupported_client_transport;
1531 /* set in the session media transport */
1532 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1534 /* configure the url used to set this transport, this we will use when
1535 * generating the response for the PLAY request */
1536 gst_rtsp_stream_transport_set_url (trans, uri);
1538 /* configure keepalive for this transport */
1539 gst_rtsp_stream_transport_set_keepalive (trans,
1540 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1542 /* create and serialize the server transport */
1543 st = make_server_transport (client, ctx, ct);
1544 trans_str = gst_rtsp_transport_as_text (st);
1545 gst_rtsp_transport_free (st);
1547 /* construct the response now */
1548 code = GST_RTSP_STS_OK;
1549 gst_rtsp_message_init_response (ctx->response, code,
1550 gst_rtsp_status_as_text (code), ctx->request);
1552 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
1556 send_message (client, session, ctx->response, FALSE);
1558 /* update the state */
1559 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1560 switch (rtspstate) {
1561 case GST_RTSP_STATE_PLAYING:
1562 case GST_RTSP_STATE_RECORDING:
1563 case GST_RTSP_STATE_READY:
1564 /* no state change */
1567 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1570 g_object_unref (session);
1573 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
1580 GST_ERROR ("client %p: no uri", client);
1581 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1586 GST_ERROR ("client %p: no transport", client);
1587 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1593 GST_ERROR ("client %p: no session pool configured", client);
1594 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1598 media_not_found_no_reply:
1600 GST_ERROR ("client %p: media '%s' not found", client, path);
1602 /* error reply is already sent */
1607 GST_ERROR ("client %p: media '%s' not found", client, path);
1608 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1614 GST_ERROR ("client %p: no control in path '%s'", client, path);
1615 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1616 g_object_unref (media);
1622 GST_ERROR ("client %p: stream '%s' not found", client, control);
1623 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1624 g_object_unref (media);
1628 service_unavailable:
1630 GST_ERROR ("client %p: can't create session", client);
1631 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1632 g_object_unref (media);
1636 sessmedia_unavailable:
1638 GST_ERROR ("client %p: can't create session media", client);
1639 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1640 g_object_unref (media);
1641 g_object_unref (session);
1645 configure_media_failed_no_reply:
1647 GST_ERROR ("client %p: configure_media failed", client);
1648 g_object_unref (session);
1650 /* error reply is already sent */
1653 unsupported_transports:
1655 GST_ERROR ("client %p: unsupported transports", client);
1656 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1657 gst_rtsp_transport_free (ct);
1658 g_object_unref (session);
1662 unsupported_client_transport:
1664 GST_ERROR ("client %p: unsupported client transport", client);
1665 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1666 gst_rtsp_transport_free (ct);
1667 g_object_unref (session);
1673 static GstSDPMessage *
1674 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1676 GstRTSPClientPrivate *priv = client->priv;
1681 gst_sdp_message_new (&sdp);
1683 /* some standard things first */
1684 gst_sdp_message_set_version (sdp, "0");
1691 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1694 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1695 gst_sdp_message_set_information (sdp, "rtsp-server");
1696 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1697 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1698 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1699 gst_sdp_message_add_attribute (sdp, "control", "*");
1701 info.is_ipv6 = priv->is_ipv6;
1702 info.server_ip = priv->server_ip;
1704 /* create an SDP for the media object */
1705 if (!gst_rtsp_media_setup_sdp (media, sdp, &info))
1713 GST_ERROR ("client %p: could not create SDP", client);
1714 gst_sdp_message_free (sdp);
1719 /* for the describe we must generate an SDP */
1721 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
1723 GstRTSPClientPrivate *priv = client->priv;
1728 GstRTSPMedia *media;
1729 GstRTSPClientClass *klass;
1731 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1736 /* check what kind of format is accepted, we don't really do anything with it
1737 * and always return SDP for now. */
1742 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
1744 if (res == GST_RTSP_ENOTIMPL)
1747 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1751 if (!priv->mount_points)
1752 goto no_mount_points;
1754 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
1757 /* find the media object for the uri */
1758 if (!(media = find_media (client, ctx, path, NULL)))
1761 /* create an SDP for the media object on this client */
1762 if (!(sdp = klass->create_sdp (client, media)))
1765 /* we suspend after the describe */
1766 gst_rtsp_media_suspend (media);
1767 g_object_unref (media);
1769 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
1770 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
1772 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
1775 /* content base for some clients that might screw up creating the setup uri */
1776 str = make_base_url (client, ctx->uri, path);
1779 GST_INFO ("adding content-base: %s", str);
1780 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE, str);
1782 /* add SDP to the response body */
1783 str = gst_sdp_message_as_text (sdp);
1784 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
1785 gst_sdp_message_free (sdp);
1787 send_message (client, ctx->session, ctx->response, FALSE);
1789 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1797 GST_ERROR ("client %p: no uri", client);
1798 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1803 GST_ERROR ("client %p: no mount points configured", client);
1804 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1809 GST_ERROR ("client %p: can't find path for url", client);
1810 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1815 GST_ERROR ("client %p: no media", client);
1817 /* error reply is already sent */
1822 GST_ERROR ("client %p: can't create SDP", client);
1823 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1825 g_object_unref (media);
1831 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
1833 GstRTSPMethod options;
1836 options = GST_RTSP_DESCRIBE |
1841 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1843 str = gst_rtsp_options_as_text (options);
1845 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
1846 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
1848 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
1851 send_message (client, ctx->session, ctx->response, FALSE);
1853 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1859 /* remove duplicate and trailing '/' */
1861 sanitize_uri (GstRTSPUrl * uri)
1865 gboolean have_slash, prev_slash;
1867 s = d = uri->abspath;
1868 len = strlen (uri->abspath);
1872 for (i = 0; i < len; i++) {
1873 have_slash = s[i] == '/';
1875 if (!have_slash || !prev_slash)
1877 prev_slash = have_slash;
1879 len = d - uri->abspath;
1880 /* don't remove the first slash if that's the only thing left */
1881 if (len > 1 && *(d - 1) == '/')
1887 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1889 GstRTSPClientPrivate *priv = client->priv;
1891 GST_INFO ("client %p: session %p finished", client, session);
1893 /* unlink all media managed in this session */
1894 client_unlink_session (client, session);
1896 /* remove the session */
1897 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1898 GST_INFO ("client %p: all sessions finalized, close the connection",
1900 close_connection (client);
1905 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1907 GstRTSPClientPrivate *priv = client->priv;
1908 GstRTSPMethod method;
1909 const gchar *uristr;
1910 GstRTSPUrl *uri = NULL;
1911 GstRTSPVersion version;
1913 GstRTSPSession *session = NULL;
1914 GstRTSPContext sctx = { NULL }, *ctx;
1915 GstRTSPMessage response = { 0 };
1918 if (!(ctx = gst_rtsp_context_get_current ())) {
1920 ctx->auth = priv->auth;
1921 gst_rtsp_context_push_current (ctx);
1924 ctx->conn = priv->connection;
1925 ctx->client = client;
1926 ctx->request = request;
1927 ctx->response = &response;
1929 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1930 gst_rtsp_message_dump (request);
1933 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1935 GST_INFO ("client %p: received a request %s %s %s", client,
1936 gst_rtsp_method_as_text (method), uristr,
1937 gst_rtsp_version_as_text (version));
1939 /* we can only handle 1.0 requests */
1940 if (version != GST_RTSP_VERSION_1_0)
1943 ctx->method = method;
1945 /* we always try to parse the url first */
1946 if (strcmp (uristr, "*") == 0) {
1947 /* special case where we have * as uri, keep uri = NULL */
1948 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
1949 /* check if the uristr is an absolute path <=> scheme and host information
1953 scheme = g_uri_parse_scheme (uristr);
1954 if (scheme == NULL && g_str_has_prefix (uristr, "/")) {
1955 gchar *absolute_uristr = NULL;
1957 GST_WARNING_OBJECT (client, "request doesn't contain absolute url");
1958 if (priv->server_ip == NULL) {
1959 GST_WARNING_OBJECT (client, "host information missing");
1964 g_strdup_printf ("rtsp://%s%s", priv->server_ip, uristr);
1966 GST_DEBUG_OBJECT (client, "absolute url: %s", absolute_uristr);
1967 if (gst_rtsp_url_parse (absolute_uristr, &uri) != GST_RTSP_OK) {
1968 g_free (absolute_uristr);
1971 g_free (absolute_uristr);
1978 /* get the session if there is any */
1979 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1980 if (res == GST_RTSP_OK) {
1981 if (priv->session_pool == NULL)
1984 /* we had a session in the request, find it again */
1985 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1986 goto session_not_found;
1988 /* we add the session to the client list of watched sessions. When a session
1989 * disappears because it times out, we will be notified. If all sessions are
1990 * gone, we will close the connection */
1991 client_watch_session (client, session);
1994 /* sanitize the uri */
1998 ctx->session = session;
2000 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
2001 goto not_authorized;
2003 /* now see what is asked and dispatch to a dedicated handler */
2005 case GST_RTSP_OPTIONS:
2006 handle_options_request (client, ctx);
2008 case GST_RTSP_DESCRIBE:
2009 handle_describe_request (client, ctx);
2011 case GST_RTSP_SETUP:
2012 handle_setup_request (client, ctx);
2015 handle_play_request (client, ctx);
2017 case GST_RTSP_PAUSE:
2018 handle_pause_request (client, ctx);
2020 case GST_RTSP_TEARDOWN:
2021 handle_teardown_request (client, ctx);
2023 case GST_RTSP_SET_PARAMETER:
2024 handle_set_param_request (client, ctx);
2026 case GST_RTSP_GET_PARAMETER:
2027 handle_get_param_request (client, ctx);
2029 case GST_RTSP_ANNOUNCE:
2030 case GST_RTSP_RECORD:
2031 case GST_RTSP_REDIRECT:
2032 goto not_implemented;
2033 case GST_RTSP_INVALID:
2040 gst_rtsp_context_pop_current (ctx);
2042 g_object_unref (session);
2044 gst_rtsp_url_free (uri);
2050 GST_ERROR ("client %p: version %d not supported", client, version);
2051 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
2057 GST_ERROR ("client %p: bad request", client);
2058 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2063 GST_ERROR ("client %p: no pool configured", client);
2064 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2069 GST_ERROR ("client %p: session not found", client);
2070 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2075 GST_ERROR ("client %p: not allowed", client);
2076 /* error reply is already sent */
2081 GST_ERROR ("client %p: method %d not implemented", client, method);
2082 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
2089 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
2091 GstRTSPClientPrivate *priv = client->priv;
2093 GstRTSPSession *session = NULL;
2094 GstRTSPContext sctx = { NULL }, *ctx;
2097 if (!(ctx = gst_rtsp_context_get_current ())) {
2099 ctx->auth = priv->auth;
2100 gst_rtsp_context_push_current (ctx);
2103 ctx->conn = priv->connection;
2104 ctx->client = client;
2105 ctx->request = NULL;
2107 ctx->method = GST_RTSP_INVALID;
2108 ctx->response = response;
2110 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2111 gst_rtsp_message_dump (response);
2114 GST_INFO ("client %p: received a response", client);
2116 /* get the session if there is any */
2118 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
2119 if (res == GST_RTSP_OK) {
2120 if (priv->session_pool == NULL)
2123 /* we had a session in the request, find it again */
2124 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2125 goto session_not_found;
2127 /* we add the session to the client list of watched sessions. When a session
2128 * disappears because it times out, we will be notified. If all sessions are
2129 * gone, we will close the connection */
2130 client_watch_session (client, session);
2133 ctx->session = session;
2135 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
2140 gst_rtsp_context_pop_current (ctx);
2142 g_object_unref (session);
2147 GST_ERROR ("client %p: no pool configured", client);
2152 GST_ERROR ("client %p: session not found", client);
2158 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2160 GstRTSPClientPrivate *priv = client->priv;
2169 /* find the stream for this message */
2170 res = gst_rtsp_message_parse_data (message, &channel);
2171 if (res != GST_RTSP_OK)
2174 gst_rtsp_message_steal_body (message, &data, &size);
2176 buffer = gst_buffer_new_wrapped (data, size);
2179 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2180 GstRTSPStreamTransport *trans;
2181 GstRTSPStream *stream;
2182 const GstRTSPTransport *tr;
2186 tr = gst_rtsp_stream_transport_get_transport (trans);
2187 stream = gst_rtsp_stream_transport_get_stream (trans);
2189 /* check for TCP transport */
2190 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2191 /* dispatch to the stream based on the channel number */
2192 if (tr->interleaved.min == channel) {
2193 gst_rtsp_stream_recv_rtp (stream, buffer);
2196 } else if (tr->interleaved.max == channel) {
2197 gst_rtsp_stream_recv_rtcp (stream, buffer);
2204 gst_buffer_unref (buffer);
2208 * gst_rtsp_client_set_session_pool:
2209 * @client: a #GstRTSPClient
2210 * @pool: a #GstRTSPSessionPool
2212 * Set @pool as the sessionpool for @client which it will use to find
2213 * or allocate sessions. the sessionpool is usually inherited from the server
2214 * that created the client but can be overridden later.
2217 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2218 GstRTSPSessionPool * pool)
2220 GstRTSPSessionPool *old;
2221 GstRTSPClientPrivate *priv;
2223 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2225 priv = client->priv;
2228 g_object_ref (pool);
2230 g_mutex_lock (&priv->lock);
2231 old = priv->session_pool;
2232 priv->session_pool = pool;
2233 g_mutex_unlock (&priv->lock);
2236 g_object_unref (old);
2240 * gst_rtsp_client_get_session_pool:
2241 * @client: a #GstRTSPClient
2243 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2245 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2247 GstRTSPSessionPool *
2248 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2250 GstRTSPClientPrivate *priv;
2251 GstRTSPSessionPool *result;
2253 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2255 priv = client->priv;
2257 g_mutex_lock (&priv->lock);
2258 if ((result = priv->session_pool))
2259 g_object_ref (result);
2260 g_mutex_unlock (&priv->lock);
2266 * gst_rtsp_client_set_mount_points:
2267 * @client: a #GstRTSPClient
2268 * @mounts: a #GstRTSPMountPoints
2270 * Set @mounts as the mount points for @client which it will use to map urls
2271 * to media streams. These mount points are usually inherited from the server that
2272 * created the client but can be overriden later.
2275 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2276 GstRTSPMountPoints * mounts)
2278 GstRTSPClientPrivate *priv;
2279 GstRTSPMountPoints *old;
2281 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2283 priv = client->priv;
2286 g_object_ref (mounts);
2288 g_mutex_lock (&priv->lock);
2289 old = priv->mount_points;
2290 priv->mount_points = mounts;
2291 g_mutex_unlock (&priv->lock);
2294 g_object_unref (old);
2298 * gst_rtsp_client_get_mount_points:
2299 * @client: a #GstRTSPClient
2301 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2303 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2305 GstRTSPMountPoints *
2306 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2308 GstRTSPClientPrivate *priv;
2309 GstRTSPMountPoints *result;
2311 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2313 priv = client->priv;
2315 g_mutex_lock (&priv->lock);
2316 if ((result = priv->mount_points))
2317 g_object_ref (result);
2318 g_mutex_unlock (&priv->lock);
2324 * gst_rtsp_client_set_auth:
2325 * @client: a #GstRTSPClient
2326 * @auth: a #GstRTSPAuth
2328 * configure @auth to be used as the authentication manager of @client.
2331 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2333 GstRTSPClientPrivate *priv;
2336 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2338 priv = client->priv;
2341 g_object_ref (auth);
2343 g_mutex_lock (&priv->lock);
2346 g_mutex_unlock (&priv->lock);
2349 g_object_unref (old);
2354 * gst_rtsp_client_get_auth:
2355 * @client: a #GstRTSPClient
2357 * Get the #GstRTSPAuth used as the authentication manager of @client.
2359 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2363 gst_rtsp_client_get_auth (GstRTSPClient * client)
2365 GstRTSPClientPrivate *priv;
2366 GstRTSPAuth *result;
2368 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2370 priv = client->priv;
2372 g_mutex_lock (&priv->lock);
2373 if ((result = priv->auth))
2374 g_object_ref (result);
2375 g_mutex_unlock (&priv->lock);
2381 * gst_rtsp_client_set_thread_pool:
2382 * @client: a #GstRTSPClient
2383 * @pool: a #GstRTSPThreadPool
2385 * configure @pool to be used as the thread pool of @client.
2388 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2389 GstRTSPThreadPool * pool)
2391 GstRTSPClientPrivate *priv;
2392 GstRTSPThreadPool *old;
2394 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2396 priv = client->priv;
2399 g_object_ref (pool);
2401 g_mutex_lock (&priv->lock);
2402 old = priv->thread_pool;
2403 priv->thread_pool = pool;
2404 g_mutex_unlock (&priv->lock);
2407 g_object_unref (old);
2411 * gst_rtsp_client_get_thread_pool:
2412 * @client: a #GstRTSPClient
2414 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2416 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2420 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2422 GstRTSPClientPrivate *priv;
2423 GstRTSPThreadPool *result;
2425 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2427 priv = client->priv;
2429 g_mutex_lock (&priv->lock);
2430 if ((result = priv->thread_pool))
2431 g_object_ref (result);
2432 g_mutex_unlock (&priv->lock);
2438 * gst_rtsp_client_set_connection:
2439 * @client: a #GstRTSPClient
2440 * @conn: (transfer full): a #GstRTSPConnection
2442 * Set the #GstRTSPConnection of @client. This function takes ownership of
2445 * Returns: %TRUE on success.
2448 gst_rtsp_client_set_connection (GstRTSPClient * client,
2449 GstRTSPConnection * conn)
2451 GstRTSPClientPrivate *priv;
2452 GSocket *read_socket;
2453 GSocketAddress *address;
2455 GError *error = NULL;
2457 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2458 g_return_val_if_fail (conn != NULL, FALSE);
2460 priv = client->priv;
2462 read_socket = gst_rtsp_connection_get_read_socket (conn);
2464 if (!(address = g_socket_get_local_address (read_socket, &error)))
2467 g_free (priv->server_ip);
2468 /* keep the original ip that the client connected to */
2469 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2470 GInetAddress *iaddr;
2472 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2474 /* socket might be ipv6 but adress still ipv4 */
2475 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2476 priv->server_ip = g_inet_address_to_string (iaddr);
2477 g_object_unref (address);
2479 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2480 priv->server_ip = g_strdup ("unknown");
2483 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2484 priv->server_ip, priv->is_ipv6);
2486 url = gst_rtsp_connection_get_url (conn);
2487 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2489 priv->connection = conn;
2496 GST_ERROR ("could not get local address %s", error->message);
2497 g_error_free (error);
2503 * gst_rtsp_client_get_connection:
2504 * @client: a #GstRTSPClient
2506 * Get the #GstRTSPConnection of @client.
2508 * Returns: (transfer none): the #GstRTSPConnection of @client.
2509 * The connection object returned remains valid until the client is freed.
2512 gst_rtsp_client_get_connection (GstRTSPClient * client)
2514 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2516 return client->priv->connection;
2520 * gst_rtsp_client_set_send_func:
2521 * @client: a #GstRTSPClient
2522 * @func: a #GstRTSPClientSendFunc
2523 * @user_data: user data passed to @func
2524 * @notify: called when @user_data is no longer in use
2526 * Set @func as the callback that will be called when a new message needs to be
2527 * sent to the client. @user_data is passed to @func and @notify is called when
2528 * @user_data is no longer in use.
2530 * By default, the client will send the messages on the #GstRTSPConnection that
2531 * was configured with gst_rtsp_client_attach() was called.
2534 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2535 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2537 GstRTSPClientPrivate *priv;
2538 GDestroyNotify old_notify;
2541 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2543 priv = client->priv;
2545 g_mutex_lock (&priv->send_lock);
2546 priv->send_func = func;
2547 old_notify = priv->send_notify;
2548 old_data = priv->send_data;
2549 priv->send_notify = notify;
2550 priv->send_data = user_data;
2551 g_mutex_unlock (&priv->send_lock);
2554 old_notify (old_data);
2558 * gst_rtsp_client_handle_message:
2559 * @client: a #GstRTSPClient
2560 * @message: an #GstRTSPMessage
2562 * Let the client handle @message.
2564 * Returns: a #GstRTSPResult.
2567 gst_rtsp_client_handle_message (GstRTSPClient * client,
2568 GstRTSPMessage * message)
2570 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2571 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2573 switch (message->type) {
2574 case GST_RTSP_MESSAGE_REQUEST:
2575 handle_request (client, message);
2577 case GST_RTSP_MESSAGE_RESPONSE:
2578 handle_response (client, message);
2580 case GST_RTSP_MESSAGE_DATA:
2581 handle_data (client, message);
2590 * gst_rtsp_client_send_message:
2591 * @client: a #GstRTSPClient
2592 * @session: a #GstRTSPSession to send the message to or %NULL
2593 * @message: The #GstRTSPMessage to send
2595 * Send a message message to the remote end. @message must be a
2596 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
2599 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
2600 GstRTSPMessage * message)
2602 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2603 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2604 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
2605 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
2607 send_message (client, session, message, FALSE);
2612 static GstRTSPResult
2613 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2614 gboolean close, gpointer user_data)
2616 GstRTSPClientPrivate *priv = client->priv;
2618 /* send the response and store the seq number so we can wait until it's
2619 * written to the client to close the connection */
2620 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2621 &priv->close_seq : NULL);
2624 static GstRTSPResult
2625 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2628 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2631 static GstRTSPResult
2632 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2634 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2635 GstRTSPClientPrivate *priv = client->priv;
2637 if (priv->close_seq && priv->close_seq == cseq) {
2638 priv->close_seq = 0;
2639 close_connection (client);
2645 static GstRTSPResult
2646 closed (GstRTSPWatch * watch, gpointer user_data)
2648 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2649 GstRTSPClientPrivate *priv = client->priv;
2650 const gchar *tunnelid;
2652 GST_INFO ("client %p: connection closed", client);
2654 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2655 g_mutex_lock (&tunnels_lock);
2656 /* remove from tunnelids */
2657 g_hash_table_remove (tunnels, tunnelid);
2658 g_mutex_unlock (&tunnels_lock);
2661 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2666 static GstRTSPResult
2667 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2669 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2672 str = gst_rtsp_strresult (result);
2673 GST_INFO ("client %p: received an error %s", client, str);
2679 static GstRTSPResult
2680 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2681 GstRTSPMessage * message, guint id, gpointer user_data)
2683 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2686 str = gst_rtsp_strresult (result);
2688 ("client %p: error when handling message %p with id %d: %s",
2689 client, message, id, str);
2696 remember_tunnel (GstRTSPClient * client)
2698 GstRTSPClientPrivate *priv = client->priv;
2699 const gchar *tunnelid;
2701 /* store client in the pending tunnels */
2702 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2703 if (tunnelid == NULL)
2706 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2708 /* we can't have two clients connecting with the same tunnelid */
2709 g_mutex_lock (&tunnels_lock);
2710 if (g_hash_table_lookup (tunnels, tunnelid))
2711 goto tunnel_existed;
2713 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2714 g_mutex_unlock (&tunnels_lock);
2721 GST_ERROR ("client %p: no tunnelid provided", client);
2726 g_mutex_unlock (&tunnels_lock);
2727 GST_ERROR ("client %p: tunnel session %s already existed", client,
2733 static GstRTSPStatusCode
2734 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2736 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2737 GstRTSPClientPrivate *priv = client->priv;
2739 GST_INFO ("client %p: tunnel start (connection %p)", client,
2742 if (!remember_tunnel (client))
2745 return GST_RTSP_STS_OK;
2750 GST_ERROR ("client %p: error starting tunnel", client);
2751 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2755 static GstRTSPResult
2756 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2758 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2759 GstRTSPClientPrivate *priv = client->priv;
2761 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2764 /* ignore error, it'll only be a problem when the client does a POST again */
2765 remember_tunnel (client);
2770 static GstRTSPResult
2771 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2773 const gchar *tunnelid;
2774 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2775 GstRTSPClientPrivate *priv = client->priv;
2776 GstRTSPClient *oclient;
2777 GstRTSPClientPrivate *opriv;
2779 GST_INFO ("client %p: tunnel complete", client);
2781 /* find previous tunnel */
2782 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2783 if (tunnelid == NULL)
2786 g_mutex_lock (&tunnels_lock);
2787 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2790 /* remove the old client from the table. ref before because removing it will
2791 * remove the ref to it. */
2792 g_object_ref (oclient);
2793 g_hash_table_remove (tunnels, tunnelid);
2795 opriv = oclient->priv;
2797 if (opriv->watch == NULL)
2799 g_mutex_unlock (&tunnels_lock);
2801 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2802 opriv->connection, priv->connection);
2804 /* merge the tunnels into the first client */
2805 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2806 gst_rtsp_watch_reset (opriv->watch);
2807 g_object_unref (oclient);
2814 GST_ERROR ("client %p: no tunnelid provided", client);
2815 return GST_RTSP_ERROR;
2819 g_mutex_unlock (&tunnels_lock);
2820 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2821 return GST_RTSP_ERROR;
2825 g_mutex_unlock (&tunnels_lock);
2826 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2827 g_object_unref (oclient);
2828 return GST_RTSP_ERROR;
2832 static GstRTSPWatchFuncs watch_funcs = {
2844 client_watch_notify (GstRTSPClient * client)
2846 GstRTSPClientPrivate *priv = client->priv;
2848 GST_INFO ("client %p: watch destroyed", client);
2850 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2851 g_object_unref (client);
2855 * gst_rtsp_client_attach:
2856 * @client: a #GstRTSPClient
2857 * @context: (allow-none): a #GMainContext
2859 * Attaches @client to @context. When the mainloop for @context is run, the
2860 * client will be dispatched. When @context is %NULL, the default context will be
2863 * This function should be called when the client properties and urls are fully
2864 * configured and the client is ready to start.
2866 * Returns: the ID (greater than 0) for the source within the GMainContext.
2869 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2871 GstRTSPClientPrivate *priv;
2874 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2875 priv = client->priv;
2876 g_return_val_if_fail (priv->connection != NULL, 0);
2877 g_return_val_if_fail (priv->watch == NULL, 0);
2879 /* create watch for the connection and attach */
2880 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2881 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2882 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2883 (GDestroyNotify) gst_rtsp_watch_unref);
2885 /* FIXME make this configurable. We don't want to do this yet because it will
2886 * be superceeded by a cache object later */
2887 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2889 GST_INFO ("attaching to context %p", context);
2890 res = gst_rtsp_watch_attach (priv->watch, context);
2896 * gst_rtsp_client_session_filter:
2897 * @client: a #GstRTSPClient
2898 * @func: (scope call) (allow-none): a callback
2899 * @user_data: user data passed to @func
2901 * Call @func for each session managed by @client. The result value of @func
2902 * determines what happens to the session. @func will be called with @client
2903 * locked so no further actions on @client can be performed from @func.
2905 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
2908 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
2910 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
2911 * will also be added with an additional ref to the result #GList of this
2914 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each session.
2916 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
2917 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2918 * element in the #GList should be unreffed before the list is freed.
2921 gst_rtsp_client_session_filter (GstRTSPClient * client,
2922 GstRTSPClientSessionFilterFunc func, gpointer user_data)
2924 GstRTSPClientPrivate *priv;
2925 GList *result, *walk, *next;
2927 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2929 priv = client->priv;
2933 g_mutex_lock (&priv->lock);
2934 for (walk = priv->sessions; walk; walk = next) {
2935 GstRTSPSession *sess = walk->data;
2936 GstRTSPFilterResult res;
2938 next = g_list_next (walk);
2941 res = func (client, sess, user_data);
2943 res = GST_RTSP_FILTER_REF;
2946 case GST_RTSP_FILTER_REMOVE:
2947 /* stop watching the session and pretent it went away */
2948 client_cleanup_session (client, sess);
2950 case GST_RTSP_FILTER_REF:
2951 result = g_list_prepend (result, g_object_ref (sess));
2953 case GST_RTSP_FILTER_KEEP:
2958 g_mutex_unlock (&priv->lock);