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 /* wait for paused state change to complete */
219 ret = gst_element_get_state (media->pipeline, NULL, NULL, -1);
223 /* grab RTPInfo from the payloaders now */
224 rtpinfo = g_string_new ("");
225 n_streams = gst_rtsp_media_n_streams (client->media);
226 for (i = 0; i < n_streams; i++) {
227 GstRTSPMediaStream *stream;
229 stream = gst_rtsp_media_get_stream (client->media, i);
231 g_object_get (G_OBJECT (stream->payloader), "seqnum", &seqnum, NULL);
232 g_object_get (G_OBJECT (stream->payloader), "timestamp", ×tamp, NULL);
235 g_string_append (rtpinfo, ", ");
236 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u", uri, i, seqnum, timestamp);
239 /* construct the response now */
240 code = GST_RTSP_STS_OK;
241 gst_rtsp_message_init_response (&response, code, gst_rtsp_status_as_text (code), request);
243 /* add the RTP-Info header */
244 gst_rtsp_message_add_header (&response, GST_RTSP_HDR_RTP_INFO, rtpinfo->str);
245 g_string_free (rtpinfo, TRUE);
247 gst_rtsp_connection_send (client->connection, &response, NULL);
249 /* start playing after sending the request */
250 gst_rtsp_session_media_play (media);
251 g_object_unref (session);
258 handle_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
263 handle_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, request);
268 handle_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
274 handle_setup_response (GstRTSPClient *client, const gchar *uri, GstRTSPMessage *request)
280 gboolean have_transport;
281 GstRTSPTransport *ct, *st;
282 GstRTSPSession *session;
284 GstRTSPLowerTrans supported;
285 GstRTSPMessage response = { 0 };
286 GstRTSPStatusCode code;
287 GstRTSPSessionStream *stream;
288 gchar *trans_str, *pos;
290 GstRTSPSessionMedia *media;
291 gboolean need_session;
293 /* find the media associated with the uri */
294 if (client->media == NULL) {
295 if ((client->media = gst_rtsp_media_new (uri)) == NULL)
299 /* parse the transport */
300 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_TRANSPORT, &transport, 0);
301 if (res != GST_RTSP_OK)
302 goto unsupported_transports;
304 transports = g_strsplit (transport, ",", 0);
305 gst_rtsp_transport_new (&ct);
307 /* loop through the transports, try to parse */
308 have_transport = FALSE;
309 for (i = 0; transports[i]; i++) {
311 gst_rtsp_transport_init (ct);
312 res = gst_rtsp_transport_parse (transports[i], ct);
313 if (res == GST_RTSP_OK) {
314 have_transport = TRUE;
318 g_strfreev (transports);
320 /* we have not found anything usable, error out */
321 if (!have_transport) {
322 gst_rtsp_transport_free (ct);
323 goto unsupported_transports;
326 /* we have a valid transport, check if we can handle it */
327 if (ct->trans != GST_RTSP_TRANS_RTP)
328 goto unsupported_transports;
329 if (ct->profile != GST_RTSP_PROFILE_AVP)
330 goto unsupported_transports;
331 supported = GST_RTSP_LOWER_TRANS_UDP |
332 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
333 if (!(ct->lower_transport & supported))
334 goto unsupported_transports;
336 /* a setup request creates a session for a client, check if the client already
337 * sent a session id to us */
338 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
339 if (res == GST_RTSP_OK) {
340 /* we had a session in the request, find it again */
341 if (!(session = gst_rtsp_session_pool_find (client->pool, sessid)))
342 goto session_not_found;
343 need_session = FALSE;
346 /* create a session if this fails we probably reached our session limit or
348 if (!(session = gst_rtsp_session_pool_create (client->pool)))
349 goto service_unavailable;
353 /* get a handle to the configuration of the media in the session */
354 media = gst_rtsp_session_get_media (session, client->media);
358 /* parse the stream we need to configure */
359 if (!(pos = strstr (uri, "stream=")))
362 pos += strlen ("stream=");
363 if (sscanf (pos, "%u", &streamid) != 1)
366 /* get a handle to the stream in the media */
367 stream = gst_rtsp_session_get_stream (media, streamid);
369 /* setup the server transport from the client transport */
370 st = gst_rtsp_session_stream_set_transport (stream, inet_ntoa (client->address.sin_addr), ct);
372 /* serialize the server transport */
373 trans_str = gst_rtsp_transport_as_text (st);
375 /* construct the response now */
376 code = GST_RTSP_STS_OK;
377 gst_rtsp_message_init_response (&response, code, gst_rtsp_status_as_text (code), request);
380 gst_rtsp_message_add_header (&response, GST_RTSP_HDR_SESSION, session->sessionid);
381 gst_rtsp_message_add_header (&response, GST_RTSP_HDR_TRANSPORT, trans_str);
383 g_object_unref (session);
385 gst_rtsp_connection_send (client->connection, &response, NULL);
392 handle_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
397 handle_generic_response (client, GST_RTSP_STS_BAD_REQUEST, request);
402 handle_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
405 unsupported_transports:
407 handle_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, request);
412 handle_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, request);
418 handle_describe_response (GstRTSPClient *client, const gchar *uri, GstRTSPMessage *request)
420 GstRTSPMessage response = { 0 };
425 GstElement *pipeline = NULL;
427 /* check what kind of format is accepted */
430 /* for the describe we must generate an SDP */
431 if (!(media = gst_rtsp_media_new (uri)))
434 /* create a pipeline if we have to */
435 if (pipeline == NULL) {
436 pipeline = gst_pipeline_new ("client-pipeline");
439 /* prepare the media into the pipeline */
440 if (!gst_rtsp_media_prepare (media, GST_BIN (pipeline)))
443 /* link fakesink to all stream pads and set the pipeline to PLAYING */
444 n_streams = gst_rtsp_media_n_streams (media);
445 for (i = 0; i < n_streams; i++) {
446 GstRTSPMediaStream *stream;
450 stream = gst_rtsp_media_get_stream (media, i);
452 sink = gst_element_factory_make ("fakesink", NULL);
453 gst_bin_add (GST_BIN (pipeline), sink);
455 sinkpad = gst_element_get_static_pad (sink, "sink");
456 gst_pad_link (stream->srcpad, sinkpad);
457 gst_object_unref (sinkpad);
460 /* now play and wait till we get the pads blocked. At that time the pipeline
461 * is prerolled and we have the caps on the streams too. */
462 gst_element_set_state (pipeline, GST_STATE_PLAYING);
464 /* wait for state change to complete */
465 gst_element_get_state (pipeline, NULL, NULL, -1);
467 /* we should now be able to construct the SDP message */
468 gst_sdp_message_new (&sdp);
470 /* some standard things first */
471 gst_sdp_message_set_version (sdp, "0");
472 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", "IP4", "127.0.0.1");
473 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
474 gst_sdp_message_set_information (sdp, "rtsp-server");
475 gst_sdp_message_add_time (sdp, "0", "0", NULL);
476 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
477 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
479 for (i = 0; i < n_streams; i++) {
480 GstRTSPMediaStream *stream;
483 const gchar *caps_str, *caps_enc, *caps_params;
485 gint caps_pt, caps_rate;
490 stream = gst_rtsp_media_get_stream (media, i);
491 gst_sdp_media_new (&smedia);
493 s = gst_caps_get_structure (stream->caps, 0);
495 /* get media type and payload for the m= line */
496 caps_str = gst_structure_get_string (s, "media");
497 gst_sdp_media_set_media (smedia, caps_str);
499 gst_structure_get_int (s, "payload", &caps_pt);
500 tmp = g_strdup_printf ("%d", caps_pt);
501 gst_sdp_media_add_format (smedia, tmp);
504 gst_sdp_media_set_port_info (smedia, 0, 1);
505 gst_sdp_media_set_proto (smedia, "RTP/AVP");
507 /* for the c= line */
508 gst_sdp_media_add_connection (smedia, "IN", "IP4", "127.0.0.1", 0, 0);
510 /* get clock-rate, media type and params for the rtpmap attribute */
511 gst_structure_get_int (s, "clock-rate", &caps_rate);
512 caps_enc = gst_structure_get_string (s, "encoding-name");
513 caps_params = gst_structure_get_string (s, "encoding-params");
516 tmp = g_strdup_printf ("%d %s/%d/%s", caps_pt, caps_enc, caps_rate,
519 tmp = g_strdup_printf ("%d %s/%d", caps_pt, caps_enc, caps_rate);
521 gst_sdp_media_add_attribute (smedia, "rtpmap", tmp);
525 tmp = g_strdup_printf ("stream=%d", i);
526 gst_sdp_media_add_attribute (smedia, "control", tmp);
529 /* collect all other properties and add them to fmtp */
530 fmtp = g_string_new ("");
531 g_string_append_printf (fmtp, "%d ", caps_pt);
533 n_fields = gst_structure_n_fields (s);
534 for (j = 0; j < n_fields; j++) {
535 const gchar *fname, *fval;
537 fname = gst_structure_nth_field_name (s, j);
539 /* filter out standard properties */
540 if (!strcmp (fname, "media"))
542 if (!strcmp (fname, "payload"))
544 if (!strcmp (fname, "clock-rate"))
546 if (!strcmp (fname, "encoding-name"))
548 if (!strcmp (fname, "encoding-params"))
550 if (!strcmp (fname, "ssrc"))
552 if (!strcmp (fname, "clock-base"))
554 if (!strcmp (fname, "seqnum-base"))
557 if ((fval = gst_structure_get_string (s, fname))) {
558 g_string_append_printf (fmtp, "%s%s=%s", first ? "":";", fname, fval);
563 tmp = g_string_free (fmtp, FALSE);
564 gst_sdp_media_add_attribute (smedia, "fmtp", tmp);
568 g_string_free (fmtp, TRUE);
570 gst_sdp_message_add_media (sdp, smedia);
572 /* go back to NULL */
573 gst_element_set_state (pipeline, GST_STATE_NULL);
575 g_object_unref (media);
577 gst_object_unref (pipeline);
580 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK,
581 gst_rtsp_status_as_text (GST_RTSP_STS_OK), request);
583 /* add SDP to the response body */
584 sdptext = gst_sdp_message_as_text (sdp);
585 gst_rtsp_message_take_body (&response, (guint8 *)sdptext, strlen (sdptext));
586 gst_sdp_message_free (sdp);
588 gst_rtsp_connection_send (client->connection, &response, NULL);
595 handle_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
601 handle_options_response (GstRTSPClient *client, const gchar *uri, GstRTSPMessage *request)
603 GstRTSPMessage response = { 0 };
604 GstRTSPMethod options;
607 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK,
608 gst_rtsp_status_as_text (GST_RTSP_STS_OK), request);
610 options = GST_RTSP_DESCRIBE |
617 /* always return options.. */
618 str = g_string_new ("OPTIONS");
620 if (options & GST_RTSP_DESCRIBE)
621 g_string_append (str, ", DESCRIBE");
622 if (options & GST_RTSP_ANNOUNCE)
623 g_string_append (str, ", ANNOUNCE");
624 if (options & GST_RTSP_GET_PARAMETER)
625 g_string_append (str, ", GET_PARAMETER");
626 if (options & GST_RTSP_PAUSE)
627 g_string_append (str, ", PAUSE");
628 if (options & GST_RTSP_PLAY)
629 g_string_append (str, ", PLAY");
630 if (options & GST_RTSP_RECORD)
631 g_string_append (str, ", RECORD");
632 if (options & GST_RTSP_REDIRECT)
633 g_string_append (str, ", REDIRECT");
634 if (options & GST_RTSP_SETUP)
635 g_string_append (str, ", SETUP");
636 if (options & GST_RTSP_SET_PARAMETER)
637 g_string_append (str, ", SET_PARAMETER");
638 if (options & GST_RTSP_TEARDOWN)
639 g_string_append (str, ", TEARDOWN");
641 gst_rtsp_message_add_header (&response, GST_RTSP_HDR_PUBLIC, str->str);
643 g_string_free (str, TRUE);
645 gst_rtsp_connection_send (client->connection, &response, NULL);
648 /* this function runs in a client specific thread and handles all rtsp messages
651 handle_client (GstRTSPClient *client)
653 GstRTSPMessage request = { 0 };
655 GstRTSPMethod method;
657 GstRTSPVersion version;
660 /* start by waiting for a message from the client */
661 res = gst_rtsp_connection_receive (client->connection, &request, NULL);
666 gst_rtsp_message_dump (&request);
669 gst_rtsp_message_parse_request (&request, &method, &uri, &version);
671 if (version != GST_RTSP_VERSION_1_0) {
672 /* we can only handle 1.0 requests */
673 handle_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED, &request);
677 /* now see what is asked and dispatch to a dedicated handler */
679 case GST_RTSP_OPTIONS:
680 handle_options_response (client, uri, &request);
682 case GST_RTSP_DESCRIBE:
683 handle_describe_response (client, uri, &request);
686 handle_setup_response (client, uri, &request);
689 handle_play_response (client, uri, &request);
692 handle_pause_response (client, uri, &request);
694 case GST_RTSP_TEARDOWN:
695 handle_teardown_response (client, uri, &request);
697 case GST_RTSP_ANNOUNCE:
698 case GST_RTSP_GET_PARAMETER:
699 case GST_RTSP_RECORD:
700 case GST_RTSP_REDIRECT:
701 case GST_RTSP_SET_PARAMETER:
702 handle_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &request);
704 case GST_RTSP_INVALID:
706 handle_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &request);
710 g_object_unref (client);
716 g_print ("receive failed, disconnect client %p\n", client);
717 gst_rtsp_connection_close (client->connection);
718 g_object_unref (client);
723 /* called when we need to accept a new request from a client */
725 client_accept (GstRTSPClient *client, GIOChannel *channel)
727 /* a new client connected. */
728 int server_sock_fd, fd;
729 unsigned int address_len;
730 GstRTSPConnection *conn;
732 server_sock_fd = g_io_channel_unix_get_fd (channel);
734 address_len = sizeof (client->address);
735 memset (&client->address, 0, address_len);
737 fd = accept (server_sock_fd, (struct sockaddr *) &client->address,
742 /* now create the connection object */
743 gst_rtsp_connection_create (NULL, &conn);
746 /* FIXME some hackery, we need to have a connection method to accept server
748 gst_poll_add_fd (conn->fdset, &conn->fd);
750 g_print ("added new client %p ip %s with fd %d\n", client,
751 inet_ntoa (client->address.sin_addr), conn->fd.fd);
753 client->connection = conn;
760 g_error ("Could not accept client on server socket %d: %s (%d)",
761 server_sock_fd, g_strerror (errno), errno);
767 * gst_rtsp_client_set_session_pool:
768 * @client: a #GstRTSPClient
769 * @pool: a #GstRTSPSessionPool
771 * Set @pool as the sessionpool for @client which it will use to find
772 * or allocate sessions.
775 gst_rtsp_client_set_session_pool (GstRTSPClient *client, GstRTSPSessionPool *pool)
777 GstRTSPSessionPool *old;
784 g_object_unref (old);
788 * gst_rtsp_client_get_session_pool:
789 * @client: a #GstRTSPClient
791 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
793 * Returns: a #GstRTSPSessionPool, unref after usage.
796 gst_rtsp_client_get_session_pool (GstRTSPClient *client)
798 GstRTSPSessionPool *result;
800 if ((result = client->pool))
801 g_object_ref (result);
807 * gst_rtsp_client_set_media_factory:
808 * @client: a #GstRTSPClient
809 * @factory: a #GstRTSPMediaFactory
811 * Set @factory as the media factory for @client which it will use to map urls
815 gst_rtsp_client_set_media_factory (GstRTSPClient *client, GstRTSPMediaFactory *factory)
817 GstRTSPMediaFactory *old;
819 old = client->factory;
821 if (old != factory) {
823 g_object_ref (factory);
824 client->factory = factory;
826 g_object_unref (old);
831 * gst_rtsp_client_get_media_factory:
832 * @client: a #GstRTSPClient
834 * Get the #GstRTSPMediaFactory object that @client uses to manage its sessions.
836 * Returns: a #GstRTSPMediaFactory, unref after usage.
838 GstRTSPMediaFactory *
839 gst_rtsp_client_get_media_factory (GstRTSPClient *client)
841 GstRTSPMediaFactory *result;
843 if ((result = client->factory))
844 g_object_ref (result);
851 * gst_rtsp_client_attach:
852 * @client: a #GstRTSPClient
853 * @channel: a #GIOChannel
855 * Accept a new connection for @client on the socket in @source.
857 * This function should be called when the client properties and urls are fully
858 * configured and the client is ready to start.
860 * Returns: %TRUE if the client could be accepted.
863 gst_rtsp_client_accept (GstRTSPClient *client, GIOChannel *channel)
865 if (!client_accept (client, channel))
868 /* client accepted, spawn a thread for the client */
869 g_object_ref (client);
870 client->thread = g_thread_create ((GThreadFunc)handle_client, client, TRUE, NULL);