2 * Copyright (C) <2005-2009> Wim Taymans <wim.taymans@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 * Unless otherwise indicated, Source Code is licensed under MIT license.
21 * See further explanation attached in License Statement (distributed in the file
24 * Permission is hereby granted, free of charge, to any person obtaining a copy of
25 * this software and associated documentation files (the "Software"), to deal in
26 * the Software without restriction, including without limitation the rights to
27 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
28 * of the Software, and to permit persons to whom the Software is furnished to do
29 * so, subject to the following conditions:
31 * The above copyright notice and this permission notice shall be included in all
32 * copies or substantial portions of the Software.
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
44 * SECTION:gstrtspconnection
45 * @short_description: manage RTSP connections
46 * @see_also: gstrtspurl
50 * This object manages the RTSP connection to the server. It provides function
51 * to receive and send bytes and messages.
55 * Last reviewed on 2007-07-24 (0.10.14)
72 /* we include this here to get the G_OS_* defines */
77 /* ws2_32.dll has getaddrinfo and freeaddrinfo on Windows XP and later.
78 * minwg32 headers check WINVER before allowing the use of these */
84 #define EINPROGRESS WSAEINPROGRESS
86 #include <sys/ioctl.h>
88 #include <sys/socket.h>
90 #include <netinet/in.h>
93 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
94 #include <sys/filio.h>
97 #include "gstrtspconnection.h"
98 #include "gstrtspbase64.h"
103 struct sockaddr_in sa_in;
104 struct sockaddr_in6 sa_in6;
105 struct sockaddr_storage sa_stor;
112 guchar out[3]; /* the size must be evenly divisible by 3 */
118 #define SEND_FLAGS MSG_NOSIGNAL
124 #define READ_SOCKET(fd, buf, len) recv (fd, (char *)buf, len, 0)
125 #define WRITE_SOCKET(fd, buf, len) send (fd, (const char *)buf, len, SEND_FLAGS)
126 #define SETSOCKOPT(sock, level, name, val, len) setsockopt (sock, level, name, (const char *)val, len)
127 #define CLOSE_SOCKET(sock) closesocket (sock)
128 #define ERRNO_IS_EAGAIN (WSAGetLastError () == WSAEWOULDBLOCK)
129 #define ERRNO_IS_EINTR (WSAGetLastError () == WSAEINTR)
130 /* According to Microsoft's connect() documentation this one returns
131 * WSAEWOULDBLOCK and not WSAEINPROGRESS. */
132 #define ERRNO_IS_EINPROGRESS (WSAGetLastError () == WSAEWOULDBLOCK)
134 #define READ_SOCKET(fd, buf, len) read (fd, buf, len)
135 #define WRITE_SOCKET(fd, buf, len) send (fd, buf, len, SEND_FLAGS)
136 #define SETSOCKOPT(sock, level, name, val, len) setsockopt (sock, level, name, val, len)
137 #define CLOSE_SOCKET(sock) close (sock)
138 #define ERRNO_IS_EAGAIN (errno == EAGAIN)
139 #define ERRNO_IS_EINTR (errno == EINTR)
140 #define ERRNO_IS_EINPROGRESS (errno == EINPROGRESS)
143 #define ADD_POLLFD(fdset, pfd, fd) \
146 gst_poll_add_fd (fdset, pfd); \
149 #define REMOVE_POLLFD(fdset, pfd) \
151 if ((pfd)->fd != -1) { \
152 GST_DEBUG ("remove fd %d", (pfd)->fd); \
153 gst_poll_remove_fd (fdset, pfd); \
154 CLOSE_SOCKET ((pfd)->fd); \
164 TUNNEL_STATE_COMPLETE
165 } GstRTSPTunnelState;
167 #define TUNNELID_LEN 24
169 struct _GstRTSPConnection
172 /* URL for the connection */
175 /* connection state */
182 gboolean manual_http;
184 gchar tunnelid[TUNNELID_LEN];
186 GstRTSPTunnelState tstate;
193 gchar *initial_buffer;
194 gsize initial_buffer_offset;
197 gint cseq; /* sequence number */
198 gchar session_id[512]; /* session id */
199 gint timeout; /* session timeout in seconds */
200 GTimer *timer; /* timeout timer */
203 GstRTSPAuthMethod auth_method;
206 GHashTable *auth_params;
227 READ_AHEAD_EOH = -1, /* end of headers */
228 READ_AHEAD_CRLF = -2,
229 READ_AHEAD_CRLFCR = -3
232 /* a structure for constructing RTSPMessages */
236 GstRTSPResult status;
246 build_reset (GstRTSPBuilder * builder)
248 g_free (builder->body_data);
249 memset (builder, 0, sizeof (GstRTSPBuilder));
253 * gst_rtsp_connection_create:
254 * @url: a #GstRTSPUrl
255 * @conn: storage for a #GstRTSPConnection
257 * Create a newly allocated #GstRTSPConnection from @url and store it in @conn.
258 * The connection will not yet attempt to connect to @url, use
259 * gst_rtsp_connection_connect().
261 * A copy of @url will be made.
263 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
266 gst_rtsp_connection_create (const GstRTSPUrl * url, GstRTSPConnection ** conn)
268 GstRTSPConnection *newconn;
274 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
277 error = WSAStartup (0x0202, &w);
282 if (w.wVersion != 0x0202)
286 newconn = g_new0 (GstRTSPConnection, 1);
288 if ((newconn->fdset = gst_poll_new (TRUE)) == NULL)
291 newconn->url = gst_rtsp_url_copy (url);
292 newconn->fd0.fd = -1;
293 newconn->fd1.fd = -1;
294 newconn->timer = g_timer_new ();
295 newconn->timeout = 60;
298 newconn->auth_method = GST_RTSP_AUTH_NONE;
299 newconn->username = NULL;
300 newconn->passwd = NULL;
301 newconn->auth_params = NULL;
311 g_warning ("Error %d on WSAStartup", error);
312 return GST_RTSP_EWSASTART;
316 g_warning ("Windows sockets are not version 0x202 (current 0x%x)",
319 return GST_RTSP_EWSAVERSION;
328 return GST_RTSP_ESYS;
333 * gst_rtsp_connection_create_from_fd:
334 * @fd: a file descriptor
335 * @ip: the IP address of the other end
336 * @port: the port used by the other end
337 * @initial_buffer: data already read from @fd
338 * @conn: storage for a #GstRTSPConnection
340 * Create a new #GstRTSPConnection for handling communication on the existing
341 * file descriptor @fd. The @initial_buffer contains any data already read from
342 * @fd which should be used before starting to read new data.
344 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
349 gst_rtsp_connection_create_from_fd (gint fd, const gchar * ip, guint16 port,
350 const gchar * initial_buffer, GstRTSPConnection ** conn)
352 GstRTSPConnection *newconn = NULL;
359 g_return_val_if_fail (fd >= 0, GST_RTSP_EINVAL);
360 g_return_val_if_fail (ip != NULL, GST_RTSP_EINVAL);
361 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
363 /* set to non-blocking mode so that we can cancel the communication */
365 fcntl (fd, F_SETFL, O_NONBLOCK);
367 ioctlsocket (fd, FIONBIO, &flags);
368 #endif /* G_OS_WIN32 */
370 /* create a url for the client address */
371 url = g_new0 (GstRTSPUrl, 1);
372 url->host = g_strdup (ip);
375 /* now create the connection object */
376 GST_RTSP_CHECK (gst_rtsp_connection_create (url, &newconn), newconn_failed);
377 gst_rtsp_url_free (url);
379 ADD_POLLFD (newconn->fdset, &newconn->fd0, fd);
381 /* both read and write initially */
382 newconn->readfd = &newconn->fd0;
383 newconn->writefd = &newconn->fd0;
385 newconn->ip = g_strdup (ip);
387 newconn->initial_buffer = g_strdup (initial_buffer);
396 gst_rtsp_url_free (url);
402 * gst_rtsp_connection_accept:
404 * @conn: storage for a #GstRTSPConnection
406 * Accept a new connection on @sock and create a new #GstRTSPConnection for
407 * handling communication on new socket.
409 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
414 gst_rtsp_connection_accept (gint sock, GstRTSPConnection ** conn)
417 union gst_sockaddr sa;
418 socklen_t slen = sizeof (sa);
419 gchar ip[INET6_ADDRSTRLEN];
422 g_return_val_if_fail (sock >= 0, GST_RTSP_EINVAL);
423 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
425 memset (&sa, 0, slen);
428 fd = accept (sock, &sa.sa, &slen);
430 fd = accept (sock, &sa.sa, (gint *) & slen);
431 #endif /* G_OS_WIN32 */
435 if (getnameinfo (&sa.sa, slen, ip, sizeof (ip), NULL, 0, NI_NUMERICHOST) != 0)
436 goto getnameinfo_failed;
438 if (sa.sa.sa_family == AF_INET)
439 port = sa.sa_in.sin_port;
440 else if (sa.sa.sa_family == AF_INET6)
441 port = sa.sa_in6.sin6_port;
445 return gst_rtsp_connection_create_from_fd (fd, ip, port, NULL, conn);
450 return GST_RTSP_ESYS;
456 return GST_RTSP_ERROR;
461 do_resolve (const gchar * host)
463 static gchar ip[INET6_ADDRSTRLEN];
464 struct addrinfo *aires, hints;
468 memset (&hints, 0, sizeof (struct addrinfo));
469 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
470 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
471 hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
472 hints.ai_protocol = 0; /* Any protocol */
473 hints.ai_canonname = NULL;
474 hints.ai_addr = NULL;
475 hints.ai_next = NULL;
477 aierr = getaddrinfo (host, NULL, &hints, &aires);
481 for (ai = aires; ai; ai = ai->ai_next) {
482 if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) {
489 aierr = getnameinfo (ai->ai_addr, ai->ai_addrlen, ip, sizeof (ip), NULL, 0,
490 NI_NUMERICHOST | NI_NUMERICSERV);
494 freeaddrinfo (aires);
496 return g_strdup (ip);
501 GST_ERROR ("no addrinfo found for %s: %s", host, gai_strerror (aierr));
506 GST_ERROR ("no family found for %s", host);
507 freeaddrinfo (aires);
512 GST_ERROR ("no address found for %s: %s", host, gai_strerror (aierr));
513 freeaddrinfo (aires);
519 do_connect (const gchar * ip, guint16 port, GstPollFD * fdout,
520 GstPoll * fdset, GTimeVal * timeout)
523 struct addrinfo hints;
524 struct addrinfo *aires;
527 gchar service[NI_MAXSERV];
530 unsigned long flags = 1;
531 #endif /* G_OS_WIN32 */
535 memset (&hints, 0, sizeof hints);
536 hints.ai_flags = AI_NUMERICHOST;
537 hints.ai_family = AF_UNSPEC;
538 hints.ai_socktype = SOCK_STREAM;
539 g_snprintf (service, sizeof (service) - 1, "%hu", port);
540 service[sizeof (service) - 1] = '\0';
542 aierr = getaddrinfo (ip, service, &hints, &aires);
546 for (ai = aires; ai; ai = ai->ai_next) {
547 if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) {
554 fd = socket (ai->ai_family, SOCK_STREAM, 0);
558 /* set to non-blocking mode so that we can cancel the connect */
560 fcntl (fd, F_SETFL, O_NONBLOCK);
562 ioctlsocket (fd, FIONBIO, &flags);
563 #endif /* G_OS_WIN32 */
565 /* add the socket to our fdset */
566 ADD_POLLFD (fdset, fdout, fd);
568 /* we are going to connect ASYNC now */
569 ret = connect (fd, ai->ai_addr, ai->ai_addrlen);
572 if (!ERRNO_IS_EINPROGRESS)
575 /* wait for connect to complete up to the specified timeout or until we got
577 gst_poll_fd_ctl_write (fdset, fdout, TRUE);
579 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
582 retval = gst_poll_wait (fdset, to);
583 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
587 else if (retval == -1)
590 /* we can still have an error connecting on windows */
591 if (gst_poll_fd_has_error (fdset, fdout)) {
592 socklen_t len = sizeof (errno);
594 getsockopt (fd, SOL_SOCKET, SO_ERROR, &errno, &len);
596 getsockopt (fd, SOL_SOCKET, SO_ERROR, (char *) &errno, &len);
601 gst_poll_fd_ignored (fdset, fdout);
604 freeaddrinfo (aires);
611 GST_ERROR ("no addrinfo found for %s: %s", ip, gai_strerror (aierr));
612 return GST_RTSP_ERROR;
616 GST_ERROR ("no family found for %s", ip);
617 freeaddrinfo (aires);
618 return GST_RTSP_ERROR;
622 GST_ERROR ("no socket %d (%s)", errno, g_strerror (errno));
623 freeaddrinfo (aires);
624 return GST_RTSP_ESYS;
628 GST_ERROR ("system error %d (%s)", errno, g_strerror (errno));
629 REMOVE_POLLFD (fdset, fdout);
630 freeaddrinfo (aires);
631 return GST_RTSP_ESYS;
635 GST_ERROR ("timeout");
636 REMOVE_POLLFD (fdset, fdout);
637 freeaddrinfo (aires);
638 return GST_RTSP_ETIMEOUT;
643 setup_tunneling (GstRTSPConnection * conn, GTimeVal * timeout)
650 guint16 port, url_port;
654 GstRTSPMessage response;
657 memset (&response, 0, sizeof (response));
658 gst_rtsp_message_init (&response);
660 /* create a random sessionid */
661 for (i = 0; i < TUNNELID_LEN; i++)
662 conn->tunnelid[i] = g_random_int_range ('a', 'z');
663 conn->tunnelid[TUNNELID_LEN - 1] = '\0';
666 /* get the port from the url */
667 gst_rtsp_url_get_port (url, &url_port);
669 if (conn->proxy_host) {
670 uri = g_strdup_printf ("http://%s:%d%s%s%s", url->host, url_port,
671 url->abspath, url->query ? "?" : "", url->query ? url->query : "");
672 hostparam = g_strdup_printf ("%s:%d", url->host, url_port);
673 ip = conn->proxy_host;
674 port = conn->proxy_port;
676 uri = g_strdup_printf ("%s%s%s", url->abspath, url->query ? "?" : "",
677 url->query ? url->query : "");
683 /* create the GET request for the read connection */
684 GST_RTSP_CHECK (gst_rtsp_message_new_request (&msg, GST_RTSP_GET, uri),
686 msg->type = GST_RTSP_MESSAGE_HTTP_REQUEST;
688 if (hostparam != NULL)
689 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_HOST, hostparam);
690 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_X_SESSIONCOOKIE,
692 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_ACCEPT,
693 "application/x-rtsp-tunnelled");
694 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CACHE_CONTROL, "no-cache");
695 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_PRAGMA, "no-cache");
697 /* we start by writing to this fd */
698 conn->writefd = &conn->fd0;
700 /* we need to temporarily set conn->tunneled to FALSE to prevent the HTTP
701 * request from being base64 encoded */
702 conn->tunneled = FALSE;
703 GST_RTSP_CHECK (gst_rtsp_connection_send (conn, msg, timeout), write_failed);
704 gst_rtsp_message_free (msg);
705 conn->tunneled = TRUE;
707 /* receive the response to the GET request */
708 /* we need to temporarily set manual_http to TRUE since
709 * gst_rtsp_connection_receive() will treat the HTTP response as a parsing
710 * failure otherwise */
711 old_http = conn->manual_http;
712 conn->manual_http = TRUE;
713 GST_RTSP_CHECK (gst_rtsp_connection_receive (conn, &response, timeout),
715 conn->manual_http = old_http;
717 if (response.type != GST_RTSP_MESSAGE_HTTP_RESPONSE ||
718 response.type_data.response.code != GST_RTSP_STS_OK)
721 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_X_SERVER_IP_ADDRESS,
722 &value, 0) == GST_RTSP_OK) {
723 if (conn->proxy_host) {
724 /* if we use a proxy we need to change the destination url */
726 url->host = g_strdup (value);
728 hostparam = g_strdup_printf ("%s:%d", url->host, url_port);
730 /* and resolve the new ip address */
731 if (!(ip = do_resolve (value)))
738 /* connect to the host/port */
739 res = do_connect (ip, port, &conn->fd1, conn->fdset, timeout);
740 if (res != GST_RTSP_OK)
743 /* this is now our writing socket */
744 conn->writefd = &conn->fd1;
746 /* create the POST request for the write connection */
747 GST_RTSP_CHECK (gst_rtsp_message_new_request (&msg, GST_RTSP_POST, uri),
749 msg->type = GST_RTSP_MESSAGE_HTTP_REQUEST;
751 if (hostparam != NULL)
752 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_HOST, hostparam);
753 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_X_SESSIONCOOKIE,
755 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_ACCEPT,
756 "application/x-rtsp-tunnelled");
757 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CACHE_CONTROL, "no-cache");
758 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_PRAGMA, "no-cache");
759 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_EXPIRES,
760 "Sun, 9 Jan 1972 00:00:00 GMT");
761 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CONTENT_LENGTH, "32767");
763 /* we need to temporarily set conn->tunneled to FALSE to prevent the HTTP
764 * request from being base64 encoded */
765 conn->tunneled = FALSE;
766 GST_RTSP_CHECK (gst_rtsp_connection_send (conn, msg, timeout), write_failed);
767 gst_rtsp_message_free (msg);
768 conn->tunneled = TRUE;
771 gst_rtsp_message_unset (&response);
780 GST_ERROR ("failed to create request (%d)", res);
785 GST_ERROR ("write failed (%d)", res);
786 gst_rtsp_message_free (msg);
787 conn->tunneled = TRUE;
792 GST_ERROR ("read failed (%d)", res);
793 conn->manual_http = FALSE;
798 GST_ERROR ("got failure response %d %s", response.type_data.response.code,
799 response.type_data.response.reason);
800 res = GST_RTSP_ERROR;
805 GST_ERROR ("could not resolve %s", conn->ip);
811 GST_ERROR ("failed to connect");
817 * gst_rtsp_connection_connect:
818 * @conn: a #GstRTSPConnection
819 * @timeout: a #GTimeVal timeout
821 * Attempt to connect to the url of @conn made with
822 * gst_rtsp_connection_create(). If @timeout is #NULL this function can block
823 * forever. If @timeout contains a valid timeout, this function will return
824 * #GST_RTSP_ETIMEOUT after the timeout expired.
826 * This function can be cancelled with gst_rtsp_connection_flush().
828 * Returns: #GST_RTSP_OK when a connection could be made.
831 gst_rtsp_connection_connect (GstRTSPConnection * conn, GTimeVal * timeout)
838 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
839 g_return_val_if_fail (conn->url != NULL, GST_RTSP_EINVAL);
840 g_return_val_if_fail (conn->fd0.fd < 0, GST_RTSP_EINVAL);
844 if (conn->proxy_host && conn->tunneled) {
845 if (!(ip = do_resolve (conn->proxy_host))) {
846 GST_ERROR ("could not resolve %s", conn->proxy_host);
849 port = conn->proxy_port;
850 g_free (conn->proxy_host);
851 conn->proxy_host = ip;
853 if (!(ip = do_resolve (url->host))) {
854 GST_ERROR ("could not resolve %s", url->host);
857 /* get the port from the url */
858 gst_rtsp_url_get_port (url, &port);
864 /* connect to the host/port */
865 res = do_connect (ip, port, &conn->fd0, conn->fdset, timeout);
866 if (res != GST_RTSP_OK)
869 /* this is our read URL */
870 conn->readfd = &conn->fd0;
872 if (conn->tunneled) {
873 res = setup_tunneling (conn, timeout);
874 if (res != GST_RTSP_OK)
875 goto tunneling_failed;
877 conn->writefd = &conn->fd0;
884 return GST_RTSP_ENET;
888 GST_ERROR ("failed to connect");
893 GST_ERROR ("failed to setup tunneling");
899 auth_digest_compute_hex_urp (const gchar * username,
900 const gchar * realm, const gchar * password, gchar hex_urp[33])
902 GChecksum *md5_context = g_checksum_new (G_CHECKSUM_MD5);
903 const gchar *digest_string;
905 g_checksum_update (md5_context, (const guchar *) username, strlen (username));
906 g_checksum_update (md5_context, (const guchar *) ":", 1);
907 g_checksum_update (md5_context, (const guchar *) realm, strlen (realm));
908 g_checksum_update (md5_context, (const guchar *) ":", 1);
909 g_checksum_update (md5_context, (const guchar *) password, strlen (password));
910 digest_string = g_checksum_get_string (md5_context);
912 memset (hex_urp, 0, 33);
913 memcpy (hex_urp, digest_string, strlen (digest_string));
915 g_checksum_free (md5_context);
919 auth_digest_compute_response (const gchar * method,
920 const gchar * uri, const gchar * hex_a1, const gchar * nonce,
923 char hex_a2[33] = { 0, };
924 GChecksum *md5_context = g_checksum_new (G_CHECKSUM_MD5);
925 const gchar *digest_string;
928 g_checksum_update (md5_context, (const guchar *) method, strlen (method));
929 g_checksum_update (md5_context, (const guchar *) ":", 1);
930 g_checksum_update (md5_context, (const guchar *) uri, strlen (uri));
931 digest_string = g_checksum_get_string (md5_context);
932 memcpy (hex_a2, digest_string, strlen (digest_string));
935 g_checksum_reset (md5_context);
936 g_checksum_update (md5_context, (const guchar *) hex_a1, strlen (hex_a1));
937 g_checksum_update (md5_context, (const guchar *) ":", 1);
938 g_checksum_update (md5_context, (const guchar *) nonce, strlen (nonce));
939 g_checksum_update (md5_context, (const guchar *) ":", 1);
941 g_checksum_update (md5_context, (const guchar *) hex_a2, 32);
942 digest_string = g_checksum_get_string (md5_context);
943 memset (response, 0, 33);
944 memcpy (response, digest_string, strlen (digest_string));
946 g_checksum_free (md5_context);
950 add_auth_header (GstRTSPConnection * conn, GstRTSPMessage * message)
952 switch (conn->auth_method) {
953 case GST_RTSP_AUTH_BASIC:{
958 if (conn->username == NULL || conn->passwd == NULL)
961 user_pass = g_strdup_printf ("%s:%s", conn->username, conn->passwd);
962 user_pass64 = g_base64_encode ((guchar *) user_pass, strlen (user_pass));
963 auth_string = g_strdup_printf ("Basic %s", user_pass64);
965 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
969 g_free (user_pass64);
972 case GST_RTSP_AUTH_DIGEST:{
973 gchar response[33], hex_urp[33];
974 gchar *auth_string, *auth_string2;
981 /* we need to have some params set */
982 if (conn->auth_params == NULL || conn->username == NULL ||
983 conn->passwd == NULL)
986 /* we need the realm and nonce */
987 realm = (gchar *) g_hash_table_lookup (conn->auth_params, "realm");
988 nonce = (gchar *) g_hash_table_lookup (conn->auth_params, "nonce");
989 if (realm == NULL || nonce == NULL)
992 auth_digest_compute_hex_urp (conn->username, realm, conn->passwd,
995 method = gst_rtsp_method_as_text (message->type_data.request.method);
996 uri = message->type_data.request.uri;
998 /* Assume no qop, algorithm=md5, stale=false */
999 /* For algorithm MD5, a1 = urp. */
1000 auth_digest_compute_response (method, uri, hex_urp, nonce, response);
1001 auth_string = g_strdup_printf ("Digest username=\"%s\", "
1002 "realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"",
1003 conn->username, realm, nonce, uri, response);
1005 opaque = (gchar *) g_hash_table_lookup (conn->auth_params, "opaque");
1007 auth_string2 = g_strdup_printf ("%s, opaque=\"%s\"", auth_string,
1009 g_free (auth_string);
1010 auth_string = auth_string2;
1012 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
1023 gen_date_string (gchar * date_string, guint len)
1025 static const char wkdays[7][4] =
1026 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
1027 static const char months[12][4] =
1028 { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
1036 #ifdef HAVE_GMTIME_R
1042 g_snprintf (date_string, len, "%s, %02d %s %04d %02d:%02d:%02d GMT",
1043 wkdays[tm.tm_wday], tm.tm_mday, months[tm.tm_mon], tm.tm_year + 1900,
1044 tm.tm_hour, tm.tm_min, tm.tm_sec);
1047 static GstRTSPResult
1048 write_bytes (gint fd, const guint8 * buffer, guint * idx, guint size)
1052 if (G_UNLIKELY (*idx > size))
1053 return GST_RTSP_ERROR;
1060 r = WRITE_SOCKET (fd, &buffer[*idx], left);
1061 if (G_UNLIKELY (r == 0)) {
1062 return GST_RTSP_EINTR;
1063 } else if (G_UNLIKELY (r < 0)) {
1064 if (ERRNO_IS_EAGAIN)
1065 return GST_RTSP_EINTR;
1066 if (!ERRNO_IS_EINTR)
1067 return GST_RTSP_ESYS;
1077 fill_raw_bytes (GstRTSPConnection * conn, guint8 * buffer, guint size)
1081 if (G_UNLIKELY (conn->initial_buffer != NULL)) {
1082 gsize left = strlen (&conn->initial_buffer[conn->initial_buffer_offset]);
1084 out = MIN (left, size);
1085 memcpy (buffer, &conn->initial_buffer[conn->initial_buffer_offset], out);
1087 if (left == (gsize) out) {
1088 g_free (conn->initial_buffer);
1089 conn->initial_buffer = NULL;
1090 conn->initial_buffer_offset = 0;
1092 conn->initial_buffer_offset += out;
1095 if (G_LIKELY (size > (guint) out)) {
1098 r = READ_SOCKET (conn->readfd->fd, &buffer[out], size - out);
1110 fill_bytes (GstRTSPConnection * conn, guint8 * buffer, guint size)
1112 DecodeCtx *ctx = conn->ctxp;
1117 guint8 in[sizeof (ctx->out) * 4 / 3];
1120 while (size > 0 && ctx->cout < ctx->coutl) {
1121 /* we have some leftover bytes */
1122 *buffer++ = ctx->out[ctx->cout++];
1127 /* got what we needed? */
1131 /* try to read more bytes */
1132 r = fill_raw_bytes (conn, in, sizeof (in));
1141 g_base64_decode_step ((gchar *) in, r, ctx->out, &ctx->state,
1145 out = fill_raw_bytes (conn, buffer, size);
1151 static GstRTSPResult
1152 read_bytes (GstRTSPConnection * conn, guint8 * buffer, guint * idx, guint size)
1156 if (G_UNLIKELY (*idx > size))
1157 return GST_RTSP_ERROR;
1164 r = fill_bytes (conn, &buffer[*idx], left);
1165 if (G_UNLIKELY (r == 0)) {
1166 return GST_RTSP_EEOF;
1167 } else if (G_UNLIKELY (r < 0)) {
1168 if (ERRNO_IS_EAGAIN)
1169 return GST_RTSP_EINTR;
1170 if (!ERRNO_IS_EINTR)
1171 return GST_RTSP_ESYS;
1180 /* The code below tries to handle clients using \r, \n or \r\n to indicate the
1181 * end of a line. It even does its best to handle clients which mix them (even
1182 * though this is a really stupid idea (tm).) It also handles Line White Space
1183 * (LWS), where a line end followed by whitespace is considered LWS. This is
1184 * the method used in RTSP (and HTTP) to break long lines.
1186 static GstRTSPResult
1187 read_line (GstRTSPConnection * conn, guint8 * buffer, guint * idx, guint size)
1193 if (conn->read_ahead == READ_AHEAD_EOH) {
1194 /* the last call to read_line() already determined that we have reached
1195 * the end of the headers, so convey that information now */
1196 conn->read_ahead = 0;
1198 } else if (conn->read_ahead == READ_AHEAD_CRLF) {
1199 /* the last call to read_line() left off after having read \r\n */
1201 } else if (conn->read_ahead == READ_AHEAD_CRLFCR) {
1202 /* the last call to read_line() left off after having read \r\n\r */
1204 } else if (conn->read_ahead != 0) {
1205 /* the last call to read_line() left us with a character to start with */
1206 c = (guint8) conn->read_ahead;
1207 conn->read_ahead = 0;
1209 /* read the next character */
1210 r = fill_bytes (conn, &c, 1);
1211 if (G_UNLIKELY (r == 0)) {
1212 return GST_RTSP_EEOF;
1213 } else if (G_UNLIKELY (r < 0)) {
1214 if (ERRNO_IS_EAGAIN)
1215 return GST_RTSP_EINTR;
1216 if (!ERRNO_IS_EINTR)
1217 return GST_RTSP_ESYS;
1222 /* special treatment of line endings */
1223 if (c == '\r' || c == '\n') {
1227 /* need to read ahead one more character to know what to do... */
1228 r = fill_bytes (conn, &read_ahead, 1);
1229 if (G_UNLIKELY (r == 0)) {
1230 return GST_RTSP_EEOF;
1231 } else if (G_UNLIKELY (r < 0)) {
1232 if (ERRNO_IS_EAGAIN) {
1233 /* remember the original character we read and try again next time */
1234 if (conn->read_ahead == 0)
1235 conn->read_ahead = c;
1236 return GST_RTSP_EINTR;
1238 if (!ERRNO_IS_EINTR)
1239 return GST_RTSP_ESYS;
1243 if (read_ahead == ' ' || read_ahead == '\t') {
1244 if (conn->read_ahead == READ_AHEAD_CRLFCR) {
1245 /* got \r\n\r followed by whitespace, treat it as a normal line
1246 * followed by one starting with LWS */
1247 conn->read_ahead = read_ahead;
1250 /* got LWS, change the line ending to a space and continue */
1252 conn->read_ahead = read_ahead;
1254 } else if (conn->read_ahead == READ_AHEAD_CRLFCR) {
1255 if (read_ahead == '\r' || read_ahead == '\n') {
1256 /* got \r\n\r\r or \r\n\r\n, treat it as the end of the headers */
1257 conn->read_ahead = READ_AHEAD_EOH;
1260 /* got \r\n\r followed by something else, this is not really
1261 * supported since we have probably just eaten the first character
1262 * of the body or the next message, so just ignore the second \r
1263 * and live with it... */
1264 conn->read_ahead = read_ahead;
1267 } else if (conn->read_ahead == READ_AHEAD_CRLF) {
1268 if (read_ahead == '\r') {
1269 /* got \r\n\r so far, need one more character... */
1270 conn->read_ahead = READ_AHEAD_CRLFCR;
1272 } else if (read_ahead == '\n') {
1273 /* got \r\n\n, treat it as the end of the headers */
1274 conn->read_ahead = READ_AHEAD_EOH;
1277 /* found the end of a line, keep read_ahead for the next line */
1278 conn->read_ahead = read_ahead;
1281 } else if (c == read_ahead) {
1282 /* got double \r or \n, treat it as the end of the headers */
1283 conn->read_ahead = READ_AHEAD_EOH;
1285 } else if (c == '\r' && read_ahead == '\n') {
1286 /* got \r\n so far, still need more to know what to do... */
1287 conn->read_ahead = READ_AHEAD_CRLF;
1290 /* found the end of a line, keep read_ahead for the next line */
1291 conn->read_ahead = read_ahead;
1296 if (G_LIKELY (*idx < size - 1))
1297 buffer[(*idx)++] = c;
1299 buffer[*idx] = '\0';
1305 * gst_rtsp_connection_write:
1306 * @conn: a #GstRTSPConnection
1307 * @data: the data to write
1308 * @size: the size of @data
1309 * @timeout: a timeout value or #NULL
1311 * Attempt to write @size bytes of @data to the connected @conn, blocking up to
1312 * the specified @timeout. @timeout can be #NULL, in which case this function
1313 * might block forever.
1315 * This function can be cancelled with gst_rtsp_connection_flush().
1317 * Returns: #GST_RTSP_OK on success.
1320 gst_rtsp_connection_write (GstRTSPConnection * conn, const guint8 * data,
1321 guint size, GTimeVal * timeout)
1328 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1329 g_return_val_if_fail (data != NULL || size == 0, GST_RTSP_EINVAL);
1330 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
1332 gst_poll_set_controllable (conn->fdset, TRUE);
1333 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, TRUE);
1334 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, FALSE);
1335 /* clear all previous poll results */
1336 gst_poll_fd_ignored (conn->fdset, conn->writefd);
1337 gst_poll_fd_ignored (conn->fdset, conn->readfd);
1339 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1345 res = write_bytes (conn->writefd->fd, data, &offset, size);
1346 if (G_LIKELY (res == GST_RTSP_OK))
1348 if (G_UNLIKELY (res != GST_RTSP_EINTR))
1351 /* not all is written, wait until we can write more */
1353 retval = gst_poll_wait (conn->fdset, to);
1354 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1356 if (G_UNLIKELY (retval == 0))
1359 if (G_UNLIKELY (retval == -1)) {
1366 /* could also be an error with read socket */
1367 if (gst_poll_fd_has_error (conn->fdset, conn->readfd))
1375 return GST_RTSP_ETIMEOUT;
1379 return GST_RTSP_ESYS;
1383 return GST_RTSP_EINTR;
1387 return GST_RTSP_ENET;
1396 message_to_string (GstRTSPConnection * conn, GstRTSPMessage * message)
1398 GString *str = NULL;
1400 str = g_string_new ("");
1402 switch (message->type) {
1403 case GST_RTSP_MESSAGE_REQUEST:
1404 /* create request string, add CSeq */
1405 g_string_append_printf (str, "%s %s RTSP/1.0\r\n"
1407 gst_rtsp_method_as_text (message->type_data.request.method),
1408 message->type_data.request.uri, conn->cseq++);
1409 /* add session id if we have one */
1410 if (conn->session_id[0] != '\0') {
1411 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
1412 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SESSION,
1415 /* add any authentication headers */
1416 add_auth_header (conn, message);
1418 case GST_RTSP_MESSAGE_RESPONSE:
1419 /* create response string */
1420 g_string_append_printf (str, "RTSP/1.0 %d %s\r\n",
1421 message->type_data.response.code, message->type_data.response.reason);
1423 case GST_RTSP_MESSAGE_HTTP_REQUEST:
1424 /* create request string */
1425 g_string_append_printf (str, "%s %s HTTP/%s\r\n",
1426 gst_rtsp_method_as_text (message->type_data.request.method),
1427 message->type_data.request.uri,
1428 gst_rtsp_version_as_text (message->type_data.request.version));
1429 /* add any authentication headers */
1430 add_auth_header (conn, message);
1432 case GST_RTSP_MESSAGE_HTTP_RESPONSE:
1433 /* create response string */
1434 g_string_append_printf (str, "HTTP/%s %d %s\r\n",
1435 gst_rtsp_version_as_text (message->type_data.request.version),
1436 message->type_data.response.code, message->type_data.response.reason);
1438 case GST_RTSP_MESSAGE_DATA:
1440 guint8 data_header[4];
1442 /* prepare data header */
1443 data_header[0] = '$';
1444 data_header[1] = message->type_data.data.channel;
1445 data_header[2] = (message->body_size >> 8) & 0xff;
1446 data_header[3] = message->body_size & 0xff;
1448 /* create string with header and data */
1449 str = g_string_append_len (str, (gchar *) data_header, 4);
1451 g_string_append_len (str, (gchar *) message->body,
1452 message->body_size);
1456 g_string_free (str, TRUE);
1457 g_return_val_if_reached (NULL);
1461 /* append headers and body */
1462 if (message->type != GST_RTSP_MESSAGE_DATA) {
1463 gchar date_string[100];
1465 gen_date_string (date_string, sizeof (date_string));
1467 /* add date header */
1468 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_DATE, -1);
1469 gst_rtsp_message_add_header (message, GST_RTSP_HDR_DATE, date_string);
1471 /* append headers */
1472 gst_rtsp_message_append_headers (message, str);
1474 /* append Content-Length and body if needed */
1475 if (message->body != NULL && message->body_size > 0) {
1478 len = g_strdup_printf ("%d", message->body_size);
1479 g_string_append_printf (str, "%s: %s\r\n",
1480 gst_rtsp_header_as_text (GST_RTSP_HDR_CONTENT_LENGTH), len);
1482 /* header ends here */
1483 g_string_append (str, "\r\n");
1485 g_string_append_len (str, (gchar *) message->body,
1486 message->body_size);
1488 /* just end headers */
1489 g_string_append (str, "\r\n");
1497 * gst_rtsp_connection_send:
1498 * @conn: a #GstRTSPConnection
1499 * @message: the message to send
1500 * @timeout: a timeout value or #NULL
1502 * Attempt to send @message to the connected @conn, blocking up to
1503 * the specified @timeout. @timeout can be #NULL, in which case this function
1504 * might block forever.
1506 * This function can be cancelled with gst_rtsp_connection_flush().
1508 * Returns: #GST_RTSP_OK on success.
1511 gst_rtsp_connection_send (GstRTSPConnection * conn, GstRTSPMessage * message,
1514 GString *string = NULL;
1519 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1520 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1522 if (G_UNLIKELY (!(string = message_to_string (conn, message))))
1525 if (conn->tunneled) {
1526 str = g_base64_encode ((const guchar *) string->str, string->len);
1527 g_string_free (string, TRUE);
1532 g_string_free (string, FALSE);
1536 res = gst_rtsp_connection_write (conn, (guint8 *) str, len, timeout);
1544 g_warning ("Wrong message");
1545 return GST_RTSP_EINVAL;
1549 static GstRTSPResult
1550 parse_string (gchar * dest, gint size, gchar ** src)
1552 GstRTSPResult res = GST_RTSP_OK;
1557 while (g_ascii_isspace (**src))
1560 while (!g_ascii_isspace (**src) && **src != '\0') {
1562 dest[idx++] = **src;
1564 res = GST_RTSP_EPARSE;
1573 static GstRTSPResult
1574 parse_protocol_version (gchar * protocol, GstRTSPMsgType * type,
1575 GstRTSPVersion * version)
1577 GstRTSPResult res = GST_RTSP_OK;
1580 if (G_LIKELY ((ver = strchr (protocol, '/')) != NULL)) {
1587 /* the version number must be formatted as X.Y with nothing following */
1588 if (sscanf (ver, "%u.%u%c", &major, &minor, &dummychar) != 2)
1589 res = GST_RTSP_EPARSE;
1591 if (g_ascii_strcasecmp (protocol, "RTSP") == 0) {
1592 if (major != 1 || minor != 0) {
1593 *version = GST_RTSP_VERSION_INVALID;
1594 res = GST_RTSP_ERROR;
1596 } else if (g_ascii_strcasecmp (protocol, "HTTP") == 0) {
1597 if (*type == GST_RTSP_MESSAGE_REQUEST)
1598 *type = GST_RTSP_MESSAGE_HTTP_REQUEST;
1599 else if (*type == GST_RTSP_MESSAGE_RESPONSE)
1600 *type = GST_RTSP_MESSAGE_HTTP_RESPONSE;
1602 if (major == 1 && minor == 1) {
1603 *version = GST_RTSP_VERSION_1_1;
1604 } else if (major != 1 || minor != 0) {
1605 *version = GST_RTSP_VERSION_INVALID;
1606 res = GST_RTSP_ERROR;
1609 res = GST_RTSP_EPARSE;
1611 res = GST_RTSP_EPARSE;
1616 static GstRTSPResult
1617 parse_response_status (guint8 * buffer, GstRTSPMessage * msg)
1619 GstRTSPResult res = GST_RTSP_OK;
1621 gchar versionstr[20];
1626 bptr = (gchar *) buffer;
1628 if (parse_string (versionstr, sizeof (versionstr), &bptr) != GST_RTSP_OK)
1629 res = GST_RTSP_EPARSE;
1631 if (parse_string (codestr, sizeof (codestr), &bptr) != GST_RTSP_OK)
1632 res = GST_RTSP_EPARSE;
1633 code = atoi (codestr);
1634 if (G_UNLIKELY (*codestr == '\0' || code < 0 || code >= 600))
1635 res = GST_RTSP_EPARSE;
1637 while (g_ascii_isspace (*bptr))
1640 if (G_UNLIKELY (gst_rtsp_message_init_response (msg, code, bptr,
1641 NULL) != GST_RTSP_OK))
1642 res = GST_RTSP_EPARSE;
1644 res2 = parse_protocol_version (versionstr, &msg->type,
1645 &msg->type_data.response.version);
1646 if (G_LIKELY (res == GST_RTSP_OK))
1652 static GstRTSPResult
1653 parse_request_line (guint8 * buffer, GstRTSPMessage * msg)
1655 GstRTSPResult res = GST_RTSP_OK;
1657 gchar versionstr[20];
1658 gchar methodstr[20];
1661 GstRTSPMethod method;
1663 bptr = (gchar *) buffer;
1665 if (parse_string (methodstr, sizeof (methodstr), &bptr) != GST_RTSP_OK)
1666 res = GST_RTSP_EPARSE;
1667 method = gst_rtsp_find_method (methodstr);
1669 if (parse_string (urlstr, sizeof (urlstr), &bptr) != GST_RTSP_OK)
1670 res = GST_RTSP_EPARSE;
1671 if (G_UNLIKELY (*urlstr == '\0'))
1672 res = GST_RTSP_EPARSE;
1674 if (parse_string (versionstr, sizeof (versionstr), &bptr) != GST_RTSP_OK)
1675 res = GST_RTSP_EPARSE;
1677 if (G_UNLIKELY (*bptr != '\0'))
1678 res = GST_RTSP_EPARSE;
1680 if (G_UNLIKELY (gst_rtsp_message_init_request (msg, method,
1681 urlstr) != GST_RTSP_OK))
1682 res = GST_RTSP_EPARSE;
1684 res2 = parse_protocol_version (versionstr, &msg->type,
1685 &msg->type_data.request.version);
1686 if (G_LIKELY (res == GST_RTSP_OK))
1689 if (G_LIKELY (msg->type == GST_RTSP_MESSAGE_REQUEST)) {
1690 /* GET and POST are not allowed as RTSP methods */
1691 if (msg->type_data.request.method == GST_RTSP_GET ||
1692 msg->type_data.request.method == GST_RTSP_POST) {
1693 msg->type_data.request.method = GST_RTSP_INVALID;
1694 if (res == GST_RTSP_OK)
1695 res = GST_RTSP_ERROR;
1697 } else if (msg->type == GST_RTSP_MESSAGE_HTTP_REQUEST) {
1698 /* only GET and POST are allowed as HTTP methods */
1699 if (msg->type_data.request.method != GST_RTSP_GET &&
1700 msg->type_data.request.method != GST_RTSP_POST) {
1701 msg->type_data.request.method = GST_RTSP_INVALID;
1702 if (res == GST_RTSP_OK)
1703 res = GST_RTSP_ERROR;
1710 /* parsing lines means reading a Key: Value pair */
1711 static GstRTSPResult
1712 parse_line (guint8 * buffer, GstRTSPMessage * msg)
1714 GstRTSPHeaderField field;
1715 gchar *line = (gchar *) buffer;
1718 if ((value = strchr (line, ':')) == NULL || value == line)
1721 /* trim space before the colon */
1722 if (value[-1] == ' ')
1725 /* replace the colon with a NUL */
1728 /* find the header */
1729 field = gst_rtsp_find_header_field (line);
1730 if (field == GST_RTSP_HDR_INVALID)
1733 /* split up the value in multiple key:value pairs if it contains comma(s) */
1734 while (*value != '\0') {
1736 gchar *comma = NULL;
1737 gboolean quoted = FALSE;
1740 /* trim leading space */
1744 /* for headers which may not appear multiple times, and thus may not
1745 * contain multiple values on the same line, we can short-circuit the loop
1746 * below and the entire value results in just one key:value pair*/
1747 if (!gst_rtsp_header_allow_multiple (field))
1748 next_value = value + strlen (value);
1752 /* find the next value, taking special care of quotes and comments */
1753 while (*next_value != '\0') {
1754 if ((quoted || comment != 0) && *next_value == '\\' &&
1755 next_value[1] != '\0')
1757 else if (comment == 0 && *next_value == '"')
1759 else if (!quoted && *next_value == '(')
1761 else if (comment != 0 && *next_value == ')')
1763 else if (!quoted && comment == 0) {
1764 /* To quote RFC 2068: "User agents MUST take special care in parsing
1765 * the WWW-Authenticate field value if it contains more than one
1766 * challenge, or if more than one WWW-Authenticate header field is
1767 * provided, since the contents of a challenge may itself contain a
1768 * comma-separated list of authentication parameters."
1770 * What this means is that we cannot just look for an unquoted comma
1771 * when looking for multiple values in Proxy-Authenticate and
1772 * WWW-Authenticate headers. Instead we need to look for the sequence
1773 * "comma [space] token space token" before we can split after the
1776 if (field == GST_RTSP_HDR_PROXY_AUTHENTICATE ||
1777 field == GST_RTSP_HDR_WWW_AUTHENTICATE) {
1778 if (*next_value == ',') {
1779 if (next_value[1] == ' ') {
1780 /* skip any space following the comma so we do not mistake it for
1781 * separating between two tokens */
1785 } else if (*next_value == ' ' && next_value[1] != ',' &&
1786 next_value[1] != '=' && comma != NULL) {
1791 } else if (*next_value == ',')
1799 if (value != next_value && next_value[-1] == ' ')
1800 next_value[-1] = '\0';
1802 if (*next_value != '\0')
1803 *next_value++ = '\0';
1805 /* add the key:value pair */
1807 gst_rtsp_message_add_header (msg, field, value);
1818 return GST_RTSP_EPARSE;
1822 /* convert all consecutive whitespace to a single space */
1824 normalize_line (guint8 * buffer)
1827 if (g_ascii_isspace (*buffer)) {
1831 for (tmp = buffer; g_ascii_isspace (*tmp); tmp++) {
1834 memmove (buffer, tmp, strlen ((gchar *) tmp) + 1);
1842 * GST_RTSP_OK when a complete message was read.
1843 * GST_RTSP_EEOF: when the read socket is closed
1844 * GST_RTSP_EINTR: when more data is needed.
1845 * GST_RTSP_..: some other error occured.
1847 static GstRTSPResult
1848 build_next (GstRTSPBuilder * builder, GstRTSPMessage * message,
1849 GstRTSPConnection * conn)
1854 switch (builder->state) {
1859 builder->offset = 0;
1861 read_bytes (conn, (guint8 *) builder->buffer, &builder->offset, 1);
1862 if (res != GST_RTSP_OK)
1865 c = builder->buffer[0];
1867 /* we have 1 bytes now and we can see if this is a data message or
1870 /* data message, prepare for the header */
1871 builder->state = STATE_DATA_HEADER;
1872 } else if (c == '\n' || c == '\r') {
1873 /* skip \n and \r */
1874 builder->offset = 0;
1877 builder->state = STATE_READ_LINES;
1881 case STATE_DATA_HEADER:
1884 read_bytes (conn, (guint8 *) builder->buffer, &builder->offset, 4);
1885 if (res != GST_RTSP_OK)
1888 gst_rtsp_message_init_data (message, builder->buffer[1]);
1890 builder->body_len = (builder->buffer[2] << 8) | builder->buffer[3];
1891 builder->body_data = g_malloc (builder->body_len + 1);
1892 builder->body_data[builder->body_len] = '\0';
1893 builder->offset = 0;
1894 builder->state = STATE_DATA_BODY;
1897 case STATE_DATA_BODY:
1900 read_bytes (conn, builder->body_data, &builder->offset,
1902 if (res != GST_RTSP_OK)
1905 /* we have the complete body now, store in the message adjusting the
1906 * length to include the traling '\0' */
1907 gst_rtsp_message_take_body (message,
1908 (guint8 *) builder->body_data, builder->body_len + 1);
1909 builder->body_data = NULL;
1910 builder->body_len = 0;
1912 builder->state = STATE_END;
1915 case STATE_READ_LINES:
1917 res = read_line (conn, builder->buffer, &builder->offset,
1918 sizeof (builder->buffer));
1919 if (res != GST_RTSP_OK)
1922 /* we have a regular response */
1923 if (builder->buffer[0] == '\0') {
1926 /* empty line, end of message header */
1927 /* see if there is a Content-Length header, but ignore it if this
1928 * is a POST request with an x-sessioncookie header */
1929 if (gst_rtsp_message_get_header (message,
1930 GST_RTSP_HDR_CONTENT_LENGTH, &hdrval, 0) == GST_RTSP_OK &&
1931 (message->type != GST_RTSP_MESSAGE_HTTP_REQUEST ||
1932 message->type_data.request.method != GST_RTSP_POST ||
1933 gst_rtsp_message_get_header (message,
1934 GST_RTSP_HDR_X_SESSIONCOOKIE, NULL, 0) != GST_RTSP_OK)) {
1935 /* there is, prepare to read the body */
1936 builder->body_len = atol (hdrval);
1937 builder->body_data = g_try_malloc (builder->body_len + 1);
1938 /* we can't do much here, we need the length to know how many bytes
1939 * we need to read next and when allocation fails, something is
1940 * probably wrong with the length. */
1941 if (builder->body_data == NULL)
1942 goto invalid_body_len;
1944 builder->body_data[builder->body_len] = '\0';
1945 builder->offset = 0;
1946 builder->state = STATE_DATA_BODY;
1948 builder->state = STATE_END;
1953 /* we have a line */
1954 normalize_line (builder->buffer);
1955 if (builder->line == 0) {
1956 /* first line, check for response status */
1957 if (memcmp (builder->buffer, "RTSP", 4) == 0 ||
1958 memcmp (builder->buffer, "HTTP", 4) == 0) {
1959 builder->status = parse_response_status (builder->buffer, message);
1961 builder->status = parse_request_line (builder->buffer, message);
1964 /* else just parse the line */
1965 res = parse_line (builder->buffer, message);
1966 if (res != GST_RTSP_OK)
1967 builder->status = res;
1970 builder->offset = 0;
1975 gchar *session_cookie;
1978 if (message->type == GST_RTSP_MESSAGE_DATA) {
1979 /* data messages don't have headers */
1984 /* save the tunnel session in the connection */
1985 if (message->type == GST_RTSP_MESSAGE_HTTP_REQUEST &&
1986 !conn->manual_http &&
1987 conn->tstate == TUNNEL_STATE_NONE &&
1988 gst_rtsp_message_get_header (message, GST_RTSP_HDR_X_SESSIONCOOKIE,
1989 &session_cookie, 0) == GST_RTSP_OK) {
1990 strncpy (conn->tunnelid, session_cookie, TUNNELID_LEN);
1991 conn->tunnelid[TUNNELID_LEN - 1] = '\0';
1992 conn->tunneled = TRUE;
1995 /* save session id in the connection for further use */
1996 if (message->type == GST_RTSP_MESSAGE_RESPONSE &&
1997 gst_rtsp_message_get_header (message, GST_RTSP_HDR_SESSION,
1998 &session_id, 0) == GST_RTSP_OK) {
2001 maxlen = sizeof (conn->session_id) - 1;
2002 /* the sessionid can have attributes marked with ;
2003 * Make sure we strip them */
2004 for (i = 0; session_id[i] != '\0'; i++) {
2005 if (session_id[i] == ';') {
2010 } while (g_ascii_isspace (session_id[i]));
2011 if (g_str_has_prefix (&session_id[i], "timeout=")) {
2014 /* if we parsed something valid, configure */
2015 if ((to = atoi (&session_id[i + 8])) > 0)
2022 /* make sure to not overflow */
2023 strncpy (conn->session_id, session_id, maxlen);
2024 conn->session_id[maxlen] = '\0';
2026 res = builder->status;
2030 res = GST_RTSP_ERROR;
2040 GST_DEBUG ("could not allocate body");
2041 return GST_RTSP_ERROR;
2046 * gst_rtsp_connection_read:
2047 * @conn: a #GstRTSPConnection
2048 * @data: the data to read
2049 * @size: the size of @data
2050 * @timeout: a timeout value or #NULL
2052 * Attempt to read @size bytes into @data from the connected @conn, blocking up to
2053 * the specified @timeout. @timeout can be #NULL, in which case this function
2054 * might block forever.
2056 * This function can be cancelled with gst_rtsp_connection_flush().
2058 * Returns: #GST_RTSP_OK on success.
2061 gst_rtsp_connection_read (GstRTSPConnection * conn, guint8 * data, guint size,
2069 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2070 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
2071 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2073 if (G_UNLIKELY (size == 0))
2078 /* configure timeout if any */
2079 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
2081 gst_poll_set_controllable (conn->fdset, TRUE);
2082 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
2083 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
2086 res = read_bytes (conn, data, &offset, size);
2087 if (G_UNLIKELY (res == GST_RTSP_EEOF))
2089 if (G_LIKELY (res == GST_RTSP_OK))
2091 if (G_UNLIKELY (res != GST_RTSP_EINTR))
2095 retval = gst_poll_wait (conn->fdset, to);
2096 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
2098 /* check for timeout */
2099 if (G_UNLIKELY (retval == 0))
2100 goto select_timeout;
2102 if (G_UNLIKELY (retval == -1)) {
2109 /* could also be an error with write socket */
2110 if (gst_poll_fd_has_error (conn->fdset, conn->writefd))
2113 gst_poll_set_controllable (conn->fdset, FALSE);
2120 return GST_RTSP_ESYS;
2124 return GST_RTSP_ETIMEOUT;
2128 return GST_RTSP_EINTR;
2132 return GST_RTSP_EEOF;
2136 res = GST_RTSP_ENET;
2144 static GstRTSPMessage *
2145 gen_tunnel_reply (GstRTSPConnection * conn, GstRTSPStatusCode code,
2146 const GstRTSPMessage * request)
2148 GstRTSPMessage *msg;
2151 if (gst_rtsp_status_as_text (code) == NULL)
2152 code = GST_RTSP_STS_INTERNAL_SERVER_ERROR;
2154 GST_RTSP_CHECK (gst_rtsp_message_new_response (&msg, code, NULL, request),
2157 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_SERVER,
2158 "GStreamer RTSP Server");
2159 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CONNECTION, "close");
2160 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CACHE_CONTROL, "no-store");
2161 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_PRAGMA, "no-cache");
2163 if (code == GST_RTSP_STS_OK) {
2165 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_X_SERVER_IP_ADDRESS,
2167 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CONTENT_TYPE,
2168 "application/x-rtsp-tunnelled");
2181 * gst_rtsp_connection_receive:
2182 * @conn: a #GstRTSPConnection
2183 * @message: the message to read
2184 * @timeout: a timeout value or #NULL
2186 * Attempt to read into @message from the connected @conn, blocking up to
2187 * the specified @timeout. @timeout can be #NULL, in which case this function
2188 * might block forever.
2190 * This function can be cancelled with gst_rtsp_connection_flush().
2192 * Returns: #GST_RTSP_OK on success.
2195 gst_rtsp_connection_receive (GstRTSPConnection * conn, GstRTSPMessage * message,
2199 GstRTSPBuilder builder;
2203 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2204 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2205 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2207 /* configure timeout if any */
2208 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
2210 gst_poll_set_controllable (conn->fdset, TRUE);
2211 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
2212 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
2214 memset (&builder, 0, sizeof (GstRTSPBuilder));
2216 res = build_next (&builder, message, conn);
2217 if (G_UNLIKELY (res == GST_RTSP_EEOF))
2219 else if (G_LIKELY (res == GST_RTSP_OK)) {
2220 if (!conn->manual_http) {
2221 if (message->type == GST_RTSP_MESSAGE_HTTP_REQUEST) {
2222 if (conn->tstate == TUNNEL_STATE_NONE &&
2223 message->type_data.request.method == GST_RTSP_GET) {
2224 GstRTSPMessage *response;
2226 conn->tstate = TUNNEL_STATE_GET;
2228 /* tunnel GET request, we can reply now */
2229 response = gen_tunnel_reply (conn, GST_RTSP_STS_OK, message);
2230 res = gst_rtsp_connection_send (conn, response, timeout);
2231 gst_rtsp_message_free (response);
2232 if (res == GST_RTSP_OK)
2233 res = GST_RTSP_ETGET;
2235 } else if (conn->tstate == TUNNEL_STATE_NONE &&
2236 message->type_data.request.method == GST_RTSP_POST) {
2237 conn->tstate = TUNNEL_STATE_POST;
2239 /* tunnel POST request, the caller now has to link the two
2241 res = GST_RTSP_ETPOST;
2244 res = GST_RTSP_EPARSE;
2247 } else if (message->type == GST_RTSP_MESSAGE_HTTP_RESPONSE) {
2248 res = GST_RTSP_EPARSE;
2254 } else if (G_UNLIKELY (res != GST_RTSP_EINTR))
2258 retval = gst_poll_wait (conn->fdset, to);
2259 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
2261 /* check for timeout */
2262 if (G_UNLIKELY (retval == 0))
2263 goto select_timeout;
2265 if (G_UNLIKELY (retval == -1)) {
2272 /* could also be an error with write socket */
2273 if (gst_poll_fd_has_error (conn->fdset, conn->writefd))
2276 gst_poll_set_controllable (conn->fdset, FALSE);
2279 /* we have a message here */
2280 build_reset (&builder);
2287 res = GST_RTSP_ESYS;
2292 res = GST_RTSP_ETIMEOUT;
2297 res = GST_RTSP_EINTR;
2302 res = GST_RTSP_EEOF;
2307 res = GST_RTSP_ENET;
2313 build_reset (&builder);
2314 gst_rtsp_message_unset (message);
2320 * gst_rtsp_connection_close:
2321 * @conn: a #GstRTSPConnection
2323 * Close the connected @conn. After this call, the connection is in the same
2324 * state as when it was first created.
2326 * Returns: #GST_RTSP_OK on success.
2329 gst_rtsp_connection_close (GstRTSPConnection * conn)
2331 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2336 conn->read_ahead = 0;
2338 g_free (conn->initial_buffer);
2339 conn->initial_buffer = NULL;
2340 conn->initial_buffer_offset = 0;
2342 REMOVE_POLLFD (conn->fdset, &conn->fd0);
2343 REMOVE_POLLFD (conn->fdset, &conn->fd1);
2344 conn->writefd = NULL;
2345 conn->readfd = NULL;
2346 conn->tunneled = FALSE;
2347 conn->tstate = TUNNEL_STATE_NONE;
2349 g_free (conn->username);
2350 conn->username = NULL;
2351 g_free (conn->passwd);
2352 conn->passwd = NULL;
2353 gst_rtsp_connection_clear_auth_params (conn);
2356 conn->session_id[0] = '\0';
2362 * gst_rtsp_connection_free:
2363 * @conn: a #GstRTSPConnection
2365 * Close and free @conn.
2367 * Returns: #GST_RTSP_OK on success.
2370 gst_rtsp_connection_free (GstRTSPConnection * conn)
2374 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2376 res = gst_rtsp_connection_close (conn);
2377 gst_poll_free (conn->fdset);
2378 g_timer_destroy (conn->timer);
2379 gst_rtsp_url_free (conn->url);
2380 g_free (conn->proxy_host);
2390 * gst_rtsp_connection_poll:
2391 * @conn: a #GstRTSPConnection
2392 * @events: a bitmask of #GstRTSPEvent flags to check
2393 * @revents: location for result flags
2394 * @timeout: a timeout
2396 * Wait up to the specified @timeout for the connection to become available for
2397 * at least one of the operations specified in @events. When the function returns
2398 * with #GST_RTSP_OK, @revents will contain a bitmask of available operations on
2401 * @timeout can be #NULL, in which case this function might block forever.
2403 * This function can be cancelled with gst_rtsp_connection_flush().
2405 * Returns: #GST_RTSP_OK on success.
2410 gst_rtsp_connection_poll (GstRTSPConnection * conn, GstRTSPEvent events,
2411 GstRTSPEvent * revents, GTimeVal * timeout)
2416 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2417 g_return_val_if_fail (events != 0, GST_RTSP_EINVAL);
2418 g_return_val_if_fail (revents != NULL, GST_RTSP_EINVAL);
2419 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2420 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2422 gst_poll_set_controllable (conn->fdset, TRUE);
2424 /* add fd to writer set when asked to */
2425 gst_poll_fd_ctl_write (conn->fdset, conn->writefd,
2426 events & GST_RTSP_EV_WRITE);
2428 /* add fd to reader set when asked to */
2429 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, events & GST_RTSP_EV_READ);
2431 /* configure timeout if any */
2432 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
2435 retval = gst_poll_wait (conn->fdset, to);
2436 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
2438 if (G_UNLIKELY (retval == 0))
2439 goto select_timeout;
2441 if (G_UNLIKELY (retval == -1)) {
2449 if (events & GST_RTSP_EV_READ) {
2450 if (gst_poll_fd_can_read (conn->fdset, conn->readfd))
2451 *revents |= GST_RTSP_EV_READ;
2453 if (events & GST_RTSP_EV_WRITE) {
2454 if (gst_poll_fd_can_write (conn->fdset, conn->writefd))
2455 *revents |= GST_RTSP_EV_WRITE;
2462 return GST_RTSP_ETIMEOUT;
2466 return GST_RTSP_ESYS;
2470 return GST_RTSP_EINTR;
2475 * gst_rtsp_connection_next_timeout:
2476 * @conn: a #GstRTSPConnection
2477 * @timeout: a timeout
2479 * Calculate the next timeout for @conn, storing the result in @timeout.
2481 * Returns: #GST_RTSP_OK.
2484 gst_rtsp_connection_next_timeout (GstRTSPConnection * conn, GTimeVal * timeout)
2490 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2491 g_return_val_if_fail (timeout != NULL, GST_RTSP_EINVAL);
2493 elapsed = g_timer_elapsed (conn->timer, &usec);
2494 if (elapsed >= conn->timeout) {
2498 sec = conn->timeout - elapsed;
2499 if (usec <= G_USEC_PER_SEC)
2500 usec = G_USEC_PER_SEC - usec;
2505 timeout->tv_sec = sec;
2506 timeout->tv_usec = usec;
2512 * gst_rtsp_connection_reset_timeout:
2513 * @conn: a #GstRTSPConnection
2515 * Reset the timeout of @conn.
2517 * Returns: #GST_RTSP_OK.
2520 gst_rtsp_connection_reset_timeout (GstRTSPConnection * conn)
2522 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2524 g_timer_start (conn->timer);
2530 * gst_rtsp_connection_flush:
2531 * @conn: a #GstRTSPConnection
2532 * @flush: start or stop the flush
2534 * Start or stop the flushing action on @conn. When flushing, all current
2535 * and future actions on @conn will return #GST_RTSP_EINTR until the connection
2536 * is set to non-flushing mode again.
2538 * Returns: #GST_RTSP_OK.
2541 gst_rtsp_connection_flush (GstRTSPConnection * conn, gboolean flush)
2543 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2545 gst_poll_set_flushing (conn->fdset, flush);
2551 * gst_rtsp_connection_set_proxy:
2552 * @conn: a #GstRTSPConnection
2553 * @host: the proxy host
2554 * @port: the proxy port
2556 * Set the proxy host and port.
2558 * Returns: #GST_RTSP_OK.
2563 gst_rtsp_connection_set_proxy (GstRTSPConnection * conn,
2564 const gchar * host, guint port)
2566 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2568 g_free (conn->proxy_host);
2569 conn->proxy_host = g_strdup (host);
2570 conn->proxy_port = port;
2576 * gst_rtsp_connection_set_auth:
2577 * @conn: a #GstRTSPConnection
2578 * @method: authentication method
2580 * @pass: the password
2582 * Configure @conn for authentication mode @method with @user and @pass as the
2583 * user and password respectively.
2585 * Returns: #GST_RTSP_OK.
2588 gst_rtsp_connection_set_auth (GstRTSPConnection * conn,
2589 GstRTSPAuthMethod method, const gchar * user, const gchar * pass)
2591 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2593 if (method == GST_RTSP_AUTH_DIGEST && ((user == NULL || pass == NULL)
2594 || g_strrstr (user, ":") != NULL))
2595 return GST_RTSP_EINVAL;
2597 /* Make sure the username and passwd are being set for authentication */
2598 if (method == GST_RTSP_AUTH_NONE && (user == NULL || pass == NULL))
2599 return GST_RTSP_EINVAL;
2601 /* ":" chars are not allowed in usernames for basic auth */
2602 if (method == GST_RTSP_AUTH_BASIC && g_strrstr (user, ":") != NULL)
2603 return GST_RTSP_EINVAL;
2605 g_free (conn->username);
2606 g_free (conn->passwd);
2608 conn->auth_method = method;
2609 conn->username = g_strdup (user);
2610 conn->passwd = g_strdup (pass);
2617 * @key: ASCII string to hash
2619 * Hashes @key in a case-insensitive manner.
2621 * Returns: the hash code.
2624 str_case_hash (gconstpointer key)
2626 const char *p = key;
2627 guint h = g_ascii_toupper (*p);
2630 for (p += 1; *p != '\0'; p++)
2631 h = (h << 5) - h + g_ascii_toupper (*p);
2638 * @v1: an ASCII string
2639 * @v2: another ASCII string
2641 * Compares @v1 and @v2 in a case-insensitive manner
2643 * Returns: %TRUE if they are equal (modulo case)
2646 str_case_equal (gconstpointer v1, gconstpointer v2)
2648 const char *string1 = v1;
2649 const char *string2 = v2;
2651 return g_ascii_strcasecmp (string1, string2) == 0;
2655 * gst_rtsp_connection_set_auth_param:
2656 * @conn: a #GstRTSPConnection
2657 * @param: authentication directive
2660 * Setup @conn with authentication directives. This is not necesary for
2661 * methods #GST_RTSP_AUTH_NONE and #GST_RTSP_AUTH_BASIC. For
2662 * #GST_RTSP_AUTH_DIGEST, directives should be taken from the digest challenge
2663 * in the WWW-Authenticate response header and can include realm, domain,
2664 * nonce, opaque, stale, algorithm, qop as per RFC2617.
2669 gst_rtsp_connection_set_auth_param (GstRTSPConnection * conn,
2670 const gchar * param, const gchar * value)
2672 g_return_if_fail (conn != NULL);
2673 g_return_if_fail (param != NULL);
2675 if (conn->auth_params == NULL) {
2677 g_hash_table_new_full (str_case_hash, str_case_equal, g_free, g_free);
2679 g_hash_table_insert (conn->auth_params, g_strdup (param), g_strdup (value));
2683 * gst_rtsp_connection_clear_auth_params:
2684 * @conn: a #GstRTSPConnection
2686 * Clear the list of authentication directives stored in @conn.
2691 gst_rtsp_connection_clear_auth_params (GstRTSPConnection * conn)
2693 g_return_if_fail (conn != NULL);
2695 if (conn->auth_params != NULL) {
2696 g_hash_table_destroy (conn->auth_params);
2697 conn->auth_params = NULL;
2701 static GstRTSPResult
2702 set_qos_dscp (gint fd, guint qos_dscp)
2704 union gst_sockaddr sa;
2705 socklen_t slen = sizeof (sa);
2712 if (getsockname (fd, &sa.sa, &slen) < 0)
2713 goto no_getsockname;
2715 af = sa.sa.sa_family;
2717 /* if this is an IPv4-mapped address then do IPv4 QoS */
2718 if (af == AF_INET6) {
2719 if (IN6_IS_ADDR_V4MAPPED (&sa.sa_in6.sin6_addr))
2723 /* extract and shift 6 bits of the DSCP */
2724 tos = (qos_dscp & 0x3f) << 2;
2728 if (SETSOCKOPT (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0)
2733 if (SETSOCKOPT (fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof (tos)) < 0)
2747 return GST_RTSP_ESYS;
2752 return GST_RTSP_ERROR;
2757 * gst_rtsp_connection_set_qos_dscp:
2758 * @conn: a #GstRTSPConnection
2759 * @qos_dscp: DSCP value
2761 * Configure @conn to use the specified DSCP value.
2763 * Returns: #GST_RTSP_OK on success.
2768 gst_rtsp_connection_set_qos_dscp (GstRTSPConnection * conn, guint qos_dscp)
2772 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2773 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2774 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2776 res = set_qos_dscp (conn->fd0.fd, qos_dscp);
2777 if (res == GST_RTSP_OK)
2778 res = set_qos_dscp (conn->fd1.fd, qos_dscp);
2785 * gst_rtsp_connection_get_url:
2786 * @conn: a #GstRTSPConnection
2788 * Retrieve the URL of the other end of @conn.
2790 * Returns: The URL. This value remains valid until the
2791 * connection is freed.
2796 gst_rtsp_connection_get_url (const GstRTSPConnection * conn)
2798 g_return_val_if_fail (conn != NULL, NULL);
2804 * gst_rtsp_connection_get_ip:
2805 * @conn: a #GstRTSPConnection
2807 * Retrieve the IP address of the other end of @conn.
2809 * Returns: The IP address as a string. this value remains valid until the
2810 * connection is closed.
2815 gst_rtsp_connection_get_ip (const GstRTSPConnection * conn)
2817 g_return_val_if_fail (conn != NULL, NULL);
2823 * gst_rtsp_connection_set_ip:
2824 * @conn: a #GstRTSPConnection
2825 * @ip: an ip address
2827 * Set the IP address of the server.
2832 gst_rtsp_connection_set_ip (GstRTSPConnection * conn, const gchar * ip)
2834 g_return_if_fail (conn != NULL);
2837 conn->ip = g_strdup (ip);
2841 * gst_rtsp_connection_get_readfd:
2842 * @conn: a #GstRTSPConnection
2844 * Get the file descriptor for reading.
2846 * Returns: the file descriptor used for reading or -1 on error. The file
2847 * descriptor remains valid until the connection is closed.
2852 gst_rtsp_connection_get_readfd (const GstRTSPConnection * conn)
2854 g_return_val_if_fail (conn != NULL, -1);
2855 g_return_val_if_fail (conn->readfd != NULL, -1);
2857 return conn->readfd->fd;
2861 * gst_rtsp_connection_get_writefd:
2862 * @conn: a #GstRTSPConnection
2864 * Get the file descriptor for writing.
2866 * Returns: the file descriptor used for writing or -1 on error. The file
2867 * descriptor remains valid until the connection is closed.
2872 gst_rtsp_connection_get_writefd (const GstRTSPConnection * conn)
2874 g_return_val_if_fail (conn != NULL, -1);
2875 g_return_val_if_fail (conn->writefd != NULL, -1);
2877 return conn->writefd->fd;
2881 * gst_rtsp_connection_set_http_mode:
2882 * @conn: a #GstRTSPConnection
2883 * @enable: %TRUE to enable manual HTTP mode
2885 * By setting the HTTP mode to %TRUE the message parsing will support HTTP
2886 * messages in addition to the RTSP messages. It will also disable the
2887 * automatic handling of setting up an HTTP tunnel.
2892 gst_rtsp_connection_set_http_mode (GstRTSPConnection * conn, gboolean enable)
2894 g_return_if_fail (conn != NULL);
2896 conn->manual_http = enable;
2900 * gst_rtsp_connection_set_tunneled:
2901 * @conn: a #GstRTSPConnection
2902 * @tunneled: the new state
2904 * Set the HTTP tunneling state of the connection. This must be configured before
2905 * the @conn is connected.
2910 gst_rtsp_connection_set_tunneled (GstRTSPConnection * conn, gboolean tunneled)
2912 g_return_if_fail (conn != NULL);
2913 g_return_if_fail (conn->readfd == NULL);
2914 g_return_if_fail (conn->writefd == NULL);
2916 conn->tunneled = tunneled;
2920 * gst_rtsp_connection_is_tunneled:
2921 * @conn: a #GstRTSPConnection
2923 * Get the tunneling state of the connection.
2925 * Returns: if @conn is using HTTP tunneling.
2930 gst_rtsp_connection_is_tunneled (const GstRTSPConnection * conn)
2932 g_return_val_if_fail (conn != NULL, FALSE);
2934 return conn->tunneled;
2938 * gst_rtsp_connection_get_tunnelid:
2939 * @conn: a #GstRTSPConnection
2941 * Get the tunnel session id the connection.
2943 * Returns: returns a non-empty string if @conn is being tunneled over HTTP.
2948 gst_rtsp_connection_get_tunnelid (const GstRTSPConnection * conn)
2950 g_return_val_if_fail (conn != NULL, NULL);
2952 if (!conn->tunneled)
2955 return conn->tunnelid;
2959 * gst_rtsp_connection_do_tunnel:
2960 * @conn: a #GstRTSPConnection
2961 * @conn2: a #GstRTSPConnection or %NULL
2963 * If @conn received the first tunnel connection and @conn2 received
2964 * the second tunnel connection, link the two connections together so that
2965 * @conn manages the tunneled connection.
2967 * After this call, @conn2 cannot be used anymore and must be freed with
2968 * gst_rtsp_connection_free().
2970 * If @conn2 is %NULL then only the base64 decoding context will be setup for
2973 * Returns: return GST_RTSP_OK on success.
2978 gst_rtsp_connection_do_tunnel (GstRTSPConnection * conn,
2979 GstRTSPConnection * conn2)
2981 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2983 if (conn2 != NULL) {
2984 g_return_val_if_fail (conn->tstate == TUNNEL_STATE_GET, GST_RTSP_EINVAL);
2985 g_return_val_if_fail (conn2->tstate == TUNNEL_STATE_POST, GST_RTSP_EINVAL);
2986 g_return_val_if_fail (!memcmp (conn2->tunnelid, conn->tunnelid,
2987 TUNNELID_LEN), GST_RTSP_EINVAL);
2989 /* both connections have fd0 as the read/write socket. start by taking the
2990 * socket from conn2 and set it as the socket in conn */
2991 conn->fd1 = conn2->fd0;
2993 /* clean up some of the state of conn2 */
2994 gst_poll_remove_fd (conn2->fdset, &conn2->fd0);
2996 conn2->readfd = conn2->writefd = NULL;
2998 /* We make fd0 the write socket and fd1 the read socket. */
2999 conn->writefd = &conn->fd0;
3000 conn->readfd = &conn->fd1;
3002 conn->tstate = TUNNEL_STATE_COMPLETE;
3005 /* we need base64 decoding for the readfd */
3006 conn->ctx.state = 0;
3009 conn->ctx.coutl = 0;
3010 conn->ctxp = &conn->ctx;
3015 #define READ_ERR (G_IO_HUP | G_IO_ERR | G_IO_NVAL)
3016 #define READ_COND (G_IO_IN | READ_ERR)
3017 #define WRITE_ERR (G_IO_HUP | G_IO_ERR | G_IO_NVAL)
3018 #define WRITE_COND (G_IO_OUT | WRITE_ERR)
3027 /* async functions */
3028 struct _GstRTSPWatch
3032 GstRTSPConnection *conn;
3034 GstRTSPBuilder builder;
3035 GstRTSPMessage message;
3040 /* queued message for transmission */
3049 GstRTSPWatchFuncs funcs;
3052 GDestroyNotify notify;
3056 gst_rtsp_source_prepare (GSource * source, gint * timeout)
3058 GstRTSPWatch *watch = (GstRTSPWatch *) source;
3060 if (watch->conn->initial_buffer != NULL)
3063 *timeout = (watch->conn->timeout * 1000);
3069 gst_rtsp_source_check (GSource * source)
3071 GstRTSPWatch *watch = (GstRTSPWatch *) source;
3073 if (watch->readfd.revents & READ_COND)
3076 if (watch->writefd.revents & WRITE_COND)
3083 gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback G_GNUC_UNUSED,
3084 gpointer user_data G_GNUC_UNUSED)
3086 GstRTSPWatch *watch = (GstRTSPWatch *) source;
3087 GstRTSPResult res = GST_RTSP_ERROR;
3088 gboolean keep_running = TRUE;
3090 /* first read as much as we can */
3091 if (watch->readfd.revents & READ_COND || watch->conn->initial_buffer != NULL) {
3093 if (watch->readfd.revents & READ_ERR)
3096 res = build_next (&watch->builder, &watch->message, watch->conn);
3097 if (res == GST_RTSP_EINTR)
3099 else if (G_UNLIKELY (res == GST_RTSP_EEOF)) {
3100 watch->readfd.events = 0;
3101 watch->readfd.revents = 0;
3102 g_source_remove_poll ((GSource *) watch, &watch->readfd);
3103 /* When we are in tunnelled mode, the read socket can be closed and we
3104 * should be prepared for a new POST method to reopen it */
3105 if (watch->conn->tstate == TUNNEL_STATE_COMPLETE) {
3106 /* remove the read connection for the tunnel */
3107 /* we accept a new POST request */
3108 watch->conn->tstate = TUNNEL_STATE_GET;
3109 /* and signal that we lost our tunnel */
3110 if (watch->funcs.tunnel_lost)
3111 res = watch->funcs.tunnel_lost (watch, watch->user_data);
3115 } else if (G_LIKELY (res == GST_RTSP_OK)) {
3116 if (!watch->conn->manual_http &&
3117 watch->message.type == GST_RTSP_MESSAGE_HTTP_REQUEST) {
3118 if (watch->conn->tstate == TUNNEL_STATE_NONE &&
3119 watch->message.type_data.request.method == GST_RTSP_GET) {
3120 GstRTSPMessage *response;
3121 GstRTSPStatusCode code;
3123 watch->conn->tstate = TUNNEL_STATE_GET;
3125 if (watch->funcs.tunnel_start)
3126 code = watch->funcs.tunnel_start (watch, watch->user_data);
3128 code = GST_RTSP_STS_OK;
3130 /* queue the response */
3131 response = gen_tunnel_reply (watch->conn, code, &watch->message);
3132 gst_rtsp_watch_send_message (watch, response, NULL);
3133 gst_rtsp_message_free (response);
3135 } else if (watch->conn->tstate == TUNNEL_STATE_NONE &&
3136 watch->message.type_data.request.method == GST_RTSP_POST) {
3137 watch->conn->tstate = TUNNEL_STATE_POST;
3139 /* in the callback the connection should be tunneled with the
3141 if (watch->funcs.tunnel_complete)
3142 watch->funcs.tunnel_complete (watch, watch->user_data);
3148 if (!watch->conn->manual_http) {
3149 /* if manual HTTP support is not enabled, then restore the message to
3150 * what it would have looked like without the support for parsing HTTP
3151 * messages being present */
3152 if (watch->message.type == GST_RTSP_MESSAGE_HTTP_REQUEST) {
3153 watch->message.type = GST_RTSP_MESSAGE_REQUEST;
3154 watch->message.type_data.request.method = GST_RTSP_INVALID;
3155 if (watch->message.type_data.request.version != GST_RTSP_VERSION_1_0)
3156 watch->message.type_data.request.version = GST_RTSP_VERSION_INVALID;
3157 res = GST_RTSP_EPARSE;
3158 } else if (watch->message.type == GST_RTSP_MESSAGE_HTTP_RESPONSE) {
3159 watch->message.type = GST_RTSP_MESSAGE_RESPONSE;
3160 if (watch->message.type_data.response.version != GST_RTSP_VERSION_1_0)
3161 watch->message.type_data.response.version =
3162 GST_RTSP_VERSION_INVALID;
3163 res = GST_RTSP_EPARSE;
3167 if (G_LIKELY (res == GST_RTSP_OK)) {
3168 if (watch->funcs.message_received)
3169 watch->funcs.message_received (watch, &watch->message,
3176 gst_rtsp_message_unset (&watch->message);
3177 build_reset (&watch->builder);
3181 if (watch->writefd.revents & WRITE_COND) {
3182 if (watch->writefd.revents & WRITE_ERR)
3185 g_mutex_lock (watch->mutex);
3187 if (watch->write_data == NULL) {
3190 /* get a new message from the queue */
3191 rec = g_queue_pop_tail (watch->messages);
3195 watch->write_off = 0;
3196 watch->write_data = rec->data;
3197 watch->write_size = rec->size;
3198 watch->write_id = rec->id;
3200 g_slice_free (GstRTSPRec, rec);
3203 res = write_bytes (watch->writefd.fd, watch->write_data,
3204 &watch->write_off, watch->write_size);
3205 g_mutex_unlock (watch->mutex);
3207 if (res == GST_RTSP_EINTR)
3209 else if (G_LIKELY (res == GST_RTSP_OK)) {
3210 if (watch->funcs.message_sent)
3211 watch->funcs.message_sent (watch, watch->write_id, watch->user_data);
3215 g_mutex_lock (watch->mutex);
3217 g_free (watch->write_data);
3218 watch->write_data = NULL;
3221 watch->writefd.events = WRITE_ERR;
3223 g_mutex_unlock (watch->mutex);
3227 return keep_running;
3232 if (watch->funcs.closed)
3233 watch->funcs.closed (watch, watch->user_data);
3235 /* always stop when the readfd returns EOF in non-tunneled mode */
3240 watch->readfd.events = 0;
3241 watch->readfd.revents = 0;
3242 g_source_remove_poll ((GSource *) watch, &watch->readfd);
3243 keep_running = (watch->writefd.events != 0);
3246 if (watch->funcs.error_full)
3247 GST_RTSP_CHECK (watch->funcs.error_full (watch, res, &watch->message,
3248 0, watch->user_data), error);
3256 watch->writefd.events = 0;
3257 watch->writefd.revents = 0;
3258 g_source_remove_poll ((GSource *) watch, &watch->writefd);
3259 keep_running = (watch->readfd.events != 0);
3262 if (watch->funcs.error_full)
3263 GST_RTSP_CHECK (watch->funcs.error_full (watch, res, NULL,
3264 watch->write_id, watch->user_data), error);
3272 if (watch->funcs.error)
3273 watch->funcs.error (watch, res, watch->user_data);
3275 return keep_running;
3280 gst_rtsp_rec_free (gpointer data)
3282 GstRTSPRec *rec = data;
3285 g_slice_free (GstRTSPRec, rec);
3289 gst_rtsp_source_finalize (GSource * source)
3291 GstRTSPWatch *watch = (GstRTSPWatch *) source;
3293 build_reset (&watch->builder);
3294 gst_rtsp_message_unset (&watch->message);
3296 g_queue_foreach (watch->messages, (GFunc) gst_rtsp_rec_free, NULL);
3297 g_queue_free (watch->messages);
3298 watch->messages = NULL;
3299 g_free (watch->write_data);
3301 g_mutex_free (watch->mutex);
3304 watch->notify (watch->user_data);
3307 static GSourceFuncs gst_rtsp_source_funcs = {
3308 gst_rtsp_source_prepare,
3309 gst_rtsp_source_check,
3310 gst_rtsp_source_dispatch,
3311 gst_rtsp_source_finalize,
3317 * gst_rtsp_watch_new:
3318 * @conn: a #GstRTSPConnection
3319 * @funcs: watch functions
3320 * @user_data: user data to pass to @funcs
3321 * @notify: notify when @user_data is not referenced anymore
3323 * Create a watch object for @conn. The functions provided in @funcs will be
3324 * called with @user_data when activity happened on the watch.
3326 * The new watch is usually created so that it can be attached to a
3327 * maincontext with gst_rtsp_watch_attach().
3329 * @conn must exist for the entire lifetime of the watch.
3331 * Returns: a #GstRTSPWatch that can be used for asynchronous RTSP
3332 * communication. Free with gst_rtsp_watch_unref () after usage.
3337 gst_rtsp_watch_new (GstRTSPConnection * conn,
3338 GstRTSPWatchFuncs * funcs, gpointer user_data, GDestroyNotify notify)
3340 GstRTSPWatch *result;
3342 g_return_val_if_fail (conn != NULL, NULL);
3343 g_return_val_if_fail (funcs != NULL, NULL);
3344 g_return_val_if_fail (conn->readfd != NULL, NULL);
3345 g_return_val_if_fail (conn->writefd != NULL, NULL);
3347 result = (GstRTSPWatch *) g_source_new (&gst_rtsp_source_funcs,
3348 sizeof (GstRTSPWatch));
3350 result->conn = conn;
3351 result->builder.state = STATE_START;
3353 result->mutex = g_mutex_new ();
3354 result->messages = g_queue_new ();
3356 result->readfd.fd = -1;
3357 result->writefd.fd = -1;
3359 gst_rtsp_watch_reset (result);
3361 result->funcs = *funcs;
3362 result->user_data = user_data;
3363 result->notify = notify;
3369 * gst_rtsp_watch_reset:
3370 * @watch: a #GstRTSPWatch
3372 * Reset @watch, this is usually called after gst_rtsp_connection_do_tunnel()
3373 * when the file descriptors of the connection might have changed.
3378 gst_rtsp_watch_reset (GstRTSPWatch * watch)
3380 if (watch->readfd.fd != -1)
3381 g_source_remove_poll ((GSource *) watch, &watch->readfd);
3382 if (watch->writefd.fd != -1)
3383 g_source_remove_poll ((GSource *) watch, &watch->writefd);
3385 watch->readfd.fd = watch->conn->readfd->fd;
3386 watch->readfd.events = READ_COND;
3387 watch->readfd.revents = 0;
3389 watch->writefd.fd = watch->conn->writefd->fd;
3390 watch->writefd.events = WRITE_ERR;
3391 watch->writefd.revents = 0;
3393 if (watch->readfd.fd != -1)
3394 g_source_add_poll ((GSource *) watch, &watch->readfd);
3395 if (watch->writefd.fd != -1)
3396 g_source_add_poll ((GSource *) watch, &watch->writefd);
3400 * gst_rtsp_watch_attach:
3401 * @watch: a #GstRTSPWatch
3402 * @context: a GMainContext (if NULL, the default context will be used)
3404 * Adds a #GstRTSPWatch to a context so that it will be executed within that context.
3406 * Returns: the ID (greater than 0) for the watch within the GMainContext.
3411 gst_rtsp_watch_attach (GstRTSPWatch * watch, GMainContext * context)
3413 g_return_val_if_fail (watch != NULL, 0);
3415 return g_source_attach ((GSource *) watch, context);
3419 * gst_rtsp_watch_unref:
3420 * @watch: a #GstRTSPWatch
3422 * Decreases the reference count of @watch by one. If the resulting reference
3423 * count is zero the watch and associated memory will be destroyed.
3428 gst_rtsp_watch_unref (GstRTSPWatch * watch)
3430 g_return_if_fail (watch != NULL);
3432 g_source_unref ((GSource *) watch);
3436 * gst_rtsp_watch_write_data:
3437 * @watch: a #GstRTSPWatch
3438 * @data: the data to queue
3439 * @size: the size of @data
3440 * @id: location for a message ID or %NULL
3442 * Write @data using the connection of the @watch. If it cannot be sent
3443 * immediately, it will be queued for transmission in @watch. The contents of
3444 * @message will then be serialized and transmitted when the connection of the
3445 * @watch becomes writable. In case the @message is queued, the ID returned in
3446 * @id will be non-zero and used as the ID argument in the message_sent
3449 * This function will take ownership of @data and g_free() it after use.
3451 * Returns: #GST_RTSP_OK on success.
3456 gst_rtsp_watch_write_data (GstRTSPWatch * watch, const guint8 * data,
3457 guint size, guint * id)
3462 GMainContext *context = NULL;
3464 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3465 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
3466 g_return_val_if_fail (size != 0, GST_RTSP_EINVAL);
3468 g_mutex_lock (watch->mutex);
3470 /* try to send the message synchronously first */
3471 if (watch->messages->length == 0) {
3472 res = write_bytes (watch->writefd.fd, data, &off, size);
3473 if (res != GST_RTSP_EINTR) {
3476 g_free ((gpointer) data);
3481 /* make a record with the data and id for sending async */
3482 rec = g_slice_new (GstRTSPRec);
3484 rec->data = (guint8 *) data;
3487 rec->data = g_memdup (data + off, size - off);
3488 rec->size = size - off;
3489 g_free ((gpointer) data);
3493 /* make sure rec->id is never 0 */
3494 rec->id = ++watch->id;
3495 } while (G_UNLIKELY (rec->id == 0));
3497 /* add the record to a queue. FIXME we would like to have an upper limit here */
3498 g_queue_push_head (watch->messages, rec);
3500 /* make sure the main context will now also check for writability on the
3502 if (watch->writefd.events != WRITE_COND) {
3503 watch->writefd.events = WRITE_COND;
3504 context = ((GSource *) watch)->context;
3512 g_mutex_unlock (watch->mutex);
3515 g_main_context_wakeup (context);
3521 * gst_rtsp_watch_send_message:
3522 * @watch: a #GstRTSPWatch
3523 * @message: a #GstRTSPMessage
3524 * @id: location for a message ID or %NULL
3526 * Send a @message using the connection of the @watch. If it cannot be sent
3527 * immediately, it will be queued for transmission in @watch. The contents of
3528 * @message will then be serialized and transmitted when the connection of the
3529 * @watch becomes writable. In case the @message is queued, the ID returned in
3530 * @id will be non-zero and used as the ID argument in the message_sent
3533 * Returns: #GST_RTSP_OK on success.
3538 gst_rtsp_watch_send_message (GstRTSPWatch * watch, GstRTSPMessage * message,
3544 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3545 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3547 /* make a record with the message as a string and id */
3548 str = message_to_string (watch->conn, message);
3550 return gst_rtsp_watch_write_data (watch,
3551 (guint8 *) g_string_free (str, FALSE), size, id);
3555 * gst_rtsp_watch_queue_data:
3556 * @watch: a #GstRTSPWatch
3557 * @data: the data to queue
3558 * @size: the size of @data
3560 * Queue @data for transmission in @watch. It will be transmitted when the
3561 * connection of the @watch becomes writable.
3563 * This function will take ownership of @data and g_free() it after use.
3565 * The return value of this function will be used as the id argument in the
3566 * message_sent callback.
3568 * Deprecated: Use gst_rtsp_watch_write_data()
3574 #ifndef GST_REMOVE_DEPRECATED
3575 #ifdef GST_DISABLE_DEPRECATED
3577 gst_rtsp_watch_queue_data (GstRTSPWatch * watch, const guint8 * data,
3581 gst_rtsp_watch_queue_data (GstRTSPWatch * watch, const guint8 * data,
3585 GMainContext *context = NULL;
3587 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3588 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
3589 g_return_val_if_fail (size != 0, GST_RTSP_EINVAL);
3591 g_mutex_lock (watch->mutex);
3593 /* make a record with the data and id */
3594 rec = g_slice_new (GstRTSPRec);
3595 rec->data = (guint8 *) data;
3598 /* make sure rec->id is never 0 */
3599 rec->id = ++watch->id;
3600 } while (G_UNLIKELY (rec->id == 0));
3602 /* add the record to a queue. FIXME we would like to have an upper limit here */
3603 g_queue_push_head (watch->messages, rec);
3605 /* make sure the main context will now also check for writability on the
3607 if (watch->writefd.events != WRITE_COND) {
3608 watch->writefd.events = WRITE_COND;
3609 context = ((GSource *) watch)->context;
3611 g_mutex_unlock (watch->mutex);
3614 g_main_context_wakeup (context);
3618 #endif /* GST_REMOVE_DEPRECATED */
3621 * gst_rtsp_watch_queue_message:
3622 * @watch: a #GstRTSPWatch
3623 * @message: a #GstRTSPMessage
3625 * Queue a @message for transmission in @watch. The contents of this
3626 * message will be serialized and transmitted when the connection of the
3627 * @watch becomes writable.
3629 * The return value of this function will be used as the id argument in the
3630 * message_sent callback.
3632 * Deprecated: Use gst_rtsp_watch_send_message()
3638 #ifndef GST_REMOVE_DEPRECATED
3639 #ifdef GST_DISABLE_DEPRECATED
3641 gst_rtsp_watch_queue_message (GstRTSPWatch * watch, GstRTSPMessage * message);
3644 gst_rtsp_watch_queue_message (GstRTSPWatch * watch, GstRTSPMessage * message)
3649 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3650 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3652 /* make a record with the message as a string and id */
3653 str = message_to_string (watch->conn, message);
3655 return gst_rtsp_watch_queue_data (watch,
3656 (guint8 *) g_string_free (str, FALSE), size);
3658 #endif /* GST_REMOVE_DEPRECATED */