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 */
62 GstRTSPConnection *connection;
64 GMainContext *watch_context;
69 GstRTSPClientSendFunc send_func; /* protected by send_lock */
70 gpointer send_data; /* protected by send_lock */
71 GDestroyNotify send_notify; /* protected by send_lock */
73 GstRTSPSessionPool *session_pool;
74 gulong session_removed_id;
75 GstRTSPMountPoints *mount_points;
77 GstRTSPThreadPool *thread_pool;
79 /* used to cache the media in the last requested DESCRIBE so that
80 * we can pick it up in the next SETUP immediately */
86 guint sessions_cookie;
88 gboolean drop_backlog;
91 static GMutex tunnels_lock;
92 static GHashTable *tunnels; /* protected by tunnels_lock */
94 #define DEFAULT_SESSION_POOL NULL
95 #define DEFAULT_MOUNT_POINTS NULL
96 #define DEFAULT_DROP_BACKLOG TRUE
111 SIGNAL_OPTIONS_REQUEST,
112 SIGNAL_DESCRIBE_REQUEST,
113 SIGNAL_SETUP_REQUEST,
115 SIGNAL_PAUSE_REQUEST,
116 SIGNAL_TEARDOWN_REQUEST,
117 SIGNAL_SET_PARAMETER_REQUEST,
118 SIGNAL_GET_PARAMETER_REQUEST,
119 SIGNAL_HANDLE_RESPONSE,
124 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
125 #define GST_CAT_DEFAULT rtsp_client_debug
127 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
129 static void gst_rtsp_client_get_property (GObject * object, guint propid,
130 GValue * value, GParamSpec * pspec);
131 static void gst_rtsp_client_set_property (GObject * object, guint propid,
132 const GValue * value, GParamSpec * pspec);
133 static void gst_rtsp_client_finalize (GObject * obj);
135 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
136 static void unlink_session_transports (GstRTSPClient * client,
137 GstRTSPSession * session, GstRTSPSessionMedia * sessmedia);
138 static gboolean default_configure_client_media (GstRTSPClient * client,
139 GstRTSPMedia * media, GstRTSPStream * stream, GstRTSPContext * ctx);
140 static gboolean default_configure_client_transport (GstRTSPClient * client,
141 GstRTSPContext * ctx, GstRTSPTransport * ct);
142 static GstRTSPResult default_params_set (GstRTSPClient * client,
143 GstRTSPContext * ctx);
144 static GstRTSPResult default_params_get (GstRTSPClient * client,
145 GstRTSPContext * ctx);
146 static gchar *default_make_path_from_uri (GstRTSPClient * client,
147 const GstRTSPUrl * uri);
148 static void client_session_removed (GstRTSPSessionPool * pool,
149 GstRTSPSession * session, GstRTSPClient * client);
151 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
154 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
156 GObjectClass *gobject_class;
158 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
160 gobject_class = G_OBJECT_CLASS (klass);
162 gobject_class->get_property = gst_rtsp_client_get_property;
163 gobject_class->set_property = gst_rtsp_client_set_property;
164 gobject_class->finalize = gst_rtsp_client_finalize;
166 klass->create_sdp = create_sdp;
167 klass->configure_client_media = default_configure_client_media;
168 klass->configure_client_transport = default_configure_client_transport;
169 klass->params_set = default_params_set;
170 klass->params_get = default_params_get;
171 klass->make_path_from_uri = default_make_path_from_uri;
173 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
174 g_param_spec_object ("session-pool", "Session Pool",
175 "The session pool to use for client session",
176 GST_TYPE_RTSP_SESSION_POOL,
177 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
179 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
180 g_param_spec_object ("mount-points", "Mount Points",
181 "The mount points to use for client session",
182 GST_TYPE_RTSP_MOUNT_POINTS,
183 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
185 g_object_class_install_property (gobject_class, PROP_DROP_BACKLOG,
186 g_param_spec_boolean ("drop-backlog", "Drop Backlog",
187 "Drop data when the backlog queue is full",
188 DEFAULT_DROP_BACKLOG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
190 gst_rtsp_client_signals[SIGNAL_CLOSED] =
191 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
192 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
193 g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
195 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
196 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
197 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
198 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
200 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
201 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
202 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
203 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
204 GST_TYPE_RTSP_CONTEXT);
206 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
207 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
208 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
209 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
210 GST_TYPE_RTSP_CONTEXT);
212 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
213 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
214 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
215 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
216 GST_TYPE_RTSP_CONTEXT);
218 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
219 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
220 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
221 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
222 GST_TYPE_RTSP_CONTEXT);
224 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
225 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
226 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
227 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
228 GST_TYPE_RTSP_CONTEXT);
230 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
231 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
232 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
233 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
234 GST_TYPE_RTSP_CONTEXT);
236 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
237 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
238 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
239 set_parameter_request), NULL, NULL, g_cclosure_marshal_generic,
240 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
242 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
243 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
244 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
245 get_parameter_request), NULL, NULL, g_cclosure_marshal_generic,
246 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
248 gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE] =
249 g_signal_new ("handle-response", G_TYPE_FROM_CLASS (klass),
250 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
251 handle_response), NULL, NULL, g_cclosure_marshal_generic,
252 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
255 * GstRTSPClient::send-message:
256 * @client: The RTSP client
257 * @session: (type GstRtspServer.RTSPSession): The session
258 * @message: (type GstRtsp.RTSPMessage): The message
260 gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE] =
261 g_signal_new ("send-message", G_TYPE_FROM_CLASS (klass),
262 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
263 G_TYPE_NONE, 2, GST_TYPE_RTSP_CONTEXT, G_TYPE_POINTER);
266 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
267 g_mutex_init (&tunnels_lock);
269 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
273 gst_rtsp_client_init (GstRTSPClient * client)
275 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
279 g_mutex_init (&priv->lock);
280 g_mutex_init (&priv->send_lock);
282 priv->drop_backlog = DEFAULT_DROP_BACKLOG;
285 static GstRTSPFilterResult
286 filter_session_media (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
289 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
291 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
292 unlink_session_transports (client, sess, sessmedia);
294 /* unmanage the media in the session */
295 return GST_RTSP_FILTER_REMOVE;
299 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
301 GstRTSPClientPrivate *priv = client->priv;
303 g_mutex_lock (&priv->lock);
304 /* check if we already know about this session */
305 if (g_list_find (priv->sessions, session) == NULL) {
306 GST_INFO ("watching session %p", session);
308 priv->sessions = g_list_prepend (priv->sessions, g_object_ref (session));
309 priv->sessions_cookie++;
311 /* connect removed session handler, it will be disconnected when the last
312 * session gets removed */
313 if (priv->session_removed_id == 0)
314 priv->session_removed_id = g_signal_connect_data (priv->session_pool,
315 "session-removed", G_CALLBACK (client_session_removed),
316 g_object_ref (client), (GClosureNotify) g_object_unref, 0);
318 g_mutex_unlock (&priv->lock);
323 /* should be called with lock */
325 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session,
328 GstRTSPClientPrivate *priv = client->priv;
330 GST_INFO ("client %p: unwatch session %p", client, session);
333 link = g_list_find (priv->sessions, session);
338 priv->sessions = g_list_delete_link (priv->sessions, link);
339 priv->sessions_cookie++;
341 /* if this was the last session, disconnect the handler.
342 * This will also drop the extra client ref */
343 if (!priv->sessions) {
344 g_signal_handler_disconnect (priv->session_pool, priv->session_removed_id);
345 priv->session_removed_id = 0;
348 /* unlink all media managed in this session */
349 gst_rtsp_session_filter (session, filter_session_media, client);
351 /* remove the session */
352 g_object_unref (session);
355 static GstRTSPFilterResult
356 cleanup_session (GstRTSPClient * client, GstRTSPSession * sess,
359 return GST_RTSP_FILTER_REMOVE;
362 /* A client is finalized when the connection is broken */
364 gst_rtsp_client_finalize (GObject * obj)
366 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
367 GstRTSPClientPrivate *priv = client->priv;
369 GST_INFO ("finalize client %p", client);
372 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
373 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
376 g_source_destroy ((GSource *) priv->watch);
378 if (priv->watch_context)
379 g_main_context_unref (priv->watch_context);
381 /* all sessions should have been removed by now. We keep a ref to
382 * the client object for the session removed handler. The ref is
383 * dropped when the last session is removed from the list. */
384 g_assert (priv->sessions == NULL);
385 g_assert (priv->session_removed_id == 0);
387 if (priv->connection)
388 gst_rtsp_connection_free (priv->connection);
389 if (priv->session_pool) {
390 g_object_unref (priv->session_pool);
392 if (priv->mount_points)
393 g_object_unref (priv->mount_points);
395 g_object_unref (priv->auth);
396 if (priv->thread_pool)
397 g_object_unref (priv->thread_pool);
402 gst_rtsp_media_unprepare (priv->media);
403 g_object_unref (priv->media);
406 g_free (priv->server_ip);
407 g_mutex_clear (&priv->lock);
408 g_mutex_clear (&priv->send_lock);
410 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
414 gst_rtsp_client_get_property (GObject * object, guint propid,
415 GValue * value, GParamSpec * pspec)
417 GstRTSPClient *client = GST_RTSP_CLIENT (object);
418 GstRTSPClientPrivate *priv = client->priv;
421 case PROP_SESSION_POOL:
422 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
424 case PROP_MOUNT_POINTS:
425 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
427 case PROP_DROP_BACKLOG:
428 g_value_set_boolean (value, priv->drop_backlog);
431 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
436 gst_rtsp_client_set_property (GObject * object, guint propid,
437 const GValue * value, GParamSpec * pspec)
439 GstRTSPClient *client = GST_RTSP_CLIENT (object);
440 GstRTSPClientPrivate *priv = client->priv;
443 case PROP_SESSION_POOL:
444 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
446 case PROP_MOUNT_POINTS:
447 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
449 case PROP_DROP_BACKLOG:
450 g_mutex_lock (&priv->lock);
451 priv->drop_backlog = g_value_get_boolean (value);
452 g_mutex_unlock (&priv->lock);
455 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
460 * gst_rtsp_client_new:
462 * Create a new #GstRTSPClient instance.
464 * Returns: (transfer full): a new #GstRTSPClient
467 gst_rtsp_client_new (void)
469 GstRTSPClient *result;
471 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
477 send_message (GstRTSPClient * client, GstRTSPContext * ctx,
478 GstRTSPMessage * message, gboolean close)
480 GstRTSPClientPrivate *priv = client->priv;
482 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
483 "GStreamer RTSP server");
485 /* remove any previous header */
486 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
488 /* add the new session header for new session ids */
490 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
491 gst_rtsp_session_get_header (ctx->session));
494 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
495 gst_rtsp_message_dump (message);
499 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
501 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE],
504 g_mutex_lock (&priv->send_lock);
506 priv->send_func (client, message, close, priv->send_data);
507 g_mutex_unlock (&priv->send_lock);
509 gst_rtsp_message_unset (message);
513 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
514 GstRTSPContext * ctx)
516 gst_rtsp_message_init_response (ctx->response, code,
517 gst_rtsp_status_as_text (code), ctx->request);
521 send_message (client, ctx, ctx->response, FALSE);
525 send_option_not_supported_response (GstRTSPClient * client,
526 GstRTSPContext * ctx, const gchar * unsupported_options)
528 GstRTSPStatusCode code = GST_RTSP_STS_OPTION_NOT_SUPPORTED;
530 gst_rtsp_message_init_response (ctx->response, code,
531 gst_rtsp_status_as_text (code), ctx->request);
533 if (unsupported_options != NULL) {
534 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_UNSUPPORTED,
535 unsupported_options);
540 send_message (client, ctx, ctx->response, FALSE);
544 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
546 if (path1 == NULL || path2 == NULL)
549 if (strlen (path1) != len2)
552 if (strncmp (path1, path2, len2))
558 /* this function is called to initially find the media for the DESCRIBE request
559 * but is cached for when the same client (without breaking the connection) is
560 * doing a setup for the exact same url. */
561 static GstRTSPMedia *
562 find_media (GstRTSPClient * client, GstRTSPContext * ctx, gchar * path,
565 GstRTSPClientPrivate *priv = client->priv;
566 GstRTSPMediaFactory *factory;
570 /* find the longest matching factory for the uri first */
571 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
575 ctx->factory = factory;
577 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
578 goto no_factory_access;
580 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
586 path_len = strlen (path);
588 if (!paths_are_equal (priv->path, path, path_len)) {
589 GstRTSPThread *thread;
591 /* remove any previously cached values before we try to construct a new
597 gst_rtsp_media_unprepare (priv->media);
598 g_object_unref (priv->media);
602 /* prepare the media and add it to the pipeline */
603 if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
608 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
609 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
613 /* prepare the media */
614 if (!(gst_rtsp_media_prepare (media, thread)))
617 /* now keep track of the uri and the media */
618 priv->path = g_strndup (path, path_len);
621 /* we have seen this path before, used cached media */
624 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
627 g_object_unref (factory);
631 g_object_ref (media);
638 GST_ERROR ("client %p: no factory for path %s", client, path);
639 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
644 GST_ERROR ("client %p: not authorized to see factory path %s", client,
646 /* error reply is already sent */
651 GST_ERROR ("client %p: not authorized for factory path %s", client, path);
652 /* error reply is already sent */
657 GST_ERROR ("client %p: can't create media", client);
658 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
659 g_object_unref (factory);
665 GST_ERROR ("client %p: can't create thread", client);
666 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
667 g_object_unref (media);
669 g_object_unref (factory);
675 GST_ERROR ("client %p: can't prepare media", client);
676 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
677 g_object_unref (media);
679 g_object_unref (factory);
686 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
688 GstRTSPClientPrivate *priv = client->priv;
689 GstRTSPMessage message = { 0 };
694 gst_rtsp_message_init_data (&message, channel);
696 /* FIXME, need some sort of iovec RTSPMessage here */
697 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
700 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
702 g_mutex_lock (&priv->send_lock);
704 priv->send_func (client, &message, FALSE, priv->send_data);
705 g_mutex_unlock (&priv->send_lock);
707 gst_rtsp_message_steal_body (&message, &data, &usize);
708 gst_buffer_unmap (buffer, &map_info);
710 gst_rtsp_message_unset (&message);
716 link_transport (GstRTSPClient * client, GstRTSPSession * session,
717 GstRTSPStreamTransport * trans)
719 GstRTSPClientPrivate *priv = client->priv;
721 GST_DEBUG ("client %p: linking transport %p", client, trans);
723 gst_rtsp_stream_transport_set_callbacks (trans,
724 (GstRTSPSendFunc) do_send_data,
725 (GstRTSPSendFunc) do_send_data, client, NULL);
727 priv->transports = g_list_prepend (priv->transports, trans);
729 /* make sure our session can't expire */
730 gst_rtsp_session_prevent_expire (session);
734 link_session_transports (GstRTSPClient * client, GstRTSPSession * session,
735 GstRTSPSessionMedia * sessmedia)
740 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
741 for (i = 0; i < n_streams; i++) {
742 GstRTSPStreamTransport *trans;
743 const GstRTSPTransport *tr;
745 /* get the transport, if there is no transport configured, skip this stream */
746 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
750 tr = gst_rtsp_stream_transport_get_transport (trans);
752 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
753 /* for TCP, link the stream to the TCP connection of the client */
754 link_transport (client, session, trans);
760 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
761 GstRTSPStreamTransport * trans)
763 GstRTSPClientPrivate *priv = client->priv;
765 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
767 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
769 priv->transports = g_list_remove (priv->transports, trans);
771 /* our session can now expire */
772 gst_rtsp_session_allow_expire (session);
776 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
777 GstRTSPSessionMedia * sessmedia)
782 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
783 for (i = 0; i < n_streams; i++) {
784 GstRTSPStreamTransport *trans;
785 const GstRTSPTransport *tr;
787 /* get the transport, if there is no transport configured, skip this stream */
788 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
792 tr = gst_rtsp_stream_transport_get_transport (trans);
794 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
795 /* for TCP, unlink the stream from the TCP connection of the client */
796 unlink_transport (client, session, trans);
802 close_connection (GstRTSPClient * client)
804 GstRTSPClientPrivate *priv = client->priv;
805 const gchar *tunnelid;
807 GST_DEBUG ("client %p: closing connection", client);
809 if (priv->connection) {
810 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
811 g_mutex_lock (&tunnels_lock);
812 /* remove from tunnelids */
813 g_hash_table_remove (tunnels, tunnelid);
814 g_mutex_unlock (&tunnels_lock);
816 gst_rtsp_connection_close (priv->connection);
819 /* connection is now closed, destroy the watch which will also cause the
820 * closed signal to be emitted */
822 GST_DEBUG ("client %p: destroying watch", client);
823 g_source_destroy ((GSource *) priv->watch);
825 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
830 default_make_path_from_uri (GstRTSPClient * client, const GstRTSPUrl * uri)
835 path = g_strconcat (uri->abspath, "?", uri->query, NULL);
837 path = g_strdup (uri->abspath);
843 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
845 GstRTSPClientPrivate *priv = client->priv;
846 GstRTSPClientClass *klass;
847 GstRTSPSession *session;
848 GstRTSPSessionMedia *sessmedia;
849 GstRTSPStatusCode code;
852 gboolean keep_session;
857 session = ctx->session;
862 klass = GST_RTSP_CLIENT_GET_CLASS (client);
863 path = klass->make_path_from_uri (client, ctx->uri);
865 /* get a handle to the configuration of the media in the session */
866 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
870 /* only aggregate control for now.. */
871 if (path[matched] != '\0')
876 ctx->sessmedia = sessmedia;
878 /* we emit the signal before closing the connection */
879 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
882 /* make sure we unblock the backlog and don't accept new messages
884 if (priv->watch != NULL)
885 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
887 /* unlink the all TCP callbacks */
888 unlink_session_transports (client, session, sessmedia);
890 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
892 /* allow messages again so that we can send the reply */
893 if (priv->watch != NULL)
894 gst_rtsp_watch_set_flushing (priv->watch, FALSE);
896 /* unmanage the media in the session, returns false if all media session
898 keep_session = gst_rtsp_session_release_media (session, sessmedia);
900 /* construct the response now */
901 code = GST_RTSP_STS_OK;
902 gst_rtsp_message_init_response (ctx->response, code,
903 gst_rtsp_status_as_text (code), ctx->request);
905 send_message (client, ctx, ctx->response, TRUE);
908 /* remove the session */
909 gst_rtsp_session_pool_remove (priv->session_pool, session);
917 GST_ERROR ("client %p: no session", client);
918 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
923 GST_ERROR ("client %p: no uri supplied", client);
924 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
929 GST_ERROR ("client %p: no media for uri", client);
930 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
936 GST_ERROR ("client %p: no aggregate path %s", client, path);
937 send_generic_response (client,
938 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
945 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
949 res = gst_rtsp_params_set (client, ctx);
955 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
959 res = gst_rtsp_params_get (client, ctx);
965 handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
971 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
972 if (res != GST_RTSP_OK)
976 /* no body, keep-alive request */
977 send_generic_response (client, GST_RTSP_STS_OK, ctx);
979 /* there is a body, handle the params */
980 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
981 if (res != GST_RTSP_OK)
984 send_message (client, ctx, ctx->response, FALSE);
987 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
995 GST_ERROR ("client %p: bad request", client);
996 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1002 handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
1008 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
1009 if (res != GST_RTSP_OK)
1013 /* no body, keep-alive request */
1014 send_generic_response (client, GST_RTSP_STS_OK, ctx);
1016 /* there is a body, handle the params */
1017 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
1018 if (res != GST_RTSP_OK)
1021 send_message (client, ctx, ctx->response, FALSE);
1024 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
1032 GST_ERROR ("client %p: bad request", client);
1033 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1039 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
1041 GstRTSPSession *session;
1042 GstRTSPClientClass *klass;
1043 GstRTSPSessionMedia *sessmedia;
1044 GstRTSPStatusCode code;
1045 GstRTSPState rtspstate;
1049 if (!(session = ctx->session))
1055 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1056 path = klass->make_path_from_uri (client, ctx->uri);
1058 /* get a handle to the configuration of the media in the session */
1059 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1063 if (path[matched] != '\0')
1068 ctx->sessmedia = sessmedia;
1070 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1071 /* the session state must be playing or recording */
1072 if (rtspstate != GST_RTSP_STATE_PLAYING &&
1073 rtspstate != GST_RTSP_STATE_RECORDING)
1076 /* unlink the all TCP callbacks */
1077 unlink_session_transports (client, session, sessmedia);
1079 /* then pause sending */
1080 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
1082 /* construct the response now */
1083 code = GST_RTSP_STS_OK;
1084 gst_rtsp_message_init_response (ctx->response, code,
1085 gst_rtsp_status_as_text (code), ctx->request);
1087 send_message (client, ctx, ctx->response, FALSE);
1089 /* the state is now READY */
1090 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1092 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
1099 GST_ERROR ("client %p: no seesion", client);
1100 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1105 GST_ERROR ("client %p: no uri supplied", client);
1106 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1111 GST_ERROR ("client %p: no media for uri", client);
1112 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1118 GST_ERROR ("client %p: no aggregate path %s", client, path);
1119 send_generic_response (client,
1120 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1126 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
1127 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1133 /* convert @url and @path to a URL used as a content base for the factory
1134 * located at @path */
1136 make_base_url (GstRTSPClient * client, GstRTSPUrl * url, const gchar * path)
1142 /* check for trailing '/' and append one */
1143 trail = (path[strlen (path) - 1] != '/' ? "/" : "");
1148 tmp.abspath = g_strdup_printf ("%s%s", path, trail);
1150 result = gst_rtsp_url_get_request_uri (&tmp);
1151 g_free (tmp.abspath);
1157 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
1159 GstRTSPSession *session;
1160 GstRTSPClientClass *klass;
1161 GstRTSPSessionMedia *sessmedia;
1162 GstRTSPMedia *media;
1163 GstRTSPStatusCode code;
1166 GstRTSPTimeRange *range;
1168 GstRTSPState rtspstate;
1169 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1170 gchar *path, *rtpinfo;
1173 if (!(session = ctx->session))
1176 if (!(uri = ctx->uri))
1179 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1180 path = klass->make_path_from_uri (client, uri);
1182 /* get a handle to the configuration of the media in the session */
1183 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1187 if (path[matched] != '\0')
1192 ctx->sessmedia = sessmedia;
1193 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1195 /* the session state must be playing or ready */
1196 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1197 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1200 /* in play we first unsuspend, media could be suspended from SDP or PAUSED */
1201 if (!gst_rtsp_media_unsuspend (media))
1202 goto unsuspend_failed;
1204 /* parse the range header if we have one */
1205 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1206 if (res == GST_RTSP_OK) {
1207 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1208 /* we have a range, seek to the position */
1210 gst_rtsp_media_seek (media, range);
1211 gst_rtsp_range_free (range);
1215 /* link the all TCP callbacks */
1216 link_session_transports (client, session, sessmedia);
1218 /* grab RTPInfo from the media now */
1219 rtpinfo = gst_rtsp_session_media_get_rtpinfo (sessmedia);
1221 /* construct the response now */
1222 code = GST_RTSP_STS_OK;
1223 gst_rtsp_message_init_response (ctx->response, code,
1224 gst_rtsp_status_as_text (code), ctx->request);
1226 /* add the RTP-Info header */
1228 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO,
1232 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1234 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1236 send_message (client, ctx, ctx->response, FALSE);
1238 /* start playing after sending the response */
1239 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1241 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1243 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1250 GST_ERROR ("client %p: no session", client);
1251 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1256 GST_ERROR ("client %p: no uri supplied", client);
1257 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1262 GST_ERROR ("client %p: media not found", client);
1263 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1268 GST_ERROR ("client %p: no aggregate path %s", client, path);
1269 send_generic_response (client,
1270 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1276 GST_ERROR ("client %p: not PLAYING or READY", client);
1277 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1283 GST_ERROR ("client %p: unsuspend failed", client);
1284 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1290 do_keepalive (GstRTSPSession * session)
1292 GST_INFO ("keep session %p alive", session);
1293 gst_rtsp_session_touch (session);
1296 /* parse @transport and return a valid transport in @tr. only transports
1297 * supported by @stream are returned. Returns FALSE if no valid transport
1300 parse_transport (const char *transport, GstRTSPStream * stream,
1301 GstRTSPTransport * tr)
1308 gst_rtsp_transport_init (tr);
1310 GST_DEBUG ("parsing transports %s", transport);
1312 transports = g_strsplit (transport, ",", 0);
1314 /* loop through the transports, try to parse */
1315 for (i = 0; transports[i]; i++) {
1316 res = gst_rtsp_transport_parse (transports[i], tr);
1317 if (res != GST_RTSP_OK) {
1318 /* no valid transport, search some more */
1319 GST_WARNING ("could not parse transport %s", transports[i]);
1323 /* we have a transport, see if it's supported */
1324 if (!gst_rtsp_stream_is_transport_supported (stream, tr)) {
1325 GST_WARNING ("unsupported transport %s", transports[i]);
1329 /* we have a valid transport */
1330 GST_INFO ("found valid transport %s", transports[i]);
1335 gst_rtsp_transport_init (tr);
1337 g_strfreev (transports);
1343 default_configure_client_media (GstRTSPClient * client, GstRTSPMedia * media,
1344 GstRTSPStream * stream, GstRTSPContext * ctx)
1346 GstRTSPMessage *request = ctx->request;
1347 gchar *blocksize_str;
1349 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1350 &blocksize_str, 0) == GST_RTSP_OK) {
1354 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1355 if (end == blocksize_str)
1358 /* we don't want to change the mtu when this media
1359 * can be shared because it impacts other clients */
1360 if (gst_rtsp_media_is_shared (media))
1363 if (blocksize > G_MAXUINT)
1364 blocksize = G_MAXUINT;
1366 gst_rtsp_stream_set_mtu (stream, blocksize);
1374 GST_ERROR_OBJECT (client, "failed to parse blocksize");
1375 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1381 default_configure_client_transport (GstRTSPClient * client,
1382 GstRTSPContext * ctx, GstRTSPTransport * ct)
1384 GstRTSPClientPrivate *priv = client->priv;
1386 /* we have a valid transport now, set the destination of the client. */
1387 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1388 gboolean use_client_settings;
1390 use_client_settings =
1391 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1393 if (ct->destination && use_client_settings) {
1394 GstRTSPAddress *addr;
1396 addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1397 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1402 gst_rtsp_address_free (addr);
1404 GstRTSPAddress *addr;
1405 GSocketFamily family;
1407 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1409 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1413 g_free (ct->destination);
1414 ct->destination = g_strdup (addr->address);
1415 ct->port.min = addr->port;
1416 ct->port.max = addr->port + addr->n_ports - 1;
1417 ct->ttl = addr->ttl;
1419 gst_rtsp_address_free (addr);
1424 url = gst_rtsp_connection_get_url (priv->connection);
1425 g_free (ct->destination);
1426 ct->destination = g_strdup (url->host);
1428 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1430 GSocketAddress *addr;
1432 sock = gst_rtsp_connection_get_read_socket (priv->connection);
1433 if ((addr = g_socket_get_remote_address (sock, NULL))) {
1434 /* our read port is the sender port of client */
1435 ct->client_port.min =
1436 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1437 g_object_unref (addr);
1439 if ((addr = g_socket_get_local_address (sock, NULL))) {
1440 ct->server_port.max =
1441 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1442 g_object_unref (addr);
1444 sock = gst_rtsp_connection_get_write_socket (priv->connection);
1445 if ((addr = g_socket_get_remote_address (sock, NULL))) {
1446 /* our write port is the receiver port of client */
1447 ct->client_port.max =
1448 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1449 g_object_unref (addr);
1451 if ((addr = g_socket_get_local_address (sock, NULL))) {
1452 ct->server_port.min =
1453 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1454 g_object_unref (addr);
1456 /* check if the client selected channels for TCP */
1457 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1458 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1468 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1473 static GstRTSPTransport *
1474 make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
1475 GstRTSPTransport * ct)
1477 GstRTSPTransport *st;
1479 GSocketFamily family;
1481 /* prepare the server transport */
1482 gst_rtsp_transport_new (&st);
1484 st->trans = ct->trans;
1485 st->profile = ct->profile;
1486 st->lower_transport = ct->lower_transport;
1488 addr = g_inet_address_new_from_string (ct->destination);
1491 GST_ERROR ("failed to get inet addr from client destination");
1492 family = G_SOCKET_FAMILY_IPV4;
1494 family = g_inet_address_get_family (addr);
1495 g_object_unref (addr);
1499 switch (st->lower_transport) {
1500 case GST_RTSP_LOWER_TRANS_UDP:
1501 st->client_port = ct->client_port;
1502 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
1504 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1505 st->port = ct->port;
1506 st->destination = g_strdup (ct->destination);
1509 case GST_RTSP_LOWER_TRANS_TCP:
1510 st->interleaved = ct->interleaved;
1511 st->client_port = ct->client_port;
1512 st->server_port = ct->server_port;
1517 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
1522 #define AES_128_KEY_LEN 16
1523 #define AES_256_KEY_LEN 32
1525 #define HMAC_32_KEY_LEN 4
1526 #define HMAC_80_KEY_LEN 10
1529 mikey_apply_policy (GstCaps * caps, GstMIKEYMessage * msg, guint8 policy)
1531 const gchar *srtp_cipher;
1532 const gchar *srtp_auth;
1533 const GstMIKEYPayload *sp;
1536 /* loop over Security policy until we find one containing policy */
1538 if ((sp = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, i)) == NULL)
1541 if (((GstMIKEYPayloadSP *) sp)->policy == policy)
1545 /* the default ciphers */
1546 srtp_cipher = "aes-128-icm";
1547 srtp_auth = "hmac-sha1-80";
1549 /* now override the defaults with what is in the Security Policy */
1553 /* collect all the params and go over them */
1554 len = gst_mikey_payload_sp_get_n_params (sp);
1555 for (i = 0; i < len; i++) {
1556 const GstMIKEYPayloadSPParam *param =
1557 gst_mikey_payload_sp_get_param (sp, i);
1559 switch (param->type) {
1560 case GST_MIKEY_SP_SRTP_ENC_ALG:
1561 switch (param->val[0]) {
1563 srtp_cipher = "null";
1567 srtp_cipher = "aes-128-icm";
1573 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
1574 switch (param->val[0]) {
1575 case AES_128_KEY_LEN:
1576 srtp_cipher = "aes-128-icm";
1578 case AES_256_KEY_LEN:
1579 srtp_cipher = "aes-256-icm";
1585 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1586 switch (param->val[0]) {
1592 srtp_auth = "hmac-sha1-80";
1598 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
1599 switch (param->val[0]) {
1600 case HMAC_32_KEY_LEN:
1601 srtp_auth = "hmac-sha1-32";
1603 case HMAC_80_KEY_LEN:
1604 srtp_auth = "hmac-sha1-80";
1610 case GST_MIKEY_SP_SRTP_SRTP_ENC:
1612 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
1619 /* now configure the SRTP parameters */
1620 gst_caps_set_simple (caps,
1621 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
1622 "srtp-auth", G_TYPE_STRING, srtp_auth,
1623 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
1624 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
1630 handle_mikey_data (GstRTSPClient * client, GstRTSPContext * ctx,
1631 guint8 * data, gsize size)
1633 GstMIKEYMessage *msg;
1635 GstCaps *caps = NULL;
1636 GstMIKEYPayloadKEMAC *kemac;
1637 const GstMIKEYPayloadKeyData *pkd;
1640 /* the MIKEY message contains a CSB or crypto session bundle. It is a
1641 * set of Crypto Sessions protected with the same master key.
1642 * In the context of SRTP, an RTP and its RTCP stream is part of a
1644 if ((msg = gst_mikey_message_new_from_data (data, size, NULL, NULL)) == NULL)
1647 /* we can only handle SRTP crypto sessions for now */
1648 if (msg->map_type != GST_MIKEY_MAP_TYPE_SRTP)
1649 goto invalid_map_type;
1651 /* get the number of crypto sessions. This maps SSRC to its
1652 * security parameters */
1653 n_cs = gst_mikey_message_get_n_cs (msg);
1655 goto no_crypto_sessions;
1657 /* we also need keys */
1658 if (!(kemac = (GstMIKEYPayloadKEMAC *) gst_mikey_message_find_payload
1659 (msg, GST_MIKEY_PT_KEMAC, 0)))
1662 /* we don't support encrypted keys */
1663 if (kemac->enc_alg != GST_MIKEY_ENC_NULL
1664 || kemac->mac_alg != GST_MIKEY_MAC_NULL)
1665 goto unsupported_encryption;
1667 /* get Key data sub-payload */
1668 pkd = (const GstMIKEYPayloadKeyData *)
1669 gst_mikey_payload_kemac_get_sub (&kemac->pt, 0);
1672 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
1675 /* go over all crypto sessions and create the security policy for each
1677 for (i = 0; i < n_cs; i++) {
1678 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
1680 caps = gst_caps_new_simple ("application/x-srtp",
1681 "ssrc", G_TYPE_UINT, map->ssrc,
1682 "roc", G_TYPE_UINT, map->roc, "srtp-key", GST_TYPE_BUFFER, key, NULL);
1683 mikey_apply_policy (caps, msg, map->policy);
1685 gst_rtsp_stream_update_crypto (ctx->stream, map->ssrc, caps);
1686 gst_caps_unref (caps);
1688 gst_mikey_message_unref (msg);
1695 GST_DEBUG_OBJECT (client, "failed to parse MIKEY message");
1700 GST_DEBUG_OBJECT (client, "invalid map type %d", msg->map_type);
1701 goto cleanup_message;
1705 GST_DEBUG_OBJECT (client, "no crypto sessions");
1706 goto cleanup_message;
1710 GST_DEBUG_OBJECT (client, "no keys found");
1711 goto cleanup_message;
1713 unsupported_encryption:
1715 GST_DEBUG_OBJECT (client, "unsupported key encryption");
1716 goto cleanup_message;
1720 gst_mikey_message_unref (msg);
1725 #define IS_STRIP_CHAR(c) (g_ascii_isspace ((guchar)(c)) || ((c) == '\"'))
1728 strip_chars (gchar * str)
1735 if (!IS_STRIP_CHAR (str[len]))
1739 for (s = str; *s && IS_STRIP_CHAR (*s); s++);
1740 memmove (str, s, len + 1);
1743 /* KeyMgmt = "KeyMgmt" ":" key-mgmt-spec 0*("," key-mgmt-spec)
1744 * key-mgmt-spec = "prot" "=" KMPID ";" ["uri" "=" %x22 URI %x22 ";"]
1747 handle_keymgmt (GstRTSPClient * client, GstRTSPContext * ctx, gchar * keymgmt)
1752 specs = g_strsplit (keymgmt, ",", 0);
1753 for (i = 0; specs[i]; i++) {
1756 split = g_strsplit (specs[i], ";", 0);
1757 for (j = 0; split[j]; j++) {
1758 g_strstrip (split[j]);
1759 if (g_str_has_prefix (split[j], "prot=")) {
1760 g_strstrip (split[j] + 5);
1761 if (!g_str_equal (split[j] + 5, "mikey"))
1763 GST_DEBUG ("found mikey");
1764 } else if (g_str_has_prefix (split[j], "uri=")) {
1765 strip_chars (split[j] + 4);
1766 GST_DEBUG ("found uri '%s'", split[j] + 4);
1767 } else if (g_str_has_prefix (split[j], "data=")) {
1770 strip_chars (split[j] + 5);
1771 GST_DEBUG ("found data '%s'", split[j] + 5);
1772 data = g_base64_decode_inplace (split[j] + 5, &size);
1773 handle_mikey_data (client, ctx, data, size);
1781 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
1783 GstRTSPClientPrivate *priv = client->priv;
1786 gchar *transport, *keymgmt;
1787 GstRTSPTransport *ct, *st;
1788 GstRTSPStatusCode code;
1789 GstRTSPSession *session;
1790 GstRTSPStreamTransport *trans;
1792 GstRTSPSessionMedia *sessmedia;
1793 GstRTSPMedia *media;
1794 GstRTSPStream *stream;
1795 GstRTSPState rtspstate;
1796 GstRTSPClientClass *klass;
1797 gchar *path, *control;
1799 gboolean new_session = FALSE;
1805 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1806 path = klass->make_path_from_uri (client, uri);
1808 /* parse the transport */
1810 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
1812 if (res != GST_RTSP_OK)
1815 /* we create the session after parsing stuff so that we don't make
1816 * a session for malformed requests */
1817 if (priv->session_pool == NULL)
1820 session = ctx->session;
1823 g_object_ref (session);
1824 /* get a handle to the configuration of the media in the session, this can
1825 * return NULL if this is a new url to manage in this session. */
1826 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1828 /* we need a new media configuration in this session */
1832 /* we have no session media, find one and manage it */
1833 if (sessmedia == NULL) {
1834 /* get a handle to the configuration of the media in the session */
1835 media = find_media (client, ctx, path, &matched);
1837 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1838 g_object_ref (media);
1840 goto media_not_found;
1842 /* no media, not found then */
1844 goto media_not_found_no_reply;
1846 if (path[matched] == '\0')
1847 goto control_not_found;
1849 /* path is what matched. */
1850 path[matched] = '\0';
1851 /* control is remainder */
1852 control = &path[matched + 1];
1854 /* find the stream now using the control part */
1855 stream = gst_rtsp_media_find_stream (media, control);
1857 goto stream_not_found;
1859 /* now we have a uri identifying a valid media and stream */
1860 ctx->stream = stream;
1863 if (session == NULL) {
1864 /* create a session if this fails we probably reached our session limit or
1866 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1867 goto service_unavailable;
1869 /* make sure this client is closed when the session is closed */
1870 client_watch_session (client, session);
1873 /* signal new session */
1874 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1877 ctx->session = session;
1880 if (!klass->configure_client_media (client, media, stream, ctx))
1881 goto configure_media_failed_no_reply;
1883 gst_rtsp_transport_new (&ct);
1885 /* parse and find a usable supported transport */
1886 if (!parse_transport (transport, stream, ct))
1887 goto unsupported_transports;
1889 /* update the client transport */
1890 if (!klass->configure_client_transport (client, ctx, ct))
1891 goto unsupported_client_transport;
1893 /* parse the keymgmt */
1894 if (gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_KEYMGMT,
1895 &keymgmt, 0) == GST_RTSP_OK) {
1896 if (!handle_keymgmt (client, ctx, keymgmt))
1900 if (sessmedia == NULL) {
1901 /* manage the media in our session now, if not done already */
1902 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1903 /* if we stil have no media, error */
1904 if (sessmedia == NULL)
1905 goto sessmedia_unavailable;
1907 g_object_unref (media);
1910 ctx->sessmedia = sessmedia;
1912 /* set in the session media transport */
1913 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1915 /* configure the url used to set this transport, this we will use when
1916 * generating the response for the PLAY request */
1917 gst_rtsp_stream_transport_set_url (trans, uri);
1919 /* configure keepalive for this transport */
1920 gst_rtsp_stream_transport_set_keepalive (trans,
1921 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1923 /* create and serialize the server transport */
1924 st = make_server_transport (client, ctx, ct);
1925 trans_str = gst_rtsp_transport_as_text (st);
1926 gst_rtsp_transport_free (st);
1928 /* construct the response now */
1929 code = GST_RTSP_STS_OK;
1930 gst_rtsp_message_init_response (ctx->response, code,
1931 gst_rtsp_status_as_text (code), ctx->request);
1933 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
1937 send_message (client, ctx, ctx->response, FALSE);
1939 /* update the state */
1940 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1941 switch (rtspstate) {
1942 case GST_RTSP_STATE_PLAYING:
1943 case GST_RTSP_STATE_RECORDING:
1944 case GST_RTSP_STATE_READY:
1945 /* no state change */
1948 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1951 g_object_unref (session);
1954 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
1961 GST_ERROR ("client %p: no uri", client);
1962 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1967 GST_ERROR ("client %p: no transport", client);
1968 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1973 GST_ERROR ("client %p: no session pool configured", client);
1974 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1977 media_not_found_no_reply:
1979 GST_ERROR ("client %p: media '%s' not found", client, path);
1980 /* error reply is already sent */
1985 GST_ERROR ("client %p: media '%s' not found", client, path);
1986 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1991 GST_ERROR ("client %p: no control in path '%s'", client, path);
1992 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1993 g_object_unref (media);
1998 GST_ERROR ("client %p: stream '%s' not found", client, control);
1999 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2000 g_object_unref (media);
2003 service_unavailable:
2005 GST_ERROR ("client %p: can't create session", client);
2006 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2007 g_object_unref (media);
2010 sessmedia_unavailable:
2012 GST_ERROR ("client %p: can't create session media", client);
2013 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2014 g_object_unref (media);
2015 goto cleanup_session;
2017 configure_media_failed_no_reply:
2019 GST_ERROR ("client %p: configure_media failed", client);
2020 /* error reply is already sent */
2021 goto cleanup_session;
2023 unsupported_transports:
2025 GST_ERROR ("client %p: unsupported transports", client);
2026 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
2027 goto cleanup_transport;
2029 unsupported_client_transport:
2031 GST_ERROR ("client %p: unsupported client transport", client);
2032 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
2033 goto cleanup_transport;
2037 GST_ERROR ("client %p: keymgmt error", client);
2038 send_generic_response (client, GST_RTSP_STS_KEY_MANAGEMENT_FAILURE, ctx);
2039 goto cleanup_transport;
2043 gst_rtsp_transport_free (ct);
2046 gst_rtsp_session_pool_remove (priv->session_pool, session);
2047 g_object_unref (session);
2054 static GstSDPMessage *
2055 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
2057 GstRTSPClientPrivate *priv = client->priv;
2062 gst_sdp_message_new (&sdp);
2064 /* some standard things first */
2065 gst_sdp_message_set_version (sdp, "0");
2072 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
2075 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
2076 gst_sdp_message_set_information (sdp, "rtsp-server");
2077 gst_sdp_message_add_time (sdp, "0", "0", NULL);
2078 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
2079 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
2080 gst_sdp_message_add_attribute (sdp, "control", "*");
2082 info.is_ipv6 = priv->is_ipv6;
2083 info.server_ip = priv->server_ip;
2085 /* create an SDP for the media object */
2086 if (!gst_rtsp_media_setup_sdp (media, sdp, &info))
2094 GST_ERROR ("client %p: could not create SDP", client);
2095 gst_sdp_message_free (sdp);
2100 /* for the describe we must generate an SDP */
2102 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
2104 GstRTSPClientPrivate *priv = client->priv;
2109 GstRTSPMedia *media;
2110 GstRTSPClientClass *klass;
2112 klass = GST_RTSP_CLIENT_GET_CLASS (client);
2117 /* check what kind of format is accepted, we don't really do anything with it
2118 * and always return SDP for now. */
2123 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
2125 if (res == GST_RTSP_ENOTIMPL)
2128 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
2132 if (!priv->mount_points)
2133 goto no_mount_points;
2135 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
2138 /* find the media object for the uri */
2139 if (!(media = find_media (client, ctx, path, NULL)))
2142 /* create an SDP for the media object on this client */
2143 if (!(sdp = klass->create_sdp (client, media)))
2146 /* we suspend after the describe */
2147 gst_rtsp_media_suspend (media);
2148 g_object_unref (media);
2150 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2151 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2153 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
2156 /* content base for some clients that might screw up creating the setup uri */
2157 str = make_base_url (client, ctx->uri, path);
2160 GST_INFO ("adding content-base: %s", str);
2161 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE, str);
2163 /* add SDP to the response body */
2164 str = gst_sdp_message_as_text (sdp);
2165 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
2166 gst_sdp_message_free (sdp);
2168 send_message (client, ctx, ctx->response, FALSE);
2170 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
2178 GST_ERROR ("client %p: no uri", client);
2179 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2184 GST_ERROR ("client %p: no mount points configured", client);
2185 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2190 GST_ERROR ("client %p: can't find path for url", client);
2191 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2196 GST_ERROR ("client %p: no media", client);
2198 /* error reply is already sent */
2203 GST_ERROR ("client %p: can't create SDP", client);
2204 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2206 g_object_unref (media);
2212 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
2214 GstRTSPMethod options;
2217 options = GST_RTSP_DESCRIBE |
2222 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
2224 str = gst_rtsp_options_as_text (options);
2226 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2227 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2229 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
2232 send_message (client, ctx, ctx->response, FALSE);
2234 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
2240 /* remove duplicate and trailing '/' */
2242 sanitize_uri (GstRTSPUrl * uri)
2246 gboolean have_slash, prev_slash;
2248 s = d = uri->abspath;
2249 len = strlen (uri->abspath);
2253 for (i = 0; i < len; i++) {
2254 have_slash = s[i] == '/';
2256 if (!have_slash || !prev_slash)
2258 prev_slash = have_slash;
2260 len = d - uri->abspath;
2261 /* don't remove the first slash if that's the only thing left */
2262 if (len > 1 && *(d - 1) == '/')
2267 /* is called when the session is removed from its session pool. */
2269 client_session_removed (GstRTSPSessionPool * pool, GstRTSPSession * session,
2270 GstRTSPClient * client)
2272 GstRTSPClientPrivate *priv = client->priv;
2274 GST_INFO ("client %p: session %p removed", client, session);
2276 g_mutex_lock (&priv->lock);
2277 client_unwatch_session (client, session, NULL);
2278 g_mutex_unlock (&priv->lock);
2281 /* Returns TRUE if there are no Require headers, otherwise returns FALSE
2282 * and also returns a newly-allocated string of (comma-separated) unsupported
2283 * options in the unsupported_reqs variable .
2285 * There may be multiple Require headers, but we must send one single
2286 * Unsupported header with all the unsupported options as response. If
2287 * an incoming Require header contained a comma-separated list of options
2288 * GstRtspConnection will already have split that list up into multiple
2291 * TODO: allow the application to decide what features are supported
2294 check_request_requirements (GstRTSPMessage * msg, gchar ** unsupported_reqs)
2297 GPtrArray *arr = NULL;
2303 res = gst_rtsp_message_get_header (msg, GST_RTSP_HDR_REQUIRE, &reqs, i++);
2305 if (res == GST_RTSP_ENOTIMPL)
2309 arr = g_ptr_array_new_with_free_func ((GDestroyNotify) g_free);
2311 g_ptr_array_add (arr, g_strdup (reqs));
2315 /* if we don't have any Require headers at all, all is fine */
2319 /* otherwise we've now processed at all the Require headers */
2320 g_ptr_array_add (arr, NULL);
2322 /* for now we don't commit to supporting anything, so will just report
2323 * all of the required options as unsupported */
2324 *unsupported_reqs = g_strjoinv (", ", (gchar **) arr->pdata);
2326 g_ptr_array_unref (arr);
2331 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
2333 GstRTSPClientPrivate *priv = client->priv;
2334 GstRTSPMethod method;
2335 const gchar *uristr;
2336 GstRTSPUrl *uri = NULL;
2337 GstRTSPVersion version;
2339 GstRTSPSession *session = NULL;
2340 GstRTSPContext sctx = { NULL }, *ctx;
2341 GstRTSPMessage response = { 0 };
2342 gchar *unsupported_reqs = NULL;
2345 if (!(ctx = gst_rtsp_context_get_current ())) {
2347 ctx->auth = priv->auth;
2348 gst_rtsp_context_push_current (ctx);
2351 ctx->conn = priv->connection;
2352 ctx->client = client;
2353 ctx->request = request;
2354 ctx->response = &response;
2356 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2357 gst_rtsp_message_dump (request);
2360 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
2362 GST_INFO ("client %p: received a request %s %s %s", client,
2363 gst_rtsp_method_as_text (method), uristr,
2364 gst_rtsp_version_as_text (version));
2366 /* we can only handle 1.0 requests */
2367 if (version != GST_RTSP_VERSION_1_0)
2370 ctx->method = method;
2372 /* we always try to parse the url first */
2373 if (strcmp (uristr, "*") == 0) {
2374 /* special case where we have * as uri, keep uri = NULL */
2375 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
2376 /* check if the uristr is an absolute path <=> scheme and host information
2380 scheme = g_uri_parse_scheme (uristr);
2381 if (scheme == NULL && g_str_has_prefix (uristr, "/")) {
2382 gchar *absolute_uristr = NULL;
2384 GST_WARNING_OBJECT (client, "request doesn't contain absolute url");
2385 if (priv->server_ip == NULL) {
2386 GST_WARNING_OBJECT (client, "host information missing");
2391 g_strdup_printf ("rtsp://%s%s", priv->server_ip, uristr);
2393 GST_DEBUG_OBJECT (client, "absolute url: %s", absolute_uristr);
2394 if (gst_rtsp_url_parse (absolute_uristr, &uri) != GST_RTSP_OK) {
2395 g_free (absolute_uristr);
2398 g_free (absolute_uristr);
2405 /* get the session if there is any */
2406 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
2407 if (res == GST_RTSP_OK) {
2408 if (priv->session_pool == NULL)
2411 /* we had a session in the request, find it again */
2412 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2413 goto session_not_found;
2415 /* we add the session to the client list of watched sessions. When a session
2416 * disappears because it times out, we will be notified. If all sessions are
2417 * gone, we will close the connection */
2418 client_watch_session (client, session);
2421 /* sanitize the uri */
2425 ctx->session = session;
2427 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
2428 goto not_authorized;
2430 /* handle any 'Require' headers */
2431 if (!check_request_requirements (ctx->request, &unsupported_reqs))
2432 goto unsupported_requirement;
2434 /* now see what is asked and dispatch to a dedicated handler */
2436 case GST_RTSP_OPTIONS:
2437 handle_options_request (client, ctx);
2439 case GST_RTSP_DESCRIBE:
2440 handle_describe_request (client, ctx);
2442 case GST_RTSP_SETUP:
2443 handle_setup_request (client, ctx);
2446 handle_play_request (client, ctx);
2448 case GST_RTSP_PAUSE:
2449 handle_pause_request (client, ctx);
2451 case GST_RTSP_TEARDOWN:
2452 handle_teardown_request (client, ctx);
2454 case GST_RTSP_SET_PARAMETER:
2455 handle_set_param_request (client, ctx);
2457 case GST_RTSP_GET_PARAMETER:
2458 handle_get_param_request (client, ctx);
2460 case GST_RTSP_ANNOUNCE:
2461 case GST_RTSP_RECORD:
2462 case GST_RTSP_REDIRECT:
2463 goto not_implemented;
2464 case GST_RTSP_INVALID:
2471 gst_rtsp_context_pop_current (ctx);
2473 g_object_unref (session);
2475 gst_rtsp_url_free (uri);
2481 GST_ERROR ("client %p: version %d not supported", client, version);
2482 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
2488 GST_ERROR ("client %p: bad request", client);
2489 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2494 GST_ERROR ("client %p: no pool configured", client);
2495 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2500 GST_ERROR ("client %p: session not found", client);
2501 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2506 GST_ERROR ("client %p: not allowed", client);
2507 /* error reply is already sent */
2510 unsupported_requirement:
2512 GST_ERROR ("client %p: Required option is not supported (%s)", client,
2514 send_option_not_supported_response (client, ctx, unsupported_reqs);
2515 g_free (unsupported_reqs);
2520 GST_ERROR ("client %p: method %d not implemented", client, method);
2521 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
2528 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
2530 GstRTSPClientPrivate *priv = client->priv;
2532 GstRTSPSession *session = NULL;
2533 GstRTSPContext sctx = { NULL }, *ctx;
2536 if (!(ctx = gst_rtsp_context_get_current ())) {
2538 ctx->auth = priv->auth;
2539 gst_rtsp_context_push_current (ctx);
2542 ctx->conn = priv->connection;
2543 ctx->client = client;
2544 ctx->request = NULL;
2546 ctx->method = GST_RTSP_INVALID;
2547 ctx->response = response;
2549 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2550 gst_rtsp_message_dump (response);
2553 GST_INFO ("client %p: received a response", client);
2555 /* get the session if there is any */
2557 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
2558 if (res == GST_RTSP_OK) {
2559 if (priv->session_pool == NULL)
2562 /* we had a session in the request, find it again */
2563 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2564 goto session_not_found;
2566 /* we add the session to the client list of watched sessions. When a session
2567 * disappears because it times out, we will be notified. If all sessions are
2568 * gone, we will close the connection */
2569 client_watch_session (client, session);
2572 ctx->session = session;
2574 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
2579 gst_rtsp_context_pop_current (ctx);
2581 g_object_unref (session);
2586 GST_ERROR ("client %p: no pool configured", client);
2591 GST_ERROR ("client %p: session not found", client);
2597 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2599 GstRTSPClientPrivate *priv = client->priv;
2608 /* find the stream for this message */
2609 res = gst_rtsp_message_parse_data (message, &channel);
2610 if (res != GST_RTSP_OK)
2613 gst_rtsp_message_steal_body (message, &data, &size);
2615 buffer = gst_buffer_new_wrapped (data, size);
2618 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2619 GstRTSPStreamTransport *trans;
2620 GstRTSPStream *stream;
2621 const GstRTSPTransport *tr;
2625 tr = gst_rtsp_stream_transport_get_transport (trans);
2626 stream = gst_rtsp_stream_transport_get_stream (trans);
2628 /* check for TCP transport */
2629 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2630 /* dispatch to the stream based on the channel number */
2631 if (tr->interleaved.min == channel) {
2632 gst_rtsp_stream_recv_rtp (stream, buffer);
2635 } else if (tr->interleaved.max == channel) {
2636 gst_rtsp_stream_recv_rtcp (stream, buffer);
2643 gst_buffer_unref (buffer);
2647 * gst_rtsp_client_set_session_pool:
2648 * @client: a #GstRTSPClient
2649 * @pool: (transfer none): a #GstRTSPSessionPool
2651 * Set @pool as the sessionpool for @client which it will use to find
2652 * or allocate sessions. the sessionpool is usually inherited from the server
2653 * that created the client but can be overridden later.
2656 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2657 GstRTSPSessionPool * pool)
2659 GstRTSPSessionPool *old;
2660 GstRTSPClientPrivate *priv;
2662 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2664 priv = client->priv;
2667 g_object_ref (pool);
2669 g_mutex_lock (&priv->lock);
2670 old = priv->session_pool;
2671 priv->session_pool = pool;
2673 if (priv->session_removed_id) {
2674 g_signal_handler_disconnect (old, priv->session_removed_id);
2675 priv->session_removed_id = 0;
2677 g_mutex_unlock (&priv->lock);
2679 /* FIXME, should remove all sessions from the old pool for this client */
2681 g_object_unref (old);
2685 * gst_rtsp_client_get_session_pool:
2686 * @client: a #GstRTSPClient
2688 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2690 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2692 GstRTSPSessionPool *
2693 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2695 GstRTSPClientPrivate *priv;
2696 GstRTSPSessionPool *result;
2698 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2700 priv = client->priv;
2702 g_mutex_lock (&priv->lock);
2703 if ((result = priv->session_pool))
2704 g_object_ref (result);
2705 g_mutex_unlock (&priv->lock);
2711 * gst_rtsp_client_set_mount_points:
2712 * @client: a #GstRTSPClient
2713 * @mounts: (transfer none): a #GstRTSPMountPoints
2715 * Set @mounts as the mount points for @client which it will use to map urls
2716 * to media streams. These mount points are usually inherited from the server that
2717 * created the client but can be overriden later.
2720 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2721 GstRTSPMountPoints * mounts)
2723 GstRTSPClientPrivate *priv;
2724 GstRTSPMountPoints *old;
2726 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2728 priv = client->priv;
2731 g_object_ref (mounts);
2733 g_mutex_lock (&priv->lock);
2734 old = priv->mount_points;
2735 priv->mount_points = mounts;
2736 g_mutex_unlock (&priv->lock);
2739 g_object_unref (old);
2743 * gst_rtsp_client_get_mount_points:
2744 * @client: a #GstRTSPClient
2746 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2748 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2750 GstRTSPMountPoints *
2751 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2753 GstRTSPClientPrivate *priv;
2754 GstRTSPMountPoints *result;
2756 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2758 priv = client->priv;
2760 g_mutex_lock (&priv->lock);
2761 if ((result = priv->mount_points))
2762 g_object_ref (result);
2763 g_mutex_unlock (&priv->lock);
2769 * gst_rtsp_client_set_auth:
2770 * @client: a #GstRTSPClient
2771 * @auth: (transfer none): a #GstRTSPAuth
2773 * configure @auth to be used as the authentication manager of @client.
2776 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2778 GstRTSPClientPrivate *priv;
2781 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2783 priv = client->priv;
2786 g_object_ref (auth);
2788 g_mutex_lock (&priv->lock);
2791 g_mutex_unlock (&priv->lock);
2794 g_object_unref (old);
2799 * gst_rtsp_client_get_auth:
2800 * @client: a #GstRTSPClient
2802 * Get the #GstRTSPAuth used as the authentication manager of @client.
2804 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2808 gst_rtsp_client_get_auth (GstRTSPClient * client)
2810 GstRTSPClientPrivate *priv;
2811 GstRTSPAuth *result;
2813 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2815 priv = client->priv;
2817 g_mutex_lock (&priv->lock);
2818 if ((result = priv->auth))
2819 g_object_ref (result);
2820 g_mutex_unlock (&priv->lock);
2826 * gst_rtsp_client_set_thread_pool:
2827 * @client: a #GstRTSPClient
2828 * @pool: (transfer none): a #GstRTSPThreadPool
2830 * configure @pool to be used as the thread pool of @client.
2833 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2834 GstRTSPThreadPool * pool)
2836 GstRTSPClientPrivate *priv;
2837 GstRTSPThreadPool *old;
2839 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2841 priv = client->priv;
2844 g_object_ref (pool);
2846 g_mutex_lock (&priv->lock);
2847 old = priv->thread_pool;
2848 priv->thread_pool = pool;
2849 g_mutex_unlock (&priv->lock);
2852 g_object_unref (old);
2856 * gst_rtsp_client_get_thread_pool:
2857 * @client: a #GstRTSPClient
2859 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2861 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2865 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2867 GstRTSPClientPrivate *priv;
2868 GstRTSPThreadPool *result;
2870 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2872 priv = client->priv;
2874 g_mutex_lock (&priv->lock);
2875 if ((result = priv->thread_pool))
2876 g_object_ref (result);
2877 g_mutex_unlock (&priv->lock);
2883 * gst_rtsp_client_set_connection:
2884 * @client: a #GstRTSPClient
2885 * @conn: (transfer full): a #GstRTSPConnection
2887 * Set the #GstRTSPConnection of @client. This function takes ownership of
2890 * Returns: %TRUE on success.
2893 gst_rtsp_client_set_connection (GstRTSPClient * client,
2894 GstRTSPConnection * conn)
2896 GstRTSPClientPrivate *priv;
2897 GSocket *read_socket;
2898 GSocketAddress *address;
2900 GError *error = NULL;
2902 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2903 g_return_val_if_fail (conn != NULL, FALSE);
2905 priv = client->priv;
2907 read_socket = gst_rtsp_connection_get_read_socket (conn);
2909 if (!(address = g_socket_get_local_address (read_socket, &error)))
2912 g_free (priv->server_ip);
2913 /* keep the original ip that the client connected to */
2914 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2915 GInetAddress *iaddr;
2917 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2919 /* socket might be ipv6 but adress still ipv4 */
2920 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2921 priv->server_ip = g_inet_address_to_string (iaddr);
2922 g_object_unref (address);
2924 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2925 priv->server_ip = g_strdup ("unknown");
2928 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2929 priv->server_ip, priv->is_ipv6);
2931 url = gst_rtsp_connection_get_url (conn);
2932 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2934 priv->connection = conn;
2941 GST_ERROR ("could not get local address %s", error->message);
2942 g_error_free (error);
2948 * gst_rtsp_client_get_connection:
2949 * @client: a #GstRTSPClient
2951 * Get the #GstRTSPConnection of @client.
2953 * Returns: (transfer none): the #GstRTSPConnection of @client.
2954 * The connection object returned remains valid until the client is freed.
2957 gst_rtsp_client_get_connection (GstRTSPClient * client)
2959 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2961 return client->priv->connection;
2965 * gst_rtsp_client_set_send_func:
2966 * @client: a #GstRTSPClient
2967 * @func: (scope notified): a #GstRTSPClientSendFunc
2968 * @user_data: (closure): user data passed to @func
2969 * @notify: (allow-none): called when @user_data is no longer in use
2971 * Set @func as the callback that will be called when a new message needs to be
2972 * sent to the client. @user_data is passed to @func and @notify is called when
2973 * @user_data is no longer in use.
2975 * By default, the client will send the messages on the #GstRTSPConnection that
2976 * was configured with gst_rtsp_client_attach() was called.
2979 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2980 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2982 GstRTSPClientPrivate *priv;
2983 GDestroyNotify old_notify;
2986 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2988 priv = client->priv;
2990 g_mutex_lock (&priv->send_lock);
2991 priv->send_func = func;
2992 old_notify = priv->send_notify;
2993 old_data = priv->send_data;
2994 priv->send_notify = notify;
2995 priv->send_data = user_data;
2996 g_mutex_unlock (&priv->send_lock);
2999 old_notify (old_data);
3003 * gst_rtsp_client_handle_message:
3004 * @client: a #GstRTSPClient
3005 * @message: (transfer none): an #GstRTSPMessage
3007 * Let the client handle @message.
3009 * Returns: a #GstRTSPResult.
3012 gst_rtsp_client_handle_message (GstRTSPClient * client,
3013 GstRTSPMessage * message)
3015 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
3016 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3018 switch (message->type) {
3019 case GST_RTSP_MESSAGE_REQUEST:
3020 handle_request (client, message);
3022 case GST_RTSP_MESSAGE_RESPONSE:
3023 handle_response (client, message);
3025 case GST_RTSP_MESSAGE_DATA:
3026 handle_data (client, message);
3035 * gst_rtsp_client_send_message:
3036 * @client: a #GstRTSPClient
3037 * @session: (allow-none) (transfer none): a #GstRTSPSession to send
3038 * the message to or %NULL
3039 * @message: (transfer none): The #GstRTSPMessage to send
3041 * Send a message message to the remote end. @message must be a
3042 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
3045 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
3046 GstRTSPMessage * message)
3048 GstRTSPContext sctx = { NULL }
3050 GstRTSPClientPrivate *priv;
3052 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
3053 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3054 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
3055 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
3057 priv = client->priv;
3059 if (!(ctx = gst_rtsp_context_get_current ())) {
3061 ctx->auth = priv->auth;
3062 gst_rtsp_context_push_current (ctx);
3065 ctx->conn = priv->connection;
3066 ctx->client = client;
3067 ctx->session = session;
3069 send_message (client, ctx, message, FALSE);
3072 gst_rtsp_context_pop_current (ctx);
3077 static GstRTSPResult
3078 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
3079 gboolean close, gpointer user_data)
3081 GstRTSPClientPrivate *priv = client->priv;
3089 /* send the response and store the seq number so we can wait until it's
3090 * written to the client to close the connection */
3092 gst_rtsp_watch_send_message (priv->watch, message,
3093 close ? &priv->close_seq : NULL);
3094 if (ret == GST_RTSP_OK)
3097 if (ret != GST_RTSP_ENOMEM)
3101 if (priv->drop_backlog)
3104 /* queue was full, wait for more space */
3105 GST_DEBUG_OBJECT (client, "waiting for backlog");
3106 ret = gst_rtsp_watch_wait_backlog (priv->watch, &time);
3107 GST_DEBUG_OBJECT (client, "Resend due to backlog full");
3108 } while (ret != GST_RTSP_EINTR);
3115 GST_DEBUG_OBJECT (client, "got error %d", ret);
3120 static GstRTSPResult
3121 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
3124 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
3127 static GstRTSPResult
3128 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
3130 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3131 GstRTSPClientPrivate *priv = client->priv;
3133 if (priv->close_seq && priv->close_seq == cseq) {
3134 GST_INFO ("client %p: send close message", client);
3135 priv->close_seq = 0;
3136 close_connection (client);
3142 static GstRTSPResult
3143 closed (GstRTSPWatch * watch, gpointer user_data)
3145 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3146 GstRTSPClientPrivate *priv = client->priv;
3147 const gchar *tunnelid;
3149 GST_INFO ("client %p: connection closed", client);
3151 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
3152 g_mutex_lock (&tunnels_lock);
3153 /* remove from tunnelids */
3154 g_hash_table_remove (tunnels, tunnelid);
3155 g_mutex_unlock (&tunnels_lock);
3158 gst_rtsp_watch_set_flushing (watch, TRUE);
3159 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
3164 static GstRTSPResult
3165 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
3167 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3170 str = gst_rtsp_strresult (result);
3171 GST_INFO ("client %p: received an error %s", client, str);
3177 static GstRTSPResult
3178 error_full (GstRTSPWatch * watch, GstRTSPResult result,
3179 GstRTSPMessage * message, guint id, gpointer user_data)
3181 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3184 str = gst_rtsp_strresult (result);
3186 ("client %p: error when handling message %p with id %d: %s",
3187 client, message, id, str);
3194 remember_tunnel (GstRTSPClient * client)
3196 GstRTSPClientPrivate *priv = client->priv;
3197 const gchar *tunnelid;
3199 /* store client in the pending tunnels */
3200 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
3201 if (tunnelid == NULL)
3204 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
3206 /* we can't have two clients connecting with the same tunnelid */
3207 g_mutex_lock (&tunnels_lock);
3208 if (g_hash_table_lookup (tunnels, tunnelid))
3209 goto tunnel_existed;
3211 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
3212 g_mutex_unlock (&tunnels_lock);
3219 GST_ERROR ("client %p: no tunnelid provided", client);
3224 g_mutex_unlock (&tunnels_lock);
3225 GST_ERROR ("client %p: tunnel session %s already existed", client,
3231 static GstRTSPResult
3232 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
3234 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3235 GstRTSPClientPrivate *priv = client->priv;
3237 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
3240 /* ignore error, it'll only be a problem when the client does a POST again */
3241 remember_tunnel (client);
3247 handle_tunnel (GstRTSPClient * client)
3249 GstRTSPClientPrivate *priv = client->priv;
3250 GstRTSPClient *oclient;
3251 GstRTSPClientPrivate *opriv;
3252 const gchar *tunnelid;
3254 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
3255 if (tunnelid == NULL)
3258 /* check for previous tunnel */
3259 g_mutex_lock (&tunnels_lock);
3260 oclient = g_hash_table_lookup (tunnels, tunnelid);
3262 if (oclient == NULL) {
3263 /* no previous tunnel, remember tunnel */
3264 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
3265 g_mutex_unlock (&tunnels_lock);
3267 GST_INFO ("client %p: no previous tunnel found, remembering tunnel (%p)",
3268 client, priv->connection);
3270 /* merge both tunnels into the first client */
3271 /* remove the old client from the table. ref before because removing it will
3272 * remove the ref to it. */
3273 g_object_ref (oclient);
3274 g_hash_table_remove (tunnels, tunnelid);
3275 g_mutex_unlock (&tunnels_lock);
3277 opriv = oclient->priv;
3279 if (opriv->watch == NULL)
3282 GST_INFO ("client %p: found previous tunnel %p (old %p, new %p)", client,
3283 oclient, opriv->connection, priv->connection);
3285 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
3286 gst_rtsp_watch_reset (priv->watch);
3287 gst_rtsp_watch_reset (opriv->watch);
3288 g_object_unref (oclient);
3290 /* the old client owns the tunnel now, the new one will be freed */
3291 g_source_destroy ((GSource *) priv->watch);
3293 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
3301 GST_ERROR ("client %p: no tunnelid provided", client);
3306 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
3307 g_object_unref (oclient);
3312 static GstRTSPStatusCode
3313 tunnel_get (GstRTSPWatch * watch, gpointer user_data)
3315 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3317 GST_INFO ("client %p: tunnel get (connection %p)", client,
3318 client->priv->connection);
3320 if (!handle_tunnel (client)) {
3321 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
3324 return GST_RTSP_STS_OK;
3327 static GstRTSPResult
3328 tunnel_post (GstRTSPWatch * watch, gpointer user_data)
3330 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3332 GST_INFO ("client %p: tunnel post (connection %p)", client,
3333 client->priv->connection);
3335 if (!handle_tunnel (client)) {
3336 return GST_RTSP_ERROR;
3342 static GstRTSPResult
3343 tunnel_http_response (GstRTSPWatch * watch, GstRTSPMessage * request,
3344 GstRTSPMessage * response, gpointer user_data)
3346 GstRTSPClientClass *klass;
3348 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3349 klass = GST_RTSP_CLIENT_GET_CLASS (client);
3351 if (klass->tunnel_http_response) {
3352 klass->tunnel_http_response (client, request, response);
3358 static GstRTSPWatchFuncs watch_funcs = {
3367 tunnel_http_response
3371 client_watch_notify (GstRTSPClient * client)
3373 GstRTSPClientPrivate *priv = client->priv;
3375 GST_INFO ("client %p: watch destroyed", client);
3377 g_main_context_unref (priv->watch_context);
3378 priv->watch_context = NULL;
3379 /* remove all sessions and so drop the extra client ref */
3380 gst_rtsp_client_session_filter (client, cleanup_session, NULL);
3381 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
3382 g_object_unref (client);
3386 * gst_rtsp_client_attach:
3387 * @client: a #GstRTSPClient
3388 * @context: (allow-none): a #GMainContext
3390 * Attaches @client to @context. When the mainloop for @context is run, the
3391 * client will be dispatched. When @context is %NULL, the default context will be
3394 * This function should be called when the client properties and urls are fully
3395 * configured and the client is ready to start.
3397 * Returns: the ID (greater than 0) for the source within the GMainContext.
3400 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
3402 GstRTSPClientPrivate *priv;
3405 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
3406 priv = client->priv;
3407 g_return_val_if_fail (priv->connection != NULL, 0);
3408 g_return_val_if_fail (priv->watch == NULL, 0);
3410 /* make sure noone will free the context before the watch is destroyed */
3411 priv->watch_context = g_main_context_ref (context);
3413 /* create watch for the connection and attach */
3414 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
3415 g_object_ref (client), (GDestroyNotify) client_watch_notify);
3416 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
3417 (GDestroyNotify) gst_rtsp_watch_unref);
3419 /* FIXME make this configurable. We don't want to do this yet because it will
3420 * be superceeded by a cache object later */
3421 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
3423 GST_INFO ("client %p: attaching to context %p", client, context);
3424 res = gst_rtsp_watch_attach (priv->watch, context);
3430 * gst_rtsp_client_session_filter:
3431 * @client: a #GstRTSPClient
3432 * @func: (scope call) (allow-none): a callback
3433 * @user_data: user data passed to @func
3435 * Call @func for each session managed by @client. The result value of @func
3436 * determines what happens to the session. @func will be called with @client
3437 * locked so no further actions on @client can be performed from @func.
3439 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
3442 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
3444 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
3445 * will also be added with an additional ref to the result #GList of this
3448 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each session.
3450 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
3451 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3452 * element in the #GList should be unreffed before the list is freed.
3455 gst_rtsp_client_session_filter (GstRTSPClient * client,
3456 GstRTSPClientSessionFilterFunc func, gpointer user_data)
3458 GstRTSPClientPrivate *priv;
3459 GList *result, *walk, *next;
3460 GHashTable *visited;
3463 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
3465 priv = client->priv;
3469 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
3471 g_mutex_lock (&priv->lock);
3473 cookie = priv->sessions_cookie;
3474 for (walk = priv->sessions; walk; walk = next) {
3475 GstRTSPSession *sess = walk->data;
3476 GstRTSPFilterResult res;
3479 next = g_list_next (walk);
3482 /* only visit each session once */
3483 if (g_hash_table_contains (visited, sess))
3486 g_hash_table_add (visited, g_object_ref (sess));
3487 g_mutex_unlock (&priv->lock);
3489 res = func (client, sess, user_data);
3491 g_mutex_lock (&priv->lock);
3493 res = GST_RTSP_FILTER_REF;
3495 changed = (cookie != priv->sessions_cookie);
3498 case GST_RTSP_FILTER_REMOVE:
3499 /* stop watching the session and pretend it went away, if the list was
3500 * changed, we can't use the current list position, try to see if we
3501 * still have the session */
3502 client_unwatch_session (client, sess, changed ? NULL : walk);
3503 cookie = priv->sessions_cookie;
3505 case GST_RTSP_FILTER_REF:
3506 result = g_list_prepend (result, g_object_ref (sess));
3508 case GST_RTSP_FILTER_KEEP:
3515 g_mutex_unlock (&priv->lock);
3518 g_hash_table_unref (visited);