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);
333 gst_rtsp_url_free (priv->uri);
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 compare_uri (const GstRTSPUrl * uri1, const GstRTSPUrl * uri2)
469 if (uri1 == NULL || uri2 == NULL)
472 if (strcmp (uri1->abspath, uri2->abspath))
478 /* this function is called to initially find the media for the DESCRIBE request
479 * but is cached for when the same client (without breaking the connection) is
480 * doing a setup for the exact same url. */
481 static GstRTSPMedia *
482 find_media (GstRTSPClient * client, GstRTSPClientState * state)
484 GstRTSPClientPrivate *priv = client->priv;
485 GstRTSPMediaFactory *factory;
489 if (!compare_uri (priv->uri, state->uri)) {
490 /* remove any previously cached values before we try to construct a new
493 gst_rtsp_url_free (priv->uri);
496 gst_rtsp_media_unprepare (priv->media);
497 g_object_unref (priv->media);
501 if (!priv->mount_points)
502 goto no_mount_points;
504 /* find the factory for the uri first */
505 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
506 state->uri->abspath, NULL)))
509 /* check if we have access to the factory */
510 if ((auth = gst_rtsp_media_factory_get_auth (factory))) {
511 state->factory = factory;
513 if (!gst_rtsp_auth_check (auth, client, 0, state))
516 state->factory = NULL;
517 g_object_unref (auth);
520 /* prepare the media and add it to the pipeline */
521 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
524 g_object_unref (factory);
527 /* prepare the media */
528 if (!(gst_rtsp_media_prepare (media)))
531 /* now keep track of the uri and the media */
532 priv->uri = gst_rtsp_url_copy (state->uri);
534 state->media = media;
536 /* we have seen this uri before, used cached media */
538 state->media = media;
539 GST_INFO ("reusing cached media %p", media);
543 g_object_ref (media);
550 GST_ERROR ("client %p: no mount points configured", client);
551 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
556 GST_ERROR ("client %p: no factory for uri", client);
557 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
562 GST_ERROR ("client %p: unauthorized request", client);
563 handle_unauthorized_request (client, auth, state);
564 g_object_unref (factory);
565 state->factory = NULL;
566 g_object_unref (auth);
571 GST_ERROR ("client %p: can't create media", client);
572 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
573 g_object_unref (factory);
578 GST_ERROR ("client %p: can't prepare media", client);
579 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
580 g_object_unref (media);
586 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
588 GstRTSPClientPrivate *priv = client->priv;
589 GstRTSPMessage message = { 0 };
594 gst_rtsp_message_init_data (&message, channel);
596 /* FIXME, need some sort of iovec RTSPMessage here */
597 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
600 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
602 g_mutex_lock (&priv->send_lock);
604 priv->send_func (client, &message, FALSE, priv->send_data);
605 g_mutex_unlock (&priv->send_lock);
607 gst_rtsp_message_steal_body (&message, &data, &usize);
608 gst_buffer_unmap (buffer, &map_info);
610 gst_rtsp_message_unset (&message);
616 link_transport (GstRTSPClient * client, GstRTSPSession * session,
617 GstRTSPStreamTransport * trans)
619 GstRTSPClientPrivate *priv = client->priv;
621 GST_DEBUG ("client %p: linking transport %p", client, trans);
623 gst_rtsp_stream_transport_set_callbacks (trans,
624 (GstRTSPSendFunc) do_send_data,
625 (GstRTSPSendFunc) do_send_data, client, NULL);
627 priv->transports = g_list_prepend (priv->transports, trans);
629 /* make sure our session can't expire */
630 gst_rtsp_session_prevent_expire (session);
634 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
635 GstRTSPStreamTransport * trans)
637 GstRTSPClientPrivate *priv = client->priv;
639 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
641 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
643 priv->transports = g_list_remove (priv->transports, trans);
645 /* our session can now expire */
646 gst_rtsp_session_allow_expire (session);
650 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
651 GstRTSPSessionMedia * sessmedia)
656 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
657 for (i = 0; i < n_streams; i++) {
658 GstRTSPStreamTransport *trans;
659 const GstRTSPTransport *tr;
661 /* get the transport, if there is no transport configured, skip this stream */
662 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
666 tr = gst_rtsp_stream_transport_get_transport (trans);
668 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
669 /* for TCP, unlink the stream from the TCP connection of the client */
670 unlink_transport (client, session, trans);
676 close_connection (GstRTSPClient * client)
678 GstRTSPClientPrivate *priv = client->priv;
679 const gchar *tunnelid;
681 GST_DEBUG ("client %p: closing connection", client);
683 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
684 g_mutex_lock (&tunnels_lock);
685 /* remove from tunnelids */
686 g_hash_table_remove (tunnels, tunnelid);
687 g_mutex_unlock (&tunnels_lock);
690 gst_rtsp_connection_close (priv->connection);
694 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
696 GstRTSPClientPrivate *priv = client->priv;
697 GstRTSPSession *session;
698 GstRTSPSessionMedia *sessmedia;
699 GstRTSPStatusCode code;
706 session = state->session;
711 path = state->uri->abspath;
713 /* get a handle to the configuration of the media in the session */
714 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
718 state->sessmedia = sessmedia;
720 /* we emit the signal before closing the connection */
721 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
724 /* unlink the all TCP callbacks */
725 unlink_session_transports (client, session, sessmedia);
727 /* remove the session from the watched sessions */
728 client_unwatch_session (client, session);
730 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
732 /* unmanage the media in the session, returns false if all media session
734 if (!gst_rtsp_session_release_media (session, sessmedia)) {
735 /* remove the session */
736 gst_rtsp_session_pool_remove (priv->session_pool, session);
738 /* construct the response now */
739 code = GST_RTSP_STS_OK;
740 gst_rtsp_message_init_response (state->response, code,
741 gst_rtsp_status_as_text (code), state->request);
743 send_message (client, session, state->response, TRUE);
750 GST_ERROR ("client %p: no session", client);
751 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
756 GST_ERROR ("client %p: no uri supplied", client);
757 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
762 GST_ERROR ("client %p: no media for uri", client);
763 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
769 default_params_set (GstRTSPClient * client, GstRTSPClientState * state)
773 res = gst_rtsp_params_set (client, state);
779 default_params_get (GstRTSPClient * client, GstRTSPClientState * state)
783 res = gst_rtsp_params_get (client, state);
789 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
795 res = gst_rtsp_message_get_body (state->request, &data, &size);
796 if (res != GST_RTSP_OK)
800 /* no body, keep-alive request */
801 send_generic_response (client, GST_RTSP_STS_OK, state);
803 /* there is a body, handle the params */
804 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, state);
805 if (res != GST_RTSP_OK)
808 send_message (client, state->session, state->response, FALSE);
811 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
819 GST_ERROR ("client %p: bad request", client);
820 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
826 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
832 res = gst_rtsp_message_get_body (state->request, &data, &size);
833 if (res != GST_RTSP_OK)
837 /* no body, keep-alive request */
838 send_generic_response (client, GST_RTSP_STS_OK, state);
840 /* there is a body, handle the params */
841 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, state);
842 if (res != GST_RTSP_OK)
845 send_message (client, state->session, state->response, FALSE);
848 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
856 GST_ERROR ("client %p: bad request", client);
857 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
863 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
865 GstRTSPSession *session;
866 GstRTSPSessionMedia *sessmedia;
867 GstRTSPStatusCode code;
868 GstRTSPState rtspstate;
872 if (!(session = state->session))
878 path = state->uri->abspath;
880 /* get a handle to the configuration of the media in the session */
881 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
885 state->sessmedia = sessmedia;
887 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
888 /* the session state must be playing or recording */
889 if (rtspstate != GST_RTSP_STATE_PLAYING &&
890 rtspstate != GST_RTSP_STATE_RECORDING)
893 /* unlink the all TCP callbacks */
894 unlink_session_transports (client, session, sessmedia);
896 /* then pause sending */
897 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
899 /* construct the response now */
900 code = GST_RTSP_STS_OK;
901 gst_rtsp_message_init_response (state->response, code,
902 gst_rtsp_status_as_text (code), state->request);
904 send_message (client, session, state->response, FALSE);
906 /* the state is now READY */
907 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
909 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
917 GST_ERROR ("client %p: no seesion", client);
918 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
923 GST_ERROR ("client %p: no uri supplied", client);
924 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
929 GST_ERROR ("client %p: no media for uri", client);
930 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
935 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
936 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
943 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
945 GstRTSPSession *session;
946 GstRTSPSessionMedia *sessmedia;
948 GstRTSPStatusCode code;
950 guint n_streams, i, infocount;
952 GstRTSPTimeRange *range;
954 GstRTSPState rtspstate;
955 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
959 if (!(session = state->session))
965 path = state->uri->abspath;
967 /* get a handle to the configuration of the media in the session */
968 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
972 state->sessmedia = sessmedia;
973 state->media = media = gst_rtsp_session_media_get_media (sessmedia);
975 /* the session state must be playing or ready */
976 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
977 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
980 /* parse the range header if we have one */
982 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
983 if (res == GST_RTSP_OK) {
984 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
985 /* we have a range, seek to the position */
987 gst_rtsp_media_seek (media, range);
988 gst_rtsp_range_free (range);
992 /* grab RTPInfo from the payloaders now */
993 rtpinfo = g_string_new ("");
995 n_streams = gst_rtsp_media_n_streams (media);
996 for (i = 0, infocount = 0; i < n_streams; i++) {
997 GstRTSPStreamTransport *trans;
998 GstRTSPStream *stream;
999 const GstRTSPTransport *tr;
1003 /* get the transport, if there is no transport configured, skip this stream */
1004 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
1005 if (trans == NULL) {
1006 GST_INFO ("stream %d is not configured", i);
1009 tr = gst_rtsp_stream_transport_get_transport (trans);
1011 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1012 /* for TCP, link the stream to the TCP connection of the client */
1013 link_transport (client, session, trans);
1016 stream = gst_rtsp_stream_transport_get_stream (trans);
1017 if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
1019 g_string_append (rtpinfo, ", ");
1021 uristr = gst_rtsp_url_get_request_uri (state->uri);
1022 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
1023 uristr, i, seq, rtptime);
1028 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
1032 /* construct the response now */
1033 code = GST_RTSP_STS_OK;
1034 gst_rtsp_message_init_response (state->response, code,
1035 gst_rtsp_status_as_text (code), state->request);
1037 /* add the RTP-Info header */
1038 if (infocount > 0) {
1039 str = g_string_free (rtpinfo, FALSE);
1040 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
1042 g_string_free (rtpinfo, TRUE);
1046 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1047 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
1049 send_message (client, session, state->response, FALSE);
1051 /* start playing after sending the request */
1052 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1054 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1056 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
1064 GST_ERROR ("client %p: no session", client);
1065 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1070 GST_ERROR ("client %p: no uri supplied", client);
1071 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1076 GST_ERROR ("client %p: media not found", client);
1077 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1082 GST_ERROR ("client %p: not PLAYING or READY", client);
1083 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1090 do_keepalive (GstRTSPSession * session)
1092 GST_INFO ("keep session %p alive", session);
1093 gst_rtsp_session_touch (session);
1096 /* parse @transport and return a valid transport in @tr. only transports
1097 * from @supported are returned. Returns FALSE if no valid transport
1100 parse_transport (const char *transport, GstRTSPLowerTrans supported,
1101 GstRTSPTransport * tr)
1108 gst_rtsp_transport_init (tr);
1110 GST_DEBUG ("parsing transports %s", transport);
1112 transports = g_strsplit (transport, ",", 0);
1114 /* loop through the transports, try to parse */
1115 for (i = 0; transports[i]; i++) {
1116 res = gst_rtsp_transport_parse (transports[i], tr);
1117 if (res != GST_RTSP_OK) {
1118 /* no valid transport, search some more */
1119 GST_WARNING ("could not parse transport %s", transports[i]);
1123 /* we have a transport, see if it's RTP/AVP */
1124 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1125 GST_WARNING ("invalid transport %s", transports[i]);
1129 if (!(tr->lower_transport & supported)) {
1130 GST_WARNING ("unsupported transport %s", transports[i]);
1134 /* we have a valid transport */
1135 GST_INFO ("found valid transport %s", transports[i]);
1140 gst_rtsp_transport_init (tr);
1142 g_strfreev (transports);
1148 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
1149 GstRTSPMessage * request)
1151 gchar *blocksize_str;
1152 gboolean ret = TRUE;
1154 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1155 &blocksize_str, 0) == GST_RTSP_OK) {
1159 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1160 if (end == blocksize_str) {
1161 GST_ERROR ("failed to parse blocksize");
1164 /* we don't want to change the mtu when this media
1165 * can be shared because it impacts other clients */
1166 if (gst_rtsp_media_is_shared (media))
1169 if (blocksize > G_MAXUINT)
1170 blocksize = G_MAXUINT;
1171 gst_rtsp_stream_set_mtu (stream, blocksize);
1178 default_configure_client_transport (GstRTSPClient * client,
1179 GstRTSPClientState * state, GstRTSPTransport * ct)
1181 GstRTSPClientPrivate *priv = client->priv;
1183 /* we have a valid transport now, set the destination of the client. */
1184 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1185 if (ct->destination && priv->use_client_settings) {
1186 GstRTSPAddress *addr;
1188 addr = gst_rtsp_stream_reserve_address (state->stream, ct->destination,
1189 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1194 gst_rtsp_address_free (addr);
1196 GstRTSPAddress *addr;
1197 GSocketFamily family;
1199 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1201 addr = gst_rtsp_stream_get_multicast_address (state->stream, family);
1205 g_free (ct->destination);
1206 ct->destination = g_strdup (addr->address);
1207 ct->port.min = addr->port;
1208 ct->port.max = addr->port + addr->n_ports - 1;
1209 ct->ttl = addr->ttl;
1211 gst_rtsp_address_free (addr);
1216 url = gst_rtsp_connection_get_url (priv->connection);
1217 g_free (ct->destination);
1218 ct->destination = g_strdup (url->host);
1220 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1221 /* check if the client selected channels for TCP */
1222 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1223 gst_rtsp_session_media_alloc_channels (state->sessmedia,
1233 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1238 static GstRTSPTransport *
1239 make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
1240 GstRTSPTransport * ct)
1242 GstRTSPTransport *st;
1244 GSocketFamily family;
1246 /* prepare the server transport */
1247 gst_rtsp_transport_new (&st);
1249 st->trans = ct->trans;
1250 st->profile = ct->profile;
1251 st->lower_transport = ct->lower_transport;
1253 addr = g_inet_address_new_from_string (ct->destination);
1256 GST_ERROR ("failed to get inet addr from client destination");
1257 family = G_SOCKET_FAMILY_IPV4;
1259 family = g_inet_address_get_family (addr);
1260 g_object_unref (addr);
1264 switch (st->lower_transport) {
1265 case GST_RTSP_LOWER_TRANS_UDP:
1266 st->client_port = ct->client_port;
1267 gst_rtsp_stream_get_server_port (state->stream, &st->server_port, family);
1269 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1270 st->port = ct->port;
1271 st->destination = g_strdup (ct->destination);
1274 case GST_RTSP_LOWER_TRANS_TCP:
1275 st->interleaved = ct->interleaved;
1280 gst_rtsp_stream_get_ssrc (state->stream, &st->ssrc);
1286 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
1288 GstRTSPClientPrivate *priv = client->priv;
1292 GstRTSPTransport *ct, *st;
1293 GstRTSPLowerTrans supported;
1294 GstRTSPStatusCode code;
1295 GstRTSPSession *session;
1296 GstRTSPStreamTransport *trans;
1297 gchar *trans_str, *pos;
1299 GstRTSPSessionMedia *sessmedia;
1300 GstRTSPMedia *media;
1301 GstRTSPStream *stream;
1302 GstRTSPState rtspstate;
1303 GstRTSPClientClass *klass;
1311 path = state->uri->abspath;
1313 /* the uri contains the stream number we added in the SDP config, which is
1314 * always /stream=%d so we need to strip that off
1315 * parse the stream we need to configure, look for the stream in the abspath
1316 * first and then in the query. */
1317 if (uri->abspath == NULL || !(pos = strstr (uri->abspath, "/stream="))) {
1318 if (uri->query == NULL || !(pos = strstr (uri->query, "/stream=")))
1322 /* we can mofify the parsed uri in place */
1325 pos += strlen ("/stream=");
1326 if (sscanf (pos, "%u", &streamid) != 1)
1329 /* parse the transport */
1331 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
1333 if (res != GST_RTSP_OK)
1336 gst_rtsp_transport_new (&ct);
1338 /* our supported transports */
1339 supported = GST_RTSP_LOWER_TRANS_UDP |
1340 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
1342 /* parse and find a usable supported transport */
1343 if (!parse_transport (transport, supported, ct))
1344 goto unsupported_transports;
1346 /* we create the session after parsing stuff so that we don't make
1347 * a session for malformed requests */
1348 if (priv->session_pool == NULL)
1351 session = state->session;
1354 g_object_ref (session);
1355 /* get a handle to the configuration of the media in the session, this can
1356 * return NULL if this is a new url to manage in this session. */
1357 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1359 /* create a session if this fails we probably reached our session limit or
1361 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1362 goto service_unavailable;
1364 /* make sure this client is closed when the session is closed */
1365 client_watch_session (client, session);
1367 /* signal new session */
1368 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1371 state->session = session;
1373 /* we need a new media configuration in this session */
1377 /* we have no media, find one and manage it */
1378 if (sessmedia == NULL) {
1379 /* get a handle to the configuration of the media in the session */
1380 if ((media = find_media (client, state))) {
1381 /* manage the media in our session now */
1382 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1386 /* if we stil have no media, error */
1387 if (sessmedia == NULL)
1390 state->sessmedia = sessmedia;
1391 state->media = media = gst_rtsp_session_media_get_media (sessmedia);
1393 /* now get the stream */
1394 stream = gst_rtsp_media_get_stream (media, streamid);
1398 state->stream = stream;
1400 /* set blocksize on this stream */
1401 if (!handle_blocksize (media, stream, state->request))
1402 goto invalid_blocksize;
1404 /* update the client transport */
1405 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1406 if (!klass->configure_client_transport (client, state, ct))
1407 goto unsupported_client_transport;
1409 /* set in the session media transport */
1410 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1412 /* configure keepalive for this transport */
1413 gst_rtsp_stream_transport_set_keepalive (trans,
1414 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1416 /* create and serialize the server transport */
1417 st = make_server_transport (client, state, ct);
1418 trans_str = gst_rtsp_transport_as_text (st);
1419 gst_rtsp_transport_free (st);
1421 /* construct the response now */
1422 code = GST_RTSP_STS_OK;
1423 gst_rtsp_message_init_response (state->response, code,
1424 gst_rtsp_status_as_text (code), state->request);
1426 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1430 send_message (client, session, state->response, FALSE);
1432 /* update the state */
1433 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1434 switch (rtspstate) {
1435 case GST_RTSP_STATE_PLAYING:
1436 case GST_RTSP_STATE_RECORDING:
1437 case GST_RTSP_STATE_READY:
1438 /* no state change */
1441 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1444 g_object_unref (session);
1446 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST],
1454 GST_ERROR ("client %p: no uri", client);
1455 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1460 GST_ERROR ("client %p: bad request", client);
1461 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1466 GST_ERROR ("client %p: media not found", client);
1467 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1468 g_object_unref (session);
1469 gst_rtsp_transport_free (ct);
1474 GST_ERROR ("client %p: invalid blocksize", client);
1475 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1476 g_object_unref (session);
1477 gst_rtsp_transport_free (ct);
1480 unsupported_client_transport:
1482 GST_ERROR ("client %p: unsupported client transport", client);
1483 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1484 g_object_unref (session);
1485 gst_rtsp_transport_free (ct);
1490 GST_ERROR ("client %p: no transport", client);
1491 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1494 unsupported_transports:
1496 GST_ERROR ("client %p: unsupported transports", client);
1497 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1498 gst_rtsp_transport_free (ct);
1503 GST_ERROR ("client %p: no session pool configured", client);
1504 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1505 gst_rtsp_transport_free (ct);
1508 service_unavailable:
1510 GST_ERROR ("client %p: can't create session", client);
1511 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1512 gst_rtsp_transport_free (ct);
1517 static GstSDPMessage *
1518 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1520 GstRTSPClientPrivate *priv = client->priv;
1525 gst_sdp_message_new (&sdp);
1527 /* some standard things first */
1528 gst_sdp_message_set_version (sdp, "0");
1535 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1538 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1539 gst_sdp_message_set_information (sdp, "rtsp-server");
1540 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1541 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1542 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1543 gst_sdp_message_add_attribute (sdp, "control", "*");
1545 info.is_ipv6 = priv->is_ipv6;
1546 info.server_ip = priv->server_ip;
1548 /* create an SDP for the media object */
1549 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1557 GST_ERROR ("client %p: could not create SDP", client);
1558 gst_sdp_message_free (sdp);
1563 /* for the describe we must generate an SDP */
1565 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1570 gchar *str, *content_base;
1571 GstRTSPMedia *media;
1572 GstRTSPClientClass *klass;
1574 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1579 /* check what kind of format is accepted, we don't really do anything with it
1580 * and always return SDP for now. */
1585 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1587 if (res == GST_RTSP_ENOTIMPL)
1590 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1594 /* find the media object for the uri */
1595 if (!(media = find_media (client, state)))
1598 /* create an SDP for the media object on this client */
1599 if (!(sdp = klass->create_sdp (client, media)))
1602 g_object_unref (media);
1604 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1605 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1607 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1610 /* content base for some clients that might screw up creating the setup uri */
1611 str = gst_rtsp_url_get_request_uri (state->uri);
1612 str_len = strlen (str);
1614 /* check for trailing '/' and append one */
1615 if (str[str_len - 1] != '/') {
1616 content_base = g_malloc (str_len + 2);
1617 memcpy (content_base, str, str_len);
1618 content_base[str_len] = '/';
1619 content_base[str_len + 1] = '\0';
1625 GST_INFO ("adding content-base: %s", content_base);
1627 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1629 g_free (content_base);
1631 /* add SDP to the response body */
1632 str = gst_sdp_message_as_text (sdp);
1633 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1634 gst_sdp_message_free (sdp);
1636 send_message (client, state->session, state->response, FALSE);
1638 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1646 GST_ERROR ("client %p: no uri", client);
1647 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1652 GST_ERROR ("client %p: no media", client);
1653 /* error reply is already sent */
1658 GST_ERROR ("client %p: can't create SDP", client);
1659 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1660 g_object_unref (media);
1666 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1668 GstRTSPMethod options;
1671 options = GST_RTSP_DESCRIBE |
1676 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1678 str = gst_rtsp_options_as_text (options);
1680 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1681 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1683 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1686 send_message (client, state->session, state->response, FALSE);
1688 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1694 /* remove duplicate and trailing '/' */
1696 sanitize_uri (GstRTSPUrl * uri)
1700 gboolean have_slash, prev_slash;
1702 s = d = uri->abspath;
1703 len = strlen (uri->abspath);
1707 for (i = 0; i < len; i++) {
1708 have_slash = s[i] == '/';
1710 if (!have_slash || !prev_slash)
1712 prev_slash = have_slash;
1714 len = d - uri->abspath;
1715 /* don't remove the first slash if that's the only thing left */
1716 if (len > 1 && *(d - 1) == '/')
1722 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1724 GstRTSPClientPrivate *priv = client->priv;
1726 GST_INFO ("client %p: session %p finished", client, session);
1728 /* unlink all media managed in this session */
1729 client_unlink_session (client, session);
1731 /* remove the session */
1732 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1733 GST_INFO ("client %p: all sessions finalized, close the connection",
1735 close_connection (client);
1740 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1742 GstRTSPClientPrivate *priv = client->priv;
1743 GstRTSPMethod method;
1744 const gchar *uristr;
1745 GstRTSPUrl *uri = NULL;
1746 GstRTSPVersion version;
1748 GstRTSPSession *session = NULL;
1749 GstRTSPClientState state = { NULL };
1750 GstRTSPMessage response = { 0 };
1753 state.request = request;
1754 state.response = &response;
1756 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1757 gst_rtsp_message_dump (request);
1760 GST_INFO ("client %p: received a request", client);
1762 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1764 /* we can only handle 1.0 requests */
1765 if (version != GST_RTSP_VERSION_1_0)
1768 state.method = method;
1770 /* we always try to parse the url first */
1771 if (strcmp (uristr, "*") == 0) {
1772 /* special case where we have * as uri, keep uri = NULL */
1773 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1776 /* get the session if there is any */
1777 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1778 if (res == GST_RTSP_OK) {
1779 if (priv->session_pool == NULL)
1782 /* we had a session in the request, find it again */
1783 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1784 goto session_not_found;
1786 /* we add the session to the client list of watched sessions. When a session
1787 * disappears because it times out, we will be notified. If all sessions are
1788 * gone, we will close the connection */
1789 client_watch_session (client, session);
1792 /* sanitize the uri */
1796 state.session = session;
1799 if (!gst_rtsp_auth_check (priv->auth, client, 0, &state))
1800 goto not_authorized;
1803 /* now see what is asked and dispatch to a dedicated handler */
1805 case GST_RTSP_OPTIONS:
1806 handle_options_request (client, &state);
1808 case GST_RTSP_DESCRIBE:
1809 handle_describe_request (client, &state);
1811 case GST_RTSP_SETUP:
1812 handle_setup_request (client, &state);
1815 handle_play_request (client, &state);
1817 case GST_RTSP_PAUSE:
1818 handle_pause_request (client, &state);
1820 case GST_RTSP_TEARDOWN:
1821 handle_teardown_request (client, &state);
1823 case GST_RTSP_SET_PARAMETER:
1824 handle_set_param_request (client, &state);
1826 case GST_RTSP_GET_PARAMETER:
1827 handle_get_param_request (client, &state);
1829 case GST_RTSP_ANNOUNCE:
1830 case GST_RTSP_RECORD:
1831 case GST_RTSP_REDIRECT:
1832 goto not_implemented;
1833 case GST_RTSP_INVALID:
1840 g_object_unref (session);
1842 gst_rtsp_url_free (uri);
1848 GST_ERROR ("client %p: version %d not supported", client, version);
1849 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1855 GST_ERROR ("client %p: bad request", client);
1856 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1861 GST_ERROR ("client %p: no pool configured", client);
1862 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1867 GST_ERROR ("client %p: session not found", client);
1868 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1873 GST_ERROR ("client %p: not allowed", client);
1874 handle_unauthorized_request (client, priv->auth, &state);
1879 GST_ERROR ("client %p: method %d not implemented", client, method);
1880 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1886 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1888 GstRTSPClientPrivate *priv = client->priv;
1897 /* find the stream for this message */
1898 res = gst_rtsp_message_parse_data (message, &channel);
1899 if (res != GST_RTSP_OK)
1902 gst_rtsp_message_steal_body (message, &data, &size);
1904 buffer = gst_buffer_new_wrapped (data, size);
1907 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1908 GstRTSPStreamTransport *trans;
1909 GstRTSPStream *stream;
1910 const GstRTSPTransport *tr;
1914 tr = gst_rtsp_stream_transport_get_transport (trans);
1915 stream = gst_rtsp_stream_transport_get_stream (trans);
1917 /* check for TCP transport */
1918 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1919 /* dispatch to the stream based on the channel number */
1920 if (tr->interleaved.min == channel) {
1921 gst_rtsp_stream_recv_rtp (stream, buffer);
1924 } else if (tr->interleaved.max == channel) {
1925 gst_rtsp_stream_recv_rtcp (stream, buffer);
1932 gst_buffer_unref (buffer);
1936 * gst_rtsp_client_set_session_pool:
1937 * @client: a #GstRTSPClient
1938 * @pool: a #GstRTSPSessionPool
1940 * Set @pool as the sessionpool for @client which it will use to find
1941 * or allocate sessions. the sessionpool is usually inherited from the server
1942 * that created the client but can be overridden later.
1945 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
1946 GstRTSPSessionPool * pool)
1948 GstRTSPSessionPool *old;
1949 GstRTSPClientPrivate *priv;
1951 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1953 priv = client->priv;
1956 g_object_ref (pool);
1958 g_mutex_lock (&priv->lock);
1959 old = priv->session_pool;
1960 priv->session_pool = pool;
1961 g_mutex_unlock (&priv->lock);
1964 g_object_unref (old);
1968 * gst_rtsp_client_get_session_pool:
1969 * @client: a #GstRTSPClient
1971 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
1973 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
1975 GstRTSPSessionPool *
1976 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
1978 GstRTSPClientPrivate *priv;
1979 GstRTSPSessionPool *result;
1981 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1983 priv = client->priv;
1985 g_mutex_lock (&priv->lock);
1986 if ((result = priv->session_pool))
1987 g_object_ref (result);
1988 g_mutex_unlock (&priv->lock);
1994 * gst_rtsp_client_set_mount_points:
1995 * @client: a #GstRTSPClient
1996 * @mounts: a #GstRTSPMountPoints
1998 * Set @mounts as the mount points for @client which it will use to map urls
1999 * to media streams. These mount points are usually inherited from the server that
2000 * created the client but can be overriden later.
2003 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2004 GstRTSPMountPoints * mounts)
2006 GstRTSPClientPrivate *priv;
2007 GstRTSPMountPoints *old;
2009 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2011 priv = client->priv;
2014 g_object_ref (mounts);
2016 g_mutex_lock (&priv->lock);
2017 old = priv->mount_points;
2018 priv->mount_points = mounts;
2019 g_mutex_unlock (&priv->lock);
2022 g_object_unref (old);
2026 * gst_rtsp_client_get_mount_points:
2027 * @client: a #GstRTSPClient
2029 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2031 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2033 GstRTSPMountPoints *
2034 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2036 GstRTSPClientPrivate *priv;
2037 GstRTSPMountPoints *result;
2039 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2041 priv = client->priv;
2043 g_mutex_lock (&priv->lock);
2044 if ((result = priv->mount_points))
2045 g_object_ref (result);
2046 g_mutex_unlock (&priv->lock);
2052 * gst_rtsp_client_set_use_client_settings:
2053 * @client: a #GstRTSPClient
2054 * @use_client_settings: whether to use client settings for multicast
2056 * Use client transport settings (destination and ttl) for multicast.
2057 * When @use_client_settings is %FALSE, the server settings will be
2061 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
2062 gboolean use_client_settings)
2064 GstRTSPClientPrivate *priv;
2066 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2068 priv = client->priv;
2070 g_mutex_lock (&priv->lock);
2071 priv->use_client_settings = use_client_settings;
2072 g_mutex_unlock (&priv->lock);
2076 * gst_rtsp_client_get_use_client_settings:
2077 * @client: a #GstRTSPClient
2079 * Check if client transport settings (destination and ttl) for multicast
2083 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
2085 GstRTSPClientPrivate *priv;
2088 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2090 priv = client->priv;
2092 g_mutex_lock (&priv->lock);
2093 res = priv->use_client_settings;
2094 g_mutex_unlock (&priv->lock);
2100 * gst_rtsp_client_set_auth:
2101 * @client: a #GstRTSPClient
2102 * @auth: a #GstRTSPAuth
2104 * configure @auth to be used as the authentication manager of @client.
2107 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2109 GstRTSPClientPrivate *priv;
2112 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2114 priv = client->priv;
2117 g_object_ref (auth);
2119 g_mutex_lock (&priv->lock);
2122 g_mutex_unlock (&priv->lock);
2125 g_object_unref (old);
2130 * gst_rtsp_client_get_auth:
2131 * @client: a #GstRTSPClient
2133 * Get the #GstRTSPAuth used as the authentication manager of @client.
2135 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2139 gst_rtsp_client_get_auth (GstRTSPClient * client)
2141 GstRTSPClientPrivate *priv;
2142 GstRTSPAuth *result;
2144 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2146 priv = client->priv;
2148 g_mutex_lock (&priv->lock);
2149 if ((result = priv->auth))
2150 g_object_ref (result);
2151 g_mutex_unlock (&priv->lock);
2157 * gst_rtsp_client_set_connection:
2158 * @client: a #GstRTSPClient
2159 * @conn: (transfer full): a #GstRTSPConnection
2161 * Set the #GstRTSPConnection of @client. This function takes ownership of
2164 * Returns: %TRUE on success.
2167 gst_rtsp_client_set_connection (GstRTSPClient * client,
2168 GstRTSPConnection * conn)
2170 GstRTSPClientPrivate *priv;
2171 GSocket *read_socket;
2172 GSocketAddress *address;
2174 GError *error = NULL;
2176 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2177 g_return_val_if_fail (conn != NULL, FALSE);
2179 priv = client->priv;
2181 read_socket = gst_rtsp_connection_get_read_socket (conn);
2183 if (!(address = g_socket_get_local_address (read_socket, &error)))
2186 g_free (priv->server_ip);
2187 /* keep the original ip that the client connected to */
2188 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2189 GInetAddress *iaddr;
2191 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2193 /* socket might be ipv6 but adress still ipv4 */
2194 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2195 priv->server_ip = g_inet_address_to_string (iaddr);
2196 g_object_unref (address);
2198 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2199 priv->server_ip = g_strdup ("unknown");
2202 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2203 priv->server_ip, priv->is_ipv6);
2205 url = gst_rtsp_connection_get_url (conn);
2206 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2208 priv->connection = conn;
2215 GST_ERROR ("could not get local address %s", error->message);
2216 g_error_free (error);
2222 * gst_rtsp_client_get_connection:
2223 * @client: a #GstRTSPClient
2225 * Get the #GstRTSPConnection of @client.
2227 * Returns: (transfer none): the #GstRTSPConnection of @client.
2228 * The connection object returned remains valid until the client is freed.
2231 gst_rtsp_client_get_connection (GstRTSPClient * client)
2233 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2235 return client->priv->connection;
2239 * gst_rtsp_client_set_send_func:
2240 * @client: a #GstRTSPClient
2241 * @func: a #GstRTSPClientSendFunc
2242 * @user_data: user data passed to @func
2243 * @notify: called when @user_data is no longer in use
2245 * Set @func as the callback that will be called when a new message needs to be
2246 * sent to the client. @user_data is passed to @func and @notify is called when
2247 * @user_data is no longer in use.
2250 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2251 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2253 GstRTSPClientPrivate *priv;
2254 GDestroyNotify old_notify;
2257 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2259 priv = client->priv;
2261 g_mutex_lock (&priv->send_lock);
2262 priv->send_func = func;
2263 old_notify = priv->send_notify;
2264 old_data = priv->send_data;
2265 priv->send_notify = notify;
2266 priv->send_data = user_data;
2267 g_mutex_unlock (&priv->send_lock);
2270 old_notify (old_data);
2274 * gst_rtsp_client_handle_message:
2275 * @client: a #GstRTSPClient
2276 * @message: an #GstRTSPMessage
2278 * Let the client handle @message.
2280 * Returns: a #GstRTSPResult.
2283 gst_rtsp_client_handle_message (GstRTSPClient * client,
2284 GstRTSPMessage * message)
2286 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2287 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2289 switch (message->type) {
2290 case GST_RTSP_MESSAGE_REQUEST:
2291 handle_request (client, message);
2293 case GST_RTSP_MESSAGE_RESPONSE:
2295 case GST_RTSP_MESSAGE_DATA:
2296 handle_data (client, message);
2305 * gst_rtsp_client_send_request:
2306 * @client: a #GstRTSPClient
2307 * @session: a #GstRTSPSession to send the request to or %NULL
2308 * @request: The request #GstRTSPMessage to send
2310 * Send a request message to the client.
2313 gst_rtsp_client_send_request (GstRTSPClient * client, GstRTSPSession * session,
2314 GstRTSPMessage * request)
2316 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2317 g_return_val_if_fail (request != NULL, GST_RTSP_EINVAL);
2318 g_return_val_if_fail (request->type == GST_RTSP_MESSAGE_REQUEST,
2321 send_message (client, session, request, FALSE);
2326 static GstRTSPResult
2327 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2328 gboolean close, gpointer user_data)
2330 GstRTSPClientPrivate *priv = client->priv;
2332 /* send the response and store the seq number so we can wait until it's
2333 * written to the client to close the connection */
2334 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2335 &priv->close_seq : NULL);
2338 static GstRTSPResult
2339 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2342 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2345 static GstRTSPResult
2346 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2348 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2349 GstRTSPClientPrivate *priv = client->priv;
2351 if (priv->close_seq && priv->close_seq == cseq) {
2352 priv->close_seq = 0;
2353 close_connection (client);
2359 static GstRTSPResult
2360 closed (GstRTSPWatch * watch, gpointer user_data)
2362 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2363 GstRTSPClientPrivate *priv = client->priv;
2364 const gchar *tunnelid;
2366 GST_INFO ("client %p: connection closed", client);
2368 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2369 g_mutex_lock (&tunnels_lock);
2370 /* remove from tunnelids */
2371 g_hash_table_remove (tunnels, tunnelid);
2372 g_mutex_unlock (&tunnels_lock);
2375 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2380 static GstRTSPResult
2381 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2383 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2386 str = gst_rtsp_strresult (result);
2387 GST_INFO ("client %p: received an error %s", client, str);
2393 static GstRTSPResult
2394 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2395 GstRTSPMessage * message, guint id, gpointer user_data)
2397 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2400 str = gst_rtsp_strresult (result);
2402 ("client %p: error when handling message %p with id %d: %s",
2403 client, message, id, str);
2410 remember_tunnel (GstRTSPClient * client)
2412 GstRTSPClientPrivate *priv = client->priv;
2413 const gchar *tunnelid;
2415 /* store client in the pending tunnels */
2416 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2417 if (tunnelid == NULL)
2420 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2422 /* we can't have two clients connecting with the same tunnelid */
2423 g_mutex_lock (&tunnels_lock);
2424 if (g_hash_table_lookup (tunnels, tunnelid))
2425 goto tunnel_existed;
2427 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2428 g_mutex_unlock (&tunnels_lock);
2435 GST_ERROR ("client %p: no tunnelid provided", client);
2440 g_mutex_unlock (&tunnels_lock);
2441 GST_ERROR ("client %p: tunnel session %s already existed", client,
2447 static GstRTSPStatusCode
2448 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2450 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2451 GstRTSPClientPrivate *priv = client->priv;
2453 GST_INFO ("client %p: tunnel start (connection %p)", client,
2456 if (!remember_tunnel (client))
2459 return GST_RTSP_STS_OK;
2464 GST_ERROR ("client %p: error starting tunnel", client);
2465 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2469 static GstRTSPResult
2470 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2472 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2473 GstRTSPClientPrivate *priv = client->priv;
2475 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2478 /* ignore error, it'll only be a problem when the client does a POST again */
2479 remember_tunnel (client);
2484 static GstRTSPResult
2485 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2487 const gchar *tunnelid;
2488 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2489 GstRTSPClientPrivate *priv = client->priv;
2490 GstRTSPClient *oclient;
2491 GstRTSPClientPrivate *opriv;
2493 GST_INFO ("client %p: tunnel complete", client);
2495 /* find previous tunnel */
2496 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2497 if (tunnelid == NULL)
2500 g_mutex_lock (&tunnels_lock);
2501 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2504 /* remove the old client from the table. ref before because removing it will
2505 * remove the ref to it. */
2506 g_object_ref (oclient);
2507 g_hash_table_remove (tunnels, tunnelid);
2509 opriv = oclient->priv;
2511 if (opriv->watch == NULL)
2513 g_mutex_unlock (&tunnels_lock);
2515 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2516 opriv->connection, priv->connection);
2518 /* merge the tunnels into the first client */
2519 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2520 gst_rtsp_watch_reset (opriv->watch);
2521 g_object_unref (oclient);
2528 GST_ERROR ("client %p: no tunnelid provided", client);
2529 return GST_RTSP_ERROR;
2533 g_mutex_unlock (&tunnels_lock);
2534 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2535 return GST_RTSP_ERROR;
2539 g_mutex_unlock (&tunnels_lock);
2540 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2541 g_object_unref (oclient);
2542 return GST_RTSP_ERROR;
2546 static GstRTSPWatchFuncs watch_funcs = {
2558 client_watch_notify (GstRTSPClient * client)
2560 GstRTSPClientPrivate *priv = client->priv;
2562 GST_INFO ("client %p: watch destroyed", client);
2564 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2565 g_object_unref (client);
2569 * gst_rtsp_client_attach:
2570 * @client: a #GstRTSPClient
2571 * @context: (allow-none): a #GMainContext
2573 * Attaches @client to @context. When the mainloop for @context is run, the
2574 * client will be dispatched. When @context is NULL, the default context will be
2577 * This function should be called when the client properties and urls are fully
2578 * configured and the client is ready to start.
2580 * Returns: the ID (greater than 0) for the source within the GMainContext.
2583 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2585 GstRTSPClientPrivate *priv;
2588 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2589 priv = client->priv;
2590 g_return_val_if_fail (priv->watch == NULL, 0);
2592 /* create watch for the connection and attach */
2593 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2594 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2595 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2596 (GDestroyNotify) gst_rtsp_watch_unref);
2598 /* FIXME make this configurable. We don't want to do this yet because it will
2599 * be superceeded by a cache object later */
2600 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2602 GST_INFO ("attaching to context %p", context);
2603 res = gst_rtsp_watch_attach (priv->watch, context);
2609 * gst_rtsp_client_session_filter:
2610 * @client: a #GstRTSPclient
2611 * @func: (scope call): a callback
2612 * @user_data: user data passed to @func
2614 * Call @func for each session managed by @client. The result value of @func
2615 * determines what happens to the session. @func will be called with @client
2616 * locked so no further actions on @client can be performed from @func.
2618 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
2621 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
2623 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
2624 * will also be added with an additional ref to the result #GList of this
2627 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
2628 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2629 * element in the #GList should be unreffed before the list is freed.
2632 gst_rtsp_client_session_filter (GstRTSPClient * client,
2633 GstRTSPClientSessionFilterFunc func, gpointer user_data)
2635 GstRTSPClientPrivate *priv;
2636 GList *result, *walk, *next;
2638 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2639 g_return_val_if_fail (func != NULL, NULL);
2641 priv = client->priv;
2645 g_mutex_lock (&priv->lock);
2646 for (walk = priv->sessions; walk; walk = next) {
2647 GstRTSPSession *sess = walk->data;
2649 next = g_list_next (walk);
2651 switch (func (client, sess, user_data)) {
2652 case GST_RTSP_FILTER_REMOVE:
2653 /* stop watching the session and pretent it went away */
2654 client_cleanup_session (client, sess);
2656 case GST_RTSP_FILTER_REF:
2657 result = g_list_prepend (result, g_object_ref (sess));
2659 case GST_RTSP_FILTER_KEEP:
2664 g_mutex_unlock (&priv->lock);