2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * @short_description: A client connection state
22 * @see_also: #GstRTSPServer, #GstRTSPThreadPool
24 * The client object handles the connection with a client for as long as a TCP
27 * A #GstRTSPClient is created by #GstRTSPServer when a new connection is
28 * accepted and it inherits the #GstRTSPMountPoints, #GstRTSPSessionPool,
29 * #GstRTSPAuth and #GstRTSPThreadPool from the server.
31 * The client connection should be configured with the #GstRTSPConnection using
32 * gst_rtsp_client_set_connection() before it can be attached to a #GMainContext
33 * using gst_rtsp_client_attach(). From then on the client will handle requests
36 * Use gst_rtsp_client_session_filter() to iterate or modify all the
37 * #GstRTSPSession objects managed by the client object.
39 * Last reviewed on 2013-07-11 (1.0.0)
45 #include "rtsp-client.h"
47 #include "rtsp-params.h"
49 #define GST_RTSP_CLIENT_GET_PRIVATE(obj) \
50 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_CLIENT, GstRTSPClientPrivate))
53 * send_lock, lock, tunnels_lock
56 struct _GstRTSPClientPrivate
58 GMutex lock; /* protects everything else */
60 GstRTSPConnection *connection;
66 GstRTSPClientSendFunc send_func; /* protected by send_lock */
67 gpointer send_data; /* protected by send_lock */
68 GDestroyNotify send_notify; /* protected by send_lock */
70 GstRTSPSessionPool *session_pool;
71 GstRTSPMountPoints *mount_points;
73 GstRTSPThreadPool *thread_pool;
75 /* used to cache the media in the last requested DESCRIBE so that
76 * we can pick it up in the next SETUP immediately */
84 static GMutex tunnels_lock;
85 static GHashTable *tunnels; /* protected by tunnels_lock */
87 #define DEFAULT_SESSION_POOL NULL
88 #define DEFAULT_MOUNT_POINTS NULL
102 SIGNAL_OPTIONS_REQUEST,
103 SIGNAL_DESCRIBE_REQUEST,
104 SIGNAL_SETUP_REQUEST,
106 SIGNAL_PAUSE_REQUEST,
107 SIGNAL_TEARDOWN_REQUEST,
108 SIGNAL_SET_PARAMETER_REQUEST,
109 SIGNAL_GET_PARAMETER_REQUEST,
113 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
114 #define GST_CAT_DEFAULT rtsp_client_debug
116 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
118 static void gst_rtsp_client_get_property (GObject * object, guint propid,
119 GValue * value, GParamSpec * pspec);
120 static void gst_rtsp_client_set_property (GObject * object, guint propid,
121 const GValue * value, GParamSpec * pspec);
122 static void gst_rtsp_client_finalize (GObject * obj);
124 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
125 static void client_session_finalized (GstRTSPClient * client,
126 GstRTSPSession * session);
127 static void unlink_session_transports (GstRTSPClient * client,
128 GstRTSPSession * session, GstRTSPSessionMedia * sessmedia);
129 static gboolean default_configure_client_transport (GstRTSPClient * client,
130 GstRTSPClientState * state, GstRTSPTransport * ct);
131 static GstRTSPResult default_params_set (GstRTSPClient * client,
132 GstRTSPClientState * state);
133 static GstRTSPResult default_params_get (GstRTSPClient * client,
134 GstRTSPClientState * state);
136 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
139 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
141 GObjectClass *gobject_class;
143 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
145 gobject_class = G_OBJECT_CLASS (klass);
147 gobject_class->get_property = gst_rtsp_client_get_property;
148 gobject_class->set_property = gst_rtsp_client_set_property;
149 gobject_class->finalize = gst_rtsp_client_finalize;
151 klass->create_sdp = create_sdp;
152 klass->configure_client_transport = default_configure_client_transport;
153 klass->params_set = default_params_set;
154 klass->params_get = default_params_get;
156 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
157 g_param_spec_object ("session-pool", "Session Pool",
158 "The session pool to use for client session",
159 GST_TYPE_RTSP_SESSION_POOL,
160 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
162 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
163 g_param_spec_object ("mount-points", "Mount Points",
164 "The mount points to use for client session",
165 GST_TYPE_RTSP_MOUNT_POINTS,
166 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
168 gst_rtsp_client_signals[SIGNAL_CLOSED] =
169 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
170 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
171 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
173 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
174 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
175 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
176 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
178 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
179 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
180 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
181 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
184 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
185 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
186 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
187 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
190 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
191 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
192 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
193 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
196 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
197 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
198 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
199 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
202 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
203 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
204 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
205 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
208 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
209 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
210 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
211 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
214 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
215 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
216 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
217 set_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
218 G_TYPE_NONE, 1, G_TYPE_POINTER);
220 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
221 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
222 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
223 get_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
224 G_TYPE_NONE, 1, G_TYPE_POINTER);
227 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
228 g_mutex_init (&tunnels_lock);
230 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
234 gst_rtsp_client_init (GstRTSPClient * client)
236 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
240 g_mutex_init (&priv->lock);
241 g_mutex_init (&priv->send_lock);
245 static GstRTSPFilterResult
246 filter_session (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
249 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
251 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
252 unlink_session_transports (client, sess, sessmedia);
254 /* unmanage the media in the session */
255 return GST_RTSP_FILTER_REMOVE;
259 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
261 /* unlink all media managed in this session */
262 gst_rtsp_session_filter (session, filter_session, client);
266 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
268 GstRTSPClientPrivate *priv = client->priv;
271 for (walk = priv->sessions; walk; walk = g_list_next (walk)) {
272 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
274 /* we already know about this session */
275 if (msession == session)
279 GST_INFO ("watching session %p", session);
281 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
283 priv->sessions = g_list_prepend (priv->sessions, session);
287 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session)
289 GstRTSPClientPrivate *priv = client->priv;
291 GST_INFO ("unwatching session %p", session);
293 g_object_weak_unref (G_OBJECT (session),
294 (GWeakNotify) client_session_finalized, client);
295 priv->sessions = g_list_remove (priv->sessions, session);
299 client_cleanup_session (GstRTSPClient * client, GstRTSPSession * session)
301 g_object_weak_unref (G_OBJECT (session),
302 (GWeakNotify) client_session_finalized, client);
303 client_unlink_session (client, session);
307 client_cleanup_sessions (GstRTSPClient * client)
309 GstRTSPClientPrivate *priv = client->priv;
312 /* remove weak-ref from sessions */
313 for (sessions = priv->sessions; sessions; sessions = g_list_next (sessions)) {
314 client_cleanup_session (client, (GstRTSPSession *) sessions->data);
316 g_list_free (priv->sessions);
317 priv->sessions = NULL;
320 /* A client is finalized when the connection is broken */
322 gst_rtsp_client_finalize (GObject * obj)
324 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
325 GstRTSPClientPrivate *priv = client->priv;
327 GST_INFO ("finalize client %p", client);
329 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
332 g_source_destroy ((GSource *) priv->watch);
334 client_cleanup_sessions (client);
336 if (priv->connection)
337 gst_rtsp_connection_free (priv->connection);
338 if (priv->session_pool)
339 g_object_unref (priv->session_pool);
340 if (priv->mount_points)
341 g_object_unref (priv->mount_points);
343 g_object_unref (priv->auth);
348 gst_rtsp_media_unprepare (priv->media);
349 g_object_unref (priv->media);
352 g_free (priv->server_ip);
353 g_mutex_clear (&priv->lock);
354 g_mutex_clear (&priv->send_lock);
356 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
360 gst_rtsp_client_get_property (GObject * object, guint propid,
361 GValue * value, GParamSpec * pspec)
363 GstRTSPClient *client = GST_RTSP_CLIENT (object);
366 case PROP_SESSION_POOL:
367 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
369 case PROP_MOUNT_POINTS:
370 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
373 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
378 gst_rtsp_client_set_property (GObject * object, guint propid,
379 const GValue * value, GParamSpec * pspec)
381 GstRTSPClient *client = GST_RTSP_CLIENT (object);
384 case PROP_SESSION_POOL:
385 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
387 case PROP_MOUNT_POINTS:
388 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
391 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
396 * gst_rtsp_client_new:
398 * Create a new #GstRTSPClient instance.
400 * Returns: a new #GstRTSPClient
403 gst_rtsp_client_new (void)
405 GstRTSPClient *result;
407 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
413 send_message (GstRTSPClient * client, GstRTSPSession * session,
414 GstRTSPMessage * message, gboolean close)
416 GstRTSPClientPrivate *priv = client->priv;
418 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
419 "GStreamer RTSP server");
421 /* remove any previous header */
422 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
424 /* add the new session header for new session ids */
426 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
427 gst_rtsp_session_get_header (session));
430 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
431 gst_rtsp_message_dump (message);
435 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
437 g_mutex_lock (&priv->send_lock);
439 priv->send_func (client, message, close, priv->send_data);
440 g_mutex_unlock (&priv->send_lock);
442 gst_rtsp_message_unset (message);
446 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
447 GstRTSPClientState * state)
449 gst_rtsp_message_init_response (state->response, code,
450 gst_rtsp_status_as_text (code), state->request);
452 send_message (client, NULL, state->response, FALSE);
456 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
458 if (path1 == NULL || path2 == NULL)
461 if (strlen (path1) != len2)
464 if (strncmp (path1, path2, len2))
470 /* this function is called to initially find the media for the DESCRIBE request
471 * but is cached for when the same client (without breaking the connection) is
472 * doing a setup for the exact same url. */
473 static GstRTSPMedia *
474 find_media (GstRTSPClient * client, GstRTSPClientState * state, gint * matched)
476 GstRTSPClientPrivate *priv = client->priv;
477 GstRTSPMediaFactory *factory;
482 if (!priv->mount_points)
483 goto no_mount_points;
485 path = state->uri->abspath;
487 /* find the longest matching factory for the uri first */
488 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
492 state->factory = factory;
494 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
495 goto no_factory_access;
497 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
503 path_len = strlen (path);
505 if (!paths_are_equal (priv->path, path, path_len)) {
506 GstRTSPThread *thread;
508 /* remove any previously cached values before we try to construct a new
514 gst_rtsp_media_unprepare (priv->media);
515 g_object_unref (priv->media);
519 /* prepare the media and add it to the pipeline */
520 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
523 state->media = media;
525 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
526 GST_RTSP_THREAD_TYPE_MEDIA, state);
530 /* prepare the media */
531 if (!(gst_rtsp_media_prepare (media, thread)))
534 /* now keep track of the uri and the media */
535 priv->path = g_strndup (path, path_len);
538 /* we have seen this path before, used cached media */
540 state->media = media;
541 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
544 g_object_unref (factory);
545 state->factory = NULL;
548 g_object_ref (media);
555 GST_ERROR ("client %p: no mount points configured", client);
556 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
561 GST_ERROR ("client %p: no factory for uri %s", client, path);
562 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
567 GST_ERROR ("client %p: not authorized to see factory uri %s", client, path);
572 GST_ERROR ("client %p: not authorized for factory uri %s", client, path);
577 GST_ERROR ("client %p: can't create media", client);
578 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
579 g_object_unref (factory);
580 state->factory = NULL;
585 GST_ERROR ("client %p: can't create thread", client);
586 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
587 g_object_unref (media);
589 g_object_unref (factory);
590 state->factory = NULL;
595 GST_ERROR ("client %p: can't prepare media", client);
596 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
597 g_object_unref (media);
599 g_object_unref (factory);
600 state->factory = NULL;
606 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
608 GstRTSPClientPrivate *priv = client->priv;
609 GstRTSPMessage message = { 0 };
614 gst_rtsp_message_init_data (&message, channel);
616 /* FIXME, need some sort of iovec RTSPMessage here */
617 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
620 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
622 g_mutex_lock (&priv->send_lock);
624 priv->send_func (client, &message, FALSE, priv->send_data);
625 g_mutex_unlock (&priv->send_lock);
627 gst_rtsp_message_steal_body (&message, &data, &usize);
628 gst_buffer_unmap (buffer, &map_info);
630 gst_rtsp_message_unset (&message);
636 link_transport (GstRTSPClient * client, GstRTSPSession * session,
637 GstRTSPStreamTransport * trans)
639 GstRTSPClientPrivate *priv = client->priv;
641 GST_DEBUG ("client %p: linking transport %p", client, trans);
643 gst_rtsp_stream_transport_set_callbacks (trans,
644 (GstRTSPSendFunc) do_send_data,
645 (GstRTSPSendFunc) do_send_data, client, NULL);
647 priv->transports = g_list_prepend (priv->transports, trans);
649 /* make sure our session can't expire */
650 gst_rtsp_session_prevent_expire (session);
654 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
655 GstRTSPStreamTransport * trans)
657 GstRTSPClientPrivate *priv = client->priv;
659 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
661 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
663 priv->transports = g_list_remove (priv->transports, trans);
665 /* our session can now expire */
666 gst_rtsp_session_allow_expire (session);
670 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
671 GstRTSPSessionMedia * sessmedia)
676 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
677 for (i = 0; i < n_streams; i++) {
678 GstRTSPStreamTransport *trans;
679 const GstRTSPTransport *tr;
681 /* get the transport, if there is no transport configured, skip this stream */
682 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
686 tr = gst_rtsp_stream_transport_get_transport (trans);
688 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
689 /* for TCP, unlink the stream from the TCP connection of the client */
690 unlink_transport (client, session, trans);
696 close_connection (GstRTSPClient * client)
698 GstRTSPClientPrivate *priv = client->priv;
699 const gchar *tunnelid;
701 GST_DEBUG ("client %p: closing connection", client);
703 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
704 g_mutex_lock (&tunnels_lock);
705 /* remove from tunnelids */
706 g_hash_table_remove (tunnels, tunnelid);
707 g_mutex_unlock (&tunnels_lock);
710 gst_rtsp_connection_close (priv->connection);
714 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
716 GstRTSPClientPrivate *priv = client->priv;
717 GstRTSPSession *session;
718 GstRTSPSessionMedia *sessmedia;
719 GstRTSPStatusCode code;
726 session = state->session;
731 path = state->uri->abspath;
733 /* get a handle to the configuration of the media in the session */
734 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
738 /* only aggregate control for now.. */
739 if (path[matched] != '\0')
742 state->sessmedia = sessmedia;
744 /* we emit the signal before closing the connection */
745 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
748 /* unlink the all TCP callbacks */
749 unlink_session_transports (client, session, sessmedia);
751 /* remove the session from the watched sessions */
752 client_unwatch_session (client, session);
754 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
756 /* unmanage the media in the session, returns false if all media session
758 if (!gst_rtsp_session_release_media (session, sessmedia)) {
759 /* remove the session */
760 gst_rtsp_session_pool_remove (priv->session_pool, session);
762 /* construct the response now */
763 code = GST_RTSP_STS_OK;
764 gst_rtsp_message_init_response (state->response, code,
765 gst_rtsp_status_as_text (code), state->request);
767 send_message (client, session, state->response, TRUE);
774 GST_ERROR ("client %p: no session", client);
775 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
780 GST_ERROR ("client %p: no uri supplied", client);
781 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
786 GST_ERROR ("client %p: no media for uri", client);
787 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
792 GST_ERROR ("client %p: no aggregate path %s", client, path);
793 send_generic_response (client,
794 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
800 default_params_set (GstRTSPClient * client, GstRTSPClientState * state)
804 res = gst_rtsp_params_set (client, state);
810 default_params_get (GstRTSPClient * client, GstRTSPClientState * state)
814 res = gst_rtsp_params_get (client, state);
820 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
826 res = gst_rtsp_message_get_body (state->request, &data, &size);
827 if (res != GST_RTSP_OK)
831 /* no body, keep-alive request */
832 send_generic_response (client, GST_RTSP_STS_OK, state);
834 /* there is a body, handle the params */
835 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, state);
836 if (res != GST_RTSP_OK)
839 send_message (client, state->session, state->response, FALSE);
842 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
850 GST_ERROR ("client %p: bad request", client);
851 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
857 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
863 res = gst_rtsp_message_get_body (state->request, &data, &size);
864 if (res != GST_RTSP_OK)
868 /* no body, keep-alive request */
869 send_generic_response (client, GST_RTSP_STS_OK, state);
871 /* there is a body, handle the params */
872 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, state);
873 if (res != GST_RTSP_OK)
876 send_message (client, state->session, state->response, FALSE);
879 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
887 GST_ERROR ("client %p: bad request", client);
888 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
894 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
896 GstRTSPSession *session;
897 GstRTSPSessionMedia *sessmedia;
898 GstRTSPStatusCode code;
899 GstRTSPState rtspstate;
903 if (!(session = state->session))
909 path = state->uri->abspath;
911 /* get a handle to the configuration of the media in the session */
912 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
916 if (path[matched] != '\0')
919 state->sessmedia = sessmedia;
921 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
922 /* the session state must be playing or recording */
923 if (rtspstate != GST_RTSP_STATE_PLAYING &&
924 rtspstate != GST_RTSP_STATE_RECORDING)
927 /* unlink the all TCP callbacks */
928 unlink_session_transports (client, session, sessmedia);
930 /* then pause sending */
931 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
933 /* construct the response now */
934 code = GST_RTSP_STS_OK;
935 gst_rtsp_message_init_response (state->response, code,
936 gst_rtsp_status_as_text (code), state->request);
938 send_message (client, session, state->response, FALSE);
940 /* the state is now READY */
941 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
943 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
951 GST_ERROR ("client %p: no seesion", client);
952 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
957 GST_ERROR ("client %p: no uri supplied", client);
958 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
963 GST_ERROR ("client %p: no media for uri", client);
964 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
969 GST_ERROR ("client %p: no aggregate path %s", client, path);
970 send_generic_response (client,
971 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
976 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
977 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
984 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
986 GstRTSPSession *session;
987 GstRTSPSessionMedia *sessmedia;
989 GstRTSPStatusCode code;
991 guint n_streams, i, infocount;
993 GstRTSPTimeRange *range;
995 GstRTSPState rtspstate;
996 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1000 if (!(session = state->session))
1006 path = state->uri->abspath;
1008 /* get a handle to the configuration of the media in the session */
1009 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1013 if (path[matched] != '\0')
1016 state->sessmedia = sessmedia;
1017 state->media = media = gst_rtsp_session_media_get_media (sessmedia);
1019 /* the session state must be playing or ready */
1020 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1021 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1024 /* parse the range header if we have one */
1026 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
1027 if (res == GST_RTSP_OK) {
1028 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1029 /* we have a range, seek to the position */
1031 gst_rtsp_media_seek (media, range);
1032 gst_rtsp_range_free (range);
1036 /* grab RTPInfo from the payloaders now */
1037 rtpinfo = g_string_new ("");
1039 n_streams = gst_rtsp_media_n_streams (media);
1040 for (i = 0, infocount = 0; i < n_streams; i++) {
1041 GstRTSPStreamTransport *trans;
1042 GstRTSPStream *stream;
1043 const GstRTSPTransport *tr;
1047 /* get the transport, if there is no transport configured, skip this stream */
1048 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
1049 if (trans == NULL) {
1050 GST_INFO ("stream %d is not configured", i);
1053 tr = gst_rtsp_stream_transport_get_transport (trans);
1055 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1056 /* for TCP, link the stream to the TCP connection of the client */
1057 link_transport (client, session, trans);
1060 stream = gst_rtsp_stream_transport_get_stream (trans);
1061 if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
1063 g_string_append (rtpinfo, ", ");
1065 uristr = gst_rtsp_url_get_request_uri (state->uri);
1066 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
1067 uristr, i, seq, rtptime);
1072 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
1076 /* construct the response now */
1077 code = GST_RTSP_STS_OK;
1078 gst_rtsp_message_init_response (state->response, code,
1079 gst_rtsp_status_as_text (code), state->request);
1081 /* add the RTP-Info header */
1082 if (infocount > 0) {
1083 str = g_string_free (rtpinfo, FALSE);
1084 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
1086 g_string_free (rtpinfo, TRUE);
1090 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1091 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
1093 send_message (client, session, state->response, FALSE);
1095 /* start playing after sending the request */
1096 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1098 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1100 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
1108 GST_ERROR ("client %p: no session", client);
1109 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1114 GST_ERROR ("client %p: no uri supplied", client);
1115 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1120 GST_ERROR ("client %p: media not found", client);
1121 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1126 GST_ERROR ("client %p: no aggregate path %s", client, path);
1127 send_generic_response (client,
1128 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
1133 GST_ERROR ("client %p: not PLAYING or READY", client);
1134 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1141 do_keepalive (GstRTSPSession * session)
1143 GST_INFO ("keep session %p alive", session);
1144 gst_rtsp_session_touch (session);
1147 /* parse @transport and return a valid transport in @tr. only transports
1148 * from @supported are returned. Returns FALSE if no valid transport
1151 parse_transport (const char *transport, GstRTSPLowerTrans supported,
1152 GstRTSPTransport * tr)
1159 gst_rtsp_transport_init (tr);
1161 GST_DEBUG ("parsing transports %s", transport);
1163 transports = g_strsplit (transport, ",", 0);
1165 /* loop through the transports, try to parse */
1166 for (i = 0; transports[i]; i++) {
1167 res = gst_rtsp_transport_parse (transports[i], tr);
1168 if (res != GST_RTSP_OK) {
1169 /* no valid transport, search some more */
1170 GST_WARNING ("could not parse transport %s", transports[i]);
1174 /* we have a transport, see if it's RTP/AVP */
1175 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1176 GST_WARNING ("invalid transport %s", transports[i]);
1180 if (!(tr->lower_transport & supported)) {
1181 GST_WARNING ("unsupported transport %s", transports[i]);
1185 /* we have a valid transport */
1186 GST_INFO ("found valid transport %s", transports[i]);
1191 gst_rtsp_transport_init (tr);
1193 g_strfreev (transports);
1199 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
1200 GstRTSPMessage * request)
1202 gchar *blocksize_str;
1203 gboolean ret = TRUE;
1205 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1206 &blocksize_str, 0) == GST_RTSP_OK) {
1210 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1211 if (end == blocksize_str) {
1212 GST_ERROR ("failed to parse blocksize");
1215 /* we don't want to change the mtu when this media
1216 * can be shared because it impacts other clients */
1217 if (gst_rtsp_media_is_shared (media))
1220 if (blocksize > G_MAXUINT)
1221 blocksize = G_MAXUINT;
1222 gst_rtsp_stream_set_mtu (stream, blocksize);
1229 default_configure_client_transport (GstRTSPClient * client,
1230 GstRTSPClientState * state, GstRTSPTransport * ct)
1232 GstRTSPClientPrivate *priv = client->priv;
1234 /* we have a valid transport now, set the destination of the client. */
1235 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1236 gboolean use_client_settings;
1238 use_client_settings =
1239 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1241 if (ct->destination && use_client_settings) {
1242 GstRTSPAddress *addr;
1244 addr = gst_rtsp_stream_reserve_address (state->stream, ct->destination,
1245 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1250 gst_rtsp_address_free (addr);
1252 GstRTSPAddress *addr;
1253 GSocketFamily family;
1255 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1257 addr = gst_rtsp_stream_get_multicast_address (state->stream, family);
1261 g_free (ct->destination);
1262 ct->destination = g_strdup (addr->address);
1263 ct->port.min = addr->port;
1264 ct->port.max = addr->port + addr->n_ports - 1;
1265 ct->ttl = addr->ttl;
1267 gst_rtsp_address_free (addr);
1272 url = gst_rtsp_connection_get_url (priv->connection);
1273 g_free (ct->destination);
1274 ct->destination = g_strdup (url->host);
1276 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1277 /* check if the client selected channels for TCP */
1278 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1279 gst_rtsp_session_media_alloc_channels (state->sessmedia,
1289 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1294 static GstRTSPTransport *
1295 make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
1296 GstRTSPTransport * ct)
1298 GstRTSPTransport *st;
1300 GSocketFamily family;
1302 /* prepare the server transport */
1303 gst_rtsp_transport_new (&st);
1305 st->trans = ct->trans;
1306 st->profile = ct->profile;
1307 st->lower_transport = ct->lower_transport;
1309 addr = g_inet_address_new_from_string (ct->destination);
1312 GST_ERROR ("failed to get inet addr from client destination");
1313 family = G_SOCKET_FAMILY_IPV4;
1315 family = g_inet_address_get_family (addr);
1316 g_object_unref (addr);
1320 switch (st->lower_transport) {
1321 case GST_RTSP_LOWER_TRANS_UDP:
1322 st->client_port = ct->client_port;
1323 gst_rtsp_stream_get_server_port (state->stream, &st->server_port, family);
1325 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1326 st->port = ct->port;
1327 st->destination = g_strdup (ct->destination);
1330 case GST_RTSP_LOWER_TRANS_TCP:
1331 st->interleaved = ct->interleaved;
1336 gst_rtsp_stream_get_ssrc (state->stream, &st->ssrc);
1342 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
1344 GstRTSPClientPrivate *priv = client->priv;
1348 GstRTSPTransport *ct, *st;
1349 GstRTSPLowerTrans supported;
1350 GstRTSPStatusCode code;
1351 GstRTSPSession *session;
1352 GstRTSPStreamTransport *trans;
1354 GstRTSPSessionMedia *sessmedia;
1355 GstRTSPMedia *media;
1356 GstRTSPStream *stream;
1357 GstRTSPState rtspstate;
1358 GstRTSPClientClass *klass;
1359 gchar *path, *control;
1366 path = uri->abspath;
1368 /* parse the transport */
1370 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
1372 if (res != GST_RTSP_OK)
1375 /* we create the session after parsing stuff so that we don't make
1376 * a session for malformed requests */
1377 if (priv->session_pool == NULL)
1380 session = state->session;
1383 g_object_ref (session);
1384 /* get a handle to the configuration of the media in the session, this can
1385 * return NULL if this is a new url to manage in this session. */
1386 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1388 /* we need a new media configuration in this session */
1392 /* we have no session media, find one and manage it */
1393 if (sessmedia == NULL) {
1394 /* get a handle to the configuration of the media in the session */
1395 media = find_media (client, state, &matched);
1397 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1398 g_object_ref (media);
1400 /* no media, not found then */
1402 goto media_not_found;
1404 /* path is what matched. We can modify the parsed uri in place */
1405 path[matched] = '\0';
1406 /* control is remainder */
1407 control = &path[matched + 1];
1409 /* find the stream now using the control part */
1410 stream = gst_rtsp_media_find_stream (media, control);
1412 goto stream_not_found;
1414 /* now we have a uri identifying a valid media and stream */
1415 state->stream = stream;
1416 state->media = media;
1418 if (session == NULL) {
1419 /* create a session if this fails we probably reached our session limit or
1421 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1422 goto service_unavailable;
1424 /* make sure this client is closed when the session is closed */
1425 client_watch_session (client, session);
1427 /* signal new session */
1428 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1431 state->session = session;
1434 if (sessmedia == NULL) {
1435 /* manage the media in our session now, if not done already */
1436 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1437 /* if we stil have no media, error */
1438 if (sessmedia == NULL)
1439 goto sessmedia_unavailable;
1441 g_object_unref (media);
1444 state->sessmedia = sessmedia;
1446 /* set blocksize on this stream */
1447 if (!handle_blocksize (media, stream, state->request))
1448 goto invalid_blocksize;
1450 gst_rtsp_transport_new (&ct);
1452 /* our supported transports */
1453 supported = GST_RTSP_LOWER_TRANS_UDP |
1454 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
1456 /* parse and find a usable supported transport */
1457 if (!parse_transport (transport, supported, ct))
1458 goto unsupported_transports;
1460 /* update the client transport */
1461 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1462 if (!klass->configure_client_transport (client, state, ct))
1463 goto unsupported_client_transport;
1465 /* set in the session media transport */
1466 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1468 /* configure keepalive for this transport */
1469 gst_rtsp_stream_transport_set_keepalive (trans,
1470 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1472 /* create and serialize the server transport */
1473 st = make_server_transport (client, state, ct);
1474 trans_str = gst_rtsp_transport_as_text (st);
1475 gst_rtsp_transport_free (st);
1477 /* construct the response now */
1478 code = GST_RTSP_STS_OK;
1479 gst_rtsp_message_init_response (state->response, code,
1480 gst_rtsp_status_as_text (code), state->request);
1482 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1486 send_message (client, session, state->response, FALSE);
1488 /* update the state */
1489 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1490 switch (rtspstate) {
1491 case GST_RTSP_STATE_PLAYING:
1492 case GST_RTSP_STATE_RECORDING:
1493 case GST_RTSP_STATE_READY:
1494 /* no state change */
1497 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1500 g_object_unref (session);
1502 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST],
1510 GST_ERROR ("client %p: no uri", client);
1511 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1516 GST_ERROR ("client %p: no transport", client);
1517 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1522 GST_ERROR ("client %p: no session pool configured", client);
1523 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1528 GST_ERROR ("client %p: media '%s' not found", client, path);
1529 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1534 GST_ERROR ("client %p: stream '%s' not found", client, control);
1535 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1536 g_object_unref (media);
1539 service_unavailable:
1541 GST_ERROR ("client %p: can't create session", client);
1542 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1543 g_object_unref (media);
1546 sessmedia_unavailable:
1548 GST_ERROR ("client %p: can't create session media", client);
1549 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1550 g_object_unref (media);
1551 g_object_unref (session);
1556 GST_ERROR ("client %p: invalid blocksize", client);
1557 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1558 g_object_unref (session);
1561 unsupported_transports:
1563 GST_ERROR ("client %p: unsupported transports", client);
1564 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1565 gst_rtsp_transport_free (ct);
1566 g_object_unref (session);
1569 unsupported_client_transport:
1571 GST_ERROR ("client %p: unsupported client transport", client);
1572 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1573 gst_rtsp_transport_free (ct);
1574 g_object_unref (session);
1579 static GstSDPMessage *
1580 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1582 GstRTSPClientPrivate *priv = client->priv;
1587 gst_sdp_message_new (&sdp);
1589 /* some standard things first */
1590 gst_sdp_message_set_version (sdp, "0");
1597 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1600 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1601 gst_sdp_message_set_information (sdp, "rtsp-server");
1602 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1603 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1604 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1605 gst_sdp_message_add_attribute (sdp, "control", "*");
1607 info.is_ipv6 = priv->is_ipv6;
1608 info.server_ip = priv->server_ip;
1610 /* create an SDP for the media object */
1611 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1619 GST_ERROR ("client %p: could not create SDP", client);
1620 gst_sdp_message_free (sdp);
1625 /* for the describe we must generate an SDP */
1627 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1632 gchar *str, *content_base;
1633 GstRTSPMedia *media;
1634 GstRTSPClientClass *klass;
1636 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1641 /* check what kind of format is accepted, we don't really do anything with it
1642 * and always return SDP for now. */
1647 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1649 if (res == GST_RTSP_ENOTIMPL)
1652 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1656 /* find the media object for the uri */
1657 if (!(media = find_media (client, state, NULL)))
1660 /* create an SDP for the media object on this client */
1661 if (!(sdp = klass->create_sdp (client, media)))
1664 g_object_unref (media);
1666 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1667 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1669 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1672 /* content base for some clients that might screw up creating the setup uri */
1673 str = gst_rtsp_url_get_request_uri (state->uri);
1674 str_len = strlen (str);
1676 /* check for trailing '/' and append one */
1677 if (str[str_len - 1] != '/') {
1678 content_base = g_malloc (str_len + 2);
1679 memcpy (content_base, str, str_len);
1680 content_base[str_len] = '/';
1681 content_base[str_len + 1] = '\0';
1687 GST_INFO ("adding content-base: %s", content_base);
1689 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1691 g_free (content_base);
1693 /* add SDP to the response body */
1694 str = gst_sdp_message_as_text (sdp);
1695 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1696 gst_sdp_message_free (sdp);
1698 send_message (client, state->session, state->response, FALSE);
1700 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1708 GST_ERROR ("client %p: no uri", client);
1709 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1714 GST_ERROR ("client %p: no media", client);
1715 /* error reply is already sent */
1720 GST_ERROR ("client %p: can't create SDP", client);
1721 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1722 g_object_unref (media);
1728 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1730 GstRTSPMethod options;
1733 options = GST_RTSP_DESCRIBE |
1738 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1740 str = gst_rtsp_options_as_text (options);
1742 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1743 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1745 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1748 send_message (client, state->session, state->response, FALSE);
1750 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1756 /* remove duplicate and trailing '/' */
1758 sanitize_uri (GstRTSPUrl * uri)
1762 gboolean have_slash, prev_slash;
1764 s = d = uri->abspath;
1765 len = strlen (uri->abspath);
1769 for (i = 0; i < len; i++) {
1770 have_slash = s[i] == '/';
1772 if (!have_slash || !prev_slash)
1774 prev_slash = have_slash;
1776 len = d - uri->abspath;
1777 /* don't remove the first slash if that's the only thing left */
1778 if (len > 1 && *(d - 1) == '/')
1784 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1786 GstRTSPClientPrivate *priv = client->priv;
1788 GST_INFO ("client %p: session %p finished", client, session);
1790 /* unlink all media managed in this session */
1791 client_unlink_session (client, session);
1793 /* remove the session */
1794 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1795 GST_INFO ("client %p: all sessions finalized, close the connection",
1797 close_connection (client);
1801 static GPrivate current_state;
1804 * gst_rtsp_client_state_get_current:
1806 * Get the current #GstRTSPClientState. This object is retrieved from the
1807 * current thread that is handling the request for a client.
1809 * Returns: a #GstRTSPClientState
1811 GstRTSPClientState *
1812 gst_rtsp_client_state_get_current (void)
1816 l = g_private_get (¤t_state);
1820 return (GstRTSPClientState *) (l->data);
1825 * gst_rtsp_client_state_push_current:
1826 * @state: a ##GstRTSPClientState
1828 * Pushes @state onto the state stack. The current
1829 * state can then be received using gst_rtsp_client_state_get_current().
1832 gst_rtsp_client_state_push_current (GstRTSPClientState * state)
1836 g_return_if_fail (state != NULL);
1838 l = g_private_get (¤t_state);
1839 l = g_slist_prepend (l, state);
1840 g_private_set (¤t_state, l);
1844 * gst_rtsp_client_state_pop_current:
1845 * @state: a #GstRTSPClientState
1847 * Pops @state off the state stack (verifying that @state
1848 * is on the top of the stack).
1851 gst_rtsp_client_state_pop_current (GstRTSPClientState * state)
1855 l = g_private_get (¤t_state);
1857 g_return_if_fail (l != NULL);
1858 g_return_if_fail (l->data == state);
1860 l = g_slist_delete_link (l, l);
1861 g_private_set (¤t_state, l);
1865 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1867 GstRTSPClientPrivate *priv = client->priv;
1868 GstRTSPMethod method;
1869 const gchar *uristr;
1870 GstRTSPUrl *uri = NULL;
1871 GstRTSPVersion version;
1873 GstRTSPSession *session = NULL;
1874 GstRTSPClientState state = { NULL };
1875 GstRTSPMessage response = { 0 };
1878 state.conn = priv->connection;
1879 state.client = client;
1880 state.request = request;
1881 state.response = &response;
1882 state.auth = priv->auth;
1883 gst_rtsp_client_state_push_current (&state);
1885 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1886 gst_rtsp_message_dump (request);
1889 GST_INFO ("client %p: received a request", client);
1891 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1893 /* we can only handle 1.0 requests */
1894 if (version != GST_RTSP_VERSION_1_0)
1897 state.method = method;
1899 /* we always try to parse the url first */
1900 if (strcmp (uristr, "*") == 0) {
1901 /* special case where we have * as uri, keep uri = NULL */
1902 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1905 /* get the session if there is any */
1906 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1907 if (res == GST_RTSP_OK) {
1908 if (priv->session_pool == NULL)
1911 /* we had a session in the request, find it again */
1912 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1913 goto session_not_found;
1915 /* we add the session to the client list of watched sessions. When a session
1916 * disappears because it times out, we will be notified. If all sessions are
1917 * gone, we will close the connection */
1918 client_watch_session (client, session);
1921 /* sanitize the uri */
1925 state.session = session;
1927 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
1928 goto not_authorized;
1930 /* now see what is asked and dispatch to a dedicated handler */
1932 case GST_RTSP_OPTIONS:
1933 handle_options_request (client, &state);
1935 case GST_RTSP_DESCRIBE:
1936 handle_describe_request (client, &state);
1938 case GST_RTSP_SETUP:
1939 handle_setup_request (client, &state);
1942 handle_play_request (client, &state);
1944 case GST_RTSP_PAUSE:
1945 handle_pause_request (client, &state);
1947 case GST_RTSP_TEARDOWN:
1948 handle_teardown_request (client, &state);
1950 case GST_RTSP_SET_PARAMETER:
1951 handle_set_param_request (client, &state);
1953 case GST_RTSP_GET_PARAMETER:
1954 handle_get_param_request (client, &state);
1956 case GST_RTSP_ANNOUNCE:
1957 case GST_RTSP_RECORD:
1958 case GST_RTSP_REDIRECT:
1959 goto not_implemented;
1960 case GST_RTSP_INVALID:
1966 gst_rtsp_client_state_pop_current (&state);
1968 g_object_unref (session);
1970 gst_rtsp_url_free (uri);
1976 GST_ERROR ("client %p: version %d not supported", client, version);
1977 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1983 GST_ERROR ("client %p: bad request", client);
1984 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1989 GST_ERROR ("client %p: no pool configured", client);
1990 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1995 GST_ERROR ("client %p: session not found", client);
1996 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
2001 GST_ERROR ("client %p: not allowed", client);
2006 GST_ERROR ("client %p: method %d not implemented", client, method);
2007 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
2013 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2015 GstRTSPClientPrivate *priv = client->priv;
2024 /* find the stream for this message */
2025 res = gst_rtsp_message_parse_data (message, &channel);
2026 if (res != GST_RTSP_OK)
2029 gst_rtsp_message_steal_body (message, &data, &size);
2031 buffer = gst_buffer_new_wrapped (data, size);
2034 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2035 GstRTSPStreamTransport *trans;
2036 GstRTSPStream *stream;
2037 const GstRTSPTransport *tr;
2041 tr = gst_rtsp_stream_transport_get_transport (trans);
2042 stream = gst_rtsp_stream_transport_get_stream (trans);
2044 /* check for TCP transport */
2045 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2046 /* dispatch to the stream based on the channel number */
2047 if (tr->interleaved.min == channel) {
2048 gst_rtsp_stream_recv_rtp (stream, buffer);
2051 } else if (tr->interleaved.max == channel) {
2052 gst_rtsp_stream_recv_rtcp (stream, buffer);
2059 gst_buffer_unref (buffer);
2063 * gst_rtsp_client_set_session_pool:
2064 * @client: a #GstRTSPClient
2065 * @pool: a #GstRTSPSessionPool
2067 * Set @pool as the sessionpool for @client which it will use to find
2068 * or allocate sessions. the sessionpool is usually inherited from the server
2069 * that created the client but can be overridden later.
2072 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2073 GstRTSPSessionPool * pool)
2075 GstRTSPSessionPool *old;
2076 GstRTSPClientPrivate *priv;
2078 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2080 priv = client->priv;
2083 g_object_ref (pool);
2085 g_mutex_lock (&priv->lock);
2086 old = priv->session_pool;
2087 priv->session_pool = pool;
2088 g_mutex_unlock (&priv->lock);
2091 g_object_unref (old);
2095 * gst_rtsp_client_get_session_pool:
2096 * @client: a #GstRTSPClient
2098 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2100 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2102 GstRTSPSessionPool *
2103 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2105 GstRTSPClientPrivate *priv;
2106 GstRTSPSessionPool *result;
2108 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2110 priv = client->priv;
2112 g_mutex_lock (&priv->lock);
2113 if ((result = priv->session_pool))
2114 g_object_ref (result);
2115 g_mutex_unlock (&priv->lock);
2121 * gst_rtsp_client_set_mount_points:
2122 * @client: a #GstRTSPClient
2123 * @mounts: a #GstRTSPMountPoints
2125 * Set @mounts as the mount points for @client which it will use to map urls
2126 * to media streams. These mount points are usually inherited from the server that
2127 * created the client but can be overriden later.
2130 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2131 GstRTSPMountPoints * mounts)
2133 GstRTSPClientPrivate *priv;
2134 GstRTSPMountPoints *old;
2136 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2138 priv = client->priv;
2141 g_object_ref (mounts);
2143 g_mutex_lock (&priv->lock);
2144 old = priv->mount_points;
2145 priv->mount_points = mounts;
2146 g_mutex_unlock (&priv->lock);
2149 g_object_unref (old);
2153 * gst_rtsp_client_get_mount_points:
2154 * @client: a #GstRTSPClient
2156 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2158 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2160 GstRTSPMountPoints *
2161 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2163 GstRTSPClientPrivate *priv;
2164 GstRTSPMountPoints *result;
2166 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2168 priv = client->priv;
2170 g_mutex_lock (&priv->lock);
2171 if ((result = priv->mount_points))
2172 g_object_ref (result);
2173 g_mutex_unlock (&priv->lock);
2179 * gst_rtsp_client_set_auth:
2180 * @client: a #GstRTSPClient
2181 * @auth: a #GstRTSPAuth
2183 * configure @auth to be used as the authentication manager of @client.
2186 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2188 GstRTSPClientPrivate *priv;
2191 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2193 priv = client->priv;
2196 g_object_ref (auth);
2198 g_mutex_lock (&priv->lock);
2201 g_mutex_unlock (&priv->lock);
2204 g_object_unref (old);
2209 * gst_rtsp_client_get_auth:
2210 * @client: a #GstRTSPClient
2212 * Get the #GstRTSPAuth used as the authentication manager of @client.
2214 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2218 gst_rtsp_client_get_auth (GstRTSPClient * client)
2220 GstRTSPClientPrivate *priv;
2221 GstRTSPAuth *result;
2223 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2225 priv = client->priv;
2227 g_mutex_lock (&priv->lock);
2228 if ((result = priv->auth))
2229 g_object_ref (result);
2230 g_mutex_unlock (&priv->lock);
2236 * gst_rtsp_client_set_thread_pool:
2237 * @client: a #GstRTSPClient
2238 * @pool: a #GstRTSPThreadPool
2240 * configure @pool to be used as the thread pool of @client.
2243 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2244 GstRTSPThreadPool * pool)
2246 GstRTSPClientPrivate *priv;
2247 GstRTSPThreadPool *old;
2249 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2251 priv = client->priv;
2254 g_object_ref (pool);
2256 g_mutex_lock (&priv->lock);
2257 old = priv->thread_pool;
2258 priv->thread_pool = pool;
2259 g_mutex_unlock (&priv->lock);
2262 g_object_unref (old);
2266 * gst_rtsp_client_get_thread_pool:
2267 * @client: a #GstRTSPClient
2269 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2271 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2275 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2277 GstRTSPClientPrivate *priv;
2278 GstRTSPThreadPool *result;
2280 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2282 priv = client->priv;
2284 g_mutex_lock (&priv->lock);
2285 if ((result = priv->thread_pool))
2286 g_object_ref (result);
2287 g_mutex_unlock (&priv->lock);
2293 * gst_rtsp_client_set_connection:
2294 * @client: a #GstRTSPClient
2295 * @conn: (transfer full): a #GstRTSPConnection
2297 * Set the #GstRTSPConnection of @client. This function takes ownership of
2300 * Returns: %TRUE on success.
2303 gst_rtsp_client_set_connection (GstRTSPClient * client,
2304 GstRTSPConnection * conn)
2306 GstRTSPClientPrivate *priv;
2307 GSocket *read_socket;
2308 GSocketAddress *address;
2310 GError *error = NULL;
2312 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2313 g_return_val_if_fail (conn != NULL, FALSE);
2315 priv = client->priv;
2317 read_socket = gst_rtsp_connection_get_read_socket (conn);
2319 if (!(address = g_socket_get_local_address (read_socket, &error)))
2322 g_free (priv->server_ip);
2323 /* keep the original ip that the client connected to */
2324 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2325 GInetAddress *iaddr;
2327 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2329 /* socket might be ipv6 but adress still ipv4 */
2330 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2331 priv->server_ip = g_inet_address_to_string (iaddr);
2332 g_object_unref (address);
2334 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2335 priv->server_ip = g_strdup ("unknown");
2338 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2339 priv->server_ip, priv->is_ipv6);
2341 url = gst_rtsp_connection_get_url (conn);
2342 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2344 priv->connection = conn;
2351 GST_ERROR ("could not get local address %s", error->message);
2352 g_error_free (error);
2358 * gst_rtsp_client_get_connection:
2359 * @client: a #GstRTSPClient
2361 * Get the #GstRTSPConnection of @client.
2363 * Returns: (transfer none): the #GstRTSPConnection of @client.
2364 * The connection object returned remains valid until the client is freed.
2367 gst_rtsp_client_get_connection (GstRTSPClient * client)
2369 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2371 return client->priv->connection;
2375 * gst_rtsp_client_set_send_func:
2376 * @client: a #GstRTSPClient
2377 * @func: a #GstRTSPClientSendFunc
2378 * @user_data: user data passed to @func
2379 * @notify: called when @user_data is no longer in use
2381 * Set @func as the callback that will be called when a new message needs to be
2382 * sent to the client. @user_data is passed to @func and @notify is called when
2383 * @user_data is no longer in use.
2385 * By default, the client will send the messages on the #GstRTSPConnection that
2386 * was configured with gst_rtsp_client_attach() was called.
2389 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2390 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2392 GstRTSPClientPrivate *priv;
2393 GDestroyNotify old_notify;
2396 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2398 priv = client->priv;
2400 g_mutex_lock (&priv->send_lock);
2401 priv->send_func = func;
2402 old_notify = priv->send_notify;
2403 old_data = priv->send_data;
2404 priv->send_notify = notify;
2405 priv->send_data = user_data;
2406 g_mutex_unlock (&priv->send_lock);
2409 old_notify (old_data);
2413 * gst_rtsp_client_handle_message:
2414 * @client: a #GstRTSPClient
2415 * @message: an #GstRTSPMessage
2417 * Let the client handle @message.
2419 * Returns: a #GstRTSPResult.
2422 gst_rtsp_client_handle_message (GstRTSPClient * client,
2423 GstRTSPMessage * message)
2425 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2426 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2428 switch (message->type) {
2429 case GST_RTSP_MESSAGE_REQUEST:
2430 handle_request (client, message);
2432 case GST_RTSP_MESSAGE_RESPONSE:
2434 case GST_RTSP_MESSAGE_DATA:
2435 handle_data (client, message);
2444 * gst_rtsp_client_send_message:
2445 * @client: a #GstRTSPClient
2446 * @session: a #GstRTSPSession to send the message to or %NULL
2447 * @message: The #GstRTSPMessage to send
2449 * Send a message message to the remote end. @message must be a
2450 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
2453 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
2454 GstRTSPMessage * message)
2456 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2457 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2458 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
2459 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
2461 send_message (client, session, message, FALSE);
2466 static GstRTSPResult
2467 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2468 gboolean close, gpointer user_data)
2470 GstRTSPClientPrivate *priv = client->priv;
2472 /* send the response and store the seq number so we can wait until it's
2473 * written to the client to close the connection */
2474 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2475 &priv->close_seq : NULL);
2478 static GstRTSPResult
2479 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2482 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2485 static GstRTSPResult
2486 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2488 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2489 GstRTSPClientPrivate *priv = client->priv;
2491 if (priv->close_seq && priv->close_seq == cseq) {
2492 priv->close_seq = 0;
2493 close_connection (client);
2499 static GstRTSPResult
2500 closed (GstRTSPWatch * watch, gpointer user_data)
2502 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2503 GstRTSPClientPrivate *priv = client->priv;
2504 const gchar *tunnelid;
2506 GST_INFO ("client %p: connection closed", client);
2508 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2509 g_mutex_lock (&tunnels_lock);
2510 /* remove from tunnelids */
2511 g_hash_table_remove (tunnels, tunnelid);
2512 g_mutex_unlock (&tunnels_lock);
2515 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2520 static GstRTSPResult
2521 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2523 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2526 str = gst_rtsp_strresult (result);
2527 GST_INFO ("client %p: received an error %s", client, str);
2533 static GstRTSPResult
2534 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2535 GstRTSPMessage * message, guint id, gpointer user_data)
2537 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2540 str = gst_rtsp_strresult (result);
2542 ("client %p: error when handling message %p with id %d: %s",
2543 client, message, id, str);
2550 remember_tunnel (GstRTSPClient * client)
2552 GstRTSPClientPrivate *priv = client->priv;
2553 const gchar *tunnelid;
2555 /* store client in the pending tunnels */
2556 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2557 if (tunnelid == NULL)
2560 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2562 /* we can't have two clients connecting with the same tunnelid */
2563 g_mutex_lock (&tunnels_lock);
2564 if (g_hash_table_lookup (tunnels, tunnelid))
2565 goto tunnel_existed;
2567 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2568 g_mutex_unlock (&tunnels_lock);
2575 GST_ERROR ("client %p: no tunnelid provided", client);
2580 g_mutex_unlock (&tunnels_lock);
2581 GST_ERROR ("client %p: tunnel session %s already existed", client,
2587 static GstRTSPStatusCode
2588 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2590 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2591 GstRTSPClientPrivate *priv = client->priv;
2593 GST_INFO ("client %p: tunnel start (connection %p)", client,
2596 if (!remember_tunnel (client))
2599 return GST_RTSP_STS_OK;
2604 GST_ERROR ("client %p: error starting tunnel", client);
2605 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2609 static GstRTSPResult
2610 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2612 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2613 GstRTSPClientPrivate *priv = client->priv;
2615 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2618 /* ignore error, it'll only be a problem when the client does a POST again */
2619 remember_tunnel (client);
2624 static GstRTSPResult
2625 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2627 const gchar *tunnelid;
2628 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2629 GstRTSPClientPrivate *priv = client->priv;
2630 GstRTSPClient *oclient;
2631 GstRTSPClientPrivate *opriv;
2633 GST_INFO ("client %p: tunnel complete", client);
2635 /* find previous tunnel */
2636 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2637 if (tunnelid == NULL)
2640 g_mutex_lock (&tunnels_lock);
2641 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2644 /* remove the old client from the table. ref before because removing it will
2645 * remove the ref to it. */
2646 g_object_ref (oclient);
2647 g_hash_table_remove (tunnels, tunnelid);
2649 opriv = oclient->priv;
2651 if (opriv->watch == NULL)
2653 g_mutex_unlock (&tunnels_lock);
2655 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2656 opriv->connection, priv->connection);
2658 /* merge the tunnels into the first client */
2659 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2660 gst_rtsp_watch_reset (opriv->watch);
2661 g_object_unref (oclient);
2668 GST_ERROR ("client %p: no tunnelid provided", client);
2669 return GST_RTSP_ERROR;
2673 g_mutex_unlock (&tunnels_lock);
2674 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2675 return GST_RTSP_ERROR;
2679 g_mutex_unlock (&tunnels_lock);
2680 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2681 g_object_unref (oclient);
2682 return GST_RTSP_ERROR;
2686 static GstRTSPWatchFuncs watch_funcs = {
2698 client_watch_notify (GstRTSPClient * client)
2700 GstRTSPClientPrivate *priv = client->priv;
2702 GST_INFO ("client %p: watch destroyed", client);
2704 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2705 g_object_unref (client);
2709 * gst_rtsp_client_attach:
2710 * @client: a #GstRTSPClient
2711 * @context: (allow-none): a #GMainContext
2713 * Attaches @client to @context. When the mainloop for @context is run, the
2714 * client will be dispatched. When @context is NULL, the default context will be
2717 * This function should be called when the client properties and urls are fully
2718 * configured and the client is ready to start.
2720 * Returns: the ID (greater than 0) for the source within the GMainContext.
2723 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2725 GstRTSPClientPrivate *priv;
2728 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2729 priv = client->priv;
2730 g_return_val_if_fail (priv->connection != NULL, 0);
2731 g_return_val_if_fail (priv->watch == NULL, 0);
2733 /* create watch for the connection and attach */
2734 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2735 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2736 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2737 (GDestroyNotify) gst_rtsp_watch_unref);
2739 /* FIXME make this configurable. We don't want to do this yet because it will
2740 * be superceeded by a cache object later */
2741 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2743 GST_INFO ("attaching to context %p", context);
2744 res = gst_rtsp_watch_attach (priv->watch, context);
2750 * gst_rtsp_client_session_filter:
2751 * @client: a #GstRTSPclient
2752 * @func: (scope call): a callback
2753 * @user_data: user data passed to @func
2755 * Call @func for each session managed by @client. The result value of @func
2756 * determines what happens to the session. @func will be called with @client
2757 * locked so no further actions on @client can be performed from @func.
2759 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
2762 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
2764 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
2765 * will also be added with an additional ref to the result #GList of this
2768 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
2769 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2770 * element in the #GList should be unreffed before the list is freed.
2773 gst_rtsp_client_session_filter (GstRTSPClient * client,
2774 GstRTSPClientSessionFilterFunc func, gpointer user_data)
2776 GstRTSPClientPrivate *priv;
2777 GList *result, *walk, *next;
2779 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2780 g_return_val_if_fail (func != NULL, NULL);
2782 priv = client->priv;
2786 g_mutex_lock (&priv->lock);
2787 for (walk = priv->sessions; walk; walk = next) {
2788 GstRTSPSession *sess = walk->data;
2790 next = g_list_next (walk);
2792 switch (func (client, sess, user_data)) {
2793 case GST_RTSP_FILTER_REMOVE:
2794 /* stop watching the session and pretent it went away */
2795 client_cleanup_session (client, sess);
2797 case GST_RTSP_FILTER_REF:
2798 result = g_list_prepend (result, g_object_ref (sess));
2800 case GST_RTSP_FILTER_KEEP:
2805 g_mutex_unlock (&priv->lock);