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 (auth, client, 0, 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;
494 if (!priv->mount_points)
495 goto no_mount_points;
497 path = state->uri->abspath;
499 /* find the longest matching factory for the uri first */
500 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
507 path_len = strlen (path);
509 if (!paths_are_equal (priv->path, path, path_len)) {
510 /* remove any previously cached values before we try to construct a new
516 gst_rtsp_media_unprepare (priv->media);
517 g_object_unref (priv->media);
521 /* check if we have access to the factory */
522 if ((auth = gst_rtsp_media_factory_get_auth (factory))) {
523 state->factory = factory;
525 if (!gst_rtsp_auth_check (auth, client, 0, state))
528 state->factory = NULL;
529 g_object_unref (auth);
532 /* prepare the media and add it to the pipeline */
533 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
536 /* prepare the media */
537 if (!(gst_rtsp_media_prepare (media)))
540 /* now keep track of the uri and the media */
541 priv->path = g_strndup (path, path_len);
543 state->media = media;
545 /* we have seen this path before, used cached media */
547 state->media = media;
548 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
551 g_object_unref (factory);
554 g_object_ref (media);
561 GST_ERROR ("client %p: no mount points configured", client);
562 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
567 GST_ERROR ("client %p: no factory for uri %s", client, path);
568 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
573 GST_ERROR ("client %p: unauthorized request", client);
574 handle_unauthorized_request (client, auth, state);
575 g_object_unref (factory);
576 state->factory = NULL;
577 g_object_unref (auth);
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);
589 GST_ERROR ("client %p: can't prepare media", client);
590 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
591 g_object_unref (media);
592 g_object_unref (factory);
598 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
600 GstRTSPClientPrivate *priv = client->priv;
601 GstRTSPMessage message = { 0 };
606 gst_rtsp_message_init_data (&message, channel);
608 /* FIXME, need some sort of iovec RTSPMessage here */
609 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
612 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
614 g_mutex_lock (&priv->send_lock);
616 priv->send_func (client, &message, FALSE, priv->send_data);
617 g_mutex_unlock (&priv->send_lock);
619 gst_rtsp_message_steal_body (&message, &data, &usize);
620 gst_buffer_unmap (buffer, &map_info);
622 gst_rtsp_message_unset (&message);
628 link_transport (GstRTSPClient * client, GstRTSPSession * session,
629 GstRTSPStreamTransport * trans)
631 GstRTSPClientPrivate *priv = client->priv;
633 GST_DEBUG ("client %p: linking transport %p", client, trans);
635 gst_rtsp_stream_transport_set_callbacks (trans,
636 (GstRTSPSendFunc) do_send_data,
637 (GstRTSPSendFunc) do_send_data, client, NULL);
639 priv->transports = g_list_prepend (priv->transports, trans);
641 /* make sure our session can't expire */
642 gst_rtsp_session_prevent_expire (session);
646 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
647 GstRTSPStreamTransport * trans)
649 GstRTSPClientPrivate *priv = client->priv;
651 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
653 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
655 priv->transports = g_list_remove (priv->transports, trans);
657 /* our session can now expire */
658 gst_rtsp_session_allow_expire (session);
662 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
663 GstRTSPSessionMedia * sessmedia)
668 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
669 for (i = 0; i < n_streams; i++) {
670 GstRTSPStreamTransport *trans;
671 const GstRTSPTransport *tr;
673 /* get the transport, if there is no transport configured, skip this stream */
674 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
678 tr = gst_rtsp_stream_transport_get_transport (trans);
680 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
681 /* for TCP, unlink the stream from the TCP connection of the client */
682 unlink_transport (client, session, trans);
688 close_connection (GstRTSPClient * client)
690 GstRTSPClientPrivate *priv = client->priv;
691 const gchar *tunnelid;
693 GST_DEBUG ("client %p: closing connection", client);
695 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
696 g_mutex_lock (&tunnels_lock);
697 /* remove from tunnelids */
698 g_hash_table_remove (tunnels, tunnelid);
699 g_mutex_unlock (&tunnels_lock);
702 gst_rtsp_connection_close (priv->connection);
706 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
708 GstRTSPClientPrivate *priv = client->priv;
709 GstRTSPSession *session;
710 GstRTSPSessionMedia *sessmedia;
711 GstRTSPStatusCode code;
718 session = state->session;
723 path = state->uri->abspath;
725 /* get a handle to the configuration of the media in the session */
726 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
730 /* only aggregate control for now.. */
731 if (path[matched] != '\0')
734 state->sessmedia = sessmedia;
736 /* we emit the signal before closing the connection */
737 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
740 /* unlink the all TCP callbacks */
741 unlink_session_transports (client, session, sessmedia);
743 /* remove the session from the watched sessions */
744 client_unwatch_session (client, session);
746 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
748 /* unmanage the media in the session, returns false if all media session
750 if (!gst_rtsp_session_release_media (session, sessmedia)) {
751 /* remove the session */
752 gst_rtsp_session_pool_remove (priv->session_pool, session);
754 /* construct the response now */
755 code = GST_RTSP_STS_OK;
756 gst_rtsp_message_init_response (state->response, code,
757 gst_rtsp_status_as_text (code), state->request);
759 send_message (client, session, state->response, TRUE);
766 GST_ERROR ("client %p: no session", client);
767 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
772 GST_ERROR ("client %p: no uri supplied", client);
773 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
778 GST_ERROR ("client %p: no media for uri", client);
779 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
784 GST_ERROR ("client %p: no aggregate path %s", client, path);
785 send_generic_response (client,
786 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
792 default_params_set (GstRTSPClient * client, GstRTSPClientState * state)
796 res = gst_rtsp_params_set (client, state);
802 default_params_get (GstRTSPClient * client, GstRTSPClientState * state)
806 res = gst_rtsp_params_get (client, state);
812 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
818 res = gst_rtsp_message_get_body (state->request, &data, &size);
819 if (res != GST_RTSP_OK)
823 /* no body, keep-alive request */
824 send_generic_response (client, GST_RTSP_STS_OK, state);
826 /* there is a body, handle the params */
827 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, state);
828 if (res != GST_RTSP_OK)
831 send_message (client, state->session, state->response, FALSE);
834 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
842 GST_ERROR ("client %p: bad request", client);
843 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
849 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
855 res = gst_rtsp_message_get_body (state->request, &data, &size);
856 if (res != GST_RTSP_OK)
860 /* no body, keep-alive request */
861 send_generic_response (client, GST_RTSP_STS_OK, state);
863 /* there is a body, handle the params */
864 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, state);
865 if (res != GST_RTSP_OK)
868 send_message (client, state->session, state->response, FALSE);
871 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
879 GST_ERROR ("client %p: bad request", client);
880 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
886 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
888 GstRTSPSession *session;
889 GstRTSPSessionMedia *sessmedia;
890 GstRTSPStatusCode code;
891 GstRTSPState rtspstate;
895 if (!(session = state->session))
901 path = state->uri->abspath;
903 /* get a handle to the configuration of the media in the session */
904 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
908 if (path[matched] != '\0')
911 state->sessmedia = sessmedia;
913 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
914 /* the session state must be playing or recording */
915 if (rtspstate != GST_RTSP_STATE_PLAYING &&
916 rtspstate != GST_RTSP_STATE_RECORDING)
919 /* unlink the all TCP callbacks */
920 unlink_session_transports (client, session, sessmedia);
922 /* then pause sending */
923 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
925 /* construct the response now */
926 code = GST_RTSP_STS_OK;
927 gst_rtsp_message_init_response (state->response, code,
928 gst_rtsp_status_as_text (code), state->request);
930 send_message (client, session, state->response, FALSE);
932 /* the state is now READY */
933 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
935 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
943 GST_ERROR ("client %p: no seesion", client);
944 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
949 GST_ERROR ("client %p: no uri supplied", client);
950 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
955 GST_ERROR ("client %p: no media for uri", client);
956 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
961 GST_ERROR ("client %p: no aggregate path %s", client, path);
962 send_generic_response (client,
963 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
968 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
969 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
976 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
978 GstRTSPSession *session;
979 GstRTSPSessionMedia *sessmedia;
981 GstRTSPStatusCode code;
983 guint n_streams, i, infocount;
985 GstRTSPTimeRange *range;
987 GstRTSPState rtspstate;
988 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
992 if (!(session = state->session))
998 path = state->uri->abspath;
1000 /* get a handle to the configuration of the media in the session */
1001 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1005 if (path[matched] != '\0')
1008 state->sessmedia = sessmedia;
1009 state->media = media = gst_rtsp_session_media_get_media (sessmedia);
1011 /* the session state must be playing or ready */
1012 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1013 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1016 /* parse the range header if we have one */
1018 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
1019 if (res == GST_RTSP_OK) {
1020 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1021 /* we have a range, seek to the position */
1023 gst_rtsp_media_seek (media, range);
1024 gst_rtsp_range_free (range);
1028 /* grab RTPInfo from the payloaders now */
1029 rtpinfo = g_string_new ("");
1031 n_streams = gst_rtsp_media_n_streams (media);
1032 for (i = 0, infocount = 0; i < n_streams; i++) {
1033 GstRTSPStreamTransport *trans;
1034 GstRTSPStream *stream;
1035 const GstRTSPTransport *tr;
1039 /* get the transport, if there is no transport configured, skip this stream */
1040 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
1041 if (trans == NULL) {
1042 GST_INFO ("stream %d is not configured", i);
1045 tr = gst_rtsp_stream_transport_get_transport (trans);
1047 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1048 /* for TCP, link the stream to the TCP connection of the client */
1049 link_transport (client, session, trans);
1052 stream = gst_rtsp_stream_transport_get_stream (trans);
1053 if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
1055 g_string_append (rtpinfo, ", ");
1057 uristr = gst_rtsp_url_get_request_uri (state->uri);
1058 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
1059 uristr, i, seq, rtptime);
1064 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
1068 /* construct the response now */
1069 code = GST_RTSP_STS_OK;
1070 gst_rtsp_message_init_response (state->response, code,
1071 gst_rtsp_status_as_text (code), state->request);
1073 /* add the RTP-Info header */
1074 if (infocount > 0) {
1075 str = g_string_free (rtpinfo, FALSE);
1076 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
1078 g_string_free (rtpinfo, TRUE);
1082 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1083 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
1085 send_message (client, session, state->response, FALSE);
1087 /* start playing after sending the request */
1088 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1090 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1092 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
1100 GST_ERROR ("client %p: no session", client);
1101 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1106 GST_ERROR ("client %p: no uri supplied", client);
1107 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1112 GST_ERROR ("client %p: media not found", client);
1113 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1118 GST_ERROR ("client %p: no aggregate path %s", client, path);
1119 send_generic_response (client,
1120 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
1125 GST_ERROR ("client %p: not PLAYING or READY", client);
1126 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1133 do_keepalive (GstRTSPSession * session)
1135 GST_INFO ("keep session %p alive", session);
1136 gst_rtsp_session_touch (session);
1139 /* parse @transport and return a valid transport in @tr. only transports
1140 * from @supported are returned. Returns FALSE if no valid transport
1143 parse_transport (const char *transport, GstRTSPLowerTrans supported,
1144 GstRTSPTransport * tr)
1151 gst_rtsp_transport_init (tr);
1153 GST_DEBUG ("parsing transports %s", transport);
1155 transports = g_strsplit (transport, ",", 0);
1157 /* loop through the transports, try to parse */
1158 for (i = 0; transports[i]; i++) {
1159 res = gst_rtsp_transport_parse (transports[i], tr);
1160 if (res != GST_RTSP_OK) {
1161 /* no valid transport, search some more */
1162 GST_WARNING ("could not parse transport %s", transports[i]);
1166 /* we have a transport, see if it's RTP/AVP */
1167 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1168 GST_WARNING ("invalid transport %s", transports[i]);
1172 if (!(tr->lower_transport & supported)) {
1173 GST_WARNING ("unsupported transport %s", transports[i]);
1177 /* we have a valid transport */
1178 GST_INFO ("found valid transport %s", transports[i]);
1183 gst_rtsp_transport_init (tr);
1185 g_strfreev (transports);
1191 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
1192 GstRTSPMessage * request)
1194 gchar *blocksize_str;
1195 gboolean ret = TRUE;
1197 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1198 &blocksize_str, 0) == GST_RTSP_OK) {
1202 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1203 if (end == blocksize_str) {
1204 GST_ERROR ("failed to parse blocksize");
1207 /* we don't want to change the mtu when this media
1208 * can be shared because it impacts other clients */
1209 if (gst_rtsp_media_is_shared (media))
1212 if (blocksize > G_MAXUINT)
1213 blocksize = G_MAXUINT;
1214 gst_rtsp_stream_set_mtu (stream, blocksize);
1221 default_configure_client_transport (GstRTSPClient * client,
1222 GstRTSPClientState * state, GstRTSPTransport * ct)
1224 GstRTSPClientPrivate *priv = client->priv;
1226 /* we have a valid transport now, set the destination of the client. */
1227 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1228 if (ct->destination && priv->use_client_settings) {
1229 GstRTSPAddress *addr;
1231 addr = gst_rtsp_stream_reserve_address (state->stream, ct->destination,
1232 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1237 gst_rtsp_address_free (addr);
1239 GstRTSPAddress *addr;
1240 GSocketFamily family;
1242 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1244 addr = gst_rtsp_stream_get_multicast_address (state->stream, family);
1248 g_free (ct->destination);
1249 ct->destination = g_strdup (addr->address);
1250 ct->port.min = addr->port;
1251 ct->port.max = addr->port + addr->n_ports - 1;
1252 ct->ttl = addr->ttl;
1254 gst_rtsp_address_free (addr);
1259 url = gst_rtsp_connection_get_url (priv->connection);
1260 g_free (ct->destination);
1261 ct->destination = g_strdup (url->host);
1263 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1264 /* check if the client selected channels for TCP */
1265 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1266 gst_rtsp_session_media_alloc_channels (state->sessmedia,
1276 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1281 static GstRTSPTransport *
1282 make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
1283 GstRTSPTransport * ct)
1285 GstRTSPTransport *st;
1287 GSocketFamily family;
1289 /* prepare the server transport */
1290 gst_rtsp_transport_new (&st);
1292 st->trans = ct->trans;
1293 st->profile = ct->profile;
1294 st->lower_transport = ct->lower_transport;
1296 addr = g_inet_address_new_from_string (ct->destination);
1299 GST_ERROR ("failed to get inet addr from client destination");
1300 family = G_SOCKET_FAMILY_IPV4;
1302 family = g_inet_address_get_family (addr);
1303 g_object_unref (addr);
1307 switch (st->lower_transport) {
1308 case GST_RTSP_LOWER_TRANS_UDP:
1309 st->client_port = ct->client_port;
1310 gst_rtsp_stream_get_server_port (state->stream, &st->server_port, family);
1312 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1313 st->port = ct->port;
1314 st->destination = g_strdup (ct->destination);
1317 case GST_RTSP_LOWER_TRANS_TCP:
1318 st->interleaved = ct->interleaved;
1323 gst_rtsp_stream_get_ssrc (state->stream, &st->ssrc);
1329 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
1331 GstRTSPClientPrivate *priv = client->priv;
1335 GstRTSPTransport *ct, *st;
1336 GstRTSPLowerTrans supported;
1337 GstRTSPStatusCode code;
1338 GstRTSPSession *session;
1339 GstRTSPStreamTransport *trans;
1341 GstRTSPSessionMedia *sessmedia;
1342 GstRTSPMedia *media;
1343 GstRTSPStream *stream;
1344 GstRTSPState rtspstate;
1345 GstRTSPClientClass *klass;
1346 gchar *path, *control;
1353 path = uri->abspath;
1355 /* parse the transport */
1357 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
1359 if (res != GST_RTSP_OK)
1362 /* we create the session after parsing stuff so that we don't make
1363 * a session for malformed requests */
1364 if (priv->session_pool == NULL)
1367 session = state->session;
1370 g_object_ref (session);
1371 /* get a handle to the configuration of the media in the session, this can
1372 * return NULL if this is a new url to manage in this session. */
1373 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1375 /* we need a new media configuration in this session */
1379 /* we have no session media, find one and manage it */
1380 if (sessmedia == NULL) {
1381 /* get a handle to the configuration of the media in the session */
1382 media = find_media (client, state, &matched);
1384 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1385 g_object_ref (media);
1387 /* no media, not found then */
1389 goto media_not_found;
1391 /* path is what matched. We can modify the parsed uri in place */
1392 path[matched] = '\0';
1393 /* control is remainder */
1394 control = &path[matched + 1];
1396 /* find the stream now using the control part */
1397 stream = gst_rtsp_media_find_stream (media, control);
1399 goto stream_not_found;
1401 /* now we have a uri identifying a valid media and stream */
1402 state->stream = stream;
1403 state->media = media;
1405 if (session == NULL) {
1406 /* create a session if this fails we probably reached our session limit or
1408 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1409 goto service_unavailable;
1411 /* make sure this client is closed when the session is closed */
1412 client_watch_session (client, session);
1414 /* signal new session */
1415 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1418 state->session = session;
1421 if (sessmedia == NULL) {
1422 /* manage the media in our session now, if not done already */
1423 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1424 /* if we stil have no media, error */
1425 if (sessmedia == NULL)
1426 goto sessmedia_unavailable;
1428 g_object_unref (media);
1431 state->sessmedia = sessmedia;
1433 /* set blocksize on this stream */
1434 if (!handle_blocksize (media, stream, state->request))
1435 goto invalid_blocksize;
1437 gst_rtsp_transport_new (&ct);
1439 /* our supported transports */
1440 supported = GST_RTSP_LOWER_TRANS_UDP |
1441 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
1443 /* parse and find a usable supported transport */
1444 if (!parse_transport (transport, supported, ct))
1445 goto unsupported_transports;
1447 /* update the client transport */
1448 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1449 if (!klass->configure_client_transport (client, state, ct))
1450 goto unsupported_client_transport;
1452 /* set in the session media transport */
1453 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1455 /* configure keepalive for this transport */
1456 gst_rtsp_stream_transport_set_keepalive (trans,
1457 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1459 /* create and serialize the server transport */
1460 st = make_server_transport (client, state, ct);
1461 trans_str = gst_rtsp_transport_as_text (st);
1462 gst_rtsp_transport_free (st);
1464 /* construct the response now */
1465 code = GST_RTSP_STS_OK;
1466 gst_rtsp_message_init_response (state->response, code,
1467 gst_rtsp_status_as_text (code), state->request);
1469 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1473 send_message (client, session, state->response, FALSE);
1475 /* update the state */
1476 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1477 switch (rtspstate) {
1478 case GST_RTSP_STATE_PLAYING:
1479 case GST_RTSP_STATE_RECORDING:
1480 case GST_RTSP_STATE_READY:
1481 /* no state change */
1484 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1487 g_object_unref (session);
1489 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST],
1497 GST_ERROR ("client %p: no uri", client);
1498 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1503 GST_ERROR ("client %p: no transport", client);
1504 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1509 GST_ERROR ("client %p: no session pool configured", client);
1510 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1515 GST_ERROR ("client %p: media '%s' not found", client, path);
1516 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1521 GST_ERROR ("client %p: stream '%s' not found", client, control);
1522 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1523 g_object_unref (media);
1526 service_unavailable:
1528 GST_ERROR ("client %p: can't create session", client);
1529 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1530 g_object_unref (media);
1533 sessmedia_unavailable:
1535 GST_ERROR ("client %p: can't create session media", client);
1536 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1537 g_object_unref (media);
1538 g_object_unref (session);
1543 GST_ERROR ("client %p: invalid blocksize", client);
1544 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1545 g_object_unref (session);
1548 unsupported_transports:
1550 GST_ERROR ("client %p: unsupported transports", client);
1551 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1552 gst_rtsp_transport_free (ct);
1553 g_object_unref (session);
1556 unsupported_client_transport:
1558 GST_ERROR ("client %p: unsupported client transport", client);
1559 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1560 gst_rtsp_transport_free (ct);
1561 g_object_unref (session);
1566 static GstSDPMessage *
1567 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1569 GstRTSPClientPrivate *priv = client->priv;
1574 gst_sdp_message_new (&sdp);
1576 /* some standard things first */
1577 gst_sdp_message_set_version (sdp, "0");
1584 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1587 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1588 gst_sdp_message_set_information (sdp, "rtsp-server");
1589 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1590 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1591 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1592 gst_sdp_message_add_attribute (sdp, "control", "*");
1594 info.is_ipv6 = priv->is_ipv6;
1595 info.server_ip = priv->server_ip;
1597 /* create an SDP for the media object */
1598 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1606 GST_ERROR ("client %p: could not create SDP", client);
1607 gst_sdp_message_free (sdp);
1612 /* for the describe we must generate an SDP */
1614 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1619 gchar *str, *content_base;
1620 GstRTSPMedia *media;
1621 GstRTSPClientClass *klass;
1623 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1628 /* check what kind of format is accepted, we don't really do anything with it
1629 * and always return SDP for now. */
1634 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1636 if (res == GST_RTSP_ENOTIMPL)
1639 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1643 /* find the media object for the uri */
1644 if (!(media = find_media (client, state, NULL)))
1647 /* create an SDP for the media object on this client */
1648 if (!(sdp = klass->create_sdp (client, media)))
1651 g_object_unref (media);
1653 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1654 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1656 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1659 /* content base for some clients that might screw up creating the setup uri */
1660 str = gst_rtsp_url_get_request_uri (state->uri);
1661 str_len = strlen (str);
1663 /* check for trailing '/' and append one */
1664 if (str[str_len - 1] != '/') {
1665 content_base = g_malloc (str_len + 2);
1666 memcpy (content_base, str, str_len);
1667 content_base[str_len] = '/';
1668 content_base[str_len + 1] = '\0';
1674 GST_INFO ("adding content-base: %s", content_base);
1676 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1678 g_free (content_base);
1680 /* add SDP to the response body */
1681 str = gst_sdp_message_as_text (sdp);
1682 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1683 gst_sdp_message_free (sdp);
1685 send_message (client, state->session, state->response, FALSE);
1687 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1695 GST_ERROR ("client %p: no uri", client);
1696 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1701 GST_ERROR ("client %p: no media", client);
1702 /* error reply is already sent */
1707 GST_ERROR ("client %p: can't create SDP", client);
1708 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1709 g_object_unref (media);
1715 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1717 GstRTSPMethod options;
1720 options = GST_RTSP_DESCRIBE |
1725 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1727 str = gst_rtsp_options_as_text (options);
1729 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1730 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1732 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1735 send_message (client, state->session, state->response, FALSE);
1737 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1743 /* remove duplicate and trailing '/' */
1745 sanitize_uri (GstRTSPUrl * uri)
1749 gboolean have_slash, prev_slash;
1751 s = d = uri->abspath;
1752 len = strlen (uri->abspath);
1756 for (i = 0; i < len; i++) {
1757 have_slash = s[i] == '/';
1759 if (!have_slash || !prev_slash)
1761 prev_slash = have_slash;
1763 len = d - uri->abspath;
1764 /* don't remove the first slash if that's the only thing left */
1765 if (len > 1 && *(d - 1) == '/')
1771 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1773 GstRTSPClientPrivate *priv = client->priv;
1775 GST_INFO ("client %p: session %p finished", client, session);
1777 /* unlink all media managed in this session */
1778 client_unlink_session (client, session);
1780 /* remove the session */
1781 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1782 GST_INFO ("client %p: all sessions finalized, close the connection",
1784 close_connection (client);
1789 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1791 GstRTSPClientPrivate *priv = client->priv;
1792 GstRTSPMethod method;
1793 const gchar *uristr;
1794 GstRTSPUrl *uri = NULL;
1795 GstRTSPVersion version;
1797 GstRTSPSession *session = NULL;
1798 GstRTSPClientState state = { NULL };
1799 GstRTSPMessage response = { 0 };
1802 state.request = request;
1803 state.response = &response;
1805 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1806 gst_rtsp_message_dump (request);
1809 GST_INFO ("client %p: received a request", client);
1811 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1813 /* we can only handle 1.0 requests */
1814 if (version != GST_RTSP_VERSION_1_0)
1817 state.method = method;
1819 /* we always try to parse the url first */
1820 if (strcmp (uristr, "*") == 0) {
1821 /* special case where we have * as uri, keep uri = NULL */
1822 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1825 /* get the session if there is any */
1826 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1827 if (res == GST_RTSP_OK) {
1828 if (priv->session_pool == NULL)
1831 /* we had a session in the request, find it again */
1832 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1833 goto session_not_found;
1835 /* we add the session to the client list of watched sessions. When a session
1836 * disappears because it times out, we will be notified. If all sessions are
1837 * gone, we will close the connection */
1838 client_watch_session (client, session);
1841 /* sanitize the uri */
1845 state.session = session;
1848 if (!gst_rtsp_auth_check (priv->auth, client, 0, &state))
1849 goto not_authorized;
1852 /* now see what is asked and dispatch to a dedicated handler */
1854 case GST_RTSP_OPTIONS:
1855 handle_options_request (client, &state);
1857 case GST_RTSP_DESCRIBE:
1858 handle_describe_request (client, &state);
1860 case GST_RTSP_SETUP:
1861 handle_setup_request (client, &state);
1864 handle_play_request (client, &state);
1866 case GST_RTSP_PAUSE:
1867 handle_pause_request (client, &state);
1869 case GST_RTSP_TEARDOWN:
1870 handle_teardown_request (client, &state);
1872 case GST_RTSP_SET_PARAMETER:
1873 handle_set_param_request (client, &state);
1875 case GST_RTSP_GET_PARAMETER:
1876 handle_get_param_request (client, &state);
1878 case GST_RTSP_ANNOUNCE:
1879 case GST_RTSP_RECORD:
1880 case GST_RTSP_REDIRECT:
1881 goto not_implemented;
1882 case GST_RTSP_INVALID:
1889 g_object_unref (session);
1891 gst_rtsp_url_free (uri);
1897 GST_ERROR ("client %p: version %d not supported", client, version);
1898 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1904 GST_ERROR ("client %p: bad request", client);
1905 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1910 GST_ERROR ("client %p: no pool configured", client);
1911 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1916 GST_ERROR ("client %p: session not found", client);
1917 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1922 GST_ERROR ("client %p: not allowed", client);
1923 handle_unauthorized_request (client, priv->auth, &state);
1928 GST_ERROR ("client %p: method %d not implemented", client, method);
1929 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1935 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1937 GstRTSPClientPrivate *priv = client->priv;
1946 /* find the stream for this message */
1947 res = gst_rtsp_message_parse_data (message, &channel);
1948 if (res != GST_RTSP_OK)
1951 gst_rtsp_message_steal_body (message, &data, &size);
1953 buffer = gst_buffer_new_wrapped (data, size);
1956 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1957 GstRTSPStreamTransport *trans;
1958 GstRTSPStream *stream;
1959 const GstRTSPTransport *tr;
1963 tr = gst_rtsp_stream_transport_get_transport (trans);
1964 stream = gst_rtsp_stream_transport_get_stream (trans);
1966 /* check for TCP transport */
1967 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1968 /* dispatch to the stream based on the channel number */
1969 if (tr->interleaved.min == channel) {
1970 gst_rtsp_stream_recv_rtp (stream, buffer);
1973 } else if (tr->interleaved.max == channel) {
1974 gst_rtsp_stream_recv_rtcp (stream, buffer);
1981 gst_buffer_unref (buffer);
1985 * gst_rtsp_client_set_session_pool:
1986 * @client: a #GstRTSPClient
1987 * @pool: a #GstRTSPSessionPool
1989 * Set @pool as the sessionpool for @client which it will use to find
1990 * or allocate sessions. the sessionpool is usually inherited from the server
1991 * that created the client but can be overridden later.
1994 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
1995 GstRTSPSessionPool * pool)
1997 GstRTSPSessionPool *old;
1998 GstRTSPClientPrivate *priv;
2000 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2002 priv = client->priv;
2005 g_object_ref (pool);
2007 g_mutex_lock (&priv->lock);
2008 old = priv->session_pool;
2009 priv->session_pool = pool;
2010 g_mutex_unlock (&priv->lock);
2013 g_object_unref (old);
2017 * gst_rtsp_client_get_session_pool:
2018 * @client: a #GstRTSPClient
2020 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2022 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2024 GstRTSPSessionPool *
2025 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2027 GstRTSPClientPrivate *priv;
2028 GstRTSPSessionPool *result;
2030 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2032 priv = client->priv;
2034 g_mutex_lock (&priv->lock);
2035 if ((result = priv->session_pool))
2036 g_object_ref (result);
2037 g_mutex_unlock (&priv->lock);
2043 * gst_rtsp_client_set_mount_points:
2044 * @client: a #GstRTSPClient
2045 * @mounts: a #GstRTSPMountPoints
2047 * Set @mounts as the mount points for @client which it will use to map urls
2048 * to media streams. These mount points are usually inherited from the server that
2049 * created the client but can be overriden later.
2052 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2053 GstRTSPMountPoints * mounts)
2055 GstRTSPClientPrivate *priv;
2056 GstRTSPMountPoints *old;
2058 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2060 priv = client->priv;
2063 g_object_ref (mounts);
2065 g_mutex_lock (&priv->lock);
2066 old = priv->mount_points;
2067 priv->mount_points = mounts;
2068 g_mutex_unlock (&priv->lock);
2071 g_object_unref (old);
2075 * gst_rtsp_client_get_mount_points:
2076 * @client: a #GstRTSPClient
2078 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2080 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2082 GstRTSPMountPoints *
2083 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2085 GstRTSPClientPrivate *priv;
2086 GstRTSPMountPoints *result;
2088 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2090 priv = client->priv;
2092 g_mutex_lock (&priv->lock);
2093 if ((result = priv->mount_points))
2094 g_object_ref (result);
2095 g_mutex_unlock (&priv->lock);
2101 * gst_rtsp_client_set_use_client_settings:
2102 * @client: a #GstRTSPClient
2103 * @use_client_settings: whether to use client settings for multicast
2105 * Use client transport settings (destination and ttl) for multicast.
2106 * When @use_client_settings is %FALSE, the server settings will be
2110 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
2111 gboolean use_client_settings)
2113 GstRTSPClientPrivate *priv;
2115 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2117 priv = client->priv;
2119 g_mutex_lock (&priv->lock);
2120 priv->use_client_settings = use_client_settings;
2121 g_mutex_unlock (&priv->lock);
2125 * gst_rtsp_client_get_use_client_settings:
2126 * @client: a #GstRTSPClient
2128 * Check if client transport settings (destination and ttl) for multicast
2132 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
2134 GstRTSPClientPrivate *priv;
2137 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2139 priv = client->priv;
2141 g_mutex_lock (&priv->lock);
2142 res = priv->use_client_settings;
2143 g_mutex_unlock (&priv->lock);
2149 * gst_rtsp_client_set_auth:
2150 * @client: a #GstRTSPClient
2151 * @auth: a #GstRTSPAuth
2153 * configure @auth to be used as the authentication manager of @client.
2156 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2158 GstRTSPClientPrivate *priv;
2161 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2163 priv = client->priv;
2166 g_object_ref (auth);
2168 g_mutex_lock (&priv->lock);
2171 g_mutex_unlock (&priv->lock);
2174 g_object_unref (old);
2179 * gst_rtsp_client_get_auth:
2180 * @client: a #GstRTSPClient
2182 * Get the #GstRTSPAuth used as the authentication manager of @client.
2184 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2188 gst_rtsp_client_get_auth (GstRTSPClient * client)
2190 GstRTSPClientPrivate *priv;
2191 GstRTSPAuth *result;
2193 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2195 priv = client->priv;
2197 g_mutex_lock (&priv->lock);
2198 if ((result = priv->auth))
2199 g_object_ref (result);
2200 g_mutex_unlock (&priv->lock);
2206 * gst_rtsp_client_set_connection:
2207 * @client: a #GstRTSPClient
2208 * @conn: (transfer full): a #GstRTSPConnection
2210 * Set the #GstRTSPConnection of @client. This function takes ownership of
2213 * Returns: %TRUE on success.
2216 gst_rtsp_client_set_connection (GstRTSPClient * client,
2217 GstRTSPConnection * conn)
2219 GstRTSPClientPrivate *priv;
2220 GSocket *read_socket;
2221 GSocketAddress *address;
2223 GError *error = NULL;
2225 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2226 g_return_val_if_fail (conn != NULL, FALSE);
2228 priv = client->priv;
2230 read_socket = gst_rtsp_connection_get_read_socket (conn);
2232 if (!(address = g_socket_get_local_address (read_socket, &error)))
2235 g_free (priv->server_ip);
2236 /* keep the original ip that the client connected to */
2237 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2238 GInetAddress *iaddr;
2240 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2242 /* socket might be ipv6 but adress still ipv4 */
2243 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2244 priv->server_ip = g_inet_address_to_string (iaddr);
2245 g_object_unref (address);
2247 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2248 priv->server_ip = g_strdup ("unknown");
2251 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2252 priv->server_ip, priv->is_ipv6);
2254 url = gst_rtsp_connection_get_url (conn);
2255 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2257 priv->connection = conn;
2264 GST_ERROR ("could not get local address %s", error->message);
2265 g_error_free (error);
2271 * gst_rtsp_client_get_connection:
2272 * @client: a #GstRTSPClient
2274 * Get the #GstRTSPConnection of @client.
2276 * Returns: (transfer none): the #GstRTSPConnection of @client.
2277 * The connection object returned remains valid until the client is freed.
2280 gst_rtsp_client_get_connection (GstRTSPClient * client)
2282 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2284 return client->priv->connection;
2288 * gst_rtsp_client_set_send_func:
2289 * @client: a #GstRTSPClient
2290 * @func: a #GstRTSPClientSendFunc
2291 * @user_data: user data passed to @func
2292 * @notify: called when @user_data is no longer in use
2294 * Set @func as the callback that will be called when a new message needs to be
2295 * sent to the client. @user_data is passed to @func and @notify is called when
2296 * @user_data is no longer in use.
2299 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2300 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2302 GstRTSPClientPrivate *priv;
2303 GDestroyNotify old_notify;
2306 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2308 priv = client->priv;
2310 g_mutex_lock (&priv->send_lock);
2311 priv->send_func = func;
2312 old_notify = priv->send_notify;
2313 old_data = priv->send_data;
2314 priv->send_notify = notify;
2315 priv->send_data = user_data;
2316 g_mutex_unlock (&priv->send_lock);
2319 old_notify (old_data);
2323 * gst_rtsp_client_handle_message:
2324 * @client: a #GstRTSPClient
2325 * @message: an #GstRTSPMessage
2327 * Let the client handle @message.
2329 * Returns: a #GstRTSPResult.
2332 gst_rtsp_client_handle_message (GstRTSPClient * client,
2333 GstRTSPMessage * message)
2335 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2336 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2338 switch (message->type) {
2339 case GST_RTSP_MESSAGE_REQUEST:
2340 handle_request (client, message);
2342 case GST_RTSP_MESSAGE_RESPONSE:
2344 case GST_RTSP_MESSAGE_DATA:
2345 handle_data (client, message);
2354 * gst_rtsp_client_send_request:
2355 * @client: a #GstRTSPClient
2356 * @session: a #GstRTSPSession to send the request to or %NULL
2357 * @request: The request #GstRTSPMessage to send
2359 * Send a request message to the client.
2362 gst_rtsp_client_send_request (GstRTSPClient * client, GstRTSPSession * session,
2363 GstRTSPMessage * request)
2365 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2366 g_return_val_if_fail (request != NULL, GST_RTSP_EINVAL);
2367 g_return_val_if_fail (request->type == GST_RTSP_MESSAGE_REQUEST,
2370 send_message (client, session, request, FALSE);
2375 static GstRTSPResult
2376 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2377 gboolean close, gpointer user_data)
2379 GstRTSPClientPrivate *priv = client->priv;
2381 /* send the response and store the seq number so we can wait until it's
2382 * written to the client to close the connection */
2383 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2384 &priv->close_seq : NULL);
2387 static GstRTSPResult
2388 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2391 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2394 static GstRTSPResult
2395 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2397 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2398 GstRTSPClientPrivate *priv = client->priv;
2400 if (priv->close_seq && priv->close_seq == cseq) {
2401 priv->close_seq = 0;
2402 close_connection (client);
2408 static GstRTSPResult
2409 closed (GstRTSPWatch * watch, gpointer user_data)
2411 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2412 GstRTSPClientPrivate *priv = client->priv;
2413 const gchar *tunnelid;
2415 GST_INFO ("client %p: connection closed", client);
2417 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2418 g_mutex_lock (&tunnels_lock);
2419 /* remove from tunnelids */
2420 g_hash_table_remove (tunnels, tunnelid);
2421 g_mutex_unlock (&tunnels_lock);
2424 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2429 static GstRTSPResult
2430 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2432 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2435 str = gst_rtsp_strresult (result);
2436 GST_INFO ("client %p: received an error %s", client, str);
2442 static GstRTSPResult
2443 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2444 GstRTSPMessage * message, guint id, gpointer user_data)
2446 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2449 str = gst_rtsp_strresult (result);
2451 ("client %p: error when handling message %p with id %d: %s",
2452 client, message, id, str);
2459 remember_tunnel (GstRTSPClient * client)
2461 GstRTSPClientPrivate *priv = client->priv;
2462 const gchar *tunnelid;
2464 /* store client in the pending tunnels */
2465 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2466 if (tunnelid == NULL)
2469 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2471 /* we can't have two clients connecting with the same tunnelid */
2472 g_mutex_lock (&tunnels_lock);
2473 if (g_hash_table_lookup (tunnels, tunnelid))
2474 goto tunnel_existed;
2476 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2477 g_mutex_unlock (&tunnels_lock);
2484 GST_ERROR ("client %p: no tunnelid provided", client);
2489 g_mutex_unlock (&tunnels_lock);
2490 GST_ERROR ("client %p: tunnel session %s already existed", client,
2496 static GstRTSPStatusCode
2497 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2499 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2500 GstRTSPClientPrivate *priv = client->priv;
2502 GST_INFO ("client %p: tunnel start (connection %p)", client,
2505 if (!remember_tunnel (client))
2508 return GST_RTSP_STS_OK;
2513 GST_ERROR ("client %p: error starting tunnel", client);
2514 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2518 static GstRTSPResult
2519 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2521 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2522 GstRTSPClientPrivate *priv = client->priv;
2524 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2527 /* ignore error, it'll only be a problem when the client does a POST again */
2528 remember_tunnel (client);
2533 static GstRTSPResult
2534 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2536 const gchar *tunnelid;
2537 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2538 GstRTSPClientPrivate *priv = client->priv;
2539 GstRTSPClient *oclient;
2540 GstRTSPClientPrivate *opriv;
2542 GST_INFO ("client %p: tunnel complete", client);
2544 /* find previous tunnel */
2545 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2546 if (tunnelid == NULL)
2549 g_mutex_lock (&tunnels_lock);
2550 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2553 /* remove the old client from the table. ref before because removing it will
2554 * remove the ref to it. */
2555 g_object_ref (oclient);
2556 g_hash_table_remove (tunnels, tunnelid);
2558 opriv = oclient->priv;
2560 if (opriv->watch == NULL)
2562 g_mutex_unlock (&tunnels_lock);
2564 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2565 opriv->connection, priv->connection);
2567 /* merge the tunnels into the first client */
2568 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2569 gst_rtsp_watch_reset (opriv->watch);
2570 g_object_unref (oclient);
2577 GST_ERROR ("client %p: no tunnelid provided", client);
2578 return GST_RTSP_ERROR;
2582 g_mutex_unlock (&tunnels_lock);
2583 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2584 return GST_RTSP_ERROR;
2588 g_mutex_unlock (&tunnels_lock);
2589 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2590 g_object_unref (oclient);
2591 return GST_RTSP_ERROR;
2595 static GstRTSPWatchFuncs watch_funcs = {
2607 client_watch_notify (GstRTSPClient * client)
2609 GstRTSPClientPrivate *priv = client->priv;
2611 GST_INFO ("client %p: watch destroyed", client);
2613 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2614 g_object_unref (client);
2618 * gst_rtsp_client_attach:
2619 * @client: a #GstRTSPClient
2620 * @context: (allow-none): a #GMainContext
2622 * Attaches @client to @context. When the mainloop for @context is run, the
2623 * client will be dispatched. When @context is NULL, the default context will be
2626 * This function should be called when the client properties and urls are fully
2627 * configured and the client is ready to start.
2629 * Returns: the ID (greater than 0) for the source within the GMainContext.
2632 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2634 GstRTSPClientPrivate *priv;
2637 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2638 priv = client->priv;
2639 g_return_val_if_fail (priv->watch == NULL, 0);
2641 /* create watch for the connection and attach */
2642 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2643 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2644 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2645 (GDestroyNotify) gst_rtsp_watch_unref);
2647 /* FIXME make this configurable. We don't want to do this yet because it will
2648 * be superceeded by a cache object later */
2649 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2651 GST_INFO ("attaching to context %p", context);
2652 res = gst_rtsp_watch_attach (priv->watch, context);
2658 * gst_rtsp_client_session_filter:
2659 * @client: a #GstRTSPclient
2660 * @func: (scope call): a callback
2661 * @user_data: user data passed to @func
2663 * Call @func for each session managed by @client. The result value of @func
2664 * determines what happens to the session. @func will be called with @client
2665 * locked so no further actions on @client can be performed from @func.
2667 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
2670 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
2672 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
2673 * will also be added with an additional ref to the result #GList of this
2676 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
2677 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2678 * element in the #GList should be unreffed before the list is freed.
2681 gst_rtsp_client_session_filter (GstRTSPClient * client,
2682 GstRTSPClientSessionFilterFunc func, gpointer user_data)
2684 GstRTSPClientPrivate *priv;
2685 GList *result, *walk, *next;
2687 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2688 g_return_val_if_fail (func != NULL, NULL);
2690 priv = client->priv;
2694 g_mutex_lock (&priv->lock);
2695 for (walk = priv->sessions; walk; walk = next) {
2696 GstRTSPSession *sess = walk->data;
2698 next = g_list_next (walk);
2700 switch (func (client, sess, user_data)) {
2701 case GST_RTSP_FILTER_REMOVE:
2702 /* stop watching the session and pretent it went away */
2703 client_cleanup_session (client, sess);
2705 case GST_RTSP_FILTER_REF:
2706 result = g_list_prepend (result, g_object_ref (sess));
2708 case GST_RTSP_FILTER_KEEP:
2713 g_mutex_unlock (&priv->lock);