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;
52 GstRTSPThreadPool *thread_pool;
54 /* used to cache the media in the last requested DESCRIBE so that
55 * we can pick it up in the next SETUP immediately */
63 static GMutex tunnels_lock;
64 static GHashTable *tunnels; /* protected by tunnels_lock */
66 #define DEFAULT_SESSION_POOL NULL
67 #define DEFAULT_MOUNT_POINTS NULL
68 #define DEFAULT_USE_CLIENT_SETTINGS FALSE
75 PROP_USE_CLIENT_SETTINGS,
83 SIGNAL_OPTIONS_REQUEST,
84 SIGNAL_DESCRIBE_REQUEST,
88 SIGNAL_TEARDOWN_REQUEST,
89 SIGNAL_SET_PARAMETER_REQUEST,
90 SIGNAL_GET_PARAMETER_REQUEST,
94 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
95 #define GST_CAT_DEFAULT rtsp_client_debug
97 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
99 static void gst_rtsp_client_get_property (GObject * object, guint propid,
100 GValue * value, GParamSpec * pspec);
101 static void gst_rtsp_client_set_property (GObject * object, guint propid,
102 const GValue * value, GParamSpec * pspec);
103 static void gst_rtsp_client_finalize (GObject * obj);
105 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
106 static void client_session_finalized (GstRTSPClient * client,
107 GstRTSPSession * session);
108 static void unlink_session_transports (GstRTSPClient * client,
109 GstRTSPSession * session, GstRTSPSessionMedia * sessmedia);
110 static gboolean default_configure_client_transport (GstRTSPClient * client,
111 GstRTSPClientState * state, GstRTSPTransport * ct);
112 static GstRTSPResult default_params_set (GstRTSPClient * client,
113 GstRTSPClientState * state);
114 static GstRTSPResult default_params_get (GstRTSPClient * client,
115 GstRTSPClientState * state);
117 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
120 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
122 GObjectClass *gobject_class;
124 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
126 gobject_class = G_OBJECT_CLASS (klass);
128 gobject_class->get_property = gst_rtsp_client_get_property;
129 gobject_class->set_property = gst_rtsp_client_set_property;
130 gobject_class->finalize = gst_rtsp_client_finalize;
132 klass->create_sdp = create_sdp;
133 klass->configure_client_transport = default_configure_client_transport;
134 klass->params_set = default_params_set;
135 klass->params_get = default_params_get;
137 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
138 g_param_spec_object ("session-pool", "Session Pool",
139 "The session pool to use for client session",
140 GST_TYPE_RTSP_SESSION_POOL,
141 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
143 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
144 g_param_spec_object ("mount-points", "Mount Points",
145 "The mount points to use for client session",
146 GST_TYPE_RTSP_MOUNT_POINTS,
147 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
149 g_object_class_install_property (gobject_class, PROP_USE_CLIENT_SETTINGS,
150 g_param_spec_boolean ("use-client-settings", "Use Client Settings",
151 "Use client settings for ttl and destination in multicast",
152 DEFAULT_USE_CLIENT_SETTINGS,
153 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
155 gst_rtsp_client_signals[SIGNAL_CLOSED] =
156 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
157 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
158 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
160 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
161 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
162 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
163 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
165 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
166 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
167 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
168 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
171 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
172 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
173 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
174 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
177 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
178 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
179 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
180 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
183 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
184 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
185 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
186 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
189 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
190 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
191 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
192 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
195 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
196 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
197 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
198 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
201 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
202 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
203 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
204 set_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
205 G_TYPE_NONE, 1, G_TYPE_POINTER);
207 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
208 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
209 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
210 get_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
211 G_TYPE_NONE, 1, G_TYPE_POINTER);
214 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
215 g_mutex_init (&tunnels_lock);
217 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
221 gst_rtsp_client_init (GstRTSPClient * client)
223 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
227 g_mutex_init (&priv->lock);
228 g_mutex_init (&priv->send_lock);
229 priv->use_client_settings = DEFAULT_USE_CLIENT_SETTINGS;
233 static GstRTSPFilterResult
234 filter_session (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
237 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
239 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
240 unlink_session_transports (client, sess, sessmedia);
242 /* unmanage the media in the session */
243 return GST_RTSP_FILTER_REMOVE;
247 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
249 /* unlink all media managed in this session */
250 gst_rtsp_session_filter (session, filter_session, client);
254 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
256 GstRTSPClientPrivate *priv = client->priv;
259 for (walk = priv->sessions; walk; walk = g_list_next (walk)) {
260 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
262 /* we already know about this session */
263 if (msession == session)
267 GST_INFO ("watching session %p", session);
269 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
271 priv->sessions = g_list_prepend (priv->sessions, session);
275 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session)
277 GstRTSPClientPrivate *priv = client->priv;
279 GST_INFO ("unwatching session %p", session);
281 g_object_weak_unref (G_OBJECT (session),
282 (GWeakNotify) client_session_finalized, client);
283 priv->sessions = g_list_remove (priv->sessions, session);
287 client_cleanup_session (GstRTSPClient * client, GstRTSPSession * session)
289 g_object_weak_unref (G_OBJECT (session),
290 (GWeakNotify) client_session_finalized, client);
291 client_unlink_session (client, session);
295 client_cleanup_sessions (GstRTSPClient * client)
297 GstRTSPClientPrivate *priv = client->priv;
300 /* remove weak-ref from sessions */
301 for (sessions = priv->sessions; sessions; sessions = g_list_next (sessions)) {
302 client_cleanup_session (client, (GstRTSPSession *) sessions->data);
304 g_list_free (priv->sessions);
305 priv->sessions = NULL;
308 /* A client is finalized when the connection is broken */
310 gst_rtsp_client_finalize (GObject * obj)
312 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
313 GstRTSPClientPrivate *priv = client->priv;
315 GST_INFO ("finalize client %p", client);
317 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
320 g_source_destroy ((GSource *) priv->watch);
322 client_cleanup_sessions (client);
324 if (priv->connection)
325 gst_rtsp_connection_free (priv->connection);
326 if (priv->session_pool)
327 g_object_unref (priv->session_pool);
328 if (priv->mount_points)
329 g_object_unref (priv->mount_points);
331 g_object_unref (priv->auth);
336 gst_rtsp_media_unprepare (priv->media);
337 g_object_unref (priv->media);
340 g_free (priv->server_ip);
341 g_mutex_clear (&priv->lock);
342 g_mutex_clear (&priv->send_lock);
344 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
348 gst_rtsp_client_get_property (GObject * object, guint propid,
349 GValue * value, GParamSpec * pspec)
351 GstRTSPClient *client = GST_RTSP_CLIENT (object);
354 case PROP_SESSION_POOL:
355 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
357 case PROP_MOUNT_POINTS:
358 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
360 case PROP_USE_CLIENT_SETTINGS:
361 g_value_set_boolean (value,
362 gst_rtsp_client_get_use_client_settings (client));
365 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
370 gst_rtsp_client_set_property (GObject * object, guint propid,
371 const GValue * value, GParamSpec * pspec)
373 GstRTSPClient *client = GST_RTSP_CLIENT (object);
376 case PROP_SESSION_POOL:
377 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
379 case PROP_MOUNT_POINTS:
380 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
382 case PROP_USE_CLIENT_SETTINGS:
383 gst_rtsp_client_set_use_client_settings (client,
384 g_value_get_boolean (value));
387 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
392 * gst_rtsp_client_new:
394 * Create a new #GstRTSPClient instance.
396 * Returns: a new #GstRTSPClient
399 gst_rtsp_client_new (void)
401 GstRTSPClient *result;
403 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
409 send_message (GstRTSPClient * client, GstRTSPSession * session,
410 GstRTSPMessage * message, gboolean close)
412 GstRTSPClientPrivate *priv = client->priv;
414 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
415 "GStreamer RTSP server");
417 /* remove any previous header */
418 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
420 /* add the new session header for new session ids */
422 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
423 gst_rtsp_session_get_header (session));
426 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
427 gst_rtsp_message_dump (message);
431 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
433 g_mutex_lock (&priv->send_lock);
435 priv->send_func (client, message, close, priv->send_data);
436 g_mutex_unlock (&priv->send_lock);
438 gst_rtsp_message_unset (message);
442 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
443 GstRTSPClientState * state)
445 gst_rtsp_message_init_response (state->response, code,
446 gst_rtsp_status_as_text (code), state->request);
448 send_message (client, NULL, state->response, FALSE);
452 handle_unauthorized_request (GstRTSPClient * client, GstRTSPAuth * auth,
453 GstRTSPClientState * state)
455 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_UNAUTHORIZED,
456 gst_rtsp_status_as_text (GST_RTSP_STS_UNAUTHORIZED), state->request);
459 /* and let the authentication manager setup the auth tokens */
460 gst_rtsp_auth_setup (auth, state);
463 send_message (client, state->session, state->response, FALSE);
468 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
470 if (path1 == NULL || path2 == NULL)
473 if (strlen (path1) != len2)
476 if (strncmp (path1, path2, len2))
482 /* this function is called to initially find the media for the DESCRIBE request
483 * but is cached for when the same client (without breaking the connection) is
484 * doing a setup for the exact same url. */
485 static GstRTSPMedia *
486 find_media (GstRTSPClient * client, GstRTSPClientState * state, gint * matched)
488 GstRTSPClientPrivate *priv = client->priv;
489 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,
504 state->factory = factory;
506 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
507 goto no_factory_access;
509 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
515 path_len = strlen (path);
517 if (!paths_are_equal (priv->path, path, path_len)) {
518 /* remove any previously cached values before we try to construct a new
524 gst_rtsp_media_unprepare (priv->media);
525 g_object_unref (priv->media);
529 /* prepare the media and add it to the pipeline */
530 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
533 /* prepare the media */
534 if (!(gst_rtsp_media_prepare (media, NULL)))
537 /* now keep track of the uri and the media */
538 priv->path = g_strndup (path, path_len);
540 state->media = media;
542 /* we have seen this path before, used cached media */
544 state->media = media;
545 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
548 g_object_unref (factory);
549 state->factory = NULL;
552 g_object_ref (media);
559 GST_ERROR ("client %p: no mount points configured", client);
560 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
565 GST_ERROR ("client %p: no factory for uri %s", client, path);
566 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
571 GST_ERROR ("client %p: not authorized to see factory uri %s", client, path);
572 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
577 GST_ERROR ("client %p: not authorized for factory uri %s", client, path);
578 handle_unauthorized_request (client, priv->auth, state);
583 GST_ERROR ("client %p: can't create media", client);
584 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
585 g_object_unref (factory);
586 state->factory = NULL;
591 GST_ERROR ("client %p: can't prepare media", client);
592 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
593 g_object_unref (media);
595 g_object_unref (factory);
596 state->factory = NULL;
602 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
604 GstRTSPClientPrivate *priv = client->priv;
605 GstRTSPMessage message = { 0 };
610 gst_rtsp_message_init_data (&message, channel);
612 /* FIXME, need some sort of iovec RTSPMessage here */
613 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
616 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
618 g_mutex_lock (&priv->send_lock);
620 priv->send_func (client, &message, FALSE, priv->send_data);
621 g_mutex_unlock (&priv->send_lock);
623 gst_rtsp_message_steal_body (&message, &data, &usize);
624 gst_buffer_unmap (buffer, &map_info);
626 gst_rtsp_message_unset (&message);
632 link_transport (GstRTSPClient * client, GstRTSPSession * session,
633 GstRTSPStreamTransport * trans)
635 GstRTSPClientPrivate *priv = client->priv;
637 GST_DEBUG ("client %p: linking transport %p", client, trans);
639 gst_rtsp_stream_transport_set_callbacks (trans,
640 (GstRTSPSendFunc) do_send_data,
641 (GstRTSPSendFunc) do_send_data, client, NULL);
643 priv->transports = g_list_prepend (priv->transports, trans);
645 /* make sure our session can't expire */
646 gst_rtsp_session_prevent_expire (session);
650 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
651 GstRTSPStreamTransport * trans)
653 GstRTSPClientPrivate *priv = client->priv;
655 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
657 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
659 priv->transports = g_list_remove (priv->transports, trans);
661 /* our session can now expire */
662 gst_rtsp_session_allow_expire (session);
666 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
667 GstRTSPSessionMedia * sessmedia)
672 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
673 for (i = 0; i < n_streams; i++) {
674 GstRTSPStreamTransport *trans;
675 const GstRTSPTransport *tr;
677 /* get the transport, if there is no transport configured, skip this stream */
678 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
682 tr = gst_rtsp_stream_transport_get_transport (trans);
684 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
685 /* for TCP, unlink the stream from the TCP connection of the client */
686 unlink_transport (client, session, trans);
692 close_connection (GstRTSPClient * client)
694 GstRTSPClientPrivate *priv = client->priv;
695 const gchar *tunnelid;
697 GST_DEBUG ("client %p: closing connection", client);
699 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
700 g_mutex_lock (&tunnels_lock);
701 /* remove from tunnelids */
702 g_hash_table_remove (tunnels, tunnelid);
703 g_mutex_unlock (&tunnels_lock);
706 gst_rtsp_connection_close (priv->connection);
710 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
712 GstRTSPClientPrivate *priv = client->priv;
713 GstRTSPSession *session;
714 GstRTSPSessionMedia *sessmedia;
715 GstRTSPStatusCode code;
722 session = state->session;
727 path = state->uri->abspath;
729 /* get a handle to the configuration of the media in the session */
730 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
734 /* only aggregate control for now.. */
735 if (path[matched] != '\0')
738 state->sessmedia = sessmedia;
740 /* we emit the signal before closing the connection */
741 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
744 /* unlink the all TCP callbacks */
745 unlink_session_transports (client, session, sessmedia);
747 /* remove the session from the watched sessions */
748 client_unwatch_session (client, session);
750 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
752 /* unmanage the media in the session, returns false if all media session
754 if (!gst_rtsp_session_release_media (session, sessmedia)) {
755 /* remove the session */
756 gst_rtsp_session_pool_remove (priv->session_pool, session);
758 /* construct the response now */
759 code = GST_RTSP_STS_OK;
760 gst_rtsp_message_init_response (state->response, code,
761 gst_rtsp_status_as_text (code), state->request);
763 send_message (client, session, state->response, TRUE);
770 GST_ERROR ("client %p: no session", client);
771 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
776 GST_ERROR ("client %p: no uri supplied", client);
777 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
782 GST_ERROR ("client %p: no media for uri", client);
783 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
788 GST_ERROR ("client %p: no aggregate path %s", client, path);
789 send_generic_response (client,
790 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
796 default_params_set (GstRTSPClient * client, GstRTSPClientState * state)
800 res = gst_rtsp_params_set (client, state);
806 default_params_get (GstRTSPClient * client, GstRTSPClientState * state)
810 res = gst_rtsp_params_get (client, state);
816 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
822 res = gst_rtsp_message_get_body (state->request, &data, &size);
823 if (res != GST_RTSP_OK)
827 /* no body, keep-alive request */
828 send_generic_response (client, GST_RTSP_STS_OK, state);
830 /* there is a body, handle the params */
831 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, state);
832 if (res != GST_RTSP_OK)
835 send_message (client, state->session, state->response, FALSE);
838 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
846 GST_ERROR ("client %p: bad request", client);
847 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
853 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
859 res = gst_rtsp_message_get_body (state->request, &data, &size);
860 if (res != GST_RTSP_OK)
864 /* no body, keep-alive request */
865 send_generic_response (client, GST_RTSP_STS_OK, state);
867 /* there is a body, handle the params */
868 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, state);
869 if (res != GST_RTSP_OK)
872 send_message (client, state->session, state->response, FALSE);
875 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
883 GST_ERROR ("client %p: bad request", client);
884 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
890 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
892 GstRTSPSession *session;
893 GstRTSPSessionMedia *sessmedia;
894 GstRTSPStatusCode code;
895 GstRTSPState rtspstate;
899 if (!(session = state->session))
905 path = state->uri->abspath;
907 /* get a handle to the configuration of the media in the session */
908 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
912 if (path[matched] != '\0')
915 state->sessmedia = sessmedia;
917 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
918 /* the session state must be playing or recording */
919 if (rtspstate != GST_RTSP_STATE_PLAYING &&
920 rtspstate != GST_RTSP_STATE_RECORDING)
923 /* unlink the all TCP callbacks */
924 unlink_session_transports (client, session, sessmedia);
926 /* then pause sending */
927 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
929 /* construct the response now */
930 code = GST_RTSP_STS_OK;
931 gst_rtsp_message_init_response (state->response, code,
932 gst_rtsp_status_as_text (code), state->request);
934 send_message (client, session, state->response, FALSE);
936 /* the state is now READY */
937 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
939 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
947 GST_ERROR ("client %p: no seesion", client);
948 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
953 GST_ERROR ("client %p: no uri supplied", client);
954 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
959 GST_ERROR ("client %p: no media for uri", client);
960 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
965 GST_ERROR ("client %p: no aggregate path %s", client, path);
966 send_generic_response (client,
967 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
972 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
973 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
980 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
982 GstRTSPSession *session;
983 GstRTSPSessionMedia *sessmedia;
985 GstRTSPStatusCode code;
987 guint n_streams, i, infocount;
989 GstRTSPTimeRange *range;
991 GstRTSPState rtspstate;
992 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
996 if (!(session = state->session))
1002 path = state->uri->abspath;
1004 /* get a handle to the configuration of the media in the session */
1005 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1009 if (path[matched] != '\0')
1012 state->sessmedia = sessmedia;
1013 state->media = media = gst_rtsp_session_media_get_media (sessmedia);
1015 /* the session state must be playing or ready */
1016 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1017 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1020 /* parse the range header if we have one */
1022 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
1023 if (res == GST_RTSP_OK) {
1024 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1025 /* we have a range, seek to the position */
1027 gst_rtsp_media_seek (media, range);
1028 gst_rtsp_range_free (range);
1032 /* grab RTPInfo from the payloaders now */
1033 rtpinfo = g_string_new ("");
1035 n_streams = gst_rtsp_media_n_streams (media);
1036 for (i = 0, infocount = 0; i < n_streams; i++) {
1037 GstRTSPStreamTransport *trans;
1038 GstRTSPStream *stream;
1039 const GstRTSPTransport *tr;
1043 /* get the transport, if there is no transport configured, skip this stream */
1044 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
1045 if (trans == NULL) {
1046 GST_INFO ("stream %d is not configured", i);
1049 tr = gst_rtsp_stream_transport_get_transport (trans);
1051 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1052 /* for TCP, link the stream to the TCP connection of the client */
1053 link_transport (client, session, trans);
1056 stream = gst_rtsp_stream_transport_get_stream (trans);
1057 if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
1059 g_string_append (rtpinfo, ", ");
1061 uristr = gst_rtsp_url_get_request_uri (state->uri);
1062 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
1063 uristr, i, seq, rtptime);
1068 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
1072 /* construct the response now */
1073 code = GST_RTSP_STS_OK;
1074 gst_rtsp_message_init_response (state->response, code,
1075 gst_rtsp_status_as_text (code), state->request);
1077 /* add the RTP-Info header */
1078 if (infocount > 0) {
1079 str = g_string_free (rtpinfo, FALSE);
1080 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
1082 g_string_free (rtpinfo, TRUE);
1086 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1087 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
1089 send_message (client, session, state->response, FALSE);
1091 /* start playing after sending the request */
1092 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1094 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1096 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
1104 GST_ERROR ("client %p: no session", client);
1105 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1110 GST_ERROR ("client %p: no uri supplied", client);
1111 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1116 GST_ERROR ("client %p: media not found", client);
1117 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1122 GST_ERROR ("client %p: no aggregate path %s", client, path);
1123 send_generic_response (client,
1124 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
1129 GST_ERROR ("client %p: not PLAYING or READY", client);
1130 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1137 do_keepalive (GstRTSPSession * session)
1139 GST_INFO ("keep session %p alive", session);
1140 gst_rtsp_session_touch (session);
1143 /* parse @transport and return a valid transport in @tr. only transports
1144 * from @supported are returned. Returns FALSE if no valid transport
1147 parse_transport (const char *transport, GstRTSPLowerTrans supported,
1148 GstRTSPTransport * tr)
1155 gst_rtsp_transport_init (tr);
1157 GST_DEBUG ("parsing transports %s", transport);
1159 transports = g_strsplit (transport, ",", 0);
1161 /* loop through the transports, try to parse */
1162 for (i = 0; transports[i]; i++) {
1163 res = gst_rtsp_transport_parse (transports[i], tr);
1164 if (res != GST_RTSP_OK) {
1165 /* no valid transport, search some more */
1166 GST_WARNING ("could not parse transport %s", transports[i]);
1170 /* we have a transport, see if it's RTP/AVP */
1171 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1172 GST_WARNING ("invalid transport %s", transports[i]);
1176 if (!(tr->lower_transport & supported)) {
1177 GST_WARNING ("unsupported transport %s", transports[i]);
1181 /* we have a valid transport */
1182 GST_INFO ("found valid transport %s", transports[i]);
1187 gst_rtsp_transport_init (tr);
1189 g_strfreev (transports);
1195 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
1196 GstRTSPMessage * request)
1198 gchar *blocksize_str;
1199 gboolean ret = TRUE;
1201 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1202 &blocksize_str, 0) == GST_RTSP_OK) {
1206 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1207 if (end == blocksize_str) {
1208 GST_ERROR ("failed to parse blocksize");
1211 /* we don't want to change the mtu when this media
1212 * can be shared because it impacts other clients */
1213 if (gst_rtsp_media_is_shared (media))
1216 if (blocksize > G_MAXUINT)
1217 blocksize = G_MAXUINT;
1218 gst_rtsp_stream_set_mtu (stream, blocksize);
1225 default_configure_client_transport (GstRTSPClient * client,
1226 GstRTSPClientState * state, GstRTSPTransport * ct)
1228 GstRTSPClientPrivate *priv = client->priv;
1230 /* we have a valid transport now, set the destination of the client. */
1231 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1232 if (ct->destination && priv->use_client_settings) {
1233 GstRTSPAddress *addr;
1235 addr = gst_rtsp_stream_reserve_address (state->stream, ct->destination,
1236 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1241 gst_rtsp_address_free (addr);
1243 GstRTSPAddress *addr;
1244 GSocketFamily family;
1246 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1248 addr = gst_rtsp_stream_get_multicast_address (state->stream, family);
1252 g_free (ct->destination);
1253 ct->destination = g_strdup (addr->address);
1254 ct->port.min = addr->port;
1255 ct->port.max = addr->port + addr->n_ports - 1;
1256 ct->ttl = addr->ttl;
1258 gst_rtsp_address_free (addr);
1263 url = gst_rtsp_connection_get_url (priv->connection);
1264 g_free (ct->destination);
1265 ct->destination = g_strdup (url->host);
1267 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1268 /* check if the client selected channels for TCP */
1269 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1270 gst_rtsp_session_media_alloc_channels (state->sessmedia,
1280 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1285 static GstRTSPTransport *
1286 make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
1287 GstRTSPTransport * ct)
1289 GstRTSPTransport *st;
1291 GSocketFamily family;
1293 /* prepare the server transport */
1294 gst_rtsp_transport_new (&st);
1296 st->trans = ct->trans;
1297 st->profile = ct->profile;
1298 st->lower_transport = ct->lower_transport;
1300 addr = g_inet_address_new_from_string (ct->destination);
1303 GST_ERROR ("failed to get inet addr from client destination");
1304 family = G_SOCKET_FAMILY_IPV4;
1306 family = g_inet_address_get_family (addr);
1307 g_object_unref (addr);
1311 switch (st->lower_transport) {
1312 case GST_RTSP_LOWER_TRANS_UDP:
1313 st->client_port = ct->client_port;
1314 gst_rtsp_stream_get_server_port (state->stream, &st->server_port, family);
1316 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1317 st->port = ct->port;
1318 st->destination = g_strdup (ct->destination);
1321 case GST_RTSP_LOWER_TRANS_TCP:
1322 st->interleaved = ct->interleaved;
1327 gst_rtsp_stream_get_ssrc (state->stream, &st->ssrc);
1333 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
1335 GstRTSPClientPrivate *priv = client->priv;
1339 GstRTSPTransport *ct, *st;
1340 GstRTSPLowerTrans supported;
1341 GstRTSPStatusCode code;
1342 GstRTSPSession *session;
1343 GstRTSPStreamTransport *trans;
1345 GstRTSPSessionMedia *sessmedia;
1346 GstRTSPMedia *media;
1347 GstRTSPStream *stream;
1348 GstRTSPState rtspstate;
1349 GstRTSPClientClass *klass;
1350 gchar *path, *control;
1357 path = uri->abspath;
1359 /* parse the transport */
1361 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
1363 if (res != GST_RTSP_OK)
1366 /* we create the session after parsing stuff so that we don't make
1367 * a session for malformed requests */
1368 if (priv->session_pool == NULL)
1371 session = state->session;
1374 g_object_ref (session);
1375 /* get a handle to the configuration of the media in the session, this can
1376 * return NULL if this is a new url to manage in this session. */
1377 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1379 /* we need a new media configuration in this session */
1383 /* we have no session media, find one and manage it */
1384 if (sessmedia == NULL) {
1385 /* get a handle to the configuration of the media in the session */
1386 media = find_media (client, state, &matched);
1388 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1389 g_object_ref (media);
1391 /* no media, not found then */
1393 goto media_not_found;
1395 /* path is what matched. We can modify the parsed uri in place */
1396 path[matched] = '\0';
1397 /* control is remainder */
1398 control = &path[matched + 1];
1400 /* find the stream now using the control part */
1401 stream = gst_rtsp_media_find_stream (media, control);
1403 goto stream_not_found;
1405 /* now we have a uri identifying a valid media and stream */
1406 state->stream = stream;
1407 state->media = media;
1409 if (session == NULL) {
1410 /* create a session if this fails we probably reached our session limit or
1412 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1413 goto service_unavailable;
1415 /* make sure this client is closed when the session is closed */
1416 client_watch_session (client, session);
1418 /* signal new session */
1419 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1422 state->session = session;
1425 if (sessmedia == NULL) {
1426 /* manage the media in our session now, if not done already */
1427 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1428 /* if we stil have no media, error */
1429 if (sessmedia == NULL)
1430 goto sessmedia_unavailable;
1432 g_object_unref (media);
1435 state->sessmedia = sessmedia;
1437 /* set blocksize on this stream */
1438 if (!handle_blocksize (media, stream, state->request))
1439 goto invalid_blocksize;
1441 gst_rtsp_transport_new (&ct);
1443 /* our supported transports */
1444 supported = GST_RTSP_LOWER_TRANS_UDP |
1445 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
1447 /* parse and find a usable supported transport */
1448 if (!parse_transport (transport, supported, ct))
1449 goto unsupported_transports;
1451 /* update the client transport */
1452 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1453 if (!klass->configure_client_transport (client, state, ct))
1454 goto unsupported_client_transport;
1456 /* set in the session media transport */
1457 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1459 /* configure keepalive for this transport */
1460 gst_rtsp_stream_transport_set_keepalive (trans,
1461 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1463 /* create and serialize the server transport */
1464 st = make_server_transport (client, state, ct);
1465 trans_str = gst_rtsp_transport_as_text (st);
1466 gst_rtsp_transport_free (st);
1468 /* construct the response now */
1469 code = GST_RTSP_STS_OK;
1470 gst_rtsp_message_init_response (state->response, code,
1471 gst_rtsp_status_as_text (code), state->request);
1473 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1477 send_message (client, session, state->response, FALSE);
1479 /* update the state */
1480 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1481 switch (rtspstate) {
1482 case GST_RTSP_STATE_PLAYING:
1483 case GST_RTSP_STATE_RECORDING:
1484 case GST_RTSP_STATE_READY:
1485 /* no state change */
1488 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1491 g_object_unref (session);
1493 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST],
1501 GST_ERROR ("client %p: no uri", client);
1502 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1507 GST_ERROR ("client %p: no transport", client);
1508 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1513 GST_ERROR ("client %p: no session pool configured", client);
1514 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1519 GST_ERROR ("client %p: media '%s' not found", client, path);
1520 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1525 GST_ERROR ("client %p: stream '%s' not found", client, control);
1526 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1527 g_object_unref (media);
1530 service_unavailable:
1532 GST_ERROR ("client %p: can't create session", client);
1533 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1534 g_object_unref (media);
1537 sessmedia_unavailable:
1539 GST_ERROR ("client %p: can't create session media", client);
1540 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1541 g_object_unref (media);
1542 g_object_unref (session);
1547 GST_ERROR ("client %p: invalid blocksize", client);
1548 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1549 g_object_unref (session);
1552 unsupported_transports:
1554 GST_ERROR ("client %p: unsupported transports", client);
1555 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1556 gst_rtsp_transport_free (ct);
1557 g_object_unref (session);
1560 unsupported_client_transport:
1562 GST_ERROR ("client %p: unsupported client transport", client);
1563 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1564 gst_rtsp_transport_free (ct);
1565 g_object_unref (session);
1570 static GstSDPMessage *
1571 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1573 GstRTSPClientPrivate *priv = client->priv;
1578 gst_sdp_message_new (&sdp);
1580 /* some standard things first */
1581 gst_sdp_message_set_version (sdp, "0");
1588 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1591 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1592 gst_sdp_message_set_information (sdp, "rtsp-server");
1593 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1594 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1595 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1596 gst_sdp_message_add_attribute (sdp, "control", "*");
1598 info.is_ipv6 = priv->is_ipv6;
1599 info.server_ip = priv->server_ip;
1601 /* create an SDP for the media object */
1602 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1610 GST_ERROR ("client %p: could not create SDP", client);
1611 gst_sdp_message_free (sdp);
1616 /* for the describe we must generate an SDP */
1618 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1623 gchar *str, *content_base;
1624 GstRTSPMedia *media;
1625 GstRTSPClientClass *klass;
1627 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1632 /* check what kind of format is accepted, we don't really do anything with it
1633 * and always return SDP for now. */
1638 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1640 if (res == GST_RTSP_ENOTIMPL)
1643 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1647 /* find the media object for the uri */
1648 if (!(media = find_media (client, state, NULL)))
1651 /* create an SDP for the media object on this client */
1652 if (!(sdp = klass->create_sdp (client, media)))
1655 g_object_unref (media);
1657 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1658 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1660 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1663 /* content base for some clients that might screw up creating the setup uri */
1664 str = gst_rtsp_url_get_request_uri (state->uri);
1665 str_len = strlen (str);
1667 /* check for trailing '/' and append one */
1668 if (str[str_len - 1] != '/') {
1669 content_base = g_malloc (str_len + 2);
1670 memcpy (content_base, str, str_len);
1671 content_base[str_len] = '/';
1672 content_base[str_len + 1] = '\0';
1678 GST_INFO ("adding content-base: %s", content_base);
1680 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1682 g_free (content_base);
1684 /* add SDP to the response body */
1685 str = gst_sdp_message_as_text (sdp);
1686 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1687 gst_sdp_message_free (sdp);
1689 send_message (client, state->session, state->response, FALSE);
1691 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1699 GST_ERROR ("client %p: no uri", client);
1700 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1705 GST_ERROR ("client %p: no media", client);
1706 /* error reply is already sent */
1711 GST_ERROR ("client %p: can't create SDP", client);
1712 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1713 g_object_unref (media);
1719 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1721 GstRTSPMethod options;
1724 options = GST_RTSP_DESCRIBE |
1729 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1731 str = gst_rtsp_options_as_text (options);
1733 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1734 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1736 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1739 send_message (client, state->session, state->response, FALSE);
1741 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1747 /* remove duplicate and trailing '/' */
1749 sanitize_uri (GstRTSPUrl * uri)
1753 gboolean have_slash, prev_slash;
1755 s = d = uri->abspath;
1756 len = strlen (uri->abspath);
1760 for (i = 0; i < len; i++) {
1761 have_slash = s[i] == '/';
1763 if (!have_slash || !prev_slash)
1765 prev_slash = have_slash;
1767 len = d - uri->abspath;
1768 /* don't remove the first slash if that's the only thing left */
1769 if (len > 1 && *(d - 1) == '/')
1775 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1777 GstRTSPClientPrivate *priv = client->priv;
1779 GST_INFO ("client %p: session %p finished", client, session);
1781 /* unlink all media managed in this session */
1782 client_unlink_session (client, session);
1784 /* remove the session */
1785 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1786 GST_INFO ("client %p: all sessions finalized, close the connection",
1788 close_connection (client);
1792 static GPrivate state_key;
1795 * gst_rtsp_client_state_get_current:
1797 * Get the current #GstRTSPClientState. This object is retrieved from the
1798 * current thread that is handling the request for a client.
1800 * Returns: a #GstRTSPClientState
1802 GstRTSPClientState *
1803 gst_rtsp_client_state_get_current (void)
1805 return g_private_get (&state_key);
1809 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1811 GstRTSPClientPrivate *priv = client->priv;
1812 GstRTSPMethod method;
1813 const gchar *uristr;
1814 GstRTSPUrl *uri = NULL;
1815 GstRTSPVersion version;
1817 GstRTSPSession *session = NULL;
1818 GstRTSPClientState state = { NULL };
1819 GstRTSPMessage response = { 0 };
1822 state.client = client;
1823 state.request = request;
1824 state.response = &response;
1825 state.auth = priv->auth;
1826 g_private_set (&state_key, &state);
1828 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1829 gst_rtsp_message_dump (request);
1832 GST_INFO ("client %p: received a request", client);
1834 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1836 /* we can only handle 1.0 requests */
1837 if (version != GST_RTSP_VERSION_1_0)
1840 state.method = method;
1842 /* we always try to parse the url first */
1843 if (strcmp (uristr, "*") == 0) {
1844 /* special case where we have * as uri, keep uri = NULL */
1845 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1848 /* get the session if there is any */
1849 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1850 if (res == GST_RTSP_OK) {
1851 if (priv->session_pool == NULL)
1854 /* we had a session in the request, find it again */
1855 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1856 goto session_not_found;
1858 /* we add the session to the client list of watched sessions. When a session
1859 * disappears because it times out, we will be notified. If all sessions are
1860 * gone, we will close the connection */
1861 client_watch_session (client, session);
1864 /* sanitize the uri */
1868 state.session = session;
1870 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
1871 goto not_authorized;
1873 /* now see what is asked and dispatch to a dedicated handler */
1875 case GST_RTSP_OPTIONS:
1876 handle_options_request (client, &state);
1878 case GST_RTSP_DESCRIBE:
1879 handle_describe_request (client, &state);
1881 case GST_RTSP_SETUP:
1882 handle_setup_request (client, &state);
1885 handle_play_request (client, &state);
1887 case GST_RTSP_PAUSE:
1888 handle_pause_request (client, &state);
1890 case GST_RTSP_TEARDOWN:
1891 handle_teardown_request (client, &state);
1893 case GST_RTSP_SET_PARAMETER:
1894 handle_set_param_request (client, &state);
1896 case GST_RTSP_GET_PARAMETER:
1897 handle_get_param_request (client, &state);
1899 case GST_RTSP_ANNOUNCE:
1900 case GST_RTSP_RECORD:
1901 case GST_RTSP_REDIRECT:
1902 goto not_implemented;
1903 case GST_RTSP_INVALID:
1909 g_private_set (&state_key, NULL);
1911 g_object_unref (session);
1913 gst_rtsp_url_free (uri);
1919 GST_ERROR ("client %p: version %d not supported", client, version);
1920 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1926 GST_ERROR ("client %p: bad request", client);
1927 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1932 GST_ERROR ("client %p: no pool configured", client);
1933 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1938 GST_ERROR ("client %p: session not found", client);
1939 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1944 GST_ERROR ("client %p: not allowed", client);
1945 handle_unauthorized_request (client, priv->auth, &state);
1950 GST_ERROR ("client %p: method %d not implemented", client, method);
1951 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1957 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1959 GstRTSPClientPrivate *priv = client->priv;
1968 /* find the stream for this message */
1969 res = gst_rtsp_message_parse_data (message, &channel);
1970 if (res != GST_RTSP_OK)
1973 gst_rtsp_message_steal_body (message, &data, &size);
1975 buffer = gst_buffer_new_wrapped (data, size);
1978 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1979 GstRTSPStreamTransport *trans;
1980 GstRTSPStream *stream;
1981 const GstRTSPTransport *tr;
1985 tr = gst_rtsp_stream_transport_get_transport (trans);
1986 stream = gst_rtsp_stream_transport_get_stream (trans);
1988 /* check for TCP transport */
1989 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1990 /* dispatch to the stream based on the channel number */
1991 if (tr->interleaved.min == channel) {
1992 gst_rtsp_stream_recv_rtp (stream, buffer);
1995 } else if (tr->interleaved.max == channel) {
1996 gst_rtsp_stream_recv_rtcp (stream, buffer);
2003 gst_buffer_unref (buffer);
2007 * gst_rtsp_client_set_session_pool:
2008 * @client: a #GstRTSPClient
2009 * @pool: a #GstRTSPSessionPool
2011 * Set @pool as the sessionpool for @client which it will use to find
2012 * or allocate sessions. the sessionpool is usually inherited from the server
2013 * that created the client but can be overridden later.
2016 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2017 GstRTSPSessionPool * pool)
2019 GstRTSPSessionPool *old;
2020 GstRTSPClientPrivate *priv;
2022 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2024 priv = client->priv;
2027 g_object_ref (pool);
2029 g_mutex_lock (&priv->lock);
2030 old = priv->session_pool;
2031 priv->session_pool = pool;
2032 g_mutex_unlock (&priv->lock);
2035 g_object_unref (old);
2039 * gst_rtsp_client_get_session_pool:
2040 * @client: a #GstRTSPClient
2042 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2044 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2046 GstRTSPSessionPool *
2047 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2049 GstRTSPClientPrivate *priv;
2050 GstRTSPSessionPool *result;
2052 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2054 priv = client->priv;
2056 g_mutex_lock (&priv->lock);
2057 if ((result = priv->session_pool))
2058 g_object_ref (result);
2059 g_mutex_unlock (&priv->lock);
2065 * gst_rtsp_client_set_mount_points:
2066 * @client: a #GstRTSPClient
2067 * @mounts: a #GstRTSPMountPoints
2069 * Set @mounts as the mount points for @client which it will use to map urls
2070 * to media streams. These mount points are usually inherited from the server that
2071 * created the client but can be overriden later.
2074 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2075 GstRTSPMountPoints * mounts)
2077 GstRTSPClientPrivate *priv;
2078 GstRTSPMountPoints *old;
2080 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2082 priv = client->priv;
2085 g_object_ref (mounts);
2087 g_mutex_lock (&priv->lock);
2088 old = priv->mount_points;
2089 priv->mount_points = mounts;
2090 g_mutex_unlock (&priv->lock);
2093 g_object_unref (old);
2097 * gst_rtsp_client_get_mount_points:
2098 * @client: a #GstRTSPClient
2100 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2102 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2104 GstRTSPMountPoints *
2105 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2107 GstRTSPClientPrivate *priv;
2108 GstRTSPMountPoints *result;
2110 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2112 priv = client->priv;
2114 g_mutex_lock (&priv->lock);
2115 if ((result = priv->mount_points))
2116 g_object_ref (result);
2117 g_mutex_unlock (&priv->lock);
2123 * gst_rtsp_client_set_use_client_settings:
2124 * @client: a #GstRTSPClient
2125 * @use_client_settings: whether to use client settings for multicast
2127 * Use client transport settings (destination and ttl) for multicast.
2128 * When @use_client_settings is %FALSE, the server settings will be
2132 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
2133 gboolean use_client_settings)
2135 GstRTSPClientPrivate *priv;
2137 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2139 priv = client->priv;
2141 g_mutex_lock (&priv->lock);
2142 priv->use_client_settings = use_client_settings;
2143 g_mutex_unlock (&priv->lock);
2147 * gst_rtsp_client_get_use_client_settings:
2148 * @client: a #GstRTSPClient
2150 * Check if client transport settings (destination and ttl) for multicast
2154 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
2156 GstRTSPClientPrivate *priv;
2159 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2161 priv = client->priv;
2163 g_mutex_lock (&priv->lock);
2164 res = priv->use_client_settings;
2165 g_mutex_unlock (&priv->lock);
2171 * gst_rtsp_client_set_auth:
2172 * @client: a #GstRTSPClient
2173 * @auth: a #GstRTSPAuth
2175 * configure @auth to be used as the authentication manager of @client.
2178 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2180 GstRTSPClientPrivate *priv;
2183 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2185 priv = client->priv;
2188 g_object_ref (auth);
2190 g_mutex_lock (&priv->lock);
2193 g_mutex_unlock (&priv->lock);
2196 g_object_unref (old);
2201 * gst_rtsp_client_get_auth:
2202 * @client: a #GstRTSPClient
2204 * Get the #GstRTSPAuth used as the authentication manager of @client.
2206 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2210 gst_rtsp_client_get_auth (GstRTSPClient * client)
2212 GstRTSPClientPrivate *priv;
2213 GstRTSPAuth *result;
2215 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2217 priv = client->priv;
2219 g_mutex_lock (&priv->lock);
2220 if ((result = priv->auth))
2221 g_object_ref (result);
2222 g_mutex_unlock (&priv->lock);
2228 * gst_rtsp_client_set_thread_pool:
2229 * @client: a #GstRTSPClient
2230 * @pool: a #GstRTSPThreadPool
2232 * configure @pool to be used as the thread pool of @client.
2235 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2236 GstRTSPThreadPool * pool)
2238 GstRTSPClientPrivate *priv;
2239 GstRTSPThreadPool *old;
2241 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2243 priv = client->priv;
2246 g_object_ref (pool);
2248 g_mutex_lock (&priv->lock);
2249 old = priv->thread_pool;
2250 priv->thread_pool = pool;
2251 g_mutex_unlock (&priv->lock);
2254 g_object_unref (old);
2258 * gst_rtsp_client_get_thread_pool:
2259 * @client: a #GstRTSPClient
2261 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2263 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2267 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2269 GstRTSPClientPrivate *priv;
2270 GstRTSPThreadPool *result;
2272 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2274 priv = client->priv;
2276 g_mutex_lock (&priv->lock);
2277 if ((result = priv->thread_pool))
2278 g_object_ref (result);
2279 g_mutex_unlock (&priv->lock);
2285 * gst_rtsp_client_set_connection:
2286 * @client: a #GstRTSPClient
2287 * @conn: (transfer full): a #GstRTSPConnection
2289 * Set the #GstRTSPConnection of @client. This function takes ownership of
2292 * Returns: %TRUE on success.
2295 gst_rtsp_client_set_connection (GstRTSPClient * client,
2296 GstRTSPConnection * conn)
2298 GstRTSPClientPrivate *priv;
2299 GSocket *read_socket;
2300 GSocketAddress *address;
2302 GError *error = NULL;
2304 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2305 g_return_val_if_fail (conn != NULL, FALSE);
2307 priv = client->priv;
2309 read_socket = gst_rtsp_connection_get_read_socket (conn);
2311 if (!(address = g_socket_get_local_address (read_socket, &error)))
2314 g_free (priv->server_ip);
2315 /* keep the original ip that the client connected to */
2316 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2317 GInetAddress *iaddr;
2319 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2321 /* socket might be ipv6 but adress still ipv4 */
2322 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2323 priv->server_ip = g_inet_address_to_string (iaddr);
2324 g_object_unref (address);
2326 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2327 priv->server_ip = g_strdup ("unknown");
2330 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2331 priv->server_ip, priv->is_ipv6);
2333 url = gst_rtsp_connection_get_url (conn);
2334 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2336 priv->connection = conn;
2343 GST_ERROR ("could not get local address %s", error->message);
2344 g_error_free (error);
2350 * gst_rtsp_client_get_connection:
2351 * @client: a #GstRTSPClient
2353 * Get the #GstRTSPConnection of @client.
2355 * Returns: (transfer none): the #GstRTSPConnection of @client.
2356 * The connection object returned remains valid until the client is freed.
2359 gst_rtsp_client_get_connection (GstRTSPClient * client)
2361 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2363 return client->priv->connection;
2367 * gst_rtsp_client_set_send_func:
2368 * @client: a #GstRTSPClient
2369 * @func: a #GstRTSPClientSendFunc
2370 * @user_data: user data passed to @func
2371 * @notify: called when @user_data is no longer in use
2373 * Set @func as the callback that will be called when a new message needs to be
2374 * sent to the client. @user_data is passed to @func and @notify is called when
2375 * @user_data is no longer in use.
2378 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2379 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2381 GstRTSPClientPrivate *priv;
2382 GDestroyNotify old_notify;
2385 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2387 priv = client->priv;
2389 g_mutex_lock (&priv->send_lock);
2390 priv->send_func = func;
2391 old_notify = priv->send_notify;
2392 old_data = priv->send_data;
2393 priv->send_notify = notify;
2394 priv->send_data = user_data;
2395 g_mutex_unlock (&priv->send_lock);
2398 old_notify (old_data);
2402 * gst_rtsp_client_handle_message:
2403 * @client: a #GstRTSPClient
2404 * @message: an #GstRTSPMessage
2406 * Let the client handle @message.
2408 * Returns: a #GstRTSPResult.
2411 gst_rtsp_client_handle_message (GstRTSPClient * client,
2412 GstRTSPMessage * message)
2414 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2415 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2417 switch (message->type) {
2418 case GST_RTSP_MESSAGE_REQUEST:
2419 handle_request (client, message);
2421 case GST_RTSP_MESSAGE_RESPONSE:
2423 case GST_RTSP_MESSAGE_DATA:
2424 handle_data (client, message);
2433 * gst_rtsp_client_send_request:
2434 * @client: a #GstRTSPClient
2435 * @session: a #GstRTSPSession to send the request to or %NULL
2436 * @request: The request #GstRTSPMessage to send
2438 * Send a request message to the client.
2441 gst_rtsp_client_send_request (GstRTSPClient * client, GstRTSPSession * session,
2442 GstRTSPMessage * request)
2444 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2445 g_return_val_if_fail (request != NULL, GST_RTSP_EINVAL);
2446 g_return_val_if_fail (request->type == GST_RTSP_MESSAGE_REQUEST,
2449 send_message (client, session, request, FALSE);
2454 static GstRTSPResult
2455 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2456 gboolean close, gpointer user_data)
2458 GstRTSPClientPrivate *priv = client->priv;
2460 /* send the response and store the seq number so we can wait until it's
2461 * written to the client to close the connection */
2462 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2463 &priv->close_seq : NULL);
2466 static GstRTSPResult
2467 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2470 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2473 static GstRTSPResult
2474 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2476 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2477 GstRTSPClientPrivate *priv = client->priv;
2479 if (priv->close_seq && priv->close_seq == cseq) {
2480 priv->close_seq = 0;
2481 close_connection (client);
2487 static GstRTSPResult
2488 closed (GstRTSPWatch * watch, gpointer user_data)
2490 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2491 GstRTSPClientPrivate *priv = client->priv;
2492 const gchar *tunnelid;
2494 GST_INFO ("client %p: connection closed", client);
2496 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2497 g_mutex_lock (&tunnels_lock);
2498 /* remove from tunnelids */
2499 g_hash_table_remove (tunnels, tunnelid);
2500 g_mutex_unlock (&tunnels_lock);
2503 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2508 static GstRTSPResult
2509 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2511 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2514 str = gst_rtsp_strresult (result);
2515 GST_INFO ("client %p: received an error %s", client, str);
2521 static GstRTSPResult
2522 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2523 GstRTSPMessage * message, guint id, gpointer user_data)
2525 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2528 str = gst_rtsp_strresult (result);
2530 ("client %p: error when handling message %p with id %d: %s",
2531 client, message, id, str);
2538 remember_tunnel (GstRTSPClient * client)
2540 GstRTSPClientPrivate *priv = client->priv;
2541 const gchar *tunnelid;
2543 /* store client in the pending tunnels */
2544 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2545 if (tunnelid == NULL)
2548 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2550 /* we can't have two clients connecting with the same tunnelid */
2551 g_mutex_lock (&tunnels_lock);
2552 if (g_hash_table_lookup (tunnels, tunnelid))
2553 goto tunnel_existed;
2555 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2556 g_mutex_unlock (&tunnels_lock);
2563 GST_ERROR ("client %p: no tunnelid provided", client);
2568 g_mutex_unlock (&tunnels_lock);
2569 GST_ERROR ("client %p: tunnel session %s already existed", client,
2575 static GstRTSPStatusCode
2576 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2578 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2579 GstRTSPClientPrivate *priv = client->priv;
2581 GST_INFO ("client %p: tunnel start (connection %p)", client,
2584 if (!remember_tunnel (client))
2587 return GST_RTSP_STS_OK;
2592 GST_ERROR ("client %p: error starting tunnel", client);
2593 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2597 static GstRTSPResult
2598 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2600 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2601 GstRTSPClientPrivate *priv = client->priv;
2603 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2606 /* ignore error, it'll only be a problem when the client does a POST again */
2607 remember_tunnel (client);
2612 static GstRTSPResult
2613 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2615 const gchar *tunnelid;
2616 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2617 GstRTSPClientPrivate *priv = client->priv;
2618 GstRTSPClient *oclient;
2619 GstRTSPClientPrivate *opriv;
2621 GST_INFO ("client %p: tunnel complete", client);
2623 /* find previous tunnel */
2624 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2625 if (tunnelid == NULL)
2628 g_mutex_lock (&tunnels_lock);
2629 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2632 /* remove the old client from the table. ref before because removing it will
2633 * remove the ref to it. */
2634 g_object_ref (oclient);
2635 g_hash_table_remove (tunnels, tunnelid);
2637 opriv = oclient->priv;
2639 if (opriv->watch == NULL)
2641 g_mutex_unlock (&tunnels_lock);
2643 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2644 opriv->connection, priv->connection);
2646 /* merge the tunnels into the first client */
2647 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2648 gst_rtsp_watch_reset (opriv->watch);
2649 g_object_unref (oclient);
2656 GST_ERROR ("client %p: no tunnelid provided", client);
2657 return GST_RTSP_ERROR;
2661 g_mutex_unlock (&tunnels_lock);
2662 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2663 return GST_RTSP_ERROR;
2667 g_mutex_unlock (&tunnels_lock);
2668 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2669 g_object_unref (oclient);
2670 return GST_RTSP_ERROR;
2674 static GstRTSPWatchFuncs watch_funcs = {
2686 client_watch_notify (GstRTSPClient * client)
2688 GstRTSPClientPrivate *priv = client->priv;
2690 GST_INFO ("client %p: watch destroyed", client);
2692 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2693 g_object_unref (client);
2697 * gst_rtsp_client_attach:
2698 * @client: a #GstRTSPClient
2699 * @context: (allow-none): a #GMainContext
2701 * Attaches @client to @context. When the mainloop for @context is run, the
2702 * client will be dispatched. When @context is NULL, the default context will be
2705 * This function should be called when the client properties and urls are fully
2706 * configured and the client is ready to start.
2708 * Returns: the ID (greater than 0) for the source within the GMainContext.
2711 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2713 GstRTSPClientPrivate *priv;
2716 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2717 priv = client->priv;
2718 g_return_val_if_fail (priv->watch == NULL, 0);
2720 /* create watch for the connection and attach */
2721 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2722 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2723 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2724 (GDestroyNotify) gst_rtsp_watch_unref);
2726 /* FIXME make this configurable. We don't want to do this yet because it will
2727 * be superceeded by a cache object later */
2728 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2730 GST_INFO ("attaching to context %p", context);
2731 res = gst_rtsp_watch_attach (priv->watch, context);
2737 * gst_rtsp_client_session_filter:
2738 * @client: a #GstRTSPclient
2739 * @func: (scope call): a callback
2740 * @user_data: user data passed to @func
2742 * Call @func for each session managed by @client. The result value of @func
2743 * determines what happens to the session. @func will be called with @client
2744 * locked so no further actions on @client can be performed from @func.
2746 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
2749 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
2751 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
2752 * will also be added with an additional ref to the result #GList of this
2755 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
2756 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2757 * element in the #GList should be unreffed before the list is freed.
2760 gst_rtsp_client_session_filter (GstRTSPClient * client,
2761 GstRTSPClientSessionFilterFunc func, gpointer user_data)
2763 GstRTSPClientPrivate *priv;
2764 GList *result, *walk, *next;
2766 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2767 g_return_val_if_fail (func != NULL, NULL);
2769 priv = client->priv;
2773 g_mutex_lock (&priv->lock);
2774 for (walk = priv->sessions; walk; walk = next) {
2775 GstRTSPSession *sess = walk->data;
2777 next = g_list_next (walk);
2779 switch (func (client, sess, user_data)) {
2780 case GST_RTSP_FILTER_REMOVE:
2781 /* stop watching the session and pretent it went away */
2782 client_cleanup_session (client, sess);
2784 case GST_RTSP_FILTER_REF:
2785 result = g_list_prepend (result, g_object_ref (sess));
2787 case GST_RTSP_FILTER_KEEP:
2792 g_mutex_unlock (&priv->lock);