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"
96 #include "gst/glib-compat-private.h"
101 struct sockaddr_in sa_in;
102 struct sockaddr_in6 sa_in6;
103 struct sockaddr_storage sa_stor;
110 guchar out[3]; /* the size must be evenly divisible by 3 */
116 #define SEND_FLAGS MSG_NOSIGNAL
122 #define READ_SOCKET(fd, buf, len) recv (fd, (char *)buf, len, 0)
123 #define WRITE_SOCKET(fd, buf, len) send (fd, (const char *)buf, len, SEND_FLAGS)
124 #define SETSOCKOPT(sock, level, name, val, len) setsockopt (sock, level, name, (const char *)val, len)
125 #define CLOSE_SOCKET(sock) closesocket (sock)
126 #define ERRNO_IS_EAGAIN (WSAGetLastError () == WSAEWOULDBLOCK)
127 #define ERRNO_IS_EINTR (WSAGetLastError () == WSAEINTR)
128 /* According to Microsoft's connect() documentation this one returns
129 * WSAEWOULDBLOCK and not WSAEINPROGRESS. */
130 #define ERRNO_IS_EINPROGRESS (WSAGetLastError () == WSAEWOULDBLOCK)
132 #define READ_SOCKET(fd, buf, len) read (fd, buf, len)
133 #define WRITE_SOCKET(fd, buf, len) send (fd, buf, len, SEND_FLAGS)
134 #define SETSOCKOPT(sock, level, name, val, len) setsockopt (sock, level, name, val, len)
135 #define CLOSE_SOCKET(sock) close (sock)
136 #define ERRNO_IS_EAGAIN (errno == EAGAIN)
137 #define ERRNO_IS_EINTR (errno == EINTR)
138 #define ERRNO_IS_EINPROGRESS (errno == EINPROGRESS)
141 #define ADD_POLLFD(fdset, pfd, fd) \
144 gst_poll_add_fd (fdset, pfd); \
147 #define REMOVE_POLLFD(fdset, pfd) \
149 if ((pfd)->fd != -1) { \
150 GST_DEBUG ("remove fd %d", (pfd)->fd); \
151 gst_poll_remove_fd (fdset, pfd); \
152 CLOSE_SOCKET ((pfd)->fd); \
162 TUNNEL_STATE_COMPLETE
163 } GstRTSPTunnelState;
165 #define TUNNELID_LEN 24
167 struct _GstRTSPConnection
170 /* URL for the connection */
173 /* connection state */
180 gboolean manual_http;
182 gchar tunnelid[TUNNELID_LEN];
184 GstRTSPTunnelState tstate;
191 gchar *initial_buffer;
192 gsize initial_buffer_offset;
195 gint cseq; /* sequence number */
196 gchar session_id[512]; /* session id */
197 gint timeout; /* session timeout in seconds */
198 GTimer *timer; /* timeout timer */
201 GstRTSPAuthMethod auth_method;
204 GHashTable *auth_params;
225 READ_AHEAD_EOH = -1, /* end of headers */
226 READ_AHEAD_CRLF = -2,
227 READ_AHEAD_CRLFCR = -3
230 /* a structure for constructing RTSPMessages */
234 GstRTSPResult status;
244 build_reset (GstRTSPBuilder * builder)
246 g_free (builder->body_data);
247 memset (builder, 0, sizeof (GstRTSPBuilder));
251 * gst_rtsp_connection_create:
252 * @url: a #GstRTSPUrl
253 * @conn: storage for a #GstRTSPConnection
255 * Create a newly allocated #GstRTSPConnection from @url and store it in @conn.
256 * The connection will not yet attempt to connect to @url, use
257 * gst_rtsp_connection_connect().
259 * A copy of @url will be made.
261 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
264 gst_rtsp_connection_create (const GstRTSPUrl * url, GstRTSPConnection ** conn)
266 GstRTSPConnection *newconn;
272 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
275 error = WSAStartup (0x0202, &w);
280 if (w.wVersion != 0x0202)
284 newconn = g_new0 (GstRTSPConnection, 1);
286 if ((newconn->fdset = gst_poll_new (TRUE)) == NULL)
289 newconn->url = gst_rtsp_url_copy (url);
290 newconn->fd0.fd = -1;
291 newconn->fd1.fd = -1;
292 newconn->timer = g_timer_new ();
293 newconn->timeout = 60;
296 newconn->auth_method = GST_RTSP_AUTH_NONE;
297 newconn->username = NULL;
298 newconn->passwd = NULL;
299 newconn->auth_params = NULL;
309 g_warning ("Error %d on WSAStartup", error);
310 return GST_RTSP_EWSASTART;
314 g_warning ("Windows sockets are not version 0x202 (current 0x%x)",
317 return GST_RTSP_EWSAVERSION;
326 return GST_RTSP_ESYS;
331 * gst_rtsp_connection_create_from_fd:
332 * @fd: a file descriptor
333 * @ip: the IP address of the other end
334 * @port: the port used by the other end
335 * @initial_buffer: data already read from @fd
336 * @conn: storage for a #GstRTSPConnection
338 * Create a new #GstRTSPConnection for handling communication on the existing
339 * file descriptor @fd. The @initial_buffer contains any data already read from
340 * @fd which should be used before starting to read new data.
342 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
347 gst_rtsp_connection_create_from_fd (gint fd, const gchar * ip, guint16 port,
348 const gchar * initial_buffer, GstRTSPConnection ** conn)
350 GstRTSPConnection *newconn = NULL;
357 g_return_val_if_fail (fd >= 0, GST_RTSP_EINVAL);
358 g_return_val_if_fail (ip != NULL, GST_RTSP_EINVAL);
359 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
361 /* set to non-blocking mode so that we can cancel the communication */
363 fcntl (fd, F_SETFL, O_NONBLOCK);
365 ioctlsocket (fd, FIONBIO, &flags);
366 #endif /* G_OS_WIN32 */
368 /* create a url for the client address */
369 url = g_new0 (GstRTSPUrl, 1);
370 url->host = g_strdup (ip);
373 /* now create the connection object */
374 GST_RTSP_CHECK (gst_rtsp_connection_create (url, &newconn), newconn_failed);
375 gst_rtsp_url_free (url);
377 ADD_POLLFD (newconn->fdset, &newconn->fd0, fd);
379 /* both read and write initially */
380 newconn->readfd = &newconn->fd0;
381 newconn->writefd = &newconn->fd0;
383 newconn->ip = g_strdup (ip);
385 newconn->initial_buffer = g_strdup (initial_buffer);
394 gst_rtsp_url_free (url);
400 * gst_rtsp_connection_accept:
402 * @conn: storage for a #GstRTSPConnection
404 * Accept a new connection on @sock and create a new #GstRTSPConnection for
405 * handling communication on new socket.
407 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
412 gst_rtsp_connection_accept (gint sock, GstRTSPConnection ** conn)
415 union gst_sockaddr sa;
416 socklen_t slen = sizeof (sa);
417 gchar ip[INET6_ADDRSTRLEN];
420 g_return_val_if_fail (sock >= 0, GST_RTSP_EINVAL);
421 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
423 memset (&sa, 0, slen);
426 fd = accept (sock, &sa.sa, &slen);
428 fd = accept (sock, &sa.sa, (gint *) & slen);
429 #endif /* G_OS_WIN32 */
433 if (getnameinfo (&sa.sa, slen, ip, sizeof (ip), NULL, 0, NI_NUMERICHOST) != 0)
434 goto getnameinfo_failed;
436 if (sa.sa.sa_family == AF_INET)
437 port = sa.sa_in.sin_port;
438 else if (sa.sa.sa_family == AF_INET6)
439 port = sa.sa_in6.sin6_port;
443 return gst_rtsp_connection_create_from_fd (fd, ip, port, NULL, conn);
448 return GST_RTSP_ESYS;
454 return GST_RTSP_ERROR;
459 do_resolve (const gchar * host)
461 gchar ip[INET6_ADDRSTRLEN];
462 struct addrinfo *aires, hints;
466 memset (&hints, 0, sizeof (struct addrinfo));
467 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
468 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
469 hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
470 hints.ai_protocol = 0; /* Any protocol */
471 hints.ai_canonname = NULL;
472 hints.ai_addr = NULL;
473 hints.ai_next = NULL;
475 aierr = getaddrinfo (host, NULL, &hints, &aires);
479 for (ai = aires; ai; ai = ai->ai_next) {
480 if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) {
487 aierr = getnameinfo (ai->ai_addr, ai->ai_addrlen, ip, sizeof (ip), NULL, 0,
488 NI_NUMERICHOST | NI_NUMERICSERV);
492 freeaddrinfo (aires);
494 return g_strdup (ip);
499 GST_ERROR ("no addrinfo found for %s: %s", host, gai_strerror (aierr));
504 GST_ERROR ("no family found for %s", host);
505 freeaddrinfo (aires);
510 GST_ERROR ("no address found for %s: %s", host, gai_strerror (aierr));
511 freeaddrinfo (aires);
517 do_connect (const gchar * ip, guint16 port, GstPollFD * fdout,
518 GstPoll * fdset, GTimeVal * timeout)
521 struct addrinfo hints;
522 struct addrinfo *aires;
525 gchar service[NI_MAXSERV];
528 unsigned long flags = 1;
529 #endif /* G_OS_WIN32 */
533 memset (&hints, 0, sizeof hints);
534 hints.ai_flags = AI_NUMERICHOST;
535 hints.ai_family = AF_UNSPEC;
536 hints.ai_socktype = SOCK_STREAM;
537 g_snprintf (service, sizeof (service) - 1, "%hu", port);
538 service[sizeof (service) - 1] = '\0';
540 aierr = getaddrinfo (ip, service, &hints, &aires);
544 for (ai = aires; ai; ai = ai->ai_next) {
545 if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) {
552 fd = socket (ai->ai_family, SOCK_STREAM, 0);
556 /* set to non-blocking mode so that we can cancel the connect */
558 fcntl (fd, F_SETFL, O_NONBLOCK);
560 ioctlsocket (fd, FIONBIO, &flags);
561 #endif /* G_OS_WIN32 */
563 /* add the socket to our fdset */
564 ADD_POLLFD (fdset, fdout, fd);
566 /* we are going to connect ASYNC now */
567 ret = connect (fd, ai->ai_addr, ai->ai_addrlen);
570 if (!ERRNO_IS_EINPROGRESS)
573 /* wait for connect to complete up to the specified timeout or until we got
575 gst_poll_fd_ctl_write (fdset, fdout, TRUE);
577 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
580 retval = gst_poll_wait (fdset, to);
581 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
585 else if (retval == -1)
588 /* we can still have an error connecting on windows */
589 if (gst_poll_fd_has_error (fdset, fdout)) {
590 socklen_t len = sizeof (errno);
592 getsockopt (fd, SOL_SOCKET, SO_ERROR, &errno, &len);
594 getsockopt (fd, SOL_SOCKET, SO_ERROR, (char *) &errno, &len);
599 /* osx wakes up select with POLLOUT if the connection is refused... */
600 socklen_t len = sizeof (errno);
601 getsockopt (fd, SOL_SOCKET, SO_ERROR, (char *) &errno, &len);
607 gst_poll_fd_ignored (fdset, fdout);
610 freeaddrinfo (aires);
617 GST_ERROR ("no addrinfo found for %s: %s", ip, gai_strerror (aierr));
618 return GST_RTSP_ERROR;
622 GST_ERROR ("no family found for %s", ip);
623 freeaddrinfo (aires);
624 return GST_RTSP_ERROR;
628 GST_ERROR ("no socket %d (%s)", errno, g_strerror (errno));
629 freeaddrinfo (aires);
630 return GST_RTSP_ESYS;
634 GST_ERROR ("system error %d (%s)", errno, g_strerror (errno));
635 REMOVE_POLLFD (fdset, fdout);
636 freeaddrinfo (aires);
637 return GST_RTSP_ESYS;
641 GST_ERROR ("timeout");
642 REMOVE_POLLFD (fdset, fdout);
643 freeaddrinfo (aires);
644 return GST_RTSP_ETIMEOUT;
649 setup_tunneling (GstRTSPConnection * conn, GTimeVal * timeout)
656 guint16 port, url_port;
660 GstRTSPMessage response;
663 memset (&response, 0, sizeof (response));
664 gst_rtsp_message_init (&response);
666 /* create a random sessionid */
667 for (i = 0; i < TUNNELID_LEN; i++)
668 conn->tunnelid[i] = g_random_int_range ('a', 'z');
669 conn->tunnelid[TUNNELID_LEN - 1] = '\0';
672 /* get the port from the url */
673 gst_rtsp_url_get_port (url, &url_port);
675 if (conn->proxy_host) {
676 uri = g_strdup_printf ("http://%s:%d%s%s%s", url->host, url_port,
677 url->abspath, url->query ? "?" : "", url->query ? url->query : "");
678 hostparam = g_strdup_printf ("%s:%d", url->host, url_port);
679 ip = conn->proxy_host;
680 port = conn->proxy_port;
682 uri = g_strdup_printf ("%s%s%s", url->abspath, url->query ? "?" : "",
683 url->query ? url->query : "");
689 /* create the GET request for the read connection */
690 GST_RTSP_CHECK (gst_rtsp_message_new_request (&msg, GST_RTSP_GET, uri),
692 msg->type = GST_RTSP_MESSAGE_HTTP_REQUEST;
694 if (hostparam != NULL)
695 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_HOST, hostparam);
696 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_X_SESSIONCOOKIE,
698 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_ACCEPT,
699 "application/x-rtsp-tunnelled");
700 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CACHE_CONTROL, "no-cache");
701 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_PRAGMA, "no-cache");
703 /* we start by writing to this fd */
704 conn->writefd = &conn->fd0;
706 /* we need to temporarily set conn->tunneled to FALSE to prevent the HTTP
707 * request from being base64 encoded */
708 conn->tunneled = FALSE;
709 GST_RTSP_CHECK (gst_rtsp_connection_send (conn, msg, timeout), write_failed);
710 gst_rtsp_message_free (msg);
711 conn->tunneled = TRUE;
713 /* receive the response to the GET request */
714 /* we need to temporarily set manual_http to TRUE since
715 * gst_rtsp_connection_receive() will treat the HTTP response as a parsing
716 * failure otherwise */
717 old_http = conn->manual_http;
718 conn->manual_http = TRUE;
719 GST_RTSP_CHECK (gst_rtsp_connection_receive (conn, &response, timeout),
721 conn->manual_http = old_http;
723 if (response.type != GST_RTSP_MESSAGE_HTTP_RESPONSE ||
724 response.type_data.response.code != GST_RTSP_STS_OK)
727 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_X_SERVER_IP_ADDRESS,
728 &value, 0) == GST_RTSP_OK) {
729 if (conn->proxy_host) {
730 /* if we use a proxy we need to change the destination url */
732 url->host = g_strdup (value);
734 hostparam = g_strdup_printf ("%s:%d", url->host, url_port);
736 /* and resolve the new ip address */
737 if (!(ip = do_resolve (value)))
744 /* connect to the host/port */
745 res = do_connect (ip, port, &conn->fd1, conn->fdset, timeout);
746 if (res != GST_RTSP_OK)
749 /* this is now our writing socket */
750 conn->writefd = &conn->fd1;
752 /* create the POST request for the write connection */
753 GST_RTSP_CHECK (gst_rtsp_message_new_request (&msg, GST_RTSP_POST, uri),
755 msg->type = GST_RTSP_MESSAGE_HTTP_REQUEST;
757 if (hostparam != NULL)
758 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_HOST, hostparam);
759 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_X_SESSIONCOOKIE,
761 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_ACCEPT,
762 "application/x-rtsp-tunnelled");
763 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CACHE_CONTROL, "no-cache");
764 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_PRAGMA, "no-cache");
765 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_EXPIRES,
766 "Sun, 9 Jan 1972 00:00:00 GMT");
767 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CONTENT_LENGTH, "32767");
769 /* we need to temporarily set conn->tunneled to FALSE to prevent the HTTP
770 * request from being base64 encoded */
771 conn->tunneled = FALSE;
772 GST_RTSP_CHECK (gst_rtsp_connection_send (conn, msg, timeout), write_failed);
773 gst_rtsp_message_free (msg);
774 conn->tunneled = TRUE;
777 gst_rtsp_message_unset (&response);
786 GST_ERROR ("failed to create request (%d)", res);
791 GST_ERROR ("write failed (%d)", res);
792 gst_rtsp_message_free (msg);
793 conn->tunneled = TRUE;
798 GST_ERROR ("read failed (%d)", res);
799 conn->manual_http = FALSE;
804 GST_ERROR ("got failure response %d %s", response.type_data.response.code,
805 response.type_data.response.reason);
806 res = GST_RTSP_ERROR;
811 GST_ERROR ("could not resolve %s", conn->ip);
817 GST_ERROR ("failed to connect");
823 * gst_rtsp_connection_connect:
824 * @conn: a #GstRTSPConnection
825 * @timeout: a #GTimeVal timeout
827 * Attempt to connect to the url of @conn made with
828 * gst_rtsp_connection_create(). If @timeout is #NULL this function can block
829 * forever. If @timeout contains a valid timeout, this function will return
830 * #GST_RTSP_ETIMEOUT after the timeout expired.
832 * This function can be cancelled with gst_rtsp_connection_flush().
834 * Returns: #GST_RTSP_OK when a connection could be made.
837 gst_rtsp_connection_connect (GstRTSPConnection * conn, GTimeVal * timeout)
844 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
845 g_return_val_if_fail (conn->url != NULL, GST_RTSP_EINVAL);
846 g_return_val_if_fail (conn->fd0.fd < 0, GST_RTSP_EINVAL);
850 if (conn->proxy_host && conn->tunneled) {
851 if (!(ip = do_resolve (conn->proxy_host))) {
852 GST_ERROR ("could not resolve %s", conn->proxy_host);
855 port = conn->proxy_port;
856 g_free (conn->proxy_host);
857 conn->proxy_host = ip;
859 if (!(ip = do_resolve (url->host))) {
860 GST_ERROR ("could not resolve %s", url->host);
863 /* get the port from the url */
864 gst_rtsp_url_get_port (url, &port);
870 /* connect to the host/port */
871 res = do_connect (ip, port, &conn->fd0, conn->fdset, timeout);
872 if (res != GST_RTSP_OK)
875 /* this is our read URL */
876 conn->readfd = &conn->fd0;
878 if (conn->tunneled) {
879 res = setup_tunneling (conn, timeout);
880 if (res != GST_RTSP_OK)
881 goto tunneling_failed;
883 conn->writefd = &conn->fd0;
890 return GST_RTSP_ENET;
894 GST_ERROR ("failed to connect");
899 GST_ERROR ("failed to setup tunneling");
905 auth_digest_compute_hex_urp (const gchar * username,
906 const gchar * realm, const gchar * password, gchar hex_urp[33])
908 GChecksum *md5_context = g_checksum_new (G_CHECKSUM_MD5);
909 const gchar *digest_string;
911 g_checksum_update (md5_context, (const guchar *) username, strlen (username));
912 g_checksum_update (md5_context, (const guchar *) ":", 1);
913 g_checksum_update (md5_context, (const guchar *) realm, strlen (realm));
914 g_checksum_update (md5_context, (const guchar *) ":", 1);
915 g_checksum_update (md5_context, (const guchar *) password, strlen (password));
916 digest_string = g_checksum_get_string (md5_context);
918 memset (hex_urp, 0, 33);
919 memcpy (hex_urp, digest_string, strlen (digest_string));
921 g_checksum_free (md5_context);
925 auth_digest_compute_response (const gchar * method,
926 const gchar * uri, const gchar * hex_a1, const gchar * nonce,
929 char hex_a2[33] = { 0, };
930 GChecksum *md5_context = g_checksum_new (G_CHECKSUM_MD5);
931 const gchar *digest_string;
934 g_checksum_update (md5_context, (const guchar *) method, strlen (method));
935 g_checksum_update (md5_context, (const guchar *) ":", 1);
936 g_checksum_update (md5_context, (const guchar *) uri, strlen (uri));
937 digest_string = g_checksum_get_string (md5_context);
938 memcpy (hex_a2, digest_string, strlen (digest_string));
941 g_checksum_reset (md5_context);
942 g_checksum_update (md5_context, (const guchar *) hex_a1, strlen (hex_a1));
943 g_checksum_update (md5_context, (const guchar *) ":", 1);
944 g_checksum_update (md5_context, (const guchar *) nonce, strlen (nonce));
945 g_checksum_update (md5_context, (const guchar *) ":", 1);
947 g_checksum_update (md5_context, (const guchar *) hex_a2, 32);
948 digest_string = g_checksum_get_string (md5_context);
949 memset (response, 0, 33);
950 memcpy (response, digest_string, strlen (digest_string));
952 g_checksum_free (md5_context);
956 add_auth_header (GstRTSPConnection * conn, GstRTSPMessage * message)
958 switch (conn->auth_method) {
959 case GST_RTSP_AUTH_BASIC:{
964 if (conn->username == NULL || conn->passwd == NULL)
967 user_pass = g_strdup_printf ("%s:%s", conn->username, conn->passwd);
968 user_pass64 = g_base64_encode ((guchar *) user_pass, strlen (user_pass));
969 auth_string = g_strdup_printf ("Basic %s", user_pass64);
971 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
975 g_free (user_pass64);
978 case GST_RTSP_AUTH_DIGEST:{
979 gchar response[33], hex_urp[33];
980 gchar *auth_string, *auth_string2;
987 /* we need to have some params set */
988 if (conn->auth_params == NULL || conn->username == NULL ||
989 conn->passwd == NULL)
992 /* we need the realm and nonce */
993 realm = (gchar *) g_hash_table_lookup (conn->auth_params, "realm");
994 nonce = (gchar *) g_hash_table_lookup (conn->auth_params, "nonce");
995 if (realm == NULL || nonce == NULL)
998 auth_digest_compute_hex_urp (conn->username, realm, conn->passwd,
1001 method = gst_rtsp_method_as_text (message->type_data.request.method);
1002 uri = message->type_data.request.uri;
1004 /* Assume no qop, algorithm=md5, stale=false */
1005 /* For algorithm MD5, a1 = urp. */
1006 auth_digest_compute_response (method, uri, hex_urp, nonce, response);
1007 auth_string = g_strdup_printf ("Digest username=\"%s\", "
1008 "realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"",
1009 conn->username, realm, nonce, uri, response);
1011 opaque = (gchar *) g_hash_table_lookup (conn->auth_params, "opaque");
1013 auth_string2 = g_strdup_printf ("%s, opaque=\"%s\"", auth_string,
1015 g_free (auth_string);
1016 auth_string = auth_string2;
1018 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
1029 gen_date_string (gchar * date_string, guint len)
1031 static const char wkdays[7][4] =
1032 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
1033 static const char months[12][4] =
1034 { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
1042 #ifdef HAVE_GMTIME_R
1048 g_snprintf (date_string, len, "%s, %02d %s %04d %02d:%02d:%02d GMT",
1049 wkdays[tm.tm_wday], tm.tm_mday, months[tm.tm_mon], tm.tm_year + 1900,
1050 tm.tm_hour, tm.tm_min, tm.tm_sec);
1053 static GstRTSPResult
1054 write_bytes (gint fd, const guint8 * buffer, guint * idx, guint size)
1058 if (G_UNLIKELY (*idx > size))
1059 return GST_RTSP_ERROR;
1066 r = WRITE_SOCKET (fd, &buffer[*idx], left);
1067 if (G_UNLIKELY (r == 0)) {
1068 return GST_RTSP_EINTR;
1069 } else if (G_UNLIKELY (r < 0)) {
1070 if (ERRNO_IS_EAGAIN)
1071 return GST_RTSP_EINTR;
1072 if (!ERRNO_IS_EINTR)
1073 return GST_RTSP_ESYS;
1083 fill_raw_bytes (GstRTSPConnection * conn, guint8 * buffer, guint size)
1087 if (G_UNLIKELY (conn->initial_buffer != NULL)) {
1088 gsize left = strlen (&conn->initial_buffer[conn->initial_buffer_offset]);
1090 out = MIN (left, size);
1091 memcpy (buffer, &conn->initial_buffer[conn->initial_buffer_offset], out);
1093 if (left == (gsize) out) {
1094 g_free (conn->initial_buffer);
1095 conn->initial_buffer = NULL;
1096 conn->initial_buffer_offset = 0;
1098 conn->initial_buffer_offset += out;
1101 if (G_LIKELY (size > (guint) out)) {
1104 r = READ_SOCKET (conn->readfd->fd, &buffer[out], size - out);
1116 fill_bytes (GstRTSPConnection * conn, guint8 * buffer, guint size)
1118 DecodeCtx *ctx = conn->ctxp;
1123 guint8 in[sizeof (ctx->out) * 4 / 3];
1126 while (size > 0 && ctx->cout < ctx->coutl) {
1127 /* we have some leftover bytes */
1128 *buffer++ = ctx->out[ctx->cout++];
1133 /* got what we needed? */
1137 /* try to read more bytes */
1138 r = fill_raw_bytes (conn, in, sizeof (in));
1147 g_base64_decode_step ((gchar *) in, r, ctx->out, &ctx->state,
1151 out = fill_raw_bytes (conn, buffer, size);
1157 static GstRTSPResult
1158 read_bytes (GstRTSPConnection * conn, guint8 * buffer, guint * idx, guint size)
1162 if (G_UNLIKELY (*idx > size))
1163 return GST_RTSP_ERROR;
1170 r = fill_bytes (conn, &buffer[*idx], left);
1171 if (G_UNLIKELY (r == 0)) {
1172 return GST_RTSP_EEOF;
1173 } else if (G_UNLIKELY (r < 0)) {
1174 if (ERRNO_IS_EAGAIN)
1175 return GST_RTSP_EINTR;
1176 if (!ERRNO_IS_EINTR)
1177 return GST_RTSP_ESYS;
1186 /* The code below tries to handle clients using \r, \n or \r\n to indicate the
1187 * end of a line. It even does its best to handle clients which mix them (even
1188 * though this is a really stupid idea (tm).) It also handles Line White Space
1189 * (LWS), where a line end followed by whitespace is considered LWS. This is
1190 * the method used in RTSP (and HTTP) to break long lines.
1192 static GstRTSPResult
1193 read_line (GstRTSPConnection * conn, guint8 * buffer, guint * idx, guint size)
1199 if (conn->read_ahead == READ_AHEAD_EOH) {
1200 /* the last call to read_line() already determined that we have reached
1201 * the end of the headers, so convey that information now */
1202 conn->read_ahead = 0;
1204 } else if (conn->read_ahead == READ_AHEAD_CRLF) {
1205 /* the last call to read_line() left off after having read \r\n */
1207 } else if (conn->read_ahead == READ_AHEAD_CRLFCR) {
1208 /* the last call to read_line() left off after having read \r\n\r */
1210 } else if (conn->read_ahead != 0) {
1211 /* the last call to read_line() left us with a character to start with */
1212 c = (guint8) conn->read_ahead;
1213 conn->read_ahead = 0;
1215 /* read the next character */
1216 r = fill_bytes (conn, &c, 1);
1217 if (G_UNLIKELY (r == 0)) {
1218 return GST_RTSP_EEOF;
1219 } else if (G_UNLIKELY (r < 0)) {
1220 if (ERRNO_IS_EAGAIN)
1221 return GST_RTSP_EINTR;
1222 if (!ERRNO_IS_EINTR)
1223 return GST_RTSP_ESYS;
1228 /* special treatment of line endings */
1229 if (c == '\r' || c == '\n') {
1233 /* need to read ahead one more character to know what to do... */
1234 r = fill_bytes (conn, &read_ahead, 1);
1235 if (G_UNLIKELY (r == 0)) {
1236 return GST_RTSP_EEOF;
1237 } else if (G_UNLIKELY (r < 0)) {
1238 if (ERRNO_IS_EAGAIN) {
1239 /* remember the original character we read and try again next time */
1240 if (conn->read_ahead == 0)
1241 conn->read_ahead = c;
1242 return GST_RTSP_EINTR;
1244 if (!ERRNO_IS_EINTR)
1245 return GST_RTSP_ESYS;
1249 if (read_ahead == ' ' || read_ahead == '\t') {
1250 if (conn->read_ahead == READ_AHEAD_CRLFCR) {
1251 /* got \r\n\r followed by whitespace, treat it as a normal line
1252 * followed by one starting with LWS */
1253 conn->read_ahead = read_ahead;
1256 /* got LWS, change the line ending to a space and continue */
1258 conn->read_ahead = read_ahead;
1260 } else if (conn->read_ahead == READ_AHEAD_CRLFCR) {
1261 if (read_ahead == '\r' || read_ahead == '\n') {
1262 /* got \r\n\r\r or \r\n\r\n, treat it as the end of the headers */
1263 conn->read_ahead = READ_AHEAD_EOH;
1266 /* got \r\n\r followed by something else, this is not really
1267 * supported since we have probably just eaten the first character
1268 * of the body or the next message, so just ignore the second \r
1269 * and live with it... */
1270 conn->read_ahead = read_ahead;
1273 } else if (conn->read_ahead == READ_AHEAD_CRLF) {
1274 if (read_ahead == '\r') {
1275 /* got \r\n\r so far, need one more character... */
1276 conn->read_ahead = READ_AHEAD_CRLFCR;
1278 } else if (read_ahead == '\n') {
1279 /* got \r\n\n, treat it as the end of the headers */
1280 conn->read_ahead = READ_AHEAD_EOH;
1283 /* found the end of a line, keep read_ahead for the next line */
1284 conn->read_ahead = read_ahead;
1287 } else if (c == read_ahead) {
1288 /* got double \r or \n, treat it as the end of the headers */
1289 conn->read_ahead = READ_AHEAD_EOH;
1291 } else if (c == '\r' && read_ahead == '\n') {
1292 /* got \r\n so far, still need more to know what to do... */
1293 conn->read_ahead = READ_AHEAD_CRLF;
1296 /* found the end of a line, keep read_ahead for the next line */
1297 conn->read_ahead = read_ahead;
1302 if (G_LIKELY (*idx < size - 1))
1303 buffer[(*idx)++] = c;
1305 buffer[*idx] = '\0';
1311 * gst_rtsp_connection_write:
1312 * @conn: a #GstRTSPConnection
1313 * @data: the data to write
1314 * @size: the size of @data
1315 * @timeout: a timeout value or #NULL
1317 * Attempt to write @size bytes of @data to the connected @conn, blocking up to
1318 * the specified @timeout. @timeout can be #NULL, in which case this function
1319 * might block forever.
1321 * This function can be cancelled with gst_rtsp_connection_flush().
1323 * Returns: #GST_RTSP_OK on success.
1326 gst_rtsp_connection_write (GstRTSPConnection * conn, const guint8 * data,
1327 guint size, GTimeVal * timeout)
1334 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1335 g_return_val_if_fail (data != NULL || size == 0, GST_RTSP_EINVAL);
1336 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
1338 gst_poll_set_controllable (conn->fdset, TRUE);
1339 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, TRUE);
1340 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, FALSE);
1341 /* clear all previous poll results */
1342 gst_poll_fd_ignored (conn->fdset, conn->writefd);
1343 gst_poll_fd_ignored (conn->fdset, conn->readfd);
1345 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1351 res = write_bytes (conn->writefd->fd, data, &offset, size);
1352 if (G_LIKELY (res == GST_RTSP_OK))
1354 if (G_UNLIKELY (res != GST_RTSP_EINTR))
1357 /* not all is written, wait until we can write more */
1359 retval = gst_poll_wait (conn->fdset, to);
1360 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1362 if (G_UNLIKELY (retval == 0))
1365 if (G_UNLIKELY (retval == -1)) {
1372 /* could also be an error with read socket */
1373 if (gst_poll_fd_has_error (conn->fdset, conn->readfd))
1381 return GST_RTSP_ETIMEOUT;
1385 return GST_RTSP_ESYS;
1389 return GST_RTSP_EINTR;
1393 return GST_RTSP_ENET;
1402 message_to_string (GstRTSPConnection * conn, GstRTSPMessage * message)
1404 GString *str = NULL;
1406 str = g_string_new ("");
1408 switch (message->type) {
1409 case GST_RTSP_MESSAGE_REQUEST:
1410 /* create request string, add CSeq */
1411 g_string_append_printf (str, "%s %s RTSP/1.0\r\n"
1413 gst_rtsp_method_as_text (message->type_data.request.method),
1414 message->type_data.request.uri, conn->cseq++);
1415 /* add session id if we have one */
1416 if (conn->session_id[0] != '\0') {
1417 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
1418 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SESSION,
1421 /* add any authentication headers */
1422 add_auth_header (conn, message);
1424 case GST_RTSP_MESSAGE_RESPONSE:
1425 /* create response string */
1426 g_string_append_printf (str, "RTSP/1.0 %d %s\r\n",
1427 message->type_data.response.code, message->type_data.response.reason);
1429 case GST_RTSP_MESSAGE_HTTP_REQUEST:
1430 /* create request string */
1431 g_string_append_printf (str, "%s %s HTTP/%s\r\n",
1432 gst_rtsp_method_as_text (message->type_data.request.method),
1433 message->type_data.request.uri,
1434 gst_rtsp_version_as_text (message->type_data.request.version));
1435 /* add any authentication headers */
1436 add_auth_header (conn, message);
1438 case GST_RTSP_MESSAGE_HTTP_RESPONSE:
1439 /* create response string */
1440 g_string_append_printf (str, "HTTP/%s %d %s\r\n",
1441 gst_rtsp_version_as_text (message->type_data.request.version),
1442 message->type_data.response.code, message->type_data.response.reason);
1444 case GST_RTSP_MESSAGE_DATA:
1446 guint8 data_header[4];
1448 /* prepare data header */
1449 data_header[0] = '$';
1450 data_header[1] = message->type_data.data.channel;
1451 data_header[2] = (message->body_size >> 8) & 0xff;
1452 data_header[3] = message->body_size & 0xff;
1454 /* create string with header and data */
1455 str = g_string_append_len (str, (gchar *) data_header, 4);
1457 g_string_append_len (str, (gchar *) message->body,
1458 message->body_size);
1462 g_string_free (str, TRUE);
1463 g_return_val_if_reached (NULL);
1467 /* append headers and body */
1468 if (message->type != GST_RTSP_MESSAGE_DATA) {
1469 gchar date_string[100];
1471 gen_date_string (date_string, sizeof (date_string));
1473 /* add date header */
1474 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_DATE, -1);
1475 gst_rtsp_message_add_header (message, GST_RTSP_HDR_DATE, date_string);
1477 /* append headers */
1478 gst_rtsp_message_append_headers (message, str);
1480 /* append Content-Length and body if needed */
1481 if (message->body != NULL && message->body_size > 0) {
1484 len = g_strdup_printf ("%d", message->body_size);
1485 g_string_append_printf (str, "%s: %s\r\n",
1486 gst_rtsp_header_as_text (GST_RTSP_HDR_CONTENT_LENGTH), len);
1488 /* header ends here */
1489 g_string_append (str, "\r\n");
1491 g_string_append_len (str, (gchar *) message->body,
1492 message->body_size);
1494 /* just end headers */
1495 g_string_append (str, "\r\n");
1503 * gst_rtsp_connection_send:
1504 * @conn: a #GstRTSPConnection
1505 * @message: the message to send
1506 * @timeout: a timeout value or #NULL
1508 * Attempt to send @message to the connected @conn, blocking up to
1509 * the specified @timeout. @timeout can be #NULL, in which case this function
1510 * might block forever.
1512 * This function can be cancelled with gst_rtsp_connection_flush().
1514 * Returns: #GST_RTSP_OK on success.
1517 gst_rtsp_connection_send (GstRTSPConnection * conn, GstRTSPMessage * message,
1520 GString *string = NULL;
1525 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1526 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1528 if (G_UNLIKELY (!(string = message_to_string (conn, message))))
1531 if (conn->tunneled) {
1532 str = g_base64_encode ((const guchar *) string->str, string->len);
1533 g_string_free (string, TRUE);
1538 g_string_free (string, FALSE);
1542 res = gst_rtsp_connection_write (conn, (guint8 *) str, len, timeout);
1550 g_warning ("Wrong message");
1551 return GST_RTSP_EINVAL;
1555 static GstRTSPResult
1556 parse_string (gchar * dest, gint size, gchar ** src)
1558 GstRTSPResult res = GST_RTSP_OK;
1563 while (g_ascii_isspace (**src))
1566 while (!g_ascii_isspace (**src) && **src != '\0') {
1568 dest[idx++] = **src;
1570 res = GST_RTSP_EPARSE;
1579 static GstRTSPResult
1580 parse_protocol_version (gchar * protocol, GstRTSPMsgType * type,
1581 GstRTSPVersion * version)
1583 GstRTSPResult res = GST_RTSP_OK;
1586 if (G_LIKELY ((ver = strchr (protocol, '/')) != NULL)) {
1593 /* the version number must be formatted as X.Y with nothing following */
1594 if (sscanf (ver, "%u.%u%c", &major, &minor, &dummychar) != 2)
1595 res = GST_RTSP_EPARSE;
1597 if (g_ascii_strcasecmp (protocol, "RTSP") == 0) {
1598 if (major != 1 || minor != 0) {
1599 *version = GST_RTSP_VERSION_INVALID;
1600 res = GST_RTSP_ERROR;
1602 } else if (g_ascii_strcasecmp (protocol, "HTTP") == 0) {
1603 if (*type == GST_RTSP_MESSAGE_REQUEST)
1604 *type = GST_RTSP_MESSAGE_HTTP_REQUEST;
1605 else if (*type == GST_RTSP_MESSAGE_RESPONSE)
1606 *type = GST_RTSP_MESSAGE_HTTP_RESPONSE;
1608 if (major == 1 && minor == 1) {
1609 *version = GST_RTSP_VERSION_1_1;
1610 } else if (major != 1 || minor != 0) {
1611 *version = GST_RTSP_VERSION_INVALID;
1612 res = GST_RTSP_ERROR;
1615 res = GST_RTSP_EPARSE;
1617 res = GST_RTSP_EPARSE;
1622 static GstRTSPResult
1623 parse_response_status (guint8 * buffer, GstRTSPMessage * msg)
1625 GstRTSPResult res = GST_RTSP_OK;
1627 gchar versionstr[20];
1632 bptr = (gchar *) buffer;
1634 if (parse_string (versionstr, sizeof (versionstr), &bptr) != GST_RTSP_OK)
1635 res = GST_RTSP_EPARSE;
1637 if (parse_string (codestr, sizeof (codestr), &bptr) != GST_RTSP_OK)
1638 res = GST_RTSP_EPARSE;
1639 code = atoi (codestr);
1640 if (G_UNLIKELY (*codestr == '\0' || code < 0 || code >= 600))
1641 res = GST_RTSP_EPARSE;
1643 while (g_ascii_isspace (*bptr))
1646 if (G_UNLIKELY (gst_rtsp_message_init_response (msg, code, bptr,
1647 NULL) != GST_RTSP_OK))
1648 res = GST_RTSP_EPARSE;
1650 res2 = parse_protocol_version (versionstr, &msg->type,
1651 &msg->type_data.response.version);
1652 if (G_LIKELY (res == GST_RTSP_OK))
1658 static GstRTSPResult
1659 parse_request_line (guint8 * buffer, GstRTSPMessage * msg)
1661 GstRTSPResult res = GST_RTSP_OK;
1663 gchar versionstr[20];
1664 gchar methodstr[20];
1667 GstRTSPMethod method;
1669 bptr = (gchar *) buffer;
1671 if (parse_string (methodstr, sizeof (methodstr), &bptr) != GST_RTSP_OK)
1672 res = GST_RTSP_EPARSE;
1673 method = gst_rtsp_find_method (methodstr);
1675 if (parse_string (urlstr, sizeof (urlstr), &bptr) != GST_RTSP_OK)
1676 res = GST_RTSP_EPARSE;
1677 if (G_UNLIKELY (*urlstr == '\0'))
1678 res = GST_RTSP_EPARSE;
1680 if (parse_string (versionstr, sizeof (versionstr), &bptr) != GST_RTSP_OK)
1681 res = GST_RTSP_EPARSE;
1683 if (G_UNLIKELY (*bptr != '\0'))
1684 res = GST_RTSP_EPARSE;
1686 if (G_UNLIKELY (gst_rtsp_message_init_request (msg, method,
1687 urlstr) != GST_RTSP_OK))
1688 res = GST_RTSP_EPARSE;
1690 res2 = parse_protocol_version (versionstr, &msg->type,
1691 &msg->type_data.request.version);
1692 if (G_LIKELY (res == GST_RTSP_OK))
1695 if (G_LIKELY (msg->type == GST_RTSP_MESSAGE_REQUEST)) {
1696 /* GET and POST are not allowed as RTSP methods */
1697 if (msg->type_data.request.method == GST_RTSP_GET ||
1698 msg->type_data.request.method == GST_RTSP_POST) {
1699 msg->type_data.request.method = GST_RTSP_INVALID;
1700 if (res == GST_RTSP_OK)
1701 res = GST_RTSP_ERROR;
1703 } else if (msg->type == GST_RTSP_MESSAGE_HTTP_REQUEST) {
1704 /* only GET and POST are allowed as HTTP methods */
1705 if (msg->type_data.request.method != GST_RTSP_GET &&
1706 msg->type_data.request.method != GST_RTSP_POST) {
1707 msg->type_data.request.method = GST_RTSP_INVALID;
1708 if (res == GST_RTSP_OK)
1709 res = GST_RTSP_ERROR;
1716 /* parsing lines means reading a Key: Value pair */
1717 static GstRTSPResult
1718 parse_line (guint8 * buffer, GstRTSPMessage * msg)
1720 GstRTSPHeaderField field;
1721 gchar *line = (gchar *) buffer;
1724 if ((value = strchr (line, ':')) == NULL || value == line)
1727 /* trim space before the colon */
1728 if (value[-1] == ' ')
1731 /* replace the colon with a NUL */
1734 /* find the header */
1735 field = gst_rtsp_find_header_field (line);
1736 if (field == GST_RTSP_HDR_INVALID)
1739 /* split up the value in multiple key:value pairs if it contains comma(s) */
1740 while (*value != '\0') {
1742 gchar *comma = NULL;
1743 gboolean quoted = FALSE;
1746 /* trim leading space */
1750 /* for headers which may not appear multiple times, and thus may not
1751 * contain multiple values on the same line, we can short-circuit the loop
1752 * below and the entire value results in just one key:value pair*/
1753 if (!gst_rtsp_header_allow_multiple (field))
1754 next_value = value + strlen (value);
1758 /* find the next value, taking special care of quotes and comments */
1759 while (*next_value != '\0') {
1760 if ((quoted || comment != 0) && *next_value == '\\' &&
1761 next_value[1] != '\0')
1763 else if (comment == 0 && *next_value == '"')
1765 else if (!quoted && *next_value == '(')
1767 else if (comment != 0 && *next_value == ')')
1769 else if (!quoted && comment == 0) {
1770 /* To quote RFC 2068: "User agents MUST take special care in parsing
1771 * the WWW-Authenticate field value if it contains more than one
1772 * challenge, or if more than one WWW-Authenticate header field is
1773 * provided, since the contents of a challenge may itself contain a
1774 * comma-separated list of authentication parameters."
1776 * What this means is that we cannot just look for an unquoted comma
1777 * when looking for multiple values in Proxy-Authenticate and
1778 * WWW-Authenticate headers. Instead we need to look for the sequence
1779 * "comma [space] token space token" before we can split after the
1782 if (field == GST_RTSP_HDR_PROXY_AUTHENTICATE ||
1783 field == GST_RTSP_HDR_WWW_AUTHENTICATE) {
1784 if (*next_value == ',') {
1785 if (next_value[1] == ' ') {
1786 /* skip any space following the comma so we do not mistake it for
1787 * separating between two tokens */
1791 } else if (*next_value == ' ' && next_value[1] != ',' &&
1792 next_value[1] != '=' && comma != NULL) {
1797 } else if (*next_value == ',')
1805 if (value != next_value && next_value[-1] == ' ')
1806 next_value[-1] = '\0';
1808 if (*next_value != '\0')
1809 *next_value++ = '\0';
1811 /* add the key:value pair */
1813 gst_rtsp_message_add_header (msg, field, value);
1824 return GST_RTSP_EPARSE;
1828 /* convert all consecutive whitespace to a single space */
1830 normalize_line (guint8 * buffer)
1833 if (g_ascii_isspace (*buffer)) {
1837 for (tmp = buffer; g_ascii_isspace (*tmp); tmp++) {
1840 memmove (buffer, tmp, strlen ((gchar *) tmp) + 1);
1848 * GST_RTSP_OK when a complete message was read.
1849 * GST_RTSP_EEOF: when the read socket is closed
1850 * GST_RTSP_EINTR: when more data is needed.
1851 * GST_RTSP_..: some other error occured.
1853 static GstRTSPResult
1854 build_next (GstRTSPBuilder * builder, GstRTSPMessage * message,
1855 GstRTSPConnection * conn)
1860 switch (builder->state) {
1865 builder->offset = 0;
1867 read_bytes (conn, (guint8 *) builder->buffer, &builder->offset, 1);
1868 if (res != GST_RTSP_OK)
1871 c = builder->buffer[0];
1873 /* we have 1 bytes now and we can see if this is a data message or
1876 /* data message, prepare for the header */
1877 builder->state = STATE_DATA_HEADER;
1878 } else if (c == '\n' || c == '\r') {
1879 /* skip \n and \r */
1880 builder->offset = 0;
1883 builder->state = STATE_READ_LINES;
1887 case STATE_DATA_HEADER:
1890 read_bytes (conn, (guint8 *) builder->buffer, &builder->offset, 4);
1891 if (res != GST_RTSP_OK)
1894 gst_rtsp_message_init_data (message, builder->buffer[1]);
1896 builder->body_len = (builder->buffer[2] << 8) | builder->buffer[3];
1897 builder->body_data = g_malloc (builder->body_len + 1);
1898 builder->body_data[builder->body_len] = '\0';
1899 builder->offset = 0;
1900 builder->state = STATE_DATA_BODY;
1903 case STATE_DATA_BODY:
1906 read_bytes (conn, builder->body_data, &builder->offset,
1908 if (res != GST_RTSP_OK)
1911 /* we have the complete body now, store in the message adjusting the
1912 * length to include the trailing '\0' */
1913 gst_rtsp_message_take_body (message,
1914 (guint8 *) builder->body_data, builder->body_len + 1);
1915 builder->body_data = NULL;
1916 builder->body_len = 0;
1918 builder->state = STATE_END;
1921 case STATE_READ_LINES:
1923 res = read_line (conn, builder->buffer, &builder->offset,
1924 sizeof (builder->buffer));
1925 if (res != GST_RTSP_OK)
1928 /* we have a regular response */
1929 if (builder->buffer[0] == '\0') {
1932 /* empty line, end of message header */
1933 /* see if there is a Content-Length header, but ignore it if this
1934 * is a POST request with an x-sessioncookie header */
1935 if (gst_rtsp_message_get_header (message,
1936 GST_RTSP_HDR_CONTENT_LENGTH, &hdrval, 0) == GST_RTSP_OK &&
1937 (message->type != GST_RTSP_MESSAGE_HTTP_REQUEST ||
1938 message->type_data.request.method != GST_RTSP_POST ||
1939 gst_rtsp_message_get_header (message,
1940 GST_RTSP_HDR_X_SESSIONCOOKIE, NULL, 0) != GST_RTSP_OK)) {
1941 /* there is, prepare to read the body */
1942 builder->body_len = atol (hdrval);
1943 builder->body_data = g_try_malloc (builder->body_len + 1);
1944 /* we can't do much here, we need the length to know how many bytes
1945 * we need to read next and when allocation fails, something is
1946 * probably wrong with the length. */
1947 if (builder->body_data == NULL)
1948 goto invalid_body_len;
1950 builder->body_data[builder->body_len] = '\0';
1951 builder->offset = 0;
1952 builder->state = STATE_DATA_BODY;
1954 builder->state = STATE_END;
1959 /* we have a line */
1960 normalize_line (builder->buffer);
1961 if (builder->line == 0) {
1962 /* first line, check for response status */
1963 if (memcmp (builder->buffer, "RTSP", 4) == 0 ||
1964 memcmp (builder->buffer, "HTTP", 4) == 0) {
1965 builder->status = parse_response_status (builder->buffer, message);
1967 builder->status = parse_request_line (builder->buffer, message);
1970 /* else just parse the line */
1971 res = parse_line (builder->buffer, message);
1972 if (res != GST_RTSP_OK)
1973 builder->status = res;
1976 builder->offset = 0;
1981 gchar *session_cookie;
1984 if (message->type == GST_RTSP_MESSAGE_DATA) {
1985 /* data messages don't have headers */
1990 /* save the tunnel session in the connection */
1991 if (message->type == GST_RTSP_MESSAGE_HTTP_REQUEST &&
1992 !conn->manual_http &&
1993 conn->tstate == TUNNEL_STATE_NONE &&
1994 gst_rtsp_message_get_header (message, GST_RTSP_HDR_X_SESSIONCOOKIE,
1995 &session_cookie, 0) == GST_RTSP_OK) {
1996 strncpy (conn->tunnelid, session_cookie, TUNNELID_LEN);
1997 conn->tunnelid[TUNNELID_LEN - 1] = '\0';
1998 conn->tunneled = TRUE;
2001 /* save session id in the connection for further use */
2002 if (message->type == GST_RTSP_MESSAGE_RESPONSE &&
2003 gst_rtsp_message_get_header (message, GST_RTSP_HDR_SESSION,
2004 &session_id, 0) == GST_RTSP_OK) {
2007 maxlen = sizeof (conn->session_id) - 1;
2008 /* the sessionid can have attributes marked with ;
2009 * Make sure we strip them */
2010 for (i = 0; session_id[i] != '\0'; i++) {
2011 if (session_id[i] == ';') {
2016 } while (g_ascii_isspace (session_id[i]));
2017 if (g_str_has_prefix (&session_id[i], "timeout=")) {
2020 /* if we parsed something valid, configure */
2021 if ((to = atoi (&session_id[i + 8])) > 0)
2028 /* make sure to not overflow */
2029 strncpy (conn->session_id, session_id, maxlen);
2030 conn->session_id[maxlen] = '\0';
2032 res = builder->status;
2036 res = GST_RTSP_ERROR;
2046 GST_DEBUG ("could not allocate body");
2047 return GST_RTSP_ERROR;
2052 * gst_rtsp_connection_read:
2053 * @conn: a #GstRTSPConnection
2054 * @data: the data to read
2055 * @size: the size of @data
2056 * @timeout: a timeout value or #NULL
2058 * Attempt to read @size bytes into @data from the connected @conn, blocking up to
2059 * the specified @timeout. @timeout can be #NULL, in which case this function
2060 * might block forever.
2062 * This function can be cancelled with gst_rtsp_connection_flush().
2064 * Returns: #GST_RTSP_OK on success.
2067 gst_rtsp_connection_read (GstRTSPConnection * conn, guint8 * data, guint size,
2075 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2076 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
2077 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2079 if (G_UNLIKELY (size == 0))
2084 /* configure timeout if any */
2085 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
2087 gst_poll_set_controllable (conn->fdset, TRUE);
2088 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
2089 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
2092 res = read_bytes (conn, data, &offset, size);
2093 if (G_UNLIKELY (res == GST_RTSP_EEOF))
2095 if (G_LIKELY (res == GST_RTSP_OK))
2097 if (G_UNLIKELY (res != GST_RTSP_EINTR))
2101 retval = gst_poll_wait (conn->fdset, to);
2102 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
2104 /* check for timeout */
2105 if (G_UNLIKELY (retval == 0))
2106 goto select_timeout;
2108 if (G_UNLIKELY (retval == -1)) {
2115 /* could also be an error with write socket */
2116 if (gst_poll_fd_has_error (conn->fdset, conn->writefd))
2119 gst_poll_set_controllable (conn->fdset, FALSE);
2126 return GST_RTSP_ESYS;
2130 return GST_RTSP_ETIMEOUT;
2134 return GST_RTSP_EINTR;
2138 return GST_RTSP_EEOF;
2142 res = GST_RTSP_ENET;
2150 static GstRTSPMessage *
2151 gen_tunnel_reply (GstRTSPConnection * conn, GstRTSPStatusCode code,
2152 const GstRTSPMessage * request)
2154 GstRTSPMessage *msg;
2157 if (gst_rtsp_status_as_text (code) == NULL)
2158 code = GST_RTSP_STS_INTERNAL_SERVER_ERROR;
2160 GST_RTSP_CHECK (gst_rtsp_message_new_response (&msg, code, NULL, request),
2163 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_SERVER,
2164 "GStreamer RTSP Server");
2165 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CONNECTION, "close");
2166 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CACHE_CONTROL, "no-store");
2167 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_PRAGMA, "no-cache");
2169 if (code == GST_RTSP_STS_OK) {
2171 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_X_SERVER_IP_ADDRESS,
2173 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CONTENT_TYPE,
2174 "application/x-rtsp-tunnelled");
2187 * gst_rtsp_connection_receive:
2188 * @conn: a #GstRTSPConnection
2189 * @message: the message to read
2190 * @timeout: a timeout value or #NULL
2192 * Attempt to read into @message from the connected @conn, blocking up to
2193 * the specified @timeout. @timeout can be #NULL, in which case this function
2194 * might block forever.
2196 * This function can be cancelled with gst_rtsp_connection_flush().
2198 * Returns: #GST_RTSP_OK on success.
2201 gst_rtsp_connection_receive (GstRTSPConnection * conn, GstRTSPMessage * message,
2205 GstRTSPBuilder builder;
2209 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2210 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2211 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2213 /* configure timeout if any */
2214 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
2216 gst_poll_set_controllable (conn->fdset, TRUE);
2217 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
2218 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
2220 memset (&builder, 0, sizeof (GstRTSPBuilder));
2222 res = build_next (&builder, message, conn);
2223 if (G_UNLIKELY (res == GST_RTSP_EEOF))
2225 else if (G_LIKELY (res == GST_RTSP_OK)) {
2226 if (!conn->manual_http) {
2227 if (message->type == GST_RTSP_MESSAGE_HTTP_REQUEST) {
2228 if (conn->tstate == TUNNEL_STATE_NONE &&
2229 message->type_data.request.method == GST_RTSP_GET) {
2230 GstRTSPMessage *response;
2232 conn->tstate = TUNNEL_STATE_GET;
2234 /* tunnel GET request, we can reply now */
2235 response = gen_tunnel_reply (conn, GST_RTSP_STS_OK, message);
2236 res = gst_rtsp_connection_send (conn, response, timeout);
2237 gst_rtsp_message_free (response);
2238 if (res == GST_RTSP_OK)
2239 res = GST_RTSP_ETGET;
2241 } else if (conn->tstate == TUNNEL_STATE_NONE &&
2242 message->type_data.request.method == GST_RTSP_POST) {
2243 conn->tstate = TUNNEL_STATE_POST;
2245 /* tunnel POST request, the caller now has to link the two
2247 res = GST_RTSP_ETPOST;
2250 res = GST_RTSP_EPARSE;
2253 } else if (message->type == GST_RTSP_MESSAGE_HTTP_RESPONSE) {
2254 res = GST_RTSP_EPARSE;
2260 } else if (G_UNLIKELY (res != GST_RTSP_EINTR))
2264 retval = gst_poll_wait (conn->fdset, to);
2265 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
2267 /* check for timeout */
2268 if (G_UNLIKELY (retval == 0))
2269 goto select_timeout;
2271 if (G_UNLIKELY (retval == -1)) {
2278 /* could also be an error with write socket */
2279 if (gst_poll_fd_has_error (conn->fdset, conn->writefd))
2282 /* once we start reading the wait cannot be controlled */
2283 if (builder.state != STATE_START)
2284 gst_poll_set_controllable (conn->fdset, FALSE);
2287 /* we have a message here */
2288 build_reset (&builder);
2295 res = GST_RTSP_ESYS;
2300 res = GST_RTSP_ETIMEOUT;
2305 res = GST_RTSP_EINTR;
2310 res = GST_RTSP_EEOF;
2315 res = GST_RTSP_ENET;
2321 build_reset (&builder);
2322 gst_rtsp_message_unset (message);
2328 * gst_rtsp_connection_close:
2329 * @conn: a #GstRTSPConnection
2331 * Close the connected @conn. After this call, the connection is in the same
2332 * state as when it was first created.
2334 * Returns: #GST_RTSP_OK on success.
2337 gst_rtsp_connection_close (GstRTSPConnection * conn)
2339 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2344 conn->read_ahead = 0;
2346 g_free (conn->initial_buffer);
2347 conn->initial_buffer = NULL;
2348 conn->initial_buffer_offset = 0;
2350 REMOVE_POLLFD (conn->fdset, &conn->fd0);
2351 REMOVE_POLLFD (conn->fdset, &conn->fd1);
2352 conn->writefd = NULL;
2353 conn->readfd = NULL;
2354 conn->tunneled = FALSE;
2355 conn->tstate = TUNNEL_STATE_NONE;
2357 g_free (conn->username);
2358 conn->username = NULL;
2359 g_free (conn->passwd);
2360 conn->passwd = NULL;
2361 gst_rtsp_connection_clear_auth_params (conn);
2364 conn->session_id[0] = '\0';
2370 * gst_rtsp_connection_free:
2371 * @conn: a #GstRTSPConnection
2373 * Close and free @conn.
2375 * Returns: #GST_RTSP_OK on success.
2378 gst_rtsp_connection_free (GstRTSPConnection * conn)
2382 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2384 res = gst_rtsp_connection_close (conn);
2385 gst_poll_free (conn->fdset);
2386 g_timer_destroy (conn->timer);
2387 gst_rtsp_url_free (conn->url);
2388 g_free (conn->proxy_host);
2398 * gst_rtsp_connection_poll:
2399 * @conn: a #GstRTSPConnection
2400 * @events: a bitmask of #GstRTSPEvent flags to check
2401 * @revents: location for result flags
2402 * @timeout: a timeout
2404 * Wait up to the specified @timeout for the connection to become available for
2405 * at least one of the operations specified in @events. When the function returns
2406 * with #GST_RTSP_OK, @revents will contain a bitmask of available operations on
2409 * @timeout can be #NULL, in which case this function might block forever.
2411 * This function can be cancelled with gst_rtsp_connection_flush().
2413 * Returns: #GST_RTSP_OK on success.
2418 gst_rtsp_connection_poll (GstRTSPConnection * conn, GstRTSPEvent events,
2419 GstRTSPEvent * revents, GTimeVal * timeout)
2424 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2425 g_return_val_if_fail (events != 0, GST_RTSP_EINVAL);
2426 g_return_val_if_fail (revents != NULL, GST_RTSP_EINVAL);
2427 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2428 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2430 gst_poll_set_controllable (conn->fdset, TRUE);
2432 /* add fd to writer set when asked to */
2433 gst_poll_fd_ctl_write (conn->fdset, conn->writefd,
2434 events & GST_RTSP_EV_WRITE);
2436 /* add fd to reader set when asked to */
2437 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, events & GST_RTSP_EV_READ);
2439 /* configure timeout if any */
2440 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
2443 retval = gst_poll_wait (conn->fdset, to);
2444 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
2446 if (G_UNLIKELY (retval == 0))
2447 goto select_timeout;
2449 if (G_UNLIKELY (retval == -1)) {
2457 if (events & GST_RTSP_EV_READ) {
2458 if (gst_poll_fd_can_read (conn->fdset, conn->readfd))
2459 *revents |= GST_RTSP_EV_READ;
2461 if (events & GST_RTSP_EV_WRITE) {
2462 if (gst_poll_fd_can_write (conn->fdset, conn->writefd))
2463 *revents |= GST_RTSP_EV_WRITE;
2470 return GST_RTSP_ETIMEOUT;
2474 return GST_RTSP_ESYS;
2478 return GST_RTSP_EINTR;
2483 * gst_rtsp_connection_next_timeout:
2484 * @conn: a #GstRTSPConnection
2485 * @timeout: a timeout
2487 * Calculate the next timeout for @conn, storing the result in @timeout.
2489 * Returns: #GST_RTSP_OK.
2492 gst_rtsp_connection_next_timeout (GstRTSPConnection * conn, GTimeVal * timeout)
2499 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2500 g_return_val_if_fail (timeout != NULL, GST_RTSP_EINVAL);
2502 ctimeout = conn->timeout;
2503 if (ctimeout >= 20) {
2504 /* Because we should act before the timeout we timeout 5
2505 * seconds in advance. */
2507 } else if (ctimeout >= 5) {
2508 /* else timeout 20% earlier */
2509 ctimeout -= ctimeout / 5;
2510 } else if (ctimeout >= 1) {
2511 /* else timeout 1 second earlier */
2515 elapsed = g_timer_elapsed (conn->timer, &usec);
2516 if (elapsed >= ctimeout) {
2520 sec = ctimeout - elapsed;
2521 if (usec <= G_USEC_PER_SEC)
2522 usec = G_USEC_PER_SEC - usec;
2527 timeout->tv_sec = sec;
2528 timeout->tv_usec = usec;
2534 * gst_rtsp_connection_reset_timeout:
2535 * @conn: a #GstRTSPConnection
2537 * Reset the timeout of @conn.
2539 * Returns: #GST_RTSP_OK.
2542 gst_rtsp_connection_reset_timeout (GstRTSPConnection * conn)
2544 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2546 g_timer_start (conn->timer);
2552 * gst_rtsp_connection_flush:
2553 * @conn: a #GstRTSPConnection
2554 * @flush: start or stop the flush
2556 * Start or stop the flushing action on @conn. When flushing, all current
2557 * and future actions on @conn will return #GST_RTSP_EINTR until the connection
2558 * is set to non-flushing mode again.
2560 * Returns: #GST_RTSP_OK.
2563 gst_rtsp_connection_flush (GstRTSPConnection * conn, gboolean flush)
2565 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2567 gst_poll_set_flushing (conn->fdset, flush);
2573 * gst_rtsp_connection_set_proxy:
2574 * @conn: a #GstRTSPConnection
2575 * @host: the proxy host
2576 * @port: the proxy port
2578 * Set the proxy host and port.
2580 * Returns: #GST_RTSP_OK.
2585 gst_rtsp_connection_set_proxy (GstRTSPConnection * conn,
2586 const gchar * host, guint port)
2588 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2590 g_free (conn->proxy_host);
2591 conn->proxy_host = g_strdup (host);
2592 conn->proxy_port = port;
2598 * gst_rtsp_connection_set_auth:
2599 * @conn: a #GstRTSPConnection
2600 * @method: authentication method
2602 * @pass: the password
2604 * Configure @conn for authentication mode @method with @user and @pass as the
2605 * user and password respectively.
2607 * Returns: #GST_RTSP_OK.
2610 gst_rtsp_connection_set_auth (GstRTSPConnection * conn,
2611 GstRTSPAuthMethod method, const gchar * user, const gchar * pass)
2613 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2615 if (method == GST_RTSP_AUTH_DIGEST && ((user == NULL || pass == NULL)
2616 || g_strrstr (user, ":") != NULL))
2617 return GST_RTSP_EINVAL;
2619 /* Make sure the username and passwd are being set for authentication */
2620 if (method == GST_RTSP_AUTH_NONE && (user == NULL || pass == NULL))
2621 return GST_RTSP_EINVAL;
2623 /* ":" chars are not allowed in usernames for basic auth */
2624 if (method == GST_RTSP_AUTH_BASIC && g_strrstr (user, ":") != NULL)
2625 return GST_RTSP_EINVAL;
2627 g_free (conn->username);
2628 g_free (conn->passwd);
2630 conn->auth_method = method;
2631 conn->username = g_strdup (user);
2632 conn->passwd = g_strdup (pass);
2639 * @key: ASCII string to hash
2641 * Hashes @key in a case-insensitive manner.
2643 * Returns: the hash code.
2646 str_case_hash (gconstpointer key)
2648 const char *p = key;
2649 guint h = g_ascii_toupper (*p);
2652 for (p += 1; *p != '\0'; p++)
2653 h = (h << 5) - h + g_ascii_toupper (*p);
2660 * @v1: an ASCII string
2661 * @v2: another ASCII string
2663 * Compares @v1 and @v2 in a case-insensitive manner
2665 * Returns: %TRUE if they are equal (modulo case)
2668 str_case_equal (gconstpointer v1, gconstpointer v2)
2670 const char *string1 = v1;
2671 const char *string2 = v2;
2673 return g_ascii_strcasecmp (string1, string2) == 0;
2677 * gst_rtsp_connection_set_auth_param:
2678 * @conn: a #GstRTSPConnection
2679 * @param: authentication directive
2682 * Setup @conn with authentication directives. This is not necesary for
2683 * methods #GST_RTSP_AUTH_NONE and #GST_RTSP_AUTH_BASIC. For
2684 * #GST_RTSP_AUTH_DIGEST, directives should be taken from the digest challenge
2685 * in the WWW-Authenticate response header and can include realm, domain,
2686 * nonce, opaque, stale, algorithm, qop as per RFC2617.
2691 gst_rtsp_connection_set_auth_param (GstRTSPConnection * conn,
2692 const gchar * param, const gchar * value)
2694 g_return_if_fail (conn != NULL);
2695 g_return_if_fail (param != NULL);
2697 if (conn->auth_params == NULL) {
2699 g_hash_table_new_full (str_case_hash, str_case_equal, g_free, g_free);
2701 g_hash_table_insert (conn->auth_params, g_strdup (param), g_strdup (value));
2705 * gst_rtsp_connection_clear_auth_params:
2706 * @conn: a #GstRTSPConnection
2708 * Clear the list of authentication directives stored in @conn.
2713 gst_rtsp_connection_clear_auth_params (GstRTSPConnection * conn)
2715 g_return_if_fail (conn != NULL);
2717 if (conn->auth_params != NULL) {
2718 g_hash_table_destroy (conn->auth_params);
2719 conn->auth_params = NULL;
2723 static GstRTSPResult
2724 set_qos_dscp (gint fd, guint qos_dscp)
2726 union gst_sockaddr sa;
2727 socklen_t slen = sizeof (sa);
2734 if (getsockname (fd, &sa.sa, &slen) < 0)
2735 goto no_getsockname;
2737 af = sa.sa.sa_family;
2739 /* if this is an IPv4-mapped address then do IPv4 QoS */
2740 if (af == AF_INET6) {
2741 if (IN6_IS_ADDR_V4MAPPED (&sa.sa_in6.sin6_addr))
2745 /* extract and shift 6 bits of the DSCP */
2746 tos = (qos_dscp & 0x3f) << 2;
2750 if (SETSOCKOPT (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0)
2755 if (SETSOCKOPT (fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof (tos)) < 0)
2769 return GST_RTSP_ESYS;
2774 return GST_RTSP_ERROR;
2779 * gst_rtsp_connection_set_qos_dscp:
2780 * @conn: a #GstRTSPConnection
2781 * @qos_dscp: DSCP value
2783 * Configure @conn to use the specified DSCP value.
2785 * Returns: #GST_RTSP_OK on success.
2790 gst_rtsp_connection_set_qos_dscp (GstRTSPConnection * conn, guint qos_dscp)
2794 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2795 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2796 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2798 res = set_qos_dscp (conn->fd0.fd, qos_dscp);
2799 if (res == GST_RTSP_OK)
2800 res = set_qos_dscp (conn->fd1.fd, qos_dscp);
2807 * gst_rtsp_connection_get_url:
2808 * @conn: a #GstRTSPConnection
2810 * Retrieve the URL of the other end of @conn.
2812 * Returns: The URL. This value remains valid until the
2813 * connection is freed.
2818 gst_rtsp_connection_get_url (const GstRTSPConnection * conn)
2820 g_return_val_if_fail (conn != NULL, NULL);
2826 * gst_rtsp_connection_get_ip:
2827 * @conn: a #GstRTSPConnection
2829 * Retrieve the IP address of the other end of @conn.
2831 * Returns: The IP address as a string. this value remains valid until the
2832 * connection is closed.
2837 gst_rtsp_connection_get_ip (const GstRTSPConnection * conn)
2839 g_return_val_if_fail (conn != NULL, NULL);
2845 * gst_rtsp_connection_set_ip:
2846 * @conn: a #GstRTSPConnection
2847 * @ip: an ip address
2849 * Set the IP address of the server.
2854 gst_rtsp_connection_set_ip (GstRTSPConnection * conn, const gchar * ip)
2856 g_return_if_fail (conn != NULL);
2859 conn->ip = g_strdup (ip);
2863 * gst_rtsp_connection_get_readfd:
2864 * @conn: a #GstRTSPConnection
2866 * Get the file descriptor for reading.
2868 * Returns: the file descriptor used for reading or -1 on error. The file
2869 * descriptor remains valid until the connection is closed.
2874 gst_rtsp_connection_get_readfd (const GstRTSPConnection * conn)
2876 g_return_val_if_fail (conn != NULL, -1);
2877 g_return_val_if_fail (conn->readfd != NULL, -1);
2879 return conn->readfd->fd;
2883 * gst_rtsp_connection_get_writefd:
2884 * @conn: a #GstRTSPConnection
2886 * Get the file descriptor for writing.
2888 * Returns: the file descriptor used for writing or -1 on error. The file
2889 * descriptor remains valid until the connection is closed.
2894 gst_rtsp_connection_get_writefd (const GstRTSPConnection * conn)
2896 g_return_val_if_fail (conn != NULL, -1);
2897 g_return_val_if_fail (conn->writefd != NULL, -1);
2899 return conn->writefd->fd;
2903 * gst_rtsp_connection_set_http_mode:
2904 * @conn: a #GstRTSPConnection
2905 * @enable: %TRUE to enable manual HTTP mode
2907 * By setting the HTTP mode to %TRUE the message parsing will support HTTP
2908 * messages in addition to the RTSP messages. It will also disable the
2909 * automatic handling of setting up an HTTP tunnel.
2914 gst_rtsp_connection_set_http_mode (GstRTSPConnection * conn, gboolean enable)
2916 g_return_if_fail (conn != NULL);
2918 conn->manual_http = enable;
2922 * gst_rtsp_connection_set_tunneled:
2923 * @conn: a #GstRTSPConnection
2924 * @tunneled: the new state
2926 * Set the HTTP tunneling state of the connection. This must be configured before
2927 * the @conn is connected.
2932 gst_rtsp_connection_set_tunneled (GstRTSPConnection * conn, gboolean tunneled)
2934 g_return_if_fail (conn != NULL);
2935 g_return_if_fail (conn->readfd == NULL);
2936 g_return_if_fail (conn->writefd == NULL);
2938 conn->tunneled = tunneled;
2942 * gst_rtsp_connection_is_tunneled:
2943 * @conn: a #GstRTSPConnection
2945 * Get the tunneling state of the connection.
2947 * Returns: if @conn is using HTTP tunneling.
2952 gst_rtsp_connection_is_tunneled (const GstRTSPConnection * conn)
2954 g_return_val_if_fail (conn != NULL, FALSE);
2956 return conn->tunneled;
2960 * gst_rtsp_connection_get_tunnelid:
2961 * @conn: a #GstRTSPConnection
2963 * Get the tunnel session id the connection.
2965 * Returns: returns a non-empty string if @conn is being tunneled over HTTP.
2970 gst_rtsp_connection_get_tunnelid (const GstRTSPConnection * conn)
2972 g_return_val_if_fail (conn != NULL, NULL);
2974 if (!conn->tunneled)
2977 return conn->tunnelid;
2981 * gst_rtsp_connection_do_tunnel:
2982 * @conn: a #GstRTSPConnection
2983 * @conn2: a #GstRTSPConnection or %NULL
2985 * If @conn received the first tunnel connection and @conn2 received
2986 * the second tunnel connection, link the two connections together so that
2987 * @conn manages the tunneled connection.
2989 * After this call, @conn2 cannot be used anymore and must be freed with
2990 * gst_rtsp_connection_free().
2992 * If @conn2 is %NULL then only the base64 decoding context will be setup for
2995 * Returns: return GST_RTSP_OK on success.
3000 gst_rtsp_connection_do_tunnel (GstRTSPConnection * conn,
3001 GstRTSPConnection * conn2)
3003 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
3005 if (conn2 != NULL) {
3006 g_return_val_if_fail (conn->tstate == TUNNEL_STATE_GET, GST_RTSP_EINVAL);
3007 g_return_val_if_fail (conn2->tstate == TUNNEL_STATE_POST, GST_RTSP_EINVAL);
3008 g_return_val_if_fail (!memcmp (conn2->tunnelid, conn->tunnelid,
3009 TUNNELID_LEN), GST_RTSP_EINVAL);
3011 /* both connections have fd0 as the read/write socket. start by taking the
3012 * socket from conn2 and set it as the socket in conn */
3013 conn->fd1 = conn2->fd0;
3015 /* clean up some of the state of conn2 */
3016 gst_poll_remove_fd (conn2->fdset, &conn2->fd0);
3018 conn2->readfd = conn2->writefd = NULL;
3020 /* We make fd0 the write socket and fd1 the read socket. */
3021 conn->writefd = &conn->fd0;
3022 conn->readfd = &conn->fd1;
3024 conn->tstate = TUNNEL_STATE_COMPLETE;
3027 /* we need base64 decoding for the readfd */
3028 conn->ctx.state = 0;
3031 conn->ctx.coutl = 0;
3032 conn->ctxp = &conn->ctx;
3037 #define READ_ERR (G_IO_HUP | G_IO_ERR | G_IO_NVAL)
3038 #define READ_COND (G_IO_IN | READ_ERR)
3039 #define WRITE_ERR (G_IO_HUP | G_IO_ERR | G_IO_NVAL)
3040 #define WRITE_COND (G_IO_OUT | WRITE_ERR)
3049 /* async functions */
3050 struct _GstRTSPWatch
3054 GstRTSPConnection *conn;
3056 GstRTSPBuilder builder;
3057 GstRTSPMessage message;
3062 /* queued message for transmission */
3071 GstRTSPWatchFuncs funcs;
3074 GDestroyNotify notify;
3078 gst_rtsp_source_prepare (GSource * source, gint * timeout)
3080 GstRTSPWatch *watch = (GstRTSPWatch *) source;
3082 if (watch->conn->initial_buffer != NULL)
3085 *timeout = (watch->conn->timeout * 1000);
3091 gst_rtsp_source_check (GSource * source)
3093 GstRTSPWatch *watch = (GstRTSPWatch *) source;
3095 if (watch->readfd.revents & READ_COND)
3098 if (watch->writefd.revents & WRITE_COND)
3105 gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback G_GNUC_UNUSED,
3106 gpointer user_data G_GNUC_UNUSED)
3108 GstRTSPWatch *watch = (GstRTSPWatch *) source;
3109 GstRTSPResult res = GST_RTSP_ERROR;
3110 gboolean keep_running = TRUE;
3112 /* first read as much as we can */
3113 if (watch->readfd.revents & READ_COND || watch->conn->initial_buffer != NULL) {
3115 if (watch->readfd.revents & READ_ERR)
3118 res = build_next (&watch->builder, &watch->message, watch->conn);
3119 if (res == GST_RTSP_EINTR)
3121 else if (G_UNLIKELY (res == GST_RTSP_EEOF)) {
3122 watch->readfd.events = 0;
3123 watch->readfd.revents = 0;
3124 g_source_remove_poll ((GSource *) watch, &watch->readfd);
3125 /* When we are in tunnelled mode, the read socket can be closed and we
3126 * should be prepared for a new POST method to reopen it */
3127 if (watch->conn->tstate == TUNNEL_STATE_COMPLETE) {
3128 /* remove the read connection for the tunnel */
3129 /* we accept a new POST request */
3130 watch->conn->tstate = TUNNEL_STATE_GET;
3131 /* and signal that we lost our tunnel */
3132 if (watch->funcs.tunnel_lost)
3133 res = watch->funcs.tunnel_lost (watch, watch->user_data);
3137 } else if (G_LIKELY (res == GST_RTSP_OK)) {
3138 if (!watch->conn->manual_http &&
3139 watch->message.type == GST_RTSP_MESSAGE_HTTP_REQUEST) {
3140 if (watch->conn->tstate == TUNNEL_STATE_NONE &&
3141 watch->message.type_data.request.method == GST_RTSP_GET) {
3142 GstRTSPMessage *response;
3143 GstRTSPStatusCode code;
3145 watch->conn->tstate = TUNNEL_STATE_GET;
3147 if (watch->funcs.tunnel_start)
3148 code = watch->funcs.tunnel_start (watch, watch->user_data);
3150 code = GST_RTSP_STS_OK;
3152 /* queue the response */
3153 response = gen_tunnel_reply (watch->conn, code, &watch->message);
3154 gst_rtsp_watch_send_message (watch, response, NULL);
3155 gst_rtsp_message_free (response);
3157 } else if (watch->conn->tstate == TUNNEL_STATE_NONE &&
3158 watch->message.type_data.request.method == GST_RTSP_POST) {
3159 watch->conn->tstate = TUNNEL_STATE_POST;
3161 /* in the callback the connection should be tunneled with the
3163 if (watch->funcs.tunnel_complete)
3164 watch->funcs.tunnel_complete (watch, watch->user_data);
3170 if (!watch->conn->manual_http) {
3171 /* if manual HTTP support is not enabled, then restore the message to
3172 * what it would have looked like without the support for parsing HTTP
3173 * messages being present */
3174 if (watch->message.type == GST_RTSP_MESSAGE_HTTP_REQUEST) {
3175 watch->message.type = GST_RTSP_MESSAGE_REQUEST;
3176 watch->message.type_data.request.method = GST_RTSP_INVALID;
3177 if (watch->message.type_data.request.version != GST_RTSP_VERSION_1_0)
3178 watch->message.type_data.request.version = GST_RTSP_VERSION_INVALID;
3179 res = GST_RTSP_EPARSE;
3180 } else if (watch->message.type == GST_RTSP_MESSAGE_HTTP_RESPONSE) {
3181 watch->message.type = GST_RTSP_MESSAGE_RESPONSE;
3182 if (watch->message.type_data.response.version != GST_RTSP_VERSION_1_0)
3183 watch->message.type_data.response.version =
3184 GST_RTSP_VERSION_INVALID;
3185 res = GST_RTSP_EPARSE;
3189 if (G_LIKELY (res == GST_RTSP_OK)) {
3190 if (watch->funcs.message_received)
3191 watch->funcs.message_received (watch, &watch->message,
3198 gst_rtsp_message_unset (&watch->message);
3199 build_reset (&watch->builder);
3203 if (watch->writefd.revents & WRITE_COND) {
3204 if (watch->writefd.revents & WRITE_ERR)
3207 g_mutex_lock (watch->mutex);
3209 if (watch->write_data == NULL) {
3212 /* get a new message from the queue */
3213 rec = g_queue_pop_tail (watch->messages);
3217 watch->write_off = 0;
3218 watch->write_data = rec->data;
3219 watch->write_size = rec->size;
3220 watch->write_id = rec->id;
3222 g_slice_free (GstRTSPRec, rec);
3225 res = write_bytes (watch->writefd.fd, watch->write_data,
3226 &watch->write_off, watch->write_size);
3227 g_mutex_unlock (watch->mutex);
3229 if (res == GST_RTSP_EINTR)
3231 else if (G_LIKELY (res == GST_RTSP_OK)) {
3232 if (watch->funcs.message_sent)
3233 watch->funcs.message_sent (watch, watch->write_id, watch->user_data);
3237 g_mutex_lock (watch->mutex);
3239 g_free (watch->write_data);
3240 watch->write_data = NULL;
3243 watch->writefd.events = WRITE_ERR;
3245 g_mutex_unlock (watch->mutex);
3249 return keep_running;
3254 if (watch->funcs.closed)
3255 watch->funcs.closed (watch, watch->user_data);
3257 /* always stop when the readfd returns EOF in non-tunneled mode */
3262 watch->readfd.events = 0;
3263 watch->readfd.revents = 0;
3264 g_source_remove_poll ((GSource *) watch, &watch->readfd);
3265 keep_running = (watch->writefd.events != 0);
3268 if (watch->funcs.error_full)
3269 GST_RTSP_CHECK (watch->funcs.error_full (watch, res, &watch->message,
3270 0, watch->user_data), error);
3278 watch->writefd.events = 0;
3279 watch->writefd.revents = 0;
3280 g_source_remove_poll ((GSource *) watch, &watch->writefd);
3281 keep_running = (watch->readfd.events != 0);
3284 if (watch->funcs.error_full)
3285 GST_RTSP_CHECK (watch->funcs.error_full (watch, res, NULL,
3286 watch->write_id, watch->user_data), error);
3294 if (watch->funcs.error)
3295 watch->funcs.error (watch, res, watch->user_data);
3297 return keep_running;
3302 gst_rtsp_rec_free (gpointer data)
3304 GstRTSPRec *rec = data;
3307 g_slice_free (GstRTSPRec, rec);
3311 gst_rtsp_source_finalize (GSource * source)
3313 GstRTSPWatch *watch = (GstRTSPWatch *) source;
3315 build_reset (&watch->builder);
3316 gst_rtsp_message_unset (&watch->message);
3318 g_queue_foreach (watch->messages, (GFunc) gst_rtsp_rec_free, NULL);
3319 g_queue_free (watch->messages);
3320 watch->messages = NULL;
3321 g_free (watch->write_data);
3323 g_mutex_free (watch->mutex);
3326 watch->notify (watch->user_data);
3329 static GSourceFuncs gst_rtsp_source_funcs = {
3330 gst_rtsp_source_prepare,
3331 gst_rtsp_source_check,
3332 gst_rtsp_source_dispatch,
3333 gst_rtsp_source_finalize,
3339 * gst_rtsp_watch_new:
3340 * @conn: a #GstRTSPConnection
3341 * @funcs: watch functions
3342 * @user_data: user data to pass to @funcs
3343 * @notify: notify when @user_data is not referenced anymore
3345 * Create a watch object for @conn. The functions provided in @funcs will be
3346 * called with @user_data when activity happened on the watch.
3348 * The new watch is usually created so that it can be attached to a
3349 * maincontext with gst_rtsp_watch_attach().
3351 * @conn must exist for the entire lifetime of the watch.
3353 * Returns: a #GstRTSPWatch that can be used for asynchronous RTSP
3354 * communication. Free with gst_rtsp_watch_unref () after usage.
3359 gst_rtsp_watch_new (GstRTSPConnection * conn,
3360 GstRTSPWatchFuncs * funcs, gpointer user_data, GDestroyNotify notify)
3362 GstRTSPWatch *result;
3364 g_return_val_if_fail (conn != NULL, NULL);
3365 g_return_val_if_fail (funcs != NULL, NULL);
3366 g_return_val_if_fail (conn->readfd != NULL, NULL);
3367 g_return_val_if_fail (conn->writefd != NULL, NULL);
3369 result = (GstRTSPWatch *) g_source_new (&gst_rtsp_source_funcs,
3370 sizeof (GstRTSPWatch));
3372 result->conn = conn;
3373 result->builder.state = STATE_START;
3375 result->mutex = g_mutex_new ();
3376 result->messages = g_queue_new ();
3378 result->readfd.fd = -1;
3379 result->writefd.fd = -1;
3381 gst_rtsp_watch_reset (result);
3383 result->funcs = *funcs;
3384 result->user_data = user_data;
3385 result->notify = notify;
3391 * gst_rtsp_watch_reset:
3392 * @watch: a #GstRTSPWatch
3394 * Reset @watch, this is usually called after gst_rtsp_connection_do_tunnel()
3395 * when the file descriptors of the connection might have changed.
3400 gst_rtsp_watch_reset (GstRTSPWatch * watch)
3402 if (watch->readfd.fd != -1)
3403 g_source_remove_poll ((GSource *) watch, &watch->readfd);
3404 if (watch->writefd.fd != -1)
3405 g_source_remove_poll ((GSource *) watch, &watch->writefd);
3407 watch->readfd.fd = watch->conn->readfd->fd;
3408 watch->readfd.events = READ_COND;
3409 watch->readfd.revents = 0;
3411 watch->writefd.fd = watch->conn->writefd->fd;
3412 watch->writefd.events = WRITE_ERR;
3413 watch->writefd.revents = 0;
3415 if (watch->readfd.fd != -1)
3416 g_source_add_poll ((GSource *) watch, &watch->readfd);
3417 if (watch->writefd.fd != -1)
3418 g_source_add_poll ((GSource *) watch, &watch->writefd);
3422 * gst_rtsp_watch_attach:
3423 * @watch: a #GstRTSPWatch
3424 * @context: a GMainContext (if NULL, the default context will be used)
3426 * Adds a #GstRTSPWatch to a context so that it will be executed within that context.
3428 * Returns: the ID (greater than 0) for the watch within the GMainContext.
3433 gst_rtsp_watch_attach (GstRTSPWatch * watch, GMainContext * context)
3435 g_return_val_if_fail (watch != NULL, 0);
3437 return g_source_attach ((GSource *) watch, context);
3441 * gst_rtsp_watch_unref:
3442 * @watch: a #GstRTSPWatch
3444 * Decreases the reference count of @watch by one. If the resulting reference
3445 * count is zero the watch and associated memory will be destroyed.
3450 gst_rtsp_watch_unref (GstRTSPWatch * watch)
3452 g_return_if_fail (watch != NULL);
3454 g_source_unref ((GSource *) watch);
3458 * gst_rtsp_watch_write_data:
3459 * @watch: a #GstRTSPWatch
3460 * @data: the data to queue
3461 * @size: the size of @data
3462 * @id: location for a message ID or %NULL
3464 * Write @data using the connection of the @watch. If it cannot be sent
3465 * immediately, it will be queued for transmission in @watch. The contents of
3466 * @message will then be serialized and transmitted when the connection of the
3467 * @watch becomes writable. In case the @message is queued, the ID returned in
3468 * @id will be non-zero and used as the ID argument in the message_sent
3471 * This function will take ownership of @data and g_free() it after use.
3473 * Returns: #GST_RTSP_OK on success.
3478 gst_rtsp_watch_write_data (GstRTSPWatch * watch, const guint8 * data,
3479 guint size, guint * id)
3484 GMainContext *context = NULL;
3486 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3487 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
3488 g_return_val_if_fail (size != 0, GST_RTSP_EINVAL);
3490 g_mutex_lock (watch->mutex);
3492 /* try to send the message synchronously first */
3493 if (watch->messages->length == 0 && watch->write_data == NULL) {
3494 res = write_bytes (watch->writefd.fd, data, &off, size);
3495 if (res != GST_RTSP_EINTR) {
3498 g_free ((gpointer) data);
3503 /* make a record with the data and id for sending async */
3504 rec = g_slice_new (GstRTSPRec);
3506 rec->data = (guint8 *) data;
3509 rec->data = g_memdup (data + off, size - off);
3510 rec->size = size - off;
3511 g_free ((gpointer) data);
3515 /* make sure rec->id is never 0 */
3516 rec->id = ++watch->id;
3517 } while (G_UNLIKELY (rec->id == 0));
3519 /* add the record to a queue. FIXME we would like to have an upper limit here */
3520 g_queue_push_head (watch->messages, rec);
3522 /* make sure the main context will now also check for writability on the
3524 if (watch->writefd.events != WRITE_COND) {
3525 watch->writefd.events = WRITE_COND;
3526 context = ((GSource *) watch)->context;
3534 g_mutex_unlock (watch->mutex);
3537 g_main_context_wakeup (context);
3543 * gst_rtsp_watch_send_message:
3544 * @watch: a #GstRTSPWatch
3545 * @message: a #GstRTSPMessage
3546 * @id: location for a message ID or %NULL
3548 * Send a @message using the connection of the @watch. If it cannot be sent
3549 * immediately, it will be queued for transmission in @watch. The contents of
3550 * @message will then be serialized and transmitted when the connection of the
3551 * @watch becomes writable. In case the @message is queued, the ID returned in
3552 * @id will be non-zero and used as the ID argument in the message_sent
3555 * Returns: #GST_RTSP_OK on success.
3560 gst_rtsp_watch_send_message (GstRTSPWatch * watch, GstRTSPMessage * message,
3566 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3567 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3569 /* make a record with the message as a string and id */
3570 str = message_to_string (watch->conn, message);
3572 return gst_rtsp_watch_write_data (watch,
3573 (guint8 *) g_string_free (str, FALSE), size, id);
3577 * gst_rtsp_watch_queue_data:
3578 * @watch: a #GstRTSPWatch
3579 * @data: the data to queue
3580 * @size: the size of @data
3582 * Queue @data for transmission in @watch. It will be transmitted when the
3583 * connection of the @watch becomes writable.
3585 * This function will take ownership of @data and g_free() it after use.
3587 * The return value of this function will be used as the id argument in the
3588 * message_sent callback.
3590 * Deprecated: Use gst_rtsp_watch_write_data()
3596 #ifndef GST_REMOVE_DEPRECATED
3597 #ifdef GST_DISABLE_DEPRECATED
3599 gst_rtsp_watch_queue_data (GstRTSPWatch * watch, const guint8 * data,
3603 gst_rtsp_watch_queue_data (GstRTSPWatch * watch, const guint8 * data,
3607 GMainContext *context = NULL;
3609 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3610 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
3611 g_return_val_if_fail (size != 0, GST_RTSP_EINVAL);
3613 g_mutex_lock (watch->mutex);
3615 /* make a record with the data and id */
3616 rec = g_slice_new (GstRTSPRec);
3617 rec->data = (guint8 *) data;
3620 /* make sure rec->id is never 0 */
3621 rec->id = ++watch->id;
3622 } while (G_UNLIKELY (rec->id == 0));
3624 /* add the record to a queue. FIXME we would like to have an upper limit here */
3625 g_queue_push_head (watch->messages, rec);
3627 /* make sure the main context will now also check for writability on the
3629 if (watch->writefd.events != WRITE_COND) {
3630 watch->writefd.events = WRITE_COND;
3631 context = ((GSource *) watch)->context;
3633 g_mutex_unlock (watch->mutex);
3636 g_main_context_wakeup (context);
3640 #endif /* GST_REMOVE_DEPRECATED */
3643 * gst_rtsp_watch_queue_message:
3644 * @watch: a #GstRTSPWatch
3645 * @message: a #GstRTSPMessage
3647 * Queue a @message for transmission in @watch. The contents of this
3648 * message will be serialized and transmitted when the connection of the
3649 * @watch becomes writable.
3651 * The return value of this function will be used as the id argument in the
3652 * message_sent callback.
3654 * Deprecated: Use gst_rtsp_watch_send_message()
3660 #ifndef GST_REMOVE_DEPRECATED
3661 #ifdef GST_DISABLE_DEPRECATED
3663 gst_rtsp_watch_queue_message (GstRTSPWatch * watch, GstRTSPMessage * message);
3666 gst_rtsp_watch_queue_message (GstRTSPWatch * watch, GstRTSPMessage * message)
3671 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3672 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3674 /* make a record with the message as a string and id */
3675 str = message_to_string (watch->conn, message);
3677 return gst_rtsp_watch_queue_data (watch,
3678 (guint8 *) g_string_free (str, FALSE), size);
3680 #endif /* GST_REMOVE_DEPRECATED */