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 static GMutex tunnels_lock;
28 static GHashTable *tunnels;
30 #define DEFAULT_SESSION_POOL NULL
31 #define DEFAULT_MOUNT_POINTS NULL
32 #define DEFAULT_USE_CLIENT_SETTINGS FALSE
39 PROP_USE_CLIENT_SETTINGS,
47 SIGNAL_OPTIONS_REQUEST,
48 SIGNAL_DESCRIBE_REQUEST,
52 SIGNAL_TEARDOWN_REQUEST,
53 SIGNAL_SET_PARAMETER_REQUEST,
54 SIGNAL_GET_PARAMETER_REQUEST,
58 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
59 #define GST_CAT_DEFAULT rtsp_client_debug
61 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
63 static void gst_rtsp_client_get_property (GObject * object, guint propid,
64 GValue * value, GParamSpec * pspec);
65 static void gst_rtsp_client_set_property (GObject * object, guint propid,
66 const GValue * value, GParamSpec * pspec);
67 static void gst_rtsp_client_finalize (GObject * obj);
69 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
70 static void client_session_finalized (GstRTSPClient * client,
71 GstRTSPSession * session);
72 static void unlink_session_transports (GstRTSPClient * client,
73 GstRTSPSession * session, GstRTSPSessionMedia * media);
75 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
78 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
80 GObjectClass *gobject_class;
82 gobject_class = G_OBJECT_CLASS (klass);
84 gobject_class->get_property = gst_rtsp_client_get_property;
85 gobject_class->set_property = gst_rtsp_client_set_property;
86 gobject_class->finalize = gst_rtsp_client_finalize;
88 klass->create_sdp = create_sdp;
90 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
91 g_param_spec_object ("session-pool", "Session Pool",
92 "The session pool to use for client session",
93 GST_TYPE_RTSP_SESSION_POOL,
94 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
96 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
97 g_param_spec_object ("mount-points", "Mount Points",
98 "The mount points to use for client session",
99 GST_TYPE_RTSP_MOUNT_POINTS,
100 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
102 g_object_class_install_property (gobject_class, PROP_USE_CLIENT_SETTINGS,
103 g_param_spec_boolean ("use-client-settings", "Use Client Settings",
104 "Use client settings for ttl and destination in multicast",
105 DEFAULT_USE_CLIENT_SETTINGS,
106 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
108 gst_rtsp_client_signals[SIGNAL_CLOSED] =
109 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
110 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
111 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
113 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
114 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
115 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
116 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
118 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
119 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
120 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
121 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
124 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
125 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
126 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
127 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
130 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
131 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
132 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
133 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
136 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
137 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
138 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
139 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
142 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
143 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
144 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
145 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
148 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
149 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
150 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
151 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
154 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
155 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
156 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
157 set_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
158 G_TYPE_NONE, 1, G_TYPE_POINTER);
160 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
161 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
162 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
163 get_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
164 G_TYPE_NONE, 1, G_TYPE_POINTER);
167 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
168 g_mutex_init (&tunnels_lock);
170 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
174 gst_rtsp_client_init (GstRTSPClient * client)
176 g_mutex_init (&client->lock);
177 client->use_client_settings = DEFAULT_USE_CLIENT_SETTINGS;
178 client->close_response_seq = 0;
182 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
184 /* unlink all media managed in this session */
185 while (session->medias) {
186 GstRTSPSessionMedia *media = session->medias->data;
188 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
189 unlink_session_transports (client, session, media);
190 /* unmanage the media in the session. this will modify session->medias */
191 gst_rtsp_session_release_media (session, media);
196 client_cleanup_sessions (GstRTSPClient * client)
200 /* remove weak-ref from sessions */
201 for (sessions = client->sessions; sessions; sessions = g_list_next (sessions)) {
202 GstRTSPSession *session = (GstRTSPSession *) sessions->data;
203 g_object_weak_unref (G_OBJECT (session),
204 (GWeakNotify) client_session_finalized, client);
205 client_unlink_session (client, session);
207 g_list_free (client->sessions);
208 client->sessions = NULL;
211 /* A client is finalized when the connection is broken */
213 gst_rtsp_client_finalize (GObject * obj)
215 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
217 GST_INFO ("finalize client %p", client);
220 g_source_destroy ((GSource *) client->watch);
222 client_cleanup_sessions (client);
224 gst_rtsp_connection_free (client->connection);
225 if (client->session_pool)
226 g_object_unref (client->session_pool);
227 if (client->mount_points)
228 g_object_unref (client->mount_points);
230 g_object_unref (client->auth);
233 gst_rtsp_url_free (client->uri);
235 gst_rtsp_media_unprepare (client->media);
236 g_object_unref (client->media);
239 g_free (client->server_ip);
240 g_mutex_clear (&client->lock);
242 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
246 gst_rtsp_client_get_property (GObject * object, guint propid,
247 GValue * value, GParamSpec * pspec)
249 GstRTSPClient *client = GST_RTSP_CLIENT (object);
252 case PROP_SESSION_POOL:
253 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
255 case PROP_MOUNT_POINTS:
256 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
258 case PROP_USE_CLIENT_SETTINGS:
259 g_value_set_boolean (value,
260 gst_rtsp_client_get_use_client_settings (client));
263 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
268 gst_rtsp_client_set_property (GObject * object, guint propid,
269 const GValue * value, GParamSpec * pspec)
271 GstRTSPClient *client = GST_RTSP_CLIENT (object);
274 case PROP_SESSION_POOL:
275 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
277 case PROP_MOUNT_POINTS:
278 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
280 case PROP_USE_CLIENT_SETTINGS:
281 gst_rtsp_client_set_use_client_settings (client,
282 g_value_get_boolean (value));
285 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
290 * gst_rtsp_client_new:
292 * Create a new #GstRTSPClient instance.
294 * Returns: a new #GstRTSPClient
297 gst_rtsp_client_new (void)
299 GstRTSPClient *result;
301 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
307 send_response (GstRTSPClient * client, GstRTSPSession * session,
308 GstRTSPMessage * response, gboolean close)
310 gst_rtsp_message_add_header (response, GST_RTSP_HDR_SERVER,
311 "GStreamer RTSP server");
313 /* remove any previous header */
314 gst_rtsp_message_remove_header (response, GST_RTSP_HDR_SESSION, -1);
316 /* add the new session header for new session ids */
318 gst_rtsp_message_take_header (response, GST_RTSP_HDR_SESSION,
319 gst_rtsp_session_get_header (session));
322 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
323 gst_rtsp_message_dump (response);
327 gst_rtsp_message_add_header (response, GST_RTSP_HDR_CONNECTION, "close");
329 /* send the response and store the seq number so we can wait until it's
330 * written to the client to close the connection */
331 gst_rtsp_watch_send_message (client->watch, response, close ?
332 &client->close_response_seq : NULL);
333 gst_rtsp_message_unset (response);
337 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
338 GstRTSPClientState * state)
340 gst_rtsp_message_init_response (state->response, code,
341 gst_rtsp_status_as_text (code), state->request);
343 send_response (client, NULL, state->response, FALSE);
347 handle_unauthorized_request (GstRTSPClient * client, GstRTSPAuth * auth,
348 GstRTSPClientState * state)
350 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_UNAUTHORIZED,
351 gst_rtsp_status_as_text (GST_RTSP_STS_UNAUTHORIZED), state->request);
354 /* and let the authentication manager setup the auth tokens */
355 gst_rtsp_auth_setup_auth (auth, client, 0, state);
358 send_response (client, state->session, state->response, FALSE);
363 compare_uri (const GstRTSPUrl * uri1, const GstRTSPUrl * uri2)
365 if (uri1 == NULL || uri2 == NULL)
368 if (strcmp (uri1->abspath, uri2->abspath))
374 /* this function is called to initially find the media for the DESCRIBE request
375 * but is cached for when the same client (without breaking the connection) is
376 * doing a setup for the exact same url. */
377 static GstRTSPMedia *
378 find_media (GstRTSPClient * client, GstRTSPClientState * state)
380 GstRTSPMediaFactory *factory;
384 if (!compare_uri (client->uri, state->uri)) {
385 /* remove any previously cached values before we try to construct a new
388 gst_rtsp_url_free (client->uri);
391 gst_rtsp_media_unprepare (client->media);
392 g_object_unref (client->media);
394 client->media = NULL;
396 if (!client->mount_points)
397 goto no_mount_points;
399 /* find the factory for the uri first */
401 gst_rtsp_mount_points_find_factory (client->mount_points,
405 state->factory = factory;
407 /* check if we have access to the factory */
408 if ((auth = gst_rtsp_media_factory_get_auth (factory))) {
409 if (!gst_rtsp_auth_check (auth, client, 0, state))
412 g_object_unref (auth);
415 /* prepare the media and add it to the pipeline */
416 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
419 g_object_unref (factory);
421 state->factory = NULL;
423 /* set ipv6 on the media before preparing */
424 media->is_ipv6 = client->is_ipv6;
425 state->media = media;
427 /* prepare the media */
428 if (!(gst_rtsp_media_prepare (media)))
431 /* now keep track of the uri and the media */
432 client->uri = gst_rtsp_url_copy (state->uri);
433 client->media = media;
435 /* we have seen this uri before, used cached media */
436 media = client->media;
437 state->media = media;
438 GST_INFO ("reusing cached media %p", media);
442 g_object_ref (media);
449 GST_ERROR ("client %p: no mount points configured", client);
450 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
455 GST_ERROR ("client %p: no factory for uri", client);
456 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
461 GST_ERROR ("client %p: unauthorized request", client);
462 handle_unauthorized_request (client, auth, state);
463 g_object_unref (factory);
464 g_object_unref (auth);
469 GST_ERROR ("client %p: can't create media", client);
470 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
471 g_object_unref (factory);
476 GST_ERROR ("client %p: can't prepare media", client);
477 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
478 g_object_unref (media);
484 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
486 GstRTSPMessage message = { 0 };
491 gst_rtsp_message_init_data (&message, channel);
493 /* FIXME, need some sort of iovec RTSPMessage here */
494 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
497 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
499 /* FIXME, client->watch could have been finalized here, we need to keep an
500 * extra refcount to the watch. */
501 gst_rtsp_watch_send_message (client->watch, &message, NULL);
503 gst_rtsp_message_steal_body (&message, &data, &usize);
504 gst_buffer_unmap (buffer, &map_info);
506 gst_rtsp_message_unset (&message);
512 link_transport (GstRTSPClient * client, GstRTSPSession * session,
513 GstRTSPStreamTransport * trans)
515 GST_DEBUG ("client %p: linking transport %p", client, trans);
516 gst_rtsp_stream_transport_set_callbacks (trans,
517 (GstRTSPSendFunc) do_send_data,
518 (GstRTSPSendFunc) do_send_data, client, NULL);
520 client->transports = g_list_prepend (client->transports, trans);
522 /* make sure our session can't expire */
523 gst_rtsp_session_prevent_expire (session);
527 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
528 GstRTSPStreamTransport * trans)
530 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
531 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
533 client->transports = g_list_remove (client->transports, trans);
535 /* our session can now expire */
536 gst_rtsp_session_allow_expire (session);
540 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
541 GstRTSPSessionMedia * media)
545 n_streams = gst_rtsp_media_n_streams (media->media);
546 for (i = 0; i < n_streams; i++) {
547 GstRTSPStreamTransport *trans;
548 GstRTSPTransport *tr;
550 /* get the transport, if there is no transport configured, skip this stream */
551 trans = gst_rtsp_session_media_get_transport (media, i);
555 tr = trans->transport;
557 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
558 /* for TCP, unlink the stream from the TCP connection of the client */
559 unlink_transport (client, session, trans);
565 close_connection (GstRTSPClient * client)
567 const gchar *tunnelid;
569 GST_DEBUG ("client %p: closing connection", client);
571 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
572 g_mutex_lock (&tunnels_lock);
573 /* remove from tunnelids */
574 g_hash_table_remove (tunnels, tunnelid);
575 g_mutex_unlock (&tunnels_lock);
578 gst_rtsp_connection_close (client->connection);
582 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
584 GstRTSPSession *session;
585 GstRTSPSessionMedia *media;
586 GstRTSPStatusCode code;
591 session = state->session;
593 /* get a handle to the configuration of the media in the session */
594 media = gst_rtsp_session_get_media (session, state->uri);
598 state->sessmedia = media;
600 /* unlink the all TCP callbacks */
601 unlink_session_transports (client, session, media);
603 /* remove the session from the watched sessions */
604 g_object_weak_unref (G_OBJECT (session),
605 (GWeakNotify) client_session_finalized, client);
606 client->sessions = g_list_remove (client->sessions, session);
608 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
610 /* unmanage the media in the session, returns false if all media session
612 if (!gst_rtsp_session_release_media (session, media)) {
613 /* remove the session */
614 gst_rtsp_session_pool_remove (client->session_pool, session);
616 /* construct the response now */
617 code = GST_RTSP_STS_OK;
618 gst_rtsp_message_init_response (state->response, code,
619 gst_rtsp_status_as_text (code), state->request);
621 send_response (client, session, state->response, TRUE);
623 /* we emit the signal before closing the connection */
624 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
632 GST_ERROR ("client %p: no session", client);
633 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
638 GST_ERROR ("client %p: no media for uri", client);
639 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
645 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
651 res = gst_rtsp_message_get_body (state->request, &data, &size);
652 if (res != GST_RTSP_OK)
656 /* no body, keep-alive request */
657 send_generic_response (client, GST_RTSP_STS_OK, state);
659 /* there is a body, handle the params */
660 res = gst_rtsp_params_get (client, state);
661 if (res != GST_RTSP_OK)
664 send_response (client, state->session, state->response, FALSE);
667 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
675 GST_ERROR ("client %p: bad request", client);
676 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
682 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
688 res = gst_rtsp_message_get_body (state->request, &data, &size);
689 if (res != GST_RTSP_OK)
693 /* no body, keep-alive request */
694 send_generic_response (client, GST_RTSP_STS_OK, state);
696 /* there is a body, handle the params */
697 res = gst_rtsp_params_set (client, state);
698 if (res != GST_RTSP_OK)
701 send_response (client, state->session, state->response, FALSE);
704 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
712 GST_ERROR ("client %p: bad request", client);
713 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
719 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
721 GstRTSPSession *session;
722 GstRTSPSessionMedia *media;
723 GstRTSPStatusCode code;
725 if (!(session = state->session))
728 /* get a handle to the configuration of the media in the session */
729 media = gst_rtsp_session_get_media (session, state->uri);
733 state->sessmedia = media;
735 /* the session state must be playing or recording */
736 if (media->state != GST_RTSP_STATE_PLAYING &&
737 media->state != GST_RTSP_STATE_RECORDING)
740 /* unlink the all TCP callbacks */
741 unlink_session_transports (client, session, media);
743 /* then pause sending */
744 gst_rtsp_session_media_set_state (media, GST_STATE_PAUSED);
746 /* construct the response now */
747 code = GST_RTSP_STS_OK;
748 gst_rtsp_message_init_response (state->response, code,
749 gst_rtsp_status_as_text (code), state->request);
751 send_response (client, session, state->response, FALSE);
753 /* the state is now READY */
754 media->state = GST_RTSP_STATE_READY;
756 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
764 GST_ERROR ("client %p: no seesion", client);
765 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
770 GST_ERROR ("client %p: no media for uri", client);
771 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
776 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
777 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
784 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
786 GstRTSPSession *session;
787 GstRTSPSessionMedia *media;
788 GstRTSPStatusCode code;
790 guint n_streams, i, infocount;
792 GstRTSPTimeRange *range;
795 if (!(session = state->session))
798 /* get a handle to the configuration of the media in the session */
799 media = gst_rtsp_session_get_media (session, state->uri);
803 state->sessmedia = media;
805 /* the session state must be playing or ready */
806 if (media->state != GST_RTSP_STATE_PLAYING &&
807 media->state != GST_RTSP_STATE_READY)
810 /* parse the range header if we have one */
812 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
813 if (res == GST_RTSP_OK) {
814 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
815 /* we have a range, seek to the position */
816 gst_rtsp_media_seek (media->media, range);
817 gst_rtsp_range_free (range);
821 /* grab RTPInfo from the payloaders now */
822 rtpinfo = g_string_new ("");
824 n_streams = gst_rtsp_media_n_streams (media->media);
825 for (i = 0, infocount = 0; i < n_streams; i++) {
826 GstRTSPStreamTransport *trans;
827 GstRTSPTransport *tr;
831 /* get the transport, if there is no transport configured, skip this stream */
832 trans = gst_rtsp_session_media_get_transport (media, i);
834 GST_INFO ("stream %d is not configured", i);
837 tr = trans->transport;
839 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
840 /* for TCP, link the stream to the TCP connection of the client */
841 link_transport (client, session, trans);
844 if (gst_rtsp_stream_get_rtpinfo (trans->stream, &rtptime, &seq)) {
846 g_string_append (rtpinfo, ", ");
848 uristr = gst_rtsp_url_get_request_uri (state->uri);
849 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
850 uristr, i, seq, rtptime);
855 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
859 /* construct the response now */
860 code = GST_RTSP_STS_OK;
861 gst_rtsp_message_init_response (state->response, code,
862 gst_rtsp_status_as_text (code), state->request);
864 /* add the RTP-Info header */
866 str = g_string_free (rtpinfo, FALSE);
867 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
869 g_string_free (rtpinfo, TRUE);
873 str = gst_rtsp_media_get_range_string (media->media, TRUE);
874 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
876 send_response (client, session, state->response, FALSE);
878 /* start playing after sending the request */
879 gst_rtsp_session_media_set_state (media, GST_STATE_PLAYING);
881 media->state = GST_RTSP_STATE_PLAYING;
883 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
891 GST_ERROR ("client %p: no session", client);
892 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
897 GST_ERROR ("client %p: media not found", client);
898 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
903 GST_ERROR ("client %p: not PLAYING or READY", client);
904 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
911 do_keepalive (GstRTSPSession * session)
913 GST_INFO ("keep session %p alive", session);
914 gst_rtsp_session_touch (session);
917 /* parse @transport and return a valid transport in @tr. only transports
918 * from @supported are returned. Returns FALSE if no valid transport
921 parse_transport (const char *transport, GstRTSPLowerTrans supported,
922 GstRTSPTransport * tr)
929 gst_rtsp_transport_init (tr);
931 GST_DEBUG ("parsing transports %s", transport);
933 transports = g_strsplit (transport, ",", 0);
935 /* loop through the transports, try to parse */
936 for (i = 0; transports[i]; i++) {
937 res = gst_rtsp_transport_parse (transports[i], tr);
938 if (res != GST_RTSP_OK) {
939 /* no valid transport, search some more */
940 GST_WARNING ("could not parse transport %s", transports[i]);
944 /* we have a transport, see if it's RTP/AVP */
945 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
946 GST_WARNING ("invalid transport %s", transports[i]);
950 if (!(tr->lower_transport & supported)) {
951 GST_WARNING ("unsupported transport %s", transports[i]);
955 /* we have a valid transport */
956 GST_INFO ("found valid transport %s", transports[i]);
961 gst_rtsp_transport_init (tr);
963 g_strfreev (transports);
969 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
970 GstRTSPMessage * request)
972 gchar *blocksize_str;
975 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
976 &blocksize_str, 0) == GST_RTSP_OK) {
980 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
981 if (end == blocksize_str) {
982 GST_ERROR ("failed to parse blocksize");
985 /* we don't want to change the mtu when this media
986 * can be shared because it impacts other clients */
987 if (gst_rtsp_media_is_shared (media))
990 if (blocksize > G_MAXUINT)
991 blocksize = G_MAXUINT;
992 gst_rtsp_stream_set_mtu (stream, blocksize);
999 configure_client_transport (GstRTSPClient * client, GstRTSPClientState * state,
1000 GstRTSPTransport * ct)
1002 /* we have a valid transport now, set the destination of the client. */
1003 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1004 if (ct->destination == NULL || !client->use_client_settings) {
1005 GstRTSPAddress *addr;
1007 addr = gst_rtsp_stream_get_address (state->stream);
1011 g_free (ct->destination);
1012 ct->destination = g_strdup (addr->address);
1013 ct->port.min = addr->port;
1014 ct->port.max = addr->port + addr->n_ports - 1;
1015 ct->ttl = addr->ttl;
1020 url = gst_rtsp_connection_get_url (client->connection);
1021 g_free (ct->destination);
1022 ct->destination = g_strdup (url->host);
1024 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1025 /* check if the client selected channels for TCP */
1026 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1027 gst_rtsp_session_media_alloc_channels (state->sessmedia,
1037 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1042 static GstRTSPTransport *
1043 make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
1044 GstRTSPTransport * ct)
1046 GstRTSPTransport *st;
1048 /* prepare the server transport */
1049 gst_rtsp_transport_new (&st);
1051 st->trans = ct->trans;
1052 st->profile = ct->profile;
1053 st->lower_transport = ct->lower_transport;
1055 switch (st->lower_transport) {
1056 case GST_RTSP_LOWER_TRANS_UDP:
1057 st->client_port = ct->client_port;
1058 st->server_port = state->stream->server_port;
1060 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1061 st->port = ct->port;
1062 st->destination = g_strdup (ct->destination);
1065 case GST_RTSP_LOWER_TRANS_TCP:
1066 st->interleaved = ct->interleaved;
1071 if (state->stream->session)
1072 g_object_get (state->stream->session, "internal-ssrc", &st->ssrc, NULL);
1078 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
1083 GstRTSPTransport *ct, *st;
1084 GstRTSPLowerTrans supported;
1085 GstRTSPStatusCode code;
1086 GstRTSPSession *session;
1087 GstRTSPStreamTransport *trans;
1088 gchar *trans_str, *pos;
1090 GstRTSPSessionMedia *sessmedia;
1091 GstRTSPMedia *media;
1092 GstRTSPStream *stream;
1096 /* the uri contains the stream number we added in the SDP config, which is
1097 * always /stream=%d so we need to strip that off
1098 * parse the stream we need to configure, look for the stream in the abspath
1099 * first and then in the query. */
1100 if (uri->abspath == NULL || !(pos = strstr (uri->abspath, "/stream="))) {
1101 if (uri->query == NULL || !(pos = strstr (uri->query, "/stream=")))
1105 /* we can mofify the parsed uri in place */
1108 pos += strlen ("/stream=");
1109 if (sscanf (pos, "%u", &streamid) != 1)
1112 /* parse the transport */
1114 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
1116 if (res != GST_RTSP_OK)
1119 gst_rtsp_transport_new (&ct);
1121 /* our supported transports */
1122 supported = GST_RTSP_LOWER_TRANS_UDP |
1123 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
1125 /* parse and find a usable supported transport */
1126 if (!parse_transport (transport, supported, ct))
1127 goto unsupported_transports;
1129 /* we create the session after parsing stuff so that we don't make
1130 * a session for malformed requests */
1131 if (client->session_pool == NULL)
1134 session = state->session;
1137 g_object_ref (session);
1138 /* get a handle to the configuration of the media in the session, this can
1139 * return NULL if this is a new url to manage in this session. */
1140 sessmedia = gst_rtsp_session_get_media (session, uri);
1142 /* create a session if this fails we probably reached our session limit or
1144 if (!(session = gst_rtsp_session_pool_create (client->session_pool)))
1145 goto service_unavailable;
1147 state->session = session;
1149 /* we need a new media configuration in this session */
1153 /* we have no media, find one and manage it */
1154 if (sessmedia == NULL) {
1155 /* get a handle to the configuration of the media in the session */
1156 if ((media = find_media (client, state))) {
1157 /* manage the media in our session now */
1158 sessmedia = gst_rtsp_session_manage_media (session, uri, media);
1162 /* if we stil have no media, error */
1163 if (sessmedia == NULL)
1166 state->sessmedia = sessmedia;
1167 state->media = media = sessmedia->media;
1169 /* now get the stream */
1170 stream = gst_rtsp_media_get_stream (media, streamid);
1174 state->stream = stream;
1176 /* set blocksize on this stream */
1177 if (!handle_blocksize (media, stream, state->request))
1178 goto invalid_blocksize;
1180 /* update the client transport */
1181 if (!configure_client_transport (client, state, ct))
1182 goto unsupported_client_transport;
1184 /* set in the session media transport */
1185 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1187 /* configure keepalive for this transport */
1188 gst_rtsp_stream_transport_set_keepalive (trans,
1189 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1191 /* create and serialize the server transport */
1192 st = make_server_transport (client, state, ct);
1193 trans_str = gst_rtsp_transport_as_text (st);
1194 gst_rtsp_transport_free (st);
1196 /* construct the response now */
1197 code = GST_RTSP_STS_OK;
1198 gst_rtsp_message_init_response (state->response, code,
1199 gst_rtsp_status_as_text (code), state->request);
1201 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1205 send_response (client, session, state->response, FALSE);
1207 /* update the state */
1208 switch (sessmedia->state) {
1209 case GST_RTSP_STATE_PLAYING:
1210 case GST_RTSP_STATE_RECORDING:
1211 case GST_RTSP_STATE_READY:
1212 /* no state change */
1215 sessmedia->state = GST_RTSP_STATE_READY;
1218 g_object_unref (session);
1220 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST],
1228 GST_ERROR ("client %p: bad request", client);
1229 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1234 GST_ERROR ("client %p: media not found", client);
1235 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1236 g_object_unref (session);
1237 gst_rtsp_transport_free (ct);
1242 GST_ERROR ("client %p: invalid blocksize", client);
1243 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1244 g_object_unref (session);
1245 gst_rtsp_transport_free (ct);
1248 unsupported_client_transport:
1250 GST_ERROR ("client %p: unsupported client transport", client);
1251 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1252 g_object_unref (session);
1253 gst_rtsp_transport_free (ct);
1258 GST_ERROR ("client %p: no transport", client);
1259 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1262 unsupported_transports:
1264 GST_ERROR ("client %p: unsupported transports", client);
1265 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1266 gst_rtsp_transport_free (ct);
1271 GST_ERROR ("client %p: no session pool configured", client);
1272 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1273 gst_rtsp_transport_free (ct);
1276 service_unavailable:
1278 GST_ERROR ("client %p: can't create session", client);
1279 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1280 gst_rtsp_transport_free (ct);
1285 static GstSDPMessage *
1286 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1292 gst_sdp_message_new (&sdp);
1294 /* some standard things first */
1295 gst_sdp_message_set_version (sdp, "0");
1297 if (client->is_ipv6)
1302 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1305 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1306 gst_sdp_message_set_information (sdp, "rtsp-server");
1307 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1308 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1309 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1310 gst_sdp_message_add_attribute (sdp, "control", "*");
1312 info.server_proto = proto;
1313 info.server_ip = g_strdup (client->server_ip);
1315 /* create an SDP for the media object */
1316 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1319 g_free (info.server_ip);
1326 GST_ERROR ("client %p: could not create SDP", client);
1327 g_free (info.server_ip);
1328 gst_sdp_message_free (sdp);
1333 /* for the describe we must generate an SDP */
1335 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1340 gchar *str, *content_base;
1341 GstRTSPMedia *media;
1342 GstRTSPClientClass *klass;
1344 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1346 /* check what kind of format is accepted, we don't really do anything with it
1347 * and always return SDP for now. */
1352 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1354 if (res == GST_RTSP_ENOTIMPL)
1357 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1361 /* find the media object for the uri */
1362 if (!(media = find_media (client, state)))
1365 /* create an SDP for the media object on this client */
1366 if (!(sdp = klass->create_sdp (client, media)))
1369 g_object_unref (media);
1371 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1372 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1374 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1377 /* content base for some clients that might screw up creating the setup uri */
1378 str = gst_rtsp_url_get_request_uri (state->uri);
1379 str_len = strlen (str);
1381 /* check for trailing '/' and append one */
1382 if (str[str_len - 1] != '/') {
1383 content_base = g_malloc (str_len + 2);
1384 memcpy (content_base, str, str_len);
1385 content_base[str_len] = '/';
1386 content_base[str_len + 1] = '\0';
1392 GST_INFO ("adding content-base: %s", content_base);
1394 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1396 g_free (content_base);
1398 /* add SDP to the response body */
1399 str = gst_sdp_message_as_text (sdp);
1400 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1401 gst_sdp_message_free (sdp);
1403 send_response (client, state->session, state->response, FALSE);
1405 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1413 GST_ERROR ("client %p: no media", client);
1414 /* error reply is already sent */
1419 GST_ERROR ("client %p: can't create SDP", client);
1420 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1421 g_object_unref (media);
1427 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1429 GstRTSPMethod options;
1432 options = GST_RTSP_DESCRIBE |
1437 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1439 str = gst_rtsp_options_as_text (options);
1441 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1442 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1444 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1447 send_response (client, state->session, state->response, FALSE);
1449 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1455 /* remove duplicate and trailing '/' */
1457 sanitize_uri (GstRTSPUrl * uri)
1461 gboolean have_slash, prev_slash;
1463 s = d = uri->abspath;
1464 len = strlen (uri->abspath);
1468 for (i = 0; i < len; i++) {
1469 have_slash = s[i] == '/';
1471 if (!have_slash || !prev_slash)
1473 prev_slash = have_slash;
1475 len = d - uri->abspath;
1476 /* don't remove the first slash if that's the only thing left */
1477 if (len > 1 && *(d - 1) == '/')
1483 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1485 GST_INFO ("client %p: session %p finished", client, session);
1487 /* unlink all media managed in this session */
1488 client_unlink_session (client, session);
1490 /* remove the session */
1491 if (!(client->sessions = g_list_remove (client->sessions, session))) {
1492 GST_INFO ("client %p: all sessions finalized, close the connection",
1494 close_connection (client);
1499 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
1503 for (walk = client->sessions; walk; walk = g_list_next (walk)) {
1504 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
1506 /* we already know about this session */
1507 if (msession == session)
1511 GST_INFO ("watching session %p", session);
1513 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
1515 client->sessions = g_list_prepend (client->sessions, session);
1517 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1522 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1524 GstRTSPMethod method;
1525 const gchar *uristr;
1527 GstRTSPVersion version;
1529 GstRTSPSession *session;
1530 GstRTSPClientState state = { NULL };
1531 GstRTSPMessage response = { 0 };
1534 state.request = request;
1535 state.response = &response;
1537 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1538 gst_rtsp_message_dump (request);
1541 GST_INFO ("client %p: received a request", client);
1543 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1545 if (version != GST_RTSP_VERSION_1_0) {
1546 /* we can only handle 1.0 requests */
1547 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1551 state.method = method;
1553 /* we always try to parse the url first */
1554 if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
1555 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1559 /* sanitize the uri */
1563 /* get the session if there is any */
1564 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1565 if (res == GST_RTSP_OK) {
1566 if (client->session_pool == NULL)
1569 /* we had a session in the request, find it again */
1570 if (!(session = gst_rtsp_session_pool_find (client->session_pool, sessid)))
1571 goto session_not_found;
1573 /* we add the session to the client list of watched sessions. When a session
1574 * disappears because it times out, we will be notified. If all sessions are
1575 * gone, we will close the connection */
1576 client_watch_session (client, session);
1580 state.session = session;
1583 if (!gst_rtsp_auth_check (client->auth, client, 0, &state))
1584 goto not_authorized;
1587 /* now see what is asked and dispatch to a dedicated handler */
1589 case GST_RTSP_OPTIONS:
1590 handle_options_request (client, &state);
1592 case GST_RTSP_DESCRIBE:
1593 handle_describe_request (client, &state);
1595 case GST_RTSP_SETUP:
1596 handle_setup_request (client, &state);
1599 handle_play_request (client, &state);
1601 case GST_RTSP_PAUSE:
1602 handle_pause_request (client, &state);
1604 case GST_RTSP_TEARDOWN:
1605 handle_teardown_request (client, &state);
1607 case GST_RTSP_SET_PARAMETER:
1608 handle_set_param_request (client, &state);
1610 case GST_RTSP_GET_PARAMETER:
1611 handle_get_param_request (client, &state);
1613 case GST_RTSP_ANNOUNCE:
1614 case GST_RTSP_RECORD:
1615 case GST_RTSP_REDIRECT:
1616 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1618 case GST_RTSP_INVALID:
1620 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1624 g_object_unref (session);
1626 gst_rtsp_url_free (uri);
1632 GST_ERROR ("client %p: no pool configured", client);
1633 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, &state);
1638 GST_ERROR ("client %p: session not found", client);
1639 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1644 GST_ERROR ("client %p: not allowed", client);
1645 handle_unauthorized_request (client, client->auth, &state);
1651 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1661 /* find the stream for this message */
1662 res = gst_rtsp_message_parse_data (message, &channel);
1663 if (res != GST_RTSP_OK)
1666 gst_rtsp_message_steal_body (message, &data, &size);
1668 buffer = gst_buffer_new_wrapped (data, size);
1671 for (walk = client->transports; walk; walk = g_list_next (walk)) {
1672 GstRTSPStreamTransport *trans;
1673 GstRTSPStream *stream;
1674 GstRTSPTransport *tr;
1678 /* we only add clients with a transport to the list */
1679 tr = trans->transport;
1680 stream = trans->stream;
1682 /* check for TCP transport */
1683 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1684 /* dispatch to the stream based on the channel number */
1685 if (tr->interleaved.min == channel) {
1686 gst_rtsp_stream_recv_rtp (stream, buffer);
1689 } else if (tr->interleaved.max == channel) {
1690 gst_rtsp_stream_recv_rtcp (stream, buffer);
1697 gst_buffer_unref (buffer);
1701 * gst_rtsp_client_set_session_pool:
1702 * @client: a #GstRTSPClient
1703 * @pool: a #GstRTSPSessionPool
1705 * Set @pool as the sessionpool for @client which it will use to find
1706 * or allocate sessions. the sessionpool is usually inherited from the server
1707 * that created the client but can be overridden later.
1710 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
1711 GstRTSPSessionPool * pool)
1713 GstRTSPSessionPool *old;
1715 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1718 g_object_ref (pool);
1720 g_mutex_lock (&client->lock);
1721 old = client->session_pool;
1722 client->session_pool = pool;
1723 g_mutex_unlock (&client->lock);
1726 g_object_unref (old);
1730 * gst_rtsp_client_get_session_pool:
1731 * @client: a #GstRTSPClient
1733 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
1735 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
1737 GstRTSPSessionPool *
1738 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
1740 GstRTSPSessionPool *result;
1742 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1744 g_mutex_lock (&client->lock);
1745 if ((result = client->session_pool))
1746 g_object_ref (result);
1747 g_mutex_unlock (&client->lock);
1753 * gst_rtsp_client_set_mount_points:
1754 * @client: a #GstRTSPClient
1755 * @mounts: a #GstRTSPMountPoints
1757 * Set @mounts as the mount points for @client which it will use to map urls
1758 * to media streams. These mount points are usually inherited from the server that
1759 * created the client but can be overriden later.
1762 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
1763 GstRTSPMountPoints * mounts)
1765 GstRTSPMountPoints *old;
1767 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1770 g_object_ref (mounts);
1772 g_mutex_lock (&client->lock);
1773 old = client->mount_points;
1774 client->mount_points = mounts;
1775 g_mutex_unlock (&client->lock);
1778 g_object_unref (old);
1782 * gst_rtsp_client_get_mount_points:
1783 * @client: a #GstRTSPClient
1785 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
1787 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
1789 GstRTSPMountPoints *
1790 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
1792 GstRTSPMountPoints *result;
1794 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1796 g_mutex_lock (&client->lock);
1797 if ((result = client->mount_points))
1798 g_object_ref (result);
1799 g_mutex_unlock (&client->lock);
1805 * gst_rtsp_client_set_use_client_settings:
1806 * @client: a #GstRTSPClient
1807 * @use_client_settings: whether to use client settings for multicast
1809 * Use client transport settings (destination and ttl) for multicast.
1810 * When @use_client_settings is %FALSE, the server settings will be
1814 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
1815 gboolean use_client_settings)
1817 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1819 g_mutex_lock (&client->lock);
1820 client->use_client_settings = use_client_settings;
1821 g_mutex_unlock (&client->lock);
1825 * gst_rtsp_client_get_use_client_settings:
1826 * @client: a #GstRTSPClient
1828 * Check if client transport settings (destination and ttl) for multicast
1832 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
1836 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
1838 g_mutex_lock (&client->lock);
1839 res = client->use_client_settings;
1840 g_mutex_unlock (&client->lock);
1846 * gst_rtsp_client_set_auth:
1847 * @client: a #GstRTSPClient
1848 * @auth: a #GstRTSPAuth
1850 * configure @auth to be used as the authentication manager of @client.
1853 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
1857 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1860 g_object_ref (auth);
1862 g_mutex_lock (&client->lock);
1864 client->auth = auth;
1865 g_mutex_unlock (&client->lock);
1868 g_object_unref (old);
1873 * gst_rtsp_client_get_auth:
1874 * @client: a #GstRTSPClient
1876 * Get the #GstRTSPAuth used as the authentication manager of @client.
1878 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
1882 gst_rtsp_client_get_auth (GstRTSPClient * client)
1884 GstRTSPAuth *result;
1886 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1888 g_mutex_lock (&client->lock);
1889 if ((result = client->auth))
1890 g_object_ref (result);
1891 g_mutex_unlock (&client->lock);
1896 static GstRTSPResult
1897 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
1900 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1902 switch (message->type) {
1903 case GST_RTSP_MESSAGE_REQUEST:
1904 handle_request (client, message);
1906 case GST_RTSP_MESSAGE_RESPONSE:
1908 case GST_RTSP_MESSAGE_DATA:
1909 handle_data (client, message);
1917 static GstRTSPResult
1918 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
1920 GstRTSPClient *client;
1922 client = GST_RTSP_CLIENT (user_data);
1923 if (client->close_response_seq && client->close_response_seq == cseq) {
1924 client->close_response_seq = 0;
1925 close_connection (client);
1931 static GstRTSPResult
1932 closed (GstRTSPWatch * watch, gpointer user_data)
1934 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1935 const gchar *tunnelid;
1937 GST_INFO ("client %p: connection closed", client);
1939 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
1940 g_mutex_lock (&tunnels_lock);
1941 /* remove from tunnelids */
1942 g_hash_table_remove (tunnels, tunnelid);
1943 g_mutex_unlock (&tunnels_lock);
1949 static GstRTSPResult
1950 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
1952 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1955 str = gst_rtsp_strresult (result);
1956 GST_INFO ("client %p: received an error %s", client, str);
1962 static GstRTSPResult
1963 error_full (GstRTSPWatch * watch, GstRTSPResult result,
1964 GstRTSPMessage * message, guint id, gpointer user_data)
1966 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1969 str = gst_rtsp_strresult (result);
1971 ("client %p: received an error %s when handling message %p with id %d",
1972 client, str, message, id);
1979 remember_tunnel (GstRTSPClient * client)
1981 const gchar *tunnelid;
1983 /* store client in the pending tunnels */
1984 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
1985 if (tunnelid == NULL)
1988 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
1990 /* we can't have two clients connecting with the same tunnelid */
1991 g_mutex_lock (&tunnels_lock);
1992 if (g_hash_table_lookup (tunnels, tunnelid))
1993 goto tunnel_existed;
1995 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
1996 g_mutex_unlock (&tunnels_lock);
2003 GST_ERROR ("client %p: no tunnelid provided", client);
2008 g_mutex_unlock (&tunnels_lock);
2009 GST_ERROR ("client %p: tunnel session %s already existed", client,
2015 static GstRTSPStatusCode
2016 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2018 GstRTSPClient *client;
2020 client = GST_RTSP_CLIENT (user_data);
2022 GST_INFO ("client %p: tunnel start (connection %p)", client,
2023 client->connection);
2025 if (!remember_tunnel (client))
2028 return GST_RTSP_STS_OK;
2033 GST_ERROR ("client %p: error starting tunnel", client);
2034 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2038 static GstRTSPResult
2039 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2041 GstRTSPClient *client;
2043 client = GST_RTSP_CLIENT (user_data);
2045 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2046 client->connection);
2048 /* ignore error, it'll only be a problem when the client does a POST again */
2049 remember_tunnel (client);
2054 static GstRTSPResult
2055 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2057 const gchar *tunnelid;
2058 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2059 GstRTSPClient *oclient;
2061 GST_INFO ("client %p: tunnel complete", client);
2063 /* find previous tunnel */
2064 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
2065 if (tunnelid == NULL)
2068 g_mutex_lock (&tunnels_lock);
2069 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2072 /* remove the old client from the table. ref before because removing it will
2073 * remove the ref to it. */
2074 g_object_ref (oclient);
2075 g_hash_table_remove (tunnels, tunnelid);
2077 if (oclient->watch == NULL)
2079 g_mutex_unlock (&tunnels_lock);
2081 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2082 oclient->connection, client->connection);
2084 /* merge the tunnels into the first client */
2085 gst_rtsp_connection_do_tunnel (oclient->connection, client->connection);
2086 gst_rtsp_watch_reset (oclient->watch);
2087 g_object_unref (oclient);
2094 GST_ERROR ("client %p: no tunnelid provided", client);
2095 return GST_RTSP_ERROR;
2099 g_mutex_unlock (&tunnels_lock);
2100 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2101 return GST_RTSP_ERROR;
2105 g_mutex_unlock (&tunnels_lock);
2106 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2107 g_object_unref (oclient);
2108 return GST_RTSP_ERROR;
2112 static GstRTSPWatchFuncs watch_funcs = {
2124 client_watch_notify (GstRTSPClient * client)
2126 GST_INFO ("client %p: watch destroyed", client);
2127 client->watch = NULL;
2128 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2129 g_object_unref (client);
2133 setup_client (GstRTSPClient * client, GSocket * socket,
2134 GstRTSPConnection * conn, GError ** error)
2136 GSocket *read_socket;
2137 GSocketAddress *address;
2140 read_socket = gst_rtsp_connection_get_read_socket (conn);
2141 client->is_ipv6 = g_socket_get_family (socket) == G_SOCKET_FAMILY_IPV6;
2143 if (!(address = g_socket_get_remote_address (read_socket, error)))
2146 g_free (client->server_ip);
2147 /* keep the original ip that the client connected to */
2148 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2149 GInetAddress *iaddr;
2151 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2153 client->server_ip = g_inet_address_to_string (iaddr);
2154 g_object_unref (address);
2156 client->server_ip = g_strdup ("unknown");
2159 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2160 client->server_ip, client->is_ipv6);
2162 url = gst_rtsp_connection_get_url (conn);
2163 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2165 client->connection = conn;
2172 GST_ERROR ("could not get remote address %s", (*error)->message);
2178 * gst_rtsp_client_use_socket:
2179 * @client: a #GstRTSPClient
2180 * @socket: a #GSocket
2181 * @ip: the IP address of the remote client
2182 * @port: the port used by the other end
2183 * @initial_buffer: any zero terminated initial data that was already read from
2187 * Take an existing network socket and use it for an RTSP connection.
2189 * Returns: %TRUE on success.
2192 gst_rtsp_client_use_socket (GstRTSPClient * client, GSocket * socket,
2193 const gchar * ip, gint port, const gchar * initial_buffer, GError ** error)
2195 GstRTSPConnection *conn;
2198 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2199 g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
2201 GST_RTSP_CHECK (gst_rtsp_connection_create_from_socket (socket, ip, port,
2202 initial_buffer, &conn), no_connection);
2204 return setup_client (client, socket, conn, error);
2209 gchar *str = gst_rtsp_strresult (res);
2211 GST_ERROR ("could not create connection from socket %p: %s", socket, str);
2218 * gst_rtsp_client_accept:
2219 * @client: a #GstRTSPClient
2220 * @socket: a #GSocket
2221 * @context: the context to run in
2222 * @cancellable: a #GCancellable
2225 * Accept a new connection for @client on @socket.
2227 * Returns: %TRUE if the client could be accepted.
2230 gst_rtsp_client_accept (GstRTSPClient * client, GSocket * socket,
2231 GCancellable * cancellable, GError ** error)
2233 GstRTSPConnection *conn;
2236 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2237 g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
2239 /* a new client connected. */
2240 GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, cancellable),
2243 return setup_client (client, socket, conn, error);
2248 gchar *str = gst_rtsp_strresult (res);
2250 GST_ERROR ("Could not accept client on server socket %p: %s", socket, str);
2257 * gst_rtsp_client_attach:
2258 * @client: a #GstRTSPClient
2259 * @context: (allow-none): a #GMainContext
2261 * Attaches @client to @context. When the mainloop for @context is run, the
2262 * client will be dispatched. When @context is NULL, the default context will be
2265 * This function should be called when the client properties and urls are fully
2266 * configured and the client is ready to start.
2268 * Returns: the ID (greater than 0) for the source within the GMainContext.
2271 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2275 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2276 g_return_val_if_fail (client->watch == NULL, 0);
2278 /* create watch for the connection and attach */
2279 client->watch = gst_rtsp_watch_new (client->connection, &watch_funcs,
2280 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2282 GST_INFO ("attaching to context %p", context);
2283 res = gst_rtsp_watch_attach (client->watch, context);
2284 gst_rtsp_watch_unref (client->watch);