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))
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) {
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);
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" "\r\n");
1858 * gst_rtsp_connection_receive:
1859 * @conn: a #GstRTSPConnection
1860 * @message: the message to read
1861 * @timeout: a timeout value or #NULL
1863 * Attempt to read into @message from the connected @conn, blocking up to
1864 * the specified @timeout. @timeout can be #NULL, in which case this function
1865 * might block forever.
1867 * This function can be cancelled with gst_rtsp_connection_flush().
1869 * Returns: #GST_RTSP_OK on success.
1872 gst_rtsp_connection_receive (GstRTSPConnection * conn, GstRTSPMessage * message,
1876 GstRTSPBuilder builder;
1880 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1881 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1882 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1884 /* configure timeout if any */
1885 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1887 gst_poll_set_controllable (conn->fdset, TRUE);
1888 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
1889 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
1891 memset (&builder, 0, sizeof (GstRTSPBuilder));
1893 res = build_next (&builder, message, conn);
1894 if (G_UNLIKELY (res == GST_RTSP_EEOF))
1896 if (G_LIKELY (res == GST_RTSP_OK))
1898 if (res == GST_RTSP_ETGET) {
1901 /* tunnel GET request, we can reply now */
1902 str = gen_tunnel_reply (conn, GST_RTSP_STS_OK);
1904 gst_rtsp_connection_write (conn, (guint8 *) str->str, str->len,
1906 g_string_free (str, TRUE);
1907 } else if (res == GST_RTSP_ETPOST) {
1908 /* tunnel POST request, return the value, the caller now has to link the
1909 * two connections. */
1911 } else if (G_UNLIKELY (res != GST_RTSP_EINTR))
1915 retval = gst_poll_wait (conn->fdset, to);
1916 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1918 /* check for timeout */
1919 if (G_UNLIKELY (retval == 0))
1920 goto select_timeout;
1922 if (G_UNLIKELY (retval == -1)) {
1928 gst_poll_set_controllable (conn->fdset, FALSE);
1931 /* we have a message here */
1932 build_reset (&builder);
1939 res = GST_RTSP_ESYS;
1944 res = GST_RTSP_ETIMEOUT;
1949 res = GST_RTSP_EINTR;
1954 res = GST_RTSP_EEOF;
1960 build_reset (&builder);
1961 gst_rtsp_message_unset (message);
1967 * gst_rtsp_connection_close:
1968 * @conn: a #GstRTSPConnection
1970 * Close the connected @conn. After this call, the connection is in the same
1971 * state as when it was first created.
1973 * Returns: #GST_RTSP_OK on success.
1976 gst_rtsp_connection_close (GstRTSPConnection * conn)
1978 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1983 REMOVE_POLLFD (conn->fdset, &conn->fd0);
1984 REMOVE_POLLFD (conn->fdset, &conn->fd1);
1985 conn->writefd = NULL;
1986 conn->readfd = NULL;
1987 conn->tunneled = FALSE;
1988 conn->tstate = TUNNEL_STATE_NONE;
1990 g_free (conn->username);
1991 conn->username = NULL;
1992 g_free (conn->passwd);
1993 conn->passwd = NULL;
1994 gst_rtsp_connection_clear_auth_params (conn);
1997 conn->session_id[0] = '\0';
2003 * gst_rtsp_connection_free:
2004 * @conn: a #GstRTSPConnection
2006 * Close and free @conn.
2008 * Returns: #GST_RTSP_OK on success.
2011 gst_rtsp_connection_free (GstRTSPConnection * conn)
2015 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2017 res = gst_rtsp_connection_close (conn);
2018 gst_poll_free (conn->fdset);
2019 g_timer_destroy (conn->timer);
2020 gst_rtsp_url_free (conn->url);
2021 g_free (conn->proxy_host);
2031 * gst_rtsp_connection_poll:
2032 * @conn: a #GstRTSPConnection
2033 * @events: a bitmask of #GstRTSPEvent flags to check
2034 * @revents: location for result flags
2035 * @timeout: a timeout
2037 * Wait up to the specified @timeout for the connection to become available for
2038 * at least one of the operations specified in @events. When the function returns
2039 * with #GST_RTSP_OK, @revents will contain a bitmask of available operations on
2042 * @timeout can be #NULL, in which case this function might block forever.
2044 * This function can be cancelled with gst_rtsp_connection_flush().
2046 * Returns: #GST_RTSP_OK on success.
2051 gst_rtsp_connection_poll (GstRTSPConnection * conn, GstRTSPEvent events,
2052 GstRTSPEvent * revents, GTimeVal * timeout)
2057 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2058 g_return_val_if_fail (events != 0, GST_RTSP_EINVAL);
2059 g_return_val_if_fail (revents != NULL, GST_RTSP_EINVAL);
2060 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2061 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2063 gst_poll_set_controllable (conn->fdset, TRUE);
2065 /* add fd to writer set when asked to */
2066 gst_poll_fd_ctl_write (conn->fdset, conn->writefd,
2067 events & GST_RTSP_EV_WRITE);
2069 /* add fd to reader set when asked to */
2070 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, events & GST_RTSP_EV_READ);
2072 /* configure timeout if any */
2073 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
2076 retval = gst_poll_wait (conn->fdset, to);
2077 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
2079 if (G_UNLIKELY (retval == 0))
2080 goto select_timeout;
2082 if (G_UNLIKELY (retval == -1)) {
2090 if (events & GST_RTSP_EV_READ) {
2091 if (gst_poll_fd_can_read (conn->fdset, conn->readfd))
2092 *revents |= GST_RTSP_EV_READ;
2094 if (events & GST_RTSP_EV_WRITE) {
2095 if (gst_poll_fd_can_write (conn->fdset, conn->writefd))
2096 *revents |= GST_RTSP_EV_WRITE;
2103 return GST_RTSP_ETIMEOUT;
2107 return GST_RTSP_ESYS;
2111 return GST_RTSP_EINTR;
2116 * gst_rtsp_connection_next_timeout:
2117 * @conn: a #GstRTSPConnection
2118 * @timeout: a timeout
2120 * Calculate the next timeout for @conn, storing the result in @timeout.
2122 * Returns: #GST_RTSP_OK.
2125 gst_rtsp_connection_next_timeout (GstRTSPConnection * conn, GTimeVal * timeout)
2131 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2132 g_return_val_if_fail (timeout != NULL, GST_RTSP_EINVAL);
2134 elapsed = g_timer_elapsed (conn->timer, &usec);
2135 if (elapsed >= conn->timeout) {
2139 sec = conn->timeout - elapsed;
2142 timeout->tv_sec = sec;
2143 timeout->tv_usec = usec;
2149 * gst_rtsp_connection_reset_timeout:
2150 * @conn: a #GstRTSPConnection
2152 * Reset the timeout of @conn.
2154 * Returns: #GST_RTSP_OK.
2157 gst_rtsp_connection_reset_timeout (GstRTSPConnection * conn)
2159 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2161 g_timer_start (conn->timer);
2167 * gst_rtsp_connection_flush:
2168 * @conn: a #GstRTSPConnection
2169 * @flush: start or stop the flush
2171 * Start or stop the flushing action on @conn. When flushing, all current
2172 * and future actions on @conn will return #GST_RTSP_EINTR until the connection
2173 * is set to non-flushing mode again.
2175 * Returns: #GST_RTSP_OK.
2178 gst_rtsp_connection_flush (GstRTSPConnection * conn, gboolean flush)
2180 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2182 gst_poll_set_flushing (conn->fdset, flush);
2188 * gst_rtsp_connection_set_proxy:
2189 * @conn: a #GstRTSPConnection
2190 * @host: the proxy host
2191 * @port: the proxy port
2193 * Set the proxy host and port.
2195 * Returns: #GST_RTSP_OK.
2200 gst_rtsp_connection_set_proxy (GstRTSPConnection * conn,
2201 const gchar * host, guint port)
2203 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2205 g_free (conn->proxy_host);
2206 conn->proxy_host = g_strdup (host);
2207 conn->proxy_port = port;
2213 * gst_rtsp_connection_set_auth:
2214 * @conn: a #GstRTSPConnection
2215 * @method: authentication method
2217 * @pass: the password
2219 * Configure @conn for authentication mode @method with @user and @pass as the
2220 * user and password respectively.
2222 * Returns: #GST_RTSP_OK.
2225 gst_rtsp_connection_set_auth (GstRTSPConnection * conn,
2226 GstRTSPAuthMethod method, const gchar * user, const gchar * pass)
2228 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2230 if (method == GST_RTSP_AUTH_DIGEST && ((user == NULL || pass == NULL)
2231 || g_strrstr (user, ":") != NULL))
2232 return GST_RTSP_EINVAL;
2234 /* Make sure the username and passwd are being set for authentication */
2235 if (method == GST_RTSP_AUTH_NONE && (user == NULL || pass == NULL))
2236 return GST_RTSP_EINVAL;
2238 /* ":" chars are not allowed in usernames for basic auth */
2239 if (method == GST_RTSP_AUTH_BASIC && g_strrstr (user, ":") != NULL)
2240 return GST_RTSP_EINVAL;
2242 g_free (conn->username);
2243 g_free (conn->passwd);
2245 conn->auth_method = method;
2246 conn->username = g_strdup (user);
2247 conn->passwd = g_strdup (pass);
2254 * @key: ASCII string to hash
2256 * Hashes @key in a case-insensitive manner.
2258 * Returns: the hash code.
2261 str_case_hash (gconstpointer key)
2263 const char *p = key;
2264 guint h = g_ascii_toupper (*p);
2267 for (p += 1; *p != '\0'; p++)
2268 h = (h << 5) - h + g_ascii_toupper (*p);
2275 * @v1: an ASCII string
2276 * @v2: another ASCII string
2278 * Compares @v1 and @v2 in a case-insensitive manner
2280 * Returns: %TRUE if they are equal (modulo case)
2283 str_case_equal (gconstpointer v1, gconstpointer v2)
2285 const char *string1 = v1;
2286 const char *string2 = v2;
2288 return g_ascii_strcasecmp (string1, string2) == 0;
2292 * gst_rtsp_connection_set_auth_param:
2293 * @conn: a #GstRTSPConnection
2294 * @param: authentication directive
2297 * Setup @conn with authentication directives. This is not necesary for
2298 * methods #GST_RTSP_AUTH_NONE and #GST_RTSP_AUTH_BASIC. For
2299 * #GST_RTSP_AUTH_DIGEST, directives should be taken from the digest challenge
2300 * in the WWW-Authenticate response header and can include realm, domain,
2301 * nonce, opaque, stale, algorithm, qop as per RFC2617.
2306 gst_rtsp_connection_set_auth_param (GstRTSPConnection * conn,
2307 const gchar * param, const gchar * value)
2309 g_return_if_fail (conn != NULL);
2310 g_return_if_fail (param != NULL);
2312 if (conn->auth_params == NULL) {
2314 g_hash_table_new_full (str_case_hash, str_case_equal, g_free, g_free);
2316 g_hash_table_insert (conn->auth_params, g_strdup (param), g_strdup (value));
2320 * gst_rtsp_connection_clear_auth_params:
2321 * @conn: a #GstRTSPConnection
2323 * Clear the list of authentication directives stored in @conn.
2328 gst_rtsp_connection_clear_auth_params (GstRTSPConnection * conn)
2330 g_return_if_fail (conn != NULL);
2332 if (conn->auth_params != NULL) {
2333 g_hash_table_destroy (conn->auth_params);
2334 conn->auth_params = NULL;
2338 static GstRTSPResult
2339 set_qos_dscp (gint fd, guint qos_dscp)
2344 struct sockaddr_in6 sa_in6;
2345 struct sockaddr_storage sa_stor;
2347 socklen_t slen = sizeof (sa);
2354 if (getsockname (fd, &sa.sa, &slen) < 0)
2355 goto no_getsockname;
2357 af = sa.sa.sa_family;
2359 /* if this is an IPv4-mapped address then do IPv4 QoS */
2360 if (af == AF_INET6) {
2361 if (IN6_IS_ADDR_V4MAPPED (&sa.sa_in6.sin6_addr))
2365 /* extract and shift 6 bits of the DSCP */
2366 tos = (qos_dscp & 0x3f) << 2;
2370 if (SETSOCKOPT (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0)
2375 if (SETSOCKOPT (fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof (tos)) < 0)
2389 return GST_RTSP_ESYS;
2394 return GST_RTSP_ERROR;
2399 * gst_rtsp_connection_set_qos_dscp:
2400 * @conn: a #GstRTSPConnection
2401 * @qos_dscp: DSCP value
2403 * Configure @conn to use the specified DSCP value.
2405 * Returns: #GST_RTSP_OK on success.
2410 gst_rtsp_connection_set_qos_dscp (GstRTSPConnection * conn, guint qos_dscp)
2414 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2415 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2416 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2418 res = set_qos_dscp (conn->fd0.fd, qos_dscp);
2419 if (res == GST_RTSP_OK)
2420 res = set_qos_dscp (conn->fd1.fd, qos_dscp);
2427 * gst_rtsp_connection_get_url:
2428 * @conn: a #GstRTSPConnection
2430 * Retrieve the URL of the other end of @conn.
2432 * Returns: The URL. This value remains valid until the
2433 * connection is freed.
2438 gst_rtsp_connection_get_url (const GstRTSPConnection * conn)
2440 g_return_val_if_fail (conn != NULL, NULL);
2446 * gst_rtsp_connection_get_ip:
2447 * @conn: a #GstRTSPConnection
2449 * Retrieve the IP address of the other end of @conn.
2451 * Returns: The IP address as a string. this value remains valid until the
2452 * connection is closed.
2457 gst_rtsp_connection_get_ip (const GstRTSPConnection * conn)
2459 g_return_val_if_fail (conn != NULL, NULL);
2465 * gst_rtsp_connection_set_ip:
2466 * @conn: a #GstRTSPConnection
2467 * @ip: an ip address
2469 * Set the IP address of the server.
2474 gst_rtsp_connection_set_ip (GstRTSPConnection * conn, const gchar * ip)
2476 g_return_if_fail (conn != NULL);
2479 conn->ip = g_strdup (ip);
2483 * gst_rtsp_connection_get_readfd:
2484 * @conn: a #GstRTSPConnection
2486 * Get the file descriptor for reading.
2488 * Returns: the file descriptor used for reading or -1 on error. The file
2489 * descriptor remains valid until the connection is closed.
2494 gst_rtsp_connection_get_readfd (const GstRTSPConnection * conn)
2496 g_return_val_if_fail (conn != NULL, -1);
2497 g_return_val_if_fail (conn->readfd != NULL, -1);
2499 return conn->readfd->fd;
2503 * gst_rtsp_connection_get_writefd:
2504 * @conn: a #GstRTSPConnection
2506 * Get the file descriptor for writing.
2508 * Returns: the file descriptor used for writing or -1 on error. The file
2509 * descriptor remains valid until the connection is closed.
2514 gst_rtsp_connection_get_writefd (const GstRTSPConnection * conn)
2516 g_return_val_if_fail (conn != NULL, -1);
2517 g_return_val_if_fail (conn->writefd != NULL, -1);
2519 return conn->writefd->fd;
2524 * gst_rtsp_connection_set_tunneled:
2525 * @conn: a #GstRTSPConnection
2526 * @tunneled: the new state
2528 * Set the HTTP tunneling state of the connection. This must be configured before
2529 * the @conn is connected.
2534 gst_rtsp_connection_set_tunneled (GstRTSPConnection * conn, gboolean tunneled)
2536 g_return_if_fail (conn != NULL);
2537 g_return_if_fail (conn->readfd == NULL);
2538 g_return_if_fail (conn->writefd == NULL);
2540 conn->tunneled = tunneled;
2544 * gst_rtsp_connection_is_tunneled:
2545 * @conn: a #GstRTSPConnection
2547 * Get the tunneling state of the connection.
2549 * Returns: if @conn is using HTTP tunneling.
2554 gst_rtsp_connection_is_tunneled (const GstRTSPConnection * conn)
2556 g_return_val_if_fail (conn != NULL, FALSE);
2558 return conn->tunneled;
2562 * gst_rtsp_connection_get_tunnelid:
2563 * @conn: a #GstRTSPConnection
2565 * Get the tunnel session id the connection.
2567 * Returns: returns a non-empty string if @conn is being tunneled over HTTP.
2572 gst_rtsp_connection_get_tunnelid (const GstRTSPConnection * conn)
2574 g_return_val_if_fail (conn != NULL, NULL);
2576 if (!conn->tunneled)
2579 return conn->tunnelid;
2583 * gst_rtsp_connection_do_tunnel:
2584 * @conn: a #GstRTSPConnection
2585 * @conn2: a #GstRTSPConnection
2587 * If @conn received the first tunnel connection and @conn2 received
2588 * the second tunnel connection, link the two connections together so that
2589 * @conn manages the tunneled connection.
2591 * After this call, @conn2 cannot be used anymore and must be freed with
2592 * gst_rtsp_connection_free().
2594 * Returns: return GST_RTSP_OK on success.
2599 gst_rtsp_connection_do_tunnel (GstRTSPConnection * conn,
2600 GstRTSPConnection * conn2)
2602 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2603 g_return_val_if_fail (conn2 != NULL, GST_RTSP_EINVAL);
2604 g_return_val_if_fail (conn->tstate == TUNNEL_STATE_GET, GST_RTSP_EINVAL);
2605 g_return_val_if_fail (conn2->tstate == TUNNEL_STATE_POST, GST_RTSP_EINVAL);
2606 g_return_val_if_fail (!memcmp (conn2->tunnelid, conn->tunnelid, TUNNELID_LEN),
2609 /* both connections have fd0 as the read/write socket. start by taking the
2610 * socket from conn2 and set it as the socket in conn */
2611 conn->fd1 = conn2->fd0;
2613 /* clean up some of the state of conn2 */
2614 gst_poll_remove_fd (conn2->fdset, &conn2->fd0);
2616 conn2->readfd = conn2->writefd = NULL;
2618 /* We make fd0 the write socket and fd1 the read socket. */
2619 conn->writefd = &conn->fd0;
2620 conn->readfd = &conn->fd1;
2622 conn->tstate = TUNNEL_STATE_COMPLETE;
2624 /* we need base64 decoding for the readfd */
2625 conn->ctx.state = 0;
2629 conn->ctxp = &conn->ctx;
2634 #define READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
2635 #define WRITE_COND (G_IO_OUT | G_IO_ERR)
2643 static GstRTSPRec *queue_response (GstRTSPWatch * watch, GString * str,
2646 /* async functions */
2647 struct _GstRTSPWatch
2651 GstRTSPConnection *conn;
2653 GstRTSPBuilder builder;
2654 GstRTSPMessage message;
2658 gboolean write_added;
2660 /* queued message for transmission */
2661 GAsyncQueue *messages;
2667 GstRTSPWatchFuncs funcs;
2670 GDestroyNotify notify;
2674 gst_rtsp_source_prepare (GSource * source, gint * timeout)
2676 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2678 *timeout = (watch->conn->timeout * 1000);
2684 gst_rtsp_source_check (GSource * source)
2686 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2688 if (watch->readfd.revents & READ_COND)
2691 if (watch->writefd.revents & WRITE_COND)
2698 gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback G_GNUC_UNUSED,
2699 gpointer user_data G_GNUC_UNUSED)
2701 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2704 /* first read as much as we can */
2705 if (watch->readfd.revents & READ_COND) {
2707 res = build_next (&watch->builder, &watch->message, watch->conn);
2708 if (res == GST_RTSP_EINTR)
2710 if (G_UNLIKELY (res == GST_RTSP_EEOF))
2712 if (res == GST_RTSP_ETGET) {
2714 GstRTSPStatusCode code;
2716 if (watch->funcs.tunnel_start)
2717 code = watch->funcs.tunnel_start (watch, watch->user_data);
2719 code = GST_RTSP_STS_OK;
2721 /* queue the response string */
2722 str = gen_tunnel_reply (watch->conn, code);
2723 queue_response (watch, str, UNKNOWN_CSEQ);
2724 } else if (res == GST_RTSP_ETPOST) {
2725 /* in the callback the connection should be tunneled with the
2727 if (watch->funcs.tunnel_complete)
2728 watch->funcs.tunnel_complete (watch, watch->user_data);
2729 } else if (G_UNLIKELY (res != GST_RTSP_OK))
2732 if (G_LIKELY (res == GST_RTSP_OK)) {
2733 if (watch->funcs.message_received)
2734 watch->funcs.message_received (watch, &watch->message,
2737 gst_rtsp_message_unset (&watch->message);
2739 build_reset (&watch->builder);
2743 if (watch->writefd.revents & WRITE_COND) {
2745 if (watch->write_data == NULL) {
2748 /* get a new message from the queue */
2749 data = g_async_queue_try_pop (watch->messages);
2753 watch->write_off = 0;
2754 watch->write_len = data->str->len;
2755 watch->write_data = (guint8 *) g_string_free (data->str, FALSE);
2756 watch->write_cseq = data->cseq;
2759 g_slice_free (GstRTSPRec, data);
2762 res = write_bytes (watch->writefd.fd, watch->write_data,
2763 &watch->write_off, watch->write_len);
2764 if (res == GST_RTSP_EINTR)
2766 if (G_UNLIKELY (res != GST_RTSP_OK))
2769 if (watch->funcs.message_sent && watch->write_cseq != UNKNOWN_CSEQ)
2770 watch->funcs.message_sent (watch, watch->write_cseq, watch->user_data);
2773 if (g_async_queue_length (watch->messages) == 0 && watch->write_added) {
2774 g_source_remove_poll ((GSource *) watch, &watch->writefd);
2775 watch->write_added = FALSE;
2776 watch->writefd.revents = 0;
2778 g_free (watch->write_data);
2779 watch->write_data = NULL;
2788 if (watch->funcs.closed)
2789 watch->funcs.closed (watch, watch->user_data);
2794 if (watch->funcs.error)
2795 watch->funcs.error (watch, res, watch->user_data);
2801 gst_rtsp_rec_free (gpointer data)
2803 GstRTSPRec *rec = data;
2805 g_string_free (rec->str, TRUE);
2807 g_slice_free (GstRTSPRec, rec);
2811 gst_rtsp_source_finalize (GSource * source)
2813 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2815 build_reset (&watch->builder);
2817 g_async_queue_unref (watch->messages);
2818 watch->messages = NULL;
2821 watch->notify (watch->user_data);
2824 static GSourceFuncs gst_rtsp_source_funcs = {
2825 gst_rtsp_source_prepare,
2826 gst_rtsp_source_check,
2827 gst_rtsp_source_dispatch,
2828 gst_rtsp_source_finalize,
2834 * gst_rtsp_watch_new:
2835 * @conn: a #GstRTSPConnection
2836 * @funcs: watch functions
2837 * @user_data: user data to pass to @funcs
2838 * @notify: notify when @user_data is not referenced anymore
2840 * Create a watch object for @conn. The functions provided in @funcs will be
2841 * called with @user_data when activity happened on the watch.
2843 * The new watch is usually created so that it can be attached to a
2844 * maincontext with gst_rtsp_watch_attach().
2846 * @conn must exist for the entire lifetime of the watch.
2848 * Returns: a #GstRTSPWatch that can be used for asynchronous RTSP
2849 * communication. Free with gst_rtsp_watch_unref () after usage.
2854 gst_rtsp_watch_new (GstRTSPConnection * conn,
2855 GstRTSPWatchFuncs * funcs, gpointer user_data, GDestroyNotify notify)
2857 GstRTSPWatch *result;
2859 g_return_val_if_fail (conn != NULL, NULL);
2860 g_return_val_if_fail (funcs != NULL, NULL);
2861 g_return_val_if_fail (conn->readfd != NULL, NULL);
2862 g_return_val_if_fail (conn->writefd != NULL, NULL);
2864 result = (GstRTSPWatch *) g_source_new (&gst_rtsp_source_funcs,
2865 sizeof (GstRTSPWatch));
2867 result->conn = conn;
2868 result->builder.state = STATE_START;
2870 result->messages = g_async_queue_new_full (gst_rtsp_rec_free);
2872 result->readfd.fd = -1;
2873 result->writefd.fd = -1;
2875 gst_rtsp_watch_reset (result);
2877 result->funcs = *funcs;
2878 result->user_data = user_data;
2879 result->notify = notify;
2881 /* only add the read fd, the write fd is only added when we have data
2883 g_source_add_poll ((GSource *) result, &result->readfd);
2889 * gst_rtsp_watch_reset:
2890 * @watch: a #GstRTSPWatch
2892 * Reset @watch, this is usually called after gst_rtsp_connection_do_tunnel()
2893 * when the file descriptors of the connection might have changed.
2898 gst_rtsp_watch_reset (GstRTSPWatch * watch)
2900 if (watch->readfd.fd != -1)
2901 g_source_remove_poll ((GSource *) watch, &watch->readfd);
2902 if (watch->writefd.fd != -1)
2903 g_source_remove_poll ((GSource *) watch, &watch->writefd);
2905 watch->readfd.fd = watch->conn->readfd->fd;
2906 watch->readfd.events = READ_COND;
2907 watch->readfd.revents = 0;
2909 watch->writefd.fd = watch->conn->writefd->fd;
2910 watch->writefd.events = WRITE_COND;
2911 watch->writefd.revents = 0;
2912 watch->write_added = FALSE;
2914 g_source_add_poll ((GSource *) watch, &watch->readfd);
2918 * gst_rtsp_watch_attach:
2919 * @watch: a #GstRTSPWatch
2920 * @context: a GMainContext (if NULL, the default context will be used)
2922 * Adds a #GstRTSPWatch to a context so that it will be executed within that context.
2924 * Returns: the ID (greater than 0) for the watch within the GMainContext.
2929 gst_rtsp_watch_attach (GstRTSPWatch * watch, GMainContext * context)
2931 g_return_val_if_fail (watch != NULL, 0);
2933 return g_source_attach ((GSource *) watch, context);
2937 * gst_rtsp_watch_unref:
2938 * @watch: a #GstRTSPWatch
2940 * Decreases the reference count of @watch by one. If the resulting reference
2941 * count is zero the watch and associated memory will be destroyed.
2946 gst_rtsp_watch_unref (GstRTSPWatch * watch)
2948 g_return_if_fail (watch != NULL);
2950 g_source_unref ((GSource *) watch);
2954 queue_response (GstRTSPWatch * watch, GString * str, guint cseq)
2958 /* make a record with the message as a string ans cseq */
2959 data = g_slice_new (GstRTSPRec);
2963 /* add the record to a queue. FIXME we would like to have an upper limit here */
2964 g_async_queue_push (watch->messages, data);
2966 /* FIXME: does the following need to be made thread-safe? (queue_response
2967 * might be called from a streaming thread, like appsink's render function) */
2968 /* make sure the main context will now also check for writability on the
2970 if (!watch->write_added) {
2971 g_source_add_poll ((GSource *) watch, &watch->writefd);
2972 watch->write_added = TRUE;
2979 * gst_rtsp_watch_queue_message:
2980 * @watch: a #GstRTSPWatch
2981 * @message: a #GstRTSPMessage
2983 * Queue a @message for transmission in @watch. The contents of this
2984 * message will be serialized and transmitted when the connection of the
2985 * watch becomes writable.
2987 * The return value of this function will be returned as the cseq argument in
2988 * the message_sent callback.
2990 * Returns: the sequence number of the message or -1 if the cseq could not be
2996 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 data = queue_response (watch, message_to_string (watch->conn, message), cseq);