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);
108 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
111 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
113 GObjectClass *gobject_class;
115 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
117 gobject_class = G_OBJECT_CLASS (klass);
119 gobject_class->get_property = gst_rtsp_client_get_property;
120 gobject_class->set_property = gst_rtsp_client_set_property;
121 gobject_class->finalize = gst_rtsp_client_finalize;
123 klass->create_sdp = create_sdp;
125 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
126 g_param_spec_object ("session-pool", "Session Pool",
127 "The session pool to use for client session",
128 GST_TYPE_RTSP_SESSION_POOL,
129 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
131 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
132 g_param_spec_object ("mount-points", "Mount Points",
133 "The mount points to use for client session",
134 GST_TYPE_RTSP_MOUNT_POINTS,
135 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
137 g_object_class_install_property (gobject_class, PROP_USE_CLIENT_SETTINGS,
138 g_param_spec_boolean ("use-client-settings", "Use Client Settings",
139 "Use client settings for ttl and destination in multicast",
140 DEFAULT_USE_CLIENT_SETTINGS,
141 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
143 gst_rtsp_client_signals[SIGNAL_CLOSED] =
144 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
145 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
146 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
148 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
149 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
150 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
151 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
153 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
154 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
155 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
156 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
159 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
160 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
161 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
162 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
165 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
166 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
167 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
168 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
171 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
172 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
173 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
174 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
177 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
178 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
179 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
180 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
183 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
184 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
185 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
186 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
189 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
190 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
191 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
192 set_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
193 G_TYPE_NONE, 1, G_TYPE_POINTER);
195 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
196 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
197 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
198 get_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
199 G_TYPE_NONE, 1, G_TYPE_POINTER);
202 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
203 g_mutex_init (&tunnels_lock);
205 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
209 gst_rtsp_client_init (GstRTSPClient * client)
211 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
215 g_mutex_init (&priv->lock);
216 g_mutex_init (&priv->send_lock);
217 priv->use_client_settings = DEFAULT_USE_CLIENT_SETTINGS;
221 static GstRTSPFilterResult
222 filter_session (GstRTSPSession * sess, GstRTSPSessionMedia * media,
225 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
227 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
228 unlink_session_transports (client, sess, media);
230 /* unmanage the media in the session */
231 return GST_RTSP_FILTER_REMOVE;
235 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
237 /* unlink all media managed in this session */
238 gst_rtsp_session_filter (session, filter_session, client);
242 client_cleanup_sessions (GstRTSPClient * client)
244 GstRTSPClientPrivate *priv = client->priv;
247 /* remove weak-ref from sessions */
248 for (sessions = priv->sessions; sessions; sessions = g_list_next (sessions)) {
249 GstRTSPSession *session = (GstRTSPSession *) sessions->data;
250 g_object_weak_unref (G_OBJECT (session),
251 (GWeakNotify) client_session_finalized, client);
252 client_unlink_session (client, session);
254 g_list_free (priv->sessions);
255 priv->sessions = NULL;
258 /* A client is finalized when the connection is broken */
260 gst_rtsp_client_finalize (GObject * obj)
262 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
263 GstRTSPClientPrivate *priv = client->priv;
265 GST_INFO ("finalize client %p", client);
267 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
270 g_source_destroy ((GSource *) priv->watch);
272 client_cleanup_sessions (client);
274 if (priv->connection)
275 gst_rtsp_connection_free (priv->connection);
276 if (priv->session_pool)
277 g_object_unref (priv->session_pool);
278 if (priv->mount_points)
279 g_object_unref (priv->mount_points);
281 g_object_unref (priv->auth);
284 gst_rtsp_url_free (priv->uri);
286 gst_rtsp_media_unprepare (priv->media);
287 g_object_unref (priv->media);
290 g_free (priv->server_ip);
291 g_mutex_clear (&priv->lock);
292 g_mutex_clear (&priv->send_lock);
294 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
298 gst_rtsp_client_get_property (GObject * object, guint propid,
299 GValue * value, GParamSpec * pspec)
301 GstRTSPClient *client = GST_RTSP_CLIENT (object);
304 case PROP_SESSION_POOL:
305 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
307 case PROP_MOUNT_POINTS:
308 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
310 case PROP_USE_CLIENT_SETTINGS:
311 g_value_set_boolean (value,
312 gst_rtsp_client_get_use_client_settings (client));
315 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
320 gst_rtsp_client_set_property (GObject * object, guint propid,
321 const GValue * value, GParamSpec * pspec)
323 GstRTSPClient *client = GST_RTSP_CLIENT (object);
326 case PROP_SESSION_POOL:
327 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
329 case PROP_MOUNT_POINTS:
330 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
332 case PROP_USE_CLIENT_SETTINGS:
333 gst_rtsp_client_set_use_client_settings (client,
334 g_value_get_boolean (value));
337 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
342 * gst_rtsp_client_new:
344 * Create a new #GstRTSPClient instance.
346 * Returns: a new #GstRTSPClient
349 gst_rtsp_client_new (void)
351 GstRTSPClient *result;
353 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
359 send_response (GstRTSPClient * client, GstRTSPSession * session,
360 GstRTSPMessage * response, gboolean close)
362 GstRTSPClientPrivate *priv = client->priv;
364 gst_rtsp_message_add_header (response, GST_RTSP_HDR_SERVER,
365 "GStreamer RTSP server");
367 /* remove any previous header */
368 gst_rtsp_message_remove_header (response, GST_RTSP_HDR_SESSION, -1);
370 /* add the new session header for new session ids */
372 gst_rtsp_message_take_header (response, GST_RTSP_HDR_SESSION,
373 gst_rtsp_session_get_header (session));
376 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
377 gst_rtsp_message_dump (response);
381 gst_rtsp_message_add_header (response, GST_RTSP_HDR_CONNECTION, "close");
383 g_mutex_lock (&priv->send_lock);
385 priv->send_func (client, response, close, priv->send_data);
386 g_mutex_unlock (&priv->send_lock);
388 gst_rtsp_message_unset (response);
392 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
393 GstRTSPClientState * state)
395 gst_rtsp_message_init_response (state->response, code,
396 gst_rtsp_status_as_text (code), state->request);
398 send_response (client, NULL, state->response, FALSE);
402 handle_unauthorized_request (GstRTSPClient * client, GstRTSPAuth * auth,
403 GstRTSPClientState * state)
405 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_UNAUTHORIZED,
406 gst_rtsp_status_as_text (GST_RTSP_STS_UNAUTHORIZED), state->request);
409 /* and let the authentication manager setup the auth tokens */
410 gst_rtsp_auth_setup_auth (auth, client, 0, state);
413 send_response (client, state->session, state->response, FALSE);
418 compare_uri (const GstRTSPUrl * uri1, const GstRTSPUrl * uri2)
420 if (uri1 == NULL || uri2 == NULL)
423 if (strcmp (uri1->abspath, uri2->abspath))
429 /* this function is called to initially find the media for the DESCRIBE request
430 * but is cached for when the same client (without breaking the connection) is
431 * doing a setup for the exact same url. */
432 static GstRTSPMedia *
433 find_media (GstRTSPClient * client, GstRTSPClientState * state)
435 GstRTSPClientPrivate *priv = client->priv;
436 GstRTSPMediaFactory *factory;
440 if (!compare_uri (priv->uri, state->uri)) {
441 /* remove any previously cached values before we try to construct a new
444 gst_rtsp_url_free (priv->uri);
447 gst_rtsp_media_unprepare (priv->media);
448 g_object_unref (priv->media);
452 if (!priv->mount_points)
453 goto no_mount_points;
455 /* find the factory for the uri first */
457 gst_rtsp_mount_points_find_factory (priv->mount_points,
461 /* check if we have access to the factory */
462 if ((auth = gst_rtsp_media_factory_get_auth (factory))) {
463 state->factory = factory;
465 if (!gst_rtsp_auth_check (auth, client, 0, state))
468 state->factory = NULL;
469 g_object_unref (auth);
472 /* prepare the media and add it to the pipeline */
473 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
476 g_object_unref (factory);
479 /* prepare the media */
480 if (!(gst_rtsp_media_prepare (media)))
483 /* now keep track of the uri and the media */
484 priv->uri = gst_rtsp_url_copy (state->uri);
486 state->media = media;
488 /* we have seen this uri before, used cached media */
490 state->media = media;
491 GST_INFO ("reusing cached media %p", media);
495 g_object_ref (media);
502 GST_ERROR ("client %p: no mount points configured", client);
503 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
508 GST_ERROR ("client %p: no factory for uri", client);
509 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
514 GST_ERROR ("client %p: unauthorized request", client);
515 handle_unauthorized_request (client, auth, state);
516 g_object_unref (factory);
517 state->factory = NULL;
518 g_object_unref (auth);
523 GST_ERROR ("client %p: can't create media", client);
524 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
525 g_object_unref (factory);
530 GST_ERROR ("client %p: can't prepare media", client);
531 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
532 g_object_unref (media);
538 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
540 GstRTSPClientPrivate *priv = client->priv;
541 GstRTSPMessage message = { 0 };
546 gst_rtsp_message_init_data (&message, channel);
548 /* FIXME, need some sort of iovec RTSPMessage here */
549 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
552 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
554 g_mutex_lock (&priv->send_lock);
556 priv->send_func (client, &message, FALSE, priv->send_data);
557 g_mutex_unlock (&priv->send_lock);
559 gst_rtsp_message_steal_body (&message, &data, &usize);
560 gst_buffer_unmap (buffer, &map_info);
562 gst_rtsp_message_unset (&message);
568 link_transport (GstRTSPClient * client, GstRTSPSession * session,
569 GstRTSPStreamTransport * trans)
571 GstRTSPClientPrivate *priv = client->priv;
573 GST_DEBUG ("client %p: linking transport %p", client, trans);
575 gst_rtsp_stream_transport_set_callbacks (trans,
576 (GstRTSPSendFunc) do_send_data,
577 (GstRTSPSendFunc) do_send_data, client, NULL);
579 priv->transports = g_list_prepend (priv->transports, trans);
581 /* make sure our session can't expire */
582 gst_rtsp_session_prevent_expire (session);
586 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
587 GstRTSPStreamTransport * trans)
589 GstRTSPClientPrivate *priv = client->priv;
591 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
593 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
595 priv->transports = g_list_remove (priv->transports, trans);
597 /* our session can now expire */
598 gst_rtsp_session_allow_expire (session);
602 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
603 GstRTSPSessionMedia * media)
608 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (media));
609 for (i = 0; i < n_streams; i++) {
610 GstRTSPStreamTransport *trans;
611 const GstRTSPTransport *tr;
613 /* get the transport, if there is no transport configured, skip this stream */
614 trans = gst_rtsp_session_media_get_transport (media, i);
618 tr = gst_rtsp_stream_transport_get_transport (trans);
620 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
621 /* for TCP, unlink the stream from the TCP connection of the client */
622 unlink_transport (client, session, trans);
628 close_connection (GstRTSPClient * client)
630 GstRTSPClientPrivate *priv = client->priv;
631 const gchar *tunnelid;
633 GST_DEBUG ("client %p: closing connection", client);
635 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
636 g_mutex_lock (&tunnels_lock);
637 /* remove from tunnelids */
638 g_hash_table_remove (tunnels, tunnelid);
639 g_mutex_unlock (&tunnels_lock);
642 gst_rtsp_connection_close (priv->connection);
646 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
648 GstRTSPClientPrivate *priv = client->priv;
649 GstRTSPSession *session;
650 GstRTSPSessionMedia *media;
651 GstRTSPStatusCode code;
656 session = state->session;
658 /* get a handle to the configuration of the media in the session */
659 media = gst_rtsp_session_get_media (session, state->uri);
663 state->sessmedia = media;
665 /* we emit the signal before closing the connection */
666 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
669 /* unlink the all TCP callbacks */
670 unlink_session_transports (client, session, media);
672 /* remove the session from the watched sessions */
673 g_object_weak_unref (G_OBJECT (session),
674 (GWeakNotify) client_session_finalized, client);
675 priv->sessions = g_list_remove (priv->sessions, session);
677 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
679 /* unmanage the media in the session, returns false if all media session
681 if (!gst_rtsp_session_release_media (session, media)) {
682 /* remove the session */
683 gst_rtsp_session_pool_remove (priv->session_pool, session);
685 /* construct the response now */
686 code = GST_RTSP_STS_OK;
687 gst_rtsp_message_init_response (state->response, code,
688 gst_rtsp_status_as_text (code), state->request);
690 send_response (client, session, state->response, TRUE);
697 GST_ERROR ("client %p: no session", client);
698 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
703 GST_ERROR ("client %p: no media for uri", client);
704 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
710 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
716 res = gst_rtsp_message_get_body (state->request, &data, &size);
717 if (res != GST_RTSP_OK)
721 /* no body, keep-alive request */
722 send_generic_response (client, GST_RTSP_STS_OK, state);
724 /* there is a body, handle the params */
725 res = gst_rtsp_params_get (client, state);
726 if (res != GST_RTSP_OK)
729 send_response (client, state->session, state->response, FALSE);
732 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
740 GST_ERROR ("client %p: bad request", client);
741 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
747 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
753 res = gst_rtsp_message_get_body (state->request, &data, &size);
754 if (res != GST_RTSP_OK)
758 /* no body, keep-alive request */
759 send_generic_response (client, GST_RTSP_STS_OK, state);
761 /* there is a body, handle the params */
762 res = gst_rtsp_params_set (client, state);
763 if (res != GST_RTSP_OK)
766 send_response (client, state->session, state->response, FALSE);
769 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
777 GST_ERROR ("client %p: bad request", client);
778 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
784 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
786 GstRTSPSession *session;
787 GstRTSPSessionMedia *media;
788 GstRTSPStatusCode code;
789 GstRTSPState rtspstate;
791 if (!(session = state->session))
794 /* get a handle to the configuration of the media in the session */
795 media = gst_rtsp_session_get_media (session, state->uri);
799 state->sessmedia = media;
801 rtspstate = gst_rtsp_session_media_get_rtsp_state (media);
802 /* the session state must be playing or recording */
803 if (rtspstate != GST_RTSP_STATE_PLAYING &&
804 rtspstate != GST_RTSP_STATE_RECORDING)
807 /* unlink the all TCP callbacks */
808 unlink_session_transports (client, session, media);
810 /* then pause sending */
811 gst_rtsp_session_media_set_state (media, GST_STATE_PAUSED);
813 /* construct the response now */
814 code = GST_RTSP_STS_OK;
815 gst_rtsp_message_init_response (state->response, code,
816 gst_rtsp_status_as_text (code), state->request);
818 send_response (client, session, state->response, FALSE);
820 /* the state is now READY */
821 gst_rtsp_session_media_set_rtsp_state (media, GST_RTSP_STATE_READY);
823 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
831 GST_ERROR ("client %p: no seesion", client);
832 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
837 GST_ERROR ("client %p: no media for uri", client);
838 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
843 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
844 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
851 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
853 GstRTSPSession *session;
854 GstRTSPSessionMedia *media;
855 GstRTSPStatusCode code;
857 guint n_streams, i, infocount;
859 GstRTSPTimeRange *range;
861 GstRTSPState rtspstate;
862 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
864 if (!(session = state->session))
867 /* get a handle to the configuration of the media in the session */
868 media = gst_rtsp_session_get_media (session, state->uri);
872 state->sessmedia = media;
874 /* the session state must be playing or ready */
875 rtspstate = gst_rtsp_session_media_get_rtsp_state (media);
876 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
879 /* parse the range header if we have one */
881 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
882 if (res == GST_RTSP_OK) {
883 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
884 /* we have a range, seek to the position */
886 gst_rtsp_media_seek (gst_rtsp_session_media_get_media (media), range);
887 gst_rtsp_range_free (range);
891 /* grab RTPInfo from the payloaders now */
892 rtpinfo = g_string_new ("");
895 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (media));
896 for (i = 0, infocount = 0; i < n_streams; i++) {
897 GstRTSPStreamTransport *trans;
898 GstRTSPStream *stream;
899 const GstRTSPTransport *tr;
903 /* get the transport, if there is no transport configured, skip this stream */
904 trans = gst_rtsp_session_media_get_transport (media, i);
906 GST_INFO ("stream %d is not configured", i);
909 tr = gst_rtsp_stream_transport_get_transport (trans);
911 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
912 /* for TCP, link the stream to the TCP connection of the client */
913 link_transport (client, session, trans);
916 stream = gst_rtsp_stream_transport_get_stream (trans);
917 if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
919 g_string_append (rtpinfo, ", ");
921 uristr = gst_rtsp_url_get_request_uri (state->uri);
922 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
923 uristr, i, seq, rtptime);
928 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
932 /* construct the response now */
933 code = GST_RTSP_STS_OK;
934 gst_rtsp_message_init_response (state->response, code,
935 gst_rtsp_status_as_text (code), state->request);
937 /* add the RTP-Info header */
939 str = g_string_free (rtpinfo, FALSE);
940 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
942 g_string_free (rtpinfo, TRUE);
947 gst_rtsp_media_get_range_string (gst_rtsp_session_media_get_media (media),
949 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
951 send_response (client, session, state->response, FALSE);
953 /* start playing after sending the request */
954 gst_rtsp_session_media_set_state (media, GST_STATE_PLAYING);
956 gst_rtsp_session_media_set_rtsp_state (media, GST_RTSP_STATE_PLAYING);
958 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
966 GST_ERROR ("client %p: no session", client);
967 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
972 GST_ERROR ("client %p: media not found", client);
973 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
978 GST_ERROR ("client %p: not PLAYING or READY", client);
979 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
986 do_keepalive (GstRTSPSession * session)
988 GST_INFO ("keep session %p alive", session);
989 gst_rtsp_session_touch (session);
992 /* parse @transport and return a valid transport in @tr. only transports
993 * from @supported are returned. Returns FALSE if no valid transport
996 parse_transport (const char *transport, GstRTSPLowerTrans supported,
997 GstRTSPTransport * tr)
1004 gst_rtsp_transport_init (tr);
1006 GST_DEBUG ("parsing transports %s", transport);
1008 transports = g_strsplit (transport, ",", 0);
1010 /* loop through the transports, try to parse */
1011 for (i = 0; transports[i]; i++) {
1012 res = gst_rtsp_transport_parse (transports[i], tr);
1013 if (res != GST_RTSP_OK) {
1014 /* no valid transport, search some more */
1015 GST_WARNING ("could not parse transport %s", transports[i]);
1019 /* we have a transport, see if it's RTP/AVP */
1020 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1021 GST_WARNING ("invalid transport %s", transports[i]);
1025 if (!(tr->lower_transport & supported)) {
1026 GST_WARNING ("unsupported transport %s", transports[i]);
1030 /* we have a valid transport */
1031 GST_INFO ("found valid transport %s", transports[i]);
1036 gst_rtsp_transport_init (tr);
1038 g_strfreev (transports);
1044 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
1045 GstRTSPMessage * request)
1047 gchar *blocksize_str;
1048 gboolean ret = TRUE;
1050 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1051 &blocksize_str, 0) == GST_RTSP_OK) {
1055 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1056 if (end == blocksize_str) {
1057 GST_ERROR ("failed to parse blocksize");
1060 /* we don't want to change the mtu when this media
1061 * can be shared because it impacts other clients */
1062 if (gst_rtsp_media_is_shared (media))
1065 if (blocksize > G_MAXUINT)
1066 blocksize = G_MAXUINT;
1067 gst_rtsp_stream_set_mtu (stream, blocksize);
1074 configure_client_transport (GstRTSPClient * client, GstRTSPClientState * state,
1075 GstRTSPTransport * ct)
1077 GstRTSPClientPrivate *priv = client->priv;
1079 /* we have a valid transport now, set the destination of the client. */
1080 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1081 if (ct->destination && priv->use_client_settings) {
1082 GstRTSPAddress *addr;
1084 addr = gst_rtsp_stream_reserve_address (state->stream, ct->destination,
1085 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1090 gst_rtsp_address_free (addr);
1092 GstRTSPAddress *addr;
1094 addr = gst_rtsp_stream_get_address (state->stream);
1098 g_free (ct->destination);
1099 ct->destination = g_strdup (addr->address);
1100 ct->port.min = addr->port;
1101 ct->port.max = addr->port + addr->n_ports - 1;
1102 ct->ttl = addr->ttl;
1104 gst_rtsp_address_free (addr);
1109 url = gst_rtsp_connection_get_url (priv->connection);
1110 g_free (ct->destination);
1111 ct->destination = g_strdup (url->host);
1113 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1114 /* check if the client selected channels for TCP */
1115 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1116 gst_rtsp_session_media_alloc_channels (state->sessmedia,
1126 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1131 static GstRTSPTransport *
1132 make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
1133 GstRTSPTransport * ct)
1135 GstRTSPTransport *st;
1137 GSocketFamily family;
1139 /* prepare the server transport */
1140 gst_rtsp_transport_new (&st);
1142 st->trans = ct->trans;
1143 st->profile = ct->profile;
1144 st->lower_transport = ct->lower_transport;
1146 addr = g_inet_address_new_from_string (ct->destination);
1149 GST_ERROR ("failed to get inet addr from client destination");
1150 family = G_SOCKET_FAMILY_IPV4;
1152 family = g_inet_address_get_family (addr);
1153 g_object_unref (addr);
1157 switch (st->lower_transport) {
1158 case GST_RTSP_LOWER_TRANS_UDP:
1159 st->client_port = ct->client_port;
1160 gst_rtsp_stream_get_server_port (state->stream, &st->server_port, family);
1162 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1163 st->port = ct->port;
1164 st->destination = g_strdup (ct->destination);
1167 case GST_RTSP_LOWER_TRANS_TCP:
1168 st->interleaved = ct->interleaved;
1173 gst_rtsp_stream_get_ssrc (state->stream, &st->ssrc);
1179 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
1181 GstRTSPClientPrivate *priv = client->priv;
1185 GstRTSPTransport *ct, *st;
1186 GstRTSPLowerTrans supported;
1187 GstRTSPStatusCode code;
1188 GstRTSPSession *session;
1189 GstRTSPStreamTransport *trans;
1190 gchar *trans_str, *pos;
1192 GstRTSPSessionMedia *sessmedia;
1193 GstRTSPMedia *media;
1194 GstRTSPStream *stream;
1195 GstRTSPState rtspstate;
1199 /* the uri contains the stream number we added in the SDP config, which is
1200 * always /stream=%d so we need to strip that off
1201 * parse the stream we need to configure, look for the stream in the abspath
1202 * first and then in the query. */
1203 if (uri->abspath == NULL || !(pos = strstr (uri->abspath, "/stream="))) {
1204 if (uri->query == NULL || !(pos = strstr (uri->query, "/stream=")))
1208 /* we can mofify the parsed uri in place */
1211 pos += strlen ("/stream=");
1212 if (sscanf (pos, "%u", &streamid) != 1)
1215 /* parse the transport */
1217 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
1219 if (res != GST_RTSP_OK)
1222 gst_rtsp_transport_new (&ct);
1224 /* our supported transports */
1225 supported = GST_RTSP_LOWER_TRANS_UDP |
1226 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
1228 /* parse and find a usable supported transport */
1229 if (!parse_transport (transport, supported, ct))
1230 goto unsupported_transports;
1232 /* we create the session after parsing stuff so that we don't make
1233 * a session for malformed requests */
1234 if (priv->session_pool == NULL)
1237 session = state->session;
1240 g_object_ref (session);
1241 /* get a handle to the configuration of the media in the session, this can
1242 * return NULL if this is a new url to manage in this session. */
1243 sessmedia = gst_rtsp_session_get_media (session, uri);
1245 /* create a session if this fails we probably reached our session limit or
1247 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1248 goto service_unavailable;
1250 state->session = session;
1252 /* we need a new media configuration in this session */
1256 /* we have no media, find one and manage it */
1257 if (sessmedia == NULL) {
1258 /* get a handle to the configuration of the media in the session */
1259 if ((media = find_media (client, state))) {
1260 /* manage the media in our session now */
1261 sessmedia = gst_rtsp_session_manage_media (session, uri, media);
1265 /* if we stil have no media, error */
1266 if (sessmedia == NULL)
1269 state->sessmedia = sessmedia;
1270 state->media = media = gst_rtsp_session_media_get_media (sessmedia);
1272 /* now get the stream */
1273 stream = gst_rtsp_media_get_stream (media, streamid);
1277 state->stream = stream;
1279 /* set blocksize on this stream */
1280 if (!handle_blocksize (media, stream, state->request))
1281 goto invalid_blocksize;
1283 /* update the client transport */
1284 if (!configure_client_transport (client, state, ct))
1285 goto unsupported_client_transport;
1287 /* set in the session media transport */
1288 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1290 /* configure keepalive for this transport */
1291 gst_rtsp_stream_transport_set_keepalive (trans,
1292 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1294 /* create and serialize the server transport */
1295 st = make_server_transport (client, state, ct);
1296 trans_str = gst_rtsp_transport_as_text (st);
1297 gst_rtsp_transport_free (st);
1299 /* construct the response now */
1300 code = GST_RTSP_STS_OK;
1301 gst_rtsp_message_init_response (state->response, code,
1302 gst_rtsp_status_as_text (code), state->request);
1304 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1308 send_response (client, session, state->response, FALSE);
1310 /* update the state */
1311 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1312 switch (rtspstate) {
1313 case GST_RTSP_STATE_PLAYING:
1314 case GST_RTSP_STATE_RECORDING:
1315 case GST_RTSP_STATE_READY:
1316 /* no state change */
1319 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1322 g_object_unref (session);
1324 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST],
1332 GST_ERROR ("client %p: bad request", client);
1333 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1338 GST_ERROR ("client %p: media not found", client);
1339 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1340 g_object_unref (session);
1341 gst_rtsp_transport_free (ct);
1346 GST_ERROR ("client %p: invalid blocksize", client);
1347 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1348 g_object_unref (session);
1349 gst_rtsp_transport_free (ct);
1352 unsupported_client_transport:
1354 GST_ERROR ("client %p: unsupported client transport", client);
1355 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1356 g_object_unref (session);
1357 gst_rtsp_transport_free (ct);
1362 GST_ERROR ("client %p: no transport", client);
1363 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1366 unsupported_transports:
1368 GST_ERROR ("client %p: unsupported transports", client);
1369 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1370 gst_rtsp_transport_free (ct);
1375 GST_ERROR ("client %p: no session pool configured", client);
1376 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1377 gst_rtsp_transport_free (ct);
1380 service_unavailable:
1382 GST_ERROR ("client %p: can't create session", client);
1383 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1384 gst_rtsp_transport_free (ct);
1389 static GstSDPMessage *
1390 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1392 GstRTSPClientPrivate *priv = client->priv;
1397 gst_sdp_message_new (&sdp);
1399 /* some standard things first */
1400 gst_sdp_message_set_version (sdp, "0");
1407 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1410 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1411 gst_sdp_message_set_information (sdp, "rtsp-server");
1412 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1413 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1414 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1415 gst_sdp_message_add_attribute (sdp, "control", "*");
1417 info.is_ipv6 = priv->is_ipv6;
1418 info.server_ip = priv->server_ip;
1420 /* create an SDP for the media object */
1421 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1429 GST_ERROR ("client %p: could not create SDP", client);
1430 gst_sdp_message_free (sdp);
1435 /* for the describe we must generate an SDP */
1437 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1442 gchar *str, *content_base;
1443 GstRTSPMedia *media;
1444 GstRTSPClientClass *klass;
1446 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1448 /* check what kind of format is accepted, we don't really do anything with it
1449 * and always return SDP for now. */
1454 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1456 if (res == GST_RTSP_ENOTIMPL)
1459 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1463 /* find the media object for the uri */
1464 if (!(media = find_media (client, state)))
1467 /* create an SDP for the media object on this client */
1468 if (!(sdp = klass->create_sdp (client, media)))
1471 g_object_unref (media);
1473 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1474 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1476 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1479 /* content base for some clients that might screw up creating the setup uri */
1480 str = gst_rtsp_url_get_request_uri (state->uri);
1481 str_len = strlen (str);
1483 /* check for trailing '/' and append one */
1484 if (str[str_len - 1] != '/') {
1485 content_base = g_malloc (str_len + 2);
1486 memcpy (content_base, str, str_len);
1487 content_base[str_len] = '/';
1488 content_base[str_len + 1] = '\0';
1494 GST_INFO ("adding content-base: %s", content_base);
1496 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1498 g_free (content_base);
1500 /* add SDP to the response body */
1501 str = gst_sdp_message_as_text (sdp);
1502 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1503 gst_sdp_message_free (sdp);
1505 send_response (client, state->session, state->response, FALSE);
1507 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1515 GST_ERROR ("client %p: no media", client);
1516 /* error reply is already sent */
1521 GST_ERROR ("client %p: can't create SDP", client);
1522 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1523 g_object_unref (media);
1529 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1531 GstRTSPMethod options;
1534 options = GST_RTSP_DESCRIBE |
1539 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1541 str = gst_rtsp_options_as_text (options);
1543 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1544 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1546 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1549 send_response (client, state->session, state->response, FALSE);
1551 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1557 /* remove duplicate and trailing '/' */
1559 sanitize_uri (GstRTSPUrl * uri)
1563 gboolean have_slash, prev_slash;
1565 s = d = uri->abspath;
1566 len = strlen (uri->abspath);
1570 for (i = 0; i < len; i++) {
1571 have_slash = s[i] == '/';
1573 if (!have_slash || !prev_slash)
1575 prev_slash = have_slash;
1577 len = d - uri->abspath;
1578 /* don't remove the first slash if that's the only thing left */
1579 if (len > 1 && *(d - 1) == '/')
1585 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1587 GstRTSPClientPrivate *priv = client->priv;
1589 GST_INFO ("client %p: session %p finished", client, session);
1591 /* unlink all media managed in this session */
1592 client_unlink_session (client, session);
1594 /* remove the session */
1595 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1596 GST_INFO ("client %p: all sessions finalized, close the connection",
1598 close_connection (client);
1603 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
1605 GstRTSPClientPrivate *priv = client->priv;
1608 for (walk = priv->sessions; walk; walk = g_list_next (walk)) {
1609 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
1611 /* we already know about this session */
1612 if (msession == session)
1616 GST_INFO ("watching session %p", session);
1618 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
1620 priv->sessions = g_list_prepend (priv->sessions, session);
1622 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1627 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1629 GstRTSPClientPrivate *priv = client->priv;
1630 GstRTSPMethod method;
1631 const gchar *uristr;
1632 GstRTSPUrl *uri = NULL;
1633 GstRTSPVersion version;
1635 GstRTSPSession *session = NULL;
1636 GstRTSPClientState state = { NULL };
1637 GstRTSPMessage response = { 0 };
1640 state.request = request;
1641 state.response = &response;
1643 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1644 gst_rtsp_message_dump (request);
1647 GST_INFO ("client %p: received a request", client);
1649 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1651 /* we can only handle 1.0 requests */
1652 if (version != GST_RTSP_VERSION_1_0)
1655 state.method = method;
1657 /* we always try to parse the url first */
1658 if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1661 /* get the session if there is any */
1662 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1663 if (res == GST_RTSP_OK) {
1664 if (priv->session_pool == NULL)
1667 /* we had a session in the request, find it again */
1668 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1669 goto session_not_found;
1671 /* we add the session to the client list of watched sessions. When a session
1672 * disappears because it times out, we will be notified. If all sessions are
1673 * gone, we will close the connection */
1674 client_watch_session (client, session);
1677 /* sanitize the uri */
1680 state.session = session;
1683 if (!gst_rtsp_auth_check (priv->auth, client, 0, &state))
1684 goto not_authorized;
1687 /* now see what is asked and dispatch to a dedicated handler */
1689 case GST_RTSP_OPTIONS:
1690 handle_options_request (client, &state);
1692 case GST_RTSP_DESCRIBE:
1693 handle_describe_request (client, &state);
1695 case GST_RTSP_SETUP:
1696 handle_setup_request (client, &state);
1699 handle_play_request (client, &state);
1701 case GST_RTSP_PAUSE:
1702 handle_pause_request (client, &state);
1704 case GST_RTSP_TEARDOWN:
1705 handle_teardown_request (client, &state);
1707 case GST_RTSP_SET_PARAMETER:
1708 handle_set_param_request (client, &state);
1710 case GST_RTSP_GET_PARAMETER:
1711 handle_get_param_request (client, &state);
1713 case GST_RTSP_ANNOUNCE:
1714 case GST_RTSP_RECORD:
1715 case GST_RTSP_REDIRECT:
1716 goto not_implemented;
1717 case GST_RTSP_INVALID:
1724 g_object_unref (session);
1726 gst_rtsp_url_free (uri);
1732 GST_ERROR ("client %p: version %d not supported", client, version);
1733 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1739 GST_ERROR ("client %p: bad request", client);
1740 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1745 GST_ERROR ("client %p: no pool configured", client);
1746 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1751 GST_ERROR ("client %p: session not found", client);
1752 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1757 GST_ERROR ("client %p: not allowed", client);
1758 handle_unauthorized_request (client, priv->auth, &state);
1763 GST_ERROR ("client %p: method %d not implemented", client, method);
1764 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1770 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1772 GstRTSPClientPrivate *priv = client->priv;
1781 /* find the stream for this message */
1782 res = gst_rtsp_message_parse_data (message, &channel);
1783 if (res != GST_RTSP_OK)
1786 gst_rtsp_message_steal_body (message, &data, &size);
1788 buffer = gst_buffer_new_wrapped (data, size);
1791 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1792 GstRTSPStreamTransport *trans;
1793 GstRTSPStream *stream;
1794 const GstRTSPTransport *tr;
1798 tr = gst_rtsp_stream_transport_get_transport (trans);
1799 stream = gst_rtsp_stream_transport_get_stream (trans);
1801 /* check for TCP transport */
1802 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1803 /* dispatch to the stream based on the channel number */
1804 if (tr->interleaved.min == channel) {
1805 gst_rtsp_stream_recv_rtp (stream, buffer);
1808 } else if (tr->interleaved.max == channel) {
1809 gst_rtsp_stream_recv_rtcp (stream, buffer);
1816 gst_buffer_unref (buffer);
1820 * gst_rtsp_client_set_session_pool:
1821 * @client: a #GstRTSPClient
1822 * @pool: a #GstRTSPSessionPool
1824 * Set @pool as the sessionpool for @client which it will use to find
1825 * or allocate sessions. the sessionpool is usually inherited from the server
1826 * that created the client but can be overridden later.
1829 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
1830 GstRTSPSessionPool * pool)
1832 GstRTSPSessionPool *old;
1833 GstRTSPClientPrivate *priv;
1835 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1837 priv = client->priv;
1840 g_object_ref (pool);
1842 g_mutex_lock (&priv->lock);
1843 old = priv->session_pool;
1844 priv->session_pool = pool;
1845 g_mutex_unlock (&priv->lock);
1848 g_object_unref (old);
1852 * gst_rtsp_client_get_session_pool:
1853 * @client: a #GstRTSPClient
1855 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
1857 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
1859 GstRTSPSessionPool *
1860 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
1862 GstRTSPClientPrivate *priv;
1863 GstRTSPSessionPool *result;
1865 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1867 priv = client->priv;
1869 g_mutex_lock (&priv->lock);
1870 if ((result = priv->session_pool))
1871 g_object_ref (result);
1872 g_mutex_unlock (&priv->lock);
1878 * gst_rtsp_client_set_mount_points:
1879 * @client: a #GstRTSPClient
1880 * @mounts: a #GstRTSPMountPoints
1882 * Set @mounts as the mount points for @client which it will use to map urls
1883 * to media streams. These mount points are usually inherited from the server that
1884 * created the client but can be overriden later.
1887 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
1888 GstRTSPMountPoints * mounts)
1890 GstRTSPClientPrivate *priv;
1891 GstRTSPMountPoints *old;
1893 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1895 priv = client->priv;
1898 g_object_ref (mounts);
1900 g_mutex_lock (&priv->lock);
1901 old = priv->mount_points;
1902 priv->mount_points = mounts;
1903 g_mutex_unlock (&priv->lock);
1906 g_object_unref (old);
1910 * gst_rtsp_client_get_mount_points:
1911 * @client: a #GstRTSPClient
1913 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
1915 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
1917 GstRTSPMountPoints *
1918 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
1920 GstRTSPClientPrivate *priv;
1921 GstRTSPMountPoints *result;
1923 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1925 priv = client->priv;
1927 g_mutex_lock (&priv->lock);
1928 if ((result = priv->mount_points))
1929 g_object_ref (result);
1930 g_mutex_unlock (&priv->lock);
1936 * gst_rtsp_client_set_use_client_settings:
1937 * @client: a #GstRTSPClient
1938 * @use_client_settings: whether to use client settings for multicast
1940 * Use client transport settings (destination and ttl) for multicast.
1941 * When @use_client_settings is %FALSE, the server settings will be
1945 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
1946 gboolean use_client_settings)
1948 GstRTSPClientPrivate *priv;
1950 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1952 priv = client->priv;
1954 g_mutex_lock (&priv->lock);
1955 priv->use_client_settings = use_client_settings;
1956 g_mutex_unlock (&priv->lock);
1960 * gst_rtsp_client_get_use_client_settings:
1961 * @client: a #GstRTSPClient
1963 * Check if client transport settings (destination and ttl) for multicast
1967 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
1969 GstRTSPClientPrivate *priv;
1972 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
1974 priv = client->priv;
1976 g_mutex_lock (&priv->lock);
1977 res = priv->use_client_settings;
1978 g_mutex_unlock (&priv->lock);
1984 * gst_rtsp_client_set_auth:
1985 * @client: a #GstRTSPClient
1986 * @auth: a #GstRTSPAuth
1988 * configure @auth to be used as the authentication manager of @client.
1991 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
1993 GstRTSPClientPrivate *priv;
1996 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1998 priv = client->priv;
2001 g_object_ref (auth);
2003 g_mutex_lock (&priv->lock);
2006 g_mutex_unlock (&priv->lock);
2009 g_object_unref (old);
2014 * gst_rtsp_client_get_auth:
2015 * @client: a #GstRTSPClient
2017 * Get the #GstRTSPAuth used as the authentication manager of @client.
2019 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2023 gst_rtsp_client_get_auth (GstRTSPClient * client)
2025 GstRTSPClientPrivate *priv;
2026 GstRTSPAuth *result;
2028 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2030 priv = client->priv;
2032 g_mutex_lock (&priv->lock);
2033 if ((result = priv->auth))
2034 g_object_ref (result);
2035 g_mutex_unlock (&priv->lock);
2041 * gst_rtsp_client_get_uri:
2042 * @client: a #GstRTSPClient
2044 * Get the #GstRTSPUrl of @client.
2046 * Returns: (transfer full): the #GstRTSPUrl of @client. Free with
2047 * gst_rtsp_url_free () after usage.
2050 gst_rtsp_client_get_uri (GstRTSPClient * client)
2052 GstRTSPClientPrivate *priv;
2053 GstRTSPUrl *result = NULL;
2055 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2057 priv = client->priv;
2059 g_mutex_lock (&priv->lock);
2060 if (priv->uri != NULL)
2061 result = gst_rtsp_url_copy (priv->uri);
2062 g_mutex_unlock (&priv->lock);
2068 * gst_rtsp_client_set_connection:
2069 * @client: a #GstRTSPClient
2070 * @conn: (transfer full): a #GstRTSPConnection
2072 * Set the #GstRTSPConnection of @client. This function takes ownership of
2075 * Returns: %TRUE on success.
2078 gst_rtsp_client_set_connection (GstRTSPClient * client,
2079 GstRTSPConnection * conn)
2081 GstRTSPClientPrivate *priv;
2082 GSocket *read_socket;
2083 GSocketAddress *address;
2085 GError *error = NULL;
2087 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2088 g_return_val_if_fail (conn != NULL, FALSE);
2090 priv = client->priv;
2092 read_socket = gst_rtsp_connection_get_read_socket (conn);
2094 if (!(address = g_socket_get_local_address (read_socket, &error)))
2097 g_free (priv->server_ip);
2098 /* keep the original ip that the client connected to */
2099 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2100 GInetAddress *iaddr;
2102 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2104 /* socket might be ipv6 but adress still ipv4 */
2105 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2106 priv->server_ip = g_inet_address_to_string (iaddr);
2107 g_object_unref (address);
2109 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2110 priv->server_ip = g_strdup ("unknown");
2113 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2114 priv->server_ip, priv->is_ipv6);
2116 url = gst_rtsp_connection_get_url (conn);
2117 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2119 priv->connection = conn;
2126 GST_ERROR ("could not get remote address %s", error->message);
2127 g_error_free (error);
2133 * gst_rtsp_client_get_connection:
2134 * @client: a #GstRTSPClient
2136 * Get the #GstRTSPConnection of @client.
2138 * Returns: (transfer none): the #GstRTSPConnection of @client.
2139 * The connection object returned remains valid until the client is freed.
2142 gst_rtsp_client_get_connection (GstRTSPClient * client)
2144 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2146 return client->priv->connection;
2150 * gst_rtsp_client_set_send_func:
2151 * @client: a #GstRTSPClient
2152 * @func: a #GstRTSPClientSendFunc
2153 * @user_data: user data passed to @func
2154 * @notify: called when @user_data is no longer in use
2156 * Set @func as the callback that will be called when a new message needs to be
2157 * sent to the client. @user_data is passed to @func and @notify is called when
2158 * @user_data is no longer in use.
2161 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2162 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2164 GstRTSPClientPrivate *priv;
2165 GDestroyNotify old_notify;
2168 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2170 priv = client->priv;
2172 g_mutex_lock (&priv->send_lock);
2173 priv->send_func = func;
2174 old_notify = priv->send_notify;
2175 old_data = priv->send_data;
2176 priv->send_notify = notify;
2177 priv->send_data = user_data;
2178 g_mutex_unlock (&priv->send_lock);
2181 old_notify (old_data);
2185 * gst_rtsp_client_handle_message:
2186 * @client: a #GstRTSPClient
2187 * @message: an #GstRTSPMessage
2189 * Let the client handle @message.
2191 * Returns: a #GstRTSPResult.
2194 gst_rtsp_client_handle_message (GstRTSPClient * client,
2195 GstRTSPMessage * message)
2197 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2198 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2200 switch (message->type) {
2201 case GST_RTSP_MESSAGE_REQUEST:
2202 handle_request (client, message);
2204 case GST_RTSP_MESSAGE_RESPONSE:
2206 case GST_RTSP_MESSAGE_DATA:
2207 handle_data (client, message);
2215 static GstRTSPResult
2216 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2217 gboolean close, gpointer user_data)
2219 GstRTSPClientPrivate *priv = client->priv;
2221 /* send the response and store the seq number so we can wait until it's
2222 * written to the client to close the connection */
2223 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2224 &priv->close_seq : NULL);
2227 static GstRTSPResult
2228 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2231 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2234 static GstRTSPResult
2235 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2237 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2238 GstRTSPClientPrivate *priv = client->priv;
2240 if (priv->close_seq && priv->close_seq == cseq) {
2241 priv->close_seq = 0;
2242 close_connection (client);
2248 static GstRTSPResult
2249 closed (GstRTSPWatch * watch, gpointer user_data)
2251 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2252 GstRTSPClientPrivate *priv = client->priv;
2253 const gchar *tunnelid;
2255 GST_INFO ("client %p: connection closed", client);
2257 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2258 g_mutex_lock (&tunnels_lock);
2259 /* remove from tunnelids */
2260 g_hash_table_remove (tunnels, tunnelid);
2261 g_mutex_unlock (&tunnels_lock);
2264 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2269 static GstRTSPResult
2270 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2272 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2275 str = gst_rtsp_strresult (result);
2276 GST_INFO ("client %p: received an error %s", client, str);
2282 static GstRTSPResult
2283 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2284 GstRTSPMessage * message, guint id, gpointer user_data)
2286 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2289 str = gst_rtsp_strresult (result);
2291 ("client %p: error when handling message %p with id %d: %s",
2292 client, message, id, str);
2299 remember_tunnel (GstRTSPClient * client)
2301 GstRTSPClientPrivate *priv = client->priv;
2302 const gchar *tunnelid;
2304 /* store client in the pending tunnels */
2305 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2306 if (tunnelid == NULL)
2309 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2311 /* we can't have two clients connecting with the same tunnelid */
2312 g_mutex_lock (&tunnels_lock);
2313 if (g_hash_table_lookup (tunnels, tunnelid))
2314 goto tunnel_existed;
2316 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2317 g_mutex_unlock (&tunnels_lock);
2324 GST_ERROR ("client %p: no tunnelid provided", client);
2329 g_mutex_unlock (&tunnels_lock);
2330 GST_ERROR ("client %p: tunnel session %s already existed", client,
2336 static GstRTSPStatusCode
2337 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2339 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2340 GstRTSPClientPrivate *priv = client->priv;
2342 GST_INFO ("client %p: tunnel start (connection %p)", client,
2345 if (!remember_tunnel (client))
2348 return GST_RTSP_STS_OK;
2353 GST_ERROR ("client %p: error starting tunnel", client);
2354 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2358 static GstRTSPResult
2359 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2361 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2362 GstRTSPClientPrivate *priv = client->priv;
2364 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2367 /* ignore error, it'll only be a problem when the client does a POST again */
2368 remember_tunnel (client);
2373 static GstRTSPResult
2374 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2376 const gchar *tunnelid;
2377 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2378 GstRTSPClientPrivate *priv = client->priv;
2379 GstRTSPClient *oclient;
2380 GstRTSPClientPrivate *opriv;
2382 GST_INFO ("client %p: tunnel complete", client);
2384 /* find previous tunnel */
2385 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2386 if (tunnelid == NULL)
2389 g_mutex_lock (&tunnels_lock);
2390 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2393 /* remove the old client from the table. ref before because removing it will
2394 * remove the ref to it. */
2395 g_object_ref (oclient);
2396 g_hash_table_remove (tunnels, tunnelid);
2398 opriv = oclient->priv;
2400 if (opriv->watch == NULL)
2402 g_mutex_unlock (&tunnels_lock);
2404 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2405 opriv->connection, priv->connection);
2407 /* merge the tunnels into the first client */
2408 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2409 gst_rtsp_watch_reset (opriv->watch);
2410 g_object_unref (oclient);
2417 GST_ERROR ("client %p: no tunnelid provided", client);
2418 return GST_RTSP_ERROR;
2422 g_mutex_unlock (&tunnels_lock);
2423 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2424 return GST_RTSP_ERROR;
2428 g_mutex_unlock (&tunnels_lock);
2429 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2430 g_object_unref (oclient);
2431 return GST_RTSP_ERROR;
2435 static GstRTSPWatchFuncs watch_funcs = {
2447 client_watch_notify (GstRTSPClient * client)
2449 GstRTSPClientPrivate *priv = client->priv;
2451 GST_INFO ("client %p: watch destroyed", client);
2453 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2454 g_object_unref (client);
2458 * gst_rtsp_client_attach:
2459 * @client: a #GstRTSPClient
2460 * @context: (allow-none): a #GMainContext
2462 * Attaches @client to @context. When the mainloop for @context is run, the
2463 * client will be dispatched. When @context is NULL, the default context will be
2466 * This function should be called when the client properties and urls are fully
2467 * configured and the client is ready to start.
2469 * Returns: the ID (greater than 0) for the source within the GMainContext.
2472 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2474 GstRTSPClientPrivate *priv;
2477 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2478 priv = client->priv;
2479 g_return_val_if_fail (priv->watch == NULL, 0);
2481 /* create watch for the connection and attach */
2482 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2483 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2484 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2485 (GDestroyNotify) gst_rtsp_watch_unref);
2487 /* FIXME make this configurable. We don't want to do this yet because it will
2488 * be superceeded by a cache object later */
2489 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2491 GST_INFO ("attaching to context %p", context);
2492 res = gst_rtsp_watch_attach (priv->watch, context);