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 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
476 if (path1 == NULL || path2 == NULL)
479 if (strlen (path1) != len2)
482 if (strncmp (path1, path2, len2))
488 /* this function is called to initially find the media for the DESCRIBE request
489 * but is cached for when the same client (without breaking the connection) is
490 * doing a setup for the exact same url. */
491 static GstRTSPMedia *
492 find_media (GstRTSPClient * client, GstRTSPClientState * state, gint * matched)
494 GstRTSPClientPrivate *priv = client->priv;
495 GstRTSPMediaFactory *factory;
500 if (!priv->mount_points)
501 goto no_mount_points;
503 path = state->uri->abspath;
505 /* find the longest matching factory for the uri first */
506 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
510 state->factory = factory;
512 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
513 goto no_factory_access;
515 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
521 path_len = strlen (path);
523 if (!paths_are_equal (priv->path, path, path_len)) {
524 GstRTSPThread *thread;
526 /* remove any previously cached values before we try to construct a new
532 gst_rtsp_media_unprepare (priv->media);
533 g_object_unref (priv->media);
537 /* prepare the media and add it to the pipeline */
538 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
541 state->media = media;
543 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
544 GST_RTSP_THREAD_TYPE_MEDIA, state);
548 /* prepare the media */
549 if (!(gst_rtsp_media_prepare (media, thread)))
552 /* now keep track of the uri and the media */
553 priv->path = g_strndup (path, path_len);
556 /* we have seen this path before, used cached media */
558 state->media = media;
559 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
562 g_object_unref (factory);
563 state->factory = NULL;
566 g_object_ref (media);
573 GST_ERROR ("client %p: no mount points configured", client);
574 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
579 GST_ERROR ("client %p: no factory for uri %s", client, path);
580 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
585 GST_ERROR ("client %p: not authorized to see factory uri %s", client, path);
590 GST_ERROR ("client %p: not authorized for factory uri %s", client, path);
595 GST_ERROR ("client %p: can't create media", client);
596 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
597 g_object_unref (factory);
598 state->factory = NULL;
603 GST_ERROR ("client %p: can't create thread", client);
604 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
605 g_object_unref (media);
607 g_object_unref (factory);
608 state->factory = NULL;
613 GST_ERROR ("client %p: can't prepare media", client);
614 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
615 g_object_unref (media);
617 g_object_unref (factory);
618 state->factory = NULL;
624 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
626 GstRTSPClientPrivate *priv = client->priv;
627 GstRTSPMessage message = { 0 };
632 gst_rtsp_message_init_data (&message, channel);
634 /* FIXME, need some sort of iovec RTSPMessage here */
635 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
638 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
640 g_mutex_lock (&priv->send_lock);
642 priv->send_func (client, &message, FALSE, priv->send_data);
643 g_mutex_unlock (&priv->send_lock);
645 gst_rtsp_message_steal_body (&message, &data, &usize);
646 gst_buffer_unmap (buffer, &map_info);
648 gst_rtsp_message_unset (&message);
654 link_transport (GstRTSPClient * client, GstRTSPSession * session,
655 GstRTSPStreamTransport * trans)
657 GstRTSPClientPrivate *priv = client->priv;
659 GST_DEBUG ("client %p: linking transport %p", client, trans);
661 gst_rtsp_stream_transport_set_callbacks (trans,
662 (GstRTSPSendFunc) do_send_data,
663 (GstRTSPSendFunc) do_send_data, client, NULL);
665 priv->transports = g_list_prepend (priv->transports, trans);
667 /* make sure our session can't expire */
668 gst_rtsp_session_prevent_expire (session);
672 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
673 GstRTSPStreamTransport * trans)
675 GstRTSPClientPrivate *priv = client->priv;
677 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
679 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
681 priv->transports = g_list_remove (priv->transports, trans);
683 /* our session can now expire */
684 gst_rtsp_session_allow_expire (session);
688 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
689 GstRTSPSessionMedia * sessmedia)
694 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
695 for (i = 0; i < n_streams; i++) {
696 GstRTSPStreamTransport *trans;
697 const GstRTSPTransport *tr;
699 /* get the transport, if there is no transport configured, skip this stream */
700 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
704 tr = gst_rtsp_stream_transport_get_transport (trans);
706 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
707 /* for TCP, unlink the stream from the TCP connection of the client */
708 unlink_transport (client, session, trans);
714 close_connection (GstRTSPClient * client)
716 GstRTSPClientPrivate *priv = client->priv;
717 const gchar *tunnelid;
719 GST_DEBUG ("client %p: closing connection", client);
721 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
722 g_mutex_lock (&tunnels_lock);
723 /* remove from tunnelids */
724 g_hash_table_remove (tunnels, tunnelid);
725 g_mutex_unlock (&tunnels_lock);
728 gst_rtsp_connection_close (priv->connection);
732 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
734 GstRTSPClientPrivate *priv = client->priv;
735 GstRTSPSession *session;
736 GstRTSPSessionMedia *sessmedia;
737 GstRTSPStatusCode code;
744 session = state->session;
749 path = state->uri->abspath;
751 /* get a handle to the configuration of the media in the session */
752 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
756 /* only aggregate control for now.. */
757 if (path[matched] != '\0')
760 state->sessmedia = sessmedia;
762 /* we emit the signal before closing the connection */
763 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
766 /* unlink the all TCP callbacks */
767 unlink_session_transports (client, session, sessmedia);
769 /* remove the session from the watched sessions */
770 client_unwatch_session (client, session);
772 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
774 /* unmanage the media in the session, returns false if all media session
776 if (!gst_rtsp_session_release_media (session, sessmedia)) {
777 /* remove the session */
778 gst_rtsp_session_pool_remove (priv->session_pool, session);
780 /* construct the response now */
781 code = GST_RTSP_STS_OK;
782 gst_rtsp_message_init_response (state->response, code,
783 gst_rtsp_status_as_text (code), state->request);
785 send_message (client, session, state->response, TRUE);
792 GST_ERROR ("client %p: no session", client);
793 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
798 GST_ERROR ("client %p: no uri supplied", client);
799 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
804 GST_ERROR ("client %p: no media for uri", client);
805 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
810 GST_ERROR ("client %p: no aggregate path %s", client, path);
811 send_generic_response (client,
812 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
818 default_params_set (GstRTSPClient * client, GstRTSPClientState * state)
822 res = gst_rtsp_params_set (client, state);
828 default_params_get (GstRTSPClient * client, GstRTSPClientState * state)
832 res = gst_rtsp_params_get (client, state);
838 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
844 res = gst_rtsp_message_get_body (state->request, &data, &size);
845 if (res != GST_RTSP_OK)
849 /* no body, keep-alive request */
850 send_generic_response (client, GST_RTSP_STS_OK, state);
852 /* there is a body, handle the params */
853 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, state);
854 if (res != GST_RTSP_OK)
857 send_message (client, state->session, state->response, FALSE);
860 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
868 GST_ERROR ("client %p: bad request", client);
869 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
875 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
881 res = gst_rtsp_message_get_body (state->request, &data, &size);
882 if (res != GST_RTSP_OK)
886 /* no body, keep-alive request */
887 send_generic_response (client, GST_RTSP_STS_OK, state);
889 /* there is a body, handle the params */
890 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, state);
891 if (res != GST_RTSP_OK)
894 send_message (client, state->session, state->response, FALSE);
897 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
905 GST_ERROR ("client %p: bad request", client);
906 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
912 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
914 GstRTSPSession *session;
915 GstRTSPSessionMedia *sessmedia;
916 GstRTSPStatusCode code;
917 GstRTSPState rtspstate;
921 if (!(session = state->session))
927 path = state->uri->abspath;
929 /* get a handle to the configuration of the media in the session */
930 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
934 if (path[matched] != '\0')
937 state->sessmedia = sessmedia;
939 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
940 /* the session state must be playing or recording */
941 if (rtspstate != GST_RTSP_STATE_PLAYING &&
942 rtspstate != GST_RTSP_STATE_RECORDING)
945 /* unlink the all TCP callbacks */
946 unlink_session_transports (client, session, sessmedia);
948 /* then pause sending */
949 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
951 /* construct the response now */
952 code = GST_RTSP_STS_OK;
953 gst_rtsp_message_init_response (state->response, code,
954 gst_rtsp_status_as_text (code), state->request);
956 send_message (client, session, state->response, FALSE);
958 /* the state is now READY */
959 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
961 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
969 GST_ERROR ("client %p: no seesion", client);
970 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
975 GST_ERROR ("client %p: no uri supplied", client);
976 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
981 GST_ERROR ("client %p: no media for uri", client);
982 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
987 GST_ERROR ("client %p: no aggregate path %s", client, path);
988 send_generic_response (client,
989 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
994 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
995 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1002 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
1004 GstRTSPSession *session;
1005 GstRTSPSessionMedia *sessmedia;
1006 GstRTSPMedia *media;
1007 GstRTSPStatusCode code;
1009 guint n_streams, i, infocount;
1011 GstRTSPTimeRange *range;
1013 GstRTSPState rtspstate;
1014 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1018 if (!(session = state->session))
1024 path = state->uri->abspath;
1026 /* get a handle to the configuration of the media in the session */
1027 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1031 if (path[matched] != '\0')
1034 state->sessmedia = sessmedia;
1035 state->media = media = gst_rtsp_session_media_get_media (sessmedia);
1037 /* the session state must be playing or ready */
1038 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1039 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1042 /* parse the range header if we have one */
1044 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
1045 if (res == GST_RTSP_OK) {
1046 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1047 /* we have a range, seek to the position */
1049 gst_rtsp_media_seek (media, range);
1050 gst_rtsp_range_free (range);
1054 /* grab RTPInfo from the payloaders now */
1055 rtpinfo = g_string_new ("");
1057 n_streams = gst_rtsp_media_n_streams (media);
1058 for (i = 0, infocount = 0; i < n_streams; i++) {
1059 GstRTSPStreamTransport *trans;
1060 GstRTSPStream *stream;
1061 const GstRTSPTransport *tr;
1065 /* get the transport, if there is no transport configured, skip this stream */
1066 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
1067 if (trans == NULL) {
1068 GST_INFO ("stream %d is not configured", i);
1071 tr = gst_rtsp_stream_transport_get_transport (trans);
1073 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1074 /* for TCP, link the stream to the TCP connection of the client */
1075 link_transport (client, session, trans);
1078 stream = gst_rtsp_stream_transport_get_stream (trans);
1079 if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
1081 g_string_append (rtpinfo, ", ");
1083 uristr = gst_rtsp_url_get_request_uri (state->uri);
1084 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
1085 uristr, i, seq, rtptime);
1090 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
1094 /* construct the response now */
1095 code = GST_RTSP_STS_OK;
1096 gst_rtsp_message_init_response (state->response, code,
1097 gst_rtsp_status_as_text (code), state->request);
1099 /* add the RTP-Info header */
1100 if (infocount > 0) {
1101 str = g_string_free (rtpinfo, FALSE);
1102 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
1104 g_string_free (rtpinfo, TRUE);
1108 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1109 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
1111 send_message (client, session, state->response, FALSE);
1113 /* start playing after sending the request */
1114 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1116 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1118 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
1126 GST_ERROR ("client %p: no session", client);
1127 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1132 GST_ERROR ("client %p: no uri supplied", client);
1133 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1138 GST_ERROR ("client %p: media not found", client);
1139 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1144 GST_ERROR ("client %p: no aggregate path %s", client, path);
1145 send_generic_response (client,
1146 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
1151 GST_ERROR ("client %p: not PLAYING or READY", client);
1152 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1159 do_keepalive (GstRTSPSession * session)
1161 GST_INFO ("keep session %p alive", session);
1162 gst_rtsp_session_touch (session);
1165 /* parse @transport and return a valid transport in @tr. only transports
1166 * from @supported are returned. Returns FALSE if no valid transport
1169 parse_transport (const char *transport, GstRTSPLowerTrans supported,
1170 GstRTSPTransport * tr)
1177 gst_rtsp_transport_init (tr);
1179 GST_DEBUG ("parsing transports %s", transport);
1181 transports = g_strsplit (transport, ",", 0);
1183 /* loop through the transports, try to parse */
1184 for (i = 0; transports[i]; i++) {
1185 res = gst_rtsp_transport_parse (transports[i], tr);
1186 if (res != GST_RTSP_OK) {
1187 /* no valid transport, search some more */
1188 GST_WARNING ("could not parse transport %s", transports[i]);
1192 /* we have a transport, see if it's RTP/AVP */
1193 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1194 GST_WARNING ("invalid transport %s", transports[i]);
1198 if (!(tr->lower_transport & supported)) {
1199 GST_WARNING ("unsupported transport %s", transports[i]);
1203 /* we have a valid transport */
1204 GST_INFO ("found valid transport %s", transports[i]);
1209 gst_rtsp_transport_init (tr);
1211 g_strfreev (transports);
1217 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
1218 GstRTSPMessage * request)
1220 gchar *blocksize_str;
1221 gboolean ret = TRUE;
1223 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1224 &blocksize_str, 0) == GST_RTSP_OK) {
1228 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1229 if (end == blocksize_str) {
1230 GST_ERROR ("failed to parse blocksize");
1233 /* we don't want to change the mtu when this media
1234 * can be shared because it impacts other clients */
1235 if (gst_rtsp_media_is_shared (media))
1238 if (blocksize > G_MAXUINT)
1239 blocksize = G_MAXUINT;
1240 gst_rtsp_stream_set_mtu (stream, blocksize);
1247 default_configure_client_transport (GstRTSPClient * client,
1248 GstRTSPClientState * state, GstRTSPTransport * ct)
1250 GstRTSPClientPrivate *priv = client->priv;
1252 /* we have a valid transport now, set the destination of the client. */
1253 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1254 if (ct->destination && priv->use_client_settings) {
1255 GstRTSPAddress *addr;
1257 addr = gst_rtsp_stream_reserve_address (state->stream, ct->destination,
1258 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1263 gst_rtsp_address_free (addr);
1265 GstRTSPAddress *addr;
1266 GSocketFamily family;
1268 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1270 addr = gst_rtsp_stream_get_multicast_address (state->stream, family);
1274 g_free (ct->destination);
1275 ct->destination = g_strdup (addr->address);
1276 ct->port.min = addr->port;
1277 ct->port.max = addr->port + addr->n_ports - 1;
1278 ct->ttl = addr->ttl;
1280 gst_rtsp_address_free (addr);
1285 url = gst_rtsp_connection_get_url (priv->connection);
1286 g_free (ct->destination);
1287 ct->destination = g_strdup (url->host);
1289 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1290 /* check if the client selected channels for TCP */
1291 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1292 gst_rtsp_session_media_alloc_channels (state->sessmedia,
1302 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1307 static GstRTSPTransport *
1308 make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
1309 GstRTSPTransport * ct)
1311 GstRTSPTransport *st;
1313 GSocketFamily family;
1315 /* prepare the server transport */
1316 gst_rtsp_transport_new (&st);
1318 st->trans = ct->trans;
1319 st->profile = ct->profile;
1320 st->lower_transport = ct->lower_transport;
1322 addr = g_inet_address_new_from_string (ct->destination);
1325 GST_ERROR ("failed to get inet addr from client destination");
1326 family = G_SOCKET_FAMILY_IPV4;
1328 family = g_inet_address_get_family (addr);
1329 g_object_unref (addr);
1333 switch (st->lower_transport) {
1334 case GST_RTSP_LOWER_TRANS_UDP:
1335 st->client_port = ct->client_port;
1336 gst_rtsp_stream_get_server_port (state->stream, &st->server_port, family);
1338 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1339 st->port = ct->port;
1340 st->destination = g_strdup (ct->destination);
1343 case GST_RTSP_LOWER_TRANS_TCP:
1344 st->interleaved = ct->interleaved;
1349 gst_rtsp_stream_get_ssrc (state->stream, &st->ssrc);
1355 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
1357 GstRTSPClientPrivate *priv = client->priv;
1361 GstRTSPTransport *ct, *st;
1362 GstRTSPLowerTrans supported;
1363 GstRTSPStatusCode code;
1364 GstRTSPSession *session;
1365 GstRTSPStreamTransport *trans;
1367 GstRTSPSessionMedia *sessmedia;
1368 GstRTSPMedia *media;
1369 GstRTSPStream *stream;
1370 GstRTSPState rtspstate;
1371 GstRTSPClientClass *klass;
1372 gchar *path, *control;
1379 path = uri->abspath;
1381 /* parse the transport */
1383 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
1385 if (res != GST_RTSP_OK)
1388 /* we create the session after parsing stuff so that we don't make
1389 * a session for malformed requests */
1390 if (priv->session_pool == NULL)
1393 session = state->session;
1396 g_object_ref (session);
1397 /* get a handle to the configuration of the media in the session, this can
1398 * return NULL if this is a new url to manage in this session. */
1399 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1401 /* we need a new media configuration in this session */
1405 /* we have no session media, find one and manage it */
1406 if (sessmedia == NULL) {
1407 /* get a handle to the configuration of the media in the session */
1408 media = find_media (client, state, &matched);
1410 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1411 g_object_ref (media);
1413 /* no media, not found then */
1415 goto media_not_found;
1417 /* path is what matched. We can modify the parsed uri in place */
1418 path[matched] = '\0';
1419 /* control is remainder */
1420 control = &path[matched + 1];
1422 /* find the stream now using the control part */
1423 stream = gst_rtsp_media_find_stream (media, control);
1425 goto stream_not_found;
1427 /* now we have a uri identifying a valid media and stream */
1428 state->stream = stream;
1429 state->media = media;
1431 if (session == NULL) {
1432 /* create a session if this fails we probably reached our session limit or
1434 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1435 goto service_unavailable;
1437 /* make sure this client is closed when the session is closed */
1438 client_watch_session (client, session);
1440 /* signal new session */
1441 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1444 state->session = session;
1447 if (sessmedia == NULL) {
1448 /* manage the media in our session now, if not done already */
1449 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1450 /* if we stil have no media, error */
1451 if (sessmedia == NULL)
1452 goto sessmedia_unavailable;
1454 g_object_unref (media);
1457 state->sessmedia = sessmedia;
1459 /* set blocksize on this stream */
1460 if (!handle_blocksize (media, stream, state->request))
1461 goto invalid_blocksize;
1463 gst_rtsp_transport_new (&ct);
1465 /* our supported transports */
1466 supported = GST_RTSP_LOWER_TRANS_UDP |
1467 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
1469 /* parse and find a usable supported transport */
1470 if (!parse_transport (transport, supported, ct))
1471 goto unsupported_transports;
1473 /* update the client transport */
1474 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1475 if (!klass->configure_client_transport (client, state, ct))
1476 goto unsupported_client_transport;
1478 /* set in the session media transport */
1479 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1481 /* configure keepalive for this transport */
1482 gst_rtsp_stream_transport_set_keepalive (trans,
1483 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1485 /* create and serialize the server transport */
1486 st = make_server_transport (client, state, ct);
1487 trans_str = gst_rtsp_transport_as_text (st);
1488 gst_rtsp_transport_free (st);
1490 /* construct the response now */
1491 code = GST_RTSP_STS_OK;
1492 gst_rtsp_message_init_response (state->response, code,
1493 gst_rtsp_status_as_text (code), state->request);
1495 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1499 send_message (client, session, state->response, FALSE);
1501 /* update the state */
1502 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1503 switch (rtspstate) {
1504 case GST_RTSP_STATE_PLAYING:
1505 case GST_RTSP_STATE_RECORDING:
1506 case GST_RTSP_STATE_READY:
1507 /* no state change */
1510 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1513 g_object_unref (session);
1515 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST],
1523 GST_ERROR ("client %p: no uri", client);
1524 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1529 GST_ERROR ("client %p: no transport", client);
1530 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1535 GST_ERROR ("client %p: no session pool configured", client);
1536 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1541 GST_ERROR ("client %p: media '%s' not found", client, path);
1542 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1547 GST_ERROR ("client %p: stream '%s' not found", client, control);
1548 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1549 g_object_unref (media);
1552 service_unavailable:
1554 GST_ERROR ("client %p: can't create session", client);
1555 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1556 g_object_unref (media);
1559 sessmedia_unavailable:
1561 GST_ERROR ("client %p: can't create session media", client);
1562 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1563 g_object_unref (media);
1564 g_object_unref (session);
1569 GST_ERROR ("client %p: invalid blocksize", client);
1570 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1571 g_object_unref (session);
1574 unsupported_transports:
1576 GST_ERROR ("client %p: unsupported transports", client);
1577 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1578 gst_rtsp_transport_free (ct);
1579 g_object_unref (session);
1582 unsupported_client_transport:
1584 GST_ERROR ("client %p: unsupported client transport", client);
1585 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1586 gst_rtsp_transport_free (ct);
1587 g_object_unref (session);
1592 static GstSDPMessage *
1593 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1595 GstRTSPClientPrivate *priv = client->priv;
1600 gst_sdp_message_new (&sdp);
1602 /* some standard things first */
1603 gst_sdp_message_set_version (sdp, "0");
1610 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1613 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1614 gst_sdp_message_set_information (sdp, "rtsp-server");
1615 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1616 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1617 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1618 gst_sdp_message_add_attribute (sdp, "control", "*");
1620 info.is_ipv6 = priv->is_ipv6;
1621 info.server_ip = priv->server_ip;
1623 /* create an SDP for the media object */
1624 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1632 GST_ERROR ("client %p: could not create SDP", client);
1633 gst_sdp_message_free (sdp);
1638 /* for the describe we must generate an SDP */
1640 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1645 gchar *str, *content_base;
1646 GstRTSPMedia *media;
1647 GstRTSPClientClass *klass;
1649 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1654 /* check what kind of format is accepted, we don't really do anything with it
1655 * and always return SDP for now. */
1660 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1662 if (res == GST_RTSP_ENOTIMPL)
1665 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1669 /* find the media object for the uri */
1670 if (!(media = find_media (client, state, NULL)))
1673 /* create an SDP for the media object on this client */
1674 if (!(sdp = klass->create_sdp (client, media)))
1677 g_object_unref (media);
1679 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1680 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1682 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1685 /* content base for some clients that might screw up creating the setup uri */
1686 str = gst_rtsp_url_get_request_uri (state->uri);
1687 str_len = strlen (str);
1689 /* check for trailing '/' and append one */
1690 if (str[str_len - 1] != '/') {
1691 content_base = g_malloc (str_len + 2);
1692 memcpy (content_base, str, str_len);
1693 content_base[str_len] = '/';
1694 content_base[str_len + 1] = '\0';
1700 GST_INFO ("adding content-base: %s", content_base);
1702 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1704 g_free (content_base);
1706 /* add SDP to the response body */
1707 str = gst_sdp_message_as_text (sdp);
1708 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1709 gst_sdp_message_free (sdp);
1711 send_message (client, state->session, state->response, FALSE);
1713 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1721 GST_ERROR ("client %p: no uri", client);
1722 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1727 GST_ERROR ("client %p: no media", client);
1728 /* error reply is already sent */
1733 GST_ERROR ("client %p: can't create SDP", client);
1734 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1735 g_object_unref (media);
1741 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1743 GstRTSPMethod options;
1746 options = GST_RTSP_DESCRIBE |
1751 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1753 str = gst_rtsp_options_as_text (options);
1755 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1756 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1758 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1761 send_message (client, state->session, state->response, FALSE);
1763 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1769 /* remove duplicate and trailing '/' */
1771 sanitize_uri (GstRTSPUrl * uri)
1775 gboolean have_slash, prev_slash;
1777 s = d = uri->abspath;
1778 len = strlen (uri->abspath);
1782 for (i = 0; i < len; i++) {
1783 have_slash = s[i] == '/';
1785 if (!have_slash || !prev_slash)
1787 prev_slash = have_slash;
1789 len = d - uri->abspath;
1790 /* don't remove the first slash if that's the only thing left */
1791 if (len > 1 && *(d - 1) == '/')
1797 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1799 GstRTSPClientPrivate *priv = client->priv;
1801 GST_INFO ("client %p: session %p finished", client, session);
1803 /* unlink all media managed in this session */
1804 client_unlink_session (client, session);
1806 /* remove the session */
1807 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1808 GST_INFO ("client %p: all sessions finalized, close the connection",
1810 close_connection (client);
1814 static GPrivate current_state;
1817 * gst_rtsp_client_state_get_current:
1819 * Get the current #GstRTSPClientState. This object is retrieved from the
1820 * current thread that is handling the request for a client.
1822 * Returns: a #GstRTSPClientState
1824 GstRTSPClientState *
1825 gst_rtsp_client_state_get_current (void)
1829 l = g_private_get (¤t_state);
1833 return (GstRTSPClientState *) (l->data);
1838 * gst_rtsp_client_state_push_current:
1839 * @state: a ##GstRTSPClientState
1841 * Pushes @state onto the state stack. The current
1842 * state can then be received using gst_rtsp_client_state_get_current().
1845 gst_rtsp_client_state_push_current (GstRTSPClientState * state)
1849 g_return_if_fail (state != NULL);
1851 l = g_private_get (¤t_state);
1852 l = g_slist_prepend (l, state);
1853 g_private_set (¤t_state, l);
1857 * gst_rtsp_client_state_pop_current:
1858 * @state: a #GstRTSPClientState
1860 * Pops @state off the state stack (verifying that @state
1861 * is on the top of the stack).
1864 gst_rtsp_client_state_pop_current (GstRTSPClientState * state)
1868 l = g_private_get (¤t_state);
1870 g_return_if_fail (l != NULL);
1871 g_return_if_fail (l->data == state);
1873 l = g_slist_delete_link (l, l);
1874 g_private_set (¤t_state, l);
1878 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1880 GstRTSPClientPrivate *priv = client->priv;
1881 GstRTSPMethod method;
1882 const gchar *uristr;
1883 GstRTSPUrl *uri = NULL;
1884 GstRTSPVersion version;
1886 GstRTSPSession *session = NULL;
1887 GstRTSPClientState state = { NULL };
1888 GstRTSPMessage response = { 0 };
1891 state.conn = priv->connection;
1892 state.client = client;
1893 state.request = request;
1894 state.response = &response;
1895 state.auth = priv->auth;
1896 gst_rtsp_client_state_push_current (&state);
1898 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1899 gst_rtsp_message_dump (request);
1902 GST_INFO ("client %p: received a request", client);
1904 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1906 /* we can only handle 1.0 requests */
1907 if (version != GST_RTSP_VERSION_1_0)
1910 state.method = method;
1912 /* we always try to parse the url first */
1913 if (strcmp (uristr, "*") == 0) {
1914 /* special case where we have * as uri, keep uri = NULL */
1915 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1918 /* get the session if there is any */
1919 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1920 if (res == GST_RTSP_OK) {
1921 if (priv->session_pool == NULL)
1924 /* we had a session in the request, find it again */
1925 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1926 goto session_not_found;
1928 /* we add the session to the client list of watched sessions. When a session
1929 * disappears because it times out, we will be notified. If all sessions are
1930 * gone, we will close the connection */
1931 client_watch_session (client, session);
1934 /* sanitize the uri */
1938 state.session = session;
1940 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
1941 goto not_authorized;
1943 /* now see what is asked and dispatch to a dedicated handler */
1945 case GST_RTSP_OPTIONS:
1946 handle_options_request (client, &state);
1948 case GST_RTSP_DESCRIBE:
1949 handle_describe_request (client, &state);
1951 case GST_RTSP_SETUP:
1952 handle_setup_request (client, &state);
1955 handle_play_request (client, &state);
1957 case GST_RTSP_PAUSE:
1958 handle_pause_request (client, &state);
1960 case GST_RTSP_TEARDOWN:
1961 handle_teardown_request (client, &state);
1963 case GST_RTSP_SET_PARAMETER:
1964 handle_set_param_request (client, &state);
1966 case GST_RTSP_GET_PARAMETER:
1967 handle_get_param_request (client, &state);
1969 case GST_RTSP_ANNOUNCE:
1970 case GST_RTSP_RECORD:
1971 case GST_RTSP_REDIRECT:
1972 goto not_implemented;
1973 case GST_RTSP_INVALID:
1979 gst_rtsp_client_state_pop_current (&state);
1981 g_object_unref (session);
1983 gst_rtsp_url_free (uri);
1989 GST_ERROR ("client %p: version %d not supported", client, version);
1990 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1996 GST_ERROR ("client %p: bad request", client);
1997 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
2002 GST_ERROR ("client %p: no pool configured", client);
2003 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
2008 GST_ERROR ("client %p: session not found", client);
2009 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
2014 GST_ERROR ("client %p: not allowed", client);
2019 GST_ERROR ("client %p: method %d not implemented", client, method);
2020 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
2026 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2028 GstRTSPClientPrivate *priv = client->priv;
2037 /* find the stream for this message */
2038 res = gst_rtsp_message_parse_data (message, &channel);
2039 if (res != GST_RTSP_OK)
2042 gst_rtsp_message_steal_body (message, &data, &size);
2044 buffer = gst_buffer_new_wrapped (data, size);
2047 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2048 GstRTSPStreamTransport *trans;
2049 GstRTSPStream *stream;
2050 const GstRTSPTransport *tr;
2054 tr = gst_rtsp_stream_transport_get_transport (trans);
2055 stream = gst_rtsp_stream_transport_get_stream (trans);
2057 /* check for TCP transport */
2058 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2059 /* dispatch to the stream based on the channel number */
2060 if (tr->interleaved.min == channel) {
2061 gst_rtsp_stream_recv_rtp (stream, buffer);
2064 } else if (tr->interleaved.max == channel) {
2065 gst_rtsp_stream_recv_rtcp (stream, buffer);
2072 gst_buffer_unref (buffer);
2076 * gst_rtsp_client_set_session_pool:
2077 * @client: a #GstRTSPClient
2078 * @pool: a #GstRTSPSessionPool
2080 * Set @pool as the sessionpool for @client which it will use to find
2081 * or allocate sessions. the sessionpool is usually inherited from the server
2082 * that created the client but can be overridden later.
2085 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2086 GstRTSPSessionPool * pool)
2088 GstRTSPSessionPool *old;
2089 GstRTSPClientPrivate *priv;
2091 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2093 priv = client->priv;
2096 g_object_ref (pool);
2098 g_mutex_lock (&priv->lock);
2099 old = priv->session_pool;
2100 priv->session_pool = pool;
2101 g_mutex_unlock (&priv->lock);
2104 g_object_unref (old);
2108 * gst_rtsp_client_get_session_pool:
2109 * @client: a #GstRTSPClient
2111 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2113 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2115 GstRTSPSessionPool *
2116 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2118 GstRTSPClientPrivate *priv;
2119 GstRTSPSessionPool *result;
2121 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2123 priv = client->priv;
2125 g_mutex_lock (&priv->lock);
2126 if ((result = priv->session_pool))
2127 g_object_ref (result);
2128 g_mutex_unlock (&priv->lock);
2134 * gst_rtsp_client_set_mount_points:
2135 * @client: a #GstRTSPClient
2136 * @mounts: a #GstRTSPMountPoints
2138 * Set @mounts as the mount points for @client which it will use to map urls
2139 * to media streams. These mount points are usually inherited from the server that
2140 * created the client but can be overriden later.
2143 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2144 GstRTSPMountPoints * mounts)
2146 GstRTSPClientPrivate *priv;
2147 GstRTSPMountPoints *old;
2149 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2151 priv = client->priv;
2154 g_object_ref (mounts);
2156 g_mutex_lock (&priv->lock);
2157 old = priv->mount_points;
2158 priv->mount_points = mounts;
2159 g_mutex_unlock (&priv->lock);
2162 g_object_unref (old);
2166 * gst_rtsp_client_get_mount_points:
2167 * @client: a #GstRTSPClient
2169 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2171 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2173 GstRTSPMountPoints *
2174 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2176 GstRTSPClientPrivate *priv;
2177 GstRTSPMountPoints *result;
2179 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2181 priv = client->priv;
2183 g_mutex_lock (&priv->lock);
2184 if ((result = priv->mount_points))
2185 g_object_ref (result);
2186 g_mutex_unlock (&priv->lock);
2192 * gst_rtsp_client_set_use_client_settings:
2193 * @client: a #GstRTSPClient
2194 * @use_client_settings: whether to use client settings for multicast
2196 * Use client transport settings (destination and ttl) for multicast.
2197 * When @use_client_settings is %FALSE, the server settings will be
2201 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
2202 gboolean use_client_settings)
2204 GstRTSPClientPrivate *priv;
2206 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2208 priv = client->priv;
2210 g_mutex_lock (&priv->lock);
2211 priv->use_client_settings = use_client_settings;
2212 g_mutex_unlock (&priv->lock);
2216 * gst_rtsp_client_get_use_client_settings:
2217 * @client: a #GstRTSPClient
2219 * Check if client transport settings (destination and ttl) for multicast
2223 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
2225 GstRTSPClientPrivate *priv;
2228 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2230 priv = client->priv;
2232 g_mutex_lock (&priv->lock);
2233 res = priv->use_client_settings;
2234 g_mutex_unlock (&priv->lock);
2240 * gst_rtsp_client_set_auth:
2241 * @client: a #GstRTSPClient
2242 * @auth: a #GstRTSPAuth
2244 * configure @auth to be used as the authentication manager of @client.
2247 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2249 GstRTSPClientPrivate *priv;
2252 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2254 priv = client->priv;
2257 g_object_ref (auth);
2259 g_mutex_lock (&priv->lock);
2262 g_mutex_unlock (&priv->lock);
2265 g_object_unref (old);
2270 * gst_rtsp_client_get_auth:
2271 * @client: a #GstRTSPClient
2273 * Get the #GstRTSPAuth used as the authentication manager of @client.
2275 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2279 gst_rtsp_client_get_auth (GstRTSPClient * client)
2281 GstRTSPClientPrivate *priv;
2282 GstRTSPAuth *result;
2284 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2286 priv = client->priv;
2288 g_mutex_lock (&priv->lock);
2289 if ((result = priv->auth))
2290 g_object_ref (result);
2291 g_mutex_unlock (&priv->lock);
2297 * gst_rtsp_client_set_thread_pool:
2298 * @client: a #GstRTSPClient
2299 * @pool: a #GstRTSPThreadPool
2301 * configure @pool to be used as the thread pool of @client.
2304 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2305 GstRTSPThreadPool * pool)
2307 GstRTSPClientPrivate *priv;
2308 GstRTSPThreadPool *old;
2310 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2312 priv = client->priv;
2315 g_object_ref (pool);
2317 g_mutex_lock (&priv->lock);
2318 old = priv->thread_pool;
2319 priv->thread_pool = pool;
2320 g_mutex_unlock (&priv->lock);
2323 g_object_unref (old);
2327 * gst_rtsp_client_get_thread_pool:
2328 * @client: a #GstRTSPClient
2330 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2332 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2336 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2338 GstRTSPClientPrivate *priv;
2339 GstRTSPThreadPool *result;
2341 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2343 priv = client->priv;
2345 g_mutex_lock (&priv->lock);
2346 if ((result = priv->thread_pool))
2347 g_object_ref (result);
2348 g_mutex_unlock (&priv->lock);
2354 * gst_rtsp_client_set_connection:
2355 * @client: a #GstRTSPClient
2356 * @conn: (transfer full): a #GstRTSPConnection
2358 * Set the #GstRTSPConnection of @client. This function takes ownership of
2361 * Returns: %TRUE on success.
2364 gst_rtsp_client_set_connection (GstRTSPClient * client,
2365 GstRTSPConnection * conn)
2367 GstRTSPClientPrivate *priv;
2368 GSocket *read_socket;
2369 GSocketAddress *address;
2371 GError *error = NULL;
2373 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2374 g_return_val_if_fail (conn != NULL, FALSE);
2376 priv = client->priv;
2378 read_socket = gst_rtsp_connection_get_read_socket (conn);
2380 if (!(address = g_socket_get_local_address (read_socket, &error)))
2383 g_free (priv->server_ip);
2384 /* keep the original ip that the client connected to */
2385 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2386 GInetAddress *iaddr;
2388 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2390 /* socket might be ipv6 but adress still ipv4 */
2391 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2392 priv->server_ip = g_inet_address_to_string (iaddr);
2393 g_object_unref (address);
2395 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2396 priv->server_ip = g_strdup ("unknown");
2399 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2400 priv->server_ip, priv->is_ipv6);
2402 url = gst_rtsp_connection_get_url (conn);
2403 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2405 priv->connection = conn;
2412 GST_ERROR ("could not get local address %s", error->message);
2413 g_error_free (error);
2419 * gst_rtsp_client_get_connection:
2420 * @client: a #GstRTSPClient
2422 * Get the #GstRTSPConnection of @client.
2424 * Returns: (transfer none): the #GstRTSPConnection of @client.
2425 * The connection object returned remains valid until the client is freed.
2428 gst_rtsp_client_get_connection (GstRTSPClient * client)
2430 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2432 return client->priv->connection;
2436 * gst_rtsp_client_set_send_func:
2437 * @client: a #GstRTSPClient
2438 * @func: a #GstRTSPClientSendFunc
2439 * @user_data: user data passed to @func
2440 * @notify: called when @user_data is no longer in use
2442 * Set @func as the callback that will be called when a new message needs to be
2443 * sent to the client. @user_data is passed to @func and @notify is called when
2444 * @user_data is no longer in use.
2446 * By default, the client will send the messages on the #GstRTSPConnection that
2447 * was configured with gst_rtsp_client_attach() was called.
2450 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2451 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2453 GstRTSPClientPrivate *priv;
2454 GDestroyNotify old_notify;
2457 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2459 priv = client->priv;
2461 g_mutex_lock (&priv->send_lock);
2462 priv->send_func = func;
2463 old_notify = priv->send_notify;
2464 old_data = priv->send_data;
2465 priv->send_notify = notify;
2466 priv->send_data = user_data;
2467 g_mutex_unlock (&priv->send_lock);
2470 old_notify (old_data);
2474 * gst_rtsp_client_handle_message:
2475 * @client: a #GstRTSPClient
2476 * @message: an #GstRTSPMessage
2478 * Let the client handle @message.
2480 * Returns: a #GstRTSPResult.
2483 gst_rtsp_client_handle_message (GstRTSPClient * client,
2484 GstRTSPMessage * message)
2486 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2487 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2489 switch (message->type) {
2490 case GST_RTSP_MESSAGE_REQUEST:
2491 handle_request (client, message);
2493 case GST_RTSP_MESSAGE_RESPONSE:
2495 case GST_RTSP_MESSAGE_DATA:
2496 handle_data (client, message);
2505 * gst_rtsp_client_send_message:
2506 * @client: a #GstRTSPClient
2507 * @session: a #GstRTSPSession to send the message to or %NULL
2508 * @message: The #GstRTSPMessage to send
2510 * Send a message message to the remote end. @message must be a
2511 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
2514 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
2515 GstRTSPMessage * message)
2517 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2518 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2519 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
2520 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
2522 send_message (client, session, message, FALSE);
2527 static GstRTSPResult
2528 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2529 gboolean close, gpointer user_data)
2531 GstRTSPClientPrivate *priv = client->priv;
2533 /* send the response and store the seq number so we can wait until it's
2534 * written to the client to close the connection */
2535 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2536 &priv->close_seq : NULL);
2539 static GstRTSPResult
2540 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2543 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2546 static GstRTSPResult
2547 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2549 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2550 GstRTSPClientPrivate *priv = client->priv;
2552 if (priv->close_seq && priv->close_seq == cseq) {
2553 priv->close_seq = 0;
2554 close_connection (client);
2560 static GstRTSPResult
2561 closed (GstRTSPWatch * watch, gpointer user_data)
2563 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2564 GstRTSPClientPrivate *priv = client->priv;
2565 const gchar *tunnelid;
2567 GST_INFO ("client %p: connection closed", client);
2569 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2570 g_mutex_lock (&tunnels_lock);
2571 /* remove from tunnelids */
2572 g_hash_table_remove (tunnels, tunnelid);
2573 g_mutex_unlock (&tunnels_lock);
2576 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2581 static GstRTSPResult
2582 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2584 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2587 str = gst_rtsp_strresult (result);
2588 GST_INFO ("client %p: received an error %s", client, str);
2594 static GstRTSPResult
2595 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2596 GstRTSPMessage * message, guint id, gpointer user_data)
2598 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2601 str = gst_rtsp_strresult (result);
2603 ("client %p: error when handling message %p with id %d: %s",
2604 client, message, id, str);
2611 remember_tunnel (GstRTSPClient * client)
2613 GstRTSPClientPrivate *priv = client->priv;
2614 const gchar *tunnelid;
2616 /* store client in the pending tunnels */
2617 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2618 if (tunnelid == NULL)
2621 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2623 /* we can't have two clients connecting with the same tunnelid */
2624 g_mutex_lock (&tunnels_lock);
2625 if (g_hash_table_lookup (tunnels, tunnelid))
2626 goto tunnel_existed;
2628 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2629 g_mutex_unlock (&tunnels_lock);
2636 GST_ERROR ("client %p: no tunnelid provided", client);
2641 g_mutex_unlock (&tunnels_lock);
2642 GST_ERROR ("client %p: tunnel session %s already existed", client,
2648 static GstRTSPStatusCode
2649 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2651 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2652 GstRTSPClientPrivate *priv = client->priv;
2654 GST_INFO ("client %p: tunnel start (connection %p)", client,
2657 if (!remember_tunnel (client))
2660 return GST_RTSP_STS_OK;
2665 GST_ERROR ("client %p: error starting tunnel", client);
2666 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2670 static GstRTSPResult
2671 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2673 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2674 GstRTSPClientPrivate *priv = client->priv;
2676 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2679 /* ignore error, it'll only be a problem when the client does a POST again */
2680 remember_tunnel (client);
2685 static GstRTSPResult
2686 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2688 const gchar *tunnelid;
2689 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2690 GstRTSPClientPrivate *priv = client->priv;
2691 GstRTSPClient *oclient;
2692 GstRTSPClientPrivate *opriv;
2694 GST_INFO ("client %p: tunnel complete", client);
2696 /* find previous tunnel */
2697 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2698 if (tunnelid == NULL)
2701 g_mutex_lock (&tunnels_lock);
2702 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2705 /* remove the old client from the table. ref before because removing it will
2706 * remove the ref to it. */
2707 g_object_ref (oclient);
2708 g_hash_table_remove (tunnels, tunnelid);
2710 opriv = oclient->priv;
2712 if (opriv->watch == NULL)
2714 g_mutex_unlock (&tunnels_lock);
2716 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2717 opriv->connection, priv->connection);
2719 /* merge the tunnels into the first client */
2720 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2721 gst_rtsp_watch_reset (opriv->watch);
2722 g_object_unref (oclient);
2729 GST_ERROR ("client %p: no tunnelid provided", client);
2730 return GST_RTSP_ERROR;
2734 g_mutex_unlock (&tunnels_lock);
2735 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2736 return GST_RTSP_ERROR;
2740 g_mutex_unlock (&tunnels_lock);
2741 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2742 g_object_unref (oclient);
2743 return GST_RTSP_ERROR;
2747 static GstRTSPWatchFuncs watch_funcs = {
2759 client_watch_notify (GstRTSPClient * client)
2761 GstRTSPClientPrivate *priv = client->priv;
2763 GST_INFO ("client %p: watch destroyed", client);
2765 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2766 g_object_unref (client);
2770 * gst_rtsp_client_attach:
2771 * @client: a #GstRTSPClient
2772 * @context: (allow-none): a #GMainContext
2774 * Attaches @client to @context. When the mainloop for @context is run, the
2775 * client will be dispatched. When @context is NULL, the default context will be
2778 * This function should be called when the client properties and urls are fully
2779 * configured and the client is ready to start.
2781 * Returns: the ID (greater than 0) for the source within the GMainContext.
2784 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2786 GstRTSPClientPrivate *priv;
2789 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2790 priv = client->priv;
2791 g_return_val_if_fail (priv->connection != NULL, 0);
2792 g_return_val_if_fail (priv->watch == NULL, 0);
2794 /* create watch for the connection and attach */
2795 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2796 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2797 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2798 (GDestroyNotify) gst_rtsp_watch_unref);
2800 /* FIXME make this configurable. We don't want to do this yet because it will
2801 * be superceeded by a cache object later */
2802 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2804 GST_INFO ("attaching to context %p", context);
2805 res = gst_rtsp_watch_attach (priv->watch, context);
2811 * gst_rtsp_client_session_filter:
2812 * @client: a #GstRTSPclient
2813 * @func: (scope call): a callback
2814 * @user_data: user data passed to @func
2816 * Call @func for each session managed by @client. The result value of @func
2817 * determines what happens to the session. @func will be called with @client
2818 * locked so no further actions on @client can be performed from @func.
2820 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
2823 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
2825 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
2826 * will also be added with an additional ref to the result #GList of this
2829 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
2830 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2831 * element in the #GList should be unreffed before the list is freed.
2834 gst_rtsp_client_session_filter (GstRTSPClient * client,
2835 GstRTSPClientSessionFilterFunc func, gpointer user_data)
2837 GstRTSPClientPrivate *priv;
2838 GList *result, *walk, *next;
2840 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2841 g_return_val_if_fail (func != NULL, NULL);
2843 priv = client->priv;
2847 g_mutex_lock (&priv->lock);
2848 for (walk = priv->sessions; walk; walk = next) {
2849 GstRTSPSession *sess = walk->data;
2851 next = g_list_next (walk);
2853 switch (func (client, sess, user_data)) {
2854 case GST_RTSP_FILTER_REMOVE:
2855 /* stop watching the session and pretent it went away */
2856 client_cleanup_session (client, sess);
2858 case GST_RTSP_FILTER_REF:
2859 result = g_list_prepend (result, g_object_ref (sess));
2861 case GST_RTSP_FILTER_KEEP:
2866 g_mutex_unlock (&priv->lock);