2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
23 #include "rtsp-client.h"
25 #include "rtsp-params.h"
27 static GMutex tunnels_lock;
28 static GHashTable *tunnels;
30 #define DEFAULT_SESSION_POOL NULL
31 #define DEFAULT_MOUNT_POINTS NULL
32 #define DEFAULT_USE_CLIENT_SETTINGS FALSE
39 PROP_USE_CLIENT_SETTINGS,
47 SIGNAL_OPTIONS_REQUEST,
48 SIGNAL_DESCRIBE_REQUEST,
52 SIGNAL_TEARDOWN_REQUEST,
53 SIGNAL_SET_PARAMETER_REQUEST,
54 SIGNAL_GET_PARAMETER_REQUEST,
58 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
59 #define GST_CAT_DEFAULT rtsp_client_debug
61 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
63 static void gst_rtsp_client_get_property (GObject * object, guint propid,
64 GValue * value, GParamSpec * pspec);
65 static void gst_rtsp_client_set_property (GObject * object, guint propid,
66 const GValue * value, GParamSpec * pspec);
67 static void gst_rtsp_client_finalize (GObject * obj);
69 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
70 static void client_session_finalized (GstRTSPClient * client,
71 GstRTSPSession * session);
72 static void unlink_session_transports (GstRTSPClient * client,
73 GstRTSPSession * session, GstRTSPSessionMedia * media);
75 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
78 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
80 GObjectClass *gobject_class;
82 gobject_class = G_OBJECT_CLASS (klass);
84 gobject_class->get_property = gst_rtsp_client_get_property;
85 gobject_class->set_property = gst_rtsp_client_set_property;
86 gobject_class->finalize = gst_rtsp_client_finalize;
88 klass->create_sdp = create_sdp;
90 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
91 g_param_spec_object ("session-pool", "Session Pool",
92 "The session pool to use for client session",
93 GST_TYPE_RTSP_SESSION_POOL,
94 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
96 g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
97 g_param_spec_object ("mount-points", "Mount Points",
98 "The mount points to use for client session",
99 GST_TYPE_RTSP_MOUNT_POINTS,
100 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
102 g_object_class_install_property (gobject_class, PROP_USE_CLIENT_SETTINGS,
103 g_param_spec_boolean ("use-client-settings", "Use Client Settings",
104 "Use client settings for ttl and destination in multicast",
105 DEFAULT_USE_CLIENT_SETTINGS,
106 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
108 gst_rtsp_client_signals[SIGNAL_CLOSED] =
109 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
110 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
111 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
113 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
114 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
115 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
116 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
118 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
119 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
120 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
121 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
124 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
125 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
126 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
127 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
130 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
131 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
132 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
133 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
136 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
137 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
138 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
139 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
142 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
143 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
144 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
145 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
148 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
149 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
150 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
151 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
154 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
155 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
156 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
157 set_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
158 G_TYPE_NONE, 1, G_TYPE_POINTER);
160 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
161 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
162 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
163 get_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
164 G_TYPE_NONE, 1, G_TYPE_POINTER);
167 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
168 g_mutex_init (&tunnels_lock);
170 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
174 gst_rtsp_client_init (GstRTSPClient * client)
176 client->use_client_settings = DEFAULT_USE_CLIENT_SETTINGS;
177 client->close_response_seq = 0;
181 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
183 /* unlink all media managed in this session */
184 while (session->medias) {
185 GstRTSPSessionMedia *media = session->medias->data;
187 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
188 unlink_session_transports (client, session, media);
189 /* unmanage the media in the session. this will modify session->medias */
190 gst_rtsp_session_release_media (session, media);
195 client_cleanup_sessions (GstRTSPClient * client)
199 /* remove weak-ref from sessions */
200 for (sessions = client->sessions; sessions; sessions = g_list_next (sessions)) {
201 GstRTSPSession *session = (GstRTSPSession *) sessions->data;
202 g_object_weak_unref (G_OBJECT (session),
203 (GWeakNotify) client_session_finalized, client);
204 client_unlink_session (client, session);
206 g_list_free (client->sessions);
207 client->sessions = NULL;
210 /* A client is finalized when the connection is broken */
212 gst_rtsp_client_finalize (GObject * obj)
214 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
216 GST_INFO ("finalize client %p", client);
219 g_source_destroy ((GSource *) client->watch);
221 client_cleanup_sessions (client);
223 gst_rtsp_connection_free (client->connection);
224 if (client->session_pool)
225 g_object_unref (client->session_pool);
226 if (client->mount_points)
227 g_object_unref (client->mount_points);
229 g_object_unref (client->auth);
232 gst_rtsp_url_free (client->uri);
234 gst_rtsp_media_unprepare (client->media);
235 g_object_unref (client->media);
238 g_free (client->server_ip);
240 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
244 gst_rtsp_client_get_property (GObject * object, guint propid,
245 GValue * value, GParamSpec * pspec)
247 GstRTSPClient *client = GST_RTSP_CLIENT (object);
250 case PROP_SESSION_POOL:
251 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
253 case PROP_MOUNT_POINTS:
254 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
256 case PROP_USE_CLIENT_SETTINGS:
257 g_value_set_boolean (value,
258 gst_rtsp_client_get_use_client_settings (client));
261 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
266 gst_rtsp_client_set_property (GObject * object, guint propid,
267 const GValue * value, GParamSpec * pspec)
269 GstRTSPClient *client = GST_RTSP_CLIENT (object);
272 case PROP_SESSION_POOL:
273 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
275 case PROP_MOUNT_POINTS:
276 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
278 case PROP_USE_CLIENT_SETTINGS:
279 gst_rtsp_client_set_use_client_settings (client,
280 g_value_get_boolean (value));
283 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
288 * gst_rtsp_client_new:
290 * Create a new #GstRTSPClient instance.
292 * Returns: a new #GstRTSPClient
295 gst_rtsp_client_new (void)
297 GstRTSPClient *result;
299 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
305 send_response (GstRTSPClient * client, GstRTSPSession * session,
306 GstRTSPMessage * response, gboolean close)
308 gst_rtsp_message_add_header (response, GST_RTSP_HDR_SERVER,
309 "GStreamer RTSP server");
311 /* remove any previous header */
312 gst_rtsp_message_remove_header (response, GST_RTSP_HDR_SESSION, -1);
314 /* add the new session header for new session ids */
316 gst_rtsp_message_take_header (response, GST_RTSP_HDR_SESSION,
317 gst_rtsp_session_get_header (session));
320 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
321 gst_rtsp_message_dump (response);
325 gst_rtsp_message_add_header (response, GST_RTSP_HDR_CONNECTION, "close");
327 /* send the response and store the seq number so we can wait until it's
328 * written to the client to close the connection */
329 gst_rtsp_watch_send_message (client->watch, response, close ?
330 &client->close_response_seq : NULL);
331 gst_rtsp_message_unset (response);
335 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
336 GstRTSPClientState * state)
338 gst_rtsp_message_init_response (state->response, code,
339 gst_rtsp_status_as_text (code), state->request);
341 send_response (client, NULL, state->response, FALSE);
345 handle_unauthorized_request (GstRTSPClient * client, GstRTSPAuth * auth,
346 GstRTSPClientState * state)
348 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_UNAUTHORIZED,
349 gst_rtsp_status_as_text (GST_RTSP_STS_UNAUTHORIZED), state->request);
352 /* and let the authentication manager setup the auth tokens */
353 gst_rtsp_auth_setup_auth (auth, client, 0, state);
356 send_response (client, state->session, state->response, FALSE);
361 compare_uri (const GstRTSPUrl * uri1, const GstRTSPUrl * uri2)
363 if (uri1 == NULL || uri2 == NULL)
366 if (strcmp (uri1->abspath, uri2->abspath))
372 /* this function is called to initially find the media for the DESCRIBE request
373 * but is cached for when the same client (without breaking the connection) is
374 * doing a setup for the exact same url. */
375 static GstRTSPMedia *
376 find_media (GstRTSPClient * client, GstRTSPClientState * state)
378 GstRTSPMediaFactory *factory;
382 if (!compare_uri (client->uri, state->uri)) {
383 /* remove any previously cached values before we try to construct a new
386 gst_rtsp_url_free (client->uri);
389 gst_rtsp_media_unprepare (client->media);
390 g_object_unref (client->media);
392 client->media = NULL;
394 if (!client->mount_points)
395 goto no_mount_points;
397 /* find the factory for the uri first */
399 gst_rtsp_mount_points_find_factory (client->mount_points,
403 state->factory = factory;
405 /* check if we have access to the factory */
406 if ((auth = gst_rtsp_media_factory_get_auth (factory))) {
407 if (!gst_rtsp_auth_check (auth, client, 0, state))
410 g_object_unref (auth);
413 /* prepare the media and add it to the pipeline */
414 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
417 g_object_unref (factory);
419 state->factory = NULL;
421 /* set ipv6 on the media before preparing */
422 media->is_ipv6 = client->is_ipv6;
423 state->media = media;
425 /* prepare the media */
426 if (!(gst_rtsp_media_prepare (media)))
429 /* now keep track of the uri and the media */
430 client->uri = gst_rtsp_url_copy (state->uri);
431 client->media = media;
433 /* we have seen this uri before, used cached media */
434 media = client->media;
435 state->media = media;
436 GST_INFO ("reusing cached media %p", media);
440 g_object_ref (media);
447 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
452 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
457 handle_unauthorized_request (client, auth, state);
458 g_object_unref (factory);
459 g_object_unref (auth);
464 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
465 g_object_unref (factory);
470 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
471 g_object_unref (media);
477 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
479 GstRTSPMessage message = { 0 };
484 gst_rtsp_message_init_data (&message, channel);
486 /* FIXME, need some sort of iovec RTSPMessage here */
487 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
490 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
492 /* FIXME, client->watch could have been finalized here, we need to keep an
493 * extra refcount to the watch. */
494 gst_rtsp_watch_send_message (client->watch, &message, NULL);
496 gst_rtsp_message_steal_body (&message, &data, &usize);
497 gst_buffer_unmap (buffer, &map_info);
499 gst_rtsp_message_unset (&message);
505 link_transport (GstRTSPClient * client, GstRTSPSession * session,
506 GstRTSPStreamTransport * trans)
508 GST_DEBUG ("client %p: linking transport %p", client, trans);
509 gst_rtsp_stream_transport_set_callbacks (trans,
510 (GstRTSPSendFunc) do_send_data,
511 (GstRTSPSendFunc) do_send_data, client, NULL);
513 client->transports = g_list_prepend (client->transports, trans);
515 /* make sure our session can't expire */
516 gst_rtsp_session_prevent_expire (session);
520 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
521 GstRTSPStreamTransport * trans)
523 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
524 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
526 client->transports = g_list_remove (client->transports, trans);
528 /* our session can now expire */
529 gst_rtsp_session_allow_expire (session);
533 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
534 GstRTSPSessionMedia * media)
538 n_streams = gst_rtsp_media_n_streams (media->media);
539 for (i = 0; i < n_streams; i++) {
540 GstRTSPStreamTransport *trans;
541 GstRTSPTransport *tr;
543 /* get the transport, if there is no transport configured, skip this stream */
544 trans = gst_rtsp_session_media_get_transport (media, i);
548 tr = trans->transport;
550 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
551 /* for TCP, unlink the stream from the TCP connection of the client */
552 unlink_transport (client, session, trans);
558 close_connection (GstRTSPClient * client)
560 const gchar *tunnelid;
562 GST_DEBUG ("client %p: closing connection", client);
564 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
565 g_mutex_lock (&tunnels_lock);
566 /* remove from tunnelids */
567 g_hash_table_remove (tunnels, tunnelid);
568 g_mutex_unlock (&tunnels_lock);
571 gst_rtsp_connection_close (client->connection);
575 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
577 GstRTSPSession *session;
578 GstRTSPSessionMedia *media;
579 GstRTSPStatusCode code;
584 session = state->session;
586 /* get a handle to the configuration of the media in the session */
587 media = gst_rtsp_session_get_media (session, state->uri);
591 state->sessmedia = media;
593 /* unlink the all TCP callbacks */
594 unlink_session_transports (client, session, media);
596 /* remove the session from the watched sessions */
597 g_object_weak_unref (G_OBJECT (session),
598 (GWeakNotify) client_session_finalized, client);
599 client->sessions = g_list_remove (client->sessions, session);
601 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
603 /* unmanage the media in the session, returns false if all media session
605 if (!gst_rtsp_session_release_media (session, media)) {
606 /* remove the session */
607 gst_rtsp_session_pool_remove (client->session_pool, session);
609 /* construct the response now */
610 code = GST_RTSP_STS_OK;
611 gst_rtsp_message_init_response (state->response, code,
612 gst_rtsp_status_as_text (code), state->request);
614 send_response (client, session, state->response, TRUE);
616 /* we emit the signal before closing the connection */
617 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
625 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
630 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
636 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
642 res = gst_rtsp_message_get_body (state->request, &data, &size);
643 if (res != GST_RTSP_OK)
647 /* no body, keep-alive request */
648 send_generic_response (client, GST_RTSP_STS_OK, state);
650 /* there is a body, handle the params */
651 res = gst_rtsp_params_get (client, state);
652 if (res != GST_RTSP_OK)
655 send_response (client, state->session, state->response, FALSE);
658 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
666 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
672 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
678 res = gst_rtsp_message_get_body (state->request, &data, &size);
679 if (res != GST_RTSP_OK)
683 /* no body, keep-alive request */
684 send_generic_response (client, GST_RTSP_STS_OK, state);
686 /* there is a body, handle the params */
687 res = gst_rtsp_params_set (client, state);
688 if (res != GST_RTSP_OK)
691 send_response (client, state->session, state->response, FALSE);
694 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
702 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
708 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
710 GstRTSPSession *session;
711 GstRTSPSessionMedia *media;
712 GstRTSPStatusCode code;
714 if (!(session = state->session))
717 /* get a handle to the configuration of the media in the session */
718 media = gst_rtsp_session_get_media (session, state->uri);
722 state->sessmedia = media;
724 /* the session state must be playing or recording */
725 if (media->state != GST_RTSP_STATE_PLAYING &&
726 media->state != GST_RTSP_STATE_RECORDING)
729 /* unlink the all TCP callbacks */
730 unlink_session_transports (client, session, media);
732 /* then pause sending */
733 gst_rtsp_session_media_set_state (media, GST_STATE_PAUSED);
735 /* construct the response now */
736 code = GST_RTSP_STS_OK;
737 gst_rtsp_message_init_response (state->response, code,
738 gst_rtsp_status_as_text (code), state->request);
740 send_response (client, session, state->response, FALSE);
742 /* the state is now READY */
743 media->state = GST_RTSP_STATE_READY;
745 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
753 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
758 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
763 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
770 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
772 GstRTSPSession *session;
773 GstRTSPSessionMedia *media;
774 GstRTSPStatusCode code;
776 guint n_streams, i, infocount;
778 GstRTSPTimeRange *range;
781 if (!(session = state->session))
784 /* get a handle to the configuration of the media in the session */
785 media = gst_rtsp_session_get_media (session, state->uri);
789 state->sessmedia = media;
791 /* the session state must be playing or ready */
792 if (media->state != GST_RTSP_STATE_PLAYING &&
793 media->state != GST_RTSP_STATE_READY)
796 /* parse the range header if we have one */
798 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
799 if (res == GST_RTSP_OK) {
800 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
801 /* we have a range, seek to the position */
802 gst_rtsp_media_seek (media->media, range);
803 gst_rtsp_range_free (range);
807 /* grab RTPInfo from the payloaders now */
808 rtpinfo = g_string_new ("");
810 n_streams = gst_rtsp_media_n_streams (media->media);
811 for (i = 0, infocount = 0; i < n_streams; i++) {
812 GstRTSPStreamTransport *trans;
813 GstRTSPTransport *tr;
817 /* get the transport, if there is no transport configured, skip this stream */
818 trans = gst_rtsp_session_media_get_transport (media, i);
820 GST_INFO ("stream %d is not configured", i);
823 tr = trans->transport;
825 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
826 /* for TCP, link the stream to the TCP connection of the client */
827 link_transport (client, session, trans);
830 if (gst_rtsp_stream_get_rtpinfo (trans->stream, &rtptime, &seq)) {
832 g_string_append (rtpinfo, ", ");
834 uristr = gst_rtsp_url_get_request_uri (state->uri);
835 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
836 uristr, i, seq, rtptime);
841 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
845 /* construct the response now */
846 code = GST_RTSP_STS_OK;
847 gst_rtsp_message_init_response (state->response, code,
848 gst_rtsp_status_as_text (code), state->request);
850 /* add the RTP-Info header */
852 str = g_string_free (rtpinfo, FALSE);
853 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
855 g_string_free (rtpinfo, TRUE);
859 str = gst_rtsp_media_get_range_string (media->media, TRUE);
860 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
862 send_response (client, session, state->response, FALSE);
864 /* start playing after sending the request */
865 gst_rtsp_session_media_set_state (media, GST_STATE_PLAYING);
867 media->state = GST_RTSP_STATE_PLAYING;
869 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
877 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
882 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
887 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
894 do_keepalive (GstRTSPSession * session)
896 GST_INFO ("keep session %p alive", session);
897 gst_rtsp_session_touch (session);
900 /* parse @transport and return a valid transport in @tr. only transports
901 * from @supported are returned. Returns FALSE if no valid transport
904 parse_transport (const char *transport, GstRTSPLowerTrans supported,
905 GstRTSPTransport * tr)
912 gst_rtsp_transport_init (tr);
914 GST_DEBUG ("parsing transports %s", transport);
916 transports = g_strsplit (transport, ",", 0);
918 /* loop through the transports, try to parse */
919 for (i = 0; transports[i]; i++) {
920 res = gst_rtsp_transport_parse (transports[i], tr);
921 if (res != GST_RTSP_OK) {
922 /* no valid transport, search some more */
923 GST_WARNING ("could not parse transport %s", transports[i]);
927 /* we have a transport, see if it's RTP/AVP */
928 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
929 GST_WARNING ("invalid transport %s", transports[i]);
933 if (!(tr->lower_transport & supported)) {
934 GST_WARNING ("unsupported transport %s", transports[i]);
938 /* we have a valid transport */
939 GST_INFO ("found valid transport %s", transports[i]);
944 gst_rtsp_transport_init (tr);
946 g_strfreev (transports);
952 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
953 GstRTSPMessage * request)
955 gchar *blocksize_str;
958 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
959 &blocksize_str, 0) == GST_RTSP_OK) {
963 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
964 if (end == blocksize_str) {
965 GST_ERROR ("failed to parse blocksize");
968 /* we don't want to change the mtu when this media
969 * can be shared because it impacts other clients */
970 if (gst_rtsp_media_is_shared (media))
973 if (blocksize > G_MAXUINT)
974 blocksize = G_MAXUINT;
975 gst_rtsp_stream_set_mtu (stream, blocksize);
982 configure_client_transport (GstRTSPClient * client, GstRTSPClientState * state,
983 GstRTSPTransport * ct)
985 /* we have a valid transport now, set the destination of the client. */
986 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
987 if (ct->destination == NULL || !client->use_client_settings) {
988 GstRTSPAddress *addr;
990 addr = gst_rtsp_stream_get_address (state->stream);
994 g_free (ct->destination);
995 ct->destination = g_strdup (addr->address);
996 ct->port.min = addr->port;
997 ct->port.max = addr->port + addr->n_ports - 1;
1003 url = gst_rtsp_connection_get_url (client->connection);
1004 g_free (ct->destination);
1005 ct->destination = g_strdup (url->host);
1007 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1008 /* check if the client selected channels for TCP */
1009 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1010 gst_rtsp_session_media_alloc_channels (state->sessmedia,
1020 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1025 static GstRTSPTransport *
1026 make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
1027 GstRTSPTransport * ct)
1029 GstRTSPTransport *st;
1031 /* prepare the server transport */
1032 gst_rtsp_transport_new (&st);
1034 st->trans = ct->trans;
1035 st->profile = ct->profile;
1036 st->lower_transport = ct->lower_transport;
1038 switch (st->lower_transport) {
1039 case GST_RTSP_LOWER_TRANS_UDP:
1040 st->client_port = ct->client_port;
1041 st->server_port = state->stream->server_port;
1043 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1044 st->port = ct->port;
1045 st->destination = g_strdup (ct->destination);
1048 case GST_RTSP_LOWER_TRANS_TCP:
1049 st->interleaved = ct->interleaved;
1054 if (state->stream->session)
1055 g_object_get (state->stream->session, "internal-ssrc", &st->ssrc, NULL);
1061 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
1066 GstRTSPTransport *ct, *st;
1067 GstRTSPLowerTrans supported;
1068 GstRTSPStatusCode code;
1069 GstRTSPSession *session;
1070 GstRTSPStreamTransport *trans;
1071 gchar *trans_str, *pos;
1073 GstRTSPSessionMedia *sessmedia;
1074 GstRTSPMedia *media;
1075 GstRTSPStream *stream;
1079 /* the uri contains the stream number we added in the SDP config, which is
1080 * always /stream=%d so we need to strip that off
1081 * parse the stream we need to configure, look for the stream in the abspath
1082 * first and then in the query. */
1083 if (uri->abspath == NULL || !(pos = strstr (uri->abspath, "/stream="))) {
1084 if (uri->query == NULL || !(pos = strstr (uri->query, "/stream=")))
1088 /* we can mofify the parsed uri in place */
1091 pos += strlen ("/stream=");
1092 if (sscanf (pos, "%u", &streamid) != 1)
1095 /* parse the transport */
1097 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
1099 if (res != GST_RTSP_OK)
1102 gst_rtsp_transport_new (&ct);
1104 /* our supported transports */
1105 supported = GST_RTSP_LOWER_TRANS_UDP |
1106 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
1108 /* parse and find a usable supported transport */
1109 if (!parse_transport (transport, supported, ct))
1110 goto unsupported_transports;
1112 /* we create the session after parsing stuff so that we don't make
1113 * a session for malformed requests */
1114 if (client->session_pool == NULL)
1117 session = state->session;
1120 g_object_ref (session);
1121 /* get a handle to the configuration of the media in the session, this can
1122 * return NULL if this is a new url to manage in this session. */
1123 sessmedia = gst_rtsp_session_get_media (session, uri);
1125 /* create a session if this fails we probably reached our session limit or
1127 if (!(session = gst_rtsp_session_pool_create (client->session_pool)))
1128 goto service_unavailable;
1130 state->session = session;
1132 /* we need a new media configuration in this session */
1136 /* we have no media, find one and manage it */
1137 if (sessmedia == NULL) {
1138 /* get a handle to the configuration of the media in the session */
1139 if ((media = find_media (client, state))) {
1140 /* manage the media in our session now */
1141 sessmedia = gst_rtsp_session_manage_media (session, uri, media);
1145 /* if we stil have no media, error */
1146 if (sessmedia == NULL)
1149 state->sessmedia = sessmedia;
1150 state->media = media = sessmedia->media;
1152 /* now get the stream */
1153 stream = gst_rtsp_media_get_stream (media, streamid);
1157 state->stream = stream;
1159 /* set blocksize on this stream */
1160 if (!handle_blocksize (media, stream, state->request))
1161 goto invalid_blocksize;
1163 /* update the client transport */
1164 if (!configure_client_transport (client, state, ct))
1165 goto unsupported_client_transport;
1167 /* set in the session media transport */
1168 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1170 /* configure keepalive for this transport */
1171 gst_rtsp_stream_transport_set_keepalive (trans,
1172 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1174 /* create and serialize the server transport */
1175 st = make_server_transport (client, state, ct);
1176 trans_str = gst_rtsp_transport_as_text (st);
1177 gst_rtsp_transport_free (st);
1179 /* construct the response now */
1180 code = GST_RTSP_STS_OK;
1181 gst_rtsp_message_init_response (state->response, code,
1182 gst_rtsp_status_as_text (code), state->request);
1184 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1188 send_response (client, session, state->response, FALSE);
1190 /* update the state */
1191 switch (sessmedia->state) {
1192 case GST_RTSP_STATE_PLAYING:
1193 case GST_RTSP_STATE_RECORDING:
1194 case GST_RTSP_STATE_READY:
1195 /* no state change */
1198 sessmedia->state = GST_RTSP_STATE_READY;
1201 g_object_unref (session);
1203 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST],
1211 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1216 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1217 g_object_unref (session);
1218 gst_rtsp_transport_free (ct);
1223 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1224 g_object_unref (session);
1225 gst_rtsp_transport_free (ct);
1228 unsupported_client_transport:
1230 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1231 g_object_unref (session);
1232 gst_rtsp_transport_free (ct);
1237 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1240 unsupported_transports:
1242 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1243 gst_rtsp_transport_free (ct);
1248 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1249 gst_rtsp_transport_free (ct);
1252 service_unavailable:
1254 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1255 gst_rtsp_transport_free (ct);
1260 static GstSDPMessage *
1261 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1267 gst_sdp_message_new (&sdp);
1269 /* some standard things first */
1270 gst_sdp_message_set_version (sdp, "0");
1272 if (client->is_ipv6)
1277 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1280 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1281 gst_sdp_message_set_information (sdp, "rtsp-server");
1282 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1283 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1284 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1285 gst_sdp_message_add_attribute (sdp, "control", "*");
1287 info.server_proto = proto;
1288 info.server_ip = g_strdup (client->server_ip);
1290 /* create an SDP for the media object */
1291 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1294 g_free (info.server_ip);
1301 g_free (info.server_ip);
1302 gst_sdp_message_free (sdp);
1307 /* for the describe we must generate an SDP */
1309 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1314 gchar *str, *content_base;
1315 GstRTSPMedia *media;
1316 GstRTSPClientClass *klass;
1318 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1320 /* check what kind of format is accepted, we don't really do anything with it
1321 * and always return SDP for now. */
1326 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1328 if (res == GST_RTSP_ENOTIMPL)
1331 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1335 /* find the media object for the uri */
1336 if (!(media = find_media (client, state)))
1339 /* create an SDP for the media object on this client */
1340 if (!(sdp = klass->create_sdp (client, media)))
1343 g_object_unref (media);
1345 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1346 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1348 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1351 /* content base for some clients that might screw up creating the setup uri */
1352 str = gst_rtsp_url_get_request_uri (state->uri);
1353 str_len = strlen (str);
1355 /* check for trailing '/' and append one */
1356 if (str[str_len - 1] != '/') {
1357 content_base = g_malloc (str_len + 2);
1358 memcpy (content_base, str, str_len);
1359 content_base[str_len] = '/';
1360 content_base[str_len + 1] = '\0';
1366 GST_INFO ("adding content-base: %s", content_base);
1368 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1370 g_free (content_base);
1372 /* add SDP to the response body */
1373 str = gst_sdp_message_as_text (sdp);
1374 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1375 gst_sdp_message_free (sdp);
1377 send_response (client, state->session, state->response, FALSE);
1379 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1387 /* error reply is already sent */
1392 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1393 g_object_unref (media);
1399 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1401 GstRTSPMethod options;
1404 options = GST_RTSP_DESCRIBE |
1409 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1411 str = gst_rtsp_options_as_text (options);
1413 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1414 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1416 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1419 send_response (client, state->session, state->response, FALSE);
1421 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1427 /* remove duplicate and trailing '/' */
1429 sanitize_uri (GstRTSPUrl * uri)
1433 gboolean have_slash, prev_slash;
1435 s = d = uri->abspath;
1436 len = strlen (uri->abspath);
1440 for (i = 0; i < len; i++) {
1441 have_slash = s[i] == '/';
1443 if (!have_slash || !prev_slash)
1445 prev_slash = have_slash;
1447 len = d - uri->abspath;
1448 /* don't remove the first slash if that's the only thing left */
1449 if (len > 1 && *(d - 1) == '/')
1455 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1457 GST_INFO ("client %p: session %p finished", client, session);
1459 /* unlink all media managed in this session */
1460 client_unlink_session (client, session);
1462 /* remove the session */
1463 if (!(client->sessions = g_list_remove (client->sessions, session))) {
1464 GST_INFO ("client %p: all sessions finalized, close the connection",
1466 close_connection (client);
1471 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
1475 for (walk = client->sessions; walk; walk = g_list_next (walk)) {
1476 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
1478 /* we already know about this session */
1479 if (msession == session)
1483 GST_INFO ("watching session %p", session);
1485 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
1487 client->sessions = g_list_prepend (client->sessions, session);
1489 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1494 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1496 GstRTSPMethod method;
1497 const gchar *uristr;
1499 GstRTSPVersion version;
1501 GstRTSPSession *session;
1502 GstRTSPClientState state = { NULL };
1503 GstRTSPMessage response = { 0 };
1506 state.request = request;
1507 state.response = &response;
1509 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1510 gst_rtsp_message_dump (request);
1513 GST_INFO ("client %p: received a request", client);
1515 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1517 if (version != GST_RTSP_VERSION_1_0) {
1518 /* we can only handle 1.0 requests */
1519 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1523 state.method = method;
1525 /* we always try to parse the url first */
1526 if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
1527 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1531 /* sanitize the uri */
1535 /* get the session if there is any */
1536 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1537 if (res == GST_RTSP_OK) {
1538 if (client->session_pool == NULL)
1541 /* we had a session in the request, find it again */
1542 if (!(session = gst_rtsp_session_pool_find (client->session_pool, sessid)))
1543 goto session_not_found;
1545 /* we add the session to the client list of watched sessions. When a session
1546 * disappears because it times out, we will be notified. If all sessions are
1547 * gone, we will close the connection */
1548 client_watch_session (client, session);
1552 state.session = session;
1555 if (!gst_rtsp_auth_check (client->auth, client, 0, &state))
1556 goto not_authorized;
1559 /* now see what is asked and dispatch to a dedicated handler */
1561 case GST_RTSP_OPTIONS:
1562 handle_options_request (client, &state);
1564 case GST_RTSP_DESCRIBE:
1565 handle_describe_request (client, &state);
1567 case GST_RTSP_SETUP:
1568 handle_setup_request (client, &state);
1571 handle_play_request (client, &state);
1573 case GST_RTSP_PAUSE:
1574 handle_pause_request (client, &state);
1576 case GST_RTSP_TEARDOWN:
1577 handle_teardown_request (client, &state);
1579 case GST_RTSP_SET_PARAMETER:
1580 handle_set_param_request (client, &state);
1582 case GST_RTSP_GET_PARAMETER:
1583 handle_get_param_request (client, &state);
1585 case GST_RTSP_ANNOUNCE:
1586 case GST_RTSP_RECORD:
1587 case GST_RTSP_REDIRECT:
1588 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1590 case GST_RTSP_INVALID:
1592 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1596 g_object_unref (session);
1598 gst_rtsp_url_free (uri);
1604 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, &state);
1609 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1614 handle_unauthorized_request (client, client->auth, &state);
1620 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1630 /* find the stream for this message */
1631 res = gst_rtsp_message_parse_data (message, &channel);
1632 if (res != GST_RTSP_OK)
1635 gst_rtsp_message_steal_body (message, &data, &size);
1637 buffer = gst_buffer_new_wrapped (data, size);
1640 for (walk = client->transports; walk; walk = g_list_next (walk)) {
1641 GstRTSPStreamTransport *trans;
1642 GstRTSPStream *stream;
1643 GstRTSPTransport *tr;
1647 /* we only add clients with a transport to the list */
1648 tr = trans->transport;
1649 stream = trans->stream;
1651 /* check for TCP transport */
1652 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1653 /* dispatch to the stream based on the channel number */
1654 if (tr->interleaved.min == channel) {
1655 gst_rtsp_stream_recv_rtp (stream, buffer);
1658 } else if (tr->interleaved.max == channel) {
1659 gst_rtsp_stream_recv_rtcp (stream, buffer);
1666 gst_buffer_unref (buffer);
1670 * gst_rtsp_client_set_session_pool:
1671 * @client: a #GstRTSPClient
1672 * @pool: a #GstRTSPSessionPool
1674 * Set @pool as the sessionpool for @client which it will use to find
1675 * or allocate sessions. the sessionpool is usually inherited from the server
1676 * that created the client but can be overridden later.
1679 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
1680 GstRTSPSessionPool * pool)
1682 GstRTSPSessionPool *old;
1684 old = client->session_pool;
1687 g_object_ref (pool);
1688 client->session_pool = pool;
1690 g_object_unref (old);
1695 * gst_rtsp_client_get_session_pool:
1696 * @client: a #GstRTSPClient
1698 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
1700 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
1702 GstRTSPSessionPool *
1703 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
1705 GstRTSPSessionPool *result;
1707 if ((result = client->session_pool))
1708 g_object_ref (result);
1714 * gst_rtsp_client_set_server:
1715 * @client: a #GstRTSPClient
1716 * @server: a #GstRTSPServer
1718 * Set @server as the server that created @client.
1721 gst_rtsp_client_set_server (GstRTSPClient * client, GstRTSPServer * server)
1725 old = client->server;
1726 if (old != server) {
1728 g_object_ref (server);
1729 client->server = server;
1731 g_object_unref (old);
1736 * gst_rtsp_client_get_server:
1737 * @client: a #GstRTSPClient
1739 * Get the #GstRTSPServer object that @client was created from.
1741 * Returns: (transfer full): a #GstRTSPServer, unref after usage.
1744 gst_rtsp_client_get_server (GstRTSPClient * client)
1746 GstRTSPServer *result;
1748 if ((result = client->server))
1749 g_object_ref (result);
1755 * gst_rtsp_client_set_mount_points:
1756 * @client: a #GstRTSPClient
1757 * @mounts: a #GstRTSPMountPoints
1759 * Set @mounts as the mount points for @client which it will use to map urls
1760 * to media streams. These mount points are usually inherited from the server that
1761 * created the client but can be overriden later.
1764 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
1765 GstRTSPMountPoints * mounts)
1767 GstRTSPMountPoints *old;
1769 old = client->mount_points;
1771 if (old != mounts) {
1773 g_object_ref (mounts);
1774 client->mount_points = mounts;
1776 g_object_unref (old);
1781 * gst_rtsp_client_get_mount_points:
1782 * @client: a #GstRTSPClient
1784 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
1786 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
1788 GstRTSPMountPoints *
1789 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
1791 GstRTSPMountPoints *result;
1793 if ((result = client->mount_points))
1794 g_object_ref (result);
1800 * gst_rtsp_client_set_use_client_settings:
1801 * @client: a #GstRTSPClient
1802 * @use_client_settings: whether to use client settings for multicast
1804 * Use client transport settings (destination and ttl) for multicast.
1805 * When @use_client_settings is %FALSE, the server settings will be
1809 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
1810 gboolean use_client_settings)
1812 client->use_client_settings = use_client_settings;
1816 * gst_rtsp_client_get_use_client_settings:
1817 * @client: a #GstRTSPClient
1819 * Check if client transport settings (destination and ttl) for multicast
1823 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
1825 return client->use_client_settings;
1829 * gst_rtsp_client_set_auth:
1830 * @client: a #GstRTSPClient
1831 * @auth: a #GstRTSPAuth
1833 * configure @auth to be used as the authentication manager of @client.
1836 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
1840 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1846 g_object_ref (auth);
1847 client->auth = auth;
1849 g_object_unref (old);
1855 * gst_rtsp_client_get_auth:
1856 * @client: a #GstRTSPClient
1858 * Get the #GstRTSPAuth used as the authentication manager of @client.
1860 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
1864 gst_rtsp_client_get_auth (GstRTSPClient * client)
1866 GstRTSPAuth *result;
1868 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1870 if ((result = client->auth))
1871 g_object_ref (result);
1876 static GstRTSPResult
1877 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
1880 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1882 switch (message->type) {
1883 case GST_RTSP_MESSAGE_REQUEST:
1884 handle_request (client, message);
1886 case GST_RTSP_MESSAGE_RESPONSE:
1888 case GST_RTSP_MESSAGE_DATA:
1889 handle_data (client, message);
1897 static GstRTSPResult
1898 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
1900 GstRTSPClient *client;
1902 client = GST_RTSP_CLIENT (user_data);
1903 if (client->close_response_seq && client->close_response_seq == cseq) {
1904 client->close_response_seq = 0;
1905 close_connection (client);
1911 static GstRTSPResult
1912 closed (GstRTSPWatch * watch, gpointer user_data)
1914 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1915 const gchar *tunnelid;
1917 GST_INFO ("client %p: connection closed", client);
1919 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
1920 g_mutex_lock (&tunnels_lock);
1921 /* remove from tunnelids */
1922 g_hash_table_remove (tunnels, tunnelid);
1923 g_mutex_unlock (&tunnels_lock);
1929 static GstRTSPResult
1930 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
1932 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1935 str = gst_rtsp_strresult (result);
1936 GST_INFO ("client %p: received an error %s", client, str);
1942 static GstRTSPResult
1943 error_full (GstRTSPWatch * watch, GstRTSPResult result,
1944 GstRTSPMessage * message, guint id, gpointer user_data)
1946 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1949 str = gst_rtsp_strresult (result);
1951 ("client %p: received an error %s when handling message %p with id %d",
1952 client, str, message, id);
1959 remember_tunnel (GstRTSPClient * client)
1961 const gchar *tunnelid;
1963 /* store client in the pending tunnels */
1964 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
1965 if (tunnelid == NULL)
1968 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
1970 /* we can't have two clients connecting with the same tunnelid */
1971 g_mutex_lock (&tunnels_lock);
1972 if (g_hash_table_lookup (tunnels, tunnelid))
1973 goto tunnel_existed;
1975 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
1976 g_mutex_unlock (&tunnels_lock);
1983 GST_ERROR ("client %p: no tunnelid provided", client);
1988 g_mutex_unlock (&tunnels_lock);
1989 GST_ERROR ("client %p: tunnel session %s already existed", client,
1995 static GstRTSPStatusCode
1996 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
1998 GstRTSPClient *client;
2000 client = GST_RTSP_CLIENT (user_data);
2002 GST_INFO ("client %p: tunnel start (connection %p)", client,
2003 client->connection);
2005 if (!remember_tunnel (client))
2008 return GST_RTSP_STS_OK;
2013 GST_ERROR ("client %p: error starting tunnel", client);
2014 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2018 static GstRTSPResult
2019 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2021 GstRTSPClient *client;
2023 client = GST_RTSP_CLIENT (user_data);
2025 GST_INFO ("client %p: tunnel lost (connection %p)", client,
2026 client->connection);
2028 /* ignore error, it'll only be a problem when the client does a POST again */
2029 remember_tunnel (client);
2034 static GstRTSPResult
2035 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2037 const gchar *tunnelid;
2038 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2039 GstRTSPClient *oclient;
2041 GST_INFO ("client %p: tunnel complete", client);
2043 /* find previous tunnel */
2044 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
2045 if (tunnelid == NULL)
2048 g_mutex_lock (&tunnels_lock);
2049 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2052 /* remove the old client from the table. ref before because removing it will
2053 * remove the ref to it. */
2054 g_object_ref (oclient);
2055 g_hash_table_remove (tunnels, tunnelid);
2057 if (oclient->watch == NULL)
2059 g_mutex_unlock (&tunnels_lock);
2061 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2062 oclient->connection, client->connection);
2064 /* merge the tunnels into the first client */
2065 gst_rtsp_connection_do_tunnel (oclient->connection, client->connection);
2066 gst_rtsp_watch_reset (oclient->watch);
2067 g_object_unref (oclient);
2074 GST_INFO ("client %p: no tunnelid provided", client);
2075 return GST_RTSP_ERROR;
2079 g_mutex_unlock (&tunnels_lock);
2080 GST_INFO ("client %p: tunnel session %s not found", client, tunnelid);
2081 return GST_RTSP_ERROR;
2085 g_mutex_unlock (&tunnels_lock);
2086 GST_INFO ("client %p: tunnel session %s was closed", client, tunnelid);
2087 g_object_unref (oclient);
2088 return GST_RTSP_ERROR;
2092 static GstRTSPWatchFuncs watch_funcs = {
2104 client_watch_notify (GstRTSPClient * client)
2106 GST_INFO ("client %p: watch destroyed", client);
2107 client->watch = NULL;
2108 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2109 g_object_unref (client);
2113 setup_client (GstRTSPClient * client, GSocket * socket,
2114 GstRTSPConnection * conn, GError ** error)
2116 GSocket *read_socket;
2117 GSocketAddress *address;
2120 read_socket = gst_rtsp_connection_get_read_socket (conn);
2121 client->is_ipv6 = g_socket_get_family (socket) == G_SOCKET_FAMILY_IPV6;
2123 if (!(address = g_socket_get_remote_address (read_socket, error)))
2126 g_free (client->server_ip);
2127 /* keep the original ip that the client connected to */
2128 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2129 GInetAddress *iaddr;
2131 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2133 client->server_ip = g_inet_address_to_string (iaddr);
2134 g_object_unref (address);
2136 client->server_ip = g_strdup ("unknown");
2139 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2140 client->server_ip, client->is_ipv6);
2142 url = gst_rtsp_connection_get_url (conn);
2143 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2145 client->connection = conn;
2152 GST_ERROR ("could not get remote address %s", (*error)->message);
2158 * gst_rtsp_client_use_socket:
2159 * @client: a #GstRTSPClient
2160 * @socket: a #GSocket
2161 * @ip: the IP address of the remote client
2162 * @port: the port used by the other end
2163 * @initial_buffer: any zero terminated initial data that was already read from
2167 * Take an existing network socket and use it for an RTSP connection.
2169 * Returns: %TRUE on success.
2172 gst_rtsp_client_use_socket (GstRTSPClient * client, GSocket * socket,
2173 const gchar * ip, gint port, const gchar * initial_buffer, GError ** error)
2175 GstRTSPConnection *conn;
2178 GST_RTSP_CHECK (gst_rtsp_connection_create_from_socket (socket, ip, port,
2179 initial_buffer, &conn), no_connection);
2181 return setup_client (client, socket, conn, error);
2186 gchar *str = gst_rtsp_strresult (res);
2188 GST_ERROR ("could not create connection from socket %p: %s", socket, str);
2195 * gst_rtsp_client_accept:
2196 * @client: a #GstRTSPClient
2197 * @socket: a #GSocket
2198 * @context: the context to run in
2199 * @cancellable: a #GCancellable
2202 * Accept a new connection for @client on @socket.
2204 * Returns: %TRUE if the client could be accepted.
2207 gst_rtsp_client_accept (GstRTSPClient * client, GSocket * socket,
2208 GCancellable * cancellable, GError ** error)
2210 GstRTSPConnection *conn;
2213 /* a new client connected. */
2214 GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, cancellable),
2217 return setup_client (client, socket, conn, error);
2222 gchar *str = gst_rtsp_strresult (res);
2224 GST_ERROR ("Could not accept client on server socket %p: %s", socket, str);
2231 * gst_rtsp_client_attach:
2232 * @client: a #GstRTSPClient
2233 * @context: (allow-none): a #GMainContext
2235 * Attaches @client to @context. When the mainloop for @context is run, the
2236 * client will be dispatched. When @context is NULL, the default context will be
2239 * This function should be called when the client properties and urls are fully
2240 * configured and the client is ready to start.
2242 * Returns: the ID (greater than 0) for the source within the GMainContext.
2245 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2249 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2250 g_return_val_if_fail (client->watch == NULL, 0);
2252 /* create watch for the connection and attach */
2253 client->watch = gst_rtsp_watch_new (client->connection, &watch_funcs,
2254 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2256 GST_INFO ("attaching to context %p", context);
2257 res = gst_rtsp_watch_attach (client->watch, context);
2258 gst_rtsp_watch_unref (client->watch);