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 */
80 #define EINPROGRESS WSAEINPROGRESS
82 #include <sys/ioctl.h>
84 #include <sys/socket.h>
85 #include <netinet/in.h>
86 #include <arpa/inet.h>
90 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
91 #include <sys/filio.h>
94 #include "gstrtspconnection.h"
95 #include "gstrtspbase64.h"
102 guchar out[3]; /* the size must be evenly divisible by 3 */
107 static GstRTSPResult read_line (gint fd, guint8 * buffer, guint * idx,
108 guint size, DecodeCtx * ctxp);
109 static GstRTSPResult parse_key_value (guint8 * buffer, gchar * key,
110 guint keysize, gchar ** value);
111 static void parse_string (gchar * dest, gint size, gchar ** src);
114 #define READ_SOCKET(fd, buf, len) recv (fd, (char *)buf, len, 0)
115 #define WRITE_SOCKET(fd, buf, len) send (fd, (const char *)buf, len, 0)
116 #define SETSOCKOPT(sock, level, name, val, len) setsockopt (sock, level, name, (const char *)val, len)
117 #define CLOSE_SOCKET(sock) closesocket (sock)
118 #define ERRNO_IS_EAGAIN (WSAGetLastError () == WSAEWOULDBLOCK)
119 #define ERRNO_IS_EINTR (WSAGetLastError () == WSAEINTR)
120 /* According to Microsoft's connect() documentation this one returns
121 * WSAEWOULDBLOCK and not WSAEINPROGRESS. */
122 #define ERRNO_IS_EINPROGRESS (WSAGetLastError () == WSAEWOULDBLOCK)
124 #define READ_SOCKET(fd, buf, len) read (fd, buf, len)
125 #define WRITE_SOCKET(fd, buf, len) write (fd, buf, len)
126 #define SETSOCKOPT(sock, level, name, val, len) setsockopt (sock, level, name, val, len)
127 #define CLOSE_SOCKET(sock) close (sock)
128 #define ERRNO_IS_EAGAIN (errno == EAGAIN)
129 #define ERRNO_IS_EINTR (errno == EINTR)
130 #define ERRNO_IS_EINPROGRESS (errno == EINPROGRESS)
133 #define ADD_POLLFD(fdset, pfd, fd) \
136 gst_poll_add_fd (fdset, pfd); \
139 #define REMOVE_POLLFD(fdset, pfd) \
141 if ((pfd)->fd != -1) { \
142 GST_DEBUG ("remove fd %d", (pfd)->fd); \
143 gst_poll_remove_fd (fdset, pfd); \
144 CLOSE_SOCKET ((pfd)->fd); \
154 TUNNEL_STATE_COMPLETE
155 } GstRTSPTunnelState;
157 #define TUNNELID_LEN 24
159 struct _GstRTSPConnection
162 /* URL for the connection */
165 /* connection state */
172 gchar tunnelid[TUNNELID_LEN];
174 GstRTSPTunnelState tstate;
180 gint cseq; /* sequence number */
181 gchar session_id[512]; /* session id */
182 gint timeout; /* session timeout in seconds */
183 GTimer *timer; /* timeout timer */
186 GstRTSPAuthMethod auth_method;
189 GHashTable *auth_params;
200 inet_aton (const char *c, struct in_addr *paddr)
202 /* note that inet_addr is deprecated on unix because
203 * inet_addr returns -1 (INADDR_NONE) for the valid 255.255.255.255
205 paddr->s_addr = inet_addr (c);
207 if (paddr->s_addr == INADDR_NONE)
224 /* a structure for constructing RTSPMessages */
237 build_reset (GstRTSPBuilder * builder)
239 g_free (builder->body_data);
240 memset (builder, 0, sizeof (GstRTSPBuilder));
244 * gst_rtsp_connection_create:
245 * @url: a #GstRTSPUrl
246 * @conn: storage for a #GstRTSPConnection
248 * Create a newly allocated #GstRTSPConnection from @url and store it in @conn.
249 * The connection will not yet attempt to connect to @url, use
250 * gst_rtsp_connection_connect().
252 * A copy of @url will be made.
254 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
257 gst_rtsp_connection_create (const GstRTSPUrl * url, GstRTSPConnection ** conn)
259 GstRTSPConnection *newconn;
265 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
268 error = WSAStartup (0x0202, &w);
273 if (w.wVersion != 0x0202)
277 newconn = g_new0 (GstRTSPConnection, 1);
279 if ((newconn->fdset = gst_poll_new (TRUE)) == NULL)
282 newconn->url = gst_rtsp_url_copy (url);
283 newconn->fd0.fd = -1;
284 newconn->fd1.fd = -1;
285 newconn->timer = g_timer_new ();
286 newconn->timeout = 60;
289 newconn->auth_method = GST_RTSP_AUTH_NONE;
290 newconn->username = NULL;
291 newconn->passwd = NULL;
292 newconn->auth_params = NULL;
302 g_warning ("Error %d on WSAStartup", error);
303 return GST_RTSP_EWSASTART;
307 g_warning ("Windows sockets are not version 0x202 (current 0x%x)",
310 return GST_RTSP_EWSAVERSION;
319 return GST_RTSP_ESYS;
324 * gst_rtsp_connection_accept:
326 * @conn: storage for a #GstRTSPConnection
328 * Accept a new connection on @sock and create a new #GstRTSPConnection for
329 * handling communication on new socket.
331 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
336 gst_rtsp_connection_accept (gint sock, GstRTSPConnection ** conn)
339 unsigned int address_len;
340 GstRTSPConnection *newconn = NULL;
341 struct sockaddr_in address;
347 address_len = sizeof (address);
348 memset (&address, 0, address_len);
351 fd = accept (sock, (struct sockaddr *) &address, &address_len);
353 fd = accept (sock, (struct sockaddr *) &address, (gint *) & address_len);
354 #endif /* G_OS_WIN32 */
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_printf ("%s", inet_ntoa (address.sin_addr));
368 url->port = address.sin_port;
370 /* now create the connection object */
371 gst_rtsp_connection_create (url, &newconn);
372 gst_rtsp_url_free (url);
374 ADD_POLLFD (newconn->fdset, &newconn->fd0, fd);
376 /* both read and write initially */
377 newconn->readfd = &newconn->fd0;
378 newconn->writefd = &newconn->fd0;
387 return GST_RTSP_ESYS;
392 do_resolve (const gchar * host)
394 struct hostent *hostinfo;
398 struct in_addr *addrp;
401 gchar ipbuf[INET_ADDRSTRLEN];
402 #endif /* G_OS_WIN32 */
406 /* first check if it already is an IP address */
407 if (inet_aton (host, &addr)) {
410 hostinfo = gethostbyname (host);
412 goto not_resolved; /* h_errno set */
414 if (hostinfo->h_addrtype != AF_INET)
415 goto not_ip; /* host not an IP host */
417 addrp = (struct in_addr *) hostinfo->h_addr_list[0];
418 /* this is not threadsafe */
419 ip = inet_ntoa (*addrp);
421 addrs = hostinfo->h_addr_list;
422 ip = inet_ntop (AF_INET, (struct in_addr *) addrs[0], ipbuf,
424 #endif /* G_OS_WIN32 */
426 return g_strdup (ip);
431 GST_ERROR ("could not resolve %s", host);
436 GST_ERROR ("not an IP address");
442 do_connect (const gchar * ip, guint16 port, GstPollFD * fdout,
443 GstPoll * fdset, GTimeVal * timeout)
446 struct sockaddr_in sa_in;
449 unsigned long flags = 1;
450 #endif /* G_OS_WIN32 */
454 memset (&sa_in, 0, sizeof (sa_in));
455 sa_in.sin_family = AF_INET; /* network socket */
456 sa_in.sin_port = htons (port); /* on port */
457 sa_in.sin_addr.s_addr = inet_addr (ip); /* on host ip */
459 fd = socket (AF_INET, SOCK_STREAM, 0);
463 /* set to non-blocking mode so that we can cancel the connect */
465 fcntl (fd, F_SETFL, O_NONBLOCK);
467 ioctlsocket (fd, FIONBIO, &flags);
468 #endif /* G_OS_WIN32 */
470 /* add the socket to our fdset */
471 ADD_POLLFD (fdset, fdout, fd);
473 /* we are going to connect ASYNC now */
474 ret = connect (fd, (struct sockaddr *) &sa_in, sizeof (sa_in));
477 if (!ERRNO_IS_EINPROGRESS)
480 /* wait for connect to complete up to the specified timeout or until we got
482 gst_poll_fd_ctl_write (fdset, fdout, TRUE);
484 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
487 retval = gst_poll_wait (fdset, to);
488 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
492 else if (retval == -1)
495 /* we can still have an error connecting on windows */
496 if (gst_poll_fd_has_error (fdset, fdout)) {
497 socklen_t len = sizeof (errno);
499 getsockopt (fd, SOL_SOCKET, SO_ERROR, &errno, &len);
501 getsockopt (fd, SOL_SOCKET, SO_ERROR, (char *) &errno, &len);
506 gst_poll_fd_ignored (fdset, fdout);
514 GST_ERROR ("no socket %d (%s)", errno, g_strerror (errno));
515 return GST_RTSP_ESYS;
519 GST_ERROR ("system error %d (%s)", errno, g_strerror (errno));
520 REMOVE_POLLFD (fdset, fdout);
521 return GST_RTSP_ESYS;
525 GST_ERROR ("timeout");
526 REMOVE_POLLFD (fdset, fdout);
527 return GST_RTSP_ETIMEOUT;
532 setup_tunneling (GstRTSPConnection * conn, GTimeVal * timeout)
540 gchar *ip, *url_port_str;
541 guint16 port, url_port;
542 gchar codestr[4], *resultstr;
547 /* create a random sessionid */
548 for (i = 0; i < TUNNELID_LEN; i++)
549 conn->tunnelid[i] = g_random_int_range ('a', 'z');
550 conn->tunnelid[TUNNELID_LEN - 1] = '\0';
553 /* get the port from the url */
554 gst_rtsp_url_get_port (url, &url_port);
556 if (conn->proxy_host) {
557 hostparam = g_strdup_printf ("Host: %s:%d\r\n", url->host, url_port);
558 url_port_str = g_strdup_printf (":%d", url_port);
559 ip = conn->proxy_host;
560 port = conn->proxy_port;
569 str = g_strdup_printf ("GET %s%s%s%s%s%s HTTP/1.0\r\n"
571 "x-sessioncookie: %s\r\n"
572 "Accept: application/x-rtsp-tunnelled\r\n"
573 "Pragma: no-cache\r\n"
574 "Cache-Control: no-cache\r\n" "\r\n",
575 conn->proxy_host ? "http://" : "",
576 conn->proxy_host ? url->host : "",
577 conn->proxy_host ? url_port_str : "",
578 url->abspath, url->query ? "?" : "", url->query ? url->query : "",
579 hostparam ? hostparam : "", conn->tunnelid);
581 /* we start by writing to this fd */
582 conn->writefd = &conn->fd0;
584 res = gst_rtsp_connection_write (conn, (guint8 *) str, strlen (str), timeout);
586 if (res != GST_RTSP_OK)
589 gst_poll_fd_ctl_write (conn->fdset, &conn->fd0, FALSE);
590 gst_poll_fd_ctl_read (conn->fdset, &conn->fd0, TRUE);
592 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
600 res = read_line (conn->fd0.fd, buffer, &idx, sizeof (buffer), NULL);
601 if (res == GST_RTSP_EEOF)
603 if (res == GST_RTSP_OK)
605 if (res != GST_RTSP_EINTR)
609 retval = gst_poll_wait (conn->fdset, to);
610 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
612 /* check for timeout */
624 /* check for last line */
625 if (buffer[0] == '\r')
627 if (buffer[0] == '\0')
631 /* first line, parse response */
632 gchar versionstr[20];
635 bptr = (gchar *) buffer;
637 parse_string (versionstr, sizeof (versionstr), &bptr);
638 parse_string (codestr, sizeof (codestr), &bptr);
639 code = atoi (codestr);
641 while (g_ascii_isspace (*bptr))
646 if (code != GST_RTSP_STS_OK)
652 /* other lines, parse key/value */
653 res = parse_key_value (buffer, key, sizeof (key), &value);
654 if (res == GST_RTSP_OK) {
655 /* we got a new ip address */
656 if (g_ascii_strcasecmp (key, "x-server-ip-address") == 0) {
657 if (conn->proxy_host) {
658 /* if we use a proxy we need to change the destination url */
660 url->host = g_strdup (value);
662 g_free (url_port_str);
664 g_strdup_printf ("Host: %s:%d\r\n", url->host, url_port);
665 url_port_str = g_strdup_printf (":%d", url_port);
667 /* and resolve the new ip address */
668 if (!(ip = do_resolve (conn->ip)))
679 /* connect to the host/port */
680 res = do_connect (ip, port, &conn->fd1, conn->fdset, timeout);
681 if (res != GST_RTSP_OK)
684 /* this is now our writing socket */
685 conn->writefd = &conn->fd1;
688 str = g_strdup_printf ("POST %s%s%s%s%s%s HTTP/1.0\r\n"
690 "x-sessioncookie: %s\r\n"
691 "Content-Type: application/x-rtsp-tunnelled\r\n"
692 "Pragma: no-cache\r\n"
693 "Cache-Control: no-cache\r\n"
694 "Content-Length: 32767\r\n"
695 "Expires: Sun, 9 Jan 1972 00:00:00 GMT\r\n"
697 conn->proxy_host ? "http://" : "",
698 conn->proxy_host ? url->host : "",
699 conn->proxy_host ? url_port_str : "",
700 url->abspath, url->query ? "?" : "", url->query ? url->query : "",
701 hostparam ? hostparam : "", conn->tunnelid);
703 res = gst_rtsp_connection_write (conn, (guint8 *) str, strlen (str), timeout);
705 if (res != GST_RTSP_OK)
710 g_free (url_port_str);
717 GST_ERROR ("write failed (%d)", res);
731 res = GST_RTSP_ETIMEOUT;
741 res = GST_RTSP_EINTR;
746 GST_ERROR ("got failure response %d %s", code, resultstr);
747 res = GST_RTSP_ERROR;
752 GST_ERROR ("could not resolve %s", conn->ip);
758 GST_ERROR ("failed to connect");
764 * gst_rtsp_connection_connect:
765 * @conn: a #GstRTSPConnection
766 * @timeout: a #GTimeVal timeout
768 * Attempt to connect to the url of @conn made with
769 * gst_rtsp_connection_create(). If @timeout is #NULL this function can block
770 * forever. If @timeout contains a valid timeout, this function will return
771 * #GST_RTSP_ETIMEOUT after the timeout expired.
773 * This function can be cancelled with gst_rtsp_connection_flush().
775 * Returns: #GST_RTSP_OK when a connection could be made.
778 gst_rtsp_connection_connect (GstRTSPConnection * conn, GTimeVal * timeout)
785 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
786 g_return_val_if_fail (conn->url != NULL, GST_RTSP_EINVAL);
787 g_return_val_if_fail (conn->fd0.fd < 0, GST_RTSP_EINVAL);
791 if (conn->proxy_host && conn->tunneled) {
792 if (!(ip = do_resolve (conn->proxy_host))) {
793 GST_ERROR ("could not resolve %s", conn->proxy_host);
796 port = conn->proxy_port;
797 g_free (conn->proxy_host);
798 conn->proxy_host = ip;
800 if (!(ip = do_resolve (url->host))) {
801 GST_ERROR ("could not resolve %s", url->host);
804 /* get the port from the url */
805 gst_rtsp_url_get_port (url, &port);
811 /* connect to the host/port */
812 res = do_connect (ip, port, &conn->fd0, conn->fdset, timeout);
813 if (res != GST_RTSP_OK)
816 /* this is our read URL */
817 conn->readfd = &conn->fd0;
819 if (conn->tunneled) {
820 res = setup_tunneling (conn, timeout);
821 if (res != GST_RTSP_OK)
822 goto tunneling_failed;
824 conn->writefd = &conn->fd0;
831 return GST_RTSP_ENET;
835 GST_ERROR ("failed to connect");
840 GST_ERROR ("failed to setup tunneling");
846 md5_digest_to_hex_string (unsigned char digest[16], char string[33])
848 static const char hexdigits[] = "0123456789abcdef";
851 for (i = 0; i < 16; i++) {
852 string[i * 2] = hexdigits[(digest[i] >> 4) & 0x0f];
853 string[i * 2 + 1] = hexdigits[digest[i] & 0x0f];
859 auth_digest_compute_hex_urp (const gchar * username,
860 const gchar * realm, const gchar * password, gchar hex_urp[33])
862 struct MD5Context md5_context;
863 unsigned char digest[16];
865 MD5Init (&md5_context);
866 MD5Update (&md5_context, username, strlen (username));
867 MD5Update (&md5_context, ":", 1);
868 MD5Update (&md5_context, realm, strlen (realm));
869 MD5Update (&md5_context, ":", 1);
870 MD5Update (&md5_context, password, strlen (password));
871 MD5Final (digest, &md5_context);
872 md5_digest_to_hex_string (digest, hex_urp);
876 auth_digest_compute_response (const gchar * method,
877 const gchar * uri, const gchar * hex_a1, const gchar * nonce,
881 struct MD5Context md5_context;
882 unsigned char digest[16];
885 MD5Init (&md5_context);
886 MD5Update (&md5_context, method, strlen (method));
887 MD5Update (&md5_context, ":", 1);
888 MD5Update (&md5_context, uri, strlen (uri));
889 MD5Final (digest, &md5_context);
890 md5_digest_to_hex_string (digest, hex_a2);
893 MD5Init (&md5_context);
894 MD5Update (&md5_context, hex_a1, strlen (hex_a1));
895 MD5Update (&md5_context, ":", 1);
896 MD5Update (&md5_context, nonce, strlen (nonce));
897 MD5Update (&md5_context, ":", 1);
899 MD5Update (&md5_context, hex_a2, 32);
900 MD5Final (digest, &md5_context);
901 md5_digest_to_hex_string (digest, response);
905 add_auth_header (GstRTSPConnection * conn, GstRTSPMessage * message)
907 switch (conn->auth_method) {
908 case GST_RTSP_AUTH_BASIC:{
913 user_pass = g_strdup_printf ("%s:%s", conn->username, conn->passwd);
914 user_pass64 = g_base64_encode ((guchar *) user_pass, strlen (user_pass));
915 auth_string = g_strdup_printf ("Basic %s", user_pass64);
917 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
921 g_free (user_pass64);
924 case GST_RTSP_AUTH_DIGEST:{
925 gchar response[33], hex_urp[33];
926 gchar *auth_string, *auth_string2;
933 /* we need to have some params set */
934 if (conn->auth_params == NULL)
937 /* we need the realm and nonce */
938 realm = (gchar *) g_hash_table_lookup (conn->auth_params, "realm");
939 nonce = (gchar *) g_hash_table_lookup (conn->auth_params, "nonce");
940 if (realm == NULL || nonce == NULL)
943 auth_digest_compute_hex_urp (conn->username, realm, conn->passwd,
946 method = gst_rtsp_method_as_text (message->type_data.request.method);
947 uri = message->type_data.request.uri;
949 /* Assume no qop, algorithm=md5, stale=false */
950 /* For algorithm MD5, a1 = urp. */
951 auth_digest_compute_response (method, uri, hex_urp, nonce, response);
952 auth_string = g_strdup_printf ("Digest username=\"%s\", "
953 "realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"",
954 conn->username, realm, nonce, uri, response);
956 opaque = (gchar *) g_hash_table_lookup (conn->auth_params, "opaque");
958 auth_string2 = g_strdup_printf ("%s, opaque=\"%s\"", auth_string,
960 g_free (auth_string);
961 auth_string = auth_string2;
963 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
974 gen_date_string (gchar * date_string, guint len)
982 g_get_current_time (&tv);
983 t = (time_t) tv.tv_sec;
986 strftime (date_string, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime_r (&t, &tm_));
988 strftime (date_string, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime (&t));
993 write_bytes (gint fd, const guint8 * buffer, guint * idx, guint size)
997 if (G_UNLIKELY (*idx > size))
998 return GST_RTSP_ERROR;
1005 r = WRITE_SOCKET (fd, &buffer[*idx], left);
1006 if (G_UNLIKELY (r == 0)) {
1007 return GST_RTSP_EINTR;
1008 } else if (G_UNLIKELY (r < 0)) {
1009 if (ERRNO_IS_EAGAIN)
1010 return GST_RTSP_EINTR;
1011 if (!ERRNO_IS_EINTR)
1012 return GST_RTSP_ESYS;
1022 fill_bytes (gint fd, guint8 * buffer, guint size, DecodeCtx * ctx)
1028 guint8 in[sizeof (ctx->out) * 4 / 3];
1031 while (size > 0 && ctx->cout < ctx->coutl) {
1032 /* we have some leftover bytes */
1033 *buffer++ = ctx->out[ctx->cout++];
1038 /* got what we needed? */
1042 /* try to read more bytes */
1043 r = READ_SOCKET (fd, in, sizeof (in));
1052 g_base64_decode_step ((gchar *) in, r, ctx->out, &ctx->state,
1056 out = READ_SOCKET (fd, buffer, size);
1062 static GstRTSPResult
1063 read_bytes (gint fd, guint8 * buffer, guint * idx, guint size, DecodeCtx * ctx)
1067 if (G_UNLIKELY (*idx > size))
1068 return GST_RTSP_ERROR;
1075 r = fill_bytes (fd, &buffer[*idx], left, ctx);
1076 if (G_UNLIKELY (r == 0)) {
1077 return GST_RTSP_EEOF;
1078 } else if (G_UNLIKELY (r < 0)) {
1079 if (ERRNO_IS_EAGAIN)
1080 return GST_RTSP_EINTR;
1081 if (!ERRNO_IS_EINTR)
1082 return GST_RTSP_ESYS;
1091 static GstRTSPResult
1092 read_line (gint fd, guint8 * buffer, guint * idx, guint size, DecodeCtx * ctx)
1098 r = fill_bytes (fd, &c, 1, ctx);
1099 if (G_UNLIKELY (r == 0)) {
1100 return GST_RTSP_EEOF;
1101 } else if (G_UNLIKELY (r < 0)) {
1102 if (ERRNO_IS_EAGAIN)
1103 return GST_RTSP_EINTR;
1104 if (!ERRNO_IS_EINTR)
1105 return GST_RTSP_ESYS;
1107 if (c == '\n') /* end on \n */
1109 if (c == '\r') /* ignore \r */
1112 if (G_LIKELY (*idx < size - 1))
1113 buffer[(*idx)++] = c;
1116 buffer[*idx] = '\0';
1122 * gst_rtsp_connection_write:
1123 * @conn: a #GstRTSPConnection
1124 * @data: the data to write
1125 * @size: the size of @data
1126 * @timeout: a timeout value or #NULL
1128 * Attempt to write @size bytes of @data to the connected @conn, blocking up to
1129 * the specified @timeout. @timeout can be #NULL, in which case this function
1130 * might block forever.
1132 * This function can be cancelled with gst_rtsp_connection_flush().
1134 * Returns: #GST_RTSP_OK on success.
1137 gst_rtsp_connection_write (GstRTSPConnection * conn, const guint8 * data,
1138 guint size, GTimeVal * timeout)
1145 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1146 g_return_val_if_fail (data != NULL || size == 0, GST_RTSP_EINVAL);
1147 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
1149 gst_poll_set_controllable (conn->fdset, TRUE);
1150 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, TRUE);
1151 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, FALSE);
1152 /* clear all previous poll results */
1153 gst_poll_fd_ignored (conn->fdset, conn->writefd);
1154 gst_poll_fd_ignored (conn->fdset, conn->readfd);
1156 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1162 res = write_bytes (conn->writefd->fd, data, &offset, size);
1163 if (G_LIKELY (res == GST_RTSP_OK))
1165 if (G_UNLIKELY (res != GST_RTSP_EINTR))
1168 /* not all is written, wait until we can write more */
1170 retval = gst_poll_wait (conn->fdset, to);
1171 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1173 if (G_UNLIKELY (retval == 0))
1176 if (G_UNLIKELY (retval == -1)) {
1188 return GST_RTSP_ETIMEOUT;
1192 return GST_RTSP_ESYS;
1196 return GST_RTSP_EINTR;
1205 message_to_string (GstRTSPConnection * conn, GstRTSPMessage * message)
1207 GString *str = NULL;
1209 str = g_string_new ("");
1211 switch (message->type) {
1212 case GST_RTSP_MESSAGE_REQUEST:
1213 /* create request string, add CSeq */
1214 g_string_append_printf (str, "%s %s RTSP/1.0\r\n"
1216 gst_rtsp_method_as_text (message->type_data.request.method),
1217 message->type_data.request.uri, conn->cseq++);
1218 /* add session id if we have one */
1219 if (conn->session_id[0] != '\0') {
1220 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SESSION,
1223 /* add any authentication headers */
1224 add_auth_header (conn, message);
1226 case GST_RTSP_MESSAGE_RESPONSE:
1227 /* create response string */
1228 g_string_append_printf (str, "RTSP/1.0 %d %s\r\n",
1229 message->type_data.response.code, message->type_data.response.reason);
1231 case GST_RTSP_MESSAGE_DATA:
1233 guint8 data_header[4];
1235 /* prepare data header */
1236 data_header[0] = '$';
1237 data_header[1] = message->type_data.data.channel;
1238 data_header[2] = (message->body_size >> 8) & 0xff;
1239 data_header[3] = message->body_size & 0xff;
1241 /* create string with header and data */
1242 str = g_string_append_len (str, (gchar *) data_header, 4);
1244 g_string_append_len (str, (gchar *) message->body,
1245 message->body_size);
1249 g_string_free (str, TRUE);
1250 g_return_val_if_reached (NULL);
1254 /* append headers and body */
1255 if (message->type != GST_RTSP_MESSAGE_DATA) {
1256 gchar date_string[100];
1258 gen_date_string (date_string, sizeof (date_string));
1260 /* add date header */
1261 gst_rtsp_message_add_header (message, GST_RTSP_HDR_DATE, date_string);
1263 /* append headers */
1264 gst_rtsp_message_append_headers (message, str);
1266 /* append Content-Length and body if needed */
1267 if (message->body != NULL && message->body_size > 0) {
1270 len = g_strdup_printf ("%d", message->body_size);
1271 g_string_append_printf (str, "%s: %s\r\n",
1272 gst_rtsp_header_as_text (GST_RTSP_HDR_CONTENT_LENGTH), len);
1274 /* header ends here */
1275 g_string_append (str, "\r\n");
1277 g_string_append_len (str, (gchar *) message->body,
1278 message->body_size);
1280 /* just end headers */
1281 g_string_append (str, "\r\n");
1289 * gst_rtsp_connection_send:
1290 * @conn: a #GstRTSPConnection
1291 * @message: the message to send
1292 * @timeout: a timeout value or #NULL
1294 * Attempt to send @message to the connected @conn, blocking up to
1295 * the specified @timeout. @timeout can be #NULL, in which case this function
1296 * might block forever.
1298 * This function can be cancelled with gst_rtsp_connection_flush().
1300 * Returns: #GST_RTSP_OK on success.
1303 gst_rtsp_connection_send (GstRTSPConnection * conn, GstRTSPMessage * message,
1306 GString *string = NULL;
1311 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1312 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1314 if (G_UNLIKELY (!(string = message_to_string (conn, message))))
1317 if (conn->tunneled) {
1318 str = g_base64_encode ((const guchar *) string->str, string->len);
1319 g_string_free (string, TRUE);
1324 g_string_free (string, FALSE);
1328 res = gst_rtsp_connection_write (conn, (guint8 *) str, len, timeout);
1336 g_warning ("Wrong message");
1337 return GST_RTSP_EINVAL;
1342 parse_string (gchar * dest, gint size, gchar ** src)
1348 while (g_ascii_isspace (**src))
1351 while (!g_ascii_isspace (**src) && **src != '\0') {
1353 dest[idx++] = **src;
1361 parse_key (gchar * dest, gint size, gchar ** src)
1366 while (**src != ':' && **src != '\0') {
1368 dest[idx++] = **src;
1375 static GstRTSPResult
1376 parse_response_status (guint8 * buffer, GstRTSPMessage * msg)
1379 gchar versionstr[20];
1384 bptr = (gchar *) buffer;
1386 parse_string (versionstr, sizeof (versionstr), &bptr);
1387 parse_string (codestr, sizeof (codestr), &bptr);
1388 code = atoi (codestr);
1390 while (g_ascii_isspace (*bptr))
1393 if (strcmp (versionstr, "RTSP/1.0") == 0)
1394 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
1396 else if (strncmp (versionstr, "RTSP/", 5) == 0) {
1397 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
1399 msg->type_data.response.version = GST_RTSP_VERSION_INVALID;
1407 return GST_RTSP_EPARSE;
1411 static GstRTSPResult
1412 parse_request_line (GstRTSPConnection * conn, guint8 * buffer,
1413 GstRTSPMessage * msg)
1415 GstRTSPResult res = GST_RTSP_OK;
1416 gchar versionstr[20];
1417 gchar methodstr[20];
1420 GstRTSPMethod method;
1421 GstRTSPTunnelState tstate = TUNNEL_STATE_NONE;
1423 bptr = (gchar *) buffer;
1425 parse_string (methodstr, sizeof (methodstr), &bptr);
1426 method = gst_rtsp_find_method (methodstr);
1427 if (method == GST_RTSP_INVALID) {
1428 /* a tunnel request is allowed when we don't have one yet */
1429 if (conn->tstate != TUNNEL_STATE_NONE)
1430 goto invalid_method;
1431 /* we need GET or POST for a valid tunnel request */
1432 if (!strcmp (methodstr, "GET"))
1433 tstate = TUNNEL_STATE_GET;
1434 else if (!strcmp (methodstr, "POST"))
1435 tstate = TUNNEL_STATE_POST;
1437 goto invalid_method;
1440 parse_string (urlstr, sizeof (urlstr), &bptr);
1441 if (G_UNLIKELY (*urlstr == '\0'))
1444 parse_string (versionstr, sizeof (versionstr), &bptr);
1446 if (G_UNLIKELY (*bptr != '\0'))
1447 goto invalid_version;
1449 if (strcmp (versionstr, "RTSP/1.0") == 0) {
1450 res = gst_rtsp_message_init_request (msg, method, urlstr);
1451 } else if (strncmp (versionstr, "RTSP/", 5) == 0) {
1452 res = gst_rtsp_message_init_request (msg, method, urlstr);
1453 msg->type_data.request.version = GST_RTSP_VERSION_INVALID;
1454 } else if (strcmp (versionstr, "HTTP/1.0") == 0) {
1455 /* tunnel request, we need a tunnel method */
1456 if (tstate == TUNNEL_STATE_NONE) {
1457 res = GST_RTSP_EPARSE;
1459 conn->tstate = tstate;
1462 res = GST_RTSP_EPARSE;
1470 GST_ERROR ("invalid method %s", methodstr);
1471 return GST_RTSP_EPARSE;
1475 GST_ERROR ("invalid url %s", urlstr);
1476 return GST_RTSP_EPARSE;
1480 GST_ERROR ("invalid version");
1481 return GST_RTSP_EPARSE;
1485 static GstRTSPResult
1486 parse_key_value (guint8 * buffer, gchar * key, guint keysize, gchar ** value)
1490 bptr = (gchar *) buffer;
1493 parse_key (key, keysize, &bptr);
1494 if (G_UNLIKELY (*bptr != ':'))
1498 while (g_ascii_isspace (*bptr))
1508 return GST_RTSP_EPARSE;
1512 /* parsing lines means reading a Key: Value pair */
1513 static GstRTSPResult
1514 parse_line (GstRTSPConnection * conn, guint8 * buffer, GstRTSPMessage * msg)
1519 GstRTSPHeaderField field;
1521 res = parse_key_value (buffer, key, sizeof (key), &value);
1522 if (G_UNLIKELY (res != GST_RTSP_OK))
1525 if (conn->tstate == TUNNEL_STATE_GET || conn->tstate == TUNNEL_STATE_POST) {
1526 /* save the tunnel session in the connection */
1527 if (!strcmp (key, "x-sessioncookie")) {
1528 strncpy (conn->tunnelid, value, TUNNELID_LEN);
1529 conn->tunnelid[TUNNELID_LEN - 1] = '\0';
1530 conn->tunneled = TRUE;
1533 field = gst_rtsp_find_header_field (key);
1534 if (field != GST_RTSP_HDR_INVALID)
1535 gst_rtsp_message_add_header (msg, field, value);
1548 * GST_RTSP_OK when a complete message was read.
1549 * GST_RTSP_EEOF: when the socket is closed
1550 * GST_RTSP_EINTR: when more data is needed.
1551 * GST_RTSP_..: some other error occured.
1553 static GstRTSPResult
1554 build_next (GstRTSPBuilder * builder, GstRTSPMessage * message,
1555 GstRTSPConnection * conn)
1560 switch (builder->state) {
1562 builder->offset = 0;
1564 read_bytes (conn->readfd->fd, (guint8 *) builder->buffer,
1565 &builder->offset, 1, conn->ctxp);
1566 if (res != GST_RTSP_OK)
1569 /* we have 1 bytes now and we can see if this is a data message or
1571 if (builder->buffer[0] == '$') {
1572 /* data message, prepare for the header */
1573 builder->state = STATE_DATA_HEADER;
1576 builder->state = STATE_READ_LINES;
1579 case STATE_DATA_HEADER:
1582 read_bytes (conn->readfd->fd, (guint8 *) builder->buffer,
1583 &builder->offset, 4, conn->ctxp);
1584 if (res != GST_RTSP_OK)
1587 gst_rtsp_message_init_data (message, builder->buffer[1]);
1589 builder->body_len = (builder->buffer[2] << 8) | builder->buffer[3];
1590 builder->body_data = g_malloc (builder->body_len + 1);
1591 builder->body_data[builder->body_len] = '\0';
1592 builder->offset = 0;
1593 builder->state = STATE_DATA_BODY;
1596 case STATE_DATA_BODY:
1599 read_bytes (conn->readfd->fd, builder->body_data, &builder->offset,
1600 builder->body_len, conn->ctxp);
1601 if (res != GST_RTSP_OK)
1604 /* we have the complete body now, store in the message adjusting the
1605 * length to include the traling '\0' */
1606 gst_rtsp_message_take_body (message,
1607 (guint8 *) builder->body_data, builder->body_len + 1);
1608 builder->body_data = NULL;
1609 builder->body_len = 0;
1611 builder->state = STATE_END;
1614 case STATE_READ_LINES:
1616 res = read_line (conn->readfd->fd, builder->buffer, &builder->offset,
1617 sizeof (builder->buffer), conn->ctxp);
1618 if (res != GST_RTSP_OK)
1621 /* we have a regular response */
1622 if (builder->buffer[0] == '\r') {
1623 builder->buffer[0] = '\0';
1626 if (builder->buffer[0] == '\0') {
1629 /* empty line, end of message header */
1630 /* see if there is a Content-Length header */
1631 if (gst_rtsp_message_get_header (message,
1632 GST_RTSP_HDR_CONTENT_LENGTH, &hdrval, 0) == GST_RTSP_OK) {
1633 /* there is, prepare to read the body */
1634 builder->body_len = atol (hdrval);
1635 builder->body_data = g_malloc (builder->body_len + 1);
1636 builder->body_data[builder->body_len] = '\0';
1637 builder->offset = 0;
1638 builder->state = STATE_DATA_BODY;
1640 builder->state = STATE_END;
1645 /* we have a line */
1646 if (builder->line == 0) {
1647 /* first line, check for response status */
1648 if (memcmp (builder->buffer, "RTSP", 4) == 0) {
1649 res = parse_response_status (builder->buffer, message);
1651 res = parse_request_line (conn, builder->buffer, message);
1653 /* the first line must parse without errors */
1654 if (res != GST_RTSP_OK)
1657 /* else just parse the line, ignore errors */
1658 parse_line (conn, builder->buffer, message);
1661 builder->offset = 0;
1668 if (conn->tstate == TUNNEL_STATE_GET) {
1669 res = GST_RTSP_ETGET;
1671 } else if (conn->tstate == TUNNEL_STATE_POST) {
1672 res = GST_RTSP_ETPOST;
1676 if (message->type == GST_RTSP_MESSAGE_DATA) {
1677 /* data messages don't have headers */
1682 /* save session id in the connection for further use */
1683 if (gst_rtsp_message_get_header (message, GST_RTSP_HDR_SESSION,
1684 &session_id, 0) == GST_RTSP_OK) {
1687 maxlen = sizeof (conn->session_id) - 1;
1688 /* the sessionid can have attributes marked with ;
1689 * Make sure we strip them */
1690 for (i = 0; session_id[i] != '\0'; i++) {
1691 if (session_id[i] == ';') {
1696 } while (g_ascii_isspace (session_id[i]));
1697 if (g_str_has_prefix (&session_id[i], "timeout=")) {
1700 /* if we parsed something valid, configure */
1701 if ((to = atoi (&session_id[i + 8])) > 0)
1708 /* make sure to not overflow */
1709 strncpy (conn->session_id, session_id, maxlen);
1710 conn->session_id[maxlen] = '\0';
1716 res = GST_RTSP_ERROR;
1725 * gst_rtsp_connection_read:
1726 * @conn: a #GstRTSPConnection
1727 * @data: the data to read
1728 * @size: the size of @data
1729 * @timeout: a timeout value or #NULL
1731 * Attempt to read @size bytes into @data from the connected @conn, blocking up to
1732 * the specified @timeout. @timeout can be #NULL, in which case this function
1733 * might block forever.
1735 * This function can be cancelled with gst_rtsp_connection_flush().
1737 * Returns: #GST_RTSP_OK on success.
1740 gst_rtsp_connection_read (GstRTSPConnection * conn, guint8 * data, guint size,
1748 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1749 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
1750 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1752 if (G_UNLIKELY (size == 0))
1757 /* configure timeout if any */
1758 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1760 gst_poll_set_controllable (conn->fdset, TRUE);
1761 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
1762 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
1765 res = read_bytes (conn->readfd->fd, data, &offset, size, conn->ctxp);
1766 if (G_UNLIKELY (res == GST_RTSP_EEOF))
1768 if (G_LIKELY (res == GST_RTSP_OK))
1770 if (G_UNLIKELY (res != GST_RTSP_EINTR))
1774 retval = gst_poll_wait (conn->fdset, to);
1775 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1777 /* check for timeout */
1778 if (G_UNLIKELY (retval == 0))
1779 goto select_timeout;
1781 if (G_UNLIKELY (retval == -1)) {
1787 gst_poll_set_controllable (conn->fdset, FALSE);
1794 return GST_RTSP_ESYS;
1798 return GST_RTSP_ETIMEOUT;
1802 return GST_RTSP_EINTR;
1806 return GST_RTSP_EEOF;
1815 gen_tunnel_reply (GstRTSPConnection * conn, GstRTSPStatusCode code)
1818 gchar date_string[100];
1819 const gchar *status;
1821 gen_date_string (date_string, sizeof (date_string));
1823 status = gst_rtsp_status_as_text (code);
1824 if (status == NULL) {
1825 code = GST_RTSP_STS_INTERNAL_SERVER_ERROR;
1826 status = "Internal Server Error";
1829 str = g_string_new ("");
1832 g_string_append_printf (str, "HTTP/1.0 %d %s\r\n", code, status);
1833 g_string_append_printf (str,
1834 "Server: GStreamer RTSP Server\r\n"
1836 "Connection: close\r\n"
1837 "Cache-Control: no-store\r\n" "Pragma: no-cache\r\n", date_string);
1838 if (code == GST_RTSP_STS_OK) {
1840 g_string_append_printf (str, "x-server-ip-address: %s\r\n", conn->ip);
1841 g_string_append_printf (str,
1842 "Content-Type: application/x-rtsp-tunnelled\r\n");
1844 g_string_append_printf (str, "\r\n");
1849 * gst_rtsp_connection_receive:
1850 * @conn: a #GstRTSPConnection
1851 * @message: the message to read
1852 * @timeout: a timeout value or #NULL
1854 * Attempt to read into @message from the connected @conn, blocking up to
1855 * the specified @timeout. @timeout can be #NULL, in which case this function
1856 * might block forever.
1858 * This function can be cancelled with gst_rtsp_connection_flush().
1860 * Returns: #GST_RTSP_OK on success.
1863 gst_rtsp_connection_receive (GstRTSPConnection * conn, GstRTSPMessage * message,
1867 GstRTSPBuilder builder;
1871 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1872 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1873 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1875 /* configure timeout if any */
1876 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1878 gst_poll_set_controllable (conn->fdset, TRUE);
1879 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
1880 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
1882 memset (&builder, 0, sizeof (GstRTSPBuilder));
1884 res = build_next (&builder, message, conn);
1885 if (G_UNLIKELY (res == GST_RTSP_EEOF))
1887 if (G_LIKELY (res == GST_RTSP_OK))
1889 if (res == GST_RTSP_ETGET) {
1892 /* tunnel GET request, we can reply now */
1893 str = gen_tunnel_reply (conn, GST_RTSP_STS_OK);
1895 gst_rtsp_connection_write (conn, (guint8 *) str->str, str->len,
1897 g_string_free (str, TRUE);
1898 } else if (res == GST_RTSP_ETPOST) {
1899 /* tunnel POST request, return the value, the caller now has to link the
1900 * two connections. */
1902 } else if (G_UNLIKELY (res != GST_RTSP_EINTR))
1906 retval = gst_poll_wait (conn->fdset, to);
1907 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1909 /* check for timeout */
1910 if (G_UNLIKELY (retval == 0))
1911 goto select_timeout;
1913 if (G_UNLIKELY (retval == -1)) {
1919 gst_poll_set_controllable (conn->fdset, FALSE);
1922 /* we have a message here */
1923 build_reset (&builder);
1930 res = GST_RTSP_ESYS;
1935 res = GST_RTSP_ETIMEOUT;
1940 res = GST_RTSP_EINTR;
1945 res = GST_RTSP_EEOF;
1951 build_reset (&builder);
1952 gst_rtsp_message_unset (message);
1958 * gst_rtsp_connection_close:
1959 * @conn: a #GstRTSPConnection
1961 * Close the connected @conn. After this call, the connection is in the same
1962 * state as when it was first created.
1964 * Returns: #GST_RTSP_OK on success.
1967 gst_rtsp_connection_close (GstRTSPConnection * conn)
1969 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1974 REMOVE_POLLFD (conn->fdset, &conn->fd0);
1975 REMOVE_POLLFD (conn->fdset, &conn->fd1);
1976 conn->writefd = NULL;
1977 conn->readfd = NULL;
1978 conn->tunneled = FALSE;
1979 conn->tstate = TUNNEL_STATE_NONE;
1981 g_free (conn->username);
1982 conn->username = NULL;
1983 g_free (conn->passwd);
1984 conn->passwd = NULL;
1985 gst_rtsp_connection_clear_auth_params (conn);
1988 conn->session_id[0] = '\0';
1994 * gst_rtsp_connection_free:
1995 * @conn: a #GstRTSPConnection
1997 * Close and free @conn.
1999 * Returns: #GST_RTSP_OK on success.
2002 gst_rtsp_connection_free (GstRTSPConnection * conn)
2006 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2008 res = gst_rtsp_connection_close (conn);
2009 gst_poll_free (conn->fdset);
2010 g_timer_destroy (conn->timer);
2011 gst_rtsp_url_free (conn->url);
2012 g_free (conn->proxy_host);
2022 * gst_rtsp_connection_poll:
2023 * @conn: a #GstRTSPConnection
2024 * @events: a bitmask of #GstRTSPEvent flags to check
2025 * @revents: location for result flags
2026 * @timeout: a timeout
2028 * Wait up to the specified @timeout for the connection to become available for
2029 * at least one of the operations specified in @events. When the function returns
2030 * with #GST_RTSP_OK, @revents will contain a bitmask of available operations on
2033 * @timeout can be #NULL, in which case this function might block forever.
2035 * This function can be cancelled with gst_rtsp_connection_flush().
2037 * Returns: #GST_RTSP_OK on success.
2042 gst_rtsp_connection_poll (GstRTSPConnection * conn, GstRTSPEvent events,
2043 GstRTSPEvent * revents, GTimeVal * timeout)
2048 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2049 g_return_val_if_fail (events != 0, GST_RTSP_EINVAL);
2050 g_return_val_if_fail (revents != NULL, GST_RTSP_EINVAL);
2051 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2052 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2054 gst_poll_set_controllable (conn->fdset, TRUE);
2056 /* add fd to writer set when asked to */
2057 gst_poll_fd_ctl_write (conn->fdset, conn->writefd,
2058 events & GST_RTSP_EV_WRITE);
2060 /* add fd to reader set when asked to */
2061 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, events & GST_RTSP_EV_READ);
2063 /* configure timeout if any */
2064 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
2067 retval = gst_poll_wait (conn->fdset, to);
2068 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
2070 if (G_UNLIKELY (retval == 0))
2071 goto select_timeout;
2073 if (G_UNLIKELY (retval == -1)) {
2081 if (events & GST_RTSP_EV_READ) {
2082 if (gst_poll_fd_can_read (conn->fdset, conn->readfd))
2083 *revents |= GST_RTSP_EV_READ;
2085 if (events & GST_RTSP_EV_WRITE) {
2086 if (gst_poll_fd_can_write (conn->fdset, conn->writefd))
2087 *revents |= GST_RTSP_EV_WRITE;
2094 return GST_RTSP_ETIMEOUT;
2098 return GST_RTSP_ESYS;
2102 return GST_RTSP_EINTR;
2107 * gst_rtsp_connection_next_timeout:
2108 * @conn: a #GstRTSPConnection
2109 * @timeout: a timeout
2111 * Calculate the next timeout for @conn, storing the result in @timeout.
2113 * Returns: #GST_RTSP_OK.
2116 gst_rtsp_connection_next_timeout (GstRTSPConnection * conn, GTimeVal * timeout)
2122 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2123 g_return_val_if_fail (timeout != NULL, GST_RTSP_EINVAL);
2125 elapsed = g_timer_elapsed (conn->timer, &usec);
2126 if (elapsed >= conn->timeout) {
2130 sec = conn->timeout - elapsed;
2133 timeout->tv_sec = sec;
2134 timeout->tv_usec = usec;
2140 * gst_rtsp_connection_reset_timeout:
2141 * @conn: a #GstRTSPConnection
2143 * Reset the timeout of @conn.
2145 * Returns: #GST_RTSP_OK.
2148 gst_rtsp_connection_reset_timeout (GstRTSPConnection * conn)
2150 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2152 g_timer_start (conn->timer);
2158 * gst_rtsp_connection_flush:
2159 * @conn: a #GstRTSPConnection
2160 * @flush: start or stop the flush
2162 * Start or stop the flushing action on @conn. When flushing, all current
2163 * and future actions on @conn will return #GST_RTSP_EINTR until the connection
2164 * is set to non-flushing mode again.
2166 * Returns: #GST_RTSP_OK.
2169 gst_rtsp_connection_flush (GstRTSPConnection * conn, gboolean flush)
2171 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2173 gst_poll_set_flushing (conn->fdset, flush);
2179 * gst_rtsp_connection_set_proxy:
2180 * @conn: a #GstRTSPConnection
2181 * @host: the proxy host
2182 * @port: the proxy port
2184 * Set the proxy host and port.
2186 * Returns: #GST_RTSP_OK.
2191 gst_rtsp_connection_set_proxy (GstRTSPConnection * conn,
2192 const gchar * host, guint port)
2194 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2196 g_free (conn->proxy_host);
2197 conn->proxy_host = g_strdup (host);
2198 conn->proxy_port = port;
2204 * gst_rtsp_connection_set_auth:
2205 * @conn: a #GstRTSPConnection
2206 * @method: authentication method
2208 * @pass: the password
2210 * Configure @conn for authentication mode @method with @user and @pass as the
2211 * user and password respectively.
2213 * Returns: #GST_RTSP_OK.
2216 gst_rtsp_connection_set_auth (GstRTSPConnection * conn,
2217 GstRTSPAuthMethod method, const gchar * user, const gchar * pass)
2219 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2221 if (method == GST_RTSP_AUTH_DIGEST && ((user == NULL || pass == NULL)
2222 || g_strrstr (user, ":") != NULL))
2223 return GST_RTSP_EINVAL;
2225 /* Make sure the username and passwd are being set for authentication */
2226 if (method == GST_RTSP_AUTH_NONE && (user == NULL || pass == NULL))
2227 return GST_RTSP_EINVAL;
2229 /* ":" chars are not allowed in usernames for basic auth */
2230 if (method == GST_RTSP_AUTH_BASIC && g_strrstr (user, ":") != NULL)
2231 return GST_RTSP_EINVAL;
2233 g_free (conn->username);
2234 g_free (conn->passwd);
2236 conn->auth_method = method;
2237 conn->username = g_strdup (user);
2238 conn->passwd = g_strdup (pass);
2245 * @key: ASCII string to hash
2247 * Hashes @key in a case-insensitive manner.
2249 * Returns: the hash code.
2252 str_case_hash (gconstpointer key)
2254 const char *p = key;
2255 guint h = g_ascii_toupper (*p);
2258 for (p += 1; *p != '\0'; p++)
2259 h = (h << 5) - h + g_ascii_toupper (*p);
2266 * @v1: an ASCII string
2267 * @v2: another ASCII string
2269 * Compares @v1 and @v2 in a case-insensitive manner
2271 * Returns: %TRUE if they are equal (modulo case)
2274 str_case_equal (gconstpointer v1, gconstpointer v2)
2276 const char *string1 = v1;
2277 const char *string2 = v2;
2279 return g_ascii_strcasecmp (string1, string2) == 0;
2283 * gst_rtsp_connection_set_auth_param:
2284 * @conn: a #GstRTSPConnection
2285 * @param: authentication directive
2288 * Setup @conn with authentication directives. This is not necesary for
2289 * methods #GST_RTSP_AUTH_NONE and #GST_RTSP_AUTH_BASIC. For
2290 * #GST_RTSP_AUTH_DIGEST, directives should be taken from the digest challenge
2291 * in the WWW-Authenticate response header and can include realm, domain,
2292 * nonce, opaque, stale, algorithm, qop as per RFC2617.
2297 gst_rtsp_connection_set_auth_param (GstRTSPConnection * conn,
2298 const gchar * param, const gchar * value)
2300 g_return_if_fail (conn != NULL);
2301 g_return_if_fail (param != NULL);
2303 if (conn->auth_params == NULL) {
2305 g_hash_table_new_full (str_case_hash, str_case_equal, g_free, g_free);
2307 g_hash_table_insert (conn->auth_params, g_strdup (param), g_strdup (value));
2311 * gst_rtsp_connection_clear_auth_params:
2312 * @conn: a #GstRTSPConnection
2314 * Clear the list of authentication directives stored in @conn.
2319 gst_rtsp_connection_clear_auth_params (GstRTSPConnection * conn)
2321 g_return_if_fail (conn != NULL);
2323 if (conn->auth_params != NULL) {
2324 g_hash_table_destroy (conn->auth_params);
2325 conn->auth_params = NULL;
2329 static GstRTSPResult
2330 set_qos_dscp (gint fd, guint qos_dscp)
2335 struct sockaddr_in6 sa_in6;
2336 struct sockaddr_storage sa_stor;
2338 socklen_t slen = sizeof (sa);
2345 if (getsockname (fd, &sa.sa, &slen) < 0)
2346 goto no_getsockname;
2348 af = sa.sa.sa_family;
2350 /* if this is an IPv4-mapped address then do IPv4 QoS */
2351 if (af == AF_INET6) {
2352 if (IN6_IS_ADDR_V4MAPPED (&sa.sa_in6.sin6_addr))
2356 /* extract and shift 6 bits of the DSCP */
2357 tos = (qos_dscp & 0x3f) << 2;
2361 if (SETSOCKOPT (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0)
2366 if (SETSOCKOPT (fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof (tos)) < 0)
2380 return GST_RTSP_ESYS;
2385 return GST_RTSP_ERROR;
2390 * gst_rtsp_connection_set_qos_dscp:
2391 * @conn: a #GstRTSPConnection
2392 * @qos_dscp: DSCP value
2394 * Configure @conn to use the specified DSCP value.
2396 * Returns: #GST_RTSP_OK on success.
2401 gst_rtsp_connection_set_qos_dscp (GstRTSPConnection * conn, guint qos_dscp)
2405 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2406 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2407 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2409 res = set_qos_dscp (conn->fd0.fd, qos_dscp);
2410 if (res == GST_RTSP_OK)
2411 res = set_qos_dscp (conn->fd1.fd, qos_dscp);
2418 * gst_rtsp_connection_get_url:
2419 * @conn: a #GstRTSPConnection
2421 * Retrieve the URL of the other end of @conn.
2423 * Returns: The URL. This value remains valid until the
2424 * connection is freed.
2429 gst_rtsp_connection_get_url (const GstRTSPConnection * conn)
2431 g_return_val_if_fail (conn != NULL, NULL);
2437 * gst_rtsp_connection_get_ip:
2438 * @conn: a #GstRTSPConnection
2440 * Retrieve the IP address of the other end of @conn.
2442 * Returns: The IP address as a string. this value remains valid until the
2443 * connection is closed.
2448 gst_rtsp_connection_get_ip (const GstRTSPConnection * conn)
2450 g_return_val_if_fail (conn != NULL, NULL);
2456 * gst_rtsp_connection_set_ip:
2457 * @conn: a #GstRTSPConnection
2458 * @ip: an ip address
2460 * Set the IP address of the server.
2465 gst_rtsp_connection_set_ip (GstRTSPConnection * conn, const gchar * ip)
2467 g_return_if_fail (conn != NULL);
2470 conn->ip = g_strdup (ip);
2474 * gst_rtsp_connection_get_readfd:
2475 * @conn: a #GstRTSPConnection
2477 * Get the file descriptor for reading.
2479 * Returns: the file descriptor used for reading or -1 on error. The file
2480 * descriptor remains valid until the connection is closed.
2485 gst_rtsp_connection_get_readfd (const GstRTSPConnection * conn)
2487 g_return_val_if_fail (conn != NULL, -1);
2488 g_return_val_if_fail (conn->readfd != NULL, -1);
2490 return conn->readfd->fd;
2494 * gst_rtsp_connection_get_writefd:
2495 * @conn: a #GstRTSPConnection
2497 * Get the file descriptor for writing.
2499 * Returns: the file descriptor used for writing or -1 on error. The file
2500 * descriptor remains valid until the connection is closed.
2505 gst_rtsp_connection_get_writefd (const GstRTSPConnection * conn)
2507 g_return_val_if_fail (conn != NULL, -1);
2508 g_return_val_if_fail (conn->writefd != NULL, -1);
2510 return conn->writefd->fd;
2515 * gst_rtsp_connection_set_tunneled:
2516 * @conn: a #GstRTSPConnection
2517 * @tunneled: the new state
2519 * Set the HTTP tunneling state of the connection. This must be configured before
2520 * the @conn is connected.
2525 gst_rtsp_connection_set_tunneled (GstRTSPConnection * conn, gboolean tunneled)
2527 g_return_if_fail (conn != NULL);
2528 g_return_if_fail (conn->readfd == NULL);
2529 g_return_if_fail (conn->writefd == NULL);
2531 conn->tunneled = tunneled;
2535 * gst_rtsp_connection_is_tunneled:
2536 * @conn: a #GstRTSPConnection
2538 * Get the tunneling state of the connection.
2540 * Returns: if @conn is using HTTP tunneling.
2545 gst_rtsp_connection_is_tunneled (const GstRTSPConnection * conn)
2547 g_return_val_if_fail (conn != NULL, FALSE);
2549 return conn->tunneled;
2553 * gst_rtsp_connection_get_tunnelid:
2554 * @conn: a #GstRTSPConnection
2556 * Get the tunnel session id the connection.
2558 * Returns: returns a non-empty string if @conn is being tunneled over HTTP.
2563 gst_rtsp_connection_get_tunnelid (const GstRTSPConnection * conn)
2565 g_return_val_if_fail (conn != NULL, NULL);
2567 if (!conn->tunneled)
2570 return conn->tunnelid;
2574 * gst_rtsp_connection_do_tunnel:
2575 * @conn: a #GstRTSPConnection
2576 * @conn2: a #GstRTSPConnection
2578 * If @conn received the first tunnel connection and @conn2 received
2579 * the second tunnel connection, link the two connections together so that
2580 * @conn manages the tunneled connection.
2582 * After this call, @conn2 cannot be used anymore and must be freed with
2583 * gst_rtsp_connection_free().
2585 * Returns: return GST_RTSP_OK on success.
2590 gst_rtsp_connection_do_tunnel (GstRTSPConnection * conn,
2591 GstRTSPConnection * conn2)
2593 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2594 g_return_val_if_fail (conn2 != NULL, GST_RTSP_EINVAL);
2595 g_return_val_if_fail (conn->tstate == TUNNEL_STATE_GET, GST_RTSP_EINVAL);
2596 g_return_val_if_fail (conn2->tstate == TUNNEL_STATE_POST, GST_RTSP_EINVAL);
2597 g_return_val_if_fail (!memcmp (conn2->tunnelid, conn->tunnelid, TUNNELID_LEN),
2600 /* both connections have fd0 as the read/write socket. start by taking the
2601 * socket from conn2 and set it as the socket in conn */
2602 conn->fd1 = conn2->fd0;
2604 /* clean up some of the state of conn2 */
2605 gst_poll_remove_fd (conn2->fdset, &conn2->fd0);
2607 conn2->readfd = conn2->writefd = NULL;
2609 /* We make fd0 the write socket and fd1 the read socket. */
2610 conn->writefd = &conn->fd0;
2611 conn->readfd = &conn->fd1;
2613 conn->tstate = TUNNEL_STATE_COMPLETE;
2615 /* we need base64 decoding for the readfd */
2616 conn->ctx.state = 0;
2619 conn->ctx.coutl = 0;
2620 conn->ctxp = &conn->ctx;
2625 #define READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
2626 #define WRITE_COND (G_IO_OUT | G_IO_ERR)
2634 static guint queue_response (GstRTSPWatch * watch, GString * str);
2636 /* async functions */
2637 struct _GstRTSPWatch
2641 GstRTSPConnection *conn;
2643 GstRTSPBuilder builder;
2644 GstRTSPMessage message;
2648 gboolean write_added;
2650 /* queued message for transmission */
2652 GAsyncQueue *messages;
2658 GstRTSPWatchFuncs funcs;
2661 GDestroyNotify notify;
2665 gst_rtsp_source_prepare (GSource * source, gint * timeout)
2667 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2669 *timeout = (watch->conn->timeout * 1000);
2675 gst_rtsp_source_check (GSource * source)
2677 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2679 if (watch->readfd.revents & READ_COND)
2682 if (watch->writefd.revents & WRITE_COND)
2689 gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback G_GNUC_UNUSED,
2690 gpointer user_data G_GNUC_UNUSED)
2692 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2695 /* first read as much as we can */
2696 if (watch->readfd.revents & READ_COND) {
2698 res = build_next (&watch->builder, &watch->message, watch->conn);
2699 if (res == GST_RTSP_EINTR)
2701 if (G_UNLIKELY (res == GST_RTSP_EEOF))
2703 if (res == GST_RTSP_ETGET) {
2705 GstRTSPStatusCode code;
2707 if (watch->funcs.tunnel_start)
2708 code = watch->funcs.tunnel_start (watch, watch->user_data);
2710 code = GST_RTSP_STS_OK;
2712 /* queue the response string */
2713 str = gen_tunnel_reply (watch->conn, code);
2714 queue_response (watch, str);
2715 } else if (res == GST_RTSP_ETPOST) {
2716 /* in the callback the connection should be tunneled with the
2718 if (watch->funcs.tunnel_complete)
2719 watch->funcs.tunnel_complete (watch, watch->user_data);
2720 } else if (G_UNLIKELY (res != GST_RTSP_OK))
2723 if (G_LIKELY (res == GST_RTSP_OK)) {
2724 if (watch->funcs.message_received)
2725 watch->funcs.message_received (watch, &watch->message,
2728 gst_rtsp_message_unset (&watch->message);
2730 build_reset (&watch->builder);
2734 if (watch->writefd.revents & WRITE_COND) {
2736 if (watch->write_data == NULL) {
2739 /* get a new message from the queue */
2740 rec = g_async_queue_try_pop (watch->messages);
2744 watch->write_off = 0;
2745 watch->write_len = rec->str->len;
2746 watch->write_data = (guint8 *) g_string_free (rec->str, FALSE);
2747 watch->write_id = rec->id;
2750 g_slice_free (GstRTSPRec, rec);
2753 res = write_bytes (watch->writefd.fd, watch->write_data,
2754 &watch->write_off, watch->write_len);
2755 if (res == GST_RTSP_EINTR)
2757 if (G_UNLIKELY (res != GST_RTSP_OK))
2760 if (watch->funcs.message_sent)
2761 watch->funcs.message_sent (watch, watch->write_id, watch->user_data);
2764 if (g_async_queue_length (watch->messages) == 0 && watch->write_added) {
2765 g_source_remove_poll ((GSource *) watch, &watch->writefd);
2766 watch->write_added = FALSE;
2767 watch->writefd.revents = 0;
2769 g_free (watch->write_data);
2770 watch->write_data = NULL;
2779 if (watch->funcs.closed)
2780 watch->funcs.closed (watch, watch->user_data);
2785 if (watch->funcs.error)
2786 watch->funcs.error (watch, res, watch->user_data);
2792 gst_rtsp_rec_free (gpointer data)
2794 GstRTSPRec *rec = data;
2796 g_string_free (rec->str, TRUE);
2798 g_slice_free (GstRTSPRec, rec);
2802 gst_rtsp_source_finalize (GSource * source)
2804 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2806 build_reset (&watch->builder);
2807 gst_rtsp_message_unset (&watch->message);
2809 g_async_queue_unref (watch->messages);
2810 watch->messages = NULL;
2812 g_free (watch->write_data);
2815 watch->notify (watch->user_data);
2818 static GSourceFuncs gst_rtsp_source_funcs = {
2819 gst_rtsp_source_prepare,
2820 gst_rtsp_source_check,
2821 gst_rtsp_source_dispatch,
2822 gst_rtsp_source_finalize,
2828 * gst_rtsp_watch_new:
2829 * @conn: a #GstRTSPConnection
2830 * @funcs: watch functions
2831 * @user_data: user data to pass to @funcs
2832 * @notify: notify when @user_data is not referenced anymore
2834 * Create a watch object for @conn. The functions provided in @funcs will be
2835 * called with @user_data when activity happened on the watch.
2837 * The new watch is usually created so that it can be attached to a
2838 * maincontext with gst_rtsp_watch_attach().
2840 * @conn must exist for the entire lifetime of the watch.
2842 * Returns: a #GstRTSPWatch that can be used for asynchronous RTSP
2843 * communication. Free with gst_rtsp_watch_unref () after usage.
2848 gst_rtsp_watch_new (GstRTSPConnection * conn,
2849 GstRTSPWatchFuncs * funcs, gpointer user_data, GDestroyNotify notify)
2851 GstRTSPWatch *result;
2853 g_return_val_if_fail (conn != NULL, NULL);
2854 g_return_val_if_fail (funcs != NULL, NULL);
2855 g_return_val_if_fail (conn->readfd != NULL, NULL);
2856 g_return_val_if_fail (conn->writefd != NULL, NULL);
2858 result = (GstRTSPWatch *) g_source_new (&gst_rtsp_source_funcs,
2859 sizeof (GstRTSPWatch));
2861 result->conn = conn;
2862 result->builder.state = STATE_START;
2864 result->messages = g_async_queue_new_full (gst_rtsp_rec_free);
2866 result->readfd.fd = -1;
2867 result->writefd.fd = -1;
2869 gst_rtsp_watch_reset (result);
2871 result->funcs = *funcs;
2872 result->user_data = user_data;
2873 result->notify = notify;
2875 /* only add the read fd, the write fd is only added when we have data
2877 g_source_add_poll ((GSource *) result, &result->readfd);
2883 * gst_rtsp_watch_reset:
2884 * @watch: a #GstRTSPWatch
2886 * Reset @watch, this is usually called after gst_rtsp_connection_do_tunnel()
2887 * when the file descriptors of the connection might have changed.
2892 gst_rtsp_watch_reset (GstRTSPWatch * watch)
2894 if (watch->readfd.fd != -1)
2895 g_source_remove_poll ((GSource *) watch, &watch->readfd);
2896 if (watch->writefd.fd != -1)
2897 g_source_remove_poll ((GSource *) watch, &watch->writefd);
2899 watch->readfd.fd = watch->conn->readfd->fd;
2900 watch->readfd.events = READ_COND;
2901 watch->readfd.revents = 0;
2903 watch->writefd.fd = watch->conn->writefd->fd;
2904 watch->writefd.events = WRITE_COND;
2905 watch->writefd.revents = 0;
2906 watch->write_added = FALSE;
2908 g_source_add_poll ((GSource *) watch, &watch->readfd);
2912 * gst_rtsp_watch_attach:
2913 * @watch: a #GstRTSPWatch
2914 * @context: a GMainContext (if NULL, the default context will be used)
2916 * Adds a #GstRTSPWatch to a context so that it will be executed within that context.
2918 * Returns: the ID (greater than 0) for the watch within the GMainContext.
2923 gst_rtsp_watch_attach (GstRTSPWatch * watch, GMainContext * context)
2925 g_return_val_if_fail (watch != NULL, 0);
2927 return g_source_attach ((GSource *) watch, context);
2931 * gst_rtsp_watch_unref:
2932 * @watch: a #GstRTSPWatch
2934 * Decreases the reference count of @watch by one. If the resulting reference
2935 * count is zero the watch and associated memory will be destroyed.
2940 gst_rtsp_watch_unref (GstRTSPWatch * watch)
2942 g_return_if_fail (watch != NULL);
2944 g_source_unref ((GSource *) watch);
2948 queue_response (GstRTSPWatch * watch, GString * str)
2952 /* make a record with the message as a string and id */
2953 rec = g_slice_new (GstRTSPRec);
2955 rec->id = ++watch->id;
2957 /* add the record to a queue. FIXME we would like to have an upper limit here */
2958 g_async_queue_push (watch->messages, rec);
2960 /* FIXME: does the following need to be made thread-safe? (this might be
2961 * called from a streaming thread, like appsink's render function) */
2962 /* make sure the main context will now also check for writability on the
2964 if (!watch->write_added) {
2965 g_source_add_poll ((GSource *) watch, &watch->writefd);
2966 watch->write_added = TRUE;
2973 * gst_rtsp_watch_queue_message:
2974 * @watch: a #GstRTSPWatch
2975 * @message: a #GstRTSPMessage
2977 * Queue a @message for transmission in @watch. The contents of this
2978 * message will be serialized and transmitted when the connection of the
2979 * @watch becomes writable.
2981 * The return value of this function will be used as the id argument in the
2982 * message_sent callback.
2989 gst_rtsp_watch_queue_message (GstRTSPWatch * watch, GstRTSPMessage * message)
2991 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
2992 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2994 /* make a record with the message as a string and id */
2995 return queue_response (watch, message_to_string (watch->conn, message));