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.
20 #include <sys/ioctl.h>
22 #include <gst/sdp/gstsdpmessage.h>
24 #include "rtsp-client.h"
28 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
31 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
33 GObjectClass *gobject_class;
35 gobject_class = G_OBJECT_CLASS (klass);
39 gst_rtsp_client_init (GstRTSPClient * client)
44 * gst_rtsp_client_new:
46 * Create a new #GstRTSPClient instance.
49 gst_rtsp_client_new (void)
51 GstRTSPClient *result;
53 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
59 handle_generic_response (GstRTSPClient *client, GstRTSPStatusCode code,
60 GstRTSPMessage *request)
62 GstRTSPMessage response = { 0 };
64 gst_rtsp_message_init_response (&response, code,
65 gst_rtsp_status_as_text (code), request);
67 gst_rtsp_connection_send (client->connection, &response, NULL);
71 handle_teardown_response (GstRTSPClient *client, const gchar *uri, GstRTSPMessage *request)
74 GstRTSPSessionMedia *media;
75 GstRTSPSession *session;
77 GstRTSPMessage response = { 0 };
78 GstRTSPStatusCode code;
80 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
81 if (res == GST_RTSP_OK) {
82 /* we had a session in the request, find it again */
83 if (!(session = gst_rtsp_session_pool_find (client->pool, sessid)))
84 goto session_not_found;
87 goto service_unavailable;
89 /* get a handle to the configuration of the media in the session */
90 media = gst_rtsp_session_get_media (session, client->media);
94 gst_rtsp_session_media_stop (media);
96 gst_rtsp_session_pool_remove (client->pool, session);
97 g_object_unref (session);
99 /* remove the session id from the request, which will also remove it from the
101 gst_rtsp_message_remove_header (request, GST_RTSP_HDR_SESSION, -1);
103 /* construct the response now */
104 code = GST_RTSP_STS_OK;
105 gst_rtsp_message_init_response (&response, code, gst_rtsp_status_as_text (code), request);
107 gst_rtsp_connection_send (client->connection, &response, NULL);
114 handle_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
123 handle_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
129 handle_pause_response (GstRTSPClient *client, const gchar *uri, GstRTSPMessage *request)
132 GstRTSPSessionMedia *media;
133 GstRTSPSession *session;
135 GstRTSPMessage response = { 0 };
136 GstRTSPStatusCode code;
138 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
139 if (res == GST_RTSP_OK) {
140 /* we had a session in the request, find it again */
141 if (!(session = gst_rtsp_session_pool_find (client->pool, sessid)))
142 goto session_not_found;
145 goto service_unavailable;
147 /* get a handle to the configuration of the media in the session */
148 media = gst_rtsp_session_get_media (session, client->media);
152 gst_rtsp_session_media_pause (media);
153 g_object_unref (session);
155 /* construct the response now */
156 code = GST_RTSP_STS_OK;
157 gst_rtsp_message_init_response (&response, code, gst_rtsp_status_as_text (code), request);
159 gst_rtsp_connection_send (client->connection, &response, NULL);
166 handle_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
175 handle_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
181 handle_play_response (GstRTSPClient *client, const gchar *uri, GstRTSPMessage *request)
184 GstRTSPSessionMedia *media;
185 GstRTSPSession *session;
187 GstRTSPMessage response = { 0 };
188 GstRTSPStatusCode code;
189 GstStateChangeReturn ret;
192 guint timestamp, seqnum;
194 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
195 if (res == GST_RTSP_OK) {
196 /* we had a session in the request, find it again */
197 if (!(session = gst_rtsp_session_pool_find (client->pool, sessid)))
198 goto session_not_found;
201 goto service_unavailable;
203 /* get a handle to the configuration of the media in the session */
204 media = gst_rtsp_session_get_media (session, client->media);
208 /* wait for paused to get the caps */
209 ret = gst_rtsp_session_media_pause (media);
211 case GST_STATE_CHANGE_NO_PREROLL:
213 case GST_STATE_CHANGE_SUCCESS:
215 case GST_STATE_CHANGE_FAILURE:
216 goto service_unavailable;
217 case GST_STATE_CHANGE_ASYNC:
218 ret = gst_element_get_state (media->pipeline, NULL, NULL, -1);
222 /* grab RTPInfo from the payloaders now */
223 rtpinfo = g_string_new ("");
224 n_streams = gst_rtsp_media_n_streams (client->media);
225 for (i = 0; i < n_streams; i++) {
226 GstRTSPMediaStream *stream;
228 stream = gst_rtsp_media_get_stream (client->media, i);
230 g_object_get (G_OBJECT (stream->payloader), "seqnum", &seqnum, NULL);
231 g_object_get (G_OBJECT (stream->payloader), "timestamp", ×tamp, NULL);
234 g_string_append (rtpinfo, ", ");
235 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u", uri, i, seqnum, timestamp);
238 /* construct the response now */
239 code = GST_RTSP_STS_OK;
240 gst_rtsp_message_init_response (&response, code, gst_rtsp_status_as_text (code), request);
242 /* add the RTP-Info header */
243 gst_rtsp_message_add_header (&response, GST_RTSP_HDR_RTP_INFO, rtpinfo->str);
244 g_string_free (rtpinfo, TRUE);
246 gst_rtsp_connection_send (client->connection, &response, NULL);
248 /* start playing after sending the request */
249 gst_rtsp_session_media_play (media);
250 g_object_unref (session);
257 handle_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
262 handle_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, request);
267 handle_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
273 handle_setup_response (GstRTSPClient *client, const gchar *uri, GstRTSPMessage *request)
279 gboolean have_transport;
280 GstRTSPTransport *ct, *st;
281 GstRTSPSession *session;
283 GstRTSPLowerTrans supported;
284 GstRTSPMessage response = { 0 };
285 GstRTSPStatusCode code;
286 GstRTSPSessionStream *stream;
287 gchar *trans_str, *pos;
289 GstRTSPSessionMedia *media;
290 gboolean need_session;
292 /* find the media associated with the uri */
293 if (client->media == NULL) {
294 if ((client->media = gst_rtsp_media_new (uri)) == NULL)
298 /* parse the transport */
299 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_TRANSPORT, &transport, 0);
300 if (res != GST_RTSP_OK)
301 goto unsupported_transports;
303 transports = g_strsplit (transport, ",", 0);
304 gst_rtsp_transport_new (&ct);
306 /* loop through the transports, try to parse */
307 have_transport = FALSE;
308 for (i = 0; transports[i]; i++) {
310 gst_rtsp_transport_init (ct);
311 res = gst_rtsp_transport_parse (transports[i], ct);
312 if (res == GST_RTSP_OK) {
313 have_transport = TRUE;
317 g_strfreev (transports);
319 /* we have not found anything usable, error out */
320 if (!have_transport) {
321 gst_rtsp_transport_free (ct);
322 goto unsupported_transports;
325 /* we have a valid transport, check if we can handle it */
326 if (ct->trans != GST_RTSP_TRANS_RTP)
327 goto unsupported_transports;
328 if (ct->profile != GST_RTSP_PROFILE_AVP)
329 goto unsupported_transports;
330 supported = GST_RTSP_LOWER_TRANS_UDP |
331 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
332 if (!(ct->lower_transport & supported))
333 goto unsupported_transports;
335 /* a setup request creates a session for a client, check if the client already
336 * sent a session id to us */
337 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
338 if (res == GST_RTSP_OK) {
339 /* we had a session in the request, find it again */
340 if (!(session = gst_rtsp_session_pool_find (client->pool, sessid)))
341 goto session_not_found;
342 need_session = FALSE;
345 /* create a session if this fails we probably reached our session limit or
347 if (!(session = gst_rtsp_session_pool_create (client->pool)))
348 goto service_unavailable;
352 /* get a handle to the configuration of the media in the session */
353 media = gst_rtsp_session_get_media (session, client->media);
357 /* parse the stream we need to configure */
358 if (!(pos = strstr (uri, "stream=")))
361 pos += strlen ("stream=");
362 if (sscanf (pos, "%u", &streamid) != 1)
365 /* get a handle to the stream in the media */
366 stream = gst_rtsp_session_get_stream (media, streamid);
368 /* setup the server transport from the client transport */
369 st = gst_rtsp_session_stream_set_transport (stream, inet_ntoa (client->address.sin_addr), ct);
371 /* serialize the server transport */
372 trans_str = gst_rtsp_transport_as_text (st);
374 /* construct the response now */
375 code = GST_RTSP_STS_OK;
376 gst_rtsp_message_init_response (&response, code, gst_rtsp_status_as_text (code), request);
379 gst_rtsp_message_add_header (&response, GST_RTSP_HDR_SESSION, session->sessionid);
380 gst_rtsp_message_add_header (&response, GST_RTSP_HDR_TRANSPORT, trans_str);
382 g_object_unref (session);
384 gst_rtsp_connection_send (client->connection, &response, NULL);
391 handle_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
396 handle_generic_response (client, GST_RTSP_STS_BAD_REQUEST, request);
401 handle_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
404 unsupported_transports:
406 handle_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, request);
411 handle_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, request);
417 handle_describe_response (GstRTSPClient *client, const gchar *uri, GstRTSPMessage *request)
419 GstRTSPMessage response = { 0 };
424 GstElement *pipeline = NULL;
426 /* check what kind of format is accepted */
429 /* for the describe we must generate an SDP */
430 if (!(media = gst_rtsp_media_new (uri)))
433 /* create a pipeline if we have to */
434 if (pipeline == NULL) {
435 pipeline = gst_pipeline_new ("client-pipeline");
438 /* prepare the media into the pipeline */
439 if (!gst_rtsp_media_prepare (media, GST_BIN (pipeline)))
442 /* link fakesink to all stream pads and set the pipeline to PLAYING */
443 n_streams = gst_rtsp_media_n_streams (media);
444 for (i = 0; i < n_streams; i++) {
445 GstRTSPMediaStream *stream;
449 stream = gst_rtsp_media_get_stream (media, i);
451 sink = gst_element_factory_make ("fakesink", NULL);
452 gst_bin_add (GST_BIN (pipeline), sink);
454 sinkpad = gst_element_get_static_pad (sink, "sink");
455 gst_pad_link (stream->srcpad, sinkpad);
456 gst_object_unref (sinkpad);
459 /* now play and wait till we get the pads blocked. At that time the pipeline
460 * is prerolled and we have the caps on the streams too. */
461 gst_element_set_state (pipeline, GST_STATE_PLAYING);
463 /* wait for state change to complete */
464 gst_element_get_state (pipeline, NULL, NULL, -1);
466 /* we should now be able to construct the SDP message */
467 gst_sdp_message_new (&sdp);
469 /* some standard things first */
470 gst_sdp_message_set_version (sdp, "0");
471 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", "IP4", "127.0.0.1");
472 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
473 gst_sdp_message_set_information (sdp, "rtsp-server");
474 gst_sdp_message_add_time (sdp, "0", "0", NULL);
475 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
476 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
478 for (i = 0; i < n_streams; i++) {
479 GstRTSPMediaStream *stream;
482 const gchar *caps_str, *caps_enc, *caps_params;
484 gint caps_pt, caps_rate;
489 stream = gst_rtsp_media_get_stream (media, i);
490 gst_sdp_media_new (&smedia);
492 s = gst_caps_get_structure (stream->caps, 0);
494 /* get media type and payload for the m= line */
495 caps_str = gst_structure_get_string (s, "media");
496 gst_sdp_media_set_media (smedia, caps_str);
498 gst_structure_get_int (s, "payload", &caps_pt);
499 tmp = g_strdup_printf ("%d", caps_pt);
500 gst_sdp_media_add_format (smedia, tmp);
503 gst_sdp_media_set_port_info (smedia, 0, 1);
504 gst_sdp_media_set_proto (smedia, "RTP/AVP");
506 /* for the c= line */
507 gst_sdp_media_add_connection (smedia, "IN", "IP4", "127.0.0.1", 0, 0);
509 /* get clock-rate, media type and params for the rtpmap attribute */
510 gst_structure_get_int (s, "clock-rate", &caps_rate);
511 caps_enc = gst_structure_get_string (s, "encoding-name");
512 caps_params = gst_structure_get_string (s, "encoding-params");
515 tmp = g_strdup_printf ("%d %s/%d/%s", caps_pt, caps_enc, caps_rate,
518 tmp = g_strdup_printf ("%d %s/%d", caps_pt, caps_enc, caps_rate);
520 gst_sdp_media_add_attribute (smedia, "rtpmap", tmp);
524 tmp = g_strdup_printf ("stream=%d", i);
525 gst_sdp_media_add_attribute (smedia, "control", tmp);
528 /* collect all other properties and add them to fmtp */
529 fmtp = g_string_new ("");
530 g_string_append_printf (fmtp, "%d ", caps_pt);
532 n_fields = gst_structure_n_fields (s);
533 for (j = 0; j < n_fields; j++) {
534 const gchar *fname, *fval;
536 fname = gst_structure_nth_field_name (s, j);
538 /* filter out standard properties */
539 if (!strcmp (fname, "media"))
541 if (!strcmp (fname, "payload"))
543 if (!strcmp (fname, "clock-rate"))
545 if (!strcmp (fname, "encoding-name"))
547 if (!strcmp (fname, "encoding-params"))
549 if (!strcmp (fname, "ssrc"))
551 if (!strcmp (fname, "clock-base"))
553 if (!strcmp (fname, "seqnum-base"))
556 if ((fval = gst_structure_get_string (s, fname))) {
557 g_string_append_printf (fmtp, "%s%s=%s", first ? "":";", fname, fval);
562 tmp = g_string_free (fmtp, FALSE);
563 gst_sdp_media_add_attribute (smedia, "fmtp", tmp);
567 g_string_free (fmtp, TRUE);
569 gst_sdp_message_add_media (sdp, smedia);
571 /* go back to NULL */
572 gst_element_set_state (pipeline, GST_STATE_NULL);
574 g_object_unref (media);
576 gst_object_unref (pipeline);
579 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK,
580 gst_rtsp_status_as_text (GST_RTSP_STS_OK), request);
582 /* add SDP to the response body */
583 sdptext = gst_sdp_message_as_text (sdp);
584 gst_rtsp_message_take_body (&response, (guint8 *)sdptext, strlen (sdptext));
585 gst_sdp_message_free (sdp);
587 gst_rtsp_connection_send (client->connection, &response, NULL);
594 handle_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
600 handle_options_response (GstRTSPClient *client, const gchar *uri, GstRTSPMessage *request)
602 GstRTSPMessage response = { 0 };
603 GstRTSPMethod options;
606 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK,
607 gst_rtsp_status_as_text (GST_RTSP_STS_OK), request);
609 options = GST_RTSP_DESCRIBE |
616 /* always return options.. */
617 str = g_string_new ("OPTIONS");
619 if (options & GST_RTSP_DESCRIBE)
620 g_string_append (str, ", DESCRIBE");
621 if (options & GST_RTSP_ANNOUNCE)
622 g_string_append (str, ", ANNOUNCE");
623 if (options & GST_RTSP_GET_PARAMETER)
624 g_string_append (str, ", GET_PARAMETER");
625 if (options & GST_RTSP_PAUSE)
626 g_string_append (str, ", PAUSE");
627 if (options & GST_RTSP_PLAY)
628 g_string_append (str, ", PLAY");
629 if (options & GST_RTSP_RECORD)
630 g_string_append (str, ", RECORD");
631 if (options & GST_RTSP_REDIRECT)
632 g_string_append (str, ", REDIRECT");
633 if (options & GST_RTSP_SETUP)
634 g_string_append (str, ", SETUP");
635 if (options & GST_RTSP_SET_PARAMETER)
636 g_string_append (str, ", SET_PARAMETER");
637 if (options & GST_RTSP_TEARDOWN)
638 g_string_append (str, ", TEARDOWN");
640 gst_rtsp_message_add_header (&response, GST_RTSP_HDR_PUBLIC, str->str);
642 g_string_free (str, TRUE);
644 gst_rtsp_connection_send (client->connection, &response, NULL);
647 /* this function runs in a client specific thread and handles all rtsp messages
650 handle_client (GstRTSPClient *client)
652 GstRTSPMessage request = { 0 };
654 GstRTSPMethod method;
656 GstRTSPVersion version;
659 /* start by waiting for a message from the client */
660 res = gst_rtsp_connection_receive (client->connection, &request, NULL);
665 gst_rtsp_message_dump (&request);
668 gst_rtsp_message_parse_request (&request, &method, &uri, &version);
670 if (version != GST_RTSP_VERSION_1_0) {
671 /* we can only handle 1.0 requests */
672 handle_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED, &request);
676 /* now see what is asked and dispatch to a dedicated handler */
678 case GST_RTSP_OPTIONS:
679 handle_options_response (client, uri, &request);
681 case GST_RTSP_DESCRIBE:
682 handle_describe_response (client, uri, &request);
685 handle_setup_response (client, uri, &request);
688 handle_play_response (client, uri, &request);
691 handle_pause_response (client, uri, &request);
693 case GST_RTSP_TEARDOWN:
694 handle_teardown_response (client, uri, &request);
696 case GST_RTSP_ANNOUNCE:
697 case GST_RTSP_GET_PARAMETER:
698 case GST_RTSP_RECORD:
699 case GST_RTSP_REDIRECT:
700 case GST_RTSP_SET_PARAMETER:
701 handle_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &request);
703 case GST_RTSP_INVALID:
705 handle_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &request);
709 g_object_unref (client);
715 g_print ("receive failed, disconnect client %p\n", client);
716 gst_rtsp_connection_close (client->connection);
717 g_object_unref (client);
722 /* called when we need to accept a new request from a client */
724 client_accept (GstRTSPClient *client, GIOChannel *channel)
726 /* a new client connected. */
728 unsigned int address_len;
729 GstRTSPConnection *conn;
731 conn = client->connection;
733 server_sock_fd = g_io_channel_unix_get_fd (channel);
735 address_len = sizeof (client->address);
736 memset (&client->address, 0, address_len);
738 conn->fd.fd = accept (server_sock_fd, (struct sockaddr *) &client->address,
740 if (conn->fd.fd == -1)
743 g_print ("added new client %p ip %s with fd %d\n", client,
744 inet_ntoa (client->address.sin_addr), conn->fd.fd);
746 /* FIXME some hackery, we need to have a connection method to accept server
748 gst_poll_add_fd (conn->fdset, &conn->fd);
755 g_error ("Could not accept client on server socket %d: %s (%d)",
756 server_sock_fd, g_strerror (errno), errno);
762 * gst_rtsp_client_set_session_pool:
763 * @client: a #GstRTSPClient
764 * @pool: a #GstRTSPSessionPool
766 * Set @pool as the sessionpool for @client which it will use to find
767 * or allocate sessions.
770 gst_rtsp_client_set_session_pool (GstRTSPClient *client, GstRTSPSessionPool *pool)
772 GstRTSPSessionPool *old;
779 g_object_unref (old);
783 * gst_rtsp_client_get_session_pool:
784 * @client: a #GstRTSPClient
786 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
788 * Returns: a #GstRTSPSessionPool, unref after usage.
791 gst_rtsp_client_get_session_pool (GstRTSPClient *client)
793 GstRTSPSessionPool *result;
795 if ((result = client->pool))
796 g_object_ref (result);
803 * gst_rtsp_client_attach:
804 * @client: a #GstRTSPClient
805 * @channel: a #GIOChannel
807 * Accept a new connection for @client on the socket in @source.
809 * This function should be called when the client properties and urls are fully
810 * configured and the client is ready to start.
812 * Returns: %TRUE if the client could be accepted.
815 gst_rtsp_client_accept (GstRTSPClient *client, GIOChannel *channel)
817 gst_rtsp_connection_create (NULL, &client->connection);
819 if (!client_accept (client, channel))
822 /* client accepted, spawn a thread for the client */
823 g_object_ref (client);
824 client->thread = g_thread_create ((GThreadFunc)handle_client, client, TRUE, NULL);
831 gst_rtsp_connection_close (client->connection);