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.
21 * @short_description: A client connection state
22 * @see_also: #GstRTSPServer, #GstRTSPThreadPool
24 * The client object handles the connection with a client for as long as a TCP
27 * A #GstRTSPClient is created by #GstRTSPServer when a new connection is
28 * accepted and it inherits the #GstRTSPMountPoints, #GstRTSPSessionPool,
29 * #GstRTSPAuth and #GstRTSPThreadPool from the server.
31 * The client connection should be configured with the #GstRTSPConnection using
32 * gst_rtsp_client_set_connection() before it can be attached to a #GMainContext
33 * using gst_rtsp_client_attach(). From then on the client will handle requests
36 * Use gst_rtsp_client_session_filter() to iterate or modify all the
37 * #GstRTSPSession objects managed by the client object.
39 * Last reviewed on 2013-07-11 (1.0.0)
45 #include "rtsp-client.h"
47 #include "rtsp-params.h"
49 #define GST_RTSP_CLIENT_GET_PRIVATE(obj) \
50 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_CLIENT, GstRTSPClientPrivate))
53 * send_lock, lock, tunnels_lock
56 struct _GstRTSPClientPrivate
58 GMutex lock; /* protects everything else */
60 GstRTSPConnection *connection;
66 GstRTSPClientSendFunc send_func; /* protected by send_lock */
67 gpointer send_data; /* protected by send_lock */
68 GDestroyNotify send_notify; /* protected by send_lock */
70 GstRTSPSessionPool *session_pool;
71 GstRTSPMountPoints *mount_points;
73 GstRTSPThreadPool *thread_pool;
75 /* used to cache the media in the last requested DESCRIBE so that
76 * we can pick it up in the next SETUP immediately */
84 static GMutex tunnels_lock;
85 static GHashTable *tunnels; /* protected by tunnels_lock */
87 #define DEFAULT_SESSION_POOL NULL
88 #define DEFAULT_MOUNT_POINTS NULL
102 SIGNAL_OPTIONS_REQUEST,
103 SIGNAL_DESCRIBE_REQUEST,
104 SIGNAL_SETUP_REQUEST,
106 SIGNAL_PAUSE_REQUEST,
107 SIGNAL_TEARDOWN_REQUEST,
108 SIGNAL_SET_PARAMETER_REQUEST,
109 SIGNAL_GET_PARAMETER_REQUEST,
110 SIGNAL_HANDLE_RESPONSE,
114 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
115 #define GST_CAT_DEFAULT rtsp_client_debug
117 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
119 static void gst_rtsp_client_get_property (GObject * object, guint propid,
120 GValue * value, GParamSpec * pspec);
121 static void gst_rtsp_client_set_property (GObject * object, guint propid,
122 const GValue * value, GParamSpec * pspec);
123 static void gst_rtsp_client_finalize (GObject * obj);
125 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
126 static void client_session_finalized (GstRTSPClient * client,
127 GstRTSPSession * session);
128 static void unlink_session_transports (GstRTSPClient * client,
129 GstRTSPSession * session, GstRTSPSessionMedia * sessmedia);
130 static gboolean default_configure_client_transport (GstRTSPClient * client,
131 GstRTSPContext * ctx, GstRTSPTransport * ct);
132 static GstRTSPResult default_params_set (GstRTSPClient * client,
133 GstRTSPContext * ctx);
134 static GstRTSPResult default_params_get (GstRTSPClient * client,
135 GstRTSPContext * ctx);
136 static gchar *default_make_path_from_uri (GstRTSPClient * client,
137 const GstRTSPUrl * uri);
139 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
142 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
144 GObjectClass *gobject_class;
146 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
148 gobject_class = G_OBJECT_CLASS (klass);
150 gobject_class->get_property = gst_rtsp_client_get_property;
151 gobject_class->set_property = gst_rtsp_client_set_property;
152 gobject_class->finalize = gst_rtsp_client_finalize;
154 klass->create_sdp = create_sdp;
155 klass->configure_client_transport = default_configure_client_transport;
156 klass->params_set = default_params_set;
157 klass->params_get = default_params_get;
158 klass->make_path_from_uri = default_make_path_from_uri;
160 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
161 g_param_spec_object ("session-pool", "Session Pool",
162 "The session pool to use for client session",
163 GST_TYPE_RTSP_SESSION_POOL,
164 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
166 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
167 g_param_spec_object ("mount-points", "Mount Points",
168 "The mount points to use for client session",
169 GST_TYPE_RTSP_MOUNT_POINTS,
170 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
172 gst_rtsp_client_signals[SIGNAL_CLOSED] =
173 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
174 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
175 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
177 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
178 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
179 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
180 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
182 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
183 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
184 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
185 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
188 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
189 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
190 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
191 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
194 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
195 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
196 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
197 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
200 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
201 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
202 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
203 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
206 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
207 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
208 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
209 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
212 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
213 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
214 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
215 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
218 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
219 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
220 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
221 set_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
222 G_TYPE_NONE, 1, G_TYPE_POINTER);
224 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
225 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
226 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
227 get_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
228 G_TYPE_NONE, 1, G_TYPE_POINTER);
230 gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE] =
231 g_signal_new ("handle-response", G_TYPE_FROM_CLASS (klass),
232 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
233 handle_response), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
234 G_TYPE_NONE, 1, G_TYPE_POINTER);
237 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
238 g_mutex_init (&tunnels_lock);
240 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
244 gst_rtsp_client_init (GstRTSPClient * client)
246 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
250 g_mutex_init (&priv->lock);
251 g_mutex_init (&priv->send_lock);
255 static GstRTSPFilterResult
256 filter_session (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
259 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
261 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
262 unlink_session_transports (client, sess, sessmedia);
264 /* unmanage the media in the session */
265 return GST_RTSP_FILTER_REMOVE;
269 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
271 /* unlink all media managed in this session */
272 gst_rtsp_session_filter (session, filter_session, client);
276 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
278 GstRTSPClientPrivate *priv = client->priv;
281 for (walk = priv->sessions; walk; walk = g_list_next (walk)) {
282 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
284 /* we already know about this session */
285 if (msession == session)
289 GST_INFO ("watching session %p", session);
291 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
293 priv->sessions = g_list_prepend (priv->sessions, session);
297 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session)
299 GstRTSPClientPrivate *priv = client->priv;
301 GST_INFO ("unwatching session %p", session);
303 g_object_weak_unref (G_OBJECT (session),
304 (GWeakNotify) client_session_finalized, client);
305 priv->sessions = g_list_remove (priv->sessions, session);
309 client_cleanup_session (GstRTSPClient * client, GstRTSPSession * session)
311 g_object_weak_unref (G_OBJECT (session),
312 (GWeakNotify) client_session_finalized, client);
313 client_unlink_session (client, session);
317 client_cleanup_sessions (GstRTSPClient * client)
319 GstRTSPClientPrivate *priv = client->priv;
322 /* remove weak-ref from sessions */
323 for (sessions = priv->sessions; sessions; sessions = g_list_next (sessions)) {
324 client_cleanup_session (client, (GstRTSPSession *) sessions->data);
326 g_list_free (priv->sessions);
327 priv->sessions = NULL;
330 /* A client is finalized when the connection is broken */
332 gst_rtsp_client_finalize (GObject * obj)
334 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
335 GstRTSPClientPrivate *priv = client->priv;
337 GST_INFO ("finalize client %p", client);
339 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
342 g_source_destroy ((GSource *) priv->watch);
344 client_cleanup_sessions (client);
346 if (priv->connection)
347 gst_rtsp_connection_free (priv->connection);
348 if (priv->session_pool)
349 g_object_unref (priv->session_pool);
350 if (priv->mount_points)
351 g_object_unref (priv->mount_points);
353 g_object_unref (priv->auth);
354 if (priv->thread_pool)
355 g_object_unref (priv->thread_pool);
360 gst_rtsp_media_unprepare (priv->media);
361 g_object_unref (priv->media);
364 g_free (priv->server_ip);
365 g_mutex_clear (&priv->lock);
366 g_mutex_clear (&priv->send_lock);
368 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
372 gst_rtsp_client_get_property (GObject * object, guint propid,
373 GValue * value, GParamSpec * pspec)
375 GstRTSPClient *client = GST_RTSP_CLIENT (object);
378 case PROP_SESSION_POOL:
379 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
381 case PROP_MOUNT_POINTS:
382 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
385 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
390 gst_rtsp_client_set_property (GObject * object, guint propid,
391 const GValue * value, GParamSpec * pspec)
393 GstRTSPClient *client = GST_RTSP_CLIENT (object);
396 case PROP_SESSION_POOL:
397 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
399 case PROP_MOUNT_POINTS:
400 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
403 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
408 * gst_rtsp_client_new:
410 * Create a new #GstRTSPClient instance.
412 * Returns: a new #GstRTSPClient
415 gst_rtsp_client_new (void)
417 GstRTSPClient *result;
419 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
425 send_message (GstRTSPClient * client, GstRTSPSession * session,
426 GstRTSPMessage * message, gboolean close)
428 GstRTSPClientPrivate *priv = client->priv;
430 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
431 "GStreamer RTSP server");
433 /* remove any previous header */
434 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
436 /* add the new session header for new session ids */
438 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
439 gst_rtsp_session_get_header (session));
442 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
443 gst_rtsp_message_dump (message);
447 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
449 g_mutex_lock (&priv->send_lock);
451 priv->send_func (client, message, close, priv->send_data);
452 g_mutex_unlock (&priv->send_lock);
454 gst_rtsp_message_unset (message);
458 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
459 GstRTSPContext * ctx)
461 gst_rtsp_message_init_response (ctx->response, code,
462 gst_rtsp_status_as_text (code), ctx->request);
464 send_message (client, NULL, ctx->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, GstRTSPContext * ctx, gchar * path,
489 GstRTSPClientPrivate *priv = client->priv;
490 GstRTSPMediaFactory *factory;
494 /* find the longest matching factory for the uri first */
495 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
499 ctx->factory = factory;
501 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
502 goto no_factory_access;
504 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
510 path_len = strlen (path);
512 if (!paths_are_equal (priv->path, path, path_len)) {
513 GstRTSPThread *thread;
515 /* remove any previously cached values before we try to construct a new
521 gst_rtsp_media_unprepare (priv->media);
522 g_object_unref (priv->media);
526 /* prepare the media and add it to the pipeline */
527 if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
532 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
533 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
537 /* prepare the media */
538 if (!(gst_rtsp_media_prepare (media, thread)))
541 /* now keep track of the uri and the media */
542 priv->path = g_strndup (path, path_len);
545 /* we have seen this path before, used cached media */
548 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
551 g_object_unref (factory);
555 g_object_ref (media);
562 GST_ERROR ("client %p: no factory for path %s", client, path);
563 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
568 GST_ERROR ("client %p: not authorized to see factory path %s", client,
570 /* error reply is already sent */
575 GST_ERROR ("client %p: not authorized for factory path %s", client, path);
576 /* error reply is already sent */
581 GST_ERROR ("client %p: can't create media", client);
582 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
583 g_object_unref (factory);
589 GST_ERROR ("client %p: can't create thread", client);
590 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
591 g_object_unref (media);
593 g_object_unref (factory);
599 GST_ERROR ("client %p: can't prepare media", client);
600 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
601 g_object_unref (media);
603 g_object_unref (factory);
610 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
612 GstRTSPClientPrivate *priv = client->priv;
613 GstRTSPMessage message = { 0 };
618 gst_rtsp_message_init_data (&message, channel);
620 /* FIXME, need some sort of iovec RTSPMessage here */
621 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
624 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
626 g_mutex_lock (&priv->send_lock);
628 priv->send_func (client, &message, FALSE, priv->send_data);
629 g_mutex_unlock (&priv->send_lock);
631 gst_rtsp_message_steal_body (&message, &data, &usize);
632 gst_buffer_unmap (buffer, &map_info);
634 gst_rtsp_message_unset (&message);
640 link_transport (GstRTSPClient * client, GstRTSPSession * session,
641 GstRTSPStreamTransport * trans)
643 GstRTSPClientPrivate *priv = client->priv;
645 GST_DEBUG ("client %p: linking transport %p", client, trans);
647 gst_rtsp_stream_transport_set_callbacks (trans,
648 (GstRTSPSendFunc) do_send_data,
649 (GstRTSPSendFunc) do_send_data, client, NULL);
651 priv->transports = g_list_prepend (priv->transports, trans);
653 /* make sure our session can't expire */
654 gst_rtsp_session_prevent_expire (session);
658 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
659 GstRTSPStreamTransport * trans)
661 GstRTSPClientPrivate *priv = client->priv;
663 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
665 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
667 priv->transports = g_list_remove (priv->transports, trans);
669 /* our session can now expire */
670 gst_rtsp_session_allow_expire (session);
674 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
675 GstRTSPSessionMedia * sessmedia)
680 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
681 for (i = 0; i < n_streams; i++) {
682 GstRTSPStreamTransport *trans;
683 const GstRTSPTransport *tr;
685 /* get the transport, if there is no transport configured, skip this stream */
686 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
690 tr = gst_rtsp_stream_transport_get_transport (trans);
692 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
693 /* for TCP, unlink the stream from the TCP connection of the client */
694 unlink_transport (client, session, trans);
700 close_connection (GstRTSPClient * client)
702 GstRTSPClientPrivate *priv = client->priv;
703 const gchar *tunnelid;
705 GST_DEBUG ("client %p: closing connection", client);
707 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
708 g_mutex_lock (&tunnels_lock);
709 /* remove from tunnelids */
710 g_hash_table_remove (tunnels, tunnelid);
711 g_mutex_unlock (&tunnels_lock);
714 gst_rtsp_connection_close (priv->connection);
718 default_make_path_from_uri (GstRTSPClient * client, const GstRTSPUrl * uri)
723 path = g_strconcat (uri->abspath, "?", uri->query, NULL);
725 path = g_strdup (uri->abspath);
731 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
733 GstRTSPClientPrivate *priv = client->priv;
734 GstRTSPClientClass *klass;
735 GstRTSPSession *session;
736 GstRTSPSessionMedia *sessmedia;
737 GstRTSPStatusCode code;
744 session = ctx->session;
749 klass = GST_RTSP_CLIENT_GET_CLASS (client);
750 path = klass->make_path_from_uri (client, ctx->uri);
752 /* get a handle to the configuration of the media in the session */
753 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
757 /* only aggregate control for now.. */
758 if (path[matched] != '\0')
763 ctx->sessmedia = sessmedia;
765 /* we emit the signal before closing the connection */
766 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
769 /* unlink the all TCP callbacks */
770 unlink_session_transports (client, session, sessmedia);
772 /* remove the session from the watched sessions */
773 client_unwatch_session (client, session);
775 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
777 /* unmanage the media in the session, returns false if all media session
779 if (!gst_rtsp_session_release_media (session, sessmedia)) {
780 /* remove the session */
781 gst_rtsp_session_pool_remove (priv->session_pool, session);
783 /* construct the response now */
784 code = GST_RTSP_STS_OK;
785 gst_rtsp_message_init_response (ctx->response, code,
786 gst_rtsp_status_as_text (code), ctx->request);
788 send_message (client, session, ctx->response, TRUE);
795 GST_ERROR ("client %p: no session", client);
796 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
801 GST_ERROR ("client %p: no uri supplied", client);
802 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
807 GST_ERROR ("client %p: no media for uri", client);
808 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
814 GST_ERROR ("client %p: no aggregate path %s", client, path);
815 send_generic_response (client,
816 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
823 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
827 res = gst_rtsp_params_set (client, ctx);
833 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
837 res = gst_rtsp_params_get (client, ctx);
843 handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
849 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
850 if (res != GST_RTSP_OK)
854 /* no body, keep-alive request */
855 send_generic_response (client, GST_RTSP_STS_OK, ctx);
857 /* there is a body, handle the params */
858 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
859 if (res != GST_RTSP_OK)
862 send_message (client, ctx->session, ctx->response, FALSE);
865 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
873 GST_ERROR ("client %p: bad request", client);
874 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
880 handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
886 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
887 if (res != GST_RTSP_OK)
891 /* no body, keep-alive request */
892 send_generic_response (client, GST_RTSP_STS_OK, ctx);
894 /* there is a body, handle the params */
895 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
896 if (res != GST_RTSP_OK)
899 send_message (client, ctx->session, ctx->response, FALSE);
902 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
910 GST_ERROR ("client %p: bad request", client);
911 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
917 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
919 GstRTSPSession *session;
920 GstRTSPClientClass *klass;
921 GstRTSPSessionMedia *sessmedia;
922 GstRTSPStatusCode code;
923 GstRTSPState rtspstate;
927 if (!(session = ctx->session))
933 klass = GST_RTSP_CLIENT_GET_CLASS (client);
934 path = klass->make_path_from_uri (client, ctx->uri);
936 /* get a handle to the configuration of the media in the session */
937 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
941 if (path[matched] != '\0')
946 ctx->sessmedia = sessmedia;
948 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
949 /* the session state must be playing or recording */
950 if (rtspstate != GST_RTSP_STATE_PLAYING &&
951 rtspstate != GST_RTSP_STATE_RECORDING)
954 /* unlink the all TCP callbacks */
955 unlink_session_transports (client, session, sessmedia);
957 /* then pause sending */
958 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
960 /* construct the response now */
961 code = GST_RTSP_STS_OK;
962 gst_rtsp_message_init_response (ctx->response, code,
963 gst_rtsp_status_as_text (code), ctx->request);
965 send_message (client, session, ctx->response, FALSE);
967 /* the state is now READY */
968 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
970 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
977 GST_ERROR ("client %p: no seesion", client);
978 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
983 GST_ERROR ("client %p: no uri supplied", client);
984 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
989 GST_ERROR ("client %p: no media for uri", client);
990 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
996 GST_ERROR ("client %p: no aggregate path %s", client, path);
997 send_generic_response (client,
998 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1004 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
1005 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1011 /* convert @url and @path to a URL used as a content base for the factory
1012 * located at @path */
1014 make_base_url (GstRTSPClient * client, GstRTSPUrl * url, gchar * path)
1017 gchar *result, *trail;
1019 /* check for trailing '/' and append one */
1020 trail = (path[strlen (path) - 1] != '/' ? "/" : "");
1025 tmp.abspath = g_strdup_printf ("%s%s", path, trail);
1027 result = gst_rtsp_url_get_request_uri (&tmp);
1028 g_free (tmp.abspath);
1034 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
1036 GstRTSPSession *session;
1037 GstRTSPClientClass *klass;
1038 GstRTSPSessionMedia *sessmedia;
1039 GstRTSPMedia *media;
1040 GstRTSPStatusCode code;
1043 guint n_streams, i, infocount;
1044 gchar *str, *base_url;
1045 GstRTSPTimeRange *range;
1047 GstRTSPState rtspstate;
1048 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1052 if (!(session = ctx->session))
1055 if (!(uri = ctx->uri))
1058 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1059 path = klass->make_path_from_uri (client, uri);
1061 /* get a handle to the configuration of the media in the session */
1062 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1066 if (path[matched] != '\0')
1069 ctx->sessmedia = sessmedia;
1070 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1072 /* the session state must be playing or ready */
1073 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1074 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1077 /* in play we first unsuspend, media could be suspended from SDP or PAUSED */
1078 if (!gst_rtsp_media_unsuspend (media))
1079 goto unsuspend_failed;
1081 /* parse the range header if we have one */
1082 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1083 if (res == GST_RTSP_OK) {
1084 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1085 /* we have a range, seek to the position */
1087 gst_rtsp_media_seek (media, range);
1088 gst_rtsp_range_free (range);
1092 /* grab RTPInfo from the payloaders now */
1093 rtpinfo = g_string_new ("");
1095 base_url = make_base_url (client, uri, path);
1097 n_streams = gst_rtsp_media_n_streams (media);
1098 for (i = 0, infocount = 0; i < n_streams; i++) {
1099 GstRTSPStreamTransport *trans;
1100 GstRTSPStream *stream;
1101 const GstRTSPTransport *tr;
1104 /* get the transport, if there is no transport configured, skip this stream */
1105 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
1106 if (trans == NULL) {
1107 GST_INFO ("stream %d is not configured", i);
1110 tr = gst_rtsp_stream_transport_get_transport (trans);
1112 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1113 /* for TCP, link the stream to the TCP connection of the client */
1114 link_transport (client, session, trans);
1117 stream = gst_rtsp_stream_transport_get_stream (trans);
1118 if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
1122 g_string_append (rtpinfo, ", ");
1124 control = gst_rtsp_stream_get_control (stream);
1125 g_string_append_printf (rtpinfo, "url=%s%s;seq=%u;rtptime=%u",
1126 base_url, control, seq, rtptime);
1131 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
1137 /* construct the response now */
1138 code = GST_RTSP_STS_OK;
1139 gst_rtsp_message_init_response (ctx->response, code,
1140 gst_rtsp_status_as_text (code), ctx->request);
1142 /* add the RTP-Info header */
1143 if (infocount > 0) {
1144 str = g_string_free (rtpinfo, FALSE);
1145 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO, str);
1147 g_string_free (rtpinfo, TRUE);
1151 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1153 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1155 send_message (client, session, ctx->response, FALSE);
1157 /* start playing after sending the request */
1158 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1160 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1162 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1169 GST_ERROR ("client %p: no session", client);
1170 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1175 GST_ERROR ("client %p: no uri supplied", client);
1176 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1181 GST_ERROR ("client %p: media not found", client);
1182 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1187 GST_ERROR ("client %p: no aggregate path %s", client, path);
1188 send_generic_response (client,
1189 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1195 GST_ERROR ("client %p: not PLAYING or READY", client);
1196 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1203 GST_ERROR ("client %p: unsuspend failed", client);
1204 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1211 do_keepalive (GstRTSPSession * session)
1213 GST_INFO ("keep session %p alive", session);
1214 gst_rtsp_session_touch (session);
1217 /* parse @transport and return a valid transport in @tr. only transports
1218 * from @supported are returned. Returns FALSE if no valid transport
1221 parse_transport (const char *transport, GstRTSPLowerTrans supported,
1222 GstRTSPTransport * tr)
1229 gst_rtsp_transport_init (tr);
1231 GST_DEBUG ("parsing transports %s", transport);
1233 transports = g_strsplit (transport, ",", 0);
1235 /* loop through the transports, try to parse */
1236 for (i = 0; transports[i]; i++) {
1237 res = gst_rtsp_transport_parse (transports[i], tr);
1238 if (res != GST_RTSP_OK) {
1239 /* no valid transport, search some more */
1240 GST_WARNING ("could not parse transport %s", transports[i]);
1244 /* we have a transport, see if it's RTP/AVP */
1245 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1246 GST_WARNING ("invalid transport %s", transports[i]);
1250 if (!(tr->lower_transport & supported)) {
1251 GST_WARNING ("unsupported transport %s", transports[i]);
1255 /* we have a valid transport */
1256 GST_INFO ("found valid transport %s", transports[i]);
1261 gst_rtsp_transport_init (tr);
1263 g_strfreev (transports);
1269 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
1270 GstRTSPMessage * request)
1272 gchar *blocksize_str;
1273 gboolean ret = TRUE;
1275 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1276 &blocksize_str, 0) == GST_RTSP_OK) {
1280 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1281 if (end == blocksize_str) {
1282 GST_ERROR ("failed to parse blocksize");
1285 /* we don't want to change the mtu when this media
1286 * can be shared because it impacts other clients */
1287 if (gst_rtsp_media_is_shared (media))
1290 if (blocksize > G_MAXUINT)
1291 blocksize = G_MAXUINT;
1292 gst_rtsp_stream_set_mtu (stream, blocksize);
1299 default_configure_client_transport (GstRTSPClient * client,
1300 GstRTSPContext * ctx, GstRTSPTransport * ct)
1302 GstRTSPClientPrivate *priv = client->priv;
1304 /* we have a valid transport now, set the destination of the client. */
1305 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1306 gboolean use_client_settings;
1308 use_client_settings =
1309 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1311 if (ct->destination && use_client_settings) {
1312 GstRTSPAddress *addr;
1314 addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1315 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1320 gst_rtsp_address_free (addr);
1322 GstRTSPAddress *addr;
1323 GSocketFamily family;
1325 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1327 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1331 g_free (ct->destination);
1332 ct->destination = g_strdup (addr->address);
1333 ct->port.min = addr->port;
1334 ct->port.max = addr->port + addr->n_ports - 1;
1335 ct->ttl = addr->ttl;
1337 gst_rtsp_address_free (addr);
1342 url = gst_rtsp_connection_get_url (priv->connection);
1343 g_free (ct->destination);
1344 ct->destination = g_strdup (url->host);
1346 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1347 /* check if the client selected channels for TCP */
1348 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1349 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1359 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1364 static GstRTSPTransport *
1365 make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
1366 GstRTSPTransport * ct)
1368 GstRTSPTransport *st;
1370 GSocketFamily family;
1372 /* prepare the server transport */
1373 gst_rtsp_transport_new (&st);
1375 st->trans = ct->trans;
1376 st->profile = ct->profile;
1377 st->lower_transport = ct->lower_transport;
1379 addr = g_inet_address_new_from_string (ct->destination);
1382 GST_ERROR ("failed to get inet addr from client destination");
1383 family = G_SOCKET_FAMILY_IPV4;
1385 family = g_inet_address_get_family (addr);
1386 g_object_unref (addr);
1390 switch (st->lower_transport) {
1391 case GST_RTSP_LOWER_TRANS_UDP:
1392 st->client_port = ct->client_port;
1393 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
1395 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1396 st->port = ct->port;
1397 st->destination = g_strdup (ct->destination);
1400 case GST_RTSP_LOWER_TRANS_TCP:
1401 st->interleaved = ct->interleaved;
1406 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
1412 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
1414 GstRTSPClientPrivate *priv = client->priv;
1418 GstRTSPTransport *ct, *st;
1419 GstRTSPLowerTrans supported;
1420 GstRTSPStatusCode code;
1421 GstRTSPSession *session;
1422 GstRTSPStreamTransport *trans;
1424 GstRTSPSessionMedia *sessmedia;
1425 GstRTSPMedia *media;
1426 GstRTSPStream *stream;
1427 GstRTSPState rtspstate;
1428 GstRTSPClientClass *klass;
1429 gchar *path, *control;
1436 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1437 path = klass->make_path_from_uri (client, uri);
1439 /* parse the transport */
1441 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
1443 if (res != GST_RTSP_OK)
1446 /* we create the session after parsing stuff so that we don't make
1447 * a session for malformed requests */
1448 if (priv->session_pool == NULL)
1451 session = ctx->session;
1454 g_object_ref (session);
1455 /* get a handle to the configuration of the media in the session, this can
1456 * return NULL if this is a new url to manage in this session. */
1457 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1459 /* we need a new media configuration in this session */
1463 /* we have no session media, find one and manage it */
1464 if (sessmedia == NULL) {
1465 /* get a handle to the configuration of the media in the session */
1466 media = find_media (client, ctx, path, &matched);
1468 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1469 g_object_ref (media);
1471 goto media_not_found;
1473 /* no media, not found then */
1475 goto media_not_found_no_reply;
1477 if (path[matched] == '\0')
1478 goto control_not_found;
1480 /* path is what matched. We can modify the parsed uri in place */
1481 path[matched] = '\0';
1482 /* control is remainder */
1483 control = &path[matched + 1];
1485 /* find the stream now using the control part */
1486 stream = gst_rtsp_media_find_stream (media, control);
1488 goto stream_not_found;
1490 /* now we have a uri identifying a valid media and stream */
1491 ctx->stream = stream;
1494 if (session == NULL) {
1495 /* create a session if this fails we probably reached our session limit or
1497 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1498 goto service_unavailable;
1500 /* make sure this client is closed when the session is closed */
1501 client_watch_session (client, session);
1503 /* signal new session */
1504 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1507 ctx->session = session;
1510 if (sessmedia == NULL) {
1511 /* manage the media in our session now, if not done already */
1512 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1513 /* if we stil have no media, error */
1514 if (sessmedia == NULL)
1515 goto sessmedia_unavailable;
1517 g_object_unref (media);
1520 ctx->sessmedia = sessmedia;
1522 /* set blocksize on this stream */
1523 if (!handle_blocksize (media, stream, ctx->request))
1524 goto invalid_blocksize;
1526 gst_rtsp_transport_new (&ct);
1528 /* our supported transports */
1529 supported = gst_rtsp_stream_get_protocols (stream);
1531 /* parse and find a usable supported transport */
1532 if (!parse_transport (transport, supported, ct))
1533 goto unsupported_transports;
1535 /* update the client transport */
1536 if (!klass->configure_client_transport (client, ctx, ct))
1537 goto unsupported_client_transport;
1539 /* set in the session media transport */
1540 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1542 /* configure keepalive for this transport */
1543 gst_rtsp_stream_transport_set_keepalive (trans,
1544 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1546 /* create and serialize the server transport */
1547 st = make_server_transport (client, ctx, ct);
1548 trans_str = gst_rtsp_transport_as_text (st);
1549 gst_rtsp_transport_free (st);
1551 /* construct the response now */
1552 code = GST_RTSP_STS_OK;
1553 gst_rtsp_message_init_response (ctx->response, code,
1554 gst_rtsp_status_as_text (code), ctx->request);
1556 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
1560 send_message (client, session, ctx->response, FALSE);
1562 /* update the state */
1563 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1564 switch (rtspstate) {
1565 case GST_RTSP_STATE_PLAYING:
1566 case GST_RTSP_STATE_RECORDING:
1567 case GST_RTSP_STATE_READY:
1568 /* no state change */
1571 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1574 g_object_unref (session);
1577 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
1584 GST_ERROR ("client %p: no uri", client);
1585 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1590 GST_ERROR ("client %p: no transport", client);
1591 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1597 GST_ERROR ("client %p: no session pool configured", client);
1598 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1602 media_not_found_no_reply:
1604 GST_ERROR ("client %p: media '%s' not found", client, path);
1606 /* error reply is already sent */
1611 GST_ERROR ("client %p: media '%s' not found", client, path);
1612 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1618 GST_ERROR ("client %p: no control in path '%s'", client, path);
1619 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1620 g_object_unref (media);
1626 GST_ERROR ("client %p: stream '%s' not found", client, control);
1627 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1628 g_object_unref (media);
1632 service_unavailable:
1634 GST_ERROR ("client %p: can't create session", client);
1635 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1636 g_object_unref (media);
1640 sessmedia_unavailable:
1642 GST_ERROR ("client %p: can't create session media", client);
1643 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1644 g_object_unref (media);
1645 g_object_unref (session);
1651 GST_ERROR ("client %p: invalid blocksize", client);
1652 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1653 g_object_unref (session);
1657 unsupported_transports:
1659 GST_ERROR ("client %p: unsupported transports", client);
1660 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1661 gst_rtsp_transport_free (ct);
1662 g_object_unref (session);
1666 unsupported_client_transport:
1668 GST_ERROR ("client %p: unsupported client transport", client);
1669 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1670 gst_rtsp_transport_free (ct);
1671 g_object_unref (session);
1677 static GstSDPMessage *
1678 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1680 GstRTSPClientPrivate *priv = client->priv;
1685 gst_sdp_message_new (&sdp);
1687 /* some standard things first */
1688 gst_sdp_message_set_version (sdp, "0");
1695 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1698 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1699 gst_sdp_message_set_information (sdp, "rtsp-server");
1700 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1701 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1702 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1703 gst_sdp_message_add_attribute (sdp, "control", "*");
1705 info.is_ipv6 = priv->is_ipv6;
1706 info.server_ip = priv->server_ip;
1708 /* create an SDP for the media object */
1709 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1717 GST_ERROR ("client %p: could not create SDP", client);
1718 gst_sdp_message_free (sdp);
1723 /* for the describe we must generate an SDP */
1725 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
1727 GstRTSPClientPrivate *priv = client->priv;
1732 GstRTSPMedia *media;
1733 GstRTSPClientClass *klass;
1735 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1740 /* check what kind of format is accepted, we don't really do anything with it
1741 * and always return SDP for now. */
1746 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
1748 if (res == GST_RTSP_ENOTIMPL)
1751 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1755 if (!priv->mount_points)
1756 goto no_mount_points;
1758 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
1761 /* find the media object for the uri */
1762 if (!(media = find_media (client, ctx, path, NULL)))
1765 /* create an SDP for the media object on this client */
1766 if (!(sdp = klass->create_sdp (client, media)))
1769 /* we suspend after the describe */
1770 gst_rtsp_media_suspend (media);
1771 g_object_unref (media);
1773 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
1774 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
1776 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
1779 /* content base for some clients that might screw up creating the setup uri */
1780 str = make_base_url (client, ctx->uri, path);
1783 GST_INFO ("adding content-base: %s", str);
1784 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE, str);
1786 /* add SDP to the response body */
1787 str = gst_sdp_message_as_text (sdp);
1788 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
1789 gst_sdp_message_free (sdp);
1791 send_message (client, ctx->session, ctx->response, FALSE);
1793 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1801 GST_ERROR ("client %p: no uri", client);
1802 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1807 GST_ERROR ("client %p: no mount points configured", client);
1808 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1813 GST_ERROR ("client %p: can't find path for url", client);
1814 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1819 GST_ERROR ("client %p: no media", client);
1821 /* error reply is already sent */
1826 GST_ERROR ("client %p: can't create SDP", client);
1827 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1829 g_object_unref (media);
1835 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
1837 GstRTSPMethod options;
1840 options = GST_RTSP_DESCRIBE |
1845 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1847 str = gst_rtsp_options_as_text (options);
1849 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
1850 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
1852 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
1855 send_message (client, ctx->session, ctx->response, FALSE);
1857 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1863 /* remove duplicate and trailing '/' */
1865 sanitize_uri (GstRTSPUrl * uri)
1869 gboolean have_slash, prev_slash;
1871 s = d = uri->abspath;
1872 len = strlen (uri->abspath);
1876 for (i = 0; i < len; i++) {
1877 have_slash = s[i] == '/';
1879 if (!have_slash || !prev_slash)
1881 prev_slash = have_slash;
1883 len = d - uri->abspath;
1884 /* don't remove the first slash if that's the only thing left */
1885 if (len > 1 && *(d - 1) == '/')
1891 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1893 GstRTSPClientPrivate *priv = client->priv;
1895 GST_INFO ("client %p: session %p finished", client, session);
1897 /* unlink all media managed in this session */
1898 client_unlink_session (client, session);
1900 /* remove the session */
1901 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1902 GST_INFO ("client %p: all sessions finalized, close the connection",
1904 close_connection (client);
1909 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1911 GstRTSPClientPrivate *priv = client->priv;
1912 GstRTSPMethod method;
1913 const gchar *uristr;
1914 GstRTSPUrl *uri = NULL;
1915 GstRTSPVersion version;
1917 GstRTSPSession *session = NULL;
1918 GstRTSPContext sctx = { NULL }, *ctx;
1919 GstRTSPMessage response = { 0 };
1922 if (!(ctx = gst_rtsp_context_get_current ())) {
1924 ctx->auth = priv->auth;
1925 gst_rtsp_context_push_current (ctx);
1928 ctx->conn = priv->connection;
1929 ctx->client = client;
1930 ctx->request = request;
1931 ctx->response = &response;
1933 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1934 gst_rtsp_message_dump (request);
1937 GST_INFO ("client %p: received a request", client);
1939 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1941 /* we can only handle 1.0 requests */
1942 if (version != GST_RTSP_VERSION_1_0)
1945 ctx->method = method;
1947 /* we always try to parse the url first */
1948 if (strcmp (uristr, "*") == 0) {
1949 /* special case where we have * as uri, keep uri = NULL */
1950 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
1951 /* check if the uristr is an absolute path <=> scheme and host information
1955 scheme = g_uri_parse_scheme (uristr);
1956 if (scheme == NULL && g_str_has_prefix (uristr, "/")) {
1957 gchar *absolute_uristr = NULL;
1959 GST_WARNING_OBJECT (client, "request doesn't contain absolute url");
1960 if (priv->server_ip == NULL) {
1961 GST_WARNING_OBJECT (client, "host information missing");
1966 g_strdup_printf ("rtsp://%s%s", priv->server_ip, uristr);
1968 GST_DEBUG_OBJECT (client, "absolute url: %s", absolute_uristr);
1969 if (gst_rtsp_url_parse (absolute_uristr, &uri) != GST_RTSP_OK) {
1970 g_free (absolute_uristr);
1973 g_free (absolute_uristr);
1980 /* get the session if there is any */
1981 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1982 if (res == GST_RTSP_OK) {
1983 if (priv->session_pool == NULL)
1986 /* we had a session in the request, find it again */
1987 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1988 goto session_not_found;
1990 /* we add the session to the client list of watched sessions. When a session
1991 * disappears because it times out, we will be notified. If all sessions are
1992 * gone, we will close the connection */
1993 client_watch_session (client, session);
1996 /* sanitize the uri */
2000 ctx->session = session;
2002 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
2003 goto not_authorized;
2005 /* now see what is asked and dispatch to a dedicated handler */
2007 case GST_RTSP_OPTIONS:
2008 handle_options_request (client, ctx);
2010 case GST_RTSP_DESCRIBE:
2011 handle_describe_request (client, ctx);
2013 case GST_RTSP_SETUP:
2014 handle_setup_request (client, ctx);
2017 handle_play_request (client, ctx);
2019 case GST_RTSP_PAUSE:
2020 handle_pause_request (client, ctx);
2022 case GST_RTSP_TEARDOWN:
2023 handle_teardown_request (client, ctx);
2025 case GST_RTSP_SET_PARAMETER:
2026 handle_set_param_request (client, ctx);
2028 case GST_RTSP_GET_PARAMETER:
2029 handle_get_param_request (client, ctx);
2031 case GST_RTSP_ANNOUNCE:
2032 case GST_RTSP_RECORD:
2033 case GST_RTSP_REDIRECT:
2034 goto not_implemented;
2035 case GST_RTSP_INVALID:
2042 gst_rtsp_context_pop_current (ctx);
2044 g_object_unref (session);
2046 gst_rtsp_url_free (uri);
2052 GST_ERROR ("client %p: version %d not supported", client, version);
2053 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
2059 GST_ERROR ("client %p: bad request", client);
2060 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2065 GST_ERROR ("client %p: no pool configured", client);
2066 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2071 GST_ERROR ("client %p: session not found", client);
2072 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2077 GST_ERROR ("client %p: not allowed", client);
2078 /* error reply is already sent */
2083 GST_ERROR ("client %p: method %d not implemented", client, method);
2084 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
2091 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
2093 GstRTSPClientPrivate *priv = client->priv;
2095 GstRTSPSession *session = NULL;
2096 GstRTSPContext sctx = { NULL }, *ctx;
2099 if (!(ctx = gst_rtsp_context_get_current ())) {
2101 ctx->auth = priv->auth;
2102 gst_rtsp_context_push_current (ctx);
2105 ctx->conn = priv->connection;
2106 ctx->client = client;
2107 ctx->request = NULL;
2109 ctx->method = GST_RTSP_INVALID;
2110 ctx->response = response;
2112 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2113 gst_rtsp_message_dump (response);
2116 GST_INFO ("client %p: received a response", client);
2118 /* get the session if there is any */
2120 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
2121 if (res == GST_RTSP_OK) {
2122 if (priv->session_pool == NULL)
2125 /* we had a session in the request, find it again */
2126 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2127 goto session_not_found;
2129 /* we add the session to the client list of watched sessions. When a session
2130 * disappears because it times out, we will be notified. If all sessions are
2131 * gone, we will close the connection */
2132 client_watch_session (client, session);
2135 ctx->session = session;
2137 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
2142 gst_rtsp_context_pop_current (ctx);
2144 g_object_unref (session);
2149 GST_ERROR ("client %p: no pool configured", client);
2154 GST_ERROR ("client %p: session not found", client);
2160 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2162 GstRTSPClientPrivate *priv = client->priv;
2171 /* find the stream for this message */
2172 res = gst_rtsp_message_parse_data (message, &channel);
2173 if (res != GST_RTSP_OK)
2176 gst_rtsp_message_steal_body (message, &data, &size);
2178 buffer = gst_buffer_new_wrapped (data, size);
2181 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2182 GstRTSPStreamTransport *trans;
2183 GstRTSPStream *stream;
2184 const GstRTSPTransport *tr;
2188 tr = gst_rtsp_stream_transport_get_transport (trans);
2189 stream = gst_rtsp_stream_transport_get_stream (trans);
2191 /* check for TCP transport */
2192 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2193 /* dispatch to the stream based on the channel number */
2194 if (tr->interleaved.min == channel) {
2195 gst_rtsp_stream_recv_rtp (stream, buffer);
2198 } else if (tr->interleaved.max == channel) {
2199 gst_rtsp_stream_recv_rtcp (stream, buffer);
2206 gst_buffer_unref (buffer);
2210 * gst_rtsp_client_set_session_pool:
2211 * @client: a #GstRTSPClient
2212 * @pool: a #GstRTSPSessionPool
2214 * Set @pool as the sessionpool for @client which it will use to find
2215 * or allocate sessions. the sessionpool is usually inherited from the server
2216 * that created the client but can be overridden later.
2219 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2220 GstRTSPSessionPool * pool)
2222 GstRTSPSessionPool *old;
2223 GstRTSPClientPrivate *priv;
2225 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2227 priv = client->priv;
2230 g_object_ref (pool);
2232 g_mutex_lock (&priv->lock);
2233 old = priv->session_pool;
2234 priv->session_pool = pool;
2235 g_mutex_unlock (&priv->lock);
2238 g_object_unref (old);
2242 * gst_rtsp_client_get_session_pool:
2243 * @client: a #GstRTSPClient
2245 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2247 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2249 GstRTSPSessionPool *
2250 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2252 GstRTSPClientPrivate *priv;
2253 GstRTSPSessionPool *result;
2255 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2257 priv = client->priv;
2259 g_mutex_lock (&priv->lock);
2260 if ((result = priv->session_pool))
2261 g_object_ref (result);
2262 g_mutex_unlock (&priv->lock);
2268 * gst_rtsp_client_set_mount_points:
2269 * @client: a #GstRTSPClient
2270 * @mounts: a #GstRTSPMountPoints
2272 * Set @mounts as the mount points for @client which it will use to map urls
2273 * to media streams. These mount points are usually inherited from the server that
2274 * created the client but can be overriden later.
2277 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2278 GstRTSPMountPoints * mounts)
2280 GstRTSPClientPrivate *priv;
2281 GstRTSPMountPoints *old;
2283 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2285 priv = client->priv;
2288 g_object_ref (mounts);
2290 g_mutex_lock (&priv->lock);
2291 old = priv->mount_points;
2292 priv->mount_points = mounts;
2293 g_mutex_unlock (&priv->lock);
2296 g_object_unref (old);
2300 * gst_rtsp_client_get_mount_points:
2301 * @client: a #GstRTSPClient
2303 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2305 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2307 GstRTSPMountPoints *
2308 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2310 GstRTSPClientPrivate *priv;
2311 GstRTSPMountPoints *result;
2313 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2315 priv = client->priv;
2317 g_mutex_lock (&priv->lock);
2318 if ((result = priv->mount_points))
2319 g_object_ref (result);
2320 g_mutex_unlock (&priv->lock);
2326 * gst_rtsp_client_set_auth:
2327 * @client: a #GstRTSPClient
2328 * @auth: a #GstRTSPAuth
2330 * configure @auth to be used as the authentication manager of @client.
2333 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2335 GstRTSPClientPrivate *priv;
2338 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2340 priv = client->priv;
2343 g_object_ref (auth);
2345 g_mutex_lock (&priv->lock);
2348 g_mutex_unlock (&priv->lock);
2351 g_object_unref (old);
2356 * gst_rtsp_client_get_auth:
2357 * @client: a #GstRTSPClient
2359 * Get the #GstRTSPAuth used as the authentication manager of @client.
2361 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2365 gst_rtsp_client_get_auth (GstRTSPClient * client)
2367 GstRTSPClientPrivate *priv;
2368 GstRTSPAuth *result;
2370 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2372 priv = client->priv;
2374 g_mutex_lock (&priv->lock);
2375 if ((result = priv->auth))
2376 g_object_ref (result);
2377 g_mutex_unlock (&priv->lock);
2383 * gst_rtsp_client_set_thread_pool:
2384 * @client: a #GstRTSPClient
2385 * @pool: a #GstRTSPThreadPool
2387 * configure @pool to be used as the thread pool of @client.
2390 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2391 GstRTSPThreadPool * pool)
2393 GstRTSPClientPrivate *priv;
2394 GstRTSPThreadPool *old;
2396 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2398 priv = client->priv;
2401 g_object_ref (pool);
2403 g_mutex_lock (&priv->lock);
2404 old = priv->thread_pool;
2405 priv->thread_pool = pool;
2406 g_mutex_unlock (&priv->lock);
2409 g_object_unref (old);
2413 * gst_rtsp_client_get_thread_pool:
2414 * @client: a #GstRTSPClient
2416 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2418 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2422 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2424 GstRTSPClientPrivate *priv;
2425 GstRTSPThreadPool *result;
2427 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2429 priv = client->priv;
2431 g_mutex_lock (&priv->lock);
2432 if ((result = priv->thread_pool))
2433 g_object_ref (result);
2434 g_mutex_unlock (&priv->lock);
2440 * gst_rtsp_client_set_connection:
2441 * @client: a #GstRTSPClient
2442 * @conn: (transfer full): a #GstRTSPConnection
2444 * Set the #GstRTSPConnection of @client. This function takes ownership of
2447 * Returns: %TRUE on success.
2450 gst_rtsp_client_set_connection (GstRTSPClient * client,
2451 GstRTSPConnection * conn)
2453 GstRTSPClientPrivate *priv;
2454 GSocket *read_socket;
2455 GSocketAddress *address;
2457 GError *error = NULL;
2459 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2460 g_return_val_if_fail (conn != NULL, FALSE);
2462 priv = client->priv;
2464 read_socket = gst_rtsp_connection_get_read_socket (conn);
2466 if (!(address = g_socket_get_local_address (read_socket, &error)))
2469 g_free (priv->server_ip);
2470 /* keep the original ip that the client connected to */
2471 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2472 GInetAddress *iaddr;
2474 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2476 /* socket might be ipv6 but adress still ipv4 */
2477 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2478 priv->server_ip = g_inet_address_to_string (iaddr);
2479 g_object_unref (address);
2481 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2482 priv->server_ip = g_strdup ("unknown");
2485 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2486 priv->server_ip, priv->is_ipv6);
2488 url = gst_rtsp_connection_get_url (conn);
2489 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2491 priv->connection = conn;
2498 GST_ERROR ("could not get local address %s", error->message);
2499 g_error_free (error);
2505 * gst_rtsp_client_get_connection:
2506 * @client: a #GstRTSPClient
2508 * Get the #GstRTSPConnection of @client.
2510 * Returns: (transfer none): the #GstRTSPConnection of @client.
2511 * The connection object returned remains valid until the client is freed.
2514 gst_rtsp_client_get_connection (GstRTSPClient * client)
2516 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2518 return client->priv->connection;
2522 * gst_rtsp_client_set_send_func:
2523 * @client: a #GstRTSPClient
2524 * @func: a #GstRTSPClientSendFunc
2525 * @user_data: user data passed to @func
2526 * @notify: called when @user_data is no longer in use
2528 * Set @func as the callback that will be called when a new message needs to be
2529 * sent to the client. @user_data is passed to @func and @notify is called when
2530 * @user_data is no longer in use.
2532 * By default, the client will send the messages on the #GstRTSPConnection that
2533 * was configured with gst_rtsp_client_attach() was called.
2536 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2537 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2539 GstRTSPClientPrivate *priv;
2540 GDestroyNotify old_notify;
2543 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2545 priv = client->priv;
2547 g_mutex_lock (&priv->send_lock);
2548 priv->send_func = func;
2549 old_notify = priv->send_notify;
2550 old_data = priv->send_data;
2551 priv->send_notify = notify;
2552 priv->send_data = user_data;
2553 g_mutex_unlock (&priv->send_lock);
2556 old_notify (old_data);
2560 * gst_rtsp_client_handle_message:
2561 * @client: a #GstRTSPClient
2562 * @message: an #GstRTSPMessage
2564 * Let the client handle @message.
2566 * Returns: a #GstRTSPResult.
2569 gst_rtsp_client_handle_message (GstRTSPClient * client,
2570 GstRTSPMessage * message)
2572 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2573 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2575 switch (message->type) {
2576 case GST_RTSP_MESSAGE_REQUEST:
2577 handle_request (client, message);
2579 case GST_RTSP_MESSAGE_RESPONSE:
2580 handle_response (client, message);
2582 case GST_RTSP_MESSAGE_DATA:
2583 handle_data (client, message);
2592 * gst_rtsp_client_send_message:
2593 * @client: a #GstRTSPClient
2594 * @session: a #GstRTSPSession to send the message to or %NULL
2595 * @message: The #GstRTSPMessage to send
2597 * Send a message message to the remote end. @message must be a
2598 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
2601 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
2602 GstRTSPMessage * message)
2604 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2605 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2606 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
2607 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
2609 send_message (client, session, message, FALSE);
2614 static GstRTSPResult
2615 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2616 gboolean close, gpointer user_data)
2618 GstRTSPClientPrivate *priv = client->priv;
2620 /* send the response and store the seq number so we can wait until it's
2621 * written to the client to close the connection */
2622 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2623 &priv->close_seq : NULL);
2626 static GstRTSPResult
2627 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2630 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2633 static GstRTSPResult
2634 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2636 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2637 GstRTSPClientPrivate *priv = client->priv;
2639 if (priv->close_seq && priv->close_seq == cseq) {
2640 priv->close_seq = 0;
2641 close_connection (client);
2647 static GstRTSPResult
2648 closed (GstRTSPWatch * watch, gpointer user_data)
2650 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2651 GstRTSPClientPrivate *priv = client->priv;
2652 const gchar *tunnelid;
2654 GST_INFO ("client %p: connection closed", client);
2656 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2657 g_mutex_lock (&tunnels_lock);
2658 /* remove from tunnelids */
2659 g_hash_table_remove (tunnels, tunnelid);
2660 g_mutex_unlock (&tunnels_lock);
2663 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2668 static GstRTSPResult
2669 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2671 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2674 str = gst_rtsp_strresult (result);
2675 GST_INFO ("client %p: received an error %s", client, str);
2681 static GstRTSPResult
2682 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2683 GstRTSPMessage * message, guint id, gpointer user_data)
2685 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2688 str = gst_rtsp_strresult (result);
2690 ("client %p: error when handling message %p with id %d: %s",
2691 client, message, id, str);
2698 remember_tunnel (GstRTSPClient * client)
2700 GstRTSPClientPrivate *priv = client->priv;
2701 const gchar *tunnelid;
2703 /* store client in the pending tunnels */
2704 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2705 if (tunnelid == NULL)
2708 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2710 /* we can't have two clients connecting with the same tunnelid */
2711 g_mutex_lock (&tunnels_lock);
2712 if (g_hash_table_lookup (tunnels, tunnelid))
2713 goto tunnel_existed;
2715 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2716 g_mutex_unlock (&tunnels_lock);
2723 GST_ERROR ("client %p: no tunnelid provided", client);
2728 g_mutex_unlock (&tunnels_lock);
2729 GST_ERROR ("client %p: tunnel session %s already existed", client,
2735 static GstRTSPStatusCode
2736 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2738 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2739 GstRTSPClientPrivate *priv = client->priv;
2741 GST_INFO ("client %p: tunnel start (connection %p)", client,
2744 if (!remember_tunnel (client))
2747 return GST_RTSP_STS_OK;
2752 GST_ERROR ("client %p: error starting tunnel", client);
2753 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2757 static GstRTSPResult
2758 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2760 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2761 GstRTSPClientPrivate *priv = client->priv;
2763 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2766 /* ignore error, it'll only be a problem when the client does a POST again */
2767 remember_tunnel (client);
2772 static GstRTSPResult
2773 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2775 const gchar *tunnelid;
2776 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2777 GstRTSPClientPrivate *priv = client->priv;
2778 GstRTSPClient *oclient;
2779 GstRTSPClientPrivate *opriv;
2781 GST_INFO ("client %p: tunnel complete", client);
2783 /* find previous tunnel */
2784 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2785 if (tunnelid == NULL)
2788 g_mutex_lock (&tunnels_lock);
2789 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2792 /* remove the old client from the table. ref before because removing it will
2793 * remove the ref to it. */
2794 g_object_ref (oclient);
2795 g_hash_table_remove (tunnels, tunnelid);
2797 opriv = oclient->priv;
2799 if (opriv->watch == NULL)
2801 g_mutex_unlock (&tunnels_lock);
2803 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2804 opriv->connection, priv->connection);
2806 /* merge the tunnels into the first client */
2807 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2808 gst_rtsp_watch_reset (opriv->watch);
2809 g_object_unref (oclient);
2816 GST_ERROR ("client %p: no tunnelid provided", client);
2817 return GST_RTSP_ERROR;
2821 g_mutex_unlock (&tunnels_lock);
2822 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2823 return GST_RTSP_ERROR;
2827 g_mutex_unlock (&tunnels_lock);
2828 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2829 g_object_unref (oclient);
2830 return GST_RTSP_ERROR;
2834 static GstRTSPWatchFuncs watch_funcs = {
2846 client_watch_notify (GstRTSPClient * client)
2848 GstRTSPClientPrivate *priv = client->priv;
2850 GST_INFO ("client %p: watch destroyed", client);
2852 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2853 g_object_unref (client);
2857 * gst_rtsp_client_attach:
2858 * @client: a #GstRTSPClient
2859 * @context: (allow-none): a #GMainContext
2861 * Attaches @client to @context. When the mainloop for @context is run, the
2862 * client will be dispatched. When @context is %NULL, the default context will be
2865 * This function should be called when the client properties and urls are fully
2866 * configured and the client is ready to start.
2868 * Returns: the ID (greater than 0) for the source within the GMainContext.
2871 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2873 GstRTSPClientPrivate *priv;
2876 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2877 priv = client->priv;
2878 g_return_val_if_fail (priv->connection != NULL, 0);
2879 g_return_val_if_fail (priv->watch == NULL, 0);
2881 /* create watch for the connection and attach */
2882 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2883 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2884 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2885 (GDestroyNotify) gst_rtsp_watch_unref);
2887 /* FIXME make this configurable. We don't want to do this yet because it will
2888 * be superceeded by a cache object later */
2889 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2891 GST_INFO ("attaching to context %p", context);
2892 res = gst_rtsp_watch_attach (priv->watch, context);
2898 * gst_rtsp_client_session_filter:
2899 * @client: a #GstRTSPClient
2900 * @func: (scope call) (allow-none): a callback
2901 * @user_data: user data passed to @func
2903 * Call @func for each session managed by @client. The result value of @func
2904 * determines what happens to the session. @func will be called with @client
2905 * locked so no further actions on @client can be performed from @func.
2907 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
2910 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
2912 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
2913 * will also be added with an additional ref to the result #GList of this
2916 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each session.
2918 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
2919 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2920 * element in the #GList should be unreffed before the list is freed.
2923 gst_rtsp_client_session_filter (GstRTSPClient * client,
2924 GstRTSPClientSessionFilterFunc func, gpointer user_data)
2926 GstRTSPClientPrivate *priv;
2927 GList *result, *walk, *next;
2929 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2931 priv = client->priv;
2935 g_mutex_lock (&priv->lock);
2936 for (walk = priv->sessions; walk; walk = next) {
2937 GstRTSPSession *sess = walk->data;
2938 GstRTSPFilterResult res;
2940 next = g_list_next (walk);
2943 res = func (client, sess, user_data);
2945 res = GST_RTSP_FILTER_REF;
2948 case GST_RTSP_FILTER_REMOVE:
2949 /* stop watching the session and pretent it went away */
2950 client_cleanup_session (client, sess);
2952 case GST_RTSP_FILTER_REF:
2953 result = g_list_prepend (result, g_object_ref (sess));
2955 case GST_RTSP_FILTER_KEEP:
2960 g_mutex_unlock (&priv->lock);