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 GstRTSPThread *thread;
520 /* remove any previously cached values before we try to construct a new
526 gst_rtsp_media_unprepare (priv->media);
527 g_object_unref (priv->media);
531 /* prepare the media and add it to the pipeline */
532 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
535 state->media = media;
537 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
538 GST_RTSP_THREAD_TYPE_MEDIA, state);
542 /* prepare the media */
543 if (!(gst_rtsp_media_prepare (media, thread)))
546 /* now keep track of the uri and the media */
547 priv->path = g_strndup (path, path_len);
550 /* we have seen this path before, used cached media */
552 state->media = media;
553 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
556 g_object_unref (factory);
557 state->factory = NULL;
560 g_object_ref (media);
567 GST_ERROR ("client %p: no mount points configured", client);
568 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
573 GST_ERROR ("client %p: no factory for uri %s", client, path);
574 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
579 GST_ERROR ("client %p: not authorized to see factory uri %s", client, path);
580 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
585 GST_ERROR ("client %p: not authorized for factory uri %s", client, path);
586 handle_unauthorized_request (client, priv->auth, state);
591 GST_ERROR ("client %p: can't create media", client);
592 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
593 g_object_unref (factory);
594 state->factory = NULL;
599 GST_ERROR ("client %p: can't create thread", client);
600 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
601 g_object_unref (media);
603 g_object_unref (factory);
604 state->factory = NULL;
609 GST_ERROR ("client %p: can't prepare media", client);
610 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
611 g_object_unref (media);
613 g_object_unref (factory);
614 state->factory = NULL;
620 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
622 GstRTSPClientPrivate *priv = client->priv;
623 GstRTSPMessage message = { 0 };
628 gst_rtsp_message_init_data (&message, channel);
630 /* FIXME, need some sort of iovec RTSPMessage here */
631 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
634 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
636 g_mutex_lock (&priv->send_lock);
638 priv->send_func (client, &message, FALSE, priv->send_data);
639 g_mutex_unlock (&priv->send_lock);
641 gst_rtsp_message_steal_body (&message, &data, &usize);
642 gst_buffer_unmap (buffer, &map_info);
644 gst_rtsp_message_unset (&message);
650 link_transport (GstRTSPClient * client, GstRTSPSession * session,
651 GstRTSPStreamTransport * trans)
653 GstRTSPClientPrivate *priv = client->priv;
655 GST_DEBUG ("client %p: linking transport %p", client, trans);
657 gst_rtsp_stream_transport_set_callbacks (trans,
658 (GstRTSPSendFunc) do_send_data,
659 (GstRTSPSendFunc) do_send_data, client, NULL);
661 priv->transports = g_list_prepend (priv->transports, trans);
663 /* make sure our session can't expire */
664 gst_rtsp_session_prevent_expire (session);
668 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
669 GstRTSPStreamTransport * trans)
671 GstRTSPClientPrivate *priv = client->priv;
673 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
675 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
677 priv->transports = g_list_remove (priv->transports, trans);
679 /* our session can now expire */
680 gst_rtsp_session_allow_expire (session);
684 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
685 GstRTSPSessionMedia * sessmedia)
690 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
691 for (i = 0; i < n_streams; i++) {
692 GstRTSPStreamTransport *trans;
693 const GstRTSPTransport *tr;
695 /* get the transport, if there is no transport configured, skip this stream */
696 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
700 tr = gst_rtsp_stream_transport_get_transport (trans);
702 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
703 /* for TCP, unlink the stream from the TCP connection of the client */
704 unlink_transport (client, session, trans);
710 close_connection (GstRTSPClient * client)
712 GstRTSPClientPrivate *priv = client->priv;
713 const gchar *tunnelid;
715 GST_DEBUG ("client %p: closing connection", client);
717 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
718 g_mutex_lock (&tunnels_lock);
719 /* remove from tunnelids */
720 g_hash_table_remove (tunnels, tunnelid);
721 g_mutex_unlock (&tunnels_lock);
724 gst_rtsp_connection_close (priv->connection);
728 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
730 GstRTSPClientPrivate *priv = client->priv;
731 GstRTSPSession *session;
732 GstRTSPSessionMedia *sessmedia;
733 GstRTSPStatusCode code;
740 session = state->session;
745 path = state->uri->abspath;
747 /* get a handle to the configuration of the media in the session */
748 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
752 /* only aggregate control for now.. */
753 if (path[matched] != '\0')
756 state->sessmedia = sessmedia;
758 /* we emit the signal before closing the connection */
759 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
762 /* unlink the all TCP callbacks */
763 unlink_session_transports (client, session, sessmedia);
765 /* remove the session from the watched sessions */
766 client_unwatch_session (client, session);
768 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
770 /* unmanage the media in the session, returns false if all media session
772 if (!gst_rtsp_session_release_media (session, sessmedia)) {
773 /* remove the session */
774 gst_rtsp_session_pool_remove (priv->session_pool, session);
776 /* construct the response now */
777 code = GST_RTSP_STS_OK;
778 gst_rtsp_message_init_response (state->response, code,
779 gst_rtsp_status_as_text (code), state->request);
781 send_message (client, session, state->response, TRUE);
788 GST_ERROR ("client %p: no session", client);
789 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
794 GST_ERROR ("client %p: no uri supplied", client);
795 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
800 GST_ERROR ("client %p: no media for uri", client);
801 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
806 GST_ERROR ("client %p: no aggregate path %s", client, path);
807 send_generic_response (client,
808 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
814 default_params_set (GstRTSPClient * client, GstRTSPClientState * state)
818 res = gst_rtsp_params_set (client, state);
824 default_params_get (GstRTSPClient * client, GstRTSPClientState * state)
828 res = gst_rtsp_params_get (client, state);
834 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
840 res = gst_rtsp_message_get_body (state->request, &data, &size);
841 if (res != GST_RTSP_OK)
845 /* no body, keep-alive request */
846 send_generic_response (client, GST_RTSP_STS_OK, state);
848 /* there is a body, handle the params */
849 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, state);
850 if (res != GST_RTSP_OK)
853 send_message (client, state->session, state->response, FALSE);
856 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
864 GST_ERROR ("client %p: bad request", client);
865 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
871 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
877 res = gst_rtsp_message_get_body (state->request, &data, &size);
878 if (res != GST_RTSP_OK)
882 /* no body, keep-alive request */
883 send_generic_response (client, GST_RTSP_STS_OK, state);
885 /* there is a body, handle the params */
886 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, state);
887 if (res != GST_RTSP_OK)
890 send_message (client, state->session, state->response, FALSE);
893 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
901 GST_ERROR ("client %p: bad request", client);
902 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
908 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
910 GstRTSPSession *session;
911 GstRTSPSessionMedia *sessmedia;
912 GstRTSPStatusCode code;
913 GstRTSPState rtspstate;
917 if (!(session = state->session))
923 path = state->uri->abspath;
925 /* get a handle to the configuration of the media in the session */
926 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
930 if (path[matched] != '\0')
933 state->sessmedia = sessmedia;
935 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
936 /* the session state must be playing or recording */
937 if (rtspstate != GST_RTSP_STATE_PLAYING &&
938 rtspstate != GST_RTSP_STATE_RECORDING)
941 /* unlink the all TCP callbacks */
942 unlink_session_transports (client, session, sessmedia);
944 /* then pause sending */
945 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
947 /* construct the response now */
948 code = GST_RTSP_STS_OK;
949 gst_rtsp_message_init_response (state->response, code,
950 gst_rtsp_status_as_text (code), state->request);
952 send_message (client, session, state->response, FALSE);
954 /* the state is now READY */
955 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
957 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
965 GST_ERROR ("client %p: no seesion", client);
966 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
971 GST_ERROR ("client %p: no uri supplied", client);
972 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
977 GST_ERROR ("client %p: no media for uri", client);
978 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
983 GST_ERROR ("client %p: no aggregate path %s", client, path);
984 send_generic_response (client,
985 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
990 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
991 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
998 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
1000 GstRTSPSession *session;
1001 GstRTSPSessionMedia *sessmedia;
1002 GstRTSPMedia *media;
1003 GstRTSPStatusCode code;
1005 guint n_streams, i, infocount;
1007 GstRTSPTimeRange *range;
1009 GstRTSPState rtspstate;
1010 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1014 if (!(session = state->session))
1020 path = state->uri->abspath;
1022 /* get a handle to the configuration of the media in the session */
1023 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1027 if (path[matched] != '\0')
1030 state->sessmedia = sessmedia;
1031 state->media = media = gst_rtsp_session_media_get_media (sessmedia);
1033 /* the session state must be playing or ready */
1034 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1035 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1038 /* parse the range header if we have one */
1040 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
1041 if (res == GST_RTSP_OK) {
1042 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1043 /* we have a range, seek to the position */
1045 gst_rtsp_media_seek (media, range);
1046 gst_rtsp_range_free (range);
1050 /* grab RTPInfo from the payloaders now */
1051 rtpinfo = g_string_new ("");
1053 n_streams = gst_rtsp_media_n_streams (media);
1054 for (i = 0, infocount = 0; i < n_streams; i++) {
1055 GstRTSPStreamTransport *trans;
1056 GstRTSPStream *stream;
1057 const GstRTSPTransport *tr;
1061 /* get the transport, if there is no transport configured, skip this stream */
1062 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
1063 if (trans == NULL) {
1064 GST_INFO ("stream %d is not configured", i);
1067 tr = gst_rtsp_stream_transport_get_transport (trans);
1069 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1070 /* for TCP, link the stream to the TCP connection of the client */
1071 link_transport (client, session, trans);
1074 stream = gst_rtsp_stream_transport_get_stream (trans);
1075 if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
1077 g_string_append (rtpinfo, ", ");
1079 uristr = gst_rtsp_url_get_request_uri (state->uri);
1080 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
1081 uristr, i, seq, rtptime);
1086 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
1090 /* construct the response now */
1091 code = GST_RTSP_STS_OK;
1092 gst_rtsp_message_init_response (state->response, code,
1093 gst_rtsp_status_as_text (code), state->request);
1095 /* add the RTP-Info header */
1096 if (infocount > 0) {
1097 str = g_string_free (rtpinfo, FALSE);
1098 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
1100 g_string_free (rtpinfo, TRUE);
1104 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1105 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
1107 send_message (client, session, state->response, FALSE);
1109 /* start playing after sending the request */
1110 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1112 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1114 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
1122 GST_ERROR ("client %p: no session", client);
1123 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1128 GST_ERROR ("client %p: no uri supplied", client);
1129 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1134 GST_ERROR ("client %p: media not found", client);
1135 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1140 GST_ERROR ("client %p: no aggregate path %s", client, path);
1141 send_generic_response (client,
1142 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, state);
1147 GST_ERROR ("client %p: not PLAYING or READY", client);
1148 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1155 do_keepalive (GstRTSPSession * session)
1157 GST_INFO ("keep session %p alive", session);
1158 gst_rtsp_session_touch (session);
1161 /* parse @transport and return a valid transport in @tr. only transports
1162 * from @supported are returned. Returns FALSE if no valid transport
1165 parse_transport (const char *transport, GstRTSPLowerTrans supported,
1166 GstRTSPTransport * tr)
1173 gst_rtsp_transport_init (tr);
1175 GST_DEBUG ("parsing transports %s", transport);
1177 transports = g_strsplit (transport, ",", 0);
1179 /* loop through the transports, try to parse */
1180 for (i = 0; transports[i]; i++) {
1181 res = gst_rtsp_transport_parse (transports[i], tr);
1182 if (res != GST_RTSP_OK) {
1183 /* no valid transport, search some more */
1184 GST_WARNING ("could not parse transport %s", transports[i]);
1188 /* we have a transport, see if it's RTP/AVP */
1189 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1190 GST_WARNING ("invalid transport %s", transports[i]);
1194 if (!(tr->lower_transport & supported)) {
1195 GST_WARNING ("unsupported transport %s", transports[i]);
1199 /* we have a valid transport */
1200 GST_INFO ("found valid transport %s", transports[i]);
1205 gst_rtsp_transport_init (tr);
1207 g_strfreev (transports);
1213 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
1214 GstRTSPMessage * request)
1216 gchar *blocksize_str;
1217 gboolean ret = TRUE;
1219 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1220 &blocksize_str, 0) == GST_RTSP_OK) {
1224 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1225 if (end == blocksize_str) {
1226 GST_ERROR ("failed to parse blocksize");
1229 /* we don't want to change the mtu when this media
1230 * can be shared because it impacts other clients */
1231 if (gst_rtsp_media_is_shared (media))
1234 if (blocksize > G_MAXUINT)
1235 blocksize = G_MAXUINT;
1236 gst_rtsp_stream_set_mtu (stream, blocksize);
1243 default_configure_client_transport (GstRTSPClient * client,
1244 GstRTSPClientState * state, GstRTSPTransport * ct)
1246 GstRTSPClientPrivate *priv = client->priv;
1248 /* we have a valid transport now, set the destination of the client. */
1249 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1250 if (ct->destination && priv->use_client_settings) {
1251 GstRTSPAddress *addr;
1253 addr = gst_rtsp_stream_reserve_address (state->stream, ct->destination,
1254 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1259 gst_rtsp_address_free (addr);
1261 GstRTSPAddress *addr;
1262 GSocketFamily family;
1264 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1266 addr = gst_rtsp_stream_get_multicast_address (state->stream, family);
1270 g_free (ct->destination);
1271 ct->destination = g_strdup (addr->address);
1272 ct->port.min = addr->port;
1273 ct->port.max = addr->port + addr->n_ports - 1;
1274 ct->ttl = addr->ttl;
1276 gst_rtsp_address_free (addr);
1281 url = gst_rtsp_connection_get_url (priv->connection);
1282 g_free (ct->destination);
1283 ct->destination = g_strdup (url->host);
1285 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1286 /* check if the client selected channels for TCP */
1287 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1288 gst_rtsp_session_media_alloc_channels (state->sessmedia,
1298 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1303 static GstRTSPTransport *
1304 make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
1305 GstRTSPTransport * ct)
1307 GstRTSPTransport *st;
1309 GSocketFamily family;
1311 /* prepare the server transport */
1312 gst_rtsp_transport_new (&st);
1314 st->trans = ct->trans;
1315 st->profile = ct->profile;
1316 st->lower_transport = ct->lower_transport;
1318 addr = g_inet_address_new_from_string (ct->destination);
1321 GST_ERROR ("failed to get inet addr from client destination");
1322 family = G_SOCKET_FAMILY_IPV4;
1324 family = g_inet_address_get_family (addr);
1325 g_object_unref (addr);
1329 switch (st->lower_transport) {
1330 case GST_RTSP_LOWER_TRANS_UDP:
1331 st->client_port = ct->client_port;
1332 gst_rtsp_stream_get_server_port (state->stream, &st->server_port, family);
1334 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1335 st->port = ct->port;
1336 st->destination = g_strdup (ct->destination);
1339 case GST_RTSP_LOWER_TRANS_TCP:
1340 st->interleaved = ct->interleaved;
1345 gst_rtsp_stream_get_ssrc (state->stream, &st->ssrc);
1351 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
1353 GstRTSPClientPrivate *priv = client->priv;
1357 GstRTSPTransport *ct, *st;
1358 GstRTSPLowerTrans supported;
1359 GstRTSPStatusCode code;
1360 GstRTSPSession *session;
1361 GstRTSPStreamTransport *trans;
1363 GstRTSPSessionMedia *sessmedia;
1364 GstRTSPMedia *media;
1365 GstRTSPStream *stream;
1366 GstRTSPState rtspstate;
1367 GstRTSPClientClass *klass;
1368 gchar *path, *control;
1375 path = uri->abspath;
1377 /* parse the transport */
1379 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
1381 if (res != GST_RTSP_OK)
1384 /* we create the session after parsing stuff so that we don't make
1385 * a session for malformed requests */
1386 if (priv->session_pool == NULL)
1389 session = state->session;
1392 g_object_ref (session);
1393 /* get a handle to the configuration of the media in the session, this can
1394 * return NULL if this is a new url to manage in this session. */
1395 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1397 /* we need a new media configuration in this session */
1401 /* we have no session media, find one and manage it */
1402 if (sessmedia == NULL) {
1403 /* get a handle to the configuration of the media in the session */
1404 media = find_media (client, state, &matched);
1406 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1407 g_object_ref (media);
1409 /* no media, not found then */
1411 goto media_not_found;
1413 /* path is what matched. We can modify the parsed uri in place */
1414 path[matched] = '\0';
1415 /* control is remainder */
1416 control = &path[matched + 1];
1418 /* find the stream now using the control part */
1419 stream = gst_rtsp_media_find_stream (media, control);
1421 goto stream_not_found;
1423 /* now we have a uri identifying a valid media and stream */
1424 state->stream = stream;
1425 state->media = media;
1427 if (session == NULL) {
1428 /* create a session if this fails we probably reached our session limit or
1430 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1431 goto service_unavailable;
1433 /* make sure this client is closed when the session is closed */
1434 client_watch_session (client, session);
1436 /* signal new session */
1437 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1440 state->session = session;
1443 if (sessmedia == NULL) {
1444 /* manage the media in our session now, if not done already */
1445 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1446 /* if we stil have no media, error */
1447 if (sessmedia == NULL)
1448 goto sessmedia_unavailable;
1450 g_object_unref (media);
1453 state->sessmedia = sessmedia;
1455 /* set blocksize on this stream */
1456 if (!handle_blocksize (media, stream, state->request))
1457 goto invalid_blocksize;
1459 gst_rtsp_transport_new (&ct);
1461 /* our supported transports */
1462 supported = GST_RTSP_LOWER_TRANS_UDP |
1463 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
1465 /* parse and find a usable supported transport */
1466 if (!parse_transport (transport, supported, ct))
1467 goto unsupported_transports;
1469 /* update the client transport */
1470 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1471 if (!klass->configure_client_transport (client, state, ct))
1472 goto unsupported_client_transport;
1474 /* set in the session media transport */
1475 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1477 /* configure keepalive for this transport */
1478 gst_rtsp_stream_transport_set_keepalive (trans,
1479 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1481 /* create and serialize the server transport */
1482 st = make_server_transport (client, state, ct);
1483 trans_str = gst_rtsp_transport_as_text (st);
1484 gst_rtsp_transport_free (st);
1486 /* construct the response now */
1487 code = GST_RTSP_STS_OK;
1488 gst_rtsp_message_init_response (state->response, code,
1489 gst_rtsp_status_as_text (code), state->request);
1491 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1495 send_message (client, session, state->response, FALSE);
1497 /* update the state */
1498 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1499 switch (rtspstate) {
1500 case GST_RTSP_STATE_PLAYING:
1501 case GST_RTSP_STATE_RECORDING:
1502 case GST_RTSP_STATE_READY:
1503 /* no state change */
1506 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1509 g_object_unref (session);
1511 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST],
1519 GST_ERROR ("client %p: no uri", client);
1520 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1525 GST_ERROR ("client %p: no transport", client);
1526 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1531 GST_ERROR ("client %p: no session pool configured", client);
1532 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1537 GST_ERROR ("client %p: media '%s' not found", client, path);
1538 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1543 GST_ERROR ("client %p: stream '%s' not found", client, control);
1544 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1545 g_object_unref (media);
1548 service_unavailable:
1550 GST_ERROR ("client %p: can't create session", client);
1551 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1552 g_object_unref (media);
1555 sessmedia_unavailable:
1557 GST_ERROR ("client %p: can't create session media", client);
1558 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1559 g_object_unref (media);
1560 g_object_unref (session);
1565 GST_ERROR ("client %p: invalid blocksize", client);
1566 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1567 g_object_unref (session);
1570 unsupported_transports:
1572 GST_ERROR ("client %p: unsupported transports", client);
1573 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1574 gst_rtsp_transport_free (ct);
1575 g_object_unref (session);
1578 unsupported_client_transport:
1580 GST_ERROR ("client %p: unsupported client transport", client);
1581 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1582 gst_rtsp_transport_free (ct);
1583 g_object_unref (session);
1588 static GstSDPMessage *
1589 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1591 GstRTSPClientPrivate *priv = client->priv;
1596 gst_sdp_message_new (&sdp);
1598 /* some standard things first */
1599 gst_sdp_message_set_version (sdp, "0");
1606 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1609 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1610 gst_sdp_message_set_information (sdp, "rtsp-server");
1611 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1612 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1613 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1614 gst_sdp_message_add_attribute (sdp, "control", "*");
1616 info.is_ipv6 = priv->is_ipv6;
1617 info.server_ip = priv->server_ip;
1619 /* create an SDP for the media object */
1620 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1628 GST_ERROR ("client %p: could not create SDP", client);
1629 gst_sdp_message_free (sdp);
1634 /* for the describe we must generate an SDP */
1636 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1641 gchar *str, *content_base;
1642 GstRTSPMedia *media;
1643 GstRTSPClientClass *klass;
1645 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1650 /* check what kind of format is accepted, we don't really do anything with it
1651 * and always return SDP for now. */
1656 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1658 if (res == GST_RTSP_ENOTIMPL)
1661 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1665 /* find the media object for the uri */
1666 if (!(media = find_media (client, state, NULL)))
1669 /* create an SDP for the media object on this client */
1670 if (!(sdp = klass->create_sdp (client, media)))
1673 g_object_unref (media);
1675 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1676 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1678 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1681 /* content base for some clients that might screw up creating the setup uri */
1682 str = gst_rtsp_url_get_request_uri (state->uri);
1683 str_len = strlen (str);
1685 /* check for trailing '/' and append one */
1686 if (str[str_len - 1] != '/') {
1687 content_base = g_malloc (str_len + 2);
1688 memcpy (content_base, str, str_len);
1689 content_base[str_len] = '/';
1690 content_base[str_len + 1] = '\0';
1696 GST_INFO ("adding content-base: %s", content_base);
1698 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1700 g_free (content_base);
1702 /* add SDP to the response body */
1703 str = gst_sdp_message_as_text (sdp);
1704 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1705 gst_sdp_message_free (sdp);
1707 send_message (client, state->session, state->response, FALSE);
1709 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1717 GST_ERROR ("client %p: no uri", client);
1718 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1723 GST_ERROR ("client %p: no media", client);
1724 /* error reply is already sent */
1729 GST_ERROR ("client %p: can't create SDP", client);
1730 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1731 g_object_unref (media);
1737 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1739 GstRTSPMethod options;
1742 options = GST_RTSP_DESCRIBE |
1747 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1749 str = gst_rtsp_options_as_text (options);
1751 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1752 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1754 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1757 send_message (client, state->session, state->response, FALSE);
1759 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1765 /* remove duplicate and trailing '/' */
1767 sanitize_uri (GstRTSPUrl * uri)
1771 gboolean have_slash, prev_slash;
1773 s = d = uri->abspath;
1774 len = strlen (uri->abspath);
1778 for (i = 0; i < len; i++) {
1779 have_slash = s[i] == '/';
1781 if (!have_slash || !prev_slash)
1783 prev_slash = have_slash;
1785 len = d - uri->abspath;
1786 /* don't remove the first slash if that's the only thing left */
1787 if (len > 1 && *(d - 1) == '/')
1793 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1795 GstRTSPClientPrivate *priv = client->priv;
1797 GST_INFO ("client %p: session %p finished", client, session);
1799 /* unlink all media managed in this session */
1800 client_unlink_session (client, session);
1802 /* remove the session */
1803 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1804 GST_INFO ("client %p: all sessions finalized, close the connection",
1806 close_connection (client);
1810 static GPrivate state_key;
1813 * gst_rtsp_client_state_get_current:
1815 * Get the current #GstRTSPClientState. This object is retrieved from the
1816 * current thread that is handling the request for a client.
1818 * Returns: a #GstRTSPClientState
1820 GstRTSPClientState *
1821 gst_rtsp_client_state_get_current (void)
1823 return g_private_get (&state_key);
1827 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1829 GstRTSPClientPrivate *priv = client->priv;
1830 GstRTSPMethod method;
1831 const gchar *uristr;
1832 GstRTSPUrl *uri = NULL;
1833 GstRTSPVersion version;
1835 GstRTSPSession *session = NULL;
1836 GstRTSPClientState state = { NULL };
1837 GstRTSPMessage response = { 0 };
1840 state.client = client;
1841 state.request = request;
1842 state.response = &response;
1843 state.auth = priv->auth;
1844 g_private_set (&state_key, &state);
1846 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1847 gst_rtsp_message_dump (request);
1850 GST_INFO ("client %p: received a request", client);
1852 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1854 /* we can only handle 1.0 requests */
1855 if (version != GST_RTSP_VERSION_1_0)
1858 state.method = method;
1860 /* we always try to parse the url first */
1861 if (strcmp (uristr, "*") == 0) {
1862 /* special case where we have * as uri, keep uri = NULL */
1863 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1866 /* get the session if there is any */
1867 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1868 if (res == GST_RTSP_OK) {
1869 if (priv->session_pool == NULL)
1872 /* we had a session in the request, find it again */
1873 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1874 goto session_not_found;
1876 /* we add the session to the client list of watched sessions. When a session
1877 * disappears because it times out, we will be notified. If all sessions are
1878 * gone, we will close the connection */
1879 client_watch_session (client, session);
1882 /* sanitize the uri */
1886 state.session = session;
1888 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
1889 goto not_authorized;
1891 /* now see what is asked and dispatch to a dedicated handler */
1893 case GST_RTSP_OPTIONS:
1894 handle_options_request (client, &state);
1896 case GST_RTSP_DESCRIBE:
1897 handle_describe_request (client, &state);
1899 case GST_RTSP_SETUP:
1900 handle_setup_request (client, &state);
1903 handle_play_request (client, &state);
1905 case GST_RTSP_PAUSE:
1906 handle_pause_request (client, &state);
1908 case GST_RTSP_TEARDOWN:
1909 handle_teardown_request (client, &state);
1911 case GST_RTSP_SET_PARAMETER:
1912 handle_set_param_request (client, &state);
1914 case GST_RTSP_GET_PARAMETER:
1915 handle_get_param_request (client, &state);
1917 case GST_RTSP_ANNOUNCE:
1918 case GST_RTSP_RECORD:
1919 case GST_RTSP_REDIRECT:
1920 goto not_implemented;
1921 case GST_RTSP_INVALID:
1927 g_private_set (&state_key, NULL);
1929 g_object_unref (session);
1931 gst_rtsp_url_free (uri);
1937 GST_ERROR ("client %p: version %d not supported", client, version);
1938 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1944 GST_ERROR ("client %p: bad request", client);
1945 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1950 GST_ERROR ("client %p: no pool configured", client);
1951 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1956 GST_ERROR ("client %p: session not found", client);
1957 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1962 GST_ERROR ("client %p: not allowed", client);
1963 handle_unauthorized_request (client, priv->auth, &state);
1968 GST_ERROR ("client %p: method %d not implemented", client, method);
1969 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1975 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1977 GstRTSPClientPrivate *priv = client->priv;
1986 /* find the stream for this message */
1987 res = gst_rtsp_message_parse_data (message, &channel);
1988 if (res != GST_RTSP_OK)
1991 gst_rtsp_message_steal_body (message, &data, &size);
1993 buffer = gst_buffer_new_wrapped (data, size);
1996 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1997 GstRTSPStreamTransport *trans;
1998 GstRTSPStream *stream;
1999 const GstRTSPTransport *tr;
2003 tr = gst_rtsp_stream_transport_get_transport (trans);
2004 stream = gst_rtsp_stream_transport_get_stream (trans);
2006 /* check for TCP transport */
2007 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2008 /* dispatch to the stream based on the channel number */
2009 if (tr->interleaved.min == channel) {
2010 gst_rtsp_stream_recv_rtp (stream, buffer);
2013 } else if (tr->interleaved.max == channel) {
2014 gst_rtsp_stream_recv_rtcp (stream, buffer);
2021 gst_buffer_unref (buffer);
2025 * gst_rtsp_client_set_session_pool:
2026 * @client: a #GstRTSPClient
2027 * @pool: a #GstRTSPSessionPool
2029 * Set @pool as the sessionpool for @client which it will use to find
2030 * or allocate sessions. the sessionpool is usually inherited from the server
2031 * that created the client but can be overridden later.
2034 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2035 GstRTSPSessionPool * pool)
2037 GstRTSPSessionPool *old;
2038 GstRTSPClientPrivate *priv;
2040 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2042 priv = client->priv;
2045 g_object_ref (pool);
2047 g_mutex_lock (&priv->lock);
2048 old = priv->session_pool;
2049 priv->session_pool = pool;
2050 g_mutex_unlock (&priv->lock);
2053 g_object_unref (old);
2057 * gst_rtsp_client_get_session_pool:
2058 * @client: a #GstRTSPClient
2060 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2062 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2064 GstRTSPSessionPool *
2065 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2067 GstRTSPClientPrivate *priv;
2068 GstRTSPSessionPool *result;
2070 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2072 priv = client->priv;
2074 g_mutex_lock (&priv->lock);
2075 if ((result = priv->session_pool))
2076 g_object_ref (result);
2077 g_mutex_unlock (&priv->lock);
2083 * gst_rtsp_client_set_mount_points:
2084 * @client: a #GstRTSPClient
2085 * @mounts: a #GstRTSPMountPoints
2087 * Set @mounts as the mount points for @client which it will use to map urls
2088 * to media streams. These mount points are usually inherited from the server that
2089 * created the client but can be overriden later.
2092 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2093 GstRTSPMountPoints * mounts)
2095 GstRTSPClientPrivate *priv;
2096 GstRTSPMountPoints *old;
2098 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2100 priv = client->priv;
2103 g_object_ref (mounts);
2105 g_mutex_lock (&priv->lock);
2106 old = priv->mount_points;
2107 priv->mount_points = mounts;
2108 g_mutex_unlock (&priv->lock);
2111 g_object_unref (old);
2115 * gst_rtsp_client_get_mount_points:
2116 * @client: a #GstRTSPClient
2118 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2120 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2122 GstRTSPMountPoints *
2123 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2125 GstRTSPClientPrivate *priv;
2126 GstRTSPMountPoints *result;
2128 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2130 priv = client->priv;
2132 g_mutex_lock (&priv->lock);
2133 if ((result = priv->mount_points))
2134 g_object_ref (result);
2135 g_mutex_unlock (&priv->lock);
2141 * gst_rtsp_client_set_use_client_settings:
2142 * @client: a #GstRTSPClient
2143 * @use_client_settings: whether to use client settings for multicast
2145 * Use client transport settings (destination and ttl) for multicast.
2146 * When @use_client_settings is %FALSE, the server settings will be
2150 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
2151 gboolean use_client_settings)
2153 GstRTSPClientPrivate *priv;
2155 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2157 priv = client->priv;
2159 g_mutex_lock (&priv->lock);
2160 priv->use_client_settings = use_client_settings;
2161 g_mutex_unlock (&priv->lock);
2165 * gst_rtsp_client_get_use_client_settings:
2166 * @client: a #GstRTSPClient
2168 * Check if client transport settings (destination and ttl) for multicast
2172 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
2174 GstRTSPClientPrivate *priv;
2177 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2179 priv = client->priv;
2181 g_mutex_lock (&priv->lock);
2182 res = priv->use_client_settings;
2183 g_mutex_unlock (&priv->lock);
2189 * gst_rtsp_client_set_auth:
2190 * @client: a #GstRTSPClient
2191 * @auth: a #GstRTSPAuth
2193 * configure @auth to be used as the authentication manager of @client.
2196 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2198 GstRTSPClientPrivate *priv;
2201 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2203 priv = client->priv;
2206 g_object_ref (auth);
2208 g_mutex_lock (&priv->lock);
2211 g_mutex_unlock (&priv->lock);
2214 g_object_unref (old);
2219 * gst_rtsp_client_get_auth:
2220 * @client: a #GstRTSPClient
2222 * Get the #GstRTSPAuth used as the authentication manager of @client.
2224 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2228 gst_rtsp_client_get_auth (GstRTSPClient * client)
2230 GstRTSPClientPrivate *priv;
2231 GstRTSPAuth *result;
2233 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2235 priv = client->priv;
2237 g_mutex_lock (&priv->lock);
2238 if ((result = priv->auth))
2239 g_object_ref (result);
2240 g_mutex_unlock (&priv->lock);
2246 * gst_rtsp_client_set_thread_pool:
2247 * @client: a #GstRTSPClient
2248 * @pool: a #GstRTSPThreadPool
2250 * configure @pool to be used as the thread pool of @client.
2253 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2254 GstRTSPThreadPool * pool)
2256 GstRTSPClientPrivate *priv;
2257 GstRTSPThreadPool *old;
2259 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2261 priv = client->priv;
2264 g_object_ref (pool);
2266 g_mutex_lock (&priv->lock);
2267 old = priv->thread_pool;
2268 priv->thread_pool = pool;
2269 g_mutex_unlock (&priv->lock);
2272 g_object_unref (old);
2276 * gst_rtsp_client_get_thread_pool:
2277 * @client: a #GstRTSPClient
2279 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2281 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2285 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2287 GstRTSPClientPrivate *priv;
2288 GstRTSPThreadPool *result;
2290 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2292 priv = client->priv;
2294 g_mutex_lock (&priv->lock);
2295 if ((result = priv->thread_pool))
2296 g_object_ref (result);
2297 g_mutex_unlock (&priv->lock);
2303 * gst_rtsp_client_set_connection:
2304 * @client: a #GstRTSPClient
2305 * @conn: (transfer full): a #GstRTSPConnection
2307 * Set the #GstRTSPConnection of @client. This function takes ownership of
2310 * Returns: %TRUE on success.
2313 gst_rtsp_client_set_connection (GstRTSPClient * client,
2314 GstRTSPConnection * conn)
2316 GstRTSPClientPrivate *priv;
2317 GSocket *read_socket;
2318 GSocketAddress *address;
2320 GError *error = NULL;
2322 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2323 g_return_val_if_fail (conn != NULL, FALSE);
2325 priv = client->priv;
2327 read_socket = gst_rtsp_connection_get_read_socket (conn);
2329 if (!(address = g_socket_get_local_address (read_socket, &error)))
2332 g_free (priv->server_ip);
2333 /* keep the original ip that the client connected to */
2334 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2335 GInetAddress *iaddr;
2337 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2339 /* socket might be ipv6 but adress still ipv4 */
2340 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2341 priv->server_ip = g_inet_address_to_string (iaddr);
2342 g_object_unref (address);
2344 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2345 priv->server_ip = g_strdup ("unknown");
2348 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2349 priv->server_ip, priv->is_ipv6);
2351 url = gst_rtsp_connection_get_url (conn);
2352 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2354 priv->connection = conn;
2361 GST_ERROR ("could not get local address %s", error->message);
2362 g_error_free (error);
2368 * gst_rtsp_client_get_connection:
2369 * @client: a #GstRTSPClient
2371 * Get the #GstRTSPConnection of @client.
2373 * Returns: (transfer none): the #GstRTSPConnection of @client.
2374 * The connection object returned remains valid until the client is freed.
2377 gst_rtsp_client_get_connection (GstRTSPClient * client)
2379 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2381 return client->priv->connection;
2385 * gst_rtsp_client_set_send_func:
2386 * @client: a #GstRTSPClient
2387 * @func: a #GstRTSPClientSendFunc
2388 * @user_data: user data passed to @func
2389 * @notify: called when @user_data is no longer in use
2391 * Set @func as the callback that will be called when a new message needs to be
2392 * sent to the client. @user_data is passed to @func and @notify is called when
2393 * @user_data is no longer in use.
2396 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2397 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2399 GstRTSPClientPrivate *priv;
2400 GDestroyNotify old_notify;
2403 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2405 priv = client->priv;
2407 g_mutex_lock (&priv->send_lock);
2408 priv->send_func = func;
2409 old_notify = priv->send_notify;
2410 old_data = priv->send_data;
2411 priv->send_notify = notify;
2412 priv->send_data = user_data;
2413 g_mutex_unlock (&priv->send_lock);
2416 old_notify (old_data);
2420 * gst_rtsp_client_handle_message:
2421 * @client: a #GstRTSPClient
2422 * @message: an #GstRTSPMessage
2424 * Let the client handle @message.
2426 * Returns: a #GstRTSPResult.
2429 gst_rtsp_client_handle_message (GstRTSPClient * client,
2430 GstRTSPMessage * message)
2432 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2433 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2435 switch (message->type) {
2436 case GST_RTSP_MESSAGE_REQUEST:
2437 handle_request (client, message);
2439 case GST_RTSP_MESSAGE_RESPONSE:
2441 case GST_RTSP_MESSAGE_DATA:
2442 handle_data (client, message);
2451 * gst_rtsp_client_send_request:
2452 * @client: a #GstRTSPClient
2453 * @session: a #GstRTSPSession to send the request to or %NULL
2454 * @request: The request #GstRTSPMessage to send
2456 * Send a request message to the client.
2459 gst_rtsp_client_send_request (GstRTSPClient * client, GstRTSPSession * session,
2460 GstRTSPMessage * request)
2462 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2463 g_return_val_if_fail (request != NULL, GST_RTSP_EINVAL);
2464 g_return_val_if_fail (request->type == GST_RTSP_MESSAGE_REQUEST,
2467 send_message (client, session, request, FALSE);
2472 static GstRTSPResult
2473 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2474 gboolean close, gpointer user_data)
2476 GstRTSPClientPrivate *priv = client->priv;
2478 /* send the response and store the seq number so we can wait until it's
2479 * written to the client to close the connection */
2480 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2481 &priv->close_seq : NULL);
2484 static GstRTSPResult
2485 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2488 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2491 static GstRTSPResult
2492 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2494 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2495 GstRTSPClientPrivate *priv = client->priv;
2497 if (priv->close_seq && priv->close_seq == cseq) {
2498 priv->close_seq = 0;
2499 close_connection (client);
2505 static GstRTSPResult
2506 closed (GstRTSPWatch * watch, gpointer user_data)
2508 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2509 GstRTSPClientPrivate *priv = client->priv;
2510 const gchar *tunnelid;
2512 GST_INFO ("client %p: connection closed", client);
2514 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2515 g_mutex_lock (&tunnels_lock);
2516 /* remove from tunnelids */
2517 g_hash_table_remove (tunnels, tunnelid);
2518 g_mutex_unlock (&tunnels_lock);
2521 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2526 static GstRTSPResult
2527 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2529 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2532 str = gst_rtsp_strresult (result);
2533 GST_INFO ("client %p: received an error %s", client, str);
2539 static GstRTSPResult
2540 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2541 GstRTSPMessage * message, guint id, gpointer user_data)
2543 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2546 str = gst_rtsp_strresult (result);
2548 ("client %p: error when handling message %p with id %d: %s",
2549 client, message, id, str);
2556 remember_tunnel (GstRTSPClient * client)
2558 GstRTSPClientPrivate *priv = client->priv;
2559 const gchar *tunnelid;
2561 /* store client in the pending tunnels */
2562 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2563 if (tunnelid == NULL)
2566 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2568 /* we can't have two clients connecting with the same tunnelid */
2569 g_mutex_lock (&tunnels_lock);
2570 if (g_hash_table_lookup (tunnels, tunnelid))
2571 goto tunnel_existed;
2573 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2574 g_mutex_unlock (&tunnels_lock);
2581 GST_ERROR ("client %p: no tunnelid provided", client);
2586 g_mutex_unlock (&tunnels_lock);
2587 GST_ERROR ("client %p: tunnel session %s already existed", client,
2593 static GstRTSPStatusCode
2594 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2596 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2597 GstRTSPClientPrivate *priv = client->priv;
2599 GST_INFO ("client %p: tunnel start (connection %p)", client,
2602 if (!remember_tunnel (client))
2605 return GST_RTSP_STS_OK;
2610 GST_ERROR ("client %p: error starting tunnel", client);
2611 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2615 static GstRTSPResult
2616 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2618 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2619 GstRTSPClientPrivate *priv = client->priv;
2621 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2624 /* ignore error, it'll only be a problem when the client does a POST again */
2625 remember_tunnel (client);
2630 static GstRTSPResult
2631 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2633 const gchar *tunnelid;
2634 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2635 GstRTSPClientPrivate *priv = client->priv;
2636 GstRTSPClient *oclient;
2637 GstRTSPClientPrivate *opriv;
2639 GST_INFO ("client %p: tunnel complete", client);
2641 /* find previous tunnel */
2642 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2643 if (tunnelid == NULL)
2646 g_mutex_lock (&tunnels_lock);
2647 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2650 /* remove the old client from the table. ref before because removing it will
2651 * remove the ref to it. */
2652 g_object_ref (oclient);
2653 g_hash_table_remove (tunnels, tunnelid);
2655 opriv = oclient->priv;
2657 if (opriv->watch == NULL)
2659 g_mutex_unlock (&tunnels_lock);
2661 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2662 opriv->connection, priv->connection);
2664 /* merge the tunnels into the first client */
2665 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2666 gst_rtsp_watch_reset (opriv->watch);
2667 g_object_unref (oclient);
2674 GST_ERROR ("client %p: no tunnelid provided", client);
2675 return GST_RTSP_ERROR;
2679 g_mutex_unlock (&tunnels_lock);
2680 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2681 return GST_RTSP_ERROR;
2685 g_mutex_unlock (&tunnels_lock);
2686 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2687 g_object_unref (oclient);
2688 return GST_RTSP_ERROR;
2692 static GstRTSPWatchFuncs watch_funcs = {
2704 client_watch_notify (GstRTSPClient * client)
2706 GstRTSPClientPrivate *priv = client->priv;
2708 GST_INFO ("client %p: watch destroyed", client);
2710 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2711 g_object_unref (client);
2715 * gst_rtsp_client_attach:
2716 * @client: a #GstRTSPClient
2717 * @context: (allow-none): a #GMainContext
2719 * Attaches @client to @context. When the mainloop for @context is run, the
2720 * client will be dispatched. When @context is NULL, the default context will be
2723 * This function should be called when the client properties and urls are fully
2724 * configured and the client is ready to start.
2726 * Returns: the ID (greater than 0) for the source within the GMainContext.
2729 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2731 GstRTSPClientPrivate *priv;
2734 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2735 priv = client->priv;
2736 g_return_val_if_fail (priv->watch == NULL, 0);
2738 /* create watch for the connection and attach */
2739 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2740 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2741 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2742 (GDestroyNotify) gst_rtsp_watch_unref);
2744 /* FIXME make this configurable. We don't want to do this yet because it will
2745 * be superceeded by a cache object later */
2746 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2748 GST_INFO ("attaching to context %p", context);
2749 res = gst_rtsp_watch_attach (priv->watch, context);
2755 * gst_rtsp_client_session_filter:
2756 * @client: a #GstRTSPclient
2757 * @func: (scope call): a callback
2758 * @user_data: user data passed to @func
2760 * Call @func for each session managed by @client. The result value of @func
2761 * determines what happens to the session. @func will be called with @client
2762 * locked so no further actions on @client can be performed from @func.
2764 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
2767 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
2769 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
2770 * will also be added with an additional ref to the result #GList of this
2773 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
2774 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2775 * element in the #GList should be unreffed before the list is freed.
2778 gst_rtsp_client_session_filter (GstRTSPClient * client,
2779 GstRTSPClientSessionFilterFunc func, gpointer user_data)
2781 GstRTSPClientPrivate *priv;
2782 GList *result, *walk, *next;
2784 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2785 g_return_val_if_fail (func != NULL, NULL);
2787 priv = client->priv;
2791 g_mutex_lock (&priv->lock);
2792 for (walk = priv->sessions; walk; walk = next) {
2793 GstRTSPSession *sess = walk->data;
2795 next = g_list_next (walk);
2797 switch (func (client, sess, user_data)) {
2798 case GST_RTSP_FILTER_REMOVE:
2799 /* stop watching the session and pretent it went away */
2800 client_cleanup_session (client, sess);
2802 case GST_RTSP_FILTER_REF:
2803 result = g_list_prepend (result, g_object_ref (sess));
2805 case GST_RTSP_FILTER_KEEP:
2810 g_mutex_unlock (&priv->lock);