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.
23 #include "rtsp-client.h"
25 #include "rtsp-params.h"
27 #define GST_RTSP_CLIENT_GET_PRIVATE(obj) \
28 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_CLIENT, GstRTSPClientPrivate))
31 * send_lock, lock, tunnels_lock
34 struct _GstRTSPClientPrivate
36 GMutex lock; /* protects everything else */
38 GstRTSPConnection *connection;
43 gboolean use_client_settings;
45 GstRTSPClientSendFunc send_func; /* protected by send_lock */
46 gpointer send_data; /* protected by send_lock */
47 GDestroyNotify send_notify; /* protected by send_lock */
49 GstRTSPSessionPool *session_pool;
50 GstRTSPMountPoints *mount_points;
60 static GMutex tunnels_lock;
61 static GHashTable *tunnels; /* protected by tunnels_lock */
63 #define DEFAULT_SESSION_POOL NULL
64 #define DEFAULT_MOUNT_POINTS NULL
65 #define DEFAULT_USE_CLIENT_SETTINGS FALSE
72 PROP_USE_CLIENT_SETTINGS,
80 SIGNAL_OPTIONS_REQUEST,
81 SIGNAL_DESCRIBE_REQUEST,
85 SIGNAL_TEARDOWN_REQUEST,
86 SIGNAL_SET_PARAMETER_REQUEST,
87 SIGNAL_GET_PARAMETER_REQUEST,
91 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
92 #define GST_CAT_DEFAULT rtsp_client_debug
94 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
96 static void gst_rtsp_client_get_property (GObject * object, guint propid,
97 GValue * value, GParamSpec * pspec);
98 static void gst_rtsp_client_set_property (GObject * object, guint propid,
99 const GValue * value, GParamSpec * pspec);
100 static void gst_rtsp_client_finalize (GObject * obj);
102 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
103 static void client_session_finalized (GstRTSPClient * client,
104 GstRTSPSession * session);
105 static void unlink_session_transports (GstRTSPClient * client,
106 GstRTSPSession * session, GstRTSPSessionMedia * media);
107 static gboolean default_configure_client_transport (GstRTSPClient * client,
108 GstRTSPClientState * state, GstRTSPTransport * ct);
109 static GstRTSPResult default_params_set (GstRTSPClient * client,
110 GstRTSPClientState * state);
111 static GstRTSPResult default_params_get (GstRTSPClient * client,
112 GstRTSPClientState * state);
114 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
117 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
119 GObjectClass *gobject_class;
121 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
123 gobject_class = G_OBJECT_CLASS (klass);
125 gobject_class->get_property = gst_rtsp_client_get_property;
126 gobject_class->set_property = gst_rtsp_client_set_property;
127 gobject_class->finalize = gst_rtsp_client_finalize;
129 klass->create_sdp = create_sdp;
130 klass->configure_client_transport = default_configure_client_transport;
131 klass->params_set = default_params_set;
132 klass->params_get = default_params_get;
134 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
135 g_param_spec_object ("session-pool", "Session Pool",
136 "The session pool to use for client session",
137 GST_TYPE_RTSP_SESSION_POOL,
138 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
140 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
141 g_param_spec_object ("mount-points", "Mount Points",
142 "The mount points to use for client session",
143 GST_TYPE_RTSP_MOUNT_POINTS,
144 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
146 g_object_class_install_property (gobject_class, PROP_USE_CLIENT_SETTINGS,
147 g_param_spec_boolean ("use-client-settings", "Use Client Settings",
148 "Use client settings for ttl and destination in multicast",
149 DEFAULT_USE_CLIENT_SETTINGS,
150 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
152 gst_rtsp_client_signals[SIGNAL_CLOSED] =
153 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
154 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
155 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
157 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
158 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
159 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
160 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
162 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
163 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
164 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
165 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
168 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
169 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
170 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
171 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
174 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
175 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
176 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
177 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
180 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
181 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
182 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
183 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
186 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
187 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
188 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
189 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
192 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
193 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
194 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
195 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
198 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
199 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
200 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
201 set_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
202 G_TYPE_NONE, 1, G_TYPE_POINTER);
204 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
205 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
206 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
207 get_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
208 G_TYPE_NONE, 1, G_TYPE_POINTER);
211 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
212 g_mutex_init (&tunnels_lock);
214 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
218 gst_rtsp_client_init (GstRTSPClient * client)
220 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
224 g_mutex_init (&priv->lock);
225 g_mutex_init (&priv->send_lock);
226 priv->use_client_settings = DEFAULT_USE_CLIENT_SETTINGS;
230 static GstRTSPFilterResult
231 filter_session (GstRTSPSession * sess, GstRTSPSessionMedia * media,
234 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
236 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
237 unlink_session_transports (client, sess, media);
239 /* unmanage the media in the session */
240 return GST_RTSP_FILTER_REMOVE;
244 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
246 /* unlink all media managed in this session */
247 gst_rtsp_session_filter (session, filter_session, client);
251 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
253 GstRTSPClientPrivate *priv = client->priv;
256 for (walk = priv->sessions; walk; walk = g_list_next (walk)) {
257 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
259 /* we already know about this session */
260 if (msession == session)
264 GST_INFO ("watching session %p", session);
266 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
268 priv->sessions = g_list_prepend (priv->sessions, session);
272 client_cleanup_sessions (GstRTSPClient * client)
274 GstRTSPClientPrivate *priv = client->priv;
277 /* remove weak-ref from sessions */
278 for (sessions = priv->sessions; sessions; sessions = g_list_next (sessions)) {
279 GstRTSPSession *session = (GstRTSPSession *) sessions->data;
280 g_object_weak_unref (G_OBJECT (session),
281 (GWeakNotify) client_session_finalized, client);
282 client_unlink_session (client, session);
284 g_list_free (priv->sessions);
285 priv->sessions = NULL;
288 /* A client is finalized when the connection is broken */
290 gst_rtsp_client_finalize (GObject * obj)
292 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
293 GstRTSPClientPrivate *priv = client->priv;
295 GST_INFO ("finalize client %p", client);
297 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
300 g_source_destroy ((GSource *) priv->watch);
302 client_cleanup_sessions (client);
304 if (priv->connection)
305 gst_rtsp_connection_free (priv->connection);
306 if (priv->session_pool)
307 g_object_unref (priv->session_pool);
308 if (priv->mount_points)
309 g_object_unref (priv->mount_points);
311 g_object_unref (priv->auth);
314 gst_rtsp_url_free (priv->uri);
316 gst_rtsp_media_unprepare (priv->media);
317 g_object_unref (priv->media);
320 g_free (priv->server_ip);
321 g_mutex_clear (&priv->lock);
322 g_mutex_clear (&priv->send_lock);
324 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
328 gst_rtsp_client_get_property (GObject * object, guint propid,
329 GValue * value, GParamSpec * pspec)
331 GstRTSPClient *client = GST_RTSP_CLIENT (object);
334 case PROP_SESSION_POOL:
335 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
337 case PROP_MOUNT_POINTS:
338 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
340 case PROP_USE_CLIENT_SETTINGS:
341 g_value_set_boolean (value,
342 gst_rtsp_client_get_use_client_settings (client));
345 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
350 gst_rtsp_client_set_property (GObject * object, guint propid,
351 const GValue * value, GParamSpec * pspec)
353 GstRTSPClient *client = GST_RTSP_CLIENT (object);
356 case PROP_SESSION_POOL:
357 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
359 case PROP_MOUNT_POINTS:
360 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
362 case PROP_USE_CLIENT_SETTINGS:
363 gst_rtsp_client_set_use_client_settings (client,
364 g_value_get_boolean (value));
367 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
372 * gst_rtsp_client_new:
374 * Create a new #GstRTSPClient instance.
376 * Returns: a new #GstRTSPClient
379 gst_rtsp_client_new (void)
381 GstRTSPClient *result;
383 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
389 send_response (GstRTSPClient * client, GstRTSPSession * session,
390 GstRTSPMessage * response, gboolean close)
392 GstRTSPClientPrivate *priv = client->priv;
394 gst_rtsp_message_add_header (response, GST_RTSP_HDR_SERVER,
395 "GStreamer RTSP server");
397 /* remove any previous header */
398 gst_rtsp_message_remove_header (response, GST_RTSP_HDR_SESSION, -1);
400 /* add the new session header for new session ids */
402 gst_rtsp_message_take_header (response, GST_RTSP_HDR_SESSION,
403 gst_rtsp_session_get_header (session));
406 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
407 gst_rtsp_message_dump (response);
411 gst_rtsp_message_add_header (response, GST_RTSP_HDR_CONNECTION, "close");
413 g_mutex_lock (&priv->send_lock);
415 priv->send_func (client, response, close, priv->send_data);
416 g_mutex_unlock (&priv->send_lock);
418 gst_rtsp_message_unset (response);
422 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
423 GstRTSPClientState * state)
425 gst_rtsp_message_init_response (state->response, code,
426 gst_rtsp_status_as_text (code), state->request);
428 send_response (client, NULL, state->response, FALSE);
432 handle_unauthorized_request (GstRTSPClient * client, GstRTSPAuth * auth,
433 GstRTSPClientState * state)
435 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_UNAUTHORIZED,
436 gst_rtsp_status_as_text (GST_RTSP_STS_UNAUTHORIZED), state->request);
439 /* and let the authentication manager setup the auth tokens */
440 gst_rtsp_auth_setup_auth (auth, client, 0, state);
443 send_response (client, state->session, state->response, FALSE);
448 compare_uri (const GstRTSPUrl * uri1, const GstRTSPUrl * uri2)
450 if (uri1 == NULL || uri2 == NULL)
453 if (strcmp (uri1->abspath, uri2->abspath))
459 /* this function is called to initially find the media for the DESCRIBE request
460 * but is cached for when the same client (without breaking the connection) is
461 * doing a setup for the exact same url. */
462 static GstRTSPMedia *
463 find_media (GstRTSPClient * client, GstRTSPClientState * state)
465 GstRTSPClientPrivate *priv = client->priv;
466 GstRTSPMediaFactory *factory;
470 if (!compare_uri (priv->uri, state->uri)) {
471 /* remove any previously cached values before we try to construct a new
474 gst_rtsp_url_free (priv->uri);
477 gst_rtsp_media_unprepare (priv->media);
478 g_object_unref (priv->media);
482 if (!priv->mount_points)
483 goto no_mount_points;
485 /* find the factory for the uri first */
487 gst_rtsp_mount_points_find_factory (priv->mount_points,
491 /* check if we have access to the factory */
492 if ((auth = gst_rtsp_media_factory_get_auth (factory))) {
493 state->factory = factory;
495 if (!gst_rtsp_auth_check (auth, client, 0, state))
498 state->factory = NULL;
499 g_object_unref (auth);
502 /* prepare the media and add it to the pipeline */
503 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
506 g_object_unref (factory);
509 /* prepare the media */
510 if (!(gst_rtsp_media_prepare (media)))
513 /* now keep track of the uri and the media */
514 priv->uri = gst_rtsp_url_copy (state->uri);
516 state->media = media;
518 /* we have seen this uri before, used cached media */
520 state->media = media;
521 GST_INFO ("reusing cached media %p", media);
525 g_object_ref (media);
532 GST_ERROR ("client %p: no mount points configured", client);
533 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
538 GST_ERROR ("client %p: no factory for uri", client);
539 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
544 GST_ERROR ("client %p: unauthorized request", client);
545 handle_unauthorized_request (client, auth, state);
546 g_object_unref (factory);
547 state->factory = NULL;
548 g_object_unref (auth);
553 GST_ERROR ("client %p: can't create media", client);
554 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
555 g_object_unref (factory);
560 GST_ERROR ("client %p: can't prepare media", client);
561 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
562 g_object_unref (media);
568 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
570 GstRTSPClientPrivate *priv = client->priv;
571 GstRTSPMessage message = { 0 };
576 gst_rtsp_message_init_data (&message, channel);
578 /* FIXME, need some sort of iovec RTSPMessage here */
579 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
582 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
584 g_mutex_lock (&priv->send_lock);
586 priv->send_func (client, &message, FALSE, priv->send_data);
587 g_mutex_unlock (&priv->send_lock);
589 gst_rtsp_message_steal_body (&message, &data, &usize);
590 gst_buffer_unmap (buffer, &map_info);
592 gst_rtsp_message_unset (&message);
598 link_transport (GstRTSPClient * client, GstRTSPSession * session,
599 GstRTSPStreamTransport * trans)
601 GstRTSPClientPrivate *priv = client->priv;
603 GST_DEBUG ("client %p: linking transport %p", client, trans);
605 gst_rtsp_stream_transport_set_callbacks (trans,
606 (GstRTSPSendFunc) do_send_data,
607 (GstRTSPSendFunc) do_send_data, client, NULL);
609 priv->transports = g_list_prepend (priv->transports, trans);
611 /* make sure our session can't expire */
612 gst_rtsp_session_prevent_expire (session);
616 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
617 GstRTSPStreamTransport * trans)
619 GstRTSPClientPrivate *priv = client->priv;
621 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
623 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
625 priv->transports = g_list_remove (priv->transports, trans);
627 /* our session can now expire */
628 gst_rtsp_session_allow_expire (session);
632 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
633 GstRTSPSessionMedia * media)
638 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (media));
639 for (i = 0; i < n_streams; i++) {
640 GstRTSPStreamTransport *trans;
641 const GstRTSPTransport *tr;
643 /* get the transport, if there is no transport configured, skip this stream */
644 trans = gst_rtsp_session_media_get_transport (media, i);
648 tr = gst_rtsp_stream_transport_get_transport (trans);
650 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
651 /* for TCP, unlink the stream from the TCP connection of the client */
652 unlink_transport (client, session, trans);
658 close_connection (GstRTSPClient * client)
660 GstRTSPClientPrivate *priv = client->priv;
661 const gchar *tunnelid;
663 GST_DEBUG ("client %p: closing connection", client);
665 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
666 g_mutex_lock (&tunnels_lock);
667 /* remove from tunnelids */
668 g_hash_table_remove (tunnels, tunnelid);
669 g_mutex_unlock (&tunnels_lock);
672 gst_rtsp_connection_close (priv->connection);
676 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
678 GstRTSPClientPrivate *priv = client->priv;
679 GstRTSPSession *session;
680 GstRTSPSessionMedia *media;
681 GstRTSPStatusCode code;
686 session = state->session;
691 /* get a handle to the configuration of the media in the session */
692 media = gst_rtsp_session_get_media (session, state->uri);
696 state->sessmedia = media;
698 /* we emit the signal before closing the connection */
699 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
702 /* unlink the all TCP callbacks */
703 unlink_session_transports (client, session, media);
705 /* remove the session from the watched sessions */
706 g_object_weak_unref (G_OBJECT (session),
707 (GWeakNotify) client_session_finalized, client);
708 priv->sessions = g_list_remove (priv->sessions, session);
710 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
712 /* unmanage the media in the session, returns false if all media session
714 if (!gst_rtsp_session_release_media (session, media)) {
715 /* remove the session */
716 gst_rtsp_session_pool_remove (priv->session_pool, session);
718 /* construct the response now */
719 code = GST_RTSP_STS_OK;
720 gst_rtsp_message_init_response (state->response, code,
721 gst_rtsp_status_as_text (code), state->request);
723 send_response (client, session, state->response, TRUE);
730 GST_ERROR ("client %p: no session", client);
731 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
736 GST_ERROR ("client %p: no uri supplied", client);
737 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
742 GST_ERROR ("client %p: no media for uri", client);
743 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
749 default_params_set (GstRTSPClient * client, GstRTSPClientState * state)
753 res = gst_rtsp_params_set (client, state);
759 default_params_get (GstRTSPClient * client, GstRTSPClientState * state)
763 res = gst_rtsp_params_get (client, state);
769 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
775 res = gst_rtsp_message_get_body (state->request, &data, &size);
776 if (res != GST_RTSP_OK)
780 /* no body, keep-alive request */
781 send_generic_response (client, GST_RTSP_STS_OK, state);
783 /* there is a body, handle the params */
784 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, state);
785 if (res != GST_RTSP_OK)
788 send_response (client, state->session, state->response, FALSE);
791 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
799 GST_ERROR ("client %p: bad request", client);
800 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
806 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
812 res = gst_rtsp_message_get_body (state->request, &data, &size);
813 if (res != GST_RTSP_OK)
817 /* no body, keep-alive request */
818 send_generic_response (client, GST_RTSP_STS_OK, state);
820 /* there is a body, handle the params */
821 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, state);
822 if (res != GST_RTSP_OK)
825 send_response (client, state->session, state->response, FALSE);
828 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
836 GST_ERROR ("client %p: bad request", client);
837 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
843 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
845 GstRTSPSession *session;
846 GstRTSPSessionMedia *media;
847 GstRTSPStatusCode code;
848 GstRTSPState rtspstate;
850 if (!(session = state->session))
856 /* get a handle to the configuration of the media in the session */
857 media = gst_rtsp_session_get_media (session, state->uri);
861 state->sessmedia = media;
863 rtspstate = gst_rtsp_session_media_get_rtsp_state (media);
864 /* the session state must be playing or recording */
865 if (rtspstate != GST_RTSP_STATE_PLAYING &&
866 rtspstate != GST_RTSP_STATE_RECORDING)
869 /* unlink the all TCP callbacks */
870 unlink_session_transports (client, session, media);
872 /* then pause sending */
873 gst_rtsp_session_media_set_state (media, GST_STATE_PAUSED);
875 /* construct the response now */
876 code = GST_RTSP_STS_OK;
877 gst_rtsp_message_init_response (state->response, code,
878 gst_rtsp_status_as_text (code), state->request);
880 send_response (client, session, state->response, FALSE);
882 /* the state is now READY */
883 gst_rtsp_session_media_set_rtsp_state (media, GST_RTSP_STATE_READY);
885 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
893 GST_ERROR ("client %p: no seesion", client);
894 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
899 GST_ERROR ("client %p: no uri supplied", client);
900 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
905 GST_ERROR ("client %p: no media for uri", client);
906 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
911 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
912 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
919 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
921 GstRTSPSession *session;
922 GstRTSPSessionMedia *media;
923 GstRTSPStatusCode code;
925 guint n_streams, i, infocount;
927 GstRTSPTimeRange *range;
929 GstRTSPState rtspstate;
930 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
932 if (!(session = state->session))
938 /* get a handle to the configuration of the media in the session */
939 media = gst_rtsp_session_get_media (session, state->uri);
943 state->sessmedia = media;
945 /* the session state must be playing or ready */
946 rtspstate = gst_rtsp_session_media_get_rtsp_state (media);
947 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
950 /* parse the range header if we have one */
952 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
953 if (res == GST_RTSP_OK) {
954 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
955 /* we have a range, seek to the position */
957 gst_rtsp_media_seek (gst_rtsp_session_media_get_media (media), range);
958 gst_rtsp_range_free (range);
962 /* grab RTPInfo from the payloaders now */
963 rtpinfo = g_string_new ("");
966 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (media));
967 for (i = 0, infocount = 0; i < n_streams; i++) {
968 GstRTSPStreamTransport *trans;
969 GstRTSPStream *stream;
970 const GstRTSPTransport *tr;
974 /* get the transport, if there is no transport configured, skip this stream */
975 trans = gst_rtsp_session_media_get_transport (media, i);
977 GST_INFO ("stream %d is not configured", i);
980 tr = gst_rtsp_stream_transport_get_transport (trans);
982 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
983 /* for TCP, link the stream to the TCP connection of the client */
984 link_transport (client, session, trans);
987 stream = gst_rtsp_stream_transport_get_stream (trans);
988 if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
990 g_string_append (rtpinfo, ", ");
992 uristr = gst_rtsp_url_get_request_uri (state->uri);
993 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
994 uristr, i, seq, rtptime);
999 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
1003 /* construct the response now */
1004 code = GST_RTSP_STS_OK;
1005 gst_rtsp_message_init_response (state->response, code,
1006 gst_rtsp_status_as_text (code), state->request);
1008 /* add the RTP-Info header */
1009 if (infocount > 0) {
1010 str = g_string_free (rtpinfo, FALSE);
1011 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
1013 g_string_free (rtpinfo, TRUE);
1018 gst_rtsp_media_get_range_string (gst_rtsp_session_media_get_media (media),
1020 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
1022 send_response (client, session, state->response, FALSE);
1024 /* start playing after sending the request */
1025 gst_rtsp_session_media_set_state (media, GST_STATE_PLAYING);
1027 gst_rtsp_session_media_set_rtsp_state (media, GST_RTSP_STATE_PLAYING);
1029 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
1037 GST_ERROR ("client %p: no session", client);
1038 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1043 GST_ERROR ("client %p: no uri supplied", client);
1044 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1049 GST_ERROR ("client %p: media not found", client);
1050 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1055 GST_ERROR ("client %p: not PLAYING or READY", client);
1056 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1063 do_keepalive (GstRTSPSession * session)
1065 GST_INFO ("keep session %p alive", session);
1066 gst_rtsp_session_touch (session);
1069 /* parse @transport and return a valid transport in @tr. only transports
1070 * from @supported are returned. Returns FALSE if no valid transport
1073 parse_transport (const char *transport, GstRTSPLowerTrans supported,
1074 GstRTSPTransport * tr)
1081 gst_rtsp_transport_init (tr);
1083 GST_DEBUG ("parsing transports %s", transport);
1085 transports = g_strsplit (transport, ",", 0);
1087 /* loop through the transports, try to parse */
1088 for (i = 0; transports[i]; i++) {
1089 res = gst_rtsp_transport_parse (transports[i], tr);
1090 if (res != GST_RTSP_OK) {
1091 /* no valid transport, search some more */
1092 GST_WARNING ("could not parse transport %s", transports[i]);
1096 /* we have a transport, see if it's RTP/AVP */
1097 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1098 GST_WARNING ("invalid transport %s", transports[i]);
1102 if (!(tr->lower_transport & supported)) {
1103 GST_WARNING ("unsupported transport %s", transports[i]);
1107 /* we have a valid transport */
1108 GST_INFO ("found valid transport %s", transports[i]);
1113 gst_rtsp_transport_init (tr);
1115 g_strfreev (transports);
1121 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
1122 GstRTSPMessage * request)
1124 gchar *blocksize_str;
1125 gboolean ret = TRUE;
1127 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1128 &blocksize_str, 0) == GST_RTSP_OK) {
1132 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1133 if (end == blocksize_str) {
1134 GST_ERROR ("failed to parse blocksize");
1137 /* we don't want to change the mtu when this media
1138 * can be shared because it impacts other clients */
1139 if (gst_rtsp_media_is_shared (media))
1142 if (blocksize > G_MAXUINT)
1143 blocksize = G_MAXUINT;
1144 gst_rtsp_stream_set_mtu (stream, blocksize);
1151 default_configure_client_transport (GstRTSPClient * client,
1152 GstRTSPClientState * state, GstRTSPTransport * ct)
1154 GstRTSPClientPrivate *priv = client->priv;
1156 /* we have a valid transport now, set the destination of the client. */
1157 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1158 if (ct->destination && priv->use_client_settings) {
1159 GstRTSPAddress *addr;
1161 addr = gst_rtsp_stream_reserve_address (state->stream, ct->destination,
1162 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1167 gst_rtsp_address_free (addr);
1169 GstRTSPAddress *addr;
1171 addr = gst_rtsp_stream_get_address (state->stream);
1175 g_free (ct->destination);
1176 ct->destination = g_strdup (addr->address);
1177 ct->port.min = addr->port;
1178 ct->port.max = addr->port + addr->n_ports - 1;
1179 ct->ttl = addr->ttl;
1181 gst_rtsp_address_free (addr);
1186 url = gst_rtsp_connection_get_url (priv->connection);
1187 g_free (ct->destination);
1188 ct->destination = g_strdup (url->host);
1190 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1191 /* check if the client selected channels for TCP */
1192 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1193 gst_rtsp_session_media_alloc_channels (state->sessmedia,
1203 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1208 static GstRTSPTransport *
1209 make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
1210 GstRTSPTransport * ct)
1212 GstRTSPTransport *st;
1214 GSocketFamily family;
1216 /* prepare the server transport */
1217 gst_rtsp_transport_new (&st);
1219 st->trans = ct->trans;
1220 st->profile = ct->profile;
1221 st->lower_transport = ct->lower_transport;
1223 addr = g_inet_address_new_from_string (ct->destination);
1226 GST_ERROR ("failed to get inet addr from client destination");
1227 family = G_SOCKET_FAMILY_IPV4;
1229 family = g_inet_address_get_family (addr);
1230 g_object_unref (addr);
1234 switch (st->lower_transport) {
1235 case GST_RTSP_LOWER_TRANS_UDP:
1236 st->client_port = ct->client_port;
1237 gst_rtsp_stream_get_server_port (state->stream, &st->server_port, family);
1239 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1240 st->port = ct->port;
1241 st->destination = g_strdup (ct->destination);
1244 case GST_RTSP_LOWER_TRANS_TCP:
1245 st->interleaved = ct->interleaved;
1250 gst_rtsp_stream_get_ssrc (state->stream, &st->ssrc);
1256 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
1258 GstRTSPClientPrivate *priv = client->priv;
1262 GstRTSPTransport *ct, *st;
1263 GstRTSPLowerTrans supported;
1264 GstRTSPStatusCode code;
1265 GstRTSPSession *session;
1266 GstRTSPStreamTransport *trans;
1267 gchar *trans_str, *pos;
1269 GstRTSPSessionMedia *sessmedia;
1270 GstRTSPMedia *media;
1271 GstRTSPStream *stream;
1272 GstRTSPState rtspstate;
1273 GstRTSPClientClass *klass;
1280 /* the uri contains the stream number we added in the SDP config, which is
1281 * always /stream=%d so we need to strip that off
1282 * parse the stream we need to configure, look for the stream in the abspath
1283 * first and then in the query. */
1284 if (uri->abspath == NULL || !(pos = strstr (uri->abspath, "/stream="))) {
1285 if (uri->query == NULL || !(pos = strstr (uri->query, "/stream=")))
1289 /* we can mofify the parsed uri in place */
1292 pos += strlen ("/stream=");
1293 if (sscanf (pos, "%u", &streamid) != 1)
1296 /* parse the transport */
1298 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
1300 if (res != GST_RTSP_OK)
1303 gst_rtsp_transport_new (&ct);
1305 /* our supported transports */
1306 supported = GST_RTSP_LOWER_TRANS_UDP |
1307 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
1309 /* parse and find a usable supported transport */
1310 if (!parse_transport (transport, supported, ct))
1311 goto unsupported_transports;
1313 /* we create the session after parsing stuff so that we don't make
1314 * a session for malformed requests */
1315 if (priv->session_pool == NULL)
1318 session = state->session;
1321 g_object_ref (session);
1322 /* get a handle to the configuration of the media in the session, this can
1323 * return NULL if this is a new url to manage in this session. */
1324 sessmedia = gst_rtsp_session_get_media (session, uri);
1326 /* create a session if this fails we probably reached our session limit or
1328 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1329 goto service_unavailable;
1331 /* make sure this client is closed when the session is closed */
1332 client_watch_session (client, session);
1334 /* signal new session */
1335 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1338 state->session = session;
1340 /* we need a new media configuration in this session */
1344 /* we have no media, find one and manage it */
1345 if (sessmedia == NULL) {
1346 /* get a handle to the configuration of the media in the session */
1347 if ((media = find_media (client, state))) {
1348 /* manage the media in our session now */
1349 sessmedia = gst_rtsp_session_manage_media (session, uri, media);
1353 /* if we stil have no media, error */
1354 if (sessmedia == NULL)
1357 state->sessmedia = sessmedia;
1358 state->media = media = gst_rtsp_session_media_get_media (sessmedia);
1360 /* now get the stream */
1361 stream = gst_rtsp_media_get_stream (media, streamid);
1365 state->stream = stream;
1367 /* set blocksize on this stream */
1368 if (!handle_blocksize (media, stream, state->request))
1369 goto invalid_blocksize;
1371 /* update the client transport */
1372 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1373 if (!klass->configure_client_transport (client, state, ct))
1374 goto unsupported_client_transport;
1376 /* set in the session media transport */
1377 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1379 /* configure keepalive for this transport */
1380 gst_rtsp_stream_transport_set_keepalive (trans,
1381 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1383 /* create and serialize the server transport */
1384 st = make_server_transport (client, state, ct);
1385 trans_str = gst_rtsp_transport_as_text (st);
1386 gst_rtsp_transport_free (st);
1388 /* construct the response now */
1389 code = GST_RTSP_STS_OK;
1390 gst_rtsp_message_init_response (state->response, code,
1391 gst_rtsp_status_as_text (code), state->request);
1393 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1397 send_response (client, session, state->response, FALSE);
1399 /* update the state */
1400 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1401 switch (rtspstate) {
1402 case GST_RTSP_STATE_PLAYING:
1403 case GST_RTSP_STATE_RECORDING:
1404 case GST_RTSP_STATE_READY:
1405 /* no state change */
1408 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1411 g_object_unref (session);
1413 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST],
1421 GST_ERROR ("client %p: no uri", client);
1422 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1427 GST_ERROR ("client %p: bad request", client);
1428 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1433 GST_ERROR ("client %p: media not found", client);
1434 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1435 g_object_unref (session);
1436 gst_rtsp_transport_free (ct);
1441 GST_ERROR ("client %p: invalid blocksize", client);
1442 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1443 g_object_unref (session);
1444 gst_rtsp_transport_free (ct);
1447 unsupported_client_transport:
1449 GST_ERROR ("client %p: unsupported client transport", client);
1450 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1451 g_object_unref (session);
1452 gst_rtsp_transport_free (ct);
1457 GST_ERROR ("client %p: no transport", client);
1458 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1461 unsupported_transports:
1463 GST_ERROR ("client %p: unsupported transports", client);
1464 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1465 gst_rtsp_transport_free (ct);
1470 GST_ERROR ("client %p: no session pool configured", client);
1471 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1472 gst_rtsp_transport_free (ct);
1475 service_unavailable:
1477 GST_ERROR ("client %p: can't create session", client);
1478 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1479 gst_rtsp_transport_free (ct);
1484 static GstSDPMessage *
1485 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1487 GstRTSPClientPrivate *priv = client->priv;
1492 gst_sdp_message_new (&sdp);
1494 /* some standard things first */
1495 gst_sdp_message_set_version (sdp, "0");
1502 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1505 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1506 gst_sdp_message_set_information (sdp, "rtsp-server");
1507 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1508 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1509 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1510 gst_sdp_message_add_attribute (sdp, "control", "*");
1512 info.is_ipv6 = priv->is_ipv6;
1513 info.server_ip = priv->server_ip;
1515 /* create an SDP for the media object */
1516 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1524 GST_ERROR ("client %p: could not create SDP", client);
1525 gst_sdp_message_free (sdp);
1530 /* for the describe we must generate an SDP */
1532 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1537 gchar *str, *content_base;
1538 GstRTSPMedia *media;
1539 GstRTSPClientClass *klass;
1541 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1546 /* check what kind of format is accepted, we don't really do anything with it
1547 * and always return SDP for now. */
1552 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1554 if (res == GST_RTSP_ENOTIMPL)
1557 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1561 /* find the media object for the uri */
1562 if (!(media = find_media (client, state)))
1565 /* create an SDP for the media object on this client */
1566 if (!(sdp = klass->create_sdp (client, media)))
1569 g_object_unref (media);
1571 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1572 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1574 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1577 /* content base for some clients that might screw up creating the setup uri */
1578 str = gst_rtsp_url_get_request_uri (state->uri);
1579 str_len = strlen (str);
1581 /* check for trailing '/' and append one */
1582 if (str[str_len - 1] != '/') {
1583 content_base = g_malloc (str_len + 2);
1584 memcpy (content_base, str, str_len);
1585 content_base[str_len] = '/';
1586 content_base[str_len + 1] = '\0';
1592 GST_INFO ("adding content-base: %s", content_base);
1594 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1596 g_free (content_base);
1598 /* add SDP to the response body */
1599 str = gst_sdp_message_as_text (sdp);
1600 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1601 gst_sdp_message_free (sdp);
1603 send_response (client, state->session, state->response, FALSE);
1605 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1613 GST_ERROR ("client %p: no uri", client);
1614 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1619 GST_ERROR ("client %p: no media", client);
1620 /* error reply is already sent */
1625 GST_ERROR ("client %p: can't create SDP", client);
1626 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1627 g_object_unref (media);
1633 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1635 GstRTSPMethod options;
1638 options = GST_RTSP_DESCRIBE |
1643 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1645 str = gst_rtsp_options_as_text (options);
1647 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1648 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1650 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1653 send_response (client, state->session, state->response, FALSE);
1655 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1661 /* remove duplicate and trailing '/' */
1663 sanitize_uri (GstRTSPUrl * uri)
1667 gboolean have_slash, prev_slash;
1669 s = d = uri->abspath;
1670 len = strlen (uri->abspath);
1674 for (i = 0; i < len; i++) {
1675 have_slash = s[i] == '/';
1677 if (!have_slash || !prev_slash)
1679 prev_slash = have_slash;
1681 len = d - uri->abspath;
1682 /* don't remove the first slash if that's the only thing left */
1683 if (len > 1 && *(d - 1) == '/')
1689 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1691 GstRTSPClientPrivate *priv = client->priv;
1693 GST_INFO ("client %p: session %p finished", client, session);
1695 /* unlink all media managed in this session */
1696 client_unlink_session (client, session);
1698 /* remove the session */
1699 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1700 GST_INFO ("client %p: all sessions finalized, close the connection",
1702 close_connection (client);
1707 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1709 GstRTSPClientPrivate *priv = client->priv;
1710 GstRTSPMethod method;
1711 const gchar *uristr;
1712 GstRTSPUrl *uri = NULL;
1713 GstRTSPVersion version;
1715 GstRTSPSession *session = NULL;
1716 GstRTSPClientState state = { NULL };
1717 GstRTSPMessage response = { 0 };
1720 state.request = request;
1721 state.response = &response;
1723 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1724 gst_rtsp_message_dump (request);
1727 GST_INFO ("client %p: received a request", client);
1729 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1731 /* we can only handle 1.0 requests */
1732 if (version != GST_RTSP_VERSION_1_0)
1735 state.method = method;
1737 /* we always try to parse the url first */
1738 if (strcmp (uristr, "*") == 0) {
1739 /* special case where we have * as uri, keep uri = NULL */
1740 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1743 /* get the session if there is any */
1744 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1745 if (res == GST_RTSP_OK) {
1746 if (priv->session_pool == NULL)
1749 /* we had a session in the request, find it again */
1750 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1751 goto session_not_found;
1753 /* we add the session to the client list of watched sessions. When a session
1754 * disappears because it times out, we will be notified. If all sessions are
1755 * gone, we will close the connection */
1756 client_watch_session (client, session);
1759 /* sanitize the uri */
1763 state.session = session;
1766 if (!gst_rtsp_auth_check (priv->auth, client, 0, &state))
1767 goto not_authorized;
1770 /* now see what is asked and dispatch to a dedicated handler */
1772 case GST_RTSP_OPTIONS:
1773 handle_options_request (client, &state);
1775 case GST_RTSP_DESCRIBE:
1776 handle_describe_request (client, &state);
1778 case GST_RTSP_SETUP:
1779 handle_setup_request (client, &state);
1782 handle_play_request (client, &state);
1784 case GST_RTSP_PAUSE:
1785 handle_pause_request (client, &state);
1787 case GST_RTSP_TEARDOWN:
1788 handle_teardown_request (client, &state);
1790 case GST_RTSP_SET_PARAMETER:
1791 handle_set_param_request (client, &state);
1793 case GST_RTSP_GET_PARAMETER:
1794 handle_get_param_request (client, &state);
1796 case GST_RTSP_ANNOUNCE:
1797 case GST_RTSP_RECORD:
1798 case GST_RTSP_REDIRECT:
1799 goto not_implemented;
1800 case GST_RTSP_INVALID:
1807 g_object_unref (session);
1809 gst_rtsp_url_free (uri);
1815 GST_ERROR ("client %p: version %d not supported", client, version);
1816 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1822 GST_ERROR ("client %p: bad request", client);
1823 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1828 GST_ERROR ("client %p: no pool configured", client);
1829 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1834 GST_ERROR ("client %p: session not found", client);
1835 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1840 GST_ERROR ("client %p: not allowed", client);
1841 handle_unauthorized_request (client, priv->auth, &state);
1846 GST_ERROR ("client %p: method %d not implemented", client, method);
1847 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1853 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1855 GstRTSPClientPrivate *priv = client->priv;
1864 /* find the stream for this message */
1865 res = gst_rtsp_message_parse_data (message, &channel);
1866 if (res != GST_RTSP_OK)
1869 gst_rtsp_message_steal_body (message, &data, &size);
1871 buffer = gst_buffer_new_wrapped (data, size);
1874 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1875 GstRTSPStreamTransport *trans;
1876 GstRTSPStream *stream;
1877 const GstRTSPTransport *tr;
1881 tr = gst_rtsp_stream_transport_get_transport (trans);
1882 stream = gst_rtsp_stream_transport_get_stream (trans);
1884 /* check for TCP transport */
1885 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1886 /* dispatch to the stream based on the channel number */
1887 if (tr->interleaved.min == channel) {
1888 gst_rtsp_stream_recv_rtp (stream, buffer);
1891 } else if (tr->interleaved.max == channel) {
1892 gst_rtsp_stream_recv_rtcp (stream, buffer);
1899 gst_buffer_unref (buffer);
1903 * gst_rtsp_client_set_session_pool:
1904 * @client: a #GstRTSPClient
1905 * @pool: a #GstRTSPSessionPool
1907 * Set @pool as the sessionpool for @client which it will use to find
1908 * or allocate sessions. the sessionpool is usually inherited from the server
1909 * that created the client but can be overridden later.
1912 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
1913 GstRTSPSessionPool * pool)
1915 GstRTSPSessionPool *old;
1916 GstRTSPClientPrivate *priv;
1918 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1920 priv = client->priv;
1923 g_object_ref (pool);
1925 g_mutex_lock (&priv->lock);
1926 old = priv->session_pool;
1927 priv->session_pool = pool;
1928 g_mutex_unlock (&priv->lock);
1931 g_object_unref (old);
1935 * gst_rtsp_client_get_session_pool:
1936 * @client: a #GstRTSPClient
1938 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
1940 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
1942 GstRTSPSessionPool *
1943 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
1945 GstRTSPClientPrivate *priv;
1946 GstRTSPSessionPool *result;
1948 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1950 priv = client->priv;
1952 g_mutex_lock (&priv->lock);
1953 if ((result = priv->session_pool))
1954 g_object_ref (result);
1955 g_mutex_unlock (&priv->lock);
1961 * gst_rtsp_client_set_mount_points:
1962 * @client: a #GstRTSPClient
1963 * @mounts: a #GstRTSPMountPoints
1965 * Set @mounts as the mount points for @client which it will use to map urls
1966 * to media streams. These mount points are usually inherited from the server that
1967 * created the client but can be overriden later.
1970 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
1971 GstRTSPMountPoints * mounts)
1973 GstRTSPClientPrivate *priv;
1974 GstRTSPMountPoints *old;
1976 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1978 priv = client->priv;
1981 g_object_ref (mounts);
1983 g_mutex_lock (&priv->lock);
1984 old = priv->mount_points;
1985 priv->mount_points = mounts;
1986 g_mutex_unlock (&priv->lock);
1989 g_object_unref (old);
1993 * gst_rtsp_client_get_mount_points:
1994 * @client: a #GstRTSPClient
1996 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
1998 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2000 GstRTSPMountPoints *
2001 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2003 GstRTSPClientPrivate *priv;
2004 GstRTSPMountPoints *result;
2006 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2008 priv = client->priv;
2010 g_mutex_lock (&priv->lock);
2011 if ((result = priv->mount_points))
2012 g_object_ref (result);
2013 g_mutex_unlock (&priv->lock);
2019 * gst_rtsp_client_set_use_client_settings:
2020 * @client: a #GstRTSPClient
2021 * @use_client_settings: whether to use client settings for multicast
2023 * Use client transport settings (destination and ttl) for multicast.
2024 * When @use_client_settings is %FALSE, the server settings will be
2028 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
2029 gboolean use_client_settings)
2031 GstRTSPClientPrivate *priv;
2033 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2035 priv = client->priv;
2037 g_mutex_lock (&priv->lock);
2038 priv->use_client_settings = use_client_settings;
2039 g_mutex_unlock (&priv->lock);
2043 * gst_rtsp_client_get_use_client_settings:
2044 * @client: a #GstRTSPClient
2046 * Check if client transport settings (destination and ttl) for multicast
2050 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
2052 GstRTSPClientPrivate *priv;
2055 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2057 priv = client->priv;
2059 g_mutex_lock (&priv->lock);
2060 res = priv->use_client_settings;
2061 g_mutex_unlock (&priv->lock);
2067 * gst_rtsp_client_set_auth:
2068 * @client: a #GstRTSPClient
2069 * @auth: a #GstRTSPAuth
2071 * configure @auth to be used as the authentication manager of @client.
2074 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2076 GstRTSPClientPrivate *priv;
2079 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2081 priv = client->priv;
2084 g_object_ref (auth);
2086 g_mutex_lock (&priv->lock);
2089 g_mutex_unlock (&priv->lock);
2092 g_object_unref (old);
2097 * gst_rtsp_client_get_auth:
2098 * @client: a #GstRTSPClient
2100 * Get the #GstRTSPAuth used as the authentication manager of @client.
2102 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2106 gst_rtsp_client_get_auth (GstRTSPClient * client)
2108 GstRTSPClientPrivate *priv;
2109 GstRTSPAuth *result;
2111 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2113 priv = client->priv;
2115 g_mutex_lock (&priv->lock);
2116 if ((result = priv->auth))
2117 g_object_ref (result);
2118 g_mutex_unlock (&priv->lock);
2124 * gst_rtsp_client_get_uri:
2125 * @client: a #GstRTSPClient
2127 * Get the #GstRTSPUrl of @client.
2129 * Returns: (transfer full): the #GstRTSPUrl of @client. Free with
2130 * gst_rtsp_url_free () after usage.
2133 gst_rtsp_client_get_uri (GstRTSPClient * client)
2135 GstRTSPClientPrivate *priv;
2136 GstRTSPUrl *result = NULL;
2138 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2140 priv = client->priv;
2142 g_mutex_lock (&priv->lock);
2143 if (priv->uri != NULL)
2144 result = gst_rtsp_url_copy (priv->uri);
2145 g_mutex_unlock (&priv->lock);
2151 * gst_rtsp_client_set_connection:
2152 * @client: a #GstRTSPClient
2153 * @conn: (transfer full): a #GstRTSPConnection
2155 * Set the #GstRTSPConnection of @client. This function takes ownership of
2158 * Returns: %TRUE on success.
2161 gst_rtsp_client_set_connection (GstRTSPClient * client,
2162 GstRTSPConnection * conn)
2164 GstRTSPClientPrivate *priv;
2165 GSocket *read_socket;
2166 GSocketAddress *address;
2168 GError *error = NULL;
2170 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2171 g_return_val_if_fail (conn != NULL, FALSE);
2173 priv = client->priv;
2175 read_socket = gst_rtsp_connection_get_read_socket (conn);
2177 if (!(address = g_socket_get_local_address (read_socket, &error)))
2180 g_free (priv->server_ip);
2181 /* keep the original ip that the client connected to */
2182 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2183 GInetAddress *iaddr;
2185 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2187 /* socket might be ipv6 but adress still ipv4 */
2188 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2189 priv->server_ip = g_inet_address_to_string (iaddr);
2190 g_object_unref (address);
2192 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2193 priv->server_ip = g_strdup ("unknown");
2196 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2197 priv->server_ip, priv->is_ipv6);
2199 url = gst_rtsp_connection_get_url (conn);
2200 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2202 priv->connection = conn;
2209 GST_ERROR ("could not get remote address %s", error->message);
2210 g_error_free (error);
2216 * gst_rtsp_client_get_connection:
2217 * @client: a #GstRTSPClient
2219 * Get the #GstRTSPConnection of @client.
2221 * Returns: (transfer none): the #GstRTSPConnection of @client.
2222 * The connection object returned remains valid until the client is freed.
2225 gst_rtsp_client_get_connection (GstRTSPClient * client)
2227 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2229 return client->priv->connection;
2233 * gst_rtsp_client_set_send_func:
2234 * @client: a #GstRTSPClient
2235 * @func: a #GstRTSPClientSendFunc
2236 * @user_data: user data passed to @func
2237 * @notify: called when @user_data is no longer in use
2239 * Set @func as the callback that will be called when a new message needs to be
2240 * sent to the client. @user_data is passed to @func and @notify is called when
2241 * @user_data is no longer in use.
2244 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2245 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2247 GstRTSPClientPrivate *priv;
2248 GDestroyNotify old_notify;
2251 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2253 priv = client->priv;
2255 g_mutex_lock (&priv->send_lock);
2256 priv->send_func = func;
2257 old_notify = priv->send_notify;
2258 old_data = priv->send_data;
2259 priv->send_notify = notify;
2260 priv->send_data = user_data;
2261 g_mutex_unlock (&priv->send_lock);
2264 old_notify (old_data);
2268 * gst_rtsp_client_handle_message:
2269 * @client: a #GstRTSPClient
2270 * @message: an #GstRTSPMessage
2272 * Let the client handle @message.
2274 * Returns: a #GstRTSPResult.
2277 gst_rtsp_client_handle_message (GstRTSPClient * client,
2278 GstRTSPMessage * message)
2280 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2281 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2283 switch (message->type) {
2284 case GST_RTSP_MESSAGE_REQUEST:
2285 handle_request (client, message);
2287 case GST_RTSP_MESSAGE_RESPONSE:
2289 case GST_RTSP_MESSAGE_DATA:
2290 handle_data (client, message);
2298 static GstRTSPResult
2299 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2300 gboolean close, gpointer user_data)
2302 GstRTSPClientPrivate *priv = client->priv;
2304 /* send the response and store the seq number so we can wait until it's
2305 * written to the client to close the connection */
2306 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2307 &priv->close_seq : NULL);
2310 static GstRTSPResult
2311 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2314 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2317 static GstRTSPResult
2318 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2320 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2321 GstRTSPClientPrivate *priv = client->priv;
2323 if (priv->close_seq && priv->close_seq == cseq) {
2324 priv->close_seq = 0;
2325 close_connection (client);
2331 static GstRTSPResult
2332 closed (GstRTSPWatch * watch, gpointer user_data)
2334 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2335 GstRTSPClientPrivate *priv = client->priv;
2336 const gchar *tunnelid;
2338 GST_INFO ("client %p: connection closed", client);
2340 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2341 g_mutex_lock (&tunnels_lock);
2342 /* remove from tunnelids */
2343 g_hash_table_remove (tunnels, tunnelid);
2344 g_mutex_unlock (&tunnels_lock);
2347 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2352 static GstRTSPResult
2353 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2355 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2358 str = gst_rtsp_strresult (result);
2359 GST_INFO ("client %p: received an error %s", client, str);
2365 static GstRTSPResult
2366 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2367 GstRTSPMessage * message, guint id, gpointer user_data)
2369 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2372 str = gst_rtsp_strresult (result);
2374 ("client %p: error when handling message %p with id %d: %s",
2375 client, message, id, str);
2382 remember_tunnel (GstRTSPClient * client)
2384 GstRTSPClientPrivate *priv = client->priv;
2385 const gchar *tunnelid;
2387 /* store client in the pending tunnels */
2388 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2389 if (tunnelid == NULL)
2392 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2394 /* we can't have two clients connecting with the same tunnelid */
2395 g_mutex_lock (&tunnels_lock);
2396 if (g_hash_table_lookup (tunnels, tunnelid))
2397 goto tunnel_existed;
2399 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2400 g_mutex_unlock (&tunnels_lock);
2407 GST_ERROR ("client %p: no tunnelid provided", client);
2412 g_mutex_unlock (&tunnels_lock);
2413 GST_ERROR ("client %p: tunnel session %s already existed", client,
2419 static GstRTSPStatusCode
2420 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2422 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2423 GstRTSPClientPrivate *priv = client->priv;
2425 GST_INFO ("client %p: tunnel start (connection %p)", client,
2428 if (!remember_tunnel (client))
2431 return GST_RTSP_STS_OK;
2436 GST_ERROR ("client %p: error starting tunnel", client);
2437 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2441 static GstRTSPResult
2442 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2444 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2445 GstRTSPClientPrivate *priv = client->priv;
2447 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2450 /* ignore error, it'll only be a problem when the client does a POST again */
2451 remember_tunnel (client);
2456 static GstRTSPResult
2457 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2459 const gchar *tunnelid;
2460 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2461 GstRTSPClientPrivate *priv = client->priv;
2462 GstRTSPClient *oclient;
2463 GstRTSPClientPrivate *opriv;
2465 GST_INFO ("client %p: tunnel complete", client);
2467 /* find previous tunnel */
2468 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2469 if (tunnelid == NULL)
2472 g_mutex_lock (&tunnels_lock);
2473 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2476 /* remove the old client from the table. ref before because removing it will
2477 * remove the ref to it. */
2478 g_object_ref (oclient);
2479 g_hash_table_remove (tunnels, tunnelid);
2481 opriv = oclient->priv;
2483 if (opriv->watch == NULL)
2485 g_mutex_unlock (&tunnels_lock);
2487 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2488 opriv->connection, priv->connection);
2490 /* merge the tunnels into the first client */
2491 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2492 gst_rtsp_watch_reset (opriv->watch);
2493 g_object_unref (oclient);
2500 GST_ERROR ("client %p: no tunnelid provided", client);
2501 return GST_RTSP_ERROR;
2505 g_mutex_unlock (&tunnels_lock);
2506 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2507 return GST_RTSP_ERROR;
2511 g_mutex_unlock (&tunnels_lock);
2512 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2513 g_object_unref (oclient);
2514 return GST_RTSP_ERROR;
2518 static GstRTSPWatchFuncs watch_funcs = {
2530 client_watch_notify (GstRTSPClient * client)
2532 GstRTSPClientPrivate *priv = client->priv;
2534 GST_INFO ("client %p: watch destroyed", client);
2536 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2537 g_object_unref (client);
2541 * gst_rtsp_client_attach:
2542 * @client: a #GstRTSPClient
2543 * @context: (allow-none): a #GMainContext
2545 * Attaches @client to @context. When the mainloop for @context is run, the
2546 * client will be dispatched. When @context is NULL, the default context will be
2549 * This function should be called when the client properties and urls are fully
2550 * configured and the client is ready to start.
2552 * Returns: the ID (greater than 0) for the source within the GMainContext.
2555 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2557 GstRTSPClientPrivate *priv;
2560 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2561 priv = client->priv;
2562 g_return_val_if_fail (priv->watch == NULL, 0);
2564 /* create watch for the connection and attach */
2565 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2566 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2567 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2568 (GDestroyNotify) gst_rtsp_watch_unref);
2570 /* FIXME make this configurable. We don't want to do this yet because it will
2571 * be superceeded by a cache object later */
2572 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2574 GST_INFO ("attaching to context %p", context);
2575 res = gst_rtsp_watch_attach (priv->watch, context);