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"
109 static GstRTSPResult read_line (gint fd, guint8 * buffer, guint * idx,
110 guint size, DecodeCtx * ctxp);
111 static GstRTSPResult parse_key_value (guint8 * buffer, gchar * key,
112 guint keysize, gchar ** value);
113 static void parse_string (gchar * dest, gint size, gchar ** src);
116 #define READ_SOCKET(fd, buf, len) recv (fd, (char *)buf, len, 0)
117 #define WRITE_SOCKET(fd, buf, len) send (fd, (const char *)buf, len, 0)
118 #define SETSOCKOPT(sock, level, name, val, len) setsockopt (sock, level, name, (const char *)val, len)
119 #define CLOSE_SOCKET(sock) closesocket (sock)
120 #define ERRNO_IS_EAGAIN (WSAGetLastError () == WSAEWOULDBLOCK)
121 #define ERRNO_IS_EINTR (WSAGetLastError () == WSAEINTR)
122 /* According to Microsoft's connect() documentation this one returns
123 * WSAEWOULDBLOCK and not WSAEINPROGRESS. */
124 #define ERRNO_IS_EINPROGRESS (WSAGetLastError () == WSAEWOULDBLOCK)
126 #define READ_SOCKET(fd, buf, len) read (fd, buf, len)
127 #define WRITE_SOCKET(fd, buf, len) write (fd, buf, len)
128 #define SETSOCKOPT(sock, level, name, val, len) setsockopt (sock, level, name, val, len)
129 #define CLOSE_SOCKET(sock) close (sock)
130 #define ERRNO_IS_EAGAIN (errno == EAGAIN)
131 #define ERRNO_IS_EINTR (errno == EINTR)
132 #define ERRNO_IS_EINPROGRESS (errno == EINPROGRESS)
135 #define ADD_POLLFD(fdset, pfd, fd) \
138 gst_poll_add_fd (fdset, pfd); \
141 #define REMOVE_POLLFD(fdset, pfd) \
143 if ((pfd)->fd != -1) { \
144 GST_DEBUG ("remove fd %d", (pfd)->fd); \
145 gst_poll_remove_fd (fdset, pfd); \
146 CLOSE_SOCKET ((pfd)->fd); \
156 TUNNEL_STATE_COMPLETE
157 } GstRTSPTunnelState;
159 #define TUNNELID_LEN 24
161 #define UNKNOWN_CSEQ ((guint) -1)
163 struct _GstRTSPConnection
166 /* URL for the connection */
169 /* connection state */
176 gchar tunnelid[TUNNELID_LEN];
178 GstRTSPTunnelState tstate;
184 gint cseq; /* sequence number */
185 gchar session_id[512]; /* session id */
186 gint timeout; /* session timeout in seconds */
187 GTimer *timer; /* timeout timer */
190 GstRTSPAuthMethod auth_method;
193 GHashTable *auth_params;
204 inet_aton (const char *c, struct in_addr *paddr)
206 /* note that inet_addr is deprecated on unix because
207 * inet_addr returns -1 (INADDR_NONE) for the valid 255.255.255.255
209 paddr->s_addr = inet_addr (c);
211 if (paddr->s_addr == INADDR_NONE)
228 /* a structure for constructing RTSPMessages */
241 build_reset (GstRTSPBuilder * builder)
243 g_free (builder->body_data);
244 memset (builder, 0, sizeof (GstRTSPBuilder));
248 * gst_rtsp_connection_create:
249 * @url: a #GstRTSPUrl
250 * @conn: storage for a #GstRTSPConnection
252 * Create a newly allocated #GstRTSPConnection from @url and store it in @conn.
253 * The connection will not yet attempt to connect to @url, use
254 * gst_rtsp_connection_connect().
256 * A copy of @url will be made.
258 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
261 gst_rtsp_connection_create (const GstRTSPUrl * url, GstRTSPConnection ** conn)
263 GstRTSPConnection *newconn;
269 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
272 error = WSAStartup (0x0202, &w);
277 if (w.wVersion != 0x0202)
281 newconn = g_new0 (GstRTSPConnection, 1);
283 if ((newconn->fdset = gst_poll_new (TRUE)) == NULL)
286 newconn->url = gst_rtsp_url_copy (url);
287 newconn->fd0.fd = -1;
288 newconn->fd1.fd = -1;
289 newconn->timer = g_timer_new ();
290 newconn->timeout = 60;
293 newconn->auth_method = GST_RTSP_AUTH_NONE;
294 newconn->username = NULL;
295 newconn->passwd = NULL;
296 newconn->auth_params = NULL;
306 g_warning ("Error %d on WSAStartup", error);
307 return GST_RTSP_EWSASTART;
311 g_warning ("Windows sockets are not version 0x202 (current 0x%x)",
314 return GST_RTSP_EWSAVERSION;
323 return GST_RTSP_ESYS;
328 * gst_rtsp_connection_accept:
330 * @conn: storage for a #GstRTSPConnection
332 * Accept a new connection on @sock and create a new #GstRTSPConnection for
333 * handling communication on new socket.
335 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
340 gst_rtsp_connection_accept (gint sock, GstRTSPConnection ** conn)
343 unsigned int address_len;
344 GstRTSPConnection *newconn = NULL;
345 struct sockaddr_in address;
351 address_len = sizeof (address);
352 memset (&address, 0, address_len);
355 fd = accept (sock, (struct sockaddr *) &address, &address_len);
357 fd = accept (sock, (struct sockaddr *) &address, (gint *) & address_len);
358 #endif /* G_OS_WIN32 */
362 /* set to non-blocking mode so that we can cancel the communication */
364 fcntl (fd, F_SETFL, O_NONBLOCK);
366 ioctlsocket (fd, FIONBIO, &flags);
367 #endif /* G_OS_WIN32 */
369 /* create a url for the client address */
370 url = g_new0 (GstRTSPUrl, 1);
371 url->host = g_strdup_printf ("%s", inet_ntoa (address.sin_addr));
372 url->port = address.sin_port;
374 /* now create the connection object */
375 gst_rtsp_connection_create (url, &newconn);
376 gst_rtsp_url_free (url);
378 ADD_POLLFD (newconn->fdset, &newconn->fd0, fd);
380 /* both read and write initially */
381 newconn->readfd = &newconn->fd0;
382 newconn->writefd = &newconn->fd0;
391 return GST_RTSP_ESYS;
396 do_resolve (const gchar * host)
398 struct hostent *hostinfo;
402 struct in_addr *addrp;
405 gchar ipbuf[INET_ADDRSTRLEN];
406 #endif /* G_OS_WIN32 */
410 /* first check if it already is an IP address */
411 if (inet_aton (host, &addr)) {
414 hostinfo = gethostbyname (host);
416 goto not_resolved; /* h_errno set */
418 if (hostinfo->h_addrtype != AF_INET)
419 goto not_ip; /* host not an IP host */
421 addrp = (struct in_addr *) hostinfo->h_addr_list[0];
422 /* this is not threadsafe */
423 ip = inet_ntoa (*addrp);
425 addrs = hostinfo->h_addr_list;
426 ip = inet_ntop (AF_INET, (struct in_addr *) addrs[0], ipbuf,
428 #endif /* G_OS_WIN32 */
430 return g_strdup (ip);
435 GST_ERROR ("could not resolve %s", host);
440 GST_ERROR ("not an IP address");
446 do_connect (const gchar * ip, guint16 port, GstPollFD * fdout,
447 GstPoll * fdset, GTimeVal * timeout)
450 struct sockaddr_in sa_in;
453 unsigned long flags = 1;
454 #endif /* G_OS_WIN32 */
458 memset (&sa_in, 0, sizeof (sa_in));
459 sa_in.sin_family = AF_INET; /* network socket */
460 sa_in.sin_port = htons (port); /* on port */
461 sa_in.sin_addr.s_addr = inet_addr (ip); /* on host ip */
463 fd = socket (AF_INET, SOCK_STREAM, 0);
467 /* set to non-blocking mode so that we can cancel the connect */
469 fcntl (fd, F_SETFL, O_NONBLOCK);
471 ioctlsocket (fd, FIONBIO, &flags);
472 #endif /* G_OS_WIN32 */
474 /* add the socket to our fdset */
475 ADD_POLLFD (fdset, fdout, fd);
477 /* we are going to connect ASYNC now */
478 ret = connect (fd, (struct sockaddr *) &sa_in, sizeof (sa_in));
481 if (!ERRNO_IS_EINPROGRESS)
484 /* wait for connect to complete up to the specified timeout or until we got
486 gst_poll_fd_ctl_write (fdset, fdout, TRUE);
488 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
491 retval = gst_poll_wait (fdset, to);
492 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
496 else if (retval == -1)
499 /* we can still have an error connecting on windows */
500 if (gst_poll_fd_has_error (fdset, fdout)) {
501 socklen_t len = sizeof (errno);
503 getsockopt (fd, SOL_SOCKET, SO_ERROR, &errno, &len);
505 getsockopt (fd, SOL_SOCKET, SO_ERROR, (char *) &errno, &len);
510 gst_poll_fd_ignored (fdset, fdout);
518 GST_ERROR ("no socket %d (%s)", errno, g_strerror (errno));
519 return GST_RTSP_ESYS;
523 GST_ERROR ("system error %d (%s)", errno, g_strerror (errno));
524 REMOVE_POLLFD (fdset, fdout);
525 return GST_RTSP_ESYS;
529 GST_ERROR ("timeout");
530 REMOVE_POLLFD (fdset, fdout);
531 return GST_RTSP_ETIMEOUT;
536 setup_tunneling (GstRTSPConnection * conn, GTimeVal * timeout)
544 gchar *ip, *url_port_str;
545 guint16 port, url_port;
546 gchar codestr[4], *resultstr;
551 /* create a random sessionid */
552 for (i = 0; i < TUNNELID_LEN; i++)
553 conn->tunnelid[i] = g_random_int_range ('a', 'z');
554 conn->tunnelid[TUNNELID_LEN - 1] = '\0';
557 /* get the port from the url */
558 gst_rtsp_url_get_port (url, &url_port);
560 if (conn->proxy_host) {
561 hostparam = g_strdup_printf ("Host: %s:%d\r\n", url->host, url_port);
562 url_port_str = g_strdup_printf (":%d", url_port);
563 ip = conn->proxy_host;
564 port = conn->proxy_port;
573 str = g_strdup_printf ("GET %s%s%s%s%s%s HTTP/1.0\r\n"
575 "x-sessioncookie: %s\r\n"
576 "Accept: application/x-rtsp-tunnelled\r\n"
577 "Pragma: no-cache\r\n"
578 "Cache-Control: no-cache\r\n" "\r\n",
579 conn->proxy_host ? "http://" : "",
580 conn->proxy_host ? url->host : "",
581 conn->proxy_host ? url_port_str : "",
582 url->abspath, url->query ? "?" : "", url->query ? url->query : "",
583 hostparam ? hostparam : "", conn->tunnelid);
585 /* we start by writing to this fd */
586 conn->writefd = &conn->fd0;
588 res = gst_rtsp_connection_write (conn, (guint8 *) str, strlen (str), timeout);
590 if (res != GST_RTSP_OK)
593 gst_poll_fd_ctl_write (conn->fdset, &conn->fd0, FALSE);
594 gst_poll_fd_ctl_read (conn->fdset, &conn->fd0, TRUE);
596 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
604 res = read_line (conn->fd0.fd, buffer, &idx, sizeof (buffer), NULL);
605 if (res == GST_RTSP_EEOF)
607 if (res == GST_RTSP_OK)
609 if (res != GST_RTSP_EINTR)
613 retval = gst_poll_wait (conn->fdset, to);
614 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
616 /* check for timeout */
628 /* check for last line */
629 if (buffer[0] == '\r')
631 if (buffer[0] == '\0')
635 /* first line, parse response */
636 gchar versionstr[20];
639 bptr = (gchar *) buffer;
641 parse_string (versionstr, sizeof (versionstr), &bptr);
642 parse_string (codestr, sizeof (codestr), &bptr);
643 code = atoi (codestr);
645 while (g_ascii_isspace (*bptr))
650 if (code != GST_RTSP_STS_OK)
656 /* other lines, parse key/value */
657 res = parse_key_value (buffer, key, sizeof (key), &value);
658 if (res == GST_RTSP_OK) {
659 /* we got a new ip address */
660 if (g_ascii_strcasecmp (key, "x-server-ip-address") == 0) {
661 if (conn->proxy_host) {
662 /* if we use a proxy we need to change the destination url */
664 url->host = g_strdup (value);
666 g_free (url_port_str);
668 g_strdup_printf ("Host: %s:%d\r\n", url->host, url_port);
669 url_port_str = g_strdup_printf (":%d", url_port);
671 /* and resolve the new ip address */
672 if (!(ip = do_resolve (conn->ip)))
683 /* connect to the host/port */
684 res = do_connect (ip, port, &conn->fd1, conn->fdset, timeout);
685 if (res != GST_RTSP_OK)
688 /* this is now our writing socket */
689 conn->writefd = &conn->fd1;
692 str = g_strdup_printf ("POST %s%s%s%s%s%s HTTP/1.0\r\n"
694 "x-sessioncookie: %s\r\n"
695 "Content-Type: application/x-rtsp-tunnelled\r\n"
696 "Pragma: no-cache\r\n"
697 "Cache-Control: no-cache\r\n"
698 "Content-Length: 32767\r\n"
699 "Expires: Sun, 9 Jan 1972 00:00:00 GMT\r\n"
701 conn->proxy_host ? "http://" : "",
702 conn->proxy_host ? url->host : "",
703 conn->proxy_host ? url_port_str : "",
704 url->abspath, url->query ? "?" : "", url->query ? url->query : "",
705 hostparam ? hostparam : "", conn->tunnelid);
707 /* we start by writing to this fd */
708 conn->writefd = &conn->fd1;
710 res = gst_rtsp_connection_write (conn, (guint8 *) str, strlen (str), timeout);
712 if (res != GST_RTSP_OK)
717 g_free (url_port_str);
724 GST_ERROR ("write failed (%d)", res);
738 res = GST_RTSP_ETIMEOUT;
748 res = GST_RTSP_EINTR;
753 GST_ERROR ("got failure response %d %s", code, resultstr);
754 res = GST_RTSP_ERROR;
759 GST_ERROR ("could not resolve %s", conn->ip);
765 GST_ERROR ("failed to connect");
771 * gst_rtsp_connection_connect:
772 * @conn: a #GstRTSPConnection
773 * @timeout: a #GTimeVal timeout
775 * Attempt to connect to the url of @conn made with
776 * gst_rtsp_connection_create(). If @timeout is #NULL this function can block
777 * forever. If @timeout contains a valid timeout, this function will return
778 * #GST_RTSP_ETIMEOUT after the timeout expired.
780 * This function can be cancelled with gst_rtsp_connection_flush().
782 * Returns: #GST_RTSP_OK when a connection could be made.
785 gst_rtsp_connection_connect (GstRTSPConnection * conn, GTimeVal * timeout)
792 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
793 g_return_val_if_fail (conn->url != NULL, GST_RTSP_EINVAL);
794 g_return_val_if_fail (conn->fd0.fd < 0, GST_RTSP_EINVAL);
798 if (conn->proxy_host && conn->tunneled) {
799 if (!(ip = do_resolve (conn->proxy_host))) {
800 GST_ERROR ("could not resolve %s", conn->proxy_host);
803 port = conn->proxy_port;
804 g_free (conn->proxy_host);
805 conn->proxy_host = ip;
807 if (!(ip = do_resolve (url->host))) {
808 GST_ERROR ("could not resolve %s", url->host);
811 /* get the port from the url */
812 gst_rtsp_url_get_port (url, &port);
818 /* connect to the host/port */
819 res = do_connect (ip, port, &conn->fd0, conn->fdset, timeout);
820 if (res != GST_RTSP_OK)
823 /* this is our read URL */
824 conn->readfd = &conn->fd0;
826 if (conn->tunneled) {
827 res = setup_tunneling (conn, timeout);
828 if (res != GST_RTSP_OK)
829 goto tunneling_failed;
831 conn->writefd = &conn->fd0;
838 return GST_RTSP_ENET;
842 GST_ERROR ("failed to connect");
847 GST_ERROR ("failed to setup tunneling");
853 md5_digest_to_hex_string (unsigned char digest[16], char string[33])
855 static const char hexdigits[] = "0123456789abcdef";
858 for (i = 0; i < 16; i++) {
859 string[i * 2] = hexdigits[(digest[i] >> 4) & 0x0f];
860 string[i * 2 + 1] = hexdigits[digest[i] & 0x0f];
866 auth_digest_compute_hex_urp (const gchar * username,
867 const gchar * realm, const gchar * password, gchar hex_urp[33])
869 struct MD5Context md5_context;
870 unsigned char digest[16];
872 MD5Init (&md5_context);
873 MD5Update (&md5_context, username, strlen (username));
874 MD5Update (&md5_context, ":", 1);
875 MD5Update (&md5_context, realm, strlen (realm));
876 MD5Update (&md5_context, ":", 1);
877 MD5Update (&md5_context, password, strlen (password));
878 MD5Final (digest, &md5_context);
879 md5_digest_to_hex_string (digest, hex_urp);
883 auth_digest_compute_response (const gchar * method,
884 const gchar * uri, const gchar * hex_a1, const gchar * nonce,
888 struct MD5Context md5_context;
889 unsigned char digest[16];
892 MD5Init (&md5_context);
893 MD5Update (&md5_context, method, strlen (method));
894 MD5Update (&md5_context, ":", 1);
895 MD5Update (&md5_context, uri, strlen (uri));
896 MD5Final (digest, &md5_context);
897 md5_digest_to_hex_string (digest, hex_a2);
900 MD5Init (&md5_context);
901 MD5Update (&md5_context, hex_a1, strlen (hex_a1));
902 MD5Update (&md5_context, ":", 1);
903 MD5Update (&md5_context, nonce, strlen (nonce));
904 MD5Update (&md5_context, ":", 1);
906 MD5Update (&md5_context, hex_a2, 32);
907 MD5Final (digest, &md5_context);
908 md5_digest_to_hex_string (digest, response);
912 add_auth_header (GstRTSPConnection * conn, GstRTSPMessage * message)
914 switch (conn->auth_method) {
915 case GST_RTSP_AUTH_BASIC:{
920 user_pass = g_strdup_printf ("%s:%s", conn->username, conn->passwd);
921 user_pass64 = g_base64_encode ((guchar *) user_pass, strlen (user_pass));
922 auth_string = g_strdup_printf ("Basic %s", user_pass64);
924 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
928 g_free (user_pass64);
931 case GST_RTSP_AUTH_DIGEST:{
932 gchar response[33], hex_urp[33];
933 gchar *auth_string, *auth_string2;
940 /* we need to have some params set */
941 if (conn->auth_params == NULL)
944 /* we need the realm and nonce */
945 realm = (gchar *) g_hash_table_lookup (conn->auth_params, "realm");
946 nonce = (gchar *) g_hash_table_lookup (conn->auth_params, "nonce");
947 if (realm == NULL || nonce == NULL)
950 auth_digest_compute_hex_urp (conn->username, realm, conn->passwd,
953 method = gst_rtsp_method_as_text (message->type_data.request.method);
954 uri = message->type_data.request.uri;
956 /* Assume no qop, algorithm=md5, stale=false */
957 /* For algorithm MD5, a1 = urp. */
958 auth_digest_compute_response (method, uri, hex_urp, nonce, response);
959 auth_string = g_strdup_printf ("Digest username=\"%s\", "
960 "realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"",
961 conn->username, realm, nonce, uri, response);
963 opaque = (gchar *) g_hash_table_lookup (conn->auth_params, "opaque");
965 auth_string2 = g_strdup_printf ("%s, opaque=\"%s\"", auth_string,
967 g_free (auth_string);
968 auth_string = auth_string2;
970 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
981 gen_date_string (gchar * date_string, guint len)
989 g_get_current_time (&tv);
990 t = (time_t) tv.tv_sec;
993 strftime (date_string, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime_r (&t, &tm_));
995 strftime (date_string, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime (&t));
1000 write_bytes (gint fd, const guint8 * buffer, guint * idx, guint size)
1004 if (G_UNLIKELY (*idx > size))
1005 return GST_RTSP_ERROR;
1012 r = WRITE_SOCKET (fd, &buffer[*idx], left);
1013 if (G_UNLIKELY (r == 0)) {
1014 return GST_RTSP_EINTR;
1015 } else if (G_UNLIKELY (r < 0)) {
1016 if (ERRNO_IS_EAGAIN)
1017 return GST_RTSP_EINTR;
1018 if (!ERRNO_IS_EINTR)
1019 return GST_RTSP_ESYS;
1029 fill_bytes (gint fd, guint8 * buffer, guint size, DecodeCtx * ctx)
1037 while (size > 0 && ctx->cout < ctx->coutl) {
1038 /* we have some leftover bytes */
1039 *buffer++ = ctx->out[ctx->cout];
1044 /* nothing in the buffer */
1048 /* try to read more bytes */
1049 r = READ_SOCKET (fd, &ctx->in[ctx->cin], 4 - ctx->cin);
1057 if (ctx->cin == 4) {
1058 r = g_base64_decode_step ((const gchar *) ctx->in, 4,
1059 (guchar *) ctx->out, &ctx->state, &ctx->save);
1066 out = READ_SOCKET (fd, buffer, size);
1072 static GstRTSPResult
1073 read_bytes (gint fd, guint8 * buffer, guint * idx, guint size, DecodeCtx * ctx)
1077 if (G_UNLIKELY (*idx > size))
1078 return GST_RTSP_ERROR;
1085 r = fill_bytes (fd, &buffer[*idx], left, ctx);
1086 if (G_UNLIKELY (r == 0)) {
1087 return GST_RTSP_EEOF;
1088 } else if (G_UNLIKELY (r < 0)) {
1089 if (ERRNO_IS_EAGAIN)
1090 return GST_RTSP_EINTR;
1091 if (!ERRNO_IS_EINTR)
1092 return GST_RTSP_ESYS;
1101 static GstRTSPResult
1102 read_line (gint fd, guint8 * buffer, guint * idx, guint size, DecodeCtx * ctx)
1108 r = fill_bytes (fd, &c, 1, ctx);
1109 if (G_UNLIKELY (r == 0)) {
1110 return GST_RTSP_EEOF;
1111 } else if (G_UNLIKELY (r < 0)) {
1112 if (ERRNO_IS_EAGAIN)
1113 return GST_RTSP_EINTR;
1114 if (!ERRNO_IS_EINTR)
1115 return GST_RTSP_ESYS;
1117 if (c == '\n') /* end on \n */
1119 if (c == '\r') /* ignore \r */
1122 if (G_LIKELY (*idx < size - 1))
1123 buffer[(*idx)++] = c;
1126 buffer[*idx] = '\0';
1132 * gst_rtsp_connection_write:
1133 * @conn: a #GstRTSPConnection
1134 * @data: the data to write
1135 * @size: the size of @data
1136 * @timeout: a timeout value or #NULL
1138 * Attempt to write @size bytes of @data to the connected @conn, blocking up to
1139 * the specified @timeout. @timeout can be #NULL, in which case this function
1140 * might block forever.
1142 * This function can be cancelled with gst_rtsp_connection_flush().
1144 * Returns: #GST_RTSP_OK on success.
1147 gst_rtsp_connection_write (GstRTSPConnection * conn, const guint8 * data,
1148 guint size, GTimeVal * timeout)
1155 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1156 g_return_val_if_fail (data != NULL || size == 0, GST_RTSP_EINVAL);
1157 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
1159 gst_poll_set_controllable (conn->fdset, TRUE);
1160 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, TRUE);
1161 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, FALSE);
1162 /* clear all previous poll results */
1163 gst_poll_fd_ignored (conn->fdset, conn->writefd);
1164 gst_poll_fd_ignored (conn->fdset, conn->readfd);
1166 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1172 res = write_bytes (conn->writefd->fd, data, &offset, size);
1173 if (G_LIKELY (res == GST_RTSP_OK))
1175 if (G_UNLIKELY (res != GST_RTSP_EINTR))
1178 /* not all is written, wait until we can write more */
1180 retval = gst_poll_wait (conn->fdset, to);
1181 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1183 if (G_UNLIKELY (retval == 0))
1186 if (G_UNLIKELY (retval == -1)) {
1198 return GST_RTSP_ETIMEOUT;
1202 return GST_RTSP_ESYS;
1206 return GST_RTSP_EINTR;
1215 message_to_string (GstRTSPConnection * conn, GstRTSPMessage * message)
1217 GString *str = NULL;
1219 str = g_string_new ("");
1221 switch (message->type) {
1222 case GST_RTSP_MESSAGE_REQUEST:
1223 /* create request string, add CSeq */
1224 g_string_append_printf (str, "%s %s RTSP/1.0\r\n"
1226 gst_rtsp_method_as_text (message->type_data.request.method),
1227 message->type_data.request.uri, conn->cseq++);
1228 /* add session id if we have one */
1229 if (conn->session_id[0] != '\0') {
1230 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SESSION,
1233 /* add any authentication headers */
1234 add_auth_header (conn, message);
1236 case GST_RTSP_MESSAGE_RESPONSE:
1237 /* create response string */
1238 g_string_append_printf (str, "RTSP/1.0 %d %s\r\n",
1239 message->type_data.response.code, message->type_data.response.reason);
1241 case GST_RTSP_MESSAGE_DATA:
1243 guint8 data_header[4];
1245 /* prepare data header */
1246 data_header[0] = '$';
1247 data_header[1] = message->type_data.data.channel;
1248 data_header[2] = (message->body_size >> 8) & 0xff;
1249 data_header[3] = message->body_size & 0xff;
1251 /* create string with header and data */
1252 str = g_string_append_len (str, (gchar *) data_header, 4);
1254 g_string_append_len (str, (gchar *) message->body,
1255 message->body_size);
1259 g_string_free (str, TRUE);
1260 g_return_val_if_reached (NULL);
1264 /* append headers and body */
1265 if (message->type != GST_RTSP_MESSAGE_DATA) {
1266 gchar date_string[100];
1268 gen_date_string (date_string, sizeof (date_string));
1270 /* add date header */
1271 gst_rtsp_message_add_header (message, GST_RTSP_HDR_DATE, date_string);
1273 /* append headers */
1274 gst_rtsp_message_append_headers (message, str);
1276 /* append Content-Length and body if needed */
1277 if (message->body != NULL && message->body_size > 0) {
1280 len = g_strdup_printf ("%d", message->body_size);
1281 g_string_append_printf (str, "%s: %s\r\n",
1282 gst_rtsp_header_as_text (GST_RTSP_HDR_CONTENT_LENGTH), len);
1284 /* header ends here */
1285 g_string_append (str, "\r\n");
1287 g_string_append_len (str, (gchar *) message->body,
1288 message->body_size);
1290 /* just end headers */
1291 g_string_append (str, "\r\n");
1299 * gst_rtsp_connection_send:
1300 * @conn: a #GstRTSPConnection
1301 * @message: the message to send
1302 * @timeout: a timeout value or #NULL
1304 * Attempt to send @message to the connected @conn, blocking up to
1305 * the specified @timeout. @timeout can be #NULL, in which case this function
1306 * might block forever.
1308 * This function can be cancelled with gst_rtsp_connection_flush().
1310 * Returns: #GST_RTSP_OK on success.
1313 gst_rtsp_connection_send (GstRTSPConnection * conn, GstRTSPMessage * message,
1316 GString *string = NULL;
1321 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1322 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1324 if (G_UNLIKELY (!(string = message_to_string (conn, message))))
1327 if (conn->tunneled) {
1328 str = g_base64_encode ((const guchar *) string->str, string->len);
1329 g_string_free (string, TRUE);
1334 g_string_free (string, FALSE);
1338 res = gst_rtsp_connection_write (conn, (guint8 *) str, len, timeout);
1346 g_warning ("Wrong message");
1347 return GST_RTSP_EINVAL;
1352 parse_string (gchar * dest, gint size, gchar ** src)
1358 while (g_ascii_isspace (**src))
1361 while (!g_ascii_isspace (**src) && **src != '\0') {
1363 dest[idx++] = **src;
1371 parse_key (gchar * dest, gint size, gchar ** src)
1376 while (**src != ':' && **src != '\0') {
1378 dest[idx++] = **src;
1385 static GstRTSPResult
1386 parse_response_status (guint8 * buffer, GstRTSPMessage * msg)
1389 gchar versionstr[20];
1394 bptr = (gchar *) buffer;
1396 parse_string (versionstr, sizeof (versionstr), &bptr);
1397 parse_string (codestr, sizeof (codestr), &bptr);
1398 code = atoi (codestr);
1400 while (g_ascii_isspace (*bptr))
1403 if (strcmp (versionstr, "RTSP/1.0") == 0)
1404 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
1406 else if (strncmp (versionstr, "RTSP/", 5) == 0) {
1407 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
1409 msg->type_data.response.version = GST_RTSP_VERSION_INVALID;
1417 return GST_RTSP_EPARSE;
1421 static GstRTSPResult
1422 parse_request_line (GstRTSPConnection * conn, guint8 * buffer,
1423 GstRTSPMessage * msg)
1425 GstRTSPResult res = GST_RTSP_OK;
1426 gchar versionstr[20];
1427 gchar methodstr[20];
1430 GstRTSPMethod method;
1431 GstRTSPTunnelState tstate = TUNNEL_STATE_NONE;
1433 bptr = (gchar *) buffer;
1435 parse_string (methodstr, sizeof (methodstr), &bptr);
1436 method = gst_rtsp_find_method (methodstr);
1437 if (method == GST_RTSP_INVALID) {
1438 /* a tunnel request is allowed when we don't have one yet */
1439 if (conn->tstate != TUNNEL_STATE_NONE)
1440 goto invalid_method;
1441 /* we need GET or POST for a valid tunnel request */
1442 if (!strcmp (methodstr, "GET"))
1443 tstate = TUNNEL_STATE_GET;
1444 else if (!strcmp (methodstr, "POST"))
1445 tstate = TUNNEL_STATE_POST;
1447 goto invalid_method;
1450 parse_string (urlstr, sizeof (urlstr), &bptr);
1451 if (G_UNLIKELY (*urlstr == '\0'))
1454 parse_string (versionstr, sizeof (versionstr), &bptr);
1456 if (G_UNLIKELY (*bptr != '\0'))
1457 goto invalid_version;
1459 if (strcmp (versionstr, "RTSP/1.0") == 0) {
1460 res = gst_rtsp_message_init_request (msg, method, urlstr);
1461 } else if (strncmp (versionstr, "RTSP/", 5) == 0) {
1462 res = gst_rtsp_message_init_request (msg, method, urlstr);
1463 msg->type_data.request.version = GST_RTSP_VERSION_INVALID;
1464 } else if (strcmp (versionstr, "HTTP/1.0") == 0) {
1465 /* tunnel request, we need a tunnel method */
1466 if (tstate == TUNNEL_STATE_NONE) {
1467 res = GST_RTSP_EPARSE;
1469 conn->tstate = tstate;
1472 res = GST_RTSP_EPARSE;
1480 GST_ERROR ("invalid method %s", methodstr);
1481 return GST_RTSP_EPARSE;
1485 GST_ERROR ("invalid url %s", urlstr);
1486 return GST_RTSP_EPARSE;
1490 GST_ERROR ("invalid version");
1491 return GST_RTSP_EPARSE;
1495 static GstRTSPResult
1496 parse_key_value (guint8 * buffer, gchar * key, guint keysize, gchar ** value)
1500 bptr = (gchar *) buffer;
1503 parse_key (key, keysize, &bptr);
1504 if (G_UNLIKELY (*bptr != ':'))
1508 while (g_ascii_isspace (*bptr))
1518 return GST_RTSP_EPARSE;
1522 /* parsing lines means reading a Key: Value pair */
1523 static GstRTSPResult
1524 parse_line (GstRTSPConnection * conn, guint8 * buffer, GstRTSPMessage * msg)
1529 GstRTSPHeaderField field;
1531 res = parse_key_value (buffer, key, sizeof (key), &value);
1532 if (G_UNLIKELY (res != GST_RTSP_OK))
1535 if (conn->tstate == TUNNEL_STATE_GET || conn->tstate == TUNNEL_STATE_POST) {
1536 /* save the tunnel session in the connection */
1537 if (!strcmp (key, "x-sessioncookie")) {
1538 strncpy (conn->tunnelid, value, TUNNELID_LEN);
1539 conn->tunnelid[TUNNELID_LEN - 1] = '\0';
1540 conn->tunneled = TRUE;
1543 field = gst_rtsp_find_header_field (key);
1544 if (field != GST_RTSP_HDR_INVALID)
1545 gst_rtsp_message_add_header (msg, field, value);
1558 * GST_RTSP_OK when a complete message was read.
1559 * GST_RTSP_EEOF: when the socket is closed
1560 * GST_RTSP_EINTR: when more data is needed.
1561 * GST_RTSP_..: some other error occured.
1563 static GstRTSPResult
1564 build_next (GstRTSPBuilder * builder, GstRTSPMessage * message,
1565 GstRTSPConnection * conn)
1570 switch (builder->state) {
1572 builder->offset = 0;
1574 read_bytes (conn->readfd->fd, (guint8 *) builder->buffer,
1575 &builder->offset, 1, conn->ctxp);
1576 if (res != GST_RTSP_OK)
1579 /* we have 1 bytes now and we can see if this is a data message or
1581 if (builder->buffer[0] == '$') {
1582 /* data message, prepare for the header */
1583 builder->state = STATE_DATA_HEADER;
1586 builder->state = STATE_READ_LINES;
1589 case STATE_DATA_HEADER:
1592 read_bytes (conn->readfd->fd, (guint8 *) builder->buffer,
1593 &builder->offset, 4, conn->ctxp);
1594 if (res != GST_RTSP_OK)
1597 gst_rtsp_message_init_data (message, builder->buffer[1]);
1599 builder->body_len = (builder->buffer[2] << 8) | builder->buffer[3];
1600 builder->body_data = g_malloc (builder->body_len + 1);
1601 builder->body_data[builder->body_len] = '\0';
1602 builder->offset = 0;
1603 builder->state = STATE_DATA_BODY;
1606 case STATE_DATA_BODY:
1609 read_bytes (conn->readfd->fd, builder->body_data, &builder->offset,
1610 builder->body_len, conn->ctxp);
1611 if (res != GST_RTSP_OK)
1614 /* we have the complete body now, store in the message adjusting the
1615 * length to include the traling '\0' */
1616 gst_rtsp_message_take_body (message,
1617 (guint8 *) builder->body_data, builder->body_len + 1);
1618 builder->body_data = NULL;
1619 builder->body_len = 0;
1621 builder->state = STATE_END;
1624 case STATE_READ_LINES:
1626 res = read_line (conn->readfd->fd, builder->buffer, &builder->offset,
1627 sizeof (builder->buffer), conn->ctxp);
1628 if (res != GST_RTSP_OK)
1631 /* we have a regular response */
1632 if (builder->buffer[0] == '\r') {
1633 builder->buffer[0] = '\0';
1636 if (builder->buffer[0] == '\0') {
1639 /* empty line, end of message header */
1640 /* see if there is a Content-Length header */
1641 if (gst_rtsp_message_get_header (message,
1642 GST_RTSP_HDR_CONTENT_LENGTH, &hdrval, 0) == GST_RTSP_OK) {
1643 /* there is, prepare to read the body */
1644 builder->body_len = atol (hdrval);
1645 builder->body_data = g_malloc (builder->body_len + 1);
1646 builder->body_data[builder->body_len] = '\0';
1647 builder->offset = 0;
1648 builder->state = STATE_DATA_BODY;
1650 builder->state = STATE_END;
1655 /* we have a line */
1656 if (builder->line == 0) {
1657 /* first line, check for response status */
1658 if (memcmp (builder->buffer, "RTSP", 4) == 0) {
1659 res = parse_response_status (builder->buffer, message);
1661 res = parse_request_line (conn, builder->buffer, message);
1663 /* the first line must parse without errors */
1664 if (res != GST_RTSP_OK)
1667 /* else just parse the line, ignore errors */
1668 parse_line (conn, builder->buffer, message);
1671 builder->offset = 0;
1678 if (conn->tstate == TUNNEL_STATE_GET) {
1679 res = GST_RTSP_ETGET;
1681 } else if (conn->tstate == TUNNEL_STATE_POST) {
1682 res = GST_RTSP_ETPOST;
1686 if (message->type == GST_RTSP_MESSAGE_DATA) {
1687 /* data messages don't have headers */
1692 /* save session id in the connection for further use */
1693 if (gst_rtsp_message_get_header (message, GST_RTSP_HDR_SESSION,
1694 &session_id, 0) == GST_RTSP_OK) {
1697 maxlen = sizeof (conn->session_id) - 1;
1698 /* the sessionid can have attributes marked with ;
1699 * Make sure we strip them */
1700 for (i = 0; session_id[i] != '\0'; i++) {
1701 if (session_id[i] == ';') {
1706 } while (g_ascii_isspace (session_id[i]));
1707 if (g_str_has_prefix (&session_id[i], "timeout=")) {
1710 /* if we parsed something valid, configure */
1711 if ((to = atoi (&session_id[i + 8])) > 0)
1718 /* make sure to not overflow */
1719 strncpy (conn->session_id, session_id, maxlen);
1720 conn->session_id[maxlen] = '\0';
1726 res = GST_RTSP_ERROR;
1735 * gst_rtsp_connection_read:
1736 * @conn: a #GstRTSPConnection
1737 * @data: the data to read
1738 * @size: the size of @data
1739 * @timeout: a timeout value or #NULL
1741 * Attempt to read @size bytes into @data from the connected @conn, blocking up to
1742 * the specified @timeout. @timeout can be #NULL, in which case this function
1743 * might block forever.
1745 * This function can be cancelled with gst_rtsp_connection_flush().
1747 * Returns: #GST_RTSP_OK on success.
1750 gst_rtsp_connection_read (GstRTSPConnection * conn, guint8 * data, guint size,
1758 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1759 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
1760 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1762 if (G_UNLIKELY (size == 0))
1767 /* configure timeout if any */
1768 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1770 gst_poll_set_controllable (conn->fdset, TRUE);
1771 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
1772 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
1775 res = read_bytes (conn->readfd->fd, data, &offset, size, conn->ctxp);
1776 if (G_UNLIKELY (res == GST_RTSP_EEOF))
1778 if (G_LIKELY (res == GST_RTSP_OK))
1780 if (G_UNLIKELY (res != GST_RTSP_EINTR))
1784 retval = gst_poll_wait (conn->fdset, to);
1785 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1787 /* check for timeout */
1788 if (G_UNLIKELY (retval == 0))
1789 goto select_timeout;
1791 if (G_UNLIKELY (retval == -1)) {
1797 gst_poll_set_controllable (conn->fdset, FALSE);
1804 return GST_RTSP_ESYS;
1808 return GST_RTSP_ETIMEOUT;
1812 return GST_RTSP_EINTR;
1816 return GST_RTSP_EEOF;
1825 gen_tunnel_reply (GstRTSPConnection * conn, GstRTSPStatusCode code)
1828 gchar date_string[100];
1829 const gchar *status;
1831 gen_date_string (date_string, sizeof (date_string));
1833 status = gst_rtsp_status_as_text (code);
1834 if (status == NULL) {
1835 code = GST_RTSP_STS_INTERNAL_SERVER_ERROR;
1836 status = "Internal Server Error";
1839 str = g_string_new ("");
1842 g_string_append_printf (str, "HTTP/1.0 %d %s\r\n", code, status);
1843 g_string_append_printf (str,
1844 "Server: GStreamer RTSP Server\r\n"
1846 "Connection: close\r\n"
1847 "Cache-Control: no-store\r\n" "Pragma: no-cache\r\n", date_string);
1848 if (code == GST_RTSP_STS_OK) {
1850 g_string_append_printf (str, "x-server-ip-address: %s\r\n", conn->ip);
1851 g_string_append_printf (str,
1852 "Content-Type: application/x-rtsp-tunnelled\r\n");
1854 g_string_append_printf (str, "\r\n");
1859 * gst_rtsp_connection_receive:
1860 * @conn: a #GstRTSPConnection
1861 * @message: the message to read
1862 * @timeout: a timeout value or #NULL
1864 * Attempt to read into @message from the connected @conn, blocking up to
1865 * the specified @timeout. @timeout can be #NULL, in which case this function
1866 * might block forever.
1868 * This function can be cancelled with gst_rtsp_connection_flush().
1870 * Returns: #GST_RTSP_OK on success.
1873 gst_rtsp_connection_receive (GstRTSPConnection * conn, GstRTSPMessage * message,
1877 GstRTSPBuilder builder;
1881 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1882 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1883 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1885 /* configure timeout if any */
1886 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1888 gst_poll_set_controllable (conn->fdset, TRUE);
1889 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
1890 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
1892 memset (&builder, 0, sizeof (GstRTSPBuilder));
1894 res = build_next (&builder, message, conn);
1895 if (G_UNLIKELY (res == GST_RTSP_EEOF))
1897 if (G_LIKELY (res == GST_RTSP_OK))
1899 if (res == GST_RTSP_ETGET) {
1902 /* tunnel GET request, we can reply now */
1903 str = gen_tunnel_reply (conn, GST_RTSP_STS_OK);
1905 gst_rtsp_connection_write (conn, (guint8 *) str->str, str->len,
1907 g_string_free (str, TRUE);
1908 } else if (res == GST_RTSP_ETPOST) {
1909 /* tunnel POST request, return the value, the caller now has to link the
1910 * two connections. */
1912 } else if (G_UNLIKELY (res != GST_RTSP_EINTR))
1916 retval = gst_poll_wait (conn->fdset, to);
1917 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1919 /* check for timeout */
1920 if (G_UNLIKELY (retval == 0))
1921 goto select_timeout;
1923 if (G_UNLIKELY (retval == -1)) {
1929 gst_poll_set_controllable (conn->fdset, FALSE);
1932 /* we have a message here */
1933 build_reset (&builder);
1940 res = GST_RTSP_ESYS;
1945 res = GST_RTSP_ETIMEOUT;
1950 res = GST_RTSP_EINTR;
1955 res = GST_RTSP_EEOF;
1961 build_reset (&builder);
1962 gst_rtsp_message_unset (message);
1968 * gst_rtsp_connection_close:
1969 * @conn: a #GstRTSPConnection
1971 * Close the connected @conn. After this call, the connection is in the same
1972 * state as when it was first created.
1974 * Returns: #GST_RTSP_OK on success.
1977 gst_rtsp_connection_close (GstRTSPConnection * conn)
1979 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1984 REMOVE_POLLFD (conn->fdset, &conn->fd0);
1985 REMOVE_POLLFD (conn->fdset, &conn->fd1);
1986 conn->writefd = NULL;
1987 conn->readfd = NULL;
1988 conn->tunneled = FALSE;
1989 conn->tstate = TUNNEL_STATE_NONE;
1991 g_free (conn->username);
1992 conn->username = NULL;
1993 g_free (conn->passwd);
1994 conn->passwd = NULL;
1995 gst_rtsp_connection_clear_auth_params (conn);
1998 conn->session_id[0] = '\0';
2004 * gst_rtsp_connection_free:
2005 * @conn: a #GstRTSPConnection
2007 * Close and free @conn.
2009 * Returns: #GST_RTSP_OK on success.
2012 gst_rtsp_connection_free (GstRTSPConnection * conn)
2016 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2018 res = gst_rtsp_connection_close (conn);
2019 gst_poll_free (conn->fdset);
2020 g_timer_destroy (conn->timer);
2021 gst_rtsp_url_free (conn->url);
2022 g_free (conn->proxy_host);
2032 * gst_rtsp_connection_poll:
2033 * @conn: a #GstRTSPConnection
2034 * @events: a bitmask of #GstRTSPEvent flags to check
2035 * @revents: location for result flags
2036 * @timeout: a timeout
2038 * Wait up to the specified @timeout for the connection to become available for
2039 * at least one of the operations specified in @events. When the function returns
2040 * with #GST_RTSP_OK, @revents will contain a bitmask of available operations on
2043 * @timeout can be #NULL, in which case this function might block forever.
2045 * This function can be cancelled with gst_rtsp_connection_flush().
2047 * Returns: #GST_RTSP_OK on success.
2052 gst_rtsp_connection_poll (GstRTSPConnection * conn, GstRTSPEvent events,
2053 GstRTSPEvent * revents, GTimeVal * timeout)
2058 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2059 g_return_val_if_fail (events != 0, GST_RTSP_EINVAL);
2060 g_return_val_if_fail (revents != NULL, GST_RTSP_EINVAL);
2061 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2062 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2064 gst_poll_set_controllable (conn->fdset, TRUE);
2066 /* add fd to writer set when asked to */
2067 gst_poll_fd_ctl_write (conn->fdset, conn->writefd,
2068 events & GST_RTSP_EV_WRITE);
2070 /* add fd to reader set when asked to */
2071 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, events & GST_RTSP_EV_READ);
2073 /* configure timeout if any */
2074 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
2077 retval = gst_poll_wait (conn->fdset, to);
2078 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
2080 if (G_UNLIKELY (retval == 0))
2081 goto select_timeout;
2083 if (G_UNLIKELY (retval == -1)) {
2091 if (events & GST_RTSP_EV_READ) {
2092 if (gst_poll_fd_can_read (conn->fdset, conn->readfd))
2093 *revents |= GST_RTSP_EV_READ;
2095 if (events & GST_RTSP_EV_WRITE) {
2096 if (gst_poll_fd_can_write (conn->fdset, conn->writefd))
2097 *revents |= GST_RTSP_EV_WRITE;
2104 return GST_RTSP_ETIMEOUT;
2108 return GST_RTSP_ESYS;
2112 return GST_RTSP_EINTR;
2117 * gst_rtsp_connection_next_timeout:
2118 * @conn: a #GstRTSPConnection
2119 * @timeout: a timeout
2121 * Calculate the next timeout for @conn, storing the result in @timeout.
2123 * Returns: #GST_RTSP_OK.
2126 gst_rtsp_connection_next_timeout (GstRTSPConnection * conn, GTimeVal * timeout)
2132 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2133 g_return_val_if_fail (timeout != NULL, GST_RTSP_EINVAL);
2135 elapsed = g_timer_elapsed (conn->timer, &usec);
2136 if (elapsed >= conn->timeout) {
2140 sec = conn->timeout - elapsed;
2143 timeout->tv_sec = sec;
2144 timeout->tv_usec = usec;
2150 * gst_rtsp_connection_reset_timeout:
2151 * @conn: a #GstRTSPConnection
2153 * Reset the timeout of @conn.
2155 * Returns: #GST_RTSP_OK.
2158 gst_rtsp_connection_reset_timeout (GstRTSPConnection * conn)
2160 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2162 g_timer_start (conn->timer);
2168 * gst_rtsp_connection_flush:
2169 * @conn: a #GstRTSPConnection
2170 * @flush: start or stop the flush
2172 * Start or stop the flushing action on @conn. When flushing, all current
2173 * and future actions on @conn will return #GST_RTSP_EINTR until the connection
2174 * is set to non-flushing mode again.
2176 * Returns: #GST_RTSP_OK.
2179 gst_rtsp_connection_flush (GstRTSPConnection * conn, gboolean flush)
2181 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2183 gst_poll_set_flushing (conn->fdset, flush);
2189 * gst_rtsp_connection_set_proxy:
2190 * @conn: a #GstRTSPConnection
2191 * @host: the proxy host
2192 * @port: the proxy port
2194 * Set the proxy host and port.
2196 * Returns: #GST_RTSP_OK.
2201 gst_rtsp_connection_set_proxy (GstRTSPConnection * conn,
2202 const gchar * host, guint port)
2204 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2206 g_free (conn->proxy_host);
2207 conn->proxy_host = g_strdup (host);
2208 conn->proxy_port = port;
2214 * gst_rtsp_connection_set_auth:
2215 * @conn: a #GstRTSPConnection
2216 * @method: authentication method
2218 * @pass: the password
2220 * Configure @conn for authentication mode @method with @user and @pass as the
2221 * user and password respectively.
2223 * Returns: #GST_RTSP_OK.
2226 gst_rtsp_connection_set_auth (GstRTSPConnection * conn,
2227 GstRTSPAuthMethod method, const gchar * user, const gchar * pass)
2229 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2231 if (method == GST_RTSP_AUTH_DIGEST && ((user == NULL || pass == NULL)
2232 || g_strrstr (user, ":") != NULL))
2233 return GST_RTSP_EINVAL;
2235 /* Make sure the username and passwd are being set for authentication */
2236 if (method == GST_RTSP_AUTH_NONE && (user == NULL || pass == NULL))
2237 return GST_RTSP_EINVAL;
2239 /* ":" chars are not allowed in usernames for basic auth */
2240 if (method == GST_RTSP_AUTH_BASIC && g_strrstr (user, ":") != NULL)
2241 return GST_RTSP_EINVAL;
2243 g_free (conn->username);
2244 g_free (conn->passwd);
2246 conn->auth_method = method;
2247 conn->username = g_strdup (user);
2248 conn->passwd = g_strdup (pass);
2255 * @key: ASCII string to hash
2257 * Hashes @key in a case-insensitive manner.
2259 * Returns: the hash code.
2262 str_case_hash (gconstpointer key)
2264 const char *p = key;
2265 guint h = g_ascii_toupper (*p);
2268 for (p += 1; *p != '\0'; p++)
2269 h = (h << 5) - h + g_ascii_toupper (*p);
2276 * @v1: an ASCII string
2277 * @v2: another ASCII string
2279 * Compares @v1 and @v2 in a case-insensitive manner
2281 * Returns: %TRUE if they are equal (modulo case)
2284 str_case_equal (gconstpointer v1, gconstpointer v2)
2286 const char *string1 = v1;
2287 const char *string2 = v2;
2289 return g_ascii_strcasecmp (string1, string2) == 0;
2293 * gst_rtsp_connection_set_auth_param:
2294 * @conn: a #GstRTSPConnection
2295 * @param: authentication directive
2298 * Setup @conn with authentication directives. This is not necesary for
2299 * methods #GST_RTSP_AUTH_NONE and #GST_RTSP_AUTH_BASIC. For
2300 * #GST_RTSP_AUTH_DIGEST, directives should be taken from the digest challenge
2301 * in the WWW-Authenticate response header and can include realm, domain,
2302 * nonce, opaque, stale, algorithm, qop as per RFC2617.
2307 gst_rtsp_connection_set_auth_param (GstRTSPConnection * conn,
2308 const gchar * param, const gchar * value)
2310 g_return_if_fail (conn != NULL);
2311 g_return_if_fail (param != NULL);
2313 if (conn->auth_params == NULL) {
2315 g_hash_table_new_full (str_case_hash, str_case_equal, g_free, g_free);
2317 g_hash_table_insert (conn->auth_params, g_strdup (param), g_strdup (value));
2321 * gst_rtsp_connection_clear_auth_params:
2322 * @conn: a #GstRTSPConnection
2324 * Clear the list of authentication directives stored in @conn.
2329 gst_rtsp_connection_clear_auth_params (GstRTSPConnection * conn)
2331 g_return_if_fail (conn != NULL);
2333 if (conn->auth_params != NULL) {
2334 g_hash_table_destroy (conn->auth_params);
2335 conn->auth_params = NULL;
2339 static GstRTSPResult
2340 set_qos_dscp (gint fd, guint qos_dscp)
2345 struct sockaddr_in6 sa_in6;
2346 struct sockaddr_storage sa_stor;
2348 socklen_t slen = sizeof (sa);
2355 if (getsockname (fd, &sa.sa, &slen) < 0)
2356 goto no_getsockname;
2358 af = sa.sa.sa_family;
2360 /* if this is an IPv4-mapped address then do IPv4 QoS */
2361 if (af == AF_INET6) {
2362 if (IN6_IS_ADDR_V4MAPPED (&sa.sa_in6.sin6_addr))
2366 /* extract and shift 6 bits of the DSCP */
2367 tos = (qos_dscp & 0x3f) << 2;
2371 if (SETSOCKOPT (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0)
2376 if (SETSOCKOPT (fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof (tos)) < 0)
2390 return GST_RTSP_ESYS;
2395 return GST_RTSP_ERROR;
2400 * gst_rtsp_connection_set_qos_dscp:
2401 * @conn: a #GstRTSPConnection
2402 * @qos_dscp: DSCP value
2404 * Configure @conn to use the specified DSCP value.
2406 * Returns: #GST_RTSP_OK on success.
2411 gst_rtsp_connection_set_qos_dscp (GstRTSPConnection * conn, guint qos_dscp)
2415 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2416 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2417 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2419 res = set_qos_dscp (conn->fd0.fd, qos_dscp);
2420 if (res == GST_RTSP_OK)
2421 res = set_qos_dscp (conn->fd1.fd, qos_dscp);
2428 * gst_rtsp_connection_get_url:
2429 * @conn: a #GstRTSPConnection
2431 * Retrieve the URL of the other end of @conn.
2433 * Returns: The URL. This value remains valid until the
2434 * connection is freed.
2439 gst_rtsp_connection_get_url (const GstRTSPConnection * conn)
2441 g_return_val_if_fail (conn != NULL, NULL);
2447 * gst_rtsp_connection_get_ip:
2448 * @conn: a #GstRTSPConnection
2450 * Retrieve the IP address of the other end of @conn.
2452 * Returns: The IP address as a string. this value remains valid until the
2453 * connection is closed.
2458 gst_rtsp_connection_get_ip (const GstRTSPConnection * conn)
2460 g_return_val_if_fail (conn != NULL, NULL);
2466 * gst_rtsp_connection_set_ip:
2467 * @conn: a #GstRTSPConnection
2468 * @ip: an ip address
2470 * Set the IP address of the server.
2475 gst_rtsp_connection_set_ip (GstRTSPConnection * conn, const gchar * ip)
2477 g_return_if_fail (conn != NULL);
2480 conn->ip = g_strdup (ip);
2484 * gst_rtsp_connection_get_readfd:
2485 * @conn: a #GstRTSPConnection
2487 * Get the file descriptor for reading.
2489 * Returns: the file descriptor used for reading or -1 on error. The file
2490 * descriptor remains valid until the connection is closed.
2495 gst_rtsp_connection_get_readfd (const GstRTSPConnection * conn)
2497 g_return_val_if_fail (conn != NULL, -1);
2498 g_return_val_if_fail (conn->readfd != NULL, -1);
2500 return conn->readfd->fd;
2504 * gst_rtsp_connection_get_writefd:
2505 * @conn: a #GstRTSPConnection
2507 * Get the file descriptor for writing.
2509 * Returns: the file descriptor used for writing or -1 on error. The file
2510 * descriptor remains valid until the connection is closed.
2515 gst_rtsp_connection_get_writefd (const GstRTSPConnection * conn)
2517 g_return_val_if_fail (conn != NULL, -1);
2518 g_return_val_if_fail (conn->writefd != NULL, -1);
2520 return conn->writefd->fd;
2525 * gst_rtsp_connection_set_tunneled:
2526 * @conn: a #GstRTSPConnection
2527 * @tunneled: the new state
2529 * Set the HTTP tunneling state of the connection. This must be configured before
2530 * the @conn is connected.
2535 gst_rtsp_connection_set_tunneled (GstRTSPConnection * conn, gboolean tunneled)
2537 g_return_if_fail (conn != NULL);
2538 g_return_if_fail (conn->readfd == NULL);
2539 g_return_if_fail (conn->writefd == NULL);
2541 conn->tunneled = tunneled;
2545 * gst_rtsp_connection_is_tunneled:
2546 * @conn: a #GstRTSPConnection
2548 * Get the tunneling state of the connection.
2550 * Returns: if @conn is using HTTP tunneling.
2555 gst_rtsp_connection_is_tunneled (const GstRTSPConnection * conn)
2557 g_return_val_if_fail (conn != NULL, FALSE);
2559 return conn->tunneled;
2563 * gst_rtsp_connection_get_tunnelid:
2564 * @conn: a #GstRTSPConnection
2566 * Get the tunnel session id the connection.
2568 * Returns: returns a non-empty string if @conn is being tunneled over HTTP.
2573 gst_rtsp_connection_get_tunnelid (const GstRTSPConnection * conn)
2575 g_return_val_if_fail (conn != NULL, NULL);
2577 if (!conn->tunneled)
2580 return conn->tunnelid;
2584 * gst_rtsp_connection_do_tunnel:
2585 * @conn: a #GstRTSPConnection
2586 * @conn2: a #GstRTSPConnection
2588 * If @conn received the first tunnel connection and @conn2 received
2589 * the second tunnel connection, link the two connections together so that
2590 * @conn manages the tunneled connection.
2592 * After this call, @conn2 cannot be used anymore and must be freed with
2593 * gst_rtsp_connection_free().
2595 * Returns: return GST_RTSP_OK on success.
2600 gst_rtsp_connection_do_tunnel (GstRTSPConnection * conn,
2601 GstRTSPConnection * conn2)
2603 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2604 g_return_val_if_fail (conn2 != NULL, GST_RTSP_EINVAL);
2605 g_return_val_if_fail (conn->tstate == TUNNEL_STATE_GET, GST_RTSP_EINVAL);
2606 g_return_val_if_fail (conn2->tstate == TUNNEL_STATE_POST, GST_RTSP_EINVAL);
2607 g_return_val_if_fail (!memcmp (conn2->tunnelid, conn->tunnelid, TUNNELID_LEN),
2610 /* both connections have fd0 as the read/write socket. start by taking the
2611 * socket from conn2 and set it as the socket in conn */
2612 conn->fd1 = conn2->fd0;
2614 /* clean up some of the state of conn2 */
2615 gst_poll_remove_fd (conn2->fdset, &conn2->fd0);
2617 conn2->readfd = conn2->writefd = NULL;
2619 /* We make fd0 the write socket and fd1 the read socket. */
2620 conn->writefd = &conn->fd0;
2621 conn->readfd = &conn->fd1;
2623 conn->tstate = TUNNEL_STATE_COMPLETE;
2625 /* we need base64 decoding for the readfd */
2626 conn->ctx.state = 0;
2630 conn->ctxp = &conn->ctx;
2635 #define READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
2636 #define WRITE_COND (G_IO_OUT | G_IO_ERR)
2644 static GstRTSPRec *queue_response (GstRTSPWatch * watch, GString * str,
2647 /* async functions */
2648 struct _GstRTSPWatch
2652 GstRTSPConnection *conn;
2654 GstRTSPBuilder builder;
2655 GstRTSPMessage message;
2659 gboolean write_added;
2661 /* queued message for transmission */
2662 GAsyncQueue *messages;
2668 GstRTSPWatchFuncs funcs;
2671 GDestroyNotify notify;
2675 gst_rtsp_source_prepare (GSource * source, gint * timeout)
2677 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2679 *timeout = (watch->conn->timeout * 1000);
2685 gst_rtsp_source_check (GSource * source)
2687 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2689 if (watch->readfd.revents & READ_COND)
2692 if (watch->writefd.revents & WRITE_COND)
2699 gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback G_GNUC_UNUSED,
2700 gpointer user_data G_GNUC_UNUSED)
2702 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2705 /* first read as much as we can */
2706 if (watch->readfd.revents & READ_COND) {
2708 res = build_next (&watch->builder, &watch->message, watch->conn);
2709 if (res == GST_RTSP_EINTR)
2711 if (G_UNLIKELY (res == GST_RTSP_EEOF))
2713 if (res == GST_RTSP_ETGET) {
2715 GstRTSPStatusCode code;
2717 if (watch->funcs.tunnel_start)
2718 code = watch->funcs.tunnel_start (watch, watch->user_data);
2720 code = GST_RTSP_STS_OK;
2722 /* queue the response string */
2723 str = gen_tunnel_reply (watch->conn, code);
2724 queue_response (watch, str, UNKNOWN_CSEQ);
2725 } else if (res == GST_RTSP_ETPOST) {
2726 /* in the callback the connection should be tunneled with the
2728 if (watch->funcs.tunnel_complete)
2729 watch->funcs.tunnel_complete (watch, watch->user_data);
2730 } else if (G_UNLIKELY (res != GST_RTSP_OK))
2733 if (G_LIKELY (res == GST_RTSP_OK)) {
2734 if (watch->funcs.message_received)
2735 watch->funcs.message_received (watch, &watch->message,
2738 gst_rtsp_message_unset (&watch->message);
2740 build_reset (&watch->builder);
2744 if (watch->writefd.revents & WRITE_COND) {
2746 if (watch->write_data == NULL) {
2749 /* get a new message from the queue */
2750 data = g_async_queue_try_pop (watch->messages);
2754 watch->write_off = 0;
2755 watch->write_len = data->str->len;
2756 watch->write_data = (guint8 *) g_string_free (data->str, FALSE);
2757 watch->write_cseq = data->cseq;
2760 g_slice_free (GstRTSPRec, data);
2763 res = write_bytes (watch->writefd.fd, watch->write_data,
2764 &watch->write_off, watch->write_len);
2765 if (res == GST_RTSP_EINTR)
2767 if (G_UNLIKELY (res != GST_RTSP_OK))
2770 if (watch->funcs.message_sent && watch->write_cseq != UNKNOWN_CSEQ)
2771 watch->funcs.message_sent (watch, watch->write_cseq, watch->user_data);
2774 if (g_async_queue_length (watch->messages) == 0 && watch->write_added) {
2775 g_source_remove_poll ((GSource *) watch, &watch->writefd);
2776 watch->write_added = FALSE;
2777 watch->writefd.revents = 0;
2779 g_free (watch->write_data);
2780 watch->write_data = NULL;
2789 if (watch->funcs.closed)
2790 watch->funcs.closed (watch, watch->user_data);
2795 if (watch->funcs.error)
2796 watch->funcs.error (watch, res, watch->user_data);
2802 gst_rtsp_rec_free (gpointer data)
2804 GstRTSPRec *rec = data;
2806 g_string_free (rec->str, TRUE);
2808 g_slice_free (GstRTSPRec, rec);
2812 gst_rtsp_source_finalize (GSource * source)
2814 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2816 build_reset (&watch->builder);
2818 g_async_queue_unref (watch->messages);
2819 watch->messages = NULL;
2822 watch->notify (watch->user_data);
2825 static GSourceFuncs gst_rtsp_source_funcs = {
2826 gst_rtsp_source_prepare,
2827 gst_rtsp_source_check,
2828 gst_rtsp_source_dispatch,
2829 gst_rtsp_source_finalize,
2835 * gst_rtsp_watch_new:
2836 * @conn: a #GstRTSPConnection
2837 * @funcs: watch functions
2838 * @user_data: user data to pass to @funcs
2839 * @notify: notify when @user_data is not referenced anymore
2841 * Create a watch object for @conn. The functions provided in @funcs will be
2842 * called with @user_data when activity happened on the watch.
2844 * The new watch is usually created so that it can be attached to a
2845 * maincontext with gst_rtsp_watch_attach().
2847 * @conn must exist for the entire lifetime of the watch.
2849 * Returns: a #GstRTSPWatch that can be used for asynchronous RTSP
2850 * communication. Free with gst_rtsp_watch_unref () after usage.
2855 gst_rtsp_watch_new (GstRTSPConnection * conn,
2856 GstRTSPWatchFuncs * funcs, gpointer user_data, GDestroyNotify notify)
2858 GstRTSPWatch *result;
2860 g_return_val_if_fail (conn != NULL, NULL);
2861 g_return_val_if_fail (funcs != NULL, NULL);
2862 g_return_val_if_fail (conn->readfd != NULL, NULL);
2863 g_return_val_if_fail (conn->writefd != NULL, NULL);
2865 result = (GstRTSPWatch *) g_source_new (&gst_rtsp_source_funcs,
2866 sizeof (GstRTSPWatch));
2868 result->conn = conn;
2869 result->builder.state = STATE_START;
2871 result->messages = g_async_queue_new_full (gst_rtsp_rec_free);
2873 result->readfd.fd = -1;
2874 result->writefd.fd = -1;
2876 gst_rtsp_watch_reset (result);
2878 result->funcs = *funcs;
2879 result->user_data = user_data;
2880 result->notify = notify;
2882 /* only add the read fd, the write fd is only added when we have data
2884 g_source_add_poll ((GSource *) result, &result->readfd);
2890 * gst_rtsp_watch_reset:
2891 * @watch: a #GstRTSPWatch
2893 * Reset @watch, this is usually called after gst_rtsp_connection_do_tunnel()
2894 * when the file descriptors of the connection might have changed.
2899 gst_rtsp_watch_reset (GstRTSPWatch * watch)
2901 if (watch->readfd.fd != -1)
2902 g_source_remove_poll ((GSource *) watch, &watch->readfd);
2903 if (watch->writefd.fd != -1)
2904 g_source_remove_poll ((GSource *) watch, &watch->writefd);
2906 watch->readfd.fd = watch->conn->readfd->fd;
2907 watch->readfd.events = READ_COND;
2908 watch->readfd.revents = 0;
2910 watch->writefd.fd = watch->conn->writefd->fd;
2911 watch->writefd.events = WRITE_COND;
2912 watch->writefd.revents = 0;
2913 watch->write_added = FALSE;
2915 g_source_add_poll ((GSource *) watch, &watch->readfd);
2919 * gst_rtsp_watch_attach:
2920 * @watch: a #GstRTSPWatch
2921 * @context: a GMainContext (if NULL, the default context will be used)
2923 * Adds a #GstRTSPWatch to a context so that it will be executed within that context.
2925 * Returns: the ID (greater than 0) for the watch within the GMainContext.
2930 gst_rtsp_watch_attach (GstRTSPWatch * watch, GMainContext * context)
2932 g_return_val_if_fail (watch != NULL, 0);
2934 return g_source_attach ((GSource *) watch, context);
2938 * gst_rtsp_watch_unref:
2939 * @watch: a #GstRTSPWatch
2941 * Decreases the reference count of @watch by one. If the resulting reference
2942 * count is zero the watch and associated memory will be destroyed.
2947 gst_rtsp_watch_unref (GstRTSPWatch * watch)
2949 g_return_if_fail (watch != NULL);
2951 g_source_unref ((GSource *) watch);
2955 queue_response (GstRTSPWatch * watch, GString * str, guint cseq)
2959 /* make a record with the message as a string ans cseq */
2960 data = g_slice_new (GstRTSPRec);
2964 /* add the record to a queue. FIXME we would like to have an upper limit here */
2965 g_async_queue_push (watch->messages, data);
2967 /* FIXME: does the following need to be made thread-safe? (queue_response
2968 * might be called from a streaming thread, like appsink's render function) */
2969 /* make sure the main context will now also check for writability on the
2971 if (!watch->write_added) {
2972 g_source_add_poll ((GSource *) watch, &watch->writefd);
2973 watch->write_added = TRUE;
2980 * gst_rtsp_watch_queue_message:
2981 * @watch: a #GstRTSPWatch
2982 * @message: a #GstRTSPMessage
2984 * Queue a @message for transmission in @watch. The contents of this
2985 * message will be serialized and transmitted when the connection of the
2986 * watch becomes writable.
2988 * The return value of this function will be returned as the cseq argument in
2989 * the message_sent callback.
2991 * Returns: the sequence number of the message or -1 if the cseq could not be
2997 gst_rtsp_watch_queue_message (GstRTSPWatch * watch, GstRTSPMessage * message)
3003 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3004 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3006 /* get the cseq from the message, when we finish writing this message on the
3007 * socket we will have to pass the cseq to the callback. */
3008 if (gst_rtsp_message_get_header (message, GST_RTSP_HDR_CSEQ, &header,
3009 0) == GST_RTSP_OK) {
3010 cseq = atoi (header);
3012 cseq = UNKNOWN_CSEQ;
3015 /* make a record with the message as a string and cseq */
3016 data = queue_response (watch, message_to_string (watch->conn, message), cseq);