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, 0, NULL, NULL, g_cclosure_marshal_generic,
266 G_TYPE_NONE, 2, GST_TYPE_RTSP_CONTEXT, G_TYPE_POINTER);
269 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
270 g_mutex_init (&tunnels_lock);
272 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
276 gst_rtsp_client_init (GstRTSPClient * client)
278 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
282 g_mutex_init (&priv->lock);
283 g_mutex_init (&priv->send_lock);
284 g_mutex_init (&priv->watch_lock);
286 priv->drop_backlog = DEFAULT_DROP_BACKLOG;
287 priv->transports = g_hash_table_new (g_direct_hash, g_direct_equal);
290 static GstRTSPFilterResult
291 filter_session_media (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
294 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
296 return GST_RTSP_FILTER_REMOVE;
300 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
302 GstRTSPClientPrivate *priv = client->priv;
304 g_mutex_lock (&priv->lock);
305 /* check if we already know about this session */
306 if (g_list_find (priv->sessions, session) == NULL) {
307 GST_INFO ("watching session %p", session);
309 priv->sessions = g_list_prepend (priv->sessions, g_object_ref (session));
310 priv->sessions_cookie++;
312 /* connect removed session handler, it will be disconnected when the last
313 * session gets removed */
314 if (priv->session_removed_id == 0)
315 priv->session_removed_id = g_signal_connect_data (priv->session_pool,
316 "session-removed", G_CALLBACK (client_session_removed),
317 g_object_ref (client), (GClosureNotify) g_object_unref, 0);
319 g_mutex_unlock (&priv->lock);
324 /* should be called with lock */
326 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session,
329 GstRTSPClientPrivate *priv = client->priv;
331 GST_INFO ("client %p: unwatch session %p", client, session);
334 link = g_list_find (priv->sessions, session);
339 priv->sessions = g_list_delete_link (priv->sessions, link);
340 priv->sessions_cookie++;
342 /* if this was the last session, disconnect the handler.
343 * This will also drop the extra client ref */
344 if (!priv->sessions) {
345 g_signal_handler_disconnect (priv->session_pool, priv->session_removed_id);
346 priv->session_removed_id = 0;
349 /* remove the session */
350 g_object_unref (session);
353 static GstRTSPFilterResult
354 cleanup_session (GstRTSPClient * client, GstRTSPSession * sess,
357 /* unlink all media managed in this session. This needs to happen
358 * without the client lock, so we really want to do it here. */
359 gst_rtsp_session_filter (sess, filter_session_media, client);
361 return GST_RTSP_FILTER_REMOVE;
364 /* A client is finalized when the connection is broken */
366 gst_rtsp_client_finalize (GObject * obj)
368 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
369 GstRTSPClientPrivate *priv = client->priv;
371 GST_INFO ("finalize client %p", client);
374 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
375 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
378 g_source_destroy ((GSource *) priv->watch);
380 if (priv->watch_context)
381 g_main_context_unref (priv->watch_context);
383 /* all sessions should have been removed by now. We keep a ref to
384 * the client object for the session removed handler. The ref is
385 * dropped when the last session is removed from the list. */
386 g_assert (priv->sessions == NULL);
387 g_assert (priv->session_removed_id == 0);
389 g_hash_table_unref (priv->transports);
391 if (priv->connection)
392 gst_rtsp_connection_free (priv->connection);
393 if (priv->session_pool) {
394 g_object_unref (priv->session_pool);
396 if (priv->mount_points)
397 g_object_unref (priv->mount_points);
399 g_object_unref (priv->auth);
400 if (priv->thread_pool)
401 g_object_unref (priv->thread_pool);
406 gst_rtsp_media_unprepare (priv->media);
407 g_object_unref (priv->media);
410 g_free (priv->server_ip);
411 g_mutex_clear (&priv->lock);
412 g_mutex_clear (&priv->send_lock);
413 g_mutex_clear (&priv->watch_lock);
415 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
419 gst_rtsp_client_get_property (GObject * object, guint propid,
420 GValue * value, GParamSpec * pspec)
422 GstRTSPClient *client = GST_RTSP_CLIENT (object);
423 GstRTSPClientPrivate *priv = client->priv;
426 case PROP_SESSION_POOL:
427 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
429 case PROP_MOUNT_POINTS:
430 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
432 case PROP_DROP_BACKLOG:
433 g_value_set_boolean (value, priv->drop_backlog);
436 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
441 gst_rtsp_client_set_property (GObject * object, guint propid,
442 const GValue * value, GParamSpec * pspec)
444 GstRTSPClient *client = GST_RTSP_CLIENT (object);
445 GstRTSPClientPrivate *priv = client->priv;
448 case PROP_SESSION_POOL:
449 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
451 case PROP_MOUNT_POINTS:
452 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
454 case PROP_DROP_BACKLOG:
455 g_mutex_lock (&priv->lock);
456 priv->drop_backlog = g_value_get_boolean (value);
457 g_mutex_unlock (&priv->lock);
460 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
465 * gst_rtsp_client_new:
467 * Create a new #GstRTSPClient instance.
469 * Returns: (transfer full): a new #GstRTSPClient
472 gst_rtsp_client_new (void)
474 GstRTSPClient *result;
476 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
482 send_message (GstRTSPClient * client, GstRTSPContext * ctx,
483 GstRTSPMessage * message, gboolean close)
485 GstRTSPClientPrivate *priv = client->priv;
487 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
488 "GStreamer RTSP server");
490 /* remove any previous header */
491 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
493 /* add the new session header for new session ids */
495 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
496 gst_rtsp_session_get_header (ctx->session));
499 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
500 gst_rtsp_message_dump (message);
504 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
506 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE],
509 g_mutex_lock (&priv->send_lock);
511 priv->send_func (client, message, close, priv->send_data);
512 g_mutex_unlock (&priv->send_lock);
514 gst_rtsp_message_unset (message);
518 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
519 GstRTSPContext * ctx)
521 gst_rtsp_message_init_response (ctx->response, code,
522 gst_rtsp_status_as_text (code), ctx->request);
526 send_message (client, ctx, ctx->response, FALSE);
530 send_option_not_supported_response (GstRTSPClient * client,
531 GstRTSPContext * ctx, const gchar * unsupported_options)
533 GstRTSPStatusCode code = GST_RTSP_STS_OPTION_NOT_SUPPORTED;
535 gst_rtsp_message_init_response (ctx->response, code,
536 gst_rtsp_status_as_text (code), ctx->request);
538 if (unsupported_options != NULL) {
539 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_UNSUPPORTED,
540 unsupported_options);
545 send_message (client, ctx, ctx->response, FALSE);
549 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
551 if (path1 == NULL || path2 == NULL)
554 if (strlen (path1) != len2)
557 if (strncmp (path1, path2, len2))
563 /* this function is called to initially find the media for the DESCRIBE request
564 * but is cached for when the same client (without breaking the connection) is
565 * doing a setup for the exact same url. */
566 static GstRTSPMedia *
567 find_media (GstRTSPClient * client, GstRTSPContext * ctx, gchar * path,
570 GstRTSPClientPrivate *priv = client->priv;
571 GstRTSPMediaFactory *factory;
575 /* find the longest matching factory for the uri first */
576 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
580 ctx->factory = factory;
582 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
583 goto no_factory_access;
585 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
591 path_len = strlen (path);
593 if (!paths_are_equal (priv->path, path, path_len)) {
594 GstRTSPThread *thread;
596 /* remove any previously cached values before we try to construct a new
602 gst_rtsp_media_unprepare (priv->media);
603 g_object_unref (priv->media);
607 /* prepare the media and add it to the pipeline */
608 if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
613 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
614 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
618 /* prepare the media */
619 if (!(gst_rtsp_media_prepare (media, thread)))
622 /* now keep track of the uri and the media */
623 priv->path = g_strndup (path, path_len);
626 /* we have seen this path before, used cached media */
629 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
632 g_object_unref (factory);
636 g_object_ref (media);
643 GST_ERROR ("client %p: no factory for path %s", client, path);
644 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
649 GST_ERROR ("client %p: not authorized to see factory path %s", client,
651 /* error reply is already sent */
656 GST_ERROR ("client %p: not authorized for factory path %s", client, path);
657 /* error reply is already sent */
662 GST_ERROR ("client %p: can't create media", client);
663 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
664 g_object_unref (factory);
670 GST_ERROR ("client %p: can't create thread", client);
671 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
672 g_object_unref (media);
674 g_object_unref (factory);
680 GST_ERROR ("client %p: can't prepare media", client);
681 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
682 g_object_unref (media);
684 g_object_unref (factory);
691 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
693 GstRTSPClientPrivate *priv = client->priv;
694 GstRTSPMessage message = { 0 };
695 GstRTSPResult res = GST_RTSP_OK;
700 gst_rtsp_message_init_data (&message, channel);
702 /* FIXME, need some sort of iovec RTSPMessage here */
703 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
706 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
708 g_mutex_lock (&priv->send_lock);
710 res = priv->send_func (client, &message, FALSE, priv->send_data);
711 g_mutex_unlock (&priv->send_lock);
713 gst_rtsp_message_steal_body (&message, &data, &usize);
714 gst_buffer_unmap (buffer, &map_info);
716 gst_rtsp_message_unset (&message);
718 return res == GST_RTSP_OK;
722 * gst_rtsp_client_close:
723 * @client: a #GstRTSPClient
725 * Close the connection of @client and remove all media it was managing.
730 gst_rtsp_client_close (GstRTSPClient * client)
732 GstRTSPClientPrivate *priv = client->priv;
733 const gchar *tunnelid;
735 GST_DEBUG ("client %p: closing connection", client);
737 if (priv->connection) {
738 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
739 g_mutex_lock (&tunnels_lock);
740 /* remove from tunnelids */
741 g_hash_table_remove (tunnels, tunnelid);
742 g_mutex_unlock (&tunnels_lock);
744 gst_rtsp_connection_close (priv->connection);
747 /* connection is now closed, destroy the watch which will also cause the
748 * closed signal to be emitted */
750 GST_DEBUG ("client %p: destroying watch", client);
751 g_source_destroy ((GSource *) priv->watch);
753 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
754 g_main_context_unref (priv->watch_context);
755 priv->watch_context = NULL;
760 default_make_path_from_uri (GstRTSPClient * client, const GstRTSPUrl * uri)
765 path = g_strconcat (uri->abspath, "?", uri->query, NULL);
767 path = g_strdup (uri->abspath);
773 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
775 GstRTSPClientPrivate *priv = client->priv;
776 GstRTSPClientClass *klass;
777 GstRTSPSession *session;
778 GstRTSPSessionMedia *sessmedia;
779 GstRTSPStatusCode code;
782 gboolean keep_session;
787 session = ctx->session;
792 klass = GST_RTSP_CLIENT_GET_CLASS (client);
793 path = klass->make_path_from_uri (client, ctx->uri);
795 /* get a handle to the configuration of the media in the session */
796 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
800 /* only aggregate control for now.. */
801 if (path[matched] != '\0')
806 ctx->sessmedia = sessmedia;
808 /* we emit the signal before closing the connection */
809 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
812 /* make sure we unblock the backlog and don't accept new messages
814 if (priv->watch != NULL)
815 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
817 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
819 /* allow messages again so that we can send the reply */
820 if (priv->watch != NULL)
821 gst_rtsp_watch_set_flushing (priv->watch, FALSE);
823 /* unmanage the media in the session, returns false if all media session
825 keep_session = gst_rtsp_session_release_media (session, sessmedia);
827 /* construct the response now */
828 code = GST_RTSP_STS_OK;
829 gst_rtsp_message_init_response (ctx->response, code,
830 gst_rtsp_status_as_text (code), ctx->request);
832 send_message (client, ctx, ctx->response, TRUE);
835 /* remove the session */
836 gst_rtsp_session_pool_remove (priv->session_pool, session);
844 GST_ERROR ("client %p: no session", client);
845 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
850 GST_ERROR ("client %p: no uri supplied", client);
851 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
856 GST_ERROR ("client %p: no media for uri", client);
857 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
863 GST_ERROR ("client %p: no aggregate path %s", client, path);
864 send_generic_response (client,
865 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
872 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
876 res = gst_rtsp_params_set (client, ctx);
882 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
886 res = gst_rtsp_params_get (client, ctx);
892 handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
898 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
899 if (res != GST_RTSP_OK)
903 /* no body, keep-alive request */
904 send_generic_response (client, GST_RTSP_STS_OK, ctx);
906 /* there is a body, handle the params */
907 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
908 if (res != GST_RTSP_OK)
911 send_message (client, ctx, ctx->response, FALSE);
914 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
922 GST_ERROR ("client %p: bad request", client);
923 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
929 handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
935 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
936 if (res != GST_RTSP_OK)
940 /* no body, keep-alive request */
941 send_generic_response (client, GST_RTSP_STS_OK, ctx);
943 /* there is a body, handle the params */
944 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
945 if (res != GST_RTSP_OK)
948 send_message (client, ctx, ctx->response, FALSE);
951 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
959 GST_ERROR ("client %p: bad request", client);
960 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
966 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
968 GstRTSPSession *session;
969 GstRTSPClientClass *klass;
970 GstRTSPSessionMedia *sessmedia;
971 GstRTSPStatusCode code;
972 GstRTSPState rtspstate;
976 if (!(session = ctx->session))
982 klass = GST_RTSP_CLIENT_GET_CLASS (client);
983 path = klass->make_path_from_uri (client, ctx->uri);
985 /* get a handle to the configuration of the media in the session */
986 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
990 if (path[matched] != '\0')
995 ctx->sessmedia = sessmedia;
997 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
998 /* the session state must be playing or recording */
999 if (rtspstate != GST_RTSP_STATE_PLAYING &&
1000 rtspstate != GST_RTSP_STATE_RECORDING)
1003 /* then pause sending */
1004 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
1006 /* construct the response now */
1007 code = GST_RTSP_STS_OK;
1008 gst_rtsp_message_init_response (ctx->response, code,
1009 gst_rtsp_status_as_text (code), ctx->request);
1011 send_message (client, ctx, ctx->response, FALSE);
1013 /* the state is now READY */
1014 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1016 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
1023 GST_ERROR ("client %p: no seesion", client);
1024 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1029 GST_ERROR ("client %p: no uri supplied", client);
1030 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1035 GST_ERROR ("client %p: no media for uri", client);
1036 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1042 GST_ERROR ("client %p: no aggregate path %s", client, path);
1043 send_generic_response (client,
1044 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1050 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
1051 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1057 /* convert @url and @path to a URL used as a content base for the factory
1058 * located at @path */
1060 make_base_url (GstRTSPClient * client, GstRTSPUrl * url, const gchar * path)
1066 /* check for trailing '/' and append one */
1067 trail = (path[strlen (path) - 1] != '/' ? "/" : "");
1072 tmp.abspath = g_strdup_printf ("%s%s", path, trail);
1074 result = gst_rtsp_url_get_request_uri (&tmp);
1075 g_free (tmp.abspath);
1081 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
1083 GstRTSPSession *session;
1084 GstRTSPClientClass *klass;
1085 GstRTSPSessionMedia *sessmedia;
1086 GstRTSPMedia *media;
1087 GstRTSPStatusCode code;
1090 GstRTSPTimeRange *range;
1092 GstRTSPState rtspstate;
1093 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1094 gchar *path, *rtpinfo;
1097 if (!(session = ctx->session))
1100 if (!(uri = ctx->uri))
1103 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1104 path = klass->make_path_from_uri (client, uri);
1106 /* get a handle to the configuration of the media in the session */
1107 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1111 if (path[matched] != '\0')
1116 ctx->sessmedia = sessmedia;
1117 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1119 /* the session state must be playing or ready */
1120 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1121 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1124 /* in play we first unsuspend, media could be suspended from SDP or PAUSED */
1125 if (!gst_rtsp_media_unsuspend (media))
1126 goto unsuspend_failed;
1128 /* parse the range header if we have one */
1129 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1130 if (res == GST_RTSP_OK) {
1131 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1132 /* we have a range, seek to the position */
1134 gst_rtsp_media_seek (media, range);
1135 gst_rtsp_range_free (range);
1139 /* grab RTPInfo from the media now */
1140 rtpinfo = gst_rtsp_session_media_get_rtpinfo (sessmedia);
1142 /* construct the response now */
1143 code = GST_RTSP_STS_OK;
1144 gst_rtsp_message_init_response (ctx->response, code,
1145 gst_rtsp_status_as_text (code), ctx->request);
1147 /* add the RTP-Info header */
1149 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO,
1153 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1155 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1157 send_message (client, ctx, ctx->response, FALSE);
1159 /* start playing after sending the response */
1160 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1162 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1164 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1171 GST_ERROR ("client %p: no session", client);
1172 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1177 GST_ERROR ("client %p: no uri supplied", client);
1178 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1183 GST_ERROR ("client %p: media not found", client);
1184 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1189 GST_ERROR ("client %p: no aggregate path %s", client, path);
1190 send_generic_response (client,
1191 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1197 GST_ERROR ("client %p: not PLAYING or READY", client);
1198 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1204 GST_ERROR ("client %p: unsuspend failed", client);
1205 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1211 do_keepalive (GstRTSPSession * session)
1213 GST_INFO ("keep session %p alive", session);
1214 gst_rtsp_session_touch (session);
1217 /* parse @transport and return a valid transport in @tr. only transports
1218 * supported by @stream are returned. Returns FALSE if no valid transport
1221 parse_transport (const char *transport, GstRTSPStream * stream,
1222 GstRTSPTransport * tr)
1229 gst_rtsp_transport_init (tr);
1231 GST_DEBUG ("parsing transports %s", transport);
1233 transports = g_strsplit (transport, ",", 0);
1235 /* loop through the transports, try to parse */
1236 for (i = 0; transports[i]; i++) {
1237 res = gst_rtsp_transport_parse (transports[i], tr);
1238 if (res != GST_RTSP_OK) {
1239 /* no valid transport, search some more */
1240 GST_WARNING ("could not parse transport %s", transports[i]);
1244 /* we have a transport, see if it's supported */
1245 if (!gst_rtsp_stream_is_transport_supported (stream, tr)) {
1246 GST_WARNING ("unsupported transport %s", transports[i]);
1250 /* we have a valid transport */
1251 GST_INFO ("found valid transport %s", transports[i]);
1256 gst_rtsp_transport_init (tr);
1258 g_strfreev (transports);
1264 default_configure_client_media (GstRTSPClient * client, GstRTSPMedia * media,
1265 GstRTSPStream * stream, GstRTSPContext * ctx)
1267 GstRTSPMessage *request = ctx->request;
1268 gchar *blocksize_str;
1270 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1271 &blocksize_str, 0) == GST_RTSP_OK) {
1275 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1276 if (end == blocksize_str)
1279 /* we don't want to change the mtu when this media
1280 * can be shared because it impacts other clients */
1281 if (gst_rtsp_media_is_shared (media))
1284 if (blocksize > G_MAXUINT)
1285 blocksize = G_MAXUINT;
1287 gst_rtsp_stream_set_mtu (stream, blocksize);
1295 GST_ERROR_OBJECT (client, "failed to parse blocksize");
1296 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1302 default_configure_client_transport (GstRTSPClient * client,
1303 GstRTSPContext * ctx, GstRTSPTransport * ct)
1305 GstRTSPClientPrivate *priv = client->priv;
1307 /* we have a valid transport now, set the destination of the client. */
1308 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1309 gboolean use_client_settings;
1311 use_client_settings =
1312 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1314 if (ct->destination && use_client_settings) {
1315 GstRTSPAddress *addr;
1317 addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1318 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1323 gst_rtsp_address_free (addr);
1325 GstRTSPAddress *addr;
1326 GSocketFamily family;
1328 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1330 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1334 g_free (ct->destination);
1335 ct->destination = g_strdup (addr->address);
1336 ct->port.min = addr->port;
1337 ct->port.max = addr->port + addr->n_ports - 1;
1338 ct->ttl = addr->ttl;
1340 gst_rtsp_address_free (addr);
1345 url = gst_rtsp_connection_get_url (priv->connection);
1346 g_free (ct->destination);
1347 ct->destination = g_strdup (url->host);
1349 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1351 GSocketAddress *addr;
1353 sock = gst_rtsp_connection_get_read_socket (priv->connection);
1354 if ((addr = g_socket_get_remote_address (sock, NULL))) {
1355 /* our read port is the sender port of client */
1356 ct->client_port.min =
1357 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1358 g_object_unref (addr);
1360 if ((addr = g_socket_get_local_address (sock, NULL))) {
1361 ct->server_port.max =
1362 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1363 g_object_unref (addr);
1365 sock = gst_rtsp_connection_get_write_socket (priv->connection);
1366 if ((addr = g_socket_get_remote_address (sock, NULL))) {
1367 /* our write port is the receiver port of client */
1368 ct->client_port.max =
1369 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1370 g_object_unref (addr);
1372 if ((addr = g_socket_get_local_address (sock, NULL))) {
1373 ct->server_port.min =
1374 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1375 g_object_unref (addr);
1377 /* check if the client selected channels for TCP */
1378 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1379 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1389 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1394 static GstRTSPTransport *
1395 make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
1396 GstRTSPTransport * ct)
1398 GstRTSPTransport *st;
1400 GSocketFamily family;
1402 /* prepare the server transport */
1403 gst_rtsp_transport_new (&st);
1405 st->trans = ct->trans;
1406 st->profile = ct->profile;
1407 st->lower_transport = ct->lower_transport;
1409 addr = g_inet_address_new_from_string (ct->destination);
1412 GST_ERROR ("failed to get inet addr from client destination");
1413 family = G_SOCKET_FAMILY_IPV4;
1415 family = g_inet_address_get_family (addr);
1416 g_object_unref (addr);
1420 switch (st->lower_transport) {
1421 case GST_RTSP_LOWER_TRANS_UDP:
1422 st->client_port = ct->client_port;
1423 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
1425 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1426 st->port = ct->port;
1427 st->destination = g_strdup (ct->destination);
1430 case GST_RTSP_LOWER_TRANS_TCP:
1431 st->interleaved = ct->interleaved;
1432 st->client_port = ct->client_port;
1433 st->server_port = ct->server_port;
1438 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
1443 #define AES_128_KEY_LEN 16
1444 #define AES_256_KEY_LEN 32
1446 #define HMAC_32_KEY_LEN 4
1447 #define HMAC_80_KEY_LEN 10
1450 mikey_apply_policy (GstCaps * caps, GstMIKEYMessage * msg, guint8 policy)
1452 const gchar *srtp_cipher;
1453 const gchar *srtp_auth;
1454 const GstMIKEYPayload *sp;
1457 /* loop over Security policy until we find one containing policy */
1459 if ((sp = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, i)) == NULL)
1462 if (((GstMIKEYPayloadSP *) sp)->policy == policy)
1466 /* the default ciphers */
1467 srtp_cipher = "aes-128-icm";
1468 srtp_auth = "hmac-sha1-80";
1470 /* now override the defaults with what is in the Security Policy */
1474 /* collect all the params and go over them */
1475 len = gst_mikey_payload_sp_get_n_params (sp);
1476 for (i = 0; i < len; i++) {
1477 const GstMIKEYPayloadSPParam *param =
1478 gst_mikey_payload_sp_get_param (sp, i);
1480 switch (param->type) {
1481 case GST_MIKEY_SP_SRTP_ENC_ALG:
1482 switch (param->val[0]) {
1484 srtp_cipher = "null";
1488 srtp_cipher = "aes-128-icm";
1494 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
1495 switch (param->val[0]) {
1496 case AES_128_KEY_LEN:
1497 srtp_cipher = "aes-128-icm";
1499 case AES_256_KEY_LEN:
1500 srtp_cipher = "aes-256-icm";
1506 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1507 switch (param->val[0]) {
1513 srtp_auth = "hmac-sha1-80";
1519 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
1520 switch (param->val[0]) {
1521 case HMAC_32_KEY_LEN:
1522 srtp_auth = "hmac-sha1-32";
1524 case HMAC_80_KEY_LEN:
1525 srtp_auth = "hmac-sha1-80";
1531 case GST_MIKEY_SP_SRTP_SRTP_ENC:
1533 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
1540 /* now configure the SRTP parameters */
1541 gst_caps_set_simple (caps,
1542 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
1543 "srtp-auth", G_TYPE_STRING, srtp_auth,
1544 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
1545 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
1551 handle_mikey_data (GstRTSPClient * client, GstRTSPContext * ctx,
1552 guint8 * data, gsize size)
1554 GstMIKEYMessage *msg;
1556 GstCaps *caps = NULL;
1557 GstMIKEYPayloadKEMAC *kemac;
1558 const GstMIKEYPayloadKeyData *pkd;
1561 /* the MIKEY message contains a CSB or crypto session bundle. It is a
1562 * set of Crypto Sessions protected with the same master key.
1563 * In the context of SRTP, an RTP and its RTCP stream is part of a
1565 if ((msg = gst_mikey_message_new_from_data (data, size, NULL, NULL)) == NULL)
1568 /* we can only handle SRTP crypto sessions for now */
1569 if (msg->map_type != GST_MIKEY_MAP_TYPE_SRTP)
1570 goto invalid_map_type;
1572 /* get the number of crypto sessions. This maps SSRC to its
1573 * security parameters */
1574 n_cs = gst_mikey_message_get_n_cs (msg);
1576 goto no_crypto_sessions;
1578 /* we also need keys */
1579 if (!(kemac = (GstMIKEYPayloadKEMAC *) gst_mikey_message_find_payload
1580 (msg, GST_MIKEY_PT_KEMAC, 0)))
1583 /* we don't support encrypted keys */
1584 if (kemac->enc_alg != GST_MIKEY_ENC_NULL
1585 || kemac->mac_alg != GST_MIKEY_MAC_NULL)
1586 goto unsupported_encryption;
1588 /* get Key data sub-payload */
1589 pkd = (const GstMIKEYPayloadKeyData *)
1590 gst_mikey_payload_kemac_get_sub (&kemac->pt, 0);
1593 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
1596 /* go over all crypto sessions and create the security policy for each
1598 for (i = 0; i < n_cs; i++) {
1599 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
1601 caps = gst_caps_new_simple ("application/x-srtp",
1602 "ssrc", G_TYPE_UINT, map->ssrc,
1603 "roc", G_TYPE_UINT, map->roc, "srtp-key", GST_TYPE_BUFFER, key, NULL);
1604 mikey_apply_policy (caps, msg, map->policy);
1606 gst_rtsp_stream_update_crypto (ctx->stream, map->ssrc, caps);
1607 gst_caps_unref (caps);
1609 gst_mikey_message_unref (msg);
1610 gst_buffer_unref (key);
1617 GST_DEBUG_OBJECT (client, "failed to parse MIKEY message");
1622 GST_DEBUG_OBJECT (client, "invalid map type %d", msg->map_type);
1623 goto cleanup_message;
1627 GST_DEBUG_OBJECT (client, "no crypto sessions");
1628 goto cleanup_message;
1632 GST_DEBUG_OBJECT (client, "no keys found");
1633 goto cleanup_message;
1635 unsupported_encryption:
1637 GST_DEBUG_OBJECT (client, "unsupported key encryption");
1638 goto cleanup_message;
1642 gst_mikey_message_unref (msg);
1647 #define IS_STRIP_CHAR(c) (g_ascii_isspace ((guchar)(c)) || ((c) == '\"'))
1650 strip_chars (gchar * str)
1657 if (!IS_STRIP_CHAR (str[len]))
1661 for (s = str; *s && IS_STRIP_CHAR (*s); s++);
1662 memmove (str, s, len + 1);
1665 /* KeyMgmt = "KeyMgmt" ":" key-mgmt-spec 0*("," key-mgmt-spec)
1666 * key-mgmt-spec = "prot" "=" KMPID ";" ["uri" "=" %x22 URI %x22 ";"]
1669 handle_keymgmt (GstRTSPClient * client, GstRTSPContext * ctx, gchar * keymgmt)
1674 specs = g_strsplit (keymgmt, ",", 0);
1675 for (i = 0; specs[i]; i++) {
1678 split = g_strsplit (specs[i], ";", 0);
1679 for (j = 0; split[j]; j++) {
1680 g_strstrip (split[j]);
1681 if (g_str_has_prefix (split[j], "prot=")) {
1682 g_strstrip (split[j] + 5);
1683 if (!g_str_equal (split[j] + 5, "mikey"))
1685 GST_DEBUG ("found mikey");
1686 } else if (g_str_has_prefix (split[j], "uri=")) {
1687 strip_chars (split[j] + 4);
1688 GST_DEBUG ("found uri '%s'", split[j] + 4);
1689 } else if (g_str_has_prefix (split[j], "data=")) {
1692 strip_chars (split[j] + 5);
1693 GST_DEBUG ("found data '%s'", split[j] + 5);
1694 data = g_base64_decode_inplace (split[j] + 5, &size);
1695 handle_mikey_data (client, ctx, data, size);
1705 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
1707 GstRTSPClientPrivate *priv = client->priv;
1710 gchar *transport, *keymgmt;
1711 GstRTSPTransport *ct, *st;
1712 GstRTSPStatusCode code;
1713 GstRTSPSession *session;
1714 GstRTSPStreamTransport *trans;
1716 GstRTSPSessionMedia *sessmedia;
1717 GstRTSPMedia *media;
1718 GstRTSPStream *stream;
1719 GstRTSPState rtspstate;
1720 GstRTSPClientClass *klass;
1721 gchar *path, *control;
1723 gboolean new_session = FALSE;
1729 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1730 path = klass->make_path_from_uri (client, uri);
1732 /* parse the transport */
1734 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
1736 if (res != GST_RTSP_OK)
1739 /* we create the session after parsing stuff so that we don't make
1740 * a session for malformed requests */
1741 if (priv->session_pool == NULL)
1744 session = ctx->session;
1747 g_object_ref (session);
1748 /* get a handle to the configuration of the media in the session, this can
1749 * return NULL if this is a new url to manage in this session. */
1750 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1752 /* we need a new media configuration in this session */
1756 /* we have no session media, find one and manage it */
1757 if (sessmedia == NULL) {
1758 /* get a handle to the configuration of the media in the session */
1759 media = find_media (client, ctx, path, &matched);
1761 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1762 g_object_ref (media);
1764 goto media_not_found;
1766 /* no media, not found then */
1768 goto media_not_found_no_reply;
1770 if (path[matched] == '\0')
1771 goto control_not_found;
1773 /* path is what matched. */
1774 path[matched] = '\0';
1775 /* control is remainder */
1776 control = &path[matched + 1];
1778 /* find the stream now using the control part */
1779 stream = gst_rtsp_media_find_stream (media, control);
1781 goto stream_not_found;
1783 /* now we have a uri identifying a valid media and stream */
1784 ctx->stream = stream;
1787 if (session == NULL) {
1788 /* create a session if this fails we probably reached our session limit or
1790 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1791 goto service_unavailable;
1793 /* make sure this client is closed when the session is closed */
1794 client_watch_session (client, session);
1797 /* signal new session */
1798 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1801 ctx->session = session;
1804 if (!klass->configure_client_media (client, media, stream, ctx))
1805 goto configure_media_failed_no_reply;
1807 gst_rtsp_transport_new (&ct);
1809 /* parse and find a usable supported transport */
1810 if (!parse_transport (transport, stream, ct))
1811 goto unsupported_transports;
1813 /* update the client transport */
1814 if (!klass->configure_client_transport (client, ctx, ct))
1815 goto unsupported_client_transport;
1817 /* parse the keymgmt */
1818 if (gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_KEYMGMT,
1819 &keymgmt, 0) == GST_RTSP_OK) {
1820 if (!handle_keymgmt (client, ctx, keymgmt))
1824 if (sessmedia == NULL) {
1825 /* manage the media in our session now, if not done already */
1826 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1827 /* if we stil have no media, error */
1828 if (sessmedia == NULL)
1829 goto sessmedia_unavailable;
1831 g_object_unref (media);
1834 ctx->sessmedia = sessmedia;
1836 /* set in the session media transport */
1837 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1841 /* configure the url used to set this transport, this we will use when
1842 * generating the response for the PLAY request */
1843 gst_rtsp_stream_transport_set_url (trans, uri);
1844 /* configure keepalive for this transport */
1845 gst_rtsp_stream_transport_set_keepalive (trans,
1846 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1848 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1849 /* our callbacks to send data on this TCP connection */
1850 gst_rtsp_stream_transport_set_callbacks (trans,
1851 (GstRTSPSendFunc) do_send_data,
1852 (GstRTSPSendFunc) do_send_data, client, NULL);
1854 g_hash_table_insert (priv->transports,
1855 GINT_TO_POINTER (ct->interleaved.min), trans);
1856 g_hash_table_insert (priv->transports,
1857 GINT_TO_POINTER (ct->interleaved.max), trans);
1860 /* create and serialize the server transport */
1861 st = make_server_transport (client, ctx, ct);
1862 trans_str = gst_rtsp_transport_as_text (st);
1863 gst_rtsp_transport_free (st);
1865 /* construct the response now */
1866 code = GST_RTSP_STS_OK;
1867 gst_rtsp_message_init_response (ctx->response, code,
1868 gst_rtsp_status_as_text (code), ctx->request);
1870 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
1874 send_message (client, ctx, ctx->response, FALSE);
1876 /* update the state */
1877 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1878 switch (rtspstate) {
1879 case GST_RTSP_STATE_PLAYING:
1880 case GST_RTSP_STATE_RECORDING:
1881 case GST_RTSP_STATE_READY:
1882 /* no state change */
1885 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1888 g_object_unref (session);
1891 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
1898 GST_ERROR ("client %p: no uri", client);
1899 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1904 GST_ERROR ("client %p: no transport", client);
1905 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1910 GST_ERROR ("client %p: no session pool configured", client);
1911 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1914 media_not_found_no_reply:
1916 GST_ERROR ("client %p: media '%s' not found", client, path);
1917 /* error reply is already sent */
1922 GST_ERROR ("client %p: media '%s' not found", client, path);
1923 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1928 GST_ERROR ("client %p: no control in path '%s'", client, path);
1929 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1930 g_object_unref (media);
1935 GST_ERROR ("client %p: stream '%s' not found", client, control);
1936 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1937 g_object_unref (media);
1940 service_unavailable:
1942 GST_ERROR ("client %p: can't create session", client);
1943 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1944 g_object_unref (media);
1947 sessmedia_unavailable:
1949 GST_ERROR ("client %p: can't create session media", client);
1950 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1951 g_object_unref (media);
1952 goto cleanup_session;
1954 configure_media_failed_no_reply:
1956 GST_ERROR ("client %p: configure_media failed", client);
1957 /* error reply is already sent */
1958 goto cleanup_session;
1960 unsupported_transports:
1962 GST_ERROR ("client %p: unsupported transports", client);
1963 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1964 goto cleanup_transport;
1966 unsupported_client_transport:
1968 GST_ERROR ("client %p: unsupported client transport", client);
1969 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1970 goto cleanup_transport;
1974 GST_ERROR ("client %p: keymgmt error", client);
1975 send_generic_response (client, GST_RTSP_STS_KEY_MANAGEMENT_FAILURE, ctx);
1976 goto cleanup_transport;
1980 gst_rtsp_transport_free (ct);
1983 gst_rtsp_session_pool_remove (priv->session_pool, session);
1984 g_object_unref (session);
1991 static GstSDPMessage *
1992 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1994 GstRTSPClientPrivate *priv = client->priv;
1999 gst_sdp_message_new (&sdp);
2001 /* some standard things first */
2002 gst_sdp_message_set_version (sdp, "0");
2009 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
2012 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
2013 gst_sdp_message_set_information (sdp, "rtsp-server");
2014 gst_sdp_message_add_time (sdp, "0", "0", NULL);
2015 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
2016 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
2017 gst_sdp_message_add_attribute (sdp, "control", "*");
2019 info.is_ipv6 = priv->is_ipv6;
2020 info.server_ip = priv->server_ip;
2022 /* create an SDP for the media object */
2023 if (!gst_rtsp_media_setup_sdp (media, sdp, &info))
2031 GST_ERROR ("client %p: could not create SDP", client);
2032 gst_sdp_message_free (sdp);
2037 /* for the describe we must generate an SDP */
2039 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
2041 GstRTSPClientPrivate *priv = client->priv;
2046 GstRTSPMedia *media;
2047 GstRTSPClientClass *klass;
2049 klass = GST_RTSP_CLIENT_GET_CLASS (client);
2054 /* check what kind of format is accepted, we don't really do anything with it
2055 * and always return SDP for now. */
2060 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
2062 if (res == GST_RTSP_ENOTIMPL)
2065 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
2069 if (!priv->mount_points)
2070 goto no_mount_points;
2072 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
2075 /* find the media object for the uri */
2076 if (!(media = find_media (client, ctx, path, NULL)))
2079 /* create an SDP for the media object on this client */
2080 if (!(sdp = klass->create_sdp (client, media)))
2083 /* we suspend after the describe */
2084 gst_rtsp_media_suspend (media);
2085 g_object_unref (media);
2087 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2088 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2090 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
2093 /* content base for some clients that might screw up creating the setup uri */
2094 str = make_base_url (client, ctx->uri, path);
2097 GST_INFO ("adding content-base: %s", str);
2098 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE, str);
2100 /* add SDP to the response body */
2101 str = gst_sdp_message_as_text (sdp);
2102 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
2103 gst_sdp_message_free (sdp);
2105 send_message (client, ctx, ctx->response, FALSE);
2107 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
2115 GST_ERROR ("client %p: no uri", client);
2116 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2121 GST_ERROR ("client %p: no mount points configured", client);
2122 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2127 GST_ERROR ("client %p: can't find path for url", client);
2128 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2133 GST_ERROR ("client %p: no media", client);
2135 /* error reply is already sent */
2140 GST_ERROR ("client %p: can't create SDP", client);
2141 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2143 g_object_unref (media);
2149 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
2151 GstRTSPMethod options;
2154 options = GST_RTSP_DESCRIBE |
2159 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
2161 str = gst_rtsp_options_as_text (options);
2163 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2164 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2166 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
2169 send_message (client, ctx, ctx->response, FALSE);
2171 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
2177 /* remove duplicate and trailing '/' */
2179 sanitize_uri (GstRTSPUrl * uri)
2183 gboolean have_slash, prev_slash;
2185 s = d = uri->abspath;
2186 len = strlen (uri->abspath);
2190 for (i = 0; i < len; i++) {
2191 have_slash = s[i] == '/';
2193 if (!have_slash || !prev_slash)
2195 prev_slash = have_slash;
2197 len = d - uri->abspath;
2198 /* don't remove the first slash if that's the only thing left */
2199 if (len > 1 && *(d - 1) == '/')
2204 /* is called when the session is removed from its session pool. */
2206 client_session_removed (GstRTSPSessionPool * pool, GstRTSPSession * session,
2207 GstRTSPClient * client)
2209 GstRTSPClientPrivate *priv = client->priv;
2211 GST_INFO ("client %p: session %p removed", client, session);
2213 g_mutex_lock (&priv->lock);
2214 if (priv->watch != NULL)
2215 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 0);
2216 client_unwatch_session (client, session, NULL);
2217 if (priv->watch != NULL)
2218 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
2219 g_mutex_unlock (&priv->lock);
2222 /* Returns TRUE if there are no Require headers, otherwise returns FALSE
2223 * and also returns a newly-allocated string of (comma-separated) unsupported
2224 * options in the unsupported_reqs variable .
2226 * There may be multiple Require headers, but we must send one single
2227 * Unsupported header with all the unsupported options as response. If
2228 * an incoming Require header contained a comma-separated list of options
2229 * GstRtspConnection will already have split that list up into multiple
2232 * TODO: allow the application to decide what features are supported
2235 check_request_requirements (GstRTSPMessage * msg, gchar ** unsupported_reqs)
2238 GPtrArray *arr = NULL;
2244 res = gst_rtsp_message_get_header (msg, GST_RTSP_HDR_REQUIRE, &reqs, i++);
2246 if (res == GST_RTSP_ENOTIMPL)
2250 arr = g_ptr_array_new_with_free_func ((GDestroyNotify) g_free);
2252 g_ptr_array_add (arr, g_strdup (reqs));
2256 /* if we don't have any Require headers at all, all is fine */
2260 /* otherwise we've now processed at all the Require headers */
2261 g_ptr_array_add (arr, NULL);
2263 /* for now we don't commit to supporting anything, so will just report
2264 * all of the required options as unsupported */
2265 *unsupported_reqs = g_strjoinv (", ", (gchar **) arr->pdata);
2267 g_ptr_array_unref (arr);
2272 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
2274 GstRTSPClientPrivate *priv = client->priv;
2275 GstRTSPMethod method;
2276 const gchar *uristr;
2277 GstRTSPUrl *uri = NULL;
2278 GstRTSPVersion version;
2280 GstRTSPSession *session = NULL;
2281 GstRTSPContext sctx = { NULL }, *ctx;
2282 GstRTSPMessage response = { 0 };
2283 gchar *unsupported_reqs = NULL;
2286 if (!(ctx = gst_rtsp_context_get_current ())) {
2288 ctx->auth = priv->auth;
2289 gst_rtsp_context_push_current (ctx);
2292 ctx->conn = priv->connection;
2293 ctx->client = client;
2294 ctx->request = request;
2295 ctx->response = &response;
2297 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2298 gst_rtsp_message_dump (request);
2301 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
2303 GST_INFO ("client %p: received a request %s %s %s", client,
2304 gst_rtsp_method_as_text (method), uristr,
2305 gst_rtsp_version_as_text (version));
2307 /* we can only handle 1.0 requests */
2308 if (version != GST_RTSP_VERSION_1_0)
2311 ctx->method = method;
2313 /* we always try to parse the url first */
2314 if (strcmp (uristr, "*") == 0) {
2315 /* special case where we have * as uri, keep uri = NULL */
2316 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
2317 /* check if the uristr is an absolute path <=> scheme and host information
2321 scheme = g_uri_parse_scheme (uristr);
2322 if (scheme == NULL && g_str_has_prefix (uristr, "/")) {
2323 gchar *absolute_uristr = NULL;
2325 GST_WARNING_OBJECT (client, "request doesn't contain absolute url");
2326 if (priv->server_ip == NULL) {
2327 GST_WARNING_OBJECT (client, "host information missing");
2332 g_strdup_printf ("rtsp://%s%s", priv->server_ip, uristr);
2334 GST_DEBUG_OBJECT (client, "absolute url: %s", absolute_uristr);
2335 if (gst_rtsp_url_parse (absolute_uristr, &uri) != GST_RTSP_OK) {
2336 g_free (absolute_uristr);
2339 g_free (absolute_uristr);
2346 /* get the session if there is any */
2347 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
2348 if (res == GST_RTSP_OK) {
2349 if (priv->session_pool == NULL)
2352 /* we had a session in the request, find it again */
2353 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2354 goto session_not_found;
2356 /* we add the session to the client list of watched sessions. When a session
2357 * disappears because it times out, we will be notified. If all sessions are
2358 * gone, we will close the connection */
2359 client_watch_session (client, session);
2362 /* sanitize the uri */
2366 ctx->session = session;
2368 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
2369 goto not_authorized;
2371 /* handle any 'Require' headers */
2372 if (!check_request_requirements (ctx->request, &unsupported_reqs))
2373 goto unsupported_requirement;
2375 /* the backlog must be unlimited while processing requests.
2376 * the causes of this are two cases of deadlocks while streaming over TCP:
2378 * 1. consider the scenario where the media pipeline's streaming thread
2379 * is blocking in the appsink (taking the appsink's preroll lock) because
2380 * the backlog is full. when a PAUSE request is received by the RTSP
2381 * client thread then the the state of the session media ought to change
2382 * to PAUSED. while most elements in the pipeline can change state this
2383 * can never happen for the appsink since its preroll lock is taken by
2386 * 2. consider the scenario where the media pipeline's streaming thread
2387 * is blocking in the appsink new_sample callback (taking the send lock
2388 * in RTSP client) because the backlog is full. when e.g. a GET request
2389 * is received by the RTSP client thread then a response ought to be sent
2390 * but this can never happen since it requires taking the send lock
2391 * already taken by another thread.
2393 * the reason that the backlog is never emptied is that the source used
2394 * for dequeing messages from the backlog is never dispatched because it
2395 * is attached to the same mainloop as the source receving RTSP requests and
2396 * therefore run by the RTSP client thread which is alreayd blocking.
2398 * without significant changes the easiest way to cope with this is to
2399 * not block indefinitely when the backlog is full, but rather let the
2400 * backlog grow in size. this in effect means that there can not be any
2401 * upper boundary on its size.
2403 if (priv->watch != NULL)
2404 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 0);
2406 /* now see what is asked and dispatch to a dedicated handler */
2408 case GST_RTSP_OPTIONS:
2409 handle_options_request (client, ctx);
2411 case GST_RTSP_DESCRIBE:
2412 handle_describe_request (client, ctx);
2414 case GST_RTSP_SETUP:
2415 handle_setup_request (client, ctx);
2418 handle_play_request (client, ctx);
2420 case GST_RTSP_PAUSE:
2421 handle_pause_request (client, ctx);
2423 case GST_RTSP_TEARDOWN:
2424 handle_teardown_request (client, ctx);
2426 case GST_RTSP_SET_PARAMETER:
2427 handle_set_param_request (client, ctx);
2429 case GST_RTSP_GET_PARAMETER:
2430 handle_get_param_request (client, ctx);
2432 case GST_RTSP_ANNOUNCE:
2433 case GST_RTSP_RECORD:
2434 case GST_RTSP_REDIRECT:
2435 if (priv->watch != NULL)
2436 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
2437 goto not_implemented;
2438 case GST_RTSP_INVALID:
2440 if (priv->watch != NULL)
2441 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
2445 if (priv->watch != NULL)
2446 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
2450 gst_rtsp_context_pop_current (ctx);
2452 g_object_unref (session);
2454 gst_rtsp_url_free (uri);
2460 GST_ERROR ("client %p: version %d not supported", client, version);
2461 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
2467 GST_ERROR ("client %p: bad request", client);
2468 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2473 GST_ERROR ("client %p: no pool configured", client);
2474 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2479 GST_ERROR ("client %p: session not found", client);
2480 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2485 GST_ERROR ("client %p: not allowed", client);
2486 /* error reply is already sent */
2489 unsupported_requirement:
2491 GST_ERROR ("client %p: Required option is not supported (%s)", client,
2493 send_option_not_supported_response (client, ctx, unsupported_reqs);
2494 g_free (unsupported_reqs);
2499 GST_ERROR ("client %p: method %d not implemented", client, method);
2500 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
2507 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
2509 GstRTSPClientPrivate *priv = client->priv;
2511 GstRTSPSession *session = NULL;
2512 GstRTSPContext sctx = { NULL }, *ctx;
2515 if (!(ctx = gst_rtsp_context_get_current ())) {
2517 ctx->auth = priv->auth;
2518 gst_rtsp_context_push_current (ctx);
2521 ctx->conn = priv->connection;
2522 ctx->client = client;
2523 ctx->request = NULL;
2525 ctx->method = GST_RTSP_INVALID;
2526 ctx->response = response;
2528 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2529 gst_rtsp_message_dump (response);
2532 GST_INFO ("client %p: received a response", client);
2534 /* get the session if there is any */
2536 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
2537 if (res == GST_RTSP_OK) {
2538 if (priv->session_pool == NULL)
2541 /* we had a session in the request, find it again */
2542 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2543 goto session_not_found;
2545 /* we add the session to the client list of watched sessions. When a session
2546 * disappears because it times out, we will be notified. If all sessions are
2547 * gone, we will close the connection */
2548 client_watch_session (client, session);
2551 ctx->session = session;
2553 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
2558 gst_rtsp_context_pop_current (ctx);
2560 g_object_unref (session);
2565 GST_ERROR ("client %p: no pool configured", client);
2570 GST_ERROR ("client %p: session not found", client);
2576 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2578 GstRTSPClientPrivate *priv = client->priv;
2584 GstRTSPStreamTransport *trans;
2586 /* find the stream for this message */
2587 res = gst_rtsp_message_parse_data (message, &channel);
2588 if (res != GST_RTSP_OK)
2591 gst_rtsp_message_steal_body (message, &data, &size);
2593 buffer = gst_buffer_new_wrapped (data, size);
2596 g_hash_table_lookup (priv->transports, GINT_TO_POINTER ((gint) channel));
2598 /* dispatch to the stream based on the channel number */
2599 gst_rtsp_stream_transport_recv_data (trans, channel, buffer);
2601 gst_buffer_unref (buffer);
2606 * gst_rtsp_client_set_session_pool:
2607 * @client: a #GstRTSPClient
2608 * @pool: (transfer none): a #GstRTSPSessionPool
2610 * Set @pool as the sessionpool for @client which it will use to find
2611 * or allocate sessions. the sessionpool is usually inherited from the server
2612 * that created the client but can be overridden later.
2615 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2616 GstRTSPSessionPool * pool)
2618 GstRTSPSessionPool *old;
2619 GstRTSPClientPrivate *priv;
2621 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2623 priv = client->priv;
2626 g_object_ref (pool);
2628 g_mutex_lock (&priv->lock);
2629 old = priv->session_pool;
2630 priv->session_pool = pool;
2632 if (priv->session_removed_id) {
2633 g_signal_handler_disconnect (old, priv->session_removed_id);
2634 priv->session_removed_id = 0;
2636 g_mutex_unlock (&priv->lock);
2638 /* FIXME, should remove all sessions from the old pool for this client */
2640 g_object_unref (old);
2644 * gst_rtsp_client_get_session_pool:
2645 * @client: a #GstRTSPClient
2647 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2649 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2651 GstRTSPSessionPool *
2652 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2654 GstRTSPClientPrivate *priv;
2655 GstRTSPSessionPool *result;
2657 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2659 priv = client->priv;
2661 g_mutex_lock (&priv->lock);
2662 if ((result = priv->session_pool))
2663 g_object_ref (result);
2664 g_mutex_unlock (&priv->lock);
2670 * gst_rtsp_client_set_mount_points:
2671 * @client: a #GstRTSPClient
2672 * @mounts: (transfer none): a #GstRTSPMountPoints
2674 * Set @mounts as the mount points for @client which it will use to map urls
2675 * to media streams. These mount points are usually inherited from the server that
2676 * created the client but can be overriden later.
2679 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2680 GstRTSPMountPoints * mounts)
2682 GstRTSPClientPrivate *priv;
2683 GstRTSPMountPoints *old;
2685 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2687 priv = client->priv;
2690 g_object_ref (mounts);
2692 g_mutex_lock (&priv->lock);
2693 old = priv->mount_points;
2694 priv->mount_points = mounts;
2695 g_mutex_unlock (&priv->lock);
2698 g_object_unref (old);
2702 * gst_rtsp_client_get_mount_points:
2703 * @client: a #GstRTSPClient
2705 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2707 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2709 GstRTSPMountPoints *
2710 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2712 GstRTSPClientPrivate *priv;
2713 GstRTSPMountPoints *result;
2715 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2717 priv = client->priv;
2719 g_mutex_lock (&priv->lock);
2720 if ((result = priv->mount_points))
2721 g_object_ref (result);
2722 g_mutex_unlock (&priv->lock);
2728 * gst_rtsp_client_set_auth:
2729 * @client: a #GstRTSPClient
2730 * @auth: (transfer none): a #GstRTSPAuth
2732 * configure @auth to be used as the authentication manager of @client.
2735 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2737 GstRTSPClientPrivate *priv;
2740 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2742 priv = client->priv;
2745 g_object_ref (auth);
2747 g_mutex_lock (&priv->lock);
2750 g_mutex_unlock (&priv->lock);
2753 g_object_unref (old);
2758 * gst_rtsp_client_get_auth:
2759 * @client: a #GstRTSPClient
2761 * Get the #GstRTSPAuth used as the authentication manager of @client.
2763 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2767 gst_rtsp_client_get_auth (GstRTSPClient * client)
2769 GstRTSPClientPrivate *priv;
2770 GstRTSPAuth *result;
2772 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2774 priv = client->priv;
2776 g_mutex_lock (&priv->lock);
2777 if ((result = priv->auth))
2778 g_object_ref (result);
2779 g_mutex_unlock (&priv->lock);
2785 * gst_rtsp_client_set_thread_pool:
2786 * @client: a #GstRTSPClient
2787 * @pool: (transfer none): a #GstRTSPThreadPool
2789 * configure @pool to be used as the thread pool of @client.
2792 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2793 GstRTSPThreadPool * pool)
2795 GstRTSPClientPrivate *priv;
2796 GstRTSPThreadPool *old;
2798 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2800 priv = client->priv;
2803 g_object_ref (pool);
2805 g_mutex_lock (&priv->lock);
2806 old = priv->thread_pool;
2807 priv->thread_pool = pool;
2808 g_mutex_unlock (&priv->lock);
2811 g_object_unref (old);
2815 * gst_rtsp_client_get_thread_pool:
2816 * @client: a #GstRTSPClient
2818 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2820 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2824 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2826 GstRTSPClientPrivate *priv;
2827 GstRTSPThreadPool *result;
2829 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2831 priv = client->priv;
2833 g_mutex_lock (&priv->lock);
2834 if ((result = priv->thread_pool))
2835 g_object_ref (result);
2836 g_mutex_unlock (&priv->lock);
2842 * gst_rtsp_client_set_connection:
2843 * @client: a #GstRTSPClient
2844 * @conn: (transfer full): a #GstRTSPConnection
2846 * Set the #GstRTSPConnection of @client. This function takes ownership of
2849 * Returns: %TRUE on success.
2852 gst_rtsp_client_set_connection (GstRTSPClient * client,
2853 GstRTSPConnection * conn)
2855 GstRTSPClientPrivate *priv;
2856 GSocket *read_socket;
2857 GSocketAddress *address;
2859 GError *error = NULL;
2861 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2862 g_return_val_if_fail (conn != NULL, FALSE);
2864 priv = client->priv;
2866 read_socket = gst_rtsp_connection_get_read_socket (conn);
2868 if (!(address = g_socket_get_local_address (read_socket, &error)))
2871 g_free (priv->server_ip);
2872 /* keep the original ip that the client connected to */
2873 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2874 GInetAddress *iaddr;
2876 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2878 /* socket might be ipv6 but adress still ipv4 */
2879 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2880 priv->server_ip = g_inet_address_to_string (iaddr);
2881 g_object_unref (address);
2883 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2884 priv->server_ip = g_strdup ("unknown");
2887 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2888 priv->server_ip, priv->is_ipv6);
2890 url = gst_rtsp_connection_get_url (conn);
2891 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2893 priv->connection = conn;
2900 GST_ERROR ("could not get local address %s", error->message);
2901 g_error_free (error);
2907 * gst_rtsp_client_get_connection:
2908 * @client: a #GstRTSPClient
2910 * Get the #GstRTSPConnection of @client.
2912 * Returns: (transfer none): the #GstRTSPConnection of @client.
2913 * The connection object returned remains valid until the client is freed.
2916 gst_rtsp_client_get_connection (GstRTSPClient * client)
2918 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2920 return client->priv->connection;
2924 * gst_rtsp_client_set_send_func:
2925 * @client: a #GstRTSPClient
2926 * @func: (scope notified): a #GstRTSPClientSendFunc
2927 * @user_data: (closure): user data passed to @func
2928 * @notify: (allow-none): called when @user_data is no longer in use
2930 * Set @func as the callback that will be called when a new message needs to be
2931 * sent to the client. @user_data is passed to @func and @notify is called when
2932 * @user_data is no longer in use.
2934 * By default, the client will send the messages on the #GstRTSPConnection that
2935 * was configured with gst_rtsp_client_attach() was called.
2938 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2939 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2941 GstRTSPClientPrivate *priv;
2942 GDestroyNotify old_notify;
2945 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2947 priv = client->priv;
2949 g_mutex_lock (&priv->send_lock);
2950 priv->send_func = func;
2951 old_notify = priv->send_notify;
2952 old_data = priv->send_data;
2953 priv->send_notify = notify;
2954 priv->send_data = user_data;
2955 g_mutex_unlock (&priv->send_lock);
2958 old_notify (old_data);
2962 * gst_rtsp_client_handle_message:
2963 * @client: a #GstRTSPClient
2964 * @message: (transfer none): an #GstRTSPMessage
2966 * Let the client handle @message.
2968 * Returns: a #GstRTSPResult.
2971 gst_rtsp_client_handle_message (GstRTSPClient * client,
2972 GstRTSPMessage * message)
2974 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2975 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2977 switch (message->type) {
2978 case GST_RTSP_MESSAGE_REQUEST:
2979 handle_request (client, message);
2981 case GST_RTSP_MESSAGE_RESPONSE:
2982 handle_response (client, message);
2984 case GST_RTSP_MESSAGE_DATA:
2985 handle_data (client, message);
2994 * gst_rtsp_client_send_message:
2995 * @client: a #GstRTSPClient
2996 * @session: (allow-none) (transfer none): a #GstRTSPSession to send
2997 * the message to or %NULL
2998 * @message: (transfer none): The #GstRTSPMessage to send
3000 * Send a message message to the remote end. @message must be a
3001 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
3004 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
3005 GstRTSPMessage * message)
3007 GstRTSPContext sctx = { NULL }
3009 GstRTSPClientPrivate *priv;
3011 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
3012 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3013 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
3014 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
3016 priv = client->priv;
3018 if (!(ctx = gst_rtsp_context_get_current ())) {
3020 ctx->auth = priv->auth;
3021 gst_rtsp_context_push_current (ctx);
3024 ctx->conn = priv->connection;
3025 ctx->client = client;
3026 ctx->session = session;
3028 send_message (client, ctx, message, FALSE);
3031 gst_rtsp_context_pop_current (ctx);
3036 static GstRTSPResult
3037 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
3038 gboolean close, gpointer user_data)
3040 GstRTSPClientPrivate *priv = client->priv;
3048 /* send the response and store the seq number so we can wait until it's
3049 * written to the client to close the connection */
3051 gst_rtsp_watch_send_message (priv->watch, message,
3052 close ? &priv->close_seq : NULL);
3053 if (ret == GST_RTSP_OK)
3056 if (ret != GST_RTSP_ENOMEM)
3060 if (priv->drop_backlog)
3063 /* queue was full, wait for more space */
3064 GST_DEBUG_OBJECT (client, "waiting for backlog");
3065 ret = gst_rtsp_watch_wait_backlog (priv->watch, &time);
3066 GST_DEBUG_OBJECT (client, "Resend due to backlog full");
3067 } while (ret != GST_RTSP_EINTR);
3074 GST_DEBUG_OBJECT (client, "got error %d", ret);
3079 static GstRTSPResult
3080 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
3083 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
3086 static GstRTSPResult
3087 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
3089 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3090 GstRTSPClientPrivate *priv = client->priv;
3092 if (priv->close_seq && priv->close_seq == cseq) {
3093 GST_INFO ("client %p: send close message", client);
3094 priv->close_seq = 0;
3095 gst_rtsp_client_close (client);
3101 static GstRTSPResult
3102 closed (GstRTSPWatch * watch, gpointer user_data)
3104 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3105 GstRTSPClientPrivate *priv = client->priv;
3106 const gchar *tunnelid;
3108 GST_INFO ("client %p: connection closed", client);
3110 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
3111 g_mutex_lock (&tunnels_lock);
3112 /* remove from tunnelids */
3113 g_hash_table_remove (tunnels, tunnelid);
3114 g_mutex_unlock (&tunnels_lock);
3117 gst_rtsp_watch_set_flushing (watch, TRUE);
3118 g_mutex_lock (&priv->watch_lock);
3119 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
3120 g_mutex_unlock (&priv->watch_lock);
3125 static GstRTSPResult
3126 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
3128 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3131 str = gst_rtsp_strresult (result);
3132 GST_INFO ("client %p: received an error %s", client, str);
3138 static GstRTSPResult
3139 error_full (GstRTSPWatch * watch, GstRTSPResult result,
3140 GstRTSPMessage * message, guint id, gpointer user_data)
3142 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3145 str = gst_rtsp_strresult (result);
3147 ("client %p: error when handling message %p with id %d: %s",
3148 client, message, id, str);
3155 remember_tunnel (GstRTSPClient * client)
3157 GstRTSPClientPrivate *priv = client->priv;
3158 const gchar *tunnelid;
3160 /* store client in the pending tunnels */
3161 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
3162 if (tunnelid == NULL)
3165 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
3167 /* we can't have two clients connecting with the same tunnelid */
3168 g_mutex_lock (&tunnels_lock);
3169 if (g_hash_table_lookup (tunnels, tunnelid))
3170 goto tunnel_existed;
3172 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
3173 g_mutex_unlock (&tunnels_lock);
3180 GST_ERROR ("client %p: no tunnelid provided", client);
3185 g_mutex_unlock (&tunnels_lock);
3186 GST_ERROR ("client %p: tunnel session %s already existed", client,
3192 static GstRTSPResult
3193 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
3195 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3196 GstRTSPClientPrivate *priv = client->priv;
3198 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
3201 /* ignore error, it'll only be a problem when the client does a POST again */
3202 remember_tunnel (client);
3208 handle_tunnel (GstRTSPClient * client)
3210 GstRTSPClientPrivate *priv = client->priv;
3211 GstRTSPClient *oclient;
3212 GstRTSPClientPrivate *opriv;
3213 const gchar *tunnelid;
3215 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
3216 if (tunnelid == NULL)
3219 /* check for previous tunnel */
3220 g_mutex_lock (&tunnels_lock);
3221 oclient = g_hash_table_lookup (tunnels, tunnelid);
3223 if (oclient == NULL) {
3224 /* no previous tunnel, remember tunnel */
3225 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
3226 g_mutex_unlock (&tunnels_lock);
3228 GST_INFO ("client %p: no previous tunnel found, remembering tunnel (%p)",
3229 client, priv->connection);
3231 /* merge both tunnels into the first client */
3232 /* remove the old client from the table. ref before because removing it will
3233 * remove the ref to it. */
3234 g_object_ref (oclient);
3235 g_hash_table_remove (tunnels, tunnelid);
3236 g_mutex_unlock (&tunnels_lock);
3238 opriv = oclient->priv;
3240 g_mutex_lock (&opriv->watch_lock);
3241 if (opriv->watch == NULL)
3244 GST_INFO ("client %p: found previous tunnel %p (old %p, new %p)", client,
3245 oclient, opriv->connection, priv->connection);
3247 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
3248 gst_rtsp_watch_reset (priv->watch);
3249 gst_rtsp_watch_reset (opriv->watch);
3250 g_mutex_unlock (&opriv->watch_lock);
3251 g_object_unref (oclient);
3253 /* the old client owns the tunnel now, the new one will be freed */
3254 g_source_destroy ((GSource *) priv->watch);
3256 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
3264 GST_ERROR ("client %p: no tunnelid provided", client);
3269 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
3270 g_mutex_unlock (&opriv->watch_lock);
3271 g_object_unref (oclient);
3276 static GstRTSPStatusCode
3277 tunnel_get (GstRTSPWatch * watch, gpointer user_data)
3279 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3281 GST_INFO ("client %p: tunnel get (connection %p)", client,
3282 client->priv->connection);
3284 if (!handle_tunnel (client)) {
3285 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
3288 return GST_RTSP_STS_OK;
3291 static GstRTSPResult
3292 tunnel_post (GstRTSPWatch * watch, gpointer user_data)
3294 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3296 GST_INFO ("client %p: tunnel post (connection %p)", client,
3297 client->priv->connection);
3299 if (!handle_tunnel (client)) {
3300 return GST_RTSP_ERROR;
3306 static GstRTSPResult
3307 tunnel_http_response (GstRTSPWatch * watch, GstRTSPMessage * request,
3308 GstRTSPMessage * response, gpointer user_data)
3310 GstRTSPClientClass *klass;
3312 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3313 klass = GST_RTSP_CLIENT_GET_CLASS (client);
3315 if (klass->tunnel_http_response) {
3316 klass->tunnel_http_response (client, request, response);
3322 static GstRTSPWatchFuncs watch_funcs = {
3331 tunnel_http_response
3335 client_watch_notify (GstRTSPClient * client)
3337 GstRTSPClientPrivate *priv = client->priv;
3339 GST_INFO ("client %p: watch destroyed", client);
3341 /* remove all sessions and so drop the extra client ref */
3342 gst_rtsp_client_session_filter (client, cleanup_session, NULL);
3343 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
3344 g_object_unref (client);
3348 * gst_rtsp_client_attach:
3349 * @client: a #GstRTSPClient
3350 * @context: (allow-none): a #GMainContext
3352 * Attaches @client to @context. When the mainloop for @context is run, the
3353 * client will be dispatched. When @context is %NULL, the default context will be
3356 * This function should be called when the client properties and urls are fully
3357 * configured and the client is ready to start.
3359 * Returns: the ID (greater than 0) for the source within the GMainContext.
3362 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
3364 GstRTSPClientPrivate *priv;
3367 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
3368 priv = client->priv;
3369 g_return_val_if_fail (priv->connection != NULL, 0);
3370 g_return_val_if_fail (priv->watch == NULL, 0);
3372 /* make sure noone will free the context before the watch is destroyed */
3373 priv->watch_context = g_main_context_ref (context);
3375 /* create watch for the connection and attach */
3376 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
3377 g_object_ref (client), (GDestroyNotify) client_watch_notify);
3378 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
3379 (GDestroyNotify) gst_rtsp_watch_unref);
3381 gst_rtsp_watch_set_send_backlog (priv->watch, 0, WATCH_BACKLOG_SIZE);
3383 GST_INFO ("client %p: attaching to context %p", client, context);
3384 res = gst_rtsp_watch_attach (priv->watch, context);
3390 * gst_rtsp_client_session_filter:
3391 * @client: a #GstRTSPClient
3392 * @func: (scope call) (allow-none): a callback
3393 * @user_data: user data passed to @func
3395 * Call @func for each session managed by @client. The result value of @func
3396 * determines what happens to the session. @func will be called with @client
3397 * locked so no further actions on @client can be performed from @func.
3399 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
3402 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
3404 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
3405 * will also be added with an additional ref to the result #GList of this
3408 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each session.
3410 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
3411 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3412 * element in the #GList should be unreffed before the list is freed.
3415 gst_rtsp_client_session_filter (GstRTSPClient * client,
3416 GstRTSPClientSessionFilterFunc func, gpointer user_data)
3418 GstRTSPClientPrivate *priv;
3419 GList *result, *walk, *next;
3420 GHashTable *visited;
3423 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
3425 priv = client->priv;
3429 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
3431 g_mutex_lock (&priv->lock);
3433 cookie = priv->sessions_cookie;
3434 for (walk = priv->sessions; walk; walk = next) {
3435 GstRTSPSession *sess = walk->data;
3436 GstRTSPFilterResult res;
3439 next = g_list_next (walk);
3442 /* only visit each session once */
3443 if (g_hash_table_contains (visited, sess))
3446 g_hash_table_add (visited, g_object_ref (sess));
3447 g_mutex_unlock (&priv->lock);
3449 res = func (client, sess, user_data);
3451 g_mutex_lock (&priv->lock);
3453 res = GST_RTSP_FILTER_REF;
3455 changed = (cookie != priv->sessions_cookie);
3458 case GST_RTSP_FILTER_REMOVE:
3459 /* stop watching the session and pretend it went away, if the list was
3460 * changed, we can't use the current list position, try to see if we
3461 * still have the session */
3462 client_unwatch_session (client, sess, changed ? NULL : walk);
3463 cookie = priv->sessions_cookie;
3465 case GST_RTSP_FILTER_REF:
3466 result = g_list_prepend (result, g_object_ref (sess));
3468 case GST_RTSP_FILTER_KEEP:
3475 g_mutex_unlock (&priv->lock);
3478 g_hash_table_unref (visited);