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, gint * matched)
485 GstRTSPClientPrivate *priv = client->priv;
486 GstRTSPMediaFactory *factory;
491 if (!priv->mount_points)
492 goto no_mount_points;
494 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
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);
559 g_object_ref (media);
566 GST_ERROR ("client %p: no mount points configured", client);
567 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
572 GST_ERROR ("client %p: can't find path for url", client);
573 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
578 GST_ERROR ("client %p: no factory for uri %s", client, path);
580 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
585 GST_ERROR ("client %p: not authorized to see factory uri %s", client, path);
591 GST_ERROR ("client %p: not authorized for factory uri %s", client, path);
597 GST_ERROR ("client %p: can't create media", client);
598 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
599 g_object_unref (factory);
606 GST_ERROR ("client %p: can't create thread", client);
607 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
608 g_object_unref (media);
610 g_object_unref (factory);
617 GST_ERROR ("client %p: can't prepare media", client);
618 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
619 g_object_unref (media);
621 g_object_unref (factory);
629 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
631 GstRTSPClientPrivate *priv = client->priv;
632 GstRTSPMessage message = { 0 };
637 gst_rtsp_message_init_data (&message, channel);
639 /* FIXME, need some sort of iovec RTSPMessage here */
640 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
643 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
645 g_mutex_lock (&priv->send_lock);
647 priv->send_func (client, &message, FALSE, priv->send_data);
648 g_mutex_unlock (&priv->send_lock);
650 gst_rtsp_message_steal_body (&message, &data, &usize);
651 gst_buffer_unmap (buffer, &map_info);
653 gst_rtsp_message_unset (&message);
659 link_transport (GstRTSPClient * client, GstRTSPSession * session,
660 GstRTSPStreamTransport * trans)
662 GstRTSPClientPrivate *priv = client->priv;
664 GST_DEBUG ("client %p: linking transport %p", client, trans);
666 gst_rtsp_stream_transport_set_callbacks (trans,
667 (GstRTSPSendFunc) do_send_data,
668 (GstRTSPSendFunc) do_send_data, client, NULL);
670 priv->transports = g_list_prepend (priv->transports, trans);
672 /* make sure our session can't expire */
673 gst_rtsp_session_prevent_expire (session);
677 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
678 GstRTSPStreamTransport * trans)
680 GstRTSPClientPrivate *priv = client->priv;
682 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
684 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
686 priv->transports = g_list_remove (priv->transports, trans);
688 /* our session can now expire */
689 gst_rtsp_session_allow_expire (session);
693 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
694 GstRTSPSessionMedia * sessmedia)
699 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
700 for (i = 0; i < n_streams; i++) {
701 GstRTSPStreamTransport *trans;
702 const GstRTSPTransport *tr;
704 /* get the transport, if there is no transport configured, skip this stream */
705 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
709 tr = gst_rtsp_stream_transport_get_transport (trans);
711 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
712 /* for TCP, unlink the stream from the TCP connection of the client */
713 unlink_transport (client, session, trans);
719 close_connection (GstRTSPClient * client)
721 GstRTSPClientPrivate *priv = client->priv;
722 const gchar *tunnelid;
724 GST_DEBUG ("client %p: closing connection", client);
726 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
727 g_mutex_lock (&tunnels_lock);
728 /* remove from tunnelids */
729 g_hash_table_remove (tunnels, tunnelid);
730 g_mutex_unlock (&tunnels_lock);
733 gst_rtsp_connection_close (priv->connection);
737 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
739 GstRTSPClientPrivate *priv = client->priv;
740 GstRTSPSession *session;
741 GstRTSPSessionMedia *sessmedia;
742 GstRTSPStatusCode code;
746 if (!(session = ctx->session))
752 if (!priv->mount_points)
753 goto no_mount_points;
755 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
758 /* get a handle to the configuration of the media in the session */
759 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
763 /* only aggregate control for now.. */
764 if (path[matched] != '\0')
769 ctx->sessmedia = sessmedia;
771 /* we emit the signal before closing the connection */
772 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
775 /* unlink the all TCP callbacks */
776 unlink_session_transports (client, session, sessmedia);
778 /* remove the session from the watched sessions */
779 client_unwatch_session (client, session);
781 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
783 /* unmanage the media in the session, returns false if all media session
785 if (!gst_rtsp_session_release_media (session, sessmedia)) {
786 /* remove the session */
787 gst_rtsp_session_pool_remove (priv->session_pool, session);
789 /* construct the response now */
790 code = GST_RTSP_STS_OK;
791 gst_rtsp_message_init_response (ctx->response, code,
792 gst_rtsp_status_as_text (code), ctx->request);
794 send_message (client, session, ctx->response, TRUE);
801 GST_ERROR ("client %p: no session", client);
802 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
807 GST_ERROR ("client %p: no uri supplied", client);
808 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
813 GST_ERROR ("client %p: no mount points configured", client);
814 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
819 GST_ERROR ("client %p: can't find path for url", client);
820 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
825 GST_ERROR ("client %p: no media for uri", client);
827 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
832 GST_ERROR ("client %p: no aggregate path %s", client, path);
834 send_generic_response (client,
835 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
841 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
845 res = gst_rtsp_params_set (client, ctx);
851 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
855 res = gst_rtsp_params_get (client, ctx);
861 handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
867 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
868 if (res != GST_RTSP_OK)
872 /* no body, keep-alive request */
873 send_generic_response (client, GST_RTSP_STS_OK, ctx);
875 /* there is a body, handle the params */
876 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
877 if (res != GST_RTSP_OK)
880 send_message (client, ctx->session, ctx->response, FALSE);
883 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
891 GST_ERROR ("client %p: bad request", client);
892 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
898 handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
904 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
905 if (res != GST_RTSP_OK)
909 /* no body, keep-alive request */
910 send_generic_response (client, GST_RTSP_STS_OK, ctx);
912 /* there is a body, handle the params */
913 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
914 if (res != GST_RTSP_OK)
917 send_message (client, ctx->session, ctx->response, FALSE);
920 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
928 GST_ERROR ("client %p: bad request", client);
929 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
935 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
937 GstRTSPClientPrivate *priv = client->priv;
938 GstRTSPSession *session;
939 GstRTSPSessionMedia *sessmedia;
940 GstRTSPStatusCode code;
941 GstRTSPState rtspstate;
945 if (!(session = ctx->session))
951 if (!priv->mount_points)
952 goto no_mount_points;
954 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
957 /* get a handle to the configuration of the media in the session */
958 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
962 if (path[matched] != '\0')
967 ctx->sessmedia = sessmedia;
969 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
970 /* the session state must be playing or recording */
971 if (rtspstate != GST_RTSP_STATE_PLAYING &&
972 rtspstate != GST_RTSP_STATE_RECORDING)
975 /* unlink the all TCP callbacks */
976 unlink_session_transports (client, session, sessmedia);
978 /* then pause sending */
979 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
981 /* construct the response now */
982 code = GST_RTSP_STS_OK;
983 gst_rtsp_message_init_response (ctx->response, code,
984 gst_rtsp_status_as_text (code), ctx->request);
986 send_message (client, session, ctx->response, FALSE);
988 /* the state is now READY */
989 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
991 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
998 GST_ERROR ("client %p: no seesion", client);
999 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1004 GST_ERROR ("client %p: no uri supplied", client);
1005 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1010 GST_ERROR ("client %p: no mount points configured", client);
1011 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1016 GST_ERROR ("client %p: can't find path for url", client);
1017 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1022 GST_ERROR ("client %p: no media for uri", client);
1024 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1029 GST_ERROR ("client %p: no aggregate path %s", client, path);
1031 send_generic_response (client,
1032 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1037 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
1038 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1045 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
1047 GstRTSPClientPrivate *priv = client->priv;
1048 GstRTSPSession *session;
1049 GstRTSPSessionMedia *sessmedia;
1050 GstRTSPMedia *media;
1051 GstRTSPStatusCode code;
1053 guint n_streams, i, infocount;
1055 GstRTSPTimeRange *range;
1057 GstRTSPState rtspstate;
1058 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1062 if (!(session = ctx->session))
1068 if (!priv->mount_points)
1069 goto no_mount_points;
1071 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
1074 /* get a handle to the configuration of the media in the session */
1075 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1079 if (path[matched] != '\0')
1084 ctx->sessmedia = sessmedia;
1085 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1087 /* the session state must be playing or ready */
1088 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1089 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1092 /* parse the range header if we have one */
1093 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1094 if (res == GST_RTSP_OK) {
1095 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1096 /* we have a range, seek to the position */
1098 gst_rtsp_media_seek (media, range);
1099 gst_rtsp_range_free (range);
1103 /* grab RTPInfo from the payloaders now */
1104 rtpinfo = g_string_new ("");
1106 n_streams = gst_rtsp_media_n_streams (media);
1107 for (i = 0, infocount = 0; i < n_streams; i++) {
1108 GstRTSPStreamTransport *trans;
1109 GstRTSPStream *stream;
1110 const GstRTSPTransport *tr;
1114 /* get the transport, if there is no transport configured, skip this stream */
1115 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
1116 if (trans == NULL) {
1117 GST_INFO ("stream %d is not configured", i);
1120 tr = gst_rtsp_stream_transport_get_transport (trans);
1122 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1123 /* for TCP, link the stream to the TCP connection of the client */
1124 link_transport (client, session, trans);
1127 stream = gst_rtsp_stream_transport_get_stream (trans);
1128 if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
1130 g_string_append (rtpinfo, ", ");
1132 uristr = gst_rtsp_url_get_request_uri (ctx->uri);
1133 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
1134 uristr, i, seq, rtptime);
1139 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
1143 /* construct the response now */
1144 code = GST_RTSP_STS_OK;
1145 gst_rtsp_message_init_response (ctx->response, code,
1146 gst_rtsp_status_as_text (code), ctx->request);
1148 /* add the RTP-Info header */
1149 if (infocount > 0) {
1150 str = g_string_free (rtpinfo, FALSE);
1151 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO, str);
1153 g_string_free (rtpinfo, TRUE);
1157 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1159 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1161 send_message (client, session, ctx->response, FALSE);
1163 /* start playing after sending the request */
1164 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1166 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1168 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1175 GST_ERROR ("client %p: no session", client);
1176 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1181 GST_ERROR ("client %p: no uri supplied", client);
1182 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1187 GST_ERROR ("client %p: no mount points configured", client);
1188 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1193 GST_ERROR ("client %p: can't find path for url", client);
1194 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1199 GST_ERROR ("client %p: media not found", client);
1201 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1206 GST_ERROR ("client %p: no aggregate path %s", client, path);
1208 send_generic_response (client,
1209 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1214 GST_ERROR ("client %p: not PLAYING or READY", client);
1215 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1222 do_keepalive (GstRTSPSession * session)
1224 GST_INFO ("keep session %p alive", session);
1225 gst_rtsp_session_touch (session);
1228 /* parse @transport and return a valid transport in @tr. only transports
1229 * from @supported are returned. Returns FALSE if no valid transport
1232 parse_transport (const char *transport, GstRTSPLowerTrans supported,
1233 GstRTSPTransport * tr)
1240 gst_rtsp_transport_init (tr);
1242 GST_DEBUG ("parsing transports %s", transport);
1244 transports = g_strsplit (transport, ",", 0);
1246 /* loop through the transports, try to parse */
1247 for (i = 0; transports[i]; i++) {
1248 res = gst_rtsp_transport_parse (transports[i], tr);
1249 if (res != GST_RTSP_OK) {
1250 /* no valid transport, search some more */
1251 GST_WARNING ("could not parse transport %s", transports[i]);
1255 /* we have a transport, see if it's RTP/AVP */
1256 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1257 GST_WARNING ("invalid transport %s", transports[i]);
1261 if (!(tr->lower_transport & supported)) {
1262 GST_WARNING ("unsupported transport %s", transports[i]);
1266 /* we have a valid transport */
1267 GST_INFO ("found valid transport %s", transports[i]);
1272 gst_rtsp_transport_init (tr);
1274 g_strfreev (transports);
1280 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
1281 GstRTSPMessage * request)
1283 gchar *blocksize_str;
1284 gboolean ret = TRUE;
1286 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1287 &blocksize_str, 0) == GST_RTSP_OK) {
1291 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1292 if (end == blocksize_str) {
1293 GST_ERROR ("failed to parse blocksize");
1296 /* we don't want to change the mtu when this media
1297 * can be shared because it impacts other clients */
1298 if (gst_rtsp_media_is_shared (media))
1301 if (blocksize > G_MAXUINT)
1302 blocksize = G_MAXUINT;
1303 gst_rtsp_stream_set_mtu (stream, blocksize);
1310 default_configure_client_transport (GstRTSPClient * client,
1311 GstRTSPContext * ctx, GstRTSPTransport * ct)
1313 GstRTSPClientPrivate *priv = client->priv;
1315 /* we have a valid transport now, set the destination of the client. */
1316 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1317 gboolean use_client_settings;
1319 use_client_settings =
1320 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1322 if (ct->destination && use_client_settings) {
1323 GstRTSPAddress *addr;
1325 addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1326 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1331 gst_rtsp_address_free (addr);
1333 GstRTSPAddress *addr;
1334 GSocketFamily family;
1336 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1338 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1342 g_free (ct->destination);
1343 ct->destination = g_strdup (addr->address);
1344 ct->port.min = addr->port;
1345 ct->port.max = addr->port + addr->n_ports - 1;
1346 ct->ttl = addr->ttl;
1348 gst_rtsp_address_free (addr);
1353 url = gst_rtsp_connection_get_url (priv->connection);
1354 g_free (ct->destination);
1355 ct->destination = g_strdup (url->host);
1357 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1358 /* check if the client selected channels for TCP */
1359 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1360 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1370 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1375 static GstRTSPTransport *
1376 make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
1377 GstRTSPTransport * ct)
1379 GstRTSPTransport *st;
1381 GSocketFamily family;
1383 /* prepare the server transport */
1384 gst_rtsp_transport_new (&st);
1386 st->trans = ct->trans;
1387 st->profile = ct->profile;
1388 st->lower_transport = ct->lower_transport;
1390 addr = g_inet_address_new_from_string (ct->destination);
1393 GST_ERROR ("failed to get inet addr from client destination");
1394 family = G_SOCKET_FAMILY_IPV4;
1396 family = g_inet_address_get_family (addr);
1397 g_object_unref (addr);
1401 switch (st->lower_transport) {
1402 case GST_RTSP_LOWER_TRANS_UDP:
1403 st->client_port = ct->client_port;
1404 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
1406 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1407 st->port = ct->port;
1408 st->destination = g_strdup (ct->destination);
1411 case GST_RTSP_LOWER_TRANS_TCP:
1412 st->interleaved = ct->interleaved;
1417 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
1423 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
1425 GstRTSPClientPrivate *priv = client->priv;
1429 GstRTSPTransport *ct, *st;
1430 GstRTSPLowerTrans supported;
1431 GstRTSPStatusCode code;
1432 GstRTSPSession *session;
1433 GstRTSPStreamTransport *trans;
1435 GstRTSPSessionMedia *sessmedia;
1436 GstRTSPMedia *media;
1437 GstRTSPStream *stream;
1438 GstRTSPState rtspstate;
1439 GstRTSPClientClass *klass;
1440 gchar *path, *control;
1443 if (!(uri = ctx->uri))
1446 if (!priv->mount_points)
1447 goto no_mount_points;
1449 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, uri)))
1452 /* parse the transport */
1454 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
1456 if (res != GST_RTSP_OK)
1459 /* we create the session after parsing stuff so that we don't make
1460 * a session for malformed requests */
1461 if (priv->session_pool == NULL)
1464 session = ctx->session;
1467 g_object_ref (session);
1468 /* get a handle to the configuration of the media in the session, this can
1469 * return NULL if this is a new url to manage in this session. */
1470 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1472 /* we need a new media configuration in this session */
1476 /* we have no session media, find one and manage it */
1477 if (sessmedia == NULL) {
1478 /* get a handle to the configuration of the media in the session */
1479 media = find_media (client, ctx, &matched);
1481 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1482 g_object_ref (media);
1484 /* no media, not found then */
1486 goto media_not_found;
1488 /* path is what matched */
1489 path[matched] = '\0';
1490 /* control is remainder */
1491 control = &path[matched + 1];
1493 /* find the stream now using the control part */
1494 stream = gst_rtsp_media_find_stream (media, control);
1496 goto stream_not_found;
1498 /* now we have a uri identifying a valid media and stream */
1499 ctx->stream = stream;
1502 if (session == NULL) {
1503 /* create a session if this fails we probably reached our session limit or
1505 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1506 goto service_unavailable;
1508 /* make sure this client is closed when the session is closed */
1509 client_watch_session (client, session);
1511 /* signal new session */
1512 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1515 ctx->session = session;
1518 if (sessmedia == NULL) {
1519 /* manage the media in our session now, if not done already */
1520 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1521 /* if we stil have no media, error */
1522 if (sessmedia == NULL)
1523 goto sessmedia_unavailable;
1525 g_object_unref (media);
1529 ctx->sessmedia = sessmedia;
1531 /* set blocksize on this stream */
1532 if (!handle_blocksize (media, stream, ctx->request))
1533 goto invalid_blocksize;
1535 gst_rtsp_transport_new (&ct);
1537 /* our supported transports */
1538 supported = gst_rtsp_stream_get_protocols (stream);
1540 /* parse and find a usable supported transport */
1541 if (!parse_transport (transport, supported, ct))
1542 goto unsupported_transports;
1544 /* update the client transport */
1545 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1546 if (!klass->configure_client_transport (client, ctx, ct))
1547 goto unsupported_client_transport;
1549 /* set in the session media transport */
1550 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1552 /* configure keepalive for this transport */
1553 gst_rtsp_stream_transport_set_keepalive (trans,
1554 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1556 /* create and serialize the server transport */
1557 st = make_server_transport (client, ctx, ct);
1558 trans_str = gst_rtsp_transport_as_text (st);
1559 gst_rtsp_transport_free (st);
1561 /* construct the response now */
1562 code = GST_RTSP_STS_OK;
1563 gst_rtsp_message_init_response (ctx->response, code,
1564 gst_rtsp_status_as_text (code), ctx->request);
1566 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
1570 send_message (client, session, ctx->response, FALSE);
1572 /* update the state */
1573 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1574 switch (rtspstate) {
1575 case GST_RTSP_STATE_PLAYING:
1576 case GST_RTSP_STATE_RECORDING:
1577 case GST_RTSP_STATE_READY:
1578 /* no state change */
1581 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1584 g_object_unref (session);
1586 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
1593 GST_ERROR ("client %p: no uri", client);
1594 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1599 GST_ERROR ("client %p: no mount points configured", client);
1600 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1605 GST_ERROR ("client %p: can't find path for url", client);
1606 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1611 GST_ERROR ("client %p: no transport", client);
1613 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1618 GST_ERROR ("client %p: no session pool configured", client);
1620 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1625 GST_ERROR ("client %p: media '%s' not found", client, path);
1627 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1632 GST_ERROR ("client %p: stream '%s' not found", client, control);
1634 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1635 g_object_unref (media);
1638 service_unavailable:
1640 GST_ERROR ("client %p: can't create session", client);
1642 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1643 g_object_unref (media);
1646 sessmedia_unavailable:
1648 GST_ERROR ("client %p: can't create session media", client);
1649 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1651 g_object_unref (media);
1652 g_object_unref (session);
1657 GST_ERROR ("client %p: invalid blocksize", client);
1658 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1659 g_object_unref (session);
1662 unsupported_transports:
1664 GST_ERROR ("client %p: unsupported transports", client);
1665 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1666 gst_rtsp_transport_free (ct);
1667 g_object_unref (session);
1670 unsupported_client_transport:
1672 GST_ERROR ("client %p: unsupported client transport", client);
1673 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1674 gst_rtsp_transport_free (ct);
1675 g_object_unref (session);
1680 static GstSDPMessage *
1681 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1683 GstRTSPClientPrivate *priv = client->priv;
1688 gst_sdp_message_new (&sdp);
1690 /* some standard things first */
1691 gst_sdp_message_set_version (sdp, "0");
1698 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1701 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1702 gst_sdp_message_set_information (sdp, "rtsp-server");
1703 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1704 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1705 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1706 gst_sdp_message_add_attribute (sdp, "control", "*");
1708 info.is_ipv6 = priv->is_ipv6;
1709 info.server_ip = priv->server_ip;
1711 /* create an SDP for the media object */
1712 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1720 GST_ERROR ("client %p: could not create SDP", client);
1721 gst_sdp_message_free (sdp);
1726 /* for the describe we must generate an SDP */
1728 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
1733 gchar *str, *str_query, *content_base;
1734 GstRTSPMedia *media;
1735 GstRTSPClientClass *klass;
1737 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1742 /* check what kind of format is accepted, we don't really do anything with it
1743 * and always return SDP for now. */
1748 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
1750 if (res == GST_RTSP_ENOTIMPL)
1753 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1757 /* find the media object for the uri */
1758 if (!(media = find_media (client, ctx, NULL)))
1761 /* create an SDP for the media object on this client */
1762 if (!(sdp = klass->create_sdp (client, media)))
1765 g_object_unref (media);
1767 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
1768 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
1770 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
1773 /* content base for some clients that might screw up creating the setup uri */
1774 str = gst_rtsp_url_get_request_uri (ctx->uri);
1775 str_len = strlen (str);
1777 /* check for query part */
1778 if (ctx->uri->query != NULL) {
1779 str_query = g_strrstr (str, "?");
1781 str_len = strlen (str);
1784 /* check for trailing '/' and append one */
1785 if (str[str_len - 1] != '/') {
1786 content_base = g_malloc (str_len + 2);
1787 memcpy (content_base, str, str_len);
1788 content_base[str_len] = '/';
1789 content_base[str_len + 1] = '\0';
1795 GST_INFO ("adding content-base: %s", content_base);
1797 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE,
1799 g_free (content_base);
1801 /* add SDP to the response body */
1802 str = gst_sdp_message_as_text (sdp);
1803 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
1804 gst_sdp_message_free (sdp);
1806 send_message (client, ctx->session, ctx->response, FALSE);
1808 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1816 GST_ERROR ("client %p: no uri", client);
1817 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1822 GST_ERROR ("client %p: no media", client);
1823 /* error reply is already sent */
1828 GST_ERROR ("client %p: can't create SDP", client);
1829 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1830 g_object_unref (media);
1836 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
1838 GstRTSPMethod options;
1841 options = GST_RTSP_DESCRIBE |
1846 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1848 str = gst_rtsp_options_as_text (options);
1850 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
1851 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
1853 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
1856 send_message (client, ctx->session, ctx->response, FALSE);
1858 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1864 /* remove duplicate and trailing '/' */
1866 sanitize_uri (GstRTSPUrl * uri)
1870 gboolean have_slash, prev_slash;
1872 s = d = uri->abspath;
1873 len = strlen (uri->abspath);
1877 for (i = 0; i < len; i++) {
1878 have_slash = s[i] == '/';
1880 if (!have_slash || !prev_slash)
1882 prev_slash = have_slash;
1884 len = d - uri->abspath;
1885 /* don't remove the first slash if that's the only thing left */
1886 if (len > 1 && *(d - 1) == '/')
1892 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1894 GstRTSPClientPrivate *priv = client->priv;
1896 GST_INFO ("client %p: session %p finished", client, session);
1898 /* unlink all media managed in this session */
1899 client_unlink_session (client, session);
1901 /* remove the session */
1902 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1903 GST_INFO ("client %p: all sessions finalized, close the connection",
1905 close_connection (client);
1910 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1912 GstRTSPClientPrivate *priv = client->priv;
1913 GstRTSPMethod method;
1914 const gchar *uristr;
1915 GstRTSPUrl *uri = NULL;
1916 GstRTSPVersion version;
1918 GstRTSPSession *session = NULL;
1919 GstRTSPContext sctx = { NULL }, *ctx;
1920 GstRTSPMessage response = { 0 };
1923 if (!(ctx = gst_rtsp_context_get_current ())) {
1925 ctx->auth = priv->auth;
1926 gst_rtsp_context_push_current (ctx);
1929 ctx->conn = priv->connection;
1930 ctx->client = client;
1931 ctx->request = request;
1932 ctx->response = &response;
1934 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1935 gst_rtsp_message_dump (request);
1938 GST_INFO ("client %p: received a request", client);
1940 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1942 /* we can only handle 1.0 requests */
1943 if (version != GST_RTSP_VERSION_1_0)
1946 ctx->method = method;
1948 /* we always try to parse the url first */
1949 if (strcmp (uristr, "*") == 0) {
1950 /* special case where we have * as uri, keep uri = NULL */
1951 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1954 /* get the session if there is any */
1955 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1956 if (res == GST_RTSP_OK) {
1957 if (priv->session_pool == NULL)
1960 /* we had a session in the request, find it again */
1961 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1962 goto session_not_found;
1964 /* we add the session to the client list of watched sessions. When a session
1965 * disappears because it times out, we will be notified. If all sessions are
1966 * gone, we will close the connection */
1967 client_watch_session (client, session);
1970 /* sanitize the uri */
1974 ctx->session = session;
1976 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
1977 goto not_authorized;
1979 /* now see what is asked and dispatch to a dedicated handler */
1981 case GST_RTSP_OPTIONS:
1982 handle_options_request (client, ctx);
1984 case GST_RTSP_DESCRIBE:
1985 handle_describe_request (client, ctx);
1987 case GST_RTSP_SETUP:
1988 handle_setup_request (client, ctx);
1991 handle_play_request (client, ctx);
1993 case GST_RTSP_PAUSE:
1994 handle_pause_request (client, ctx);
1996 case GST_RTSP_TEARDOWN:
1997 handle_teardown_request (client, ctx);
1999 case GST_RTSP_SET_PARAMETER:
2000 handle_set_param_request (client, ctx);
2002 case GST_RTSP_GET_PARAMETER:
2003 handle_get_param_request (client, ctx);
2005 case GST_RTSP_ANNOUNCE:
2006 case GST_RTSP_RECORD:
2007 case GST_RTSP_REDIRECT:
2008 goto not_implemented;
2009 case GST_RTSP_INVALID:
2016 gst_rtsp_context_pop_current (ctx);
2018 g_object_unref (session);
2020 gst_rtsp_url_free (uri);
2026 GST_ERROR ("client %p: version %d not supported", client, version);
2027 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
2033 GST_ERROR ("client %p: bad request", client);
2034 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2039 GST_ERROR ("client %p: no pool configured", client);
2040 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2045 GST_ERROR ("client %p: session not found", client);
2046 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2051 GST_ERROR ("client %p: not allowed", client);
2056 GST_ERROR ("client %p: method %d not implemented", client, method);
2057 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
2064 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
2066 GstRTSPClientPrivate *priv = client->priv;
2068 GstRTSPSession *session = NULL;
2069 GstRTSPContext sctx = { NULL }, *ctx;
2072 if (!(ctx = gst_rtsp_context_get_current ())) {
2074 ctx->auth = priv->auth;
2075 gst_rtsp_context_push_current (ctx);
2078 ctx->conn = priv->connection;
2079 ctx->client = client;
2080 ctx->request = NULL;
2082 ctx->method = GST_RTSP_INVALID;
2083 ctx->response = response;
2085 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2086 gst_rtsp_message_dump (response);
2089 GST_INFO ("client %p: received a response", client);
2091 /* get the session if there is any */
2093 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
2094 if (res == GST_RTSP_OK) {
2095 if (priv->session_pool == NULL)
2098 /* we had a session in the request, find it again */
2099 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2100 goto session_not_found;
2102 /* we add the session to the client list of watched sessions. When a session
2103 * disappears because it times out, we will be notified. If all sessions are
2104 * gone, we will close the connection */
2105 client_watch_session (client, session);
2108 ctx->session = session;
2110 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
2115 gst_rtsp_context_pop_current (ctx);
2117 g_object_unref (session);
2122 GST_ERROR ("client %p: no pool configured", client);
2127 GST_ERROR ("client %p: session not found", client);
2133 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2135 GstRTSPClientPrivate *priv = client->priv;
2144 /* find the stream for this message */
2145 res = gst_rtsp_message_parse_data (message, &channel);
2146 if (res != GST_RTSP_OK)
2149 gst_rtsp_message_steal_body (message, &data, &size);
2151 buffer = gst_buffer_new_wrapped (data, size);
2154 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2155 GstRTSPStreamTransport *trans;
2156 GstRTSPStream *stream;
2157 const GstRTSPTransport *tr;
2161 tr = gst_rtsp_stream_transport_get_transport (trans);
2162 stream = gst_rtsp_stream_transport_get_stream (trans);
2164 /* check for TCP transport */
2165 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2166 /* dispatch to the stream based on the channel number */
2167 if (tr->interleaved.min == channel) {
2168 gst_rtsp_stream_recv_rtp (stream, buffer);
2171 } else if (tr->interleaved.max == channel) {
2172 gst_rtsp_stream_recv_rtcp (stream, buffer);
2179 gst_buffer_unref (buffer);
2183 * gst_rtsp_client_set_session_pool:
2184 * @client: a #GstRTSPClient
2185 * @pool: a #GstRTSPSessionPool
2187 * Set @pool as the sessionpool for @client which it will use to find
2188 * or allocate sessions. the sessionpool is usually inherited from the server
2189 * that created the client but can be overridden later.
2192 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2193 GstRTSPSessionPool * pool)
2195 GstRTSPSessionPool *old;
2196 GstRTSPClientPrivate *priv;
2198 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2200 priv = client->priv;
2203 g_object_ref (pool);
2205 g_mutex_lock (&priv->lock);
2206 old = priv->session_pool;
2207 priv->session_pool = pool;
2208 g_mutex_unlock (&priv->lock);
2211 g_object_unref (old);
2215 * gst_rtsp_client_get_session_pool:
2216 * @client: a #GstRTSPClient
2218 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2220 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2222 GstRTSPSessionPool *
2223 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2225 GstRTSPClientPrivate *priv;
2226 GstRTSPSessionPool *result;
2228 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2230 priv = client->priv;
2232 g_mutex_lock (&priv->lock);
2233 if ((result = priv->session_pool))
2234 g_object_ref (result);
2235 g_mutex_unlock (&priv->lock);
2241 * gst_rtsp_client_set_mount_points:
2242 * @client: a #GstRTSPClient
2243 * @mounts: a #GstRTSPMountPoints
2245 * Set @mounts as the mount points for @client which it will use to map urls
2246 * to media streams. These mount points are usually inherited from the server that
2247 * created the client but can be overriden later.
2250 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2251 GstRTSPMountPoints * mounts)
2253 GstRTSPClientPrivate *priv;
2254 GstRTSPMountPoints *old;
2256 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2258 priv = client->priv;
2261 g_object_ref (mounts);
2263 g_mutex_lock (&priv->lock);
2264 old = priv->mount_points;
2265 priv->mount_points = mounts;
2266 g_mutex_unlock (&priv->lock);
2269 g_object_unref (old);
2273 * gst_rtsp_client_get_mount_points:
2274 * @client: a #GstRTSPClient
2276 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2278 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2280 GstRTSPMountPoints *
2281 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2283 GstRTSPClientPrivate *priv;
2284 GstRTSPMountPoints *result;
2286 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2288 priv = client->priv;
2290 g_mutex_lock (&priv->lock);
2291 if ((result = priv->mount_points))
2292 g_object_ref (result);
2293 g_mutex_unlock (&priv->lock);
2299 * gst_rtsp_client_set_auth:
2300 * @client: a #GstRTSPClient
2301 * @auth: a #GstRTSPAuth
2303 * configure @auth to be used as the authentication manager of @client.
2306 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2308 GstRTSPClientPrivate *priv;
2311 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2313 priv = client->priv;
2316 g_object_ref (auth);
2318 g_mutex_lock (&priv->lock);
2321 g_mutex_unlock (&priv->lock);
2324 g_object_unref (old);
2329 * gst_rtsp_client_get_auth:
2330 * @client: a #GstRTSPClient
2332 * Get the #GstRTSPAuth used as the authentication manager of @client.
2334 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2338 gst_rtsp_client_get_auth (GstRTSPClient * client)
2340 GstRTSPClientPrivate *priv;
2341 GstRTSPAuth *result;
2343 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2345 priv = client->priv;
2347 g_mutex_lock (&priv->lock);
2348 if ((result = priv->auth))
2349 g_object_ref (result);
2350 g_mutex_unlock (&priv->lock);
2356 * gst_rtsp_client_set_thread_pool:
2357 * @client: a #GstRTSPClient
2358 * @pool: a #GstRTSPThreadPool
2360 * configure @pool to be used as the thread pool of @client.
2363 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2364 GstRTSPThreadPool * pool)
2366 GstRTSPClientPrivate *priv;
2367 GstRTSPThreadPool *old;
2369 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2371 priv = client->priv;
2374 g_object_ref (pool);
2376 g_mutex_lock (&priv->lock);
2377 old = priv->thread_pool;
2378 priv->thread_pool = pool;
2379 g_mutex_unlock (&priv->lock);
2382 g_object_unref (old);
2386 * gst_rtsp_client_get_thread_pool:
2387 * @client: a #GstRTSPClient
2389 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2391 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2395 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2397 GstRTSPClientPrivate *priv;
2398 GstRTSPThreadPool *result;
2400 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2402 priv = client->priv;
2404 g_mutex_lock (&priv->lock);
2405 if ((result = priv->thread_pool))
2406 g_object_ref (result);
2407 g_mutex_unlock (&priv->lock);
2413 * gst_rtsp_client_set_connection:
2414 * @client: a #GstRTSPClient
2415 * @conn: (transfer full): a #GstRTSPConnection
2417 * Set the #GstRTSPConnection of @client. This function takes ownership of
2420 * Returns: %TRUE on success.
2423 gst_rtsp_client_set_connection (GstRTSPClient * client,
2424 GstRTSPConnection * conn)
2426 GstRTSPClientPrivate *priv;
2427 GSocket *read_socket;
2428 GSocketAddress *address;
2430 GError *error = NULL;
2432 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2433 g_return_val_if_fail (conn != NULL, FALSE);
2435 priv = client->priv;
2437 read_socket = gst_rtsp_connection_get_read_socket (conn);
2439 if (!(address = g_socket_get_local_address (read_socket, &error)))
2442 g_free (priv->server_ip);
2443 /* keep the original ip that the client connected to */
2444 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2445 GInetAddress *iaddr;
2447 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2449 /* socket might be ipv6 but adress still ipv4 */
2450 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2451 priv->server_ip = g_inet_address_to_string (iaddr);
2452 g_object_unref (address);
2454 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2455 priv->server_ip = g_strdup ("unknown");
2458 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2459 priv->server_ip, priv->is_ipv6);
2461 url = gst_rtsp_connection_get_url (conn);
2462 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2464 priv->connection = conn;
2471 GST_ERROR ("could not get local address %s", error->message);
2472 g_error_free (error);
2478 * gst_rtsp_client_get_connection:
2479 * @client: a #GstRTSPClient
2481 * Get the #GstRTSPConnection of @client.
2483 * Returns: (transfer none): the #GstRTSPConnection of @client.
2484 * The connection object returned remains valid until the client is freed.
2487 gst_rtsp_client_get_connection (GstRTSPClient * client)
2489 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2491 return client->priv->connection;
2495 * gst_rtsp_client_set_send_func:
2496 * @client: a #GstRTSPClient
2497 * @func: a #GstRTSPClientSendFunc
2498 * @user_data: user data passed to @func
2499 * @notify: called when @user_data is no longer in use
2501 * Set @func as the callback that will be called when a new message needs to be
2502 * sent to the client. @user_data is passed to @func and @notify is called when
2503 * @user_data is no longer in use.
2505 * By default, the client will send the messages on the #GstRTSPConnection that
2506 * was configured with gst_rtsp_client_attach() was called.
2509 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2510 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2512 GstRTSPClientPrivate *priv;
2513 GDestroyNotify old_notify;
2516 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2518 priv = client->priv;
2520 g_mutex_lock (&priv->send_lock);
2521 priv->send_func = func;
2522 old_notify = priv->send_notify;
2523 old_data = priv->send_data;
2524 priv->send_notify = notify;
2525 priv->send_data = user_data;
2526 g_mutex_unlock (&priv->send_lock);
2529 old_notify (old_data);
2533 * gst_rtsp_client_handle_message:
2534 * @client: a #GstRTSPClient
2535 * @message: an #GstRTSPMessage
2537 * Let the client handle @message.
2539 * Returns: a #GstRTSPResult.
2542 gst_rtsp_client_handle_message (GstRTSPClient * client,
2543 GstRTSPMessage * message)
2545 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2546 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2548 switch (message->type) {
2549 case GST_RTSP_MESSAGE_REQUEST:
2550 handle_request (client, message);
2552 case GST_RTSP_MESSAGE_RESPONSE:
2553 handle_response (client, message);
2555 case GST_RTSP_MESSAGE_DATA:
2556 handle_data (client, message);
2565 * gst_rtsp_client_send_message:
2566 * @client: a #GstRTSPClient
2567 * @session: a #GstRTSPSession to send the message to or %NULL
2568 * @message: The #GstRTSPMessage to send
2570 * Send a message message to the remote end. @message must be a
2571 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
2574 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
2575 GstRTSPMessage * message)
2577 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2578 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2579 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
2580 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
2582 send_message (client, session, message, FALSE);
2587 static GstRTSPResult
2588 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2589 gboolean close, gpointer user_data)
2591 GstRTSPClientPrivate *priv = client->priv;
2593 /* send the response and store the seq number so we can wait until it's
2594 * written to the client to close the connection */
2595 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2596 &priv->close_seq : NULL);
2599 static GstRTSPResult
2600 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2603 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2606 static GstRTSPResult
2607 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2609 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2610 GstRTSPClientPrivate *priv = client->priv;
2612 if (priv->close_seq && priv->close_seq == cseq) {
2613 priv->close_seq = 0;
2614 close_connection (client);
2620 static GstRTSPResult
2621 closed (GstRTSPWatch * watch, gpointer user_data)
2623 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2624 GstRTSPClientPrivate *priv = client->priv;
2625 const gchar *tunnelid;
2627 GST_INFO ("client %p: connection closed", client);
2629 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2630 g_mutex_lock (&tunnels_lock);
2631 /* remove from tunnelids */
2632 g_hash_table_remove (tunnels, tunnelid);
2633 g_mutex_unlock (&tunnels_lock);
2636 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2641 static GstRTSPResult
2642 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2644 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2647 str = gst_rtsp_strresult (result);
2648 GST_INFO ("client %p: received an error %s", client, str);
2654 static GstRTSPResult
2655 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2656 GstRTSPMessage * message, guint id, gpointer user_data)
2658 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2661 str = gst_rtsp_strresult (result);
2663 ("client %p: error when handling message %p with id %d: %s",
2664 client, message, id, str);
2671 remember_tunnel (GstRTSPClient * client)
2673 GstRTSPClientPrivate *priv = client->priv;
2674 const gchar *tunnelid;
2676 /* store client in the pending tunnels */
2677 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2678 if (tunnelid == NULL)
2681 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2683 /* we can't have two clients connecting with the same tunnelid */
2684 g_mutex_lock (&tunnels_lock);
2685 if (g_hash_table_lookup (tunnels, tunnelid))
2686 goto tunnel_existed;
2688 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2689 g_mutex_unlock (&tunnels_lock);
2696 GST_ERROR ("client %p: no tunnelid provided", client);
2701 g_mutex_unlock (&tunnels_lock);
2702 GST_ERROR ("client %p: tunnel session %s already existed", client,
2708 static GstRTSPStatusCode
2709 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2711 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2712 GstRTSPClientPrivate *priv = client->priv;
2714 GST_INFO ("client %p: tunnel start (connection %p)", client,
2717 if (!remember_tunnel (client))
2720 return GST_RTSP_STS_OK;
2725 GST_ERROR ("client %p: error starting tunnel", client);
2726 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2730 static GstRTSPResult
2731 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2733 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2734 GstRTSPClientPrivate *priv = client->priv;
2736 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2739 /* ignore error, it'll only be a problem when the client does a POST again */
2740 remember_tunnel (client);
2745 static GstRTSPResult
2746 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2748 const gchar *tunnelid;
2749 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2750 GstRTSPClientPrivate *priv = client->priv;
2751 GstRTSPClient *oclient;
2752 GstRTSPClientPrivate *opriv;
2754 GST_INFO ("client %p: tunnel complete", client);
2756 /* find previous tunnel */
2757 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2758 if (tunnelid == NULL)
2761 g_mutex_lock (&tunnels_lock);
2762 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2765 /* remove the old client from the table. ref before because removing it will
2766 * remove the ref to it. */
2767 g_object_ref (oclient);
2768 g_hash_table_remove (tunnels, tunnelid);
2770 opriv = oclient->priv;
2772 if (opriv->watch == NULL)
2774 g_mutex_unlock (&tunnels_lock);
2776 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2777 opriv->connection, priv->connection);
2779 /* merge the tunnels into the first client */
2780 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2781 gst_rtsp_watch_reset (opriv->watch);
2782 g_object_unref (oclient);
2789 GST_ERROR ("client %p: no tunnelid provided", client);
2790 return GST_RTSP_ERROR;
2794 g_mutex_unlock (&tunnels_lock);
2795 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2796 return GST_RTSP_ERROR;
2800 g_mutex_unlock (&tunnels_lock);
2801 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2802 g_object_unref (oclient);
2803 return GST_RTSP_ERROR;
2807 static GstRTSPWatchFuncs watch_funcs = {
2819 client_watch_notify (GstRTSPClient * client)
2821 GstRTSPClientPrivate *priv = client->priv;
2823 GST_INFO ("client %p: watch destroyed", client);
2825 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2826 g_object_unref (client);
2830 * gst_rtsp_client_attach:
2831 * @client: a #GstRTSPClient
2832 * @context: (allow-none): a #GMainContext
2834 * Attaches @client to @context. When the mainloop for @context is run, the
2835 * client will be dispatched. When @context is NULL, the default context will be
2838 * This function should be called when the client properties and urls are fully
2839 * configured and the client is ready to start.
2841 * Returns: the ID (greater than 0) for the source within the GMainContext.
2844 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2846 GstRTSPClientPrivate *priv;
2849 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2850 priv = client->priv;
2851 g_return_val_if_fail (priv->connection != NULL, 0);
2852 g_return_val_if_fail (priv->watch == NULL, 0);
2854 /* create watch for the connection and attach */
2855 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2856 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2857 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2858 (GDestroyNotify) gst_rtsp_watch_unref);
2860 /* FIXME make this configurable. We don't want to do this yet because it will
2861 * be superceeded by a cache object later */
2862 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2864 GST_INFO ("attaching to context %p", context);
2865 res = gst_rtsp_watch_attach (priv->watch, context);
2871 * gst_rtsp_client_session_filter:
2872 * @client: a #GstRTSPClient
2873 * @func: (scope call): a callback
2874 * @user_data: user data passed to @func
2876 * Call @func for each session managed by @client. The result value of @func
2877 * determines what happens to the session. @func will be called with @client
2878 * locked so no further actions on @client can be performed from @func.
2880 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
2883 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
2885 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
2886 * will also be added with an additional ref to the result #GList of this
2889 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
2890 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2891 * element in the #GList should be unreffed before the list is freed.
2894 gst_rtsp_client_session_filter (GstRTSPClient * client,
2895 GstRTSPClientSessionFilterFunc func, gpointer user_data)
2897 GstRTSPClientPrivate *priv;
2898 GList *result, *walk, *next;
2900 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2901 g_return_val_if_fail (func != NULL, NULL);
2903 priv = client->priv;
2907 g_mutex_lock (&priv->lock);
2908 for (walk = priv->sessions; walk; walk = next) {
2909 GstRTSPSession *sess = walk->data;
2911 next = g_list_next (walk);
2913 switch (func (client, sess, user_data)) {
2914 case GST_RTSP_FILTER_REMOVE:
2915 /* stop watching the session and pretent it went away */
2916 client_cleanup_session (client, sess);
2918 case GST_RTSP_FILTER_REF:
2919 result = g_list_prepend (result, g_object_ref (sess));
2921 case GST_RTSP_FILTER_KEEP:
2926 g_mutex_unlock (&priv->lock);