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 <gst/sdp/gstmikey.h>
47 #include "rtsp-client.h"
49 #include "rtsp-params.h"
51 #define GST_RTSP_CLIENT_GET_PRIVATE(obj) \
52 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_CLIENT, GstRTSPClientPrivate))
55 * send_lock, lock, tunnels_lock
58 struct _GstRTSPClientPrivate
60 GMutex lock; /* protects everything else */
63 GstRTSPConnection *connection;
65 GMainContext *watch_context;
70 GstRTSPClientSendFunc send_func; /* protected by send_lock */
71 gpointer send_data; /* protected by send_lock */
72 GDestroyNotify send_notify; /* protected by send_lock */
74 GstRTSPSessionPool *session_pool;
75 gulong session_removed_id;
76 GstRTSPMountPoints *mount_points;
78 GstRTSPThreadPool *thread_pool;
80 /* used to cache the media in the last requested DESCRIBE so that
81 * we can pick it up in the next SETUP immediately */
85 GHashTable *transports;
87 guint sessions_cookie;
89 gboolean drop_backlog;
92 static GMutex tunnels_lock;
93 static GHashTable *tunnels; /* protected by tunnels_lock */
95 /* FIXME make this configurable. We don't want to do this yet because it will
96 * be superceeded by a cache object later */
97 #define WATCH_BACKLOG_SIZE 100
99 #define DEFAULT_SESSION_POOL NULL
100 #define DEFAULT_MOUNT_POINTS NULL
101 #define DEFAULT_DROP_BACKLOG TRUE
116 SIGNAL_OPTIONS_REQUEST,
117 SIGNAL_DESCRIBE_REQUEST,
118 SIGNAL_SETUP_REQUEST,
120 SIGNAL_PAUSE_REQUEST,
121 SIGNAL_TEARDOWN_REQUEST,
122 SIGNAL_SET_PARAMETER_REQUEST,
123 SIGNAL_GET_PARAMETER_REQUEST,
124 SIGNAL_HANDLE_RESPONSE,
129 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
130 #define GST_CAT_DEFAULT rtsp_client_debug
132 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
134 static void gst_rtsp_client_get_property (GObject * object, guint propid,
135 GValue * value, GParamSpec * pspec);
136 static void gst_rtsp_client_set_property (GObject * object, guint propid,
137 const GValue * value, GParamSpec * pspec);
138 static void gst_rtsp_client_finalize (GObject * obj);
140 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
141 static gboolean default_configure_client_media (GstRTSPClient * client,
142 GstRTSPMedia * media, GstRTSPStream * stream, GstRTSPContext * ctx);
143 static gboolean default_configure_client_transport (GstRTSPClient * client,
144 GstRTSPContext * ctx, GstRTSPTransport * ct);
145 static GstRTSPResult default_params_set (GstRTSPClient * client,
146 GstRTSPContext * ctx);
147 static GstRTSPResult default_params_get (GstRTSPClient * client,
148 GstRTSPContext * ctx);
149 static gchar *default_make_path_from_uri (GstRTSPClient * client,
150 const GstRTSPUrl * uri);
151 static void client_session_removed (GstRTSPSessionPool * pool,
152 GstRTSPSession * session, GstRTSPClient * client);
154 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
157 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
159 GObjectClass *gobject_class;
161 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
163 gobject_class = G_OBJECT_CLASS (klass);
165 gobject_class->get_property = gst_rtsp_client_get_property;
166 gobject_class->set_property = gst_rtsp_client_set_property;
167 gobject_class->finalize = gst_rtsp_client_finalize;
169 klass->create_sdp = create_sdp;
170 klass->configure_client_media = default_configure_client_media;
171 klass->configure_client_transport = default_configure_client_transport;
172 klass->params_set = default_params_set;
173 klass->params_get = default_params_get;
174 klass->make_path_from_uri = default_make_path_from_uri;
176 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
177 g_param_spec_object ("session-pool", "Session Pool",
178 "The session pool to use for client session",
179 GST_TYPE_RTSP_SESSION_POOL,
180 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
182 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
183 g_param_spec_object ("mount-points", "Mount Points",
184 "The mount points to use for client session",
185 GST_TYPE_RTSP_MOUNT_POINTS,
186 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188 g_object_class_install_property (gobject_class, PROP_DROP_BACKLOG,
189 g_param_spec_boolean ("drop-backlog", "Drop Backlog",
190 "Drop data when the backlog queue is full",
191 DEFAULT_DROP_BACKLOG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
193 gst_rtsp_client_signals[SIGNAL_CLOSED] =
194 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
195 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
196 g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
198 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
199 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
200 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
201 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
203 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
204 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
205 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
206 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
207 GST_TYPE_RTSP_CONTEXT);
209 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
210 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
211 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
212 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
213 GST_TYPE_RTSP_CONTEXT);
215 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
216 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
217 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
218 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
219 GST_TYPE_RTSP_CONTEXT);
221 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
222 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
223 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
224 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
225 GST_TYPE_RTSP_CONTEXT);
227 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
228 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
229 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
230 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
231 GST_TYPE_RTSP_CONTEXT);
233 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
234 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
235 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
236 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
237 GST_TYPE_RTSP_CONTEXT);
239 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
240 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
241 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
242 set_parameter_request), NULL, NULL, g_cclosure_marshal_generic,
243 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
245 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
246 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
247 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
248 get_parameter_request), NULL, NULL, g_cclosure_marshal_generic,
249 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
251 gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE] =
252 g_signal_new ("handle-response", G_TYPE_FROM_CLASS (klass),
253 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
254 handle_response), NULL, NULL, g_cclosure_marshal_generic,
255 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
258 * GstRTSPClient::send-message:
259 * @client: The RTSP client
260 * @session: (type GstRtspServer.RTSPSession): The session
261 * @message: (type GstRtsp.RTSPMessage): The message
263 gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE] =
264 g_signal_new ("send-message", G_TYPE_FROM_CLASS (klass),
265 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
266 send_message), NULL, NULL, g_cclosure_marshal_generic,
267 G_TYPE_NONE, 2, GST_TYPE_RTSP_CONTEXT, G_TYPE_POINTER);
270 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
271 g_mutex_init (&tunnels_lock);
273 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
277 gst_rtsp_client_init (GstRTSPClient * client)
279 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
283 g_mutex_init (&priv->lock);
284 g_mutex_init (&priv->send_lock);
285 g_mutex_init (&priv->watch_lock);
287 priv->drop_backlog = DEFAULT_DROP_BACKLOG;
289 g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
293 static GstRTSPFilterResult
294 filter_session_media (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
297 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
299 return GST_RTSP_FILTER_REMOVE;
303 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
305 GstRTSPClientPrivate *priv = client->priv;
307 g_mutex_lock (&priv->lock);
308 /* check if we already know about this session */
309 if (g_list_find (priv->sessions, session) == NULL) {
310 GST_INFO ("watching session %p", session);
312 priv->sessions = g_list_prepend (priv->sessions, g_object_ref (session));
313 priv->sessions_cookie++;
315 /* connect removed session handler, it will be disconnected when the last
316 * session gets removed */
317 if (priv->session_removed_id == 0)
318 priv->session_removed_id = g_signal_connect_data (priv->session_pool,
319 "session-removed", G_CALLBACK (client_session_removed),
320 g_object_ref (client), (GClosureNotify) g_object_unref, 0);
322 g_mutex_unlock (&priv->lock);
327 /* should be called with lock */
329 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session,
332 GstRTSPClientPrivate *priv = client->priv;
334 GST_INFO ("client %p: unwatch session %p", client, session);
337 link = g_list_find (priv->sessions, session);
342 priv->sessions = g_list_delete_link (priv->sessions, link);
343 priv->sessions_cookie++;
345 /* if this was the last session, disconnect the handler.
346 * This will also drop the extra client ref */
347 if (!priv->sessions) {
348 g_signal_handler_disconnect (priv->session_pool, priv->session_removed_id);
349 priv->session_removed_id = 0;
352 /* remove the session */
353 g_object_unref (session);
356 static GstRTSPFilterResult
357 cleanup_session (GstRTSPClient * client, GstRTSPSession * sess,
360 /* unlink all media managed in this session. This needs to happen
361 * without the client lock, so we really want to do it here. */
362 gst_rtsp_session_filter (sess, filter_session_media, client);
364 return GST_RTSP_FILTER_REMOVE;
368 clean_cached_media (GstRTSPClient * client, gboolean unprepare)
370 GstRTSPClientPrivate *priv = client->priv;
378 gst_rtsp_media_unprepare (priv->media);
379 g_object_unref (priv->media);
384 /* A client is finalized when the connection is broken */
386 gst_rtsp_client_finalize (GObject * obj)
388 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
389 GstRTSPClientPrivate *priv = client->priv;
391 GST_INFO ("finalize client %p", client);
394 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
395 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
398 g_source_destroy ((GSource *) priv->watch);
400 if (priv->watch_context)
401 g_main_context_unref (priv->watch_context);
403 /* all sessions should have been removed by now. We keep a ref to
404 * the client object for the session removed handler. The ref is
405 * dropped when the last session is removed from the list. */
406 g_assert (priv->sessions == NULL);
407 g_assert (priv->session_removed_id == 0);
409 g_hash_table_unref (priv->transports);
411 if (priv->connection)
412 gst_rtsp_connection_free (priv->connection);
413 if (priv->session_pool) {
414 g_object_unref (priv->session_pool);
416 if (priv->mount_points)
417 g_object_unref (priv->mount_points);
419 g_object_unref (priv->auth);
420 if (priv->thread_pool)
421 g_object_unref (priv->thread_pool);
423 clean_cached_media (client, TRUE);
425 g_free (priv->server_ip);
426 g_mutex_clear (&priv->lock);
427 g_mutex_clear (&priv->send_lock);
428 g_mutex_clear (&priv->watch_lock);
430 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
434 gst_rtsp_client_get_property (GObject * object, guint propid,
435 GValue * value, GParamSpec * pspec)
437 GstRTSPClient *client = GST_RTSP_CLIENT (object);
438 GstRTSPClientPrivate *priv = client->priv;
441 case PROP_SESSION_POOL:
442 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
444 case PROP_MOUNT_POINTS:
445 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
447 case PROP_DROP_BACKLOG:
448 g_value_set_boolean (value, priv->drop_backlog);
451 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
456 gst_rtsp_client_set_property (GObject * object, guint propid,
457 const GValue * value, GParamSpec * pspec)
459 GstRTSPClient *client = GST_RTSP_CLIENT (object);
460 GstRTSPClientPrivate *priv = client->priv;
463 case PROP_SESSION_POOL:
464 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
466 case PROP_MOUNT_POINTS:
467 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
469 case PROP_DROP_BACKLOG:
470 g_mutex_lock (&priv->lock);
471 priv->drop_backlog = g_value_get_boolean (value);
472 g_mutex_unlock (&priv->lock);
475 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
480 * gst_rtsp_client_new:
482 * Create a new #GstRTSPClient instance.
484 * Returns: (transfer full): a new #GstRTSPClient
487 gst_rtsp_client_new (void)
489 GstRTSPClient *result;
491 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
497 send_message (GstRTSPClient * client, GstRTSPContext * ctx,
498 GstRTSPMessage * message, gboolean close)
500 GstRTSPClientPrivate *priv = client->priv;
502 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
503 "GStreamer RTSP server");
505 /* remove any previous header */
506 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
508 /* add the new session header for new session ids */
510 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
511 gst_rtsp_session_get_header (ctx->session));
514 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
515 gst_rtsp_message_dump (message);
519 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
521 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE],
524 g_mutex_lock (&priv->send_lock);
526 priv->send_func (client, message, close, priv->send_data);
527 g_mutex_unlock (&priv->send_lock);
529 gst_rtsp_message_unset (message);
533 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
534 GstRTSPContext * ctx)
536 gst_rtsp_message_init_response (ctx->response, code,
537 gst_rtsp_status_as_text (code), ctx->request);
541 send_message (client, ctx, ctx->response, FALSE);
545 send_option_not_supported_response (GstRTSPClient * client,
546 GstRTSPContext * ctx, const gchar * unsupported_options)
548 GstRTSPStatusCode code = GST_RTSP_STS_OPTION_NOT_SUPPORTED;
550 gst_rtsp_message_init_response (ctx->response, code,
551 gst_rtsp_status_as_text (code), ctx->request);
553 if (unsupported_options != NULL) {
554 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_UNSUPPORTED,
555 unsupported_options);
560 send_message (client, ctx, ctx->response, FALSE);
564 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
566 if (path1 == NULL || path2 == NULL)
569 if (strlen (path1) != len2)
572 if (strncmp (path1, path2, len2))
578 /* this function is called to initially find the media for the DESCRIBE request
579 * but is cached for when the same client (without breaking the connection) is
580 * doing a setup for the exact same url. */
581 static GstRTSPMedia *
582 find_media (GstRTSPClient * client, GstRTSPContext * ctx, gchar * path,
585 GstRTSPClientPrivate *priv = client->priv;
586 GstRTSPMediaFactory *factory;
590 /* find the longest matching factory for the uri first */
591 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
595 ctx->factory = factory;
597 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
598 goto no_factory_access;
600 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
606 path_len = strlen (path);
608 if (!paths_are_equal (priv->path, path, path_len)) {
609 GstRTSPThread *thread;
611 /* remove any previously cached values before we try to construct a new
613 clean_cached_media (client, TRUE);
615 /* prepare the media and add it to the pipeline */
616 if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
621 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
622 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
626 /* prepare the media */
627 if (!(gst_rtsp_media_prepare (media, thread)))
630 /* now keep track of the uri and the media */
631 priv->path = g_strndup (path, path_len);
634 /* we have seen this path before, used cached media */
637 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
640 g_object_unref (factory);
644 g_object_ref (media);
651 GST_ERROR ("client %p: no factory for path %s", client, path);
652 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
657 GST_ERROR ("client %p: not authorized to see factory path %s", client,
659 /* error reply is already sent */
664 GST_ERROR ("client %p: not authorized for factory path %s", client, path);
665 /* error reply is already sent */
670 GST_ERROR ("client %p: can't create media", client);
671 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
672 g_object_unref (factory);
678 GST_ERROR ("client %p: can't create thread", client);
679 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
680 g_object_unref (media);
682 g_object_unref (factory);
688 GST_ERROR ("client %p: can't prepare media", client);
689 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
690 g_object_unref (media);
692 g_object_unref (factory);
699 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
701 GstRTSPClientPrivate *priv = client->priv;
702 GstRTSPMessage message = { 0 };
703 GstRTSPResult res = GST_RTSP_OK;
708 gst_rtsp_message_init_data (&message, channel);
710 /* FIXME, need some sort of iovec RTSPMessage here */
711 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
714 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
716 g_mutex_lock (&priv->send_lock);
718 res = priv->send_func (client, &message, FALSE, priv->send_data);
719 g_mutex_unlock (&priv->send_lock);
721 gst_rtsp_message_steal_body (&message, &data, &usize);
722 gst_buffer_unmap (buffer, &map_info);
724 gst_rtsp_message_unset (&message);
726 return res == GST_RTSP_OK;
730 * gst_rtsp_client_close:
731 * @client: a #GstRTSPClient
733 * Close the connection of @client and remove all media it was managing.
738 gst_rtsp_client_close (GstRTSPClient * client)
740 GstRTSPClientPrivate *priv = client->priv;
741 const gchar *tunnelid;
743 GST_DEBUG ("client %p: closing connection", client);
745 if (priv->connection) {
746 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
747 g_mutex_lock (&tunnels_lock);
748 /* remove from tunnelids */
749 g_hash_table_remove (tunnels, tunnelid);
750 g_mutex_unlock (&tunnels_lock);
752 gst_rtsp_connection_close (priv->connection);
755 /* connection is now closed, destroy the watch which will also cause the
756 * closed signal to be emitted */
758 GST_DEBUG ("client %p: destroying watch", client);
759 g_source_destroy ((GSource *) priv->watch);
761 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
762 g_main_context_unref (priv->watch_context);
763 priv->watch_context = NULL;
768 default_make_path_from_uri (GstRTSPClient * client, const GstRTSPUrl * uri)
773 path = g_strconcat (uri->abspath, "?", uri->query, NULL);
775 path = g_strdup (uri->abspath);
781 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
783 GstRTSPClientPrivate *priv = client->priv;
784 GstRTSPClientClass *klass;
785 GstRTSPSession *session;
786 GstRTSPSessionMedia *sessmedia;
787 GstRTSPStatusCode code;
790 gboolean keep_session;
795 session = ctx->session;
800 klass = GST_RTSP_CLIENT_GET_CLASS (client);
801 path = klass->make_path_from_uri (client, ctx->uri);
803 /* get a handle to the configuration of the media in the session */
804 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
808 /* only aggregate control for now.. */
809 if (path[matched] != '\0')
814 ctx->sessmedia = sessmedia;
816 /* we emit the signal before closing the connection */
817 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
820 /* make sure we unblock the backlog and don't accept new messages
822 if (priv->watch != NULL)
823 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
825 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
827 /* allow messages again so that we can send the reply */
828 if (priv->watch != NULL)
829 gst_rtsp_watch_set_flushing (priv->watch, FALSE);
831 /* unmanage the media in the session, returns false if all media session
833 keep_session = gst_rtsp_session_release_media (session, sessmedia);
835 /* construct the response now */
836 code = GST_RTSP_STS_OK;
837 gst_rtsp_message_init_response (ctx->response, code,
838 gst_rtsp_status_as_text (code), ctx->request);
840 send_message (client, ctx, ctx->response, TRUE);
843 /* remove the session */
844 gst_rtsp_session_pool_remove (priv->session_pool, session);
852 GST_ERROR ("client %p: no session", client);
853 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
858 GST_ERROR ("client %p: no uri supplied", client);
859 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
864 GST_ERROR ("client %p: no media for uri", client);
865 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
871 GST_ERROR ("client %p: no aggregate path %s", client, path);
872 send_generic_response (client,
873 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
880 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
884 res = gst_rtsp_params_set (client, ctx);
890 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
894 res = gst_rtsp_params_get (client, ctx);
900 handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
906 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
907 if (res != GST_RTSP_OK)
911 /* no body, keep-alive request */
912 send_generic_response (client, GST_RTSP_STS_OK, ctx);
914 /* there is a body, handle the params */
915 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
916 if (res != GST_RTSP_OK)
919 send_message (client, ctx, ctx->response, FALSE);
922 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
930 GST_ERROR ("client %p: bad request", client);
931 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
937 handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
943 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
944 if (res != GST_RTSP_OK)
948 /* no body, keep-alive request */
949 send_generic_response (client, GST_RTSP_STS_OK, ctx);
951 /* there is a body, handle the params */
952 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
953 if (res != GST_RTSP_OK)
956 send_message (client, ctx, ctx->response, FALSE);
959 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
967 GST_ERROR ("client %p: bad request", client);
968 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
974 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
976 GstRTSPSession *session;
977 GstRTSPClientClass *klass;
978 GstRTSPSessionMedia *sessmedia;
979 GstRTSPStatusCode code;
980 GstRTSPState rtspstate;
984 if (!(session = ctx->session))
990 klass = GST_RTSP_CLIENT_GET_CLASS (client);
991 path = klass->make_path_from_uri (client, ctx->uri);
993 /* get a handle to the configuration of the media in the session */
994 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
998 if (path[matched] != '\0')
1003 ctx->sessmedia = sessmedia;
1005 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1006 /* the session state must be playing or recording */
1007 if (rtspstate != GST_RTSP_STATE_PLAYING &&
1008 rtspstate != GST_RTSP_STATE_RECORDING)
1011 /* then pause sending */
1012 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
1014 /* construct the response now */
1015 code = GST_RTSP_STS_OK;
1016 gst_rtsp_message_init_response (ctx->response, code,
1017 gst_rtsp_status_as_text (code), ctx->request);
1019 send_message (client, ctx, ctx->response, FALSE);
1021 /* the state is now READY */
1022 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1024 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
1031 GST_ERROR ("client %p: no seesion", client);
1032 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1037 GST_ERROR ("client %p: no uri supplied", client);
1038 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1043 GST_ERROR ("client %p: no media for uri", client);
1044 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1050 GST_ERROR ("client %p: no aggregate path %s", client, path);
1051 send_generic_response (client,
1052 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1058 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
1059 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1065 /* convert @url and @path to a URL used as a content base for the factory
1066 * located at @path */
1068 make_base_url (GstRTSPClient * client, GstRTSPUrl * url, const gchar * path)
1074 /* check for trailing '/' and append one */
1075 trail = (path[strlen (path) - 1] != '/' ? "/" : "");
1080 tmp.abspath = g_strdup_printf ("%s%s", path, trail);
1082 result = gst_rtsp_url_get_request_uri (&tmp);
1083 g_free (tmp.abspath);
1089 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
1091 GstRTSPSession *session;
1092 GstRTSPClientClass *klass;
1093 GstRTSPSessionMedia *sessmedia;
1094 GstRTSPMedia *media;
1095 GstRTSPStatusCode code;
1098 GstRTSPTimeRange *range;
1100 GstRTSPState rtspstate;
1101 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1102 gchar *path, *rtpinfo;
1105 if (!(session = ctx->session))
1108 if (!(uri = ctx->uri))
1111 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1112 path = klass->make_path_from_uri (client, uri);
1114 /* get a handle to the configuration of the media in the session */
1115 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1119 if (path[matched] != '\0')
1124 ctx->sessmedia = sessmedia;
1125 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1127 /* the session state must be playing or ready */
1128 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1129 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1132 /* in play we first unsuspend, media could be suspended from SDP or PAUSED */
1133 if (!gst_rtsp_media_unsuspend (media))
1134 goto unsuspend_failed;
1136 /* parse the range header if we have one */
1137 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1138 if (res == GST_RTSP_OK) {
1139 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1140 /* we have a range, seek to the position */
1142 gst_rtsp_media_seek (media, range);
1143 gst_rtsp_range_free (range);
1147 /* grab RTPInfo from the media now */
1148 rtpinfo = gst_rtsp_session_media_get_rtpinfo (sessmedia);
1150 /* construct the response now */
1151 code = GST_RTSP_STS_OK;
1152 gst_rtsp_message_init_response (ctx->response, code,
1153 gst_rtsp_status_as_text (code), ctx->request);
1155 /* add the RTP-Info header */
1157 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO,
1161 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1163 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1165 send_message (client, ctx, ctx->response, FALSE);
1167 /* start playing after sending the response */
1168 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1170 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1172 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1179 GST_ERROR ("client %p: no session", client);
1180 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1185 GST_ERROR ("client %p: no uri supplied", client);
1186 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1191 GST_ERROR ("client %p: media not found", client);
1192 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1197 GST_ERROR ("client %p: no aggregate path %s", client, path);
1198 send_generic_response (client,
1199 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1205 GST_ERROR ("client %p: not PLAYING or READY", client);
1206 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1212 GST_ERROR ("client %p: unsuspend failed", client);
1213 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1219 do_keepalive (GstRTSPSession * session)
1221 GST_INFO ("keep session %p alive", session);
1222 gst_rtsp_session_touch (session);
1225 /* parse @transport and return a valid transport in @tr. only transports
1226 * supported by @stream are returned. Returns FALSE if no valid transport
1229 parse_transport (const char *transport, GstRTSPStream * stream,
1230 GstRTSPTransport * tr)
1237 gst_rtsp_transport_init (tr);
1239 GST_DEBUG ("parsing transports %s", transport);
1241 transports = g_strsplit (transport, ",", 0);
1243 /* loop through the transports, try to parse */
1244 for (i = 0; transports[i]; i++) {
1245 res = gst_rtsp_transport_parse (transports[i], tr);
1246 if (res != GST_RTSP_OK) {
1247 /* no valid transport, search some more */
1248 GST_WARNING ("could not parse transport %s", transports[i]);
1252 /* we have a transport, see if it's supported */
1253 if (!gst_rtsp_stream_is_transport_supported (stream, tr)) {
1254 GST_WARNING ("unsupported transport %s", transports[i]);
1258 /* we have a valid transport */
1259 GST_INFO ("found valid transport %s", transports[i]);
1264 gst_rtsp_transport_init (tr);
1266 g_strfreev (transports);
1272 default_configure_client_media (GstRTSPClient * client, GstRTSPMedia * media,
1273 GstRTSPStream * stream, GstRTSPContext * ctx)
1275 GstRTSPMessage *request = ctx->request;
1276 gchar *blocksize_str;
1278 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1279 &blocksize_str, 0) == GST_RTSP_OK) {
1283 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1284 if (end == blocksize_str)
1287 /* we don't want to change the mtu when this media
1288 * can be shared because it impacts other clients */
1289 if (gst_rtsp_media_is_shared (media))
1292 if (blocksize > G_MAXUINT)
1293 blocksize = G_MAXUINT;
1295 gst_rtsp_stream_set_mtu (stream, blocksize);
1303 GST_ERROR_OBJECT (client, "failed to parse blocksize");
1304 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1310 default_configure_client_transport (GstRTSPClient * client,
1311 GstRTSPContext * ctx, GstRTSPTransport * ct)
1313 GstRTSPClientPrivate *priv = client->priv;
1315 /* we have a valid transport now, set the destination of the client. */
1316 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1317 gboolean use_client_settings;
1319 use_client_settings =
1320 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1322 if (ct->destination && use_client_settings) {
1323 GstRTSPAddress *addr;
1325 addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1326 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1331 gst_rtsp_address_free (addr);
1333 GstRTSPAddress *addr;
1334 GSocketFamily family;
1336 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1338 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1342 g_free (ct->destination);
1343 ct->destination = g_strdup (addr->address);
1344 ct->port.min = addr->port;
1345 ct->port.max = addr->port + addr->n_ports - 1;
1346 ct->ttl = addr->ttl;
1348 gst_rtsp_address_free (addr);
1353 url = gst_rtsp_connection_get_url (priv->connection);
1354 g_free (ct->destination);
1355 ct->destination = g_strdup (url->host);
1357 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1359 GSocketAddress *addr;
1361 sock = gst_rtsp_connection_get_read_socket (priv->connection);
1362 if ((addr = g_socket_get_remote_address (sock, NULL))) {
1363 /* our read port is the sender port of client */
1364 ct->client_port.min =
1365 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1366 g_object_unref (addr);
1368 if ((addr = g_socket_get_local_address (sock, NULL))) {
1369 ct->server_port.max =
1370 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1371 g_object_unref (addr);
1373 sock = gst_rtsp_connection_get_write_socket (priv->connection);
1374 if ((addr = g_socket_get_remote_address (sock, NULL))) {
1375 /* our write port is the receiver port of client */
1376 ct->client_port.max =
1377 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1378 g_object_unref (addr);
1380 if ((addr = g_socket_get_local_address (sock, NULL))) {
1381 ct->server_port.min =
1382 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1383 g_object_unref (addr);
1385 /* check if the client selected channels for TCP */
1386 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1387 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1397 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1402 static GstRTSPTransport *
1403 make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
1404 GstRTSPTransport * ct)
1406 GstRTSPTransport *st;
1408 GSocketFamily family;
1410 /* prepare the server transport */
1411 gst_rtsp_transport_new (&st);
1413 st->trans = ct->trans;
1414 st->profile = ct->profile;
1415 st->lower_transport = ct->lower_transport;
1417 addr = g_inet_address_new_from_string (ct->destination);
1420 GST_ERROR ("failed to get inet addr from client destination");
1421 family = G_SOCKET_FAMILY_IPV4;
1423 family = g_inet_address_get_family (addr);
1424 g_object_unref (addr);
1428 switch (st->lower_transport) {
1429 case GST_RTSP_LOWER_TRANS_UDP:
1430 st->client_port = ct->client_port;
1431 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
1433 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1434 st->port = ct->port;
1435 st->destination = g_strdup (ct->destination);
1438 case GST_RTSP_LOWER_TRANS_TCP:
1439 st->interleaved = ct->interleaved;
1440 st->client_port = ct->client_port;
1441 st->server_port = ct->server_port;
1446 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
1451 #define AES_128_KEY_LEN 16
1452 #define AES_256_KEY_LEN 32
1454 #define HMAC_32_KEY_LEN 4
1455 #define HMAC_80_KEY_LEN 10
1458 mikey_apply_policy (GstCaps * caps, GstMIKEYMessage * msg, guint8 policy)
1460 const gchar *srtp_cipher;
1461 const gchar *srtp_auth;
1462 const GstMIKEYPayload *sp;
1465 /* loop over Security policy until we find one containing policy */
1467 if ((sp = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, i)) == NULL)
1470 if (((GstMIKEYPayloadSP *) sp)->policy == policy)
1474 /* the default ciphers */
1475 srtp_cipher = "aes-128-icm";
1476 srtp_auth = "hmac-sha1-80";
1478 /* now override the defaults with what is in the Security Policy */
1482 /* collect all the params and go over them */
1483 len = gst_mikey_payload_sp_get_n_params (sp);
1484 for (i = 0; i < len; i++) {
1485 const GstMIKEYPayloadSPParam *param =
1486 gst_mikey_payload_sp_get_param (sp, i);
1488 switch (param->type) {
1489 case GST_MIKEY_SP_SRTP_ENC_ALG:
1490 switch (param->val[0]) {
1492 srtp_cipher = "null";
1496 srtp_cipher = "aes-128-icm";
1502 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
1503 switch (param->val[0]) {
1504 case AES_128_KEY_LEN:
1505 srtp_cipher = "aes-128-icm";
1507 case AES_256_KEY_LEN:
1508 srtp_cipher = "aes-256-icm";
1514 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1515 switch (param->val[0]) {
1521 srtp_auth = "hmac-sha1-80";
1527 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
1528 switch (param->val[0]) {
1529 case HMAC_32_KEY_LEN:
1530 srtp_auth = "hmac-sha1-32";
1532 case HMAC_80_KEY_LEN:
1533 srtp_auth = "hmac-sha1-80";
1539 case GST_MIKEY_SP_SRTP_SRTP_ENC:
1541 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
1548 /* now configure the SRTP parameters */
1549 gst_caps_set_simple (caps,
1550 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
1551 "srtp-auth", G_TYPE_STRING, srtp_auth,
1552 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
1553 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
1559 handle_mikey_data (GstRTSPClient * client, GstRTSPContext * ctx,
1560 guint8 * data, gsize size)
1562 GstMIKEYMessage *msg;
1564 GstCaps *caps = NULL;
1565 GstMIKEYPayloadKEMAC *kemac;
1566 const GstMIKEYPayloadKeyData *pkd;
1569 /* the MIKEY message contains a CSB or crypto session bundle. It is a
1570 * set of Crypto Sessions protected with the same master key.
1571 * In the context of SRTP, an RTP and its RTCP stream is part of a
1573 if ((msg = gst_mikey_message_new_from_data (data, size, NULL, NULL)) == NULL)
1576 /* we can only handle SRTP crypto sessions for now */
1577 if (msg->map_type != GST_MIKEY_MAP_TYPE_SRTP)
1578 goto invalid_map_type;
1580 /* get the number of crypto sessions. This maps SSRC to its
1581 * security parameters */
1582 n_cs = gst_mikey_message_get_n_cs (msg);
1584 goto no_crypto_sessions;
1586 /* we also need keys */
1587 if (!(kemac = (GstMIKEYPayloadKEMAC *) gst_mikey_message_find_payload
1588 (msg, GST_MIKEY_PT_KEMAC, 0)))
1591 /* we don't support encrypted keys */
1592 if (kemac->enc_alg != GST_MIKEY_ENC_NULL
1593 || kemac->mac_alg != GST_MIKEY_MAC_NULL)
1594 goto unsupported_encryption;
1596 /* get Key data sub-payload */
1597 pkd = (const GstMIKEYPayloadKeyData *)
1598 gst_mikey_payload_kemac_get_sub (&kemac->pt, 0);
1601 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
1604 /* go over all crypto sessions and create the security policy for each
1606 for (i = 0; i < n_cs; i++) {
1607 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
1609 caps = gst_caps_new_simple ("application/x-srtp",
1610 "ssrc", G_TYPE_UINT, map->ssrc,
1611 "roc", G_TYPE_UINT, map->roc, "srtp-key", GST_TYPE_BUFFER, key, NULL);
1612 mikey_apply_policy (caps, msg, map->policy);
1614 gst_rtsp_stream_update_crypto (ctx->stream, map->ssrc, caps);
1615 gst_caps_unref (caps);
1617 gst_mikey_message_unref (msg);
1618 gst_buffer_unref (key);
1625 GST_DEBUG_OBJECT (client, "failed to parse MIKEY message");
1630 GST_DEBUG_OBJECT (client, "invalid map type %d", msg->map_type);
1631 goto cleanup_message;
1635 GST_DEBUG_OBJECT (client, "no crypto sessions");
1636 goto cleanup_message;
1640 GST_DEBUG_OBJECT (client, "no keys found");
1641 goto cleanup_message;
1643 unsupported_encryption:
1645 GST_DEBUG_OBJECT (client, "unsupported key encryption");
1646 goto cleanup_message;
1650 gst_mikey_message_unref (msg);
1655 #define IS_STRIP_CHAR(c) (g_ascii_isspace ((guchar)(c)) || ((c) == '\"'))
1658 strip_chars (gchar * str)
1665 if (!IS_STRIP_CHAR (str[len]))
1669 for (s = str; *s && IS_STRIP_CHAR (*s); s++);
1670 memmove (str, s, len + 1);
1673 /* KeyMgmt = "KeyMgmt" ":" key-mgmt-spec 0*("," key-mgmt-spec)
1674 * key-mgmt-spec = "prot" "=" KMPID ";" ["uri" "=" %x22 URI %x22 ";"]
1677 handle_keymgmt (GstRTSPClient * client, GstRTSPContext * ctx, gchar * keymgmt)
1682 specs = g_strsplit (keymgmt, ",", 0);
1683 for (i = 0; specs[i]; i++) {
1686 split = g_strsplit (specs[i], ";", 0);
1687 for (j = 0; split[j]; j++) {
1688 g_strstrip (split[j]);
1689 if (g_str_has_prefix (split[j], "prot=")) {
1690 g_strstrip (split[j] + 5);
1691 if (!g_str_equal (split[j] + 5, "mikey"))
1693 GST_DEBUG ("found mikey");
1694 } else if (g_str_has_prefix (split[j], "uri=")) {
1695 strip_chars (split[j] + 4);
1696 GST_DEBUG ("found uri '%s'", split[j] + 4);
1697 } else if (g_str_has_prefix (split[j], "data=")) {
1700 strip_chars (split[j] + 5);
1701 GST_DEBUG ("found data '%s'", split[j] + 5);
1702 data = g_base64_decode_inplace (split[j] + 5, &size);
1703 handle_mikey_data (client, ctx, data, size);
1713 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
1715 GstRTSPClientPrivate *priv = client->priv;
1718 gchar *transport, *keymgmt;
1719 GstRTSPTransport *ct, *st;
1720 GstRTSPStatusCode code;
1721 GstRTSPSession *session;
1722 GstRTSPStreamTransport *trans;
1724 GstRTSPSessionMedia *sessmedia;
1725 GstRTSPMedia *media;
1726 GstRTSPStream *stream;
1727 GstRTSPState rtspstate;
1728 GstRTSPClientClass *klass;
1729 gchar *path, *control;
1731 gboolean new_session = FALSE;
1737 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1738 path = klass->make_path_from_uri (client, uri);
1740 /* parse the transport */
1742 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
1744 if (res != GST_RTSP_OK)
1747 /* we create the session after parsing stuff so that we don't make
1748 * a session for malformed requests */
1749 if (priv->session_pool == NULL)
1752 session = ctx->session;
1755 g_object_ref (session);
1756 /* get a handle to the configuration of the media in the session, this can
1757 * return NULL if this is a new url to manage in this session. */
1758 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1760 /* we need a new media configuration in this session */
1764 /* we have no session media, find one and manage it */
1765 if (sessmedia == NULL) {
1766 /* get a handle to the configuration of the media in the session */
1767 media = find_media (client, ctx, path, &matched);
1769 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1770 g_object_ref (media);
1772 goto media_not_found;
1774 /* no media, not found then */
1776 goto media_not_found_no_reply;
1778 if (path[matched] == '\0')
1779 goto control_not_found;
1781 /* path is what matched. */
1782 path[matched] = '\0';
1783 /* control is remainder */
1784 control = &path[matched + 1];
1786 /* find the stream now using the control part */
1787 stream = gst_rtsp_media_find_stream (media, control);
1789 goto stream_not_found;
1791 /* now we have a uri identifying a valid media and stream */
1792 ctx->stream = stream;
1795 if (session == NULL) {
1796 /* create a session if this fails we probably reached our session limit or
1798 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1799 goto service_unavailable;
1801 /* make sure this client is closed when the session is closed */
1802 client_watch_session (client, session);
1805 /* signal new session */
1806 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1809 ctx->session = session;
1812 if (!klass->configure_client_media (client, media, stream, ctx))
1813 goto configure_media_failed_no_reply;
1815 gst_rtsp_transport_new (&ct);
1817 /* parse and find a usable supported transport */
1818 if (!parse_transport (transport, stream, ct))
1819 goto unsupported_transports;
1821 /* parse the keymgmt */
1822 if (gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_KEYMGMT,
1823 &keymgmt, 0) == GST_RTSP_OK) {
1824 if (!handle_keymgmt (client, ctx, keymgmt))
1828 if (sessmedia == NULL) {
1829 /* manage the media in our session now, if not done already */
1830 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1831 /* if we stil have no media, error */
1832 if (sessmedia == NULL)
1833 goto sessmedia_unavailable;
1835 /* don't cache media anymore */
1836 clean_cached_media (client, FALSE);
1838 g_object_unref (media);
1841 ctx->sessmedia = sessmedia;
1843 /* update the client transport */
1844 if (!klass->configure_client_transport (client, ctx, ct))
1845 goto unsupported_client_transport;
1847 /* set in the session media transport */
1848 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1852 /* configure the url used to set this transport, this we will use when
1853 * generating the response for the PLAY request */
1854 gst_rtsp_stream_transport_set_url (trans, uri);
1855 /* configure keepalive for this transport */
1856 gst_rtsp_stream_transport_set_keepalive (trans,
1857 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1859 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1860 /* our callbacks to send data on this TCP connection */
1861 gst_rtsp_stream_transport_set_callbacks (trans,
1862 (GstRTSPSendFunc) do_send_data,
1863 (GstRTSPSendFunc) do_send_data, client, NULL);
1865 g_hash_table_insert (priv->transports,
1866 GINT_TO_POINTER (ct->interleaved.min), trans);
1867 g_object_ref (trans);
1868 g_hash_table_insert (priv->transports,
1869 GINT_TO_POINTER (ct->interleaved.max), trans);
1870 g_object_ref (trans);
1873 /* create and serialize the server transport */
1874 st = make_server_transport (client, ctx, ct);
1875 trans_str = gst_rtsp_transport_as_text (st);
1876 gst_rtsp_transport_free (st);
1878 /* construct the response now */
1879 code = GST_RTSP_STS_OK;
1880 gst_rtsp_message_init_response (ctx->response, code,
1881 gst_rtsp_status_as_text (code), ctx->request);
1883 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
1887 send_message (client, ctx, ctx->response, FALSE);
1889 /* update the state */
1890 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1891 switch (rtspstate) {
1892 case GST_RTSP_STATE_PLAYING:
1893 case GST_RTSP_STATE_RECORDING:
1894 case GST_RTSP_STATE_READY:
1895 /* no state change */
1898 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1901 g_object_unref (session);
1904 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
1911 GST_ERROR ("client %p: no uri", client);
1912 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1917 GST_ERROR ("client %p: no transport", client);
1918 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1923 GST_ERROR ("client %p: no session pool configured", client);
1924 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1927 media_not_found_no_reply:
1929 GST_ERROR ("client %p: media '%s' not found", client, path);
1930 /* error reply is already sent */
1935 GST_ERROR ("client %p: media '%s' not found", client, path);
1936 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1941 GST_ERROR ("client %p: no control in path '%s'", client, path);
1942 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1943 g_object_unref (media);
1948 GST_ERROR ("client %p: stream '%s' not found", client, control);
1949 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1950 g_object_unref (media);
1953 service_unavailable:
1955 GST_ERROR ("client %p: can't create session", client);
1956 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1957 g_object_unref (media);
1960 sessmedia_unavailable:
1962 GST_ERROR ("client %p: can't create session media", client);
1963 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1964 g_object_unref (media);
1965 goto cleanup_session;
1967 configure_media_failed_no_reply:
1969 GST_ERROR ("client %p: configure_media failed", client);
1970 /* error reply is already sent */
1971 goto cleanup_session;
1973 unsupported_transports:
1975 GST_ERROR ("client %p: unsupported transports", client);
1976 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1977 goto cleanup_transport;
1979 unsupported_client_transport:
1981 GST_ERROR ("client %p: unsupported client transport", client);
1982 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1983 goto cleanup_transport;
1987 GST_ERROR ("client %p: keymgmt error", client);
1988 send_generic_response (client, GST_RTSP_STS_KEY_MANAGEMENT_FAILURE, ctx);
1989 goto cleanup_transport;
1993 gst_rtsp_transport_free (ct);
1996 gst_rtsp_session_pool_remove (priv->session_pool, session);
1997 g_object_unref (session);
2004 static GstSDPMessage *
2005 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
2007 GstRTSPClientPrivate *priv = client->priv;
2011 guint64 session_id_tmp;
2012 gchar session_id[21];
2014 gst_sdp_message_new (&sdp);
2016 /* some standard things first */
2017 gst_sdp_message_set_version (sdp, "0");
2024 session_id_tmp = (((guint64) g_random_int ()) << 32) | g_random_int ();
2025 g_snprintf (session_id, sizeof (session_id), "%" G_GUINT64_FORMAT,
2028 gst_sdp_message_set_origin (sdp, "-", session_id, "1", "IN", proto,
2031 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
2032 gst_sdp_message_set_information (sdp, "rtsp-server");
2033 gst_sdp_message_add_time (sdp, "0", "0", NULL);
2034 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
2035 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
2036 gst_sdp_message_add_attribute (sdp, "control", "*");
2038 info.is_ipv6 = priv->is_ipv6;
2039 info.server_ip = priv->server_ip;
2041 /* create an SDP for the media object */
2042 if (!gst_rtsp_media_setup_sdp (media, sdp, &info))
2050 GST_ERROR ("client %p: could not create SDP", client);
2051 gst_sdp_message_free (sdp);
2056 /* for the describe we must generate an SDP */
2058 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
2060 GstRTSPClientPrivate *priv = client->priv;
2065 GstRTSPMedia *media;
2066 GstRTSPClientClass *klass;
2068 klass = GST_RTSP_CLIENT_GET_CLASS (client);
2073 /* check what kind of format is accepted, we don't really do anything with it
2074 * and always return SDP for now. */
2079 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
2081 if (res == GST_RTSP_ENOTIMPL)
2084 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
2088 if (!priv->mount_points)
2089 goto no_mount_points;
2091 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
2094 /* find the media object for the uri */
2095 if (!(media = find_media (client, ctx, path, NULL)))
2098 /* create an SDP for the media object on this client */
2099 if (!(sdp = klass->create_sdp (client, media)))
2102 /* we suspend after the describe */
2103 gst_rtsp_media_suspend (media);
2104 g_object_unref (media);
2106 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2107 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2109 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
2112 /* content base for some clients that might screw up creating the setup uri */
2113 str = make_base_url (client, ctx->uri, path);
2116 GST_INFO ("adding content-base: %s", str);
2117 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE, str);
2119 /* add SDP to the response body */
2120 str = gst_sdp_message_as_text (sdp);
2121 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
2122 gst_sdp_message_free (sdp);
2124 send_message (client, ctx, ctx->response, FALSE);
2126 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
2134 GST_ERROR ("client %p: no uri", client);
2135 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2140 GST_ERROR ("client %p: no mount points configured", client);
2141 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2146 GST_ERROR ("client %p: can't find path for url", client);
2147 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2152 GST_ERROR ("client %p: no media", client);
2154 /* error reply is already sent */
2159 GST_ERROR ("client %p: can't create SDP", client);
2160 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2162 g_object_unref (media);
2168 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
2170 GstRTSPMethod options;
2173 options = GST_RTSP_DESCRIBE |
2178 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
2180 str = gst_rtsp_options_as_text (options);
2182 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2183 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2185 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
2188 send_message (client, ctx, ctx->response, FALSE);
2190 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
2196 /* remove duplicate and trailing '/' */
2198 sanitize_uri (GstRTSPUrl * uri)
2202 gboolean have_slash, prev_slash;
2204 s = d = uri->abspath;
2205 len = strlen (uri->abspath);
2209 for (i = 0; i < len; i++) {
2210 have_slash = s[i] == '/';
2212 if (!have_slash || !prev_slash)
2214 prev_slash = have_slash;
2216 len = d - uri->abspath;
2217 /* don't remove the first slash if that's the only thing left */
2218 if (len > 1 && *(d - 1) == '/')
2223 /* is called when the session is removed from its session pool. */
2225 client_session_removed (GstRTSPSessionPool * pool, GstRTSPSession * session,
2226 GstRTSPClient * client)
2228 GstRTSPClientPrivate *priv = client->priv;
2230 GST_INFO ("client %p: session %p removed", client, session);
2232 g_mutex_lock (&priv->lock);
2233 if (priv->watch != NULL)
2234 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 0);
2235 client_unwatch_session (client, session, NULL);
2236 if (priv->watch != NULL)
2237 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
2238 g_mutex_unlock (&priv->lock);
2241 /* Returns TRUE if there are no Require headers, otherwise returns FALSE
2242 * and also returns a newly-allocated string of (comma-separated) unsupported
2243 * options in the unsupported_reqs variable .
2245 * There may be multiple Require headers, but we must send one single
2246 * Unsupported header with all the unsupported options as response. If
2247 * an incoming Require header contained a comma-separated list of options
2248 * GstRtspConnection will already have split that list up into multiple
2251 * TODO: allow the application to decide what features are supported
2254 check_request_requirements (GstRTSPMessage * msg, gchar ** unsupported_reqs)
2257 GPtrArray *arr = NULL;
2263 res = gst_rtsp_message_get_header (msg, GST_RTSP_HDR_REQUIRE, &reqs, i++);
2265 if (res == GST_RTSP_ENOTIMPL)
2269 arr = g_ptr_array_new_with_free_func ((GDestroyNotify) g_free);
2271 g_ptr_array_add (arr, g_strdup (reqs));
2275 /* if we don't have any Require headers at all, all is fine */
2279 /* otherwise we've now processed at all the Require headers */
2280 g_ptr_array_add (arr, NULL);
2282 /* for now we don't commit to supporting anything, so will just report
2283 * all of the required options as unsupported */
2284 *unsupported_reqs = g_strjoinv (", ", (gchar **) arr->pdata);
2286 g_ptr_array_unref (arr);
2291 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
2293 GstRTSPClientPrivate *priv = client->priv;
2294 GstRTSPMethod method;
2295 const gchar *uristr;
2296 GstRTSPUrl *uri = NULL;
2297 GstRTSPVersion version;
2299 GstRTSPSession *session = NULL;
2300 GstRTSPContext sctx = { NULL }, *ctx;
2301 GstRTSPMessage response = { 0 };
2302 gchar *unsupported_reqs = NULL;
2305 if (!(ctx = gst_rtsp_context_get_current ())) {
2307 ctx->auth = priv->auth;
2308 gst_rtsp_context_push_current (ctx);
2311 ctx->conn = priv->connection;
2312 ctx->client = client;
2313 ctx->request = request;
2314 ctx->response = &response;
2316 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2317 gst_rtsp_message_dump (request);
2320 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
2322 GST_INFO ("client %p: received a request %s %s %s", client,
2323 gst_rtsp_method_as_text (method), uristr,
2324 gst_rtsp_version_as_text (version));
2326 /* we can only handle 1.0 requests */
2327 if (version != GST_RTSP_VERSION_1_0)
2330 ctx->method = method;
2332 /* we always try to parse the url first */
2333 if (strcmp (uristr, "*") == 0) {
2334 /* special case where we have * as uri, keep uri = NULL */
2335 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
2336 /* check if the uristr is an absolute path <=> scheme and host information
2340 scheme = g_uri_parse_scheme (uristr);
2341 if (scheme == NULL && g_str_has_prefix (uristr, "/")) {
2342 gchar *absolute_uristr = NULL;
2344 GST_WARNING_OBJECT (client, "request doesn't contain absolute url");
2345 if (priv->server_ip == NULL) {
2346 GST_WARNING_OBJECT (client, "host information missing");
2351 g_strdup_printf ("rtsp://%s%s", priv->server_ip, uristr);
2353 GST_DEBUG_OBJECT (client, "absolute url: %s", absolute_uristr);
2354 if (gst_rtsp_url_parse (absolute_uristr, &uri) != GST_RTSP_OK) {
2355 g_free (absolute_uristr);
2358 g_free (absolute_uristr);
2365 /* get the session if there is any */
2366 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
2367 if (res == GST_RTSP_OK) {
2368 if (priv->session_pool == NULL)
2371 /* we had a session in the request, find it again */
2372 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2373 goto session_not_found;
2375 /* we add the session to the client list of watched sessions. When a session
2376 * disappears because it times out, we will be notified. If all sessions are
2377 * gone, we will close the connection */
2378 client_watch_session (client, session);
2381 /* sanitize the uri */
2385 ctx->session = session;
2387 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
2388 goto not_authorized;
2390 /* handle any 'Require' headers */
2391 if (!check_request_requirements (ctx->request, &unsupported_reqs))
2392 goto unsupported_requirement;
2394 /* the backlog must be unlimited while processing requests.
2395 * the causes of this are two cases of deadlocks while streaming over TCP:
2397 * 1. consider the scenario where the media pipeline's streaming thread
2398 * is blocking in the appsink (taking the appsink's preroll lock) because
2399 * the backlog is full. when a PAUSE request is received by the RTSP
2400 * client thread then the the state of the session media ought to change
2401 * to PAUSED. while most elements in the pipeline can change state this
2402 * can never happen for the appsink since its preroll lock is taken by
2405 * 2. consider the scenario where the media pipeline's streaming thread
2406 * is blocking in the appsink new_sample callback (taking the send lock
2407 * in RTSP client) because the backlog is full. when e.g. a GET request
2408 * is received by the RTSP client thread then a response ought to be sent
2409 * but this can never happen since it requires taking the send lock
2410 * already taken by another thread.
2412 * the reason that the backlog is never emptied is that the source used
2413 * for dequeing messages from the backlog is never dispatched because it
2414 * is attached to the same mainloop as the source receving RTSP requests and
2415 * therefore run by the RTSP client thread which is alreayd blocking.
2417 * without significant changes the easiest way to cope with this is to
2418 * not block indefinitely when the backlog is full, but rather let the
2419 * backlog grow in size. this in effect means that there can not be any
2420 * upper boundary on its size.
2422 if (priv->watch != NULL)
2423 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 0);
2425 /* now see what is asked and dispatch to a dedicated handler */
2427 case GST_RTSP_OPTIONS:
2428 handle_options_request (client, ctx);
2430 case GST_RTSP_DESCRIBE:
2431 handle_describe_request (client, ctx);
2433 case GST_RTSP_SETUP:
2434 handle_setup_request (client, ctx);
2437 handle_play_request (client, ctx);
2439 case GST_RTSP_PAUSE:
2440 handle_pause_request (client, ctx);
2442 case GST_RTSP_TEARDOWN:
2443 handle_teardown_request (client, ctx);
2445 case GST_RTSP_SET_PARAMETER:
2446 handle_set_param_request (client, ctx);
2448 case GST_RTSP_GET_PARAMETER:
2449 handle_get_param_request (client, ctx);
2451 case GST_RTSP_ANNOUNCE:
2452 case GST_RTSP_RECORD:
2453 case GST_RTSP_REDIRECT:
2454 if (priv->watch != NULL)
2455 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
2456 goto not_implemented;
2457 case GST_RTSP_INVALID:
2459 if (priv->watch != NULL)
2460 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
2464 if (priv->watch != NULL)
2465 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
2469 gst_rtsp_context_pop_current (ctx);
2471 g_object_unref (session);
2473 gst_rtsp_url_free (uri);
2479 GST_ERROR ("client %p: version %d not supported", client, version);
2480 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
2486 GST_ERROR ("client %p: bad request", client);
2487 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2492 GST_ERROR ("client %p: no pool configured", client);
2493 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2498 GST_ERROR ("client %p: session not found", client);
2499 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2504 GST_ERROR ("client %p: not allowed", client);
2505 /* error reply is already sent */
2508 unsupported_requirement:
2510 GST_ERROR ("client %p: Required option is not supported (%s)", client,
2512 send_option_not_supported_response (client, ctx, unsupported_reqs);
2513 g_free (unsupported_reqs);
2518 GST_ERROR ("client %p: method %d not implemented", client, method);
2519 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
2526 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
2528 GstRTSPClientPrivate *priv = client->priv;
2530 GstRTSPSession *session = NULL;
2531 GstRTSPContext sctx = { NULL }, *ctx;
2534 if (!(ctx = gst_rtsp_context_get_current ())) {
2536 ctx->auth = priv->auth;
2537 gst_rtsp_context_push_current (ctx);
2540 ctx->conn = priv->connection;
2541 ctx->client = client;
2542 ctx->request = NULL;
2544 ctx->method = GST_RTSP_INVALID;
2545 ctx->response = response;
2547 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2548 gst_rtsp_message_dump (response);
2551 GST_INFO ("client %p: received a response", client);
2553 /* get the session if there is any */
2555 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
2556 if (res == GST_RTSP_OK) {
2557 if (priv->session_pool == NULL)
2560 /* we had a session in the request, find it again */
2561 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2562 goto session_not_found;
2564 /* we add the session to the client list of watched sessions. When a session
2565 * disappears because it times out, we will be notified. If all sessions are
2566 * gone, we will close the connection */
2567 client_watch_session (client, session);
2570 ctx->session = session;
2572 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
2577 gst_rtsp_context_pop_current (ctx);
2579 g_object_unref (session);
2584 GST_ERROR ("client %p: no pool configured", client);
2589 GST_ERROR ("client %p: session not found", client);
2595 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2597 GstRTSPClientPrivate *priv = client->priv;
2603 GstRTSPStreamTransport *trans;
2605 /* find the stream for this message */
2606 res = gst_rtsp_message_parse_data (message, &channel);
2607 if (res != GST_RTSP_OK)
2610 gst_rtsp_message_get_body (message, &data, &size);
2612 goto invalid_length;
2614 gst_rtsp_message_steal_body (message, &data, &size);
2616 /* Strip trailing \0 */
2617 buffer = gst_buffer_new_wrapped (data, size - 1);
2620 g_hash_table_lookup (priv->transports, GINT_TO_POINTER ((gint) channel));
2622 /* dispatch to the stream based on the channel number */
2623 gst_rtsp_stream_transport_recv_data (trans, channel, buffer);
2625 gst_buffer_unref (buffer);
2629 GST_DEBUG ("client %p: Short message received, ignoring", client);
2635 * gst_rtsp_client_set_session_pool:
2636 * @client: a #GstRTSPClient
2637 * @pool: (transfer none): a #GstRTSPSessionPool
2639 * Set @pool as the sessionpool for @client which it will use to find
2640 * or allocate sessions. the sessionpool is usually inherited from the server
2641 * that created the client but can be overridden later.
2644 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2645 GstRTSPSessionPool * pool)
2647 GstRTSPSessionPool *old;
2648 GstRTSPClientPrivate *priv;
2650 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2652 priv = client->priv;
2655 g_object_ref (pool);
2657 g_mutex_lock (&priv->lock);
2658 old = priv->session_pool;
2659 priv->session_pool = pool;
2661 if (priv->session_removed_id) {
2662 g_signal_handler_disconnect (old, priv->session_removed_id);
2663 priv->session_removed_id = 0;
2665 g_mutex_unlock (&priv->lock);
2667 /* FIXME, should remove all sessions from the old pool for this client */
2669 g_object_unref (old);
2673 * gst_rtsp_client_get_session_pool:
2674 * @client: a #GstRTSPClient
2676 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2678 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2680 GstRTSPSessionPool *
2681 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2683 GstRTSPClientPrivate *priv;
2684 GstRTSPSessionPool *result;
2686 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2688 priv = client->priv;
2690 g_mutex_lock (&priv->lock);
2691 if ((result = priv->session_pool))
2692 g_object_ref (result);
2693 g_mutex_unlock (&priv->lock);
2699 * gst_rtsp_client_set_mount_points:
2700 * @client: a #GstRTSPClient
2701 * @mounts: (transfer none): a #GstRTSPMountPoints
2703 * Set @mounts as the mount points for @client which it will use to map urls
2704 * to media streams. These mount points are usually inherited from the server that
2705 * created the client but can be overriden later.
2708 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2709 GstRTSPMountPoints * mounts)
2711 GstRTSPClientPrivate *priv;
2712 GstRTSPMountPoints *old;
2714 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2716 priv = client->priv;
2719 g_object_ref (mounts);
2721 g_mutex_lock (&priv->lock);
2722 old = priv->mount_points;
2723 priv->mount_points = mounts;
2724 g_mutex_unlock (&priv->lock);
2727 g_object_unref (old);
2731 * gst_rtsp_client_get_mount_points:
2732 * @client: a #GstRTSPClient
2734 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2736 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2738 GstRTSPMountPoints *
2739 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2741 GstRTSPClientPrivate *priv;
2742 GstRTSPMountPoints *result;
2744 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2746 priv = client->priv;
2748 g_mutex_lock (&priv->lock);
2749 if ((result = priv->mount_points))
2750 g_object_ref (result);
2751 g_mutex_unlock (&priv->lock);
2757 * gst_rtsp_client_set_auth:
2758 * @client: a #GstRTSPClient
2759 * @auth: (transfer none): a #GstRTSPAuth
2761 * configure @auth to be used as the authentication manager of @client.
2764 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2766 GstRTSPClientPrivate *priv;
2769 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2771 priv = client->priv;
2774 g_object_ref (auth);
2776 g_mutex_lock (&priv->lock);
2779 g_mutex_unlock (&priv->lock);
2782 g_object_unref (old);
2787 * gst_rtsp_client_get_auth:
2788 * @client: a #GstRTSPClient
2790 * Get the #GstRTSPAuth used as the authentication manager of @client.
2792 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2796 gst_rtsp_client_get_auth (GstRTSPClient * client)
2798 GstRTSPClientPrivate *priv;
2799 GstRTSPAuth *result;
2801 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2803 priv = client->priv;
2805 g_mutex_lock (&priv->lock);
2806 if ((result = priv->auth))
2807 g_object_ref (result);
2808 g_mutex_unlock (&priv->lock);
2814 * gst_rtsp_client_set_thread_pool:
2815 * @client: a #GstRTSPClient
2816 * @pool: (transfer none): a #GstRTSPThreadPool
2818 * configure @pool to be used as the thread pool of @client.
2821 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2822 GstRTSPThreadPool * pool)
2824 GstRTSPClientPrivate *priv;
2825 GstRTSPThreadPool *old;
2827 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2829 priv = client->priv;
2832 g_object_ref (pool);
2834 g_mutex_lock (&priv->lock);
2835 old = priv->thread_pool;
2836 priv->thread_pool = pool;
2837 g_mutex_unlock (&priv->lock);
2840 g_object_unref (old);
2844 * gst_rtsp_client_get_thread_pool:
2845 * @client: a #GstRTSPClient
2847 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2849 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2853 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2855 GstRTSPClientPrivate *priv;
2856 GstRTSPThreadPool *result;
2858 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2860 priv = client->priv;
2862 g_mutex_lock (&priv->lock);
2863 if ((result = priv->thread_pool))
2864 g_object_ref (result);
2865 g_mutex_unlock (&priv->lock);
2871 * gst_rtsp_client_set_connection:
2872 * @client: a #GstRTSPClient
2873 * @conn: (transfer full): a #GstRTSPConnection
2875 * Set the #GstRTSPConnection of @client. This function takes ownership of
2878 * Returns: %TRUE on success.
2881 gst_rtsp_client_set_connection (GstRTSPClient * client,
2882 GstRTSPConnection * conn)
2884 GstRTSPClientPrivate *priv;
2885 GSocket *read_socket;
2886 GSocketAddress *address;
2888 GError *error = NULL;
2890 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2891 g_return_val_if_fail (conn != NULL, FALSE);
2893 priv = client->priv;
2895 read_socket = gst_rtsp_connection_get_read_socket (conn);
2897 if (!(address = g_socket_get_local_address (read_socket, &error)))
2900 g_free (priv->server_ip);
2901 /* keep the original ip that the client connected to */
2902 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2903 GInetAddress *iaddr;
2905 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2907 /* socket might be ipv6 but adress still ipv4 */
2908 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2909 priv->server_ip = g_inet_address_to_string (iaddr);
2910 g_object_unref (address);
2912 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2913 priv->server_ip = g_strdup ("unknown");
2916 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2917 priv->server_ip, priv->is_ipv6);
2919 url = gst_rtsp_connection_get_url (conn);
2920 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2922 priv->connection = conn;
2929 GST_ERROR ("could not get local address %s", error->message);
2930 g_error_free (error);
2936 * gst_rtsp_client_get_connection:
2937 * @client: a #GstRTSPClient
2939 * Get the #GstRTSPConnection of @client.
2941 * Returns: (transfer none): the #GstRTSPConnection of @client.
2942 * The connection object returned remains valid until the client is freed.
2945 gst_rtsp_client_get_connection (GstRTSPClient * client)
2947 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2949 return client->priv->connection;
2953 * gst_rtsp_client_set_send_func:
2954 * @client: a #GstRTSPClient
2955 * @func: (scope notified): a #GstRTSPClientSendFunc
2956 * @user_data: (closure): user data passed to @func
2957 * @notify: (allow-none): called when @user_data is no longer in use
2959 * Set @func as the callback that will be called when a new message needs to be
2960 * sent to the client. @user_data is passed to @func and @notify is called when
2961 * @user_data is no longer in use.
2963 * By default, the client will send the messages on the #GstRTSPConnection that
2964 * was configured with gst_rtsp_client_attach() was called.
2967 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2968 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2970 GstRTSPClientPrivate *priv;
2971 GDestroyNotify old_notify;
2974 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2976 priv = client->priv;
2978 g_mutex_lock (&priv->send_lock);
2979 priv->send_func = func;
2980 old_notify = priv->send_notify;
2981 old_data = priv->send_data;
2982 priv->send_notify = notify;
2983 priv->send_data = user_data;
2984 g_mutex_unlock (&priv->send_lock);
2987 old_notify (old_data);
2991 * gst_rtsp_client_handle_message:
2992 * @client: a #GstRTSPClient
2993 * @message: (transfer none): an #GstRTSPMessage
2995 * Let the client handle @message.
2997 * Returns: a #GstRTSPResult.
3000 gst_rtsp_client_handle_message (GstRTSPClient * client,
3001 GstRTSPMessage * message)
3003 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
3004 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3006 switch (message->type) {
3007 case GST_RTSP_MESSAGE_REQUEST:
3008 handle_request (client, message);
3010 case GST_RTSP_MESSAGE_RESPONSE:
3011 handle_response (client, message);
3013 case GST_RTSP_MESSAGE_DATA:
3014 handle_data (client, message);
3023 * gst_rtsp_client_send_message:
3024 * @client: a #GstRTSPClient
3025 * @session: (allow-none) (transfer none): a #GstRTSPSession to send
3026 * the message to or %NULL
3027 * @message: (transfer none): The #GstRTSPMessage to send
3029 * Send a message message to the remote end. @message must be a
3030 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
3033 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
3034 GstRTSPMessage * message)
3036 GstRTSPContext sctx = { NULL }
3038 GstRTSPClientPrivate *priv;
3040 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
3041 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3042 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
3043 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
3045 priv = client->priv;
3047 if (!(ctx = gst_rtsp_context_get_current ())) {
3049 ctx->auth = priv->auth;
3050 gst_rtsp_context_push_current (ctx);
3053 ctx->conn = priv->connection;
3054 ctx->client = client;
3055 ctx->session = session;
3057 send_message (client, ctx, message, FALSE);
3060 gst_rtsp_context_pop_current (ctx);
3065 static GstRTSPResult
3066 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
3067 gboolean close, gpointer user_data)
3069 GstRTSPClientPrivate *priv = client->priv;
3077 /* send the response and store the seq number so we can wait until it's
3078 * written to the client to close the connection */
3080 gst_rtsp_watch_send_message (priv->watch, message,
3081 close ? &priv->close_seq : NULL);
3082 if (ret == GST_RTSP_OK)
3085 if (ret != GST_RTSP_ENOMEM)
3089 if (priv->drop_backlog)
3092 /* queue was full, wait for more space */
3093 GST_DEBUG_OBJECT (client, "waiting for backlog");
3094 ret = gst_rtsp_watch_wait_backlog (priv->watch, &time);
3095 GST_DEBUG_OBJECT (client, "Resend due to backlog full");
3096 } while (ret != GST_RTSP_EINTR);
3103 GST_DEBUG_OBJECT (client, "got error %d", ret);
3108 static GstRTSPResult
3109 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
3112 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
3115 static GstRTSPResult
3116 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
3118 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3119 GstRTSPClientPrivate *priv = client->priv;
3121 if (priv->close_seq && priv->close_seq == cseq) {
3122 GST_INFO ("client %p: send close message", client);
3123 priv->close_seq = 0;
3124 gst_rtsp_client_close (client);
3130 static GstRTSPResult
3131 closed (GstRTSPWatch * watch, gpointer user_data)
3133 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3134 GstRTSPClientPrivate *priv = client->priv;
3135 const gchar *tunnelid;
3137 GST_INFO ("client %p: connection closed", client);
3139 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
3140 g_mutex_lock (&tunnels_lock);
3141 /* remove from tunnelids */
3142 g_hash_table_remove (tunnels, tunnelid);
3143 g_mutex_unlock (&tunnels_lock);
3146 gst_rtsp_watch_set_flushing (watch, TRUE);
3147 g_mutex_lock (&priv->watch_lock);
3148 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
3149 g_mutex_unlock (&priv->watch_lock);
3154 static GstRTSPResult
3155 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
3157 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3160 str = gst_rtsp_strresult (result);
3161 GST_INFO ("client %p: received an error %s", client, str);
3167 static GstRTSPResult
3168 error_full (GstRTSPWatch * watch, GstRTSPResult result,
3169 GstRTSPMessage * message, guint id, gpointer user_data)
3171 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3174 str = gst_rtsp_strresult (result);
3176 ("client %p: error when handling message %p with id %d: %s",
3177 client, message, id, str);
3184 remember_tunnel (GstRTSPClient * client)
3186 GstRTSPClientPrivate *priv = client->priv;
3187 const gchar *tunnelid;
3189 /* store client in the pending tunnels */
3190 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
3191 if (tunnelid == NULL)
3194 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
3196 /* we can't have two clients connecting with the same tunnelid */
3197 g_mutex_lock (&tunnels_lock);
3198 if (g_hash_table_lookup (tunnels, tunnelid))
3199 goto tunnel_existed;
3201 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
3202 g_mutex_unlock (&tunnels_lock);
3209 GST_ERROR ("client %p: no tunnelid provided", client);
3214 g_mutex_unlock (&tunnels_lock);
3215 GST_ERROR ("client %p: tunnel session %s already existed", client,
3221 static GstRTSPResult
3222 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
3224 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3225 GstRTSPClientPrivate *priv = client->priv;
3227 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
3230 /* ignore error, it'll only be a problem when the client does a POST again */
3231 remember_tunnel (client);
3237 handle_tunnel (GstRTSPClient * client)
3239 GstRTSPClientPrivate *priv = client->priv;
3240 GstRTSPClient *oclient;
3241 GstRTSPClientPrivate *opriv;
3242 const gchar *tunnelid;
3244 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
3245 if (tunnelid == NULL)
3248 /* check for previous tunnel */
3249 g_mutex_lock (&tunnels_lock);
3250 oclient = g_hash_table_lookup (tunnels, tunnelid);
3252 if (oclient == NULL) {
3253 /* no previous tunnel, remember tunnel */
3254 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
3255 g_mutex_unlock (&tunnels_lock);
3257 GST_INFO ("client %p: no previous tunnel found, remembering tunnel (%p)",
3258 client, priv->connection);
3260 /* merge both tunnels into the first client */
3261 /* remove the old client from the table. ref before because removing it will
3262 * remove the ref to it. */
3263 g_object_ref (oclient);
3264 g_hash_table_remove (tunnels, tunnelid);
3265 g_mutex_unlock (&tunnels_lock);
3267 opriv = oclient->priv;
3269 g_mutex_lock (&opriv->watch_lock);
3270 if (opriv->watch == NULL)
3273 GST_INFO ("client %p: found previous tunnel %p (old %p, new %p)", client,
3274 oclient, opriv->connection, priv->connection);
3276 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
3277 gst_rtsp_watch_reset (priv->watch);
3278 gst_rtsp_watch_reset (opriv->watch);
3279 g_mutex_unlock (&opriv->watch_lock);
3280 g_object_unref (oclient);
3282 /* the old client owns the tunnel now, the new one will be freed */
3283 g_source_destroy ((GSource *) priv->watch);
3285 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
3293 GST_ERROR ("client %p: no tunnelid provided", client);
3298 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
3299 g_mutex_unlock (&opriv->watch_lock);
3300 g_object_unref (oclient);
3305 static GstRTSPStatusCode
3306 tunnel_get (GstRTSPWatch * watch, gpointer user_data)
3308 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3310 GST_INFO ("client %p: tunnel get (connection %p)", client,
3311 client->priv->connection);
3313 if (!handle_tunnel (client)) {
3314 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
3317 return GST_RTSP_STS_OK;
3320 static GstRTSPResult
3321 tunnel_post (GstRTSPWatch * watch, gpointer user_data)
3323 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3325 GST_INFO ("client %p: tunnel post (connection %p)", client,
3326 client->priv->connection);
3328 if (!handle_tunnel (client)) {
3329 return GST_RTSP_ERROR;
3335 static GstRTSPResult
3336 tunnel_http_response (GstRTSPWatch * watch, GstRTSPMessage * request,
3337 GstRTSPMessage * response, gpointer user_data)
3339 GstRTSPClientClass *klass;
3341 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3342 klass = GST_RTSP_CLIENT_GET_CLASS (client);
3344 if (klass->tunnel_http_response) {
3345 klass->tunnel_http_response (client, request, response);
3351 static GstRTSPWatchFuncs watch_funcs = {
3360 tunnel_http_response
3364 client_watch_notify (GstRTSPClient * client)
3366 GstRTSPClientPrivate *priv = client->priv;
3368 GST_INFO ("client %p: watch destroyed", client);
3370 /* remove all sessions and so drop the extra client ref */
3371 gst_rtsp_client_session_filter (client, cleanup_session, NULL);
3372 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
3373 g_object_unref (client);
3377 * gst_rtsp_client_attach:
3378 * @client: a #GstRTSPClient
3379 * @context: (allow-none): a #GMainContext
3381 * Attaches @client to @context. When the mainloop for @context is run, the
3382 * client will be dispatched. When @context is %NULL, the default context will be
3385 * This function should be called when the client properties and urls are fully
3386 * configured and the client is ready to start.
3388 * Returns: the ID (greater than 0) for the source within the GMainContext.
3391 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
3393 GstRTSPClientPrivate *priv;
3396 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
3397 priv = client->priv;
3398 g_return_val_if_fail (priv->connection != NULL, 0);
3399 g_return_val_if_fail (priv->watch == NULL, 0);
3401 /* make sure noone will free the context before the watch is destroyed */
3402 priv->watch_context = g_main_context_ref (context);
3404 /* create watch for the connection and attach */
3405 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
3406 g_object_ref (client), (GDestroyNotify) client_watch_notify);
3407 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
3408 (GDestroyNotify) gst_rtsp_watch_unref);
3410 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
3412 GST_INFO ("client %p: attaching to context %p", client, context);
3413 res = gst_rtsp_watch_attach (priv->watch, context);
3419 * gst_rtsp_client_session_filter:
3420 * @client: a #GstRTSPClient
3421 * @func: (scope call) (allow-none): a callback
3422 * @user_data: user data passed to @func
3424 * Call @func for each session managed by @client. The result value of @func
3425 * determines what happens to the session. @func will be called with @client
3426 * locked so no further actions on @client can be performed from @func.
3428 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
3431 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
3433 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
3434 * will also be added with an additional ref to the result #GList of this
3437 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each session.
3439 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
3440 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3441 * element in the #GList should be unreffed before the list is freed.
3444 gst_rtsp_client_session_filter (GstRTSPClient * client,
3445 GstRTSPClientSessionFilterFunc func, gpointer user_data)
3447 GstRTSPClientPrivate *priv;
3448 GList *result, *walk, *next;
3449 GHashTable *visited;
3452 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
3454 priv = client->priv;
3458 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
3460 g_mutex_lock (&priv->lock);
3462 cookie = priv->sessions_cookie;
3463 for (walk = priv->sessions; walk; walk = next) {
3464 GstRTSPSession *sess = walk->data;
3465 GstRTSPFilterResult res;
3468 next = g_list_next (walk);
3471 /* only visit each session once */
3472 if (g_hash_table_contains (visited, sess))
3475 g_hash_table_add (visited, g_object_ref (sess));
3476 g_mutex_unlock (&priv->lock);
3478 res = func (client, sess, user_data);
3480 g_mutex_lock (&priv->lock);
3482 res = GST_RTSP_FILTER_REF;
3484 changed = (cookie != priv->sessions_cookie);
3487 case GST_RTSP_FILTER_REMOVE:
3488 /* stop watching the session and pretend it went away, if the list was
3489 * changed, we can't use the current list position, try to see if we
3490 * still have the session */
3491 client_unwatch_session (client, sess, changed ? NULL : walk);
3492 cookie = priv->sessions_cookie;
3494 case GST_RTSP_FILTER_REF:
3495 result = g_list_prepend (result, g_object_ref (sess));
3497 case GST_RTSP_FILTER_KEEP:
3504 g_mutex_unlock (&priv->lock);
3507 g_hash_table_unref (visited);