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.conn = priv->connection;
1863 state.client = client;
1864 state.request = request;
1865 state.response = &response;
1866 state.auth = priv->auth;
1867 g_private_set (&state_key, &state);
1869 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1870 gst_rtsp_message_dump (request);
1873 GST_INFO ("client %p: received a request", client);
1875 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1877 /* we can only handle 1.0 requests */
1878 if (version != GST_RTSP_VERSION_1_0)
1881 state.method = method;
1883 /* we always try to parse the url first */
1884 if (strcmp (uristr, "*") == 0) {
1885 /* special case where we have * as uri, keep uri = NULL */
1886 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1889 /* get the session if there is any */
1890 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1891 if (res == GST_RTSP_OK) {
1892 if (priv->session_pool == NULL)
1895 /* we had a session in the request, find it again */
1896 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1897 goto session_not_found;
1899 /* we add the session to the client list of watched sessions. When a session
1900 * disappears because it times out, we will be notified. If all sessions are
1901 * gone, we will close the connection */
1902 client_watch_session (client, session);
1905 /* sanitize the uri */
1909 state.session = session;
1911 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
1912 goto not_authorized;
1914 /* now see what is asked and dispatch to a dedicated handler */
1916 case GST_RTSP_OPTIONS:
1917 handle_options_request (client, &state);
1919 case GST_RTSP_DESCRIBE:
1920 handle_describe_request (client, &state);
1922 case GST_RTSP_SETUP:
1923 handle_setup_request (client, &state);
1926 handle_play_request (client, &state);
1928 case GST_RTSP_PAUSE:
1929 handle_pause_request (client, &state);
1931 case GST_RTSP_TEARDOWN:
1932 handle_teardown_request (client, &state);
1934 case GST_RTSP_SET_PARAMETER:
1935 handle_set_param_request (client, &state);
1937 case GST_RTSP_GET_PARAMETER:
1938 handle_get_param_request (client, &state);
1940 case GST_RTSP_ANNOUNCE:
1941 case GST_RTSP_RECORD:
1942 case GST_RTSP_REDIRECT:
1943 goto not_implemented;
1944 case GST_RTSP_INVALID:
1950 g_private_set (&state_key, NULL);
1952 g_object_unref (session);
1954 gst_rtsp_url_free (uri);
1960 GST_ERROR ("client %p: version %d not supported", client, version);
1961 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1967 GST_ERROR ("client %p: bad request", client);
1968 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1973 GST_ERROR ("client %p: no pool configured", client);
1974 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1979 GST_ERROR ("client %p: session not found", client);
1980 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1985 GST_ERROR ("client %p: not allowed", client);
1986 handle_unauthorized_request (client, priv->auth, &state);
1991 GST_ERROR ("client %p: method %d not implemented", client, method);
1992 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1998 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2000 GstRTSPClientPrivate *priv = client->priv;
2009 /* find the stream for this message */
2010 res = gst_rtsp_message_parse_data (message, &channel);
2011 if (res != GST_RTSP_OK)
2014 gst_rtsp_message_steal_body (message, &data, &size);
2016 buffer = gst_buffer_new_wrapped (data, size);
2019 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2020 GstRTSPStreamTransport *trans;
2021 GstRTSPStream *stream;
2022 const GstRTSPTransport *tr;
2026 tr = gst_rtsp_stream_transport_get_transport (trans);
2027 stream = gst_rtsp_stream_transport_get_stream (trans);
2029 /* check for TCP transport */
2030 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2031 /* dispatch to the stream based on the channel number */
2032 if (tr->interleaved.min == channel) {
2033 gst_rtsp_stream_recv_rtp (stream, buffer);
2036 } else if (tr->interleaved.max == channel) {
2037 gst_rtsp_stream_recv_rtcp (stream, buffer);
2044 gst_buffer_unref (buffer);
2048 * gst_rtsp_client_set_session_pool:
2049 * @client: a #GstRTSPClient
2050 * @pool: a #GstRTSPSessionPool
2052 * Set @pool as the sessionpool for @client which it will use to find
2053 * or allocate sessions. the sessionpool is usually inherited from the server
2054 * that created the client but can be overridden later.
2057 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2058 GstRTSPSessionPool * pool)
2060 GstRTSPSessionPool *old;
2061 GstRTSPClientPrivate *priv;
2063 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2065 priv = client->priv;
2068 g_object_ref (pool);
2070 g_mutex_lock (&priv->lock);
2071 old = priv->session_pool;
2072 priv->session_pool = pool;
2073 g_mutex_unlock (&priv->lock);
2076 g_object_unref (old);
2080 * gst_rtsp_client_get_session_pool:
2081 * @client: a #GstRTSPClient
2083 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2085 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2087 GstRTSPSessionPool *
2088 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2090 GstRTSPClientPrivate *priv;
2091 GstRTSPSessionPool *result;
2093 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2095 priv = client->priv;
2097 g_mutex_lock (&priv->lock);
2098 if ((result = priv->session_pool))
2099 g_object_ref (result);
2100 g_mutex_unlock (&priv->lock);
2106 * gst_rtsp_client_set_mount_points:
2107 * @client: a #GstRTSPClient
2108 * @mounts: a #GstRTSPMountPoints
2110 * Set @mounts as the mount points for @client which it will use to map urls
2111 * to media streams. These mount points are usually inherited from the server that
2112 * created the client but can be overriden later.
2115 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2116 GstRTSPMountPoints * mounts)
2118 GstRTSPClientPrivate *priv;
2119 GstRTSPMountPoints *old;
2121 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2123 priv = client->priv;
2126 g_object_ref (mounts);
2128 g_mutex_lock (&priv->lock);
2129 old = priv->mount_points;
2130 priv->mount_points = mounts;
2131 g_mutex_unlock (&priv->lock);
2134 g_object_unref (old);
2138 * gst_rtsp_client_get_mount_points:
2139 * @client: a #GstRTSPClient
2141 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2143 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2145 GstRTSPMountPoints *
2146 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2148 GstRTSPClientPrivate *priv;
2149 GstRTSPMountPoints *result;
2151 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2153 priv = client->priv;
2155 g_mutex_lock (&priv->lock);
2156 if ((result = priv->mount_points))
2157 g_object_ref (result);
2158 g_mutex_unlock (&priv->lock);
2164 * gst_rtsp_client_set_use_client_settings:
2165 * @client: a #GstRTSPClient
2166 * @use_client_settings: whether to use client settings for multicast
2168 * Use client transport settings (destination and ttl) for multicast.
2169 * When @use_client_settings is %FALSE, the server settings will be
2173 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
2174 gboolean use_client_settings)
2176 GstRTSPClientPrivate *priv;
2178 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2180 priv = client->priv;
2182 g_mutex_lock (&priv->lock);
2183 priv->use_client_settings = use_client_settings;
2184 g_mutex_unlock (&priv->lock);
2188 * gst_rtsp_client_get_use_client_settings:
2189 * @client: a #GstRTSPClient
2191 * Check if client transport settings (destination and ttl) for multicast
2195 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
2197 GstRTSPClientPrivate *priv;
2200 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2202 priv = client->priv;
2204 g_mutex_lock (&priv->lock);
2205 res = priv->use_client_settings;
2206 g_mutex_unlock (&priv->lock);
2212 * gst_rtsp_client_set_auth:
2213 * @client: a #GstRTSPClient
2214 * @auth: a #GstRTSPAuth
2216 * configure @auth to be used as the authentication manager of @client.
2219 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2221 GstRTSPClientPrivate *priv;
2224 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2226 priv = client->priv;
2229 g_object_ref (auth);
2231 g_mutex_lock (&priv->lock);
2234 g_mutex_unlock (&priv->lock);
2237 g_object_unref (old);
2242 * gst_rtsp_client_get_auth:
2243 * @client: a #GstRTSPClient
2245 * Get the #GstRTSPAuth used as the authentication manager of @client.
2247 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2251 gst_rtsp_client_get_auth (GstRTSPClient * client)
2253 GstRTSPClientPrivate *priv;
2254 GstRTSPAuth *result;
2256 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2258 priv = client->priv;
2260 g_mutex_lock (&priv->lock);
2261 if ((result = priv->auth))
2262 g_object_ref (result);
2263 g_mutex_unlock (&priv->lock);
2269 * gst_rtsp_client_set_thread_pool:
2270 * @client: a #GstRTSPClient
2271 * @pool: a #GstRTSPThreadPool
2273 * configure @pool to be used as the thread pool of @client.
2276 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2277 GstRTSPThreadPool * pool)
2279 GstRTSPClientPrivate *priv;
2280 GstRTSPThreadPool *old;
2282 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2284 priv = client->priv;
2287 g_object_ref (pool);
2289 g_mutex_lock (&priv->lock);
2290 old = priv->thread_pool;
2291 priv->thread_pool = pool;
2292 g_mutex_unlock (&priv->lock);
2295 g_object_unref (old);
2299 * gst_rtsp_client_get_thread_pool:
2300 * @client: a #GstRTSPClient
2302 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2304 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2308 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2310 GstRTSPClientPrivate *priv;
2311 GstRTSPThreadPool *result;
2313 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2315 priv = client->priv;
2317 g_mutex_lock (&priv->lock);
2318 if ((result = priv->thread_pool))
2319 g_object_ref (result);
2320 g_mutex_unlock (&priv->lock);
2326 * gst_rtsp_client_set_connection:
2327 * @client: a #GstRTSPClient
2328 * @conn: (transfer full): a #GstRTSPConnection
2330 * Set the #GstRTSPConnection of @client. This function takes ownership of
2333 * Returns: %TRUE on success.
2336 gst_rtsp_client_set_connection (GstRTSPClient * client,
2337 GstRTSPConnection * conn)
2339 GstRTSPClientPrivate *priv;
2340 GSocket *read_socket;
2341 GSocketAddress *address;
2343 GError *error = NULL;
2345 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2346 g_return_val_if_fail (conn != NULL, FALSE);
2348 priv = client->priv;
2350 read_socket = gst_rtsp_connection_get_read_socket (conn);
2352 if (!(address = g_socket_get_local_address (read_socket, &error)))
2355 g_free (priv->server_ip);
2356 /* keep the original ip that the client connected to */
2357 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2358 GInetAddress *iaddr;
2360 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2362 /* socket might be ipv6 but adress still ipv4 */
2363 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2364 priv->server_ip = g_inet_address_to_string (iaddr);
2365 g_object_unref (address);
2367 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2368 priv->server_ip = g_strdup ("unknown");
2371 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2372 priv->server_ip, priv->is_ipv6);
2374 url = gst_rtsp_connection_get_url (conn);
2375 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2377 priv->connection = conn;
2384 GST_ERROR ("could not get local address %s", error->message);
2385 g_error_free (error);
2391 * gst_rtsp_client_get_connection:
2392 * @client: a #GstRTSPClient
2394 * Get the #GstRTSPConnection of @client.
2396 * Returns: (transfer none): the #GstRTSPConnection of @client.
2397 * The connection object returned remains valid until the client is freed.
2400 gst_rtsp_client_get_connection (GstRTSPClient * client)
2402 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2404 return client->priv->connection;
2408 * gst_rtsp_client_set_send_func:
2409 * @client: a #GstRTSPClient
2410 * @func: a #GstRTSPClientSendFunc
2411 * @user_data: user data passed to @func
2412 * @notify: called when @user_data is no longer in use
2414 * Set @func as the callback that will be called when a new message needs to be
2415 * sent to the client. @user_data is passed to @func and @notify is called when
2416 * @user_data is no longer in use.
2418 * By default, the client will send the messages on the #GstRTSPConnection that
2419 * was configured with gst_rtsp_client_attach() was called.
2422 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2423 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2425 GstRTSPClientPrivate *priv;
2426 GDestroyNotify old_notify;
2429 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2431 priv = client->priv;
2433 g_mutex_lock (&priv->send_lock);
2434 priv->send_func = func;
2435 old_notify = priv->send_notify;
2436 old_data = priv->send_data;
2437 priv->send_notify = notify;
2438 priv->send_data = user_data;
2439 g_mutex_unlock (&priv->send_lock);
2442 old_notify (old_data);
2446 * gst_rtsp_client_handle_message:
2447 * @client: a #GstRTSPClient
2448 * @message: an #GstRTSPMessage
2450 * Let the client handle @message.
2452 * Returns: a #GstRTSPResult.
2455 gst_rtsp_client_handle_message (GstRTSPClient * client,
2456 GstRTSPMessage * message)
2458 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2459 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2461 switch (message->type) {
2462 case GST_RTSP_MESSAGE_REQUEST:
2463 handle_request (client, message);
2465 case GST_RTSP_MESSAGE_RESPONSE:
2467 case GST_RTSP_MESSAGE_DATA:
2468 handle_data (client, message);
2477 * gst_rtsp_client_send_request:
2478 * @client: a #GstRTSPClient
2479 * @session: a #GstRTSPSession to send the request to or %NULL
2480 * @request: The request #GstRTSPMessage to send
2482 * Send a request message to the remote end. @request must be a
2483 * #GST_RTSP_MESSAGE_REQUEST.
2486 gst_rtsp_client_send_request (GstRTSPClient * client, GstRTSPSession * session,
2487 GstRTSPMessage * request)
2489 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2490 g_return_val_if_fail (request != NULL, GST_RTSP_EINVAL);
2491 g_return_val_if_fail (request->type == GST_RTSP_MESSAGE_REQUEST,
2494 send_message (client, session, request, FALSE);
2499 static GstRTSPResult
2500 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2501 gboolean close, gpointer user_data)
2503 GstRTSPClientPrivate *priv = client->priv;
2505 /* send the response and store the seq number so we can wait until it's
2506 * written to the client to close the connection */
2507 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2508 &priv->close_seq : NULL);
2511 static GstRTSPResult
2512 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2515 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2518 static GstRTSPResult
2519 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2521 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2522 GstRTSPClientPrivate *priv = client->priv;
2524 if (priv->close_seq && priv->close_seq == cseq) {
2525 priv->close_seq = 0;
2526 close_connection (client);
2532 static GstRTSPResult
2533 closed (GstRTSPWatch * watch, gpointer user_data)
2535 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2536 GstRTSPClientPrivate *priv = client->priv;
2537 const gchar *tunnelid;
2539 GST_INFO ("client %p: connection closed", client);
2541 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2542 g_mutex_lock (&tunnels_lock);
2543 /* remove from tunnelids */
2544 g_hash_table_remove (tunnels, tunnelid);
2545 g_mutex_unlock (&tunnels_lock);
2548 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2553 static GstRTSPResult
2554 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2556 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2559 str = gst_rtsp_strresult (result);
2560 GST_INFO ("client %p: received an error %s", client, str);
2566 static GstRTSPResult
2567 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2568 GstRTSPMessage * message, guint id, gpointer user_data)
2570 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2573 str = gst_rtsp_strresult (result);
2575 ("client %p: error when handling message %p with id %d: %s",
2576 client, message, id, str);
2583 remember_tunnel (GstRTSPClient * client)
2585 GstRTSPClientPrivate *priv = client->priv;
2586 const gchar *tunnelid;
2588 /* store client in the pending tunnels */
2589 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2590 if (tunnelid == NULL)
2593 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2595 /* we can't have two clients connecting with the same tunnelid */
2596 g_mutex_lock (&tunnels_lock);
2597 if (g_hash_table_lookup (tunnels, tunnelid))
2598 goto tunnel_existed;
2600 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2601 g_mutex_unlock (&tunnels_lock);
2608 GST_ERROR ("client %p: no tunnelid provided", client);
2613 g_mutex_unlock (&tunnels_lock);
2614 GST_ERROR ("client %p: tunnel session %s already existed", client,
2620 static GstRTSPStatusCode
2621 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2623 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2624 GstRTSPClientPrivate *priv = client->priv;
2626 GST_INFO ("client %p: tunnel start (connection %p)", client,
2629 if (!remember_tunnel (client))
2632 return GST_RTSP_STS_OK;
2637 GST_ERROR ("client %p: error starting tunnel", client);
2638 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2642 static GstRTSPResult
2643 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2645 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2646 GstRTSPClientPrivate *priv = client->priv;
2648 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2651 /* ignore error, it'll only be a problem when the client does a POST again */
2652 remember_tunnel (client);
2657 static GstRTSPResult
2658 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2660 const gchar *tunnelid;
2661 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2662 GstRTSPClientPrivate *priv = client->priv;
2663 GstRTSPClient *oclient;
2664 GstRTSPClientPrivate *opriv;
2666 GST_INFO ("client %p: tunnel complete", client);
2668 /* find previous tunnel */
2669 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2670 if (tunnelid == NULL)
2673 g_mutex_lock (&tunnels_lock);
2674 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2677 /* remove the old client from the table. ref before because removing it will
2678 * remove the ref to it. */
2679 g_object_ref (oclient);
2680 g_hash_table_remove (tunnels, tunnelid);
2682 opriv = oclient->priv;
2684 if (opriv->watch == NULL)
2686 g_mutex_unlock (&tunnels_lock);
2688 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2689 opriv->connection, priv->connection);
2691 /* merge the tunnels into the first client */
2692 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2693 gst_rtsp_watch_reset (opriv->watch);
2694 g_object_unref (oclient);
2701 GST_ERROR ("client %p: no tunnelid provided", client);
2702 return GST_RTSP_ERROR;
2706 g_mutex_unlock (&tunnels_lock);
2707 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2708 return GST_RTSP_ERROR;
2712 g_mutex_unlock (&tunnels_lock);
2713 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2714 g_object_unref (oclient);
2715 return GST_RTSP_ERROR;
2719 static GstRTSPWatchFuncs watch_funcs = {
2731 client_watch_notify (GstRTSPClient * client)
2733 GstRTSPClientPrivate *priv = client->priv;
2735 GST_INFO ("client %p: watch destroyed", client);
2737 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2738 g_object_unref (client);
2742 * gst_rtsp_client_attach:
2743 * @client: a #GstRTSPClient
2744 * @context: (allow-none): a #GMainContext
2746 * Attaches @client to @context. When the mainloop for @context is run, the
2747 * client will be dispatched. When @context is NULL, the default context will be
2750 * This function should be called when the client properties and urls are fully
2751 * configured and the client is ready to start.
2753 * Returns: the ID (greater than 0) for the source within the GMainContext.
2756 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2758 GstRTSPClientPrivate *priv;
2761 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2762 priv = client->priv;
2763 g_return_val_if_fail (priv->connection != NULL, 0);
2764 g_return_val_if_fail (priv->watch == NULL, 0);
2766 /* create watch for the connection and attach */
2767 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2768 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2769 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2770 (GDestroyNotify) gst_rtsp_watch_unref);
2772 /* FIXME make this configurable. We don't want to do this yet because it will
2773 * be superceeded by a cache object later */
2774 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2776 GST_INFO ("attaching to context %p", context);
2777 res = gst_rtsp_watch_attach (priv->watch, context);
2783 * gst_rtsp_client_session_filter:
2784 * @client: a #GstRTSPclient
2785 * @func: (scope call): a callback
2786 * @user_data: user data passed to @func
2788 * Call @func for each session managed by @client. The result value of @func
2789 * determines what happens to the session. @func will be called with @client
2790 * locked so no further actions on @client can be performed from @func.
2792 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
2795 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
2797 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
2798 * will also be added with an additional ref to the result #GList of this
2801 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
2802 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2803 * element in the #GList should be unreffed before the list is freed.
2806 gst_rtsp_client_session_filter (GstRTSPClient * client,
2807 GstRTSPClientSessionFilterFunc func, gpointer user_data)
2809 GstRTSPClientPrivate *priv;
2810 GList *result, *walk, *next;
2812 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2813 g_return_val_if_fail (func != NULL, NULL);
2815 priv = client->priv;
2819 g_mutex_lock (&priv->lock);
2820 for (walk = priv->sessions; walk; walk = next) {
2821 GstRTSPSession *sess = walk->data;
2823 next = g_list_next (walk);
2825 switch (func (client, sess, user_data)) {
2826 case GST_RTSP_FILTER_REMOVE:
2827 /* stop watching the session and pretent it went away */
2828 client_cleanup_session (client, sess);
2830 case GST_RTSP_FILTER_REF:
2831 result = g_list_prepend (result, g_object_ref (sess));
2833 case GST_RTSP_FILTER_KEEP:
2838 g_mutex_unlock (&priv->lock);