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_MEDIA_MAPPING 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_MEDIA_MAPPING,
97 g_param_spec_object ("media-mapping", "Media Mapping",
98 "The media mapping to use for client session",
99 GST_TYPE_RTSP_MEDIA_MAPPING,
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 client->use_client_settings = DEFAULT_USE_CLIENT_SETTINGS;
180 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
182 /* unlink all media managed in this session */
183 while (session->medias) {
184 GstRTSPSessionMedia *media = session->medias->data;
186 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
187 unlink_session_transports (client, session, media);
188 /* unmanage the media in the session. this will modify session->medias */
189 gst_rtsp_session_release_media (session, media);
194 client_cleanup_sessions (GstRTSPClient * client)
198 /* remove weak-ref from sessions */
199 for (sessions = client->sessions; sessions; sessions = g_list_next (sessions)) {
200 GstRTSPSession *session = (GstRTSPSession *) sessions->data;
201 g_object_weak_unref (G_OBJECT (session),
202 (GWeakNotify) client_session_finalized, client);
203 client_unlink_session (client, session);
205 g_list_free (client->sessions);
206 client->sessions = NULL;
209 /* A client is finalized when the connection is broken */
211 gst_rtsp_client_finalize (GObject * obj)
213 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
215 GST_INFO ("finalize client %p", client);
218 g_source_destroy ((GSource *) client->watch);
220 client_cleanup_sessions (client);
222 gst_rtsp_connection_free (client->connection);
223 if (client->session_pool)
224 g_object_unref (client->session_pool);
225 if (client->media_mapping)
226 g_object_unref (client->media_mapping);
228 g_object_unref (client->auth);
231 gst_rtsp_url_free (client->uri);
233 g_object_unref (client->media);
235 g_free (client->server_ip);
237 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
241 gst_rtsp_client_get_property (GObject * object, guint propid,
242 GValue * value, GParamSpec * pspec)
244 GstRTSPClient *client = GST_RTSP_CLIENT (object);
247 case PROP_SESSION_POOL:
248 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
250 case PROP_MEDIA_MAPPING:
251 g_value_take_object (value, gst_rtsp_client_get_media_mapping (client));
253 case PROP_USE_CLIENT_SETTINGS:
254 g_value_set_boolean (value,
255 gst_rtsp_client_get_use_client_settings (client));
258 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
263 gst_rtsp_client_set_property (GObject * object, guint propid,
264 const GValue * value, GParamSpec * pspec)
266 GstRTSPClient *client = GST_RTSP_CLIENT (object);
269 case PROP_SESSION_POOL:
270 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
272 case PROP_MEDIA_MAPPING:
273 gst_rtsp_client_set_media_mapping (client, g_value_get_object (value));
275 case PROP_USE_CLIENT_SETTINGS:
276 gst_rtsp_client_set_use_client_settings (client,
277 g_value_get_boolean (value));
280 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
285 * gst_rtsp_client_new:
287 * Create a new #GstRTSPClient instance.
289 * Returns: a new #GstRTSPClient
292 gst_rtsp_client_new (void)
294 GstRTSPClient *result;
296 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
302 send_response (GstRTSPClient * client, GstRTSPSession * session,
303 GstRTSPMessage * response)
305 gst_rtsp_message_add_header (response, GST_RTSP_HDR_SERVER,
306 "GStreamer RTSP server");
308 /* remove any previous header */
309 gst_rtsp_message_remove_header (response, GST_RTSP_HDR_SESSION, -1);
311 /* add the new session header for new session ids */
313 gst_rtsp_message_take_header (response, GST_RTSP_HDR_SESSION,
314 gst_rtsp_session_get_header (session));
317 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
318 gst_rtsp_message_dump (response);
321 gst_rtsp_watch_send_message (client->watch, response, NULL);
322 gst_rtsp_message_unset (response);
326 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
327 GstRTSPClientState * state)
329 gst_rtsp_message_init_response (state->response, code,
330 gst_rtsp_status_as_text (code), state->request);
332 send_response (client, NULL, state->response);
336 handle_unauthorized_request (GstRTSPClient * client, GstRTSPAuth * auth,
337 GstRTSPClientState * state)
339 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_UNAUTHORIZED,
340 gst_rtsp_status_as_text (GST_RTSP_STS_UNAUTHORIZED), state->request);
343 /* and let the authentication manager setup the auth tokens */
344 gst_rtsp_auth_setup_auth (auth, client, 0, state);
347 send_response (client, state->session, state->response);
352 compare_uri (const GstRTSPUrl * uri1, const GstRTSPUrl * uri2)
354 if (uri1 == NULL || uri2 == NULL)
357 if (strcmp (uri1->abspath, uri2->abspath))
363 /* this function is called to initially find the media for the DESCRIBE request
364 * but is cached for when the same client (without breaking the connection) is
365 * doing a setup for the exact same url. */
366 static GstRTSPMedia *
367 find_media (GstRTSPClient * client, GstRTSPClientState * state)
369 GstRTSPMediaFactory *factory;
373 if (!compare_uri (client->uri, state->uri)) {
374 /* remove any previously cached values before we try to construct a new
377 gst_rtsp_url_free (client->uri);
380 g_object_unref (client->media);
381 client->media = NULL;
383 if (!client->media_mapping)
386 /* find the factory for the uri first */
388 gst_rtsp_media_mapping_find_factory (client->media_mapping,
392 state->factory = factory;
394 /* check if we have access to the factory */
395 if ((auth = gst_rtsp_media_factory_get_auth (factory))) {
396 if (!gst_rtsp_auth_check (auth, client, 0, state))
399 g_object_unref (auth);
402 /* prepare the media and add it to the pipeline */
403 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
406 g_object_unref (factory);
408 state->factory = NULL;
410 /* set ipv6 on the media before preparing */
411 media->is_ipv6 = client->is_ipv6;
412 state->media = media;
414 /* prepare the media */
415 if (!(gst_rtsp_media_prepare (media)))
418 /* now keep track of the uri and the media */
419 client->uri = gst_rtsp_url_copy (state->uri);
420 client->media = media;
422 /* we have seen this uri before, used cached media */
423 media = client->media;
424 state->media = media;
425 GST_INFO ("reusing cached media %p", media);
429 g_object_ref (media);
436 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
441 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
446 handle_unauthorized_request (client, auth, state);
447 g_object_unref (factory);
448 g_object_unref (auth);
453 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
454 g_object_unref (factory);
459 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
460 g_object_unref (media);
466 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
468 GstRTSPMessage message = { 0 };
473 gst_rtsp_message_init_data (&message, channel);
475 /* FIXME, need some sort of iovec RTSPMessage here */
476 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
479 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
481 /* FIXME, client->watch could have been finalized here, we need to keep an
482 * extra refcount to the watch. */
483 gst_rtsp_watch_send_message (client->watch, &message, NULL);
485 gst_rtsp_message_steal_body (&message, &data, &usize);
486 gst_buffer_unmap (buffer, &map_info);
488 gst_rtsp_message_unset (&message);
494 link_transport (GstRTSPClient * client, GstRTSPSession * session,
495 GstRTSPStreamTransport * trans)
497 GST_DEBUG ("client %p: linking transport %p", client, trans);
498 gst_rtsp_stream_transport_set_callbacks (trans,
499 (GstRTSPSendFunc) do_send_data,
500 (GstRTSPSendFunc) do_send_data, client, NULL);
502 client->transports = g_list_prepend (client->transports, trans);
504 /* make sure our session can't expire */
505 gst_rtsp_session_prevent_expire (session);
509 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
510 GstRTSPStreamTransport * trans)
512 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
513 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
515 client->transports = g_list_remove (client->transports, trans);
517 /* our session can now expire */
518 gst_rtsp_session_allow_expire (session);
522 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
523 GstRTSPSessionMedia * media)
527 n_streams = gst_rtsp_media_n_streams (media->media);
528 for (i = 0; i < n_streams; i++) {
529 GstRTSPStreamTransport *trans;
530 GstRTSPTransport *tr;
532 /* get the transport, if there is no transport configured, skip this stream */
533 trans = gst_rtsp_session_media_get_transport (media, i);
537 tr = trans->transport;
539 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
540 /* for TCP, unlink the stream from the TCP connection of the client */
541 unlink_transport (client, session, trans);
547 close_connection (GstRTSPClient * client)
549 const gchar *tunnelid;
551 GST_DEBUG ("client %p: closing connection", client);
553 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
554 g_mutex_lock (&tunnels_lock);
555 /* remove from tunnelids */
556 g_hash_table_remove (tunnels, tunnelid);
557 g_mutex_unlock (&tunnels_lock);
560 gst_rtsp_connection_close (client->connection);
564 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
566 GstRTSPSession *session;
567 GstRTSPSessionMedia *media;
568 GstRTSPStatusCode code;
573 session = state->session;
575 /* get a handle to the configuration of the media in the session */
576 media = gst_rtsp_session_get_media (session, state->uri);
580 state->sessmedia = media;
582 /* unlink the all TCP callbacks */
583 unlink_session_transports (client, session, media);
585 /* remove the session from the watched sessions */
586 g_object_weak_unref (G_OBJECT (session),
587 (GWeakNotify) client_session_finalized, client);
588 client->sessions = g_list_remove (client->sessions, session);
590 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
592 /* unmanage the media in the session, returns false if all media session
594 if (!gst_rtsp_session_release_media (session, media)) {
595 /* remove the session */
596 gst_rtsp_session_pool_remove (client->session_pool, session);
598 /* construct the response now */
599 code = GST_RTSP_STS_OK;
600 gst_rtsp_message_init_response (state->response, code,
601 gst_rtsp_status_as_text (code), state->request);
603 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONNECTION,
606 send_response (client, session, state->response);
608 /* we emit the signal before closing the connection */
609 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
612 close_connection (client);
619 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
624 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
630 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
636 res = gst_rtsp_message_get_body (state->request, &data, &size);
637 if (res != GST_RTSP_OK)
641 /* no body, keep-alive request */
642 send_generic_response (client, GST_RTSP_STS_OK, state);
644 /* there is a body, handle the params */
645 res = gst_rtsp_params_get (client, state);
646 if (res != GST_RTSP_OK)
649 send_response (client, state->session, state->response);
652 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
660 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
666 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
672 res = gst_rtsp_message_get_body (state->request, &data, &size);
673 if (res != GST_RTSP_OK)
677 /* no body, keep-alive request */
678 send_generic_response (client, GST_RTSP_STS_OK, state);
680 /* there is a body, handle the params */
681 res = gst_rtsp_params_set (client, state);
682 if (res != GST_RTSP_OK)
685 send_response (client, state->session, state->response);
688 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
696 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
702 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
704 GstRTSPSession *session;
705 GstRTSPSessionMedia *media;
706 GstRTSPStatusCode code;
708 if (!(session = state->session))
711 /* get a handle to the configuration of the media in the session */
712 media = gst_rtsp_session_get_media (session, state->uri);
716 state->sessmedia = media;
718 /* the session state must be playing or recording */
719 if (media->state != GST_RTSP_STATE_PLAYING &&
720 media->state != GST_RTSP_STATE_RECORDING)
723 /* unlink the all TCP callbacks */
724 unlink_session_transports (client, session, media);
726 /* then pause sending */
727 gst_rtsp_session_media_set_state (media, GST_STATE_PAUSED);
729 /* construct the response now */
730 code = GST_RTSP_STS_OK;
731 gst_rtsp_message_init_response (state->response, code,
732 gst_rtsp_status_as_text (code), state->request);
734 send_response (client, session, state->response);
736 /* the state is now READY */
737 media->state = GST_RTSP_STATE_READY;
739 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
747 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
752 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
757 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
764 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
766 GstRTSPSession *session;
767 GstRTSPSessionMedia *media;
768 GstRTSPStatusCode code;
770 guint n_streams, i, infocount;
772 GstRTSPTimeRange *range;
775 if (!(session = state->session))
778 /* get a handle to the configuration of the media in the session */
779 media = gst_rtsp_session_get_media (session, state->uri);
783 state->sessmedia = media;
785 /* the session state must be playing or ready */
786 if (media->state != GST_RTSP_STATE_PLAYING &&
787 media->state != GST_RTSP_STATE_READY)
790 /* parse the range header if we have one */
792 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
793 if (res == GST_RTSP_OK) {
794 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
795 /* we have a range, seek to the position */
796 gst_rtsp_media_seek (media->media, range);
797 gst_rtsp_range_free (range);
801 /* grab RTPInfo from the payloaders now */
802 rtpinfo = g_string_new ("");
804 n_streams = gst_rtsp_media_n_streams (media->media);
805 for (i = 0, infocount = 0; i < n_streams; i++) {
806 GstRTSPStreamTransport *trans;
807 GstRTSPTransport *tr;
811 /* get the transport, if there is no transport configured, skip this stream */
812 trans = gst_rtsp_session_media_get_transport (media, i);
814 GST_INFO ("stream %d is not configured", i);
817 tr = trans->transport;
819 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
820 /* for TCP, link the stream to the TCP connection of the client */
821 link_transport (client, session, trans);
824 if (gst_rtsp_stream_get_rtpinfo (trans->stream, &rtptime, &seq)) {
826 g_string_append (rtpinfo, ", ");
828 uristr = gst_rtsp_url_get_request_uri (state->uri);
829 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
830 uristr, i, seq, rtptime);
835 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
839 /* construct the response now */
840 code = GST_RTSP_STS_OK;
841 gst_rtsp_message_init_response (state->response, code,
842 gst_rtsp_status_as_text (code), state->request);
844 /* add the RTP-Info header */
846 str = g_string_free (rtpinfo, FALSE);
847 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
849 g_string_free (rtpinfo, TRUE);
853 str = gst_rtsp_media_get_range_string (media->media, TRUE);
854 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
856 send_response (client, session, state->response);
858 /* start playing after sending the request */
859 gst_rtsp_session_media_set_state (media, GST_STATE_PLAYING);
861 media->state = GST_RTSP_STATE_PLAYING;
863 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
871 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
876 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
881 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
888 do_keepalive (GstRTSPSession * session)
890 GST_INFO ("keep session %p alive", session);
891 gst_rtsp_session_touch (session);
894 /* parse @transport and return a valid transport in @tr. only transports
895 * from @supported are returned. Returns FALSE if no valid transport
898 parse_transport (const char *transport, GstRTSPLowerTrans supported,
899 GstRTSPTransport * tr)
906 gst_rtsp_transport_init (tr);
908 GST_DEBUG ("parsing transports %s", transport);
910 transports = g_strsplit (transport, ",", 0);
912 /* loop through the transports, try to parse */
913 for (i = 0; transports[i]; i++) {
914 res = gst_rtsp_transport_parse (transports[i], tr);
915 if (res != GST_RTSP_OK) {
916 /* no valid transport, search some more */
917 GST_WARNING ("could not parse transport %s", transports[i]);
921 /* we have a transport, see if it's RTP/AVP */
922 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
923 GST_WARNING ("invalid transport %s", transports[i]);
927 if (!(tr->lower_transport & supported)) {
928 GST_WARNING ("unsupported transport %s", transports[i]);
932 /* we have a valid transport */
933 GST_INFO ("found valid transport %s", transports[i]);
938 gst_rtsp_transport_init (tr);
940 g_strfreev (transports);
946 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
947 GstRTSPMessage * request)
949 gchar *blocksize_str;
952 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
953 &blocksize_str, 0) == GST_RTSP_OK) {
957 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
958 if (end == blocksize_str) {
959 GST_ERROR ("failed to parse blocksize");
962 /* we don't want to change the mtu when this media
963 * can be shared because it impacts other clients */
964 if (gst_rtsp_media_is_shared (media))
967 if (blocksize > G_MAXUINT)
968 blocksize = G_MAXUINT;
969 gst_rtsp_stream_set_mtu (stream, blocksize);
976 configure_client_transport (GstRTSPClient * client, GstRTSPClientState * state,
977 GstRTSPTransport * ct)
979 /* we have a valid transport now, set the destination of the client. */
980 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
981 if (ct->destination == NULL || !client->use_client_settings) {
982 GstRTSPAddress *addr;
984 addr = gst_rtsp_stream_get_address (state->stream);
988 g_free (ct->destination);
989 ct->destination = g_strdup (addr->address);
990 ct->port.min = addr->port;
991 ct->port.max = addr->port + addr->n_ports - 1;
997 url = gst_rtsp_connection_get_url (client->connection);
998 g_free (ct->destination);
999 ct->destination = g_strdup (url->host);
1001 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1002 /* check if the client selected channels for TCP */
1003 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1004 gst_rtsp_session_media_alloc_channels (state->sessmedia,
1014 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1019 static GstRTSPTransport *
1020 make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
1021 GstRTSPTransport * ct)
1023 GstRTSPTransport *st;
1025 /* prepare the server transport */
1026 gst_rtsp_transport_new (&st);
1028 st->trans = ct->trans;
1029 st->profile = ct->profile;
1030 st->lower_transport = ct->lower_transport;
1032 switch (st->lower_transport) {
1033 case GST_RTSP_LOWER_TRANS_UDP:
1034 st->client_port = ct->client_port;
1035 st->server_port = state->stream->server_port;
1037 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1038 st->port = ct->port;
1039 st->destination = g_strdup (ct->destination);
1042 case GST_RTSP_LOWER_TRANS_TCP:
1043 st->interleaved = ct->interleaved;
1048 if (state->stream->session)
1049 g_object_get (state->stream->session, "internal-ssrc", &st->ssrc, NULL);
1055 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
1060 GstRTSPTransport *ct, *st;
1061 GstRTSPLowerTrans supported;
1062 GstRTSPStatusCode code;
1063 GstRTSPSession *session;
1064 GstRTSPStreamTransport *trans;
1065 gchar *trans_str, *pos;
1067 GstRTSPSessionMedia *sessmedia;
1068 GstRTSPMedia *media;
1069 GstRTSPStream *stream;
1073 /* the uri contains the stream number we added in the SDP config, which is
1074 * always /stream=%d so we need to strip that off
1075 * parse the stream we need to configure, look for the stream in the abspath
1076 * first and then in the query. */
1077 if (uri->abspath == NULL || !(pos = strstr (uri->abspath, "/stream="))) {
1078 if (uri->query == NULL || !(pos = strstr (uri->query, "/stream=")))
1082 /* we can mofify the parsed uri in place */
1085 pos += strlen ("/stream=");
1086 if (sscanf (pos, "%u", &streamid) != 1)
1089 /* parse the transport */
1091 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
1093 if (res != GST_RTSP_OK)
1096 gst_rtsp_transport_new (&ct);
1098 /* our supported transports */
1099 supported = GST_RTSP_LOWER_TRANS_UDP |
1100 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
1102 /* parse and find a usable supported transport */
1103 if (!parse_transport (transport, supported, ct))
1104 goto unsupported_transports;
1106 /* we create the session after parsing stuff so that we don't make
1107 * a session for malformed requests */
1108 if (client->session_pool == NULL)
1111 session = state->session;
1114 g_object_ref (session);
1115 /* get a handle to the configuration of the media in the session, this can
1116 * return NULL if this is a new url to manage in this session. */
1117 sessmedia = gst_rtsp_session_get_media (session, uri);
1119 /* create a session if this fails we probably reached our session limit or
1121 if (!(session = gst_rtsp_session_pool_create (client->session_pool)))
1122 goto service_unavailable;
1124 state->session = session;
1126 /* we need a new media configuration in this session */
1130 /* we have no media, find one and manage it */
1131 if (sessmedia == NULL) {
1132 /* get a handle to the configuration of the media in the session */
1133 if ((media = find_media (client, state))) {
1134 /* manage the media in our session now */
1135 sessmedia = gst_rtsp_session_manage_media (session, uri, media);
1139 /* if we stil have no media, error */
1140 if (sessmedia == NULL)
1143 state->sessmedia = sessmedia;
1144 state->media = media = sessmedia->media;
1146 /* now get the stream */
1147 stream = gst_rtsp_media_get_stream (media, streamid);
1151 state->stream = stream;
1153 /* set blocksize on this stream */
1154 if (!handle_blocksize (media, stream, state->request))
1155 goto invalid_blocksize;
1157 /* update the client transport */
1158 if (!configure_client_transport (client, state, ct))
1159 goto unsupported_client_transport;
1161 /* set in the session media transport */
1162 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1164 /* configure keepalive for this transport */
1165 gst_rtsp_stream_transport_set_keepalive (trans,
1166 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1168 /* create and serialize the server transport */
1169 st = make_server_transport (client, state, ct);
1170 trans_str = gst_rtsp_transport_as_text (st);
1171 gst_rtsp_transport_free (st);
1173 /* construct the response now */
1174 code = GST_RTSP_STS_OK;
1175 gst_rtsp_message_init_response (state->response, code,
1176 gst_rtsp_status_as_text (code), state->request);
1178 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1182 send_response (client, session, state->response);
1184 /* update the state */
1185 switch (sessmedia->state) {
1186 case GST_RTSP_STATE_PLAYING:
1187 case GST_RTSP_STATE_RECORDING:
1188 case GST_RTSP_STATE_READY:
1189 /* no state change */
1192 sessmedia->state = GST_RTSP_STATE_READY;
1195 g_object_unref (session);
1197 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST],
1205 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1210 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1211 g_object_unref (session);
1212 gst_rtsp_transport_free (ct);
1217 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1218 g_object_unref (session);
1219 gst_rtsp_transport_free (ct);
1222 unsupported_client_transport:
1224 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1225 g_object_unref (session);
1226 gst_rtsp_transport_free (ct);
1231 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1234 unsupported_transports:
1236 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1237 gst_rtsp_transport_free (ct);
1242 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1243 gst_rtsp_transport_free (ct);
1246 service_unavailable:
1248 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1249 gst_rtsp_transport_free (ct);
1254 static GstSDPMessage *
1255 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1260 GstRTSPLowerTrans protocols;
1262 gst_sdp_message_new (&sdp);
1264 /* some standard things first */
1265 gst_sdp_message_set_version (sdp, "0");
1267 if (client->is_ipv6)
1272 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1275 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1276 gst_sdp_message_set_information (sdp, "rtsp-server");
1277 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1278 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1279 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1280 gst_sdp_message_add_attribute (sdp, "control", "*");
1282 info.server_proto = proto;
1283 protocols = gst_rtsp_media_get_protocols (media);
1284 if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST)
1286 info.server_ip = gst_rtsp_media_get_multicast_group (media);
1288 info.server_ip = g_strdup (client->server_ip);
1291 info.server_ip = g_strdup (client->server_ip);
1293 /* create an SDP for the media object */
1294 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1297 g_free (info.server_ip);
1304 g_free (info.server_ip);
1305 gst_sdp_message_free (sdp);
1310 /* for the describe we must generate an SDP */
1312 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1317 gchar *str, *content_base;
1318 GstRTSPMedia *media;
1319 GstRTSPClientClass *klass;
1321 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1323 /* check what kind of format is accepted, we don't really do anything with it
1324 * and always return SDP for now. */
1329 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1331 if (res == GST_RTSP_ENOTIMPL)
1334 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1338 /* find the media object for the uri */
1339 if (!(media = find_media (client, state)))
1342 /* create an SDP for the media object on this client */
1343 if (!(sdp = klass->create_sdp (client, media)))
1346 g_object_unref (media);
1348 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1349 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1351 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1354 /* content base for some clients that might screw up creating the setup uri */
1355 str = gst_rtsp_url_get_request_uri (state->uri);
1356 str_len = strlen (str);
1358 /* check for trailing '/' and append one */
1359 if (str[str_len - 1] != '/') {
1360 content_base = g_malloc (str_len + 2);
1361 memcpy (content_base, str, str_len);
1362 content_base[str_len] = '/';
1363 content_base[str_len + 1] = '\0';
1369 GST_INFO ("adding content-base: %s", content_base);
1371 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1373 g_free (content_base);
1375 /* add SDP to the response body */
1376 str = gst_sdp_message_as_text (sdp);
1377 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1378 gst_sdp_message_free (sdp);
1380 send_response (client, state->session, state->response);
1382 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1390 /* error reply is already sent */
1395 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1396 g_object_unref (media);
1402 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1404 GstRTSPMethod options;
1407 options = GST_RTSP_DESCRIBE |
1412 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1414 str = gst_rtsp_options_as_text (options);
1416 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1417 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1419 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1422 send_response (client, state->session, state->response);
1424 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1430 /* remove duplicate and trailing '/' */
1432 sanitize_uri (GstRTSPUrl * uri)
1436 gboolean have_slash, prev_slash;
1438 s = d = uri->abspath;
1439 len = strlen (uri->abspath);
1443 for (i = 0; i < len; i++) {
1444 have_slash = s[i] == '/';
1446 if (!have_slash || !prev_slash)
1448 prev_slash = have_slash;
1450 len = d - uri->abspath;
1451 /* don't remove the first slash if that's the only thing left */
1452 if (len > 1 && *(d - 1) == '/')
1458 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1460 GST_INFO ("client %p: session %p finished", client, session);
1462 /* unlink all media managed in this session */
1463 client_unlink_session (client, session);
1465 /* remove the session */
1466 if (!(client->sessions = g_list_remove (client->sessions, session))) {
1467 GST_INFO ("client %p: all sessions finalized, close the connection",
1469 close_connection (client);
1474 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
1478 for (walk = client->sessions; walk; walk = g_list_next (walk)) {
1479 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
1481 /* we already know about this session */
1482 if (msession == session)
1486 GST_INFO ("watching session %p", session);
1488 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
1490 client->sessions = g_list_prepend (client->sessions, session);
1492 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1497 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1499 GstRTSPMethod method;
1500 const gchar *uristr;
1502 GstRTSPVersion version;
1504 GstRTSPSession *session;
1505 GstRTSPClientState state = { NULL };
1506 GstRTSPMessage response = { 0 };
1509 state.request = request;
1510 state.response = &response;
1512 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1513 gst_rtsp_message_dump (request);
1516 GST_INFO ("client %p: received a request", client);
1518 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1520 if (version != GST_RTSP_VERSION_1_0) {
1521 /* we can only handle 1.0 requests */
1522 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1526 state.method = method;
1528 /* we always try to parse the url first */
1529 if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
1530 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1534 /* sanitize the uri */
1538 /* get the session if there is any */
1539 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1540 if (res == GST_RTSP_OK) {
1541 if (client->session_pool == NULL)
1544 /* we had a session in the request, find it again */
1545 if (!(session = gst_rtsp_session_pool_find (client->session_pool, sessid)))
1546 goto session_not_found;
1548 /* we add the session to the client list of watched sessions. When a session
1549 * disappears because it times out, we will be notified. If all sessions are
1550 * gone, we will close the connection */
1551 client_watch_session (client, session);
1555 state.session = session;
1558 if (!gst_rtsp_auth_check (client->auth, client, 0, &state))
1559 goto not_authorized;
1562 /* now see what is asked and dispatch to a dedicated handler */
1564 case GST_RTSP_OPTIONS:
1565 handle_options_request (client, &state);
1567 case GST_RTSP_DESCRIBE:
1568 handle_describe_request (client, &state);
1570 case GST_RTSP_SETUP:
1571 handle_setup_request (client, &state);
1574 handle_play_request (client, &state);
1576 case GST_RTSP_PAUSE:
1577 handle_pause_request (client, &state);
1579 case GST_RTSP_TEARDOWN:
1580 handle_teardown_request (client, &state);
1582 case GST_RTSP_SET_PARAMETER:
1583 handle_set_param_request (client, &state);
1585 case GST_RTSP_GET_PARAMETER:
1586 handle_get_param_request (client, &state);
1588 case GST_RTSP_ANNOUNCE:
1589 case GST_RTSP_RECORD:
1590 case GST_RTSP_REDIRECT:
1591 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1593 case GST_RTSP_INVALID:
1595 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1599 g_object_unref (session);
1601 gst_rtsp_url_free (uri);
1607 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, &state);
1612 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1617 handle_unauthorized_request (client, client->auth, &state);
1623 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1633 /* find the stream for this message */
1634 res = gst_rtsp_message_parse_data (message, &channel);
1635 if (res != GST_RTSP_OK)
1638 gst_rtsp_message_steal_body (message, &data, &size);
1640 buffer = gst_buffer_new_wrapped (data, size);
1643 for (walk = client->transports; walk; walk = g_list_next (walk)) {
1644 GstRTSPStreamTransport *trans;
1645 GstRTSPStream *stream;
1646 GstRTSPTransport *tr;
1650 /* we only add clients with a transport to the list */
1651 tr = trans->transport;
1652 stream = trans->stream;
1654 /* check for TCP transport */
1655 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1656 /* dispatch to the stream based on the channel number */
1657 if (tr->interleaved.min == channel) {
1658 gst_rtsp_stream_recv_rtp (stream, buffer);
1661 } else if (tr->interleaved.max == channel) {
1662 gst_rtsp_stream_recv_rtcp (stream, buffer);
1669 gst_buffer_unref (buffer);
1673 * gst_rtsp_client_set_session_pool:
1674 * @client: a #GstRTSPClient
1675 * @pool: a #GstRTSPSessionPool
1677 * Set @pool as the sessionpool for @client which it will use to find
1678 * or allocate sessions. the sessionpool is usually inherited from the server
1679 * that created the client but can be overridden later.
1682 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
1683 GstRTSPSessionPool * pool)
1685 GstRTSPSessionPool *old;
1687 old = client->session_pool;
1690 g_object_ref (pool);
1691 client->session_pool = pool;
1693 g_object_unref (old);
1698 * gst_rtsp_client_get_session_pool:
1699 * @client: a #GstRTSPClient
1701 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
1703 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
1705 GstRTSPSessionPool *
1706 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
1708 GstRTSPSessionPool *result;
1710 if ((result = client->session_pool))
1711 g_object_ref (result);
1717 * gst_rtsp_client_set_server:
1718 * @client: a #GstRTSPClient
1719 * @server: a #GstRTSPServer
1721 * Set @server as the server that created @client.
1724 gst_rtsp_client_set_server (GstRTSPClient * client, GstRTSPServer * server)
1728 old = client->server;
1729 if (old != server) {
1731 g_object_ref (server);
1732 client->server = server;
1734 g_object_unref (old);
1739 * gst_rtsp_client_get_server:
1740 * @client: a #GstRTSPClient
1742 * Get the #GstRTSPServer object that @client was created from.
1744 * Returns: (transfer full): a #GstRTSPServer, unref after usage.
1747 gst_rtsp_client_get_server (GstRTSPClient * client)
1749 GstRTSPServer *result;
1751 if ((result = client->server))
1752 g_object_ref (result);
1758 * gst_rtsp_client_set_media_mapping:
1759 * @client: a #GstRTSPClient
1760 * @mapping: a #GstRTSPMediaMapping
1762 * Set @mapping as the media mapping for @client which it will use to map urls
1763 * to media streams. These mapping is usually inherited from the server that
1764 * created the client but can be overriden later.
1767 gst_rtsp_client_set_media_mapping (GstRTSPClient * client,
1768 GstRTSPMediaMapping * mapping)
1770 GstRTSPMediaMapping *old;
1772 old = client->media_mapping;
1774 if (old != mapping) {
1776 g_object_ref (mapping);
1777 client->media_mapping = mapping;
1779 g_object_unref (old);
1784 * gst_rtsp_client_get_media_mapping:
1785 * @client: a #GstRTSPClient
1787 * Get the #GstRTSPMediaMapping object that @client uses to manage its sessions.
1789 * Returns: (transfer full): a #GstRTSPMediaMapping, unref after usage.
1791 GstRTSPMediaMapping *
1792 gst_rtsp_client_get_media_mapping (GstRTSPClient * client)
1794 GstRTSPMediaMapping *result;
1796 if ((result = client->media_mapping))
1797 g_object_ref (result);
1803 * gst_rtsp_client_set_use_client_settings:
1804 * @client: a #GstRTSPClient
1805 * @use_client_settings: whether to use client settings for multicast
1807 * Use client transport settings (destination and ttl) for multicast.
1808 * When @use_client_settings is %FALSE, the server settings will be
1812 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
1813 gboolean use_client_settings)
1815 client->use_client_settings = use_client_settings;
1819 * gst_rtsp_client_get_use_client_settings:
1820 * @client: a #GstRTSPClient
1822 * Check if client transport settings (destination and ttl) for multicast
1826 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
1828 return client->use_client_settings;
1832 * gst_rtsp_client_set_auth:
1833 * @client: a #GstRTSPClient
1834 * @auth: a #GstRTSPAuth
1836 * configure @auth to be used as the authentication manager of @client.
1839 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
1843 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1849 g_object_ref (auth);
1850 client->auth = auth;
1852 g_object_unref (old);
1858 * gst_rtsp_client_get_auth:
1859 * @client: a #GstRTSPClient
1861 * Get the #GstRTSPAuth used as the authentication manager of @client.
1863 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
1867 gst_rtsp_client_get_auth (GstRTSPClient * client)
1869 GstRTSPAuth *result;
1871 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1873 if ((result = client->auth))
1874 g_object_ref (result);
1879 static GstRTSPResult
1880 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
1883 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1885 switch (message->type) {
1886 case GST_RTSP_MESSAGE_REQUEST:
1887 handle_request (client, message);
1889 case GST_RTSP_MESSAGE_RESPONSE:
1891 case GST_RTSP_MESSAGE_DATA:
1892 handle_data (client, message);
1900 static GstRTSPResult
1901 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
1903 /* GstRTSPClient *client; */
1905 /* client = GST_RTSP_CLIENT (user_data); */
1907 /* GST_INFO ("client %p: sent a message with cseq %d", client, cseq); */
1912 static GstRTSPResult
1913 closed (GstRTSPWatch * watch, gpointer user_data)
1915 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1916 const gchar *tunnelid;
1918 GST_INFO ("client %p: connection closed", client);
1920 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
1921 g_mutex_lock (&tunnels_lock);
1922 /* remove from tunnelids */
1923 g_hash_table_remove (tunnels, tunnelid);
1924 g_mutex_unlock (&tunnels_lock);
1930 static GstRTSPResult
1931 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
1933 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1936 str = gst_rtsp_strresult (result);
1937 GST_INFO ("client %p: received an error %s", client, str);
1943 static GstRTSPResult
1944 error_full (GstRTSPWatch * watch, GstRTSPResult result,
1945 GstRTSPMessage * message, guint id, gpointer user_data)
1947 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1950 str = gst_rtsp_strresult (result);
1952 ("client %p: received an error %s when handling message %p with id %d",
1953 client, str, message, id);
1960 remember_tunnel (GstRTSPClient * client)
1962 const gchar *tunnelid;
1964 /* store client in the pending tunnels */
1965 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
1966 if (tunnelid == NULL)
1969 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
1971 /* we can't have two clients connecting with the same tunnelid */
1972 g_mutex_lock (&tunnels_lock);
1973 if (g_hash_table_lookup (tunnels, tunnelid))
1974 goto tunnel_existed;
1976 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
1977 g_mutex_unlock (&tunnels_lock);
1984 GST_ERROR ("client %p: no tunnelid provided", client);
1989 g_mutex_unlock (&tunnels_lock);
1990 GST_ERROR ("client %p: tunnel session %s already existed", client,
1996 static GstRTSPStatusCode
1997 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
1999 GstRTSPClient *client;
2001 client = GST_RTSP_CLIENT (user_data);
2003 GST_INFO ("client %p: tunnel start (connection %p)", client,
2004 client->connection);
2006 if (!remember_tunnel (client))
2009 return GST_RTSP_STS_OK;
2014 GST_ERROR ("client %p: error starting tunnel", client);
2015 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2019 static GstRTSPResult
2020 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2022 GstRTSPClient *client;
2024 client = GST_RTSP_CLIENT (user_data);
2026 GST_INFO ("client %p: tunnel lost (connection %p)", client,
2027 client->connection);
2029 /* ignore error, it'll only be a problem when the client does a POST again */
2030 remember_tunnel (client);
2035 static GstRTSPResult
2036 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2038 const gchar *tunnelid;
2039 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2040 GstRTSPClient *oclient;
2042 GST_INFO ("client %p: tunnel complete", client);
2044 /* find previous tunnel */
2045 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
2046 if (tunnelid == NULL)
2049 g_mutex_lock (&tunnels_lock);
2050 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2053 /* remove the old client from the table. ref before because removing it will
2054 * remove the ref to it. */
2055 g_object_ref (oclient);
2056 g_hash_table_remove (tunnels, tunnelid);
2058 if (oclient->watch == NULL)
2060 g_mutex_unlock (&tunnels_lock);
2062 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2063 oclient->connection, client->connection);
2065 /* merge the tunnels into the first client */
2066 gst_rtsp_connection_do_tunnel (oclient->connection, client->connection);
2067 gst_rtsp_watch_reset (oclient->watch);
2068 g_object_unref (oclient);
2075 GST_INFO ("client %p: no tunnelid provided", client);
2076 return GST_RTSP_ERROR;
2080 g_mutex_unlock (&tunnels_lock);
2081 GST_INFO ("client %p: tunnel session %s not found", client, tunnelid);
2082 return GST_RTSP_ERROR;
2086 g_mutex_unlock (&tunnels_lock);
2087 GST_INFO ("client %p: tunnel session %s was closed", client, tunnelid);
2088 g_object_unref (oclient);
2089 return GST_RTSP_ERROR;
2093 static GstRTSPWatchFuncs watch_funcs = {
2105 client_watch_notify (GstRTSPClient * client)
2107 GST_INFO ("client %p: watch destroyed", client);
2108 client->watch = NULL;
2109 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2110 g_object_unref (client);
2114 setup_client (GstRTSPClient * client, GSocket * socket,
2115 GstRTSPConnection * conn, GError ** error)
2117 GSocket *read_socket;
2118 GSocketAddress *address;
2121 read_socket = gst_rtsp_connection_get_read_socket (conn);
2122 client->is_ipv6 = g_socket_get_family (socket) == G_SOCKET_FAMILY_IPV6;
2124 if (!(address = g_socket_get_remote_address (read_socket, error)))
2127 g_free (client->server_ip);
2128 /* keep the original ip that the client connected to */
2129 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2130 GInetAddress *iaddr;
2132 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2134 client->server_ip = g_inet_address_to_string (iaddr);
2135 g_object_unref (address);
2137 client->server_ip = g_strdup ("unknown");
2140 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2141 client->server_ip, client->is_ipv6);
2143 url = gst_rtsp_connection_get_url (conn);
2144 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2146 client->connection = conn;
2153 GST_ERROR ("could not get remote address %s", (*error)->message);
2159 * gst_rtsp_client_use_socket:
2160 * @client: a #GstRTSPClient
2161 * @socket: a #GSocket
2162 * @ip: the IP address of the remote client
2163 * @port: the port used by the other end
2164 * @initial_buffer: any zero terminated initial data that was already read from
2168 * Take an existing network socket and use it for an RTSP connection.
2170 * Returns: %TRUE on success.
2173 gst_rtsp_client_use_socket (GstRTSPClient * client, GSocket * socket,
2174 const gchar * ip, gint port, const gchar * initial_buffer, GError ** error)
2176 GstRTSPConnection *conn;
2179 GST_RTSP_CHECK (gst_rtsp_connection_create_from_socket (socket, ip, port,
2180 initial_buffer, &conn), no_connection);
2182 return setup_client (client, socket, conn, error);
2187 gchar *str = gst_rtsp_strresult (res);
2189 GST_ERROR ("could not create connection from socket %p: %s", socket, str);
2196 * gst_rtsp_client_accept:
2197 * @client: a #GstRTSPClient
2198 * @socket: a #GSocket
2199 * @context: the context to run in
2200 * @cancellable: a #GCancellable
2203 * Accept a new connection for @client on @socket.
2205 * Returns: %TRUE if the client could be accepted.
2208 gst_rtsp_client_accept (GstRTSPClient * client, GSocket * socket,
2209 GCancellable * cancellable, GError ** error)
2211 GstRTSPConnection *conn;
2214 /* a new client connected. */
2215 GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, cancellable),
2218 return setup_client (client, socket, conn, error);
2223 gchar *str = gst_rtsp_strresult (res);
2225 GST_ERROR ("Could not accept client on server socket %p: %s", socket, str);
2232 * gst_rtsp_client_attach:
2233 * @client: a #GstRTSPClient
2234 * @context: (allow-none): a #GMainContext
2236 * Attaches @client to @context. When the mainloop for @context is run, the
2237 * client will be dispatched. When @context is NULL, the default context will be
2240 * This function should be called when the client properties and urls are fully
2241 * configured and the client is ready to start.
2243 * Returns: the ID (greater than 0) for the source within the GMainContext.
2246 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2250 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2251 g_return_val_if_fail (client->watch == NULL, 0);
2253 /* create watch for the connection and attach */
2254 client->watch = gst_rtsp_watch_new (client->connection, &watch_funcs,
2255 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2257 GST_INFO ("attaching to context %p", context);
2258 res = gst_rtsp_watch_attach (client->watch, context);
2259 gst_rtsp_watch_unref (client->watch);