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
48 * This object manages the RTSP connection to the server. It provides function
49 * to receive and send bytes and messages.
51 * Last reviewed on 2007-07-24 (0.10.14)
68 /* we include this here to get the G_OS_* defines */
73 /* ws2_32.dll has getaddrinfo and freeaddrinfo on Windows XP and later.
74 * minwg32 headers check WINVER before allowing the use of these */
80 #define EINPROGRESS WSAEINPROGRESS
82 #include <sys/ioctl.h>
84 #include <sys/socket.h>
86 #include <netinet/in.h>
89 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
90 #include <sys/filio.h>
93 #include "gstrtspconnection.h"
94 #include "gstrtspbase64.h"
99 struct sockaddr_in sa_in;
100 struct sockaddr_in6 sa_in6;
101 struct sockaddr_storage sa_stor;
108 guchar out[3]; /* the size must be evenly divisible by 3 */
114 #define SEND_FLAGS MSG_NOSIGNAL
120 #define READ_SOCKET(fd, buf, len) recv (fd, (char *)buf, len, 0)
121 #define WRITE_SOCKET(fd, buf, len) send (fd, (const char *)buf, len, SEND_FLAGS)
122 #define SETSOCKOPT(sock, level, name, val, len) setsockopt (sock, level, name, (const char *)val, len)
123 #define CLOSE_SOCKET(sock) closesocket (sock)
124 #define ERRNO_IS_EAGAIN (WSAGetLastError () == WSAEWOULDBLOCK)
125 #define ERRNO_IS_EINTR (WSAGetLastError () == WSAEINTR)
126 /* According to Microsoft's connect() documentation this one returns
127 * WSAEWOULDBLOCK and not WSAEINPROGRESS. */
128 #define ERRNO_IS_EINPROGRESS (WSAGetLastError () == WSAEWOULDBLOCK)
130 #define READ_SOCKET(fd, buf, len) read (fd, buf, len)
131 #define WRITE_SOCKET(fd, buf, len) send (fd, buf, len, SEND_FLAGS)
132 #define SETSOCKOPT(sock, level, name, val, len) setsockopt (sock, level, name, val, len)
133 #define CLOSE_SOCKET(sock) close (sock)
134 #define ERRNO_IS_EAGAIN (errno == EAGAIN)
135 #define ERRNO_IS_EINTR (errno == EINTR)
136 #define ERRNO_IS_EINPROGRESS (errno == EINPROGRESS)
139 #define ADD_POLLFD(fdset, pfd, fd) \
142 gst_poll_add_fd (fdset, pfd); \
145 #define REMOVE_POLLFD(fdset, pfd) \
147 if ((pfd)->fd != -1) { \
148 GST_DEBUG ("remove fd %d", (pfd)->fd); \
149 gst_poll_remove_fd (fdset, pfd); \
150 CLOSE_SOCKET ((pfd)->fd); \
160 TUNNEL_STATE_COMPLETE
161 } GstRTSPTunnelState;
163 #define TUNNELID_LEN 24
165 struct _GstRTSPConnection
168 /* URL for the connection */
171 /* connection state */
178 gboolean manual_http;
180 gchar tunnelid[TUNNELID_LEN];
182 GstRTSPTunnelState tstate;
189 gchar *initial_buffer;
190 gsize initial_buffer_offset;
193 gint cseq; /* sequence number */
194 gchar session_id[512]; /* session id */
195 gint timeout; /* session timeout in seconds */
196 GTimer *timer; /* timeout timer */
199 GstRTSPAuthMethod auth_method;
202 GHashTable *auth_params;
223 READ_AHEAD_EOH = -1, /* end of headers */
224 READ_AHEAD_CRLF = -2,
225 READ_AHEAD_CRLFCR = -3
228 /* a structure for constructing RTSPMessages */
232 GstRTSPResult status;
242 build_reset (GstRTSPBuilder * builder)
244 g_free (builder->body_data);
245 memset (builder, 0, sizeof (GstRTSPBuilder));
249 * gst_rtsp_connection_create:
250 * @url: a #GstRTSPUrl
251 * @conn: storage for a #GstRTSPConnection
253 * Create a newly allocated #GstRTSPConnection from @url and store it in @conn.
254 * The connection will not yet attempt to connect to @url, use
255 * gst_rtsp_connection_connect().
257 * A copy of @url will be made.
259 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
262 gst_rtsp_connection_create (const GstRTSPUrl * url, GstRTSPConnection ** conn)
264 GstRTSPConnection *newconn;
270 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
273 error = WSAStartup (0x0202, &w);
278 if (w.wVersion != 0x0202)
282 newconn = g_new0 (GstRTSPConnection, 1);
284 if ((newconn->fdset = gst_poll_new (TRUE)) == NULL)
287 newconn->url = gst_rtsp_url_copy (url);
288 newconn->fd0.fd = -1;
289 newconn->fd1.fd = -1;
290 newconn->timer = g_timer_new ();
291 newconn->timeout = 60;
294 newconn->auth_method = GST_RTSP_AUTH_NONE;
295 newconn->username = NULL;
296 newconn->passwd = NULL;
297 newconn->auth_params = NULL;
307 g_warning ("Error %d on WSAStartup", error);
308 return GST_RTSP_EWSASTART;
312 g_warning ("Windows sockets are not version 0x202 (current 0x%x)",
315 return GST_RTSP_EWSAVERSION;
324 return GST_RTSP_ESYS;
329 * gst_rtsp_connection_create_from_fd:
330 * @fd: a file descriptor
331 * @ip: the IP address of the other end
332 * @port: the port used by the other end
333 * @initial_buffer: data already read from @fd
334 * @conn: storage for a #GstRTSPConnection
336 * Create a new #GstRTSPConnection for handling communication on the existing
337 * file descriptor @fd. The @initial_buffer contains any data already read from
338 * @fd which should be used before starting to read new data.
340 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
345 gst_rtsp_connection_create_from_fd (gint fd, const gchar * ip, guint16 port,
346 const gchar * initial_buffer, GstRTSPConnection ** conn)
348 GstRTSPConnection *newconn = NULL;
355 g_return_val_if_fail (fd >= 0, GST_RTSP_EINVAL);
356 g_return_val_if_fail (ip != NULL, GST_RTSP_EINVAL);
357 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
359 /* set to non-blocking mode so that we can cancel the communication */
361 fcntl (fd, F_SETFL, O_NONBLOCK);
363 ioctlsocket (fd, FIONBIO, &flags);
364 #endif /* G_OS_WIN32 */
366 /* create a url for the client address */
367 url = g_new0 (GstRTSPUrl, 1);
368 url->host = g_strdup (ip);
371 /* now create the connection object */
372 GST_RTSP_CHECK (gst_rtsp_connection_create (url, &newconn), newconn_failed);
373 gst_rtsp_url_free (url);
375 ADD_POLLFD (newconn->fdset, &newconn->fd0, fd);
377 /* both read and write initially */
378 newconn->readfd = &newconn->fd0;
379 newconn->writefd = &newconn->fd0;
381 newconn->ip = g_strdup (ip);
383 newconn->initial_buffer = g_strdup (initial_buffer);
392 gst_rtsp_url_free (url);
398 * gst_rtsp_connection_accept:
400 * @conn: storage for a #GstRTSPConnection
402 * Accept a new connection on @sock and create a new #GstRTSPConnection for
403 * handling communication on new socket.
405 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
410 gst_rtsp_connection_accept (gint sock, GstRTSPConnection ** conn)
413 union gst_sockaddr sa;
414 socklen_t slen = sizeof (sa);
415 gchar ip[INET6_ADDRSTRLEN];
418 g_return_val_if_fail (sock >= 0, GST_RTSP_EINVAL);
419 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
421 memset (&sa, 0, slen);
424 fd = accept (sock, &sa.sa, &slen);
426 fd = accept (sock, &sa.sa, (gint *) & slen);
427 #endif /* G_OS_WIN32 */
431 if (getnameinfo (&sa.sa, slen, ip, sizeof (ip), NULL, 0, NI_NUMERICHOST) != 0)
432 goto getnameinfo_failed;
434 if (sa.sa.sa_family == AF_INET)
435 port = sa.sa_in.sin_port;
436 else if (sa.sa.sa_family == AF_INET6)
437 port = sa.sa_in6.sin6_port;
441 return gst_rtsp_connection_create_from_fd (fd, ip, port, NULL, conn);
446 return GST_RTSP_ESYS;
452 return GST_RTSP_ERROR;
457 do_resolve (const gchar * host)
459 static gchar ip[INET6_ADDRSTRLEN];
460 struct addrinfo *aires, hints;
464 memset (&hints, 0, sizeof (struct addrinfo));
465 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
466 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
467 hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
468 hints.ai_protocol = 0; /* Any protocol */
469 hints.ai_canonname = NULL;
470 hints.ai_addr = NULL;
471 hints.ai_next = NULL;
473 aierr = getaddrinfo (host, NULL, &hints, &aires);
477 for (ai = aires; ai; ai = ai->ai_next) {
478 if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) {
485 aierr = getnameinfo (ai->ai_addr, ai->ai_addrlen, ip, sizeof (ip), NULL, 0,
486 NI_NUMERICHOST | NI_NUMERICSERV);
490 freeaddrinfo (aires);
492 return g_strdup (ip);
497 GST_ERROR ("no addrinfo found for %s: %s", host, gai_strerror (aierr));
502 GST_ERROR ("no family found for %s", host);
503 freeaddrinfo (aires);
508 GST_ERROR ("no address found for %s: %s", host, gai_strerror (aierr));
509 freeaddrinfo (aires);
515 do_connect (const gchar * ip, guint16 port, GstPollFD * fdout,
516 GstPoll * fdset, GTimeVal * timeout)
519 struct addrinfo hints;
520 struct addrinfo *aires;
523 gchar service[NI_MAXSERV];
526 unsigned long flags = 1;
527 #endif /* G_OS_WIN32 */
531 memset (&hints, 0, sizeof hints);
532 hints.ai_flags = AI_NUMERICHOST;
533 hints.ai_family = AF_UNSPEC;
534 hints.ai_socktype = SOCK_STREAM;
535 g_snprintf (service, sizeof (service) - 1, "%hu", port);
536 service[sizeof (service) - 1] = '\0';
538 aierr = getaddrinfo (ip, service, &hints, &aires);
542 for (ai = aires; ai; ai = ai->ai_next) {
543 if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) {
550 fd = socket (ai->ai_family, SOCK_STREAM, 0);
554 /* set to non-blocking mode so that we can cancel the connect */
556 fcntl (fd, F_SETFL, O_NONBLOCK);
558 ioctlsocket (fd, FIONBIO, &flags);
559 #endif /* G_OS_WIN32 */
561 /* add the socket to our fdset */
562 ADD_POLLFD (fdset, fdout, fd);
564 /* we are going to connect ASYNC now */
565 ret = connect (fd, ai->ai_addr, ai->ai_addrlen);
568 if (!ERRNO_IS_EINPROGRESS)
571 /* wait for connect to complete up to the specified timeout or until we got
573 gst_poll_fd_ctl_write (fdset, fdout, TRUE);
575 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
578 retval = gst_poll_wait (fdset, to);
579 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
583 else if (retval == -1)
586 /* we can still have an error connecting on windows */
587 if (gst_poll_fd_has_error (fdset, fdout)) {
588 socklen_t len = sizeof (errno);
590 getsockopt (fd, SOL_SOCKET, SO_ERROR, &errno, &len);
592 getsockopt (fd, SOL_SOCKET, SO_ERROR, (char *) &errno, &len);
597 gst_poll_fd_ignored (fdset, fdout);
600 freeaddrinfo (aires);
607 GST_ERROR ("no addrinfo found for %s: %s", ip, gai_strerror (aierr));
608 return GST_RTSP_ERROR;
612 GST_ERROR ("no family found for %s", ip);
613 freeaddrinfo (aires);
614 return GST_RTSP_ERROR;
618 GST_ERROR ("no socket %d (%s)", errno, g_strerror (errno));
619 freeaddrinfo (aires);
620 return GST_RTSP_ESYS;
624 GST_ERROR ("system error %d (%s)", errno, g_strerror (errno));
625 REMOVE_POLLFD (fdset, fdout);
626 freeaddrinfo (aires);
627 return GST_RTSP_ESYS;
631 GST_ERROR ("timeout");
632 REMOVE_POLLFD (fdset, fdout);
633 freeaddrinfo (aires);
634 return GST_RTSP_ETIMEOUT;
639 setup_tunneling (GstRTSPConnection * conn, GTimeVal * timeout)
646 guint16 port, url_port;
650 GstRTSPMessage response;
653 memset (&response, 0, sizeof (response));
654 gst_rtsp_message_init (&response);
656 /* create a random sessionid */
657 for (i = 0; i < TUNNELID_LEN; i++)
658 conn->tunnelid[i] = g_random_int_range ('a', 'z');
659 conn->tunnelid[TUNNELID_LEN - 1] = '\0';
662 /* get the port from the url */
663 gst_rtsp_url_get_port (url, &url_port);
665 if (conn->proxy_host) {
666 uri = g_strdup_printf ("http://%s:%d%s%s%s", url->host, url_port,
667 url->abspath, url->query ? "?" : "", url->query ? url->query : "");
668 hostparam = g_strdup_printf ("%s:%d", url->host, url_port);
669 ip = conn->proxy_host;
670 port = conn->proxy_port;
672 uri = g_strdup_printf ("%s%s%s", url->abspath, url->query ? "?" : "",
673 url->query ? url->query : "");
679 /* create the GET request for the read connection */
680 GST_RTSP_CHECK (gst_rtsp_message_new_request (&msg, GST_RTSP_GET, uri),
682 msg->type = GST_RTSP_MESSAGE_HTTP_REQUEST;
684 if (hostparam != NULL)
685 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_HOST, hostparam);
686 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_X_SESSIONCOOKIE,
688 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_ACCEPT,
689 "application/x-rtsp-tunnelled");
690 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CACHE_CONTROL, "no-cache");
691 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_PRAGMA, "no-cache");
693 /* we start by writing to this fd */
694 conn->writefd = &conn->fd0;
696 /* we need to temporarily set conn->tunneled to FALSE to prevent the HTTP
697 * request from being base64 encoded */
698 conn->tunneled = FALSE;
699 GST_RTSP_CHECK (gst_rtsp_connection_send (conn, msg, timeout), write_failed);
700 gst_rtsp_message_free (msg);
701 conn->tunneled = TRUE;
703 /* receive the response to the GET request */
704 /* we need to temporarily set manual_http to TRUE since
705 * gst_rtsp_connection_receive() will treat the HTTP response as a parsing
706 * failure otherwise */
707 old_http = conn->manual_http;
708 conn->manual_http = TRUE;
709 GST_RTSP_CHECK (gst_rtsp_connection_receive (conn, &response, timeout),
711 conn->manual_http = old_http;
713 if (response.type != GST_RTSP_MESSAGE_HTTP_RESPONSE ||
714 response.type_data.response.code != GST_RTSP_STS_OK)
717 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_X_SERVER_IP_ADDRESS,
718 &value, 0) == GST_RTSP_OK) {
719 if (conn->proxy_host) {
720 /* if we use a proxy we need to change the destination url */
722 url->host = g_strdup (value);
724 hostparam = g_strdup_printf ("%s:%d", url->host, url_port);
726 /* and resolve the new ip address */
727 if (!(ip = do_resolve (value)))
734 /* connect to the host/port */
735 res = do_connect (ip, port, &conn->fd1, conn->fdset, timeout);
736 if (res != GST_RTSP_OK)
739 /* this is now our writing socket */
740 conn->writefd = &conn->fd1;
742 /* create the POST request for the write connection */
743 GST_RTSP_CHECK (gst_rtsp_message_new_request (&msg, GST_RTSP_POST, uri),
745 msg->type = GST_RTSP_MESSAGE_HTTP_REQUEST;
747 if (hostparam != NULL)
748 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_HOST, hostparam);
749 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_X_SESSIONCOOKIE,
751 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_ACCEPT,
752 "application/x-rtsp-tunnelled");
753 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CACHE_CONTROL, "no-cache");
754 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_PRAGMA, "no-cache");
755 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_EXPIRES,
756 "Sun, 9 Jan 1972 00:00:00 GMT");
757 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CONTENT_LENGTH, "32767");
759 /* we need to temporarily set conn->tunneled to FALSE to prevent the HTTP
760 * request from being base64 encoded */
761 conn->tunneled = FALSE;
762 GST_RTSP_CHECK (gst_rtsp_connection_send (conn, msg, timeout), write_failed);
763 gst_rtsp_message_free (msg);
764 conn->tunneled = TRUE;
767 gst_rtsp_message_unset (&response);
776 GST_ERROR ("failed to create request (%d)", res);
781 GST_ERROR ("write failed (%d)", res);
782 gst_rtsp_message_free (msg);
783 conn->tunneled = TRUE;
788 GST_ERROR ("read failed (%d)", res);
789 conn->manual_http = FALSE;
794 GST_ERROR ("got failure response %d %s", response.type_data.response.code,
795 response.type_data.response.reason);
796 res = GST_RTSP_ERROR;
801 GST_ERROR ("could not resolve %s", conn->ip);
807 GST_ERROR ("failed to connect");
813 * gst_rtsp_connection_connect:
814 * @conn: a #GstRTSPConnection
815 * @timeout: a #GTimeVal timeout
817 * Attempt to connect to the url of @conn made with
818 * gst_rtsp_connection_create(). If @timeout is #NULL this function can block
819 * forever. If @timeout contains a valid timeout, this function will return
820 * #GST_RTSP_ETIMEOUT after the timeout expired.
822 * This function can be cancelled with gst_rtsp_connection_flush().
824 * Returns: #GST_RTSP_OK when a connection could be made.
827 gst_rtsp_connection_connect (GstRTSPConnection * conn, GTimeVal * timeout)
834 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
835 g_return_val_if_fail (conn->url != NULL, GST_RTSP_EINVAL);
836 g_return_val_if_fail (conn->fd0.fd < 0, GST_RTSP_EINVAL);
840 if (conn->proxy_host && conn->tunneled) {
841 if (!(ip = do_resolve (conn->proxy_host))) {
842 GST_ERROR ("could not resolve %s", conn->proxy_host);
845 port = conn->proxy_port;
846 g_free (conn->proxy_host);
847 conn->proxy_host = ip;
849 if (!(ip = do_resolve (url->host))) {
850 GST_ERROR ("could not resolve %s", url->host);
853 /* get the port from the url */
854 gst_rtsp_url_get_port (url, &port);
860 /* connect to the host/port */
861 res = do_connect (ip, port, &conn->fd0, conn->fdset, timeout);
862 if (res != GST_RTSP_OK)
865 /* this is our read URL */
866 conn->readfd = &conn->fd0;
868 if (conn->tunneled) {
869 res = setup_tunneling (conn, timeout);
870 if (res != GST_RTSP_OK)
871 goto tunneling_failed;
873 conn->writefd = &conn->fd0;
880 return GST_RTSP_ENET;
884 GST_ERROR ("failed to connect");
889 GST_ERROR ("failed to setup tunneling");
895 auth_digest_compute_hex_urp (const gchar * username,
896 const gchar * realm, const gchar * password, gchar hex_urp[33])
898 GChecksum *md5_context = g_checksum_new (G_CHECKSUM_MD5);
899 const gchar *digest_string;
901 g_checksum_update (md5_context, (const guchar *) username, strlen (username));
902 g_checksum_update (md5_context, (const guchar *) ":", 1);
903 g_checksum_update (md5_context, (const guchar *) realm, strlen (realm));
904 g_checksum_update (md5_context, (const guchar *) ":", 1);
905 g_checksum_update (md5_context, (const guchar *) password, strlen (password));
906 digest_string = g_checksum_get_string (md5_context);
908 memset (hex_urp, 0, 33);
909 memcpy (hex_urp, digest_string, strlen (digest_string));
911 g_checksum_free (md5_context);
915 auth_digest_compute_response (const gchar * method,
916 const gchar * uri, const gchar * hex_a1, const gchar * nonce,
919 char hex_a2[33] = { 0, };
920 GChecksum *md5_context = g_checksum_new (G_CHECKSUM_MD5);
921 const gchar *digest_string;
924 g_checksum_update (md5_context, (const guchar *) method, strlen (method));
925 g_checksum_update (md5_context, (const guchar *) ":", 1);
926 g_checksum_update (md5_context, (const guchar *) uri, strlen (uri));
927 digest_string = g_checksum_get_string (md5_context);
928 memcpy (hex_a2, digest_string, strlen (digest_string));
931 g_checksum_reset (md5_context);
932 g_checksum_update (md5_context, (const guchar *) hex_a1, strlen (hex_a1));
933 g_checksum_update (md5_context, (const guchar *) ":", 1);
934 g_checksum_update (md5_context, (const guchar *) nonce, strlen (nonce));
935 g_checksum_update (md5_context, (const guchar *) ":", 1);
937 g_checksum_update (md5_context, (const guchar *) hex_a2, 32);
938 digest_string = g_checksum_get_string (md5_context);
939 memset (response, 0, 33);
940 memcpy (response, digest_string, strlen (digest_string));
942 g_checksum_free (md5_context);
946 add_auth_header (GstRTSPConnection * conn, GstRTSPMessage * message)
948 switch (conn->auth_method) {
949 case GST_RTSP_AUTH_BASIC:{
954 if (conn->username == NULL || conn->passwd == NULL)
957 user_pass = g_strdup_printf ("%s:%s", conn->username, conn->passwd);
958 user_pass64 = g_base64_encode ((guchar *) user_pass, strlen (user_pass));
959 auth_string = g_strdup_printf ("Basic %s", user_pass64);
961 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
965 g_free (user_pass64);
968 case GST_RTSP_AUTH_DIGEST:{
969 gchar response[33], hex_urp[33];
970 gchar *auth_string, *auth_string2;
977 /* we need to have some params set */
978 if (conn->auth_params == NULL || conn->username == NULL ||
979 conn->passwd == NULL)
982 /* we need the realm and nonce */
983 realm = (gchar *) g_hash_table_lookup (conn->auth_params, "realm");
984 nonce = (gchar *) g_hash_table_lookup (conn->auth_params, "nonce");
985 if (realm == NULL || nonce == NULL)
988 auth_digest_compute_hex_urp (conn->username, realm, conn->passwd,
991 method = gst_rtsp_method_as_text (message->type_data.request.method);
992 uri = message->type_data.request.uri;
994 /* Assume no qop, algorithm=md5, stale=false */
995 /* For algorithm MD5, a1 = urp. */
996 auth_digest_compute_response (method, uri, hex_urp, nonce, response);
997 auth_string = g_strdup_printf ("Digest username=\"%s\", "
998 "realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"",
999 conn->username, realm, nonce, uri, response);
1001 opaque = (gchar *) g_hash_table_lookup (conn->auth_params, "opaque");
1003 auth_string2 = g_strdup_printf ("%s, opaque=\"%s\"", auth_string,
1005 g_free (auth_string);
1006 auth_string = auth_string2;
1008 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
1019 gen_date_string (gchar * date_string, guint len)
1021 static const char wkdays[7][4] =
1022 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
1023 static const char months[12][4] =
1024 { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
1032 #ifdef HAVE_GMTIME_R
1038 g_snprintf (date_string, len, "%s, %02d %s %04d %02d:%02d:%02d GMT",
1039 wkdays[tm.tm_wday], tm.tm_mday, months[tm.tm_mon], tm.tm_year + 1900,
1040 tm.tm_hour, tm.tm_min, tm.tm_sec);
1043 static GstRTSPResult
1044 write_bytes (gint fd, const guint8 * buffer, guint * idx, guint size)
1048 if (G_UNLIKELY (*idx > size))
1049 return GST_RTSP_ERROR;
1056 r = WRITE_SOCKET (fd, &buffer[*idx], left);
1057 if (G_UNLIKELY (r == 0)) {
1058 return GST_RTSP_EINTR;
1059 } else if (G_UNLIKELY (r < 0)) {
1060 if (ERRNO_IS_EAGAIN)
1061 return GST_RTSP_EINTR;
1062 if (!ERRNO_IS_EINTR)
1063 return GST_RTSP_ESYS;
1073 fill_raw_bytes (GstRTSPConnection * conn, guint8 * buffer, guint size)
1077 if (G_UNLIKELY (conn->initial_buffer != NULL)) {
1078 gsize left = strlen (&conn->initial_buffer[conn->initial_buffer_offset]);
1080 out = MIN (left, size);
1081 memcpy (buffer, &conn->initial_buffer[conn->initial_buffer_offset], out);
1083 if (left == (gsize) out) {
1084 g_free (conn->initial_buffer);
1085 conn->initial_buffer = NULL;
1086 conn->initial_buffer_offset = 0;
1088 conn->initial_buffer_offset += out;
1091 if (G_LIKELY (size > (guint) out)) {
1094 r = READ_SOCKET (conn->readfd->fd, &buffer[out], size - out);
1106 fill_bytes (GstRTSPConnection * conn, guint8 * buffer, guint size)
1108 DecodeCtx *ctx = conn->ctxp;
1113 guint8 in[sizeof (ctx->out) * 4 / 3];
1116 while (size > 0 && ctx->cout < ctx->coutl) {
1117 /* we have some leftover bytes */
1118 *buffer++ = ctx->out[ctx->cout++];
1123 /* got what we needed? */
1127 /* try to read more bytes */
1128 r = fill_raw_bytes (conn, in, sizeof (in));
1137 g_base64_decode_step ((gchar *) in, r, ctx->out, &ctx->state,
1141 out = fill_raw_bytes (conn, buffer, size);
1147 static GstRTSPResult
1148 read_bytes (GstRTSPConnection * conn, guint8 * buffer, guint * idx, guint size)
1152 if (G_UNLIKELY (*idx > size))
1153 return GST_RTSP_ERROR;
1160 r = fill_bytes (conn, &buffer[*idx], left);
1161 if (G_UNLIKELY (r == 0)) {
1162 return GST_RTSP_EEOF;
1163 } else if (G_UNLIKELY (r < 0)) {
1164 if (ERRNO_IS_EAGAIN)
1165 return GST_RTSP_EINTR;
1166 if (!ERRNO_IS_EINTR)
1167 return GST_RTSP_ESYS;
1176 /* The code below tries to handle clients using \r, \n or \r\n to indicate the
1177 * end of a line. It even does its best to handle clients which mix them (even
1178 * though this is a really stupid idea (tm).) It also handles Line White Space
1179 * (LWS), where a line end followed by whitespace is considered LWS. This is
1180 * the method used in RTSP (and HTTP) to break long lines.
1182 static GstRTSPResult
1183 read_line (GstRTSPConnection * conn, guint8 * buffer, guint * idx, guint size)
1189 if (conn->read_ahead == READ_AHEAD_EOH) {
1190 /* the last call to read_line() already determined that we have reached
1191 * the end of the headers, so convey that information now */
1192 conn->read_ahead = 0;
1194 } else if (conn->read_ahead == READ_AHEAD_CRLF) {
1195 /* the last call to read_line() left off after having read \r\n */
1197 } else if (conn->read_ahead == READ_AHEAD_CRLFCR) {
1198 /* the last call to read_line() left off after having read \r\n\r */
1200 } else if (conn->read_ahead != 0) {
1201 /* the last call to read_line() left us with a character to start with */
1202 c = (guint8) conn->read_ahead;
1203 conn->read_ahead = 0;
1205 /* read the next character */
1206 r = fill_bytes (conn, &c, 1);
1207 if (G_UNLIKELY (r == 0)) {
1208 return GST_RTSP_EEOF;
1209 } else if (G_UNLIKELY (r < 0)) {
1210 if (ERRNO_IS_EAGAIN)
1211 return GST_RTSP_EINTR;
1212 if (!ERRNO_IS_EINTR)
1213 return GST_RTSP_ESYS;
1218 /* special treatment of line endings */
1219 if (c == '\r' || c == '\n') {
1223 /* need to read ahead one more character to know what to do... */
1224 r = fill_bytes (conn, &read_ahead, 1);
1225 if (G_UNLIKELY (r == 0)) {
1226 return GST_RTSP_EEOF;
1227 } else if (G_UNLIKELY (r < 0)) {
1228 if (ERRNO_IS_EAGAIN) {
1229 /* remember the original character we read and try again next time */
1230 if (conn->read_ahead == 0)
1231 conn->read_ahead = c;
1232 return GST_RTSP_EINTR;
1234 if (!ERRNO_IS_EINTR)
1235 return GST_RTSP_ESYS;
1239 if (read_ahead == ' ' || read_ahead == '\t') {
1240 if (conn->read_ahead == READ_AHEAD_CRLFCR) {
1241 /* got \r\n\r followed by whitespace, treat it as a normal line
1242 * followed by one starting with LWS */
1243 conn->read_ahead = read_ahead;
1246 /* got LWS, change the line ending to a space and continue */
1248 conn->read_ahead = read_ahead;
1250 } else if (conn->read_ahead == READ_AHEAD_CRLFCR) {
1251 if (read_ahead == '\r' || read_ahead == '\n') {
1252 /* got \r\n\r\r or \r\n\r\n, treat it as the end of the headers */
1253 conn->read_ahead = READ_AHEAD_EOH;
1256 /* got \r\n\r followed by something else, this is not really
1257 * supported since we have probably just eaten the first character
1258 * of the body or the next message, so just ignore the second \r
1259 * and live with it... */
1260 conn->read_ahead = read_ahead;
1263 } else if (conn->read_ahead == READ_AHEAD_CRLF) {
1264 if (read_ahead == '\r') {
1265 /* got \r\n\r so far, need one more character... */
1266 conn->read_ahead = READ_AHEAD_CRLFCR;
1268 } else if (read_ahead == '\n') {
1269 /* got \r\n\n, treat it as the end of the headers */
1270 conn->read_ahead = READ_AHEAD_EOH;
1273 /* found the end of a line, keep read_ahead for the next line */
1274 conn->read_ahead = read_ahead;
1277 } else if (c == read_ahead) {
1278 /* got double \r or \n, treat it as the end of the headers */
1279 conn->read_ahead = READ_AHEAD_EOH;
1281 } else if (c == '\r' && read_ahead == '\n') {
1282 /* got \r\n so far, still need more to know what to do... */
1283 conn->read_ahead = READ_AHEAD_CRLF;
1286 /* found the end of a line, keep read_ahead for the next line */
1287 conn->read_ahead = read_ahead;
1292 if (G_LIKELY (*idx < size - 1))
1293 buffer[(*idx)++] = c;
1295 buffer[*idx] = '\0';
1301 * gst_rtsp_connection_write:
1302 * @conn: a #GstRTSPConnection
1303 * @data: the data to write
1304 * @size: the size of @data
1305 * @timeout: a timeout value or #NULL
1307 * Attempt to write @size bytes of @data to the connected @conn, blocking up to
1308 * the specified @timeout. @timeout can be #NULL, in which case this function
1309 * might block forever.
1311 * This function can be cancelled with gst_rtsp_connection_flush().
1313 * Returns: #GST_RTSP_OK on success.
1316 gst_rtsp_connection_write (GstRTSPConnection * conn, const guint8 * data,
1317 guint size, GTimeVal * timeout)
1324 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1325 g_return_val_if_fail (data != NULL || size == 0, GST_RTSP_EINVAL);
1326 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
1328 gst_poll_set_controllable (conn->fdset, TRUE);
1329 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, TRUE);
1330 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, FALSE);
1331 /* clear all previous poll results */
1332 gst_poll_fd_ignored (conn->fdset, conn->writefd);
1333 gst_poll_fd_ignored (conn->fdset, conn->readfd);
1335 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1341 res = write_bytes (conn->writefd->fd, data, &offset, size);
1342 if (G_LIKELY (res == GST_RTSP_OK))
1344 if (G_UNLIKELY (res != GST_RTSP_EINTR))
1347 /* not all is written, wait until we can write more */
1349 retval = gst_poll_wait (conn->fdset, to);
1350 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1352 if (G_UNLIKELY (retval == 0))
1355 if (G_UNLIKELY (retval == -1)) {
1362 /* could also be an error with read socket */
1363 if (gst_poll_fd_has_error (conn->fdset, conn->readfd))
1371 return GST_RTSP_ETIMEOUT;
1375 return GST_RTSP_ESYS;
1379 return GST_RTSP_EINTR;
1383 return GST_RTSP_ENET;
1392 message_to_string (GstRTSPConnection * conn, GstRTSPMessage * message)
1394 GString *str = NULL;
1396 str = g_string_new ("");
1398 switch (message->type) {
1399 case GST_RTSP_MESSAGE_REQUEST:
1400 /* create request string, add CSeq */
1401 g_string_append_printf (str, "%s %s RTSP/1.0\r\n"
1403 gst_rtsp_method_as_text (message->type_data.request.method),
1404 message->type_data.request.uri, conn->cseq++);
1405 /* add session id if we have one */
1406 if (conn->session_id[0] != '\0') {
1407 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
1408 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SESSION,
1411 /* add any authentication headers */
1412 add_auth_header (conn, message);
1414 case GST_RTSP_MESSAGE_RESPONSE:
1415 /* create response string */
1416 g_string_append_printf (str, "RTSP/1.0 %d %s\r\n",
1417 message->type_data.response.code, message->type_data.response.reason);
1419 case GST_RTSP_MESSAGE_HTTP_REQUEST:
1420 /* create request string */
1421 g_string_append_printf (str, "%s %s HTTP/%s\r\n",
1422 gst_rtsp_method_as_text (message->type_data.request.method),
1423 message->type_data.request.uri,
1424 gst_rtsp_version_as_text (message->type_data.request.version));
1425 /* add any authentication headers */
1426 add_auth_header (conn, message);
1428 case GST_RTSP_MESSAGE_HTTP_RESPONSE:
1429 /* create response string */
1430 g_string_append_printf (str, "HTTP/%s %d %s\r\n",
1431 gst_rtsp_version_as_text (message->type_data.request.version),
1432 message->type_data.response.code, message->type_data.response.reason);
1434 case GST_RTSP_MESSAGE_DATA:
1436 guint8 data_header[4];
1438 /* prepare data header */
1439 data_header[0] = '$';
1440 data_header[1] = message->type_data.data.channel;
1441 data_header[2] = (message->body_size >> 8) & 0xff;
1442 data_header[3] = message->body_size & 0xff;
1444 /* create string with header and data */
1445 str = g_string_append_len (str, (gchar *) data_header, 4);
1447 g_string_append_len (str, (gchar *) message->body,
1448 message->body_size);
1452 g_string_free (str, TRUE);
1453 g_return_val_if_reached (NULL);
1457 /* append headers and body */
1458 if (message->type != GST_RTSP_MESSAGE_DATA) {
1459 gchar date_string[100];
1461 gen_date_string (date_string, sizeof (date_string));
1463 /* add date header */
1464 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_DATE, -1);
1465 gst_rtsp_message_add_header (message, GST_RTSP_HDR_DATE, date_string);
1467 /* append headers */
1468 gst_rtsp_message_append_headers (message, str);
1470 /* append Content-Length and body if needed */
1471 if (message->body != NULL && message->body_size > 0) {
1474 len = g_strdup_printf ("%d", message->body_size);
1475 g_string_append_printf (str, "%s: %s\r\n",
1476 gst_rtsp_header_as_text (GST_RTSP_HDR_CONTENT_LENGTH), len);
1478 /* header ends here */
1479 g_string_append (str, "\r\n");
1481 g_string_append_len (str, (gchar *) message->body,
1482 message->body_size);
1484 /* just end headers */
1485 g_string_append (str, "\r\n");
1493 * gst_rtsp_connection_send:
1494 * @conn: a #GstRTSPConnection
1495 * @message: the message to send
1496 * @timeout: a timeout value or #NULL
1498 * Attempt to send @message to the connected @conn, blocking up to
1499 * the specified @timeout. @timeout can be #NULL, in which case this function
1500 * might block forever.
1502 * This function can be cancelled with gst_rtsp_connection_flush().
1504 * Returns: #GST_RTSP_OK on success.
1507 gst_rtsp_connection_send (GstRTSPConnection * conn, GstRTSPMessage * message,
1510 GString *string = NULL;
1515 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1516 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1518 if (G_UNLIKELY (!(string = message_to_string (conn, message))))
1521 if (conn->tunneled) {
1522 str = g_base64_encode ((const guchar *) string->str, string->len);
1523 g_string_free (string, TRUE);
1528 g_string_free (string, FALSE);
1532 res = gst_rtsp_connection_write (conn, (guint8 *) str, len, timeout);
1540 g_warning ("Wrong message");
1541 return GST_RTSP_EINVAL;
1545 static GstRTSPResult
1546 parse_string (gchar * dest, gint size, gchar ** src)
1548 GstRTSPResult res = GST_RTSP_OK;
1553 while (g_ascii_isspace (**src))
1556 while (!g_ascii_isspace (**src) && **src != '\0') {
1558 dest[idx++] = **src;
1560 res = GST_RTSP_EPARSE;
1569 static GstRTSPResult
1570 parse_protocol_version (gchar * protocol, GstRTSPMsgType * type,
1571 GstRTSPVersion * version)
1573 GstRTSPResult res = GST_RTSP_OK;
1576 if (G_LIKELY ((ver = strchr (protocol, '/')) != NULL)) {
1583 /* the version number must be formatted as X.Y with nothing following */
1584 if (sscanf (ver, "%u.%u%c", &major, &minor, &dummychar) != 2)
1585 res = GST_RTSP_EPARSE;
1587 if (g_ascii_strcasecmp (protocol, "RTSP") == 0) {
1588 if (major != 1 || minor != 0) {
1589 *version = GST_RTSP_VERSION_INVALID;
1590 res = GST_RTSP_ERROR;
1592 } else if (g_ascii_strcasecmp (protocol, "HTTP") == 0) {
1593 if (*type == GST_RTSP_MESSAGE_REQUEST)
1594 *type = GST_RTSP_MESSAGE_HTTP_REQUEST;
1595 else if (*type == GST_RTSP_MESSAGE_RESPONSE)
1596 *type = GST_RTSP_MESSAGE_HTTP_RESPONSE;
1598 if (major == 1 && minor == 1) {
1599 *version = GST_RTSP_VERSION_1_1;
1600 } else if (major != 1 || minor != 0) {
1601 *version = GST_RTSP_VERSION_INVALID;
1602 res = GST_RTSP_ERROR;
1605 res = GST_RTSP_EPARSE;
1607 res = GST_RTSP_EPARSE;
1612 static GstRTSPResult
1613 parse_response_status (guint8 * buffer, GstRTSPMessage * msg)
1615 GstRTSPResult res = GST_RTSP_OK;
1617 gchar versionstr[20];
1622 bptr = (gchar *) buffer;
1624 if (parse_string (versionstr, sizeof (versionstr), &bptr) != GST_RTSP_OK)
1625 res = GST_RTSP_EPARSE;
1627 if (parse_string (codestr, sizeof (codestr), &bptr) != GST_RTSP_OK)
1628 res = GST_RTSP_EPARSE;
1629 code = atoi (codestr);
1630 if (G_UNLIKELY (*codestr == '\0' || code < 0 || code >= 600))
1631 res = GST_RTSP_EPARSE;
1633 while (g_ascii_isspace (*bptr))
1636 if (G_UNLIKELY (gst_rtsp_message_init_response (msg, code, bptr,
1637 NULL) != GST_RTSP_OK))
1638 res = GST_RTSP_EPARSE;
1640 res2 = parse_protocol_version (versionstr, &msg->type,
1641 &msg->type_data.response.version);
1642 if (G_LIKELY (res == GST_RTSP_OK))
1648 static GstRTSPResult
1649 parse_request_line (guint8 * buffer, GstRTSPMessage * msg)
1651 GstRTSPResult res = GST_RTSP_OK;
1653 gchar versionstr[20];
1654 gchar methodstr[20];
1657 GstRTSPMethod method;
1659 bptr = (gchar *) buffer;
1661 if (parse_string (methodstr, sizeof (methodstr), &bptr) != GST_RTSP_OK)
1662 res = GST_RTSP_EPARSE;
1663 method = gst_rtsp_find_method (methodstr);
1665 if (parse_string (urlstr, sizeof (urlstr), &bptr) != GST_RTSP_OK)
1666 res = GST_RTSP_EPARSE;
1667 if (G_UNLIKELY (*urlstr == '\0'))
1668 res = GST_RTSP_EPARSE;
1670 if (parse_string (versionstr, sizeof (versionstr), &bptr) != GST_RTSP_OK)
1671 res = GST_RTSP_EPARSE;
1673 if (G_UNLIKELY (*bptr != '\0'))
1674 res = GST_RTSP_EPARSE;
1676 if (G_UNLIKELY (gst_rtsp_message_init_request (msg, method,
1677 urlstr) != GST_RTSP_OK))
1678 res = GST_RTSP_EPARSE;
1680 res2 = parse_protocol_version (versionstr, &msg->type,
1681 &msg->type_data.request.version);
1682 if (G_LIKELY (res == GST_RTSP_OK))
1685 if (G_LIKELY (msg->type == GST_RTSP_MESSAGE_REQUEST)) {
1686 /* GET and POST are not allowed as RTSP methods */
1687 if (msg->type_data.request.method == GST_RTSP_GET ||
1688 msg->type_data.request.method == GST_RTSP_POST) {
1689 msg->type_data.request.method = GST_RTSP_INVALID;
1690 if (res == GST_RTSP_OK)
1691 res = GST_RTSP_ERROR;
1693 } else if (msg->type == GST_RTSP_MESSAGE_HTTP_REQUEST) {
1694 /* only GET and POST are allowed as HTTP methods */
1695 if (msg->type_data.request.method != GST_RTSP_GET &&
1696 msg->type_data.request.method != GST_RTSP_POST) {
1697 msg->type_data.request.method = GST_RTSP_INVALID;
1698 if (res == GST_RTSP_OK)
1699 res = GST_RTSP_ERROR;
1706 /* parsing lines means reading a Key: Value pair */
1707 static GstRTSPResult
1708 parse_line (guint8 * buffer, GstRTSPMessage * msg)
1710 GstRTSPHeaderField field;
1711 gchar *line = (gchar *) buffer;
1714 if ((value = strchr (line, ':')) == NULL || value == line)
1717 /* trim space before the colon */
1718 if (value[-1] == ' ')
1721 /* replace the colon with a NUL */
1724 /* find the header */
1725 field = gst_rtsp_find_header_field (line);
1726 if (field == GST_RTSP_HDR_INVALID)
1729 /* split up the value in multiple key:value pairs if it contains comma(s) */
1730 while (*value != '\0') {
1732 gchar *comma = NULL;
1733 gboolean quoted = FALSE;
1736 /* trim leading space */
1740 /* for headers which may not appear multiple times, and thus may not
1741 * contain multiple values on the same line, we can short-circuit the loop
1742 * below and the entire value results in just one key:value pair*/
1743 if (!gst_rtsp_header_allow_multiple (field))
1744 next_value = value + strlen (value);
1748 /* find the next value, taking special care of quotes and comments */
1749 while (*next_value != '\0') {
1750 if ((quoted || comment != 0) && *next_value == '\\' &&
1751 next_value[1] != '\0')
1753 else if (comment == 0 && *next_value == '"')
1755 else if (!quoted && *next_value == '(')
1757 else if (comment != 0 && *next_value == ')')
1759 else if (!quoted && comment == 0) {
1760 /* To quote RFC 2068: "User agents MUST take special care in parsing
1761 * the WWW-Authenticate field value if it contains more than one
1762 * challenge, or if more than one WWW-Authenticate header field is
1763 * provided, since the contents of a challenge may itself contain a
1764 * comma-separated list of authentication parameters."
1766 * What this means is that we cannot just look for an unquoted comma
1767 * when looking for multiple values in Proxy-Authenticate and
1768 * WWW-Authenticate headers. Instead we need to look for the sequence
1769 * "comma [space] token space token" before we can split after the
1772 if (field == GST_RTSP_HDR_PROXY_AUTHENTICATE ||
1773 field == GST_RTSP_HDR_WWW_AUTHENTICATE) {
1774 if (*next_value == ',') {
1775 if (next_value[1] == ' ') {
1776 /* skip any space following the comma so we do not mistake it for
1777 * separating between two tokens */
1781 } else if (*next_value == ' ' && next_value[1] != ',' &&
1782 next_value[1] != '=' && comma != NULL) {
1787 } else if (*next_value == ',')
1795 if (value != next_value && next_value[-1] == ' ')
1796 next_value[-1] = '\0';
1798 if (*next_value != '\0')
1799 *next_value++ = '\0';
1801 /* add the key:value pair */
1803 gst_rtsp_message_add_header (msg, field, value);
1814 return GST_RTSP_EPARSE;
1818 /* convert all consecutive whitespace to a single space */
1820 normalize_line (guint8 * buffer)
1823 if (g_ascii_isspace (*buffer)) {
1827 for (tmp = buffer; g_ascii_isspace (*tmp); tmp++) {
1830 memmove (buffer, tmp, strlen ((gchar *) tmp) + 1);
1838 * GST_RTSP_OK when a complete message was read.
1839 * GST_RTSP_EEOF: when the read socket is closed
1840 * GST_RTSP_EINTR: when more data is needed.
1841 * GST_RTSP_..: some other error occured.
1843 static GstRTSPResult
1844 build_next (GstRTSPBuilder * builder, GstRTSPMessage * message,
1845 GstRTSPConnection * conn)
1850 switch (builder->state) {
1855 builder->offset = 0;
1857 read_bytes (conn, (guint8 *) builder->buffer, &builder->offset, 1);
1858 if (res != GST_RTSP_OK)
1861 c = builder->buffer[0];
1863 /* we have 1 bytes now and we can see if this is a data message or
1866 /* data message, prepare for the header */
1867 builder->state = STATE_DATA_HEADER;
1868 } else if (c == '\n' || c == '\r') {
1869 /* skip \n and \r */
1870 builder->offset = 0;
1873 builder->state = STATE_READ_LINES;
1877 case STATE_DATA_HEADER:
1880 read_bytes (conn, (guint8 *) builder->buffer, &builder->offset, 4);
1881 if (res != GST_RTSP_OK)
1884 gst_rtsp_message_init_data (message, builder->buffer[1]);
1886 builder->body_len = (builder->buffer[2] << 8) | builder->buffer[3];
1887 builder->body_data = g_malloc (builder->body_len + 1);
1888 builder->body_data[builder->body_len] = '\0';
1889 builder->offset = 0;
1890 builder->state = STATE_DATA_BODY;
1893 case STATE_DATA_BODY:
1896 read_bytes (conn, builder->body_data, &builder->offset,
1898 if (res != GST_RTSP_OK)
1901 /* we have the complete body now, store in the message adjusting the
1902 * length to include the traling '\0' */
1903 gst_rtsp_message_take_body (message,
1904 (guint8 *) builder->body_data, builder->body_len + 1);
1905 builder->body_data = NULL;
1906 builder->body_len = 0;
1908 builder->state = STATE_END;
1911 case STATE_READ_LINES:
1913 res = read_line (conn, builder->buffer, &builder->offset,
1914 sizeof (builder->buffer));
1915 if (res != GST_RTSP_OK)
1918 /* we have a regular response */
1919 if (builder->buffer[0] == '\0') {
1922 /* empty line, end of message header */
1923 /* see if there is a Content-Length header, but ignore it if this
1924 * is a POST request with an x-sessioncookie header */
1925 if (gst_rtsp_message_get_header (message,
1926 GST_RTSP_HDR_CONTENT_LENGTH, &hdrval, 0) == GST_RTSP_OK &&
1927 (message->type != GST_RTSP_MESSAGE_HTTP_REQUEST ||
1928 message->type_data.request.method != GST_RTSP_POST ||
1929 gst_rtsp_message_get_header (message,
1930 GST_RTSP_HDR_X_SESSIONCOOKIE, NULL, 0) != GST_RTSP_OK)) {
1931 /* there is, prepare to read the body */
1932 builder->body_len = atol (hdrval);
1933 builder->body_data = g_try_malloc (builder->body_len + 1);
1934 /* we can't do much here, we need the length to know how many bytes
1935 * we need to read next and when allocation fails, something is
1936 * probably wrong with the length. */
1937 if (builder->body_data == NULL)
1938 goto invalid_body_len;
1940 builder->body_data[builder->body_len] = '\0';
1941 builder->offset = 0;
1942 builder->state = STATE_DATA_BODY;
1944 builder->state = STATE_END;
1949 /* we have a line */
1950 normalize_line (builder->buffer);
1951 if (builder->line == 0) {
1952 /* first line, check for response status */
1953 if (memcmp (builder->buffer, "RTSP", 4) == 0 ||
1954 memcmp (builder->buffer, "HTTP", 4) == 0) {
1955 builder->status = parse_response_status (builder->buffer, message);
1957 builder->status = parse_request_line (builder->buffer, message);
1960 /* else just parse the line */
1961 res = parse_line (builder->buffer, message);
1962 if (res != GST_RTSP_OK)
1963 builder->status = res;
1966 builder->offset = 0;
1971 gchar *session_cookie;
1974 if (message->type == GST_RTSP_MESSAGE_DATA) {
1975 /* data messages don't have headers */
1980 /* save the tunnel session in the connection */
1981 if (message->type == GST_RTSP_MESSAGE_HTTP_REQUEST &&
1982 !conn->manual_http &&
1983 conn->tstate == TUNNEL_STATE_NONE &&
1984 gst_rtsp_message_get_header (message, GST_RTSP_HDR_X_SESSIONCOOKIE,
1985 &session_cookie, 0) == GST_RTSP_OK) {
1986 strncpy (conn->tunnelid, session_cookie, TUNNELID_LEN);
1987 conn->tunnelid[TUNNELID_LEN - 1] = '\0';
1988 conn->tunneled = TRUE;
1991 /* save session id in the connection for further use */
1992 if (message->type == GST_RTSP_MESSAGE_RESPONSE &&
1993 gst_rtsp_message_get_header (message, GST_RTSP_HDR_SESSION,
1994 &session_id, 0) == GST_RTSP_OK) {
1997 maxlen = sizeof (conn->session_id) - 1;
1998 /* the sessionid can have attributes marked with ;
1999 * Make sure we strip them */
2000 for (i = 0; session_id[i] != '\0'; i++) {
2001 if (session_id[i] == ';') {
2006 } while (g_ascii_isspace (session_id[i]));
2007 if (g_str_has_prefix (&session_id[i], "timeout=")) {
2010 /* if we parsed something valid, configure */
2011 if ((to = atoi (&session_id[i + 8])) > 0)
2018 /* make sure to not overflow */
2019 strncpy (conn->session_id, session_id, maxlen);
2020 conn->session_id[maxlen] = '\0';
2022 res = builder->status;
2026 res = GST_RTSP_ERROR;
2036 GST_DEBUG ("could not allocate body");
2037 return GST_RTSP_ERROR;
2042 * gst_rtsp_connection_read:
2043 * @conn: a #GstRTSPConnection
2044 * @data: the data to read
2045 * @size: the size of @data
2046 * @timeout: a timeout value or #NULL
2048 * Attempt to read @size bytes into @data from the connected @conn, blocking up to
2049 * the specified @timeout. @timeout can be #NULL, in which case this function
2050 * might block forever.
2052 * This function can be cancelled with gst_rtsp_connection_flush().
2054 * Returns: #GST_RTSP_OK on success.
2057 gst_rtsp_connection_read (GstRTSPConnection * conn, guint8 * data, guint size,
2065 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2066 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
2067 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2069 if (G_UNLIKELY (size == 0))
2074 /* configure timeout if any */
2075 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
2077 gst_poll_set_controllable (conn->fdset, TRUE);
2078 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
2079 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
2082 res = read_bytes (conn, data, &offset, size);
2083 if (G_UNLIKELY (res == GST_RTSP_EEOF))
2085 if (G_LIKELY (res == GST_RTSP_OK))
2087 if (G_UNLIKELY (res != GST_RTSP_EINTR))
2091 retval = gst_poll_wait (conn->fdset, to);
2092 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
2094 /* check for timeout */
2095 if (G_UNLIKELY (retval == 0))
2096 goto select_timeout;
2098 if (G_UNLIKELY (retval == -1)) {
2105 /* could also be an error with write socket */
2106 if (gst_poll_fd_has_error (conn->fdset, conn->writefd))
2109 gst_poll_set_controllable (conn->fdset, FALSE);
2116 return GST_RTSP_ESYS;
2120 return GST_RTSP_ETIMEOUT;
2124 return GST_RTSP_EINTR;
2128 return GST_RTSP_EEOF;
2132 res = GST_RTSP_ENET;
2140 static GstRTSPMessage *
2141 gen_tunnel_reply (GstRTSPConnection * conn, GstRTSPStatusCode code,
2142 const GstRTSPMessage * request)
2144 GstRTSPMessage *msg;
2147 if (gst_rtsp_status_as_text (code) == NULL)
2148 code = GST_RTSP_STS_INTERNAL_SERVER_ERROR;
2150 GST_RTSP_CHECK (gst_rtsp_message_new_response (&msg, code, NULL, request),
2153 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_SERVER,
2154 "GStreamer RTSP Server");
2155 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CONNECTION, "close");
2156 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CACHE_CONTROL, "no-store");
2157 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_PRAGMA, "no-cache");
2159 if (code == GST_RTSP_STS_OK) {
2161 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_X_SERVER_IP_ADDRESS,
2163 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CONTENT_TYPE,
2164 "application/x-rtsp-tunnelled");
2177 * gst_rtsp_connection_receive:
2178 * @conn: a #GstRTSPConnection
2179 * @message: the message to read
2180 * @timeout: a timeout value or #NULL
2182 * Attempt to read into @message from the connected @conn, blocking up to
2183 * the specified @timeout. @timeout can be #NULL, in which case this function
2184 * might block forever.
2186 * This function can be cancelled with gst_rtsp_connection_flush().
2188 * Returns: #GST_RTSP_OK on success.
2191 gst_rtsp_connection_receive (GstRTSPConnection * conn, GstRTSPMessage * message,
2195 GstRTSPBuilder builder;
2199 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2200 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2201 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2203 /* configure timeout if any */
2204 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
2206 gst_poll_set_controllable (conn->fdset, TRUE);
2207 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
2208 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
2210 memset (&builder, 0, sizeof (GstRTSPBuilder));
2212 res = build_next (&builder, message, conn);
2213 if (G_UNLIKELY (res == GST_RTSP_EEOF))
2215 else if (G_LIKELY (res == GST_RTSP_OK)) {
2216 if (!conn->manual_http) {
2217 if (message->type == GST_RTSP_MESSAGE_HTTP_REQUEST) {
2218 if (conn->tstate == TUNNEL_STATE_NONE &&
2219 message->type_data.request.method == GST_RTSP_GET) {
2220 GstRTSPMessage *response;
2222 conn->tstate = TUNNEL_STATE_GET;
2224 /* tunnel GET request, we can reply now */
2225 response = gen_tunnel_reply (conn, GST_RTSP_STS_OK, message);
2226 res = gst_rtsp_connection_send (conn, response, timeout);
2227 gst_rtsp_message_free (response);
2228 if (res == GST_RTSP_OK)
2229 res = GST_RTSP_ETGET;
2231 } else if (conn->tstate == TUNNEL_STATE_NONE &&
2232 message->type_data.request.method == GST_RTSP_POST) {
2233 conn->tstate = TUNNEL_STATE_POST;
2235 /* tunnel POST request, the caller now has to link the two
2237 res = GST_RTSP_ETPOST;
2240 res = GST_RTSP_EPARSE;
2243 } else if (message->type == GST_RTSP_MESSAGE_HTTP_RESPONSE) {
2244 res = GST_RTSP_EPARSE;
2250 } else if (G_UNLIKELY (res != GST_RTSP_EINTR))
2254 retval = gst_poll_wait (conn->fdset, to);
2255 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
2257 /* check for timeout */
2258 if (G_UNLIKELY (retval == 0))
2259 goto select_timeout;
2261 if (G_UNLIKELY (retval == -1)) {
2268 /* could also be an error with write socket */
2269 if (gst_poll_fd_has_error (conn->fdset, conn->writefd))
2272 gst_poll_set_controllable (conn->fdset, FALSE);
2275 /* we have a message here */
2276 build_reset (&builder);
2283 res = GST_RTSP_ESYS;
2288 res = GST_RTSP_ETIMEOUT;
2293 res = GST_RTSP_EINTR;
2298 res = GST_RTSP_EEOF;
2303 res = GST_RTSP_ENET;
2309 build_reset (&builder);
2310 gst_rtsp_message_unset (message);
2316 * gst_rtsp_connection_close:
2317 * @conn: a #GstRTSPConnection
2319 * Close the connected @conn. After this call, the connection is in the same
2320 * state as when it was first created.
2322 * Returns: #GST_RTSP_OK on success.
2325 gst_rtsp_connection_close (GstRTSPConnection * conn)
2327 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2332 conn->read_ahead = 0;
2334 g_free (conn->initial_buffer);
2335 conn->initial_buffer = NULL;
2336 conn->initial_buffer_offset = 0;
2338 REMOVE_POLLFD (conn->fdset, &conn->fd0);
2339 REMOVE_POLLFD (conn->fdset, &conn->fd1);
2340 conn->writefd = NULL;
2341 conn->readfd = NULL;
2342 conn->tunneled = FALSE;
2343 conn->tstate = TUNNEL_STATE_NONE;
2345 g_free (conn->username);
2346 conn->username = NULL;
2347 g_free (conn->passwd);
2348 conn->passwd = NULL;
2349 gst_rtsp_connection_clear_auth_params (conn);
2352 conn->session_id[0] = '\0';
2358 * gst_rtsp_connection_free:
2359 * @conn: a #GstRTSPConnection
2361 * Close and free @conn.
2363 * Returns: #GST_RTSP_OK on success.
2366 gst_rtsp_connection_free (GstRTSPConnection * conn)
2370 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2372 res = gst_rtsp_connection_close (conn);
2373 gst_poll_free (conn->fdset);
2374 g_timer_destroy (conn->timer);
2375 gst_rtsp_url_free (conn->url);
2376 g_free (conn->proxy_host);
2386 * gst_rtsp_connection_poll:
2387 * @conn: a #GstRTSPConnection
2388 * @events: a bitmask of #GstRTSPEvent flags to check
2389 * @revents: location for result flags
2390 * @timeout: a timeout
2392 * Wait up to the specified @timeout for the connection to become available for
2393 * at least one of the operations specified in @events. When the function returns
2394 * with #GST_RTSP_OK, @revents will contain a bitmask of available operations on
2397 * @timeout can be #NULL, in which case this function might block forever.
2399 * This function can be cancelled with gst_rtsp_connection_flush().
2401 * Returns: #GST_RTSP_OK on success.
2406 gst_rtsp_connection_poll (GstRTSPConnection * conn, GstRTSPEvent events,
2407 GstRTSPEvent * revents, GTimeVal * timeout)
2412 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2413 g_return_val_if_fail (events != 0, GST_RTSP_EINVAL);
2414 g_return_val_if_fail (revents != NULL, GST_RTSP_EINVAL);
2415 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2416 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2418 gst_poll_set_controllable (conn->fdset, TRUE);
2420 /* add fd to writer set when asked to */
2421 gst_poll_fd_ctl_write (conn->fdset, conn->writefd,
2422 events & GST_RTSP_EV_WRITE);
2424 /* add fd to reader set when asked to */
2425 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, events & GST_RTSP_EV_READ);
2427 /* configure timeout if any */
2428 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
2431 retval = gst_poll_wait (conn->fdset, to);
2432 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
2434 if (G_UNLIKELY (retval == 0))
2435 goto select_timeout;
2437 if (G_UNLIKELY (retval == -1)) {
2445 if (events & GST_RTSP_EV_READ) {
2446 if (gst_poll_fd_can_read (conn->fdset, conn->readfd))
2447 *revents |= GST_RTSP_EV_READ;
2449 if (events & GST_RTSP_EV_WRITE) {
2450 if (gst_poll_fd_can_write (conn->fdset, conn->writefd))
2451 *revents |= GST_RTSP_EV_WRITE;
2458 return GST_RTSP_ETIMEOUT;
2462 return GST_RTSP_ESYS;
2466 return GST_RTSP_EINTR;
2471 * gst_rtsp_connection_next_timeout:
2472 * @conn: a #GstRTSPConnection
2473 * @timeout: a timeout
2475 * Calculate the next timeout for @conn, storing the result in @timeout.
2477 * Returns: #GST_RTSP_OK.
2480 gst_rtsp_connection_next_timeout (GstRTSPConnection * conn, GTimeVal * timeout)
2486 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2487 g_return_val_if_fail (timeout != NULL, GST_RTSP_EINVAL);
2489 /* Because we should act before the timeout we timeout 5
2490 * seconds in advance. */
2491 elapsed = g_timer_elapsed (conn->timer, &usec) + 5;
2492 if (elapsed >= conn->timeout) {
2496 sec = conn->timeout - elapsed;
2497 if (usec <= G_USEC_PER_SEC)
2498 usec = G_USEC_PER_SEC - usec;
2503 timeout->tv_sec = sec;
2504 timeout->tv_usec = usec;
2510 * gst_rtsp_connection_reset_timeout:
2511 * @conn: a #GstRTSPConnection
2513 * Reset the timeout of @conn.
2515 * Returns: #GST_RTSP_OK.
2518 gst_rtsp_connection_reset_timeout (GstRTSPConnection * conn)
2520 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2522 g_timer_start (conn->timer);
2528 * gst_rtsp_connection_flush:
2529 * @conn: a #GstRTSPConnection
2530 * @flush: start or stop the flush
2532 * Start or stop the flushing action on @conn. When flushing, all current
2533 * and future actions on @conn will return #GST_RTSP_EINTR until the connection
2534 * is set to non-flushing mode again.
2536 * Returns: #GST_RTSP_OK.
2539 gst_rtsp_connection_flush (GstRTSPConnection * conn, gboolean flush)
2541 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2543 gst_poll_set_flushing (conn->fdset, flush);
2549 * gst_rtsp_connection_set_proxy:
2550 * @conn: a #GstRTSPConnection
2551 * @host: the proxy host
2552 * @port: the proxy port
2554 * Set the proxy host and port.
2556 * Returns: #GST_RTSP_OK.
2561 gst_rtsp_connection_set_proxy (GstRTSPConnection * conn,
2562 const gchar * host, guint port)
2564 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2566 g_free (conn->proxy_host);
2567 conn->proxy_host = g_strdup (host);
2568 conn->proxy_port = port;
2574 * gst_rtsp_connection_set_auth:
2575 * @conn: a #GstRTSPConnection
2576 * @method: authentication method
2578 * @pass: the password
2580 * Configure @conn for authentication mode @method with @user and @pass as the
2581 * user and password respectively.
2583 * Returns: #GST_RTSP_OK.
2586 gst_rtsp_connection_set_auth (GstRTSPConnection * conn,
2587 GstRTSPAuthMethod method, const gchar * user, const gchar * pass)
2589 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2591 if (method == GST_RTSP_AUTH_DIGEST && ((user == NULL || pass == NULL)
2592 || g_strrstr (user, ":") != NULL))
2593 return GST_RTSP_EINVAL;
2595 /* Make sure the username and passwd are being set for authentication */
2596 if (method == GST_RTSP_AUTH_NONE && (user == NULL || pass == NULL))
2597 return GST_RTSP_EINVAL;
2599 /* ":" chars are not allowed in usernames for basic auth */
2600 if (method == GST_RTSP_AUTH_BASIC && g_strrstr (user, ":") != NULL)
2601 return GST_RTSP_EINVAL;
2603 g_free (conn->username);
2604 g_free (conn->passwd);
2606 conn->auth_method = method;
2607 conn->username = g_strdup (user);
2608 conn->passwd = g_strdup (pass);
2615 * @key: ASCII string to hash
2617 * Hashes @key in a case-insensitive manner.
2619 * Returns: the hash code.
2622 str_case_hash (gconstpointer key)
2624 const char *p = key;
2625 guint h = g_ascii_toupper (*p);
2628 for (p += 1; *p != '\0'; p++)
2629 h = (h << 5) - h + g_ascii_toupper (*p);
2636 * @v1: an ASCII string
2637 * @v2: another ASCII string
2639 * Compares @v1 and @v2 in a case-insensitive manner
2641 * Returns: %TRUE if they are equal (modulo case)
2644 str_case_equal (gconstpointer v1, gconstpointer v2)
2646 const char *string1 = v1;
2647 const char *string2 = v2;
2649 return g_ascii_strcasecmp (string1, string2) == 0;
2653 * gst_rtsp_connection_set_auth_param:
2654 * @conn: a #GstRTSPConnection
2655 * @param: authentication directive
2658 * Setup @conn with authentication directives. This is not necesary for
2659 * methods #GST_RTSP_AUTH_NONE and #GST_RTSP_AUTH_BASIC. For
2660 * #GST_RTSP_AUTH_DIGEST, directives should be taken from the digest challenge
2661 * in the WWW-Authenticate response header and can include realm, domain,
2662 * nonce, opaque, stale, algorithm, qop as per RFC2617.
2667 gst_rtsp_connection_set_auth_param (GstRTSPConnection * conn,
2668 const gchar * param, const gchar * value)
2670 g_return_if_fail (conn != NULL);
2671 g_return_if_fail (param != NULL);
2673 if (conn->auth_params == NULL) {
2675 g_hash_table_new_full (str_case_hash, str_case_equal, g_free, g_free);
2677 g_hash_table_insert (conn->auth_params, g_strdup (param), g_strdup (value));
2681 * gst_rtsp_connection_clear_auth_params:
2682 * @conn: a #GstRTSPConnection
2684 * Clear the list of authentication directives stored in @conn.
2689 gst_rtsp_connection_clear_auth_params (GstRTSPConnection * conn)
2691 g_return_if_fail (conn != NULL);
2693 if (conn->auth_params != NULL) {
2694 g_hash_table_destroy (conn->auth_params);
2695 conn->auth_params = NULL;
2699 static GstRTSPResult
2700 set_qos_dscp (gint fd, guint qos_dscp)
2702 union gst_sockaddr sa;
2703 socklen_t slen = sizeof (sa);
2710 if (getsockname (fd, &sa.sa, &slen) < 0)
2711 goto no_getsockname;
2713 af = sa.sa.sa_family;
2715 /* if this is an IPv4-mapped address then do IPv4 QoS */
2716 if (af == AF_INET6) {
2717 if (IN6_IS_ADDR_V4MAPPED (&sa.sa_in6.sin6_addr))
2721 /* extract and shift 6 bits of the DSCP */
2722 tos = (qos_dscp & 0x3f) << 2;
2726 if (SETSOCKOPT (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0)
2731 if (SETSOCKOPT (fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof (tos)) < 0)
2745 return GST_RTSP_ESYS;
2750 return GST_RTSP_ERROR;
2755 * gst_rtsp_connection_set_qos_dscp:
2756 * @conn: a #GstRTSPConnection
2757 * @qos_dscp: DSCP value
2759 * Configure @conn to use the specified DSCP value.
2761 * Returns: #GST_RTSP_OK on success.
2766 gst_rtsp_connection_set_qos_dscp (GstRTSPConnection * conn, guint qos_dscp)
2770 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2771 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2772 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2774 res = set_qos_dscp (conn->fd0.fd, qos_dscp);
2775 if (res == GST_RTSP_OK)
2776 res = set_qos_dscp (conn->fd1.fd, qos_dscp);
2783 * gst_rtsp_connection_get_url:
2784 * @conn: a #GstRTSPConnection
2786 * Retrieve the URL of the other end of @conn.
2788 * Returns: The URL. This value remains valid until the
2789 * connection is freed.
2794 gst_rtsp_connection_get_url (const GstRTSPConnection * conn)
2796 g_return_val_if_fail (conn != NULL, NULL);
2802 * gst_rtsp_connection_get_ip:
2803 * @conn: a #GstRTSPConnection
2805 * Retrieve the IP address of the other end of @conn.
2807 * Returns: The IP address as a string. this value remains valid until the
2808 * connection is closed.
2813 gst_rtsp_connection_get_ip (const GstRTSPConnection * conn)
2815 g_return_val_if_fail (conn != NULL, NULL);
2821 * gst_rtsp_connection_set_ip:
2822 * @conn: a #GstRTSPConnection
2823 * @ip: an ip address
2825 * Set the IP address of the server.
2830 gst_rtsp_connection_set_ip (GstRTSPConnection * conn, const gchar * ip)
2832 g_return_if_fail (conn != NULL);
2835 conn->ip = g_strdup (ip);
2839 * gst_rtsp_connection_get_readfd:
2840 * @conn: a #GstRTSPConnection
2842 * Get the file descriptor for reading.
2844 * Returns: the file descriptor used for reading or -1 on error. The file
2845 * descriptor remains valid until the connection is closed.
2850 gst_rtsp_connection_get_readfd (const GstRTSPConnection * conn)
2852 g_return_val_if_fail (conn != NULL, -1);
2853 g_return_val_if_fail (conn->readfd != NULL, -1);
2855 return conn->readfd->fd;
2859 * gst_rtsp_connection_get_writefd:
2860 * @conn: a #GstRTSPConnection
2862 * Get the file descriptor for writing.
2864 * Returns: the file descriptor used for writing or -1 on error. The file
2865 * descriptor remains valid until the connection is closed.
2870 gst_rtsp_connection_get_writefd (const GstRTSPConnection * conn)
2872 g_return_val_if_fail (conn != NULL, -1);
2873 g_return_val_if_fail (conn->writefd != NULL, -1);
2875 return conn->writefd->fd;
2879 * gst_rtsp_connection_set_http_mode:
2880 * @conn: a #GstRTSPConnection
2881 * @enable: %TRUE to enable manual HTTP mode
2883 * By setting the HTTP mode to %TRUE the message parsing will support HTTP
2884 * messages in addition to the RTSP messages. It will also disable the
2885 * automatic handling of setting up an HTTP tunnel.
2890 gst_rtsp_connection_set_http_mode (GstRTSPConnection * conn, gboolean enable)
2892 g_return_if_fail (conn != NULL);
2894 conn->manual_http = enable;
2898 * gst_rtsp_connection_set_tunneled:
2899 * @conn: a #GstRTSPConnection
2900 * @tunneled: the new state
2902 * Set the HTTP tunneling state of the connection. This must be configured before
2903 * the @conn is connected.
2908 gst_rtsp_connection_set_tunneled (GstRTSPConnection * conn, gboolean tunneled)
2910 g_return_if_fail (conn != NULL);
2911 g_return_if_fail (conn->readfd == NULL);
2912 g_return_if_fail (conn->writefd == NULL);
2914 conn->tunneled = tunneled;
2918 * gst_rtsp_connection_is_tunneled:
2919 * @conn: a #GstRTSPConnection
2921 * Get the tunneling state of the connection.
2923 * Returns: if @conn is using HTTP tunneling.
2928 gst_rtsp_connection_is_tunneled (const GstRTSPConnection * conn)
2930 g_return_val_if_fail (conn != NULL, FALSE);
2932 return conn->tunneled;
2936 * gst_rtsp_connection_get_tunnelid:
2937 * @conn: a #GstRTSPConnection
2939 * Get the tunnel session id the connection.
2941 * Returns: returns a non-empty string if @conn is being tunneled over HTTP.
2946 gst_rtsp_connection_get_tunnelid (const GstRTSPConnection * conn)
2948 g_return_val_if_fail (conn != NULL, NULL);
2950 if (!conn->tunneled)
2953 return conn->tunnelid;
2957 * gst_rtsp_connection_do_tunnel:
2958 * @conn: a #GstRTSPConnection
2959 * @conn2: a #GstRTSPConnection or %NULL
2961 * If @conn received the first tunnel connection and @conn2 received
2962 * the second tunnel connection, link the two connections together so that
2963 * @conn manages the tunneled connection.
2965 * After this call, @conn2 cannot be used anymore and must be freed with
2966 * gst_rtsp_connection_free().
2968 * If @conn2 is %NULL then only the base64 decoding context will be setup for
2971 * Returns: return GST_RTSP_OK on success.
2976 gst_rtsp_connection_do_tunnel (GstRTSPConnection * conn,
2977 GstRTSPConnection * conn2)
2979 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2981 if (conn2 != NULL) {
2982 g_return_val_if_fail (conn->tstate == TUNNEL_STATE_GET, GST_RTSP_EINVAL);
2983 g_return_val_if_fail (conn2->tstate == TUNNEL_STATE_POST, GST_RTSP_EINVAL);
2984 g_return_val_if_fail (!memcmp (conn2->tunnelid, conn->tunnelid,
2985 TUNNELID_LEN), GST_RTSP_EINVAL);
2987 /* both connections have fd0 as the read/write socket. start by taking the
2988 * socket from conn2 and set it as the socket in conn */
2989 conn->fd1 = conn2->fd0;
2991 /* clean up some of the state of conn2 */
2992 gst_poll_remove_fd (conn2->fdset, &conn2->fd0);
2994 conn2->readfd = conn2->writefd = NULL;
2996 /* We make fd0 the write socket and fd1 the read socket. */
2997 conn->writefd = &conn->fd0;
2998 conn->readfd = &conn->fd1;
3000 conn->tstate = TUNNEL_STATE_COMPLETE;
3003 /* we need base64 decoding for the readfd */
3004 conn->ctx.state = 0;
3007 conn->ctx.coutl = 0;
3008 conn->ctxp = &conn->ctx;
3013 #define READ_ERR (G_IO_HUP | G_IO_ERR | G_IO_NVAL)
3014 #define READ_COND (G_IO_IN | READ_ERR)
3015 #define WRITE_ERR (G_IO_HUP | G_IO_ERR | G_IO_NVAL)
3016 #define WRITE_COND (G_IO_OUT | WRITE_ERR)
3025 /* async functions */
3026 struct _GstRTSPWatch
3030 GstRTSPConnection *conn;
3032 GstRTSPBuilder builder;
3033 GstRTSPMessage message;
3038 /* queued message for transmission */
3047 GstRTSPWatchFuncs funcs;
3050 GDestroyNotify notify;
3054 gst_rtsp_source_prepare (GSource * source, gint * timeout)
3056 GstRTSPWatch *watch = (GstRTSPWatch *) source;
3058 if (watch->conn->initial_buffer != NULL)
3061 *timeout = (watch->conn->timeout * 1000);
3067 gst_rtsp_source_check (GSource * source)
3069 GstRTSPWatch *watch = (GstRTSPWatch *) source;
3071 if (watch->readfd.revents & READ_COND)
3074 if (watch->writefd.revents & WRITE_COND)
3081 gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback G_GNUC_UNUSED,
3082 gpointer user_data G_GNUC_UNUSED)
3084 GstRTSPWatch *watch = (GstRTSPWatch *) source;
3085 GstRTSPResult res = GST_RTSP_ERROR;
3086 gboolean keep_running = TRUE;
3088 /* first read as much as we can */
3089 if (watch->readfd.revents & READ_COND || watch->conn->initial_buffer != NULL) {
3091 if (watch->readfd.revents & READ_ERR)
3094 res = build_next (&watch->builder, &watch->message, watch->conn);
3095 if (res == GST_RTSP_EINTR)
3097 else if (G_UNLIKELY (res == GST_RTSP_EEOF)) {
3098 watch->readfd.events = 0;
3099 watch->readfd.revents = 0;
3100 g_source_remove_poll ((GSource *) watch, &watch->readfd);
3101 /* When we are in tunnelled mode, the read socket can be closed and we
3102 * should be prepared for a new POST method to reopen it */
3103 if (watch->conn->tstate == TUNNEL_STATE_COMPLETE) {
3104 /* remove the read connection for the tunnel */
3105 /* we accept a new POST request */
3106 watch->conn->tstate = TUNNEL_STATE_GET;
3107 /* and signal that we lost our tunnel */
3108 if (watch->funcs.tunnel_lost)
3109 res = watch->funcs.tunnel_lost (watch, watch->user_data);
3113 } else if (G_LIKELY (res == GST_RTSP_OK)) {
3114 if (!watch->conn->manual_http &&
3115 watch->message.type == GST_RTSP_MESSAGE_HTTP_REQUEST) {
3116 if (watch->conn->tstate == TUNNEL_STATE_NONE &&
3117 watch->message.type_data.request.method == GST_RTSP_GET) {
3118 GstRTSPMessage *response;
3119 GstRTSPStatusCode code;
3121 watch->conn->tstate = TUNNEL_STATE_GET;
3123 if (watch->funcs.tunnel_start)
3124 code = watch->funcs.tunnel_start (watch, watch->user_data);
3126 code = GST_RTSP_STS_OK;
3128 /* queue the response */
3129 response = gen_tunnel_reply (watch->conn, code, &watch->message);
3130 gst_rtsp_watch_send_message (watch, response, NULL);
3131 gst_rtsp_message_free (response);
3133 } else if (watch->conn->tstate == TUNNEL_STATE_NONE &&
3134 watch->message.type_data.request.method == GST_RTSP_POST) {
3135 watch->conn->tstate = TUNNEL_STATE_POST;
3137 /* in the callback the connection should be tunneled with the
3139 if (watch->funcs.tunnel_complete)
3140 watch->funcs.tunnel_complete (watch, watch->user_data);
3146 if (!watch->conn->manual_http) {
3147 /* if manual HTTP support is not enabled, then restore the message to
3148 * what it would have looked like without the support for parsing HTTP
3149 * messages being present */
3150 if (watch->message.type == GST_RTSP_MESSAGE_HTTP_REQUEST) {
3151 watch->message.type = GST_RTSP_MESSAGE_REQUEST;
3152 watch->message.type_data.request.method = GST_RTSP_INVALID;
3153 if (watch->message.type_data.request.version != GST_RTSP_VERSION_1_0)
3154 watch->message.type_data.request.version = GST_RTSP_VERSION_INVALID;
3155 res = GST_RTSP_EPARSE;
3156 } else if (watch->message.type == GST_RTSP_MESSAGE_HTTP_RESPONSE) {
3157 watch->message.type = GST_RTSP_MESSAGE_RESPONSE;
3158 if (watch->message.type_data.response.version != GST_RTSP_VERSION_1_0)
3159 watch->message.type_data.response.version =
3160 GST_RTSP_VERSION_INVALID;
3161 res = GST_RTSP_EPARSE;
3165 if (G_LIKELY (res == GST_RTSP_OK)) {
3166 if (watch->funcs.message_received)
3167 watch->funcs.message_received (watch, &watch->message,
3174 gst_rtsp_message_unset (&watch->message);
3175 build_reset (&watch->builder);
3179 if (watch->writefd.revents & WRITE_COND) {
3180 if (watch->writefd.revents & WRITE_ERR)
3183 g_mutex_lock (watch->mutex);
3185 if (watch->write_data == NULL) {
3188 /* get a new message from the queue */
3189 rec = g_queue_pop_tail (watch->messages);
3193 watch->write_off = 0;
3194 watch->write_data = rec->data;
3195 watch->write_size = rec->size;
3196 watch->write_id = rec->id;
3198 g_slice_free (GstRTSPRec, rec);
3201 res = write_bytes (watch->writefd.fd, watch->write_data,
3202 &watch->write_off, watch->write_size);
3203 g_mutex_unlock (watch->mutex);
3205 if (res == GST_RTSP_EINTR)
3207 else if (G_LIKELY (res == GST_RTSP_OK)) {
3208 if (watch->funcs.message_sent)
3209 watch->funcs.message_sent (watch, watch->write_id, watch->user_data);
3213 g_mutex_lock (watch->mutex);
3215 g_free (watch->write_data);
3216 watch->write_data = NULL;
3219 watch->writefd.events = WRITE_ERR;
3221 g_mutex_unlock (watch->mutex);
3225 return keep_running;
3230 if (watch->funcs.closed)
3231 watch->funcs.closed (watch, watch->user_data);
3233 /* always stop when the readfd returns EOF in non-tunneled mode */
3238 watch->readfd.events = 0;
3239 watch->readfd.revents = 0;
3240 g_source_remove_poll ((GSource *) watch, &watch->readfd);
3241 keep_running = (watch->writefd.events != 0);
3244 if (watch->funcs.error_full)
3245 GST_RTSP_CHECK (watch->funcs.error_full (watch, res, &watch->message,
3246 0, watch->user_data), error);
3254 watch->writefd.events = 0;
3255 watch->writefd.revents = 0;
3256 g_source_remove_poll ((GSource *) watch, &watch->writefd);
3257 keep_running = (watch->readfd.events != 0);
3260 if (watch->funcs.error_full)
3261 GST_RTSP_CHECK (watch->funcs.error_full (watch, res, NULL,
3262 watch->write_id, watch->user_data), error);
3270 if (watch->funcs.error)
3271 watch->funcs.error (watch, res, watch->user_data);
3273 return keep_running;
3278 gst_rtsp_rec_free (gpointer data)
3280 GstRTSPRec *rec = data;
3283 g_slice_free (GstRTSPRec, rec);
3287 gst_rtsp_source_finalize (GSource * source)
3289 GstRTSPWatch *watch = (GstRTSPWatch *) source;
3291 build_reset (&watch->builder);
3292 gst_rtsp_message_unset (&watch->message);
3294 g_queue_foreach (watch->messages, (GFunc) gst_rtsp_rec_free, NULL);
3295 g_queue_free (watch->messages);
3296 watch->messages = NULL;
3297 g_free (watch->write_data);
3299 g_mutex_free (watch->mutex);
3302 watch->notify (watch->user_data);
3305 static GSourceFuncs gst_rtsp_source_funcs = {
3306 gst_rtsp_source_prepare,
3307 gst_rtsp_source_check,
3308 gst_rtsp_source_dispatch,
3309 gst_rtsp_source_finalize,
3315 * gst_rtsp_watch_new:
3316 * @conn: a #GstRTSPConnection
3317 * @funcs: watch functions
3318 * @user_data: user data to pass to @funcs
3319 * @notify: notify when @user_data is not referenced anymore
3321 * Create a watch object for @conn. The functions provided in @funcs will be
3322 * called with @user_data when activity happened on the watch.
3324 * The new watch is usually created so that it can be attached to a
3325 * maincontext with gst_rtsp_watch_attach().
3327 * @conn must exist for the entire lifetime of the watch.
3329 * Returns: a #GstRTSPWatch that can be used for asynchronous RTSP
3330 * communication. Free with gst_rtsp_watch_unref () after usage.
3335 gst_rtsp_watch_new (GstRTSPConnection * conn,
3336 GstRTSPWatchFuncs * funcs, gpointer user_data, GDestroyNotify notify)
3338 GstRTSPWatch *result;
3340 g_return_val_if_fail (conn != NULL, NULL);
3341 g_return_val_if_fail (funcs != NULL, NULL);
3342 g_return_val_if_fail (conn->readfd != NULL, NULL);
3343 g_return_val_if_fail (conn->writefd != NULL, NULL);
3345 result = (GstRTSPWatch *) g_source_new (&gst_rtsp_source_funcs,
3346 sizeof (GstRTSPWatch));
3348 result->conn = conn;
3349 result->builder.state = STATE_START;
3351 result->mutex = g_mutex_new ();
3352 result->messages = g_queue_new ();
3354 result->readfd.fd = -1;
3355 result->writefd.fd = -1;
3357 gst_rtsp_watch_reset (result);
3359 result->funcs = *funcs;
3360 result->user_data = user_data;
3361 result->notify = notify;
3367 * gst_rtsp_watch_reset:
3368 * @watch: a #GstRTSPWatch
3370 * Reset @watch, this is usually called after gst_rtsp_connection_do_tunnel()
3371 * when the file descriptors of the connection might have changed.
3376 gst_rtsp_watch_reset (GstRTSPWatch * watch)
3378 if (watch->readfd.fd != -1)
3379 g_source_remove_poll ((GSource *) watch, &watch->readfd);
3380 if (watch->writefd.fd != -1)
3381 g_source_remove_poll ((GSource *) watch, &watch->writefd);
3383 watch->readfd.fd = watch->conn->readfd->fd;
3384 watch->readfd.events = READ_COND;
3385 watch->readfd.revents = 0;
3387 watch->writefd.fd = watch->conn->writefd->fd;
3388 watch->writefd.events = WRITE_ERR;
3389 watch->writefd.revents = 0;
3391 if (watch->readfd.fd != -1)
3392 g_source_add_poll ((GSource *) watch, &watch->readfd);
3393 if (watch->writefd.fd != -1)
3394 g_source_add_poll ((GSource *) watch, &watch->writefd);
3398 * gst_rtsp_watch_attach:
3399 * @watch: a #GstRTSPWatch
3400 * @context: a GMainContext (if NULL, the default context will be used)
3402 * Adds a #GstRTSPWatch to a context so that it will be executed within that context.
3404 * Returns: the ID (greater than 0) for the watch within the GMainContext.
3409 gst_rtsp_watch_attach (GstRTSPWatch * watch, GMainContext * context)
3411 g_return_val_if_fail (watch != NULL, 0);
3413 return g_source_attach ((GSource *) watch, context);
3417 * gst_rtsp_watch_unref:
3418 * @watch: a #GstRTSPWatch
3420 * Decreases the reference count of @watch by one. If the resulting reference
3421 * count is zero the watch and associated memory will be destroyed.
3426 gst_rtsp_watch_unref (GstRTSPWatch * watch)
3428 g_return_if_fail (watch != NULL);
3430 g_source_unref ((GSource *) watch);
3434 * gst_rtsp_watch_write_data:
3435 * @watch: a #GstRTSPWatch
3436 * @data: the data to queue
3437 * @size: the size of @data
3438 * @id: location for a message ID or %NULL
3440 * Write @data using the connection of the @watch. If it cannot be sent
3441 * immediately, it will be queued for transmission in @watch. The contents of
3442 * @message will then be serialized and transmitted when the connection of the
3443 * @watch becomes writable. In case the @message is queued, the ID returned in
3444 * @id will be non-zero and used as the ID argument in the message_sent
3447 * This function will take ownership of @data and g_free() it after use.
3449 * Returns: #GST_RTSP_OK on success.
3454 gst_rtsp_watch_write_data (GstRTSPWatch * watch, const guint8 * data,
3455 guint size, guint * id)
3460 GMainContext *context = NULL;
3462 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3463 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
3464 g_return_val_if_fail (size != 0, GST_RTSP_EINVAL);
3466 g_mutex_lock (watch->mutex);
3468 /* try to send the message synchronously first */
3469 if (watch->messages->length == 0) {
3470 res = write_bytes (watch->writefd.fd, data, &off, size);
3471 if (res != GST_RTSP_EINTR) {
3474 g_free ((gpointer) data);
3479 /* make a record with the data and id for sending async */
3480 rec = g_slice_new (GstRTSPRec);
3482 rec->data = (guint8 *) data;
3485 rec->data = g_memdup (data + off, size - off);
3486 rec->size = size - off;
3487 g_free ((gpointer) data);
3491 /* make sure rec->id is never 0 */
3492 rec->id = ++watch->id;
3493 } while (G_UNLIKELY (rec->id == 0));
3495 /* add the record to a queue. FIXME we would like to have an upper limit here */
3496 g_queue_push_head (watch->messages, rec);
3498 /* make sure the main context will now also check for writability on the
3500 if (watch->writefd.events != WRITE_COND) {
3501 watch->writefd.events = WRITE_COND;
3502 context = ((GSource *) watch)->context;
3510 g_mutex_unlock (watch->mutex);
3513 g_main_context_wakeup (context);
3519 * gst_rtsp_watch_send_message:
3520 * @watch: a #GstRTSPWatch
3521 * @message: a #GstRTSPMessage
3522 * @id: location for a message ID or %NULL
3524 * Send a @message using the connection of the @watch. If it cannot be sent
3525 * immediately, it will be queued for transmission in @watch. The contents of
3526 * @message will then be serialized and transmitted when the connection of the
3527 * @watch becomes writable. In case the @message is queued, the ID returned in
3528 * @id will be non-zero and used as the ID argument in the message_sent
3531 * Returns: #GST_RTSP_OK on success.
3536 gst_rtsp_watch_send_message (GstRTSPWatch * watch, GstRTSPMessage * message,
3542 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3543 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3545 /* make a record with the message as a string and id */
3546 str = message_to_string (watch->conn, message);
3548 return gst_rtsp_watch_write_data (watch,
3549 (guint8 *) g_string_free (str, FALSE), size, id);
3553 * gst_rtsp_watch_queue_data:
3554 * @watch: a #GstRTSPWatch
3555 * @data: the data to queue
3556 * @size: the size of @data
3558 * Queue @data for transmission in @watch. It will be transmitted when the
3559 * connection of the @watch becomes writable.
3561 * This function will take ownership of @data and g_free() it after use.
3563 * The return value of this function will be used as the id argument in the
3564 * message_sent callback.
3566 * Deprecated: Use gst_rtsp_watch_write_data()
3572 #ifndef GST_REMOVE_DEPRECATED
3573 #ifdef GST_DISABLE_DEPRECATED
3575 gst_rtsp_watch_queue_data (GstRTSPWatch * watch, const guint8 * data,
3579 gst_rtsp_watch_queue_data (GstRTSPWatch * watch, const guint8 * data,
3583 GMainContext *context = NULL;
3585 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3586 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
3587 g_return_val_if_fail (size != 0, GST_RTSP_EINVAL);
3589 g_mutex_lock (watch->mutex);
3591 /* make a record with the data and id */
3592 rec = g_slice_new (GstRTSPRec);
3593 rec->data = (guint8 *) data;
3596 /* make sure rec->id is never 0 */
3597 rec->id = ++watch->id;
3598 } while (G_UNLIKELY (rec->id == 0));
3600 /* add the record to a queue. FIXME we would like to have an upper limit here */
3601 g_queue_push_head (watch->messages, rec);
3603 /* make sure the main context will now also check for writability on the
3605 if (watch->writefd.events != WRITE_COND) {
3606 watch->writefd.events = WRITE_COND;
3607 context = ((GSource *) watch)->context;
3609 g_mutex_unlock (watch->mutex);
3612 g_main_context_wakeup (context);
3616 #endif /* GST_REMOVE_DEPRECATED */
3619 * gst_rtsp_watch_queue_message:
3620 * @watch: a #GstRTSPWatch
3621 * @message: a #GstRTSPMessage
3623 * Queue a @message for transmission in @watch. The contents of this
3624 * message will be serialized and transmitted when the connection of the
3625 * @watch becomes writable.
3627 * The return value of this function will be used as the id argument in the
3628 * message_sent callback.
3630 * Deprecated: Use gst_rtsp_watch_send_message()
3636 #ifndef GST_REMOVE_DEPRECATED
3637 #ifdef GST_DISABLE_DEPRECATED
3639 gst_rtsp_watch_queue_message (GstRTSPWatch * watch, GstRTSPMessage * message);
3642 gst_rtsp_watch_queue_message (GstRTSPWatch * watch, GstRTSPMessage * message)
3647 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3648 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3650 /* make a record with the message as a string and id */
3651 str = message_to_string (watch->conn, message);
3653 return gst_rtsp_watch_queue_data (watch,
3654 (guint8 *) g_string_free (str, FALSE), size);
3656 #endif /* GST_REMOVE_DEPRECATED */