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_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 if (client->send_notify)
223 client->send_notify (client->send_data);
225 client_cleanup_sessions (client);
227 if (client->connection)
228 gst_rtsp_connection_free (client->connection);
229 if (client->session_pool)
230 g_object_unref (client->session_pool);
231 if (client->mount_points)
232 g_object_unref (client->mount_points);
234 g_object_unref (client->auth);
237 gst_rtsp_url_free (client->uri);
239 gst_rtsp_media_unprepare (client->media);
240 g_object_unref (client->media);
243 g_free (client->server_ip);
244 g_mutex_clear (&client->lock);
246 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
250 gst_rtsp_client_get_property (GObject * object, guint propid,
251 GValue * value, GParamSpec * pspec)
253 GstRTSPClient *client = GST_RTSP_CLIENT (object);
256 case PROP_SESSION_POOL:
257 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
259 case PROP_MOUNT_POINTS:
260 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
262 case PROP_USE_CLIENT_SETTINGS:
263 g_value_set_boolean (value,
264 gst_rtsp_client_get_use_client_settings (client));
267 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
272 gst_rtsp_client_set_property (GObject * object, guint propid,
273 const GValue * value, GParamSpec * pspec)
275 GstRTSPClient *client = GST_RTSP_CLIENT (object);
278 case PROP_SESSION_POOL:
279 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
281 case PROP_MOUNT_POINTS:
282 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
284 case PROP_USE_CLIENT_SETTINGS:
285 gst_rtsp_client_set_use_client_settings (client,
286 g_value_get_boolean (value));
289 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
294 * gst_rtsp_client_new:
296 * Create a new #GstRTSPClient instance.
298 * Returns: a new #GstRTSPClient
301 gst_rtsp_client_new (void)
303 GstRTSPClient *result;
305 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
311 send_response (GstRTSPClient * client, GstRTSPSession * session,
312 GstRTSPMessage * response, gboolean close)
314 gst_rtsp_message_add_header (response, GST_RTSP_HDR_SERVER,
315 "GStreamer RTSP server");
317 /* remove any previous header */
318 gst_rtsp_message_remove_header (response, GST_RTSP_HDR_SESSION, -1);
320 /* add the new session header for new session ids */
322 gst_rtsp_message_take_header (response, GST_RTSP_HDR_SESSION,
323 gst_rtsp_session_get_header (session));
326 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
327 gst_rtsp_message_dump (response);
331 gst_rtsp_message_add_header (response, GST_RTSP_HDR_CONNECTION, "close");
333 if (client->send_func)
334 client->send_func (client, response, close, client->send_data);
336 gst_rtsp_message_unset (response);
340 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
341 GstRTSPClientState * state)
343 gst_rtsp_message_init_response (state->response, code,
344 gst_rtsp_status_as_text (code), state->request);
346 send_response (client, NULL, state->response, FALSE);
350 handle_unauthorized_request (GstRTSPClient * client, GstRTSPAuth * auth,
351 GstRTSPClientState * state)
353 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_UNAUTHORIZED,
354 gst_rtsp_status_as_text (GST_RTSP_STS_UNAUTHORIZED), state->request);
357 /* and let the authentication manager setup the auth tokens */
358 gst_rtsp_auth_setup_auth (auth, client, 0, state);
361 send_response (client, state->session, state->response, FALSE);
366 compare_uri (const GstRTSPUrl * uri1, const GstRTSPUrl * uri2)
368 if (uri1 == NULL || uri2 == NULL)
371 if (strcmp (uri1->abspath, uri2->abspath))
377 /* this function is called to initially find the media for the DESCRIBE request
378 * but is cached for when the same client (without breaking the connection) is
379 * doing a setup for the exact same url. */
380 static GstRTSPMedia *
381 find_media (GstRTSPClient * client, GstRTSPClientState * state)
383 GstRTSPMediaFactory *factory;
387 if (!compare_uri (client->uri, state->uri)) {
388 /* remove any previously cached values before we try to construct a new
391 gst_rtsp_url_free (client->uri);
394 gst_rtsp_media_unprepare (client->media);
395 g_object_unref (client->media);
397 client->media = NULL;
399 if (!client->mount_points)
400 goto no_mount_points;
402 /* find the factory for the uri first */
404 gst_rtsp_mount_points_find_factory (client->mount_points,
408 state->factory = factory;
410 /* check if we have access to the factory */
411 if ((auth = gst_rtsp_media_factory_get_auth (factory))) {
412 if (!gst_rtsp_auth_check (auth, client, 0, state))
415 g_object_unref (auth);
418 /* prepare the media and add it to the pipeline */
419 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
422 g_object_unref (factory);
424 state->factory = NULL;
426 /* set ipv6 on the media before preparing */
427 media->is_ipv6 = client->is_ipv6;
428 state->media = media;
430 /* prepare the media */
431 if (!(gst_rtsp_media_prepare (media)))
434 /* now keep track of the uri and the media */
435 client->uri = gst_rtsp_url_copy (state->uri);
436 client->media = media;
438 /* we have seen this uri before, used cached media */
439 media = client->media;
440 state->media = media;
441 GST_INFO ("reusing cached media %p", media);
445 g_object_ref (media);
452 GST_ERROR ("client %p: no mount points configured", client);
453 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
458 GST_ERROR ("client %p: no factory for uri", client);
459 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
464 GST_ERROR ("client %p: unauthorized request", client);
465 handle_unauthorized_request (client, auth, state);
466 g_object_unref (factory);
467 g_object_unref (auth);
472 GST_ERROR ("client %p: can't create media", client);
473 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
474 g_object_unref (factory);
479 GST_ERROR ("client %p: can't prepare media", client);
480 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
481 g_object_unref (media);
487 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
489 GstRTSPMessage message = { 0 };
494 gst_rtsp_message_init_data (&message, channel);
496 /* FIXME, need some sort of iovec RTSPMessage here */
497 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
500 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
502 if (client->send_func)
503 client->send_func (client, &message, FALSE, client->send_data);
505 gst_rtsp_message_steal_body (&message, &data, &usize);
506 gst_buffer_unmap (buffer, &map_info);
508 gst_rtsp_message_unset (&message);
514 link_transport (GstRTSPClient * client, GstRTSPSession * session,
515 GstRTSPStreamTransport * trans)
517 GST_DEBUG ("client %p: linking transport %p", client, trans);
518 gst_rtsp_stream_transport_set_callbacks (trans,
519 (GstRTSPSendFunc) do_send_data,
520 (GstRTSPSendFunc) do_send_data, client, NULL);
522 client->transports = g_list_prepend (client->transports, trans);
524 /* make sure our session can't expire */
525 gst_rtsp_session_prevent_expire (session);
529 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
530 GstRTSPStreamTransport * trans)
532 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
533 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
535 client->transports = g_list_remove (client->transports, trans);
537 /* our session can now expire */
538 gst_rtsp_session_allow_expire (session);
542 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
543 GstRTSPSessionMedia * media)
547 n_streams = gst_rtsp_media_n_streams (media->media);
548 for (i = 0; i < n_streams; i++) {
549 GstRTSPStreamTransport *trans;
550 GstRTSPTransport *tr;
552 /* get the transport, if there is no transport configured, skip this stream */
553 trans = gst_rtsp_session_media_get_transport (media, i);
557 tr = trans->transport;
559 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
560 /* for TCP, unlink the stream from the TCP connection of the client */
561 unlink_transport (client, session, trans);
567 close_connection (GstRTSPClient * client)
569 const gchar *tunnelid;
571 GST_DEBUG ("client %p: closing connection", client);
573 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
574 g_mutex_lock (&tunnels_lock);
575 /* remove from tunnelids */
576 g_hash_table_remove (tunnels, tunnelid);
577 g_mutex_unlock (&tunnels_lock);
580 gst_rtsp_connection_close (client->connection);
584 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
586 GstRTSPSession *session;
587 GstRTSPSessionMedia *media;
588 GstRTSPStatusCode code;
593 session = state->session;
595 /* get a handle to the configuration of the media in the session */
596 media = gst_rtsp_session_get_media (session, state->uri);
600 state->sessmedia = media;
602 /* unlink the all TCP callbacks */
603 unlink_session_transports (client, session, media);
605 /* remove the session from the watched sessions */
606 g_object_weak_unref (G_OBJECT (session),
607 (GWeakNotify) client_session_finalized, client);
608 client->sessions = g_list_remove (client->sessions, session);
610 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
612 /* unmanage the media in the session, returns false if all media session
614 if (!gst_rtsp_session_release_media (session, media)) {
615 /* remove the session */
616 gst_rtsp_session_pool_remove (client->session_pool, session);
618 /* construct the response now */
619 code = GST_RTSP_STS_OK;
620 gst_rtsp_message_init_response (state->response, code,
621 gst_rtsp_status_as_text (code), state->request);
623 send_response (client, session, state->response, TRUE);
625 /* we emit the signal before closing the connection */
626 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
634 GST_ERROR ("client %p: no session", client);
635 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
640 GST_ERROR ("client %p: no media for uri", client);
641 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
647 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
653 res = gst_rtsp_message_get_body (state->request, &data, &size);
654 if (res != GST_RTSP_OK)
658 /* no body, keep-alive request */
659 send_generic_response (client, GST_RTSP_STS_OK, state);
661 /* there is a body, handle the params */
662 res = gst_rtsp_params_get (client, state);
663 if (res != GST_RTSP_OK)
666 send_response (client, state->session, state->response, FALSE);
669 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
677 GST_ERROR ("client %p: bad request", client);
678 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
684 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
690 res = gst_rtsp_message_get_body (state->request, &data, &size);
691 if (res != GST_RTSP_OK)
695 /* no body, keep-alive request */
696 send_generic_response (client, GST_RTSP_STS_OK, state);
698 /* there is a body, handle the params */
699 res = gst_rtsp_params_set (client, state);
700 if (res != GST_RTSP_OK)
703 send_response (client, state->session, state->response, FALSE);
706 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
714 GST_ERROR ("client %p: bad request", client);
715 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
721 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
723 GstRTSPSession *session;
724 GstRTSPSessionMedia *media;
725 GstRTSPStatusCode code;
727 if (!(session = state->session))
730 /* get a handle to the configuration of the media in the session */
731 media = gst_rtsp_session_get_media (session, state->uri);
735 state->sessmedia = media;
737 /* the session state must be playing or recording */
738 if (media->state != GST_RTSP_STATE_PLAYING &&
739 media->state != GST_RTSP_STATE_RECORDING)
742 /* unlink the all TCP callbacks */
743 unlink_session_transports (client, session, media);
745 /* then pause sending */
746 gst_rtsp_session_media_set_state (media, GST_STATE_PAUSED);
748 /* construct the response now */
749 code = GST_RTSP_STS_OK;
750 gst_rtsp_message_init_response (state->response, code,
751 gst_rtsp_status_as_text (code), state->request);
753 send_response (client, session, state->response, FALSE);
755 /* the state is now READY */
756 media->state = GST_RTSP_STATE_READY;
758 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
766 GST_ERROR ("client %p: no seesion", client);
767 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
772 GST_ERROR ("client %p: no media for uri", client);
773 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
778 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
779 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
786 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
788 GstRTSPSession *session;
789 GstRTSPSessionMedia *media;
790 GstRTSPStatusCode code;
792 guint n_streams, i, infocount;
794 GstRTSPTimeRange *range;
797 if (!(session = state->session))
800 /* get a handle to the configuration of the media in the session */
801 media = gst_rtsp_session_get_media (session, state->uri);
805 state->sessmedia = media;
807 /* the session state must be playing or ready */
808 if (media->state != GST_RTSP_STATE_PLAYING &&
809 media->state != GST_RTSP_STATE_READY)
812 /* parse the range header if we have one */
814 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
815 if (res == GST_RTSP_OK) {
816 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
817 /* we have a range, seek to the position */
818 gst_rtsp_media_seek (media->media, range);
819 gst_rtsp_range_free (range);
823 /* grab RTPInfo from the payloaders now */
824 rtpinfo = g_string_new ("");
826 n_streams = gst_rtsp_media_n_streams (media->media);
827 for (i = 0, infocount = 0; i < n_streams; i++) {
828 GstRTSPStreamTransport *trans;
829 GstRTSPTransport *tr;
833 /* get the transport, if there is no transport configured, skip this stream */
834 trans = gst_rtsp_session_media_get_transport (media, i);
836 GST_INFO ("stream %d is not configured", i);
839 tr = trans->transport;
841 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
842 /* for TCP, link the stream to the TCP connection of the client */
843 link_transport (client, session, trans);
846 if (gst_rtsp_stream_get_rtpinfo (trans->stream, &rtptime, &seq)) {
848 g_string_append (rtpinfo, ", ");
850 uristr = gst_rtsp_url_get_request_uri (state->uri);
851 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
852 uristr, i, seq, rtptime);
857 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
861 /* construct the response now */
862 code = GST_RTSP_STS_OK;
863 gst_rtsp_message_init_response (state->response, code,
864 gst_rtsp_status_as_text (code), state->request);
866 /* add the RTP-Info header */
868 str = g_string_free (rtpinfo, FALSE);
869 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
871 g_string_free (rtpinfo, TRUE);
875 str = gst_rtsp_media_get_range_string (media->media, TRUE);
876 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
878 send_response (client, session, state->response, FALSE);
880 /* start playing after sending the request */
881 gst_rtsp_session_media_set_state (media, GST_STATE_PLAYING);
883 media->state = GST_RTSP_STATE_PLAYING;
885 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
893 GST_ERROR ("client %p: no session", client);
894 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
899 GST_ERROR ("client %p: media not found", client);
900 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
905 GST_ERROR ("client %p: not PLAYING or READY", client);
906 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
913 do_keepalive (GstRTSPSession * session)
915 GST_INFO ("keep session %p alive", session);
916 gst_rtsp_session_touch (session);
919 /* parse @transport and return a valid transport in @tr. only transports
920 * from @supported are returned. Returns FALSE if no valid transport
923 parse_transport (const char *transport, GstRTSPLowerTrans supported,
924 GstRTSPTransport * tr)
931 gst_rtsp_transport_init (tr);
933 GST_DEBUG ("parsing transports %s", transport);
935 transports = g_strsplit (transport, ",", 0);
937 /* loop through the transports, try to parse */
938 for (i = 0; transports[i]; i++) {
939 res = gst_rtsp_transport_parse (transports[i], tr);
940 if (res != GST_RTSP_OK) {
941 /* no valid transport, search some more */
942 GST_WARNING ("could not parse transport %s", transports[i]);
946 /* we have a transport, see if it's RTP/AVP */
947 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
948 GST_WARNING ("invalid transport %s", transports[i]);
952 if (!(tr->lower_transport & supported)) {
953 GST_WARNING ("unsupported transport %s", transports[i]);
957 /* we have a valid transport */
958 GST_INFO ("found valid transport %s", transports[i]);
963 gst_rtsp_transport_init (tr);
965 g_strfreev (transports);
971 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
972 GstRTSPMessage * request)
974 gchar *blocksize_str;
977 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
978 &blocksize_str, 0) == GST_RTSP_OK) {
982 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
983 if (end == blocksize_str) {
984 GST_ERROR ("failed to parse blocksize");
987 /* we don't want to change the mtu when this media
988 * can be shared because it impacts other clients */
989 if (gst_rtsp_media_is_shared (media))
992 if (blocksize > G_MAXUINT)
993 blocksize = G_MAXUINT;
994 gst_rtsp_stream_set_mtu (stream, blocksize);
1001 configure_client_transport (GstRTSPClient * client, GstRTSPClientState * state,
1002 GstRTSPTransport * ct)
1004 /* we have a valid transport now, set the destination of the client. */
1005 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1006 if (ct->destination == NULL || !client->use_client_settings) {
1007 GstRTSPAddress *addr;
1009 addr = gst_rtsp_stream_get_address (state->stream);
1013 g_free (ct->destination);
1014 ct->destination = g_strdup (addr->address);
1015 ct->port.min = addr->port;
1016 ct->port.max = addr->port + addr->n_ports - 1;
1017 ct->ttl = addr->ttl;
1022 url = gst_rtsp_connection_get_url (client->connection);
1023 g_free (ct->destination);
1024 ct->destination = g_strdup (url->host);
1026 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1027 /* check if the client selected channels for TCP */
1028 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1029 gst_rtsp_session_media_alloc_channels (state->sessmedia,
1039 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1044 static GstRTSPTransport *
1045 make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
1046 GstRTSPTransport * ct)
1048 GstRTSPTransport *st;
1050 /* prepare the server transport */
1051 gst_rtsp_transport_new (&st);
1053 st->trans = ct->trans;
1054 st->profile = ct->profile;
1055 st->lower_transport = ct->lower_transport;
1057 switch (st->lower_transport) {
1058 case GST_RTSP_LOWER_TRANS_UDP:
1059 st->client_port = ct->client_port;
1060 st->server_port = state->stream->server_port;
1062 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1063 st->port = ct->port;
1064 st->destination = g_strdup (ct->destination);
1067 case GST_RTSP_LOWER_TRANS_TCP:
1068 st->interleaved = ct->interleaved;
1073 if (state->stream->session)
1074 g_object_get (state->stream->session, "internal-ssrc", &st->ssrc, NULL);
1080 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
1085 GstRTSPTransport *ct, *st;
1086 GstRTSPLowerTrans supported;
1087 GstRTSPStatusCode code;
1088 GstRTSPSession *session;
1089 GstRTSPStreamTransport *trans;
1090 gchar *trans_str, *pos;
1092 GstRTSPSessionMedia *sessmedia;
1093 GstRTSPMedia *media;
1094 GstRTSPStream *stream;
1098 /* the uri contains the stream number we added in the SDP config, which is
1099 * always /stream=%d so we need to strip that off
1100 * parse the stream we need to configure, look for the stream in the abspath
1101 * first and then in the query. */
1102 if (uri->abspath == NULL || !(pos = strstr (uri->abspath, "/stream="))) {
1103 if (uri->query == NULL || !(pos = strstr (uri->query, "/stream=")))
1107 /* we can mofify the parsed uri in place */
1110 pos += strlen ("/stream=");
1111 if (sscanf (pos, "%u", &streamid) != 1)
1114 /* parse the transport */
1116 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
1118 if (res != GST_RTSP_OK)
1121 gst_rtsp_transport_new (&ct);
1123 /* our supported transports */
1124 supported = GST_RTSP_LOWER_TRANS_UDP |
1125 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
1127 /* parse and find a usable supported transport */
1128 if (!parse_transport (transport, supported, ct))
1129 goto unsupported_transports;
1131 /* we create the session after parsing stuff so that we don't make
1132 * a session for malformed requests */
1133 if (client->session_pool == NULL)
1136 session = state->session;
1139 g_object_ref (session);
1140 /* get a handle to the configuration of the media in the session, this can
1141 * return NULL if this is a new url to manage in this session. */
1142 sessmedia = gst_rtsp_session_get_media (session, uri);
1144 /* create a session if this fails we probably reached our session limit or
1146 if (!(session = gst_rtsp_session_pool_create (client->session_pool)))
1147 goto service_unavailable;
1149 state->session = session;
1151 /* we need a new media configuration in this session */
1155 /* we have no media, find one and manage it */
1156 if (sessmedia == NULL) {
1157 /* get a handle to the configuration of the media in the session */
1158 if ((media = find_media (client, state))) {
1159 /* manage the media in our session now */
1160 sessmedia = gst_rtsp_session_manage_media (session, uri, media);
1164 /* if we stil have no media, error */
1165 if (sessmedia == NULL)
1168 state->sessmedia = sessmedia;
1169 state->media = media = sessmedia->media;
1171 /* now get the stream */
1172 stream = gst_rtsp_media_get_stream (media, streamid);
1176 state->stream = stream;
1178 /* set blocksize on this stream */
1179 if (!handle_blocksize (media, stream, state->request))
1180 goto invalid_blocksize;
1182 /* update the client transport */
1183 if (!configure_client_transport (client, state, ct))
1184 goto unsupported_client_transport;
1186 /* set in the session media transport */
1187 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1189 /* configure keepalive for this transport */
1190 gst_rtsp_stream_transport_set_keepalive (trans,
1191 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1193 /* create and serialize the server transport */
1194 st = make_server_transport (client, state, ct);
1195 trans_str = gst_rtsp_transport_as_text (st);
1196 gst_rtsp_transport_free (st);
1198 /* construct the response now */
1199 code = GST_RTSP_STS_OK;
1200 gst_rtsp_message_init_response (state->response, code,
1201 gst_rtsp_status_as_text (code), state->request);
1203 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1207 send_response (client, session, state->response, FALSE);
1209 /* update the state */
1210 switch (sessmedia->state) {
1211 case GST_RTSP_STATE_PLAYING:
1212 case GST_RTSP_STATE_RECORDING:
1213 case GST_RTSP_STATE_READY:
1214 /* no state change */
1217 sessmedia->state = GST_RTSP_STATE_READY;
1220 g_object_unref (session);
1222 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST],
1230 GST_ERROR ("client %p: bad request", client);
1231 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1236 GST_ERROR ("client %p: media not found", client);
1237 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1238 g_object_unref (session);
1239 gst_rtsp_transport_free (ct);
1244 GST_ERROR ("client %p: invalid blocksize", client);
1245 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1246 g_object_unref (session);
1247 gst_rtsp_transport_free (ct);
1250 unsupported_client_transport:
1252 GST_ERROR ("client %p: unsupported client transport", client);
1253 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1254 g_object_unref (session);
1255 gst_rtsp_transport_free (ct);
1260 GST_ERROR ("client %p: no transport", client);
1261 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1264 unsupported_transports:
1266 GST_ERROR ("client %p: unsupported transports", client);
1267 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1268 gst_rtsp_transport_free (ct);
1273 GST_ERROR ("client %p: no session pool configured", client);
1274 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
1275 gst_rtsp_transport_free (ct);
1278 service_unavailable:
1280 GST_ERROR ("client %p: can't create session", client);
1281 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1282 gst_rtsp_transport_free (ct);
1287 static GstSDPMessage *
1288 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1294 gst_sdp_message_new (&sdp);
1296 /* some standard things first */
1297 gst_sdp_message_set_version (sdp, "0");
1299 if (client->is_ipv6)
1304 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1307 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1308 gst_sdp_message_set_information (sdp, "rtsp-server");
1309 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1310 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1311 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1312 gst_sdp_message_add_attribute (sdp, "control", "*");
1314 info.server_proto = proto;
1315 info.server_ip = g_strdup (client->server_ip);
1317 /* create an SDP for the media object */
1318 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1321 g_free (info.server_ip);
1328 GST_ERROR ("client %p: could not create SDP", client);
1329 g_free (info.server_ip);
1330 gst_sdp_message_free (sdp);
1335 /* for the describe we must generate an SDP */
1337 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1342 gchar *str, *content_base;
1343 GstRTSPMedia *media;
1344 GstRTSPClientClass *klass;
1346 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1348 /* check what kind of format is accepted, we don't really do anything with it
1349 * and always return SDP for now. */
1354 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1356 if (res == GST_RTSP_ENOTIMPL)
1359 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1363 /* find the media object for the uri */
1364 if (!(media = find_media (client, state)))
1367 /* create an SDP for the media object on this client */
1368 if (!(sdp = klass->create_sdp (client, media)))
1371 g_object_unref (media);
1373 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1374 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1376 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1379 /* content base for some clients that might screw up creating the setup uri */
1380 str = gst_rtsp_url_get_request_uri (state->uri);
1381 str_len = strlen (str);
1383 /* check for trailing '/' and append one */
1384 if (str[str_len - 1] != '/') {
1385 content_base = g_malloc (str_len + 2);
1386 memcpy (content_base, str, str_len);
1387 content_base[str_len] = '/';
1388 content_base[str_len + 1] = '\0';
1394 GST_INFO ("adding content-base: %s", content_base);
1396 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1398 g_free (content_base);
1400 /* add SDP to the response body */
1401 str = gst_sdp_message_as_text (sdp);
1402 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1403 gst_sdp_message_free (sdp);
1405 send_response (client, state->session, state->response, FALSE);
1407 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1415 GST_ERROR ("client %p: no media", client);
1416 /* error reply is already sent */
1421 GST_ERROR ("client %p: can't create SDP", client);
1422 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1423 g_object_unref (media);
1429 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1431 GstRTSPMethod options;
1434 options = GST_RTSP_DESCRIBE |
1439 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1441 str = gst_rtsp_options_as_text (options);
1443 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1444 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1446 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1449 send_response (client, state->session, state->response, FALSE);
1451 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1457 /* remove duplicate and trailing '/' */
1459 sanitize_uri (GstRTSPUrl * uri)
1463 gboolean have_slash, prev_slash;
1465 s = d = uri->abspath;
1466 len = strlen (uri->abspath);
1470 for (i = 0; i < len; i++) {
1471 have_slash = s[i] == '/';
1473 if (!have_slash || !prev_slash)
1475 prev_slash = have_slash;
1477 len = d - uri->abspath;
1478 /* don't remove the first slash if that's the only thing left */
1479 if (len > 1 && *(d - 1) == '/')
1485 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1487 GST_INFO ("client %p: session %p finished", client, session);
1489 /* unlink all media managed in this session */
1490 client_unlink_session (client, session);
1492 /* remove the session */
1493 if (!(client->sessions = g_list_remove (client->sessions, session))) {
1494 GST_INFO ("client %p: all sessions finalized, close the connection",
1496 close_connection (client);
1501 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
1505 for (walk = client->sessions; walk; walk = g_list_next (walk)) {
1506 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
1508 /* we already know about this session */
1509 if (msession == session)
1513 GST_INFO ("watching session %p", session);
1515 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
1517 client->sessions = g_list_prepend (client->sessions, session);
1519 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1524 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1526 GstRTSPMethod method;
1527 const gchar *uristr;
1528 GstRTSPUrl *uri = NULL;
1529 GstRTSPVersion version;
1531 GstRTSPSession *session = NULL;
1532 GstRTSPClientState state = { NULL };
1533 GstRTSPMessage response = { 0 };
1536 state.request = request;
1537 state.response = &response;
1539 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1540 gst_rtsp_message_dump (request);
1543 GST_INFO ("client %p: received a request", client);
1545 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1547 /* we can only handle 1.0 requests */
1548 if (version != GST_RTSP_VERSION_1_0)
1551 state.method = method;
1553 /* we always try to parse the url first */
1554 if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1557 /* get the session if there is any */
1558 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1559 if (res == GST_RTSP_OK) {
1560 if (client->session_pool == NULL)
1563 /* we had a session in the request, find it again */
1564 if (!(session = gst_rtsp_session_pool_find (client->session_pool, sessid)))
1565 goto session_not_found;
1567 /* we add the session to the client list of watched sessions. When a session
1568 * disappears because it times out, we will be notified. If all sessions are
1569 * gone, we will close the connection */
1570 client_watch_session (client, session);
1573 /* sanitize the uri */
1576 state.session = session;
1579 if (!gst_rtsp_auth_check (client->auth, client, 0, &state))
1580 goto not_authorized;
1583 /* now see what is asked and dispatch to a dedicated handler */
1585 case GST_RTSP_OPTIONS:
1586 handle_options_request (client, &state);
1588 case GST_RTSP_DESCRIBE:
1589 handle_describe_request (client, &state);
1591 case GST_RTSP_SETUP:
1592 handle_setup_request (client, &state);
1595 handle_play_request (client, &state);
1597 case GST_RTSP_PAUSE:
1598 handle_pause_request (client, &state);
1600 case GST_RTSP_TEARDOWN:
1601 handle_teardown_request (client, &state);
1603 case GST_RTSP_SET_PARAMETER:
1604 handle_set_param_request (client, &state);
1606 case GST_RTSP_GET_PARAMETER:
1607 handle_get_param_request (client, &state);
1609 case GST_RTSP_ANNOUNCE:
1610 case GST_RTSP_RECORD:
1611 case GST_RTSP_REDIRECT:
1612 goto not_implemented;
1613 case GST_RTSP_INVALID:
1620 g_object_unref (session);
1622 gst_rtsp_url_free (uri);
1628 GST_ERROR ("client %p: version %d not supported", client, version);
1629 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1635 GST_ERROR ("client %p: bad request", client);
1636 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1641 GST_ERROR ("client %p: no pool configured", client);
1642 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1647 GST_ERROR ("client %p: session not found", client);
1648 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1653 GST_ERROR ("client %p: not allowed", client);
1654 handle_unauthorized_request (client, client->auth, &state);
1659 GST_ERROR ("client %p: method %d not implemented", client, method);
1660 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1666 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1676 /* find the stream for this message */
1677 res = gst_rtsp_message_parse_data (message, &channel);
1678 if (res != GST_RTSP_OK)
1681 gst_rtsp_message_steal_body (message, &data, &size);
1683 buffer = gst_buffer_new_wrapped (data, size);
1686 for (walk = client->transports; walk; walk = g_list_next (walk)) {
1687 GstRTSPStreamTransport *trans;
1688 GstRTSPStream *stream;
1689 GstRTSPTransport *tr;
1693 /* we only add clients with a transport to the list */
1694 tr = trans->transport;
1695 stream = trans->stream;
1697 /* check for TCP transport */
1698 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1699 /* dispatch to the stream based on the channel number */
1700 if (tr->interleaved.min == channel) {
1701 gst_rtsp_stream_recv_rtp (stream, buffer);
1704 } else if (tr->interleaved.max == channel) {
1705 gst_rtsp_stream_recv_rtcp (stream, buffer);
1712 gst_buffer_unref (buffer);
1716 * gst_rtsp_client_set_session_pool:
1717 * @client: a #GstRTSPClient
1718 * @pool: a #GstRTSPSessionPool
1720 * Set @pool as the sessionpool for @client which it will use to find
1721 * or allocate sessions. the sessionpool is usually inherited from the server
1722 * that created the client but can be overridden later.
1725 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
1726 GstRTSPSessionPool * pool)
1728 GstRTSPSessionPool *old;
1730 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1733 g_object_ref (pool);
1735 g_mutex_lock (&client->lock);
1736 old = client->session_pool;
1737 client->session_pool = pool;
1738 g_mutex_unlock (&client->lock);
1741 g_object_unref (old);
1745 * gst_rtsp_client_get_session_pool:
1746 * @client: a #GstRTSPClient
1748 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
1750 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
1752 GstRTSPSessionPool *
1753 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
1755 GstRTSPSessionPool *result;
1757 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1759 g_mutex_lock (&client->lock);
1760 if ((result = client->session_pool))
1761 g_object_ref (result);
1762 g_mutex_unlock (&client->lock);
1768 * gst_rtsp_client_set_mount_points:
1769 * @client: a #GstRTSPClient
1770 * @mounts: a #GstRTSPMountPoints
1772 * Set @mounts as the mount points for @client which it will use to map urls
1773 * to media streams. These mount points are usually inherited from the server that
1774 * created the client but can be overriden later.
1777 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
1778 GstRTSPMountPoints * mounts)
1780 GstRTSPMountPoints *old;
1782 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1785 g_object_ref (mounts);
1787 g_mutex_lock (&client->lock);
1788 old = client->mount_points;
1789 client->mount_points = mounts;
1790 g_mutex_unlock (&client->lock);
1793 g_object_unref (old);
1797 * gst_rtsp_client_get_mount_points:
1798 * @client: a #GstRTSPClient
1800 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
1802 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
1804 GstRTSPMountPoints *
1805 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
1807 GstRTSPMountPoints *result;
1809 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1811 g_mutex_lock (&client->lock);
1812 if ((result = client->mount_points))
1813 g_object_ref (result);
1814 g_mutex_unlock (&client->lock);
1820 * gst_rtsp_client_set_use_client_settings:
1821 * @client: a #GstRTSPClient
1822 * @use_client_settings: whether to use client settings for multicast
1824 * Use client transport settings (destination and ttl) for multicast.
1825 * When @use_client_settings is %FALSE, the server settings will be
1829 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
1830 gboolean use_client_settings)
1832 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1834 g_mutex_lock (&client->lock);
1835 client->use_client_settings = use_client_settings;
1836 g_mutex_unlock (&client->lock);
1840 * gst_rtsp_client_get_use_client_settings:
1841 * @client: a #GstRTSPClient
1843 * Check if client transport settings (destination and ttl) for multicast
1847 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
1851 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
1853 g_mutex_lock (&client->lock);
1854 res = client->use_client_settings;
1855 g_mutex_unlock (&client->lock);
1861 * gst_rtsp_client_set_auth:
1862 * @client: a #GstRTSPClient
1863 * @auth: a #GstRTSPAuth
1865 * configure @auth to be used as the authentication manager of @client.
1868 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
1872 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1875 g_object_ref (auth);
1877 g_mutex_lock (&client->lock);
1879 client->auth = auth;
1880 g_mutex_unlock (&client->lock);
1883 g_object_unref (old);
1888 * gst_rtsp_client_get_auth:
1889 * @client: a #GstRTSPClient
1891 * Get the #GstRTSPAuth used as the authentication manager of @client.
1893 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
1897 gst_rtsp_client_get_auth (GstRTSPClient * client)
1899 GstRTSPAuth *result;
1901 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1903 g_mutex_lock (&client->lock);
1904 if ((result = client->auth))
1905 g_object_ref (result);
1906 g_mutex_unlock (&client->lock);
1912 * gst_rtsp_client_set_send_func:
1913 * @client: a #GstRTSPClient
1914 * @func: a #GstRTSPClientSendFunc
1915 * @user_data: user data passed to @func
1916 * @notify: called when @user_data is no longer in use
1918 * Set @func as the callback that will be called when a new message needs to be
1919 * sent to the client. @user_data is passed to @func and @notify is called when
1920 * @user_data is no longer in use.
1923 gst_rtsp_client_set_send_func (GstRTSPClient * client,
1924 GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
1926 GDestroyNotify old_notify;
1929 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1931 g_mutex_lock (&client->lock);
1932 client->send_func = func;
1933 old_notify = client->send_notify;
1934 old_data = client->send_data;
1935 client->send_notify = notify;
1936 client->send_data = user_data;
1937 g_mutex_unlock (&client->lock);
1940 old_notify (old_data);
1944 * gst_rtsp_client_handle_message:
1945 * @client: a #GstRTSPClient
1946 * @message: an #GstRTSPMessage
1948 * Let the client handle @message.
1950 * Returns: a #GstRTSPResult.
1953 gst_rtsp_client_handle_message (GstRTSPClient * client,
1954 GstRTSPMessage * message)
1956 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
1957 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1959 switch (message->type) {
1960 case GST_RTSP_MESSAGE_REQUEST:
1961 handle_request (client, message);
1963 case GST_RTSP_MESSAGE_RESPONSE:
1965 case GST_RTSP_MESSAGE_DATA:
1966 handle_data (client, message);
1974 static GstRTSPResult
1975 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
1976 gboolean close, gpointer user_data)
1978 /* send the response and store the seq number so we can wait until it's
1979 * written to the client to close the connection */
1980 return gst_rtsp_watch_send_message (client->watch, message, close ?
1981 &client->close_seq : NULL);
1984 static GstRTSPResult
1985 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
1988 return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
1991 static GstRTSPResult
1992 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
1994 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1996 if (client->close_seq && client->close_seq == cseq) {
1997 client->close_seq = 0;
1998 close_connection (client);
2004 static GstRTSPResult
2005 closed (GstRTSPWatch * watch, gpointer user_data)
2007 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2008 const gchar *tunnelid;
2010 GST_INFO ("client %p: connection closed", client);
2012 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
2013 g_mutex_lock (&tunnels_lock);
2014 /* remove from tunnelids */
2015 g_hash_table_remove (tunnels, tunnelid);
2016 g_mutex_unlock (&tunnels_lock);
2022 static GstRTSPResult
2023 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2025 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2028 str = gst_rtsp_strresult (result);
2029 GST_INFO ("client %p: received an error %s", client, str);
2035 static GstRTSPResult
2036 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2037 GstRTSPMessage * message, guint id, gpointer user_data)
2039 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2042 str = gst_rtsp_strresult (result);
2044 ("client %p: received an error %s when handling message %p with id %d",
2045 client, str, message, id);
2052 remember_tunnel (GstRTSPClient * client)
2054 const gchar *tunnelid;
2056 /* store client in the pending tunnels */
2057 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
2058 if (tunnelid == NULL)
2061 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2063 /* we can't have two clients connecting with the same tunnelid */
2064 g_mutex_lock (&tunnels_lock);
2065 if (g_hash_table_lookup (tunnels, tunnelid))
2066 goto tunnel_existed;
2068 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2069 g_mutex_unlock (&tunnels_lock);
2076 GST_ERROR ("client %p: no tunnelid provided", client);
2081 g_mutex_unlock (&tunnels_lock);
2082 GST_ERROR ("client %p: tunnel session %s already existed", client,
2088 static GstRTSPStatusCode
2089 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2091 GstRTSPClient *client;
2093 client = GST_RTSP_CLIENT (user_data);
2095 GST_INFO ("client %p: tunnel start (connection %p)", client,
2096 client->connection);
2098 if (!remember_tunnel (client))
2101 return GST_RTSP_STS_OK;
2106 GST_ERROR ("client %p: error starting tunnel", client);
2107 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2111 static GstRTSPResult
2112 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2114 GstRTSPClient *client;
2116 client = GST_RTSP_CLIENT (user_data);
2118 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2119 client->connection);
2121 /* ignore error, it'll only be a problem when the client does a POST again */
2122 remember_tunnel (client);
2127 static GstRTSPResult
2128 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2130 const gchar *tunnelid;
2131 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2132 GstRTSPClient *oclient;
2134 GST_INFO ("client %p: tunnel complete", client);
2136 /* find previous tunnel */
2137 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
2138 if (tunnelid == NULL)
2141 g_mutex_lock (&tunnels_lock);
2142 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2145 /* remove the old client from the table. ref before because removing it will
2146 * remove the ref to it. */
2147 g_object_ref (oclient);
2148 g_hash_table_remove (tunnels, tunnelid);
2150 if (oclient->watch == NULL)
2152 g_mutex_unlock (&tunnels_lock);
2154 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2155 oclient->connection, client->connection);
2157 /* merge the tunnels into the first client */
2158 gst_rtsp_connection_do_tunnel (oclient->connection, client->connection);
2159 gst_rtsp_watch_reset (oclient->watch);
2160 g_object_unref (oclient);
2167 GST_ERROR ("client %p: no tunnelid provided", client);
2168 return GST_RTSP_ERROR;
2172 g_mutex_unlock (&tunnels_lock);
2173 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2174 return GST_RTSP_ERROR;
2178 g_mutex_unlock (&tunnels_lock);
2179 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2180 g_object_unref (oclient);
2181 return GST_RTSP_ERROR;
2185 static GstRTSPWatchFuncs watch_funcs = {
2197 client_watch_notify (GstRTSPClient * client)
2199 GST_INFO ("client %p: watch destroyed", client);
2200 client->watch = NULL;
2201 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2202 g_object_unref (client);
2206 setup_client (GstRTSPClient * client, GSocket * socket,
2207 GstRTSPConnection * conn, GError ** error)
2209 GSocket *read_socket;
2210 GSocketAddress *address;
2213 read_socket = gst_rtsp_connection_get_read_socket (conn);
2214 client->is_ipv6 = g_socket_get_family (socket) == G_SOCKET_FAMILY_IPV6;
2216 if (!(address = g_socket_get_remote_address (read_socket, error)))
2219 g_free (client->server_ip);
2220 /* keep the original ip that the client connected to */
2221 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2222 GInetAddress *iaddr;
2224 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2226 client->server_ip = g_inet_address_to_string (iaddr);
2227 g_object_unref (address);
2229 client->server_ip = g_strdup ("unknown");
2232 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2233 client->server_ip, client->is_ipv6);
2235 url = gst_rtsp_connection_get_url (conn);
2236 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2238 client->connection = conn;
2245 GST_ERROR ("could not get remote address %s", (*error)->message);
2251 * gst_rtsp_client_use_socket:
2252 * @client: a #GstRTSPClient
2253 * @socket: a #GSocket
2254 * @ip: the IP address of the remote client
2255 * @port: the port used by the other end
2256 * @initial_buffer: any zero terminated initial data that was already read from
2260 * Take an existing network socket and use it for an RTSP connection.
2262 * Returns: %TRUE on success.
2265 gst_rtsp_client_use_socket (GstRTSPClient * client, GSocket * socket,
2266 const gchar * ip, gint port, const gchar * initial_buffer, GError ** error)
2268 GstRTSPConnection *conn;
2271 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2272 g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
2274 GST_RTSP_CHECK (gst_rtsp_connection_create_from_socket (socket, ip, port,
2275 initial_buffer, &conn), no_connection);
2277 return setup_client (client, socket, conn, error);
2282 gchar *str = gst_rtsp_strresult (res);
2284 GST_ERROR ("could not create connection from socket %p: %s", socket, str);
2291 * gst_rtsp_client_accept:
2292 * @client: a #GstRTSPClient
2293 * @socket: a #GSocket
2294 * @context: the context to run in
2295 * @cancellable: a #GCancellable
2298 * Accept a new connection for @client on @socket.
2300 * Returns: %TRUE if the client could be accepted.
2303 gst_rtsp_client_accept (GstRTSPClient * client, GSocket * socket,
2304 GCancellable * cancellable, GError ** error)
2306 GstRTSPConnection *conn;
2309 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2310 g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
2312 /* a new client connected. */
2313 GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, cancellable),
2316 return setup_client (client, socket, conn, error);
2321 gchar *str = gst_rtsp_strresult (res);
2323 GST_ERROR ("Could not accept client on server socket %p: %s", socket, str);
2330 * gst_rtsp_client_attach:
2331 * @client: a #GstRTSPClient
2332 * @context: (allow-none): a #GMainContext
2334 * Attaches @client to @context. When the mainloop for @context is run, the
2335 * client will be dispatched. When @context is NULL, the default context will be
2338 * This function should be called when the client properties and urls are fully
2339 * configured and the client is ready to start.
2341 * Returns: the ID (greater than 0) for the source within the GMainContext.
2344 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2348 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2349 g_return_val_if_fail (client->watch == NULL, 0);
2351 /* create watch for the connection and attach */
2352 client->watch = gst_rtsp_watch_new (client->connection, &watch_funcs,
2353 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2354 gst_rtsp_client_set_send_func (client, do_send_message, NULL, NULL);
2356 GST_INFO ("attaching to context %p", context);
2357 res = gst_rtsp_watch_attach (client->watch, context);
2358 gst_rtsp_watch_unref (client->watch);