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, client, 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 (priv->auth, client, GST_RTSP_AUTH_CHECK_FACTORY,
512 path_len = strlen (path);
514 if (!paths_are_equal (priv->path, path, path_len)) {
515 /* remove any previously cached values before we try to construct a new
521 gst_rtsp_media_unprepare (priv->media);
522 g_object_unref (priv->media);
526 /* prepare the media and add it to the pipeline */
527 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
530 /* prepare the media */
531 if (!(gst_rtsp_media_prepare (media, NULL)))
534 /* now keep track of the uri and the media */
535 priv->path = g_strndup (path, path_len);
537 state->media = media;
539 /* we have seen this path before, used cached media */
541 state->media = media;
542 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
545 g_object_unref (factory);
546 state->factory = NULL;
549 g_object_ref (media);
556 GST_ERROR ("client %p: no mount points configured", client);
557 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
562 GST_ERROR ("client %p: no factory for uri %s", client, path);
563 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
568 GST_ERROR ("client %p: not authorized for factory %p", client, factory);
569 handle_unauthorized_request (client, priv->auth, state);
574 GST_ERROR ("client %p: can't create media", client);
575 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
576 g_object_unref (factory);
577 state->factory = NULL;
582 GST_ERROR ("client %p: can't prepare media", client);
583 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
584 g_object_unref (media);
585 state->media = media;
586 g_object_unref (factory);
587 state->factory = NULL;
593 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
595 GstRTSPClientPrivate *priv = client->priv;
596 GstRTSPMessage message = { 0 };
601 gst_rtsp_message_init_data (&message, channel);
603 /* FIXME, need some sort of iovec RTSPMessage here */
604 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
607 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
609 g_mutex_lock (&priv->send_lock);
611 priv->send_func (client, &message, FALSE, priv->send_data);
612 g_mutex_unlock (&priv->send_lock);
614 gst_rtsp_message_steal_body (&message, &data, &usize);
615 gst_buffer_unmap (buffer, &map_info);
617 gst_rtsp_message_unset (&message);
623 link_transport (GstRTSPClient * client, GstRTSPSession * session,
624 GstRTSPStreamTransport * trans)
626 GstRTSPClientPrivate *priv = client->priv;
628 GST_DEBUG ("client %p: linking transport %p", client, trans);
630 gst_rtsp_stream_transport_set_callbacks (trans,
631 (GstRTSPSendFunc) do_send_data,
632 (GstRTSPSendFunc) do_send_data, client, NULL);
634 priv->transports = g_list_prepend (priv->transports, trans);
636 /* make sure our session can't expire */
637 gst_rtsp_session_prevent_expire (session);
641 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
642 GstRTSPStreamTransport * trans)
644 GstRTSPClientPrivate *priv = client->priv;
646 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
648 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
650 priv->transports = g_list_remove (priv->transports, trans);
652 /* our session can now expire */
653 gst_rtsp_session_allow_expire (session);
657 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
658 GstRTSPSessionMedia * sessmedia)
663 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
664 for (i = 0; i < n_streams; i++) {
665 GstRTSPStreamTransport *trans;
666 const GstRTSPTransport *tr;
668 /* get the transport, if there is no transport configured, skip this stream */
669 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
673 tr = gst_rtsp_stream_transport_get_transport (trans);
675 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
676 /* for TCP, unlink the stream from the TCP connection of the client */
677 unlink_transport (client, session, trans);
683 close_connection (GstRTSPClient * client)
685 GstRTSPClientPrivate *priv = client->priv;
686 const gchar *tunnelid;
688 GST_DEBUG ("client %p: closing connection", client);
690 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
691 g_mutex_lock (&tunnels_lock);
692 /* remove from tunnelids */
693 g_hash_table_remove (tunnels, tunnelid);
694 g_mutex_unlock (&tunnels_lock);
697 gst_rtsp_connection_close (priv->connection);
701 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
703 GstRTSPClientPrivate *priv = client->priv;
704 GstRTSPSession *session;
705 GstRTSPSessionMedia *sessmedia;
706 GstRTSPStatusCode code;
713 session = state->session;
718 path = state->uri->abspath;
720 /* get a handle to the configuration of the media in the session */
721 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
725 /* only aggregate control for now.. */
726 if (path[matched] != '\0')
729 state->sessmedia = sessmedia;
731 /* we emit the signal before closing the connection */
732 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
735 /* unlink the all TCP callbacks */
736 unlink_session_transports (client, session, sessmedia);
738 /* remove the session from the watched sessions */
739 client_unwatch_session (client, session);
741 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
743 /* unmanage the media in the session, returns false if all media session
745 if (!gst_rtsp_session_release_media (session, sessmedia)) {
746 /* remove the session */
747 gst_rtsp_session_pool_remove (priv->session_pool, session);
749 /* construct the response now */
750 code = GST_RTSP_STS_OK;
751 gst_rtsp_message_init_response (state->response, code,
752 gst_rtsp_status_as_text (code), state->request);
754 send_message (client, session, state->response, TRUE);
761 GST_ERROR ("client %p: no session", client);
762 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
767 GST_ERROR ("client %p: no uri supplied", client);
768 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
773 GST_ERROR ("client %p: no media for uri", client);
774 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
779 GST_ERROR ("client %p: no aggregate path %s", client, path);
780 send_generic_response (client,
781 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
787 default_params_set (GstRTSPClient * client, GstRTSPClientState * state)
791 res = gst_rtsp_params_set (client, state);
797 default_params_get (GstRTSPClient * client, GstRTSPClientState * state)
801 res = gst_rtsp_params_get (client, state);
807 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
813 res = gst_rtsp_message_get_body (state->request, &data, &size);
814 if (res != GST_RTSP_OK)
818 /* no body, keep-alive request */
819 send_generic_response (client, GST_RTSP_STS_OK, state);
821 /* there is a body, handle the params */
822 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, state);
823 if (res != GST_RTSP_OK)
826 send_message (client, state->session, state->response, FALSE);
829 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
837 GST_ERROR ("client %p: bad request", client);
838 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
844 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
850 res = gst_rtsp_message_get_body (state->request, &data, &size);
851 if (res != GST_RTSP_OK)
855 /* no body, keep-alive request */
856 send_generic_response (client, GST_RTSP_STS_OK, state);
858 /* there is a body, handle the params */
859 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, state);
860 if (res != GST_RTSP_OK)
863 send_message (client, state->session, state->response, FALSE);
866 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
874 GST_ERROR ("client %p: bad request", client);
875 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
881 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
883 GstRTSPSession *session;
884 GstRTSPSessionMedia *sessmedia;
885 GstRTSPStatusCode code;
886 GstRTSPState rtspstate;
890 if (!(session = state->session))
896 path = state->uri->abspath;
898 /* get a handle to the configuration of the media in the session */
899 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
903 if (path[matched] != '\0')
906 state->sessmedia = sessmedia;
908 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
909 /* the session state must be playing or recording */
910 if (rtspstate != GST_RTSP_STATE_PLAYING &&
911 rtspstate != GST_RTSP_STATE_RECORDING)
914 /* unlink the all TCP callbacks */
915 unlink_session_transports (client, session, sessmedia);
917 /* then pause sending */
918 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
920 /* construct the response now */
921 code = GST_RTSP_STS_OK;
922 gst_rtsp_message_init_response (state->response, code,
923 gst_rtsp_status_as_text (code), state->request);
925 send_message (client, session, state->response, FALSE);
927 /* the state is now READY */
928 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
930 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
938 GST_ERROR ("client %p: no seesion", client);
939 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
944 GST_ERROR ("client %p: no uri supplied", client);
945 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
950 GST_ERROR ("client %p: no media for uri", client);
951 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
956 GST_ERROR ("client %p: no aggregate path %s", client, path);
957 send_generic_response (client,
958 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
963 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
964 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
971 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
973 GstRTSPSession *session;
974 GstRTSPSessionMedia *sessmedia;
976 GstRTSPStatusCode code;
978 guint n_streams, i, infocount;
980 GstRTSPTimeRange *range;
982 GstRTSPState rtspstate;
983 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
987 if (!(session = state->session))
993 path = state->uri->abspath;
995 /* get a handle to the configuration of the media in the session */
996 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1000 if (path[matched] != '\0')
1003 state->sessmedia = sessmedia;
1004 state->media = media = gst_rtsp_session_media_get_media (sessmedia);
1006 /* the session state must be playing or ready */
1007 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1008 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1011 /* parse the range header if we have one */
1013 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
1014 if (res == GST_RTSP_OK) {
1015 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1016 /* we have a range, seek to the position */
1018 gst_rtsp_media_seek (media, range);
1019 gst_rtsp_range_free (range);
1023 /* grab RTPInfo from the payloaders now */
1024 rtpinfo = g_string_new ("");
1026 n_streams = gst_rtsp_media_n_streams (media);
1027 for (i = 0, infocount = 0; i < n_streams; i++) {
1028 GstRTSPStreamTransport *trans;
1029 GstRTSPStream *stream;
1030 const GstRTSPTransport *tr;
1034 /* get the transport, if there is no transport configured, skip this stream */
1035 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
1036 if (trans == NULL) {
1037 GST_INFO ("stream %d is not configured", i);
1040 tr = gst_rtsp_stream_transport_get_transport (trans);
1042 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1043 /* for TCP, link the stream to the TCP connection of the client */
1044 link_transport (client, session, trans);
1047 stream = gst_rtsp_stream_transport_get_stream (trans);
1048 if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
1050 g_string_append (rtpinfo, ", ");
1052 uristr = gst_rtsp_url_get_request_uri (state->uri);
1053 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
1054 uristr, i, seq, rtptime);
1059 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
1063 /* construct the response now */
1064 code = GST_RTSP_STS_OK;
1065 gst_rtsp_message_init_response (state->response, code,
1066 gst_rtsp_status_as_text (code), state->request);
1068 /* add the RTP-Info header */
1069 if (infocount > 0) {
1070 str = g_string_free (rtpinfo, FALSE);
1071 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
1073 g_string_free (rtpinfo, TRUE);
1077 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1078 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
1080 send_message (client, session, state->response, FALSE);
1082 /* start playing after sending the request */
1083 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1085 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1087 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
1095 GST_ERROR ("client %p: no session", client);
1096 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1101 GST_ERROR ("client %p: no uri supplied", client);
1102 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1107 GST_ERROR ("client %p: media not found", client);
1108 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1113 GST_ERROR ("client %p: no aggregate path %s", client, path);
1114 send_generic_response (client,
1115 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
1120 GST_ERROR ("client %p: not PLAYING or READY", client);
1121 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1128 do_keepalive (GstRTSPSession * session)
1130 GST_INFO ("keep session %p alive", session);
1131 gst_rtsp_session_touch (session);
1134 /* parse @transport and return a valid transport in @tr. only transports
1135 * from @supported are returned. Returns FALSE if no valid transport
1138 parse_transport (const char *transport, GstRTSPLowerTrans supported,
1139 GstRTSPTransport * tr)
1146 gst_rtsp_transport_init (tr);
1148 GST_DEBUG ("parsing transports %s", transport);
1150 transports = g_strsplit (transport, ",", 0);
1152 /* loop through the transports, try to parse */
1153 for (i = 0; transports[i]; i++) {
1154 res = gst_rtsp_transport_parse (transports[i], tr);
1155 if (res != GST_RTSP_OK) {
1156 /* no valid transport, search some more */
1157 GST_WARNING ("could not parse transport %s", transports[i]);
1161 /* we have a transport, see if it's RTP/AVP */
1162 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1163 GST_WARNING ("invalid transport %s", transports[i]);
1167 if (!(tr->lower_transport & supported)) {
1168 GST_WARNING ("unsupported transport %s", transports[i]);
1172 /* we have a valid transport */
1173 GST_INFO ("found valid transport %s", transports[i]);
1178 gst_rtsp_transport_init (tr);
1180 g_strfreev (transports);
1186 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
1187 GstRTSPMessage * request)
1189 gchar *blocksize_str;
1190 gboolean ret = TRUE;
1192 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1193 &blocksize_str, 0) == GST_RTSP_OK) {
1197 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1198 if (end == blocksize_str) {
1199 GST_ERROR ("failed to parse blocksize");
1202 /* we don't want to change the mtu when this media
1203 * can be shared because it impacts other clients */
1204 if (gst_rtsp_media_is_shared (media))
1207 if (blocksize > G_MAXUINT)
1208 blocksize = G_MAXUINT;
1209 gst_rtsp_stream_set_mtu (stream, blocksize);
1216 default_configure_client_transport (GstRTSPClient * client,
1217 GstRTSPClientState * state, GstRTSPTransport * ct)
1219 GstRTSPClientPrivate *priv = client->priv;
1221 /* we have a valid transport now, set the destination of the client. */
1222 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1223 if (ct->destination && priv->use_client_settings) {
1224 GstRTSPAddress *addr;
1226 addr = gst_rtsp_stream_reserve_address (state->stream, ct->destination,
1227 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1232 gst_rtsp_address_free (addr);
1234 GstRTSPAddress *addr;
1235 GSocketFamily family;
1237 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1239 addr = gst_rtsp_stream_get_multicast_address (state->stream, family);
1243 g_free (ct->destination);
1244 ct->destination = g_strdup (addr->address);
1245 ct->port.min = addr->port;
1246 ct->port.max = addr->port + addr->n_ports - 1;
1247 ct->ttl = addr->ttl;
1249 gst_rtsp_address_free (addr);
1254 url = gst_rtsp_connection_get_url (priv->connection);
1255 g_free (ct->destination);
1256 ct->destination = g_strdup (url->host);
1258 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1259 /* check if the client selected channels for TCP */
1260 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1261 gst_rtsp_session_media_alloc_channels (state->sessmedia,
1271 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1276 static GstRTSPTransport *
1277 make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
1278 GstRTSPTransport * ct)
1280 GstRTSPTransport *st;
1282 GSocketFamily family;
1284 /* prepare the server transport */
1285 gst_rtsp_transport_new (&st);
1287 st->trans = ct->trans;
1288 st->profile = ct->profile;
1289 st->lower_transport = ct->lower_transport;
1291 addr = g_inet_address_new_from_string (ct->destination);
1294 GST_ERROR ("failed to get inet addr from client destination");
1295 family = G_SOCKET_FAMILY_IPV4;
1297 family = g_inet_address_get_family (addr);
1298 g_object_unref (addr);
1302 switch (st->lower_transport) {
1303 case GST_RTSP_LOWER_TRANS_UDP:
1304 st->client_port = ct->client_port;
1305 gst_rtsp_stream_get_server_port (state->stream, &st->server_port, family);
1307 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1308 st->port = ct->port;
1309 st->destination = g_strdup (ct->destination);
1312 case GST_RTSP_LOWER_TRANS_TCP:
1313 st->interleaved = ct->interleaved;
1318 gst_rtsp_stream_get_ssrc (state->stream, &st->ssrc);
1324 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
1326 GstRTSPClientPrivate *priv = client->priv;
1330 GstRTSPTransport *ct, *st;
1331 GstRTSPLowerTrans supported;
1332 GstRTSPStatusCode code;
1333 GstRTSPSession *session;
1334 GstRTSPStreamTransport *trans;
1336 GstRTSPSessionMedia *sessmedia;
1337 GstRTSPMedia *media;
1338 GstRTSPStream *stream;
1339 GstRTSPState rtspstate;
1340 GstRTSPClientClass *klass;
1341 gchar *path, *control;
1348 path = uri->abspath;
1350 /* parse the transport */
1352 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
1354 if (res != GST_RTSP_OK)
1357 /* we create the session after parsing stuff so that we don't make
1358 * a session for malformed requests */
1359 if (priv->session_pool == NULL)
1362 session = state->session;
1365 g_object_ref (session);
1366 /* get a handle to the configuration of the media in the session, this can
1367 * return NULL if this is a new url to manage in this session. */
1368 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1370 /* we need a new media configuration in this session */
1374 /* we have no session media, find one and manage it */
1375 if (sessmedia == NULL) {
1376 /* get a handle to the configuration of the media in the session */
1377 media = find_media (client, state, &matched);
1379 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1380 g_object_ref (media);
1382 /* no media, not found then */
1384 goto media_not_found;
1386 /* path is what matched. We can modify the parsed uri in place */
1387 path[matched] = '\0';
1388 /* control is remainder */
1389 control = &path[matched + 1];
1391 /* find the stream now using the control part */
1392 stream = gst_rtsp_media_find_stream (media, control);
1394 goto stream_not_found;
1396 /* now we have a uri identifying a valid media and stream */
1397 state->stream = stream;
1398 state->media = media;
1400 if (session == NULL) {
1401 /* create a session if this fails we probably reached our session limit or
1403 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1404 goto service_unavailable;
1406 /* make sure this client is closed when the session is closed */
1407 client_watch_session (client, session);
1409 /* signal new session */
1410 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1413 state->session = session;
1416 if (sessmedia == NULL) {
1417 /* manage the media in our session now, if not done already */
1418 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1419 /* if we stil have no media, error */
1420 if (sessmedia == NULL)
1421 goto sessmedia_unavailable;
1423 g_object_unref (media);
1426 state->sessmedia = sessmedia;
1428 /* set blocksize on this stream */
1429 if (!handle_blocksize (media, stream, state->request))
1430 goto invalid_blocksize;
1432 gst_rtsp_transport_new (&ct);
1434 /* our supported transports */
1435 supported = GST_RTSP_LOWER_TRANS_UDP |
1436 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
1438 /* parse and find a usable supported transport */
1439 if (!parse_transport (transport, supported, ct))
1440 goto unsupported_transports;
1442 /* update the client transport */
1443 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1444 if (!klass->configure_client_transport (client, state, ct))
1445 goto unsupported_client_transport;
1447 /* set in the session media transport */
1448 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1450 /* configure keepalive for this transport */
1451 gst_rtsp_stream_transport_set_keepalive (trans,
1452 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1454 /* create and serialize the server transport */
1455 st = make_server_transport (client, state, ct);
1456 trans_str = gst_rtsp_transport_as_text (st);
1457 gst_rtsp_transport_free (st);
1459 /* construct the response now */
1460 code = GST_RTSP_STS_OK;
1461 gst_rtsp_message_init_response (state->response, code,
1462 gst_rtsp_status_as_text (code), state->request);
1464 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1468 send_message (client, session, state->response, FALSE);
1470 /* update the state */
1471 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1472 switch (rtspstate) {
1473 case GST_RTSP_STATE_PLAYING:
1474 case GST_RTSP_STATE_RECORDING:
1475 case GST_RTSP_STATE_READY:
1476 /* no state change */
1479 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1482 g_object_unref (session);
1484 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST],
1492 GST_ERROR ("client %p: no uri", client);
1493 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1498 GST_ERROR ("client %p: no transport", client);
1499 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1504 GST_ERROR ("client %p: no session pool configured", client);
1505 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1510 GST_ERROR ("client %p: media '%s' not found", client, path);
1511 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1516 GST_ERROR ("client %p: stream '%s' not found", client, control);
1517 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1518 g_object_unref (media);
1521 service_unavailable:
1523 GST_ERROR ("client %p: can't create session", client);
1524 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1525 g_object_unref (media);
1528 sessmedia_unavailable:
1530 GST_ERROR ("client %p: can't create session media", client);
1531 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1532 g_object_unref (media);
1533 g_object_unref (session);
1538 GST_ERROR ("client %p: invalid blocksize", client);
1539 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1540 g_object_unref (session);
1543 unsupported_transports:
1545 GST_ERROR ("client %p: unsupported transports", client);
1546 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1547 gst_rtsp_transport_free (ct);
1548 g_object_unref (session);
1551 unsupported_client_transport:
1553 GST_ERROR ("client %p: unsupported client transport", client);
1554 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1555 gst_rtsp_transport_free (ct);
1556 g_object_unref (session);
1561 static GstSDPMessage *
1562 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1564 GstRTSPClientPrivate *priv = client->priv;
1569 gst_sdp_message_new (&sdp);
1571 /* some standard things first */
1572 gst_sdp_message_set_version (sdp, "0");
1579 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1582 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1583 gst_sdp_message_set_information (sdp, "rtsp-server");
1584 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1585 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1586 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1587 gst_sdp_message_add_attribute (sdp, "control", "*");
1589 info.is_ipv6 = priv->is_ipv6;
1590 info.server_ip = priv->server_ip;
1592 /* create an SDP for the media object */
1593 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1601 GST_ERROR ("client %p: could not create SDP", client);
1602 gst_sdp_message_free (sdp);
1607 /* for the describe we must generate an SDP */
1609 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1614 gchar *str, *content_base;
1615 GstRTSPMedia *media;
1616 GstRTSPClientClass *klass;
1618 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1623 /* check what kind of format is accepted, we don't really do anything with it
1624 * and always return SDP for now. */
1629 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1631 if (res == GST_RTSP_ENOTIMPL)
1634 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1638 /* find the media object for the uri */
1639 if (!(media = find_media (client, state, NULL)))
1642 /* create an SDP for the media object on this client */
1643 if (!(sdp = klass->create_sdp (client, media)))
1646 g_object_unref (media);
1648 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1649 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1651 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1654 /* content base for some clients that might screw up creating the setup uri */
1655 str = gst_rtsp_url_get_request_uri (state->uri);
1656 str_len = strlen (str);
1658 /* check for trailing '/' and append one */
1659 if (str[str_len - 1] != '/') {
1660 content_base = g_malloc (str_len + 2);
1661 memcpy (content_base, str, str_len);
1662 content_base[str_len] = '/';
1663 content_base[str_len + 1] = '\0';
1669 GST_INFO ("adding content-base: %s", content_base);
1671 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1673 g_free (content_base);
1675 /* add SDP to the response body */
1676 str = gst_sdp_message_as_text (sdp);
1677 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1678 gst_sdp_message_free (sdp);
1680 send_message (client, state->session, state->response, FALSE);
1682 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1690 GST_ERROR ("client %p: no uri", client);
1691 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1696 GST_ERROR ("client %p: no media", client);
1697 /* error reply is already sent */
1702 GST_ERROR ("client %p: can't create SDP", client);
1703 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1704 g_object_unref (media);
1710 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1712 GstRTSPMethod options;
1715 options = GST_RTSP_DESCRIBE |
1720 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1722 str = gst_rtsp_options_as_text (options);
1724 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1725 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1727 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1730 send_message (client, state->session, state->response, FALSE);
1732 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1738 /* remove duplicate and trailing '/' */
1740 sanitize_uri (GstRTSPUrl * uri)
1744 gboolean have_slash, prev_slash;
1746 s = d = uri->abspath;
1747 len = strlen (uri->abspath);
1751 for (i = 0; i < len; i++) {
1752 have_slash = s[i] == '/';
1754 if (!have_slash || !prev_slash)
1756 prev_slash = have_slash;
1758 len = d - uri->abspath;
1759 /* don't remove the first slash if that's the only thing left */
1760 if (len > 1 && *(d - 1) == '/')
1766 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1768 GstRTSPClientPrivate *priv = client->priv;
1770 GST_INFO ("client %p: session %p finished", client, session);
1772 /* unlink all media managed in this session */
1773 client_unlink_session (client, session);
1775 /* remove the session */
1776 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1777 GST_INFO ("client %p: all sessions finalized, close the connection",
1779 close_connection (client);
1784 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1786 GstRTSPClientPrivate *priv = client->priv;
1787 GstRTSPMethod method;
1788 const gchar *uristr;
1789 GstRTSPUrl *uri = NULL;
1790 GstRTSPVersion version;
1792 GstRTSPSession *session = NULL;
1793 GstRTSPClientState state = { NULL };
1794 GstRTSPMessage response = { 0 };
1797 state.request = request;
1798 state.response = &response;
1800 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1801 gst_rtsp_message_dump (request);
1804 GST_INFO ("client %p: received a request", client);
1806 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1808 /* we can only handle 1.0 requests */
1809 if (version != GST_RTSP_VERSION_1_0)
1812 state.method = method;
1814 /* we always try to parse the url first */
1815 if (strcmp (uristr, "*") == 0) {
1816 /* special case where we have * as uri, keep uri = NULL */
1817 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1820 /* get the session if there is any */
1821 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1822 if (res == GST_RTSP_OK) {
1823 if (priv->session_pool == NULL)
1826 /* we had a session in the request, find it again */
1827 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1828 goto session_not_found;
1830 /* we add the session to the client list of watched sessions. When a session
1831 * disappears because it times out, we will be notified. If all sessions are
1832 * gone, we will close the connection */
1833 client_watch_session (client, session);
1836 /* sanitize the uri */
1840 state.session = session;
1843 if (!gst_rtsp_auth_check (priv->auth, client, GST_RTSP_AUTH_CHECK_URL,
1845 goto not_authorized;
1847 state.auth = priv->auth;
1850 /* now see what is asked and dispatch to a dedicated handler */
1852 case GST_RTSP_OPTIONS:
1853 handle_options_request (client, &state);
1855 case GST_RTSP_DESCRIBE:
1856 handle_describe_request (client, &state);
1858 case GST_RTSP_SETUP:
1859 handle_setup_request (client, &state);
1862 handle_play_request (client, &state);
1864 case GST_RTSP_PAUSE:
1865 handle_pause_request (client, &state);
1867 case GST_RTSP_TEARDOWN:
1868 handle_teardown_request (client, &state);
1870 case GST_RTSP_SET_PARAMETER:
1871 handle_set_param_request (client, &state);
1873 case GST_RTSP_GET_PARAMETER:
1874 handle_get_param_request (client, &state);
1876 case GST_RTSP_ANNOUNCE:
1877 case GST_RTSP_RECORD:
1878 case GST_RTSP_REDIRECT:
1879 goto not_implemented;
1880 case GST_RTSP_INVALID:
1887 g_object_unref (session);
1889 gst_rtsp_url_free (uri);
1895 GST_ERROR ("client %p: version %d not supported", client, version);
1896 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1902 GST_ERROR ("client %p: bad request", client);
1903 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1908 GST_ERROR ("client %p: no pool configured", client);
1909 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1914 GST_ERROR ("client %p: session not found", client);
1915 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1920 GST_ERROR ("client %p: not allowed", client);
1921 handle_unauthorized_request (client, priv->auth, &state);
1926 GST_ERROR ("client %p: method %d not implemented", client, method);
1927 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1933 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1935 GstRTSPClientPrivate *priv = client->priv;
1944 /* find the stream for this message */
1945 res = gst_rtsp_message_parse_data (message, &channel);
1946 if (res != GST_RTSP_OK)
1949 gst_rtsp_message_steal_body (message, &data, &size);
1951 buffer = gst_buffer_new_wrapped (data, size);
1954 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1955 GstRTSPStreamTransport *trans;
1956 GstRTSPStream *stream;
1957 const GstRTSPTransport *tr;
1961 tr = gst_rtsp_stream_transport_get_transport (trans);
1962 stream = gst_rtsp_stream_transport_get_stream (trans);
1964 /* check for TCP transport */
1965 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1966 /* dispatch to the stream based on the channel number */
1967 if (tr->interleaved.min == channel) {
1968 gst_rtsp_stream_recv_rtp (stream, buffer);
1971 } else if (tr->interleaved.max == channel) {
1972 gst_rtsp_stream_recv_rtcp (stream, buffer);
1979 gst_buffer_unref (buffer);
1983 * gst_rtsp_client_set_session_pool:
1984 * @client: a #GstRTSPClient
1985 * @pool: a #GstRTSPSessionPool
1987 * Set @pool as the sessionpool for @client which it will use to find
1988 * or allocate sessions. the sessionpool is usually inherited from the server
1989 * that created the client but can be overridden later.
1992 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
1993 GstRTSPSessionPool * pool)
1995 GstRTSPSessionPool *old;
1996 GstRTSPClientPrivate *priv;
1998 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2000 priv = client->priv;
2003 g_object_ref (pool);
2005 g_mutex_lock (&priv->lock);
2006 old = priv->session_pool;
2007 priv->session_pool = pool;
2008 g_mutex_unlock (&priv->lock);
2011 g_object_unref (old);
2015 * gst_rtsp_client_get_session_pool:
2016 * @client: a #GstRTSPClient
2018 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2020 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2022 GstRTSPSessionPool *
2023 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2025 GstRTSPClientPrivate *priv;
2026 GstRTSPSessionPool *result;
2028 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2030 priv = client->priv;
2032 g_mutex_lock (&priv->lock);
2033 if ((result = priv->session_pool))
2034 g_object_ref (result);
2035 g_mutex_unlock (&priv->lock);
2041 * gst_rtsp_client_set_mount_points:
2042 * @client: a #GstRTSPClient
2043 * @mounts: a #GstRTSPMountPoints
2045 * Set @mounts as the mount points for @client which it will use to map urls
2046 * to media streams. These mount points are usually inherited from the server that
2047 * created the client but can be overriden later.
2050 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2051 GstRTSPMountPoints * mounts)
2053 GstRTSPClientPrivate *priv;
2054 GstRTSPMountPoints *old;
2056 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2058 priv = client->priv;
2061 g_object_ref (mounts);
2063 g_mutex_lock (&priv->lock);
2064 old = priv->mount_points;
2065 priv->mount_points = mounts;
2066 g_mutex_unlock (&priv->lock);
2069 g_object_unref (old);
2073 * gst_rtsp_client_get_mount_points:
2074 * @client: a #GstRTSPClient
2076 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2078 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2080 GstRTSPMountPoints *
2081 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2083 GstRTSPClientPrivate *priv;
2084 GstRTSPMountPoints *result;
2086 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2088 priv = client->priv;
2090 g_mutex_lock (&priv->lock);
2091 if ((result = priv->mount_points))
2092 g_object_ref (result);
2093 g_mutex_unlock (&priv->lock);
2099 * gst_rtsp_client_set_use_client_settings:
2100 * @client: a #GstRTSPClient
2101 * @use_client_settings: whether to use client settings for multicast
2103 * Use client transport settings (destination and ttl) for multicast.
2104 * When @use_client_settings is %FALSE, the server settings will be
2108 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
2109 gboolean use_client_settings)
2111 GstRTSPClientPrivate *priv;
2113 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2115 priv = client->priv;
2117 g_mutex_lock (&priv->lock);
2118 priv->use_client_settings = use_client_settings;
2119 g_mutex_unlock (&priv->lock);
2123 * gst_rtsp_client_get_use_client_settings:
2124 * @client: a #GstRTSPClient
2126 * Check if client transport settings (destination and ttl) for multicast
2130 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
2132 GstRTSPClientPrivate *priv;
2135 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2137 priv = client->priv;
2139 g_mutex_lock (&priv->lock);
2140 res = priv->use_client_settings;
2141 g_mutex_unlock (&priv->lock);
2147 * gst_rtsp_client_set_auth:
2148 * @client: a #GstRTSPClient
2149 * @auth: a #GstRTSPAuth
2151 * configure @auth to be used as the authentication manager of @client.
2154 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2156 GstRTSPClientPrivate *priv;
2159 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2161 priv = client->priv;
2164 g_object_ref (auth);
2166 g_mutex_lock (&priv->lock);
2169 g_mutex_unlock (&priv->lock);
2172 g_object_unref (old);
2177 * gst_rtsp_client_get_auth:
2178 * @client: a #GstRTSPClient
2180 * Get the #GstRTSPAuth used as the authentication manager of @client.
2182 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2186 gst_rtsp_client_get_auth (GstRTSPClient * client)
2188 GstRTSPClientPrivate *priv;
2189 GstRTSPAuth *result;
2191 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2193 priv = client->priv;
2195 g_mutex_lock (&priv->lock);
2196 if ((result = priv->auth))
2197 g_object_ref (result);
2198 g_mutex_unlock (&priv->lock);
2204 * gst_rtsp_client_set_connection:
2205 * @client: a #GstRTSPClient
2206 * @conn: (transfer full): a #GstRTSPConnection
2208 * Set the #GstRTSPConnection of @client. This function takes ownership of
2211 * Returns: %TRUE on success.
2214 gst_rtsp_client_set_connection (GstRTSPClient * client,
2215 GstRTSPConnection * conn)
2217 GstRTSPClientPrivate *priv;
2218 GSocket *read_socket;
2219 GSocketAddress *address;
2221 GError *error = NULL;
2223 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2224 g_return_val_if_fail (conn != NULL, FALSE);
2226 priv = client->priv;
2228 read_socket = gst_rtsp_connection_get_read_socket (conn);
2230 if (!(address = g_socket_get_local_address (read_socket, &error)))
2233 g_free (priv->server_ip);
2234 /* keep the original ip that the client connected to */
2235 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2236 GInetAddress *iaddr;
2238 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2240 /* socket might be ipv6 but adress still ipv4 */
2241 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2242 priv->server_ip = g_inet_address_to_string (iaddr);
2243 g_object_unref (address);
2245 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2246 priv->server_ip = g_strdup ("unknown");
2249 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2250 priv->server_ip, priv->is_ipv6);
2252 url = gst_rtsp_connection_get_url (conn);
2253 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2255 priv->connection = conn;
2262 GST_ERROR ("could not get local address %s", error->message);
2263 g_error_free (error);
2269 * gst_rtsp_client_get_connection:
2270 * @client: a #GstRTSPClient
2272 * Get the #GstRTSPConnection of @client.
2274 * Returns: (transfer none): the #GstRTSPConnection of @client.
2275 * The connection object returned remains valid until the client is freed.
2278 gst_rtsp_client_get_connection (GstRTSPClient * client)
2280 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2282 return client->priv->connection;
2286 * gst_rtsp_client_set_send_func:
2287 * @client: a #GstRTSPClient
2288 * @func: a #GstRTSPClientSendFunc
2289 * @user_data: user data passed to @func
2290 * @notify: called when @user_data is no longer in use
2292 * Set @func as the callback that will be called when a new message needs to be
2293 * sent to the client. @user_data is passed to @func and @notify is called when
2294 * @user_data is no longer in use.
2297 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2298 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2300 GstRTSPClientPrivate *priv;
2301 GDestroyNotify old_notify;
2304 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2306 priv = client->priv;
2308 g_mutex_lock (&priv->send_lock);
2309 priv->send_func = func;
2310 old_notify = priv->send_notify;
2311 old_data = priv->send_data;
2312 priv->send_notify = notify;
2313 priv->send_data = user_data;
2314 g_mutex_unlock (&priv->send_lock);
2317 old_notify (old_data);
2321 * gst_rtsp_client_handle_message:
2322 * @client: a #GstRTSPClient
2323 * @message: an #GstRTSPMessage
2325 * Let the client handle @message.
2327 * Returns: a #GstRTSPResult.
2330 gst_rtsp_client_handle_message (GstRTSPClient * client,
2331 GstRTSPMessage * message)
2333 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2334 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2336 switch (message->type) {
2337 case GST_RTSP_MESSAGE_REQUEST:
2338 handle_request (client, message);
2340 case GST_RTSP_MESSAGE_RESPONSE:
2342 case GST_RTSP_MESSAGE_DATA:
2343 handle_data (client, message);
2352 * gst_rtsp_client_send_request:
2353 * @client: a #GstRTSPClient
2354 * @session: a #GstRTSPSession to send the request to or %NULL
2355 * @request: The request #GstRTSPMessage to send
2357 * Send a request message to the client.
2360 gst_rtsp_client_send_request (GstRTSPClient * client, GstRTSPSession * session,
2361 GstRTSPMessage * request)
2363 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2364 g_return_val_if_fail (request != NULL, GST_RTSP_EINVAL);
2365 g_return_val_if_fail (request->type == GST_RTSP_MESSAGE_REQUEST,
2368 send_message (client, session, request, FALSE);
2373 static GstRTSPResult
2374 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2375 gboolean close, gpointer user_data)
2377 GstRTSPClientPrivate *priv = client->priv;
2379 /* send the response and store the seq number so we can wait until it's
2380 * written to the client to close the connection */
2381 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2382 &priv->close_seq : NULL);
2385 static GstRTSPResult
2386 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2389 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2392 static GstRTSPResult
2393 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2395 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2396 GstRTSPClientPrivate *priv = client->priv;
2398 if (priv->close_seq && priv->close_seq == cseq) {
2399 priv->close_seq = 0;
2400 close_connection (client);
2406 static GstRTSPResult
2407 closed (GstRTSPWatch * watch, gpointer user_data)
2409 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2410 GstRTSPClientPrivate *priv = client->priv;
2411 const gchar *tunnelid;
2413 GST_INFO ("client %p: connection closed", client);
2415 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2416 g_mutex_lock (&tunnels_lock);
2417 /* remove from tunnelids */
2418 g_hash_table_remove (tunnels, tunnelid);
2419 g_mutex_unlock (&tunnels_lock);
2422 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2427 static GstRTSPResult
2428 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2430 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2433 str = gst_rtsp_strresult (result);
2434 GST_INFO ("client %p: received an error %s", client, str);
2440 static GstRTSPResult
2441 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2442 GstRTSPMessage * message, guint id, gpointer user_data)
2444 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2447 str = gst_rtsp_strresult (result);
2449 ("client %p: error when handling message %p with id %d: %s",
2450 client, message, id, str);
2457 remember_tunnel (GstRTSPClient * client)
2459 GstRTSPClientPrivate *priv = client->priv;
2460 const gchar *tunnelid;
2462 /* store client in the pending tunnels */
2463 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2464 if (tunnelid == NULL)
2467 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2469 /* we can't have two clients connecting with the same tunnelid */
2470 g_mutex_lock (&tunnels_lock);
2471 if (g_hash_table_lookup (tunnels, tunnelid))
2472 goto tunnel_existed;
2474 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2475 g_mutex_unlock (&tunnels_lock);
2482 GST_ERROR ("client %p: no tunnelid provided", client);
2487 g_mutex_unlock (&tunnels_lock);
2488 GST_ERROR ("client %p: tunnel session %s already existed", client,
2494 static GstRTSPStatusCode
2495 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2497 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2498 GstRTSPClientPrivate *priv = client->priv;
2500 GST_INFO ("client %p: tunnel start (connection %p)", client,
2503 if (!remember_tunnel (client))
2506 return GST_RTSP_STS_OK;
2511 GST_ERROR ("client %p: error starting tunnel", client);
2512 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2516 static GstRTSPResult
2517 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2519 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2520 GstRTSPClientPrivate *priv = client->priv;
2522 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2525 /* ignore error, it'll only be a problem when the client does a POST again */
2526 remember_tunnel (client);
2531 static GstRTSPResult
2532 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2534 const gchar *tunnelid;
2535 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2536 GstRTSPClientPrivate *priv = client->priv;
2537 GstRTSPClient *oclient;
2538 GstRTSPClientPrivate *opriv;
2540 GST_INFO ("client %p: tunnel complete", client);
2542 /* find previous tunnel */
2543 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2544 if (tunnelid == NULL)
2547 g_mutex_lock (&tunnels_lock);
2548 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2551 /* remove the old client from the table. ref before because removing it will
2552 * remove the ref to it. */
2553 g_object_ref (oclient);
2554 g_hash_table_remove (tunnels, tunnelid);
2556 opriv = oclient->priv;
2558 if (opriv->watch == NULL)
2560 g_mutex_unlock (&tunnels_lock);
2562 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2563 opriv->connection, priv->connection);
2565 /* merge the tunnels into the first client */
2566 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2567 gst_rtsp_watch_reset (opriv->watch);
2568 g_object_unref (oclient);
2575 GST_ERROR ("client %p: no tunnelid provided", client);
2576 return GST_RTSP_ERROR;
2580 g_mutex_unlock (&tunnels_lock);
2581 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2582 return GST_RTSP_ERROR;
2586 g_mutex_unlock (&tunnels_lock);
2587 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2588 g_object_unref (oclient);
2589 return GST_RTSP_ERROR;
2593 static GstRTSPWatchFuncs watch_funcs = {
2605 client_watch_notify (GstRTSPClient * client)
2607 GstRTSPClientPrivate *priv = client->priv;
2609 GST_INFO ("client %p: watch destroyed", client);
2611 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2612 g_object_unref (client);
2616 * gst_rtsp_client_attach:
2617 * @client: a #GstRTSPClient
2618 * @context: (allow-none): a #GMainContext
2620 * Attaches @client to @context. When the mainloop for @context is run, the
2621 * client will be dispatched. When @context is NULL, the default context will be
2624 * This function should be called when the client properties and urls are fully
2625 * configured and the client is ready to start.
2627 * Returns: the ID (greater than 0) for the source within the GMainContext.
2630 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2632 GstRTSPClientPrivate *priv;
2635 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2636 priv = client->priv;
2637 g_return_val_if_fail (priv->watch == NULL, 0);
2639 /* create watch for the connection and attach */
2640 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2641 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2642 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2643 (GDestroyNotify) gst_rtsp_watch_unref);
2645 /* FIXME make this configurable. We don't want to do this yet because it will
2646 * be superceeded by a cache object later */
2647 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2649 GST_INFO ("attaching to context %p", context);
2650 res = gst_rtsp_watch_attach (priv->watch, context);
2656 * gst_rtsp_client_session_filter:
2657 * @client: a #GstRTSPclient
2658 * @func: (scope call): a callback
2659 * @user_data: user data passed to @func
2661 * Call @func for each session managed by @client. The result value of @func
2662 * determines what happens to the session. @func will be called with @client
2663 * locked so no further actions on @client can be performed from @func.
2665 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
2668 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
2670 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
2671 * will also be added with an additional ref to the result #GList of this
2674 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
2675 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2676 * element in the #GList should be unreffed before the list is freed.
2679 gst_rtsp_client_session_filter (GstRTSPClient * client,
2680 GstRTSPClientSessionFilterFunc func, gpointer user_data)
2682 GstRTSPClientPrivate *priv;
2683 GList *result, *walk, *next;
2685 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2686 g_return_val_if_fail (func != NULL, NULL);
2688 priv = client->priv;
2692 g_mutex_lock (&priv->lock);
2693 for (walk = priv->sessions; walk; walk = next) {
2694 GstRTSPSession *sess = walk->data;
2696 next = g_list_next (walk);
2698 switch (func (client, sess, user_data)) {
2699 case GST_RTSP_FILTER_REMOVE:
2700 /* stop watching the session and pretent it went away */
2701 client_cleanup_session (client, sess);
2703 case GST_RTSP_FILTER_REF:
2704 result = g_list_prepend (result, g_object_ref (sess));
2706 case GST_RTSP_FILTER_KEEP:
2711 g_mutex_unlock (&priv->lock);