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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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_streams (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 (g_list_length (session->medias) > 0) {
184 GstRTSPSessionMedia *media = g_list_first (session->medias)->data;
186 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
187 unlink_session_streams (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 */
315 if (session->timeout != 60)
317 g_strdup_printf ("%s; timeout=%d", session->sessionid,
320 str = g_strdup (session->sessionid);
322 gst_rtsp_message_take_header (response, GST_RTSP_HDR_SESSION, str);
325 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
326 gst_rtsp_message_dump (response);
329 gst_rtsp_watch_send_message (client->watch, response, NULL);
330 gst_rtsp_message_unset (response);
334 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
335 GstRTSPClientState * state)
337 gst_rtsp_message_init_response (state->response, code,
338 gst_rtsp_status_as_text (code), state->request);
340 send_response (client, NULL, state->response);
344 handle_unauthorized_request (GstRTSPClient * client, GstRTSPAuth * auth,
345 GstRTSPClientState * state)
347 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_UNAUTHORIZED,
348 gst_rtsp_status_as_text (GST_RTSP_STS_UNAUTHORIZED), state->request);
351 /* and let the authentication manager setup the auth tokens */
352 gst_rtsp_auth_setup_auth (auth, client, 0, state);
355 send_response (client, state->session, state->response);
360 compare_uri (const GstRTSPUrl * uri1, const GstRTSPUrl * uri2)
362 if (uri1 == NULL || uri2 == NULL)
365 if (strcmp (uri1->abspath, uri2->abspath))
371 /* this function is called to initially find the media for the DESCRIBE request
372 * but is cached for when the same client (without breaking the connection) is
373 * doing a setup for the exact same url. */
374 static GstRTSPMedia *
375 find_media (GstRTSPClient * client, GstRTSPClientState * state)
377 GstRTSPMediaFactory *factory;
381 if (!compare_uri (client->uri, state->uri)) {
382 /* remove any previously cached values before we try to construct a new
385 gst_rtsp_url_free (client->uri);
388 g_object_unref (client->media);
389 client->media = NULL;
391 if (!client->media_mapping)
394 /* find the factory for the uri first */
396 gst_rtsp_media_mapping_find_factory (client->media_mapping,
400 state->factory = factory;
402 /* check if we have access to the factory */
403 if ((auth = gst_rtsp_media_factory_get_auth (factory))) {
404 if (!gst_rtsp_auth_check (auth, client, 0, state))
407 g_object_unref (auth);
410 /* prepare the media and add it to the pipeline */
411 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
414 g_object_unref (factory);
416 state->factory = NULL;
418 /* set ipv6 on the media before preparing */
419 media->is_ipv6 = client->is_ipv6;
420 state->media = media;
422 /* prepare the media */
423 if (!(gst_rtsp_media_prepare (media)))
426 /* now keep track of the uri and the media */
427 client->uri = gst_rtsp_url_copy (state->uri);
428 client->media = media;
430 /* we have seen this uri before, used cached media */
431 media = client->media;
432 state->media = media;
433 GST_INFO ("reusing cached media %p", media);
437 g_object_ref (media);
444 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
449 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
454 handle_unauthorized_request (client, auth, state);
455 g_object_unref (factory);
456 g_object_unref (auth);
461 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
462 g_object_unref (factory);
467 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
468 g_object_unref (media);
474 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
476 GstRTSPMessage message = { 0 };
481 gst_rtsp_message_init_data (&message, channel);
483 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
486 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
488 /* FIXME, client->watch could have been finalized here, we need to keep an
489 * extra refcount to the watch. */
490 gst_rtsp_watch_send_message (client->watch, &message, NULL);
492 gst_rtsp_message_steal_body (&message, &data, &usize);
493 gst_buffer_unmap (buffer, &map_info);
495 gst_rtsp_message_unset (&message);
501 link_stream (GstRTSPClient * client, GstRTSPSession * session,
502 GstRTSPSessionStream * stream)
504 GST_DEBUG ("client %p: linking stream %p", client, stream);
505 gst_rtsp_session_stream_set_callbacks (stream, (GstRTSPSendFunc) do_send_data,
506 (GstRTSPSendFunc) do_send_data, client, NULL);
507 client->streams = g_list_prepend (client->streams, stream);
508 /* make sure our session can't expire */
509 gst_rtsp_session_prevent_expire (session);
513 unlink_stream (GstRTSPClient * client, GstRTSPSession * session,
514 GstRTSPSessionStream * stream)
516 GST_DEBUG ("client %p: unlinking stream %p", client, stream);
517 gst_rtsp_session_stream_set_callbacks (stream, NULL, NULL, NULL, NULL);
518 client->streams = g_list_remove (client->streams, stream);
519 /* our session can now expire */
520 gst_rtsp_session_allow_expire (session);
524 unlink_session_streams (GstRTSPClient * client, GstRTSPSession * session,
525 GstRTSPSessionMedia * media)
529 n_streams = gst_rtsp_media_n_streams (media->media);
530 for (i = 0; i < n_streams; i++) {
531 GstRTSPSessionStream *sstream;
532 GstRTSPTransport *tr;
534 /* get the stream as configured in the session */
535 sstream = gst_rtsp_session_media_get_stream (media, i);
536 /* get the transport, if there is no transport configured, skip this stream */
537 if (!(tr = sstream->trans.transport))
540 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
541 /* for TCP, unlink the stream from the TCP connection of the client */
542 unlink_stream (client, session, sstream);
548 close_connection (GstRTSPClient * client)
550 const gchar *tunnelid;
552 GST_DEBUG ("client %p: closing connection", client);
554 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
555 g_mutex_lock (&tunnels_lock);
556 /* remove from tunnelids */
557 g_hash_table_remove (tunnels, tunnelid);
558 g_mutex_unlock (&tunnels_lock);
561 gst_rtsp_connection_close (client->connection);
565 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
567 GstRTSPSession *session;
568 GstRTSPSessionMedia *media;
569 GstRTSPStatusCode code;
574 session = state->session;
576 /* get a handle to the configuration of the media in the session */
577 media = gst_rtsp_session_get_media (session, state->uri);
581 state->sessmedia = media;
583 /* unlink the all TCP callbacks */
584 unlink_session_streams (client, session, media);
586 /* remove the session from the watched sessions */
587 g_object_weak_unref (G_OBJECT (session),
588 (GWeakNotify) client_session_finalized, client);
589 client->sessions = g_list_remove (client->sessions, session);
591 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
593 /* unmanage the media in the session, returns false if all media session
595 if (!gst_rtsp_session_release_media (session, media)) {
596 /* remove the session */
597 gst_rtsp_session_pool_remove (client->session_pool, session);
599 /* construct the response now */
600 code = GST_RTSP_STS_OK;
601 gst_rtsp_message_init_response (state->response, code,
602 gst_rtsp_status_as_text (code), state->request);
604 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONNECTION,
607 send_response (client, session, state->response);
609 /* we emit the signal before closing the connection */
610 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
613 close_connection (client);
620 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
625 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
631 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
637 res = gst_rtsp_message_get_body (state->request, &data, &size);
638 if (res != GST_RTSP_OK)
642 /* no body, keep-alive request */
643 send_generic_response (client, GST_RTSP_STS_OK, state);
645 /* there is a body, handle the params */
646 res = gst_rtsp_params_get (client, state);
647 if (res != GST_RTSP_OK)
650 send_response (client, state->session, state->response);
653 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
661 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
667 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
673 res = gst_rtsp_message_get_body (state->request, &data, &size);
674 if (res != GST_RTSP_OK)
678 /* no body, keep-alive request */
679 send_generic_response (client, GST_RTSP_STS_OK, state);
681 /* there is a body, handle the params */
682 res = gst_rtsp_params_set (client, state);
683 if (res != GST_RTSP_OK)
686 send_response (client, state->session, state->response);
689 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
697 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
703 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
705 GstRTSPSession *session;
706 GstRTSPSessionMedia *media;
707 GstRTSPStatusCode code;
709 if (!(session = state->session))
712 /* get a handle to the configuration of the media in the session */
713 media = gst_rtsp_session_get_media (session, state->uri);
717 state->sessmedia = media;
719 /* the session state must be playing or recording */
720 if (media->state != GST_RTSP_STATE_PLAYING &&
721 media->state != GST_RTSP_STATE_RECORDING)
724 /* unlink the all TCP callbacks */
725 unlink_session_streams (client, session, media);
727 /* then pause sending */
728 gst_rtsp_session_media_set_state (media, GST_STATE_PAUSED);
730 /* construct the response now */
731 code = GST_RTSP_STS_OK;
732 gst_rtsp_message_init_response (state->response, code,
733 gst_rtsp_status_as_text (code), state->request);
735 send_response (client, session, state->response);
737 /* the state is now READY */
738 media->state = GST_RTSP_STATE_READY;
740 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
748 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
753 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
758 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
765 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
767 GstRTSPSession *session;
768 GstRTSPSessionMedia *media;
769 GstRTSPStatusCode code;
771 guint n_streams, i, infocount;
772 guint timestamp, seqnum;
774 GstRTSPTimeRange *range;
777 if (!(session = state->session))
780 /* get a handle to the configuration of the media in the session */
781 media = gst_rtsp_session_get_media (session, state->uri);
785 state->sessmedia = media;
787 /* the session state must be playing or ready */
788 if (media->state != GST_RTSP_STATE_PLAYING &&
789 media->state != GST_RTSP_STATE_READY)
792 /* parse the range header if we have one */
794 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
795 if (res == GST_RTSP_OK) {
796 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
797 /* we have a range, seek to the position */
798 gst_rtsp_media_seek (media->media, range);
799 gst_rtsp_range_free (range);
803 /* grab RTPInfo from the payloaders now */
804 rtpinfo = g_string_new ("");
806 n_streams = gst_rtsp_media_n_streams (media->media);
807 for (i = 0, infocount = 0; i < n_streams; i++) {
808 GstRTSPSessionStream *sstream;
809 GstRTSPMediaStream *stream;
810 GstRTSPTransport *tr;
811 GObjectClass *payobjclass;
814 /* get the stream as configured in the session */
815 sstream = gst_rtsp_session_media_get_stream (media, i);
816 /* get the transport, if there is no transport configured, skip this stream */
817 if (!(tr = sstream->trans.transport)) {
818 GST_INFO ("stream %d is not configured", i);
822 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
823 /* for TCP, link the stream to the TCP connection of the client */
824 link_stream (client, session, sstream);
827 stream = sstream->media_stream;
829 payobjclass = G_OBJECT_GET_CLASS (stream->payloader);
831 if (g_object_class_find_property (payobjclass, "seqnum") &&
832 g_object_class_find_property (payobjclass, "timestamp")) {
835 payobj = G_OBJECT (stream->payloader);
837 /* only add RTP-Info for streams with seqnum and timestamp */
838 g_object_get (payobj, "seqnum", &seqnum, "timestamp", ×tamp, NULL);
841 g_string_append (rtpinfo, ", ");
843 uristr = gst_rtsp_url_get_request_uri (state->uri);
844 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
845 uristr, i, seqnum, timestamp);
850 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
854 /* construct the response now */
855 code = GST_RTSP_STS_OK;
856 gst_rtsp_message_init_response (state->response, code,
857 gst_rtsp_status_as_text (code), state->request);
859 /* add the RTP-Info header */
861 str = g_string_free (rtpinfo, FALSE);
862 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
864 g_string_free (rtpinfo, TRUE);
868 str = gst_rtsp_media_get_range_string (media->media, TRUE);
869 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
871 send_response (client, session, state->response);
873 /* start playing after sending the request */
874 gst_rtsp_session_media_set_state (media, GST_STATE_PLAYING);
876 media->state = GST_RTSP_STATE_PLAYING;
878 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
886 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
891 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
896 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
903 do_keepalive (GstRTSPSession * session)
905 GST_INFO ("keep session %p alive", session);
906 gst_rtsp_session_touch (session);
910 handle_blocksize (GstRTSPMedia * media, GstRTSPMessage * request)
912 gchar *blocksize_str;
915 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
916 &blocksize_str, 0) == GST_RTSP_OK) {
920 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
921 if (end == blocksize_str) {
922 GST_ERROR ("failed to parse blocksize");
925 if (blocksize > G_MAXUINT)
926 blocksize = G_MAXUINT;
927 gst_rtsp_media_handle_mtu (media, (guint) blocksize);
935 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
941 gboolean have_transport;
942 GstRTSPTransport *ct, *st;
944 GstRTSPLowerTrans supported;
945 GstRTSPStatusCode code;
946 GstRTSPSession *session;
947 GstRTSPSessionStream *stream;
948 gchar *trans_str, *pos;
950 GstRTSPSessionMedia *media;
954 /* the uri contains the stream number we added in the SDP config, which is
955 * always /stream=%d so we need to strip that off
956 * parse the stream we need to configure, look for the stream in the abspath
957 * first and then in the query. */
958 if (uri->abspath == NULL || !(pos = strstr (uri->abspath, "/stream="))) {
959 if (uri->query == NULL || !(pos = strstr (uri->query, "/stream=")))
963 /* we can mofify the parse uri in place */
966 pos += strlen ("/stream=");
967 if (sscanf (pos, "%u", &streamid) != 1)
970 /* parse the transport */
972 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
974 if (res != GST_RTSP_OK)
977 transports = g_strsplit (transport, ",", 0);
978 gst_rtsp_transport_new (&ct);
980 /* init transports */
981 have_transport = FALSE;
982 gst_rtsp_transport_init (ct);
984 /* our supported transports */
985 supported = GST_RTSP_LOWER_TRANS_UDP |
986 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
988 /* loop through the transports, try to parse */
989 for (i = 0; transports[i]; i++) {
990 res = gst_rtsp_transport_parse (transports[i], ct);
991 if (res != GST_RTSP_OK) {
992 /* no valid transport, search some more */
993 GST_WARNING ("could not parse transport %s", transports[i]);
997 /* we have a transport, see if it's RTP/AVP */
998 if (ct->trans != GST_RTSP_TRANS_RTP || ct->profile != GST_RTSP_PROFILE_AVP) {
999 GST_WARNING ("invalid transport %s", transports[i]);
1003 if (!(ct->lower_transport & supported)) {
1004 GST_WARNING ("unsupported transport %s", transports[i]);
1008 /* we have a valid transport */
1009 GST_INFO ("found valid transport %s", transports[i]);
1010 have_transport = TRUE;
1014 gst_rtsp_transport_init (ct);
1016 g_strfreev (transports);
1018 /* we have not found anything usable, error out */
1019 if (!have_transport)
1020 goto unsupported_transports;
1022 if (client->session_pool == NULL)
1025 session = state->session;
1028 g_object_ref (session);
1029 /* get a handle to the configuration of the media in the session, this can
1030 * return NULL if this is a new url to manage in this session. */
1031 media = gst_rtsp_session_get_media (session, uri);
1033 /* create a session if this fails we probably reached our session limit or
1035 if (!(session = gst_rtsp_session_pool_create (client->session_pool)))
1036 goto service_unavailable;
1038 state->session = session;
1040 /* we need a new media configuration in this session */
1044 /* we have no media, find one and manage it */
1045 if (media == NULL) {
1048 /* get a handle to the configuration of the media in the session */
1049 if ((m = find_media (client, state))) {
1050 /* manage the media in our session now */
1051 media = gst_rtsp_session_manage_media (session, uri, m);
1055 /* if we stil have no media, error */
1059 state->sessmedia = media;
1061 if (!handle_blocksize (media->media, state->request))
1062 goto invalid_blocksize;
1064 /* we have a valid transport now, set the destination of the client. */
1065 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1066 if (ct->destination == NULL || !client->use_client_settings) {
1067 g_free (ct->destination);
1068 ct->destination = gst_rtsp_media_get_multicast_group (media->media);
1070 /* reset ttl if client settings are not allowed */
1071 if (!client->use_client_settings) {
1077 url = gst_rtsp_connection_get_url (client->connection);
1078 g_free (ct->destination);
1079 ct->destination = g_strdup (url->host);
1081 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1082 /* check if the client selected channels for TCP */
1083 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1084 gst_rtsp_session_media_alloc_channels (media, &ct->interleaved);
1089 /* get a handle to the stream in the media */
1090 if (!(stream = gst_rtsp_session_media_get_stream (media, streamid)))
1093 st = gst_rtsp_session_stream_set_transport (stream, ct);
1095 /* configure keepalive for this transport */
1096 gst_rtsp_session_stream_set_keepalive (stream,
1097 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1099 /* serialize the server transport */
1100 trans_str = gst_rtsp_transport_as_text (st);
1101 gst_rtsp_transport_free (st);
1103 /* construct the response now */
1104 code = GST_RTSP_STS_OK;
1105 gst_rtsp_message_init_response (state->response, code,
1106 gst_rtsp_status_as_text (code), state->request);
1108 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1112 send_response (client, session, state->response);
1114 /* update the state */
1115 switch (media->state) {
1116 case GST_RTSP_STATE_PLAYING:
1117 case GST_RTSP_STATE_RECORDING:
1118 case GST_RTSP_STATE_READY:
1119 /* no state change */
1122 media->state = GST_RTSP_STATE_READY;
1125 g_object_unref (session);
1127 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST],
1135 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1140 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1141 g_object_unref (session);
1142 gst_rtsp_transport_free (ct);
1147 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1148 g_object_unref (session);
1149 gst_rtsp_transport_free (ct);
1154 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1155 g_object_unref (session);
1156 gst_rtsp_transport_free (ct);
1161 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1164 unsupported_transports:
1166 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1167 gst_rtsp_transport_free (ct);
1172 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1173 gst_rtsp_transport_free (ct);
1176 service_unavailable:
1178 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1179 gst_rtsp_transport_free (ct);
1184 static GstSDPMessage *
1185 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1190 GstRTSPLowerTrans protocols;
1192 gst_sdp_message_new (&sdp);
1194 /* some standard things first */
1195 gst_sdp_message_set_version (sdp, "0");
1197 if (client->is_ipv6)
1202 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1205 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1206 gst_sdp_message_set_information (sdp, "rtsp-server");
1207 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1208 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1209 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1210 gst_sdp_message_add_attribute (sdp, "control", "*");
1212 info.server_proto = proto;
1213 protocols = gst_rtsp_media_get_protocols (media);
1214 if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST)
1215 info.server_ip = gst_rtsp_media_get_multicast_group (media);
1217 info.server_ip = g_strdup (client->server_ip);
1219 /* create an SDP for the media object */
1220 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1223 g_free (info.server_ip);
1230 g_free (info.server_ip);
1231 gst_sdp_message_free (sdp);
1236 /* for the describe we must generate an SDP */
1238 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1243 gchar *str, *content_base;
1244 GstRTSPMedia *media;
1245 GstRTSPClientClass *klass;
1247 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1249 /* check what kind of format is accepted, we don't really do anything with it
1250 * and always return SDP for now. */
1255 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1257 if (res == GST_RTSP_ENOTIMPL)
1260 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1264 /* find the media object for the uri */
1265 if (!(media = find_media (client, state)))
1269 /* create an SDP for the media object on this client */
1270 if (!(sdp = klass->create_sdp (client, media)))
1273 g_object_unref (media);
1275 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1276 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1278 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1281 /* content base for some clients that might screw up creating the setup uri */
1282 str = gst_rtsp_url_get_request_uri (state->uri);
1283 str_len = strlen (str);
1285 /* check for trailing '/' and append one */
1286 if (str[str_len - 1] != '/') {
1287 content_base = g_malloc (str_len + 2);
1288 memcpy (content_base, str, str_len);
1289 content_base[str_len] = '/';
1290 content_base[str_len + 1] = '\0';
1296 GST_INFO ("adding content-base: %s", content_base);
1298 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1300 g_free (content_base);
1302 /* add SDP to the response body */
1303 str = gst_sdp_message_as_text (sdp);
1304 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1305 gst_sdp_message_free (sdp);
1307 send_response (client, state->session, state->response);
1309 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1317 /* error reply is already sent */
1322 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1323 g_object_unref (media);
1329 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1331 GstRTSPMethod options;
1334 options = GST_RTSP_DESCRIBE |
1339 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1341 str = gst_rtsp_options_as_text (options);
1343 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1344 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1346 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1349 send_response (client, state->session, state->response);
1351 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1357 /* remove duplicate and trailing '/' */
1359 sanitize_uri (GstRTSPUrl * uri)
1363 gboolean have_slash, prev_slash;
1365 s = d = uri->abspath;
1366 len = strlen (uri->abspath);
1370 for (i = 0; i < len; i++) {
1371 have_slash = s[i] == '/';
1373 if (!have_slash || !prev_slash)
1375 prev_slash = have_slash;
1377 len = d - uri->abspath;
1378 /* don't remove the first slash if that's the only thing left */
1379 if (len > 1 && *(d - 1) == '/')
1385 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1387 GST_INFO ("client %p: session %p finished", client, session);
1389 /* unlink all media managed in this session */
1390 client_unlink_session (client, session);
1392 /* remove the session */
1393 if (!(client->sessions = g_list_remove (client->sessions, session))) {
1394 GST_INFO ("client %p: all sessions finalized, close the connection",
1396 close_connection (client);
1401 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
1405 for (walk = client->sessions; walk; walk = g_list_next (walk)) {
1406 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
1408 /* we already know about this session */
1409 if (msession == session)
1413 GST_INFO ("watching session %p", session);
1415 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
1417 client->sessions = g_list_prepend (client->sessions, session);
1419 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1424 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1426 GstRTSPMethod method;
1427 const gchar *uristr;
1429 GstRTSPVersion version;
1431 GstRTSPSession *session;
1432 GstRTSPClientState state = { NULL };
1433 GstRTSPMessage response = { 0 };
1436 state.request = request;
1437 state.response = &response;
1439 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1440 gst_rtsp_message_dump (request);
1443 GST_INFO ("client %p: received a request", client);
1445 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1447 if (version != GST_RTSP_VERSION_1_0) {
1448 /* we can only handle 1.0 requests */
1449 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1453 state.method = method;
1455 /* we always try to parse the url first */
1456 if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
1457 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1461 /* sanitize the uri */
1465 /* get the session if there is any */
1466 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1467 if (res == GST_RTSP_OK) {
1468 if (client->session_pool == NULL)
1471 /* we had a session in the request, find it again */
1472 if (!(session = gst_rtsp_session_pool_find (client->session_pool, sessid)))
1473 goto session_not_found;
1475 /* we add the session to the client list of watched sessions. When a session
1476 * disappears because it times out, we will be notified. If all sessions are
1477 * gone, we will close the connection */
1478 client_watch_session (client, session);
1482 state.session = session;
1485 if (!gst_rtsp_auth_check (client->auth, client, 0, &state))
1486 goto not_authorized;
1489 /* now see what is asked and dispatch to a dedicated handler */
1491 case GST_RTSP_OPTIONS:
1492 handle_options_request (client, &state);
1494 case GST_RTSP_DESCRIBE:
1495 handle_describe_request (client, &state);
1497 case GST_RTSP_SETUP:
1498 handle_setup_request (client, &state);
1501 handle_play_request (client, &state);
1503 case GST_RTSP_PAUSE:
1504 handle_pause_request (client, &state);
1506 case GST_RTSP_TEARDOWN:
1507 handle_teardown_request (client, &state);
1509 case GST_RTSP_SET_PARAMETER:
1510 handle_set_param_request (client, &state);
1512 case GST_RTSP_GET_PARAMETER:
1513 handle_get_param_request (client, &state);
1515 case GST_RTSP_ANNOUNCE:
1516 case GST_RTSP_RECORD:
1517 case GST_RTSP_REDIRECT:
1518 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1520 case GST_RTSP_INVALID:
1522 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1526 g_object_unref (session);
1528 gst_rtsp_url_free (uri);
1534 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, &state);
1539 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1544 handle_unauthorized_request (client, client->auth, &state);
1550 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1560 /* find the stream for this message */
1561 res = gst_rtsp_message_parse_data (message, &channel);
1562 if (res != GST_RTSP_OK)
1565 gst_rtsp_message_steal_body (message, &data, &size);
1567 buffer = gst_buffer_new_wrapped (data, size);
1570 for (walk = client->streams; walk; walk = g_list_next (walk)) {
1571 GstRTSPSessionStream *stream = (GstRTSPSessionStream *) walk->data;
1572 GstRTSPMediaStream *mstream;
1573 GstRTSPTransport *tr;
1575 /* get the transport, if there is no transport configured, skip this stream */
1576 if (!(tr = stream->trans.transport))
1579 /* we also need a media stream */
1580 if (!(mstream = stream->media_stream))
1583 /* check for TCP transport */
1584 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1585 /* dispatch to the stream based on the channel number */
1586 if (tr->interleaved.min == channel) {
1587 gst_rtsp_media_stream_rtp (mstream, buffer);
1590 } else if (tr->interleaved.max == channel) {
1591 gst_rtsp_media_stream_rtcp (mstream, buffer);
1598 gst_buffer_unref (buffer);
1602 * gst_rtsp_client_set_session_pool:
1603 * @client: a #GstRTSPClient
1604 * @pool: a #GstRTSPSessionPool
1606 * Set @pool as the sessionpool for @client which it will use to find
1607 * or allocate sessions. the sessionpool is usually inherited from the server
1608 * that created the client but can be overridden later.
1611 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
1612 GstRTSPSessionPool * pool)
1614 GstRTSPSessionPool *old;
1616 old = client->session_pool;
1619 g_object_ref (pool);
1620 client->session_pool = pool;
1622 g_object_unref (old);
1627 * gst_rtsp_client_get_session_pool:
1628 * @client: a #GstRTSPClient
1630 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
1632 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
1634 GstRTSPSessionPool *
1635 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
1637 GstRTSPSessionPool *result;
1639 if ((result = client->session_pool))
1640 g_object_ref (result);
1646 * gst_rtsp_client_set_server:
1647 * @client: a #GstRTSPClient
1648 * @server: a #GstRTSPServer
1650 * Set @server as the server that created @client.
1653 gst_rtsp_client_set_server (GstRTSPClient * client, GstRTSPServer * server)
1657 old = client->server;
1658 if (old != server) {
1660 g_object_ref (server);
1661 client->server = server;
1663 g_object_unref (old);
1668 * gst_rtsp_client_get_server:
1669 * @client: a #GstRTSPClient
1671 * Get the #GstRTSPServer object that @client was created from.
1673 * Returns: (transfer full): a #GstRTSPServer, unref after usage.
1676 gst_rtsp_client_get_server (GstRTSPClient * client)
1678 GstRTSPServer *result;
1680 if ((result = client->server))
1681 g_object_ref (result);
1687 * gst_rtsp_client_set_media_mapping:
1688 * @client: a #GstRTSPClient
1689 * @mapping: a #GstRTSPMediaMapping
1691 * Set @mapping as the media mapping for @client which it will use to map urls
1692 * to media streams. These mapping is usually inherited from the server that
1693 * created the client but can be overriden later.
1696 gst_rtsp_client_set_media_mapping (GstRTSPClient * client,
1697 GstRTSPMediaMapping * mapping)
1699 GstRTSPMediaMapping *old;
1701 old = client->media_mapping;
1703 if (old != mapping) {
1705 g_object_ref (mapping);
1706 client->media_mapping = mapping;
1708 g_object_unref (old);
1713 * gst_rtsp_client_get_media_mapping:
1714 * @client: a #GstRTSPClient
1716 * Get the #GstRTSPMediaMapping object that @client uses to manage its sessions.
1718 * Returns: (transfer full): a #GstRTSPMediaMapping, unref after usage.
1720 GstRTSPMediaMapping *
1721 gst_rtsp_client_get_media_mapping (GstRTSPClient * client)
1723 GstRTSPMediaMapping *result;
1725 if ((result = client->media_mapping))
1726 g_object_ref (result);
1732 * gst_rtsp_client_set_use_client_settings:
1733 * @client: a #GstRTSPClient
1734 * @use_client_settings: whether to use client settings for multicast
1736 * Use client transport settings (destination and ttl) for multicast.
1737 * When @use_client_settings is %FALSE, the server settings will be
1741 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
1742 gboolean use_client_settings)
1744 client->use_client_settings = use_client_settings;
1748 * gst_rtsp_client_get_use_client_settings:
1749 * @client: a #GstRTSPClient
1751 * Check if client transport settings (destination and ttl) for multicast
1755 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
1757 return client->use_client_settings;
1761 * gst_rtsp_client_set_auth:
1762 * @client: a #GstRTSPClient
1763 * @auth: a #GstRTSPAuth
1765 * configure @auth to be used as the authentication manager of @client.
1768 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
1772 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1778 g_object_ref (auth);
1779 client->auth = auth;
1781 g_object_unref (old);
1787 * gst_rtsp_client_get_auth:
1788 * @client: a #GstRTSPClient
1790 * Get the #GstRTSPAuth used as the authentication manager of @client.
1792 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
1796 gst_rtsp_client_get_auth (GstRTSPClient * client)
1798 GstRTSPAuth *result;
1800 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1802 if ((result = client->auth))
1803 g_object_ref (result);
1808 static GstRTSPResult
1809 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
1812 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1814 switch (message->type) {
1815 case GST_RTSP_MESSAGE_REQUEST:
1816 handle_request (client, message);
1818 case GST_RTSP_MESSAGE_RESPONSE:
1820 case GST_RTSP_MESSAGE_DATA:
1821 handle_data (client, message);
1829 static GstRTSPResult
1830 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
1832 /* GstRTSPClient *client; */
1834 /* client = GST_RTSP_CLIENT (user_data); */
1836 /* GST_INFO ("client %p: sent a message with cseq %d", client, cseq); */
1841 static GstRTSPResult
1842 closed (GstRTSPWatch * watch, gpointer user_data)
1844 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1845 const gchar *tunnelid;
1847 GST_INFO ("client %p: connection closed", client);
1849 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
1850 g_mutex_lock (&tunnels_lock);
1851 /* remove from tunnelids */
1852 g_hash_table_remove (tunnels, tunnelid);
1853 g_mutex_unlock (&tunnels_lock);
1859 static GstRTSPResult
1860 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
1862 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1865 str = gst_rtsp_strresult (result);
1866 GST_INFO ("client %p: received an error %s", client, str);
1872 static GstRTSPResult
1873 error_full (GstRTSPWatch * watch, GstRTSPResult result,
1874 GstRTSPMessage * message, guint id, gpointer user_data)
1876 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1879 str = gst_rtsp_strresult (result);
1881 ("client %p: received an error %s when handling message %p with id %d",
1882 client, str, message, id);
1889 remember_tunnel (GstRTSPClient * client)
1891 const gchar *tunnelid;
1893 /* store client in the pending tunnels */
1894 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
1895 if (tunnelid == NULL)
1898 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
1900 /* we can't have two clients connecting with the same tunnelid */
1901 g_mutex_lock (&tunnels_lock);
1902 if (g_hash_table_lookup (tunnels, tunnelid))
1903 goto tunnel_existed;
1905 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
1906 g_mutex_unlock (&tunnels_lock);
1913 GST_ERROR ("client %p: no tunnelid provided", client);
1918 g_mutex_unlock (&tunnels_lock);
1919 GST_ERROR ("client %p: tunnel session %s already existed", client,
1925 static GstRTSPStatusCode
1926 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
1928 GstRTSPClient *client;
1930 client = GST_RTSP_CLIENT (user_data);
1932 GST_INFO ("client %p: tunnel start (connection %p)", client,
1933 client->connection);
1935 if (!remember_tunnel (client))
1938 return GST_RTSP_STS_OK;
1943 GST_ERROR ("client %p: error starting tunnel", client);
1944 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
1948 static GstRTSPResult
1949 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
1951 GstRTSPClient *client;
1953 client = GST_RTSP_CLIENT (user_data);
1955 GST_INFO ("client %p: tunnel lost (connection %p)", client,
1956 client->connection);
1958 /* ignore error, it'll only be a problem when the client does a POST again */
1959 remember_tunnel (client);
1964 static GstRTSPResult
1965 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
1967 const gchar *tunnelid;
1968 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1969 GstRTSPClient *oclient;
1971 GST_INFO ("client %p: tunnel complete", client);
1973 /* find previous tunnel */
1974 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
1975 if (tunnelid == NULL)
1978 g_mutex_lock (&tunnels_lock);
1979 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
1982 /* remove the old client from the table. ref before because removing it will
1983 * remove the ref to it. */
1984 g_object_ref (oclient);
1985 g_hash_table_remove (tunnels, tunnelid);
1987 if (oclient->watch == NULL)
1989 g_mutex_unlock (&tunnels_lock);
1991 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
1992 oclient->connection, client->connection);
1994 /* merge the tunnels into the first client */
1995 gst_rtsp_connection_do_tunnel (oclient->connection, client->connection);
1996 gst_rtsp_watch_reset (oclient->watch);
1997 g_object_unref (oclient);
2004 GST_INFO ("client %p: no tunnelid provided", client);
2005 return GST_RTSP_ERROR;
2009 g_mutex_unlock (&tunnels_lock);
2010 GST_INFO ("client %p: tunnel session %s not found", client, tunnelid);
2011 return GST_RTSP_ERROR;
2015 g_mutex_unlock (&tunnels_lock);
2016 GST_INFO ("client %p: tunnel session %s was closed", client, tunnelid);
2017 g_object_unref (oclient);
2018 return GST_RTSP_ERROR;
2022 static GstRTSPWatchFuncs watch_funcs = {
2034 client_watch_notify (GstRTSPClient * client)
2036 GST_INFO ("client %p: watch destroyed", client);
2037 client->watchid = 0;
2038 client->watch = NULL;
2039 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2040 g_object_unref (client);
2044 attach_client (GstRTSPClient * client, GSocket * socket,
2045 GstRTSPConnection * conn, GError ** error)
2047 GSocket *read_socket;
2048 GSocketAddress *address;
2050 GMainContext *context;
2053 read_socket = gst_rtsp_connection_get_read_socket (conn);
2054 client->is_ipv6 = g_socket_get_family (socket) == G_SOCKET_FAMILY_IPV6;
2056 if (!(address = g_socket_get_remote_address (read_socket, error)))
2059 g_free (client->server_ip);
2060 /* keep the original ip that the client connected to */
2061 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2062 GInetAddress *iaddr;
2064 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2066 client->server_ip = g_inet_address_to_string (iaddr);
2067 g_object_unref (address);
2069 client->server_ip = g_strdup ("unknown");
2072 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2073 client->server_ip, client->is_ipv6);
2075 url = gst_rtsp_connection_get_url (conn);
2076 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2078 client->connection = conn;
2080 /* create watch for the connection and attach */
2081 client->watch = gst_rtsp_watch_new (client->connection, &watch_funcs,
2082 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2084 /* find the context to add the watch */
2085 if ((source = g_main_current_source ()))
2086 context = g_source_get_context (source);
2090 GST_INFO ("attaching to context %p", context);
2092 client->watchid = gst_rtsp_watch_attach (client->watch, context);
2093 gst_rtsp_watch_unref (client->watch);
2100 GST_ERROR ("could not get remote address %s", (*error)->message);
2106 * gst_rtsp_client_create_from_socket:
2107 * @client: a #GstRTSPClient
2108 * @socket: a #GSocket
2109 * @ip: the IP address of the remote client
2110 * @port: the port used by the other end
2111 * @initial_buffer: any initial data that was already read from the socket
2114 * Take an existing network socket and use it for an RTSP connection.
2116 * Returns: %TRUE on success.
2119 gst_rtsp_client_create_from_socket (GstRTSPClient * client, GSocket * socket,
2120 const gchar * ip, gint port, const gchar * initial_buffer, GError ** error)
2122 GstRTSPConnection *conn;
2125 GST_RTSP_CHECK (gst_rtsp_connection_create_from_socket (socket, ip, port,
2126 initial_buffer, &conn), no_connection);
2128 return attach_client (client, socket, conn, error);
2133 gchar *str = gst_rtsp_strresult (res);
2135 GST_ERROR ("could not create connection from socket %p: %s", socket, str);
2142 * gst_rtsp_client_accept:
2143 * @client: a #GstRTSPClient
2144 * @socket: a #GSocket
2145 * @cancellable: a #GCancellable
2148 * Accept a new connection for @client on @socket.
2150 * This function should be called when the client properties and urls are fully
2151 * configured and the client is ready to start.
2153 * Returns: %TRUE if the client could be accepted.
2156 gst_rtsp_client_accept (GstRTSPClient * client, GSocket * socket,
2157 GCancellable * cancellable, GError ** error)
2159 GstRTSPConnection *conn;
2162 /* a new client connected. */
2163 GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, cancellable),
2166 return attach_client (client, socket, conn, error);
2171 gchar *str = gst_rtsp_strresult (res);
2173 GST_ERROR ("Could not accept client on server socket %p: %s", socket, str);