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 res = gst_rtsp_connection_write (conn, (guint8 *) str, strlen (str), timeout);
709 if (res != GST_RTSP_OK)
714 g_free (url_port_str);
721 GST_ERROR ("write failed (%d)", res);
735 res = GST_RTSP_ETIMEOUT;
745 res = GST_RTSP_EINTR;
750 GST_ERROR ("got failure response %d %s", code, resultstr);
751 res = GST_RTSP_ERROR;
756 GST_ERROR ("could not resolve %s", conn->ip);
762 GST_ERROR ("failed to connect");
768 * gst_rtsp_connection_connect:
769 * @conn: a #GstRTSPConnection
770 * @timeout: a #GTimeVal timeout
772 * Attempt to connect to the url of @conn made with
773 * gst_rtsp_connection_create(). If @timeout is #NULL this function can block
774 * forever. If @timeout contains a valid timeout, this function will return
775 * #GST_RTSP_ETIMEOUT after the timeout expired.
777 * This function can be cancelled with gst_rtsp_connection_flush().
779 * Returns: #GST_RTSP_OK when a connection could be made.
782 gst_rtsp_connection_connect (GstRTSPConnection * conn, GTimeVal * timeout)
789 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
790 g_return_val_if_fail (conn->url != NULL, GST_RTSP_EINVAL);
791 g_return_val_if_fail (conn->fd0.fd < 0, GST_RTSP_EINVAL);
795 if (conn->proxy_host && conn->tunneled) {
796 if (!(ip = do_resolve (conn->proxy_host))) {
797 GST_ERROR ("could not resolve %s", conn->proxy_host);
800 port = conn->proxy_port;
801 g_free (conn->proxy_host);
802 conn->proxy_host = ip;
804 if (!(ip = do_resolve (url->host))) {
805 GST_ERROR ("could not resolve %s", url->host);
808 /* get the port from the url */
809 gst_rtsp_url_get_port (url, &port);
815 /* connect to the host/port */
816 res = do_connect (ip, port, &conn->fd0, conn->fdset, timeout);
817 if (res != GST_RTSP_OK)
820 /* this is our read URL */
821 conn->readfd = &conn->fd0;
823 if (conn->tunneled) {
824 res = setup_tunneling (conn, timeout);
825 if (res != GST_RTSP_OK)
826 goto tunneling_failed;
828 conn->writefd = &conn->fd0;
835 return GST_RTSP_ENET;
839 GST_ERROR ("failed to connect");
844 GST_ERROR ("failed to setup tunneling");
850 md5_digest_to_hex_string (unsigned char digest[16], char string[33])
852 static const char hexdigits[] = "0123456789abcdef";
855 for (i = 0; i < 16; i++) {
856 string[i * 2] = hexdigits[(digest[i] >> 4) & 0x0f];
857 string[i * 2 + 1] = hexdigits[digest[i] & 0x0f];
863 auth_digest_compute_hex_urp (const gchar * username,
864 const gchar * realm, const gchar * password, gchar hex_urp[33])
866 struct MD5Context md5_context;
867 unsigned char digest[16];
869 MD5Init (&md5_context);
870 MD5Update (&md5_context, username, strlen (username));
871 MD5Update (&md5_context, ":", 1);
872 MD5Update (&md5_context, realm, strlen (realm));
873 MD5Update (&md5_context, ":", 1);
874 MD5Update (&md5_context, password, strlen (password));
875 MD5Final (digest, &md5_context);
876 md5_digest_to_hex_string (digest, hex_urp);
880 auth_digest_compute_response (const gchar * method,
881 const gchar * uri, const gchar * hex_a1, const gchar * nonce,
885 struct MD5Context md5_context;
886 unsigned char digest[16];
889 MD5Init (&md5_context);
890 MD5Update (&md5_context, method, strlen (method));
891 MD5Update (&md5_context, ":", 1);
892 MD5Update (&md5_context, uri, strlen (uri));
893 MD5Final (digest, &md5_context);
894 md5_digest_to_hex_string (digest, hex_a2);
897 MD5Init (&md5_context);
898 MD5Update (&md5_context, hex_a1, strlen (hex_a1));
899 MD5Update (&md5_context, ":", 1);
900 MD5Update (&md5_context, nonce, strlen (nonce));
901 MD5Update (&md5_context, ":", 1);
903 MD5Update (&md5_context, hex_a2, 32);
904 MD5Final (digest, &md5_context);
905 md5_digest_to_hex_string (digest, response);
909 add_auth_header (GstRTSPConnection * conn, GstRTSPMessage * message)
911 switch (conn->auth_method) {
912 case GST_RTSP_AUTH_BASIC:{
917 user_pass = g_strdup_printf ("%s:%s", conn->username, conn->passwd);
918 user_pass64 = g_base64_encode ((guchar *) user_pass, strlen (user_pass));
919 auth_string = g_strdup_printf ("Basic %s", user_pass64);
921 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
925 g_free (user_pass64);
928 case GST_RTSP_AUTH_DIGEST:{
929 gchar response[33], hex_urp[33];
930 gchar *auth_string, *auth_string2;
937 /* we need to have some params set */
938 if (conn->auth_params == NULL)
941 /* we need the realm and nonce */
942 realm = (gchar *) g_hash_table_lookup (conn->auth_params, "realm");
943 nonce = (gchar *) g_hash_table_lookup (conn->auth_params, "nonce");
944 if (realm == NULL || nonce == NULL)
947 auth_digest_compute_hex_urp (conn->username, realm, conn->passwd,
950 method = gst_rtsp_method_as_text (message->type_data.request.method);
951 uri = message->type_data.request.uri;
953 /* Assume no qop, algorithm=md5, stale=false */
954 /* For algorithm MD5, a1 = urp. */
955 auth_digest_compute_response (method, uri, hex_urp, nonce, response);
956 auth_string = g_strdup_printf ("Digest username=\"%s\", "
957 "realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"",
958 conn->username, realm, nonce, uri, response);
960 opaque = (gchar *) g_hash_table_lookup (conn->auth_params, "opaque");
962 auth_string2 = g_strdup_printf ("%s, opaque=\"%s\"", auth_string,
964 g_free (auth_string);
965 auth_string = auth_string2;
967 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
978 gen_date_string (gchar * date_string, guint len)
986 g_get_current_time (&tv);
987 t = (time_t) tv.tv_sec;
990 strftime (date_string, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime_r (&t, &tm_));
992 strftime (date_string, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime (&t));
997 write_bytes (gint fd, const guint8 * buffer, guint * idx, guint size)
1001 if (G_UNLIKELY (*idx > size))
1002 return GST_RTSP_ERROR;
1009 r = WRITE_SOCKET (fd, &buffer[*idx], left);
1010 if (G_UNLIKELY (r == 0)) {
1011 return GST_RTSP_EINTR;
1012 } else if (G_UNLIKELY (r < 0)) {
1013 if (ERRNO_IS_EAGAIN)
1014 return GST_RTSP_EINTR;
1015 if (!ERRNO_IS_EINTR)
1016 return GST_RTSP_ESYS;
1026 fill_bytes (gint fd, guint8 * buffer, guint size, DecodeCtx * ctx)
1034 while (size > 0 && ctx->cout < ctx->coutl) {
1035 /* we have some leftover bytes */
1036 *buffer++ = ctx->out[ctx->cout];
1041 /* nothing in the buffer */
1045 /* try to read more bytes */
1046 r = READ_SOCKET (fd, &ctx->in[ctx->cin], 4 - ctx->cin);
1054 if (ctx->cin == 4) {
1055 r = g_base64_decode_step ((const gchar *) ctx->in, 4,
1056 (guchar *) ctx->out, &ctx->state, &ctx->save);
1063 out = READ_SOCKET (fd, buffer, size);
1069 static GstRTSPResult
1070 read_bytes (gint fd, guint8 * buffer, guint * idx, guint size, DecodeCtx * ctx)
1074 if (G_UNLIKELY (*idx > size))
1075 return GST_RTSP_ERROR;
1082 r = fill_bytes (fd, &buffer[*idx], left, ctx);
1083 if (G_UNLIKELY (r == 0)) {
1084 return GST_RTSP_EEOF;
1085 } else if (G_UNLIKELY (r < 0)) {
1086 if (ERRNO_IS_EAGAIN)
1087 return GST_RTSP_EINTR;
1088 if (!ERRNO_IS_EINTR)
1089 return GST_RTSP_ESYS;
1098 static GstRTSPResult
1099 read_line (gint fd, guint8 * buffer, guint * idx, guint size, DecodeCtx * ctx)
1105 r = fill_bytes (fd, &c, 1, ctx);
1106 if (G_UNLIKELY (r == 0)) {
1107 return GST_RTSP_EEOF;
1108 } else if (G_UNLIKELY (r < 0)) {
1109 if (ERRNO_IS_EAGAIN)
1110 return GST_RTSP_EINTR;
1111 if (!ERRNO_IS_EINTR)
1112 return GST_RTSP_ESYS;
1114 if (c == '\n') /* end on \n */
1116 if (c == '\r') /* ignore \r */
1119 if (G_LIKELY (*idx < size - 1))
1120 buffer[(*idx)++] = c;
1123 buffer[*idx] = '\0';
1129 * gst_rtsp_connection_write:
1130 * @conn: a #GstRTSPConnection
1131 * @data: the data to write
1132 * @size: the size of @data
1133 * @timeout: a timeout value or #NULL
1135 * Attempt to write @size bytes of @data to the connected @conn, blocking up to
1136 * the specified @timeout. @timeout can be #NULL, in which case this function
1137 * might block forever.
1139 * This function can be cancelled with gst_rtsp_connection_flush().
1141 * Returns: #GST_RTSP_OK on success.
1144 gst_rtsp_connection_write (GstRTSPConnection * conn, const guint8 * data,
1145 guint size, GTimeVal * timeout)
1152 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1153 g_return_val_if_fail (data != NULL || size == 0, GST_RTSP_EINVAL);
1154 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
1156 gst_poll_set_controllable (conn->fdset, TRUE);
1157 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, TRUE);
1158 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, FALSE);
1159 /* clear all previous poll results */
1160 gst_poll_fd_ignored (conn->fdset, conn->writefd);
1161 gst_poll_fd_ignored (conn->fdset, conn->readfd);
1163 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1169 res = write_bytes (conn->writefd->fd, data, &offset, size);
1170 if (G_LIKELY (res == GST_RTSP_OK))
1172 if (G_UNLIKELY (res != GST_RTSP_EINTR))
1175 /* not all is written, wait until we can write more */
1177 retval = gst_poll_wait (conn->fdset, to);
1178 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1180 if (G_UNLIKELY (retval == 0))
1183 if (G_UNLIKELY (retval == -1)) {
1195 return GST_RTSP_ETIMEOUT;
1199 return GST_RTSP_ESYS;
1203 return GST_RTSP_EINTR;
1212 message_to_string (GstRTSPConnection * conn, GstRTSPMessage * message)
1214 GString *str = NULL;
1216 str = g_string_new ("");
1218 switch (message->type) {
1219 case GST_RTSP_MESSAGE_REQUEST:
1220 /* create request string, add CSeq */
1221 g_string_append_printf (str, "%s %s RTSP/1.0\r\n"
1223 gst_rtsp_method_as_text (message->type_data.request.method),
1224 message->type_data.request.uri, conn->cseq++);
1225 /* add session id if we have one */
1226 if (conn->session_id[0] != '\0') {
1227 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SESSION,
1230 /* add any authentication headers */
1231 add_auth_header (conn, message);
1233 case GST_RTSP_MESSAGE_RESPONSE:
1234 /* create response string */
1235 g_string_append_printf (str, "RTSP/1.0 %d %s\r\n",
1236 message->type_data.response.code, message->type_data.response.reason);
1238 case GST_RTSP_MESSAGE_DATA:
1240 guint8 data_header[4];
1242 /* prepare data header */
1243 data_header[0] = '$';
1244 data_header[1] = message->type_data.data.channel;
1245 data_header[2] = (message->body_size >> 8) & 0xff;
1246 data_header[3] = message->body_size & 0xff;
1248 /* create string with header and data */
1249 str = g_string_append_len (str, (gchar *) data_header, 4);
1251 g_string_append_len (str, (gchar *) message->body,
1252 message->body_size);
1256 g_string_free (str, TRUE);
1257 g_return_val_if_reached (NULL);
1261 /* append headers and body */
1262 if (message->type != GST_RTSP_MESSAGE_DATA) {
1263 gchar date_string[100];
1265 gen_date_string (date_string, sizeof (date_string));
1267 /* add date header */
1268 gst_rtsp_message_add_header (message, GST_RTSP_HDR_DATE, date_string);
1270 /* append headers */
1271 gst_rtsp_message_append_headers (message, str);
1273 /* append Content-Length and body if needed */
1274 if (message->body != NULL && message->body_size > 0) {
1277 len = g_strdup_printf ("%d", message->body_size);
1278 g_string_append_printf (str, "%s: %s\r\n",
1279 gst_rtsp_header_as_text (GST_RTSP_HDR_CONTENT_LENGTH), len);
1281 /* header ends here */
1282 g_string_append (str, "\r\n");
1284 g_string_append_len (str, (gchar *) message->body,
1285 message->body_size);
1287 /* just end headers */
1288 g_string_append (str, "\r\n");
1296 * gst_rtsp_connection_send:
1297 * @conn: a #GstRTSPConnection
1298 * @message: the message to send
1299 * @timeout: a timeout value or #NULL
1301 * Attempt to send @message to the connected @conn, blocking up to
1302 * the specified @timeout. @timeout can be #NULL, in which case this function
1303 * might block forever.
1305 * This function can be cancelled with gst_rtsp_connection_flush().
1307 * Returns: #GST_RTSP_OK on success.
1310 gst_rtsp_connection_send (GstRTSPConnection * conn, GstRTSPMessage * message,
1313 GString *string = NULL;
1318 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1319 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1321 if (G_UNLIKELY (!(string = message_to_string (conn, message))))
1324 if (conn->tunneled) {
1325 str = g_base64_encode ((const guchar *) string->str, string->len);
1326 g_string_free (string, TRUE);
1331 g_string_free (string, FALSE);
1335 res = gst_rtsp_connection_write (conn, (guint8 *) str, len, timeout);
1343 g_warning ("Wrong message");
1344 return GST_RTSP_EINVAL;
1349 parse_string (gchar * dest, gint size, gchar ** src)
1355 while (g_ascii_isspace (**src))
1358 while (!g_ascii_isspace (**src) && **src != '\0') {
1360 dest[idx++] = **src;
1368 parse_key (gchar * dest, gint size, gchar ** src)
1373 while (**src != ':' && **src != '\0') {
1375 dest[idx++] = **src;
1382 static GstRTSPResult
1383 parse_response_status (guint8 * buffer, GstRTSPMessage * msg)
1386 gchar versionstr[20];
1391 bptr = (gchar *) buffer;
1393 parse_string (versionstr, sizeof (versionstr), &bptr);
1394 parse_string (codestr, sizeof (codestr), &bptr);
1395 code = atoi (codestr);
1397 while (g_ascii_isspace (*bptr))
1400 if (strcmp (versionstr, "RTSP/1.0") == 0)
1401 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
1403 else if (strncmp (versionstr, "RTSP/", 5) == 0) {
1404 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
1406 msg->type_data.response.version = GST_RTSP_VERSION_INVALID;
1414 return GST_RTSP_EPARSE;
1418 static GstRTSPResult
1419 parse_request_line (GstRTSPConnection * conn, guint8 * buffer,
1420 GstRTSPMessage * msg)
1422 GstRTSPResult res = GST_RTSP_OK;
1423 gchar versionstr[20];
1424 gchar methodstr[20];
1427 GstRTSPMethod method;
1428 GstRTSPTunnelState tstate = TUNNEL_STATE_NONE;
1430 bptr = (gchar *) buffer;
1432 parse_string (methodstr, sizeof (methodstr), &bptr);
1433 method = gst_rtsp_find_method (methodstr);
1434 if (method == GST_RTSP_INVALID) {
1435 /* a tunnel request is allowed when we don't have one yet */
1436 if (conn->tstate != TUNNEL_STATE_NONE)
1437 goto invalid_method;
1438 /* we need GET or POST for a valid tunnel request */
1439 if (!strcmp (methodstr, "GET"))
1440 tstate = TUNNEL_STATE_GET;
1441 else if (!strcmp (methodstr, "POST"))
1442 tstate = TUNNEL_STATE_POST;
1444 goto invalid_method;
1447 parse_string (urlstr, sizeof (urlstr), &bptr);
1448 if (G_UNLIKELY (*urlstr == '\0'))
1451 parse_string (versionstr, sizeof (versionstr), &bptr);
1453 if (G_UNLIKELY (*bptr != '\0'))
1454 goto invalid_version;
1456 if (strcmp (versionstr, "RTSP/1.0") == 0) {
1457 res = gst_rtsp_message_init_request (msg, method, urlstr);
1458 } else if (strncmp (versionstr, "RTSP/", 5) == 0) {
1459 res = gst_rtsp_message_init_request (msg, method, urlstr);
1460 msg->type_data.request.version = GST_RTSP_VERSION_INVALID;
1461 } else if (strcmp (versionstr, "HTTP/1.0") == 0) {
1462 /* tunnel request, we need a tunnel method */
1463 if (tstate == TUNNEL_STATE_NONE) {
1464 res = GST_RTSP_EPARSE;
1466 conn->tstate = tstate;
1469 res = GST_RTSP_EPARSE;
1477 GST_ERROR ("invalid method %s", methodstr);
1478 return GST_RTSP_EPARSE;
1482 GST_ERROR ("invalid url %s", urlstr);
1483 return GST_RTSP_EPARSE;
1487 GST_ERROR ("invalid version");
1488 return GST_RTSP_EPARSE;
1492 static GstRTSPResult
1493 parse_key_value (guint8 * buffer, gchar * key, guint keysize, gchar ** value)
1497 bptr = (gchar *) buffer;
1500 parse_key (key, keysize, &bptr);
1501 if (G_UNLIKELY (*bptr != ':'))
1505 while (g_ascii_isspace (*bptr))
1515 return GST_RTSP_EPARSE;
1519 /* parsing lines means reading a Key: Value pair */
1520 static GstRTSPResult
1521 parse_line (GstRTSPConnection * conn, guint8 * buffer, GstRTSPMessage * msg)
1526 GstRTSPHeaderField field;
1528 res = parse_key_value (buffer, key, sizeof (key), &value);
1529 if (G_UNLIKELY (res != GST_RTSP_OK))
1532 if (conn->tstate == TUNNEL_STATE_GET || conn->tstate == TUNNEL_STATE_POST) {
1533 /* save the tunnel session in the connection */
1534 if (!strcmp (key, "x-sessioncookie")) {
1535 strncpy (conn->tunnelid, value, TUNNELID_LEN);
1536 conn->tunnelid[TUNNELID_LEN - 1] = '\0';
1537 conn->tunneled = TRUE;
1540 field = gst_rtsp_find_header_field (key);
1541 if (field != GST_RTSP_HDR_INVALID)
1542 gst_rtsp_message_add_header (msg, field, value);
1555 * GST_RTSP_OK when a complete message was read.
1556 * GST_RTSP_EEOF: when the socket is closed
1557 * GST_RTSP_EINTR: when more data is needed.
1558 * GST_RTSP_..: some other error occured.
1560 static GstRTSPResult
1561 build_next (GstRTSPBuilder * builder, GstRTSPMessage * message,
1562 GstRTSPConnection * conn)
1567 switch (builder->state) {
1569 builder->offset = 0;
1571 read_bytes (conn->readfd->fd, (guint8 *) builder->buffer,
1572 &builder->offset, 1, conn->ctxp);
1573 if (res != GST_RTSP_OK)
1576 /* we have 1 bytes now and we can see if this is a data message or
1578 if (builder->buffer[0] == '$') {
1579 /* data message, prepare for the header */
1580 builder->state = STATE_DATA_HEADER;
1583 builder->state = STATE_READ_LINES;
1586 case STATE_DATA_HEADER:
1589 read_bytes (conn->readfd->fd, (guint8 *) builder->buffer,
1590 &builder->offset, 4, conn->ctxp);
1591 if (res != GST_RTSP_OK)
1594 gst_rtsp_message_init_data (message, builder->buffer[1]);
1596 builder->body_len = (builder->buffer[2] << 8) | builder->buffer[3];
1597 builder->body_data = g_malloc (builder->body_len + 1);
1598 builder->body_data[builder->body_len] = '\0';
1599 builder->offset = 0;
1600 builder->state = STATE_DATA_BODY;
1603 case STATE_DATA_BODY:
1606 read_bytes (conn->readfd->fd, builder->body_data, &builder->offset,
1607 builder->body_len, conn->ctxp);
1608 if (res != GST_RTSP_OK)
1611 /* we have the complete body now, store in the message adjusting the
1612 * length to include the traling '\0' */
1613 gst_rtsp_message_take_body (message,
1614 (guint8 *) builder->body_data, builder->body_len + 1);
1615 builder->body_data = NULL;
1616 builder->body_len = 0;
1618 builder->state = STATE_END;
1621 case STATE_READ_LINES:
1623 res = read_line (conn->readfd->fd, builder->buffer, &builder->offset,
1624 sizeof (builder->buffer), conn->ctxp);
1625 if (res != GST_RTSP_OK)
1628 /* we have a regular response */
1629 if (builder->buffer[0] == '\r') {
1630 builder->buffer[0] = '\0';
1633 if (builder->buffer[0] == '\0') {
1636 /* empty line, end of message header */
1637 /* see if there is a Content-Length header */
1638 if (gst_rtsp_message_get_header (message,
1639 GST_RTSP_HDR_CONTENT_LENGTH, &hdrval, 0) == GST_RTSP_OK) {
1640 /* there is, prepare to read the body */
1641 builder->body_len = atol (hdrval);
1642 builder->body_data = g_malloc (builder->body_len + 1);
1643 builder->body_data[builder->body_len] = '\0';
1644 builder->offset = 0;
1645 builder->state = STATE_DATA_BODY;
1647 builder->state = STATE_END;
1652 /* we have a line */
1653 if (builder->line == 0) {
1654 /* first line, check for response status */
1655 if (memcmp (builder->buffer, "RTSP", 4) == 0) {
1656 res = parse_response_status (builder->buffer, message);
1658 res = parse_request_line (conn, builder->buffer, message);
1660 /* the first line must parse without errors */
1661 if (res != GST_RTSP_OK)
1664 /* else just parse the line, ignore errors */
1665 parse_line (conn, builder->buffer, message);
1668 builder->offset = 0;
1675 if (conn->tstate == TUNNEL_STATE_GET) {
1676 res = GST_RTSP_ETGET;
1678 } else if (conn->tstate == TUNNEL_STATE_POST) {
1679 res = GST_RTSP_ETPOST;
1683 if (message->type == GST_RTSP_MESSAGE_DATA) {
1684 /* data messages don't have headers */
1689 /* save session id in the connection for further use */
1690 if (gst_rtsp_message_get_header (message, GST_RTSP_HDR_SESSION,
1691 &session_id, 0) == GST_RTSP_OK) {
1694 maxlen = sizeof (conn->session_id) - 1;
1695 /* the sessionid can have attributes marked with ;
1696 * Make sure we strip them */
1697 for (i = 0; session_id[i] != '\0'; i++) {
1698 if (session_id[i] == ';') {
1703 } while (g_ascii_isspace (session_id[i]));
1704 if (g_str_has_prefix (&session_id[i], "timeout=")) {
1707 /* if we parsed something valid, configure */
1708 if ((to = atoi (&session_id[i + 8])) > 0)
1715 /* make sure to not overflow */
1716 strncpy (conn->session_id, session_id, maxlen);
1717 conn->session_id[maxlen] = '\0';
1723 res = GST_RTSP_ERROR;
1732 * gst_rtsp_connection_read:
1733 * @conn: a #GstRTSPConnection
1734 * @data: the data to read
1735 * @size: the size of @data
1736 * @timeout: a timeout value or #NULL
1738 * Attempt to read @size bytes into @data from the connected @conn, blocking up to
1739 * the specified @timeout. @timeout can be #NULL, in which case this function
1740 * might block forever.
1742 * This function can be cancelled with gst_rtsp_connection_flush().
1744 * Returns: #GST_RTSP_OK on success.
1747 gst_rtsp_connection_read (GstRTSPConnection * conn, guint8 * data, guint size,
1755 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1756 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
1757 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1759 if (G_UNLIKELY (size == 0))
1764 /* configure timeout if any */
1765 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1767 gst_poll_set_controllable (conn->fdset, TRUE);
1768 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
1769 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
1772 res = read_bytes (conn->readfd->fd, data, &offset, size, conn->ctxp);
1773 if (G_UNLIKELY (res == GST_RTSP_EEOF))
1775 if (G_LIKELY (res == GST_RTSP_OK))
1777 if (G_UNLIKELY (res != GST_RTSP_EINTR))
1781 retval = gst_poll_wait (conn->fdset, to);
1782 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1784 /* check for timeout */
1785 if (G_UNLIKELY (retval == 0))
1786 goto select_timeout;
1788 if (G_UNLIKELY (retval == -1)) {
1794 gst_poll_set_controllable (conn->fdset, FALSE);
1801 return GST_RTSP_ESYS;
1805 return GST_RTSP_ETIMEOUT;
1809 return GST_RTSP_EINTR;
1813 return GST_RTSP_EEOF;
1822 gen_tunnel_reply (GstRTSPConnection * conn, GstRTSPStatusCode code)
1825 gchar date_string[100];
1826 const gchar *status;
1828 gen_date_string (date_string, sizeof (date_string));
1830 status = gst_rtsp_status_as_text (code);
1831 if (status == NULL) {
1832 code = GST_RTSP_STS_INTERNAL_SERVER_ERROR;
1833 status = "Internal Server Error";
1836 str = g_string_new ("");
1839 g_string_append_printf (str, "HTTP/1.0 %d %s\r\n", code, status);
1840 g_string_append_printf (str,
1841 "Server: GStreamer RTSP Server\r\n"
1843 "Connection: close\r\n"
1844 "Cache-Control: no-store\r\n" "Pragma: no-cache\r\n", date_string);
1845 if (code == GST_RTSP_STS_OK) {
1847 g_string_append_printf (str, "x-server-ip-address: %s\r\n", conn->ip);
1848 g_string_append_printf (str,
1849 "Content-Type: application/x-rtsp-tunnelled\r\n");
1851 g_string_append_printf (str, "\r\n");
1856 * gst_rtsp_connection_receive:
1857 * @conn: a #GstRTSPConnection
1858 * @message: the message to read
1859 * @timeout: a timeout value or #NULL
1861 * Attempt to read into @message from the connected @conn, blocking up to
1862 * the specified @timeout. @timeout can be #NULL, in which case this function
1863 * might block forever.
1865 * This function can be cancelled with gst_rtsp_connection_flush().
1867 * Returns: #GST_RTSP_OK on success.
1870 gst_rtsp_connection_receive (GstRTSPConnection * conn, GstRTSPMessage * message,
1874 GstRTSPBuilder builder;
1878 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1879 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1880 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1882 /* configure timeout if any */
1883 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1885 gst_poll_set_controllable (conn->fdset, TRUE);
1886 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
1887 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
1889 memset (&builder, 0, sizeof (GstRTSPBuilder));
1891 res = build_next (&builder, message, conn);
1892 if (G_UNLIKELY (res == GST_RTSP_EEOF))
1894 if (G_LIKELY (res == GST_RTSP_OK))
1896 if (res == GST_RTSP_ETGET) {
1899 /* tunnel GET request, we can reply now */
1900 str = gen_tunnel_reply (conn, GST_RTSP_STS_OK);
1902 gst_rtsp_connection_write (conn, (guint8 *) str->str, str->len,
1904 g_string_free (str, TRUE);
1905 } else if (res == GST_RTSP_ETPOST) {
1906 /* tunnel POST request, return the value, the caller now has to link the
1907 * two connections. */
1909 } else if (G_UNLIKELY (res != GST_RTSP_EINTR))
1913 retval = gst_poll_wait (conn->fdset, to);
1914 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1916 /* check for timeout */
1917 if (G_UNLIKELY (retval == 0))
1918 goto select_timeout;
1920 if (G_UNLIKELY (retval == -1)) {
1926 gst_poll_set_controllable (conn->fdset, FALSE);
1929 /* we have a message here */
1930 build_reset (&builder);
1937 res = GST_RTSP_ESYS;
1942 res = GST_RTSP_ETIMEOUT;
1947 res = GST_RTSP_EINTR;
1952 res = GST_RTSP_EEOF;
1958 build_reset (&builder);
1959 gst_rtsp_message_unset (message);
1965 * gst_rtsp_connection_close:
1966 * @conn: a #GstRTSPConnection
1968 * Close the connected @conn. After this call, the connection is in the same
1969 * state as when it was first created.
1971 * Returns: #GST_RTSP_OK on success.
1974 gst_rtsp_connection_close (GstRTSPConnection * conn)
1976 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1981 REMOVE_POLLFD (conn->fdset, &conn->fd0);
1982 REMOVE_POLLFD (conn->fdset, &conn->fd1);
1983 conn->writefd = NULL;
1984 conn->readfd = NULL;
1985 conn->tunneled = FALSE;
1986 conn->tstate = TUNNEL_STATE_NONE;
1988 g_free (conn->username);
1989 conn->username = NULL;
1990 g_free (conn->passwd);
1991 conn->passwd = NULL;
1992 gst_rtsp_connection_clear_auth_params (conn);
1995 conn->session_id[0] = '\0';
2001 * gst_rtsp_connection_free:
2002 * @conn: a #GstRTSPConnection
2004 * Close and free @conn.
2006 * Returns: #GST_RTSP_OK on success.
2009 gst_rtsp_connection_free (GstRTSPConnection * conn)
2013 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2015 res = gst_rtsp_connection_close (conn);
2016 gst_poll_free (conn->fdset);
2017 g_timer_destroy (conn->timer);
2018 gst_rtsp_url_free (conn->url);
2019 g_free (conn->proxy_host);
2029 * gst_rtsp_connection_poll:
2030 * @conn: a #GstRTSPConnection
2031 * @events: a bitmask of #GstRTSPEvent flags to check
2032 * @revents: location for result flags
2033 * @timeout: a timeout
2035 * Wait up to the specified @timeout for the connection to become available for
2036 * at least one of the operations specified in @events. When the function returns
2037 * with #GST_RTSP_OK, @revents will contain a bitmask of available operations on
2040 * @timeout can be #NULL, in which case this function might block forever.
2042 * This function can be cancelled with gst_rtsp_connection_flush().
2044 * Returns: #GST_RTSP_OK on success.
2049 gst_rtsp_connection_poll (GstRTSPConnection * conn, GstRTSPEvent events,
2050 GstRTSPEvent * revents, GTimeVal * timeout)
2055 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2056 g_return_val_if_fail (events != 0, GST_RTSP_EINVAL);
2057 g_return_val_if_fail (revents != NULL, GST_RTSP_EINVAL);
2058 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2059 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2061 gst_poll_set_controllable (conn->fdset, TRUE);
2063 /* add fd to writer set when asked to */
2064 gst_poll_fd_ctl_write (conn->fdset, conn->writefd,
2065 events & GST_RTSP_EV_WRITE);
2067 /* add fd to reader set when asked to */
2068 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, events & GST_RTSP_EV_READ);
2070 /* configure timeout if any */
2071 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
2074 retval = gst_poll_wait (conn->fdset, to);
2075 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
2077 if (G_UNLIKELY (retval == 0))
2078 goto select_timeout;
2080 if (G_UNLIKELY (retval == -1)) {
2088 if (events & GST_RTSP_EV_READ) {
2089 if (gst_poll_fd_can_read (conn->fdset, conn->readfd))
2090 *revents |= GST_RTSP_EV_READ;
2092 if (events & GST_RTSP_EV_WRITE) {
2093 if (gst_poll_fd_can_write (conn->fdset, conn->writefd))
2094 *revents |= GST_RTSP_EV_WRITE;
2101 return GST_RTSP_ETIMEOUT;
2105 return GST_RTSP_ESYS;
2109 return GST_RTSP_EINTR;
2114 * gst_rtsp_connection_next_timeout:
2115 * @conn: a #GstRTSPConnection
2116 * @timeout: a timeout
2118 * Calculate the next timeout for @conn, storing the result in @timeout.
2120 * Returns: #GST_RTSP_OK.
2123 gst_rtsp_connection_next_timeout (GstRTSPConnection * conn, GTimeVal * timeout)
2129 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2130 g_return_val_if_fail (timeout != NULL, GST_RTSP_EINVAL);
2132 elapsed = g_timer_elapsed (conn->timer, &usec);
2133 if (elapsed >= conn->timeout) {
2137 sec = conn->timeout - elapsed;
2140 timeout->tv_sec = sec;
2141 timeout->tv_usec = usec;
2147 * gst_rtsp_connection_reset_timeout:
2148 * @conn: a #GstRTSPConnection
2150 * Reset the timeout of @conn.
2152 * Returns: #GST_RTSP_OK.
2155 gst_rtsp_connection_reset_timeout (GstRTSPConnection * conn)
2157 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2159 g_timer_start (conn->timer);
2165 * gst_rtsp_connection_flush:
2166 * @conn: a #GstRTSPConnection
2167 * @flush: start or stop the flush
2169 * Start or stop the flushing action on @conn. When flushing, all current
2170 * and future actions on @conn will return #GST_RTSP_EINTR until the connection
2171 * is set to non-flushing mode again.
2173 * Returns: #GST_RTSP_OK.
2176 gst_rtsp_connection_flush (GstRTSPConnection * conn, gboolean flush)
2178 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2180 gst_poll_set_flushing (conn->fdset, flush);
2186 * gst_rtsp_connection_set_proxy:
2187 * @conn: a #GstRTSPConnection
2188 * @host: the proxy host
2189 * @port: the proxy port
2191 * Set the proxy host and port.
2193 * Returns: #GST_RTSP_OK.
2198 gst_rtsp_connection_set_proxy (GstRTSPConnection * conn,
2199 const gchar * host, guint port)
2201 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2203 g_free (conn->proxy_host);
2204 conn->proxy_host = g_strdup (host);
2205 conn->proxy_port = port;
2211 * gst_rtsp_connection_set_auth:
2212 * @conn: a #GstRTSPConnection
2213 * @method: authentication method
2215 * @pass: the password
2217 * Configure @conn for authentication mode @method with @user and @pass as the
2218 * user and password respectively.
2220 * Returns: #GST_RTSP_OK.
2223 gst_rtsp_connection_set_auth (GstRTSPConnection * conn,
2224 GstRTSPAuthMethod method, const gchar * user, const gchar * pass)
2226 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2228 if (method == GST_RTSP_AUTH_DIGEST && ((user == NULL || pass == NULL)
2229 || g_strrstr (user, ":") != NULL))
2230 return GST_RTSP_EINVAL;
2232 /* Make sure the username and passwd are being set for authentication */
2233 if (method == GST_RTSP_AUTH_NONE && (user == NULL || pass == NULL))
2234 return GST_RTSP_EINVAL;
2236 /* ":" chars are not allowed in usernames for basic auth */
2237 if (method == GST_RTSP_AUTH_BASIC && g_strrstr (user, ":") != NULL)
2238 return GST_RTSP_EINVAL;
2240 g_free (conn->username);
2241 g_free (conn->passwd);
2243 conn->auth_method = method;
2244 conn->username = g_strdup (user);
2245 conn->passwd = g_strdup (pass);
2252 * @key: ASCII string to hash
2254 * Hashes @key in a case-insensitive manner.
2256 * Returns: the hash code.
2259 str_case_hash (gconstpointer key)
2261 const char *p = key;
2262 guint h = g_ascii_toupper (*p);
2265 for (p += 1; *p != '\0'; p++)
2266 h = (h << 5) - h + g_ascii_toupper (*p);
2273 * @v1: an ASCII string
2274 * @v2: another ASCII string
2276 * Compares @v1 and @v2 in a case-insensitive manner
2278 * Returns: %TRUE if they are equal (modulo case)
2281 str_case_equal (gconstpointer v1, gconstpointer v2)
2283 const char *string1 = v1;
2284 const char *string2 = v2;
2286 return g_ascii_strcasecmp (string1, string2) == 0;
2290 * gst_rtsp_connection_set_auth_param:
2291 * @conn: a #GstRTSPConnection
2292 * @param: authentication directive
2295 * Setup @conn with authentication directives. This is not necesary for
2296 * methods #GST_RTSP_AUTH_NONE and #GST_RTSP_AUTH_BASIC. For
2297 * #GST_RTSP_AUTH_DIGEST, directives should be taken from the digest challenge
2298 * in the WWW-Authenticate response header and can include realm, domain,
2299 * nonce, opaque, stale, algorithm, qop as per RFC2617.
2304 gst_rtsp_connection_set_auth_param (GstRTSPConnection * conn,
2305 const gchar * param, const gchar * value)
2307 g_return_if_fail (conn != NULL);
2308 g_return_if_fail (param != NULL);
2310 if (conn->auth_params == NULL) {
2312 g_hash_table_new_full (str_case_hash, str_case_equal, g_free, g_free);
2314 g_hash_table_insert (conn->auth_params, g_strdup (param), g_strdup (value));
2318 * gst_rtsp_connection_clear_auth_params:
2319 * @conn: a #GstRTSPConnection
2321 * Clear the list of authentication directives stored in @conn.
2326 gst_rtsp_connection_clear_auth_params (GstRTSPConnection * conn)
2328 g_return_if_fail (conn != NULL);
2330 if (conn->auth_params != NULL) {
2331 g_hash_table_destroy (conn->auth_params);
2332 conn->auth_params = NULL;
2336 static GstRTSPResult
2337 set_qos_dscp (gint fd, guint qos_dscp)
2342 struct sockaddr_in6 sa_in6;
2343 struct sockaddr_storage sa_stor;
2345 socklen_t slen = sizeof (sa);
2352 if (getsockname (fd, &sa.sa, &slen) < 0)
2353 goto no_getsockname;
2355 af = sa.sa.sa_family;
2357 /* if this is an IPv4-mapped address then do IPv4 QoS */
2358 if (af == AF_INET6) {
2359 if (IN6_IS_ADDR_V4MAPPED (&sa.sa_in6.sin6_addr))
2363 /* extract and shift 6 bits of the DSCP */
2364 tos = (qos_dscp & 0x3f) << 2;
2368 if (SETSOCKOPT (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0)
2373 if (SETSOCKOPT (fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof (tos)) < 0)
2387 return GST_RTSP_ESYS;
2392 return GST_RTSP_ERROR;
2397 * gst_rtsp_connection_set_qos_dscp:
2398 * @conn: a #GstRTSPConnection
2399 * @qos_dscp: DSCP value
2401 * Configure @conn to use the specified DSCP value.
2403 * Returns: #GST_RTSP_OK on success.
2408 gst_rtsp_connection_set_qos_dscp (GstRTSPConnection * conn, guint qos_dscp)
2412 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2413 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2414 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2416 res = set_qos_dscp (conn->fd0.fd, qos_dscp);
2417 if (res == GST_RTSP_OK)
2418 res = set_qos_dscp (conn->fd1.fd, qos_dscp);
2425 * gst_rtsp_connection_get_url:
2426 * @conn: a #GstRTSPConnection
2428 * Retrieve the URL of the other end of @conn.
2430 * Returns: The URL. This value remains valid until the
2431 * connection is freed.
2436 gst_rtsp_connection_get_url (const GstRTSPConnection * conn)
2438 g_return_val_if_fail (conn != NULL, NULL);
2444 * gst_rtsp_connection_get_ip:
2445 * @conn: a #GstRTSPConnection
2447 * Retrieve the IP address of the other end of @conn.
2449 * Returns: The IP address as a string. this value remains valid until the
2450 * connection is closed.
2455 gst_rtsp_connection_get_ip (const GstRTSPConnection * conn)
2457 g_return_val_if_fail (conn != NULL, NULL);
2463 * gst_rtsp_connection_set_ip:
2464 * @conn: a #GstRTSPConnection
2465 * @ip: an ip address
2467 * Set the IP address of the server.
2472 gst_rtsp_connection_set_ip (GstRTSPConnection * conn, const gchar * ip)
2474 g_return_if_fail (conn != NULL);
2477 conn->ip = g_strdup (ip);
2481 * gst_rtsp_connection_get_readfd:
2482 * @conn: a #GstRTSPConnection
2484 * Get the file descriptor for reading.
2486 * Returns: the file descriptor used for reading or -1 on error. The file
2487 * descriptor remains valid until the connection is closed.
2492 gst_rtsp_connection_get_readfd (const GstRTSPConnection * conn)
2494 g_return_val_if_fail (conn != NULL, -1);
2495 g_return_val_if_fail (conn->readfd != NULL, -1);
2497 return conn->readfd->fd;
2501 * gst_rtsp_connection_get_writefd:
2502 * @conn: a #GstRTSPConnection
2504 * Get the file descriptor for writing.
2506 * Returns: the file descriptor used for writing or -1 on error. The file
2507 * descriptor remains valid until the connection is closed.
2512 gst_rtsp_connection_get_writefd (const GstRTSPConnection * conn)
2514 g_return_val_if_fail (conn != NULL, -1);
2515 g_return_val_if_fail (conn->writefd != NULL, -1);
2517 return conn->writefd->fd;
2522 * gst_rtsp_connection_set_tunneled:
2523 * @conn: a #GstRTSPConnection
2524 * @tunneled: the new state
2526 * Set the HTTP tunneling state of the connection. This must be configured before
2527 * the @conn is connected.
2532 gst_rtsp_connection_set_tunneled (GstRTSPConnection * conn, gboolean tunneled)
2534 g_return_if_fail (conn != NULL);
2535 g_return_if_fail (conn->readfd == NULL);
2536 g_return_if_fail (conn->writefd == NULL);
2538 conn->tunneled = tunneled;
2542 * gst_rtsp_connection_is_tunneled:
2543 * @conn: a #GstRTSPConnection
2545 * Get the tunneling state of the connection.
2547 * Returns: if @conn is using HTTP tunneling.
2552 gst_rtsp_connection_is_tunneled (const GstRTSPConnection * conn)
2554 g_return_val_if_fail (conn != NULL, FALSE);
2556 return conn->tunneled;
2560 * gst_rtsp_connection_get_tunnelid:
2561 * @conn: a #GstRTSPConnection
2563 * Get the tunnel session id the connection.
2565 * Returns: returns a non-empty string if @conn is being tunneled over HTTP.
2570 gst_rtsp_connection_get_tunnelid (const GstRTSPConnection * conn)
2572 g_return_val_if_fail (conn != NULL, NULL);
2574 if (!conn->tunneled)
2577 return conn->tunnelid;
2581 * gst_rtsp_connection_do_tunnel:
2582 * @conn: a #GstRTSPConnection
2583 * @conn2: a #GstRTSPConnection
2585 * If @conn received the first tunnel connection and @conn2 received
2586 * the second tunnel connection, link the two connections together so that
2587 * @conn manages the tunneled connection.
2589 * After this call, @conn2 cannot be used anymore and must be freed with
2590 * gst_rtsp_connection_free().
2592 * Returns: return GST_RTSP_OK on success.
2597 gst_rtsp_connection_do_tunnel (GstRTSPConnection * conn,
2598 GstRTSPConnection * conn2)
2600 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2601 g_return_val_if_fail (conn2 != NULL, GST_RTSP_EINVAL);
2602 g_return_val_if_fail (conn->tstate == TUNNEL_STATE_GET, GST_RTSP_EINVAL);
2603 g_return_val_if_fail (conn2->tstate == TUNNEL_STATE_POST, GST_RTSP_EINVAL);
2604 g_return_val_if_fail (!memcmp (conn2->tunnelid, conn->tunnelid, TUNNELID_LEN),
2607 /* both connections have fd0 as the read/write socket. start by taking the
2608 * socket from conn2 and set it as the socket in conn */
2609 conn->fd1 = conn2->fd0;
2611 /* clean up some of the state of conn2 */
2612 gst_poll_remove_fd (conn2->fdset, &conn2->fd0);
2614 conn2->readfd = conn2->writefd = NULL;
2616 /* We make fd0 the write socket and fd1 the read socket. */
2617 conn->writefd = &conn->fd0;
2618 conn->readfd = &conn->fd1;
2620 conn->tstate = TUNNEL_STATE_COMPLETE;
2622 /* we need base64 decoding for the readfd */
2623 conn->ctx.state = 0;
2627 conn->ctxp = &conn->ctx;
2632 #define READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
2633 #define WRITE_COND (G_IO_OUT | G_IO_ERR)
2641 static GstRTSPRec *queue_response (GstRTSPWatch * watch, GString * str,
2644 /* async functions */
2645 struct _GstRTSPWatch
2649 GstRTSPConnection *conn;
2651 GstRTSPBuilder builder;
2652 GstRTSPMessage message;
2656 gboolean write_added;
2658 /* queued message for transmission */
2659 GAsyncQueue *messages;
2665 GstRTSPWatchFuncs funcs;
2668 GDestroyNotify notify;
2672 gst_rtsp_source_prepare (GSource * source, gint * timeout)
2674 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2676 *timeout = (watch->conn->timeout * 1000);
2682 gst_rtsp_source_check (GSource * source)
2684 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2686 if (watch->readfd.revents & READ_COND)
2689 if (watch->writefd.revents & WRITE_COND)
2696 gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback G_GNUC_UNUSED,
2697 gpointer user_data G_GNUC_UNUSED)
2699 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2702 /* first read as much as we can */
2703 if (watch->readfd.revents & READ_COND) {
2705 res = build_next (&watch->builder, &watch->message, watch->conn);
2706 if (res == GST_RTSP_EINTR)
2708 if (G_UNLIKELY (res == GST_RTSP_EEOF))
2710 if (res == GST_RTSP_ETGET) {
2712 GstRTSPStatusCode code;
2714 if (watch->funcs.tunnel_start)
2715 code = watch->funcs.tunnel_start (watch, watch->user_data);
2717 code = GST_RTSP_STS_OK;
2719 /* queue the response string */
2720 str = gen_tunnel_reply (watch->conn, code);
2721 queue_response (watch, str, UNKNOWN_CSEQ);
2722 } else if (res == GST_RTSP_ETPOST) {
2723 /* in the callback the connection should be tunneled with the
2725 if (watch->funcs.tunnel_complete)
2726 watch->funcs.tunnel_complete (watch, watch->user_data);
2727 } else if (G_UNLIKELY (res != GST_RTSP_OK))
2730 if (G_LIKELY (res == GST_RTSP_OK)) {
2731 if (watch->funcs.message_received)
2732 watch->funcs.message_received (watch, &watch->message,
2735 gst_rtsp_message_unset (&watch->message);
2737 build_reset (&watch->builder);
2741 if (watch->writefd.revents & WRITE_COND) {
2743 if (watch->write_data == NULL) {
2746 /* get a new message from the queue */
2747 data = g_async_queue_try_pop (watch->messages);
2751 watch->write_off = 0;
2752 watch->write_len = data->str->len;
2753 watch->write_data = (guint8 *) g_string_free (data->str, FALSE);
2754 watch->write_cseq = data->cseq;
2757 g_slice_free (GstRTSPRec, data);
2760 res = write_bytes (watch->writefd.fd, watch->write_data,
2761 &watch->write_off, watch->write_len);
2762 if (res == GST_RTSP_EINTR)
2764 if (G_UNLIKELY (res != GST_RTSP_OK))
2767 if (watch->funcs.message_sent && watch->write_cseq != UNKNOWN_CSEQ)
2768 watch->funcs.message_sent (watch, watch->write_cseq, watch->user_data);
2771 if (g_async_queue_length (watch->messages) == 0 && watch->write_added) {
2772 g_source_remove_poll ((GSource *) watch, &watch->writefd);
2773 watch->write_added = FALSE;
2774 watch->writefd.revents = 0;
2776 g_free (watch->write_data);
2777 watch->write_data = NULL;
2786 if (watch->funcs.closed)
2787 watch->funcs.closed (watch, watch->user_data);
2792 if (watch->funcs.error)
2793 watch->funcs.error (watch, res, watch->user_data);
2799 gst_rtsp_rec_free (gpointer data)
2801 GstRTSPRec *rec = data;
2803 g_string_free (rec->str, TRUE);
2805 g_slice_free (GstRTSPRec, rec);
2809 gst_rtsp_source_finalize (GSource * source)
2811 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2813 build_reset (&watch->builder);
2814 gst_rtsp_message_unset (&watch->message);
2816 g_async_queue_unref (watch->messages);
2817 watch->messages = NULL;
2819 g_free (watch->write_data);
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 and 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)
3002 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3003 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3005 /* get the cseq from the message, when we finish writing this message on the
3006 * socket we will have to pass the cseq to the callback. */
3007 if (gst_rtsp_message_get_header (message, GST_RTSP_HDR_CSEQ, &header,
3008 0) == GST_RTSP_OK) {
3009 cseq = atoi (header);
3011 cseq = UNKNOWN_CSEQ;
3014 /* make a record with the message as a string and cseq */
3015 queue_response (watch, message_to_string (watch->conn, message), cseq);