2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * @short_description: A client connection state
22 * @see_also: #GstRTSPServer, #GstRTSPThreadPool
24 * The client object handles the connection with a client for as long as a TCP
27 * A #GstRTSPClient is created by #GstRTSPServer when a new connection is
28 * accepted and it inherits the #GstRTSPMountPoints, #GstRTSPSessionPool,
29 * #GstRTSPAuth and #GstRTSPThreadPool from the server.
31 * The client connection should be configured with the #GstRTSPConnection using
32 * gst_rtsp_client_set_connection() before it can be attached to a #GMainContext
33 * using gst_rtsp_client_attach(). From then on the client will handle requests
36 * Use gst_rtsp_client_session_filter() to iterate or modify all the
37 * #GstRTSPSession objects managed by the client object.
39 * Last reviewed on 2013-07-11 (1.0.0)
45 #include "rtsp-client.h"
47 #include "rtsp-params.h"
49 #define GST_RTSP_CLIENT_GET_PRIVATE(obj) \
50 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_CLIENT, GstRTSPClientPrivate))
53 * send_lock, lock, tunnels_lock
56 struct _GstRTSPClientPrivate
58 GMutex lock; /* protects everything else */
60 GstRTSPConnection *connection;
66 GstRTSPClientSendFunc send_func; /* protected by send_lock */
67 gpointer send_data; /* protected by send_lock */
68 GDestroyNotify send_notify; /* protected by send_lock */
70 GstRTSPSessionPool *session_pool;
71 GstRTSPMountPoints *mount_points;
73 GstRTSPThreadPool *thread_pool;
75 /* used to cache the media in the last requested DESCRIBE so that
76 * we can pick it up in the next SETUP immediately */
84 static GMutex tunnels_lock;
85 static GHashTable *tunnels; /* protected by tunnels_lock */
87 #define DEFAULT_SESSION_POOL NULL
88 #define DEFAULT_MOUNT_POINTS NULL
102 SIGNAL_OPTIONS_REQUEST,
103 SIGNAL_DESCRIBE_REQUEST,
104 SIGNAL_SETUP_REQUEST,
106 SIGNAL_PAUSE_REQUEST,
107 SIGNAL_TEARDOWN_REQUEST,
108 SIGNAL_SET_PARAMETER_REQUEST,
109 SIGNAL_GET_PARAMETER_REQUEST,
110 SIGNAL_HANDLE_RESPONSE,
114 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
115 #define GST_CAT_DEFAULT rtsp_client_debug
117 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
119 static void gst_rtsp_client_get_property (GObject * object, guint propid,
120 GValue * value, GParamSpec * pspec);
121 static void gst_rtsp_client_set_property (GObject * object, guint propid,
122 const GValue * value, GParamSpec * pspec);
123 static void gst_rtsp_client_finalize (GObject * obj);
125 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
126 static void client_session_finalized (GstRTSPClient * client,
127 GstRTSPSession * session);
128 static void unlink_session_transports (GstRTSPClient * client,
129 GstRTSPSession * session, GstRTSPSessionMedia * sessmedia);
130 static gboolean default_configure_client_transport (GstRTSPClient * client,
131 GstRTSPContext * ctx, GstRTSPTransport * ct);
132 static GstRTSPResult default_params_set (GstRTSPClient * client,
133 GstRTSPContext * ctx);
134 static GstRTSPResult default_params_get (GstRTSPClient * client,
135 GstRTSPContext * ctx);
137 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
140 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
142 GObjectClass *gobject_class;
144 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
146 gobject_class = G_OBJECT_CLASS (klass);
148 gobject_class->get_property = gst_rtsp_client_get_property;
149 gobject_class->set_property = gst_rtsp_client_set_property;
150 gobject_class->finalize = gst_rtsp_client_finalize;
152 klass->create_sdp = create_sdp;
153 klass->configure_client_transport = default_configure_client_transport;
154 klass->params_set = default_params_set;
155 klass->params_get = default_params_get;
157 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
158 g_param_spec_object ("session-pool", "Session Pool",
159 "The session pool to use for client session",
160 GST_TYPE_RTSP_SESSION_POOL,
161 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
163 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
164 g_param_spec_object ("mount-points", "Mount Points",
165 "The mount points to use for client session",
166 GST_TYPE_RTSP_MOUNT_POINTS,
167 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
169 gst_rtsp_client_signals[SIGNAL_CLOSED] =
170 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
171 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
172 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
174 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
175 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
176 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
177 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
179 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
180 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
181 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
182 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
185 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
186 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
187 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
188 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
191 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
192 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
193 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
194 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
197 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
198 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
199 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
200 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
203 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
204 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
205 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
206 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
209 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
210 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
211 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
212 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
215 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
216 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
217 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
218 set_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
219 G_TYPE_NONE, 1, G_TYPE_POINTER);
221 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
222 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
223 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
224 get_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
225 G_TYPE_NONE, 1, G_TYPE_POINTER);
227 gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE] =
228 g_signal_new ("handle-response", G_TYPE_FROM_CLASS (klass),
229 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
230 handle_response), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
231 G_TYPE_NONE, 1, G_TYPE_POINTER);
234 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
235 g_mutex_init (&tunnels_lock);
237 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
241 gst_rtsp_client_init (GstRTSPClient * client)
243 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
247 g_mutex_init (&priv->lock);
248 g_mutex_init (&priv->send_lock);
252 static GstRTSPFilterResult
253 filter_session (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
256 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
258 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
259 unlink_session_transports (client, sess, sessmedia);
261 /* unmanage the media in the session */
262 return GST_RTSP_FILTER_REMOVE;
266 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
268 /* unlink all media managed in this session */
269 gst_rtsp_session_filter (session, filter_session, client);
273 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
275 GstRTSPClientPrivate *priv = client->priv;
278 for (walk = priv->sessions; walk; walk = g_list_next (walk)) {
279 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
281 /* we already know about this session */
282 if (msession == session)
286 GST_INFO ("watching session %p", session);
288 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
290 priv->sessions = g_list_prepend (priv->sessions, session);
294 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session)
296 GstRTSPClientPrivate *priv = client->priv;
298 GST_INFO ("unwatching session %p", session);
300 g_object_weak_unref (G_OBJECT (session),
301 (GWeakNotify) client_session_finalized, client);
302 priv->sessions = g_list_remove (priv->sessions, session);
306 client_cleanup_session (GstRTSPClient * client, GstRTSPSession * session)
308 g_object_weak_unref (G_OBJECT (session),
309 (GWeakNotify) client_session_finalized, client);
310 client_unlink_session (client, session);
314 client_cleanup_sessions (GstRTSPClient * client)
316 GstRTSPClientPrivate *priv = client->priv;
319 /* remove weak-ref from sessions */
320 for (sessions = priv->sessions; sessions; sessions = g_list_next (sessions)) {
321 client_cleanup_session (client, (GstRTSPSession *) sessions->data);
323 g_list_free (priv->sessions);
324 priv->sessions = NULL;
327 /* A client is finalized when the connection is broken */
329 gst_rtsp_client_finalize (GObject * obj)
331 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
332 GstRTSPClientPrivate *priv = client->priv;
334 GST_INFO ("finalize client %p", client);
336 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
339 g_source_destroy ((GSource *) priv->watch);
341 client_cleanup_sessions (client);
343 if (priv->connection)
344 gst_rtsp_connection_free (priv->connection);
345 if (priv->session_pool)
346 g_object_unref (priv->session_pool);
347 if (priv->mount_points)
348 g_object_unref (priv->mount_points);
350 g_object_unref (priv->auth);
351 if (priv->thread_pool)
352 g_object_unref (priv->thread_pool);
357 gst_rtsp_media_unprepare (priv->media);
358 g_object_unref (priv->media);
361 g_free (priv->server_ip);
362 g_mutex_clear (&priv->lock);
363 g_mutex_clear (&priv->send_lock);
365 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
369 gst_rtsp_client_get_property (GObject * object, guint propid,
370 GValue * value, GParamSpec * pspec)
372 GstRTSPClient *client = GST_RTSP_CLIENT (object);
375 case PROP_SESSION_POOL:
376 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
378 case PROP_MOUNT_POINTS:
379 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
382 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
387 gst_rtsp_client_set_property (GObject * object, guint propid,
388 const GValue * value, GParamSpec * pspec)
390 GstRTSPClient *client = GST_RTSP_CLIENT (object);
393 case PROP_SESSION_POOL:
394 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
396 case PROP_MOUNT_POINTS:
397 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
400 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
405 * gst_rtsp_client_new:
407 * Create a new #GstRTSPClient instance.
409 * Returns: a new #GstRTSPClient
412 gst_rtsp_client_new (void)
414 GstRTSPClient *result;
416 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
422 send_message (GstRTSPClient * client, GstRTSPSession * session,
423 GstRTSPMessage * message, gboolean close)
425 GstRTSPClientPrivate *priv = client->priv;
427 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
428 "GStreamer RTSP server");
430 /* remove any previous header */
431 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
433 /* add the new session header for new session ids */
435 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
436 gst_rtsp_session_get_header (session));
439 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
440 gst_rtsp_message_dump (message);
444 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
446 g_mutex_lock (&priv->send_lock);
448 priv->send_func (client, message, close, priv->send_data);
449 g_mutex_unlock (&priv->send_lock);
451 gst_rtsp_message_unset (message);
455 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
456 GstRTSPContext * ctx)
458 gst_rtsp_message_init_response (ctx->response, code,
459 gst_rtsp_status_as_text (code), ctx->request);
461 send_message (client, NULL, ctx->response, FALSE);
465 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
467 if (path1 == NULL || path2 == NULL)
470 if (strlen (path1) != len2)
473 if (strncmp (path1, path2, len2))
479 /* this function is called to initially find the media for the DESCRIBE request
480 * but is cached for when the same client (without breaking the connection) is
481 * doing a setup for the exact same url. */
482 static GstRTSPMedia *
483 find_media (GstRTSPClient * client, GstRTSPContext * ctx, gchar * path,
486 GstRTSPClientPrivate *priv = client->priv;
487 GstRTSPMediaFactory *factory;
491 /* find the longest matching factory for the uri first */
492 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
496 ctx->factory = factory;
498 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
499 goto no_factory_access;
501 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
507 path_len = strlen (path);
509 if (!paths_are_equal (priv->path, path, path_len)) {
510 GstRTSPThread *thread;
512 /* remove any previously cached values before we try to construct a new
518 gst_rtsp_media_unprepare (priv->media);
519 g_object_unref (priv->media);
523 /* prepare the media and add it to the pipeline */
524 if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
529 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
530 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
534 /* prepare the media */
535 if (!(gst_rtsp_media_prepare (media, thread)))
538 /* now keep track of the uri and the media */
539 priv->path = g_strndup (path, path_len);
542 /* we have seen this path before, used cached media */
545 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
548 g_object_unref (factory);
552 g_object_ref (media);
559 GST_ERROR ("client %p: no factory for path %s", client, path);
560 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
565 GST_ERROR ("client %p: not authorized to see factory path %s", client,
571 GST_ERROR ("client %p: not authorized for factory path %s", client, path);
576 GST_ERROR ("client %p: can't create media", client);
577 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
578 g_object_unref (factory);
584 GST_ERROR ("client %p: can't create thread", client);
585 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
586 g_object_unref (media);
588 g_object_unref (factory);
594 GST_ERROR ("client %p: can't prepare media", client);
595 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
596 g_object_unref (media);
598 g_object_unref (factory);
605 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
607 GstRTSPClientPrivate *priv = client->priv;
608 GstRTSPMessage message = { 0 };
613 gst_rtsp_message_init_data (&message, channel);
615 /* FIXME, need some sort of iovec RTSPMessage here */
616 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
619 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
621 g_mutex_lock (&priv->send_lock);
623 priv->send_func (client, &message, FALSE, priv->send_data);
624 g_mutex_unlock (&priv->send_lock);
626 gst_rtsp_message_steal_body (&message, &data, &usize);
627 gst_buffer_unmap (buffer, &map_info);
629 gst_rtsp_message_unset (&message);
635 link_transport (GstRTSPClient * client, GstRTSPSession * session,
636 GstRTSPStreamTransport * trans)
638 GstRTSPClientPrivate *priv = client->priv;
640 GST_DEBUG ("client %p: linking transport %p", client, trans);
642 gst_rtsp_stream_transport_set_callbacks (trans,
643 (GstRTSPSendFunc) do_send_data,
644 (GstRTSPSendFunc) do_send_data, client, NULL);
646 priv->transports = g_list_prepend (priv->transports, trans);
648 /* make sure our session can't expire */
649 gst_rtsp_session_prevent_expire (session);
653 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
654 GstRTSPStreamTransport * trans)
656 GstRTSPClientPrivate *priv = client->priv;
658 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
660 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
662 priv->transports = g_list_remove (priv->transports, trans);
664 /* our session can now expire */
665 gst_rtsp_session_allow_expire (session);
669 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
670 GstRTSPSessionMedia * sessmedia)
675 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
676 for (i = 0; i < n_streams; i++) {
677 GstRTSPStreamTransport *trans;
678 const GstRTSPTransport *tr;
680 /* get the transport, if there is no transport configured, skip this stream */
681 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
685 tr = gst_rtsp_stream_transport_get_transport (trans);
687 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
688 /* for TCP, unlink the stream from the TCP connection of the client */
689 unlink_transport (client, session, trans);
695 close_connection (GstRTSPClient * client)
697 GstRTSPClientPrivate *priv = client->priv;
698 const gchar *tunnelid;
700 GST_DEBUG ("client %p: closing connection", client);
702 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
703 g_mutex_lock (&tunnels_lock);
704 /* remove from tunnelids */
705 g_hash_table_remove (tunnels, tunnelid);
706 g_mutex_unlock (&tunnels_lock);
709 gst_rtsp_connection_close (priv->connection);
713 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
715 GstRTSPClientPrivate *priv = client->priv;
716 GstRTSPSession *session;
717 GstRTSPSessionMedia *sessmedia;
718 GstRTSPStatusCode code;
725 session = ctx->session;
730 path = ctx->uri->abspath;
732 /* get a handle to the configuration of the media in the session */
733 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
737 /* only aggregate control for now.. */
738 if (path[matched] != '\0')
741 ctx->sessmedia = sessmedia;
743 /* we emit the signal before closing the connection */
744 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
747 /* unlink the all TCP callbacks */
748 unlink_session_transports (client, session, sessmedia);
750 /* remove the session from the watched sessions */
751 client_unwatch_session (client, session);
753 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
755 /* unmanage the media in the session, returns false if all media session
757 if (!gst_rtsp_session_release_media (session, sessmedia)) {
758 /* remove the session */
759 gst_rtsp_session_pool_remove (priv->session_pool, session);
761 /* construct the response now */
762 code = GST_RTSP_STS_OK;
763 gst_rtsp_message_init_response (ctx->response, code,
764 gst_rtsp_status_as_text (code), ctx->request);
766 send_message (client, session, ctx->response, TRUE);
773 GST_ERROR ("client %p: no session", client);
774 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
779 GST_ERROR ("client %p: no uri supplied", client);
780 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
785 GST_ERROR ("client %p: no media for uri", client);
786 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
791 GST_ERROR ("client %p: no aggregate path %s", client, path);
792 send_generic_response (client,
793 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
799 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
803 res = gst_rtsp_params_set (client, ctx);
809 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
813 res = gst_rtsp_params_get (client, ctx);
819 handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
825 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
826 if (res != GST_RTSP_OK)
830 /* no body, keep-alive request */
831 send_generic_response (client, GST_RTSP_STS_OK, ctx);
833 /* there is a body, handle the params */
834 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
835 if (res != GST_RTSP_OK)
838 send_message (client, ctx->session, ctx->response, FALSE);
841 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
849 GST_ERROR ("client %p: bad request", client);
850 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
856 handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
862 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
863 if (res != GST_RTSP_OK)
867 /* no body, keep-alive request */
868 send_generic_response (client, GST_RTSP_STS_OK, ctx);
870 /* there is a body, handle the params */
871 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
872 if (res != GST_RTSP_OK)
875 send_message (client, ctx->session, ctx->response, FALSE);
878 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
886 GST_ERROR ("client %p: bad request", client);
887 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
893 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
895 GstRTSPSession *session;
896 GstRTSPSessionMedia *sessmedia;
897 GstRTSPStatusCode code;
898 GstRTSPState rtspstate;
902 if (!(session = ctx->session))
908 path = ctx->uri->abspath;
910 /* get a handle to the configuration of the media in the session */
911 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
915 if (path[matched] != '\0')
918 ctx->sessmedia = sessmedia;
920 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
921 /* the session state must be playing or recording */
922 if (rtspstate != GST_RTSP_STATE_PLAYING &&
923 rtspstate != GST_RTSP_STATE_RECORDING)
926 /* unlink the all TCP callbacks */
927 unlink_session_transports (client, session, sessmedia);
929 /* then pause sending */
930 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
932 /* construct the response now */
933 code = GST_RTSP_STS_OK;
934 gst_rtsp_message_init_response (ctx->response, code,
935 gst_rtsp_status_as_text (code), ctx->request);
937 send_message (client, session, ctx->response, FALSE);
939 /* the state is now READY */
940 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
942 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
949 GST_ERROR ("client %p: no seesion", client);
950 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
955 GST_ERROR ("client %p: no uri supplied", client);
956 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
961 GST_ERROR ("client %p: no media for uri", client);
962 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
967 GST_ERROR ("client %p: no aggregate path %s", client, path);
968 send_generic_response (client,
969 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
974 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
975 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
981 /* convert @url and @path to a URL used as a content base for the factory
982 * located at @path */
984 make_base_url (GstRTSPClient * client, GstRTSPUrl * url, gchar * path)
987 gchar *result, *trail;
989 /* check for trailing '/' and append one */
990 trail = (path[strlen (path) - 1] != '/' ? "/" : "");
995 tmp.abspath = g_strdup_printf ("%s%s", path, trail);
997 result = gst_rtsp_url_get_request_uri (&tmp);
998 g_free (tmp.abspath);
1004 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
1006 GstRTSPSession *session;
1007 GstRTSPSessionMedia *sessmedia;
1008 GstRTSPMedia *media;
1009 GstRTSPStatusCode code;
1012 guint n_streams, i, infocount;
1013 gchar *str, *base_url;
1014 GstRTSPTimeRange *range;
1016 GstRTSPState rtspstate;
1017 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1021 if (!(session = ctx->session))
1024 if (!(uri = ctx->uri))
1027 path = uri->abspath;
1029 /* get a handle to the configuration of the media in the session */
1030 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1034 if (path[matched] != '\0')
1037 ctx->sessmedia = sessmedia;
1038 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1040 /* the session state must be playing or ready */
1041 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1042 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1045 /* parse the range header if we have one */
1046 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1047 if (res == GST_RTSP_OK) {
1048 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1049 /* we have a range, seek to the position */
1051 gst_rtsp_media_seek (media, range);
1052 gst_rtsp_range_free (range);
1056 /* grab RTPInfo from the payloaders now */
1057 rtpinfo = g_string_new ("");
1059 base_url = make_base_url (client, uri, path);
1061 n_streams = gst_rtsp_media_n_streams (media);
1062 for (i = 0, infocount = 0; i < n_streams; i++) {
1063 GstRTSPStreamTransport *trans;
1064 GstRTSPStream *stream;
1065 const GstRTSPTransport *tr;
1068 /* get the transport, if there is no transport configured, skip this stream */
1069 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
1070 if (trans == NULL) {
1071 GST_INFO ("stream %d is not configured", i);
1074 tr = gst_rtsp_stream_transport_get_transport (trans);
1076 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1077 /* for TCP, link the stream to the TCP connection of the client */
1078 link_transport (client, session, trans);
1081 stream = gst_rtsp_stream_transport_get_stream (trans);
1082 if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
1086 g_string_append (rtpinfo, ", ");
1088 control = gst_rtsp_stream_get_control (stream);
1089 g_string_append_printf (rtpinfo, "url=%s%s;seq=%u;rtptime=%u",
1090 base_url, control, seq, rtptime);
1095 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
1100 /* construct the response now */
1101 code = GST_RTSP_STS_OK;
1102 gst_rtsp_message_init_response (ctx->response, code,
1103 gst_rtsp_status_as_text (code), ctx->request);
1105 /* add the RTP-Info header */
1106 if (infocount > 0) {
1107 str = g_string_free (rtpinfo, FALSE);
1108 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO, str);
1110 g_string_free (rtpinfo, TRUE);
1114 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1116 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1118 send_message (client, session, ctx->response, FALSE);
1120 /* start playing after sending the request */
1121 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1123 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1125 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1132 GST_ERROR ("client %p: no session", client);
1133 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1138 GST_ERROR ("client %p: no uri supplied", client);
1139 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1144 GST_ERROR ("client %p: media not found", client);
1145 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1150 GST_ERROR ("client %p: no aggregate path %s", client, path);
1151 send_generic_response (client,
1152 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1157 GST_ERROR ("client %p: not PLAYING or READY", client);
1158 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1165 do_keepalive (GstRTSPSession * session)
1167 GST_INFO ("keep session %p alive", session);
1168 gst_rtsp_session_touch (session);
1171 /* parse @transport and return a valid transport in @tr. only transports
1172 * from @supported are returned. Returns FALSE if no valid transport
1175 parse_transport (const char *transport, GstRTSPLowerTrans supported,
1176 GstRTSPTransport * tr)
1183 gst_rtsp_transport_init (tr);
1185 GST_DEBUG ("parsing transports %s", transport);
1187 transports = g_strsplit (transport, ",", 0);
1189 /* loop through the transports, try to parse */
1190 for (i = 0; transports[i]; i++) {
1191 res = gst_rtsp_transport_parse (transports[i], tr);
1192 if (res != GST_RTSP_OK) {
1193 /* no valid transport, search some more */
1194 GST_WARNING ("could not parse transport %s", transports[i]);
1198 /* we have a transport, see if it's RTP/AVP */
1199 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1200 GST_WARNING ("invalid transport %s", transports[i]);
1204 if (!(tr->lower_transport & supported)) {
1205 GST_WARNING ("unsupported transport %s", transports[i]);
1209 /* we have a valid transport */
1210 GST_INFO ("found valid transport %s", transports[i]);
1215 gst_rtsp_transport_init (tr);
1217 g_strfreev (transports);
1223 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
1224 GstRTSPMessage * request)
1226 gchar *blocksize_str;
1227 gboolean ret = TRUE;
1229 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1230 &blocksize_str, 0) == GST_RTSP_OK) {
1234 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1235 if (end == blocksize_str) {
1236 GST_ERROR ("failed to parse blocksize");
1239 /* we don't want to change the mtu when this media
1240 * can be shared because it impacts other clients */
1241 if (gst_rtsp_media_is_shared (media))
1244 if (blocksize > G_MAXUINT)
1245 blocksize = G_MAXUINT;
1246 gst_rtsp_stream_set_mtu (stream, blocksize);
1253 default_configure_client_transport (GstRTSPClient * client,
1254 GstRTSPContext * ctx, GstRTSPTransport * ct)
1256 GstRTSPClientPrivate *priv = client->priv;
1258 /* we have a valid transport now, set the destination of the client. */
1259 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1260 gboolean use_client_settings;
1262 use_client_settings =
1263 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1265 if (ct->destination && use_client_settings) {
1266 GstRTSPAddress *addr;
1268 addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1269 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1274 gst_rtsp_address_free (addr);
1276 GstRTSPAddress *addr;
1277 GSocketFamily family;
1279 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1281 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1285 g_free (ct->destination);
1286 ct->destination = g_strdup (addr->address);
1287 ct->port.min = addr->port;
1288 ct->port.max = addr->port + addr->n_ports - 1;
1289 ct->ttl = addr->ttl;
1291 gst_rtsp_address_free (addr);
1296 url = gst_rtsp_connection_get_url (priv->connection);
1297 g_free (ct->destination);
1298 ct->destination = g_strdup (url->host);
1300 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1301 /* check if the client selected channels for TCP */
1302 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1303 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1313 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1318 static GstRTSPTransport *
1319 make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
1320 GstRTSPTransport * ct)
1322 GstRTSPTransport *st;
1324 GSocketFamily family;
1326 /* prepare the server transport */
1327 gst_rtsp_transport_new (&st);
1329 st->trans = ct->trans;
1330 st->profile = ct->profile;
1331 st->lower_transport = ct->lower_transport;
1333 addr = g_inet_address_new_from_string (ct->destination);
1336 GST_ERROR ("failed to get inet addr from client destination");
1337 family = G_SOCKET_FAMILY_IPV4;
1339 family = g_inet_address_get_family (addr);
1340 g_object_unref (addr);
1344 switch (st->lower_transport) {
1345 case GST_RTSP_LOWER_TRANS_UDP:
1346 st->client_port = ct->client_port;
1347 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
1349 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1350 st->port = ct->port;
1351 st->destination = g_strdup (ct->destination);
1354 case GST_RTSP_LOWER_TRANS_TCP:
1355 st->interleaved = ct->interleaved;
1360 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
1366 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
1368 GstRTSPClientPrivate *priv = client->priv;
1372 GstRTSPTransport *ct, *st;
1373 GstRTSPLowerTrans supported;
1374 GstRTSPStatusCode code;
1375 GstRTSPSession *session;
1376 GstRTSPStreamTransport *trans;
1378 GstRTSPSessionMedia *sessmedia;
1379 GstRTSPMedia *media;
1380 GstRTSPStream *stream;
1381 GstRTSPState rtspstate;
1382 GstRTSPClientClass *klass;
1383 gchar *path, *control;
1390 path = uri->abspath;
1392 /* parse the transport */
1394 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
1396 if (res != GST_RTSP_OK)
1399 /* we create the session after parsing stuff so that we don't make
1400 * a session for malformed requests */
1401 if (priv->session_pool == NULL)
1404 session = ctx->session;
1407 g_object_ref (session);
1408 /* get a handle to the configuration of the media in the session, this can
1409 * return NULL if this is a new url to manage in this session. */
1410 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1412 /* we need a new media configuration in this session */
1416 /* we have no session media, find one and manage it */
1417 if (sessmedia == NULL) {
1418 /* get a handle to the configuration of the media in the session */
1419 media = find_media (client, ctx, path, &matched);
1421 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1422 g_object_ref (media);
1424 /* no media, not found then */
1426 goto media_not_found;
1428 if (path[matched] == '\0')
1429 goto control_not_found;
1431 /* path is what matched. We can modify the parsed uri in place */
1432 path[matched] = '\0';
1433 /* control is remainder */
1434 control = &path[matched + 1];
1436 /* find the stream now using the control part */
1437 stream = gst_rtsp_media_find_stream (media, control);
1439 goto stream_not_found;
1441 /* now we have a uri identifying a valid media and stream */
1442 ctx->stream = stream;
1445 if (session == NULL) {
1446 /* create a session if this fails we probably reached our session limit or
1448 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1449 goto service_unavailable;
1451 /* make sure this client is closed when the session is closed */
1452 client_watch_session (client, session);
1454 /* signal new session */
1455 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1458 ctx->session = session;
1461 if (sessmedia == NULL) {
1462 /* manage the media in our session now, if not done already */
1463 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1464 /* if we stil have no media, error */
1465 if (sessmedia == NULL)
1466 goto sessmedia_unavailable;
1468 g_object_unref (media);
1471 ctx->sessmedia = sessmedia;
1473 /* set blocksize on this stream */
1474 if (!handle_blocksize (media, stream, ctx->request))
1475 goto invalid_blocksize;
1477 gst_rtsp_transport_new (&ct);
1479 /* our supported transports */
1480 supported = gst_rtsp_stream_get_protocols (stream);
1482 /* parse and find a usable supported transport */
1483 if (!parse_transport (transport, supported, ct))
1484 goto unsupported_transports;
1486 /* update the client transport */
1487 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1488 if (!klass->configure_client_transport (client, ctx, ct))
1489 goto unsupported_client_transport;
1491 /* set in the session media transport */
1492 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1494 /* configure keepalive for this transport */
1495 gst_rtsp_stream_transport_set_keepalive (trans,
1496 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1498 /* create and serialize the server transport */
1499 st = make_server_transport (client, ctx, ct);
1500 trans_str = gst_rtsp_transport_as_text (st);
1501 gst_rtsp_transport_free (st);
1503 /* construct the response now */
1504 code = GST_RTSP_STS_OK;
1505 gst_rtsp_message_init_response (ctx->response, code,
1506 gst_rtsp_status_as_text (code), ctx->request);
1508 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
1512 send_message (client, session, ctx->response, FALSE);
1514 /* update the state */
1515 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1516 switch (rtspstate) {
1517 case GST_RTSP_STATE_PLAYING:
1518 case GST_RTSP_STATE_RECORDING:
1519 case GST_RTSP_STATE_READY:
1520 /* no state change */
1523 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1526 g_object_unref (session);
1528 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
1535 GST_ERROR ("client %p: no uri", client);
1536 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1541 GST_ERROR ("client %p: no transport", client);
1542 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1547 GST_ERROR ("client %p: no session pool configured", client);
1548 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1553 GST_ERROR ("client %p: media '%s' not found", client, path);
1554 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1559 GST_ERROR ("client %p: no control in path '%s'", client, path);
1560 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1561 g_object_unref (media);
1566 GST_ERROR ("client %p: stream '%s' not found", client, control);
1567 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1568 g_object_unref (media);
1571 service_unavailable:
1573 GST_ERROR ("client %p: can't create session", client);
1574 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1575 g_object_unref (media);
1578 sessmedia_unavailable:
1580 GST_ERROR ("client %p: can't create session media", client);
1581 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1582 g_object_unref (media);
1583 g_object_unref (session);
1588 GST_ERROR ("client %p: invalid blocksize", client);
1589 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1590 g_object_unref (session);
1593 unsupported_transports:
1595 GST_ERROR ("client %p: unsupported transports", client);
1596 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1597 gst_rtsp_transport_free (ct);
1598 g_object_unref (session);
1601 unsupported_client_transport:
1603 GST_ERROR ("client %p: unsupported client transport", client);
1604 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1605 gst_rtsp_transport_free (ct);
1606 g_object_unref (session);
1611 static GstSDPMessage *
1612 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1614 GstRTSPClientPrivate *priv = client->priv;
1619 gst_sdp_message_new (&sdp);
1621 /* some standard things first */
1622 gst_sdp_message_set_version (sdp, "0");
1629 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1632 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1633 gst_sdp_message_set_information (sdp, "rtsp-server");
1634 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1635 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1636 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1637 gst_sdp_message_add_attribute (sdp, "control", "*");
1639 info.is_ipv6 = priv->is_ipv6;
1640 info.server_ip = priv->server_ip;
1642 /* create an SDP for the media object */
1643 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1651 GST_ERROR ("client %p: could not create SDP", client);
1652 gst_sdp_message_free (sdp);
1657 /* for the describe we must generate an SDP */
1659 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
1661 GstRTSPClientPrivate *priv = client->priv;
1666 GstRTSPMedia *media;
1667 GstRTSPClientClass *klass;
1669 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1674 /* check what kind of format is accepted, we don't really do anything with it
1675 * and always return SDP for now. */
1680 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
1682 if (res == GST_RTSP_ENOTIMPL)
1685 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1689 if (!priv->mount_points)
1690 goto no_mount_points;
1692 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
1695 /* find the media object for the uri */
1696 if (!(media = find_media (client, ctx, path, NULL)))
1699 /* create an SDP for the media object on this client */
1700 if (!(sdp = klass->create_sdp (client, media)))
1703 g_object_unref (media);
1705 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
1706 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
1708 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
1711 /* content base for some clients that might screw up creating the setup uri */
1712 str = make_base_url (client, ctx->uri, path);
1715 GST_INFO ("adding content-base: %s", str);
1716 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE, str);
1718 /* add SDP to the response body */
1719 str = gst_sdp_message_as_text (sdp);
1720 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
1721 gst_sdp_message_free (sdp);
1723 send_message (client, ctx->session, ctx->response, FALSE);
1725 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1733 GST_ERROR ("client %p: no uri", client);
1734 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1739 GST_ERROR ("client %p: no mount points configured", client);
1740 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1745 GST_ERROR ("client %p: can't find path for url", client);
1746 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1751 GST_ERROR ("client %p: no media", client);
1753 /* error reply is already sent */
1758 GST_ERROR ("client %p: can't create SDP", client);
1759 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1761 g_object_unref (media);
1767 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
1769 GstRTSPMethod options;
1772 options = GST_RTSP_DESCRIBE |
1777 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1779 str = gst_rtsp_options_as_text (options);
1781 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
1782 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
1784 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
1787 send_message (client, ctx->session, ctx->response, FALSE);
1789 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1795 /* remove duplicate and trailing '/' */
1797 sanitize_uri (GstRTSPUrl * uri)
1801 gboolean have_slash, prev_slash;
1803 s = d = uri->abspath;
1804 len = strlen (uri->abspath);
1808 for (i = 0; i < len; i++) {
1809 have_slash = s[i] == '/';
1811 if (!have_slash || !prev_slash)
1813 prev_slash = have_slash;
1815 len = d - uri->abspath;
1816 /* don't remove the first slash if that's the only thing left */
1817 if (len > 1 && *(d - 1) == '/')
1823 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1825 GstRTSPClientPrivate *priv = client->priv;
1827 GST_INFO ("client %p: session %p finished", client, session);
1829 /* unlink all media managed in this session */
1830 client_unlink_session (client, session);
1832 /* remove the session */
1833 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1834 GST_INFO ("client %p: all sessions finalized, close the connection",
1836 close_connection (client);
1841 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1843 GstRTSPClientPrivate *priv = client->priv;
1844 GstRTSPMethod method;
1845 const gchar *uristr;
1846 GstRTSPUrl *uri = NULL;
1847 GstRTSPVersion version;
1849 GstRTSPSession *session = NULL;
1850 GstRTSPContext sctx = { NULL }, *ctx;
1851 GstRTSPMessage response = { 0 };
1854 if (!(ctx = gst_rtsp_context_get_current ())) {
1856 ctx->auth = priv->auth;
1857 gst_rtsp_context_push_current (ctx);
1860 ctx->conn = priv->connection;
1861 ctx->client = client;
1862 ctx->request = request;
1863 ctx->response = &response;
1865 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1866 gst_rtsp_message_dump (request);
1869 GST_INFO ("client %p: received a request", client);
1871 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1873 /* we can only handle 1.0 requests */
1874 if (version != GST_RTSP_VERSION_1_0)
1877 ctx->method = method;
1879 /* we always try to parse the url first */
1880 if (strcmp (uristr, "*") == 0) {
1881 /* special case where we have * as uri, keep uri = NULL */
1882 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1885 /* get the session if there is any */
1886 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1887 if (res == GST_RTSP_OK) {
1888 if (priv->session_pool == NULL)
1891 /* we had a session in the request, find it again */
1892 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1893 goto session_not_found;
1895 /* we add the session to the client list of watched sessions. When a session
1896 * disappears because it times out, we will be notified. If all sessions are
1897 * gone, we will close the connection */
1898 client_watch_session (client, session);
1901 /* sanitize the uri */
1905 ctx->session = session;
1907 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
1908 goto not_authorized;
1910 /* now see what is asked and dispatch to a dedicated handler */
1912 case GST_RTSP_OPTIONS:
1913 handle_options_request (client, ctx);
1915 case GST_RTSP_DESCRIBE:
1916 handle_describe_request (client, ctx);
1918 case GST_RTSP_SETUP:
1919 handle_setup_request (client, ctx);
1922 handle_play_request (client, ctx);
1924 case GST_RTSP_PAUSE:
1925 handle_pause_request (client, ctx);
1927 case GST_RTSP_TEARDOWN:
1928 handle_teardown_request (client, ctx);
1930 case GST_RTSP_SET_PARAMETER:
1931 handle_set_param_request (client, ctx);
1933 case GST_RTSP_GET_PARAMETER:
1934 handle_get_param_request (client, ctx);
1936 case GST_RTSP_ANNOUNCE:
1937 case GST_RTSP_RECORD:
1938 case GST_RTSP_REDIRECT:
1939 goto not_implemented;
1940 case GST_RTSP_INVALID:
1947 gst_rtsp_context_pop_current (ctx);
1949 g_object_unref (session);
1951 gst_rtsp_url_free (uri);
1957 GST_ERROR ("client %p: version %d not supported", client, version);
1958 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1964 GST_ERROR ("client %p: bad request", client);
1965 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1970 GST_ERROR ("client %p: no pool configured", client);
1971 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1976 GST_ERROR ("client %p: session not found", client);
1977 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1982 GST_ERROR ("client %p: not allowed", client);
1987 GST_ERROR ("client %p: method %d not implemented", client, method);
1988 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
1995 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
1997 GstRTSPClientPrivate *priv = client->priv;
1999 GstRTSPSession *session = NULL;
2000 GstRTSPContext sctx = { NULL }, *ctx;
2003 if (!(ctx = gst_rtsp_context_get_current ())) {
2005 ctx->auth = priv->auth;
2006 gst_rtsp_context_push_current (ctx);
2009 ctx->conn = priv->connection;
2010 ctx->client = client;
2011 ctx->request = NULL;
2013 ctx->method = GST_RTSP_INVALID;
2014 ctx->response = response;
2016 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2017 gst_rtsp_message_dump (response);
2020 GST_INFO ("client %p: received a response", client);
2022 /* get the session if there is any */
2024 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
2025 if (res == GST_RTSP_OK) {
2026 if (priv->session_pool == NULL)
2029 /* we had a session in the request, find it again */
2030 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2031 goto session_not_found;
2033 /* we add the session to the client list of watched sessions. When a session
2034 * disappears because it times out, we will be notified. If all sessions are
2035 * gone, we will close the connection */
2036 client_watch_session (client, session);
2039 ctx->session = session;
2041 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
2046 gst_rtsp_context_pop_current (ctx);
2048 g_object_unref (session);
2053 GST_ERROR ("client %p: no pool configured", client);
2058 GST_ERROR ("client %p: session not found", client);
2064 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2066 GstRTSPClientPrivate *priv = client->priv;
2075 /* find the stream for this message */
2076 res = gst_rtsp_message_parse_data (message, &channel);
2077 if (res != GST_RTSP_OK)
2080 gst_rtsp_message_steal_body (message, &data, &size);
2082 buffer = gst_buffer_new_wrapped (data, size);
2085 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2086 GstRTSPStreamTransport *trans;
2087 GstRTSPStream *stream;
2088 const GstRTSPTransport *tr;
2092 tr = gst_rtsp_stream_transport_get_transport (trans);
2093 stream = gst_rtsp_stream_transport_get_stream (trans);
2095 /* check for TCP transport */
2096 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2097 /* dispatch to the stream based on the channel number */
2098 if (tr->interleaved.min == channel) {
2099 gst_rtsp_stream_recv_rtp (stream, buffer);
2102 } else if (tr->interleaved.max == channel) {
2103 gst_rtsp_stream_recv_rtcp (stream, buffer);
2110 gst_buffer_unref (buffer);
2114 * gst_rtsp_client_set_session_pool:
2115 * @client: a #GstRTSPClient
2116 * @pool: a #GstRTSPSessionPool
2118 * Set @pool as the sessionpool for @client which it will use to find
2119 * or allocate sessions. the sessionpool is usually inherited from the server
2120 * that created the client but can be overridden later.
2123 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2124 GstRTSPSessionPool * pool)
2126 GstRTSPSessionPool *old;
2127 GstRTSPClientPrivate *priv;
2129 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2131 priv = client->priv;
2134 g_object_ref (pool);
2136 g_mutex_lock (&priv->lock);
2137 old = priv->session_pool;
2138 priv->session_pool = pool;
2139 g_mutex_unlock (&priv->lock);
2142 g_object_unref (old);
2146 * gst_rtsp_client_get_session_pool:
2147 * @client: a #GstRTSPClient
2149 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2151 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2153 GstRTSPSessionPool *
2154 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2156 GstRTSPClientPrivate *priv;
2157 GstRTSPSessionPool *result;
2159 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2161 priv = client->priv;
2163 g_mutex_lock (&priv->lock);
2164 if ((result = priv->session_pool))
2165 g_object_ref (result);
2166 g_mutex_unlock (&priv->lock);
2172 * gst_rtsp_client_set_mount_points:
2173 * @client: a #GstRTSPClient
2174 * @mounts: a #GstRTSPMountPoints
2176 * Set @mounts as the mount points for @client which it will use to map urls
2177 * to media streams. These mount points are usually inherited from the server that
2178 * created the client but can be overriden later.
2181 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2182 GstRTSPMountPoints * mounts)
2184 GstRTSPClientPrivate *priv;
2185 GstRTSPMountPoints *old;
2187 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2189 priv = client->priv;
2192 g_object_ref (mounts);
2194 g_mutex_lock (&priv->lock);
2195 old = priv->mount_points;
2196 priv->mount_points = mounts;
2197 g_mutex_unlock (&priv->lock);
2200 g_object_unref (old);
2204 * gst_rtsp_client_get_mount_points:
2205 * @client: a #GstRTSPClient
2207 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2209 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2211 GstRTSPMountPoints *
2212 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2214 GstRTSPClientPrivate *priv;
2215 GstRTSPMountPoints *result;
2217 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2219 priv = client->priv;
2221 g_mutex_lock (&priv->lock);
2222 if ((result = priv->mount_points))
2223 g_object_ref (result);
2224 g_mutex_unlock (&priv->lock);
2230 * gst_rtsp_client_set_auth:
2231 * @client: a #GstRTSPClient
2232 * @auth: a #GstRTSPAuth
2234 * configure @auth to be used as the authentication manager of @client.
2237 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2239 GstRTSPClientPrivate *priv;
2242 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2244 priv = client->priv;
2247 g_object_ref (auth);
2249 g_mutex_lock (&priv->lock);
2252 g_mutex_unlock (&priv->lock);
2255 g_object_unref (old);
2260 * gst_rtsp_client_get_auth:
2261 * @client: a #GstRTSPClient
2263 * Get the #GstRTSPAuth used as the authentication manager of @client.
2265 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2269 gst_rtsp_client_get_auth (GstRTSPClient * client)
2271 GstRTSPClientPrivate *priv;
2272 GstRTSPAuth *result;
2274 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2276 priv = client->priv;
2278 g_mutex_lock (&priv->lock);
2279 if ((result = priv->auth))
2280 g_object_ref (result);
2281 g_mutex_unlock (&priv->lock);
2287 * gst_rtsp_client_set_thread_pool:
2288 * @client: a #GstRTSPClient
2289 * @pool: a #GstRTSPThreadPool
2291 * configure @pool to be used as the thread pool of @client.
2294 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2295 GstRTSPThreadPool * pool)
2297 GstRTSPClientPrivate *priv;
2298 GstRTSPThreadPool *old;
2300 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2302 priv = client->priv;
2305 g_object_ref (pool);
2307 g_mutex_lock (&priv->lock);
2308 old = priv->thread_pool;
2309 priv->thread_pool = pool;
2310 g_mutex_unlock (&priv->lock);
2313 g_object_unref (old);
2317 * gst_rtsp_client_get_thread_pool:
2318 * @client: a #GstRTSPClient
2320 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2322 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2326 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2328 GstRTSPClientPrivate *priv;
2329 GstRTSPThreadPool *result;
2331 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2333 priv = client->priv;
2335 g_mutex_lock (&priv->lock);
2336 if ((result = priv->thread_pool))
2337 g_object_ref (result);
2338 g_mutex_unlock (&priv->lock);
2344 * gst_rtsp_client_set_connection:
2345 * @client: a #GstRTSPClient
2346 * @conn: (transfer full): a #GstRTSPConnection
2348 * Set the #GstRTSPConnection of @client. This function takes ownership of
2351 * Returns: %TRUE on success.
2354 gst_rtsp_client_set_connection (GstRTSPClient * client,
2355 GstRTSPConnection * conn)
2357 GstRTSPClientPrivate *priv;
2358 GSocket *read_socket;
2359 GSocketAddress *address;
2361 GError *error = NULL;
2363 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2364 g_return_val_if_fail (conn != NULL, FALSE);
2366 priv = client->priv;
2368 read_socket = gst_rtsp_connection_get_read_socket (conn);
2370 if (!(address = g_socket_get_local_address (read_socket, &error)))
2373 g_free (priv->server_ip);
2374 /* keep the original ip that the client connected to */
2375 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2376 GInetAddress *iaddr;
2378 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2380 /* socket might be ipv6 but adress still ipv4 */
2381 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2382 priv->server_ip = g_inet_address_to_string (iaddr);
2383 g_object_unref (address);
2385 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2386 priv->server_ip = g_strdup ("unknown");
2389 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2390 priv->server_ip, priv->is_ipv6);
2392 url = gst_rtsp_connection_get_url (conn);
2393 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2395 priv->connection = conn;
2402 GST_ERROR ("could not get local address %s", error->message);
2403 g_error_free (error);
2409 * gst_rtsp_client_get_connection:
2410 * @client: a #GstRTSPClient
2412 * Get the #GstRTSPConnection of @client.
2414 * Returns: (transfer none): the #GstRTSPConnection of @client.
2415 * The connection object returned remains valid until the client is freed.
2418 gst_rtsp_client_get_connection (GstRTSPClient * client)
2420 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2422 return client->priv->connection;
2426 * gst_rtsp_client_set_send_func:
2427 * @client: a #GstRTSPClient
2428 * @func: a #GstRTSPClientSendFunc
2429 * @user_data: user data passed to @func
2430 * @notify: called when @user_data is no longer in use
2432 * Set @func as the callback that will be called when a new message needs to be
2433 * sent to the client. @user_data is passed to @func and @notify is called when
2434 * @user_data is no longer in use.
2436 * By default, the client will send the messages on the #GstRTSPConnection that
2437 * was configured with gst_rtsp_client_attach() was called.
2440 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2441 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2443 GstRTSPClientPrivate *priv;
2444 GDestroyNotify old_notify;
2447 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2449 priv = client->priv;
2451 g_mutex_lock (&priv->send_lock);
2452 priv->send_func = func;
2453 old_notify = priv->send_notify;
2454 old_data = priv->send_data;
2455 priv->send_notify = notify;
2456 priv->send_data = user_data;
2457 g_mutex_unlock (&priv->send_lock);
2460 old_notify (old_data);
2464 * gst_rtsp_client_handle_message:
2465 * @client: a #GstRTSPClient
2466 * @message: an #GstRTSPMessage
2468 * Let the client handle @message.
2470 * Returns: a #GstRTSPResult.
2473 gst_rtsp_client_handle_message (GstRTSPClient * client,
2474 GstRTSPMessage * message)
2476 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2477 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2479 switch (message->type) {
2480 case GST_RTSP_MESSAGE_REQUEST:
2481 handle_request (client, message);
2483 case GST_RTSP_MESSAGE_RESPONSE:
2484 handle_response (client, message);
2486 case GST_RTSP_MESSAGE_DATA:
2487 handle_data (client, message);
2496 * gst_rtsp_client_send_message:
2497 * @client: a #GstRTSPClient
2498 * @session: a #GstRTSPSession to send the message to or %NULL
2499 * @message: The #GstRTSPMessage to send
2501 * Send a message message to the remote end. @message must be a
2502 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
2505 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
2506 GstRTSPMessage * message)
2508 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2509 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2510 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
2511 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
2513 send_message (client, session, message, FALSE);
2518 static GstRTSPResult
2519 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2520 gboolean close, gpointer user_data)
2522 GstRTSPClientPrivate *priv = client->priv;
2524 /* send the response and store the seq number so we can wait until it's
2525 * written to the client to close the connection */
2526 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2527 &priv->close_seq : NULL);
2530 static GstRTSPResult
2531 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2534 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2537 static GstRTSPResult
2538 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2540 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2541 GstRTSPClientPrivate *priv = client->priv;
2543 if (priv->close_seq && priv->close_seq == cseq) {
2544 priv->close_seq = 0;
2545 close_connection (client);
2551 static GstRTSPResult
2552 closed (GstRTSPWatch * watch, gpointer user_data)
2554 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2555 GstRTSPClientPrivate *priv = client->priv;
2556 const gchar *tunnelid;
2558 GST_INFO ("client %p: connection closed", client);
2560 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2561 g_mutex_lock (&tunnels_lock);
2562 /* remove from tunnelids */
2563 g_hash_table_remove (tunnels, tunnelid);
2564 g_mutex_unlock (&tunnels_lock);
2567 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2572 static GstRTSPResult
2573 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2575 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2578 str = gst_rtsp_strresult (result);
2579 GST_INFO ("client %p: received an error %s", client, str);
2585 static GstRTSPResult
2586 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2587 GstRTSPMessage * message, guint id, gpointer user_data)
2589 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2592 str = gst_rtsp_strresult (result);
2594 ("client %p: error when handling message %p with id %d: %s",
2595 client, message, id, str);
2602 remember_tunnel (GstRTSPClient * client)
2604 GstRTSPClientPrivate *priv = client->priv;
2605 const gchar *tunnelid;
2607 /* store client in the pending tunnels */
2608 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2609 if (tunnelid == NULL)
2612 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2614 /* we can't have two clients connecting with the same tunnelid */
2615 g_mutex_lock (&tunnels_lock);
2616 if (g_hash_table_lookup (tunnels, tunnelid))
2617 goto tunnel_existed;
2619 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2620 g_mutex_unlock (&tunnels_lock);
2627 GST_ERROR ("client %p: no tunnelid provided", client);
2632 g_mutex_unlock (&tunnels_lock);
2633 GST_ERROR ("client %p: tunnel session %s already existed", client,
2639 static GstRTSPStatusCode
2640 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2642 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2643 GstRTSPClientPrivate *priv = client->priv;
2645 GST_INFO ("client %p: tunnel start (connection %p)", client,
2648 if (!remember_tunnel (client))
2651 return GST_RTSP_STS_OK;
2656 GST_ERROR ("client %p: error starting tunnel", client);
2657 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2661 static GstRTSPResult
2662 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2664 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2665 GstRTSPClientPrivate *priv = client->priv;
2667 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2670 /* ignore error, it'll only be a problem when the client does a POST again */
2671 remember_tunnel (client);
2676 static GstRTSPResult
2677 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2679 const gchar *tunnelid;
2680 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2681 GstRTSPClientPrivate *priv = client->priv;
2682 GstRTSPClient *oclient;
2683 GstRTSPClientPrivate *opriv;
2685 GST_INFO ("client %p: tunnel complete", client);
2687 /* find previous tunnel */
2688 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2689 if (tunnelid == NULL)
2692 g_mutex_lock (&tunnels_lock);
2693 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2696 /* remove the old client from the table. ref before because removing it will
2697 * remove the ref to it. */
2698 g_object_ref (oclient);
2699 g_hash_table_remove (tunnels, tunnelid);
2701 opriv = oclient->priv;
2703 if (opriv->watch == NULL)
2705 g_mutex_unlock (&tunnels_lock);
2707 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2708 opriv->connection, priv->connection);
2710 /* merge the tunnels into the first client */
2711 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2712 gst_rtsp_watch_reset (opriv->watch);
2713 g_object_unref (oclient);
2720 GST_ERROR ("client %p: no tunnelid provided", client);
2721 return GST_RTSP_ERROR;
2725 g_mutex_unlock (&tunnels_lock);
2726 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2727 return GST_RTSP_ERROR;
2731 g_mutex_unlock (&tunnels_lock);
2732 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2733 g_object_unref (oclient);
2734 return GST_RTSP_ERROR;
2738 static GstRTSPWatchFuncs watch_funcs = {
2750 client_watch_notify (GstRTSPClient * client)
2752 GstRTSPClientPrivate *priv = client->priv;
2754 GST_INFO ("client %p: watch destroyed", client);
2756 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2757 g_object_unref (client);
2761 * gst_rtsp_client_attach:
2762 * @client: a #GstRTSPClient
2763 * @context: (allow-none): a #GMainContext
2765 * Attaches @client to @context. When the mainloop for @context is run, the
2766 * client will be dispatched. When @context is NULL, the default context will be
2769 * This function should be called when the client properties and urls are fully
2770 * configured and the client is ready to start.
2772 * Returns: the ID (greater than 0) for the source within the GMainContext.
2775 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2777 GstRTSPClientPrivate *priv;
2780 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2781 priv = client->priv;
2782 g_return_val_if_fail (priv->connection != NULL, 0);
2783 g_return_val_if_fail (priv->watch == NULL, 0);
2785 /* create watch for the connection and attach */
2786 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2787 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2788 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2789 (GDestroyNotify) gst_rtsp_watch_unref);
2791 /* FIXME make this configurable. We don't want to do this yet because it will
2792 * be superceeded by a cache object later */
2793 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2795 GST_INFO ("attaching to context %p", context);
2796 res = gst_rtsp_watch_attach (priv->watch, context);
2802 * gst_rtsp_client_session_filter:
2803 * @client: a #GstRTSPClient
2804 * @func: (scope call): a callback
2805 * @user_data: user data passed to @func
2807 * Call @func for each session managed by @client. The result value of @func
2808 * determines what happens to the session. @func will be called with @client
2809 * locked so no further actions on @client can be performed from @func.
2811 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
2814 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
2816 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
2817 * will also be added with an additional ref to the result #GList of this
2820 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
2821 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2822 * element in the #GList should be unreffed before the list is freed.
2825 gst_rtsp_client_session_filter (GstRTSPClient * client,
2826 GstRTSPClientSessionFilterFunc func, gpointer user_data)
2828 GstRTSPClientPrivate *priv;
2829 GList *result, *walk, *next;
2831 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2832 g_return_val_if_fail (func != NULL, NULL);
2834 priv = client->priv;
2838 g_mutex_lock (&priv->lock);
2839 for (walk = priv->sessions; walk; walk = next) {
2840 GstRTSPSession *sess = walk->data;
2842 next = g_list_next (walk);
2844 switch (func (client, sess, user_data)) {
2845 case GST_RTSP_FILTER_REMOVE:
2846 /* stop watching the session and pretent it went away */
2847 client_cleanup_session (client, sess);
2849 case GST_RTSP_FILTER_REF:
2850 result = g_list_prepend (result, g_object_ref (sess));
2852 case GST_RTSP_FILTER_KEEP:
2857 g_mutex_unlock (&priv->lock);