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.
23 #include "rtsp-client.h"
25 #include "rtsp-params.h"
27 #define GST_RTSP_CLIENT_GET_PRIVATE(obj) \
28 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_CLIENT, GstRTSPClientPrivate))
31 * send_lock, lock, tunnels_lock
34 struct _GstRTSPClientPrivate
36 GMutex lock; /* protects everything else */
38 GstRTSPConnection *connection;
43 gboolean use_client_settings;
45 GstRTSPClientSendFunc send_func; /* protected by send_lock */
46 gpointer send_data; /* protected by send_lock */
47 GDestroyNotify send_notify; /* protected by send_lock */
49 GstRTSPSessionPool *session_pool;
50 GstRTSPMountPoints *mount_points;
53 /* used to cache the media in the last requested DESCRIBE so that
54 * we can pick it up in the next SETUP immediately */
62 static GMutex tunnels_lock;
63 static GHashTable *tunnels; /* protected by tunnels_lock */
65 #define DEFAULT_SESSION_POOL NULL
66 #define DEFAULT_MOUNT_POINTS NULL
67 #define DEFAULT_USE_CLIENT_SETTINGS FALSE
74 PROP_USE_CLIENT_SETTINGS,
82 SIGNAL_OPTIONS_REQUEST,
83 SIGNAL_DESCRIBE_REQUEST,
87 SIGNAL_TEARDOWN_REQUEST,
88 SIGNAL_SET_PARAMETER_REQUEST,
89 SIGNAL_GET_PARAMETER_REQUEST,
93 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
94 #define GST_CAT_DEFAULT rtsp_client_debug
96 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
98 static void gst_rtsp_client_get_property (GObject * object, guint propid,
99 GValue * value, GParamSpec * pspec);
100 static void gst_rtsp_client_set_property (GObject * object, guint propid,
101 const GValue * value, GParamSpec * pspec);
102 static void gst_rtsp_client_finalize (GObject * obj);
104 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
105 static void client_session_finalized (GstRTSPClient * client,
106 GstRTSPSession * session);
107 static void unlink_session_transports (GstRTSPClient * client,
108 GstRTSPSession * session, GstRTSPSessionMedia * sessmedia);
109 static gboolean default_configure_client_transport (GstRTSPClient * client,
110 GstRTSPClientState * state, GstRTSPTransport * ct);
111 static GstRTSPResult default_params_set (GstRTSPClient * client,
112 GstRTSPClientState * state);
113 static GstRTSPResult default_params_get (GstRTSPClient * client,
114 GstRTSPClientState * state);
116 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
119 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
121 GObjectClass *gobject_class;
123 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
125 gobject_class = G_OBJECT_CLASS (klass);
127 gobject_class->get_property = gst_rtsp_client_get_property;
128 gobject_class->set_property = gst_rtsp_client_set_property;
129 gobject_class->finalize = gst_rtsp_client_finalize;
131 klass->create_sdp = create_sdp;
132 klass->configure_client_transport = default_configure_client_transport;
133 klass->params_set = default_params_set;
134 klass->params_get = default_params_get;
136 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
137 g_param_spec_object ("session-pool", "Session Pool",
138 "The session pool to use for client session",
139 GST_TYPE_RTSP_SESSION_POOL,
140 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
142 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
143 g_param_spec_object ("mount-points", "Mount Points",
144 "The mount points to use for client session",
145 GST_TYPE_RTSP_MOUNT_POINTS,
146 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
148 g_object_class_install_property (gobject_class, PROP_USE_CLIENT_SETTINGS,
149 g_param_spec_boolean ("use-client-settings", "Use Client Settings",
150 "Use client settings for ttl and destination in multicast",
151 DEFAULT_USE_CLIENT_SETTINGS,
152 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
154 gst_rtsp_client_signals[SIGNAL_CLOSED] =
155 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
156 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
157 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
159 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
160 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
161 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
162 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
164 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
165 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
166 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
167 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
170 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
171 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
172 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
173 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
176 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
177 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
178 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
179 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
182 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
183 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
184 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
185 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
188 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
189 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
190 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
191 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
194 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
195 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
196 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
197 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
200 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
201 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
202 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
203 set_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
204 G_TYPE_NONE, 1, G_TYPE_POINTER);
206 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
207 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
208 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
209 get_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
210 G_TYPE_NONE, 1, G_TYPE_POINTER);
213 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
214 g_mutex_init (&tunnels_lock);
216 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
220 gst_rtsp_client_init (GstRTSPClient * client)
222 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
226 g_mutex_init (&priv->lock);
227 g_mutex_init (&priv->send_lock);
228 priv->use_client_settings = DEFAULT_USE_CLIENT_SETTINGS;
232 static GstRTSPFilterResult
233 filter_session (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
236 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
238 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
239 unlink_session_transports (client, sess, sessmedia);
241 /* unmanage the media in the session */
242 return GST_RTSP_FILTER_REMOVE;
246 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
248 /* unlink all media managed in this session */
249 gst_rtsp_session_filter (session, filter_session, client);
253 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
255 GstRTSPClientPrivate *priv = client->priv;
258 for (walk = priv->sessions; walk; walk = g_list_next (walk)) {
259 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
261 /* we already know about this session */
262 if (msession == session)
266 GST_INFO ("watching session %p", session);
268 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
270 priv->sessions = g_list_prepend (priv->sessions, session);
274 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session)
276 GstRTSPClientPrivate *priv = client->priv;
278 GST_INFO ("unwatching session %p", session);
280 g_object_weak_unref (G_OBJECT (session),
281 (GWeakNotify) client_session_finalized, client);
282 priv->sessions = g_list_remove (priv->sessions, session);
286 client_cleanup_session (GstRTSPClient * client, GstRTSPSession * session)
288 g_object_weak_unref (G_OBJECT (session),
289 (GWeakNotify) client_session_finalized, client);
290 client_unlink_session (client, session);
294 client_cleanup_sessions (GstRTSPClient * client)
296 GstRTSPClientPrivate *priv = client->priv;
299 /* remove weak-ref from sessions */
300 for (sessions = priv->sessions; sessions; sessions = g_list_next (sessions)) {
301 client_cleanup_session (client, (GstRTSPSession *) sessions->data);
303 g_list_free (priv->sessions);
304 priv->sessions = NULL;
307 /* A client is finalized when the connection is broken */
309 gst_rtsp_client_finalize (GObject * obj)
311 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
312 GstRTSPClientPrivate *priv = client->priv;
314 GST_INFO ("finalize client %p", client);
316 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
319 g_source_destroy ((GSource *) priv->watch);
321 client_cleanup_sessions (client);
323 if (priv->connection)
324 gst_rtsp_connection_free (priv->connection);
325 if (priv->session_pool)
326 g_object_unref (priv->session_pool);
327 if (priv->mount_points)
328 g_object_unref (priv->mount_points);
330 g_object_unref (priv->auth);
335 gst_rtsp_media_unprepare (priv->media);
336 g_object_unref (priv->media);
339 g_free (priv->server_ip);
340 g_mutex_clear (&priv->lock);
341 g_mutex_clear (&priv->send_lock);
343 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
347 gst_rtsp_client_get_property (GObject * object, guint propid,
348 GValue * value, GParamSpec * pspec)
350 GstRTSPClient *client = GST_RTSP_CLIENT (object);
353 case PROP_SESSION_POOL:
354 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
356 case PROP_MOUNT_POINTS:
357 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
359 case PROP_USE_CLIENT_SETTINGS:
360 g_value_set_boolean (value,
361 gst_rtsp_client_get_use_client_settings (client));
364 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
369 gst_rtsp_client_set_property (GObject * object, guint propid,
370 const GValue * value, GParamSpec * pspec)
372 GstRTSPClient *client = GST_RTSP_CLIENT (object);
375 case PROP_SESSION_POOL:
376 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
378 case PROP_MOUNT_POINTS:
379 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
381 case PROP_USE_CLIENT_SETTINGS:
382 gst_rtsp_client_set_use_client_settings (client,
383 g_value_get_boolean (value));
386 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
391 * gst_rtsp_client_new:
393 * Create a new #GstRTSPClient instance.
395 * Returns: a new #GstRTSPClient
398 gst_rtsp_client_new (void)
400 GstRTSPClient *result;
402 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
408 send_message (GstRTSPClient * client, GstRTSPSession * session,
409 GstRTSPMessage * message, gboolean close)
411 GstRTSPClientPrivate *priv = client->priv;
413 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
414 "GStreamer RTSP server");
416 /* remove any previous header */
417 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
419 /* add the new session header for new session ids */
421 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
422 gst_rtsp_session_get_header (session));
425 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
426 gst_rtsp_message_dump (message);
430 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
432 g_mutex_lock (&priv->send_lock);
434 priv->send_func (client, message, close, priv->send_data);
435 g_mutex_unlock (&priv->send_lock);
437 gst_rtsp_message_unset (message);
441 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
442 GstRTSPClientState * state)
444 gst_rtsp_message_init_response (state->response, code,
445 gst_rtsp_status_as_text (code), state->request);
447 send_message (client, NULL, state->response, FALSE);
451 handle_unauthorized_request (GstRTSPClient * client, GstRTSPAuth * auth,
452 GstRTSPClientState * state)
454 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_UNAUTHORIZED,
455 gst_rtsp_status_as_text (GST_RTSP_STS_UNAUTHORIZED), state->request);
458 /* and let the authentication manager setup the auth tokens */
459 gst_rtsp_auth_setup (auth, state);
462 send_message (client, state->session, state->response, FALSE);
467 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
469 if (path1 == NULL || path2 == NULL)
472 if (strlen (path1) != len2)
475 if (strncmp (path1, path2, len2))
481 /* this function is called to initially find the media for the DESCRIBE request
482 * but is cached for when the same client (without breaking the connection) is
483 * doing a setup for the exact same url. */
484 static GstRTSPMedia *
485 find_media (GstRTSPClient * client, GstRTSPClientState * state, gint * matched)
487 GstRTSPClientPrivate *priv = client->priv;
488 GstRTSPMediaFactory *factory;
493 if (!priv->mount_points)
494 goto no_mount_points;
496 path = state->uri->abspath;
498 /* find the longest matching factory for the uri first */
499 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
503 state->factory = factory;
505 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
506 goto no_factory_access;
508 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
514 path_len = strlen (path);
516 if (!paths_are_equal (priv->path, path, path_len)) {
517 /* remove any previously cached values before we try to construct a new
523 gst_rtsp_media_unprepare (priv->media);
524 g_object_unref (priv->media);
528 /* prepare the media and add it to the pipeline */
529 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
532 /* prepare the media */
533 if (!(gst_rtsp_media_prepare (media, NULL)))
536 /* now keep track of the uri and the media */
537 priv->path = g_strndup (path, path_len);
539 state->media = media;
541 /* we have seen this path before, used cached media */
543 state->media = media;
544 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
547 g_object_unref (factory);
548 state->factory = NULL;
551 g_object_ref (media);
558 GST_ERROR ("client %p: no mount points configured", client);
559 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
564 GST_ERROR ("client %p: no factory for uri %s", client, path);
565 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
570 GST_ERROR ("client %p: not authorized to see factory uri %s", client, path);
571 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
576 GST_ERROR ("client %p: not authorized for factory uri %s", client, path);
577 handle_unauthorized_request (client, priv->auth, state);
582 GST_ERROR ("client %p: can't create media", client);
583 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
584 g_object_unref (factory);
585 state->factory = NULL;
590 GST_ERROR ("client %p: can't prepare media", client);
591 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
592 g_object_unref (media);
593 state->media = media;
594 g_object_unref (factory);
595 state->factory = NULL;
601 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
603 GstRTSPClientPrivate *priv = client->priv;
604 GstRTSPMessage message = { 0 };
609 gst_rtsp_message_init_data (&message, channel);
611 /* FIXME, need some sort of iovec RTSPMessage here */
612 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
615 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
617 g_mutex_lock (&priv->send_lock);
619 priv->send_func (client, &message, FALSE, priv->send_data);
620 g_mutex_unlock (&priv->send_lock);
622 gst_rtsp_message_steal_body (&message, &data, &usize);
623 gst_buffer_unmap (buffer, &map_info);
625 gst_rtsp_message_unset (&message);
631 link_transport (GstRTSPClient * client, GstRTSPSession * session,
632 GstRTSPStreamTransport * trans)
634 GstRTSPClientPrivate *priv = client->priv;
636 GST_DEBUG ("client %p: linking transport %p", client, trans);
638 gst_rtsp_stream_transport_set_callbacks (trans,
639 (GstRTSPSendFunc) do_send_data,
640 (GstRTSPSendFunc) do_send_data, client, NULL);
642 priv->transports = g_list_prepend (priv->transports, trans);
644 /* make sure our session can't expire */
645 gst_rtsp_session_prevent_expire (session);
649 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
650 GstRTSPStreamTransport * trans)
652 GstRTSPClientPrivate *priv = client->priv;
654 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
656 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
658 priv->transports = g_list_remove (priv->transports, trans);
660 /* our session can now expire */
661 gst_rtsp_session_allow_expire (session);
665 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
666 GstRTSPSessionMedia * sessmedia)
671 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
672 for (i = 0; i < n_streams; i++) {
673 GstRTSPStreamTransport *trans;
674 const GstRTSPTransport *tr;
676 /* get the transport, if there is no transport configured, skip this stream */
677 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
681 tr = gst_rtsp_stream_transport_get_transport (trans);
683 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
684 /* for TCP, unlink the stream from the TCP connection of the client */
685 unlink_transport (client, session, trans);
691 close_connection (GstRTSPClient * client)
693 GstRTSPClientPrivate *priv = client->priv;
694 const gchar *tunnelid;
696 GST_DEBUG ("client %p: closing connection", client);
698 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
699 g_mutex_lock (&tunnels_lock);
700 /* remove from tunnelids */
701 g_hash_table_remove (tunnels, tunnelid);
702 g_mutex_unlock (&tunnels_lock);
705 gst_rtsp_connection_close (priv->connection);
709 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
711 GstRTSPClientPrivate *priv = client->priv;
712 GstRTSPSession *session;
713 GstRTSPSessionMedia *sessmedia;
714 GstRTSPStatusCode code;
721 session = state->session;
726 path = state->uri->abspath;
728 /* get a handle to the configuration of the media in the session */
729 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
733 /* only aggregate control for now.. */
734 if (path[matched] != '\0')
737 state->sessmedia = sessmedia;
739 /* we emit the signal before closing the connection */
740 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
743 /* unlink the all TCP callbacks */
744 unlink_session_transports (client, session, sessmedia);
746 /* remove the session from the watched sessions */
747 client_unwatch_session (client, session);
749 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
751 /* unmanage the media in the session, returns false if all media session
753 if (!gst_rtsp_session_release_media (session, sessmedia)) {
754 /* remove the session */
755 gst_rtsp_session_pool_remove (priv->session_pool, session);
757 /* construct the response now */
758 code = GST_RTSP_STS_OK;
759 gst_rtsp_message_init_response (state->response, code,
760 gst_rtsp_status_as_text (code), state->request);
762 send_message (client, session, state->response, TRUE);
769 GST_ERROR ("client %p: no session", client);
770 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
775 GST_ERROR ("client %p: no uri supplied", client);
776 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
781 GST_ERROR ("client %p: no media for uri", client);
782 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
787 GST_ERROR ("client %p: no aggregate path %s", client, path);
788 send_generic_response (client,
789 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
795 default_params_set (GstRTSPClient * client, GstRTSPClientState * state)
799 res = gst_rtsp_params_set (client, state);
805 default_params_get (GstRTSPClient * client, GstRTSPClientState * state)
809 res = gst_rtsp_params_get (client, state);
815 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
821 res = gst_rtsp_message_get_body (state->request, &data, &size);
822 if (res != GST_RTSP_OK)
826 /* no body, keep-alive request */
827 send_generic_response (client, GST_RTSP_STS_OK, state);
829 /* there is a body, handle the params */
830 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, state);
831 if (res != GST_RTSP_OK)
834 send_message (client, state->session, state->response, FALSE);
837 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
845 GST_ERROR ("client %p: bad request", client);
846 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
852 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
858 res = gst_rtsp_message_get_body (state->request, &data, &size);
859 if (res != GST_RTSP_OK)
863 /* no body, keep-alive request */
864 send_generic_response (client, GST_RTSP_STS_OK, state);
866 /* there is a body, handle the params */
867 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, state);
868 if (res != GST_RTSP_OK)
871 send_message (client, state->session, state->response, FALSE);
874 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
882 GST_ERROR ("client %p: bad request", client);
883 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
889 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
891 GstRTSPSession *session;
892 GstRTSPSessionMedia *sessmedia;
893 GstRTSPStatusCode code;
894 GstRTSPState rtspstate;
898 if (!(session = state->session))
904 path = state->uri->abspath;
906 /* get a handle to the configuration of the media in the session */
907 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
911 if (path[matched] != '\0')
914 state->sessmedia = sessmedia;
916 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
917 /* the session state must be playing or recording */
918 if (rtspstate != GST_RTSP_STATE_PLAYING &&
919 rtspstate != GST_RTSP_STATE_RECORDING)
922 /* unlink the all TCP callbacks */
923 unlink_session_transports (client, session, sessmedia);
925 /* then pause sending */
926 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
928 /* construct the response now */
929 code = GST_RTSP_STS_OK;
930 gst_rtsp_message_init_response (state->response, code,
931 gst_rtsp_status_as_text (code), state->request);
933 send_message (client, session, state->response, FALSE);
935 /* the state is now READY */
936 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
938 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
946 GST_ERROR ("client %p: no seesion", client);
947 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
952 GST_ERROR ("client %p: no uri supplied", client);
953 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
958 GST_ERROR ("client %p: no media for uri", client);
959 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
964 GST_ERROR ("client %p: no aggregate path %s", client, path);
965 send_generic_response (client,
966 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
971 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
972 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
979 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
981 GstRTSPSession *session;
982 GstRTSPSessionMedia *sessmedia;
984 GstRTSPStatusCode code;
986 guint n_streams, i, infocount;
988 GstRTSPTimeRange *range;
990 GstRTSPState rtspstate;
991 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
995 if (!(session = state->session))
1001 path = state->uri->abspath;
1003 /* get a handle to the configuration of the media in the session */
1004 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1008 if (path[matched] != '\0')
1011 state->sessmedia = sessmedia;
1012 state->media = media = gst_rtsp_session_media_get_media (sessmedia);
1014 /* the session state must be playing or ready */
1015 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1016 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1019 /* parse the range header if we have one */
1021 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
1022 if (res == GST_RTSP_OK) {
1023 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1024 /* we have a range, seek to the position */
1026 gst_rtsp_media_seek (media, range);
1027 gst_rtsp_range_free (range);
1031 /* grab RTPInfo from the payloaders now */
1032 rtpinfo = g_string_new ("");
1034 n_streams = gst_rtsp_media_n_streams (media);
1035 for (i = 0, infocount = 0; i < n_streams; i++) {
1036 GstRTSPStreamTransport *trans;
1037 GstRTSPStream *stream;
1038 const GstRTSPTransport *tr;
1042 /* get the transport, if there is no transport configured, skip this stream */
1043 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
1044 if (trans == NULL) {
1045 GST_INFO ("stream %d is not configured", i);
1048 tr = gst_rtsp_stream_transport_get_transport (trans);
1050 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1051 /* for TCP, link the stream to the TCP connection of the client */
1052 link_transport (client, session, trans);
1055 stream = gst_rtsp_stream_transport_get_stream (trans);
1056 if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
1058 g_string_append (rtpinfo, ", ");
1060 uristr = gst_rtsp_url_get_request_uri (state->uri);
1061 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
1062 uristr, i, seq, rtptime);
1067 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
1071 /* construct the response now */
1072 code = GST_RTSP_STS_OK;
1073 gst_rtsp_message_init_response (state->response, code,
1074 gst_rtsp_status_as_text (code), state->request);
1076 /* add the RTP-Info header */
1077 if (infocount > 0) {
1078 str = g_string_free (rtpinfo, FALSE);
1079 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
1081 g_string_free (rtpinfo, TRUE);
1085 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1086 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
1088 send_message (client, session, state->response, FALSE);
1090 /* start playing after sending the request */
1091 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1093 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1095 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
1103 GST_ERROR ("client %p: no session", client);
1104 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1109 GST_ERROR ("client %p: no uri supplied", client);
1110 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1115 GST_ERROR ("client %p: media not found", client);
1116 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1121 GST_ERROR ("client %p: no aggregate path %s", client, path);
1122 send_generic_response (client,
1123 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
1128 GST_ERROR ("client %p: not PLAYING or READY", client);
1129 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1136 do_keepalive (GstRTSPSession * session)
1138 GST_INFO ("keep session %p alive", session);
1139 gst_rtsp_session_touch (session);
1142 /* parse @transport and return a valid transport in @tr. only transports
1143 * from @supported are returned. Returns FALSE if no valid transport
1146 parse_transport (const char *transport, GstRTSPLowerTrans supported,
1147 GstRTSPTransport * tr)
1154 gst_rtsp_transport_init (tr);
1156 GST_DEBUG ("parsing transports %s", transport);
1158 transports = g_strsplit (transport, ",", 0);
1160 /* loop through the transports, try to parse */
1161 for (i = 0; transports[i]; i++) {
1162 res = gst_rtsp_transport_parse (transports[i], tr);
1163 if (res != GST_RTSP_OK) {
1164 /* no valid transport, search some more */
1165 GST_WARNING ("could not parse transport %s", transports[i]);
1169 /* we have a transport, see if it's RTP/AVP */
1170 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1171 GST_WARNING ("invalid transport %s", transports[i]);
1175 if (!(tr->lower_transport & supported)) {
1176 GST_WARNING ("unsupported transport %s", transports[i]);
1180 /* we have a valid transport */
1181 GST_INFO ("found valid transport %s", transports[i]);
1186 gst_rtsp_transport_init (tr);
1188 g_strfreev (transports);
1194 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
1195 GstRTSPMessage * request)
1197 gchar *blocksize_str;
1198 gboolean ret = TRUE;
1200 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1201 &blocksize_str, 0) == GST_RTSP_OK) {
1205 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1206 if (end == blocksize_str) {
1207 GST_ERROR ("failed to parse blocksize");
1210 /* we don't want to change the mtu when this media
1211 * can be shared because it impacts other clients */
1212 if (gst_rtsp_media_is_shared (media))
1215 if (blocksize > G_MAXUINT)
1216 blocksize = G_MAXUINT;
1217 gst_rtsp_stream_set_mtu (stream, blocksize);
1224 default_configure_client_transport (GstRTSPClient * client,
1225 GstRTSPClientState * state, GstRTSPTransport * ct)
1227 GstRTSPClientPrivate *priv = client->priv;
1229 /* we have a valid transport now, set the destination of the client. */
1230 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1231 if (ct->destination && priv->use_client_settings) {
1232 GstRTSPAddress *addr;
1234 addr = gst_rtsp_stream_reserve_address (state->stream, ct->destination,
1235 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1240 gst_rtsp_address_free (addr);
1242 GstRTSPAddress *addr;
1243 GSocketFamily family;
1245 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1247 addr = gst_rtsp_stream_get_multicast_address (state->stream, family);
1251 g_free (ct->destination);
1252 ct->destination = g_strdup (addr->address);
1253 ct->port.min = addr->port;
1254 ct->port.max = addr->port + addr->n_ports - 1;
1255 ct->ttl = addr->ttl;
1257 gst_rtsp_address_free (addr);
1262 url = gst_rtsp_connection_get_url (priv->connection);
1263 g_free (ct->destination);
1264 ct->destination = g_strdup (url->host);
1266 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1267 /* check if the client selected channels for TCP */
1268 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1269 gst_rtsp_session_media_alloc_channels (state->sessmedia,
1279 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1284 static GstRTSPTransport *
1285 make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
1286 GstRTSPTransport * ct)
1288 GstRTSPTransport *st;
1290 GSocketFamily family;
1292 /* prepare the server transport */
1293 gst_rtsp_transport_new (&st);
1295 st->trans = ct->trans;
1296 st->profile = ct->profile;
1297 st->lower_transport = ct->lower_transport;
1299 addr = g_inet_address_new_from_string (ct->destination);
1302 GST_ERROR ("failed to get inet addr from client destination");
1303 family = G_SOCKET_FAMILY_IPV4;
1305 family = g_inet_address_get_family (addr);
1306 g_object_unref (addr);
1310 switch (st->lower_transport) {
1311 case GST_RTSP_LOWER_TRANS_UDP:
1312 st->client_port = ct->client_port;
1313 gst_rtsp_stream_get_server_port (state->stream, &st->server_port, family);
1315 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1316 st->port = ct->port;
1317 st->destination = g_strdup (ct->destination);
1320 case GST_RTSP_LOWER_TRANS_TCP:
1321 st->interleaved = ct->interleaved;
1326 gst_rtsp_stream_get_ssrc (state->stream, &st->ssrc);
1332 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
1334 GstRTSPClientPrivate *priv = client->priv;
1338 GstRTSPTransport *ct, *st;
1339 GstRTSPLowerTrans supported;
1340 GstRTSPStatusCode code;
1341 GstRTSPSession *session;
1342 GstRTSPStreamTransport *trans;
1344 GstRTSPSessionMedia *sessmedia;
1345 GstRTSPMedia *media;
1346 GstRTSPStream *stream;
1347 GstRTSPState rtspstate;
1348 GstRTSPClientClass *klass;
1349 gchar *path, *control;
1356 path = uri->abspath;
1358 /* parse the transport */
1360 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
1362 if (res != GST_RTSP_OK)
1365 /* we create the session after parsing stuff so that we don't make
1366 * a session for malformed requests */
1367 if (priv->session_pool == NULL)
1370 session = state->session;
1373 g_object_ref (session);
1374 /* get a handle to the configuration of the media in the session, this can
1375 * return NULL if this is a new url to manage in this session. */
1376 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1378 /* we need a new media configuration in this session */
1382 /* we have no session media, find one and manage it */
1383 if (sessmedia == NULL) {
1384 /* get a handle to the configuration of the media in the session */
1385 media = find_media (client, state, &matched);
1387 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1388 g_object_ref (media);
1390 /* no media, not found then */
1392 goto media_not_found;
1394 /* path is what matched. We can modify the parsed uri in place */
1395 path[matched] = '\0';
1396 /* control is remainder */
1397 control = &path[matched + 1];
1399 /* find the stream now using the control part */
1400 stream = gst_rtsp_media_find_stream (media, control);
1402 goto stream_not_found;
1404 /* now we have a uri identifying a valid media and stream */
1405 state->stream = stream;
1406 state->media = media;
1408 if (session == NULL) {
1409 /* create a session if this fails we probably reached our session limit or
1411 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1412 goto service_unavailable;
1414 /* make sure this client is closed when the session is closed */
1415 client_watch_session (client, session);
1417 /* signal new session */
1418 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1421 state->session = session;
1424 if (sessmedia == NULL) {
1425 /* manage the media in our session now, if not done already */
1426 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1427 /* if we stil have no media, error */
1428 if (sessmedia == NULL)
1429 goto sessmedia_unavailable;
1431 g_object_unref (media);
1434 state->sessmedia = sessmedia;
1436 /* set blocksize on this stream */
1437 if (!handle_blocksize (media, stream, state->request))
1438 goto invalid_blocksize;
1440 gst_rtsp_transport_new (&ct);
1442 /* our supported transports */
1443 supported = GST_RTSP_LOWER_TRANS_UDP |
1444 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
1446 /* parse and find a usable supported transport */
1447 if (!parse_transport (transport, supported, ct))
1448 goto unsupported_transports;
1450 /* update the client transport */
1451 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1452 if (!klass->configure_client_transport (client, state, ct))
1453 goto unsupported_client_transport;
1455 /* set in the session media transport */
1456 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1458 /* configure keepalive for this transport */
1459 gst_rtsp_stream_transport_set_keepalive (trans,
1460 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1462 /* create and serialize the server transport */
1463 st = make_server_transport (client, state, ct);
1464 trans_str = gst_rtsp_transport_as_text (st);
1465 gst_rtsp_transport_free (st);
1467 /* construct the response now */
1468 code = GST_RTSP_STS_OK;
1469 gst_rtsp_message_init_response (state->response, code,
1470 gst_rtsp_status_as_text (code), state->request);
1472 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1476 send_message (client, session, state->response, FALSE);
1478 /* update the state */
1479 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1480 switch (rtspstate) {
1481 case GST_RTSP_STATE_PLAYING:
1482 case GST_RTSP_STATE_RECORDING:
1483 case GST_RTSP_STATE_READY:
1484 /* no state change */
1487 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1490 g_object_unref (session);
1492 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST],
1500 GST_ERROR ("client %p: no uri", client);
1501 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1506 GST_ERROR ("client %p: no transport", client);
1507 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1512 GST_ERROR ("client %p: no session pool configured", client);
1513 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1518 GST_ERROR ("client %p: media '%s' not found", client, path);
1519 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1524 GST_ERROR ("client %p: stream '%s' not found", client, control);
1525 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1526 g_object_unref (media);
1529 service_unavailable:
1531 GST_ERROR ("client %p: can't create session", client);
1532 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1533 g_object_unref (media);
1536 sessmedia_unavailable:
1538 GST_ERROR ("client %p: can't create session media", client);
1539 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1540 g_object_unref (media);
1541 g_object_unref (session);
1546 GST_ERROR ("client %p: invalid blocksize", client);
1547 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1548 g_object_unref (session);
1551 unsupported_transports:
1553 GST_ERROR ("client %p: unsupported transports", client);
1554 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1555 gst_rtsp_transport_free (ct);
1556 g_object_unref (session);
1559 unsupported_client_transport:
1561 GST_ERROR ("client %p: unsupported client transport", client);
1562 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1563 gst_rtsp_transport_free (ct);
1564 g_object_unref (session);
1569 static GstSDPMessage *
1570 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1572 GstRTSPClientPrivate *priv = client->priv;
1577 gst_sdp_message_new (&sdp);
1579 /* some standard things first */
1580 gst_sdp_message_set_version (sdp, "0");
1587 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1590 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1591 gst_sdp_message_set_information (sdp, "rtsp-server");
1592 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1593 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1594 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1595 gst_sdp_message_add_attribute (sdp, "control", "*");
1597 info.is_ipv6 = priv->is_ipv6;
1598 info.server_ip = priv->server_ip;
1600 /* create an SDP for the media object */
1601 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1609 GST_ERROR ("client %p: could not create SDP", client);
1610 gst_sdp_message_free (sdp);
1615 /* for the describe we must generate an SDP */
1617 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1622 gchar *str, *content_base;
1623 GstRTSPMedia *media;
1624 GstRTSPClientClass *klass;
1626 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1631 /* check what kind of format is accepted, we don't really do anything with it
1632 * and always return SDP for now. */
1637 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1639 if (res == GST_RTSP_ENOTIMPL)
1642 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1646 /* find the media object for the uri */
1647 if (!(media = find_media (client, state, NULL)))
1650 /* create an SDP for the media object on this client */
1651 if (!(sdp = klass->create_sdp (client, media)))
1654 g_object_unref (media);
1656 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1657 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1659 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1662 /* content base for some clients that might screw up creating the setup uri */
1663 str = gst_rtsp_url_get_request_uri (state->uri);
1664 str_len = strlen (str);
1666 /* check for trailing '/' and append one */
1667 if (str[str_len - 1] != '/') {
1668 content_base = g_malloc (str_len + 2);
1669 memcpy (content_base, str, str_len);
1670 content_base[str_len] = '/';
1671 content_base[str_len + 1] = '\0';
1677 GST_INFO ("adding content-base: %s", content_base);
1679 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1681 g_free (content_base);
1683 /* add SDP to the response body */
1684 str = gst_sdp_message_as_text (sdp);
1685 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1686 gst_sdp_message_free (sdp);
1688 send_message (client, state->session, state->response, FALSE);
1690 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1698 GST_ERROR ("client %p: no uri", client);
1699 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1704 GST_ERROR ("client %p: no media", client);
1705 /* error reply is already sent */
1710 GST_ERROR ("client %p: can't create SDP", client);
1711 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1712 g_object_unref (media);
1718 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1720 GstRTSPMethod options;
1723 options = GST_RTSP_DESCRIBE |
1728 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1730 str = gst_rtsp_options_as_text (options);
1732 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1733 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1735 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1738 send_message (client, state->session, state->response, FALSE);
1740 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1746 /* remove duplicate and trailing '/' */
1748 sanitize_uri (GstRTSPUrl * uri)
1752 gboolean have_slash, prev_slash;
1754 s = d = uri->abspath;
1755 len = strlen (uri->abspath);
1759 for (i = 0; i < len; i++) {
1760 have_slash = s[i] == '/';
1762 if (!have_slash || !prev_slash)
1764 prev_slash = have_slash;
1766 len = d - uri->abspath;
1767 /* don't remove the first slash if that's the only thing left */
1768 if (len > 1 && *(d - 1) == '/')
1774 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1776 GstRTSPClientPrivate *priv = client->priv;
1778 GST_INFO ("client %p: session %p finished", client, session);
1780 /* unlink all media managed in this session */
1781 client_unlink_session (client, session);
1783 /* remove the session */
1784 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1785 GST_INFO ("client %p: all sessions finalized, close the connection",
1787 close_connection (client);
1791 static GPrivate state_key;
1794 * gst_rtsp_client_state_get_current:
1796 * Get the current #GstRTSPClientState. This object is retrieved from the
1797 * current thread that is handling the request for a client.
1799 * Returns: a #GstRTSPClientState
1801 GstRTSPClientState *
1802 gst_rtsp_client_state_get_current (void)
1804 return g_private_get (&state_key);
1808 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1810 GstRTSPClientPrivate *priv = client->priv;
1811 GstRTSPMethod method;
1812 const gchar *uristr;
1813 GstRTSPUrl *uri = NULL;
1814 GstRTSPVersion version;
1816 GstRTSPSession *session = NULL;
1817 GstRTSPClientState state = { NULL };
1818 GstRTSPMessage response = { 0 };
1821 state.client = client;
1822 state.request = request;
1823 state.response = &response;
1824 state.auth = priv->auth;
1825 g_private_set (&state_key, &state);
1827 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1828 gst_rtsp_message_dump (request);
1831 GST_INFO ("client %p: received a request", client);
1833 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1835 /* we can only handle 1.0 requests */
1836 if (version != GST_RTSP_VERSION_1_0)
1839 state.method = method;
1841 /* we always try to parse the url first */
1842 if (strcmp (uristr, "*") == 0) {
1843 /* special case where we have * as uri, keep uri = NULL */
1844 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1847 /* get the session if there is any */
1848 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1849 if (res == GST_RTSP_OK) {
1850 if (priv->session_pool == NULL)
1853 /* we had a session in the request, find it again */
1854 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1855 goto session_not_found;
1857 /* we add the session to the client list of watched sessions. When a session
1858 * disappears because it times out, we will be notified. If all sessions are
1859 * gone, we will close the connection */
1860 client_watch_session (client, session);
1863 /* sanitize the uri */
1867 state.session = session;
1869 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
1870 goto not_authorized;
1872 /* now see what is asked and dispatch to a dedicated handler */
1874 case GST_RTSP_OPTIONS:
1875 handle_options_request (client, &state);
1877 case GST_RTSP_DESCRIBE:
1878 handle_describe_request (client, &state);
1880 case GST_RTSP_SETUP:
1881 handle_setup_request (client, &state);
1884 handle_play_request (client, &state);
1886 case GST_RTSP_PAUSE:
1887 handle_pause_request (client, &state);
1889 case GST_RTSP_TEARDOWN:
1890 handle_teardown_request (client, &state);
1892 case GST_RTSP_SET_PARAMETER:
1893 handle_set_param_request (client, &state);
1895 case GST_RTSP_GET_PARAMETER:
1896 handle_get_param_request (client, &state);
1898 case GST_RTSP_ANNOUNCE:
1899 case GST_RTSP_RECORD:
1900 case GST_RTSP_REDIRECT:
1901 goto not_implemented;
1902 case GST_RTSP_INVALID:
1908 g_private_set (&state_key, NULL);
1910 g_object_unref (session);
1912 gst_rtsp_url_free (uri);
1918 GST_ERROR ("client %p: version %d not supported", client, version);
1919 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1925 GST_ERROR ("client %p: bad request", client);
1926 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1931 GST_ERROR ("client %p: no pool configured", client);
1932 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1937 GST_ERROR ("client %p: session not found", client);
1938 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1943 GST_ERROR ("client %p: not allowed", client);
1944 handle_unauthorized_request (client, priv->auth, &state);
1949 GST_ERROR ("client %p: method %d not implemented", client, method);
1950 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1956 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1958 GstRTSPClientPrivate *priv = client->priv;
1967 /* find the stream for this message */
1968 res = gst_rtsp_message_parse_data (message, &channel);
1969 if (res != GST_RTSP_OK)
1972 gst_rtsp_message_steal_body (message, &data, &size);
1974 buffer = gst_buffer_new_wrapped (data, size);
1977 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1978 GstRTSPStreamTransport *trans;
1979 GstRTSPStream *stream;
1980 const GstRTSPTransport *tr;
1984 tr = gst_rtsp_stream_transport_get_transport (trans);
1985 stream = gst_rtsp_stream_transport_get_stream (trans);
1987 /* check for TCP transport */
1988 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1989 /* dispatch to the stream based on the channel number */
1990 if (tr->interleaved.min == channel) {
1991 gst_rtsp_stream_recv_rtp (stream, buffer);
1994 } else if (tr->interleaved.max == channel) {
1995 gst_rtsp_stream_recv_rtcp (stream, buffer);
2002 gst_buffer_unref (buffer);
2006 * gst_rtsp_client_set_session_pool:
2007 * @client: a #GstRTSPClient
2008 * @pool: a #GstRTSPSessionPool
2010 * Set @pool as the sessionpool for @client which it will use to find
2011 * or allocate sessions. the sessionpool is usually inherited from the server
2012 * that created the client but can be overridden later.
2015 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2016 GstRTSPSessionPool * pool)
2018 GstRTSPSessionPool *old;
2019 GstRTSPClientPrivate *priv;
2021 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2023 priv = client->priv;
2026 g_object_ref (pool);
2028 g_mutex_lock (&priv->lock);
2029 old = priv->session_pool;
2030 priv->session_pool = pool;
2031 g_mutex_unlock (&priv->lock);
2034 g_object_unref (old);
2038 * gst_rtsp_client_get_session_pool:
2039 * @client: a #GstRTSPClient
2041 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2043 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2045 GstRTSPSessionPool *
2046 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2048 GstRTSPClientPrivate *priv;
2049 GstRTSPSessionPool *result;
2051 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2053 priv = client->priv;
2055 g_mutex_lock (&priv->lock);
2056 if ((result = priv->session_pool))
2057 g_object_ref (result);
2058 g_mutex_unlock (&priv->lock);
2064 * gst_rtsp_client_set_mount_points:
2065 * @client: a #GstRTSPClient
2066 * @mounts: a #GstRTSPMountPoints
2068 * Set @mounts as the mount points for @client which it will use to map urls
2069 * to media streams. These mount points are usually inherited from the server that
2070 * created the client but can be overriden later.
2073 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2074 GstRTSPMountPoints * mounts)
2076 GstRTSPClientPrivate *priv;
2077 GstRTSPMountPoints *old;
2079 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2081 priv = client->priv;
2084 g_object_ref (mounts);
2086 g_mutex_lock (&priv->lock);
2087 old = priv->mount_points;
2088 priv->mount_points = mounts;
2089 g_mutex_unlock (&priv->lock);
2092 g_object_unref (old);
2096 * gst_rtsp_client_get_mount_points:
2097 * @client: a #GstRTSPClient
2099 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2101 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2103 GstRTSPMountPoints *
2104 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2106 GstRTSPClientPrivate *priv;
2107 GstRTSPMountPoints *result;
2109 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2111 priv = client->priv;
2113 g_mutex_lock (&priv->lock);
2114 if ((result = priv->mount_points))
2115 g_object_ref (result);
2116 g_mutex_unlock (&priv->lock);
2122 * gst_rtsp_client_set_use_client_settings:
2123 * @client: a #GstRTSPClient
2124 * @use_client_settings: whether to use client settings for multicast
2126 * Use client transport settings (destination and ttl) for multicast.
2127 * When @use_client_settings is %FALSE, the server settings will be
2131 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
2132 gboolean use_client_settings)
2134 GstRTSPClientPrivate *priv;
2136 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2138 priv = client->priv;
2140 g_mutex_lock (&priv->lock);
2141 priv->use_client_settings = use_client_settings;
2142 g_mutex_unlock (&priv->lock);
2146 * gst_rtsp_client_get_use_client_settings:
2147 * @client: a #GstRTSPClient
2149 * Check if client transport settings (destination and ttl) for multicast
2153 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
2155 GstRTSPClientPrivate *priv;
2158 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2160 priv = client->priv;
2162 g_mutex_lock (&priv->lock);
2163 res = priv->use_client_settings;
2164 g_mutex_unlock (&priv->lock);
2170 * gst_rtsp_client_set_auth:
2171 * @client: a #GstRTSPClient
2172 * @auth: a #GstRTSPAuth
2174 * configure @auth to be used as the authentication manager of @client.
2177 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2179 GstRTSPClientPrivate *priv;
2182 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2184 priv = client->priv;
2187 g_object_ref (auth);
2189 g_mutex_lock (&priv->lock);
2192 g_mutex_unlock (&priv->lock);
2195 g_object_unref (old);
2200 * gst_rtsp_client_get_auth:
2201 * @client: a #GstRTSPClient
2203 * Get the #GstRTSPAuth used as the authentication manager of @client.
2205 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2209 gst_rtsp_client_get_auth (GstRTSPClient * client)
2211 GstRTSPClientPrivate *priv;
2212 GstRTSPAuth *result;
2214 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2216 priv = client->priv;
2218 g_mutex_lock (&priv->lock);
2219 if ((result = priv->auth))
2220 g_object_ref (result);
2221 g_mutex_unlock (&priv->lock);
2227 * gst_rtsp_client_set_connection:
2228 * @client: a #GstRTSPClient
2229 * @conn: (transfer full): a #GstRTSPConnection
2231 * Set the #GstRTSPConnection of @client. This function takes ownership of
2234 * Returns: %TRUE on success.
2237 gst_rtsp_client_set_connection (GstRTSPClient * client,
2238 GstRTSPConnection * conn)
2240 GstRTSPClientPrivate *priv;
2241 GSocket *read_socket;
2242 GSocketAddress *address;
2244 GError *error = NULL;
2246 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2247 g_return_val_if_fail (conn != NULL, FALSE);
2249 priv = client->priv;
2251 read_socket = gst_rtsp_connection_get_read_socket (conn);
2253 if (!(address = g_socket_get_local_address (read_socket, &error)))
2256 g_free (priv->server_ip);
2257 /* keep the original ip that the client connected to */
2258 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2259 GInetAddress *iaddr;
2261 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2263 /* socket might be ipv6 but adress still ipv4 */
2264 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2265 priv->server_ip = g_inet_address_to_string (iaddr);
2266 g_object_unref (address);
2268 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2269 priv->server_ip = g_strdup ("unknown");
2272 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2273 priv->server_ip, priv->is_ipv6);
2275 url = gst_rtsp_connection_get_url (conn);
2276 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2278 priv->connection = conn;
2285 GST_ERROR ("could not get local address %s", error->message);
2286 g_error_free (error);
2292 * gst_rtsp_client_get_connection:
2293 * @client: a #GstRTSPClient
2295 * Get the #GstRTSPConnection of @client.
2297 * Returns: (transfer none): the #GstRTSPConnection of @client.
2298 * The connection object returned remains valid until the client is freed.
2301 gst_rtsp_client_get_connection (GstRTSPClient * client)
2303 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2305 return client->priv->connection;
2309 * gst_rtsp_client_set_send_func:
2310 * @client: a #GstRTSPClient
2311 * @func: a #GstRTSPClientSendFunc
2312 * @user_data: user data passed to @func
2313 * @notify: called when @user_data is no longer in use
2315 * Set @func as the callback that will be called when a new message needs to be
2316 * sent to the client. @user_data is passed to @func and @notify is called when
2317 * @user_data is no longer in use.
2320 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2321 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2323 GstRTSPClientPrivate *priv;
2324 GDestroyNotify old_notify;
2327 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2329 priv = client->priv;
2331 g_mutex_lock (&priv->send_lock);
2332 priv->send_func = func;
2333 old_notify = priv->send_notify;
2334 old_data = priv->send_data;
2335 priv->send_notify = notify;
2336 priv->send_data = user_data;
2337 g_mutex_unlock (&priv->send_lock);
2340 old_notify (old_data);
2344 * gst_rtsp_client_handle_message:
2345 * @client: a #GstRTSPClient
2346 * @message: an #GstRTSPMessage
2348 * Let the client handle @message.
2350 * Returns: a #GstRTSPResult.
2353 gst_rtsp_client_handle_message (GstRTSPClient * client,
2354 GstRTSPMessage * message)
2356 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2357 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2359 switch (message->type) {
2360 case GST_RTSP_MESSAGE_REQUEST:
2361 handle_request (client, message);
2363 case GST_RTSP_MESSAGE_RESPONSE:
2365 case GST_RTSP_MESSAGE_DATA:
2366 handle_data (client, message);
2375 * gst_rtsp_client_send_request:
2376 * @client: a #GstRTSPClient
2377 * @session: a #GstRTSPSession to send the request to or %NULL
2378 * @request: The request #GstRTSPMessage to send
2380 * Send a request message to the client.
2383 gst_rtsp_client_send_request (GstRTSPClient * client, GstRTSPSession * session,
2384 GstRTSPMessage * request)
2386 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2387 g_return_val_if_fail (request != NULL, GST_RTSP_EINVAL);
2388 g_return_val_if_fail (request->type == GST_RTSP_MESSAGE_REQUEST,
2391 send_message (client, session, request, FALSE);
2396 static GstRTSPResult
2397 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2398 gboolean close, gpointer user_data)
2400 GstRTSPClientPrivate *priv = client->priv;
2402 /* send the response and store the seq number so we can wait until it's
2403 * written to the client to close the connection */
2404 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2405 &priv->close_seq : NULL);
2408 static GstRTSPResult
2409 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2412 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2415 static GstRTSPResult
2416 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2418 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2419 GstRTSPClientPrivate *priv = client->priv;
2421 if (priv->close_seq && priv->close_seq == cseq) {
2422 priv->close_seq = 0;
2423 close_connection (client);
2429 static GstRTSPResult
2430 closed (GstRTSPWatch * watch, gpointer user_data)
2432 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2433 GstRTSPClientPrivate *priv = client->priv;
2434 const gchar *tunnelid;
2436 GST_INFO ("client %p: connection closed", client);
2438 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2439 g_mutex_lock (&tunnels_lock);
2440 /* remove from tunnelids */
2441 g_hash_table_remove (tunnels, tunnelid);
2442 g_mutex_unlock (&tunnels_lock);
2445 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2450 static GstRTSPResult
2451 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2453 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2456 str = gst_rtsp_strresult (result);
2457 GST_INFO ("client %p: received an error %s", client, str);
2463 static GstRTSPResult
2464 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2465 GstRTSPMessage * message, guint id, gpointer user_data)
2467 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2470 str = gst_rtsp_strresult (result);
2472 ("client %p: error when handling message %p with id %d: %s",
2473 client, message, id, str);
2480 remember_tunnel (GstRTSPClient * client)
2482 GstRTSPClientPrivate *priv = client->priv;
2483 const gchar *tunnelid;
2485 /* store client in the pending tunnels */
2486 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2487 if (tunnelid == NULL)
2490 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2492 /* we can't have two clients connecting with the same tunnelid */
2493 g_mutex_lock (&tunnels_lock);
2494 if (g_hash_table_lookup (tunnels, tunnelid))
2495 goto tunnel_existed;
2497 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2498 g_mutex_unlock (&tunnels_lock);
2505 GST_ERROR ("client %p: no tunnelid provided", client);
2510 g_mutex_unlock (&tunnels_lock);
2511 GST_ERROR ("client %p: tunnel session %s already existed", client,
2517 static GstRTSPStatusCode
2518 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2520 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2521 GstRTSPClientPrivate *priv = client->priv;
2523 GST_INFO ("client %p: tunnel start (connection %p)", client,
2526 if (!remember_tunnel (client))
2529 return GST_RTSP_STS_OK;
2534 GST_ERROR ("client %p: error starting tunnel", client);
2535 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2539 static GstRTSPResult
2540 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2542 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2543 GstRTSPClientPrivate *priv = client->priv;
2545 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2548 /* ignore error, it'll only be a problem when the client does a POST again */
2549 remember_tunnel (client);
2554 static GstRTSPResult
2555 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2557 const gchar *tunnelid;
2558 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2559 GstRTSPClientPrivate *priv = client->priv;
2560 GstRTSPClient *oclient;
2561 GstRTSPClientPrivate *opriv;
2563 GST_INFO ("client %p: tunnel complete", client);
2565 /* find previous tunnel */
2566 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2567 if (tunnelid == NULL)
2570 g_mutex_lock (&tunnels_lock);
2571 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2574 /* remove the old client from the table. ref before because removing it will
2575 * remove the ref to it. */
2576 g_object_ref (oclient);
2577 g_hash_table_remove (tunnels, tunnelid);
2579 opriv = oclient->priv;
2581 if (opriv->watch == NULL)
2583 g_mutex_unlock (&tunnels_lock);
2585 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2586 opriv->connection, priv->connection);
2588 /* merge the tunnels into the first client */
2589 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2590 gst_rtsp_watch_reset (opriv->watch);
2591 g_object_unref (oclient);
2598 GST_ERROR ("client %p: no tunnelid provided", client);
2599 return GST_RTSP_ERROR;
2603 g_mutex_unlock (&tunnels_lock);
2604 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2605 return GST_RTSP_ERROR;
2609 g_mutex_unlock (&tunnels_lock);
2610 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2611 g_object_unref (oclient);
2612 return GST_RTSP_ERROR;
2616 static GstRTSPWatchFuncs watch_funcs = {
2628 client_watch_notify (GstRTSPClient * client)
2630 GstRTSPClientPrivate *priv = client->priv;
2632 GST_INFO ("client %p: watch destroyed", client);
2634 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2635 g_object_unref (client);
2639 * gst_rtsp_client_attach:
2640 * @client: a #GstRTSPClient
2641 * @context: (allow-none): a #GMainContext
2643 * Attaches @client to @context. When the mainloop for @context is run, the
2644 * client will be dispatched. When @context is NULL, the default context will be
2647 * This function should be called when the client properties and urls are fully
2648 * configured and the client is ready to start.
2650 * Returns: the ID (greater than 0) for the source within the GMainContext.
2653 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2655 GstRTSPClientPrivate *priv;
2658 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2659 priv = client->priv;
2660 g_return_val_if_fail (priv->watch == NULL, 0);
2662 /* create watch for the connection and attach */
2663 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2664 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2665 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2666 (GDestroyNotify) gst_rtsp_watch_unref);
2668 /* FIXME make this configurable. We don't want to do this yet because it will
2669 * be superceeded by a cache object later */
2670 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2672 GST_INFO ("attaching to context %p", context);
2673 res = gst_rtsp_watch_attach (priv->watch, context);
2679 * gst_rtsp_client_session_filter:
2680 * @client: a #GstRTSPclient
2681 * @func: (scope call): a callback
2682 * @user_data: user data passed to @func
2684 * Call @func for each session managed by @client. The result value of @func
2685 * determines what happens to the session. @func will be called with @client
2686 * locked so no further actions on @client can be performed from @func.
2688 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
2691 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
2693 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
2694 * will also be added with an additional ref to the result #GList of this
2697 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
2698 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2699 * element in the #GList should be unreffed before the list is freed.
2702 gst_rtsp_client_session_filter (GstRTSPClient * client,
2703 GstRTSPClientSessionFilterFunc func, gpointer user_data)
2705 GstRTSPClientPrivate *priv;
2706 GList *result, *walk, *next;
2708 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2709 g_return_val_if_fail (func != NULL, NULL);
2711 priv = client->priv;
2715 g_mutex_lock (&priv->lock);
2716 for (walk = priv->sessions; walk; walk = next) {
2717 GstRTSPSession *sess = walk->data;
2719 next = g_list_next (walk);
2721 switch (func (client, sess, user_data)) {
2722 case GST_RTSP_FILTER_REMOVE:
2723 /* stop watching the session and pretent it went away */
2724 client_cleanup_session (client, sess);
2726 case GST_RTSP_FILTER_REF:
2727 result = g_list_prepend (result, g_object_ref (sess));
2729 case GST_RTSP_FILTER_KEEP:
2734 g_mutex_unlock (&priv->lock);