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))
30 struct _GstRTSPClientPrivate
33 GstRTSPConnection *connection;
38 gboolean use_client_settings;
40 GstRTSPClientSendFunc send_func;
42 GDestroyNotify send_notify;
44 GstRTSPSessionPool *session_pool;
45 GstRTSPMountPoints *mount_points;
55 static GMutex tunnels_lock;
56 static GHashTable *tunnels;
58 #define DEFAULT_SESSION_POOL NULL
59 #define DEFAULT_MOUNT_POINTS NULL
60 #define DEFAULT_USE_CLIENT_SETTINGS FALSE
67 PROP_USE_CLIENT_SETTINGS,
75 SIGNAL_OPTIONS_REQUEST,
76 SIGNAL_DESCRIBE_REQUEST,
80 SIGNAL_TEARDOWN_REQUEST,
81 SIGNAL_SET_PARAMETER_REQUEST,
82 SIGNAL_GET_PARAMETER_REQUEST,
86 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
87 #define GST_CAT_DEFAULT rtsp_client_debug
89 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
91 static void gst_rtsp_client_get_property (GObject * object, guint propid,
92 GValue * value, GParamSpec * pspec);
93 static void gst_rtsp_client_set_property (GObject * object, guint propid,
94 const GValue * value, GParamSpec * pspec);
95 static void gst_rtsp_client_finalize (GObject * obj);
97 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
98 static void client_session_finalized (GstRTSPClient * client,
99 GstRTSPSession * session);
100 static void unlink_session_transports (GstRTSPClient * client,
101 GstRTSPSession * session, GstRTSPSessionMedia * media);
103 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
106 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
108 GObjectClass *gobject_class;
110 g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
112 gobject_class = G_OBJECT_CLASS (klass);
114 gobject_class->get_property = gst_rtsp_client_get_property;
115 gobject_class->set_property = gst_rtsp_client_set_property;
116 gobject_class->finalize = gst_rtsp_client_finalize;
118 klass->create_sdp = create_sdp;
120 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
121 g_param_spec_object ("session-pool", "Session Pool",
122 "The session pool to use for client session",
123 GST_TYPE_RTSP_SESSION_POOL,
124 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
126 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
127 g_param_spec_object ("mount-points", "Mount Points",
128 "The mount points to use for client session",
129 GST_TYPE_RTSP_MOUNT_POINTS,
130 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
132 g_object_class_install_property (gobject_class, PROP_USE_CLIENT_SETTINGS,
133 g_param_spec_boolean ("use-client-settings", "Use Client Settings",
134 "Use client settings for ttl and destination in multicast",
135 DEFAULT_USE_CLIENT_SETTINGS,
136 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
138 gst_rtsp_client_signals[SIGNAL_CLOSED] =
139 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
140 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
141 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
143 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
144 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
145 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
146 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
148 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
149 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
150 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
151 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
154 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
155 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
156 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
157 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
160 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
161 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
162 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
163 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
166 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
167 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
168 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
169 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
172 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
173 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
174 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
175 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
178 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
179 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
180 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
181 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
184 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
185 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
186 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
187 set_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
188 G_TYPE_NONE, 1, G_TYPE_POINTER);
190 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
191 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
192 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
193 get_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
194 G_TYPE_NONE, 1, G_TYPE_POINTER);
197 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
198 g_mutex_init (&tunnels_lock);
200 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
204 gst_rtsp_client_init (GstRTSPClient * client)
206 GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
210 g_mutex_init (&priv->lock);
211 priv->use_client_settings = DEFAULT_USE_CLIENT_SETTINGS;
215 static GstRTSPFilterResult
216 filter_session (GstRTSPSession * sess, GstRTSPSessionMedia * media,
219 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
221 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
222 unlink_session_transports (client, sess, media);
224 /* unmanage the media in the session */
225 return GST_RTSP_FILTER_REMOVE;
229 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
231 /* unlink all media managed in this session */
232 gst_rtsp_session_filter (session, filter_session, client);
236 client_cleanup_sessions (GstRTSPClient * client)
238 GstRTSPClientPrivate *priv = client->priv;
241 /* remove weak-ref from sessions */
242 for (sessions = priv->sessions; sessions; sessions = g_list_next (sessions)) {
243 GstRTSPSession *session = (GstRTSPSession *) sessions->data;
244 g_object_weak_unref (G_OBJECT (session),
245 (GWeakNotify) client_session_finalized, client);
246 client_unlink_session (client, session);
248 g_list_free (priv->sessions);
249 priv->sessions = NULL;
252 /* A client is finalized when the connection is broken */
254 gst_rtsp_client_finalize (GObject * obj)
256 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
257 GstRTSPClientPrivate *priv = client->priv;
259 GST_INFO ("finalize client %p", client);
262 g_source_destroy ((GSource *) priv->watch);
264 if (priv->send_notify)
265 priv->send_notify (priv->send_data);
267 client_cleanup_sessions (client);
269 if (priv->connection)
270 gst_rtsp_connection_free (priv->connection);
271 if (priv->session_pool)
272 g_object_unref (priv->session_pool);
273 if (priv->mount_points)
274 g_object_unref (priv->mount_points);
276 g_object_unref (priv->auth);
279 gst_rtsp_url_free (priv->uri);
281 gst_rtsp_media_unprepare (priv->media);
282 g_object_unref (priv->media);
285 g_free (priv->server_ip);
286 g_mutex_clear (&priv->lock);
288 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
292 gst_rtsp_client_get_property (GObject * object, guint propid,
293 GValue * value, GParamSpec * pspec)
295 GstRTSPClient *client = GST_RTSP_CLIENT (object);
298 case PROP_SESSION_POOL:
299 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
301 case PROP_MOUNT_POINTS:
302 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
304 case PROP_USE_CLIENT_SETTINGS:
305 g_value_set_boolean (value,
306 gst_rtsp_client_get_use_client_settings (client));
309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
314 gst_rtsp_client_set_property (GObject * object, guint propid,
315 const GValue * value, GParamSpec * pspec)
317 GstRTSPClient *client = GST_RTSP_CLIENT (object);
320 case PROP_SESSION_POOL:
321 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
323 case PROP_MOUNT_POINTS:
324 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
326 case PROP_USE_CLIENT_SETTINGS:
327 gst_rtsp_client_set_use_client_settings (client,
328 g_value_get_boolean (value));
331 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
336 * gst_rtsp_client_new:
338 * Create a new #GstRTSPClient instance.
340 * Returns: a new #GstRTSPClient
343 gst_rtsp_client_new (void)
345 GstRTSPClient *result;
347 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
353 send_response (GstRTSPClient * client, GstRTSPSession * session,
354 GstRTSPMessage * response, gboolean close)
356 GstRTSPClientPrivate *priv = client->priv;
358 gst_rtsp_message_add_header (response, GST_RTSP_HDR_SERVER,
359 "GStreamer RTSP server");
361 /* remove any previous header */
362 gst_rtsp_message_remove_header (response, GST_RTSP_HDR_SESSION, -1);
364 /* add the new session header for new session ids */
366 gst_rtsp_message_take_header (response, GST_RTSP_HDR_SESSION,
367 gst_rtsp_session_get_header (session));
370 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
371 gst_rtsp_message_dump (response);
375 gst_rtsp_message_add_header (response, GST_RTSP_HDR_CONNECTION, "close");
378 priv->send_func (client, response, close, priv->send_data);
380 gst_rtsp_message_unset (response);
384 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
385 GstRTSPClientState * state)
387 gst_rtsp_message_init_response (state->response, code,
388 gst_rtsp_status_as_text (code), state->request);
390 send_response (client, NULL, state->response, FALSE);
394 handle_unauthorized_request (GstRTSPClient * client, GstRTSPAuth * auth,
395 GstRTSPClientState * state)
397 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_UNAUTHORIZED,
398 gst_rtsp_status_as_text (GST_RTSP_STS_UNAUTHORIZED), state->request);
401 /* and let the authentication manager setup the auth tokens */
402 gst_rtsp_auth_setup_auth (auth, client, 0, state);
405 send_response (client, state->session, state->response, FALSE);
410 compare_uri (const GstRTSPUrl * uri1, const GstRTSPUrl * uri2)
412 if (uri1 == NULL || uri2 == NULL)
415 if (strcmp (uri1->abspath, uri2->abspath))
421 /* this function is called to initially find the media for the DESCRIBE request
422 * but is cached for when the same client (without breaking the connection) is
423 * doing a setup for the exact same url. */
424 static GstRTSPMedia *
425 find_media (GstRTSPClient * client, GstRTSPClientState * state)
427 GstRTSPClientPrivate *priv = client->priv;
428 GstRTSPMediaFactory *factory;
432 if (!compare_uri (priv->uri, state->uri)) {
433 /* remove any previously cached values before we try to construct a new
436 gst_rtsp_url_free (priv->uri);
439 gst_rtsp_media_unprepare (priv->media);
440 g_object_unref (priv->media);
444 if (!priv->mount_points)
445 goto no_mount_points;
447 /* find the factory for the uri first */
449 gst_rtsp_mount_points_find_factory (priv->mount_points,
453 /* check if we have access to the factory */
454 if ((auth = gst_rtsp_media_factory_get_auth (factory))) {
455 state->factory = factory;
457 if (!gst_rtsp_auth_check (auth, client, 0, state))
460 state->factory = NULL;
461 g_object_unref (auth);
464 /* prepare the media and add it to the pipeline */
465 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
468 g_object_unref (factory);
471 /* prepare the media */
472 if (!(gst_rtsp_media_prepare (media)))
475 /* now keep track of the uri and the media */
476 priv->uri = gst_rtsp_url_copy (state->uri);
478 state->media = media;
480 /* we have seen this uri before, used cached media */
482 state->media = media;
483 GST_INFO ("reusing cached media %p", media);
487 g_object_ref (media);
494 GST_ERROR ("client %p: no mount points configured", client);
495 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
500 GST_ERROR ("client %p: no factory for uri", client);
501 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
506 GST_ERROR ("client %p: unauthorized request", client);
507 handle_unauthorized_request (client, auth, state);
508 g_object_unref (factory);
509 state->factory = NULL;
510 g_object_unref (auth);
515 GST_ERROR ("client %p: can't create media", client);
516 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
517 g_object_unref (factory);
522 GST_ERROR ("client %p: can't prepare media", client);
523 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
524 g_object_unref (media);
530 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
532 GstRTSPClientPrivate *priv = client->priv;
533 GstRTSPMessage message = { 0 };
538 gst_rtsp_message_init_data (&message, channel);
540 /* FIXME, need some sort of iovec RTSPMessage here */
541 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
544 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
547 priv->send_func (client, &message, FALSE, priv->send_data);
549 gst_rtsp_message_steal_body (&message, &data, &usize);
550 gst_buffer_unmap (buffer, &map_info);
552 gst_rtsp_message_unset (&message);
558 link_transport (GstRTSPClient * client, GstRTSPSession * session,
559 GstRTSPStreamTransport * trans)
561 GstRTSPClientPrivate *priv = client->priv;
563 GST_DEBUG ("client %p: linking transport %p", client, trans);
565 gst_rtsp_stream_transport_set_callbacks (trans,
566 (GstRTSPSendFunc) do_send_data,
567 (GstRTSPSendFunc) do_send_data, client, NULL);
569 priv->transports = g_list_prepend (priv->transports, trans);
571 /* make sure our session can't expire */
572 gst_rtsp_session_prevent_expire (session);
576 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
577 GstRTSPStreamTransport * trans)
579 GstRTSPClientPrivate *priv = client->priv;
581 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
583 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
585 priv->transports = g_list_remove (priv->transports, trans);
587 /* our session can now expire */
588 gst_rtsp_session_allow_expire (session);
592 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
593 GstRTSPSessionMedia * media)
598 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (media));
599 for (i = 0; i < n_streams; i++) {
600 GstRTSPStreamTransport *trans;
601 const GstRTSPTransport *tr;
603 /* get the transport, if there is no transport configured, skip this stream */
604 trans = gst_rtsp_session_media_get_transport (media, i);
608 tr = gst_rtsp_stream_transport_get_transport (trans);
610 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
611 /* for TCP, unlink the stream from the TCP connection of the client */
612 unlink_transport (client, session, trans);
618 close_connection (GstRTSPClient * client)
620 GstRTSPClientPrivate *priv = client->priv;
621 const gchar *tunnelid;
623 GST_DEBUG ("client %p: closing connection", client);
625 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
626 g_mutex_lock (&tunnels_lock);
627 /* remove from tunnelids */
628 g_hash_table_remove (tunnels, tunnelid);
629 g_mutex_unlock (&tunnels_lock);
632 gst_rtsp_connection_close (priv->connection);
636 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
638 GstRTSPClientPrivate *priv = client->priv;
639 GstRTSPSession *session;
640 GstRTSPSessionMedia *media;
641 GstRTSPStatusCode code;
646 session = state->session;
648 /* get a handle to the configuration of the media in the session */
649 media = gst_rtsp_session_get_media (session, state->uri);
653 state->sessmedia = media;
655 /* unlink the all TCP callbacks */
656 unlink_session_transports (client, session, media);
658 /* remove the session from the watched sessions */
659 g_object_weak_unref (G_OBJECT (session),
660 (GWeakNotify) client_session_finalized, client);
661 priv->sessions = g_list_remove (priv->sessions, session);
663 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
665 /* unmanage the media in the session, returns false if all media session
667 if (!gst_rtsp_session_release_media (session, media)) {
668 /* remove the session */
669 gst_rtsp_session_pool_remove (priv->session_pool, session);
671 /* construct the response now */
672 code = GST_RTSP_STS_OK;
673 gst_rtsp_message_init_response (state->response, code,
674 gst_rtsp_status_as_text (code), state->request);
676 send_response (client, session, state->response, TRUE);
678 /* we emit the signal before closing the connection */
679 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
687 GST_ERROR ("client %p: no session", client);
688 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
693 GST_ERROR ("client %p: no media for uri", client);
694 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
700 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
706 res = gst_rtsp_message_get_body (state->request, &data, &size);
707 if (res != GST_RTSP_OK)
711 /* no body, keep-alive request */
712 send_generic_response (client, GST_RTSP_STS_OK, state);
714 /* there is a body, handle the params */
715 res = gst_rtsp_params_get (client, state);
716 if (res != GST_RTSP_OK)
719 send_response (client, state->session, state->response, FALSE);
722 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
730 GST_ERROR ("client %p: bad request", client);
731 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
737 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
743 res = gst_rtsp_message_get_body (state->request, &data, &size);
744 if (res != GST_RTSP_OK)
748 /* no body, keep-alive request */
749 send_generic_response (client, GST_RTSP_STS_OK, state);
751 /* there is a body, handle the params */
752 res = gst_rtsp_params_set (client, state);
753 if (res != GST_RTSP_OK)
756 send_response (client, state->session, state->response, FALSE);
759 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
767 GST_ERROR ("client %p: bad request", client);
768 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
774 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
776 GstRTSPSession *session;
777 GstRTSPSessionMedia *media;
778 GstRTSPStatusCode code;
779 GstRTSPState rtspstate;
781 if (!(session = state->session))
784 /* get a handle to the configuration of the media in the session */
785 media = gst_rtsp_session_get_media (session, state->uri);
789 state->sessmedia = media;
791 rtspstate = gst_rtsp_session_media_get_rtsp_state (media);
792 /* the session state must be playing or recording */
793 if (rtspstate != GST_RTSP_STATE_PLAYING &&
794 rtspstate != GST_RTSP_STATE_RECORDING)
797 /* unlink the all TCP callbacks */
798 unlink_session_transports (client, session, media);
800 /* then pause sending */
801 gst_rtsp_session_media_set_state (media, GST_STATE_PAUSED);
803 /* construct the response now */
804 code = GST_RTSP_STS_OK;
805 gst_rtsp_message_init_response (state->response, code,
806 gst_rtsp_status_as_text (code), state->request);
808 send_response (client, session, state->response, FALSE);
810 /* the state is now READY */
811 gst_rtsp_session_media_set_rtsp_state (media, GST_RTSP_STATE_READY);
813 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
821 GST_ERROR ("client %p: no seesion", client);
822 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
827 GST_ERROR ("client %p: no media for uri", client);
828 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
833 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
834 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
841 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
843 GstRTSPSession *session;
844 GstRTSPSessionMedia *media;
845 GstRTSPStatusCode code;
847 guint n_streams, i, infocount;
849 GstRTSPTimeRange *range;
851 GstRTSPState rtspstate;
853 if (!(session = state->session))
856 /* get a handle to the configuration of the media in the session */
857 media = gst_rtsp_session_get_media (session, state->uri);
861 state->sessmedia = media;
863 /* the session state must be playing or ready */
864 rtspstate = gst_rtsp_session_media_get_rtsp_state (media);
865 if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
868 /* parse the range header if we have one */
870 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
871 if (res == GST_RTSP_OK) {
872 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
873 /* we have a range, seek to the position */
874 gst_rtsp_media_seek (gst_rtsp_session_media_get_media (media), range);
875 gst_rtsp_range_free (range);
879 /* grab RTPInfo from the payloaders now */
880 rtpinfo = g_string_new ("");
883 gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (media));
884 for (i = 0, infocount = 0; i < n_streams; i++) {
885 GstRTSPStreamTransport *trans;
886 GstRTSPStream *stream;
887 const GstRTSPTransport *tr;
891 /* get the transport, if there is no transport configured, skip this stream */
892 trans = gst_rtsp_session_media_get_transport (media, i);
894 GST_INFO ("stream %d is not configured", i);
897 tr = gst_rtsp_stream_transport_get_transport (trans);
899 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
900 /* for TCP, link the stream to the TCP connection of the client */
901 link_transport (client, session, trans);
904 stream = gst_rtsp_stream_transport_get_stream (trans);
905 if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
907 g_string_append (rtpinfo, ", ");
909 uristr = gst_rtsp_url_get_request_uri (state->uri);
910 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
911 uristr, i, seq, rtptime);
916 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
920 /* construct the response now */
921 code = GST_RTSP_STS_OK;
922 gst_rtsp_message_init_response (state->response, code,
923 gst_rtsp_status_as_text (code), state->request);
925 /* add the RTP-Info header */
927 str = g_string_free (rtpinfo, FALSE);
928 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
930 g_string_free (rtpinfo, TRUE);
935 gst_rtsp_media_get_range_string (gst_rtsp_session_media_get_media (media),
937 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
939 send_response (client, session, state->response, FALSE);
941 /* start playing after sending the request */
942 gst_rtsp_session_media_set_state (media, GST_STATE_PLAYING);
944 gst_rtsp_session_media_set_rtsp_state (media, GST_RTSP_STATE_PLAYING);
946 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
954 GST_ERROR ("client %p: no session", client);
955 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
960 GST_ERROR ("client %p: media not found", client);
961 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
966 GST_ERROR ("client %p: not PLAYING or READY", client);
967 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
974 do_keepalive (GstRTSPSession * session)
976 GST_INFO ("keep session %p alive", session);
977 gst_rtsp_session_touch (session);
980 /* parse @transport and return a valid transport in @tr. only transports
981 * from @supported are returned. Returns FALSE if no valid transport
984 parse_transport (const char *transport, GstRTSPLowerTrans supported,
985 GstRTSPTransport * tr)
992 gst_rtsp_transport_init (tr);
994 GST_DEBUG ("parsing transports %s", transport);
996 transports = g_strsplit (transport, ",", 0);
998 /* loop through the transports, try to parse */
999 for (i = 0; transports[i]; i++) {
1000 res = gst_rtsp_transport_parse (transports[i], tr);
1001 if (res != GST_RTSP_OK) {
1002 /* no valid transport, search some more */
1003 GST_WARNING ("could not parse transport %s", transports[i]);
1007 /* we have a transport, see if it's RTP/AVP */
1008 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1009 GST_WARNING ("invalid transport %s", transports[i]);
1013 if (!(tr->lower_transport & supported)) {
1014 GST_WARNING ("unsupported transport %s", transports[i]);
1018 /* we have a valid transport */
1019 GST_INFO ("found valid transport %s", transports[i]);
1024 gst_rtsp_transport_init (tr);
1026 g_strfreev (transports);
1032 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
1033 GstRTSPMessage * request)
1035 gchar *blocksize_str;
1036 gboolean ret = TRUE;
1038 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1039 &blocksize_str, 0) == GST_RTSP_OK) {
1043 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1044 if (end == blocksize_str) {
1045 GST_ERROR ("failed to parse blocksize");
1048 /* we don't want to change the mtu when this media
1049 * can be shared because it impacts other clients */
1050 if (gst_rtsp_media_is_shared (media))
1053 if (blocksize > G_MAXUINT)
1054 blocksize = G_MAXUINT;
1055 gst_rtsp_stream_set_mtu (stream, blocksize);
1062 configure_client_transport (GstRTSPClient * client, GstRTSPClientState * state,
1063 GstRTSPTransport * ct)
1065 GstRTSPClientPrivate *priv = client->priv;
1067 /* we have a valid transport now, set the destination of the client. */
1068 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1069 if (ct->destination == NULL || !priv->use_client_settings) {
1070 GstRTSPAddress *addr;
1072 addr = gst_rtsp_stream_get_address (state->stream);
1076 g_free (ct->destination);
1077 ct->destination = g_strdup (addr->address);
1078 ct->port.min = addr->port;
1079 ct->port.max = addr->port + addr->n_ports - 1;
1080 ct->ttl = addr->ttl;
1085 url = gst_rtsp_connection_get_url (priv->connection);
1086 g_free (ct->destination);
1087 ct->destination = g_strdup (url->host);
1089 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1090 /* check if the client selected channels for TCP */
1091 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1092 gst_rtsp_session_media_alloc_channels (state->sessmedia,
1102 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1107 static GstRTSPTransport *
1108 make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
1109 GstRTSPTransport * ct)
1111 GstRTSPTransport *st;
1113 /* prepare the server transport */
1114 gst_rtsp_transport_new (&st);
1116 st->trans = ct->trans;
1117 st->profile = ct->profile;
1118 st->lower_transport = ct->lower_transport;
1120 switch (st->lower_transport) {
1121 case GST_RTSP_LOWER_TRANS_UDP:
1122 st->client_port = ct->client_port;
1123 gst_rtsp_stream_get_server_port (state->stream, &st->server_port);
1125 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1126 st->port = ct->port;
1127 st->destination = g_strdup (ct->destination);
1130 case GST_RTSP_LOWER_TRANS_TCP:
1131 st->interleaved = ct->interleaved;
1136 gst_rtsp_stream_get_ssrc (state->stream, &st->ssrc);
1142 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
1144 GstRTSPClientPrivate *priv = client->priv;
1148 GstRTSPTransport *ct, *st;
1149 GstRTSPLowerTrans supported;
1150 GstRTSPStatusCode code;
1151 GstRTSPSession *session;
1152 GstRTSPStreamTransport *trans;
1153 gchar *trans_str, *pos;
1155 GstRTSPSessionMedia *sessmedia;
1156 GstRTSPMedia *media;
1157 GstRTSPStream *stream;
1158 GstRTSPState rtspstate;
1162 /* the uri contains the stream number we added in the SDP config, which is
1163 * always /stream=%d so we need to strip that off
1164 * parse the stream we need to configure, look for the stream in the abspath
1165 * first and then in the query. */
1166 if (uri->abspath == NULL || !(pos = strstr (uri->abspath, "/stream="))) {
1167 if (uri->query == NULL || !(pos = strstr (uri->query, "/stream=")))
1171 /* we can mofify the parsed uri in place */
1174 pos += strlen ("/stream=");
1175 if (sscanf (pos, "%u", &streamid) != 1)
1178 /* parse the transport */
1180 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
1182 if (res != GST_RTSP_OK)
1185 gst_rtsp_transport_new (&ct);
1187 /* our supported transports */
1188 supported = GST_RTSP_LOWER_TRANS_UDP |
1189 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
1191 /* parse and find a usable supported transport */
1192 if (!parse_transport (transport, supported, ct))
1193 goto unsupported_transports;
1195 /* we create the session after parsing stuff so that we don't make
1196 * a session for malformed requests */
1197 if (priv->session_pool == NULL)
1200 session = state->session;
1203 g_object_ref (session);
1204 /* get a handle to the configuration of the media in the session, this can
1205 * return NULL if this is a new url to manage in this session. */
1206 sessmedia = gst_rtsp_session_get_media (session, uri);
1208 /* create a session if this fails we probably reached our session limit or
1210 if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1211 goto service_unavailable;
1213 state->session = session;
1215 /* we need a new media configuration in this session */
1219 /* we have no media, find one and manage it */
1220 if (sessmedia == NULL) {
1221 /* get a handle to the configuration of the media in the session */
1222 if ((media = find_media (client, state))) {
1223 /* manage the media in our session now */
1224 sessmedia = gst_rtsp_session_manage_media (session, uri, media);
1228 /* if we stil have no media, error */
1229 if (sessmedia == NULL)
1232 state->sessmedia = sessmedia;
1233 state->media = media = gst_rtsp_session_media_get_media (sessmedia);
1235 /* now get the stream */
1236 stream = gst_rtsp_media_get_stream (media, streamid);
1240 state->stream = stream;
1242 /* set blocksize on this stream */
1243 if (!handle_blocksize (media, stream, state->request))
1244 goto invalid_blocksize;
1246 /* update the client transport */
1247 if (!configure_client_transport (client, state, ct))
1248 goto unsupported_client_transport;
1250 /* set in the session media transport */
1251 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1253 /* configure keepalive for this transport */
1254 gst_rtsp_stream_transport_set_keepalive (trans,
1255 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1257 /* create and serialize the server transport */
1258 st = make_server_transport (client, state, ct);
1259 trans_str = gst_rtsp_transport_as_text (st);
1260 gst_rtsp_transport_free (st);
1262 /* construct the response now */
1263 code = GST_RTSP_STS_OK;
1264 gst_rtsp_message_init_response (state->response, code,
1265 gst_rtsp_status_as_text (code), state->request);
1267 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1271 send_response (client, session, state->response, FALSE);
1273 /* update the state */
1274 rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1275 switch (rtspstate) {
1276 case GST_RTSP_STATE_PLAYING:
1277 case GST_RTSP_STATE_RECORDING:
1278 case GST_RTSP_STATE_READY:
1279 /* no state change */
1282 gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1285 g_object_unref (session);
1287 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST],
1295 GST_ERROR ("client %p: bad request", client);
1296 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1301 GST_ERROR ("client %p: media not found", client);
1302 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1303 g_object_unref (session);
1304 gst_rtsp_transport_free (ct);
1309 GST_ERROR ("client %p: invalid blocksize", client);
1310 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1311 g_object_unref (session);
1312 gst_rtsp_transport_free (ct);
1315 unsupported_client_transport:
1317 GST_ERROR ("client %p: unsupported client transport", client);
1318 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1319 g_object_unref (session);
1320 gst_rtsp_transport_free (ct);
1325 GST_ERROR ("client %p: no transport", client);
1326 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1329 unsupported_transports:
1331 GST_ERROR ("client %p: unsupported transports", client);
1332 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1333 gst_rtsp_transport_free (ct);
1338 GST_ERROR ("client %p: no session pool configured", client);
1339 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1340 gst_rtsp_transport_free (ct);
1343 service_unavailable:
1345 GST_ERROR ("client %p: can't create session", client);
1346 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1347 gst_rtsp_transport_free (ct);
1352 static GstSDPMessage *
1353 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1355 GstRTSPClientPrivate *priv = client->priv;
1360 gst_sdp_message_new (&sdp);
1362 /* some standard things first */
1363 gst_sdp_message_set_version (sdp, "0");
1370 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1373 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1374 gst_sdp_message_set_information (sdp, "rtsp-server");
1375 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1376 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1377 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1378 gst_sdp_message_add_attribute (sdp, "control", "*");
1380 info.server_proto = proto;
1381 info.server_ip = g_strdup (priv->server_ip);
1383 /* create an SDP for the media object */
1384 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1387 g_free (info.server_ip);
1394 GST_ERROR ("client %p: could not create SDP", client);
1395 g_free (info.server_ip);
1396 gst_sdp_message_free (sdp);
1401 /* for the describe we must generate an SDP */
1403 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1408 gchar *str, *content_base;
1409 GstRTSPMedia *media;
1410 GstRTSPClientClass *klass;
1412 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1414 /* check what kind of format is accepted, we don't really do anything with it
1415 * and always return SDP for now. */
1420 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1422 if (res == GST_RTSP_ENOTIMPL)
1425 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1429 /* find the media object for the uri */
1430 if (!(media = find_media (client, state)))
1433 /* create an SDP for the media object on this client */
1434 if (!(sdp = klass->create_sdp (client, media)))
1437 g_object_unref (media);
1439 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1440 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1442 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1445 /* content base for some clients that might screw up creating the setup uri */
1446 str = gst_rtsp_url_get_request_uri (state->uri);
1447 str_len = strlen (str);
1449 /* check for trailing '/' and append one */
1450 if (str[str_len - 1] != '/') {
1451 content_base = g_malloc (str_len + 2);
1452 memcpy (content_base, str, str_len);
1453 content_base[str_len] = '/';
1454 content_base[str_len + 1] = '\0';
1460 GST_INFO ("adding content-base: %s", content_base);
1462 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1464 g_free (content_base);
1466 /* add SDP to the response body */
1467 str = gst_sdp_message_as_text (sdp);
1468 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1469 gst_sdp_message_free (sdp);
1471 send_response (client, state->session, state->response, FALSE);
1473 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1481 GST_ERROR ("client %p: no media", client);
1482 /* error reply is already sent */
1487 GST_ERROR ("client %p: can't create SDP", client);
1488 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1489 g_object_unref (media);
1495 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1497 GstRTSPMethod options;
1500 options = GST_RTSP_DESCRIBE |
1505 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1507 str = gst_rtsp_options_as_text (options);
1509 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1510 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1512 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1515 send_response (client, state->session, state->response, FALSE);
1517 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1523 /* remove duplicate and trailing '/' */
1525 sanitize_uri (GstRTSPUrl * uri)
1529 gboolean have_slash, prev_slash;
1531 s = d = uri->abspath;
1532 len = strlen (uri->abspath);
1536 for (i = 0; i < len; i++) {
1537 have_slash = s[i] == '/';
1539 if (!have_slash || !prev_slash)
1541 prev_slash = have_slash;
1543 len = d - uri->abspath;
1544 /* don't remove the first slash if that's the only thing left */
1545 if (len > 1 && *(d - 1) == '/')
1551 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1553 GstRTSPClientPrivate *priv = client->priv;
1555 GST_INFO ("client %p: session %p finished", client, session);
1557 /* unlink all media managed in this session */
1558 client_unlink_session (client, session);
1560 /* remove the session */
1561 if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1562 GST_INFO ("client %p: all sessions finalized, close the connection",
1564 close_connection (client);
1569 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
1571 GstRTSPClientPrivate *priv = client->priv;
1574 for (walk = priv->sessions; walk; walk = g_list_next (walk)) {
1575 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
1577 /* we already know about this session */
1578 if (msession == session)
1582 GST_INFO ("watching session %p", session);
1584 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
1586 priv->sessions = g_list_prepend (priv->sessions, session);
1588 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1593 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1595 GstRTSPClientPrivate *priv = client->priv;
1596 GstRTSPMethod method;
1597 const gchar *uristr;
1598 GstRTSPUrl *uri = NULL;
1599 GstRTSPVersion version;
1601 GstRTSPSession *session = NULL;
1602 GstRTSPClientState state = { NULL };
1603 GstRTSPMessage response = { 0 };
1606 state.request = request;
1607 state.response = &response;
1609 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1610 gst_rtsp_message_dump (request);
1613 GST_INFO ("client %p: received a request", client);
1615 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1617 /* we can only handle 1.0 requests */
1618 if (version != GST_RTSP_VERSION_1_0)
1621 state.method = method;
1623 /* we always try to parse the url first */
1624 if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1627 /* get the session if there is any */
1628 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1629 if (res == GST_RTSP_OK) {
1630 if (priv->session_pool == NULL)
1633 /* we had a session in the request, find it again */
1634 if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1635 goto session_not_found;
1637 /* we add the session to the client list of watched sessions. When a session
1638 * disappears because it times out, we will be notified. If all sessions are
1639 * gone, we will close the connection */
1640 client_watch_session (client, session);
1643 /* sanitize the uri */
1646 state.session = session;
1649 if (!gst_rtsp_auth_check (priv->auth, client, 0, &state))
1650 goto not_authorized;
1653 /* now see what is asked and dispatch to a dedicated handler */
1655 case GST_RTSP_OPTIONS:
1656 handle_options_request (client, &state);
1658 case GST_RTSP_DESCRIBE:
1659 handle_describe_request (client, &state);
1661 case GST_RTSP_SETUP:
1662 handle_setup_request (client, &state);
1665 handle_play_request (client, &state);
1667 case GST_RTSP_PAUSE:
1668 handle_pause_request (client, &state);
1670 case GST_RTSP_TEARDOWN:
1671 handle_teardown_request (client, &state);
1673 case GST_RTSP_SET_PARAMETER:
1674 handle_set_param_request (client, &state);
1676 case GST_RTSP_GET_PARAMETER:
1677 handle_get_param_request (client, &state);
1679 case GST_RTSP_ANNOUNCE:
1680 case GST_RTSP_RECORD:
1681 case GST_RTSP_REDIRECT:
1682 goto not_implemented;
1683 case GST_RTSP_INVALID:
1690 g_object_unref (session);
1692 gst_rtsp_url_free (uri);
1698 GST_ERROR ("client %p: version %d not supported", client, version);
1699 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1705 GST_ERROR ("client %p: bad request", client);
1706 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1711 GST_ERROR ("client %p: no pool configured", client);
1712 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1717 GST_ERROR ("client %p: session not found", client);
1718 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1723 GST_ERROR ("client %p: not allowed", client);
1724 handle_unauthorized_request (client, priv->auth, &state);
1729 GST_ERROR ("client %p: method %d not implemented", client, method);
1730 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1736 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1738 GstRTSPClientPrivate *priv = client->priv;
1747 /* find the stream for this message */
1748 res = gst_rtsp_message_parse_data (message, &channel);
1749 if (res != GST_RTSP_OK)
1752 gst_rtsp_message_steal_body (message, &data, &size);
1754 buffer = gst_buffer_new_wrapped (data, size);
1757 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1758 GstRTSPStreamTransport *trans;
1759 GstRTSPStream *stream;
1760 const GstRTSPTransport *tr;
1764 tr = gst_rtsp_stream_transport_get_transport (trans);
1765 stream = gst_rtsp_stream_transport_get_stream (trans);
1767 /* check for TCP transport */
1768 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1769 /* dispatch to the stream based on the channel number */
1770 if (tr->interleaved.min == channel) {
1771 gst_rtsp_stream_recv_rtp (stream, buffer);
1774 } else if (tr->interleaved.max == channel) {
1775 gst_rtsp_stream_recv_rtcp (stream, buffer);
1782 gst_buffer_unref (buffer);
1786 * gst_rtsp_client_set_session_pool:
1787 * @client: a #GstRTSPClient
1788 * @pool: a #GstRTSPSessionPool
1790 * Set @pool as the sessionpool for @client which it will use to find
1791 * or allocate sessions. the sessionpool is usually inherited from the server
1792 * that created the client but can be overridden later.
1795 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
1796 GstRTSPSessionPool * pool)
1798 GstRTSPSessionPool *old;
1799 GstRTSPClientPrivate *priv;
1801 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1803 priv = client->priv;
1806 g_object_ref (pool);
1808 g_mutex_lock (&priv->lock);
1809 old = priv->session_pool;
1810 priv->session_pool = pool;
1811 g_mutex_unlock (&priv->lock);
1814 g_object_unref (old);
1818 * gst_rtsp_client_get_session_pool:
1819 * @client: a #GstRTSPClient
1821 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
1823 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
1825 GstRTSPSessionPool *
1826 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
1828 GstRTSPClientPrivate *priv;
1829 GstRTSPSessionPool *result;
1831 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1833 priv = client->priv;
1835 g_mutex_lock (&priv->lock);
1836 if ((result = priv->session_pool))
1837 g_object_ref (result);
1838 g_mutex_unlock (&priv->lock);
1844 * gst_rtsp_client_set_mount_points:
1845 * @client: a #GstRTSPClient
1846 * @mounts: a #GstRTSPMountPoints
1848 * Set @mounts as the mount points for @client which it will use to map urls
1849 * to media streams. These mount points are usually inherited from the server that
1850 * created the client but can be overriden later.
1853 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
1854 GstRTSPMountPoints * mounts)
1856 GstRTSPClientPrivate *priv;
1857 GstRTSPMountPoints *old;
1859 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1861 priv = client->priv;
1864 g_object_ref (mounts);
1866 g_mutex_lock (&priv->lock);
1867 old = priv->mount_points;
1868 priv->mount_points = mounts;
1869 g_mutex_unlock (&priv->lock);
1872 g_object_unref (old);
1876 * gst_rtsp_client_get_mount_points:
1877 * @client: a #GstRTSPClient
1879 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
1881 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
1883 GstRTSPMountPoints *
1884 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
1886 GstRTSPClientPrivate *priv;
1887 GstRTSPMountPoints *result;
1889 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1891 priv = client->priv;
1893 g_mutex_lock (&priv->lock);
1894 if ((result = priv->mount_points))
1895 g_object_ref (result);
1896 g_mutex_unlock (&priv->lock);
1902 * gst_rtsp_client_set_use_client_settings:
1903 * @client: a #GstRTSPClient
1904 * @use_client_settings: whether to use client settings for multicast
1906 * Use client transport settings (destination and ttl) for multicast.
1907 * When @use_client_settings is %FALSE, the server settings will be
1911 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
1912 gboolean use_client_settings)
1914 GstRTSPClientPrivate *priv;
1916 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1918 priv = client->priv;
1920 g_mutex_lock (&priv->lock);
1921 priv->use_client_settings = use_client_settings;
1922 g_mutex_unlock (&priv->lock);
1926 * gst_rtsp_client_get_use_client_settings:
1927 * @client: a #GstRTSPClient
1929 * Check if client transport settings (destination and ttl) for multicast
1933 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
1935 GstRTSPClientPrivate *priv;
1938 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
1940 priv = client->priv;
1942 g_mutex_lock (&priv->lock);
1943 res = priv->use_client_settings;
1944 g_mutex_unlock (&priv->lock);
1950 * gst_rtsp_client_set_auth:
1951 * @client: a #GstRTSPClient
1952 * @auth: a #GstRTSPAuth
1954 * configure @auth to be used as the authentication manager of @client.
1957 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
1959 GstRTSPClientPrivate *priv;
1962 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1964 priv = client->priv;
1967 g_object_ref (auth);
1969 g_mutex_lock (&priv->lock);
1972 g_mutex_unlock (&priv->lock);
1975 g_object_unref (old);
1980 * gst_rtsp_client_get_auth:
1981 * @client: a #GstRTSPClient
1983 * Get the #GstRTSPAuth used as the authentication manager of @client.
1985 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
1989 gst_rtsp_client_get_auth (GstRTSPClient * client)
1991 GstRTSPClientPrivate *priv;
1992 GstRTSPAuth *result;
1994 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1996 priv = client->priv;
1998 g_mutex_lock (&priv->lock);
1999 if ((result = priv->auth))
2000 g_object_ref (result);
2001 g_mutex_unlock (&priv->lock);
2007 * gst_rtsp_client_set_send_func:
2008 * @client: a #GstRTSPClient
2009 * @func: a #GstRTSPClientSendFunc
2010 * @user_data: user data passed to @func
2011 * @notify: called when @user_data is no longer in use
2013 * Set @func as the callback that will be called when a new message needs to be
2014 * sent to the client. @user_data is passed to @func and @notify is called when
2015 * @user_data is no longer in use.
2018 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2019 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2021 GstRTSPClientPrivate *priv;
2022 GDestroyNotify old_notify;
2025 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2027 priv = client->priv;
2029 g_mutex_lock (&priv->lock);
2030 priv->send_func = func;
2031 old_notify = priv->send_notify;
2032 old_data = priv->send_data;
2033 priv->send_notify = notify;
2034 priv->send_data = user_data;
2035 g_mutex_unlock (&priv->lock);
2038 old_notify (old_data);
2042 * gst_rtsp_client_handle_message:
2043 * @client: a #GstRTSPClient
2044 * @message: an #GstRTSPMessage
2046 * Let the client handle @message.
2048 * Returns: a #GstRTSPResult.
2051 gst_rtsp_client_handle_message (GstRTSPClient * client,
2052 GstRTSPMessage * message)
2054 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2055 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2057 switch (message->type) {
2058 case GST_RTSP_MESSAGE_REQUEST:
2059 handle_request (client, message);
2061 case GST_RTSP_MESSAGE_RESPONSE:
2063 case GST_RTSP_MESSAGE_DATA:
2064 handle_data (client, message);
2072 static GstRTSPResult
2073 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2074 gboolean close, gpointer user_data)
2076 GstRTSPClientPrivate *priv = client->priv;
2078 /* send the response and store the seq number so we can wait until it's
2079 * written to the client to close the connection */
2080 return gst_rtsp_watch_send_message (priv->watch, message, close ?
2081 &priv->close_seq : NULL);
2084 static GstRTSPResult
2085 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2088 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2091 static GstRTSPResult
2092 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2094 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2095 GstRTSPClientPrivate *priv = client->priv;
2097 if (priv->close_seq && priv->close_seq == cseq) {
2098 priv->close_seq = 0;
2099 close_connection (client);
2105 static GstRTSPResult
2106 closed (GstRTSPWatch * watch, gpointer user_data)
2108 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2109 GstRTSPClientPrivate *priv = client->priv;
2110 const gchar *tunnelid;
2112 GST_INFO ("client %p: connection closed", client);
2114 if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2115 g_mutex_lock (&tunnels_lock);
2116 /* remove from tunnelids */
2117 g_hash_table_remove (tunnels, tunnelid);
2118 g_mutex_unlock (&tunnels_lock);
2124 static GstRTSPResult
2125 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2127 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2130 str = gst_rtsp_strresult (result);
2131 GST_INFO ("client %p: received an error %s", client, str);
2137 static GstRTSPResult
2138 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2139 GstRTSPMessage * message, guint id, gpointer user_data)
2141 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2144 str = gst_rtsp_strresult (result);
2146 ("client %p: received an error %s when handling message %p with id %d",
2147 client, str, message, id);
2154 remember_tunnel (GstRTSPClient * client)
2156 GstRTSPClientPrivate *priv = client->priv;
2157 const gchar *tunnelid;
2159 /* store client in the pending tunnels */
2160 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2161 if (tunnelid == NULL)
2164 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2166 /* we can't have two clients connecting with the same tunnelid */
2167 g_mutex_lock (&tunnels_lock);
2168 if (g_hash_table_lookup (tunnels, tunnelid))
2169 goto tunnel_existed;
2171 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2172 g_mutex_unlock (&tunnels_lock);
2179 GST_ERROR ("client %p: no tunnelid provided", client);
2184 g_mutex_unlock (&tunnels_lock);
2185 GST_ERROR ("client %p: tunnel session %s already existed", client,
2191 static GstRTSPStatusCode
2192 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2194 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2195 GstRTSPClientPrivate *priv = client->priv;
2197 GST_INFO ("client %p: tunnel start (connection %p)", client,
2200 if (!remember_tunnel (client))
2203 return GST_RTSP_STS_OK;
2208 GST_ERROR ("client %p: error starting tunnel", client);
2209 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2213 static GstRTSPResult
2214 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2216 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2217 GstRTSPClientPrivate *priv = client->priv;
2219 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2222 /* ignore error, it'll only be a problem when the client does a POST again */
2223 remember_tunnel (client);
2228 static GstRTSPResult
2229 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2231 const gchar *tunnelid;
2232 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2233 GstRTSPClientPrivate *priv = client->priv;
2234 GstRTSPClient *oclient;
2235 GstRTSPClientPrivate *opriv;
2237 GST_INFO ("client %p: tunnel complete", client);
2239 /* find previous tunnel */
2240 tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2241 if (tunnelid == NULL)
2244 g_mutex_lock (&tunnels_lock);
2245 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2248 /* remove the old client from the table. ref before because removing it will
2249 * remove the ref to it. */
2250 g_object_ref (oclient);
2251 g_hash_table_remove (tunnels, tunnelid);
2253 opriv = oclient->priv;
2255 if (opriv->watch == NULL)
2257 g_mutex_unlock (&tunnels_lock);
2259 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2260 opriv->connection, priv->connection);
2262 /* merge the tunnels into the first client */
2263 gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2264 gst_rtsp_watch_reset (opriv->watch);
2265 g_object_unref (oclient);
2272 GST_ERROR ("client %p: no tunnelid provided", client);
2273 return GST_RTSP_ERROR;
2277 g_mutex_unlock (&tunnels_lock);
2278 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2279 return GST_RTSP_ERROR;
2283 g_mutex_unlock (&tunnels_lock);
2284 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2285 g_object_unref (oclient);
2286 return GST_RTSP_ERROR;
2290 static GstRTSPWatchFuncs watch_funcs = {
2302 client_watch_notify (GstRTSPClient * client)
2304 GstRTSPClientPrivate *priv = client->priv;
2306 GST_INFO ("client %p: watch destroyed", client);
2308 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2309 g_object_unref (client);
2313 setup_client (GstRTSPClient * client, GSocket * socket,
2314 GstRTSPConnection * conn, GError ** error)
2316 GstRTSPClientPrivate *priv = client->priv;
2317 GSocket *read_socket;
2318 GSocketAddress *address;
2321 read_socket = gst_rtsp_connection_get_read_socket (conn);
2322 priv->is_ipv6 = g_socket_get_family (socket) == G_SOCKET_FAMILY_IPV6;
2324 if (!(address = g_socket_get_remote_address (read_socket, error)))
2327 g_free (priv->server_ip);
2328 /* keep the original ip that the client connected to */
2329 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2330 GInetAddress *iaddr;
2332 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2334 priv->server_ip = g_inet_address_to_string (iaddr);
2335 g_object_unref (address);
2337 priv->server_ip = g_strdup ("unknown");
2340 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2341 priv->server_ip, priv->is_ipv6);
2343 url = gst_rtsp_connection_get_url (conn);
2344 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2346 priv->connection = conn;
2353 GST_ERROR ("could not get remote address %s", (*error)->message);
2359 * gst_rtsp_client_use_socket:
2360 * @client: a #GstRTSPClient
2361 * @socket: a #GSocket
2362 * @ip: the IP address of the remote client
2363 * @port: the port used by the other end
2364 * @initial_buffer: any zero terminated initial data that was already read from
2368 * Take an existing network socket and use it for an RTSP connection.
2370 * Returns: %TRUE on success.
2373 gst_rtsp_client_use_socket (GstRTSPClient * client, GSocket * socket,
2374 const gchar * ip, gint port, const gchar * initial_buffer, GError ** error)
2376 GstRTSPConnection *conn;
2379 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2380 g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
2382 GST_RTSP_CHECK (gst_rtsp_connection_create_from_socket (socket, ip, port,
2383 initial_buffer, &conn), no_connection);
2385 return setup_client (client, socket, conn, error);
2390 gchar *str = gst_rtsp_strresult (res);
2392 GST_ERROR ("could not create connection from socket %p: %s", socket, str);
2399 * gst_rtsp_client_accept:
2400 * @client: a #GstRTSPClient
2401 * @socket: a #GSocket
2402 * @context: the context to run in
2403 * @cancellable: a #GCancellable
2406 * Accept a new connection for @client on @socket.
2408 * Returns: %TRUE if the client could be accepted.
2411 gst_rtsp_client_accept (GstRTSPClient * client, GSocket * socket,
2412 GCancellable * cancellable, GError ** error)
2414 GstRTSPConnection *conn;
2417 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2418 g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
2420 /* a new client connected. */
2421 GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, cancellable),
2424 return setup_client (client, socket, conn, error);
2429 gchar *str = gst_rtsp_strresult (res);
2431 GST_ERROR ("Could not accept client on server socket %p: %s", socket, str);
2438 * gst_rtsp_client_attach:
2439 * @client: a #GstRTSPClient
2440 * @context: (allow-none): a #GMainContext
2442 * Attaches @client to @context. When the mainloop for @context is run, the
2443 * client will be dispatched. When @context is NULL, the default context will be
2446 * This function should be called when the client properties and urls are fully
2447 * configured and the client is ready to start.
2449 * Returns: the ID (greater than 0) for the source within the GMainContext.
2452 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2454 GstRTSPClientPrivate *priv;
2457 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2458 priv = client->priv;
2459 g_return_val_if_fail (priv->watch == NULL, 0);
2461 /* create watch for the connection and attach */
2462 priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2463 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2464 gst_rtsp_client_set_send_func (client, do_send_message, NULL, NULL);
2466 GST_INFO ("attaching to context %p", context);
2467 res = gst_rtsp_watch_attach (priv->watch, context);
2468 gst_rtsp_watch_unref (priv->watch);