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 */
87 guint sessions_cookie;
89 gboolean drop_backlog;
92 static GMutex tunnels_lock;
93 static GHashTable *tunnels; /* protected by tunnels_lock */
95 #define DEFAULT_SESSION_POOL NULL
96 #define DEFAULT_MOUNT_POINTS NULL
97 #define DEFAULT_DROP_BACKLOG TRUE
112 SIGNAL_OPTIONS_REQUEST,
113 SIGNAL_DESCRIBE_REQUEST,
114 SIGNAL_SETUP_REQUEST,
116 SIGNAL_PAUSE_REQUEST,
117 SIGNAL_TEARDOWN_REQUEST,
118 SIGNAL_SET_PARAMETER_REQUEST,
119 SIGNAL_GET_PARAMETER_REQUEST,
120 SIGNAL_HANDLE_RESPONSE,
125 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
126 #define GST_CAT_DEFAULT rtsp_client_debug
128 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
130 static void gst_rtsp_client_get_property (GObject * object, guint propid,
131 GValue * value, GParamSpec * pspec);
132 static void gst_rtsp_client_set_property (GObject * object, guint propid,
133 const GValue * value, GParamSpec * pspec);
134 static void gst_rtsp_client_finalize (GObject * obj);
136 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
137 static void unlink_session_transports (GstRTSPClient * client,
138 GstRTSPSession * session, GstRTSPSessionMedia * sessmedia);
139 static gboolean default_configure_client_media (GstRTSPClient * client,
140 GstRTSPMedia * media, GstRTSPStream * stream, GstRTSPContext * ctx);
141 static gboolean default_configure_client_transport (GstRTSPClient * client,
142 GstRTSPContext * ctx, GstRTSPTransport * ct);
143 static GstRTSPResult default_params_set (GstRTSPClient * client,
144 GstRTSPContext * ctx);
145 static GstRTSPResult default_params_get (GstRTSPClient * client,
146 GstRTSPContext * ctx);
147 static gchar *default_make_path_from_uri (GstRTSPClient * client,
148 const GstRTSPUrl * uri);
149 static gboolean default_handle_options_request (GstRTSPClient * client,
150 GstRTSPContext * ctx);
151 static gboolean default_handle_set_param_request (GstRTSPClient * client,
152 GstRTSPContext * ctx);
153 static gboolean default_handle_get_param_request (GstRTSPClient * client,
154 GstRTSPContext * ctx);
155 static void client_session_removed (GstRTSPSessionPool * pool,
156 GstRTSPSession * session, GstRTSPClient * client);
158 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
161 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
163 GObjectClass *gobject_class;
165 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
167 gobject_class = G_OBJECT_CLASS (klass);
169 gobject_class->get_property = gst_rtsp_client_get_property;
170 gobject_class->set_property = gst_rtsp_client_set_property;
171 gobject_class->finalize = gst_rtsp_client_finalize;
173 klass->create_sdp = create_sdp;
174 klass->configure_client_media = default_configure_client_media;
175 klass->configure_client_transport = default_configure_client_transport;
176 klass->params_set = default_params_set;
177 klass->params_get = default_params_get;
178 klass->make_path_from_uri = default_make_path_from_uri;
179 klass->handle_options_request = default_handle_options_request;
180 klass->handle_set_param_request = default_handle_set_param_request;
181 klass->handle_get_param_request = default_handle_get_param_request;
183 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
184 g_param_spec_object ("session-pool", "Session Pool",
185 "The session pool to use for client session",
186 GST_TYPE_RTSP_SESSION_POOL,
187 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
189 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
190 g_param_spec_object ("mount-points", "Mount Points",
191 "The mount points to use for client session",
192 GST_TYPE_RTSP_MOUNT_POINTS,
193 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
195 g_object_class_install_property (gobject_class, PROP_DROP_BACKLOG,
196 g_param_spec_boolean ("drop-backlog", "Drop Backlog",
197 "Drop data when the backlog queue is full",
198 DEFAULT_DROP_BACKLOG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
200 gst_rtsp_client_signals[SIGNAL_CLOSED] =
201 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
202 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
203 g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
205 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
206 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
207 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
208 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
210 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
211 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
212 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
213 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
214 GST_TYPE_RTSP_CONTEXT);
216 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
217 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
218 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
219 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
220 GST_TYPE_RTSP_CONTEXT);
222 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
223 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
224 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
225 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
226 GST_TYPE_RTSP_CONTEXT);
228 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
229 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
230 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
231 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
232 GST_TYPE_RTSP_CONTEXT);
234 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
235 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
236 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
237 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
238 GST_TYPE_RTSP_CONTEXT);
240 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
241 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
242 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
243 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
244 GST_TYPE_RTSP_CONTEXT);
246 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
247 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
248 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
249 set_parameter_request), NULL, NULL, g_cclosure_marshal_generic,
250 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
252 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
253 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
254 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
255 get_parameter_request), NULL, NULL, g_cclosure_marshal_generic,
256 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
258 gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE] =
259 g_signal_new ("handle-response", G_TYPE_FROM_CLASS (klass),
260 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
261 handle_response), NULL, NULL, g_cclosure_marshal_generic,
262 G_TYPE_NONE, 1, GST_TYPE_RTSP_CONTEXT);
265 * GstRTSPClient::send-message:
266 * @client: The RTSP client
267 * @session: (type GstRtspServer.RTSPSession): The session
268 * @message: (type GstRtsp.RTSPMessage): The message
270 gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE] =
271 g_signal_new ("send-message", G_TYPE_FROM_CLASS (klass),
272 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
273 G_TYPE_NONE, 2, GST_TYPE_RTSP_CONTEXT, G_TYPE_POINTER);
276 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
277 g_mutex_init (&tunnels_lock);
279 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
283 gst_rtsp_client_init (GstRTSPClient * client)
285 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
289 g_mutex_init (&priv->lock);
290 g_mutex_init (&priv->send_lock);
291 g_mutex_init (&priv->watch_lock);
293 priv->drop_backlog = DEFAULT_DROP_BACKLOG;
296 static GstRTSPFilterResult
297 filter_session_media (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
300 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
302 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
303 unlink_session_transports (client, sess, sessmedia);
305 /* unmanage the media in the session */
306 return GST_RTSP_FILTER_REMOVE;
310 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
312 GstRTSPClientPrivate *priv = client->priv;
314 g_mutex_lock (&priv->lock);
315 /* check if we already know about this session */
316 if (g_list_find (priv->sessions, session) == NULL) {
317 GST_INFO ("watching session %p", session);
319 priv->sessions = g_list_prepend (priv->sessions, g_object_ref (session));
320 priv->sessions_cookie++;
322 /* connect removed session handler, it will be disconnected when the last
323 * session gets removed */
324 if (priv->session_removed_id == 0)
325 priv->session_removed_id = g_signal_connect_data (priv->session_pool,
326 "session-removed", G_CALLBACK (client_session_removed),
327 g_object_ref (client), (GClosureNotify) g_object_unref, 0);
329 g_mutex_unlock (&priv->lock);
334 /* should be called with lock */
336 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session,
339 GstRTSPClientPrivate *priv = client->priv;
341 GST_INFO ("client %p: unwatch session %p", client, session);
344 link = g_list_find (priv->sessions, session);
349 priv->sessions = g_list_delete_link (priv->sessions, link);
350 priv->sessions_cookie++;
352 /* if this was the last session, disconnect the handler.
353 * This will also drop the extra client ref */
354 if (!priv->sessions) {
355 g_signal_handler_disconnect (priv->session_pool, priv->session_removed_id);
356 priv->session_removed_id = 0;
359 /* unlink all media managed in this session */
360 gst_rtsp_session_filter (session, filter_session_media, client);
362 /* remove the session */
363 g_object_unref (session);
366 static GstRTSPFilterResult
367 cleanup_session (GstRTSPClient * client, GstRTSPSession * sess,
370 return GST_RTSP_FILTER_REMOVE;
373 /* A client is finalized when the connection is broken */
375 gst_rtsp_client_finalize (GObject * obj)
377 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
378 GstRTSPClientPrivate *priv = client->priv;
380 GST_INFO ("finalize client %p", client);
383 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
384 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
387 g_source_destroy ((GSource *) priv->watch);
389 if (priv->watch_context)
390 g_main_context_unref (priv->watch_context);
392 /* all sessions should have been removed by now. We keep a ref to
393 * the client object for the session removed handler. The ref is
394 * dropped when the last session is removed from the list. */
395 g_assert (priv->sessions == NULL);
396 g_assert (priv->session_removed_id == 0);
398 if (priv->connection)
399 gst_rtsp_connection_free (priv->connection);
400 if (priv->session_pool) {
401 g_object_unref (priv->session_pool);
403 if (priv->mount_points)
404 g_object_unref (priv->mount_points);
406 g_object_unref (priv->auth);
407 if (priv->thread_pool)
408 g_object_unref (priv->thread_pool);
413 gst_rtsp_media_unprepare (priv->media);
414 g_object_unref (priv->media);
417 g_free (priv->server_ip);
418 g_mutex_clear (&priv->lock);
419 g_mutex_clear (&priv->send_lock);
420 g_mutex_clear (&priv->watch_lock);
422 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
426 gst_rtsp_client_get_property (GObject * object, guint propid,
427 GValue * value, GParamSpec * pspec)
429 GstRTSPClient *client = GST_RTSP_CLIENT (object);
430 GstRTSPClientPrivate *priv = client->priv;
433 case PROP_SESSION_POOL:
434 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
436 case PROP_MOUNT_POINTS:
437 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
439 case PROP_DROP_BACKLOG:
440 g_value_set_boolean (value, priv->drop_backlog);
443 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
448 gst_rtsp_client_set_property (GObject * object, guint propid,
449 const GValue * value, GParamSpec * pspec)
451 GstRTSPClient *client = GST_RTSP_CLIENT (object);
452 GstRTSPClientPrivate *priv = client->priv;
455 case PROP_SESSION_POOL:
456 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
458 case PROP_MOUNT_POINTS:
459 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
461 case PROP_DROP_BACKLOG:
462 g_mutex_lock (&priv->lock);
463 priv->drop_backlog = g_value_get_boolean (value);
464 g_mutex_unlock (&priv->lock);
467 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
472 * gst_rtsp_client_new:
474 * Create a new #GstRTSPClient instance.
476 * Returns: (transfer full): a new #GstRTSPClient
479 gst_rtsp_client_new (void)
481 GstRTSPClient *result;
483 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
489 send_message (GstRTSPClient * client, GstRTSPContext * ctx,
490 GstRTSPMessage * message, gboolean close)
492 GstRTSPClientPrivate *priv = client->priv;
494 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
495 "GStreamer RTSP server");
497 /* remove any previous header */
498 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
500 /* add the new session header for new session ids */
502 gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
503 gst_rtsp_session_get_header (ctx->session));
506 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
507 gst_rtsp_message_dump (message);
511 gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
513 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SEND_MESSAGE],
516 g_mutex_lock (&priv->send_lock);
518 priv->send_func (client, message, close, priv->send_data);
519 g_mutex_unlock (&priv->send_lock);
521 gst_rtsp_message_unset (message);
525 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
526 GstRTSPContext * ctx)
528 gst_rtsp_message_init_response (ctx->response, code,
529 gst_rtsp_status_as_text (code), ctx->request);
533 send_message (client, ctx, ctx->response, FALSE);
537 send_option_not_supported_response (GstRTSPClient * client,
538 GstRTSPContext * ctx, const gchar * unsupported_options)
540 GstRTSPStatusCode code = GST_RTSP_STS_OPTION_NOT_SUPPORTED;
542 gst_rtsp_message_init_response (ctx->response, code,
543 gst_rtsp_status_as_text (code), ctx->request);
545 if (unsupported_options != NULL) {
546 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_UNSUPPORTED,
547 unsupported_options);
552 send_message (client, ctx, ctx->response, FALSE);
556 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
558 if (path1 == NULL || path2 == NULL)
561 if (strlen (path1) != len2)
564 if (strncmp (path1, path2, len2))
570 /* this function is called to initially find the media for the DESCRIBE request
571 * but is cached for when the same client (without breaking the connection) is
572 * doing a setup for the exact same url. */
573 static GstRTSPMedia *
574 find_media (GstRTSPClient * client, GstRTSPContext * ctx, gchar * path,
577 GstRTSPClientPrivate *priv = client->priv;
578 GstRTSPMediaFactory *factory;
582 /* find the longest matching factory for the uri first */
583 if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
587 ctx->factory = factory;
589 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
590 goto no_factory_access;
592 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
598 path_len = strlen (path);
600 if (!paths_are_equal (priv->path, path, path_len)) {
601 GstRTSPThread *thread;
603 /* remove any previously cached values before we try to construct a new
609 gst_rtsp_media_unprepare (priv->media);
610 g_object_unref (priv->media);
614 /* prepare the media and add it to the pipeline */
615 if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
620 thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
621 GST_RTSP_THREAD_TYPE_MEDIA, ctx);
625 /* prepare the media */
626 if (!(gst_rtsp_media_prepare (media, thread)))
629 /* now keep track of the uri and the media */
630 priv->path = g_strndup (path, path_len);
633 /* we have seen this path before, used cached media */
636 GST_INFO ("reusing cached media %p for path %s", media, priv->path);
639 g_object_unref (factory);
643 g_object_ref (media);
650 GST_ERROR ("client %p: no factory for path %s", client, path);
651 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
656 GST_ERROR ("client %p: not authorized to see factory path %s", client,
658 /* error reply is already sent */
663 GST_ERROR ("client %p: not authorized for factory path %s", client, path);
664 /* error reply is already sent */
669 GST_ERROR ("client %p: can't create media", client);
670 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
671 g_object_unref (factory);
677 GST_ERROR ("client %p: can't create thread", client);
678 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
679 g_object_unref (media);
681 g_object_unref (factory);
687 GST_ERROR ("client %p: can't prepare media", client);
688 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
689 g_object_unref (media);
691 g_object_unref (factory);
698 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
700 GstRTSPClientPrivate *priv = client->priv;
701 GstRTSPMessage message = { 0 };
706 gst_rtsp_message_init_data (&message, channel);
708 /* FIXME, need some sort of iovec RTSPMessage here */
709 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
712 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
714 g_mutex_lock (&priv->send_lock);
716 priv->send_func (client, &message, FALSE, priv->send_data);
717 g_mutex_unlock (&priv->send_lock);
719 gst_rtsp_message_steal_body (&message, &data, &usize);
720 gst_buffer_unmap (buffer, &map_info);
722 gst_rtsp_message_unset (&message);
728 link_transport (GstRTSPClient * client, GstRTSPSession * session,
729 GstRTSPStreamTransport * trans)
731 GstRTSPClientPrivate *priv = client->priv;
733 GST_DEBUG ("client %p: linking transport %p", client, trans);
735 gst_rtsp_stream_transport_set_callbacks (trans,
736 (GstRTSPSendFunc) do_send_data,
737 (GstRTSPSendFunc) do_send_data, client, NULL);
739 priv->transports = g_list_prepend (priv->transports, trans);
741 /* make sure our session can't expire */
742 gst_rtsp_session_prevent_expire (session);
746 link_session_transports (GstRTSPClient * client, GstRTSPSession * session,
747 GstRTSPSessionMedia * sessmedia)
752 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
753 for (i = 0; i < n_streams; i++) {
754 GstRTSPStreamTransport *trans;
755 const GstRTSPTransport *tr;
757 /* get the transport, if there is no transport configured, skip this stream */
758 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
762 tr = gst_rtsp_stream_transport_get_transport (trans);
764 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
765 /* for TCP, link the stream to the TCP connection of the client */
766 link_transport (client, session, trans);
772 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
773 GstRTSPStreamTransport * trans)
775 GstRTSPClientPrivate *priv = client->priv;
777 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
779 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
781 priv->transports = g_list_remove (priv->transports, trans);
783 /* our session can now expire */
784 gst_rtsp_session_allow_expire (session);
788 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
789 GstRTSPSessionMedia * sessmedia)
794 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
795 for (i = 0; i < n_streams; i++) {
796 GstRTSPStreamTransport *trans;
797 const GstRTSPTransport *tr;
799 /* get the transport, if there is no transport configured, skip this stream */
800 trans = gst_rtsp_session_media_get_transport (sessmedia, i);
804 tr = gst_rtsp_stream_transport_get_transport (trans);
806 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
807 /* for TCP, unlink the stream from the TCP connection of the client */
808 unlink_transport (client, session, trans);
814 * gst_rtsp_client_close:
815 * @client: a #GstRTSPClient
817 * Close the connection of @client and remove all media it was managing.
822 gst_rtsp_client_close (GstRTSPClient * client)
824 GstRTSPClientPrivate *priv = client->priv;
825 const gchar *tunnelid;
827 GST_DEBUG ("client %p: closing connection", client);
829 if (priv->connection) {
830 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
831 g_mutex_lock (&tunnels_lock);
832 /* remove from tunnelids */
833 g_hash_table_remove (tunnels, tunnelid);
834 g_mutex_unlock (&tunnels_lock);
836 gst_rtsp_connection_close (priv->connection);
839 /* connection is now closed, destroy the watch which will also cause the
840 * closed signal to be emitted */
842 GST_DEBUG ("client %p: destroying watch", client);
843 g_source_destroy ((GSource *) priv->watch);
845 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
850 default_make_path_from_uri (GstRTSPClient * client, const GstRTSPUrl * uri)
855 path = g_strconcat (uri->abspath, "?", uri->query, NULL);
857 path = g_strdup (uri->abspath);
863 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
865 GstRTSPClientPrivate *priv = client->priv;
866 GstRTSPClientClass *klass;
867 GstRTSPSession *session;
868 GstRTSPSessionMedia *sessmedia;
869 GstRTSPStatusCode code;
872 gboolean keep_session;
877 session = ctx->session;
882 klass = GST_RTSP_CLIENT_GET_CLASS (client);
883 path = klass->make_path_from_uri (client, ctx->uri);
885 /* get a handle to the configuration of the media in the session */
886 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
890 /* only aggregate control for now.. */
891 if (path[matched] != '\0')
896 ctx->sessmedia = sessmedia;
898 /* we emit the signal before closing the connection */
899 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
902 /* make sure we unblock the backlog and don't accept new messages
904 if (priv->watch != NULL)
905 gst_rtsp_watch_set_flushing (priv->watch, TRUE);
907 /* unlink the all TCP callbacks */
908 unlink_session_transports (client, session, sessmedia);
910 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
912 /* allow messages again so that we can send the reply */
913 if (priv->watch != NULL)
914 gst_rtsp_watch_set_flushing (priv->watch, FALSE);
916 /* unmanage the media in the session, returns false if all media session
918 keep_session = gst_rtsp_session_release_media (session, sessmedia);
920 /* construct the response now */
921 code = GST_RTSP_STS_OK;
922 gst_rtsp_message_init_response (ctx->response, code,
923 gst_rtsp_status_as_text (code), ctx->request);
925 send_message (client, ctx, ctx->response, TRUE);
928 /* remove the session */
929 gst_rtsp_session_pool_remove (priv->session_pool, session);
937 GST_ERROR ("client %p: no session", client);
938 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
943 GST_ERROR ("client %p: no uri supplied", client);
944 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
949 GST_ERROR ("client %p: no media for uri", client);
950 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
956 GST_ERROR ("client %p: no aggregate path %s", client, path);
957 send_generic_response (client,
958 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
965 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
969 res = gst_rtsp_params_set (client, ctx);
975 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
979 res = gst_rtsp_params_get (client, ctx);
985 default_handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
991 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
992 if (res != GST_RTSP_OK)
996 /* no body, keep-alive request */
997 send_generic_response (client, GST_RTSP_STS_OK, ctx);
999 /* there is a body, handle the params */
1000 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
1001 if (res != GST_RTSP_OK)
1004 send_message (client, ctx, ctx->response, FALSE);
1007 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
1015 GST_ERROR ("client %p: bad request", client);
1016 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1022 default_handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
1028 res = gst_rtsp_message_get_body (ctx->request, &data, &size);
1029 if (res != GST_RTSP_OK)
1033 /* no body, keep-alive request */
1034 send_generic_response (client, GST_RTSP_STS_OK, ctx);
1036 /* there is a body, handle the params */
1037 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
1038 if (res != GST_RTSP_OK)
1041 send_message (client, ctx, ctx->response, FALSE);
1044 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
1052 GST_ERROR ("client %p: bad request", client);
1053 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1059 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
1061 GstRTSPSession *session;
1062 GstRTSPClientClass *klass;
1063 GstRTSPSessionMedia *sessmedia;
1064 GstRTSPStatusCode code;
1065 GstRTSPState rtspstate;
1069 if (!(session = ctx->session))
1075 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1076 path = klass->make_path_from_uri (client, ctx->uri);
1078 /* get a handle to the configuration of the media in the session */
1079 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1083 if (path[matched] != '\0')
1088 ctx->sessmedia = sessmedia;
1090 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1091 /* the session state must be playing or recording */
1092 if (rtspstate != GST_RTSP_STATE_PLAYING &&
1093 rtspstate != GST_RTSP_STATE_RECORDING)
1096 /* unlink the all TCP callbacks */
1097 unlink_session_transports (client, session, sessmedia);
1099 /* then pause sending */
1100 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
1102 /* construct the response now */
1103 code = GST_RTSP_STS_OK;
1104 gst_rtsp_message_init_response (ctx->response, code,
1105 gst_rtsp_status_as_text (code), ctx->request);
1107 send_message (client, ctx, ctx->response, FALSE);
1109 /* the state is now READY */
1110 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1112 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
1119 GST_ERROR ("client %p: no seesion", client);
1120 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1125 GST_ERROR ("client %p: no uri supplied", client);
1126 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1131 GST_ERROR ("client %p: no media for uri", client);
1132 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1138 GST_ERROR ("client %p: no aggregate path %s", client, path);
1139 send_generic_response (client,
1140 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1146 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
1147 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1153 /* convert @url and @path to a URL used as a content base for the factory
1154 * located at @path */
1156 make_base_url (GstRTSPClient * client, GstRTSPUrl * url, const gchar * path)
1162 /* check for trailing '/' and append one */
1163 trail = (path[strlen (path) - 1] != '/' ? "/" : "");
1168 tmp.abspath = g_strdup_printf ("%s%s", path, trail);
1170 result = gst_rtsp_url_get_request_uri (&tmp);
1171 g_free (tmp.abspath);
1177 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
1179 GstRTSPSession *session;
1180 GstRTSPClientClass *klass;
1181 GstRTSPSessionMedia *sessmedia;
1182 GstRTSPMedia *media;
1183 GstRTSPStatusCode code;
1186 GstRTSPTimeRange *range;
1188 GstRTSPState rtspstate;
1189 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
1190 gchar *path, *rtpinfo;
1193 if (!(session = ctx->session))
1196 if (!(uri = ctx->uri))
1199 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1200 path = klass->make_path_from_uri (client, uri);
1202 /* get a handle to the configuration of the media in the session */
1203 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1207 if (path[matched] != '\0')
1212 ctx->sessmedia = sessmedia;
1213 ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1215 /* the session state must be playing or ready */
1216 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1217 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1220 /* in play we first unsuspend, media could be suspended from SDP or PAUSED */
1221 if (!gst_rtsp_media_unsuspend (media))
1222 goto unsuspend_failed;
1224 /* parse the range header if we have one */
1225 res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1226 if (res == GST_RTSP_OK) {
1227 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1228 /* we have a range, seek to the position */
1230 gst_rtsp_media_seek (media, range);
1231 gst_rtsp_range_free (range);
1235 /* link the all TCP callbacks */
1236 link_session_transports (client, session, sessmedia);
1238 /* grab RTPInfo from the media now */
1239 rtpinfo = gst_rtsp_session_media_get_rtpinfo (sessmedia);
1241 /* construct the response now */
1242 code = GST_RTSP_STS_OK;
1243 gst_rtsp_message_init_response (ctx->response, code,
1244 gst_rtsp_status_as_text (code), ctx->request);
1246 /* add the RTP-Info header */
1248 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO,
1252 str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1254 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1256 send_message (client, ctx, ctx->response, FALSE);
1258 /* start playing after sending the response */
1259 gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1261 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1263 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1270 GST_ERROR ("client %p: no session", client);
1271 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1276 GST_ERROR ("client %p: no uri supplied", client);
1277 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1282 GST_ERROR ("client %p: media not found", client);
1283 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1288 GST_ERROR ("client %p: no aggregate path %s", client, path);
1289 send_generic_response (client,
1290 GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1296 GST_ERROR ("client %p: not PLAYING or READY", client);
1297 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1303 GST_ERROR ("client %p: unsuspend failed", client);
1304 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1310 do_keepalive (GstRTSPSession * session)
1312 GST_INFO ("keep session %p alive", session);
1313 gst_rtsp_session_touch (session);
1316 /* parse @transport and return a valid transport in @tr. only transports
1317 * supported by @stream are returned. Returns FALSE if no valid transport
1320 parse_transport (const char *transport, GstRTSPStream * stream,
1321 GstRTSPTransport * tr)
1328 gst_rtsp_transport_init (tr);
1330 GST_DEBUG ("parsing transports %s", transport);
1332 transports = g_strsplit (transport, ",", 0);
1334 /* loop through the transports, try to parse */
1335 for (i = 0; transports[i]; i++) {
1336 res = gst_rtsp_transport_parse (transports[i], tr);
1337 if (res != GST_RTSP_OK) {
1338 /* no valid transport, search some more */
1339 GST_WARNING ("could not parse transport %s", transports[i]);
1343 /* we have a transport, see if it's supported */
1344 if (!gst_rtsp_stream_is_transport_supported (stream, tr)) {
1345 GST_WARNING ("unsupported transport %s", transports[i]);
1349 /* we have a valid transport */
1350 GST_INFO ("found valid transport %s", transports[i]);
1355 gst_rtsp_transport_init (tr);
1357 g_strfreev (transports);
1363 default_configure_client_media (GstRTSPClient * client, GstRTSPMedia * media,
1364 GstRTSPStream * stream, GstRTSPContext * ctx)
1366 GstRTSPMessage *request = ctx->request;
1367 gchar *blocksize_str;
1369 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1370 &blocksize_str, 0) == GST_RTSP_OK) {
1374 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1375 if (end == blocksize_str)
1378 /* we don't want to change the mtu when this media
1379 * can be shared because it impacts other clients */
1380 if (gst_rtsp_media_is_shared (media))
1383 if (blocksize > G_MAXUINT)
1384 blocksize = G_MAXUINT;
1386 gst_rtsp_stream_set_mtu (stream, blocksize);
1394 GST_ERROR_OBJECT (client, "failed to parse blocksize");
1395 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1401 default_configure_client_transport (GstRTSPClient * client,
1402 GstRTSPContext * ctx, GstRTSPTransport * ct)
1404 GstRTSPClientPrivate *priv = client->priv;
1406 /* we have a valid transport now, set the destination of the client. */
1407 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1408 gboolean use_client_settings;
1410 use_client_settings =
1411 gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1413 if (ct->destination && use_client_settings) {
1414 GstRTSPAddress *addr;
1416 addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1417 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1422 gst_rtsp_address_free (addr);
1424 GstRTSPAddress *addr;
1425 GSocketFamily family;
1427 family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1429 addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1433 g_free (ct->destination);
1434 ct->destination = g_strdup (addr->address);
1435 ct->port.min = addr->port;
1436 ct->port.max = addr->port + addr->n_ports - 1;
1437 ct->ttl = addr->ttl;
1439 gst_rtsp_address_free (addr);
1444 url = gst_rtsp_connection_get_url (priv->connection);
1445 g_free (ct->destination);
1446 ct->destination = g_strdup (url->host);
1448 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1450 GSocketAddress *addr;
1452 sock = gst_rtsp_connection_get_read_socket (priv->connection);
1453 if ((addr = g_socket_get_remote_address (sock, NULL))) {
1454 /* our read port is the sender port of client */
1455 ct->client_port.min =
1456 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1457 g_object_unref (addr);
1459 if ((addr = g_socket_get_local_address (sock, NULL))) {
1460 ct->server_port.max =
1461 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1462 g_object_unref (addr);
1464 sock = gst_rtsp_connection_get_write_socket (priv->connection);
1465 if ((addr = g_socket_get_remote_address (sock, NULL))) {
1466 /* our write port is the receiver port of client */
1467 ct->client_port.max =
1468 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1469 g_object_unref (addr);
1471 if ((addr = g_socket_get_local_address (sock, NULL))) {
1472 ct->server_port.min =
1473 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
1474 g_object_unref (addr);
1476 /* check if the client selected channels for TCP */
1477 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1478 gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1488 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1493 static GstRTSPTransport *
1494 make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
1495 GstRTSPTransport * ct)
1497 GstRTSPTransport *st;
1499 GSocketFamily family;
1501 /* prepare the server transport */
1502 gst_rtsp_transport_new (&st);
1504 st->trans = ct->trans;
1505 st->profile = ct->profile;
1506 st->lower_transport = ct->lower_transport;
1508 addr = g_inet_address_new_from_string (ct->destination);
1511 GST_ERROR ("failed to get inet addr from client destination");
1512 family = G_SOCKET_FAMILY_IPV4;
1514 family = g_inet_address_get_family (addr);
1515 g_object_unref (addr);
1519 switch (st->lower_transport) {
1520 case GST_RTSP_LOWER_TRANS_UDP:
1521 st->client_port = ct->client_port;
1522 gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
1524 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1525 st->port = ct->port;
1526 st->destination = g_strdup (ct->destination);
1529 case GST_RTSP_LOWER_TRANS_TCP:
1530 st->interleaved = ct->interleaved;
1531 st->client_port = ct->client_port;
1532 st->server_port = ct->server_port;
1537 gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
1542 #define AES_128_KEY_LEN 16
1543 #define AES_256_KEY_LEN 32
1545 #define HMAC_32_KEY_LEN 4
1546 #define HMAC_80_KEY_LEN 10
1549 mikey_apply_policy (GstCaps * caps, GstMIKEYMessage * msg, guint8 policy)
1551 const gchar *srtp_cipher;
1552 const gchar *srtp_auth;
1553 const GstMIKEYPayload *sp;
1556 /* loop over Security policy until we find one containing policy */
1558 if ((sp = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, i)) == NULL)
1561 if (((GstMIKEYPayloadSP *) sp)->policy == policy)
1565 /* the default ciphers */
1566 srtp_cipher = "aes-128-icm";
1567 srtp_auth = "hmac-sha1-80";
1569 /* now override the defaults with what is in the Security Policy */
1573 /* collect all the params and go over them */
1574 len = gst_mikey_payload_sp_get_n_params (sp);
1575 for (i = 0; i < len; i++) {
1576 const GstMIKEYPayloadSPParam *param =
1577 gst_mikey_payload_sp_get_param (sp, i);
1579 switch (param->type) {
1580 case GST_MIKEY_SP_SRTP_ENC_ALG:
1581 switch (param->val[0]) {
1583 srtp_cipher = "null";
1587 srtp_cipher = "aes-128-icm";
1593 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
1594 switch (param->val[0]) {
1595 case AES_128_KEY_LEN:
1596 srtp_cipher = "aes-128-icm";
1598 case AES_256_KEY_LEN:
1599 srtp_cipher = "aes-256-icm";
1605 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1606 switch (param->val[0]) {
1612 srtp_auth = "hmac-sha1-80";
1618 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
1619 switch (param->val[0]) {
1620 case HMAC_32_KEY_LEN:
1621 srtp_auth = "hmac-sha1-32";
1623 case HMAC_80_KEY_LEN:
1624 srtp_auth = "hmac-sha1-80";
1630 case GST_MIKEY_SP_SRTP_SRTP_ENC:
1632 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
1639 /* now configure the SRTP parameters */
1640 gst_caps_set_simple (caps,
1641 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
1642 "srtp-auth", G_TYPE_STRING, srtp_auth,
1643 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
1644 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
1650 handle_mikey_data (GstRTSPClient * client, GstRTSPContext * ctx,
1651 guint8 * data, gsize size)
1653 GstMIKEYMessage *msg;
1655 GstCaps *caps = NULL;
1656 GstMIKEYPayloadKEMAC *kemac;
1657 const GstMIKEYPayloadKeyData *pkd;
1660 /* the MIKEY message contains a CSB or crypto session bundle. It is a
1661 * set of Crypto Sessions protected with the same master key.
1662 * In the context of SRTP, an RTP and its RTCP stream is part of a
1664 if ((msg = gst_mikey_message_new_from_data (data, size, NULL, NULL)) == NULL)
1667 /* we can only handle SRTP crypto sessions for now */
1668 if (msg->map_type != GST_MIKEY_MAP_TYPE_SRTP)
1669 goto invalid_map_type;
1671 /* get the number of crypto sessions. This maps SSRC to its
1672 * security parameters */
1673 n_cs = gst_mikey_message_get_n_cs (msg);
1675 goto no_crypto_sessions;
1677 /* we also need keys */
1678 if (!(kemac = (GstMIKEYPayloadKEMAC *) gst_mikey_message_find_payload
1679 (msg, GST_MIKEY_PT_KEMAC, 0)))
1682 /* we don't support encrypted keys */
1683 if (kemac->enc_alg != GST_MIKEY_ENC_NULL
1684 || kemac->mac_alg != GST_MIKEY_MAC_NULL)
1685 goto unsupported_encryption;
1687 /* get Key data sub-payload */
1688 pkd = (const GstMIKEYPayloadKeyData *)
1689 gst_mikey_payload_kemac_get_sub (&kemac->pt, 0);
1692 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
1695 /* go over all crypto sessions and create the security policy for each
1697 for (i = 0; i < n_cs; i++) {
1698 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
1700 caps = gst_caps_new_simple ("application/x-srtp",
1701 "ssrc", G_TYPE_UINT, map->ssrc,
1702 "roc", G_TYPE_UINT, map->roc, "srtp-key", GST_TYPE_BUFFER, key, NULL);
1703 mikey_apply_policy (caps, msg, map->policy);
1705 gst_rtsp_stream_update_crypto (ctx->stream, map->ssrc, caps);
1706 gst_caps_unref (caps);
1708 gst_mikey_message_unref (msg);
1709 gst_buffer_unref (key);
1716 GST_DEBUG_OBJECT (client, "failed to parse MIKEY message");
1721 GST_DEBUG_OBJECT (client, "invalid map type %d", msg->map_type);
1722 goto cleanup_message;
1726 GST_DEBUG_OBJECT (client, "no crypto sessions");
1727 goto cleanup_message;
1731 GST_DEBUG_OBJECT (client, "no keys found");
1732 goto cleanup_message;
1734 unsupported_encryption:
1736 GST_DEBUG_OBJECT (client, "unsupported key encryption");
1737 goto cleanup_message;
1741 gst_mikey_message_unref (msg);
1746 #define IS_STRIP_CHAR(c) (g_ascii_isspace ((guchar)(c)) || ((c) == '\"'))
1749 strip_chars (gchar * str)
1756 if (!IS_STRIP_CHAR (str[len]))
1760 for (s = str; *s && IS_STRIP_CHAR (*s); s++);
1761 memmove (str, s, len + 1);
1764 /* KeyMgmt = "KeyMgmt" ":" key-mgmt-spec 0*("," key-mgmt-spec)
1765 * key-mgmt-spec = "prot" "=" KMPID ";" ["uri" "=" %x22 URI %x22 ";"]
1768 handle_keymgmt (GstRTSPClient * client, GstRTSPContext * ctx, gchar * keymgmt)
1773 specs = g_strsplit (keymgmt, ",", 0);
1774 for (i = 0; specs[i]; i++) {
1777 split = g_strsplit (specs[i], ";", 0);
1778 for (j = 0; split[j]; j++) {
1779 g_strstrip (split[j]);
1780 if (g_str_has_prefix (split[j], "prot=")) {
1781 g_strstrip (split[j] + 5);
1782 if (!g_str_equal (split[j] + 5, "mikey"))
1784 GST_DEBUG ("found mikey");
1785 } else if (g_str_has_prefix (split[j], "uri=")) {
1786 strip_chars (split[j] + 4);
1787 GST_DEBUG ("found uri '%s'", split[j] + 4);
1788 } else if (g_str_has_prefix (split[j], "data=")) {
1791 strip_chars (split[j] + 5);
1792 GST_DEBUG ("found data '%s'", split[j] + 5);
1793 data = g_base64_decode_inplace (split[j] + 5, &size);
1794 handle_mikey_data (client, ctx, data, size);
1804 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
1806 GstRTSPClientPrivate *priv = client->priv;
1809 gchar *transport, *keymgmt;
1810 GstRTSPTransport *ct, *st;
1811 GstRTSPStatusCode code;
1812 GstRTSPSession *session;
1813 GstRTSPStreamTransport *trans;
1815 GstRTSPSessionMedia *sessmedia;
1816 GstRTSPMedia *media;
1817 GstRTSPStream *stream;
1818 GstRTSPState rtspstate;
1819 GstRTSPClientClass *klass;
1820 gchar *path, *control;
1822 gboolean new_session = FALSE;
1828 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1829 path = klass->make_path_from_uri (client, uri);
1831 /* parse the transport */
1833 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
1835 if (res != GST_RTSP_OK)
1838 /* we create the session after parsing stuff so that we don't make
1839 * a session for malformed requests */
1840 if (priv->session_pool == NULL)
1843 session = ctx->session;
1846 g_object_ref (session);
1847 /* get a handle to the configuration of the media in the session, this can
1848 * return NULL if this is a new url to manage in this session. */
1849 sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1851 /* we need a new media configuration in this session */
1855 /* we have no session media, find one and manage it */
1856 if (sessmedia == NULL) {
1857 /* get a handle to the configuration of the media in the session */
1858 media = find_media (client, ctx, path, &matched);
1860 if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1861 g_object_ref (media);
1863 goto media_not_found;
1865 /* no media, not found then */
1867 goto media_not_found_no_reply;
1869 /* FIXME-WFD : wfd url problem */
1871 if (path[matched] == '\0')
1872 goto control_not_found;
1874 /* path is what matched. */
1875 path[matched] = '\0';
1876 /* control is remainder */
1877 control = &path[matched + 1];
1879 control = g_strdup ("stream=0");
1882 /* find the stream now using the control part */
1883 stream = gst_rtsp_media_find_stream (media, control);
1885 goto stream_not_found;
1887 /* now we have a uri identifying a valid media and stream */
1888 ctx->stream = stream;
1891 if (session == NULL) {
1892 /* create a session if this fails we probably reached our session limit or
1894 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1895 goto service_unavailable;
1897 /* make sure this client is closed when the session is closed */
1898 client_watch_session (client, session);
1901 /* signal new session */
1902 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1905 ctx->session = session;
1908 if (!klass->configure_client_media (client, media, stream, ctx))
1909 goto configure_media_failed_no_reply;
1911 gst_rtsp_transport_new (&ct);
1913 /* parse and find a usable supported transport */
1914 if (!parse_transport (transport, stream, ct))
1915 goto unsupported_transports;
1917 /* update the client transport */
1918 if (!klass->configure_client_transport (client, ctx, ct))
1919 goto unsupported_client_transport;
1921 /* parse the keymgmt */
1922 if (gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_KEYMGMT,
1923 &keymgmt, 0) == GST_RTSP_OK) {
1924 if (!handle_keymgmt (client, ctx, keymgmt))
1928 if (sessmedia == NULL) {
1929 /* manage the media in our session now, if not done already */
1930 sessmedia = gst_rtsp_session_manage_media (session, path, media);
1931 /* if we stil have no media, error */
1932 if (sessmedia == NULL)
1933 goto sessmedia_unavailable;
1935 g_object_unref (media);
1938 ctx->sessmedia = sessmedia;
1940 /* set in the session media transport */
1941 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1943 /* configure the url used to set this transport, this we will use when
1944 * generating the response for the PLAY request */
1945 gst_rtsp_stream_transport_set_url (trans, uri);
1947 /* configure keepalive for this transport */
1948 gst_rtsp_stream_transport_set_keepalive (trans,
1949 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1951 /* create and serialize the server transport */
1952 st = make_server_transport (client, ctx, ct);
1953 trans_str = gst_rtsp_transport_as_text (st);
1955 /* FIXME-WFD : Temporarily force to set profile string */
1956 trans_str = g_strjoinv ("RTP/AVP/UDP", g_strsplit (trans_str, "RTP/AVP", -1));
1958 gst_rtsp_transport_free (st);
1960 /* construct the response now */
1961 code = GST_RTSP_STS_OK;
1962 gst_rtsp_message_init_response (ctx->response, code,
1963 gst_rtsp_status_as_text (code), ctx->request);
1965 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
1969 send_message (client, ctx, ctx->response, FALSE);
1971 /* update the state */
1972 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1973 switch (rtspstate) {
1974 case GST_RTSP_STATE_PLAYING:
1975 case GST_RTSP_STATE_RECORDING:
1976 case GST_RTSP_STATE_READY:
1977 /* no state change */
1980 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1983 g_object_unref (session);
1986 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
1993 GST_ERROR ("client %p: no uri", client);
1994 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1999 GST_ERROR ("client %p: no transport", client);
2000 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
2005 GST_ERROR ("client %p: no session pool configured", client);
2006 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2009 media_not_found_no_reply:
2011 GST_ERROR ("client %p: media '%s' not found", client, path);
2012 /* error reply is already sent */
2017 GST_ERROR ("client %p: media '%s' not found", client, path);
2018 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2024 GST_ERROR ("client %p: no control in path '%s'", client, path);
2025 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2026 g_object_unref (media);
2032 GST_ERROR ("client %p: stream '%s' not found", client, control);
2033 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2034 g_object_unref (media);
2037 service_unavailable:
2039 GST_ERROR ("client %p: can't create session", client);
2040 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2041 g_object_unref (media);
2044 sessmedia_unavailable:
2046 GST_ERROR ("client %p: can't create session media", client);
2047 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2048 g_object_unref (media);
2049 goto cleanup_session;
2051 configure_media_failed_no_reply:
2053 GST_ERROR ("client %p: configure_media failed", client);
2054 /* error reply is already sent */
2055 goto cleanup_session;
2057 unsupported_transports:
2059 GST_ERROR ("client %p: unsupported transports", client);
2060 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
2061 goto cleanup_transport;
2063 unsupported_client_transport:
2065 GST_ERROR ("client %p: unsupported client transport", client);
2066 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
2067 goto cleanup_transport;
2071 GST_ERROR ("client %p: keymgmt error", client);
2072 send_generic_response (client, GST_RTSP_STS_KEY_MANAGEMENT_FAILURE, ctx);
2073 goto cleanup_transport;
2077 gst_rtsp_transport_free (ct);
2080 gst_rtsp_session_pool_remove (priv->session_pool, session);
2081 g_object_unref (session);
2088 static GstSDPMessage *
2089 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
2091 GstRTSPClientPrivate *priv = client->priv;
2096 gst_sdp_message_new (&sdp);
2098 /* some standard things first */
2099 gst_sdp_message_set_version (sdp, "0");
2106 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
2109 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
2110 gst_sdp_message_set_information (sdp, "rtsp-server");
2111 gst_sdp_message_add_time (sdp, "0", "0", NULL);
2112 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
2113 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
2114 gst_sdp_message_add_attribute (sdp, "control", "*");
2116 info.is_ipv6 = priv->is_ipv6;
2117 info.server_ip = priv->server_ip;
2119 /* create an SDP for the media object */
2120 if (!gst_rtsp_media_setup_sdp (media, sdp, &info))
2128 GST_ERROR ("client %p: could not create SDP", client);
2129 gst_sdp_message_free (sdp);
2134 /* for the describe we must generate an SDP */
2136 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
2138 GstRTSPClientPrivate *priv = client->priv;
2143 GstRTSPMedia *media;
2144 GstRTSPClientClass *klass;
2146 klass = GST_RTSP_CLIENT_GET_CLASS (client);
2151 /* check what kind of format is accepted, we don't really do anything with it
2152 * and always return SDP for now. */
2157 gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
2159 if (res == GST_RTSP_ENOTIMPL)
2162 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
2166 if (!priv->mount_points)
2167 goto no_mount_points;
2169 if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
2172 /* find the media object for the uri */
2173 if (!(media = find_media (client, ctx, path, NULL)))
2176 /* create an SDP for the media object on this client */
2177 if (!(sdp = klass->create_sdp (client, media)))
2180 /* we suspend after the describe */
2181 gst_rtsp_media_suspend (media);
2182 g_object_unref (media);
2184 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2185 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2187 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
2190 /* content base for some clients that might screw up creating the setup uri */
2191 str = make_base_url (client, ctx->uri, path);
2194 GST_INFO ("adding content-base: %s", str);
2195 gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE, str);
2197 /* add SDP to the response body */
2198 str = gst_sdp_message_as_text (sdp);
2199 gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
2200 gst_sdp_message_free (sdp);
2202 send_message (client, ctx, ctx->response, FALSE);
2204 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
2212 GST_ERROR ("client %p: no uri", client);
2213 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2218 GST_ERROR ("client %p: no mount points configured", client);
2219 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2224 GST_ERROR ("client %p: can't find path for url", client);
2225 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
2230 GST_ERROR ("client %p: no media", client);
2232 /* error reply is already sent */
2237 GST_ERROR ("client %p: can't create SDP", client);
2238 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
2240 g_object_unref (media);
2246 default_handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
2248 GstRTSPMethod options;
2251 options = GST_RTSP_DESCRIBE |
2256 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
2258 str = gst_rtsp_options_as_text (options);
2260 gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
2261 gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
2263 gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
2266 send_message (client, ctx, ctx->response, FALSE);
2268 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
2274 /* remove duplicate and trailing '/' */
2276 sanitize_uri (GstRTSPUrl * uri)
2280 gboolean have_slash, prev_slash;
2282 s = d = uri->abspath;
2283 len = strlen (uri->abspath);
2287 for (i = 0; i < len; i++) {
2288 have_slash = s[i] == '/';
2290 if (!have_slash || !prev_slash)
2292 prev_slash = have_slash;
2294 len = d - uri->abspath;
2295 /* don't remove the first slash if that's the only thing left */
2296 if (len > 1 && *(d - 1) == '/')
2301 /* is called when the session is removed from its session pool. */
2303 client_session_removed (GstRTSPSessionPool * pool, GstRTSPSession * session,
2304 GstRTSPClient * client)
2306 GstRTSPClientPrivate *priv = client->priv;
2308 GST_INFO ("client %p: session %p removed", client, session);
2310 g_mutex_lock (&priv->lock);
2311 client_unwatch_session (client, session, NULL);
2312 g_mutex_unlock (&priv->lock);
2315 /* Returns TRUE if there are no Require headers, otherwise returns FALSE
2316 * and also returns a newly-allocated string of (comma-separated) unsupported
2317 * options in the unsupported_reqs variable .
2319 * There may be multiple Require headers, but we must send one single
2320 * Unsupported header with all the unsupported options as response. If
2321 * an incoming Require header contained a comma-separated list of options
2322 * GstRtspConnection will already have split that list up into multiple
2325 * TODO: allow the application to decide what features are supported
2328 check_request_requirements (GstRTSPMessage * msg, gchar ** unsupported_reqs)
2331 GPtrArray *arr = NULL;
2337 res = gst_rtsp_message_get_header (msg, GST_RTSP_HDR_REQUIRE, &reqs, i++);
2339 if (res == GST_RTSP_ENOTIMPL)
2343 arr = g_ptr_array_new_with_free_func ((GDestroyNotify) g_free);
2345 g_ptr_array_add (arr, g_strdup (reqs));
2349 /* if we don't have any Require headers at all, all is fine */
2353 /* otherwise we've now processed at all the Require headers */
2354 g_ptr_array_add (arr, NULL);
2356 /* for now we don't commit to supporting anything, so will just report
2357 * all of the required options as unsupported */
2358 *unsupported_reqs = g_strjoinv (", ", (gchar **) arr->pdata);
2360 g_ptr_array_unref (arr);
2365 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
2367 GstRTSPClientPrivate *priv = client->priv;
2368 GstRTSPMethod method;
2369 const gchar *uristr;
2370 GstRTSPUrl *uri = NULL;
2371 GstRTSPVersion version;
2373 GstRTSPSession *session = NULL;
2374 GstRTSPContext sctx = { NULL }, *ctx;
2375 GstRTSPMessage response = { 0 };
2376 gchar *unsupported_reqs = NULL;
2378 GstRTSPClientClass *klass;
2380 klass = GST_RTSP_CLIENT_GET_CLASS (client);
2382 if (!(ctx = gst_rtsp_context_get_current ())) {
2384 ctx->auth = priv->auth;
2385 gst_rtsp_context_push_current (ctx);
2388 ctx->conn = priv->connection;
2389 ctx->client = client;
2390 ctx->request = request;
2391 ctx->response = &response;
2393 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2394 gst_rtsp_message_dump (request);
2397 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
2399 GST_INFO ("client %p: received a request %s %s %s", client,
2400 gst_rtsp_method_as_text (method), uristr,
2401 gst_rtsp_version_as_text (version));
2403 /* we can only handle 1.0 requests */
2404 if (version != GST_RTSP_VERSION_1_0)
2407 ctx->method = method;
2409 /* we always try to parse the url first */
2410 if (strcmp (uristr, "*") == 0) {
2411 /* special case where we have * as uri, keep uri = NULL */
2412 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
2413 /* check if the uristr is an absolute path <=> scheme and host information
2417 scheme = g_uri_parse_scheme (uristr);
2418 if (scheme == NULL && g_str_has_prefix (uristr, "/")) {
2419 gchar *absolute_uristr = NULL;
2421 GST_WARNING_OBJECT (client, "request doesn't contain absolute url");
2422 if (priv->server_ip == NULL) {
2423 GST_WARNING_OBJECT (client, "host information missing");
2428 g_strdup_printf ("rtsp://%s%s", priv->server_ip, uristr);
2430 GST_DEBUG_OBJECT (client, "absolute url: %s", absolute_uristr);
2431 if (gst_rtsp_url_parse (absolute_uristr, &uri) != GST_RTSP_OK) {
2432 g_free (absolute_uristr);
2435 g_free (absolute_uristr);
2442 /* get the session if there is any */
2443 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
2444 if (res == GST_RTSP_OK) {
2445 if (priv->session_pool == NULL)
2448 /* we had a session in the request, find it again */
2449 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2450 goto session_not_found;
2452 /* we add the session to the client list of watched sessions. When a session
2453 * disappears because it times out, we will be notified. If all sessions are
2454 * gone, we will close the connection */
2455 client_watch_session (client, session);
2458 /* sanitize the uri */
2462 ctx->session = session;
2464 if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
2465 goto not_authorized;
2468 /* FIXME-WFD : How does it handle this */
2469 /* handle any 'Require' headers */
2470 if (!check_request_requirements (ctx->request, &unsupported_reqs))
2471 goto unsupported_requirement;
2474 /* now see what is asked and dispatch to a dedicated handler */
2476 case GST_RTSP_OPTIONS:
2477 klass->handle_options_request (client, ctx);
2479 case GST_RTSP_DESCRIBE:
2480 handle_describe_request (client, ctx);
2482 case GST_RTSP_SETUP:
2483 handle_setup_request (client, ctx);
2486 handle_play_request (client, ctx);
2488 case GST_RTSP_PAUSE:
2489 handle_pause_request (client, ctx);
2491 case GST_RTSP_TEARDOWN:
2492 handle_teardown_request (client, ctx);
2494 case GST_RTSP_SET_PARAMETER:
2495 klass->handle_set_param_request (client, ctx);
2497 case GST_RTSP_GET_PARAMETER:
2498 klass->handle_get_param_request (client, ctx);
2500 case GST_RTSP_ANNOUNCE:
2501 case GST_RTSP_RECORD:
2502 case GST_RTSP_REDIRECT:
2503 goto not_implemented;
2504 case GST_RTSP_INVALID:
2511 gst_rtsp_context_pop_current (ctx);
2513 g_object_unref (session);
2515 gst_rtsp_url_free (uri);
2521 GST_ERROR ("client %p: version %d not supported", client, version);
2522 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
2528 GST_ERROR ("client %p: bad request", client);
2529 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
2534 GST_ERROR ("client %p: no pool configured", client);
2535 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2540 GST_ERROR ("client %p: session not found", client);
2541 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
2546 GST_ERROR ("client %p: not allowed", client);
2547 /* error reply is already sent */
2551 unsupported_requirement:
2553 GST_ERROR ("client %p: Required option is not supported (%s)", client,
2555 send_option_not_supported_response (client, ctx, unsupported_reqs);
2556 g_free (unsupported_reqs);
2562 GST_ERROR ("client %p: method %d not implemented", client, method);
2563 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
2570 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
2572 GstRTSPClientPrivate *priv = client->priv;
2574 GstRTSPSession *session = NULL;
2575 GstRTSPContext sctx = { NULL }, *ctx;
2578 if (!(ctx = gst_rtsp_context_get_current ())) {
2580 ctx->auth = priv->auth;
2581 gst_rtsp_context_push_current (ctx);
2584 ctx->conn = priv->connection;
2585 ctx->client = client;
2586 ctx->request = NULL;
2588 ctx->method = GST_RTSP_INVALID;
2589 ctx->response = response;
2591 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2592 gst_rtsp_message_dump (response);
2595 GST_INFO ("client %p: received a response", client);
2597 /* get the session if there is any */
2599 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
2600 if (res == GST_RTSP_OK) {
2601 if (priv->session_pool == NULL)
2604 /* we had a session in the request, find it again */
2605 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2606 goto session_not_found;
2608 /* we add the session to the client list of watched sessions. When a session
2609 * disappears because it times out, we will be notified. If all sessions are
2610 * gone, we will close the connection */
2611 client_watch_session (client, session);
2614 ctx->session = session;
2616 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
2621 gst_rtsp_context_pop_current (ctx);
2623 g_object_unref (session);
2628 GST_ERROR ("client %p: no pool configured", client);
2633 GST_ERROR ("client %p: session not found", client);
2639 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2641 GstRTSPClientPrivate *priv = client->priv;
2650 /* find the stream for this message */
2651 res = gst_rtsp_message_parse_data (message, &channel);
2652 if (res != GST_RTSP_OK)
2655 gst_rtsp_message_steal_body (message, &data, &size);
2657 buffer = gst_buffer_new_wrapped (data, size);
2660 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2661 GstRTSPStreamTransport *trans;
2662 GstRTSPStream *stream;
2663 const GstRTSPTransport *tr;
2667 tr = gst_rtsp_stream_transport_get_transport (trans);
2668 stream = gst_rtsp_stream_transport_get_stream (trans);
2670 /* check for TCP transport */
2671 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2672 /* dispatch to the stream based on the channel number */
2673 if (tr->interleaved.min == channel) {
2674 gst_rtsp_stream_recv_rtp (stream, buffer);
2677 } else if (tr->interleaved.max == channel) {
2678 gst_rtsp_stream_recv_rtcp (stream, buffer);
2685 gst_buffer_unref (buffer);
2689 * gst_rtsp_client_set_session_pool:
2690 * @client: a #GstRTSPClient
2691 * @pool: (transfer none): a #GstRTSPSessionPool
2693 * Set @pool as the sessionpool for @client which it will use to find
2694 * or allocate sessions. the sessionpool is usually inherited from the server
2695 * that created the client but can be overridden later.
2698 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2699 GstRTSPSessionPool * pool)
2701 GstRTSPSessionPool *old;
2702 GstRTSPClientPrivate *priv;
2704 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2706 priv = client->priv;
2709 g_object_ref (pool);
2711 g_mutex_lock (&priv->lock);
2712 old = priv->session_pool;
2713 priv->session_pool = pool;
2715 if (priv->session_removed_id) {
2716 g_signal_handler_disconnect (old, priv->session_removed_id);
2717 priv->session_removed_id = 0;
2719 g_mutex_unlock (&priv->lock);
2721 /* FIXME, should remove all sessions from the old pool for this client */
2723 g_object_unref (old);
2727 * gst_rtsp_client_get_session_pool:
2728 * @client: a #GstRTSPClient
2730 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2732 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2734 GstRTSPSessionPool *
2735 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2737 GstRTSPClientPrivate *priv;
2738 GstRTSPSessionPool *result;
2740 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2742 priv = client->priv;
2744 g_mutex_lock (&priv->lock);
2745 if ((result = priv->session_pool))
2746 g_object_ref (result);
2747 g_mutex_unlock (&priv->lock);
2753 * gst_rtsp_client_set_mount_points:
2754 * @client: a #GstRTSPClient
2755 * @mounts: (transfer none): a #GstRTSPMountPoints
2757 * Set @mounts as the mount points for @client which it will use to map urls
2758 * to media streams. These mount points are usually inherited from the server that
2759 * created the client but can be overriden later.
2762 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2763 GstRTSPMountPoints * mounts)
2765 GstRTSPClientPrivate *priv;
2766 GstRTSPMountPoints *old;
2768 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2770 priv = client->priv;
2773 g_object_ref (mounts);
2775 g_mutex_lock (&priv->lock);
2776 old = priv->mount_points;
2777 priv->mount_points = mounts;
2778 g_mutex_unlock (&priv->lock);
2781 g_object_unref (old);
2785 * gst_rtsp_client_get_mount_points:
2786 * @client: a #GstRTSPClient
2788 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2790 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2792 GstRTSPMountPoints *
2793 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2795 GstRTSPClientPrivate *priv;
2796 GstRTSPMountPoints *result;
2798 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2800 priv = client->priv;
2802 g_mutex_lock (&priv->lock);
2803 if ((result = priv->mount_points))
2804 g_object_ref (result);
2805 g_mutex_unlock (&priv->lock);
2811 * gst_rtsp_client_set_auth:
2812 * @client: a #GstRTSPClient
2813 * @auth: (transfer none): a #GstRTSPAuth
2815 * configure @auth to be used as the authentication manager of @client.
2818 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2820 GstRTSPClientPrivate *priv;
2823 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2825 priv = client->priv;
2828 g_object_ref (auth);
2830 g_mutex_lock (&priv->lock);
2833 g_mutex_unlock (&priv->lock);
2836 g_object_unref (old);
2841 * gst_rtsp_client_get_auth:
2842 * @client: a #GstRTSPClient
2844 * Get the #GstRTSPAuth used as the authentication manager of @client.
2846 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2850 gst_rtsp_client_get_auth (GstRTSPClient * client)
2852 GstRTSPClientPrivate *priv;
2853 GstRTSPAuth *result;
2855 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2857 priv = client->priv;
2859 g_mutex_lock (&priv->lock);
2860 if ((result = priv->auth))
2861 g_object_ref (result);
2862 g_mutex_unlock (&priv->lock);
2868 * gst_rtsp_client_set_thread_pool:
2869 * @client: a #GstRTSPClient
2870 * @pool: (transfer none): a #GstRTSPThreadPool
2872 * configure @pool to be used as the thread pool of @client.
2875 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2876 GstRTSPThreadPool * pool)
2878 GstRTSPClientPrivate *priv;
2879 GstRTSPThreadPool *old;
2881 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2883 priv = client->priv;
2886 g_object_ref (pool);
2888 g_mutex_lock (&priv->lock);
2889 old = priv->thread_pool;
2890 priv->thread_pool = pool;
2891 g_mutex_unlock (&priv->lock);
2894 g_object_unref (old);
2898 * gst_rtsp_client_get_thread_pool:
2899 * @client: a #GstRTSPClient
2901 * Get the #GstRTSPThreadPool used as the thread pool of @client.
2903 * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2907 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2909 GstRTSPClientPrivate *priv;
2910 GstRTSPThreadPool *result;
2912 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2914 priv = client->priv;
2916 g_mutex_lock (&priv->lock);
2917 if ((result = priv->thread_pool))
2918 g_object_ref (result);
2919 g_mutex_unlock (&priv->lock);
2925 * gst_rtsp_client_set_connection:
2926 * @client: a #GstRTSPClient
2927 * @conn: (transfer full): a #GstRTSPConnection
2929 * Set the #GstRTSPConnection of @client. This function takes ownership of
2932 * Returns: %TRUE on success.
2935 gst_rtsp_client_set_connection (GstRTSPClient * client,
2936 GstRTSPConnection * conn)
2938 GstRTSPClientPrivate *priv;
2939 GSocket *read_socket;
2940 GSocketAddress *address;
2942 GError *error = NULL;
2944 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2945 g_return_val_if_fail (conn != NULL, FALSE);
2947 priv = client->priv;
2949 read_socket = gst_rtsp_connection_get_read_socket (conn);
2951 if (!(address = g_socket_get_local_address (read_socket, &error)))
2954 g_free (priv->server_ip);
2955 /* keep the original ip that the client connected to */
2956 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2957 GInetAddress *iaddr;
2959 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2961 /* socket might be ipv6 but adress still ipv4 */
2962 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2963 priv->server_ip = g_inet_address_to_string (iaddr);
2964 g_object_unref (address);
2966 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2967 priv->server_ip = g_strdup ("unknown");
2970 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2971 priv->server_ip, priv->is_ipv6);
2973 url = gst_rtsp_connection_get_url (conn);
2974 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2976 priv->connection = conn;
2983 GST_ERROR ("could not get local address %s", error->message);
2984 g_error_free (error);
2990 * gst_rtsp_client_get_connection:
2991 * @client: a #GstRTSPClient
2993 * Get the #GstRTSPConnection of @client.
2995 * Returns: (transfer none): the #GstRTSPConnection of @client.
2996 * The connection object returned remains valid until the client is freed.
2999 gst_rtsp_client_get_connection (GstRTSPClient * client)
3001 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
3003 return client->priv->connection;
3007 * gst_rtsp_client_set_send_func:
3008 * @client: a #GstRTSPClient
3009 * @func: (scope notified): a #GstRTSPClientSendFunc
3010 * @user_data: (closure): user data passed to @func
3011 * @notify: (allow-none): called when @user_data is no longer in use
3013 * Set @func as the callback that will be called when a new message needs to be
3014 * sent to the client. @user_data is passed to @func and @notify is called when
3015 * @user_data is no longer in use.
3017 * By default, the client will send the messages on the #GstRTSPConnection that
3018 * was configured with gst_rtsp_client_attach() was called.
3021 gst_rtsp_client_set_send_func (GstRTSPClient * client,
3022 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
3024 GstRTSPClientPrivate *priv;
3025 GDestroyNotify old_notify;
3028 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
3030 priv = client->priv;
3032 g_mutex_lock (&priv->send_lock);
3033 priv->send_func = func;
3034 old_notify = priv->send_notify;
3035 old_data = priv->send_data;
3036 priv->send_notify = notify;
3037 priv->send_data = user_data;
3038 g_mutex_unlock (&priv->send_lock);
3041 old_notify (old_data);
3045 * gst_rtsp_client_handle_message:
3046 * @client: a #GstRTSPClient
3047 * @message: (transfer none): an #GstRTSPMessage
3049 * Let the client handle @message.
3051 * Returns: a #GstRTSPResult.
3054 gst_rtsp_client_handle_message (GstRTSPClient * client,
3055 GstRTSPMessage * message)
3057 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
3058 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3060 switch (message->type) {
3061 case GST_RTSP_MESSAGE_REQUEST:
3062 handle_request (client, message);
3064 case GST_RTSP_MESSAGE_RESPONSE:
3065 handle_response (client, message);
3067 case GST_RTSP_MESSAGE_DATA:
3068 handle_data (client, message);
3077 * gst_rtsp_client_send_message:
3078 * @client: a #GstRTSPClient
3079 * @session: (allow-none) (transfer none): a #GstRTSPSession to send
3080 * the message to or %NULL
3081 * @message: (transfer none): The #GstRTSPMessage to send
3083 * Send a message message to the remote end. @message must be a
3084 * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
3087 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
3088 GstRTSPMessage * message)
3090 GstRTSPContext sctx = { NULL }
3092 GstRTSPClientPrivate *priv;
3094 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
3095 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3096 g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
3097 message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
3099 priv = client->priv;
3101 if (!(ctx = gst_rtsp_context_get_current ())) {
3103 ctx->auth = priv->auth;
3104 gst_rtsp_context_push_current (ctx);
3107 ctx->conn = priv->connection;
3108 ctx->client = client;
3109 ctx->session = session;
3111 send_message (client, ctx, message, FALSE);
3114 gst_rtsp_context_pop_current (ctx);
3119 static GstRTSPResult
3120 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
3121 gboolean close, gpointer user_data)
3123 GstRTSPClientPrivate *priv = client->priv;
3131 /* send the response and store the seq number so we can wait until it's
3132 * written to the client to close the connection */
3134 gst_rtsp_watch_send_message (priv->watch, message,
3135 close ? &priv->close_seq : NULL);
3136 if (ret == GST_RTSP_OK)
3139 if (ret != GST_RTSP_ENOMEM)
3143 if (priv->drop_backlog)
3146 /* queue was full, wait for more space */
3147 GST_DEBUG_OBJECT (client, "waiting for backlog");
3148 ret = gst_rtsp_watch_wait_backlog (priv->watch, &time);
3149 GST_DEBUG_OBJECT (client, "Resend due to backlog full");
3150 } while (ret != GST_RTSP_EINTR);
3157 GST_DEBUG_OBJECT (client, "got error %d", ret);
3162 static GstRTSPResult
3163 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
3166 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
3169 static GstRTSPResult
3170 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
3172 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3173 GstRTSPClientPrivate *priv = client->priv;
3175 if (priv->close_seq && priv->close_seq == cseq) {
3176 GST_INFO ("client %p: send close message", client);
3177 priv->close_seq = 0;
3178 gst_rtsp_client_close (client);
3184 static GstRTSPResult
3185 closed (GstRTSPWatch * watch, gpointer user_data)
3187 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3188 GstRTSPClientPrivate *priv = client->priv;
3189 const gchar *tunnelid;
3191 GST_INFO ("client %p: connection closed", client);
3193 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
3194 g_mutex_lock (&tunnels_lock);
3195 /* remove from tunnelids */
3196 g_hash_table_remove (tunnels, tunnelid);
3197 g_mutex_unlock (&tunnels_lock);
3200 gst_rtsp_watch_set_flushing (watch, TRUE);
3201 g_mutex_lock (&priv->watch_lock);
3202 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
3203 g_mutex_unlock (&priv->watch_lock);
3208 static GstRTSPResult
3209 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
3211 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3214 str = gst_rtsp_strresult (result);
3215 GST_INFO ("client %p: received an error %s", client, str);
3221 static GstRTSPResult
3222 error_full (GstRTSPWatch * watch, GstRTSPResult result,
3223 GstRTSPMessage * message, guint id, gpointer user_data)
3225 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3228 str = gst_rtsp_strresult (result);
3230 ("client %p: error when handling message %p with id %d: %s",
3231 client, message, id, str);
3238 remember_tunnel (GstRTSPClient * client)
3240 GstRTSPClientPrivate *priv = client->priv;
3241 const gchar *tunnelid;
3243 /* store client in the pending tunnels */
3244 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
3245 if (tunnelid == NULL)
3248 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
3250 /* we can't have two clients connecting with the same tunnelid */
3251 g_mutex_lock (&tunnels_lock);
3252 if (g_hash_table_lookup (tunnels, tunnelid))
3253 goto tunnel_existed;
3255 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
3256 g_mutex_unlock (&tunnels_lock);
3263 GST_ERROR ("client %p: no tunnelid provided", client);
3268 g_mutex_unlock (&tunnels_lock);
3269 GST_ERROR ("client %p: tunnel session %s already existed", client,
3275 static GstRTSPResult
3276 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
3278 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3279 GstRTSPClientPrivate *priv = client->priv;
3281 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
3284 /* ignore error, it'll only be a problem when the client does a POST again */
3285 remember_tunnel (client);
3291 handle_tunnel (GstRTSPClient * client)
3293 GstRTSPClientPrivate *priv = client->priv;
3294 GstRTSPClient *oclient;
3295 GstRTSPClientPrivate *opriv;
3296 const gchar *tunnelid;
3298 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
3299 if (tunnelid == NULL)
3302 /* check for previous tunnel */
3303 g_mutex_lock (&tunnels_lock);
3304 oclient = g_hash_table_lookup (tunnels, tunnelid);
3306 if (oclient == NULL) {
3307 /* no previous tunnel, remember tunnel */
3308 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
3309 g_mutex_unlock (&tunnels_lock);
3311 GST_INFO ("client %p: no previous tunnel found, remembering tunnel (%p)",
3312 client, priv->connection);
3314 /* merge both tunnels into the first client */
3315 /* remove the old client from the table. ref before because removing it will
3316 * remove the ref to it. */
3317 g_object_ref (oclient);
3318 g_hash_table_remove (tunnels, tunnelid);
3319 g_mutex_unlock (&tunnels_lock);
3321 opriv = oclient->priv;
3323 g_mutex_lock (&opriv->watch_lock);
3324 if (opriv->watch == NULL)
3327 GST_INFO ("client %p: found previous tunnel %p (old %p, new %p)", client,
3328 oclient, opriv->connection, priv->connection);
3330 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
3331 gst_rtsp_watch_reset (priv->watch);
3332 gst_rtsp_watch_reset (opriv->watch);
3333 g_mutex_unlock (&opriv->watch_lock);
3334 g_object_unref (oclient);
3336 /* the old client owns the tunnel now, the new one will be freed */
3337 g_source_destroy ((GSource *) priv->watch);
3339 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
3347 GST_ERROR ("client %p: no tunnelid provided", client);
3352 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
3353 g_mutex_unlock (&opriv->watch_lock);
3354 g_object_unref (oclient);
3359 static GstRTSPStatusCode
3360 tunnel_get (GstRTSPWatch * watch, gpointer user_data)
3362 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3364 GST_INFO ("client %p: tunnel get (connection %p)", client,
3365 client->priv->connection);
3367 if (!handle_tunnel (client)) {
3368 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
3371 return GST_RTSP_STS_OK;
3374 static GstRTSPResult
3375 tunnel_post (GstRTSPWatch * watch, gpointer user_data)
3377 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3379 GST_INFO ("client %p: tunnel post (connection %p)", client,
3380 client->priv->connection);
3382 if (!handle_tunnel (client)) {
3383 return GST_RTSP_ERROR;
3389 static GstRTSPResult
3390 tunnel_http_response (GstRTSPWatch * watch, GstRTSPMessage * request,
3391 GstRTSPMessage * response, gpointer user_data)
3393 GstRTSPClientClass *klass;
3395 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
3396 klass = GST_RTSP_CLIENT_GET_CLASS (client);
3398 if (klass->tunnel_http_response) {
3399 klass->tunnel_http_response (client, request, response);
3405 static GstRTSPWatchFuncs watch_funcs = {
3414 tunnel_http_response
3418 client_watch_notify (GstRTSPClient * client)
3420 GstRTSPClientPrivate *priv = client->priv;
3422 GST_INFO ("client %p: watch destroyed", client);
3424 g_main_context_unref (priv->watch_context);
3425 priv->watch_context = NULL;
3426 /* remove all sessions and so drop the extra client ref */
3427 gst_rtsp_client_session_filter (client, cleanup_session, NULL);
3428 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
3429 g_object_unref (client);
3433 * gst_rtsp_client_attach:
3434 * @client: a #GstRTSPClient
3435 * @context: (allow-none): a #GMainContext
3437 * Attaches @client to @context. When the mainloop for @context is run, the
3438 * client will be dispatched. When @context is %NULL, the default context will be
3441 * This function should be called when the client properties and urls are fully
3442 * configured and the client is ready to start.
3444 * Returns: the ID (greater than 0) for the source within the GMainContext.
3447 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
3449 GstRTSPClientPrivate *priv;
3452 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
3453 priv = client->priv;
3454 g_return_val_if_fail (priv->connection != NULL, 0);
3455 g_return_val_if_fail (priv->watch == NULL, 0);
3457 /* make sure noone will free the context before the watch is destroyed */
3458 priv->watch_context = g_main_context_ref (context);
3460 /* create watch for the connection and attach */
3461 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
3462 g_object_ref (client), (GDestroyNotify) client_watch_notify);
3463 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
3464 (GDestroyNotify) gst_rtsp_watch_unref);
3466 /* FIXME make this configurable. We don't want to do this yet because it will
3467 * be superceeded by a cache object later */
3468 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
3470 GST_INFO ("client %p: attaching to context %p", client, context);
3471 res = gst_rtsp_watch_attach (priv->watch, context);
3477 * gst_rtsp_client_session_filter:
3478 * @client: a #GstRTSPClient
3479 * @func: (scope call) (allow-none): a callback
3480 * @user_data: user data passed to @func
3482 * Call @func for each session managed by @client. The result value of @func
3483 * determines what happens to the session. @func will be called with @client
3484 * locked so no further actions on @client can be performed from @func.
3486 * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
3489 * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
3491 * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
3492 * will also be added with an additional ref to the result #GList of this
3495 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each session.
3497 * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
3498 * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3499 * element in the #GList should be unreffed before the list is freed.
3502 gst_rtsp_client_session_filter (GstRTSPClient * client,
3503 GstRTSPClientSessionFilterFunc func, gpointer user_data)
3505 GstRTSPClientPrivate *priv;
3506 GList *result, *walk, *next;
3507 GHashTable *visited;
3510 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
3512 priv = client->priv;
3516 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
3518 g_mutex_lock (&priv->lock);
3520 cookie = priv->sessions_cookie;
3521 for (walk = priv->sessions; walk; walk = next) {
3522 GstRTSPSession *sess = walk->data;
3523 GstRTSPFilterResult res;
3526 next = g_list_next (walk);
3529 /* only visit each session once */
3530 if (g_hash_table_contains (visited, sess))
3533 g_hash_table_add (visited, g_object_ref (sess));
3534 g_mutex_unlock (&priv->lock);
3536 res = func (client, sess, user_data);
3538 g_mutex_lock (&priv->lock);
3540 res = GST_RTSP_FILTER_REF;
3542 changed = (cookie != priv->sessions_cookie);
3545 case GST_RTSP_FILTER_REMOVE:
3546 /* stop watching the session and pretend it went away, if the list was
3547 * changed, we can't use the current list position, try to see if we
3548 * still have the session */
3549 client_unwatch_session (client, sess, changed ? NULL : walk);
3550 cookie = priv->sessions_cookie;
3552 case GST_RTSP_FILTER_REF:
3553 result = g_list_prepend (result, g_object_ref (sess));
3555 case GST_RTSP_FILTER_KEEP:
3562 g_mutex_unlock (&priv->lock);
3565 g_hash_table_unref (visited);