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)
73 /* we include this here to get the G_OS_* defines */
78 /* ws2_32.dll has getaddrinfo and freeaddrinfo on Windows XP and later.
79 * minwg32 headers check WINVER before allowing the use of these */
85 #define EINPROGRESS WSAEINPROGRESS
87 #include <sys/ioctl.h>
89 #include <sys/socket.h>
93 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
94 #include <sys/filio.h>
97 #include "gstrtspconnection.h"
98 #include "gstrtspbase64.h"
104 struct sockaddr_in sa_in;
105 struct sockaddr_in6 sa_in6;
106 struct sockaddr_storage sa_stor;
113 guchar out[3]; /* the size must be evenly divisible by 3 */
118 static GstRTSPResult read_line (gint fd, guint8 * buffer, guint * idx,
119 guint size, DecodeCtx * ctxp);
120 static GstRTSPResult parse_key_value (guint8 * buffer, gchar * key,
121 guint keysize, gchar ** value);
122 static void parse_string (gchar * dest, gint size, gchar ** src);
125 #define READ_SOCKET(fd, buf, len) recv (fd, (char *)buf, len, 0)
126 #define WRITE_SOCKET(fd, buf, len) send (fd, (const char *)buf, len, 0)
127 #define SETSOCKOPT(sock, level, name, val, len) setsockopt (sock, level, name, (const char *)val, len)
128 #define CLOSE_SOCKET(sock) closesocket (sock)
129 #define ERRNO_IS_EAGAIN (WSAGetLastError () == WSAEWOULDBLOCK)
130 #define ERRNO_IS_EINTR (WSAGetLastError () == WSAEINTR)
131 /* According to Microsoft's connect() documentation this one returns
132 * WSAEWOULDBLOCK and not WSAEINPROGRESS. */
133 #define ERRNO_IS_EINPROGRESS (WSAGetLastError () == WSAEWOULDBLOCK)
135 #define READ_SOCKET(fd, buf, len) read (fd, buf, len)
136 #define WRITE_SOCKET(fd, buf, len) write (fd, buf, len)
137 #define SETSOCKOPT(sock, level, name, val, len) setsockopt (sock, level, name, val, len)
138 #define CLOSE_SOCKET(sock) close (sock)
139 #define ERRNO_IS_EAGAIN (errno == EAGAIN)
140 #define ERRNO_IS_EINTR (errno == EINTR)
141 #define ERRNO_IS_EINPROGRESS (errno == EINPROGRESS)
144 #define ADD_POLLFD(fdset, pfd, fd) \
147 gst_poll_add_fd (fdset, pfd); \
150 #define REMOVE_POLLFD(fdset, pfd) \
152 if ((pfd)->fd != -1) { \
153 GST_DEBUG ("remove fd %d", (pfd)->fd); \
154 gst_poll_remove_fd (fdset, pfd); \
155 CLOSE_SOCKET ((pfd)->fd); \
165 TUNNEL_STATE_COMPLETE
166 } GstRTSPTunnelState;
168 #define TUNNELID_LEN 24
170 struct _GstRTSPConnection
173 /* URL for the connection */
176 /* connection state */
183 gchar tunnelid[TUNNELID_LEN];
185 GstRTSPTunnelState tstate;
191 gint cseq; /* sequence number */
192 gchar session_id[512]; /* session id */
193 gint timeout; /* session timeout in seconds */
194 GTimer *timer; /* timeout timer */
197 GstRTSPAuthMethod auth_method;
200 GHashTable *auth_params;
219 /* a structure for constructing RTSPMessages */
232 build_reset (GstRTSPBuilder * builder)
234 g_free (builder->body_data);
235 memset (builder, 0, sizeof (GstRTSPBuilder));
239 * gst_rtsp_connection_create:
240 * @url: a #GstRTSPUrl
241 * @conn: storage for a #GstRTSPConnection
243 * Create a newly allocated #GstRTSPConnection from @url and store it in @conn.
244 * The connection will not yet attempt to connect to @url, use
245 * gst_rtsp_connection_connect().
247 * A copy of @url will be made.
249 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
252 gst_rtsp_connection_create (const GstRTSPUrl * url, GstRTSPConnection ** conn)
254 GstRTSPConnection *newconn;
260 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
263 error = WSAStartup (0x0202, &w);
268 if (w.wVersion != 0x0202)
272 newconn = g_new0 (GstRTSPConnection, 1);
274 if ((newconn->fdset = gst_poll_new (TRUE)) == NULL)
277 newconn->url = gst_rtsp_url_copy (url);
278 newconn->fd0.fd = -1;
279 newconn->fd1.fd = -1;
280 newconn->timer = g_timer_new ();
281 newconn->timeout = 60;
284 newconn->auth_method = GST_RTSP_AUTH_NONE;
285 newconn->username = NULL;
286 newconn->passwd = NULL;
287 newconn->auth_params = NULL;
297 g_warning ("Error %d on WSAStartup", error);
298 return GST_RTSP_EWSASTART;
302 g_warning ("Windows sockets are not version 0x202 (current 0x%x)",
305 return GST_RTSP_EWSAVERSION;
314 return GST_RTSP_ESYS;
319 * gst_rtsp_connection_accept:
321 * @conn: storage for a #GstRTSPConnection
323 * Accept a new connection on @sock and create a new #GstRTSPConnection for
324 * handling communication on new socket.
326 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
331 gst_rtsp_connection_accept (gint sock, GstRTSPConnection ** conn)
334 GstRTSPConnection *newconn = NULL;
335 union gst_sockaddr sa;
336 socklen_t slen = sizeof (sa);
337 gchar ip[INET6_ADDRSTRLEN];
343 memset (&sa, 0, slen);
346 fd = accept (sock, &sa.sa, &slen);
348 fd = accept (sock, &sa.sa, (gint *) & slen);
349 #endif /* G_OS_WIN32 */
353 if (getnameinfo (&sa.sa, slen, ip, sizeof (ip), NULL, 0, NI_NUMERICHOST) != 0)
354 goto getnameinfo_failed;
355 if (sa.sa.sa_family != AF_INET && sa.sa.sa_family != AF_INET6)
358 /* set to non-blocking mode so that we can cancel the communication */
360 fcntl (fd, F_SETFL, O_NONBLOCK);
362 ioctlsocket (fd, FIONBIO, &flags);
363 #endif /* G_OS_WIN32 */
365 /* create a url for the client address */
366 url = g_new0 (GstRTSPUrl, 1);
367 url->host = g_strdup (ip);
368 if (sa.sa.sa_family == AF_INET)
369 url->port = sa.sa_in.sin_port;
371 url->port = sa.sa_in6.sin6_port;
373 /* now create the connection object */
374 gst_rtsp_connection_create (url, &newconn);
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;
390 return GST_RTSP_ESYS;
396 return GST_RTSP_ERROR;
401 do_resolve (const gchar * host)
403 static gchar ip[INET6_ADDRSTRLEN];
404 struct addrinfo *aires;
408 aierr = getaddrinfo (host, NULL, NULL, &aires);
412 for (ai = aires; ai; ai = ai->ai_next) {
413 if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) {
420 aierr = getnameinfo (ai->ai_addr, ai->ai_addrlen, ip, sizeof (ip), NULL, 0,
421 NI_NUMERICHOST | NI_NUMERICSERV);
425 freeaddrinfo (aires);
427 return g_strdup (ip);
432 GST_ERROR ("no addrinfo found for %s: %s", host, gai_strerror (aierr));
437 GST_ERROR ("no family found for %s", host);
438 freeaddrinfo (aires);
443 GST_ERROR ("no address found for %s: %s", host, gai_strerror (aierr));
444 freeaddrinfo (aires);
450 do_connect (const gchar * ip, guint16 port, GstPollFD * fdout,
451 GstPoll * fdset, GTimeVal * timeout)
454 struct addrinfo hints;
455 struct addrinfo *aires;
458 gchar service[NI_MAXSERV];
461 unsigned long flags = 1;
462 #endif /* G_OS_WIN32 */
466 memset (&hints, 0, sizeof hints);
467 hints.ai_flags = AI_NUMERICHOST;
468 hints.ai_family = AF_UNSPEC;
469 hints.ai_socktype = SOCK_STREAM;
470 g_snprintf (service, sizeof (service) - 1, "%hu", port);
471 service[sizeof (service) - 1] = '\0';
473 aierr = getaddrinfo (ip, service, &hints, &aires);
477 for (ai = aires; ai; ai = ai->ai_next) {
478 if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) {
485 fd = socket (ai->ai_family, SOCK_STREAM, 0);
489 /* set to non-blocking mode so that we can cancel the connect */
491 fcntl (fd, F_SETFL, O_NONBLOCK);
493 ioctlsocket (fd, FIONBIO, &flags);
494 #endif /* G_OS_WIN32 */
496 /* add the socket to our fdset */
497 ADD_POLLFD (fdset, fdout, fd);
499 /* we are going to connect ASYNC now */
500 ret = connect (fd, ai->ai_addr, ai->ai_addrlen);
503 if (!ERRNO_IS_EINPROGRESS)
506 /* wait for connect to complete up to the specified timeout or until we got
508 gst_poll_fd_ctl_write (fdset, fdout, TRUE);
510 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
513 retval = gst_poll_wait (fdset, to);
514 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
518 else if (retval == -1)
521 /* we can still have an error connecting on windows */
522 if (gst_poll_fd_has_error (fdset, fdout)) {
523 socklen_t len = sizeof (errno);
525 getsockopt (fd, SOL_SOCKET, SO_ERROR, &errno, &len);
527 getsockopt (fd, SOL_SOCKET, SO_ERROR, (char *) &errno, &len);
532 gst_poll_fd_ignored (fdset, fdout);
535 freeaddrinfo (aires);
542 GST_ERROR ("no addrinfo found for %s: %s", ip, gai_strerror (aierr));
543 return GST_RTSP_ERROR;
547 GST_ERROR ("no family found for %s", ip);
548 freeaddrinfo (aires);
549 return GST_RTSP_ERROR;
553 GST_ERROR ("no socket %d (%s)", errno, g_strerror (errno));
554 freeaddrinfo (aires);
555 return GST_RTSP_ESYS;
559 GST_ERROR ("system error %d (%s)", errno, g_strerror (errno));
560 REMOVE_POLLFD (fdset, fdout);
561 freeaddrinfo (aires);
562 return GST_RTSP_ESYS;
566 GST_ERROR ("timeout");
567 REMOVE_POLLFD (fdset, fdout);
568 freeaddrinfo (aires);
569 return GST_RTSP_ETIMEOUT;
574 setup_tunneling (GstRTSPConnection * conn, GTimeVal * timeout)
582 gchar *ip, *url_port_str;
583 guint16 port, url_port;
584 gchar codestr[4], *resultstr;
589 /* create a random sessionid */
590 for (i = 0; i < TUNNELID_LEN; i++)
591 conn->tunnelid[i] = g_random_int_range ('a', 'z');
592 conn->tunnelid[TUNNELID_LEN - 1] = '\0';
595 /* get the port from the url */
596 gst_rtsp_url_get_port (url, &url_port);
598 if (conn->proxy_host) {
599 hostparam = g_strdup_printf ("Host: %s:%d\r\n", url->host, url_port);
600 url_port_str = g_strdup_printf (":%d", url_port);
601 ip = conn->proxy_host;
602 port = conn->proxy_port;
611 str = g_strdup_printf ("GET %s%s%s%s%s%s HTTP/1.0\r\n"
613 "x-sessioncookie: %s\r\n"
614 "Accept: application/x-rtsp-tunnelled\r\n"
615 "Pragma: no-cache\r\n"
616 "Cache-Control: no-cache\r\n" "\r\n",
617 conn->proxy_host ? "http://" : "",
618 conn->proxy_host ? url->host : "",
619 conn->proxy_host ? url_port_str : "",
620 url->abspath, url->query ? "?" : "", url->query ? url->query : "",
621 hostparam ? hostparam : "", conn->tunnelid);
623 /* we start by writing to this fd */
624 conn->writefd = &conn->fd0;
626 res = gst_rtsp_connection_write (conn, (guint8 *) str, strlen (str), timeout);
628 if (res != GST_RTSP_OK)
631 gst_poll_fd_ctl_write (conn->fdset, &conn->fd0, FALSE);
632 gst_poll_fd_ctl_read (conn->fdset, &conn->fd0, TRUE);
634 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
642 res = read_line (conn->fd0.fd, buffer, &idx, sizeof (buffer), NULL);
643 if (res == GST_RTSP_EEOF)
645 if (res == GST_RTSP_OK)
647 if (res != GST_RTSP_EINTR)
651 retval = gst_poll_wait (conn->fdset, to);
652 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
654 /* check for timeout */
666 /* check for last line */
667 if (buffer[0] == '\r')
669 if (buffer[0] == '\0')
673 /* first line, parse response */
674 gchar versionstr[20];
677 bptr = (gchar *) buffer;
679 parse_string (versionstr, sizeof (versionstr), &bptr);
680 parse_string (codestr, sizeof (codestr), &bptr);
681 code = atoi (codestr);
683 while (g_ascii_isspace (*bptr))
688 if (code != GST_RTSP_STS_OK)
694 /* other lines, parse key/value */
695 res = parse_key_value (buffer, key, sizeof (key), &value);
696 if (res == GST_RTSP_OK) {
697 /* we got a new ip address */
698 if (g_ascii_strcasecmp (key, "x-server-ip-address") == 0) {
699 if (conn->proxy_host) {
700 /* if we use a proxy we need to change the destination url */
702 url->host = g_strdup (value);
704 g_free (url_port_str);
706 g_strdup_printf ("Host: %s:%d\r\n", url->host, url_port);
707 url_port_str = g_strdup_printf (":%d", url_port);
709 /* and resolve the new ip address */
710 if (!(ip = do_resolve (conn->ip)))
721 /* connect to the host/port */
722 res = do_connect (ip, port, &conn->fd1, conn->fdset, timeout);
723 if (res != GST_RTSP_OK)
726 /* this is now our writing socket */
727 conn->writefd = &conn->fd1;
730 str = g_strdup_printf ("POST %s%s%s%s%s%s HTTP/1.0\r\n"
732 "x-sessioncookie: %s\r\n"
733 "Content-Type: application/x-rtsp-tunnelled\r\n"
734 "Pragma: no-cache\r\n"
735 "Cache-Control: no-cache\r\n"
736 "Content-Length: 32767\r\n"
737 "Expires: Sun, 9 Jan 1972 00:00:00 GMT\r\n"
739 conn->proxy_host ? "http://" : "",
740 conn->proxy_host ? url->host : "",
741 conn->proxy_host ? url_port_str : "",
742 url->abspath, url->query ? "?" : "", url->query ? url->query : "",
743 hostparam ? hostparam : "", conn->tunnelid);
745 res = gst_rtsp_connection_write (conn, (guint8 *) str, strlen (str), timeout);
747 if (res != GST_RTSP_OK)
752 g_free (url_port_str);
759 GST_ERROR ("write failed (%d)", res);
773 res = GST_RTSP_ETIMEOUT;
783 res = GST_RTSP_EINTR;
788 GST_ERROR ("got failure response %d %s", code, resultstr);
789 res = GST_RTSP_ERROR;
794 GST_ERROR ("could not resolve %s", conn->ip);
800 GST_ERROR ("failed to connect");
806 * gst_rtsp_connection_connect:
807 * @conn: a #GstRTSPConnection
808 * @timeout: a #GTimeVal timeout
810 * Attempt to connect to the url of @conn made with
811 * gst_rtsp_connection_create(). If @timeout is #NULL this function can block
812 * forever. If @timeout contains a valid timeout, this function will return
813 * #GST_RTSP_ETIMEOUT after the timeout expired.
815 * This function can be cancelled with gst_rtsp_connection_flush().
817 * Returns: #GST_RTSP_OK when a connection could be made.
820 gst_rtsp_connection_connect (GstRTSPConnection * conn, GTimeVal * timeout)
827 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
828 g_return_val_if_fail (conn->url != NULL, GST_RTSP_EINVAL);
829 g_return_val_if_fail (conn->fd0.fd < 0, GST_RTSP_EINVAL);
833 if (conn->proxy_host && conn->tunneled) {
834 if (!(ip = do_resolve (conn->proxy_host))) {
835 GST_ERROR ("could not resolve %s", conn->proxy_host);
838 port = conn->proxy_port;
839 g_free (conn->proxy_host);
840 conn->proxy_host = ip;
842 if (!(ip = do_resolve (url->host))) {
843 GST_ERROR ("could not resolve %s", url->host);
846 /* get the port from the url */
847 gst_rtsp_url_get_port (url, &port);
853 /* connect to the host/port */
854 res = do_connect (ip, port, &conn->fd0, conn->fdset, timeout);
855 if (res != GST_RTSP_OK)
858 /* this is our read URL */
859 conn->readfd = &conn->fd0;
861 if (conn->tunneled) {
862 res = setup_tunneling (conn, timeout);
863 if (res != GST_RTSP_OK)
864 goto tunneling_failed;
866 conn->writefd = &conn->fd0;
873 return GST_RTSP_ENET;
877 GST_ERROR ("failed to connect");
882 GST_ERROR ("failed to setup tunneling");
888 md5_digest_to_hex_string (unsigned char digest[16], char string[33])
890 static const char hexdigits[] = "0123456789abcdef";
893 for (i = 0; i < 16; i++) {
894 string[i * 2] = hexdigits[(digest[i] >> 4) & 0x0f];
895 string[i * 2 + 1] = hexdigits[digest[i] & 0x0f];
901 auth_digest_compute_hex_urp (const gchar * username,
902 const gchar * realm, const gchar * password, gchar hex_urp[33])
904 struct MD5Context md5_context;
905 unsigned char digest[16];
907 MD5Init (&md5_context);
908 MD5Update (&md5_context, username, strlen (username));
909 MD5Update (&md5_context, ":", 1);
910 MD5Update (&md5_context, realm, strlen (realm));
911 MD5Update (&md5_context, ":", 1);
912 MD5Update (&md5_context, password, strlen (password));
913 MD5Final (digest, &md5_context);
914 md5_digest_to_hex_string (digest, hex_urp);
918 auth_digest_compute_response (const gchar * method,
919 const gchar * uri, const gchar * hex_a1, const gchar * nonce,
923 struct MD5Context md5_context;
924 unsigned char digest[16];
927 MD5Init (&md5_context);
928 MD5Update (&md5_context, method, strlen (method));
929 MD5Update (&md5_context, ":", 1);
930 MD5Update (&md5_context, uri, strlen (uri));
931 MD5Final (digest, &md5_context);
932 md5_digest_to_hex_string (digest, hex_a2);
935 MD5Init (&md5_context);
936 MD5Update (&md5_context, hex_a1, strlen (hex_a1));
937 MD5Update (&md5_context, ":", 1);
938 MD5Update (&md5_context, nonce, strlen (nonce));
939 MD5Update (&md5_context, ":", 1);
941 MD5Update (&md5_context, hex_a2, 32);
942 MD5Final (digest, &md5_context);
943 md5_digest_to_hex_string (digest, response);
947 add_auth_header (GstRTSPConnection * conn, GstRTSPMessage * message)
949 switch (conn->auth_method) {
950 case GST_RTSP_AUTH_BASIC:{
955 user_pass = g_strdup_printf ("%s:%s", conn->username, conn->passwd);
956 user_pass64 = g_base64_encode ((guchar *) user_pass, strlen (user_pass));
957 auth_string = g_strdup_printf ("Basic %s", user_pass64);
959 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
963 g_free (user_pass64);
966 case GST_RTSP_AUTH_DIGEST:{
967 gchar response[33], hex_urp[33];
968 gchar *auth_string, *auth_string2;
975 /* we need to have some params set */
976 if (conn->auth_params == NULL)
979 /* we need the realm and nonce */
980 realm = (gchar *) g_hash_table_lookup (conn->auth_params, "realm");
981 nonce = (gchar *) g_hash_table_lookup (conn->auth_params, "nonce");
982 if (realm == NULL || nonce == NULL)
985 auth_digest_compute_hex_urp (conn->username, realm, conn->passwd,
988 method = gst_rtsp_method_as_text (message->type_data.request.method);
989 uri = message->type_data.request.uri;
991 /* Assume no qop, algorithm=md5, stale=false */
992 /* For algorithm MD5, a1 = urp. */
993 auth_digest_compute_response (method, uri, hex_urp, nonce, response);
994 auth_string = g_strdup_printf ("Digest username=\"%s\", "
995 "realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"",
996 conn->username, realm, nonce, uri, response);
998 opaque = (gchar *) g_hash_table_lookup (conn->auth_params, "opaque");
1000 auth_string2 = g_strdup_printf ("%s, opaque=\"%s\"", auth_string,
1002 g_free (auth_string);
1003 auth_string = auth_string2;
1005 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
1016 gen_date_string (gchar * date_string, guint len)
1020 #ifdef HAVE_GMTIME_R
1024 g_get_current_time (&tv);
1025 t = (time_t) tv.tv_sec;
1027 #ifdef HAVE_GMTIME_R
1028 strftime (date_string, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime_r (&t, &tm_));
1030 strftime (date_string, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime (&t));
1034 static GstRTSPResult
1035 write_bytes (gint fd, const guint8 * buffer, guint * idx, guint size)
1039 if (G_UNLIKELY (*idx > size))
1040 return GST_RTSP_ERROR;
1047 r = WRITE_SOCKET (fd, &buffer[*idx], left);
1048 if (G_UNLIKELY (r == 0)) {
1049 return GST_RTSP_EINTR;
1050 } else if (G_UNLIKELY (r < 0)) {
1051 if (ERRNO_IS_EAGAIN)
1052 return GST_RTSP_EINTR;
1053 if (!ERRNO_IS_EINTR)
1054 return GST_RTSP_ESYS;
1064 fill_bytes (gint fd, guint8 * buffer, guint size, DecodeCtx * ctx)
1070 guint8 in[sizeof (ctx->out) * 4 / 3];
1073 while (size > 0 && ctx->cout < ctx->coutl) {
1074 /* we have some leftover bytes */
1075 *buffer++ = ctx->out[ctx->cout++];
1080 /* got what we needed? */
1084 /* try to read more bytes */
1085 r = READ_SOCKET (fd, in, sizeof (in));
1094 g_base64_decode_step ((gchar *) in, r, ctx->out, &ctx->state,
1098 out = READ_SOCKET (fd, buffer, size);
1104 static GstRTSPResult
1105 read_bytes (gint fd, guint8 * buffer, guint * idx, guint size, DecodeCtx * ctx)
1109 if (G_UNLIKELY (*idx > size))
1110 return GST_RTSP_ERROR;
1117 r = fill_bytes (fd, &buffer[*idx], left, ctx);
1118 if (G_UNLIKELY (r == 0)) {
1119 return GST_RTSP_EEOF;
1120 } else if (G_UNLIKELY (r < 0)) {
1121 if (ERRNO_IS_EAGAIN)
1122 return GST_RTSP_EINTR;
1123 if (!ERRNO_IS_EINTR)
1124 return GST_RTSP_ESYS;
1133 static GstRTSPResult
1134 read_line (gint fd, guint8 * buffer, guint * idx, guint size, DecodeCtx * ctx)
1140 r = fill_bytes (fd, &c, 1, ctx);
1141 if (G_UNLIKELY (r == 0)) {
1142 return GST_RTSP_EEOF;
1143 } else if (G_UNLIKELY (r < 0)) {
1144 if (ERRNO_IS_EAGAIN)
1145 return GST_RTSP_EINTR;
1146 if (!ERRNO_IS_EINTR)
1147 return GST_RTSP_ESYS;
1149 if (c == '\n') /* end on \n */
1151 if (c == '\r') /* ignore \r */
1154 if (G_LIKELY (*idx < size - 1))
1155 buffer[(*idx)++] = c;
1158 buffer[*idx] = '\0';
1164 * gst_rtsp_connection_write:
1165 * @conn: a #GstRTSPConnection
1166 * @data: the data to write
1167 * @size: the size of @data
1168 * @timeout: a timeout value or #NULL
1170 * Attempt to write @size bytes of @data to the connected @conn, blocking up to
1171 * the specified @timeout. @timeout can be #NULL, in which case this function
1172 * might block forever.
1174 * This function can be cancelled with gst_rtsp_connection_flush().
1176 * Returns: #GST_RTSP_OK on success.
1179 gst_rtsp_connection_write (GstRTSPConnection * conn, const guint8 * data,
1180 guint size, GTimeVal * timeout)
1187 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1188 g_return_val_if_fail (data != NULL || size == 0, GST_RTSP_EINVAL);
1189 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
1191 gst_poll_set_controllable (conn->fdset, TRUE);
1192 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, TRUE);
1193 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, FALSE);
1194 /* clear all previous poll results */
1195 gst_poll_fd_ignored (conn->fdset, conn->writefd);
1196 gst_poll_fd_ignored (conn->fdset, conn->readfd);
1198 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1204 res = write_bytes (conn->writefd->fd, data, &offset, size);
1205 if (G_LIKELY (res == GST_RTSP_OK))
1207 if (G_UNLIKELY (res != GST_RTSP_EINTR))
1210 /* not all is written, wait until we can write more */
1212 retval = gst_poll_wait (conn->fdset, to);
1213 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1215 if (G_UNLIKELY (retval == 0))
1218 if (G_UNLIKELY (retval == -1)) {
1230 return GST_RTSP_ETIMEOUT;
1234 return GST_RTSP_ESYS;
1238 return GST_RTSP_EINTR;
1247 message_to_string (GstRTSPConnection * conn, GstRTSPMessage * message)
1249 GString *str = NULL;
1251 str = g_string_new ("");
1253 switch (message->type) {
1254 case GST_RTSP_MESSAGE_REQUEST:
1255 /* create request string, add CSeq */
1256 g_string_append_printf (str, "%s %s RTSP/1.0\r\n"
1258 gst_rtsp_method_as_text (message->type_data.request.method),
1259 message->type_data.request.uri, conn->cseq++);
1260 /* add session id if we have one */
1261 if (conn->session_id[0] != '\0') {
1262 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SESSION,
1265 /* add any authentication headers */
1266 add_auth_header (conn, message);
1268 case GST_RTSP_MESSAGE_RESPONSE:
1269 /* create response string */
1270 g_string_append_printf (str, "RTSP/1.0 %d %s\r\n",
1271 message->type_data.response.code, message->type_data.response.reason);
1273 case GST_RTSP_MESSAGE_DATA:
1275 guint8 data_header[4];
1277 /* prepare data header */
1278 data_header[0] = '$';
1279 data_header[1] = message->type_data.data.channel;
1280 data_header[2] = (message->body_size >> 8) & 0xff;
1281 data_header[3] = message->body_size & 0xff;
1283 /* create string with header and data */
1284 str = g_string_append_len (str, (gchar *) data_header, 4);
1286 g_string_append_len (str, (gchar *) message->body,
1287 message->body_size);
1291 g_string_free (str, TRUE);
1292 g_return_val_if_reached (NULL);
1296 /* append headers and body */
1297 if (message->type != GST_RTSP_MESSAGE_DATA) {
1298 gchar date_string[100];
1300 gen_date_string (date_string, sizeof (date_string));
1302 /* add date header */
1303 gst_rtsp_message_add_header (message, GST_RTSP_HDR_DATE, date_string);
1305 /* append headers */
1306 gst_rtsp_message_append_headers (message, str);
1308 /* append Content-Length and body if needed */
1309 if (message->body != NULL && message->body_size > 0) {
1312 len = g_strdup_printf ("%d", message->body_size);
1313 g_string_append_printf (str, "%s: %s\r\n",
1314 gst_rtsp_header_as_text (GST_RTSP_HDR_CONTENT_LENGTH), len);
1316 /* header ends here */
1317 g_string_append (str, "\r\n");
1319 g_string_append_len (str, (gchar *) message->body,
1320 message->body_size);
1322 /* just end headers */
1323 g_string_append (str, "\r\n");
1331 * gst_rtsp_connection_send:
1332 * @conn: a #GstRTSPConnection
1333 * @message: the message to send
1334 * @timeout: a timeout value or #NULL
1336 * Attempt to send @message to the connected @conn, blocking up to
1337 * the specified @timeout. @timeout can be #NULL, in which case this function
1338 * might block forever.
1340 * This function can be cancelled with gst_rtsp_connection_flush().
1342 * Returns: #GST_RTSP_OK on success.
1345 gst_rtsp_connection_send (GstRTSPConnection * conn, GstRTSPMessage * message,
1348 GString *string = NULL;
1353 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1354 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1356 if (G_UNLIKELY (!(string = message_to_string (conn, message))))
1359 if (conn->tunneled) {
1360 str = g_base64_encode ((const guchar *) string->str, string->len);
1361 g_string_free (string, TRUE);
1366 g_string_free (string, FALSE);
1370 res = gst_rtsp_connection_write (conn, (guint8 *) str, len, timeout);
1378 g_warning ("Wrong message");
1379 return GST_RTSP_EINVAL;
1384 parse_string (gchar * dest, gint size, gchar ** src)
1390 while (g_ascii_isspace (**src))
1393 while (!g_ascii_isspace (**src) && **src != '\0') {
1395 dest[idx++] = **src;
1403 parse_key (gchar * dest, gint size, gchar ** src)
1408 while (**src != ':' && **src != '\0') {
1410 dest[idx++] = **src;
1417 static GstRTSPResult
1418 parse_response_status (guint8 * buffer, GstRTSPMessage * msg)
1421 gchar versionstr[20];
1426 bptr = (gchar *) buffer;
1428 parse_string (versionstr, sizeof (versionstr), &bptr);
1429 parse_string (codestr, sizeof (codestr), &bptr);
1430 code = atoi (codestr);
1432 while (g_ascii_isspace (*bptr))
1435 if (strcmp (versionstr, "RTSP/1.0") == 0)
1436 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
1438 else if (strncmp (versionstr, "RTSP/", 5) == 0) {
1439 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
1441 msg->type_data.response.version = GST_RTSP_VERSION_INVALID;
1449 return GST_RTSP_EPARSE;
1453 static GstRTSPResult
1454 parse_request_line (GstRTSPConnection * conn, guint8 * buffer,
1455 GstRTSPMessage * msg)
1457 GstRTSPResult res = GST_RTSP_OK;
1458 gchar versionstr[20];
1459 gchar methodstr[20];
1462 GstRTSPMethod method;
1463 GstRTSPTunnelState tstate = TUNNEL_STATE_NONE;
1465 bptr = (gchar *) buffer;
1467 parse_string (methodstr, sizeof (methodstr), &bptr);
1468 method = gst_rtsp_find_method (methodstr);
1469 if (method == GST_RTSP_INVALID) {
1470 /* a tunnel request is allowed when we don't have one yet */
1471 if (conn->tstate != TUNNEL_STATE_NONE)
1472 goto invalid_method;
1473 /* we need GET or POST for a valid tunnel request */
1474 if (!strcmp (methodstr, "GET"))
1475 tstate = TUNNEL_STATE_GET;
1476 else if (!strcmp (methodstr, "POST"))
1477 tstate = TUNNEL_STATE_POST;
1479 goto invalid_method;
1482 parse_string (urlstr, sizeof (urlstr), &bptr);
1483 if (G_UNLIKELY (*urlstr == '\0'))
1486 parse_string (versionstr, sizeof (versionstr), &bptr);
1488 if (G_UNLIKELY (*bptr != '\0'))
1489 goto invalid_version;
1491 if (strcmp (versionstr, "RTSP/1.0") == 0) {
1492 res = gst_rtsp_message_init_request (msg, method, urlstr);
1493 } else if (strncmp (versionstr, "RTSP/", 5) == 0) {
1494 res = gst_rtsp_message_init_request (msg, method, urlstr);
1495 msg->type_data.request.version = GST_RTSP_VERSION_INVALID;
1496 } else if (strcmp (versionstr, "HTTP/1.0") == 0) {
1497 /* tunnel request, we need a tunnel method */
1498 if (tstate == TUNNEL_STATE_NONE) {
1499 res = GST_RTSP_EPARSE;
1501 conn->tstate = tstate;
1504 res = GST_RTSP_EPARSE;
1512 GST_ERROR ("invalid method %s", methodstr);
1513 return GST_RTSP_EPARSE;
1517 GST_ERROR ("invalid url %s", urlstr);
1518 return GST_RTSP_EPARSE;
1522 GST_ERROR ("invalid version");
1523 return GST_RTSP_EPARSE;
1527 static GstRTSPResult
1528 parse_key_value (guint8 * buffer, gchar * key, guint keysize, gchar ** value)
1532 bptr = (gchar *) buffer;
1535 parse_key (key, keysize, &bptr);
1536 if (G_UNLIKELY (*bptr != ':'))
1540 while (g_ascii_isspace (*bptr))
1550 return GST_RTSP_EPARSE;
1554 /* parsing lines means reading a Key: Value pair */
1555 static GstRTSPResult
1556 parse_line (GstRTSPConnection * conn, guint8 * buffer, GstRTSPMessage * msg)
1561 GstRTSPHeaderField field;
1563 res = parse_key_value (buffer, key, sizeof (key), &value);
1564 if (G_UNLIKELY (res != GST_RTSP_OK))
1567 if (conn->tstate == TUNNEL_STATE_GET || conn->tstate == TUNNEL_STATE_POST) {
1568 /* save the tunnel session in the connection */
1569 if (!strcmp (key, "x-sessioncookie")) {
1570 strncpy (conn->tunnelid, value, TUNNELID_LEN);
1571 conn->tunnelid[TUNNELID_LEN - 1] = '\0';
1572 conn->tunneled = TRUE;
1575 field = gst_rtsp_find_header_field (key);
1576 if (field != GST_RTSP_HDR_INVALID)
1577 gst_rtsp_message_add_header (msg, field, value);
1590 * GST_RTSP_OK when a complete message was read.
1591 * GST_RTSP_EEOF: when the socket is closed
1592 * GST_RTSP_EINTR: when more data is needed.
1593 * GST_RTSP_..: some other error occured.
1595 static GstRTSPResult
1596 build_next (GstRTSPBuilder * builder, GstRTSPMessage * message,
1597 GstRTSPConnection * conn)
1602 switch (builder->state) {
1604 builder->offset = 0;
1606 read_bytes (conn->readfd->fd, (guint8 *) builder->buffer,
1607 &builder->offset, 1, conn->ctxp);
1608 if (res != GST_RTSP_OK)
1611 /* we have 1 bytes now and we can see if this is a data message or
1613 if (builder->buffer[0] == '$') {
1614 /* data message, prepare for the header */
1615 builder->state = STATE_DATA_HEADER;
1618 builder->state = STATE_READ_LINES;
1621 case STATE_DATA_HEADER:
1624 read_bytes (conn->readfd->fd, (guint8 *) builder->buffer,
1625 &builder->offset, 4, conn->ctxp);
1626 if (res != GST_RTSP_OK)
1629 gst_rtsp_message_init_data (message, builder->buffer[1]);
1631 builder->body_len = (builder->buffer[2] << 8) | builder->buffer[3];
1632 builder->body_data = g_malloc (builder->body_len + 1);
1633 builder->body_data[builder->body_len] = '\0';
1634 builder->offset = 0;
1635 builder->state = STATE_DATA_BODY;
1638 case STATE_DATA_BODY:
1641 read_bytes (conn->readfd->fd, builder->body_data, &builder->offset,
1642 builder->body_len, conn->ctxp);
1643 if (res != GST_RTSP_OK)
1646 /* we have the complete body now, store in the message adjusting the
1647 * length to include the traling '\0' */
1648 gst_rtsp_message_take_body (message,
1649 (guint8 *) builder->body_data, builder->body_len + 1);
1650 builder->body_data = NULL;
1651 builder->body_len = 0;
1653 builder->state = STATE_END;
1656 case STATE_READ_LINES:
1658 res = read_line (conn->readfd->fd, builder->buffer, &builder->offset,
1659 sizeof (builder->buffer), conn->ctxp);
1660 if (res != GST_RTSP_OK)
1663 /* we have a regular response */
1664 if (builder->buffer[0] == '\r') {
1665 builder->buffer[0] = '\0';
1668 if (builder->buffer[0] == '\0') {
1671 /* empty line, end of message header */
1672 /* see if there is a Content-Length header */
1673 if (gst_rtsp_message_get_header (message,
1674 GST_RTSP_HDR_CONTENT_LENGTH, &hdrval, 0) == GST_RTSP_OK) {
1675 /* there is, prepare to read the body */
1676 builder->body_len = atol (hdrval);
1677 builder->body_data = g_malloc (builder->body_len + 1);
1678 builder->body_data[builder->body_len] = '\0';
1679 builder->offset = 0;
1680 builder->state = STATE_DATA_BODY;
1682 builder->state = STATE_END;
1687 /* we have a line */
1688 if (builder->line == 0) {
1689 /* first line, check for response status */
1690 if (memcmp (builder->buffer, "RTSP", 4) == 0) {
1691 res = parse_response_status (builder->buffer, message);
1693 res = parse_request_line (conn, builder->buffer, message);
1695 /* the first line must parse without errors */
1696 if (res != GST_RTSP_OK)
1699 /* else just parse the line, ignore errors */
1700 parse_line (conn, builder->buffer, message);
1703 builder->offset = 0;
1710 if (conn->tstate == TUNNEL_STATE_GET) {
1711 res = GST_RTSP_ETGET;
1713 } else if (conn->tstate == TUNNEL_STATE_POST) {
1714 res = GST_RTSP_ETPOST;
1718 if (message->type == GST_RTSP_MESSAGE_DATA) {
1719 /* data messages don't have headers */
1724 /* save session id in the connection for further use */
1725 if (message->type == GST_RTSP_MESSAGE_RESPONSE &&
1726 gst_rtsp_message_get_header (message, GST_RTSP_HDR_SESSION,
1727 &session_id, 0) == GST_RTSP_OK) {
1730 maxlen = sizeof (conn->session_id) - 1;
1731 /* the sessionid can have attributes marked with ;
1732 * Make sure we strip them */
1733 for (i = 0; session_id[i] != '\0'; i++) {
1734 if (session_id[i] == ';') {
1739 } while (g_ascii_isspace (session_id[i]));
1740 if (g_str_has_prefix (&session_id[i], "timeout=")) {
1743 /* if we parsed something valid, configure */
1744 if ((to = atoi (&session_id[i + 8])) > 0)
1751 /* make sure to not overflow */
1752 strncpy (conn->session_id, session_id, maxlen);
1753 conn->session_id[maxlen] = '\0';
1759 res = GST_RTSP_ERROR;
1768 * gst_rtsp_connection_read:
1769 * @conn: a #GstRTSPConnection
1770 * @data: the data to read
1771 * @size: the size of @data
1772 * @timeout: a timeout value or #NULL
1774 * Attempt to read @size bytes into @data from the connected @conn, blocking up to
1775 * the specified @timeout. @timeout can be #NULL, in which case this function
1776 * might block forever.
1778 * This function can be cancelled with gst_rtsp_connection_flush().
1780 * Returns: #GST_RTSP_OK on success.
1783 gst_rtsp_connection_read (GstRTSPConnection * conn, guint8 * data, guint size,
1791 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1792 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
1793 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1795 if (G_UNLIKELY (size == 0))
1800 /* configure timeout if any */
1801 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1803 gst_poll_set_controllable (conn->fdset, TRUE);
1804 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
1805 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
1808 res = read_bytes (conn->readfd->fd, data, &offset, size, conn->ctxp);
1809 if (G_UNLIKELY (res == GST_RTSP_EEOF))
1811 if (G_LIKELY (res == GST_RTSP_OK))
1813 if (G_UNLIKELY (res != GST_RTSP_EINTR))
1817 retval = gst_poll_wait (conn->fdset, to);
1818 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1820 /* check for timeout */
1821 if (G_UNLIKELY (retval == 0))
1822 goto select_timeout;
1824 if (G_UNLIKELY (retval == -1)) {
1830 gst_poll_set_controllable (conn->fdset, FALSE);
1837 return GST_RTSP_ESYS;
1841 return GST_RTSP_ETIMEOUT;
1845 return GST_RTSP_EINTR;
1849 return GST_RTSP_EEOF;
1858 gen_tunnel_reply (GstRTSPConnection * conn, GstRTSPStatusCode code)
1861 gchar date_string[100];
1862 const gchar *status;
1864 gen_date_string (date_string, sizeof (date_string));
1866 status = gst_rtsp_status_as_text (code);
1867 if (status == NULL) {
1868 code = GST_RTSP_STS_INTERNAL_SERVER_ERROR;
1869 status = "Internal Server Error";
1872 str = g_string_new ("");
1875 g_string_append_printf (str, "HTTP/1.0 %d %s\r\n", code, status);
1876 g_string_append_printf (str,
1877 "Server: GStreamer RTSP Server\r\n"
1879 "Connection: close\r\n"
1880 "Cache-Control: no-store\r\n" "Pragma: no-cache\r\n", date_string);
1881 if (code == GST_RTSP_STS_OK) {
1883 g_string_append_printf (str, "x-server-ip-address: %s\r\n", conn->ip);
1884 g_string_append_printf (str,
1885 "Content-Type: application/x-rtsp-tunnelled\r\n");
1887 g_string_append_printf (str, "\r\n");
1892 * gst_rtsp_connection_receive:
1893 * @conn: a #GstRTSPConnection
1894 * @message: the message to read
1895 * @timeout: a timeout value or #NULL
1897 * Attempt to read into @message from the connected @conn, blocking up to
1898 * the specified @timeout. @timeout can be #NULL, in which case this function
1899 * might block forever.
1901 * This function can be cancelled with gst_rtsp_connection_flush().
1903 * Returns: #GST_RTSP_OK on success.
1906 gst_rtsp_connection_receive (GstRTSPConnection * conn, GstRTSPMessage * message,
1910 GstRTSPBuilder builder;
1914 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1915 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1916 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1918 /* configure timeout if any */
1919 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1921 gst_poll_set_controllable (conn->fdset, TRUE);
1922 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
1923 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
1925 memset (&builder, 0, sizeof (GstRTSPBuilder));
1927 res = build_next (&builder, message, conn);
1928 if (G_UNLIKELY (res == GST_RTSP_EEOF))
1930 if (G_LIKELY (res == GST_RTSP_OK))
1932 if (res == GST_RTSP_ETGET) {
1935 /* tunnel GET request, we can reply now */
1936 str = gen_tunnel_reply (conn, GST_RTSP_STS_OK);
1938 gst_rtsp_connection_write (conn, (guint8 *) str->str, str->len,
1940 g_string_free (str, TRUE);
1941 } else if (res == GST_RTSP_ETPOST) {
1942 /* tunnel POST request, return the value, the caller now has to link the
1943 * two connections. */
1945 } else if (G_UNLIKELY (res != GST_RTSP_EINTR))
1949 retval = gst_poll_wait (conn->fdset, to);
1950 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1952 /* check for timeout */
1953 if (G_UNLIKELY (retval == 0))
1954 goto select_timeout;
1956 if (G_UNLIKELY (retval == -1)) {
1962 gst_poll_set_controllable (conn->fdset, FALSE);
1965 /* we have a message here */
1966 build_reset (&builder);
1973 res = GST_RTSP_ESYS;
1978 res = GST_RTSP_ETIMEOUT;
1983 res = GST_RTSP_EINTR;
1988 res = GST_RTSP_EEOF;
1994 build_reset (&builder);
1995 gst_rtsp_message_unset (message);
2001 * gst_rtsp_connection_close:
2002 * @conn: a #GstRTSPConnection
2004 * Close the connected @conn. After this call, the connection is in the same
2005 * state as when it was first created.
2007 * Returns: #GST_RTSP_OK on success.
2010 gst_rtsp_connection_close (GstRTSPConnection * conn)
2012 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2017 REMOVE_POLLFD (conn->fdset, &conn->fd0);
2018 REMOVE_POLLFD (conn->fdset, &conn->fd1);
2019 conn->writefd = NULL;
2020 conn->readfd = NULL;
2021 conn->tunneled = FALSE;
2022 conn->tstate = TUNNEL_STATE_NONE;
2024 g_free (conn->username);
2025 conn->username = NULL;
2026 g_free (conn->passwd);
2027 conn->passwd = NULL;
2028 gst_rtsp_connection_clear_auth_params (conn);
2031 conn->session_id[0] = '\0';
2037 * gst_rtsp_connection_free:
2038 * @conn: a #GstRTSPConnection
2040 * Close and free @conn.
2042 * Returns: #GST_RTSP_OK on success.
2045 gst_rtsp_connection_free (GstRTSPConnection * conn)
2049 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2051 res = gst_rtsp_connection_close (conn);
2052 gst_poll_free (conn->fdset);
2053 g_timer_destroy (conn->timer);
2054 gst_rtsp_url_free (conn->url);
2055 g_free (conn->proxy_host);
2065 * gst_rtsp_connection_poll:
2066 * @conn: a #GstRTSPConnection
2067 * @events: a bitmask of #GstRTSPEvent flags to check
2068 * @revents: location for result flags
2069 * @timeout: a timeout
2071 * Wait up to the specified @timeout for the connection to become available for
2072 * at least one of the operations specified in @events. When the function returns
2073 * with #GST_RTSP_OK, @revents will contain a bitmask of available operations on
2076 * @timeout can be #NULL, in which case this function might block forever.
2078 * This function can be cancelled with gst_rtsp_connection_flush().
2080 * Returns: #GST_RTSP_OK on success.
2085 gst_rtsp_connection_poll (GstRTSPConnection * conn, GstRTSPEvent events,
2086 GstRTSPEvent * revents, GTimeVal * timeout)
2091 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2092 g_return_val_if_fail (events != 0, GST_RTSP_EINVAL);
2093 g_return_val_if_fail (revents != NULL, GST_RTSP_EINVAL);
2094 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2095 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2097 gst_poll_set_controllable (conn->fdset, TRUE);
2099 /* add fd to writer set when asked to */
2100 gst_poll_fd_ctl_write (conn->fdset, conn->writefd,
2101 events & GST_RTSP_EV_WRITE);
2103 /* add fd to reader set when asked to */
2104 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, events & GST_RTSP_EV_READ);
2106 /* configure timeout if any */
2107 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
2110 retval = gst_poll_wait (conn->fdset, to);
2111 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
2113 if (G_UNLIKELY (retval == 0))
2114 goto select_timeout;
2116 if (G_UNLIKELY (retval == -1)) {
2124 if (events & GST_RTSP_EV_READ) {
2125 if (gst_poll_fd_can_read (conn->fdset, conn->readfd))
2126 *revents |= GST_RTSP_EV_READ;
2128 if (events & GST_RTSP_EV_WRITE) {
2129 if (gst_poll_fd_can_write (conn->fdset, conn->writefd))
2130 *revents |= GST_RTSP_EV_WRITE;
2137 return GST_RTSP_ETIMEOUT;
2141 return GST_RTSP_ESYS;
2145 return GST_RTSP_EINTR;
2150 * gst_rtsp_connection_next_timeout:
2151 * @conn: a #GstRTSPConnection
2152 * @timeout: a timeout
2154 * Calculate the next timeout for @conn, storing the result in @timeout.
2156 * Returns: #GST_RTSP_OK.
2159 gst_rtsp_connection_next_timeout (GstRTSPConnection * conn, GTimeVal * timeout)
2165 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2166 g_return_val_if_fail (timeout != NULL, GST_RTSP_EINVAL);
2168 elapsed = g_timer_elapsed (conn->timer, &usec);
2169 if (elapsed >= conn->timeout) {
2173 sec = conn->timeout - elapsed;
2176 timeout->tv_sec = sec;
2177 timeout->tv_usec = usec;
2183 * gst_rtsp_connection_reset_timeout:
2184 * @conn: a #GstRTSPConnection
2186 * Reset the timeout of @conn.
2188 * Returns: #GST_RTSP_OK.
2191 gst_rtsp_connection_reset_timeout (GstRTSPConnection * conn)
2193 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2195 g_timer_start (conn->timer);
2201 * gst_rtsp_connection_flush:
2202 * @conn: a #GstRTSPConnection
2203 * @flush: start or stop the flush
2205 * Start or stop the flushing action on @conn. When flushing, all current
2206 * and future actions on @conn will return #GST_RTSP_EINTR until the connection
2207 * is set to non-flushing mode again.
2209 * Returns: #GST_RTSP_OK.
2212 gst_rtsp_connection_flush (GstRTSPConnection * conn, gboolean flush)
2214 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2216 gst_poll_set_flushing (conn->fdset, flush);
2222 * gst_rtsp_connection_set_proxy:
2223 * @conn: a #GstRTSPConnection
2224 * @host: the proxy host
2225 * @port: the proxy port
2227 * Set the proxy host and port.
2229 * Returns: #GST_RTSP_OK.
2234 gst_rtsp_connection_set_proxy (GstRTSPConnection * conn,
2235 const gchar * host, guint port)
2237 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2239 g_free (conn->proxy_host);
2240 conn->proxy_host = g_strdup (host);
2241 conn->proxy_port = port;
2247 * gst_rtsp_connection_set_auth:
2248 * @conn: a #GstRTSPConnection
2249 * @method: authentication method
2251 * @pass: the password
2253 * Configure @conn for authentication mode @method with @user and @pass as the
2254 * user and password respectively.
2256 * Returns: #GST_RTSP_OK.
2259 gst_rtsp_connection_set_auth (GstRTSPConnection * conn,
2260 GstRTSPAuthMethod method, const gchar * user, const gchar * pass)
2262 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2264 if (method == GST_RTSP_AUTH_DIGEST && ((user == NULL || pass == NULL)
2265 || g_strrstr (user, ":") != NULL))
2266 return GST_RTSP_EINVAL;
2268 /* Make sure the username and passwd are being set for authentication */
2269 if (method == GST_RTSP_AUTH_NONE && (user == NULL || pass == NULL))
2270 return GST_RTSP_EINVAL;
2272 /* ":" chars are not allowed in usernames for basic auth */
2273 if (method == GST_RTSP_AUTH_BASIC && g_strrstr (user, ":") != NULL)
2274 return GST_RTSP_EINVAL;
2276 g_free (conn->username);
2277 g_free (conn->passwd);
2279 conn->auth_method = method;
2280 conn->username = g_strdup (user);
2281 conn->passwd = g_strdup (pass);
2288 * @key: ASCII string to hash
2290 * Hashes @key in a case-insensitive manner.
2292 * Returns: the hash code.
2295 str_case_hash (gconstpointer key)
2297 const char *p = key;
2298 guint h = g_ascii_toupper (*p);
2301 for (p += 1; *p != '\0'; p++)
2302 h = (h << 5) - h + g_ascii_toupper (*p);
2309 * @v1: an ASCII string
2310 * @v2: another ASCII string
2312 * Compares @v1 and @v2 in a case-insensitive manner
2314 * Returns: %TRUE if they are equal (modulo case)
2317 str_case_equal (gconstpointer v1, gconstpointer v2)
2319 const char *string1 = v1;
2320 const char *string2 = v2;
2322 return g_ascii_strcasecmp (string1, string2) == 0;
2326 * gst_rtsp_connection_set_auth_param:
2327 * @conn: a #GstRTSPConnection
2328 * @param: authentication directive
2331 * Setup @conn with authentication directives. This is not necesary for
2332 * methods #GST_RTSP_AUTH_NONE and #GST_RTSP_AUTH_BASIC. For
2333 * #GST_RTSP_AUTH_DIGEST, directives should be taken from the digest challenge
2334 * in the WWW-Authenticate response header and can include realm, domain,
2335 * nonce, opaque, stale, algorithm, qop as per RFC2617.
2340 gst_rtsp_connection_set_auth_param (GstRTSPConnection * conn,
2341 const gchar * param, const gchar * value)
2343 g_return_if_fail (conn != NULL);
2344 g_return_if_fail (param != NULL);
2346 if (conn->auth_params == NULL) {
2348 g_hash_table_new_full (str_case_hash, str_case_equal, g_free, g_free);
2350 g_hash_table_insert (conn->auth_params, g_strdup (param), g_strdup (value));
2354 * gst_rtsp_connection_clear_auth_params:
2355 * @conn: a #GstRTSPConnection
2357 * Clear the list of authentication directives stored in @conn.
2362 gst_rtsp_connection_clear_auth_params (GstRTSPConnection * conn)
2364 g_return_if_fail (conn != NULL);
2366 if (conn->auth_params != NULL) {
2367 g_hash_table_destroy (conn->auth_params);
2368 conn->auth_params = NULL;
2372 static GstRTSPResult
2373 set_qos_dscp (gint fd, guint qos_dscp)
2375 union gst_sockaddr sa;
2376 socklen_t slen = sizeof (sa);
2383 if (getsockname (fd, &sa.sa, &slen) < 0)
2384 goto no_getsockname;
2386 af = sa.sa.sa_family;
2388 /* if this is an IPv4-mapped address then do IPv4 QoS */
2389 if (af == AF_INET6) {
2390 if (IN6_IS_ADDR_V4MAPPED (&sa.sa_in6.sin6_addr))
2394 /* extract and shift 6 bits of the DSCP */
2395 tos = (qos_dscp & 0x3f) << 2;
2399 if (SETSOCKOPT (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0)
2404 if (SETSOCKOPT (fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof (tos)) < 0)
2418 return GST_RTSP_ESYS;
2423 return GST_RTSP_ERROR;
2428 * gst_rtsp_connection_set_qos_dscp:
2429 * @conn: a #GstRTSPConnection
2430 * @qos_dscp: DSCP value
2432 * Configure @conn to use the specified DSCP value.
2434 * Returns: #GST_RTSP_OK on success.
2439 gst_rtsp_connection_set_qos_dscp (GstRTSPConnection * conn, guint qos_dscp)
2443 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2444 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2445 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2447 res = set_qos_dscp (conn->fd0.fd, qos_dscp);
2448 if (res == GST_RTSP_OK)
2449 res = set_qos_dscp (conn->fd1.fd, qos_dscp);
2456 * gst_rtsp_connection_get_url:
2457 * @conn: a #GstRTSPConnection
2459 * Retrieve the URL of the other end of @conn.
2461 * Returns: The URL. This value remains valid until the
2462 * connection is freed.
2467 gst_rtsp_connection_get_url (const GstRTSPConnection * conn)
2469 g_return_val_if_fail (conn != NULL, NULL);
2475 * gst_rtsp_connection_get_ip:
2476 * @conn: a #GstRTSPConnection
2478 * Retrieve the IP address of the other end of @conn.
2480 * Returns: The IP address as a string. this value remains valid until the
2481 * connection is closed.
2486 gst_rtsp_connection_get_ip (const GstRTSPConnection * conn)
2488 g_return_val_if_fail (conn != NULL, NULL);
2494 * gst_rtsp_connection_set_ip:
2495 * @conn: a #GstRTSPConnection
2496 * @ip: an ip address
2498 * Set the IP address of the server.
2503 gst_rtsp_connection_set_ip (GstRTSPConnection * conn, const gchar * ip)
2505 g_return_if_fail (conn != NULL);
2508 conn->ip = g_strdup (ip);
2512 * gst_rtsp_connection_get_readfd:
2513 * @conn: a #GstRTSPConnection
2515 * Get the file descriptor for reading.
2517 * Returns: the file descriptor used for reading or -1 on error. The file
2518 * descriptor remains valid until the connection is closed.
2523 gst_rtsp_connection_get_readfd (const GstRTSPConnection * conn)
2525 g_return_val_if_fail (conn != NULL, -1);
2526 g_return_val_if_fail (conn->readfd != NULL, -1);
2528 return conn->readfd->fd;
2532 * gst_rtsp_connection_get_writefd:
2533 * @conn: a #GstRTSPConnection
2535 * Get the file descriptor for writing.
2537 * Returns: the file descriptor used for writing or -1 on error. The file
2538 * descriptor remains valid until the connection is closed.
2543 gst_rtsp_connection_get_writefd (const GstRTSPConnection * conn)
2545 g_return_val_if_fail (conn != NULL, -1);
2546 g_return_val_if_fail (conn->writefd != NULL, -1);
2548 return conn->writefd->fd;
2553 * gst_rtsp_connection_set_tunneled:
2554 * @conn: a #GstRTSPConnection
2555 * @tunneled: the new state
2557 * Set the HTTP tunneling state of the connection. This must be configured before
2558 * the @conn is connected.
2563 gst_rtsp_connection_set_tunneled (GstRTSPConnection * conn, gboolean tunneled)
2565 g_return_if_fail (conn != NULL);
2566 g_return_if_fail (conn->readfd == NULL);
2567 g_return_if_fail (conn->writefd == NULL);
2569 conn->tunneled = tunneled;
2573 * gst_rtsp_connection_is_tunneled:
2574 * @conn: a #GstRTSPConnection
2576 * Get the tunneling state of the connection.
2578 * Returns: if @conn is using HTTP tunneling.
2583 gst_rtsp_connection_is_tunneled (const GstRTSPConnection * conn)
2585 g_return_val_if_fail (conn != NULL, FALSE);
2587 return conn->tunneled;
2591 * gst_rtsp_connection_get_tunnelid:
2592 * @conn: a #GstRTSPConnection
2594 * Get the tunnel session id the connection.
2596 * Returns: returns a non-empty string if @conn is being tunneled over HTTP.
2601 gst_rtsp_connection_get_tunnelid (const GstRTSPConnection * conn)
2603 g_return_val_if_fail (conn != NULL, NULL);
2605 if (!conn->tunneled)
2608 return conn->tunnelid;
2612 * gst_rtsp_connection_do_tunnel:
2613 * @conn: a #GstRTSPConnection
2614 * @conn2: a #GstRTSPConnection
2616 * If @conn received the first tunnel connection and @conn2 received
2617 * the second tunnel connection, link the two connections together so that
2618 * @conn manages the tunneled connection.
2620 * After this call, @conn2 cannot be used anymore and must be freed with
2621 * gst_rtsp_connection_free().
2623 * Returns: return GST_RTSP_OK on success.
2628 gst_rtsp_connection_do_tunnel (GstRTSPConnection * conn,
2629 GstRTSPConnection * conn2)
2631 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2632 g_return_val_if_fail (conn2 != NULL, GST_RTSP_EINVAL);
2633 g_return_val_if_fail (conn->tstate == TUNNEL_STATE_GET, GST_RTSP_EINVAL);
2634 g_return_val_if_fail (conn2->tstate == TUNNEL_STATE_POST, GST_RTSP_EINVAL);
2635 g_return_val_if_fail (!memcmp (conn2->tunnelid, conn->tunnelid, TUNNELID_LEN),
2638 /* both connections have fd0 as the read/write socket. start by taking the
2639 * socket from conn2 and set it as the socket in conn */
2640 conn->fd1 = conn2->fd0;
2642 /* clean up some of the state of conn2 */
2643 gst_poll_remove_fd (conn2->fdset, &conn2->fd0);
2645 conn2->readfd = conn2->writefd = NULL;
2647 /* We make fd0 the write socket and fd1 the read socket. */
2648 conn->writefd = &conn->fd0;
2649 conn->readfd = &conn->fd1;
2651 conn->tstate = TUNNEL_STATE_COMPLETE;
2653 /* we need base64 decoding for the readfd */
2654 conn->ctx.state = 0;
2657 conn->ctx.coutl = 0;
2658 conn->ctxp = &conn->ctx;
2663 #define READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
2664 #define WRITE_COND (G_IO_OUT | G_IO_ERR)
2673 /* async functions */
2674 struct _GstRTSPWatch
2678 GstRTSPConnection *conn;
2680 GstRTSPBuilder builder;
2681 GstRTSPMessage message;
2685 gboolean write_added;
2687 /* queued message for transmission */
2689 GAsyncQueue *messages;
2695 GstRTSPWatchFuncs funcs;
2698 GDestroyNotify notify;
2702 gst_rtsp_source_prepare (GSource * source, gint * timeout)
2704 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2706 *timeout = (watch->conn->timeout * 1000);
2712 gst_rtsp_source_check (GSource * source)
2714 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2716 if (watch->readfd.revents & READ_COND)
2719 if (watch->writefd.revents & WRITE_COND)
2726 gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback G_GNUC_UNUSED,
2727 gpointer user_data G_GNUC_UNUSED)
2729 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2732 /* first read as much as we can */
2733 if (watch->readfd.revents & READ_COND) {
2735 res = build_next (&watch->builder, &watch->message, watch->conn);
2736 if (res == GST_RTSP_EINTR)
2738 if (G_UNLIKELY (res == GST_RTSP_EEOF))
2740 if (res == GST_RTSP_ETGET) {
2742 GstRTSPStatusCode code;
2745 if (watch->funcs.tunnel_start)
2746 code = watch->funcs.tunnel_start (watch, watch->user_data);
2748 code = GST_RTSP_STS_OK;
2750 /* queue the response string */
2751 str = gen_tunnel_reply (watch->conn, code);
2753 gst_rtsp_watch_queue_data (watch, (guint8 *) g_string_free (str, FALSE),
2755 } else if (res == GST_RTSP_ETPOST) {
2756 /* in the callback the connection should be tunneled with the
2758 if (watch->funcs.tunnel_complete)
2759 watch->funcs.tunnel_complete (watch, watch->user_data);
2760 } else if (G_UNLIKELY (res != GST_RTSP_OK))
2763 if (G_LIKELY (res == GST_RTSP_OK)) {
2764 if (watch->funcs.message_received)
2765 watch->funcs.message_received (watch, &watch->message,
2768 gst_rtsp_message_unset (&watch->message);
2770 build_reset (&watch->builder);
2774 if (watch->writefd.revents & WRITE_COND) {
2776 if (watch->write_data == NULL) {
2779 /* get a new message from the queue */
2780 rec = g_async_queue_try_pop (watch->messages);
2784 watch->write_off = 0;
2785 watch->write_data = rec->data;
2786 watch->write_size = rec->size;
2787 watch->write_id = rec->id;
2789 g_slice_free (GstRTSPRec, rec);
2792 res = write_bytes (watch->writefd.fd, watch->write_data,
2793 &watch->write_off, watch->write_size);
2794 if (res == GST_RTSP_EINTR)
2796 if (G_UNLIKELY (res != GST_RTSP_OK))
2799 if (watch->funcs.message_sent)
2800 watch->funcs.message_sent (watch, watch->write_id, watch->user_data);
2803 if (g_async_queue_length (watch->messages) == 0 && watch->write_added) {
2804 g_source_remove_poll ((GSource *) watch, &watch->writefd);
2805 watch->write_added = FALSE;
2806 watch->writefd.revents = 0;
2808 g_free (watch->write_data);
2809 watch->write_data = NULL;
2818 if (watch->funcs.closed)
2819 watch->funcs.closed (watch, watch->user_data);
2824 if (watch->funcs.error)
2825 watch->funcs.error (watch, res, watch->user_data);
2831 gst_rtsp_rec_free (gpointer data)
2833 GstRTSPRec *rec = data;
2836 g_slice_free (GstRTSPRec, rec);
2840 gst_rtsp_source_finalize (GSource * source)
2842 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2844 build_reset (&watch->builder);
2845 gst_rtsp_message_unset (&watch->message);
2847 g_async_queue_unref (watch->messages);
2848 watch->messages = NULL;
2850 g_free (watch->write_data);
2853 watch->notify (watch->user_data);
2856 static GSourceFuncs gst_rtsp_source_funcs = {
2857 gst_rtsp_source_prepare,
2858 gst_rtsp_source_check,
2859 gst_rtsp_source_dispatch,
2860 gst_rtsp_source_finalize,
2866 * gst_rtsp_watch_new:
2867 * @conn: a #GstRTSPConnection
2868 * @funcs: watch functions
2869 * @user_data: user data to pass to @funcs
2870 * @notify: notify when @user_data is not referenced anymore
2872 * Create a watch object for @conn. The functions provided in @funcs will be
2873 * called with @user_data when activity happened on the watch.
2875 * The new watch is usually created so that it can be attached to a
2876 * maincontext with gst_rtsp_watch_attach().
2878 * @conn must exist for the entire lifetime of the watch.
2880 * Returns: a #GstRTSPWatch that can be used for asynchronous RTSP
2881 * communication. Free with gst_rtsp_watch_unref () after usage.
2886 gst_rtsp_watch_new (GstRTSPConnection * conn,
2887 GstRTSPWatchFuncs * funcs, gpointer user_data, GDestroyNotify notify)
2889 GstRTSPWatch *result;
2891 g_return_val_if_fail (conn != NULL, NULL);
2892 g_return_val_if_fail (funcs != NULL, NULL);
2893 g_return_val_if_fail (conn->readfd != NULL, NULL);
2894 g_return_val_if_fail (conn->writefd != NULL, NULL);
2896 result = (GstRTSPWatch *) g_source_new (&gst_rtsp_source_funcs,
2897 sizeof (GstRTSPWatch));
2899 result->conn = conn;
2900 result->builder.state = STATE_START;
2902 result->messages = g_async_queue_new_full (gst_rtsp_rec_free);
2904 result->readfd.fd = -1;
2905 result->writefd.fd = -1;
2907 gst_rtsp_watch_reset (result);
2909 result->funcs = *funcs;
2910 result->user_data = user_data;
2911 result->notify = notify;
2913 /* only add the read fd, the write fd is only added when we have data
2915 g_source_add_poll ((GSource *) result, &result->readfd);
2921 * gst_rtsp_watch_reset:
2922 * @watch: a #GstRTSPWatch
2924 * Reset @watch, this is usually called after gst_rtsp_connection_do_tunnel()
2925 * when the file descriptors of the connection might have changed.
2930 gst_rtsp_watch_reset (GstRTSPWatch * watch)
2932 if (watch->readfd.fd != -1)
2933 g_source_remove_poll ((GSource *) watch, &watch->readfd);
2934 if (watch->writefd.fd != -1)
2935 g_source_remove_poll ((GSource *) watch, &watch->writefd);
2937 watch->readfd.fd = watch->conn->readfd->fd;
2938 watch->readfd.events = READ_COND;
2939 watch->readfd.revents = 0;
2941 watch->writefd.fd = watch->conn->writefd->fd;
2942 watch->writefd.events = WRITE_COND;
2943 watch->writefd.revents = 0;
2944 watch->write_added = FALSE;
2946 g_source_add_poll ((GSource *) watch, &watch->readfd);
2950 * gst_rtsp_watch_attach:
2951 * @watch: a #GstRTSPWatch
2952 * @context: a GMainContext (if NULL, the default context will be used)
2954 * Adds a #GstRTSPWatch to a context so that it will be executed within that context.
2956 * Returns: the ID (greater than 0) for the watch within the GMainContext.
2961 gst_rtsp_watch_attach (GstRTSPWatch * watch, GMainContext * context)
2963 g_return_val_if_fail (watch != NULL, 0);
2965 return g_source_attach ((GSource *) watch, context);
2969 * gst_rtsp_watch_unref:
2970 * @watch: a #GstRTSPWatch
2972 * Decreases the reference count of @watch by one. If the resulting reference
2973 * count is zero the watch and associated memory will be destroyed.
2978 gst_rtsp_watch_unref (GstRTSPWatch * watch)
2980 g_return_if_fail (watch != NULL);
2982 g_source_unref ((GSource *) watch);
2986 * gst_rtsp_watch_queue_data:
2987 * @watch: a #GstRTSPWatch
2988 * @data: the data to queue
2989 * @size: the size of @data
2991 * Queue @data for transmission in @watch. It will be transmitted when the
2992 * connection of the @watch becomes writable.
2994 * This function will take ownership of @data and g_free() it after use.
2996 * The return value of this function will be used as the id argument in the
2997 * message_sent callback.
3004 gst_rtsp_watch_queue_data (GstRTSPWatch * watch, const guint8 * data,
3009 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3010 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
3011 g_return_val_if_fail (size != 0, GST_RTSP_EINVAL);
3013 /* make a record with the data and id */
3014 rec = g_slice_new (GstRTSPRec);
3015 rec->data = (guint8 *) data;
3018 /* make sure rec->id is never 0 */
3019 rec->id = ++watch->id;
3020 } while (G_UNLIKELY (rec->id == 0));
3022 /* add the record to a queue. FIXME we would like to have an upper limit here */
3023 g_async_queue_push (watch->messages, rec);
3025 /* FIXME: does the following need to be made thread-safe? (this might be
3026 * called from a streaming thread, like appsink's render function) */
3027 /* make sure the main context will now also check for writability on the
3029 if (!watch->write_added) {
3030 g_source_add_poll ((GSource *) watch, &watch->writefd);
3031 watch->write_added = TRUE;
3038 * gst_rtsp_watch_queue_message:
3039 * @watch: a #GstRTSPWatch
3040 * @message: a #GstRTSPMessage
3042 * Queue a @message for transmission in @watch. The contents of this
3043 * message will be serialized and transmitted when the connection of the
3044 * @watch becomes writable.
3046 * The return value of this function will be used as the id argument in the
3047 * message_sent callback.
3054 gst_rtsp_watch_queue_message (GstRTSPWatch * watch, GstRTSPMessage * message)
3059 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3060 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3062 /* make a record with the message as a string and id */
3063 str = message_to_string (watch->conn, message);
3065 return gst_rtsp_watch_queue_data (watch,
3066 (guint8 *) g_string_free (str, FALSE), size);