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 <gst/sdp/gstmikey.h>
47 #include "rtsp-client.h"
49 #include "rtsp-params.h"
51 #define GST_RTSP_CLIENT_GET_PRIVATE(obj) \
52 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_CLIENT, GstRTSPClientPrivate))
55 * send_lock, lock, tunnels_lock
58 struct _GstRTSPClientPrivate
60 GMutex lock; /* protects everything else */
62 GstRTSPConnection *connection;
68 GstRTSPClientSendFunc send_func; /* protected by send_lock */
69 gpointer send_data; /* protected by send_lock */
70 GDestroyNotify send_notify; /* protected by send_lock */
72 GstRTSPSessionPool *session_pool;
73 GstRTSPMountPoints *mount_points;
75 GstRTSPThreadPool *thread_pool;
77 /* used to cache the media in the last requested DESCRIBE so that
78 * we can pick it up in the next SETUP immediately */
85 gboolean drop_backlog;
88 static GMutex tunnels_lock;
89 static GHashTable *tunnels; /* protected by tunnels_lock */
91 #define DEFAULT_SESSION_POOL NULL
92 #define DEFAULT_MOUNT_POINTS NULL
93 #define DEFAULT_DROP_BACKLOG TRUE
108 SIGNAL_OPTIONS_REQUEST,
109 SIGNAL_DESCRIBE_REQUEST,
110 SIGNAL_SETUP_REQUEST,
112 SIGNAL_PAUSE_REQUEST,
113 SIGNAL_TEARDOWN_REQUEST,
114 SIGNAL_SET_PARAMETER_REQUEST,
115 SIGNAL_GET_PARAMETER_REQUEST,
116 SIGNAL_HANDLE_RESPONSE,
120 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
121 #define GST_CAT_DEFAULT rtsp_client_debug
123 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
125 static void gst_rtsp_client_get_property (GObject * object, guint propid,
126 GValue * value, GParamSpec * pspec);
127 static void gst_rtsp_client_set_property (GObject * object, guint propid,
128 const GValue * value, GParamSpec * pspec);
129 static void gst_rtsp_client_finalize (GObject * obj);
131 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
132 static void client_session_finalized (GstRTSPClient * client,
133 GstRTSPSession * session);
134 static void unlink_session_transports (GstRTSPClient * client,
135 GstRTSPSession * session, GstRTSPSessionMedia * sessmedia);
136 static gboolean default_configure_client_media (GstRTSPClient * client,
137 GstRTSPMedia * media, GstRTSPStream * stream, GstRTSPContext * ctx);
138 static gboolean default_configure_client_transport (GstRTSPClient * client,
139 GstRTSPContext * ctx, GstRTSPTransport * ct);
140 static GstRTSPResult default_params_set (GstRTSPClient * client,
141 GstRTSPContext * ctx);
142 static GstRTSPResult default_params_get (GstRTSPClient * client,
143 GstRTSPContext * ctx);
144 static gchar *default_make_path_from_uri (GstRTSPClient * client,
145 const GstRTSPUrl * uri);
147 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
150 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
152 GObjectClass *gobject_class;
154 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
156 gobject_class = G_OBJECT_CLASS (klass);
158 gobject_class->get_property = gst_rtsp_client_get_property;
159 gobject_class->set_property = gst_rtsp_client_set_property;
160 gobject_class->finalize = gst_rtsp_client_finalize;
162 klass->create_sdp = create_sdp;
163 klass->configure_client_media = default_configure_client_media;
164 klass->configure_client_transport = default_configure_client_transport;
165 klass->params_set = default_params_set;
166 klass->params_get = default_params_get;
167 klass->make_path_from_uri = default_make_path_from_uri;
169 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
170 g_param_spec_object ("session-pool", "Session Pool",
171 "The session pool to use for client session",
172 GST_TYPE_RTSP_SESSION_POOL,
173 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
175 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
176 g_param_spec_object ("mount-points", "Mount Points",
177 "The mount points to use for client session",
178 GST_TYPE_RTSP_MOUNT_POINTS,
179 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
181 g_object_class_install_property (gobject_class, PROP_DROP_BACKLOG,
182 g_param_spec_boolean ("drop-backlog", "Drop Backlog",
183 "Drop data when the backlog queue is full",
184 DEFAULT_DROP_BACKLOG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
186 gst_rtsp_client_signals[SIGNAL_CLOSED] =
187 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
188 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
189 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
191 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
192 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
193 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
194 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
196 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
197 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
198 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
199 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
202 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
203 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
204 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
205 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
208 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
209 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
210 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
211 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
214 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
215 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
216 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
217 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
220 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
221 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
222 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
223 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
226 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
227 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
228 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
229 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
232 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
233 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
234 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
235 set_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
236 G_TYPE_NONE, 1, G_TYPE_POINTER);
238 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
239 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
240 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
241 get_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
242 G_TYPE_NONE, 1, G_TYPE_POINTER);
244 gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE] =
245 g_signal_new ("handle-response", G_TYPE_FROM_CLASS (klass),
246 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
247 handle_response), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
248 G_TYPE_NONE, 1, G_TYPE_POINTER);
251 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
252 g_mutex_init (&tunnels_lock);
254 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
258 gst_rtsp_client_init (GstRTSPClient * client)
260 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
264 g_mutex_init (&priv->lock);
265 g_mutex_init (&priv->send_lock);
267 priv->drop_backlog = DEFAULT_DROP_BACKLOG;
270 static GstRTSPFilterResult
271 filter_session (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
274 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
276 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
277 unlink_session_transports (client, sess, sessmedia);
279 /* unmanage the media in the session */
280 return GST_RTSP_FILTER_REMOVE;
284 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
286 /* unlink all media managed in this session */
287 gst_rtsp_session_filter (session, filter_session, client);
291 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
293 GstRTSPClientPrivate *priv = client->priv;
296 for (walk = priv->sessions; walk; walk = g_list_next (walk)) {
297 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
299 /* we already know about this session */
300 if (msession == session)
304 GST_INFO ("watching session %p", session);
306 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
308 priv->sessions = g_list_prepend (priv->sessions, session);
312 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session)
314 GstRTSPClientPrivate *priv = client->priv;
316 GST_INFO ("unwatching session %p", session);
318 g_object_weak_unref (G_OBJECT (session),
319 (GWeakNotify) client_session_finalized, client);
320 priv->sessions = g_list_remove (priv->sessions, session);
324 client_cleanup_session (GstRTSPClient * client, GstRTSPSession * session)
326 g_object_weak_unref (G_OBJECT (session),
327 (GWeakNotify) client_session_finalized, client);
328 client_unlink_session (client, session);
332 client_cleanup_sessions (GstRTSPClient * client)
334 GstRTSPClientPrivate *priv = client->priv;
337 /* remove weak-ref from sessions */
338 for (sessions = priv->sessions; sessions; sessions = g_list_next (sessions)) {
339 client_cleanup_session (client, (GstRTSPSession *) sessions->data);
341 g_list_free (priv->sessions);
342 priv->sessions = NULL;
345 /* A client is finalized when the connection is broken */
347 gst_rtsp_client_finalize (GObject * obj)
349 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
350 GstRTSPClientPrivate *priv = client->priv;
352 GST_INFO ("finalize client %p", client);
355 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
356 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
359 g_source_destroy ((GSource *) priv->watch);
361 client_cleanup_sessions (client);
363 if (priv->connection)
364 gst_rtsp_connection_free (priv->connection);
365 if (priv->session_pool)
366 g_object_unref (priv->session_pool);
367 if (priv->mount_points)
368 g_object_unref (priv->mount_points);
370 g_object_unref (priv->auth);
371 if (priv->thread_pool)
372 g_object_unref (priv->thread_pool);
377 gst_rtsp_media_unprepare (priv->media);
378 g_object_unref (priv->media);
381 g_free (priv->server_ip);
382 g_mutex_clear (&priv->lock);
383 g_mutex_clear (&priv->send_lock);
385 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
389 gst_rtsp_client_get_property (GObject * object, guint propid,
390 GValue * value, GParamSpec * pspec)
392 GstRTSPClient *client = GST_RTSP_CLIENT (object);
393 GstRTSPClientPrivate *priv = client->priv;
396 case PROP_SESSION_POOL:
397 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
399 case PROP_MOUNT_POINTS:
400 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
402 case PROP_DROP_BACKLOG:
403 g_value_set_boolean (value, priv->drop_backlog);
406 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
411 gst_rtsp_client_set_property (GObject * object, guint propid,
412 const GValue * value, GParamSpec * pspec)
414 GstRTSPClient *client = GST_RTSP_CLIENT (object);
415 GstRTSPClientPrivate *priv = client->priv;
418 case PROP_SESSION_POOL:
419 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
421 case PROP_MOUNT_POINTS:
422 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
424 case PROP_DROP_BACKLOG:
425 g_mutex_lock (&priv->lock);
426 priv->drop_backlog = g_value_get_boolean (value);
427 g_mutex_unlock (&priv->lock);
430 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
435 * gst_rtsp_client_new:
437 * Create a new #GstRTSPClient instance.
439 * Returns: (transfer full): a new #GstRTSPClient
442 gst_rtsp_client_new (void)
444 GstRTSPClient *result;
446 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
452 send_message (GstRTSPClient * client, GstRTSPContext * ctx,
453 GstRTSPMessage * message, gboolean close)
455 GstRTSPClientPrivate *priv = client->priv;
457 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
458 "GStreamer RTSP server");
460 /* remove any previous header */
461 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
463 /* add the new session header for new session ids */
465 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
466 gst_rtsp_session_get_header (ctx->session));
469 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
470 gst_rtsp_message_dump (message);
474 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
476 g_mutex_lock (&priv->send_lock);
478 priv->send_func (client, message, close, priv->send_data);
479 g_mutex_unlock (&priv->send_lock);
481 gst_rtsp_message_unset (message);
485 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
486 GstRTSPContext * ctx)
488 gst_rtsp_message_init_response (ctx->response, code,
489 gst_rtsp_status_as_text (code), ctx->request);
493 send_message (client, ctx, ctx->response, FALSE);
497 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
499 if (path1 == NULL || path2 == NULL)
502 if (strlen (path1) != len2)
505 if (strncmp (path1, path2, len2))
511 /* this function is called to initially find the media for the DESCRIBE request
512 * but is cached for when the same client (without breaking the connection) is
513 * doing a setup for the exact same url. */
514 static GstRTSPMedia *
515 find_media (GstRTSPClient * client, GstRTSPContext * ctx, gchar * path,
518 GstRTSPClientPrivate *priv = client->priv;
519 GstRTSPMediaFactory *factory;
523 /* find the longest matching factory for the uri first */
524 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
528 ctx->factory = factory;
530 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
531 goto no_factory_access;
533 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
539 path_len = strlen (path);
541 if (!paths_are_equal (priv->path, path, path_len)) {
542 GstRTSPThread *thread;
544 /* remove any previously cached values before we try to construct a new
550 gst_rtsp_media_unprepare (priv->media);
551 g_object_unref (priv->media);
555 /* prepare the media and add it to the pipeline */
556 if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
561 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
562 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
566 /* prepare the media */
567 if (!(gst_rtsp_media_prepare (media, thread)))
570 /* now keep track of the uri and the media */
571 priv->path = g_strndup (path, path_len);
574 /* we have seen this path before, used cached media */
577 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
580 g_object_unref (factory);
584 g_object_ref (media);
591 GST_ERROR ("client %p: no factory for path %s", client, path);
592 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
597 GST_ERROR ("client %p: not authorized to see factory path %s", client,
599 /* error reply is already sent */
604 GST_ERROR ("client %p: not authorized for factory path %s", client, path);
605 /* error reply is already sent */
610 GST_ERROR ("client %p: can't create media", client);
611 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
612 g_object_unref (factory);
618 GST_ERROR ("client %p: can't create thread", client);
619 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
620 g_object_unref (media);
622 g_object_unref (factory);
628 GST_ERROR ("client %p: can't prepare media", client);
629 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
630 g_object_unref (media);
632 g_object_unref (factory);
639 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
641 GstRTSPClientPrivate *priv = client->priv;
642 GstRTSPMessage message = { 0 };
647 gst_rtsp_message_init_data (&message, channel);
649 /* FIXME, need some sort of iovec RTSPMessage here */
650 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
653 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
655 g_mutex_lock (&priv->send_lock);
657 priv->send_func (client, &message, FALSE, priv->send_data);
658 g_mutex_unlock (&priv->send_lock);
660 gst_rtsp_message_steal_body (&message, &data, &usize);
661 gst_buffer_unmap (buffer, &map_info);
663 gst_rtsp_message_unset (&message);
669 link_transport (GstRTSPClient * client, GstRTSPSession * session,
670 GstRTSPStreamTransport * trans)
672 GstRTSPClientPrivate *priv = client->priv;
674 GST_DEBUG ("client %p: linking transport %p", client, trans);
676 gst_rtsp_stream_transport_set_callbacks (trans,
677 (GstRTSPSendFunc) do_send_data,
678 (GstRTSPSendFunc) do_send_data, client, NULL);
680 priv->transports = g_list_prepend (priv->transports, trans);
682 /* make sure our session can't expire */
683 gst_rtsp_session_prevent_expire (session);
687 link_session_transports (GstRTSPClient * client, GstRTSPSession * session,
688 GstRTSPSessionMedia * sessmedia)
693 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
694 for (i = 0; i < n_streams; i++) {
695 GstRTSPStreamTransport *trans;
696 const GstRTSPTransport *tr;
698 /* get the transport, if there is no transport configured, skip this stream */
699 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
703 tr = gst_rtsp_stream_transport_get_transport (trans);
705 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
706 /* for TCP, link the stream to the TCP connection of the client */
707 link_transport (client, session, trans);
713 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
714 GstRTSPStreamTransport * trans)
716 GstRTSPClientPrivate *priv = client->priv;
718 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
720 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
722 priv->transports = g_list_remove (priv->transports, trans);
724 /* our session can now expire */
725 gst_rtsp_session_allow_expire (session);
729 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
730 GstRTSPSessionMedia * sessmedia)
735 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
736 for (i = 0; i < n_streams; i++) {
737 GstRTSPStreamTransport *trans;
738 const GstRTSPTransport *tr;
740 /* get the transport, if there is no transport configured, skip this stream */
741 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
745 tr = gst_rtsp_stream_transport_get_transport (trans);
747 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
748 /* for TCP, unlink the stream from the TCP connection of the client */
749 unlink_transport (client, session, trans);
755 close_connection (GstRTSPClient * client)
757 GstRTSPClientPrivate *priv = client->priv;
758 const gchar *tunnelid;
760 GST_DEBUG ("client %p: closing connection", client);
762 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
763 g_mutex_lock (&tunnels_lock);
764 /* remove from tunnelids */
765 g_hash_table_remove (tunnels, tunnelid);
766 g_mutex_unlock (&tunnels_lock);
769 gst_rtsp_connection_close (priv->connection);
773 default_make_path_from_uri (GstRTSPClient * client, const GstRTSPUrl * uri)
778 path = g_strconcat (uri->abspath, "?", uri->query, NULL);
780 path = g_strdup (uri->abspath);
786 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
788 GstRTSPClientPrivate *priv = client->priv;
789 GstRTSPClientClass *klass;
790 GstRTSPSession *session;
791 GstRTSPSessionMedia *sessmedia;
792 GstRTSPStatusCode code;
799 session = ctx->session;
804 klass = GST_RTSP_CLIENT_GET_CLASS (client);
805 path = klass->make_path_from_uri (client, ctx->uri);
807 /* get a handle to the configuration of the media in the session */
808 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
812 /* only aggregate control for now.. */
813 if (path[matched] != '\0')
818 ctx->sessmedia = sessmedia;
820 /* we emit the signal before closing the connection */
821 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
824 /* make sure we unblock the backlog and don't accept new messages
826 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
828 /* unlink the all TCP callbacks */
829 unlink_session_transports (client, session, sessmedia);
831 /* remove the session from the watched sessions */
832 client_unwatch_session (client, session);
834 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
836 /* allow messages again so that we can send the reply */
837 gst_rtsp_watch_set_flushing (priv->watch, FALSE);
839 /* unmanage the media in the session, returns false if all media session
841 if (!gst_rtsp_session_release_media (session, sessmedia)) {
842 /* remove the session */
843 gst_rtsp_session_pool_remove (priv->session_pool, session);
845 /* construct the response now */
846 code = GST_RTSP_STS_OK;
847 gst_rtsp_message_init_response (ctx->response, code,
848 gst_rtsp_status_as_text (code), ctx->request);
850 send_message (client, ctx, ctx->response, TRUE);
857 GST_ERROR ("client %p: no session", client);
858 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
863 GST_ERROR ("client %p: no uri supplied", client);
864 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
869 GST_ERROR ("client %p: no media for uri", client);
870 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
876 GST_ERROR ("client %p: no aggregate path %s", client, path);
877 send_generic_response (client,
878 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
885 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
889 res = gst_rtsp_params_set (client, ctx);
895 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
899 res = gst_rtsp_params_get (client, ctx);
905 handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
911 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
912 if (res != GST_RTSP_OK)
916 /* no body, keep-alive request */
917 send_generic_response (client, GST_RTSP_STS_OK, ctx);
919 /* there is a body, handle the params */
920 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
921 if (res != GST_RTSP_OK)
924 send_message (client, ctx, ctx->response, FALSE);
927 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
935 GST_ERROR ("client %p: bad request", client);
936 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
942 handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
948 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
949 if (res != GST_RTSP_OK)
953 /* no body, keep-alive request */
954 send_generic_response (client, GST_RTSP_STS_OK, ctx);
956 /* there is a body, handle the params */
957 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
958 if (res != GST_RTSP_OK)
961 send_message (client, ctx, ctx->response, FALSE);
964 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
972 GST_ERROR ("client %p: bad request", client);
973 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
979 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
981 GstRTSPSession *session;
982 GstRTSPClientClass *klass;
983 GstRTSPSessionMedia *sessmedia;
984 GstRTSPStatusCode code;
985 GstRTSPState rtspstate;
989 if (!(session = ctx->session))
995 klass = GST_RTSP_CLIENT_GET_CLASS (client);
996 path = klass->make_path_from_uri (client, ctx->uri);
998 /* get a handle to the configuration of the media in the session */
999 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1003 if (path[matched] != '\0')
1008 ctx->sessmedia = sessmedia;
1010 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1011 /* the session state must be playing or recording */
1012 if (rtspstate != GST_RTSP_STATE_PLAYING &&
1013 rtspstate != GST_RTSP_STATE_RECORDING)
1016 /* unlink the all TCP callbacks */
1017 unlink_session_transports (client, session, sessmedia);
1019 /* then pause sending */
1020 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
1022 /* construct the response now */
1023 code = GST_RTSP_STS_OK;
1024 gst_rtsp_message_init_response (ctx->response, code,
1025 gst_rtsp_status_as_text (code), ctx->request);
1027 send_message (client, ctx, ctx->response, FALSE);
1029 /* the state is now READY */
1030 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1032 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
1039 GST_ERROR ("client %p: no seesion", client);
1040 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1045 GST_ERROR ("client %p: no uri supplied", client);
1046 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1051 GST_ERROR ("client %p: no media for uri", client);
1052 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1058 GST_ERROR ("client %p: no aggregate path %s", client, path);
1059 send_generic_response (client,
1060 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1066 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
1067 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1073 /* convert @url and @path to a URL used as a content base for the factory
1074 * located at @path */
1076 make_base_url (GstRTSPClient * client, GstRTSPUrl * url, const gchar * path)
1082 /* check for trailing '/' and append one */
1083 trail = (path[strlen (path) - 1] != '/' ? "/" : "");
1088 tmp.abspath = g_strdup_printf ("%s%s", path, trail);
1090 result = gst_rtsp_url_get_request_uri (&tmp);
1091 g_free (tmp.abspath);
1097 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
1099 GstRTSPSession *session;
1100 GstRTSPClientClass *klass;
1101 GstRTSPSessionMedia *sessmedia;
1102 GstRTSPMedia *media;
1103 GstRTSPStatusCode code;
1106 GstRTSPTimeRange *range;
1108 GstRTSPState rtspstate;
1109 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1110 gchar *path, *rtpinfo;
1113 if (!(session = ctx->session))
1116 if (!(uri = ctx->uri))
1119 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1120 path = klass->make_path_from_uri (client, uri);
1122 /* get a handle to the configuration of the media in the session */
1123 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1127 if (path[matched] != '\0')
1132 ctx->sessmedia = sessmedia;
1133 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1135 /* the session state must be playing or ready */
1136 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1137 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1140 /* in play we first unsuspend, media could be suspended from SDP or PAUSED */
1141 if (!gst_rtsp_media_unsuspend (media))
1142 goto unsuspend_failed;
1144 /* parse the range header if we have one */
1145 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1146 if (res == GST_RTSP_OK) {
1147 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1148 /* we have a range, seek to the position */
1150 gst_rtsp_media_seek (media, range);
1151 gst_rtsp_range_free (range);
1155 /* link the all TCP callbacks */
1156 link_session_transports (client, session, sessmedia);
1158 /* grab RTPInfo from the media now */
1159 rtpinfo = gst_rtsp_session_media_get_rtpinfo (sessmedia);
1161 /* construct the response now */
1162 code = GST_RTSP_STS_OK;
1163 gst_rtsp_message_init_response (ctx->response, code,
1164 gst_rtsp_status_as_text (code), ctx->request);
1166 /* add the RTP-Info header */
1168 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO,
1172 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1174 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1176 send_message (client, ctx, ctx->response, FALSE);
1178 /* start playing after sending the response */
1179 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1181 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1183 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1190 GST_ERROR ("client %p: no session", client);
1191 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1196 GST_ERROR ("client %p: no uri supplied", client);
1197 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1202 GST_ERROR ("client %p: media not found", client);
1203 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1208 GST_ERROR ("client %p: no aggregate path %s", client, path);
1209 send_generic_response (client,
1210 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1216 GST_ERROR ("client %p: not PLAYING or READY", client);
1217 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1223 GST_ERROR ("client %p: unsuspend failed", client);
1224 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1230 do_keepalive (GstRTSPSession * session)
1232 GST_INFO ("keep session %p alive", session);
1233 gst_rtsp_session_touch (session);
1236 /* parse @transport and return a valid transport in @tr. only transports
1237 * supported by @stream are returned. Returns FALSE if no valid transport
1240 parse_transport (const char *transport, GstRTSPStream * stream,
1241 GstRTSPTransport * tr)
1248 gst_rtsp_transport_init (tr);
1250 GST_DEBUG ("parsing transports %s", transport);
1252 transports = g_strsplit (transport, ",", 0);
1254 /* loop through the transports, try to parse */
1255 for (i = 0; transports[i]; i++) {
1256 res = gst_rtsp_transport_parse (transports[i], tr);
1257 if (res != GST_RTSP_OK) {
1258 /* no valid transport, search some more */
1259 GST_WARNING ("could not parse transport %s", transports[i]);
1263 /* we have a transport, see if it's supported */
1264 if (!gst_rtsp_stream_is_transport_supported (stream, tr)) {
1265 GST_WARNING ("unsupported transport %s", transports[i]);
1269 /* we have a valid transport */
1270 GST_INFO ("found valid transport %s", transports[i]);
1275 gst_rtsp_transport_init (tr);
1277 g_strfreev (transports);
1283 default_configure_client_media (GstRTSPClient * client, GstRTSPMedia * media,
1284 GstRTSPStream * stream, GstRTSPContext * ctx)
1286 GstRTSPMessage *request = ctx->request;
1287 gchar *blocksize_str;
1289 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1290 &blocksize_str, 0) == GST_RTSP_OK) {
1294 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1295 if (end == blocksize_str)
1298 /* we don't want to change the mtu when this media
1299 * can be shared because it impacts other clients */
1300 if (gst_rtsp_media_is_shared (media))
1303 if (blocksize > G_MAXUINT)
1304 blocksize = G_MAXUINT;
1306 gst_rtsp_stream_set_mtu (stream, blocksize);
1314 GST_ERROR_OBJECT (client, "failed to parse blocksize");
1315 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1321 default_configure_client_transport (GstRTSPClient * client,
1322 GstRTSPContext * ctx, GstRTSPTransport * ct)
1324 GstRTSPClientPrivate *priv = client->priv;
1326 /* we have a valid transport now, set the destination of the client. */
1327 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1328 gboolean use_client_settings;
1330 use_client_settings =
1331 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1333 if (ct->destination && use_client_settings) {
1334 GstRTSPAddress *addr;
1336 addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1337 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1342 gst_rtsp_address_free (addr);
1344 GstRTSPAddress *addr;
1345 GSocketFamily family;
1347 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1349 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1353 g_free (ct->destination);
1354 ct->destination = g_strdup (addr->address);
1355 ct->port.min = addr->port;
1356 ct->port.max = addr->port + addr->n_ports - 1;
1357 ct->ttl = addr->ttl;
1359 gst_rtsp_address_free (addr);
1364 url = gst_rtsp_connection_get_url (priv->connection);
1365 g_free (ct->destination);
1366 ct->destination = g_strdup (url->host);
1368 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1369 /* check if the client selected channels for TCP */
1370 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1371 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1381 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1386 static GstRTSPTransport *
1387 make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
1388 GstRTSPTransport * ct)
1390 GstRTSPTransport *st;
1392 GSocketFamily family;
1394 /* prepare the server transport */
1395 gst_rtsp_transport_new (&st);
1397 st->trans = ct->trans;
1398 st->profile = ct->profile;
1399 st->lower_transport = ct->lower_transport;
1401 addr = g_inet_address_new_from_string (ct->destination);
1404 GST_ERROR ("failed to get inet addr from client destination");
1405 family = G_SOCKET_FAMILY_IPV4;
1407 family = g_inet_address_get_family (addr);
1408 g_object_unref (addr);
1412 switch (st->lower_transport) {
1413 case GST_RTSP_LOWER_TRANS_UDP:
1414 st->client_port = ct->client_port;
1415 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
1417 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1418 st->port = ct->port;
1419 st->destination = g_strdup (ct->destination);
1422 case GST_RTSP_LOWER_TRANS_TCP:
1423 st->interleaved = ct->interleaved;
1428 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
1434 mikey_apply_policy (GstCaps * caps, GstMIKEYMessage * msg, guint8 policy)
1436 const gchar *srtp_cipher;
1437 const gchar *srtp_auth;
1438 const GstMIKEYPayload *sp;
1441 /* loop over Security policy until we find one containing policy */
1443 if ((sp = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, i)) == NULL)
1446 if (((GstMIKEYPayloadSP *) sp)->policy == policy)
1450 /* the default ciphers */
1451 srtp_cipher = "aes-128-icm";
1452 srtp_auth = "hmac-sha1-80";
1454 /* now override the defaults with what is in the Security Policy */
1458 /* collect all the params and go over them */
1459 len = gst_mikey_payload_sp_get_n_params (sp);
1460 for (i = 0; i < len; i++) {
1461 const GstMIKEYPayloadSPParam *param =
1462 gst_mikey_payload_sp_get_param (sp, i);
1464 switch (param->type) {
1465 case GST_MIKEY_SP_SRTP_ENC_ALG:
1466 switch (param->val[0]) {
1468 srtp_cipher = "null";
1472 srtp_cipher = "aes-128-icm";
1478 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1479 switch (param->val[0]) {
1485 srtp_auth = "hmac-sha1-80";
1491 case GST_MIKEY_SP_SRTP_SRTP_ENC:
1493 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
1500 /* now configure the SRTP parameters */
1501 gst_caps_set_simple (caps,
1502 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
1503 "srtp-auth", G_TYPE_STRING, srtp_auth,
1504 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
1505 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
1511 handle_mikey_data (GstRTSPClient * client, GstRTSPContext * ctx,
1512 guint8 * data, gsize size)
1514 GstMIKEYMessage *msg;
1516 GstCaps *caps = NULL;
1517 GstMIKEYPayloadKEMAC *kemac;
1518 const GstMIKEYPayloadKeyData *pkd;
1521 /* the MIKEY message contains a CSB or crypto session bundle. It is a
1522 * set of Crypto Sessions protected with the same master key.
1523 * In the context of SRTP, an RTP and its RTCP stream is part of a
1525 if ((msg = gst_mikey_message_new_from_data (data, size, NULL, NULL)) == NULL)
1528 /* we can only handle SRTP crypto sessions for now */
1529 if (msg->map_type != GST_MIKEY_MAP_TYPE_SRTP)
1530 goto invalid_map_type;
1532 /* get the number of crypto sessions. This maps SSRC to its
1533 * security parameters */
1534 n_cs = gst_mikey_message_get_n_cs (msg);
1536 goto no_crypto_sessions;
1538 /* we also need keys */
1539 if (!(kemac = (GstMIKEYPayloadKEMAC *) gst_mikey_message_find_payload
1540 (msg, GST_MIKEY_PT_KEMAC, 0)))
1543 /* we don't support encrypted keys */
1544 if (kemac->enc_alg != GST_MIKEY_ENC_NULL
1545 || kemac->mac_alg != GST_MIKEY_MAC_NULL)
1546 goto unsupported_encryption;
1548 /* get Key data sub-payload */
1549 pkd = (const GstMIKEYPayloadKeyData *)
1550 gst_mikey_payload_kemac_get_sub (&kemac->pt, 0);
1553 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
1556 /* go over all crypto sessions and create the security policy for each
1558 for (i = 0; i < n_cs; i++) {
1559 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
1561 caps = gst_caps_new_simple ("application/x-srtp",
1562 "ssrc", G_TYPE_UINT, map->ssrc,
1563 "roc", G_TYPE_UINT, map->roc, "srtp-key", GST_TYPE_BUFFER, key, NULL);
1564 mikey_apply_policy (caps, msg, map->policy);
1566 gst_rtsp_stream_update_crypto (ctx->stream, map->ssrc, caps);
1567 gst_caps_unref (caps);
1569 gst_mikey_message_free (msg);
1576 GST_DEBUG_OBJECT (client, "failed to parse MIKEY message");
1581 GST_DEBUG_OBJECT (client, "invalid map type %d", msg->map_type);
1582 goto cleanup_message;
1586 GST_DEBUG_OBJECT (client, "no crypto sessions");
1587 goto cleanup_message;
1591 GST_DEBUG_OBJECT (client, "no keys found");
1592 goto cleanup_message;
1594 unsupported_encryption:
1596 GST_DEBUG_OBJECT (client, "unsupported key encryption");
1597 goto cleanup_message;
1601 gst_mikey_message_free (msg);
1606 #define IS_STRIP_CHAR(c) (g_ascii_isspace ((guchar)(c)) || ((c) == '\"'))
1609 strip_chars (gchar * str)
1616 if (!IS_STRIP_CHAR (str[len]))
1620 for (s = str; *s && IS_STRIP_CHAR (*s); s++);
1621 memmove (str, s, len + 1);
1625 * KeyMgmt = "KeyMgmt" ":" key-mgmt-spec 0*("," key-mgmt-spec)
1626 * key-mgmt-spec = "prot" "=" KMPID ";" ["uri" "=" %x22 URI %x22 ";"]
1629 handle_keymgmt (GstRTSPClient * client, GstRTSPContext * ctx, gchar * keymgmt)
1634 specs = g_strsplit (keymgmt, ",", 0);
1635 for (i = 0; specs[i]; i++) {
1638 split = g_strsplit (specs[i], ";", 0);
1639 for (j = 0; split[j]; j++) {
1640 g_strstrip (split[j]);
1641 if (g_str_has_prefix (split[j], "prot=")) {
1642 g_strstrip (split[j] + 5);
1643 if (!g_str_equal (split[j] + 5, "mikey"))
1645 GST_DEBUG ("found mikey");
1646 } else if (g_str_has_prefix (split[j], "uri=")) {
1647 strip_chars (split[j] + 4);
1648 GST_DEBUG ("found uri '%s'", split[j] + 4);
1649 } else if (g_str_has_prefix (split[j], "data=")) {
1652 strip_chars (split[j] + 5);
1653 GST_DEBUG ("found data '%s'", split[j] + 5);
1654 data = g_base64_decode_inplace (split[j] + 5, &size);
1655 handle_mikey_data (client, ctx, data, size);
1663 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
1665 GstRTSPClientPrivate *priv = client->priv;
1668 gchar *transport, *keymgmt;
1669 GstRTSPTransport *ct, *st;
1670 GstRTSPStatusCode code;
1671 GstRTSPSession *session;
1672 GstRTSPStreamTransport *trans;
1674 GstRTSPSessionMedia *sessmedia;
1675 GstRTSPMedia *media;
1676 GstRTSPStream *stream;
1677 GstRTSPState rtspstate;
1678 GstRTSPClientClass *klass;
1679 gchar *path, *control;
1686 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1687 path = klass->make_path_from_uri (client, uri);
1689 /* parse the transport */
1691 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
1693 if (res != GST_RTSP_OK)
1696 /* we create the session after parsing stuff so that we don't make
1697 * a session for malformed requests */
1698 if (priv->session_pool == NULL)
1701 session = ctx->session;
1704 g_object_ref (session);
1705 /* get a handle to the configuration of the media in the session, this can
1706 * return NULL if this is a new url to manage in this session. */
1707 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1709 /* we need a new media configuration in this session */
1713 /* we have no session media, find one and manage it */
1714 if (sessmedia == NULL) {
1715 /* get a handle to the configuration of the media in the session */
1716 media = find_media (client, ctx, path, &matched);
1718 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1719 g_object_ref (media);
1721 goto media_not_found;
1723 /* no media, not found then */
1725 goto media_not_found_no_reply;
1727 if (path[matched] == '\0')
1728 goto control_not_found;
1730 /* path is what matched. */
1731 path[matched] = '\0';
1732 /* control is remainder */
1733 control = &path[matched + 1];
1735 /* find the stream now using the control part */
1736 stream = gst_rtsp_media_find_stream (media, control);
1738 goto stream_not_found;
1740 /* now we have a uri identifying a valid media and stream */
1741 ctx->stream = stream;
1744 if (session == NULL) {
1745 /* create a session if this fails we probably reached our session limit or
1747 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1748 goto service_unavailable;
1750 /* make sure this client is closed when the session is closed */
1751 client_watch_session (client, session);
1753 /* signal new session */
1754 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1757 ctx->session = session;
1760 if (sessmedia == NULL) {
1761 /* manage the media in our session now, if not done already */
1762 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1763 /* if we stil have no media, error */
1764 if (sessmedia == NULL)
1765 goto sessmedia_unavailable;
1767 g_object_unref (media);
1770 ctx->sessmedia = sessmedia;
1772 if (!klass->configure_client_media (client, media, stream, ctx))
1773 goto configure_media_failed_no_reply;
1775 gst_rtsp_transport_new (&ct);
1777 /* parse and find a usable supported transport */
1778 if (!parse_transport (transport, stream, ct))
1779 goto unsupported_transports;
1781 /* update the client transport */
1782 if (!klass->configure_client_transport (client, ctx, ct))
1783 goto unsupported_client_transport;
1785 /* parse the keymgmt */
1786 if (gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_KEYMGMT,
1787 &keymgmt, 0) == GST_RTSP_OK) {
1788 if (!handle_keymgmt (client, ctx, keymgmt))
1792 /* set in the session media transport */
1793 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1795 /* configure the url used to set this transport, this we will use when
1796 * generating the response for the PLAY request */
1797 gst_rtsp_stream_transport_set_url (trans, uri);
1799 /* configure keepalive for this transport */
1800 gst_rtsp_stream_transport_set_keepalive (trans,
1801 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1803 /* create and serialize the server transport */
1804 st = make_server_transport (client, ctx, ct);
1805 trans_str = gst_rtsp_transport_as_text (st);
1806 gst_rtsp_transport_free (st);
1808 /* construct the response now */
1809 code = GST_RTSP_STS_OK;
1810 gst_rtsp_message_init_response (ctx->response, code,
1811 gst_rtsp_status_as_text (code), ctx->request);
1813 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
1817 send_message (client, ctx, ctx->response, FALSE);
1819 /* update the state */
1820 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1821 switch (rtspstate) {
1822 case GST_RTSP_STATE_PLAYING:
1823 case GST_RTSP_STATE_RECORDING:
1824 case GST_RTSP_STATE_READY:
1825 /* no state change */
1828 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1831 g_object_unref (session);
1834 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
1841 GST_ERROR ("client %p: no uri", client);
1842 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1847 GST_ERROR ("client %p: no transport", client);
1848 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1853 GST_ERROR ("client %p: no session pool configured", client);
1854 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1857 media_not_found_no_reply:
1859 GST_ERROR ("client %p: media '%s' not found", client, path);
1860 /* error reply is already sent */
1865 GST_ERROR ("client %p: media '%s' not found", client, path);
1866 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1871 GST_ERROR ("client %p: no control in path '%s'", client, path);
1872 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1873 g_object_unref (media);
1878 GST_ERROR ("client %p: stream '%s' not found", client, control);
1879 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1880 g_object_unref (media);
1883 service_unavailable:
1885 GST_ERROR ("client %p: can't create session", client);
1886 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1887 g_object_unref (media);
1890 sessmedia_unavailable:
1892 GST_ERROR ("client %p: can't create session media", client);
1893 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1894 g_object_unref (media);
1895 goto cleanup_session;
1897 configure_media_failed_no_reply:
1899 GST_ERROR ("client %p: configure_media failed", client);
1900 /* error reply is already sent */
1901 goto cleanup_session;
1903 unsupported_transports:
1905 GST_ERROR ("client %p: unsupported transports", client);
1906 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1907 goto cleanup_transport;
1909 unsupported_client_transport:
1911 GST_ERROR ("client %p: unsupported client transport", client);
1912 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1913 goto cleanup_transport;
1917 GST_ERROR ("client %p: keymgmt error", client);
1918 send_generic_response (client, GST_RTSP_STS_KEY_MANAGEMENT_FAILURE, ctx);
1919 goto cleanup_transport;
1923 gst_rtsp_transport_free (ct);
1925 g_object_unref (session);
1932 static GstSDPMessage *
1933 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1935 GstRTSPClientPrivate *priv = client->priv;
1940 gst_sdp_message_new (&sdp);
1942 /* some standard things first */
1943 gst_sdp_message_set_version (sdp, "0");
1950 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1953 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1954 gst_sdp_message_set_information (sdp, "rtsp-server");
1955 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1956 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1957 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1958 gst_sdp_message_add_attribute (sdp, "control", "*");
1960 info.is_ipv6 = priv->is_ipv6;
1961 info.server_ip = priv->server_ip;
1963 /* create an SDP for the media object */
1964 if (!gst_rtsp_media_setup_sdp (media, sdp, &info))
1972 GST_ERROR ("client %p: could not create SDP", client);
1973 gst_sdp_message_free (sdp);
1978 /* for the describe we must generate an SDP */
1980 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
1982 GstRTSPClientPrivate *priv = client->priv;
1987 GstRTSPMedia *media;
1988 GstRTSPClientClass *klass;
1990 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1995 /* check what kind of format is accepted, we don't really do anything with it
1996 * and always return SDP for now. */
2001 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
2003 if (res == GST_RTSP_ENOTIMPL)
2006 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
2010 if (!priv->mount_points)
2011 goto no_mount_points;
2013 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
2016 /* find the media object for the uri */
2017 if (!(media = find_media (client, ctx, path, NULL)))
2020 /* create an SDP for the media object on this client */
2021 if (!(sdp = klass->create_sdp (client, media)))
2024 /* we suspend after the describe */
2025 gst_rtsp_media_suspend (media);
2026 g_object_unref (media);
2028 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2029 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2031 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
2034 /* content base for some clients that might screw up creating the setup uri */
2035 str = make_base_url (client, ctx->uri, path);
2038 GST_INFO ("adding content-base: %s", str);
2039 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE, str);
2041 /* add SDP to the response body */
2042 str = gst_sdp_message_as_text (sdp);
2043 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
2044 gst_sdp_message_free (sdp);
2046 send_message (client, ctx, ctx->response, FALSE);
2048 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
2056 GST_ERROR ("client %p: no uri", client);
2057 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2062 GST_ERROR ("client %p: no mount points configured", client);
2063 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2068 GST_ERROR ("client %p: can't find path for url", client);
2069 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2074 GST_ERROR ("client %p: no media", client);
2076 /* error reply is already sent */
2081 GST_ERROR ("client %p: can't create SDP", client);
2082 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2084 g_object_unref (media);
2090 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
2092 GstRTSPMethod options;
2095 options = GST_RTSP_DESCRIBE |
2100 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
2102 str = gst_rtsp_options_as_text (options);
2104 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2105 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2107 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
2110 send_message (client, ctx, ctx->response, FALSE);
2112 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
2118 /* remove duplicate and trailing '/' */
2120 sanitize_uri (GstRTSPUrl * uri)
2124 gboolean have_slash, prev_slash;
2126 s = d = uri->abspath;
2127 len = strlen (uri->abspath);
2131 for (i = 0; i < len; i++) {
2132 have_slash = s[i] == '/';
2134 if (!have_slash || !prev_slash)
2136 prev_slash = have_slash;
2138 len = d - uri->abspath;
2139 /* don't remove the first slash if that's the only thing left */
2140 if (len > 1 && *(d - 1) == '/')
2146 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
2148 GstRTSPClientPrivate *priv = client->priv;
2150 GST_INFO ("client %p: session %p finished", client, session);
2152 /* unlink all media managed in this session */
2153 client_unlink_session (client, session);
2155 /* remove the session */
2156 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
2157 GST_INFO ("client %p: all sessions finalized, close the connection",
2159 close_connection (client);
2164 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
2166 GstRTSPClientPrivate *priv = client->priv;
2167 GstRTSPMethod method;
2168 const gchar *uristr;
2169 GstRTSPUrl *uri = NULL;
2170 GstRTSPVersion version;
2172 GstRTSPSession *session = NULL;
2173 GstRTSPContext sctx = { NULL }, *ctx;
2174 GstRTSPMessage response = { 0 };
2177 if (!(ctx = gst_rtsp_context_get_current ())) {
2179 ctx->auth = priv->auth;
2180 gst_rtsp_context_push_current (ctx);
2183 ctx->conn = priv->connection;
2184 ctx->client = client;
2185 ctx->request = request;
2186 ctx->response = &response;
2188 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2189 gst_rtsp_message_dump (request);
2192 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
2194 GST_INFO ("client %p: received a request %s %s %s", client,
2195 gst_rtsp_method_as_text (method), uristr,
2196 gst_rtsp_version_as_text (version));
2198 /* we can only handle 1.0 requests */
2199 if (version != GST_RTSP_VERSION_1_0)
2202 ctx->method = method;
2204 /* we always try to parse the url first */
2205 if (strcmp (uristr, "*") == 0) {
2206 /* special case where we have * as uri, keep uri = NULL */
2207 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
2208 /* check if the uristr is an absolute path <=> scheme and host information
2212 scheme = g_uri_parse_scheme (uristr);
2213 if (scheme == NULL && g_str_has_prefix (uristr, "/")) {
2214 gchar *absolute_uristr = NULL;
2216 GST_WARNING_OBJECT (client, "request doesn't contain absolute url");
2217 if (priv->server_ip == NULL) {
2218 GST_WARNING_OBJECT (client, "host information missing");
2223 g_strdup_printf ("rtsp://%s%s", priv->server_ip, uristr);
2225 GST_DEBUG_OBJECT (client, "absolute url: %s", absolute_uristr);
2226 if (gst_rtsp_url_parse (absolute_uristr, &uri) != GST_RTSP_OK) {
2227 g_free (absolute_uristr);
2230 g_free (absolute_uristr);
2237 /* get the session if there is any */
2238 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
2239 if (res == GST_RTSP_OK) {
2240 if (priv->session_pool == NULL)
2243 /* we had a session in the request, find it again */
2244 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2245 goto session_not_found;
2247 /* we add the session to the client list of watched sessions. When a session
2248 * disappears because it times out, we will be notified. If all sessions are
2249 * gone, we will close the connection */
2250 client_watch_session (client, session);
2253 /* sanitize the uri */
2257 ctx->session = session;
2259 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
2260 goto not_authorized;
2262 /* now see what is asked and dispatch to a dedicated handler */
2264 case GST_RTSP_OPTIONS:
2265 handle_options_request (client, ctx);
2267 case GST_RTSP_DESCRIBE:
2268 handle_describe_request (client, ctx);
2270 case GST_RTSP_SETUP:
2271 handle_setup_request (client, ctx);
2274 handle_play_request (client, ctx);
2276 case GST_RTSP_PAUSE:
2277 handle_pause_request (client, ctx);
2279 case GST_RTSP_TEARDOWN:
2280 handle_teardown_request (client, ctx);
2282 case GST_RTSP_SET_PARAMETER:
2283 handle_set_param_request (client, ctx);
2285 case GST_RTSP_GET_PARAMETER:
2286 handle_get_param_request (client, ctx);
2288 case GST_RTSP_ANNOUNCE:
2289 case GST_RTSP_RECORD:
2290 case GST_RTSP_REDIRECT:
2291 goto not_implemented;
2292 case GST_RTSP_INVALID:
2299 gst_rtsp_context_pop_current (ctx);
2301 g_object_unref (session);
2303 gst_rtsp_url_free (uri);
2309 GST_ERROR ("client %p: version %d not supported", client, version);
2310 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
2316 GST_ERROR ("client %p: bad request", client);
2317 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2322 GST_ERROR ("client %p: no pool configured", client);
2323 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2328 GST_ERROR ("client %p: session not found", client);
2329 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2334 GST_ERROR ("client %p: not allowed", client);
2335 /* error reply is already sent */
2340 GST_ERROR ("client %p: method %d not implemented", client, method);
2341 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
2348 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
2350 GstRTSPClientPrivate *priv = client->priv;
2352 GstRTSPSession *session = NULL;
2353 GstRTSPContext sctx = { NULL }, *ctx;
2356 if (!(ctx = gst_rtsp_context_get_current ())) {
2358 ctx->auth = priv->auth;
2359 gst_rtsp_context_push_current (ctx);
2362 ctx->conn = priv->connection;
2363 ctx->client = client;
2364 ctx->request = NULL;
2366 ctx->method = GST_RTSP_INVALID;
2367 ctx->response = response;
2369 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2370 gst_rtsp_message_dump (response);
2373 GST_INFO ("client %p: received a response", client);
2375 /* get the session if there is any */
2377 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
2378 if (res == GST_RTSP_OK) {
2379 if (priv->session_pool == NULL)
2382 /* we had a session in the request, find it again */
2383 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2384 goto session_not_found;
2386 /* we add the session to the client list of watched sessions. When a session
2387 * disappears because it times out, we will be notified. If all sessions are
2388 * gone, we will close the connection */
2389 client_watch_session (client, session);
2392 ctx->session = session;
2394 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
2399 gst_rtsp_context_pop_current (ctx);
2401 g_object_unref (session);
2406 GST_ERROR ("client %p: no pool configured", client);
2411 GST_ERROR ("client %p: session not found", client);
2417 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2419 GstRTSPClientPrivate *priv = client->priv;
2428 /* find the stream for this message */
2429 res = gst_rtsp_message_parse_data (message, &channel);
2430 if (res != GST_RTSP_OK)
2433 gst_rtsp_message_steal_body (message, &data, &size);
2435 buffer = gst_buffer_new_wrapped (data, size);
2438 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2439 GstRTSPStreamTransport *trans;
2440 GstRTSPStream *stream;
2441 const GstRTSPTransport *tr;
2445 tr = gst_rtsp_stream_transport_get_transport (trans);
2446 stream = gst_rtsp_stream_transport_get_stream (trans);
2448 /* check for TCP transport */
2449 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2450 /* dispatch to the stream based on the channel number */
2451 if (tr->interleaved.min == channel) {
2452 gst_rtsp_stream_recv_rtp (stream, buffer);
2455 } else if (tr->interleaved.max == channel) {
2456 gst_rtsp_stream_recv_rtcp (stream, buffer);
2463 gst_buffer_unref (buffer);
2467 * gst_rtsp_client_set_session_pool:
2468 * @client: a #GstRTSPClient
2469 * @pool: (transfer none): a #GstRTSPSessionPool
2471 * Set @pool as the sessionpool for @client which it will use to find
2472 * or allocate sessions. the sessionpool is usually inherited from the server
2473 * that created the client but can be overridden later.
2476 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2477 GstRTSPSessionPool * pool)
2479 GstRTSPSessionPool *old;
2480 GstRTSPClientPrivate *priv;
2482 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2484 priv = client->priv;
2487 g_object_ref (pool);
2489 g_mutex_lock (&priv->lock);
2490 old = priv->session_pool;
2491 priv->session_pool = pool;
2492 g_mutex_unlock (&priv->lock);
2495 g_object_unref (old);
2499 * gst_rtsp_client_get_session_pool:
2500 * @client: a #GstRTSPClient
2502 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2504 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2506 GstRTSPSessionPool *
2507 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2509 GstRTSPClientPrivate *priv;
2510 GstRTSPSessionPool *result;
2512 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2514 priv = client->priv;
2516 g_mutex_lock (&priv->lock);
2517 if ((result = priv->session_pool))
2518 g_object_ref (result);
2519 g_mutex_unlock (&priv->lock);
2525 * gst_rtsp_client_set_mount_points:
2526 * @client: a #GstRTSPClient
2527 * @mounts: (transfer none): a #GstRTSPMountPoints
2529 * Set @mounts as the mount points for @client which it will use to map urls
2530 * to media streams. These mount points are usually inherited from the server that
2531 * created the client but can be overriden later.
2534 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2535 GstRTSPMountPoints * mounts)
2537 GstRTSPClientPrivate *priv;
2538 GstRTSPMountPoints *old;
2540 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2542 priv = client->priv;
2545 g_object_ref (mounts);
2547 g_mutex_lock (&priv->lock);
2548 old = priv->mount_points;
2549 priv->mount_points = mounts;
2550 g_mutex_unlock (&priv->lock);
2553 g_object_unref (old);
2557 * gst_rtsp_client_get_mount_points:
2558 * @client: a #GstRTSPClient
2560 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2562 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2564 GstRTSPMountPoints *
2565 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2567 GstRTSPClientPrivate *priv;
2568 GstRTSPMountPoints *result;
2570 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2572 priv = client->priv;
2574 g_mutex_lock (&priv->lock);
2575 if ((result = priv->mount_points))
2576 g_object_ref (result);
2577 g_mutex_unlock (&priv->lock);
2583 * gst_rtsp_client_set_auth:
2584 * @client: a #GstRTSPClient
2585 * @auth: (transfer none): a #GstRTSPAuth
2587 * configure @auth to be used as the authentication manager of @client.
2590 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2592 GstRTSPClientPrivate *priv;
2595 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2597 priv = client->priv;
2600 g_object_ref (auth);
2602 g_mutex_lock (&priv->lock);
2605 g_mutex_unlock (&priv->lock);
2608 g_object_unref (old);
2613 * gst_rtsp_client_get_auth:
2614 * @client: a #GstRTSPClient
2616 * Get the #GstRTSPAuth used as the authentication manager of @client.
2618 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2622 gst_rtsp_client_get_auth (GstRTSPClient * client)
2624 GstRTSPClientPrivate *priv;
2625 GstRTSPAuth *result;
2627 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2629 priv = client->priv;
2631 g_mutex_lock (&priv->lock);
2632 if ((result = priv->auth))
2633 g_object_ref (result);
2634 g_mutex_unlock (&priv->lock);
2640 * gst_rtsp_client_set_thread_pool:
2641 * @client: a #GstRTSPClient
2642 * @pool: (transfer none): a #GstRTSPThreadPool
2644 * configure @pool to be used as the thread pool of @client.
2647 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2648 GstRTSPThreadPool * pool)
2650 GstRTSPClientPrivate *priv;
2651 GstRTSPThreadPool *old;
2653 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2655 priv = client->priv;
2658 g_object_ref (pool);
2660 g_mutex_lock (&priv->lock);
2661 old = priv->thread_pool;
2662 priv->thread_pool = pool;
2663 g_mutex_unlock (&priv->lock);
2666 g_object_unref (old);
2670 * gst_rtsp_client_get_thread_pool:
2671 * @client: a #GstRTSPClient
2673 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2675 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2679 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2681 GstRTSPClientPrivate *priv;
2682 GstRTSPThreadPool *result;
2684 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2686 priv = client->priv;
2688 g_mutex_lock (&priv->lock);
2689 if ((result = priv->thread_pool))
2690 g_object_ref (result);
2691 g_mutex_unlock (&priv->lock);
2697 * gst_rtsp_client_set_connection:
2698 * @client: a #GstRTSPClient
2699 * @conn: (transfer full): a #GstRTSPConnection
2701 * Set the #GstRTSPConnection of @client. This function takes ownership of
2704 * Returns: %TRUE on success.
2707 gst_rtsp_client_set_connection (GstRTSPClient * client,
2708 GstRTSPConnection * conn)
2710 GstRTSPClientPrivate *priv;
2711 GSocket *read_socket;
2712 GSocketAddress *address;
2714 GError *error = NULL;
2716 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2717 g_return_val_if_fail (conn != NULL, FALSE);
2719 priv = client->priv;
2721 read_socket = gst_rtsp_connection_get_read_socket (conn);
2723 if (!(address = g_socket_get_local_address (read_socket, &error)))
2726 g_free (priv->server_ip);
2727 /* keep the original ip that the client connected to */
2728 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2729 GInetAddress *iaddr;
2731 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2733 /* socket might be ipv6 but adress still ipv4 */
2734 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2735 priv->server_ip = g_inet_address_to_string (iaddr);
2736 g_object_unref (address);
2738 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2739 priv->server_ip = g_strdup ("unknown");
2742 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2743 priv->server_ip, priv->is_ipv6);
2745 url = gst_rtsp_connection_get_url (conn);
2746 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2748 priv->connection = conn;
2755 GST_ERROR ("could not get local address %s", error->message);
2756 g_error_free (error);
2762 * gst_rtsp_client_get_connection:
2763 * @client: a #GstRTSPClient
2765 * Get the #GstRTSPConnection of @client.
2767 * Returns: (transfer none): the #GstRTSPConnection of @client.
2768 * The connection object returned remains valid until the client is freed.
2771 gst_rtsp_client_get_connection (GstRTSPClient * client)
2773 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2775 return client->priv->connection;
2779 * gst_rtsp_client_set_send_func:
2780 * @client: a #GstRTSPClient
2781 * @func: (scope notified): a #GstRTSPClientSendFunc
2782 * @user_data: (closure): user data passed to @func
2783 * @notify: (allow-none): called when @user_data is no longer in use
2785 * Set @func as the callback that will be called when a new message needs to be
2786 * sent to the client. @user_data is passed to @func and @notify is called when
2787 * @user_data is no longer in use.
2789 * By default, the client will send the messages on the #GstRTSPConnection that
2790 * was configured with gst_rtsp_client_attach() was called.
2793 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2794 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2796 GstRTSPClientPrivate *priv;
2797 GDestroyNotify old_notify;
2800 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2802 priv = client->priv;
2804 g_mutex_lock (&priv->send_lock);
2805 priv->send_func = func;
2806 old_notify = priv->send_notify;
2807 old_data = priv->send_data;
2808 priv->send_notify = notify;
2809 priv->send_data = user_data;
2810 g_mutex_unlock (&priv->send_lock);
2813 old_notify (old_data);
2817 * gst_rtsp_client_handle_message:
2818 * @client: a #GstRTSPClient
2819 * @message: (transfer none): an #GstRTSPMessage
2821 * Let the client handle @message.
2823 * Returns: a #GstRTSPResult.
2826 gst_rtsp_client_handle_message (GstRTSPClient * client,
2827 GstRTSPMessage * message)
2829 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2830 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2832 switch (message->type) {
2833 case GST_RTSP_MESSAGE_REQUEST:
2834 handle_request (client, message);
2836 case GST_RTSP_MESSAGE_RESPONSE:
2837 handle_response (client, message);
2839 case GST_RTSP_MESSAGE_DATA:
2840 handle_data (client, message);
2849 * gst_rtsp_client_send_message:
2850 * @client: a #GstRTSPClient
2851 * @session: (transfer none): a #GstRTSPSession to send the message to or %NULL
2852 * @message: (transfer none): The #GstRTSPMessage to send
2854 * Send a message message to the remote end. @message must be a
2855 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
2858 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
2859 GstRTSPMessage * message)
2861 GstRTSPContext sctx = { NULL }
2863 GstRTSPClientPrivate *priv;
2865 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2866 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2867 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
2868 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
2870 priv = client->priv;
2872 if (!(ctx = gst_rtsp_context_get_current ())) {
2874 ctx->auth = priv->auth;
2875 gst_rtsp_context_push_current (ctx);
2878 ctx->conn = priv->connection;
2879 ctx->client = client;
2880 ctx->session = session;
2882 send_message (client, ctx, message, FALSE);
2885 gst_rtsp_context_pop_current (ctx);
2890 static GstRTSPResult
2891 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2892 gboolean close, gpointer user_data)
2894 GstRTSPClientPrivate *priv = client->priv;
2902 /* send the response and store the seq number so we can wait until it's
2903 * written to the client to close the connection */
2905 gst_rtsp_watch_send_message (priv->watch, message,
2906 close ? &priv->close_seq : NULL);
2907 if (ret == GST_RTSP_OK)
2910 if (ret != GST_RTSP_ENOMEM)
2914 if (priv->drop_backlog)
2917 /* queue was full, wait for more space */
2918 GST_DEBUG_OBJECT (client, "waiting for backlog");
2919 ret = gst_rtsp_watch_wait_backlog (priv->watch, &time);
2920 GST_DEBUG_OBJECT (client, "Resend due to backlog full");
2921 } while (ret != GST_RTSP_EINTR);
2928 GST_DEBUG_OBJECT (client, "got error %d", ret);
2933 static GstRTSPResult
2934 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2937 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2940 static GstRTSPResult
2941 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2943 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2944 GstRTSPClientPrivate *priv = client->priv;
2946 if (priv->close_seq && priv->close_seq == cseq) {
2947 priv->close_seq = 0;
2948 close_connection (client);
2954 static GstRTSPResult
2955 closed (GstRTSPWatch * watch, gpointer user_data)
2957 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2958 GstRTSPClientPrivate *priv = client->priv;
2959 const gchar *tunnelid;
2961 GST_INFO ("client %p: connection closed", client);
2963 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2964 g_mutex_lock (&tunnels_lock);
2965 /* remove from tunnelids */
2966 g_hash_table_remove (tunnels, tunnelid);
2967 g_mutex_unlock (&tunnels_lock);
2970 gst_rtsp_watch_set_flushing (watch, TRUE);
2971 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2976 static GstRTSPResult
2977 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2979 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2982 str = gst_rtsp_strresult (result);
2983 GST_INFO ("client %p: received an error %s", client, str);
2989 static GstRTSPResult
2990 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2991 GstRTSPMessage * message, guint id, gpointer user_data)
2993 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2996 str = gst_rtsp_strresult (result);
2998 ("client %p: error when handling message %p with id %d: %s",
2999 client, message, id, str);
3006 remember_tunnel (GstRTSPClient * client)
3008 GstRTSPClientPrivate *priv = client->priv;
3009 const gchar *tunnelid;
3011 /* store client in the pending tunnels */
3012 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
3013 if (tunnelid == NULL)
3016 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
3018 /* we can't have two clients connecting with the same tunnelid */
3019 g_mutex_lock (&tunnels_lock);
3020 if (g_hash_table_lookup (tunnels, tunnelid))
3021 goto tunnel_existed;
3023 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
3024 g_mutex_unlock (&tunnels_lock);
3031 GST_ERROR ("client %p: no tunnelid provided", client);
3036 g_mutex_unlock (&tunnels_lock);
3037 GST_ERROR ("client %p: tunnel session %s already existed", client,
3043 static GstRTSPResult
3044 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
3046 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3047 GstRTSPClientPrivate *priv = client->priv;
3049 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
3052 /* ignore error, it'll only be a problem when the client does a POST again */
3053 remember_tunnel (client);
3059 handle_tunnel (GstRTSPClient * client)
3061 GstRTSPClientPrivate *priv = client->priv;
3062 GstRTSPClient *oclient;
3063 GstRTSPClientPrivate *opriv;
3064 const gchar *tunnelid;
3066 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
3067 if (tunnelid == NULL)
3070 /* check for previous tunnel */
3071 g_mutex_lock (&tunnels_lock);
3072 oclient = g_hash_table_lookup (tunnels, tunnelid);
3074 if (oclient == NULL) {
3075 /* no previous tunnel, remember tunnel */
3076 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
3077 g_mutex_unlock (&tunnels_lock);
3079 GST_INFO ("client %p: no previous tunnel found, remembering tunnel (%p)",
3080 client, priv->connection);
3082 /* merge both tunnels into the first client */
3083 /* remove the old client from the table. ref before because removing it will
3084 * remove the ref to it. */
3085 g_object_ref (oclient);
3086 g_hash_table_remove (tunnels, tunnelid);
3087 g_mutex_unlock (&tunnels_lock);
3089 opriv = oclient->priv;
3091 if (opriv->watch == NULL)
3094 GST_INFO ("client %p: found previous tunnel %p (old %p, new %p)", client,
3095 oclient, opriv->connection, priv->connection);
3097 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
3098 gst_rtsp_watch_reset (priv->watch);
3099 gst_rtsp_watch_reset (opriv->watch);
3100 g_object_unref (oclient);
3102 /* the old client owns the tunnel now, the new one will be freed */
3103 g_source_destroy ((GSource *) priv->watch);
3105 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
3113 GST_ERROR ("client %p: no tunnelid provided", client);
3118 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
3119 g_object_unref (oclient);
3124 static GstRTSPStatusCode
3125 tunnel_get (GstRTSPWatch * watch, gpointer user_data)
3127 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3129 GST_INFO ("client %p: tunnel get (connection %p)", client,
3130 client->priv->connection);
3132 if (!handle_tunnel (client)) {
3133 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
3136 return GST_RTSP_STS_OK;
3139 static GstRTSPResult
3140 tunnel_post (GstRTSPWatch * watch, gpointer user_data)
3142 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3144 GST_INFO ("client %p: tunnel post (connection %p)", client,
3145 client->priv->connection);
3147 if (!handle_tunnel (client)) {
3148 return GST_RTSP_ERROR;
3154 static GstRTSPResult
3155 tunnel_http_response (GstRTSPWatch * watch, GstRTSPMessage * request,
3156 GstRTSPMessage * response, gpointer user_data)
3158 GstRTSPClientClass *klass;
3160 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3161 klass = GST_RTSP_CLIENT_GET_CLASS (client);
3163 if (klass->tunnel_http_response) {
3164 klass->tunnel_http_response (client, request, response);
3170 static GstRTSPWatchFuncs watch_funcs = {
3179 tunnel_http_response
3183 client_watch_notify (GstRTSPClient * client)
3185 GstRTSPClientPrivate *priv = client->priv;
3187 GST_INFO ("client %p: watch destroyed", client);
3189 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
3190 g_object_unref (client);
3194 * gst_rtsp_client_attach:
3195 * @client: a #GstRTSPClient
3196 * @context: (allow-none): a #GMainContext
3198 * Attaches @client to @context. When the mainloop for @context is run, the
3199 * client will be dispatched. When @context is %NULL, the default context will be
3202 * This function should be called when the client properties and urls are fully
3203 * configured and the client is ready to start.
3205 * Returns: the ID (greater than 0) for the source within the GMainContext.
3208 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
3210 GstRTSPClientPrivate *priv;
3213 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
3214 priv = client->priv;
3215 g_return_val_if_fail (priv->connection != NULL, 0);
3216 g_return_val_if_fail (priv->watch == NULL, 0);
3218 /* create watch for the connection and attach */
3219 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
3220 g_object_ref (client), (GDestroyNotify) client_watch_notify);
3221 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
3222 (GDestroyNotify) gst_rtsp_watch_unref);
3224 /* FIXME make this configurable. We don't want to do this yet because it will
3225 * be superceeded by a cache object later */
3226 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
3228 GST_INFO ("attaching to context %p", context);
3229 res = gst_rtsp_watch_attach (priv->watch, context);
3235 * gst_rtsp_client_session_filter:
3236 * @client: a #GstRTSPClient
3237 * @func: (scope call) (allow-none): a callback
3238 * @user_data: user data passed to @func
3240 * Call @func for each session managed by @client. The result value of @func
3241 * determines what happens to the session. @func will be called with @client
3242 * locked so no further actions on @client can be performed from @func.
3244 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
3247 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
3249 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
3250 * will also be added with an additional ref to the result #GList of this
3253 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each session.
3255 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
3256 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3257 * element in the #GList should be unreffed before the list is freed.
3260 gst_rtsp_client_session_filter (GstRTSPClient * client,
3261 GstRTSPClientSessionFilterFunc func, gpointer user_data)
3263 GstRTSPClientPrivate *priv;
3264 GList *result, *walk, *next;
3266 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
3268 priv = client->priv;
3272 g_mutex_lock (&priv->lock);
3273 for (walk = priv->sessions; walk; walk = next) {
3274 GstRTSPSession *sess = walk->data;
3275 GstRTSPFilterResult res;
3277 next = g_list_next (walk);
3280 res = func (client, sess, user_data);
3282 res = GST_RTSP_FILTER_REF;
3285 case GST_RTSP_FILTER_REMOVE:
3286 /* stop watching the session and pretent it went away */
3287 client_cleanup_session (client, sess);
3289 case GST_RTSP_FILTER_REF:
3290 result = g_list_prepend (result, g_object_ref (sess));
3292 case GST_RTSP_FILTER_KEEP:
3297 g_mutex_unlock (&priv->lock);