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 state_key;
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)
1845 return g_private_get (&state_key);
1849 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1851 GstRTSPClientPrivate *priv = client->priv;
1852 GstRTSPMethod method;
1853 const gchar *uristr;
1854 GstRTSPUrl *uri = NULL;
1855 GstRTSPVersion version;
1857 GstRTSPSession *session = NULL;
1858 GstRTSPClientState state = { NULL };
1859 GstRTSPMessage response = { 0 };
1862 state.client = client;
1863 state.request = request;
1864 state.response = &response;
1865 state.auth = priv->auth;
1866 g_private_set (&state_key, &state);
1868 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1869 gst_rtsp_message_dump (request);
1872 GST_INFO ("client %p: received a request", client);
1874 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1876 /* we can only handle 1.0 requests */
1877 if (version != GST_RTSP_VERSION_1_0)
1880 state.method = method;
1882 /* we always try to parse the url first */
1883 if (strcmp (uristr, "*") == 0) {
1884 /* special case where we have * as uri, keep uri = NULL */
1885 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1888 /* get the session if there is any */
1889 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1890 if (res == GST_RTSP_OK) {
1891 if (priv->session_pool == NULL)
1894 /* we had a session in the request, find it again */
1895 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1896 goto session_not_found;
1898 /* we add the session to the client list of watched sessions. When a session
1899 * disappears because it times out, we will be notified. If all sessions are
1900 * gone, we will close the connection */
1901 client_watch_session (client, session);
1904 /* sanitize the uri */
1908 state.session = session;
1910 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
1911 goto not_authorized;
1913 /* now see what is asked and dispatch to a dedicated handler */
1915 case GST_RTSP_OPTIONS:
1916 handle_options_request (client, &state);
1918 case GST_RTSP_DESCRIBE:
1919 handle_describe_request (client, &state);
1921 case GST_RTSP_SETUP:
1922 handle_setup_request (client, &state);
1925 handle_play_request (client, &state);
1927 case GST_RTSP_PAUSE:
1928 handle_pause_request (client, &state);
1930 case GST_RTSP_TEARDOWN:
1931 handle_teardown_request (client, &state);
1933 case GST_RTSP_SET_PARAMETER:
1934 handle_set_param_request (client, &state);
1936 case GST_RTSP_GET_PARAMETER:
1937 handle_get_param_request (client, &state);
1939 case GST_RTSP_ANNOUNCE:
1940 case GST_RTSP_RECORD:
1941 case GST_RTSP_REDIRECT:
1942 goto not_implemented;
1943 case GST_RTSP_INVALID:
1949 g_private_set (&state_key, NULL);
1951 g_object_unref (session);
1953 gst_rtsp_url_free (uri);
1959 GST_ERROR ("client %p: version %d not supported", client, version);
1960 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1966 GST_ERROR ("client %p: bad request", client);
1967 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1972 GST_ERROR ("client %p: no pool configured", client);
1973 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1978 GST_ERROR ("client %p: session not found", client);
1979 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1984 GST_ERROR ("client %p: not allowed", client);
1985 handle_unauthorized_request (client, priv->auth, &state);
1990 GST_ERROR ("client %p: method %d not implemented", client, method);
1991 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1997 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1999 GstRTSPClientPrivate *priv = client->priv;
2008 /* find the stream for this message */
2009 res = gst_rtsp_message_parse_data (message, &channel);
2010 if (res != GST_RTSP_OK)
2013 gst_rtsp_message_steal_body (message, &data, &size);
2015 buffer = gst_buffer_new_wrapped (data, size);
2018 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2019 GstRTSPStreamTransport *trans;
2020 GstRTSPStream *stream;
2021 const GstRTSPTransport *tr;
2025 tr = gst_rtsp_stream_transport_get_transport (trans);
2026 stream = gst_rtsp_stream_transport_get_stream (trans);
2028 /* check for TCP transport */
2029 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2030 /* dispatch to the stream based on the channel number */
2031 if (tr->interleaved.min == channel) {
2032 gst_rtsp_stream_recv_rtp (stream, buffer);
2035 } else if (tr->interleaved.max == channel) {
2036 gst_rtsp_stream_recv_rtcp (stream, buffer);
2043 gst_buffer_unref (buffer);
2047 * gst_rtsp_client_set_session_pool:
2048 * @client: a #GstRTSPClient
2049 * @pool: a #GstRTSPSessionPool
2051 * Set @pool as the sessionpool for @client which it will use to find
2052 * or allocate sessions. the sessionpool is usually inherited from the server
2053 * that created the client but can be overridden later.
2056 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2057 GstRTSPSessionPool * pool)
2059 GstRTSPSessionPool *old;
2060 GstRTSPClientPrivate *priv;
2062 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2064 priv = client->priv;
2067 g_object_ref (pool);
2069 g_mutex_lock (&priv->lock);
2070 old = priv->session_pool;
2071 priv->session_pool = pool;
2072 g_mutex_unlock (&priv->lock);
2075 g_object_unref (old);
2079 * gst_rtsp_client_get_session_pool:
2080 * @client: a #GstRTSPClient
2082 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2084 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2086 GstRTSPSessionPool *
2087 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2089 GstRTSPClientPrivate *priv;
2090 GstRTSPSessionPool *result;
2092 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2094 priv = client->priv;
2096 g_mutex_lock (&priv->lock);
2097 if ((result = priv->session_pool))
2098 g_object_ref (result);
2099 g_mutex_unlock (&priv->lock);
2105 * gst_rtsp_client_set_mount_points:
2106 * @client: a #GstRTSPClient
2107 * @mounts: a #GstRTSPMountPoints
2109 * Set @mounts as the mount points for @client which it will use to map urls
2110 * to media streams. These mount points are usually inherited from the server that
2111 * created the client but can be overriden later.
2114 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2115 GstRTSPMountPoints * mounts)
2117 GstRTSPClientPrivate *priv;
2118 GstRTSPMountPoints *old;
2120 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2122 priv = client->priv;
2125 g_object_ref (mounts);
2127 g_mutex_lock (&priv->lock);
2128 old = priv->mount_points;
2129 priv->mount_points = mounts;
2130 g_mutex_unlock (&priv->lock);
2133 g_object_unref (old);
2137 * gst_rtsp_client_get_mount_points:
2138 * @client: a #GstRTSPClient
2140 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2142 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2144 GstRTSPMountPoints *
2145 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2147 GstRTSPClientPrivate *priv;
2148 GstRTSPMountPoints *result;
2150 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2152 priv = client->priv;
2154 g_mutex_lock (&priv->lock);
2155 if ((result = priv->mount_points))
2156 g_object_ref (result);
2157 g_mutex_unlock (&priv->lock);
2163 * gst_rtsp_client_set_use_client_settings:
2164 * @client: a #GstRTSPClient
2165 * @use_client_settings: whether to use client settings for multicast
2167 * Use client transport settings (destination and ttl) for multicast.
2168 * When @use_client_settings is %FALSE, the server settings will be
2172 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
2173 gboolean use_client_settings)
2175 GstRTSPClientPrivate *priv;
2177 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2179 priv = client->priv;
2181 g_mutex_lock (&priv->lock);
2182 priv->use_client_settings = use_client_settings;
2183 g_mutex_unlock (&priv->lock);
2187 * gst_rtsp_client_get_use_client_settings:
2188 * @client: a #GstRTSPClient
2190 * Check if client transport settings (destination and ttl) for multicast
2194 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
2196 GstRTSPClientPrivate *priv;
2199 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2201 priv = client->priv;
2203 g_mutex_lock (&priv->lock);
2204 res = priv->use_client_settings;
2205 g_mutex_unlock (&priv->lock);
2211 * gst_rtsp_client_set_auth:
2212 * @client: a #GstRTSPClient
2213 * @auth: a #GstRTSPAuth
2215 * configure @auth to be used as the authentication manager of @client.
2218 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2220 GstRTSPClientPrivate *priv;
2223 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2225 priv = client->priv;
2228 g_object_ref (auth);
2230 g_mutex_lock (&priv->lock);
2233 g_mutex_unlock (&priv->lock);
2236 g_object_unref (old);
2241 * gst_rtsp_client_get_auth:
2242 * @client: a #GstRTSPClient
2244 * Get the #GstRTSPAuth used as the authentication manager of @client.
2246 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2250 gst_rtsp_client_get_auth (GstRTSPClient * client)
2252 GstRTSPClientPrivate *priv;
2253 GstRTSPAuth *result;
2255 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2257 priv = client->priv;
2259 g_mutex_lock (&priv->lock);
2260 if ((result = priv->auth))
2261 g_object_ref (result);
2262 g_mutex_unlock (&priv->lock);
2268 * gst_rtsp_client_set_thread_pool:
2269 * @client: a #GstRTSPClient
2270 * @pool: a #GstRTSPThreadPool
2272 * configure @pool to be used as the thread pool of @client.
2275 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2276 GstRTSPThreadPool * pool)
2278 GstRTSPClientPrivate *priv;
2279 GstRTSPThreadPool *old;
2281 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2283 priv = client->priv;
2286 g_object_ref (pool);
2288 g_mutex_lock (&priv->lock);
2289 old = priv->thread_pool;
2290 priv->thread_pool = pool;
2291 g_mutex_unlock (&priv->lock);
2294 g_object_unref (old);
2298 * gst_rtsp_client_get_thread_pool:
2299 * @client: a #GstRTSPClient
2301 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2303 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2307 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2309 GstRTSPClientPrivate *priv;
2310 GstRTSPThreadPool *result;
2312 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2314 priv = client->priv;
2316 g_mutex_lock (&priv->lock);
2317 if ((result = priv->thread_pool))
2318 g_object_ref (result);
2319 g_mutex_unlock (&priv->lock);
2325 * gst_rtsp_client_set_connection:
2326 * @client: a #GstRTSPClient
2327 * @conn: (transfer full): a #GstRTSPConnection
2329 * Set the #GstRTSPConnection of @client. This function takes ownership of
2332 * Returns: %TRUE on success.
2335 gst_rtsp_client_set_connection (GstRTSPClient * client,
2336 GstRTSPConnection * conn)
2338 GstRTSPClientPrivate *priv;
2339 GSocket *read_socket;
2340 GSocketAddress *address;
2342 GError *error = NULL;
2344 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2345 g_return_val_if_fail (conn != NULL, FALSE);
2347 priv = client->priv;
2349 read_socket = gst_rtsp_connection_get_read_socket (conn);
2351 if (!(address = g_socket_get_local_address (read_socket, &error)))
2354 g_free (priv->server_ip);
2355 /* keep the original ip that the client connected to */
2356 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2357 GInetAddress *iaddr;
2359 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2361 /* socket might be ipv6 but adress still ipv4 */
2362 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2363 priv->server_ip = g_inet_address_to_string (iaddr);
2364 g_object_unref (address);
2366 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2367 priv->server_ip = g_strdup ("unknown");
2370 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2371 priv->server_ip, priv->is_ipv6);
2373 url = gst_rtsp_connection_get_url (conn);
2374 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2376 priv->connection = conn;
2383 GST_ERROR ("could not get local address %s", error->message);
2384 g_error_free (error);
2390 * gst_rtsp_client_get_connection:
2391 * @client: a #GstRTSPClient
2393 * Get the #GstRTSPConnection of @client.
2395 * Returns: (transfer none): the #GstRTSPConnection of @client.
2396 * The connection object returned remains valid until the client is freed.
2399 gst_rtsp_client_get_connection (GstRTSPClient * client)
2401 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2403 return client->priv->connection;
2407 * gst_rtsp_client_set_send_func:
2408 * @client: a #GstRTSPClient
2409 * @func: a #GstRTSPClientSendFunc
2410 * @user_data: user data passed to @func
2411 * @notify: called when @user_data is no longer in use
2413 * Set @func as the callback that will be called when a new message needs to be
2414 * sent to the client. @user_data is passed to @func and @notify is called when
2415 * @user_data is no longer in use.
2417 * By default, the client will send the messages on the #GstRTSPConnection that
2418 * was configured with gst_rtsp_client_attach() was called.
2421 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2422 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2424 GstRTSPClientPrivate *priv;
2425 GDestroyNotify old_notify;
2428 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2430 priv = client->priv;
2432 g_mutex_lock (&priv->send_lock);
2433 priv->send_func = func;
2434 old_notify = priv->send_notify;
2435 old_data = priv->send_data;
2436 priv->send_notify = notify;
2437 priv->send_data = user_data;
2438 g_mutex_unlock (&priv->send_lock);
2441 old_notify (old_data);
2445 * gst_rtsp_client_handle_message:
2446 * @client: a #GstRTSPClient
2447 * @message: an #GstRTSPMessage
2449 * Let the client handle @message.
2451 * Returns: a #GstRTSPResult.
2454 gst_rtsp_client_handle_message (GstRTSPClient * client,
2455 GstRTSPMessage * message)
2457 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2458 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2460 switch (message->type) {
2461 case GST_RTSP_MESSAGE_REQUEST:
2462 handle_request (client, message);
2464 case GST_RTSP_MESSAGE_RESPONSE:
2466 case GST_RTSP_MESSAGE_DATA:
2467 handle_data (client, message);
2476 * gst_rtsp_client_send_request:
2477 * @client: a #GstRTSPClient
2478 * @session: a #GstRTSPSession to send the request to or %NULL
2479 * @request: The request #GstRTSPMessage to send
2481 * Send a request message to the remote end. @request must be a
2482 * #GST_RTSP_MESSAGE_REQUEST.
2485 gst_rtsp_client_send_request (GstRTSPClient * client, GstRTSPSession * session,
2486 GstRTSPMessage * request)
2488 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2489 g_return_val_if_fail (request != NULL, GST_RTSP_EINVAL);
2490 g_return_val_if_fail (request->type == GST_RTSP_MESSAGE_REQUEST,
2493 send_message (client, session, request, FALSE);
2498 static GstRTSPResult
2499 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2500 gboolean close, gpointer user_data)
2502 GstRTSPClientPrivate *priv = client->priv;
2504 /* send the response and store the seq number so we can wait until it's
2505 * written to the client to close the connection */
2506 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2507 &priv->close_seq : NULL);
2510 static GstRTSPResult
2511 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2514 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2517 static GstRTSPResult
2518 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2520 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2521 GstRTSPClientPrivate *priv = client->priv;
2523 if (priv->close_seq && priv->close_seq == cseq) {
2524 priv->close_seq = 0;
2525 close_connection (client);
2531 static GstRTSPResult
2532 closed (GstRTSPWatch * watch, gpointer user_data)
2534 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2535 GstRTSPClientPrivate *priv = client->priv;
2536 const gchar *tunnelid;
2538 GST_INFO ("client %p: connection closed", client);
2540 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2541 g_mutex_lock (&tunnels_lock);
2542 /* remove from tunnelids */
2543 g_hash_table_remove (tunnels, tunnelid);
2544 g_mutex_unlock (&tunnels_lock);
2547 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2552 static GstRTSPResult
2553 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2555 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2558 str = gst_rtsp_strresult (result);
2559 GST_INFO ("client %p: received an error %s", client, str);
2565 static GstRTSPResult
2566 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2567 GstRTSPMessage * message, guint id, gpointer user_data)
2569 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2572 str = gst_rtsp_strresult (result);
2574 ("client %p: error when handling message %p with id %d: %s",
2575 client, message, id, str);
2582 remember_tunnel (GstRTSPClient * client)
2584 GstRTSPClientPrivate *priv = client->priv;
2585 const gchar *tunnelid;
2587 /* store client in the pending tunnels */
2588 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2589 if (tunnelid == NULL)
2592 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2594 /* we can't have two clients connecting with the same tunnelid */
2595 g_mutex_lock (&tunnels_lock);
2596 if (g_hash_table_lookup (tunnels, tunnelid))
2597 goto tunnel_existed;
2599 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2600 g_mutex_unlock (&tunnels_lock);
2607 GST_ERROR ("client %p: no tunnelid provided", client);
2612 g_mutex_unlock (&tunnels_lock);
2613 GST_ERROR ("client %p: tunnel session %s already existed", client,
2619 static GstRTSPStatusCode
2620 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2622 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2623 GstRTSPClientPrivate *priv = client->priv;
2625 GST_INFO ("client %p: tunnel start (connection %p)", client,
2628 if (!remember_tunnel (client))
2631 return GST_RTSP_STS_OK;
2636 GST_ERROR ("client %p: error starting tunnel", client);
2637 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2641 static GstRTSPResult
2642 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2644 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2645 GstRTSPClientPrivate *priv = client->priv;
2647 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2650 /* ignore error, it'll only be a problem when the client does a POST again */
2651 remember_tunnel (client);
2656 static GstRTSPResult
2657 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2659 const gchar *tunnelid;
2660 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2661 GstRTSPClientPrivate *priv = client->priv;
2662 GstRTSPClient *oclient;
2663 GstRTSPClientPrivate *opriv;
2665 GST_INFO ("client %p: tunnel complete", client);
2667 /* find previous tunnel */
2668 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2669 if (tunnelid == NULL)
2672 g_mutex_lock (&tunnels_lock);
2673 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2676 /* remove the old client from the table. ref before because removing it will
2677 * remove the ref to it. */
2678 g_object_ref (oclient);
2679 g_hash_table_remove (tunnels, tunnelid);
2681 opriv = oclient->priv;
2683 if (opriv->watch == NULL)
2685 g_mutex_unlock (&tunnels_lock);
2687 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2688 opriv->connection, priv->connection);
2690 /* merge the tunnels into the first client */
2691 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2692 gst_rtsp_watch_reset (opriv->watch);
2693 g_object_unref (oclient);
2700 GST_ERROR ("client %p: no tunnelid provided", client);
2701 return GST_RTSP_ERROR;
2705 g_mutex_unlock (&tunnels_lock);
2706 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2707 return GST_RTSP_ERROR;
2711 g_mutex_unlock (&tunnels_lock);
2712 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2713 g_object_unref (oclient);
2714 return GST_RTSP_ERROR;
2718 static GstRTSPWatchFuncs watch_funcs = {
2730 client_watch_notify (GstRTSPClient * client)
2732 GstRTSPClientPrivate *priv = client->priv;
2734 GST_INFO ("client %p: watch destroyed", client);
2736 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2737 g_object_unref (client);
2741 * gst_rtsp_client_attach:
2742 * @client: a #GstRTSPClient
2743 * @context: (allow-none): a #GMainContext
2745 * Attaches @client to @context. When the mainloop for @context is run, the
2746 * client will be dispatched. When @context is NULL, the default context will be
2749 * This function should be called when the client properties and urls are fully
2750 * configured and the client is ready to start.
2752 * Returns: the ID (greater than 0) for the source within the GMainContext.
2755 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2757 GstRTSPClientPrivate *priv;
2760 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2761 priv = client->priv;
2762 g_return_val_if_fail (priv->connection != NULL, 0);
2763 g_return_val_if_fail (priv->watch == NULL, 0);
2765 /* create watch for the connection and attach */
2766 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2767 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2768 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2769 (GDestroyNotify) gst_rtsp_watch_unref);
2771 /* FIXME make this configurable. We don't want to do this yet because it will
2772 * be superceeded by a cache object later */
2773 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2775 GST_INFO ("attaching to context %p", context);
2776 res = gst_rtsp_watch_attach (priv->watch, context);
2782 * gst_rtsp_client_session_filter:
2783 * @client: a #GstRTSPclient
2784 * @func: (scope call): a callback
2785 * @user_data: user data passed to @func
2787 * Call @func for each session managed by @client. The result value of @func
2788 * determines what happens to the session. @func will be called with @client
2789 * locked so no further actions on @client can be performed from @func.
2791 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
2794 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
2796 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
2797 * will also be added with an additional ref to the result #GList of this
2800 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
2801 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2802 * element in the #GList should be unreffed before the list is freed.
2805 gst_rtsp_client_session_filter (GstRTSPClient * client,
2806 GstRTSPClientSessionFilterFunc func, gpointer user_data)
2808 GstRTSPClientPrivate *priv;
2809 GList *result, *walk, *next;
2811 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2812 g_return_val_if_fail (func != NULL, NULL);
2814 priv = client->priv;
2818 g_mutex_lock (&priv->lock);
2819 for (walk = priv->sessions; walk; walk = next) {
2820 GstRTSPSession *sess = walk->data;
2822 next = g_list_next (walk);
2824 switch (func (client, sess, user_data)) {
2825 case GST_RTSP_FILTER_REMOVE:
2826 /* stop watching the session and pretent it went away */
2827 client_cleanup_session (client, sess);
2829 case GST_RTSP_FILTER_REF:
2830 result = g_list_prepend (result, g_object_ref (sess));
2832 case GST_RTSP_FILTER_KEEP:
2837 g_mutex_unlock (&priv->lock);