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_cleanup_sessions (GstRTSPClient * client)
253 GstRTSPClientPrivate *priv = client->priv;
256 /* remove weak-ref from sessions */
257 for (sessions = priv->sessions; sessions; sessions = g_list_next (sessions)) {
258 GstRTSPSession *session = (GstRTSPSession *) sessions->data;
259 g_object_weak_unref (G_OBJECT (session),
260 (GWeakNotify) client_session_finalized, client);
261 client_unlink_session (client, session);
263 g_list_free (priv->sessions);
264 priv->sessions = NULL;
267 /* A client is finalized when the connection is broken */
269 gst_rtsp_client_finalize (GObject * obj)
271 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
272 GstRTSPClientPrivate *priv = client->priv;
274 GST_INFO ("finalize client %p", client);
276 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
279 g_source_destroy ((GSource *) priv->watch);
281 client_cleanup_sessions (client);
283 if (priv->connection)
284 gst_rtsp_connection_free (priv->connection);
285 if (priv->session_pool)
286 g_object_unref (priv->session_pool);
287 if (priv->mount_points)
288 g_object_unref (priv->mount_points);
290 g_object_unref (priv->auth);
293 gst_rtsp_url_free (priv->uri);
295 gst_rtsp_media_unprepare (priv->media);
296 g_object_unref (priv->media);
299 g_free (priv->server_ip);
300 g_mutex_clear (&priv->lock);
301 g_mutex_clear (&priv->send_lock);
303 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
307 gst_rtsp_client_get_property (GObject * object, guint propid,
308 GValue * value, GParamSpec * pspec)
310 GstRTSPClient *client = GST_RTSP_CLIENT (object);
313 case PROP_SESSION_POOL:
314 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
316 case PROP_MOUNT_POINTS:
317 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
319 case PROP_USE_CLIENT_SETTINGS:
320 g_value_set_boolean (value,
321 gst_rtsp_client_get_use_client_settings (client));
324 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
329 gst_rtsp_client_set_property (GObject * object, guint propid,
330 const GValue * value, GParamSpec * pspec)
332 GstRTSPClient *client = GST_RTSP_CLIENT (object);
335 case PROP_SESSION_POOL:
336 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
338 case PROP_MOUNT_POINTS:
339 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
341 case PROP_USE_CLIENT_SETTINGS:
342 gst_rtsp_client_set_use_client_settings (client,
343 g_value_get_boolean (value));
346 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
351 * gst_rtsp_client_new:
353 * Create a new #GstRTSPClient instance.
355 * Returns: a new #GstRTSPClient
358 gst_rtsp_client_new (void)
360 GstRTSPClient *result;
362 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
368 send_response (GstRTSPClient * client, GstRTSPSession * session,
369 GstRTSPMessage * response, gboolean close)
371 GstRTSPClientPrivate *priv = client->priv;
373 gst_rtsp_message_add_header (response, GST_RTSP_HDR_SERVER,
374 "GStreamer RTSP server");
376 /* remove any previous header */
377 gst_rtsp_message_remove_header (response, GST_RTSP_HDR_SESSION, -1);
379 /* add the new session header for new session ids */
381 gst_rtsp_message_take_header (response, GST_RTSP_HDR_SESSION,
382 gst_rtsp_session_get_header (session));
385 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
386 gst_rtsp_message_dump (response);
390 gst_rtsp_message_add_header (response, GST_RTSP_HDR_CONNECTION, "close");
392 g_mutex_lock (&priv->send_lock);
394 priv->send_func (client, response, close, priv->send_data);
395 g_mutex_unlock (&priv->send_lock);
397 gst_rtsp_message_unset (response);
401 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
402 GstRTSPClientState * state)
404 gst_rtsp_message_init_response (state->response, code,
405 gst_rtsp_status_as_text (code), state->request);
407 send_response (client, NULL, state->response, FALSE);
411 handle_unauthorized_request (GstRTSPClient * client, GstRTSPAuth * auth,
412 GstRTSPClientState * state)
414 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_UNAUTHORIZED,
415 gst_rtsp_status_as_text (GST_RTSP_STS_UNAUTHORIZED), state->request);
418 /* and let the authentication manager setup the auth tokens */
419 gst_rtsp_auth_setup_auth (auth, client, 0, state);
422 send_response (client, state->session, state->response, FALSE);
427 compare_uri (const GstRTSPUrl * uri1, const GstRTSPUrl * uri2)
429 if (uri1 == NULL || uri2 == NULL)
432 if (strcmp (uri1->abspath, uri2->abspath))
438 /* this function is called to initially find the media for the DESCRIBE request
439 * but is cached for when the same client (without breaking the connection) is
440 * doing a setup for the exact same url. */
441 static GstRTSPMedia *
442 find_media (GstRTSPClient * client, GstRTSPClientState * state)
444 GstRTSPClientPrivate *priv = client->priv;
445 GstRTSPMediaFactory *factory;
449 if (!compare_uri (priv->uri, state->uri)) {
450 /* remove any previously cached values before we try to construct a new
453 gst_rtsp_url_free (priv->uri);
456 gst_rtsp_media_unprepare (priv->media);
457 g_object_unref (priv->media);
461 if (!priv->mount_points)
462 goto no_mount_points;
464 /* find the factory for the uri first */
466 gst_rtsp_mount_points_find_factory (priv->mount_points,
470 /* check if we have access to the factory */
471 if ((auth = gst_rtsp_media_factory_get_auth (factory))) {
472 state->factory = factory;
474 if (!gst_rtsp_auth_check (auth, client, 0, state))
477 state->factory = NULL;
478 g_object_unref (auth);
481 /* prepare the media and add it to the pipeline */
482 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
485 g_object_unref (factory);
488 /* prepare the media */
489 if (!(gst_rtsp_media_prepare (media)))
492 /* now keep track of the uri and the media */
493 priv->uri = gst_rtsp_url_copy (state->uri);
495 state->media = media;
497 /* we have seen this uri before, used cached media */
499 state->media = media;
500 GST_INFO ("reusing cached media %p", media);
504 g_object_ref (media);
511 GST_ERROR ("client %p: no mount points configured", client);
512 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
517 GST_ERROR ("client %p: no factory for uri", client);
518 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
523 GST_ERROR ("client %p: unauthorized request", client);
524 handle_unauthorized_request (client, auth, state);
525 g_object_unref (factory);
526 state->factory = NULL;
527 g_object_unref (auth);
532 GST_ERROR ("client %p: can't create media", client);
533 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
534 g_object_unref (factory);
539 GST_ERROR ("client %p: can't prepare media", client);
540 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
541 g_object_unref (media);
547 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
549 GstRTSPClientPrivate *priv = client->priv;
550 GstRTSPMessage message = { 0 };
555 gst_rtsp_message_init_data (&message, channel);
557 /* FIXME, need some sort of iovec RTSPMessage here */
558 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
561 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
563 g_mutex_lock (&priv->send_lock);
565 priv->send_func (client, &message, FALSE, priv->send_data);
566 g_mutex_unlock (&priv->send_lock);
568 gst_rtsp_message_steal_body (&message, &data, &usize);
569 gst_buffer_unmap (buffer, &map_info);
571 gst_rtsp_message_unset (&message);
577 link_transport (GstRTSPClient * client, GstRTSPSession * session,
578 GstRTSPStreamTransport * trans)
580 GstRTSPClientPrivate *priv = client->priv;
582 GST_DEBUG ("client %p: linking transport %p", client, trans);
584 gst_rtsp_stream_transport_set_callbacks (trans,
585 (GstRTSPSendFunc) do_send_data,
586 (GstRTSPSendFunc) do_send_data, client, NULL);
588 priv->transports = g_list_prepend (priv->transports, trans);
590 /* make sure our session can't expire */
591 gst_rtsp_session_prevent_expire (session);
595 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
596 GstRTSPStreamTransport * trans)
598 GstRTSPClientPrivate *priv = client->priv;
600 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
602 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
604 priv->transports = g_list_remove (priv->transports, trans);
606 /* our session can now expire */
607 gst_rtsp_session_allow_expire (session);
611 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
612 GstRTSPSessionMedia * media)
617 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (media));
618 for (i = 0; i < n_streams; i++) {
619 GstRTSPStreamTransport *trans;
620 const GstRTSPTransport *tr;
622 /* get the transport, if there is no transport configured, skip this stream */
623 trans = gst_rtsp_session_media_get_transport (media, i);
627 tr = gst_rtsp_stream_transport_get_transport (trans);
629 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
630 /* for TCP, unlink the stream from the TCP connection of the client */
631 unlink_transport (client, session, trans);
637 close_connection (GstRTSPClient * client)
639 GstRTSPClientPrivate *priv = client->priv;
640 const gchar *tunnelid;
642 GST_DEBUG ("client %p: closing connection", client);
644 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
645 g_mutex_lock (&tunnels_lock);
646 /* remove from tunnelids */
647 g_hash_table_remove (tunnels, tunnelid);
648 g_mutex_unlock (&tunnels_lock);
651 gst_rtsp_connection_close (priv->connection);
655 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
657 GstRTSPClientPrivate *priv = client->priv;
658 GstRTSPSession *session;
659 GstRTSPSessionMedia *media;
660 GstRTSPStatusCode code;
665 session = state->session;
670 /* get a handle to the configuration of the media in the session */
671 media = gst_rtsp_session_get_media (session, state->uri);
675 state->sessmedia = media;
677 /* we emit the signal before closing the connection */
678 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
681 /* unlink the all TCP callbacks */
682 unlink_session_transports (client, session, media);
684 /* remove the session from the watched sessions */
685 g_object_weak_unref (G_OBJECT (session),
686 (GWeakNotify) client_session_finalized, client);
687 priv->sessions = g_list_remove (priv->sessions, session);
689 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
691 /* unmanage the media in the session, returns false if all media session
693 if (!gst_rtsp_session_release_media (session, media)) {
694 /* remove the session */
695 gst_rtsp_session_pool_remove (priv->session_pool, session);
697 /* construct the response now */
698 code = GST_RTSP_STS_OK;
699 gst_rtsp_message_init_response (state->response, code,
700 gst_rtsp_status_as_text (code), state->request);
702 send_response (client, session, state->response, TRUE);
709 GST_ERROR ("client %p: no session", client);
710 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
715 GST_ERROR ("client %p: no uri supplied", client);
716 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
721 GST_ERROR ("client %p: no media for uri", client);
722 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
728 default_params_set (GstRTSPClient * client, GstRTSPClientState * state)
732 res = gst_rtsp_params_set (client, state);
738 default_params_get (GstRTSPClient * client, GstRTSPClientState * state)
742 res = gst_rtsp_params_get (client, state);
748 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
754 res = gst_rtsp_message_get_body (state->request, &data, &size);
755 if (res != GST_RTSP_OK)
759 /* no body, keep-alive request */
760 send_generic_response (client, GST_RTSP_STS_OK, state);
762 /* there is a body, handle the params */
763 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, state);
764 if (res != GST_RTSP_OK)
767 send_response (client, state->session, state->response, FALSE);
770 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
778 GST_ERROR ("client %p: bad request", client);
779 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
785 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
791 res = gst_rtsp_message_get_body (state->request, &data, &size);
792 if (res != GST_RTSP_OK)
796 /* no body, keep-alive request */
797 send_generic_response (client, GST_RTSP_STS_OK, state);
799 /* there is a body, handle the params */
800 res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, state);
801 if (res != GST_RTSP_OK)
804 send_response (client, state->session, state->response, FALSE);
807 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
815 GST_ERROR ("client %p: bad request", client);
816 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
822 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
824 GstRTSPSession *session;
825 GstRTSPSessionMedia *media;
826 GstRTSPStatusCode code;
827 GstRTSPState rtspstate;
829 if (!(session = state->session))
835 /* get a handle to the configuration of the media in the session */
836 media = gst_rtsp_session_get_media (session, state->uri);
840 state->sessmedia = media;
842 rtspstate = gst_rtsp_session_media_get_rtsp_state (media);
843 /* the session state must be playing or recording */
844 if (rtspstate != GST_RTSP_STATE_PLAYING &&
845 rtspstate != GST_RTSP_STATE_RECORDING)
848 /* unlink the all TCP callbacks */
849 unlink_session_transports (client, session, media);
851 /* then pause sending */
852 gst_rtsp_session_media_set_state (media, GST_STATE_PAUSED);
854 /* construct the response now */
855 code = GST_RTSP_STS_OK;
856 gst_rtsp_message_init_response (state->response, code,
857 gst_rtsp_status_as_text (code), state->request);
859 send_response (client, session, state->response, FALSE);
861 /* the state is now READY */
862 gst_rtsp_session_media_set_rtsp_state (media, GST_RTSP_STATE_READY);
864 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
872 GST_ERROR ("client %p: no seesion", client);
873 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
878 GST_ERROR ("client %p: no uri supplied", client);
879 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
884 GST_ERROR ("client %p: no media for uri", client);
885 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
890 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
891 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
898 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
900 GstRTSPSession *session;
901 GstRTSPSessionMedia *media;
902 GstRTSPStatusCode code;
904 guint n_streams, i, infocount;
906 GstRTSPTimeRange *range;
908 GstRTSPState rtspstate;
909 GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
911 if (!(session = state->session))
917 /* get a handle to the configuration of the media in the session */
918 media = gst_rtsp_session_get_media (session, state->uri);
922 state->sessmedia = media;
924 /* the session state must be playing or ready */
925 rtspstate = gst_rtsp_session_media_get_rtsp_state (media);
926 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
929 /* parse the range header if we have one */
931 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
932 if (res == GST_RTSP_OK) {
933 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
934 /* we have a range, seek to the position */
936 gst_rtsp_media_seek (gst_rtsp_session_media_get_media (media), range);
937 gst_rtsp_range_free (range);
941 /* grab RTPInfo from the payloaders now */
942 rtpinfo = g_string_new ("");
945 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (media));
946 for (i = 0, infocount = 0; i < n_streams; i++) {
947 GstRTSPStreamTransport *trans;
948 GstRTSPStream *stream;
949 const GstRTSPTransport *tr;
953 /* get the transport, if there is no transport configured, skip this stream */
954 trans = gst_rtsp_session_media_get_transport (media, i);
956 GST_INFO ("stream %d is not configured", i);
959 tr = gst_rtsp_stream_transport_get_transport (trans);
961 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
962 /* for TCP, link the stream to the TCP connection of the client */
963 link_transport (client, session, trans);
966 stream = gst_rtsp_stream_transport_get_stream (trans);
967 if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
969 g_string_append (rtpinfo, ", ");
971 uristr = gst_rtsp_url_get_request_uri (state->uri);
972 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
973 uristr, i, seq, rtptime);
978 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
982 /* construct the response now */
983 code = GST_RTSP_STS_OK;
984 gst_rtsp_message_init_response (state->response, code,
985 gst_rtsp_status_as_text (code), state->request);
987 /* add the RTP-Info header */
989 str = g_string_free (rtpinfo, FALSE);
990 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
992 g_string_free (rtpinfo, TRUE);
997 gst_rtsp_media_get_range_string (gst_rtsp_session_media_get_media (media),
999 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
1001 send_response (client, session, state->response, FALSE);
1003 /* start playing after sending the request */
1004 gst_rtsp_session_media_set_state (media, GST_STATE_PLAYING);
1006 gst_rtsp_session_media_set_rtsp_state (media, GST_RTSP_STATE_PLAYING);
1008 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
1016 GST_ERROR ("client %p: no session", client);
1017 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1022 GST_ERROR ("client %p: no uri supplied", client);
1023 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1028 GST_ERROR ("client %p: media not found", client);
1029 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1034 GST_ERROR ("client %p: not PLAYING or READY", client);
1035 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1042 do_keepalive (GstRTSPSession * session)
1044 GST_INFO ("keep session %p alive", session);
1045 gst_rtsp_session_touch (session);
1048 /* parse @transport and return a valid transport in @tr. only transports
1049 * from @supported are returned. Returns FALSE if no valid transport
1052 parse_transport (const char *transport, GstRTSPLowerTrans supported,
1053 GstRTSPTransport * tr)
1060 gst_rtsp_transport_init (tr);
1062 GST_DEBUG ("parsing transports %s", transport);
1064 transports = g_strsplit (transport, ",", 0);
1066 /* loop through the transports, try to parse */
1067 for (i = 0; transports[i]; i++) {
1068 res = gst_rtsp_transport_parse (transports[i], tr);
1069 if (res != GST_RTSP_OK) {
1070 /* no valid transport, search some more */
1071 GST_WARNING ("could not parse transport %s", transports[i]);
1075 /* we have a transport, see if it's RTP/AVP */
1076 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1077 GST_WARNING ("invalid transport %s", transports[i]);
1081 if (!(tr->lower_transport & supported)) {
1082 GST_WARNING ("unsupported transport %s", transports[i]);
1086 /* we have a valid transport */
1087 GST_INFO ("found valid transport %s", transports[i]);
1092 gst_rtsp_transport_init (tr);
1094 g_strfreev (transports);
1100 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
1101 GstRTSPMessage * request)
1103 gchar *blocksize_str;
1104 gboolean ret = TRUE;
1106 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1107 &blocksize_str, 0) == GST_RTSP_OK) {
1111 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1112 if (end == blocksize_str) {
1113 GST_ERROR ("failed to parse blocksize");
1116 /* we don't want to change the mtu when this media
1117 * can be shared because it impacts other clients */
1118 if (gst_rtsp_media_is_shared (media))
1121 if (blocksize > G_MAXUINT)
1122 blocksize = G_MAXUINT;
1123 gst_rtsp_stream_set_mtu (stream, blocksize);
1130 default_configure_client_transport (GstRTSPClient * client,
1131 GstRTSPClientState * state, GstRTSPTransport * ct)
1133 GstRTSPClientPrivate *priv = client->priv;
1135 /* we have a valid transport now, set the destination of the client. */
1136 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1137 if (ct->destination && priv->use_client_settings) {
1138 GstRTSPAddress *addr;
1140 addr = gst_rtsp_stream_reserve_address (state->stream, ct->destination,
1141 ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1146 gst_rtsp_address_free (addr);
1148 GstRTSPAddress *addr;
1150 addr = gst_rtsp_stream_get_address (state->stream);
1154 g_free (ct->destination);
1155 ct->destination = g_strdup (addr->address);
1156 ct->port.min = addr->port;
1157 ct->port.max = addr->port + addr->n_ports - 1;
1158 ct->ttl = addr->ttl;
1160 gst_rtsp_address_free (addr);
1165 url = gst_rtsp_connection_get_url (priv->connection);
1166 g_free (ct->destination);
1167 ct->destination = g_strdup (url->host);
1169 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1170 /* check if the client selected channels for TCP */
1171 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1172 gst_rtsp_session_media_alloc_channels (state->sessmedia,
1182 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1187 static GstRTSPTransport *
1188 make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
1189 GstRTSPTransport * ct)
1191 GstRTSPTransport *st;
1193 GSocketFamily family;
1195 /* prepare the server transport */
1196 gst_rtsp_transport_new (&st);
1198 st->trans = ct->trans;
1199 st->profile = ct->profile;
1200 st->lower_transport = ct->lower_transport;
1202 addr = g_inet_address_new_from_string (ct->destination);
1205 GST_ERROR ("failed to get inet addr from client destination");
1206 family = G_SOCKET_FAMILY_IPV4;
1208 family = g_inet_address_get_family (addr);
1209 g_object_unref (addr);
1213 switch (st->lower_transport) {
1214 case GST_RTSP_LOWER_TRANS_UDP:
1215 st->client_port = ct->client_port;
1216 gst_rtsp_stream_get_server_port (state->stream, &st->server_port, family);
1218 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1219 st->port = ct->port;
1220 st->destination = g_strdup (ct->destination);
1223 case GST_RTSP_LOWER_TRANS_TCP:
1224 st->interleaved = ct->interleaved;
1229 gst_rtsp_stream_get_ssrc (state->stream, &st->ssrc);
1235 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
1237 GstRTSPClientPrivate *priv = client->priv;
1241 GstRTSPTransport *ct, *st;
1242 GstRTSPLowerTrans supported;
1243 GstRTSPStatusCode code;
1244 GstRTSPSession *session;
1245 GstRTSPStreamTransport *trans;
1246 gchar *trans_str, *pos;
1248 GstRTSPSessionMedia *sessmedia;
1249 GstRTSPMedia *media;
1250 GstRTSPStream *stream;
1251 GstRTSPState rtspstate;
1252 GstRTSPClientClass *klass;
1259 /* the uri contains the stream number we added in the SDP config, which is
1260 * always /stream=%d so we need to strip that off
1261 * parse the stream we need to configure, look for the stream in the abspath
1262 * first and then in the query. */
1263 if (uri->abspath == NULL || !(pos = strstr (uri->abspath, "/stream="))) {
1264 if (uri->query == NULL || !(pos = strstr (uri->query, "/stream=")))
1268 /* we can mofify the parsed uri in place */
1271 pos += strlen ("/stream=");
1272 if (sscanf (pos, "%u", &streamid) != 1)
1275 /* parse the transport */
1277 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
1279 if (res != GST_RTSP_OK)
1282 gst_rtsp_transport_new (&ct);
1284 /* our supported transports */
1285 supported = GST_RTSP_LOWER_TRANS_UDP |
1286 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
1288 /* parse and find a usable supported transport */
1289 if (!parse_transport (transport, supported, ct))
1290 goto unsupported_transports;
1292 /* we create the session after parsing stuff so that we don't make
1293 * a session for malformed requests */
1294 if (priv->session_pool == NULL)
1297 session = state->session;
1300 g_object_ref (session);
1301 /* get a handle to the configuration of the media in the session, this can
1302 * return NULL if this is a new url to manage in this session. */
1303 sessmedia = gst_rtsp_session_get_media (session, uri);
1305 /* create a session if this fails we probably reached our session limit or
1307 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1308 goto service_unavailable;
1310 state->session = session;
1312 /* we need a new media configuration in this session */
1316 /* we have no media, find one and manage it */
1317 if (sessmedia == NULL) {
1318 /* get a handle to the configuration of the media in the session */
1319 if ((media = find_media (client, state))) {
1320 /* manage the media in our session now */
1321 sessmedia = gst_rtsp_session_manage_media (session, uri, media);
1325 /* if we stil have no media, error */
1326 if (sessmedia == NULL)
1329 state->sessmedia = sessmedia;
1330 state->media = media = gst_rtsp_session_media_get_media (sessmedia);
1332 /* now get the stream */
1333 stream = gst_rtsp_media_get_stream (media, streamid);
1337 state->stream = stream;
1339 /* set blocksize on this stream */
1340 if (!handle_blocksize (media, stream, state->request))
1341 goto invalid_blocksize;
1343 /* update the client transport */
1344 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1345 if (!klass->configure_client_transport (client, state, ct))
1346 goto unsupported_client_transport;
1348 /* set in the session media transport */
1349 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1351 /* configure keepalive for this transport */
1352 gst_rtsp_stream_transport_set_keepalive (trans,
1353 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1355 /* create and serialize the server transport */
1356 st = make_server_transport (client, state, ct);
1357 trans_str = gst_rtsp_transport_as_text (st);
1358 gst_rtsp_transport_free (st);
1360 /* construct the response now */
1361 code = GST_RTSP_STS_OK;
1362 gst_rtsp_message_init_response (state->response, code,
1363 gst_rtsp_status_as_text (code), state->request);
1365 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1369 send_response (client, session, state->response, FALSE);
1371 /* update the state */
1372 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1373 switch (rtspstate) {
1374 case GST_RTSP_STATE_PLAYING:
1375 case GST_RTSP_STATE_RECORDING:
1376 case GST_RTSP_STATE_READY:
1377 /* no state change */
1380 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1383 g_object_unref (session);
1385 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST],
1393 GST_ERROR ("client %p: no uri", client);
1394 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1399 GST_ERROR ("client %p: bad request", client);
1400 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1405 GST_ERROR ("client %p: media not found", client);
1406 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1407 g_object_unref (session);
1408 gst_rtsp_transport_free (ct);
1413 GST_ERROR ("client %p: invalid blocksize", client);
1414 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1415 g_object_unref (session);
1416 gst_rtsp_transport_free (ct);
1419 unsupported_client_transport:
1421 GST_ERROR ("client %p: unsupported client transport", client);
1422 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1423 g_object_unref (session);
1424 gst_rtsp_transport_free (ct);
1429 GST_ERROR ("client %p: no transport", client);
1430 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1433 unsupported_transports:
1435 GST_ERROR ("client %p: unsupported transports", client);
1436 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1437 gst_rtsp_transport_free (ct);
1442 GST_ERROR ("client %p: no session pool configured", client);
1443 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1444 gst_rtsp_transport_free (ct);
1447 service_unavailable:
1449 GST_ERROR ("client %p: can't create session", client);
1450 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1451 gst_rtsp_transport_free (ct);
1456 static GstSDPMessage *
1457 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1459 GstRTSPClientPrivate *priv = client->priv;
1464 gst_sdp_message_new (&sdp);
1466 /* some standard things first */
1467 gst_sdp_message_set_version (sdp, "0");
1474 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1477 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1478 gst_sdp_message_set_information (sdp, "rtsp-server");
1479 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1480 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1481 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1482 gst_sdp_message_add_attribute (sdp, "control", "*");
1484 info.is_ipv6 = priv->is_ipv6;
1485 info.server_ip = priv->server_ip;
1487 /* create an SDP for the media object */
1488 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1496 GST_ERROR ("client %p: could not create SDP", client);
1497 gst_sdp_message_free (sdp);
1502 /* for the describe we must generate an SDP */
1504 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1509 gchar *str, *content_base;
1510 GstRTSPMedia *media;
1511 GstRTSPClientClass *klass;
1513 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1518 /* check what kind of format is accepted, we don't really do anything with it
1519 * and always return SDP for now. */
1524 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1526 if (res == GST_RTSP_ENOTIMPL)
1529 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1533 /* find the media object for the uri */
1534 if (!(media = find_media (client, state)))
1537 /* create an SDP for the media object on this client */
1538 if (!(sdp = klass->create_sdp (client, media)))
1541 g_object_unref (media);
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_CONTENT_TYPE,
1549 /* content base for some clients that might screw up creating the setup uri */
1550 str = gst_rtsp_url_get_request_uri (state->uri);
1551 str_len = strlen (str);
1553 /* check for trailing '/' and append one */
1554 if (str[str_len - 1] != '/') {
1555 content_base = g_malloc (str_len + 2);
1556 memcpy (content_base, str, str_len);
1557 content_base[str_len] = '/';
1558 content_base[str_len + 1] = '\0';
1564 GST_INFO ("adding content-base: %s", content_base);
1566 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1568 g_free (content_base);
1570 /* add SDP to the response body */
1571 str = gst_sdp_message_as_text (sdp);
1572 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1573 gst_sdp_message_free (sdp);
1575 send_response (client, state->session, state->response, FALSE);
1577 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1585 GST_ERROR ("client %p: no uri", client);
1586 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1591 GST_ERROR ("client %p: no media", client);
1592 /* error reply is already sent */
1597 GST_ERROR ("client %p: can't create SDP", client);
1598 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1599 g_object_unref (media);
1605 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1607 GstRTSPMethod options;
1610 options = GST_RTSP_DESCRIBE |
1615 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1617 str = gst_rtsp_options_as_text (options);
1619 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1620 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1622 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1625 send_response (client, state->session, state->response, FALSE);
1627 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1633 /* remove duplicate and trailing '/' */
1635 sanitize_uri (GstRTSPUrl * uri)
1639 gboolean have_slash, prev_slash;
1641 s = d = uri->abspath;
1642 len = strlen (uri->abspath);
1646 for (i = 0; i < len; i++) {
1647 have_slash = s[i] == '/';
1649 if (!have_slash || !prev_slash)
1651 prev_slash = have_slash;
1653 len = d - uri->abspath;
1654 /* don't remove the first slash if that's the only thing left */
1655 if (len > 1 && *(d - 1) == '/')
1661 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1663 GstRTSPClientPrivate *priv = client->priv;
1665 GST_INFO ("client %p: session %p finished", client, session);
1667 /* unlink all media managed in this session */
1668 client_unlink_session (client, session);
1670 /* remove the session */
1671 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1672 GST_INFO ("client %p: all sessions finalized, close the connection",
1674 close_connection (client);
1679 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
1681 GstRTSPClientPrivate *priv = client->priv;
1684 for (walk = priv->sessions; walk; walk = g_list_next (walk)) {
1685 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
1687 /* we already know about this session */
1688 if (msession == session)
1692 GST_INFO ("watching session %p", session);
1694 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
1696 priv->sessions = g_list_prepend (priv->sessions, session);
1698 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1703 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1705 GstRTSPClientPrivate *priv = client->priv;
1706 GstRTSPMethod method;
1707 const gchar *uristr;
1708 GstRTSPUrl *uri = NULL;
1709 GstRTSPVersion version;
1711 GstRTSPSession *session = NULL;
1712 GstRTSPClientState state = { NULL };
1713 GstRTSPMessage response = { 0 };
1716 state.request = request;
1717 state.response = &response;
1719 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1720 gst_rtsp_message_dump (request);
1723 GST_INFO ("client %p: received a request", client);
1725 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1727 /* we can only handle 1.0 requests */
1728 if (version != GST_RTSP_VERSION_1_0)
1731 state.method = method;
1733 /* we always try to parse the url first */
1734 if (strcmp (uristr, "*") == 0) {
1735 /* special case where we have * as uri, keep uri = NULL */
1736 } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1739 /* get the session if there is any */
1740 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1741 if (res == GST_RTSP_OK) {
1742 if (priv->session_pool == NULL)
1745 /* we had a session in the request, find it again */
1746 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1747 goto session_not_found;
1749 /* we add the session to the client list of watched sessions. When a session
1750 * disappears because it times out, we will be notified. If all sessions are
1751 * gone, we will close the connection */
1752 client_watch_session (client, session);
1755 /* sanitize the uri */
1759 state.session = session;
1762 if (!gst_rtsp_auth_check (priv->auth, client, 0, &state))
1763 goto not_authorized;
1766 /* now see what is asked and dispatch to a dedicated handler */
1768 case GST_RTSP_OPTIONS:
1769 handle_options_request (client, &state);
1771 case GST_RTSP_DESCRIBE:
1772 handle_describe_request (client, &state);
1774 case GST_RTSP_SETUP:
1775 handle_setup_request (client, &state);
1778 handle_play_request (client, &state);
1780 case GST_RTSP_PAUSE:
1781 handle_pause_request (client, &state);
1783 case GST_RTSP_TEARDOWN:
1784 handle_teardown_request (client, &state);
1786 case GST_RTSP_SET_PARAMETER:
1787 handle_set_param_request (client, &state);
1789 case GST_RTSP_GET_PARAMETER:
1790 handle_get_param_request (client, &state);
1792 case GST_RTSP_ANNOUNCE:
1793 case GST_RTSP_RECORD:
1794 case GST_RTSP_REDIRECT:
1795 goto not_implemented;
1796 case GST_RTSP_INVALID:
1803 g_object_unref (session);
1805 gst_rtsp_url_free (uri);
1811 GST_ERROR ("client %p: version %d not supported", client, version);
1812 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1818 GST_ERROR ("client %p: bad request", client);
1819 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1824 GST_ERROR ("client %p: no pool configured", client);
1825 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1830 GST_ERROR ("client %p: session not found", client);
1831 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1836 GST_ERROR ("client %p: not allowed", client);
1837 handle_unauthorized_request (client, priv->auth, &state);
1842 GST_ERROR ("client %p: method %d not implemented", client, method);
1843 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1849 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1851 GstRTSPClientPrivate *priv = client->priv;
1860 /* find the stream for this message */
1861 res = gst_rtsp_message_parse_data (message, &channel);
1862 if (res != GST_RTSP_OK)
1865 gst_rtsp_message_steal_body (message, &data, &size);
1867 buffer = gst_buffer_new_wrapped (data, size);
1870 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1871 GstRTSPStreamTransport *trans;
1872 GstRTSPStream *stream;
1873 const GstRTSPTransport *tr;
1877 tr = gst_rtsp_stream_transport_get_transport (trans);
1878 stream = gst_rtsp_stream_transport_get_stream (trans);
1880 /* check for TCP transport */
1881 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1882 /* dispatch to the stream based on the channel number */
1883 if (tr->interleaved.min == channel) {
1884 gst_rtsp_stream_recv_rtp (stream, buffer);
1887 } else if (tr->interleaved.max == channel) {
1888 gst_rtsp_stream_recv_rtcp (stream, buffer);
1895 gst_buffer_unref (buffer);
1899 * gst_rtsp_client_set_session_pool:
1900 * @client: a #GstRTSPClient
1901 * @pool: a #GstRTSPSessionPool
1903 * Set @pool as the sessionpool for @client which it will use to find
1904 * or allocate sessions. the sessionpool is usually inherited from the server
1905 * that created the client but can be overridden later.
1908 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
1909 GstRTSPSessionPool * pool)
1911 GstRTSPSessionPool *old;
1912 GstRTSPClientPrivate *priv;
1914 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1916 priv = client->priv;
1919 g_object_ref (pool);
1921 g_mutex_lock (&priv->lock);
1922 old = priv->session_pool;
1923 priv->session_pool = pool;
1924 g_mutex_unlock (&priv->lock);
1927 g_object_unref (old);
1931 * gst_rtsp_client_get_session_pool:
1932 * @client: a #GstRTSPClient
1934 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
1936 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
1938 GstRTSPSessionPool *
1939 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
1941 GstRTSPClientPrivate *priv;
1942 GstRTSPSessionPool *result;
1944 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1946 priv = client->priv;
1948 g_mutex_lock (&priv->lock);
1949 if ((result = priv->session_pool))
1950 g_object_ref (result);
1951 g_mutex_unlock (&priv->lock);
1957 * gst_rtsp_client_set_mount_points:
1958 * @client: a #GstRTSPClient
1959 * @mounts: a #GstRTSPMountPoints
1961 * Set @mounts as the mount points for @client which it will use to map urls
1962 * to media streams. These mount points are usually inherited from the server that
1963 * created the client but can be overriden later.
1966 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
1967 GstRTSPMountPoints * mounts)
1969 GstRTSPClientPrivate *priv;
1970 GstRTSPMountPoints *old;
1972 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1974 priv = client->priv;
1977 g_object_ref (mounts);
1979 g_mutex_lock (&priv->lock);
1980 old = priv->mount_points;
1981 priv->mount_points = mounts;
1982 g_mutex_unlock (&priv->lock);
1985 g_object_unref (old);
1989 * gst_rtsp_client_get_mount_points:
1990 * @client: a #GstRTSPClient
1992 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
1994 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
1996 GstRTSPMountPoints *
1997 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
1999 GstRTSPClientPrivate *priv;
2000 GstRTSPMountPoints *result;
2002 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2004 priv = client->priv;
2006 g_mutex_lock (&priv->lock);
2007 if ((result = priv->mount_points))
2008 g_object_ref (result);
2009 g_mutex_unlock (&priv->lock);
2015 * gst_rtsp_client_set_use_client_settings:
2016 * @client: a #GstRTSPClient
2017 * @use_client_settings: whether to use client settings for multicast
2019 * Use client transport settings (destination and ttl) for multicast.
2020 * When @use_client_settings is %FALSE, the server settings will be
2024 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
2025 gboolean use_client_settings)
2027 GstRTSPClientPrivate *priv;
2029 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2031 priv = client->priv;
2033 g_mutex_lock (&priv->lock);
2034 priv->use_client_settings = use_client_settings;
2035 g_mutex_unlock (&priv->lock);
2039 * gst_rtsp_client_get_use_client_settings:
2040 * @client: a #GstRTSPClient
2042 * Check if client transport settings (destination and ttl) for multicast
2046 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
2048 GstRTSPClientPrivate *priv;
2051 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2053 priv = client->priv;
2055 g_mutex_lock (&priv->lock);
2056 res = priv->use_client_settings;
2057 g_mutex_unlock (&priv->lock);
2063 * gst_rtsp_client_set_auth:
2064 * @client: a #GstRTSPClient
2065 * @auth: a #GstRTSPAuth
2067 * configure @auth to be used as the authentication manager of @client.
2070 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2072 GstRTSPClientPrivate *priv;
2075 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2077 priv = client->priv;
2080 g_object_ref (auth);
2082 g_mutex_lock (&priv->lock);
2085 g_mutex_unlock (&priv->lock);
2088 g_object_unref (old);
2093 * gst_rtsp_client_get_auth:
2094 * @client: a #GstRTSPClient
2096 * Get the #GstRTSPAuth used as the authentication manager of @client.
2098 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2102 gst_rtsp_client_get_auth (GstRTSPClient * client)
2104 GstRTSPClientPrivate *priv;
2105 GstRTSPAuth *result;
2107 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2109 priv = client->priv;
2111 g_mutex_lock (&priv->lock);
2112 if ((result = priv->auth))
2113 g_object_ref (result);
2114 g_mutex_unlock (&priv->lock);
2120 * gst_rtsp_client_get_uri:
2121 * @client: a #GstRTSPClient
2123 * Get the #GstRTSPUrl of @client.
2125 * Returns: (transfer full): the #GstRTSPUrl of @client. Free with
2126 * gst_rtsp_url_free () after usage.
2129 gst_rtsp_client_get_uri (GstRTSPClient * client)
2131 GstRTSPClientPrivate *priv;
2132 GstRTSPUrl *result = NULL;
2134 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2136 priv = client->priv;
2138 g_mutex_lock (&priv->lock);
2139 if (priv->uri != NULL)
2140 result = gst_rtsp_url_copy (priv->uri);
2141 g_mutex_unlock (&priv->lock);
2147 * gst_rtsp_client_set_connection:
2148 * @client: a #GstRTSPClient
2149 * @conn: (transfer full): a #GstRTSPConnection
2151 * Set the #GstRTSPConnection of @client. This function takes ownership of
2154 * Returns: %TRUE on success.
2157 gst_rtsp_client_set_connection (GstRTSPClient * client,
2158 GstRTSPConnection * conn)
2160 GstRTSPClientPrivate *priv;
2161 GSocket *read_socket;
2162 GSocketAddress *address;
2164 GError *error = NULL;
2166 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2167 g_return_val_if_fail (conn != NULL, FALSE);
2169 priv = client->priv;
2171 read_socket = gst_rtsp_connection_get_read_socket (conn);
2173 if (!(address = g_socket_get_local_address (read_socket, &error)))
2176 g_free (priv->server_ip);
2177 /* keep the original ip that the client connected to */
2178 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2179 GInetAddress *iaddr;
2181 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2183 /* socket might be ipv6 but adress still ipv4 */
2184 priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2185 priv->server_ip = g_inet_address_to_string (iaddr);
2186 g_object_unref (address);
2188 priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2189 priv->server_ip = g_strdup ("unknown");
2192 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2193 priv->server_ip, priv->is_ipv6);
2195 url = gst_rtsp_connection_get_url (conn);
2196 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2198 priv->connection = conn;
2205 GST_ERROR ("could not get remote address %s", error->message);
2206 g_error_free (error);
2212 * gst_rtsp_client_get_connection:
2213 * @client: a #GstRTSPClient
2215 * Get the #GstRTSPConnection of @client.
2217 * Returns: (transfer none): the #GstRTSPConnection of @client.
2218 * The connection object returned remains valid until the client is freed.
2221 gst_rtsp_client_get_connection (GstRTSPClient * client)
2223 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2225 return client->priv->connection;
2229 * gst_rtsp_client_set_send_func:
2230 * @client: a #GstRTSPClient
2231 * @func: a #GstRTSPClientSendFunc
2232 * @user_data: user data passed to @func
2233 * @notify: called when @user_data is no longer in use
2235 * Set @func as the callback that will be called when a new message needs to be
2236 * sent to the client. @user_data is passed to @func and @notify is called when
2237 * @user_data is no longer in use.
2240 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2241 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2243 GstRTSPClientPrivate *priv;
2244 GDestroyNotify old_notify;
2247 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2249 priv = client->priv;
2251 g_mutex_lock (&priv->send_lock);
2252 priv->send_func = func;
2253 old_notify = priv->send_notify;
2254 old_data = priv->send_data;
2255 priv->send_notify = notify;
2256 priv->send_data = user_data;
2257 g_mutex_unlock (&priv->send_lock);
2260 old_notify (old_data);
2264 * gst_rtsp_client_handle_message:
2265 * @client: a #GstRTSPClient
2266 * @message: an #GstRTSPMessage
2268 * Let the client handle @message.
2270 * Returns: a #GstRTSPResult.
2273 gst_rtsp_client_handle_message (GstRTSPClient * client,
2274 GstRTSPMessage * message)
2276 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2277 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2279 switch (message->type) {
2280 case GST_RTSP_MESSAGE_REQUEST:
2281 handle_request (client, message);
2283 case GST_RTSP_MESSAGE_RESPONSE:
2285 case GST_RTSP_MESSAGE_DATA:
2286 handle_data (client, message);
2294 static GstRTSPResult
2295 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2296 gboolean close, gpointer user_data)
2298 GstRTSPClientPrivate *priv = client->priv;
2300 /* send the response and store the seq number so we can wait until it's
2301 * written to the client to close the connection */
2302 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2303 &priv->close_seq : NULL);
2306 static GstRTSPResult
2307 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2310 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2313 static GstRTSPResult
2314 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2316 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2317 GstRTSPClientPrivate *priv = client->priv;
2319 if (priv->close_seq && priv->close_seq == cseq) {
2320 priv->close_seq = 0;
2321 close_connection (client);
2327 static GstRTSPResult
2328 closed (GstRTSPWatch * watch, gpointer user_data)
2330 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2331 GstRTSPClientPrivate *priv = client->priv;
2332 const gchar *tunnelid;
2334 GST_INFO ("client %p: connection closed", client);
2336 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2337 g_mutex_lock (&tunnels_lock);
2338 /* remove from tunnelids */
2339 g_hash_table_remove (tunnels, tunnelid);
2340 g_mutex_unlock (&tunnels_lock);
2343 gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2348 static GstRTSPResult
2349 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2351 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2354 str = gst_rtsp_strresult (result);
2355 GST_INFO ("client %p: received an error %s", client, str);
2361 static GstRTSPResult
2362 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2363 GstRTSPMessage * message, guint id, gpointer user_data)
2365 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2368 str = gst_rtsp_strresult (result);
2370 ("client %p: error when handling message %p with id %d: %s",
2371 client, message, id, str);
2378 remember_tunnel (GstRTSPClient * client)
2380 GstRTSPClientPrivate *priv = client->priv;
2381 const gchar *tunnelid;
2383 /* store client in the pending tunnels */
2384 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2385 if (tunnelid == NULL)
2388 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2390 /* we can't have two clients connecting with the same tunnelid */
2391 g_mutex_lock (&tunnels_lock);
2392 if (g_hash_table_lookup (tunnels, tunnelid))
2393 goto tunnel_existed;
2395 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2396 g_mutex_unlock (&tunnels_lock);
2403 GST_ERROR ("client %p: no tunnelid provided", client);
2408 g_mutex_unlock (&tunnels_lock);
2409 GST_ERROR ("client %p: tunnel session %s already existed", client,
2415 static GstRTSPStatusCode
2416 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2418 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2419 GstRTSPClientPrivate *priv = client->priv;
2421 GST_INFO ("client %p: tunnel start (connection %p)", client,
2424 if (!remember_tunnel (client))
2427 return GST_RTSP_STS_OK;
2432 GST_ERROR ("client %p: error starting tunnel", client);
2433 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2437 static GstRTSPResult
2438 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2440 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2441 GstRTSPClientPrivate *priv = client->priv;
2443 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2446 /* ignore error, it'll only be a problem when the client does a POST again */
2447 remember_tunnel (client);
2452 static GstRTSPResult
2453 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2455 const gchar *tunnelid;
2456 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2457 GstRTSPClientPrivate *priv = client->priv;
2458 GstRTSPClient *oclient;
2459 GstRTSPClientPrivate *opriv;
2461 GST_INFO ("client %p: tunnel complete", client);
2463 /* find previous tunnel */
2464 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2465 if (tunnelid == NULL)
2468 g_mutex_lock (&tunnels_lock);
2469 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2472 /* remove the old client from the table. ref before because removing it will
2473 * remove the ref to it. */
2474 g_object_ref (oclient);
2475 g_hash_table_remove (tunnels, tunnelid);
2477 opriv = oclient->priv;
2479 if (opriv->watch == NULL)
2481 g_mutex_unlock (&tunnels_lock);
2483 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2484 opriv->connection, priv->connection);
2486 /* merge the tunnels into the first client */
2487 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2488 gst_rtsp_watch_reset (opriv->watch);
2489 g_object_unref (oclient);
2496 GST_ERROR ("client %p: no tunnelid provided", client);
2497 return GST_RTSP_ERROR;
2501 g_mutex_unlock (&tunnels_lock);
2502 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2503 return GST_RTSP_ERROR;
2507 g_mutex_unlock (&tunnels_lock);
2508 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2509 g_object_unref (oclient);
2510 return GST_RTSP_ERROR;
2514 static GstRTSPWatchFuncs watch_funcs = {
2526 client_watch_notify (GstRTSPClient * client)
2528 GstRTSPClientPrivate *priv = client->priv;
2530 GST_INFO ("client %p: watch destroyed", client);
2532 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2533 g_object_unref (client);
2537 * gst_rtsp_client_attach:
2538 * @client: a #GstRTSPClient
2539 * @context: (allow-none): a #GMainContext
2541 * Attaches @client to @context. When the mainloop for @context is run, the
2542 * client will be dispatched. When @context is NULL, the default context will be
2545 * This function should be called when the client properties and urls are fully
2546 * configured and the client is ready to start.
2548 * Returns: the ID (greater than 0) for the source within the GMainContext.
2551 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2553 GstRTSPClientPrivate *priv;
2556 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2557 priv = client->priv;
2558 g_return_val_if_fail (priv->watch == NULL, 0);
2560 /* create watch for the connection and attach */
2561 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2562 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2563 gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2564 (GDestroyNotify) gst_rtsp_watch_unref);
2566 /* FIXME make this configurable. We don't want to do this yet because it will
2567 * be superceeded by a cache object later */
2568 gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2570 GST_INFO ("attaching to context %p", context);
2571 res = gst_rtsp_watch_attach (priv->watch, context);