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;
65 gboolean use_client_settings;
67 GstRTSPClientSendFunc send_func; /* protected by send_lock */
68 gpointer send_data; /* protected by send_lock */
69 GDestroyNotify send_notify; /* protected by send_lock */
71 GstRTSPSessionPool *session_pool;
72 GstRTSPMountPoints *mount_points;
74 GstRTSPThreadPool *thread_pool;
76 /* used to cache the media in the last requested DESCRIBE so that
77 * we can pick it up in the next SETUP immediately */
85 static GMutex tunnels_lock;
86 static GHashTable *tunnels; /* protected by tunnels_lock */
88 #define DEFAULT_SESSION_POOL NULL
89 #define DEFAULT_MOUNT_POINTS NULL
90 #define DEFAULT_USE_CLIENT_SETTINGS FALSE
97 PROP_USE_CLIENT_SETTINGS,
105 SIGNAL_OPTIONS_REQUEST,
106 SIGNAL_DESCRIBE_REQUEST,
107 SIGNAL_SETUP_REQUEST,
109 SIGNAL_PAUSE_REQUEST,
110 SIGNAL_TEARDOWN_REQUEST,
111 SIGNAL_SET_PARAMETER_REQUEST,
112 SIGNAL_GET_PARAMETER_REQUEST,
116 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
117 #define GST_CAT_DEFAULT rtsp_client_debug
119 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
121 static void gst_rtsp_client_get_property (GObject * object, guint propid,
122 GValue * value, GParamSpec * pspec);
123 static void gst_rtsp_client_set_property (GObject * object, guint propid,
124 const GValue * value, GParamSpec * pspec);
125 static void gst_rtsp_client_finalize (GObject * obj);
127 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
128 static void client_session_finalized (GstRTSPClient * client,
129 GstRTSPSession * session);
130 static void unlink_session_transports (GstRTSPClient * client,
131 GstRTSPSession * session, GstRTSPSessionMedia * sessmedia);
132 static gboolean default_configure_client_transport (GstRTSPClient * client,
133 GstRTSPClientState * state, GstRTSPTransport * ct);
134 static GstRTSPResult default_params_set (GstRTSPClient * client,
135 GstRTSPClientState * state);
136 static GstRTSPResult default_params_get (GstRTSPClient * client,
137 GstRTSPClientState * state);
139 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
142 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
144 GObjectClass *gobject_class;
146 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
148 gobject_class = G_OBJECT_CLASS (klass);
150 gobject_class->get_property = gst_rtsp_client_get_property;
151 gobject_class->set_property = gst_rtsp_client_set_property;
152 gobject_class->finalize = gst_rtsp_client_finalize;
154 klass->create_sdp = create_sdp;
155 klass->configure_client_transport = default_configure_client_transport;
156 klass->params_set = default_params_set;
157 klass->params_get = default_params_get;
159 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
160 g_param_spec_object ("session-pool", "Session Pool",
161 "The session pool to use for client session",
162 GST_TYPE_RTSP_SESSION_POOL,
163 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
165 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
166 g_param_spec_object ("mount-points", "Mount Points",
167 "The mount points to use for client session",
168 GST_TYPE_RTSP_MOUNT_POINTS,
169 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
171 g_object_class_install_property (gobject_class, PROP_USE_CLIENT_SETTINGS,
172 g_param_spec_boolean ("use-client-settings", "Use Client Settings",
173 "Use client settings for ttl and destination in multicast",
174 DEFAULT_USE_CLIENT_SETTINGS,
175 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
177 gst_rtsp_client_signals[SIGNAL_CLOSED] =
178 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
179 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
180 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
182 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
183 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
184 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
185 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
187 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
188 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
189 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
190 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
193 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
194 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
195 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
196 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
199 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
200 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
201 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
202 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
205 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
206 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
207 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
208 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
211 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
212 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
213 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
214 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
217 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
218 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
219 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
220 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
223 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
224 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
225 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
226 set_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
227 G_TYPE_NONE, 1, G_TYPE_POINTER);
229 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
230 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
231 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
232 get_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
233 G_TYPE_NONE, 1, G_TYPE_POINTER);
236 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
237 g_mutex_init (&tunnels_lock);
239 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
243 gst_rtsp_client_init (GstRTSPClient * client)
245 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
249 g_mutex_init (&priv->lock);
250 g_mutex_init (&priv->send_lock);
251 priv->use_client_settings = DEFAULT_USE_CLIENT_SETTINGS;
255 static GstRTSPFilterResult
256 filter_session (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
259 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
261 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
262 unlink_session_transports (client, sess, sessmedia);
264 /* unmanage the media in the session */
265 return GST_RTSP_FILTER_REMOVE;
269 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
271 /* unlink all media managed in this session */
272 gst_rtsp_session_filter (session, filter_session, client);
276 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
278 GstRTSPClientPrivate *priv = client->priv;
281 for (walk = priv->sessions; walk; walk = g_list_next (walk)) {
282 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
284 /* we already know about this session */
285 if (msession == session)
289 GST_INFO ("watching session %p", session);
291 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
293 priv->sessions = g_list_prepend (priv->sessions, session);
297 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session)
299 GstRTSPClientPrivate *priv = client->priv;
301 GST_INFO ("unwatching session %p", session);
303 g_object_weak_unref (G_OBJECT (session),
304 (GWeakNotify) client_session_finalized, client);
305 priv->sessions = g_list_remove (priv->sessions, session);
309 client_cleanup_session (GstRTSPClient * client, GstRTSPSession * session)
311 g_object_weak_unref (G_OBJECT (session),
312 (GWeakNotify) client_session_finalized, client);
313 client_unlink_session (client, session);
317 client_cleanup_sessions (GstRTSPClient * client)
319 GstRTSPClientPrivate *priv = client->priv;
322 /* remove weak-ref from sessions */
323 for (sessions = priv->sessions; sessions; sessions = g_list_next (sessions)) {
324 client_cleanup_session (client, (GstRTSPSession *) sessions->data);
326 g_list_free (priv->sessions);
327 priv->sessions = NULL;
330 /* A client is finalized when the connection is broken */
332 gst_rtsp_client_finalize (GObject * obj)
334 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
335 GstRTSPClientPrivate *priv = client->priv;
337 GST_INFO ("finalize client %p", client);
339 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
342 g_source_destroy ((GSource *) priv->watch);
344 client_cleanup_sessions (client);
346 if (priv->connection)
347 gst_rtsp_connection_free (priv->connection);
348 if (priv->session_pool)
349 g_object_unref (priv->session_pool);
350 if (priv->mount_points)
351 g_object_unref (priv->mount_points);
353 g_object_unref (priv->auth);
358 gst_rtsp_media_unprepare (priv->media);
359 g_object_unref (priv->media);
362 g_free (priv->server_ip);
363 g_mutex_clear (&priv->lock);
364 g_mutex_clear (&priv->send_lock);
366 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
370 gst_rtsp_client_get_property (GObject * object, guint propid,
371 GValue * value, GParamSpec * pspec)
373 GstRTSPClient *client = GST_RTSP_CLIENT (object);
376 case PROP_SESSION_POOL:
377 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
379 case PROP_MOUNT_POINTS:
380 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
382 case PROP_USE_CLIENT_SETTINGS:
383 g_value_set_boolean (value,
384 gst_rtsp_client_get_use_client_settings (client));
387 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
392 gst_rtsp_client_set_property (GObject * object, guint propid,
393 const GValue * value, GParamSpec * pspec)
395 GstRTSPClient *client = GST_RTSP_CLIENT (object);
398 case PROP_SESSION_POOL:
399 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
401 case PROP_MOUNT_POINTS:
402 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
404 case PROP_USE_CLIENT_SETTINGS:
405 gst_rtsp_client_set_use_client_settings (client,
406 g_value_get_boolean (value));
409 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
414 * gst_rtsp_client_new:
416 * Create a new #GstRTSPClient instance.
418 * Returns: a new #GstRTSPClient
421 gst_rtsp_client_new (void)
423 GstRTSPClient *result;
425 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
431 send_message (GstRTSPClient * client, GstRTSPSession * session,
432 GstRTSPMessage * message, gboolean close)
434 GstRTSPClientPrivate *priv = client->priv;
436 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
437 "GStreamer RTSP server");
439 /* remove any previous header */
440 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
442 /* add the new session header for new session ids */
444 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
445 gst_rtsp_session_get_header (session));
448 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
449 gst_rtsp_message_dump (message);
453 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
455 g_mutex_lock (&priv->send_lock);
457 priv->send_func (client, message, close, priv->send_data);
458 g_mutex_unlock (&priv->send_lock);
460 gst_rtsp_message_unset (message);
464 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
465 GstRTSPClientState * state)
467 gst_rtsp_message_init_response (state->response, code,
468 gst_rtsp_status_as_text (code), state->request);
470 send_message (client, NULL, state->response, FALSE);
474 handle_unauthorized_request (GstRTSPClient * client, GstRTSPAuth * auth,
475 GstRTSPClientState * state)
477 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_UNAUTHORIZED,
478 gst_rtsp_status_as_text (GST_RTSP_STS_UNAUTHORIZED), state->request);
481 /* and let the authentication manager setup the auth tokens */
482 gst_rtsp_auth_setup (auth, state);
485 send_message (client, state->session, state->response, FALSE);
490 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
492 if (path1 == NULL || path2 == NULL)
495 if (strlen (path1) != len2)
498 if (strncmp (path1, path2, len2))
504 /* this function is called to initially find the media for the DESCRIBE request
505 * but is cached for when the same client (without breaking the connection) is
506 * doing a setup for the exact same url. */
507 static GstRTSPMedia *
508 find_media (GstRTSPClient * client, GstRTSPClientState * state, gint * matched)
510 GstRTSPClientPrivate *priv = client->priv;
511 GstRTSPMediaFactory *factory;
516 if (!priv->mount_points)
517 goto no_mount_points;
519 path = state->uri->abspath;
521 /* find the longest matching factory for the uri first */
522 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
526 state->factory = factory;
528 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
529 goto no_factory_access;
531 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
537 path_len = strlen (path);
539 if (!paths_are_equal (priv->path, path, path_len)) {
540 GstRTSPThread *thread;
542 /* remove any previously cached values before we try to construct a new
548 gst_rtsp_media_unprepare (priv->media);
549 g_object_unref (priv->media);
553 /* prepare the media and add it to the pipeline */
554 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
557 state->media = media;
559 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
560 GST_RTSP_THREAD_TYPE_MEDIA, state);
564 /* prepare the media */
565 if (!(gst_rtsp_media_prepare (media, thread)))
568 /* now keep track of the uri and the media */
569 priv->path = g_strndup (path, path_len);
572 /* we have seen this path before, used cached media */
574 state->media = media;
575 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
578 g_object_unref (factory);
579 state->factory = NULL;
582 g_object_ref (media);
589 GST_ERROR ("client %p: no mount points configured", client);
590 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
595 GST_ERROR ("client %p: no factory for uri %s", client, path);
596 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
601 GST_ERROR ("client %p: not authorized to see factory uri %s", client, path);
602 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
607 GST_ERROR ("client %p: not authorized for factory uri %s", client, path);
608 handle_unauthorized_request (client, priv->auth, state);
613 GST_ERROR ("client %p: can't create media", client);
614 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
615 g_object_unref (factory);
616 state->factory = NULL;
621 GST_ERROR ("client %p: can't create thread", client);
622 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
623 g_object_unref (media);
625 g_object_unref (factory);
626 state->factory = NULL;
631 GST_ERROR ("client %p: can't prepare media", client);
632 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
633 g_object_unref (media);
635 g_object_unref (factory);
636 state->factory = NULL;
642 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
644 GstRTSPClientPrivate *priv = client->priv;
645 GstRTSPMessage message = { 0 };
650 gst_rtsp_message_init_data (&message, channel);
652 /* FIXME, need some sort of iovec RTSPMessage here */
653 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
656 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
658 g_mutex_lock (&priv->send_lock);
660 priv->send_func (client, &message, FALSE, priv->send_data);
661 g_mutex_unlock (&priv->send_lock);
663 gst_rtsp_message_steal_body (&message, &data, &usize);
664 gst_buffer_unmap (buffer, &map_info);
666 gst_rtsp_message_unset (&message);
672 link_transport (GstRTSPClient * client, GstRTSPSession * session,
673 GstRTSPStreamTransport * trans)
675 GstRTSPClientPrivate *priv = client->priv;
677 GST_DEBUG ("client %p: linking transport %p", client, trans);
679 gst_rtsp_stream_transport_set_callbacks (trans,
680 (GstRTSPSendFunc) do_send_data,
681 (GstRTSPSendFunc) do_send_data, client, NULL);
683 priv->transports = g_list_prepend (priv->transports, trans);
685 /* make sure our session can't expire */
686 gst_rtsp_session_prevent_expire (session);
690 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
691 GstRTSPStreamTransport * trans)
693 GstRTSPClientPrivate *priv = client->priv;
695 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
697 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
699 priv->transports = g_list_remove (priv->transports, trans);
701 /* our session can now expire */
702 gst_rtsp_session_allow_expire (session);
706 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
707 GstRTSPSessionMedia * sessmedia)
712 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
713 for (i = 0; i < n_streams; i++) {
714 GstRTSPStreamTransport *trans;
715 const GstRTSPTransport *tr;
717 /* get the transport, if there is no transport configured, skip this stream */
718 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
722 tr = gst_rtsp_stream_transport_get_transport (trans);
724 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
725 /* for TCP, unlink the stream from the TCP connection of the client */
726 unlink_transport (client, session, trans);
732 close_connection (GstRTSPClient * client)
734 GstRTSPClientPrivate *priv = client->priv;
735 const gchar *tunnelid;
737 GST_DEBUG ("client %p: closing connection", client);
739 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
740 g_mutex_lock (&tunnels_lock);
741 /* remove from tunnelids */
742 g_hash_table_remove (tunnels, tunnelid);
743 g_mutex_unlock (&tunnels_lock);
746 gst_rtsp_connection_close (priv->connection);
750 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
752 GstRTSPClientPrivate *priv = client->priv;
753 GstRTSPSession *session;
754 GstRTSPSessionMedia *sessmedia;
755 GstRTSPStatusCode code;
762 session = state->session;
767 path = state->uri->abspath;
769 /* get a handle to the configuration of the media in the session */
770 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
774 /* only aggregate control for now.. */
775 if (path[matched] != '\0')
778 state->sessmedia = sessmedia;
780 /* we emit the signal before closing the connection */
781 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
784 /* unlink the all TCP callbacks */
785 unlink_session_transports (client, session, sessmedia);
787 /* remove the session from the watched sessions */
788 client_unwatch_session (client, session);
790 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
792 /* unmanage the media in the session, returns false if all media session
794 if (!gst_rtsp_session_release_media (session, sessmedia)) {
795 /* remove the session */
796 gst_rtsp_session_pool_remove (priv->session_pool, session);
798 /* construct the response now */
799 code = GST_RTSP_STS_OK;
800 gst_rtsp_message_init_response (state->response, code,
801 gst_rtsp_status_as_text (code), state->request);
803 send_message (client, session, state->response, TRUE);
810 GST_ERROR ("client %p: no session", client);
811 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
816 GST_ERROR ("client %p: no uri supplied", client);
817 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
822 GST_ERROR ("client %p: no media for uri", client);
823 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
828 GST_ERROR ("client %p: no aggregate path %s", client, path);
829 send_generic_response (client,
830 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
836 default_params_set (GstRTSPClient * client, GstRTSPClientState * state)
840 res = gst_rtsp_params_set (client, state);
846 default_params_get (GstRTSPClient * client, GstRTSPClientState * state)
850 res = gst_rtsp_params_get (client, state);
856 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
862 res = gst_rtsp_message_get_body (state->request, &data, &size);
863 if (res != GST_RTSP_OK)
867 /* no body, keep-alive request */
868 send_generic_response (client, GST_RTSP_STS_OK, state);
870 /* there is a body, handle the params */
871 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, state);
872 if (res != GST_RTSP_OK)
875 send_message (client, state->session, state->response, FALSE);
878 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
886 GST_ERROR ("client %p: bad request", client);
887 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
893 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
899 res = gst_rtsp_message_get_body (state->request, &data, &size);
900 if (res != GST_RTSP_OK)
904 /* no body, keep-alive request */
905 send_generic_response (client, GST_RTSP_STS_OK, state);
907 /* there is a body, handle the params */
908 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, state);
909 if (res != GST_RTSP_OK)
912 send_message (client, state->session, state->response, FALSE);
915 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
923 GST_ERROR ("client %p: bad request", client);
924 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
930 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
932 GstRTSPSession *session;
933 GstRTSPSessionMedia *sessmedia;
934 GstRTSPStatusCode code;
935 GstRTSPState rtspstate;
939 if (!(session = state->session))
945 path = state->uri->abspath;
947 /* get a handle to the configuration of the media in the session */
948 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
952 if (path[matched] != '\0')
955 state->sessmedia = sessmedia;
957 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
958 /* the session state must be playing or recording */
959 if (rtspstate != GST_RTSP_STATE_PLAYING &&
960 rtspstate != GST_RTSP_STATE_RECORDING)
963 /* unlink the all TCP callbacks */
964 unlink_session_transports (client, session, sessmedia);
966 /* then pause sending */
967 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
969 /* construct the response now */
970 code = GST_RTSP_STS_OK;
971 gst_rtsp_message_init_response (state->response, code,
972 gst_rtsp_status_as_text (code), state->request);
974 send_message (client, session, state->response, FALSE);
976 /* the state is now READY */
977 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
979 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
987 GST_ERROR ("client %p: no seesion", client);
988 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
993 GST_ERROR ("client %p: no uri supplied", client);
994 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
999 GST_ERROR ("client %p: no media for uri", client);
1000 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1005 GST_ERROR ("client %p: no aggregate path %s", client, path);
1006 send_generic_response (client,
1007 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
1012 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
1013 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1020 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
1022 GstRTSPSession *session;
1023 GstRTSPSessionMedia *sessmedia;
1024 GstRTSPMedia *media;
1025 GstRTSPStatusCode code;
1027 guint n_streams, i, infocount;
1029 GstRTSPTimeRange *range;
1031 GstRTSPState rtspstate;
1032 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1036 if (!(session = state->session))
1042 path = state->uri->abspath;
1044 /* get a handle to the configuration of the media in the session */
1045 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1049 if (path[matched] != '\0')
1052 state->sessmedia = sessmedia;
1053 state->media = media = gst_rtsp_session_media_get_media (sessmedia);
1055 /* the session state must be playing or ready */
1056 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1057 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1060 /* parse the range header if we have one */
1062 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
1063 if (res == GST_RTSP_OK) {
1064 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1065 /* we have a range, seek to the position */
1067 gst_rtsp_media_seek (media, range);
1068 gst_rtsp_range_free (range);
1072 /* grab RTPInfo from the payloaders now */
1073 rtpinfo = g_string_new ("");
1075 n_streams = gst_rtsp_media_n_streams (media);
1076 for (i = 0, infocount = 0; i < n_streams; i++) {
1077 GstRTSPStreamTransport *trans;
1078 GstRTSPStream *stream;
1079 const GstRTSPTransport *tr;
1083 /* get the transport, if there is no transport configured, skip this stream */
1084 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
1085 if (trans == NULL) {
1086 GST_INFO ("stream %d is not configured", i);
1089 tr = gst_rtsp_stream_transport_get_transport (trans);
1091 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1092 /* for TCP, link the stream to the TCP connection of the client */
1093 link_transport (client, session, trans);
1096 stream = gst_rtsp_stream_transport_get_stream (trans);
1097 if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
1099 g_string_append (rtpinfo, ", ");
1101 uristr = gst_rtsp_url_get_request_uri (state->uri);
1102 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
1103 uristr, i, seq, rtptime);
1108 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
1112 /* construct the response now */
1113 code = GST_RTSP_STS_OK;
1114 gst_rtsp_message_init_response (state->response, code,
1115 gst_rtsp_status_as_text (code), state->request);
1117 /* add the RTP-Info header */
1118 if (infocount > 0) {
1119 str = g_string_free (rtpinfo, FALSE);
1120 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
1122 g_string_free (rtpinfo, TRUE);
1126 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1127 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
1129 send_message (client, session, state->response, FALSE);
1131 /* start playing after sending the request */
1132 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1134 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1136 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
1144 GST_ERROR ("client %p: no session", client);
1145 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1150 GST_ERROR ("client %p: no uri supplied", client);
1151 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1156 GST_ERROR ("client %p: media not found", client);
1157 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1162 GST_ERROR ("client %p: no aggregate path %s", client, path);
1163 send_generic_response (client,
1164 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
1169 GST_ERROR ("client %p: not PLAYING or READY", client);
1170 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1177 do_keepalive (GstRTSPSession * session)
1179 GST_INFO ("keep session %p alive", session);
1180 gst_rtsp_session_touch (session);
1183 /* parse @transport and return a valid transport in @tr. only transports
1184 * from @supported are returned. Returns FALSE if no valid transport
1187 parse_transport (const char *transport, GstRTSPLowerTrans supported,
1188 GstRTSPTransport * tr)
1195 gst_rtsp_transport_init (tr);
1197 GST_DEBUG ("parsing transports %s", transport);
1199 transports = g_strsplit (transport, ",", 0);
1201 /* loop through the transports, try to parse */
1202 for (i = 0; transports[i]; i++) {
1203 res = gst_rtsp_transport_parse (transports[i], tr);
1204 if (res != GST_RTSP_OK) {
1205 /* no valid transport, search some more */
1206 GST_WARNING ("could not parse transport %s", transports[i]);
1210 /* we have a transport, see if it's RTP/AVP */
1211 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1212 GST_WARNING ("invalid transport %s", transports[i]);
1216 if (!(tr->lower_transport & supported)) {
1217 GST_WARNING ("unsupported transport %s", transports[i]);
1221 /* we have a valid transport */
1222 GST_INFO ("found valid transport %s", transports[i]);
1227 gst_rtsp_transport_init (tr);
1229 g_strfreev (transports);
1235 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
1236 GstRTSPMessage * request)
1238 gchar *blocksize_str;
1239 gboolean ret = TRUE;
1241 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1242 &blocksize_str, 0) == GST_RTSP_OK) {
1246 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1247 if (end == blocksize_str) {
1248 GST_ERROR ("failed to parse blocksize");
1251 /* we don't want to change the mtu when this media
1252 * can be shared because it impacts other clients */
1253 if (gst_rtsp_media_is_shared (media))
1256 if (blocksize > G_MAXUINT)
1257 blocksize = G_MAXUINT;
1258 gst_rtsp_stream_set_mtu (stream, blocksize);
1265 default_configure_client_transport (GstRTSPClient * client,
1266 GstRTSPClientState * state, GstRTSPTransport * ct)
1268 GstRTSPClientPrivate *priv = client->priv;
1270 /* we have a valid transport now, set the destination of the client. */
1271 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1272 if (ct->destination && priv->use_client_settings) {
1273 GstRTSPAddress *addr;
1275 addr = gst_rtsp_stream_reserve_address (state->stream, ct->destination,
1276 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1281 gst_rtsp_address_free (addr);
1283 GstRTSPAddress *addr;
1284 GSocketFamily family;
1286 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1288 addr = gst_rtsp_stream_get_multicast_address (state->stream, family);
1292 g_free (ct->destination);
1293 ct->destination = g_strdup (addr->address);
1294 ct->port.min = addr->port;
1295 ct->port.max = addr->port + addr->n_ports - 1;
1296 ct->ttl = addr->ttl;
1298 gst_rtsp_address_free (addr);
1303 url = gst_rtsp_connection_get_url (priv->connection);
1304 g_free (ct->destination);
1305 ct->destination = g_strdup (url->host);
1307 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1308 /* check if the client selected channels for TCP */
1309 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1310 gst_rtsp_session_media_alloc_channels (state->sessmedia,
1320 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1325 static GstRTSPTransport *
1326 make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
1327 GstRTSPTransport * ct)
1329 GstRTSPTransport *st;
1331 GSocketFamily family;
1333 /* prepare the server transport */
1334 gst_rtsp_transport_new (&st);
1336 st->trans = ct->trans;
1337 st->profile = ct->profile;
1338 st->lower_transport = ct->lower_transport;
1340 addr = g_inet_address_new_from_string (ct->destination);
1343 GST_ERROR ("failed to get inet addr from client destination");
1344 family = G_SOCKET_FAMILY_IPV4;
1346 family = g_inet_address_get_family (addr);
1347 g_object_unref (addr);
1351 switch (st->lower_transport) {
1352 case GST_RTSP_LOWER_TRANS_UDP:
1353 st->client_port = ct->client_port;
1354 gst_rtsp_stream_get_server_port (state->stream, &st->server_port, family);
1356 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1357 st->port = ct->port;
1358 st->destination = g_strdup (ct->destination);
1361 case GST_RTSP_LOWER_TRANS_TCP:
1362 st->interleaved = ct->interleaved;
1367 gst_rtsp_stream_get_ssrc (state->stream, &st->ssrc);
1373 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
1375 GstRTSPClientPrivate *priv = client->priv;
1379 GstRTSPTransport *ct, *st;
1380 GstRTSPLowerTrans supported;
1381 GstRTSPStatusCode code;
1382 GstRTSPSession *session;
1383 GstRTSPStreamTransport *trans;
1385 GstRTSPSessionMedia *sessmedia;
1386 GstRTSPMedia *media;
1387 GstRTSPStream *stream;
1388 GstRTSPState rtspstate;
1389 GstRTSPClientClass *klass;
1390 gchar *path, *control;
1397 path = uri->abspath;
1399 /* parse the transport */
1401 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
1403 if (res != GST_RTSP_OK)
1406 /* we create the session after parsing stuff so that we don't make
1407 * a session for malformed requests */
1408 if (priv->session_pool == NULL)
1411 session = state->session;
1414 g_object_ref (session);
1415 /* get a handle to the configuration of the media in the session, this can
1416 * return NULL if this is a new url to manage in this session. */
1417 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1419 /* we need a new media configuration in this session */
1423 /* we have no session media, find one and manage it */
1424 if (sessmedia == NULL) {
1425 /* get a handle to the configuration of the media in the session */
1426 media = find_media (client, state, &matched);
1428 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1429 g_object_ref (media);
1431 /* no media, not found then */
1433 goto media_not_found;
1435 /* path is what matched. We can modify the parsed uri in place */
1436 path[matched] = '\0';
1437 /* control is remainder */
1438 control = &path[matched + 1];
1440 /* find the stream now using the control part */
1441 stream = gst_rtsp_media_find_stream (media, control);
1443 goto stream_not_found;
1445 /* now we have a uri identifying a valid media and stream */
1446 state->stream = stream;
1447 state->media = media;
1449 if (session == NULL) {
1450 /* create a session if this fails we probably reached our session limit or
1452 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1453 goto service_unavailable;
1455 /* make sure this client is closed when the session is closed */
1456 client_watch_session (client, session);
1458 /* signal new session */
1459 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1462 state->session = session;
1465 if (sessmedia == NULL) {
1466 /* manage the media in our session now, if not done already */
1467 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1468 /* if we stil have no media, error */
1469 if (sessmedia == NULL)
1470 goto sessmedia_unavailable;
1472 g_object_unref (media);
1475 state->sessmedia = sessmedia;
1477 /* set blocksize on this stream */
1478 if (!handle_blocksize (media, stream, state->request))
1479 goto invalid_blocksize;
1481 gst_rtsp_transport_new (&ct);
1483 /* our supported transports */
1484 supported = GST_RTSP_LOWER_TRANS_UDP |
1485 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
1487 /* parse and find a usable supported transport */
1488 if (!parse_transport (transport, supported, ct))
1489 goto unsupported_transports;
1491 /* update the client transport */
1492 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1493 if (!klass->configure_client_transport (client, state, ct))
1494 goto unsupported_client_transport;
1496 /* set in the session media transport */
1497 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1499 /* configure keepalive for this transport */
1500 gst_rtsp_stream_transport_set_keepalive (trans,
1501 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1503 /* create and serialize the server transport */
1504 st = make_server_transport (client, state, ct);
1505 trans_str = gst_rtsp_transport_as_text (st);
1506 gst_rtsp_transport_free (st);
1508 /* construct the response now */
1509 code = GST_RTSP_STS_OK;
1510 gst_rtsp_message_init_response (state->response, code,
1511 gst_rtsp_status_as_text (code), state->request);
1513 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1517 send_message (client, session, state->response, FALSE);
1519 /* update the state */
1520 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1521 switch (rtspstate) {
1522 case GST_RTSP_STATE_PLAYING:
1523 case GST_RTSP_STATE_RECORDING:
1524 case GST_RTSP_STATE_READY:
1525 /* no state change */
1528 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1531 g_object_unref (session);
1533 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST],
1541 GST_ERROR ("client %p: no uri", client);
1542 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1547 GST_ERROR ("client %p: no transport", client);
1548 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1553 GST_ERROR ("client %p: no session pool configured", client);
1554 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1559 GST_ERROR ("client %p: media '%s' not found", client, path);
1560 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1565 GST_ERROR ("client %p: stream '%s' not found", client, control);
1566 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1567 g_object_unref (media);
1570 service_unavailable:
1572 GST_ERROR ("client %p: can't create session", client);
1573 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1574 g_object_unref (media);
1577 sessmedia_unavailable:
1579 GST_ERROR ("client %p: can't create session media", client);
1580 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1581 g_object_unref (media);
1582 g_object_unref (session);
1587 GST_ERROR ("client %p: invalid blocksize", client);
1588 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1589 g_object_unref (session);
1592 unsupported_transports:
1594 GST_ERROR ("client %p: unsupported transports", client);
1595 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1596 gst_rtsp_transport_free (ct);
1597 g_object_unref (session);
1600 unsupported_client_transport:
1602 GST_ERROR ("client %p: unsupported client transport", client);
1603 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1604 gst_rtsp_transport_free (ct);
1605 g_object_unref (session);
1610 static GstSDPMessage *
1611 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1613 GstRTSPClientPrivate *priv = client->priv;
1618 gst_sdp_message_new (&sdp);
1620 /* some standard things first */
1621 gst_sdp_message_set_version (sdp, "0");
1628 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1631 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1632 gst_sdp_message_set_information (sdp, "rtsp-server");
1633 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1634 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1635 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1636 gst_sdp_message_add_attribute (sdp, "control", "*");
1638 info.is_ipv6 = priv->is_ipv6;
1639 info.server_ip = priv->server_ip;
1641 /* create an SDP for the media object */
1642 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1650 GST_ERROR ("client %p: could not create SDP", client);
1651 gst_sdp_message_free (sdp);
1656 /* for the describe we must generate an SDP */
1658 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1663 gchar *str, *content_base;
1664 GstRTSPMedia *media;
1665 GstRTSPClientClass *klass;
1667 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1672 /* check what kind of format is accepted, we don't really do anything with it
1673 * and always return SDP for now. */
1678 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1680 if (res == GST_RTSP_ENOTIMPL)
1683 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1687 /* find the media object for the uri */
1688 if (!(media = find_media (client, state, NULL)))
1691 /* create an SDP for the media object on this client */
1692 if (!(sdp = klass->create_sdp (client, media)))
1695 g_object_unref (media);
1697 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1698 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1700 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1703 /* content base for some clients that might screw up creating the setup uri */
1704 str = gst_rtsp_url_get_request_uri (state->uri);
1705 str_len = strlen (str);
1707 /* check for trailing '/' and append one */
1708 if (str[str_len - 1] != '/') {
1709 content_base = g_malloc (str_len + 2);
1710 memcpy (content_base, str, str_len);
1711 content_base[str_len] = '/';
1712 content_base[str_len + 1] = '\0';
1718 GST_INFO ("adding content-base: %s", content_base);
1720 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1722 g_free (content_base);
1724 /* add SDP to the response body */
1725 str = gst_sdp_message_as_text (sdp);
1726 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1727 gst_sdp_message_free (sdp);
1729 send_message (client, state->session, state->response, FALSE);
1731 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1739 GST_ERROR ("client %p: no uri", client);
1740 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1745 GST_ERROR ("client %p: no media", client);
1746 /* error reply is already sent */
1751 GST_ERROR ("client %p: can't create SDP", client);
1752 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1753 g_object_unref (media);
1759 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1761 GstRTSPMethod options;
1764 options = GST_RTSP_DESCRIBE |
1769 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1771 str = gst_rtsp_options_as_text (options);
1773 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1774 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1776 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1779 send_message (client, state->session, state->response, FALSE);
1781 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1787 /* remove duplicate and trailing '/' */
1789 sanitize_uri (GstRTSPUrl * uri)
1793 gboolean have_slash, prev_slash;
1795 s = d = uri->abspath;
1796 len = strlen (uri->abspath);
1800 for (i = 0; i < len; i++) {
1801 have_slash = s[i] == '/';
1803 if (!have_slash || !prev_slash)
1805 prev_slash = have_slash;
1807 len = d - uri->abspath;
1808 /* don't remove the first slash if that's the only thing left */
1809 if (len > 1 && *(d - 1) == '/')
1815 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1817 GstRTSPClientPrivate *priv = client->priv;
1819 GST_INFO ("client %p: session %p finished", client, session);
1821 /* unlink all media managed in this session */
1822 client_unlink_session (client, session);
1824 /* remove the session */
1825 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1826 GST_INFO ("client %p: all sessions finalized, close the connection",
1828 close_connection (client);
1832 static GPrivate current_state;
1835 * gst_rtsp_client_state_get_current:
1837 * Get the current #GstRTSPClientState. This object is retrieved from the
1838 * current thread that is handling the request for a client.
1840 * Returns: a #GstRTSPClientState
1842 GstRTSPClientState *
1843 gst_rtsp_client_state_get_current (void)
1847 l = g_private_get (¤t_state);
1851 return (GstRTSPClientState *) (l->data);
1856 * gst_rtsp_client_state_push_current:
1857 * @state: a ##GstRTSPClientState
1859 * Pushes @state onto the state stack. The current
1860 * state can then be received using gst_rtsp_client_state_get_current().
1863 gst_rtsp_client_state_push_current (GstRTSPClientState * state)
1867 g_return_if_fail (state != NULL);
1869 l = g_private_get (¤t_state);
1870 l = g_slist_prepend (l, state);
1871 g_private_set (¤t_state, l);
1875 * gst_rtsp_client_state_pop_current:
1876 * @state: a #GstRTSPClientState
1878 * Pops @state off the state stack (verifying that @state
1879 * is on the top of the stack).
1882 gst_rtsp_client_state_pop_current (GstRTSPClientState * state)
1886 l = g_private_get (¤t_state);
1888 g_return_if_fail (l != NULL);
1889 g_return_if_fail (l->data == state);
1891 l = g_slist_delete_link (l, l);
1892 g_private_set (¤t_state, l);
1896 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1898 GstRTSPClientPrivate *priv = client->priv;
1899 GstRTSPMethod method;
1900 const gchar *uristr;
1901 GstRTSPUrl *uri = NULL;
1902 GstRTSPVersion version;
1904 GstRTSPSession *session = NULL;
1905 GstRTSPClientState state = { NULL };
1906 GstRTSPMessage response = { 0 };
1909 state.conn = priv->connection;
1910 state.client = client;
1911 state.request = request;
1912 state.response = &response;
1913 state.auth = priv->auth;
1914 gst_rtsp_client_state_push_current (&state);
1916 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1917 gst_rtsp_message_dump (request);
1920 GST_INFO ("client %p: received a request", client);
1922 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1924 /* we can only handle 1.0 requests */
1925 if (version != GST_RTSP_VERSION_1_0)
1928 state.method = method;
1930 /* we always try to parse the url first */
1931 if (strcmp (uristr, "*") == 0) {
1932 /* special case where we have * as uri, keep uri = NULL */
1933 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1936 /* get the session if there is any */
1937 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1938 if (res == GST_RTSP_OK) {
1939 if (priv->session_pool == NULL)
1942 /* we had a session in the request, find it again */
1943 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1944 goto session_not_found;
1946 /* we add the session to the client list of watched sessions. When a session
1947 * disappears because it times out, we will be notified. If all sessions are
1948 * gone, we will close the connection */
1949 client_watch_session (client, session);
1952 /* sanitize the uri */
1956 state.session = session;
1958 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
1959 goto not_authorized;
1961 /* now see what is asked and dispatch to a dedicated handler */
1963 case GST_RTSP_OPTIONS:
1964 handle_options_request (client, &state);
1966 case GST_RTSP_DESCRIBE:
1967 handle_describe_request (client, &state);
1969 case GST_RTSP_SETUP:
1970 handle_setup_request (client, &state);
1973 handle_play_request (client, &state);
1975 case GST_RTSP_PAUSE:
1976 handle_pause_request (client, &state);
1978 case GST_RTSP_TEARDOWN:
1979 handle_teardown_request (client, &state);
1981 case GST_RTSP_SET_PARAMETER:
1982 handle_set_param_request (client, &state);
1984 case GST_RTSP_GET_PARAMETER:
1985 handle_get_param_request (client, &state);
1987 case GST_RTSP_ANNOUNCE:
1988 case GST_RTSP_RECORD:
1989 case GST_RTSP_REDIRECT:
1990 goto not_implemented;
1991 case GST_RTSP_INVALID:
1997 gst_rtsp_client_state_pop_current (&state);
1999 g_object_unref (session);
2001 gst_rtsp_url_free (uri);
2007 GST_ERROR ("client %p: version %d not supported", client, version);
2008 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
2014 GST_ERROR ("client %p: bad request", client);
2015 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
2020 GST_ERROR ("client %p: no pool configured", client);
2021 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
2026 GST_ERROR ("client %p: session not found", client);
2027 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
2032 GST_ERROR ("client %p: not allowed", client);
2033 handle_unauthorized_request (client, priv->auth, &state);
2038 GST_ERROR ("client %p: method %d not implemented", client, method);
2039 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
2045 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2047 GstRTSPClientPrivate *priv = client->priv;
2056 /* find the stream for this message */
2057 res = gst_rtsp_message_parse_data (message, &channel);
2058 if (res != GST_RTSP_OK)
2061 gst_rtsp_message_steal_body (message, &data, &size);
2063 buffer = gst_buffer_new_wrapped (data, size);
2066 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2067 GstRTSPStreamTransport *trans;
2068 GstRTSPStream *stream;
2069 const GstRTSPTransport *tr;
2073 tr = gst_rtsp_stream_transport_get_transport (trans);
2074 stream = gst_rtsp_stream_transport_get_stream (trans);
2076 /* check for TCP transport */
2077 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2078 /* dispatch to the stream based on the channel number */
2079 if (tr->interleaved.min == channel) {
2080 gst_rtsp_stream_recv_rtp (stream, buffer);
2083 } else if (tr->interleaved.max == channel) {
2084 gst_rtsp_stream_recv_rtcp (stream, buffer);
2091 gst_buffer_unref (buffer);
2095 * gst_rtsp_client_set_session_pool:
2096 * @client: a #GstRTSPClient
2097 * @pool: a #GstRTSPSessionPool
2099 * Set @pool as the sessionpool for @client which it will use to find
2100 * or allocate sessions. the sessionpool is usually inherited from the server
2101 * that created the client but can be overridden later.
2104 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2105 GstRTSPSessionPool * pool)
2107 GstRTSPSessionPool *old;
2108 GstRTSPClientPrivate *priv;
2110 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2112 priv = client->priv;
2115 g_object_ref (pool);
2117 g_mutex_lock (&priv->lock);
2118 old = priv->session_pool;
2119 priv->session_pool = pool;
2120 g_mutex_unlock (&priv->lock);
2123 g_object_unref (old);
2127 * gst_rtsp_client_get_session_pool:
2128 * @client: a #GstRTSPClient
2130 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2132 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2134 GstRTSPSessionPool *
2135 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2137 GstRTSPClientPrivate *priv;
2138 GstRTSPSessionPool *result;
2140 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2142 priv = client->priv;
2144 g_mutex_lock (&priv->lock);
2145 if ((result = priv->session_pool))
2146 g_object_ref (result);
2147 g_mutex_unlock (&priv->lock);
2153 * gst_rtsp_client_set_mount_points:
2154 * @client: a #GstRTSPClient
2155 * @mounts: a #GstRTSPMountPoints
2157 * Set @mounts as the mount points for @client which it will use to map urls
2158 * to media streams. These mount points are usually inherited from the server that
2159 * created the client but can be overriden later.
2162 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2163 GstRTSPMountPoints * mounts)
2165 GstRTSPClientPrivate *priv;
2166 GstRTSPMountPoints *old;
2168 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2170 priv = client->priv;
2173 g_object_ref (mounts);
2175 g_mutex_lock (&priv->lock);
2176 old = priv->mount_points;
2177 priv->mount_points = mounts;
2178 g_mutex_unlock (&priv->lock);
2181 g_object_unref (old);
2185 * gst_rtsp_client_get_mount_points:
2186 * @client: a #GstRTSPClient
2188 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2190 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2192 GstRTSPMountPoints *
2193 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2195 GstRTSPClientPrivate *priv;
2196 GstRTSPMountPoints *result;
2198 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2200 priv = client->priv;
2202 g_mutex_lock (&priv->lock);
2203 if ((result = priv->mount_points))
2204 g_object_ref (result);
2205 g_mutex_unlock (&priv->lock);
2211 * gst_rtsp_client_set_use_client_settings:
2212 * @client: a #GstRTSPClient
2213 * @use_client_settings: whether to use client settings for multicast
2215 * Use client transport settings (destination and ttl) for multicast.
2216 * When @use_client_settings is %FALSE, the server settings will be
2220 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
2221 gboolean use_client_settings)
2223 GstRTSPClientPrivate *priv;
2225 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2227 priv = client->priv;
2229 g_mutex_lock (&priv->lock);
2230 priv->use_client_settings = use_client_settings;
2231 g_mutex_unlock (&priv->lock);
2235 * gst_rtsp_client_get_use_client_settings:
2236 * @client: a #GstRTSPClient
2238 * Check if client transport settings (destination and ttl) for multicast
2242 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
2244 GstRTSPClientPrivate *priv;
2247 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2249 priv = client->priv;
2251 g_mutex_lock (&priv->lock);
2252 res = priv->use_client_settings;
2253 g_mutex_unlock (&priv->lock);
2259 * gst_rtsp_client_set_auth:
2260 * @client: a #GstRTSPClient
2261 * @auth: a #GstRTSPAuth
2263 * configure @auth to be used as the authentication manager of @client.
2266 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2268 GstRTSPClientPrivate *priv;
2271 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2273 priv = client->priv;
2276 g_object_ref (auth);
2278 g_mutex_lock (&priv->lock);
2281 g_mutex_unlock (&priv->lock);
2284 g_object_unref (old);
2289 * gst_rtsp_client_get_auth:
2290 * @client: a #GstRTSPClient
2292 * Get the #GstRTSPAuth used as the authentication manager of @client.
2294 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2298 gst_rtsp_client_get_auth (GstRTSPClient * client)
2300 GstRTSPClientPrivate *priv;
2301 GstRTSPAuth *result;
2303 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2305 priv = client->priv;
2307 g_mutex_lock (&priv->lock);
2308 if ((result = priv->auth))
2309 g_object_ref (result);
2310 g_mutex_unlock (&priv->lock);
2316 * gst_rtsp_client_set_thread_pool:
2317 * @client: a #GstRTSPClient
2318 * @pool: a #GstRTSPThreadPool
2320 * configure @pool to be used as the thread pool of @client.
2323 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2324 GstRTSPThreadPool * pool)
2326 GstRTSPClientPrivate *priv;
2327 GstRTSPThreadPool *old;
2329 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2331 priv = client->priv;
2334 g_object_ref (pool);
2336 g_mutex_lock (&priv->lock);
2337 old = priv->thread_pool;
2338 priv->thread_pool = pool;
2339 g_mutex_unlock (&priv->lock);
2342 g_object_unref (old);
2346 * gst_rtsp_client_get_thread_pool:
2347 * @client: a #GstRTSPClient
2349 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2351 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2355 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2357 GstRTSPClientPrivate *priv;
2358 GstRTSPThreadPool *result;
2360 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2362 priv = client->priv;
2364 g_mutex_lock (&priv->lock);
2365 if ((result = priv->thread_pool))
2366 g_object_ref (result);
2367 g_mutex_unlock (&priv->lock);
2373 * gst_rtsp_client_set_connection:
2374 * @client: a #GstRTSPClient
2375 * @conn: (transfer full): a #GstRTSPConnection
2377 * Set the #GstRTSPConnection of @client. This function takes ownership of
2380 * Returns: %TRUE on success.
2383 gst_rtsp_client_set_connection (GstRTSPClient * client,
2384 GstRTSPConnection * conn)
2386 GstRTSPClientPrivate *priv;
2387 GSocket *read_socket;
2388 GSocketAddress *address;
2390 GError *error = NULL;
2392 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2393 g_return_val_if_fail (conn != NULL, FALSE);
2395 priv = client->priv;
2397 read_socket = gst_rtsp_connection_get_read_socket (conn);
2399 if (!(address = g_socket_get_local_address (read_socket, &error)))
2402 g_free (priv->server_ip);
2403 /* keep the original ip that the client connected to */
2404 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2405 GInetAddress *iaddr;
2407 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2409 /* socket might be ipv6 but adress still ipv4 */
2410 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2411 priv->server_ip = g_inet_address_to_string (iaddr);
2412 g_object_unref (address);
2414 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2415 priv->server_ip = g_strdup ("unknown");
2418 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2419 priv->server_ip, priv->is_ipv6);
2421 url = gst_rtsp_connection_get_url (conn);
2422 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2424 priv->connection = conn;
2431 GST_ERROR ("could not get local address %s", error->message);
2432 g_error_free (error);
2438 * gst_rtsp_client_get_connection:
2439 * @client: a #GstRTSPClient
2441 * Get the #GstRTSPConnection of @client.
2443 * Returns: (transfer none): the #GstRTSPConnection of @client.
2444 * The connection object returned remains valid until the client is freed.
2447 gst_rtsp_client_get_connection (GstRTSPClient * client)
2449 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2451 return client->priv->connection;
2455 * gst_rtsp_client_set_send_func:
2456 * @client: a #GstRTSPClient
2457 * @func: a #GstRTSPClientSendFunc
2458 * @user_data: user data passed to @func
2459 * @notify: called when @user_data is no longer in use
2461 * Set @func as the callback that will be called when a new message needs to be
2462 * sent to the client. @user_data is passed to @func and @notify is called when
2463 * @user_data is no longer in use.
2465 * By default, the client will send the messages on the #GstRTSPConnection that
2466 * was configured with gst_rtsp_client_attach() was called.
2469 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2470 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2472 GstRTSPClientPrivate *priv;
2473 GDestroyNotify old_notify;
2476 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2478 priv = client->priv;
2480 g_mutex_lock (&priv->send_lock);
2481 priv->send_func = func;
2482 old_notify = priv->send_notify;
2483 old_data = priv->send_data;
2484 priv->send_notify = notify;
2485 priv->send_data = user_data;
2486 g_mutex_unlock (&priv->send_lock);
2489 old_notify (old_data);
2493 * gst_rtsp_client_handle_message:
2494 * @client: a #GstRTSPClient
2495 * @message: an #GstRTSPMessage
2497 * Let the client handle @message.
2499 * Returns: a #GstRTSPResult.
2502 gst_rtsp_client_handle_message (GstRTSPClient * client,
2503 GstRTSPMessage * message)
2505 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2506 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2508 switch (message->type) {
2509 case GST_RTSP_MESSAGE_REQUEST:
2510 handle_request (client, message);
2512 case GST_RTSP_MESSAGE_RESPONSE:
2514 case GST_RTSP_MESSAGE_DATA:
2515 handle_data (client, message);
2524 * gst_rtsp_client_send_request:
2525 * @client: a #GstRTSPClient
2526 * @session: a #GstRTSPSession to send the request to or %NULL
2527 * @request: The request #GstRTSPMessage to send
2529 * Send a request message to the remote end. @request must be a
2530 * #GST_RTSP_MESSAGE_REQUEST.
2533 gst_rtsp_client_send_request (GstRTSPClient * client, GstRTSPSession * session,
2534 GstRTSPMessage * request)
2536 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2537 g_return_val_if_fail (request != NULL, GST_RTSP_EINVAL);
2538 g_return_val_if_fail (request->type == GST_RTSP_MESSAGE_REQUEST,
2541 send_message (client, session, request, FALSE);
2546 static GstRTSPResult
2547 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2548 gboolean close, gpointer user_data)
2550 GstRTSPClientPrivate *priv = client->priv;
2552 /* send the response and store the seq number so we can wait until it's
2553 * written to the client to close the connection */
2554 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2555 &priv->close_seq : NULL);
2558 static GstRTSPResult
2559 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2562 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2565 static GstRTSPResult
2566 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2568 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2569 GstRTSPClientPrivate *priv = client->priv;
2571 if (priv->close_seq && priv->close_seq == cseq) {
2572 priv->close_seq = 0;
2573 close_connection (client);
2579 static GstRTSPResult
2580 closed (GstRTSPWatch * watch, gpointer user_data)
2582 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2583 GstRTSPClientPrivate *priv = client->priv;
2584 const gchar *tunnelid;
2586 GST_INFO ("client %p: connection closed", client);
2588 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2589 g_mutex_lock (&tunnels_lock);
2590 /* remove from tunnelids */
2591 g_hash_table_remove (tunnels, tunnelid);
2592 g_mutex_unlock (&tunnels_lock);
2595 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2600 static GstRTSPResult
2601 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2603 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2606 str = gst_rtsp_strresult (result);
2607 GST_INFO ("client %p: received an error %s", client, str);
2613 static GstRTSPResult
2614 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2615 GstRTSPMessage * message, guint id, gpointer user_data)
2617 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2620 str = gst_rtsp_strresult (result);
2622 ("client %p: error when handling message %p with id %d: %s",
2623 client, message, id, str);
2630 remember_tunnel (GstRTSPClient * client)
2632 GstRTSPClientPrivate *priv = client->priv;
2633 const gchar *tunnelid;
2635 /* store client in the pending tunnels */
2636 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2637 if (tunnelid == NULL)
2640 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2642 /* we can't have two clients connecting with the same tunnelid */
2643 g_mutex_lock (&tunnels_lock);
2644 if (g_hash_table_lookup (tunnels, tunnelid))
2645 goto tunnel_existed;
2647 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2648 g_mutex_unlock (&tunnels_lock);
2655 GST_ERROR ("client %p: no tunnelid provided", client);
2660 g_mutex_unlock (&tunnels_lock);
2661 GST_ERROR ("client %p: tunnel session %s already existed", client,
2667 static GstRTSPStatusCode
2668 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2670 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2671 GstRTSPClientPrivate *priv = client->priv;
2673 GST_INFO ("client %p: tunnel start (connection %p)", client,
2676 if (!remember_tunnel (client))
2679 return GST_RTSP_STS_OK;
2684 GST_ERROR ("client %p: error starting tunnel", client);
2685 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2689 static GstRTSPResult
2690 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2692 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2693 GstRTSPClientPrivate *priv = client->priv;
2695 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2698 /* ignore error, it'll only be a problem when the client does a POST again */
2699 remember_tunnel (client);
2704 static GstRTSPResult
2705 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2707 const gchar *tunnelid;
2708 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2709 GstRTSPClientPrivate *priv = client->priv;
2710 GstRTSPClient *oclient;
2711 GstRTSPClientPrivate *opriv;
2713 GST_INFO ("client %p: tunnel complete", client);
2715 /* find previous tunnel */
2716 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2717 if (tunnelid == NULL)
2720 g_mutex_lock (&tunnels_lock);
2721 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2724 /* remove the old client from the table. ref before because removing it will
2725 * remove the ref to it. */
2726 g_object_ref (oclient);
2727 g_hash_table_remove (tunnels, tunnelid);
2729 opriv = oclient->priv;
2731 if (opriv->watch == NULL)
2733 g_mutex_unlock (&tunnels_lock);
2735 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2736 opriv->connection, priv->connection);
2738 /* merge the tunnels into the first client */
2739 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2740 gst_rtsp_watch_reset (opriv->watch);
2741 g_object_unref (oclient);
2748 GST_ERROR ("client %p: no tunnelid provided", client);
2749 return GST_RTSP_ERROR;
2753 g_mutex_unlock (&tunnels_lock);
2754 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2755 return GST_RTSP_ERROR;
2759 g_mutex_unlock (&tunnels_lock);
2760 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2761 g_object_unref (oclient);
2762 return GST_RTSP_ERROR;
2766 static GstRTSPWatchFuncs watch_funcs = {
2778 client_watch_notify (GstRTSPClient * client)
2780 GstRTSPClientPrivate *priv = client->priv;
2782 GST_INFO ("client %p: watch destroyed", client);
2784 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2785 g_object_unref (client);
2789 * gst_rtsp_client_attach:
2790 * @client: a #GstRTSPClient
2791 * @context: (allow-none): a #GMainContext
2793 * Attaches @client to @context. When the mainloop for @context is run, the
2794 * client will be dispatched. When @context is NULL, the default context will be
2797 * This function should be called when the client properties and urls are fully
2798 * configured and the client is ready to start.
2800 * Returns: the ID (greater than 0) for the source within the GMainContext.
2803 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2805 GstRTSPClientPrivate *priv;
2808 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2809 priv = client->priv;
2810 g_return_val_if_fail (priv->connection != NULL, 0);
2811 g_return_val_if_fail (priv->watch == NULL, 0);
2813 /* create watch for the connection and attach */
2814 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2815 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2816 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2817 (GDestroyNotify) gst_rtsp_watch_unref);
2819 /* FIXME make this configurable. We don't want to do this yet because it will
2820 * be superceeded by a cache object later */
2821 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2823 GST_INFO ("attaching to context %p", context);
2824 res = gst_rtsp_watch_attach (priv->watch, context);
2830 * gst_rtsp_client_session_filter:
2831 * @client: a #GstRTSPclient
2832 * @func: (scope call): a callback
2833 * @user_data: user data passed to @func
2835 * Call @func for each session managed by @client. The result value of @func
2836 * determines what happens to the session. @func will be called with @client
2837 * locked so no further actions on @client can be performed from @func.
2839 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
2842 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
2844 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
2845 * will also be added with an additional ref to the result #GList of this
2848 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
2849 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2850 * element in the #GList should be unreffed before the list is freed.
2853 gst_rtsp_client_session_filter (GstRTSPClient * client,
2854 GstRTSPClientSessionFilterFunc func, gpointer user_data)
2856 GstRTSPClientPrivate *priv;
2857 GList *result, *walk, *next;
2859 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2860 g_return_val_if_fail (func != NULL, NULL);
2862 priv = client->priv;
2866 g_mutex_lock (&priv->lock);
2867 for (walk = priv->sessions; walk; walk = next) {
2868 GstRTSPSession *sess = walk->data;
2870 next = g_list_next (walk);
2872 switch (func (client, sess, user_data)) {
2873 case GST_RTSP_FILTER_REMOVE:
2874 /* stop watching the session and pretent it went away */
2875 client_cleanup_session (client, sess);
2877 case GST_RTSP_FILTER_REF:
2878 result = g_list_prepend (result, g_object_ref (sess));
2880 case GST_RTSP_FILTER_KEEP:
2885 g_mutex_unlock (&priv->lock);