2 * Copyright (C) <2005-2009> Wim Taymans <wim.taymans@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 * Unless otherwise indicated, Source Code is licensed under MIT license.
21 * See further explanation attached in License Statement (distributed in the file
24 * Permission is hereby granted, free of charge, to any person obtaining a copy of
25 * this software and associated documentation files (the "Software"), to deal in
26 * the Software without restriction, including without limitation the rights to
27 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
28 * of the Software, and to permit persons to whom the Software is furnished to do
29 * so, subject to the following conditions:
31 * The above copyright notice and this permission notice shall be included in all
32 * copies or substantial portions of the Software.
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
44 * SECTION:gstrtspconnection
45 * @short_description: manage RTSP connections
46 * @see_also: gstrtspurl
50 * This object manages the RTSP connection to the server. It provides function
51 * to receive and send bytes and messages.
55 * Last reviewed on 2007-07-24 (0.10.14)
72 /* we include this here to get the G_OS_* defines */
77 /* ws2_32.dll has getaddrinfo and freeaddrinfo on Windows XP and later.
78 * minwg32 headers check WINVER before allowing the use of these */
84 #define EINPROGRESS WSAEINPROGRESS
86 #include <sys/ioctl.h>
88 #include <sys/socket.h>
90 #include <netinet/in.h>
93 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
94 #include <sys/filio.h>
97 #include "gstrtspconnection.h"
98 #include "gstrtspbase64.h"
103 struct sockaddr_in sa_in;
104 struct sockaddr_in6 sa_in6;
105 struct sockaddr_storage sa_stor;
112 guchar out[3]; /* the size must be evenly divisible by 3 */
117 static GstRTSPResult read_line (GstRTSPConnection * conn, guint8 * buffer,
118 guint * idx, guint size);
119 static GstRTSPResult parse_key_value (guint8 * buffer, gchar * key,
120 guint keysize, gchar ** value);
121 static void parse_string (gchar * dest, gint size, gchar ** src);
124 #define READ_SOCKET(fd, buf, len) recv (fd, (char *)buf, len, 0)
125 #define WRITE_SOCKET(fd, buf, len) send (fd, (const char *)buf, len, 0)
126 #define SETSOCKOPT(sock, level, name, val, len) setsockopt (sock, level, name, (const char *)val, len)
127 #define CLOSE_SOCKET(sock) closesocket (sock)
128 #define ERRNO_IS_EAGAIN (WSAGetLastError () == WSAEWOULDBLOCK)
129 #define ERRNO_IS_EINTR (WSAGetLastError () == WSAEINTR)
130 /* According to Microsoft's connect() documentation this one returns
131 * WSAEWOULDBLOCK and not WSAEINPROGRESS. */
132 #define ERRNO_IS_EINPROGRESS (WSAGetLastError () == WSAEWOULDBLOCK)
134 #define READ_SOCKET(fd, buf, len) read (fd, buf, len)
135 #define WRITE_SOCKET(fd, buf, len) write (fd, buf, len)
136 #define SETSOCKOPT(sock, level, name, val, len) setsockopt (sock, level, name, val, len)
137 #define CLOSE_SOCKET(sock) close (sock)
138 #define ERRNO_IS_EAGAIN (errno == EAGAIN)
139 #define ERRNO_IS_EINTR (errno == EINTR)
140 #define ERRNO_IS_EINPROGRESS (errno == EINPROGRESS)
143 #define ADD_POLLFD(fdset, pfd, fd) \
146 gst_poll_add_fd (fdset, pfd); \
149 #define REMOVE_POLLFD(fdset, pfd) \
151 if ((pfd)->fd != -1) { \
152 GST_DEBUG ("remove fd %d", (pfd)->fd); \
153 gst_poll_remove_fd (fdset, pfd); \
154 CLOSE_SOCKET ((pfd)->fd); \
164 TUNNEL_STATE_COMPLETE
165 } GstRTSPTunnelState;
167 #define TUNNELID_LEN 24
169 struct _GstRTSPConnection
172 /* URL for the connection */
175 /* connection state */
182 gchar tunnelid[TUNNELID_LEN];
184 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;
221 /* a structure for constructing RTSPMessages */
234 build_reset (GstRTSPBuilder * builder)
236 g_free (builder->body_data);
237 memset (builder, 0, sizeof (GstRTSPBuilder));
241 * gst_rtsp_connection_create:
242 * @url: a #GstRTSPUrl
243 * @conn: storage for a #GstRTSPConnection
245 * Create a newly allocated #GstRTSPConnection from @url and store it in @conn.
246 * The connection will not yet attempt to connect to @url, use
247 * gst_rtsp_connection_connect().
249 * A copy of @url will be made.
251 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
254 gst_rtsp_connection_create (const GstRTSPUrl * url, GstRTSPConnection ** conn)
256 GstRTSPConnection *newconn;
262 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
265 error = WSAStartup (0x0202, &w);
270 if (w.wVersion != 0x0202)
274 newconn = g_new0 (GstRTSPConnection, 1);
276 if ((newconn->fdset = gst_poll_new (TRUE)) == NULL)
279 newconn->url = gst_rtsp_url_copy (url);
280 newconn->fd0.fd = -1;
281 newconn->fd1.fd = -1;
282 newconn->timer = g_timer_new ();
283 newconn->timeout = 60;
286 newconn->auth_method = GST_RTSP_AUTH_NONE;
287 newconn->username = NULL;
288 newconn->passwd = NULL;
289 newconn->auth_params = NULL;
299 g_warning ("Error %d on WSAStartup", error);
300 return GST_RTSP_EWSASTART;
304 g_warning ("Windows sockets are not version 0x202 (current 0x%x)",
307 return GST_RTSP_EWSAVERSION;
316 return GST_RTSP_ESYS;
321 * gst_rtsp_connection_accept:
323 * @conn: storage for a #GstRTSPConnection
325 * Accept a new connection on @sock and create a new #GstRTSPConnection for
326 * handling communication on new socket.
328 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
333 gst_rtsp_connection_accept (gint sock, GstRTSPConnection ** conn)
336 GstRTSPConnection *newconn = NULL;
337 union gst_sockaddr sa;
338 socklen_t slen = sizeof (sa);
339 gchar ip[INET6_ADDRSTRLEN];
345 memset (&sa, 0, slen);
348 fd = accept (sock, &sa.sa, &slen);
350 fd = accept (sock, &sa.sa, (gint *) & slen);
351 #endif /* G_OS_WIN32 */
355 if (getnameinfo (&sa.sa, slen, ip, sizeof (ip), NULL, 0, NI_NUMERICHOST) != 0)
356 goto getnameinfo_failed;
357 if (sa.sa.sa_family != AF_INET && sa.sa.sa_family != AF_INET6)
360 /* set to non-blocking mode so that we can cancel the communication */
362 fcntl (fd, F_SETFL, O_NONBLOCK);
364 ioctlsocket (fd, FIONBIO, &flags);
365 #endif /* G_OS_WIN32 */
367 /* create a url for the client address */
368 url = g_new0 (GstRTSPUrl, 1);
369 url->host = g_strdup (ip);
370 if (sa.sa.sa_family == AF_INET)
371 url->port = sa.sa_in.sin_port;
373 url->port = sa.sa_in6.sin6_port;
375 /* now create the connection object */
376 gst_rtsp_connection_create (url, &newconn);
377 gst_rtsp_url_free (url);
379 ADD_POLLFD (newconn->fdset, &newconn->fd0, fd);
381 /* both read and write initially */
382 newconn->readfd = &newconn->fd0;
383 newconn->writefd = &newconn->fd0;
392 return GST_RTSP_ESYS;
398 return GST_RTSP_ERROR;
403 do_resolve (const gchar * host)
405 static gchar ip[INET6_ADDRSTRLEN];
406 struct addrinfo *aires;
410 aierr = getaddrinfo (host, NULL, NULL, &aires);
414 for (ai = aires; ai; ai = ai->ai_next) {
415 if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) {
422 aierr = getnameinfo (ai->ai_addr, ai->ai_addrlen, ip, sizeof (ip), NULL, 0,
423 NI_NUMERICHOST | NI_NUMERICSERV);
427 freeaddrinfo (aires);
429 return g_strdup (ip);
434 GST_ERROR ("no addrinfo found for %s: %s", host, gai_strerror (aierr));
439 GST_ERROR ("no family found for %s", host);
440 freeaddrinfo (aires);
445 GST_ERROR ("no address found for %s: %s", host, gai_strerror (aierr));
446 freeaddrinfo (aires);
452 do_connect (const gchar * ip, guint16 port, GstPollFD * fdout,
453 GstPoll * fdset, GTimeVal * timeout)
456 struct addrinfo hints;
457 struct addrinfo *aires;
460 gchar service[NI_MAXSERV];
463 unsigned long flags = 1;
464 #endif /* G_OS_WIN32 */
468 memset (&hints, 0, sizeof hints);
469 hints.ai_flags = AI_NUMERICHOST;
470 hints.ai_family = AF_UNSPEC;
471 hints.ai_socktype = SOCK_STREAM;
472 g_snprintf (service, sizeof (service) - 1, "%hu", port);
473 service[sizeof (service) - 1] = '\0';
475 aierr = getaddrinfo (ip, service, &hints, &aires);
479 for (ai = aires; ai; ai = ai->ai_next) {
480 if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) {
487 fd = socket (ai->ai_family, SOCK_STREAM, 0);
491 /* set to non-blocking mode so that we can cancel the connect */
493 fcntl (fd, F_SETFL, O_NONBLOCK);
495 ioctlsocket (fd, FIONBIO, &flags);
496 #endif /* G_OS_WIN32 */
498 /* add the socket to our fdset */
499 ADD_POLLFD (fdset, fdout, fd);
501 /* we are going to connect ASYNC now */
502 ret = connect (fd, ai->ai_addr, ai->ai_addrlen);
505 if (!ERRNO_IS_EINPROGRESS)
508 /* wait for connect to complete up to the specified timeout or until we got
510 gst_poll_fd_ctl_write (fdset, fdout, TRUE);
512 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
515 retval = gst_poll_wait (fdset, to);
516 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
520 else if (retval == -1)
523 /* we can still have an error connecting on windows */
524 if (gst_poll_fd_has_error (fdset, fdout)) {
525 socklen_t len = sizeof (errno);
527 getsockopt (fd, SOL_SOCKET, SO_ERROR, &errno, &len);
529 getsockopt (fd, SOL_SOCKET, SO_ERROR, (char *) &errno, &len);
534 gst_poll_fd_ignored (fdset, fdout);
537 freeaddrinfo (aires);
544 GST_ERROR ("no addrinfo found for %s: %s", ip, gai_strerror (aierr));
545 return GST_RTSP_ERROR;
549 GST_ERROR ("no family found for %s", ip);
550 freeaddrinfo (aires);
551 return GST_RTSP_ERROR;
555 GST_ERROR ("no socket %d (%s)", errno, g_strerror (errno));
556 freeaddrinfo (aires);
557 return GST_RTSP_ESYS;
561 GST_ERROR ("system error %d (%s)", errno, g_strerror (errno));
562 REMOVE_POLLFD (fdset, fdout);
563 freeaddrinfo (aires);
564 return GST_RTSP_ESYS;
568 GST_ERROR ("timeout");
569 REMOVE_POLLFD (fdset, fdout);
570 freeaddrinfo (aires);
571 return GST_RTSP_ETIMEOUT;
576 setup_tunneling (GstRTSPConnection * conn, GTimeVal * timeout)
584 gchar *ip, *url_port_str;
585 guint16 port, url_port;
586 gchar codestr[4], *resultstr;
591 /* create a random sessionid */
592 for (i = 0; i < TUNNELID_LEN; i++)
593 conn->tunnelid[i] = g_random_int_range ('a', 'z');
594 conn->tunnelid[TUNNELID_LEN - 1] = '\0';
597 /* get the port from the url */
598 gst_rtsp_url_get_port (url, &url_port);
600 if (conn->proxy_host) {
601 hostparam = g_strdup_printf ("Host: %s:%d\r\n", url->host, url_port);
602 url_port_str = g_strdup_printf (":%d", url_port);
603 ip = conn->proxy_host;
604 port = conn->proxy_port;
613 str = g_strdup_printf ("GET %s%s%s%s%s%s HTTP/1.0\r\n"
615 "x-sessioncookie: %s\r\n"
616 "Accept: application/x-rtsp-tunnelled\r\n"
617 "Pragma: no-cache\r\n"
618 "Cache-Control: no-cache\r\n" "\r\n",
619 conn->proxy_host ? "http://" : "",
620 conn->proxy_host ? url->host : "",
621 conn->proxy_host ? url_port_str : "",
622 url->abspath, url->query ? "?" : "", url->query ? url->query : "",
623 hostparam ? hostparam : "", conn->tunnelid);
625 /* we start by writing to this fd */
626 conn->writefd = &conn->fd0;
628 res = gst_rtsp_connection_write (conn, (guint8 *) str, strlen (str), timeout);
630 if (res != GST_RTSP_OK)
633 gst_poll_fd_ctl_write (conn->fdset, &conn->fd0, FALSE);
634 gst_poll_fd_ctl_read (conn->fdset, &conn->fd0, TRUE);
636 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
644 res = read_line (conn, buffer, &idx, sizeof (buffer));
645 if (res == GST_RTSP_EEOF)
647 if (res == GST_RTSP_OK)
649 if (res != GST_RTSP_EINTR)
653 retval = gst_poll_wait (conn->fdset, to);
654 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
656 /* check for timeout */
668 /* check for last line */
669 if (buffer[0] == '\r')
671 if (buffer[0] == '\0')
675 /* first line, parse response */
676 gchar versionstr[20];
679 bptr = (gchar *) buffer;
681 parse_string (versionstr, sizeof (versionstr), &bptr);
682 parse_string (codestr, sizeof (codestr), &bptr);
683 code = atoi (codestr);
685 while (g_ascii_isspace (*bptr))
690 if (code != GST_RTSP_STS_OK)
696 /* other lines, parse key/value */
697 res = parse_key_value (buffer, key, sizeof (key), &value);
698 if (res == GST_RTSP_OK) {
699 /* we got a new ip address */
700 if (g_ascii_strcasecmp (key, "x-server-ip-address") == 0) {
701 if (conn->proxy_host) {
702 /* if we use a proxy we need to change the destination url */
704 url->host = g_strdup (value);
706 g_free (url_port_str);
708 g_strdup_printf ("Host: %s:%d\r\n", url->host, url_port);
709 url_port_str = g_strdup_printf (":%d", url_port);
711 /* and resolve the new ip address */
712 if (!(ip = do_resolve (conn->ip)))
723 /* connect to the host/port */
724 res = do_connect (ip, port, &conn->fd1, conn->fdset, timeout);
725 if (res != GST_RTSP_OK)
728 /* this is now our writing socket */
729 conn->writefd = &conn->fd1;
732 str = g_strdup_printf ("POST %s%s%s%s%s%s HTTP/1.0\r\n"
734 "x-sessioncookie: %s\r\n"
735 "Content-Type: application/x-rtsp-tunnelled\r\n"
736 "Pragma: no-cache\r\n"
737 "Cache-Control: no-cache\r\n"
738 "Content-Length: 32767\r\n"
739 "Expires: Sun, 9 Jan 1972 00:00:00 GMT\r\n"
741 conn->proxy_host ? "http://" : "",
742 conn->proxy_host ? url->host : "",
743 conn->proxy_host ? url_port_str : "",
744 url->abspath, url->query ? "?" : "", url->query ? url->query : "",
745 hostparam ? hostparam : "", conn->tunnelid);
747 res = gst_rtsp_connection_write (conn, (guint8 *) str, strlen (str), timeout);
749 if (res != GST_RTSP_OK)
754 g_free (url_port_str);
761 GST_ERROR ("write failed (%d)", res);
775 res = GST_RTSP_ETIMEOUT;
785 res = GST_RTSP_EINTR;
790 GST_ERROR ("got failure response %d %s", code, resultstr);
791 res = GST_RTSP_ERROR;
796 GST_ERROR ("could not resolve %s", conn->ip);
802 GST_ERROR ("failed to connect");
808 * gst_rtsp_connection_connect:
809 * @conn: a #GstRTSPConnection
810 * @timeout: a #GTimeVal timeout
812 * Attempt to connect to the url of @conn made with
813 * gst_rtsp_connection_create(). If @timeout is #NULL this function can block
814 * forever. If @timeout contains a valid timeout, this function will return
815 * #GST_RTSP_ETIMEOUT after the timeout expired.
817 * This function can be cancelled with gst_rtsp_connection_flush().
819 * Returns: #GST_RTSP_OK when a connection could be made.
822 gst_rtsp_connection_connect (GstRTSPConnection * conn, GTimeVal * timeout)
829 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
830 g_return_val_if_fail (conn->url != NULL, GST_RTSP_EINVAL);
831 g_return_val_if_fail (conn->fd0.fd < 0, GST_RTSP_EINVAL);
835 if (conn->proxy_host && conn->tunneled) {
836 if (!(ip = do_resolve (conn->proxy_host))) {
837 GST_ERROR ("could not resolve %s", conn->proxy_host);
840 port = conn->proxy_port;
841 g_free (conn->proxy_host);
842 conn->proxy_host = ip;
844 if (!(ip = do_resolve (url->host))) {
845 GST_ERROR ("could not resolve %s", url->host);
848 /* get the port from the url */
849 gst_rtsp_url_get_port (url, &port);
855 /* connect to the host/port */
856 res = do_connect (ip, port, &conn->fd0, conn->fdset, timeout);
857 if (res != GST_RTSP_OK)
860 /* this is our read URL */
861 conn->readfd = &conn->fd0;
863 if (conn->tunneled) {
864 res = setup_tunneling (conn, timeout);
865 if (res != GST_RTSP_OK)
866 goto tunneling_failed;
868 conn->writefd = &conn->fd0;
875 return GST_RTSP_ENET;
879 GST_ERROR ("failed to connect");
884 GST_ERROR ("failed to setup tunneling");
890 auth_digest_compute_hex_urp (const gchar * username,
891 const gchar * realm, const gchar * password, gchar hex_urp[33])
893 GChecksum *md5_context = g_checksum_new (G_CHECKSUM_MD5);
894 const gchar *digest_string;
896 g_checksum_update (md5_context, (const guchar *) username, strlen (username));
897 g_checksum_update (md5_context, (const guchar *) ":", 1);
898 g_checksum_update (md5_context, (const guchar *) realm, strlen (realm));
899 g_checksum_update (md5_context, (const guchar *) ":", 1);
900 g_checksum_update (md5_context, (const guchar *) password, strlen (password));
901 digest_string = g_checksum_get_string (md5_context);
903 memset (hex_urp, 0, 33);
904 memcpy (hex_urp, digest_string, strlen (digest_string));
906 g_checksum_free (md5_context);
910 auth_digest_compute_response (const gchar * method,
911 const gchar * uri, const gchar * hex_a1, const gchar * nonce,
914 char hex_a2[33] = { 0, };
915 GChecksum *md5_context = g_checksum_new (G_CHECKSUM_MD5);
916 const gchar *digest_string;
919 g_checksum_update (md5_context, (const guchar *) method, strlen (method));
920 g_checksum_update (md5_context, (const guchar *) ":", 1);
921 g_checksum_update (md5_context, (const guchar *) uri, strlen (uri));
922 digest_string = g_checksum_get_string (md5_context);
923 memcpy (hex_a2, digest_string, strlen (digest_string));
926 #if GLIB_CHECK_VERSION (2, 18, 0)
927 g_checksum_reset (md5_context);
929 g_checksum_free (md5_context);
930 md5_context = g_checksum_new (G_CHECKSUM_MD5);
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 user_pass = g_strdup_printf ("%s:%s", conn->username, conn->passwd);
955 user_pass64 = g_base64_encode ((guchar *) user_pass, strlen (user_pass));
956 auth_string = g_strdup_printf ("Basic %s", user_pass64);
958 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
962 g_free (user_pass64);
965 case GST_RTSP_AUTH_DIGEST:{
966 gchar response[33], hex_urp[33];
967 gchar *auth_string, *auth_string2;
974 /* we need to have some params set */
975 if (conn->auth_params == NULL)
978 /* we need the realm and nonce */
979 realm = (gchar *) g_hash_table_lookup (conn->auth_params, "realm");
980 nonce = (gchar *) g_hash_table_lookup (conn->auth_params, "nonce");
981 if (realm == NULL || nonce == NULL)
984 auth_digest_compute_hex_urp (conn->username, realm, conn->passwd,
987 method = gst_rtsp_method_as_text (message->type_data.request.method);
988 uri = message->type_data.request.uri;
990 /* Assume no qop, algorithm=md5, stale=false */
991 /* For algorithm MD5, a1 = urp. */
992 auth_digest_compute_response (method, uri, hex_urp, nonce, response);
993 auth_string = g_strdup_printf ("Digest username=\"%s\", "
994 "realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"",
995 conn->username, realm, nonce, uri, response);
997 opaque = (gchar *) g_hash_table_lookup (conn->auth_params, "opaque");
999 auth_string2 = g_strdup_printf ("%s, opaque=\"%s\"", auth_string,
1001 g_free (auth_string);
1002 auth_string = auth_string2;
1004 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
1015 gen_date_string (gchar * date_string, guint len)
1019 #ifdef HAVE_GMTIME_R
1023 g_get_current_time (&tv);
1024 t = (time_t) tv.tv_sec;
1026 #ifdef HAVE_GMTIME_R
1027 strftime (date_string, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime_r (&t, &tm_));
1029 strftime (date_string, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime (&t));
1033 static GstRTSPResult
1034 write_bytes (gint fd, const guint8 * buffer, guint * idx, guint size)
1038 if (G_UNLIKELY (*idx > size))
1039 return GST_RTSP_ERROR;
1046 r = WRITE_SOCKET (fd, &buffer[*idx], left);
1047 if (G_UNLIKELY (r == 0)) {
1048 return GST_RTSP_EINTR;
1049 } else if (G_UNLIKELY (r < 0)) {
1050 if (ERRNO_IS_EAGAIN)
1051 return GST_RTSP_EINTR;
1052 if (!ERRNO_IS_EINTR)
1053 return GST_RTSP_ESYS;
1063 fill_raw_bytes (GstRTSPConnection * conn, guint8 * buffer, guint size)
1067 if (G_UNLIKELY (conn->initial_buffer != NULL)) {
1068 gsize left = strlen (&conn->initial_buffer[conn->initial_buffer_offset]);
1070 out = MIN (left, size);
1071 memcpy (buffer, &conn->initial_buffer[conn->initial_buffer_offset], out);
1073 if (left == (gsize) out) {
1074 g_free (conn->initial_buffer);
1075 conn->initial_buffer = NULL;
1076 conn->initial_buffer_offset = 0;
1078 conn->initial_buffer_offset += out;
1081 if (G_LIKELY (size > (guint) out)) {
1084 r = READ_SOCKET (conn->readfd->fd, &buffer[out], size - out);
1096 fill_bytes (GstRTSPConnection * conn, guint8 * buffer, guint size)
1098 DecodeCtx *ctx = conn->ctxp;
1103 guint8 in[sizeof (ctx->out) * 4 / 3];
1106 while (size > 0 && ctx->cout < ctx->coutl) {
1107 /* we have some leftover bytes */
1108 *buffer++ = ctx->out[ctx->cout++];
1113 /* got what we needed? */
1117 /* try to read more bytes */
1118 r = fill_raw_bytes (conn, in, sizeof (in));
1127 g_base64_decode_step ((gchar *) in, r, ctx->out, &ctx->state,
1131 out = fill_raw_bytes (conn, buffer, size);
1137 static GstRTSPResult
1138 read_bytes (GstRTSPConnection * conn, guint8 * buffer, guint * idx, guint size)
1142 if (G_UNLIKELY (*idx > size))
1143 return GST_RTSP_ERROR;
1150 r = fill_bytes (conn, &buffer[*idx], left);
1151 if (G_UNLIKELY (r == 0)) {
1152 return GST_RTSP_EEOF;
1153 } else if (G_UNLIKELY (r < 0)) {
1154 if (ERRNO_IS_EAGAIN)
1155 return GST_RTSP_EINTR;
1156 if (!ERRNO_IS_EINTR)
1157 return GST_RTSP_ESYS;
1166 static GstRTSPResult
1167 read_line (GstRTSPConnection * conn, guint8 * buffer, guint * idx, guint size)
1173 r = fill_bytes (conn, &c, 1);
1174 if (G_UNLIKELY (r == 0)) {
1175 return GST_RTSP_EEOF;
1176 } else if (G_UNLIKELY (r < 0)) {
1177 if (ERRNO_IS_EAGAIN)
1178 return GST_RTSP_EINTR;
1179 if (!ERRNO_IS_EINTR)
1180 return GST_RTSP_ESYS;
1182 if (c == '\n') /* end on \n */
1184 if (c == '\r') /* ignore \r */
1187 if (G_LIKELY (*idx < size - 1))
1188 buffer[(*idx)++] = c;
1191 buffer[*idx] = '\0';
1197 * gst_rtsp_connection_write:
1198 * @conn: a #GstRTSPConnection
1199 * @data: the data to write
1200 * @size: the size of @data
1201 * @timeout: a timeout value or #NULL
1203 * Attempt to write @size bytes of @data to the connected @conn, blocking up to
1204 * the specified @timeout. @timeout can be #NULL, in which case this function
1205 * might block forever.
1207 * This function can be cancelled with gst_rtsp_connection_flush().
1209 * Returns: #GST_RTSP_OK on success.
1212 gst_rtsp_connection_write (GstRTSPConnection * conn, const guint8 * data,
1213 guint size, GTimeVal * timeout)
1220 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1221 g_return_val_if_fail (data != NULL || size == 0, GST_RTSP_EINVAL);
1222 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
1224 gst_poll_set_controllable (conn->fdset, TRUE);
1225 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, TRUE);
1226 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, FALSE);
1227 /* clear all previous poll results */
1228 gst_poll_fd_ignored (conn->fdset, conn->writefd);
1229 gst_poll_fd_ignored (conn->fdset, conn->readfd);
1231 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1237 res = write_bytes (conn->writefd->fd, data, &offset, size);
1238 if (G_LIKELY (res == GST_RTSP_OK))
1240 if (G_UNLIKELY (res != GST_RTSP_EINTR))
1243 /* not all is written, wait until we can write more */
1245 retval = gst_poll_wait (conn->fdset, to);
1246 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1248 if (G_UNLIKELY (retval == 0))
1251 if (G_UNLIKELY (retval == -1)) {
1263 return GST_RTSP_ETIMEOUT;
1267 return GST_RTSP_ESYS;
1271 return GST_RTSP_EINTR;
1280 message_to_string (GstRTSPConnection * conn, GstRTSPMessage * message)
1282 GString *str = NULL;
1284 str = g_string_new ("");
1286 switch (message->type) {
1287 case GST_RTSP_MESSAGE_REQUEST:
1288 /* create request string, add CSeq */
1289 g_string_append_printf (str, "%s %s RTSP/1.0\r\n"
1291 gst_rtsp_method_as_text (message->type_data.request.method),
1292 message->type_data.request.uri, conn->cseq++);
1293 /* add session id if we have one */
1294 if (conn->session_id[0] != '\0') {
1295 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
1296 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SESSION,
1299 /* add any authentication headers */
1300 add_auth_header (conn, message);
1302 case GST_RTSP_MESSAGE_RESPONSE:
1303 /* create response string */
1304 g_string_append_printf (str, "RTSP/1.0 %d %s\r\n",
1305 message->type_data.response.code, message->type_data.response.reason);
1307 case GST_RTSP_MESSAGE_DATA:
1309 guint8 data_header[4];
1311 /* prepare data header */
1312 data_header[0] = '$';
1313 data_header[1] = message->type_data.data.channel;
1314 data_header[2] = (message->body_size >> 8) & 0xff;
1315 data_header[3] = message->body_size & 0xff;
1317 /* create string with header and data */
1318 str = g_string_append_len (str, (gchar *) data_header, 4);
1320 g_string_append_len (str, (gchar *) message->body,
1321 message->body_size);
1325 g_string_free (str, TRUE);
1326 g_return_val_if_reached (NULL);
1330 /* append headers and body */
1331 if (message->type != GST_RTSP_MESSAGE_DATA) {
1332 gchar date_string[100];
1334 gen_date_string (date_string, sizeof (date_string));
1336 /* add date header */
1337 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_DATE, -1);
1338 gst_rtsp_message_add_header (message, GST_RTSP_HDR_DATE, date_string);
1340 /* append headers */
1341 gst_rtsp_message_append_headers (message, str);
1343 /* append Content-Length and body if needed */
1344 if (message->body != NULL && message->body_size > 0) {
1347 len = g_strdup_printf ("%d", message->body_size);
1348 g_string_append_printf (str, "%s: %s\r\n",
1349 gst_rtsp_header_as_text (GST_RTSP_HDR_CONTENT_LENGTH), len);
1351 /* header ends here */
1352 g_string_append (str, "\r\n");
1354 g_string_append_len (str, (gchar *) message->body,
1355 message->body_size);
1357 /* just end headers */
1358 g_string_append (str, "\r\n");
1366 * gst_rtsp_connection_send:
1367 * @conn: a #GstRTSPConnection
1368 * @message: the message to send
1369 * @timeout: a timeout value or #NULL
1371 * Attempt to send @message to the connected @conn, blocking up to
1372 * the specified @timeout. @timeout can be #NULL, in which case this function
1373 * might block forever.
1375 * This function can be cancelled with gst_rtsp_connection_flush().
1377 * Returns: #GST_RTSP_OK on success.
1380 gst_rtsp_connection_send (GstRTSPConnection * conn, GstRTSPMessage * message,
1383 GString *string = NULL;
1388 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1389 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1391 if (G_UNLIKELY (!(string = message_to_string (conn, message))))
1394 if (conn->tunneled) {
1395 str = g_base64_encode ((const guchar *) string->str, string->len);
1396 g_string_free (string, TRUE);
1401 g_string_free (string, FALSE);
1405 res = gst_rtsp_connection_write (conn, (guint8 *) str, len, timeout);
1413 g_warning ("Wrong message");
1414 return GST_RTSP_EINVAL;
1419 parse_string (gchar * dest, gint size, gchar ** src)
1425 while (g_ascii_isspace (**src))
1428 while (!g_ascii_isspace (**src) && **src != '\0') {
1430 dest[idx++] = **src;
1438 parse_key (gchar * dest, gint size, gchar ** src)
1443 while (**src != ':' && **src != '\0') {
1445 dest[idx++] = **src;
1452 static GstRTSPResult
1453 parse_response_status (guint8 * buffer, GstRTSPMessage * msg)
1456 gchar versionstr[20];
1461 bptr = (gchar *) buffer;
1463 parse_string (versionstr, sizeof (versionstr), &bptr);
1464 parse_string (codestr, sizeof (codestr), &bptr);
1465 code = atoi (codestr);
1467 while (g_ascii_isspace (*bptr))
1470 if (strcmp (versionstr, "RTSP/1.0") == 0)
1471 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
1473 else if (strncmp (versionstr, "RTSP/", 5) == 0) {
1474 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
1476 msg->type_data.response.version = GST_RTSP_VERSION_INVALID;
1484 return GST_RTSP_EPARSE;
1488 static GstRTSPResult
1489 parse_request_line (GstRTSPConnection * conn, guint8 * buffer,
1490 GstRTSPMessage * msg)
1492 GstRTSPResult res = GST_RTSP_OK;
1493 gchar versionstr[20];
1494 gchar methodstr[20];
1497 GstRTSPMethod method;
1498 GstRTSPTunnelState tstate = TUNNEL_STATE_NONE;
1500 bptr = (gchar *) buffer;
1502 parse_string (methodstr, sizeof (methodstr), &bptr);
1503 method = gst_rtsp_find_method (methodstr);
1504 if (method == GST_RTSP_INVALID) {
1505 /* a tunnel request is allowed when we don't have one yet */
1506 if (conn->tstate != TUNNEL_STATE_NONE)
1507 goto invalid_method;
1508 /* we need GET or POST for a valid tunnel request */
1509 if (!strcmp (methodstr, "GET"))
1510 tstate = TUNNEL_STATE_GET;
1511 else if (!strcmp (methodstr, "POST"))
1512 tstate = TUNNEL_STATE_POST;
1514 goto invalid_method;
1517 parse_string (urlstr, sizeof (urlstr), &bptr);
1518 if (G_UNLIKELY (*urlstr == '\0'))
1521 parse_string (versionstr, sizeof (versionstr), &bptr);
1523 if (G_UNLIKELY (*bptr != '\0'))
1524 goto invalid_version;
1526 if (strcmp (versionstr, "RTSP/1.0") == 0) {
1527 res = gst_rtsp_message_init_request (msg, method, urlstr);
1528 } else if (strncmp (versionstr, "RTSP/", 5) == 0) {
1529 res = gst_rtsp_message_init_request (msg, method, urlstr);
1530 msg->type_data.request.version = GST_RTSP_VERSION_INVALID;
1531 } else if (strcmp (versionstr, "HTTP/1.0") == 0) {
1532 /* tunnel request, we need a tunnel method */
1533 if (tstate == TUNNEL_STATE_NONE) {
1534 res = GST_RTSP_EPARSE;
1536 conn->tstate = tstate;
1539 res = GST_RTSP_EPARSE;
1547 GST_ERROR ("invalid method %s", methodstr);
1548 return GST_RTSP_EPARSE;
1552 GST_ERROR ("invalid url %s", urlstr);
1553 return GST_RTSP_EPARSE;
1557 GST_ERROR ("invalid version");
1558 return GST_RTSP_EPARSE;
1562 static GstRTSPResult
1563 parse_key_value (guint8 * buffer, gchar * key, guint keysize, gchar ** value)
1567 bptr = (gchar *) buffer;
1570 parse_key (key, keysize, &bptr);
1571 if (G_UNLIKELY (*bptr != ':'))
1575 while (g_ascii_isspace (*bptr))
1585 return GST_RTSP_EPARSE;
1589 /* parsing lines means reading a Key: Value pair */
1590 static GstRTSPResult
1591 parse_line (GstRTSPConnection * conn, guint8 * buffer, GstRTSPMessage * msg)
1596 GstRTSPHeaderField field;
1598 res = parse_key_value (buffer, key, sizeof (key), &value);
1599 if (G_UNLIKELY (res != GST_RTSP_OK))
1602 if (conn->tstate == TUNNEL_STATE_GET || conn->tstate == TUNNEL_STATE_POST) {
1603 /* save the tunnel session in the connection */
1604 if (!strcmp (key, "x-sessioncookie")) {
1605 strncpy (conn->tunnelid, value, TUNNELID_LEN);
1606 conn->tunnelid[TUNNELID_LEN - 1] = '\0';
1607 conn->tunneled = TRUE;
1610 field = gst_rtsp_find_header_field (key);
1611 if (field != GST_RTSP_HDR_INVALID)
1612 gst_rtsp_message_add_header (msg, field, value);
1625 * GST_RTSP_OK when a complete message was read.
1626 * GST_RTSP_EEOF: when the socket is closed
1627 * GST_RTSP_EINTR: when more data is needed.
1628 * GST_RTSP_..: some other error occured.
1630 static GstRTSPResult
1631 build_next (GstRTSPBuilder * builder, GstRTSPMessage * message,
1632 GstRTSPConnection * conn)
1637 switch (builder->state) {
1639 builder->offset = 0;
1641 read_bytes (conn, (guint8 *) builder->buffer, &builder->offset, 1);
1642 if (res != GST_RTSP_OK)
1645 /* we have 1 bytes now and we can see if this is a data message or
1647 if (builder->buffer[0] == '$') {
1648 /* data message, prepare for the header */
1649 builder->state = STATE_DATA_HEADER;
1652 builder->state = STATE_READ_LINES;
1655 case STATE_DATA_HEADER:
1658 read_bytes (conn, (guint8 *) builder->buffer, &builder->offset, 4);
1659 if (res != GST_RTSP_OK)
1662 gst_rtsp_message_init_data (message, builder->buffer[1]);
1664 builder->body_len = (builder->buffer[2] << 8) | builder->buffer[3];
1665 builder->body_data = g_malloc (builder->body_len + 1);
1666 builder->body_data[builder->body_len] = '\0';
1667 builder->offset = 0;
1668 builder->state = STATE_DATA_BODY;
1671 case STATE_DATA_BODY:
1674 read_bytes (conn, builder->body_data, &builder->offset,
1676 if (res != GST_RTSP_OK)
1679 /* we have the complete body now, store in the message adjusting the
1680 * length to include the traling '\0' */
1681 gst_rtsp_message_take_body (message,
1682 (guint8 *) builder->body_data, builder->body_len + 1);
1683 builder->body_data = NULL;
1684 builder->body_len = 0;
1686 builder->state = STATE_END;
1689 case STATE_READ_LINES:
1691 res = read_line (conn, builder->buffer, &builder->offset,
1692 sizeof (builder->buffer));
1693 if (res != GST_RTSP_OK)
1696 /* we have a regular response */
1697 if (builder->buffer[0] == '\r') {
1698 builder->buffer[0] = '\0';
1701 if (builder->buffer[0] == '\0') {
1704 /* empty line, end of message header */
1705 /* see if there is a Content-Length header */
1706 if (gst_rtsp_message_get_header (message,
1707 GST_RTSP_HDR_CONTENT_LENGTH, &hdrval, 0) == GST_RTSP_OK) {
1708 /* there is, prepare to read the body */
1709 builder->body_len = atol (hdrval);
1710 builder->body_data = g_malloc (builder->body_len + 1);
1711 builder->body_data[builder->body_len] = '\0';
1712 builder->offset = 0;
1713 builder->state = STATE_DATA_BODY;
1715 builder->state = STATE_END;
1720 /* we have a line */
1721 if (builder->line == 0) {
1722 /* first line, check for response status */
1723 if (memcmp (builder->buffer, "RTSP", 4) == 0) {
1724 res = parse_response_status (builder->buffer, message);
1726 res = parse_request_line (conn, builder->buffer, message);
1728 /* the first line must parse without errors */
1729 if (res != GST_RTSP_OK)
1732 /* else just parse the line, ignore errors */
1733 parse_line (conn, builder->buffer, message);
1736 builder->offset = 0;
1743 if (conn->tstate == TUNNEL_STATE_GET) {
1744 res = GST_RTSP_ETGET;
1746 } else if (conn->tstate == TUNNEL_STATE_POST) {
1747 res = GST_RTSP_ETPOST;
1751 if (message->type == GST_RTSP_MESSAGE_DATA) {
1752 /* data messages don't have headers */
1757 /* save session id in the connection for further use */
1758 if (message->type == GST_RTSP_MESSAGE_RESPONSE &&
1759 gst_rtsp_message_get_header (message, GST_RTSP_HDR_SESSION,
1760 &session_id, 0) == GST_RTSP_OK) {
1763 maxlen = sizeof (conn->session_id) - 1;
1764 /* the sessionid can have attributes marked with ;
1765 * Make sure we strip them */
1766 for (i = 0; session_id[i] != '\0'; i++) {
1767 if (session_id[i] == ';') {
1772 } while (g_ascii_isspace (session_id[i]));
1773 if (g_str_has_prefix (&session_id[i], "timeout=")) {
1776 /* if we parsed something valid, configure */
1777 if ((to = atoi (&session_id[i + 8])) > 0)
1784 /* make sure to not overflow */
1785 strncpy (conn->session_id, session_id, maxlen);
1786 conn->session_id[maxlen] = '\0';
1792 res = GST_RTSP_ERROR;
1801 * gst_rtsp_connection_read:
1802 * @conn: a #GstRTSPConnection
1803 * @data: the data to read
1804 * @size: the size of @data
1805 * @timeout: a timeout value or #NULL
1807 * Attempt to read @size bytes into @data from the connected @conn, blocking up to
1808 * the specified @timeout. @timeout can be #NULL, in which case this function
1809 * might block forever.
1811 * This function can be cancelled with gst_rtsp_connection_flush().
1813 * Returns: #GST_RTSP_OK on success.
1816 gst_rtsp_connection_read (GstRTSPConnection * conn, guint8 * data, guint size,
1824 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1825 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
1826 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1828 if (G_UNLIKELY (size == 0))
1833 /* configure timeout if any */
1834 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1836 gst_poll_set_controllable (conn->fdset, TRUE);
1837 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
1838 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
1841 res = read_bytes (conn, data, &offset, size);
1842 if (G_UNLIKELY (res == GST_RTSP_EEOF))
1844 if (G_LIKELY (res == GST_RTSP_OK))
1846 if (G_UNLIKELY (res != GST_RTSP_EINTR))
1850 retval = gst_poll_wait (conn->fdset, to);
1851 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1853 /* check for timeout */
1854 if (G_UNLIKELY (retval == 0))
1855 goto select_timeout;
1857 if (G_UNLIKELY (retval == -1)) {
1863 gst_poll_set_controllable (conn->fdset, FALSE);
1870 return GST_RTSP_ESYS;
1874 return GST_RTSP_ETIMEOUT;
1878 return GST_RTSP_EINTR;
1882 return GST_RTSP_EEOF;
1891 gen_tunnel_reply (GstRTSPConnection * conn, GstRTSPStatusCode code)
1894 gchar date_string[100];
1895 const gchar *status;
1897 gen_date_string (date_string, sizeof (date_string));
1899 status = gst_rtsp_status_as_text (code);
1900 if (status == NULL) {
1901 code = GST_RTSP_STS_INTERNAL_SERVER_ERROR;
1902 status = "Internal Server Error";
1905 str = g_string_new ("");
1908 g_string_append_printf (str, "HTTP/1.0 %d %s\r\n", code, status);
1909 g_string_append_printf (str,
1910 "Server: GStreamer RTSP Server\r\n"
1912 "Connection: close\r\n"
1913 "Cache-Control: no-store\r\n" "Pragma: no-cache\r\n", date_string);
1914 if (code == GST_RTSP_STS_OK) {
1916 g_string_append_printf (str, "x-server-ip-address: %s\r\n", conn->ip);
1917 g_string_append_printf (str,
1918 "Content-Type: application/x-rtsp-tunnelled\r\n");
1920 g_string_append_printf (str, "\r\n");
1925 * gst_rtsp_connection_receive:
1926 * @conn: a #GstRTSPConnection
1927 * @message: the message to read
1928 * @timeout: a timeout value or #NULL
1930 * Attempt to read into @message from the connected @conn, blocking up to
1931 * the specified @timeout. @timeout can be #NULL, in which case this function
1932 * might block forever.
1934 * This function can be cancelled with gst_rtsp_connection_flush().
1936 * Returns: #GST_RTSP_OK on success.
1939 gst_rtsp_connection_receive (GstRTSPConnection * conn, GstRTSPMessage * message,
1943 GstRTSPBuilder builder;
1947 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1948 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1949 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1951 /* configure timeout if any */
1952 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1954 gst_poll_set_controllable (conn->fdset, TRUE);
1955 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
1956 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
1958 memset (&builder, 0, sizeof (GstRTSPBuilder));
1960 res = build_next (&builder, message, conn);
1961 if (G_UNLIKELY (res == GST_RTSP_EEOF))
1963 if (G_LIKELY (res == GST_RTSP_OK))
1965 if (res == GST_RTSP_ETGET) {
1968 /* tunnel GET request, we can reply now */
1969 str = gen_tunnel_reply (conn, GST_RTSP_STS_OK);
1971 gst_rtsp_connection_write (conn, (guint8 *) str->str, str->len,
1973 g_string_free (str, TRUE);
1974 } else if (res == GST_RTSP_ETPOST) {
1975 /* tunnel POST request, return the value, the caller now has to link the
1976 * two connections. */
1978 } else if (G_UNLIKELY (res != GST_RTSP_EINTR))
1982 retval = gst_poll_wait (conn->fdset, to);
1983 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1985 /* check for timeout */
1986 if (G_UNLIKELY (retval == 0))
1987 goto select_timeout;
1989 if (G_UNLIKELY (retval == -1)) {
1995 gst_poll_set_controllable (conn->fdset, FALSE);
1998 /* we have a message here */
1999 build_reset (&builder);
2006 res = GST_RTSP_ESYS;
2011 res = GST_RTSP_ETIMEOUT;
2016 res = GST_RTSP_EINTR;
2021 res = GST_RTSP_EEOF;
2027 build_reset (&builder);
2028 gst_rtsp_message_unset (message);
2034 * gst_rtsp_connection_close:
2035 * @conn: a #GstRTSPConnection
2037 * Close the connected @conn. After this call, the connection is in the same
2038 * state as when it was first created.
2040 * Returns: #GST_RTSP_OK on success.
2043 gst_rtsp_connection_close (GstRTSPConnection * conn)
2045 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2050 g_free (conn->initial_buffer);
2051 conn->initial_buffer = NULL;
2052 conn->initial_buffer_offset = 0;
2054 REMOVE_POLLFD (conn->fdset, &conn->fd0);
2055 REMOVE_POLLFD (conn->fdset, &conn->fd1);
2056 conn->writefd = NULL;
2057 conn->readfd = NULL;
2058 conn->tunneled = FALSE;
2059 conn->tstate = TUNNEL_STATE_NONE;
2061 g_free (conn->username);
2062 conn->username = NULL;
2063 g_free (conn->passwd);
2064 conn->passwd = NULL;
2065 gst_rtsp_connection_clear_auth_params (conn);
2068 conn->session_id[0] = '\0';
2074 * gst_rtsp_connection_free:
2075 * @conn: a #GstRTSPConnection
2077 * Close and free @conn.
2079 * Returns: #GST_RTSP_OK on success.
2082 gst_rtsp_connection_free (GstRTSPConnection * conn)
2086 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2088 res = gst_rtsp_connection_close (conn);
2089 gst_poll_free (conn->fdset);
2090 g_timer_destroy (conn->timer);
2091 gst_rtsp_url_free (conn->url);
2092 g_free (conn->proxy_host);
2102 * gst_rtsp_connection_poll:
2103 * @conn: a #GstRTSPConnection
2104 * @events: a bitmask of #GstRTSPEvent flags to check
2105 * @revents: location for result flags
2106 * @timeout: a timeout
2108 * Wait up to the specified @timeout for the connection to become available for
2109 * at least one of the operations specified in @events. When the function returns
2110 * with #GST_RTSP_OK, @revents will contain a bitmask of available operations on
2113 * @timeout can be #NULL, in which case this function might block forever.
2115 * This function can be cancelled with gst_rtsp_connection_flush().
2117 * Returns: #GST_RTSP_OK on success.
2122 gst_rtsp_connection_poll (GstRTSPConnection * conn, GstRTSPEvent events,
2123 GstRTSPEvent * revents, GTimeVal * timeout)
2128 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2129 g_return_val_if_fail (events != 0, GST_RTSP_EINVAL);
2130 g_return_val_if_fail (revents != NULL, GST_RTSP_EINVAL);
2131 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2132 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2134 gst_poll_set_controllable (conn->fdset, TRUE);
2136 /* add fd to writer set when asked to */
2137 gst_poll_fd_ctl_write (conn->fdset, conn->writefd,
2138 events & GST_RTSP_EV_WRITE);
2140 /* add fd to reader set when asked to */
2141 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, events & GST_RTSP_EV_READ);
2143 /* configure timeout if any */
2144 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
2147 retval = gst_poll_wait (conn->fdset, to);
2148 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
2150 if (G_UNLIKELY (retval == 0))
2151 goto select_timeout;
2153 if (G_UNLIKELY (retval == -1)) {
2161 if (events & GST_RTSP_EV_READ) {
2162 if (gst_poll_fd_can_read (conn->fdset, conn->readfd))
2163 *revents |= GST_RTSP_EV_READ;
2165 if (events & GST_RTSP_EV_WRITE) {
2166 if (gst_poll_fd_can_write (conn->fdset, conn->writefd))
2167 *revents |= GST_RTSP_EV_WRITE;
2174 return GST_RTSP_ETIMEOUT;
2178 return GST_RTSP_ESYS;
2182 return GST_RTSP_EINTR;
2187 * gst_rtsp_connection_next_timeout:
2188 * @conn: a #GstRTSPConnection
2189 * @timeout: a timeout
2191 * Calculate the next timeout for @conn, storing the result in @timeout.
2193 * Returns: #GST_RTSP_OK.
2196 gst_rtsp_connection_next_timeout (GstRTSPConnection * conn, GTimeVal * timeout)
2202 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2203 g_return_val_if_fail (timeout != NULL, GST_RTSP_EINVAL);
2205 elapsed = g_timer_elapsed (conn->timer, &usec);
2206 if (elapsed >= conn->timeout) {
2210 sec = conn->timeout - elapsed;
2213 timeout->tv_sec = sec;
2214 timeout->tv_usec = usec;
2220 * gst_rtsp_connection_reset_timeout:
2221 * @conn: a #GstRTSPConnection
2223 * Reset the timeout of @conn.
2225 * Returns: #GST_RTSP_OK.
2228 gst_rtsp_connection_reset_timeout (GstRTSPConnection * conn)
2230 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2232 g_timer_start (conn->timer);
2238 * gst_rtsp_connection_flush:
2239 * @conn: a #GstRTSPConnection
2240 * @flush: start or stop the flush
2242 * Start or stop the flushing action on @conn. When flushing, all current
2243 * and future actions on @conn will return #GST_RTSP_EINTR until the connection
2244 * is set to non-flushing mode again.
2246 * Returns: #GST_RTSP_OK.
2249 gst_rtsp_connection_flush (GstRTSPConnection * conn, gboolean flush)
2251 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2253 gst_poll_set_flushing (conn->fdset, flush);
2259 * gst_rtsp_connection_set_proxy:
2260 * @conn: a #GstRTSPConnection
2261 * @host: the proxy host
2262 * @port: the proxy port
2264 * Set the proxy host and port.
2266 * Returns: #GST_RTSP_OK.
2271 gst_rtsp_connection_set_proxy (GstRTSPConnection * conn,
2272 const gchar * host, guint port)
2274 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2276 g_free (conn->proxy_host);
2277 conn->proxy_host = g_strdup (host);
2278 conn->proxy_port = port;
2284 * gst_rtsp_connection_set_auth:
2285 * @conn: a #GstRTSPConnection
2286 * @method: authentication method
2288 * @pass: the password
2290 * Configure @conn for authentication mode @method with @user and @pass as the
2291 * user and password respectively.
2293 * Returns: #GST_RTSP_OK.
2296 gst_rtsp_connection_set_auth (GstRTSPConnection * conn,
2297 GstRTSPAuthMethod method, const gchar * user, const gchar * pass)
2299 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2301 if (method == GST_RTSP_AUTH_DIGEST && ((user == NULL || pass == NULL)
2302 || g_strrstr (user, ":") != NULL))
2303 return GST_RTSP_EINVAL;
2305 /* Make sure the username and passwd are being set for authentication */
2306 if (method == GST_RTSP_AUTH_NONE && (user == NULL || pass == NULL))
2307 return GST_RTSP_EINVAL;
2309 /* ":" chars are not allowed in usernames for basic auth */
2310 if (method == GST_RTSP_AUTH_BASIC && g_strrstr (user, ":") != NULL)
2311 return GST_RTSP_EINVAL;
2313 g_free (conn->username);
2314 g_free (conn->passwd);
2316 conn->auth_method = method;
2317 conn->username = g_strdup (user);
2318 conn->passwd = g_strdup (pass);
2325 * @key: ASCII string to hash
2327 * Hashes @key in a case-insensitive manner.
2329 * Returns: the hash code.
2332 str_case_hash (gconstpointer key)
2334 const char *p = key;
2335 guint h = g_ascii_toupper (*p);
2338 for (p += 1; *p != '\0'; p++)
2339 h = (h << 5) - h + g_ascii_toupper (*p);
2346 * @v1: an ASCII string
2347 * @v2: another ASCII string
2349 * Compares @v1 and @v2 in a case-insensitive manner
2351 * Returns: %TRUE if they are equal (modulo case)
2354 str_case_equal (gconstpointer v1, gconstpointer v2)
2356 const char *string1 = v1;
2357 const char *string2 = v2;
2359 return g_ascii_strcasecmp (string1, string2) == 0;
2363 * gst_rtsp_connection_set_auth_param:
2364 * @conn: a #GstRTSPConnection
2365 * @param: authentication directive
2368 * Setup @conn with authentication directives. This is not necesary for
2369 * methods #GST_RTSP_AUTH_NONE and #GST_RTSP_AUTH_BASIC. For
2370 * #GST_RTSP_AUTH_DIGEST, directives should be taken from the digest challenge
2371 * in the WWW-Authenticate response header and can include realm, domain,
2372 * nonce, opaque, stale, algorithm, qop as per RFC2617.
2377 gst_rtsp_connection_set_auth_param (GstRTSPConnection * conn,
2378 const gchar * param, const gchar * value)
2380 g_return_if_fail (conn != NULL);
2381 g_return_if_fail (param != NULL);
2383 if (conn->auth_params == NULL) {
2385 g_hash_table_new_full (str_case_hash, str_case_equal, g_free, g_free);
2387 g_hash_table_insert (conn->auth_params, g_strdup (param), g_strdup (value));
2391 * gst_rtsp_connection_clear_auth_params:
2392 * @conn: a #GstRTSPConnection
2394 * Clear the list of authentication directives stored in @conn.
2399 gst_rtsp_connection_clear_auth_params (GstRTSPConnection * conn)
2401 g_return_if_fail (conn != NULL);
2403 if (conn->auth_params != NULL) {
2404 g_hash_table_destroy (conn->auth_params);
2405 conn->auth_params = NULL;
2409 static GstRTSPResult
2410 set_qos_dscp (gint fd, guint qos_dscp)
2412 union gst_sockaddr sa;
2413 socklen_t slen = sizeof (sa);
2420 if (getsockname (fd, &sa.sa, &slen) < 0)
2421 goto no_getsockname;
2423 af = sa.sa.sa_family;
2425 /* if this is an IPv4-mapped address then do IPv4 QoS */
2426 if (af == AF_INET6) {
2427 if (IN6_IS_ADDR_V4MAPPED (&sa.sa_in6.sin6_addr))
2431 /* extract and shift 6 bits of the DSCP */
2432 tos = (qos_dscp & 0x3f) << 2;
2436 if (SETSOCKOPT (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0)
2441 if (SETSOCKOPT (fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof (tos)) < 0)
2455 return GST_RTSP_ESYS;
2460 return GST_RTSP_ERROR;
2465 * gst_rtsp_connection_set_qos_dscp:
2466 * @conn: a #GstRTSPConnection
2467 * @qos_dscp: DSCP value
2469 * Configure @conn to use the specified DSCP value.
2471 * Returns: #GST_RTSP_OK on success.
2476 gst_rtsp_connection_set_qos_dscp (GstRTSPConnection * conn, guint qos_dscp)
2480 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2481 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2482 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2484 res = set_qos_dscp (conn->fd0.fd, qos_dscp);
2485 if (res == GST_RTSP_OK)
2486 res = set_qos_dscp (conn->fd1.fd, qos_dscp);
2493 * gst_rtsp_connection_get_url:
2494 * @conn: a #GstRTSPConnection
2496 * Retrieve the URL of the other end of @conn.
2498 * Returns: The URL. This value remains valid until the
2499 * connection is freed.
2504 gst_rtsp_connection_get_url (const GstRTSPConnection * conn)
2506 g_return_val_if_fail (conn != NULL, NULL);
2512 * gst_rtsp_connection_get_ip:
2513 * @conn: a #GstRTSPConnection
2515 * Retrieve the IP address of the other end of @conn.
2517 * Returns: The IP address as a string. this value remains valid until the
2518 * connection is closed.
2523 gst_rtsp_connection_get_ip (const GstRTSPConnection * conn)
2525 g_return_val_if_fail (conn != NULL, NULL);
2531 * gst_rtsp_connection_set_ip:
2532 * @conn: a #GstRTSPConnection
2533 * @ip: an ip address
2535 * Set the IP address of the server.
2540 gst_rtsp_connection_set_ip (GstRTSPConnection * conn, const gchar * ip)
2542 g_return_if_fail (conn != NULL);
2545 conn->ip = g_strdup (ip);
2549 * gst_rtsp_connection_get_readfd:
2550 * @conn: a #GstRTSPConnection
2552 * Get the file descriptor for reading.
2554 * Returns: the file descriptor used for reading or -1 on error. The file
2555 * descriptor remains valid until the connection is closed.
2560 gst_rtsp_connection_get_readfd (const GstRTSPConnection * conn)
2562 g_return_val_if_fail (conn != NULL, -1);
2563 g_return_val_if_fail (conn->readfd != NULL, -1);
2565 return conn->readfd->fd;
2569 * gst_rtsp_connection_get_writefd:
2570 * @conn: a #GstRTSPConnection
2572 * Get the file descriptor for writing.
2574 * Returns: the file descriptor used for writing or -1 on error. The file
2575 * descriptor remains valid until the connection is closed.
2580 gst_rtsp_connection_get_writefd (const GstRTSPConnection * conn)
2582 g_return_val_if_fail (conn != NULL, -1);
2583 g_return_val_if_fail (conn->writefd != NULL, -1);
2585 return conn->writefd->fd;
2590 * gst_rtsp_connection_set_tunneled:
2591 * @conn: a #GstRTSPConnection
2592 * @tunneled: the new state
2594 * Set the HTTP tunneling state of the connection. This must be configured before
2595 * the @conn is connected.
2600 gst_rtsp_connection_set_tunneled (GstRTSPConnection * conn, gboolean tunneled)
2602 g_return_if_fail (conn != NULL);
2603 g_return_if_fail (conn->readfd == NULL);
2604 g_return_if_fail (conn->writefd == NULL);
2606 conn->tunneled = tunneled;
2610 * gst_rtsp_connection_is_tunneled:
2611 * @conn: a #GstRTSPConnection
2613 * Get the tunneling state of the connection.
2615 * Returns: if @conn is using HTTP tunneling.
2620 gst_rtsp_connection_is_tunneled (const GstRTSPConnection * conn)
2622 g_return_val_if_fail (conn != NULL, FALSE);
2624 return conn->tunneled;
2628 * gst_rtsp_connection_get_tunnelid:
2629 * @conn: a #GstRTSPConnection
2631 * Get the tunnel session id the connection.
2633 * Returns: returns a non-empty string if @conn is being tunneled over HTTP.
2638 gst_rtsp_connection_get_tunnelid (const GstRTSPConnection * conn)
2640 g_return_val_if_fail (conn != NULL, NULL);
2642 if (!conn->tunneled)
2645 return conn->tunnelid;
2649 * gst_rtsp_connection_do_tunnel:
2650 * @conn: a #GstRTSPConnection
2651 * @conn2: a #GstRTSPConnection
2653 * If @conn received the first tunnel connection and @conn2 received
2654 * the second tunnel connection, link the two connections together so that
2655 * @conn manages the tunneled connection.
2657 * After this call, @conn2 cannot be used anymore and must be freed with
2658 * gst_rtsp_connection_free().
2660 * Returns: return GST_RTSP_OK on success.
2665 gst_rtsp_connection_do_tunnel (GstRTSPConnection * conn,
2666 GstRTSPConnection * conn2)
2668 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2669 g_return_val_if_fail (conn2 != NULL, GST_RTSP_EINVAL);
2670 g_return_val_if_fail (conn->tstate == TUNNEL_STATE_GET, GST_RTSP_EINVAL);
2671 g_return_val_if_fail (conn2->tstate == TUNNEL_STATE_POST, GST_RTSP_EINVAL);
2672 g_return_val_if_fail (!memcmp (conn2->tunnelid, conn->tunnelid, TUNNELID_LEN),
2675 /* both connections have fd0 as the read/write socket. start by taking the
2676 * socket from conn2 and set it as the socket in conn */
2677 conn->fd1 = conn2->fd0;
2679 /* clean up some of the state of conn2 */
2680 gst_poll_remove_fd (conn2->fdset, &conn2->fd0);
2682 conn2->readfd = conn2->writefd = NULL;
2684 /* We make fd0 the write socket and fd1 the read socket. */
2685 conn->writefd = &conn->fd0;
2686 conn->readfd = &conn->fd1;
2688 conn->tstate = TUNNEL_STATE_COMPLETE;
2690 /* we need base64 decoding for the readfd */
2691 conn->ctx.state = 0;
2694 conn->ctx.coutl = 0;
2695 conn->ctxp = &conn->ctx;
2700 #define READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
2701 #define WRITE_COND (G_IO_OUT | G_IO_ERR)
2710 /* async functions */
2711 struct _GstRTSPWatch
2715 GstRTSPConnection *conn;
2717 GstRTSPBuilder builder;
2718 GstRTSPMessage message;
2722 gboolean write_added;
2724 /* queued message for transmission */
2726 GAsyncQueue *messages;
2732 GstRTSPWatchFuncs funcs;
2735 GDestroyNotify notify;
2739 gst_rtsp_source_prepare (GSource * source, gint * timeout)
2741 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2743 if (watch->conn->initial_buffer != NULL)
2746 *timeout = (watch->conn->timeout * 1000);
2752 gst_rtsp_source_check (GSource * source)
2754 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2756 if (watch->readfd.revents & READ_COND)
2759 if (watch->writefd.revents & WRITE_COND)
2766 gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback G_GNUC_UNUSED,
2767 gpointer user_data G_GNUC_UNUSED)
2769 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2772 /* first read as much as we can */
2773 if (watch->readfd.revents & READ_COND || watch->conn->initial_buffer != NULL) {
2775 res = build_next (&watch->builder, &watch->message, watch->conn);
2776 if (res == GST_RTSP_EINTR)
2778 if (G_UNLIKELY (res == GST_RTSP_EEOF))
2780 if (res == GST_RTSP_ETGET) {
2782 GstRTSPStatusCode code;
2785 if (watch->funcs.tunnel_start)
2786 code = watch->funcs.tunnel_start (watch, watch->user_data);
2788 code = GST_RTSP_STS_OK;
2790 /* queue the response string */
2791 str = gen_tunnel_reply (watch->conn, code);
2793 gst_rtsp_watch_queue_data (watch, (guint8 *) g_string_free (str, FALSE),
2795 } else if (res == GST_RTSP_ETPOST) {
2796 /* in the callback the connection should be tunneled with the
2798 if (watch->funcs.tunnel_complete)
2799 watch->funcs.tunnel_complete (watch, watch->user_data);
2800 } else if (G_UNLIKELY (res != GST_RTSP_OK))
2803 if (G_LIKELY (res == GST_RTSP_OK)) {
2804 if (watch->funcs.message_received)
2805 watch->funcs.message_received (watch, &watch->message,
2808 gst_rtsp_message_unset (&watch->message);
2810 build_reset (&watch->builder);
2814 if (watch->writefd.revents & WRITE_COND) {
2816 if (watch->write_data == NULL) {
2819 /* get a new message from the queue */
2820 rec = g_async_queue_try_pop (watch->messages);
2824 watch->write_off = 0;
2825 watch->write_data = rec->data;
2826 watch->write_size = rec->size;
2827 watch->write_id = rec->id;
2829 g_slice_free (GstRTSPRec, rec);
2832 res = write_bytes (watch->writefd.fd, watch->write_data,
2833 &watch->write_off, watch->write_size);
2834 if (res == GST_RTSP_EINTR)
2836 if (G_UNLIKELY (res != GST_RTSP_OK))
2839 if (watch->funcs.message_sent)
2840 watch->funcs.message_sent (watch, watch->write_id, watch->user_data);
2843 if (g_async_queue_length (watch->messages) == 0 && watch->write_added) {
2844 g_source_remove_poll ((GSource *) watch, &watch->writefd);
2845 watch->write_added = FALSE;
2846 watch->writefd.revents = 0;
2848 g_free (watch->write_data);
2849 watch->write_data = NULL;
2858 if (watch->funcs.closed)
2859 watch->funcs.closed (watch, watch->user_data);
2864 if (watch->funcs.error)
2865 watch->funcs.error (watch, res, watch->user_data);
2871 gst_rtsp_rec_free (gpointer data)
2873 GstRTSPRec *rec = data;
2876 g_slice_free (GstRTSPRec, rec);
2880 gst_rtsp_source_finalize (GSource * source)
2882 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2884 build_reset (&watch->builder);
2885 gst_rtsp_message_unset (&watch->message);
2887 g_async_queue_unref (watch->messages);
2888 watch->messages = NULL;
2890 g_free (watch->write_data);
2893 watch->notify (watch->user_data);
2896 static GSourceFuncs gst_rtsp_source_funcs = {
2897 gst_rtsp_source_prepare,
2898 gst_rtsp_source_check,
2899 gst_rtsp_source_dispatch,
2900 gst_rtsp_source_finalize,
2906 * gst_rtsp_watch_new:
2907 * @conn: a #GstRTSPConnection
2908 * @funcs: watch functions
2909 * @user_data: user data to pass to @funcs
2910 * @notify: notify when @user_data is not referenced anymore
2912 * Create a watch object for @conn. The functions provided in @funcs will be
2913 * called with @user_data when activity happened on the watch.
2915 * The new watch is usually created so that it can be attached to a
2916 * maincontext with gst_rtsp_watch_attach().
2918 * @conn must exist for the entire lifetime of the watch.
2920 * Returns: a #GstRTSPWatch that can be used for asynchronous RTSP
2921 * communication. Free with gst_rtsp_watch_unref () after usage.
2926 gst_rtsp_watch_new (GstRTSPConnection * conn,
2927 GstRTSPWatchFuncs * funcs, gpointer user_data, GDestroyNotify notify)
2929 GstRTSPWatch *result;
2931 g_return_val_if_fail (conn != NULL, NULL);
2932 g_return_val_if_fail (funcs != NULL, NULL);
2933 g_return_val_if_fail (conn->readfd != NULL, NULL);
2934 g_return_val_if_fail (conn->writefd != NULL, NULL);
2936 result = (GstRTSPWatch *) g_source_new (&gst_rtsp_source_funcs,
2937 sizeof (GstRTSPWatch));
2939 result->conn = conn;
2940 result->builder.state = STATE_START;
2942 result->messages = g_async_queue_new_full (gst_rtsp_rec_free);
2944 result->readfd.fd = -1;
2945 result->writefd.fd = -1;
2947 gst_rtsp_watch_reset (result);
2949 result->funcs = *funcs;
2950 result->user_data = user_data;
2951 result->notify = notify;
2953 /* only add the read fd, the write fd is only added when we have data
2955 g_source_add_poll ((GSource *) result, &result->readfd);
2961 * gst_rtsp_watch_reset:
2962 * @watch: a #GstRTSPWatch
2964 * Reset @watch, this is usually called after gst_rtsp_connection_do_tunnel()
2965 * when the file descriptors of the connection might have changed.
2970 gst_rtsp_watch_reset (GstRTSPWatch * watch)
2972 if (watch->readfd.fd != -1)
2973 g_source_remove_poll ((GSource *) watch, &watch->readfd);
2974 if (watch->writefd.fd != -1)
2975 g_source_remove_poll ((GSource *) watch, &watch->writefd);
2977 watch->readfd.fd = watch->conn->readfd->fd;
2978 watch->readfd.events = READ_COND;
2979 watch->readfd.revents = 0;
2981 watch->writefd.fd = watch->conn->writefd->fd;
2982 watch->writefd.events = WRITE_COND;
2983 watch->writefd.revents = 0;
2984 watch->write_added = FALSE;
2986 g_source_add_poll ((GSource *) watch, &watch->readfd);
2990 * gst_rtsp_watch_attach:
2991 * @watch: a #GstRTSPWatch
2992 * @context: a GMainContext (if NULL, the default context will be used)
2994 * Adds a #GstRTSPWatch to a context so that it will be executed within that context.
2996 * Returns: the ID (greater than 0) for the watch within the GMainContext.
3001 gst_rtsp_watch_attach (GstRTSPWatch * watch, GMainContext * context)
3003 g_return_val_if_fail (watch != NULL, 0);
3005 return g_source_attach ((GSource *) watch, context);
3009 * gst_rtsp_watch_unref:
3010 * @watch: a #GstRTSPWatch
3012 * Decreases the reference count of @watch by one. If the resulting reference
3013 * count is zero the watch and associated memory will be destroyed.
3018 gst_rtsp_watch_unref (GstRTSPWatch * watch)
3020 g_return_if_fail (watch != NULL);
3022 g_source_unref ((GSource *) watch);
3026 * gst_rtsp_watch_queue_data:
3027 * @watch: a #GstRTSPWatch
3028 * @data: the data to queue
3029 * @size: the size of @data
3031 * Queue @data for transmission in @watch. It will be transmitted when the
3032 * connection of the @watch becomes writable.
3034 * This function will take ownership of @data and g_free() it after use.
3036 * The return value of this function will be used as the id argument in the
3037 * message_sent callback.
3044 gst_rtsp_watch_queue_data (GstRTSPWatch * watch, const guint8 * data,
3049 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3050 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
3051 g_return_val_if_fail (size != 0, GST_RTSP_EINVAL);
3053 /* make a record with the data and id */
3054 rec = g_slice_new (GstRTSPRec);
3055 rec->data = (guint8 *) data;
3058 /* make sure rec->id is never 0 */
3059 rec->id = ++watch->id;
3060 } while (G_UNLIKELY (rec->id == 0));
3062 /* add the record to a queue. FIXME we would like to have an upper limit here */
3063 g_async_queue_push (watch->messages, rec);
3065 /* FIXME: does the following need to be made thread-safe? (this might be
3066 * called from a streaming thread, like appsink's render function) */
3067 /* make sure the main context will now also check for writability on the
3069 if (!watch->write_added) {
3070 g_source_add_poll ((GSource *) watch, &watch->writefd);
3071 watch->write_added = TRUE;
3078 * gst_rtsp_watch_queue_message:
3079 * @watch: a #GstRTSPWatch
3080 * @message: a #GstRTSPMessage
3082 * Queue a @message for transmission in @watch. The contents of this
3083 * message will be serialized and transmitted when the connection of the
3084 * @watch becomes writable.
3086 * The return value of this function will be used as the id argument in the
3087 * message_sent callback.
3094 gst_rtsp_watch_queue_message (GstRTSPWatch * watch, GstRTSPMessage * message)
3099 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3100 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3102 /* make a record with the message as a string and id */
3103 str = message_to_string (watch->conn, message);
3105 return gst_rtsp_watch_queue_data (watch,
3106 (guint8 *) g_string_free (str, FALSE), size);