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 struct _GstRTSPConnection
164 /* URL for the connection */
167 /* connection state */
174 gchar tunnelid[TUNNELID_LEN];
176 GstRTSPTunnelState tstate;
182 gint cseq; /* sequence number */
183 gchar session_id[512]; /* session id */
184 gint timeout; /* session timeout in seconds */
185 GTimer *timer; /* timeout timer */
188 GstRTSPAuthMethod auth_method;
191 GHashTable *auth_params;
202 inet_aton (const char *c, struct in_addr *paddr)
204 /* note that inet_addr is deprecated on unix because
205 * inet_addr returns -1 (INADDR_NONE) for the valid 255.255.255.255
207 paddr->s_addr = inet_addr (c);
209 if (paddr->s_addr == INADDR_NONE)
226 /* a structure for constructing RTSPMessages */
239 build_reset (GstRTSPBuilder * builder)
241 g_free (builder->body_data);
242 memset (builder, 0, sizeof (GstRTSPBuilder));
246 * gst_rtsp_connection_create:
247 * @url: a #GstRTSPUrl
248 * @conn: storage for a #GstRTSPConnection
250 * Create a newly allocated #GstRTSPConnection from @url and store it in @conn.
251 * The connection will not yet attempt to connect to @url, use
252 * gst_rtsp_connection_connect().
254 * A copy of @url will be made.
256 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
259 gst_rtsp_connection_create (const GstRTSPUrl * url, GstRTSPConnection ** conn)
261 GstRTSPConnection *newconn;
267 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
270 error = WSAStartup (0x0202, &w);
275 if (w.wVersion != 0x0202)
279 newconn = g_new0 (GstRTSPConnection, 1);
281 if ((newconn->fdset = gst_poll_new (TRUE)) == NULL)
284 newconn->url = gst_rtsp_url_copy (url);
285 newconn->fd0.fd = -1;
286 newconn->fd1.fd = -1;
287 newconn->timer = g_timer_new ();
288 newconn->timeout = 60;
291 newconn->auth_method = GST_RTSP_AUTH_NONE;
292 newconn->username = NULL;
293 newconn->passwd = NULL;
294 newconn->auth_params = NULL;
304 g_warning ("Error %d on WSAStartup", error);
305 return GST_RTSP_EWSASTART;
309 g_warning ("Windows sockets are not version 0x202 (current 0x%x)",
312 return GST_RTSP_EWSAVERSION;
321 return GST_RTSP_ESYS;
326 * gst_rtsp_connection_accept:
328 * @conn: storage for a #GstRTSPConnection
330 * Accept a new connection on @sock and create a new #GstRTSPConnection for
331 * handling communication on new socket.
333 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
338 gst_rtsp_connection_accept (gint sock, GstRTSPConnection ** conn)
341 unsigned int address_len;
342 GstRTSPConnection *newconn = NULL;
343 struct sockaddr_in address;
349 address_len = sizeof (address);
350 memset (&address, 0, address_len);
353 fd = accept (sock, (struct sockaddr *) &address, &address_len);
355 fd = accept (sock, (struct sockaddr *) &address, (gint *) & address_len);
356 #endif /* G_OS_WIN32 */
360 /* set to non-blocking mode so that we can cancel the communication */
362 fcntl (fd, F_SETFL, O_NONBLOCK);
364 ioctlsocket (fd, FIONBIO, &flags);
365 #endif /* G_OS_WIN32 */
367 /* create a url for the client address */
368 url = g_new0 (GstRTSPUrl, 1);
369 url->host = g_strdup_printf ("%s", inet_ntoa (address.sin_addr));
370 url->port = address.sin_port;
372 /* now create the connection object */
373 gst_rtsp_connection_create (url, &newconn);
374 gst_rtsp_url_free (url);
376 ADD_POLLFD (newconn->fdset, &newconn->fd0, fd);
378 /* both read and write initially */
379 newconn->readfd = &newconn->fd0;
380 newconn->writefd = &newconn->fd0;
389 return GST_RTSP_ESYS;
394 do_resolve (const gchar * host)
396 struct hostent *hostinfo;
400 struct in_addr *addrp;
403 gchar ipbuf[INET_ADDRSTRLEN];
404 #endif /* G_OS_WIN32 */
408 /* first check if it already is an IP address */
409 if (inet_aton (host, &addr)) {
412 hostinfo = gethostbyname (host);
414 goto not_resolved; /* h_errno set */
416 if (hostinfo->h_addrtype != AF_INET)
417 goto not_ip; /* host not an IP host */
419 addrp = (struct in_addr *) hostinfo->h_addr_list[0];
420 /* this is not threadsafe */
421 ip = inet_ntoa (*addrp);
423 addrs = hostinfo->h_addr_list;
424 ip = inet_ntop (AF_INET, (struct in_addr *) addrs[0], ipbuf,
426 #endif /* G_OS_WIN32 */
428 return g_strdup (ip);
433 GST_ERROR ("could not resolve %s", host);
438 GST_ERROR ("not an IP address");
444 do_connect (const gchar * ip, guint16 port, GstPollFD * fdout,
445 GstPoll * fdset, GTimeVal * timeout)
448 struct sockaddr_in sa_in;
451 unsigned long flags = 1;
452 #endif /* G_OS_WIN32 */
456 memset (&sa_in, 0, sizeof (sa_in));
457 sa_in.sin_family = AF_INET; /* network socket */
458 sa_in.sin_port = htons (port); /* on port */
459 sa_in.sin_addr.s_addr = inet_addr (ip); /* on host ip */
461 fd = socket (AF_INET, SOCK_STREAM, 0);
465 /* set to non-blocking mode so that we can cancel the connect */
467 fcntl (fd, F_SETFL, O_NONBLOCK);
469 ioctlsocket (fd, FIONBIO, &flags);
470 #endif /* G_OS_WIN32 */
472 /* add the socket to our fdset */
473 ADD_POLLFD (fdset, fdout, fd);
475 /* we are going to connect ASYNC now */
476 ret = connect (fd, (struct sockaddr *) &sa_in, sizeof (sa_in));
479 if (!ERRNO_IS_EINPROGRESS)
482 /* wait for connect to complete up to the specified timeout or until we got
484 gst_poll_fd_ctl_write (fdset, fdout, TRUE);
486 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
489 retval = gst_poll_wait (fdset, to);
490 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
494 else if (retval == -1)
497 /* we can still have an error connecting on windows */
498 if (gst_poll_fd_has_error (fdset, fdout)) {
499 socklen_t len = sizeof (errno);
501 getsockopt (fd, SOL_SOCKET, SO_ERROR, &errno, &len);
503 getsockopt (fd, SOL_SOCKET, SO_ERROR, (char *) &errno, &len);
508 gst_poll_fd_ignored (fdset, fdout);
516 GST_ERROR ("no socket %d (%s)", errno, g_strerror (errno));
517 return GST_RTSP_ESYS;
521 GST_ERROR ("system error %d (%s)", errno, g_strerror (errno));
522 REMOVE_POLLFD (fdset, fdout);
523 return GST_RTSP_ESYS;
527 GST_ERROR ("timeout");
528 REMOVE_POLLFD (fdset, fdout);
529 return GST_RTSP_ETIMEOUT;
534 setup_tunneling (GstRTSPConnection * conn, GTimeVal * timeout)
542 gchar *ip, *url_port_str;
543 guint16 port, url_port;
544 gchar codestr[4], *resultstr;
549 /* create a random sessionid */
550 for (i = 0; i < TUNNELID_LEN; i++)
551 conn->tunnelid[i] = g_random_int_range ('a', 'z');
552 conn->tunnelid[TUNNELID_LEN - 1] = '\0';
555 /* get the port from the url */
556 gst_rtsp_url_get_port (url, &url_port);
558 if (conn->proxy_host) {
559 hostparam = g_strdup_printf ("Host: %s:%d\r\n", url->host, url_port);
560 url_port_str = g_strdup_printf (":%d", url_port);
561 ip = conn->proxy_host;
562 port = conn->proxy_port;
571 str = g_strdup_printf ("GET %s%s%s%s%s%s HTTP/1.0\r\n"
573 "x-sessioncookie: %s\r\n"
574 "Accept: application/x-rtsp-tunnelled\r\n"
575 "Pragma: no-cache\r\n"
576 "Cache-Control: no-cache\r\n" "\r\n",
577 conn->proxy_host ? "http://" : "",
578 conn->proxy_host ? url->host : "",
579 conn->proxy_host ? url_port_str : "",
580 url->abspath, url->query ? "?" : "", url->query ? url->query : "",
581 hostparam ? hostparam : "", conn->tunnelid);
583 /* we start by writing to this fd */
584 conn->writefd = &conn->fd0;
586 res = gst_rtsp_connection_write (conn, (guint8 *) str, strlen (str), timeout);
588 if (res != GST_RTSP_OK)
591 gst_poll_fd_ctl_write (conn->fdset, &conn->fd0, FALSE);
592 gst_poll_fd_ctl_read (conn->fdset, &conn->fd0, TRUE);
594 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
602 res = read_line (conn->fd0.fd, buffer, &idx, sizeof (buffer), NULL);
603 if (res == GST_RTSP_EEOF)
605 if (res == GST_RTSP_OK)
607 if (res != GST_RTSP_EINTR)
611 retval = gst_poll_wait (conn->fdset, to);
612 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
614 /* check for timeout */
626 /* check for last line */
627 if (buffer[0] == '\r')
629 if (buffer[0] == '\0')
633 /* first line, parse response */
634 gchar versionstr[20];
637 bptr = (gchar *) buffer;
639 parse_string (versionstr, sizeof (versionstr), &bptr);
640 parse_string (codestr, sizeof (codestr), &bptr);
641 code = atoi (codestr);
643 while (g_ascii_isspace (*bptr))
648 if (code != GST_RTSP_STS_OK)
654 /* other lines, parse key/value */
655 res = parse_key_value (buffer, key, sizeof (key), &value);
656 if (res == GST_RTSP_OK) {
657 /* we got a new ip address */
658 if (g_ascii_strcasecmp (key, "x-server-ip-address") == 0) {
659 if (conn->proxy_host) {
660 /* if we use a proxy we need to change the destination url */
662 url->host = g_strdup (value);
664 g_free (url_port_str);
666 g_strdup_printf ("Host: %s:%d\r\n", url->host, url_port);
667 url_port_str = g_strdup_printf (":%d", url_port);
669 /* and resolve the new ip address */
670 if (!(ip = do_resolve (conn->ip)))
681 /* connect to the host/port */
682 res = do_connect (ip, port, &conn->fd1, conn->fdset, timeout);
683 if (res != GST_RTSP_OK)
686 /* this is now our writing socket */
687 conn->writefd = &conn->fd1;
690 str = g_strdup_printf ("POST %s%s%s%s%s%s HTTP/1.0\r\n"
692 "x-sessioncookie: %s\r\n"
693 "Content-Type: application/x-rtsp-tunnelled\r\n"
694 "Pragma: no-cache\r\n"
695 "Cache-Control: no-cache\r\n"
696 "Content-Length: 32767\r\n"
697 "Expires: Sun, 9 Jan 1972 00:00:00 GMT\r\n"
699 conn->proxy_host ? "http://" : "",
700 conn->proxy_host ? url->host : "",
701 conn->proxy_host ? url_port_str : "",
702 url->abspath, url->query ? "?" : "", url->query ? url->query : "",
703 hostparam ? hostparam : "", conn->tunnelid);
705 res = gst_rtsp_connection_write (conn, (guint8 *) str, strlen (str), timeout);
707 if (res != GST_RTSP_OK)
712 g_free (url_port_str);
719 GST_ERROR ("write failed (%d)", res);
733 res = GST_RTSP_ETIMEOUT;
743 res = GST_RTSP_EINTR;
748 GST_ERROR ("got failure response %d %s", code, resultstr);
749 res = GST_RTSP_ERROR;
754 GST_ERROR ("could not resolve %s", conn->ip);
760 GST_ERROR ("failed to connect");
766 * gst_rtsp_connection_connect:
767 * @conn: a #GstRTSPConnection
768 * @timeout: a #GTimeVal timeout
770 * Attempt to connect to the url of @conn made with
771 * gst_rtsp_connection_create(). If @timeout is #NULL this function can block
772 * forever. If @timeout contains a valid timeout, this function will return
773 * #GST_RTSP_ETIMEOUT after the timeout expired.
775 * This function can be cancelled with gst_rtsp_connection_flush().
777 * Returns: #GST_RTSP_OK when a connection could be made.
780 gst_rtsp_connection_connect (GstRTSPConnection * conn, GTimeVal * timeout)
787 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
788 g_return_val_if_fail (conn->url != NULL, GST_RTSP_EINVAL);
789 g_return_val_if_fail (conn->fd0.fd < 0, GST_RTSP_EINVAL);
793 if (conn->proxy_host && conn->tunneled) {
794 if (!(ip = do_resolve (conn->proxy_host))) {
795 GST_ERROR ("could not resolve %s", conn->proxy_host);
798 port = conn->proxy_port;
799 g_free (conn->proxy_host);
800 conn->proxy_host = ip;
802 if (!(ip = do_resolve (url->host))) {
803 GST_ERROR ("could not resolve %s", url->host);
806 /* get the port from the url */
807 gst_rtsp_url_get_port (url, &port);
813 /* connect to the host/port */
814 res = do_connect (ip, port, &conn->fd0, conn->fdset, timeout);
815 if (res != GST_RTSP_OK)
818 /* this is our read URL */
819 conn->readfd = &conn->fd0;
821 if (conn->tunneled) {
822 res = setup_tunneling (conn, timeout);
823 if (res != GST_RTSP_OK)
824 goto tunneling_failed;
826 conn->writefd = &conn->fd0;
833 return GST_RTSP_ENET;
837 GST_ERROR ("failed to connect");
842 GST_ERROR ("failed to setup tunneling");
848 md5_digest_to_hex_string (unsigned char digest[16], char string[33])
850 static const char hexdigits[] = "0123456789abcdef";
853 for (i = 0; i < 16; i++) {
854 string[i * 2] = hexdigits[(digest[i] >> 4) & 0x0f];
855 string[i * 2 + 1] = hexdigits[digest[i] & 0x0f];
861 auth_digest_compute_hex_urp (const gchar * username,
862 const gchar * realm, const gchar * password, gchar hex_urp[33])
864 struct MD5Context md5_context;
865 unsigned char digest[16];
867 MD5Init (&md5_context);
868 MD5Update (&md5_context, username, strlen (username));
869 MD5Update (&md5_context, ":", 1);
870 MD5Update (&md5_context, realm, strlen (realm));
871 MD5Update (&md5_context, ":", 1);
872 MD5Update (&md5_context, password, strlen (password));
873 MD5Final (digest, &md5_context);
874 md5_digest_to_hex_string (digest, hex_urp);
878 auth_digest_compute_response (const gchar * method,
879 const gchar * uri, const gchar * hex_a1, const gchar * nonce,
883 struct MD5Context md5_context;
884 unsigned char digest[16];
887 MD5Init (&md5_context);
888 MD5Update (&md5_context, method, strlen (method));
889 MD5Update (&md5_context, ":", 1);
890 MD5Update (&md5_context, uri, strlen (uri));
891 MD5Final (digest, &md5_context);
892 md5_digest_to_hex_string (digest, hex_a2);
895 MD5Init (&md5_context);
896 MD5Update (&md5_context, hex_a1, strlen (hex_a1));
897 MD5Update (&md5_context, ":", 1);
898 MD5Update (&md5_context, nonce, strlen (nonce));
899 MD5Update (&md5_context, ":", 1);
901 MD5Update (&md5_context, hex_a2, 32);
902 MD5Final (digest, &md5_context);
903 md5_digest_to_hex_string (digest, response);
907 add_auth_header (GstRTSPConnection * conn, GstRTSPMessage * message)
909 switch (conn->auth_method) {
910 case GST_RTSP_AUTH_BASIC:{
915 user_pass = g_strdup_printf ("%s:%s", conn->username, conn->passwd);
916 user_pass64 = g_base64_encode ((guchar *) user_pass, strlen (user_pass));
917 auth_string = g_strdup_printf ("Basic %s", user_pass64);
919 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
923 g_free (user_pass64);
926 case GST_RTSP_AUTH_DIGEST:{
927 gchar response[33], hex_urp[33];
928 gchar *auth_string, *auth_string2;
935 /* we need to have some params set */
936 if (conn->auth_params == NULL)
939 /* we need the realm and nonce */
940 realm = (gchar *) g_hash_table_lookup (conn->auth_params, "realm");
941 nonce = (gchar *) g_hash_table_lookup (conn->auth_params, "nonce");
942 if (realm == NULL || nonce == NULL)
945 auth_digest_compute_hex_urp (conn->username, realm, conn->passwd,
948 method = gst_rtsp_method_as_text (message->type_data.request.method);
949 uri = message->type_data.request.uri;
951 /* Assume no qop, algorithm=md5, stale=false */
952 /* For algorithm MD5, a1 = urp. */
953 auth_digest_compute_response (method, uri, hex_urp, nonce, response);
954 auth_string = g_strdup_printf ("Digest username=\"%s\", "
955 "realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"",
956 conn->username, realm, nonce, uri, response);
958 opaque = (gchar *) g_hash_table_lookup (conn->auth_params, "opaque");
960 auth_string2 = g_strdup_printf ("%s, opaque=\"%s\"", auth_string,
962 g_free (auth_string);
963 auth_string = auth_string2;
965 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
976 gen_date_string (gchar * date_string, guint len)
984 g_get_current_time (&tv);
985 t = (time_t) tv.tv_sec;
988 strftime (date_string, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime_r (&t, &tm_));
990 strftime (date_string, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime (&t));
995 write_bytes (gint fd, const guint8 * buffer, guint * idx, guint size)
999 if (G_UNLIKELY (*idx > size))
1000 return GST_RTSP_ERROR;
1007 r = WRITE_SOCKET (fd, &buffer[*idx], left);
1008 if (G_UNLIKELY (r == 0)) {
1009 return GST_RTSP_EINTR;
1010 } else if (G_UNLIKELY (r < 0)) {
1011 if (ERRNO_IS_EAGAIN)
1012 return GST_RTSP_EINTR;
1013 if (!ERRNO_IS_EINTR)
1014 return GST_RTSP_ESYS;
1024 fill_bytes (gint fd, guint8 * buffer, guint size, DecodeCtx * ctx)
1032 while (size > 0 && ctx->cout < ctx->coutl) {
1033 /* we have some leftover bytes */
1034 *buffer++ = ctx->out[ctx->cout];
1039 /* nothing in the buffer */
1043 /* try to read more bytes */
1044 r = READ_SOCKET (fd, &ctx->in[ctx->cin], 4 - ctx->cin);
1052 if (ctx->cin == 4) {
1053 r = g_base64_decode_step ((const gchar *) ctx->in, 4,
1054 (guchar *) ctx->out, &ctx->state, &ctx->save);
1061 out = READ_SOCKET (fd, buffer, size);
1067 static GstRTSPResult
1068 read_bytes (gint fd, guint8 * buffer, guint * idx, guint size, DecodeCtx * ctx)
1072 if (G_UNLIKELY (*idx > size))
1073 return GST_RTSP_ERROR;
1080 r = fill_bytes (fd, &buffer[*idx], left, ctx);
1081 if (G_UNLIKELY (r == 0)) {
1082 return GST_RTSP_EEOF;
1083 } else if (G_UNLIKELY (r < 0)) {
1084 if (ERRNO_IS_EAGAIN)
1085 return GST_RTSP_EINTR;
1086 if (!ERRNO_IS_EINTR)
1087 return GST_RTSP_ESYS;
1096 static GstRTSPResult
1097 read_line (gint fd, guint8 * buffer, guint * idx, guint size, DecodeCtx * ctx)
1103 r = fill_bytes (fd, &c, 1, ctx);
1104 if (G_UNLIKELY (r == 0)) {
1105 return GST_RTSP_EEOF;
1106 } else if (G_UNLIKELY (r < 0)) {
1107 if (ERRNO_IS_EAGAIN)
1108 return GST_RTSP_EINTR;
1109 if (!ERRNO_IS_EINTR)
1110 return GST_RTSP_ESYS;
1112 if (c == '\n') /* end on \n */
1114 if (c == '\r') /* ignore \r */
1117 if (G_LIKELY (*idx < size - 1))
1118 buffer[(*idx)++] = c;
1121 buffer[*idx] = '\0';
1127 * gst_rtsp_connection_write:
1128 * @conn: a #GstRTSPConnection
1129 * @data: the data to write
1130 * @size: the size of @data
1131 * @timeout: a timeout value or #NULL
1133 * Attempt to write @size bytes of @data to the connected @conn, blocking up to
1134 * the specified @timeout. @timeout can be #NULL, in which case this function
1135 * might block forever.
1137 * This function can be cancelled with gst_rtsp_connection_flush().
1139 * Returns: #GST_RTSP_OK on success.
1142 gst_rtsp_connection_write (GstRTSPConnection * conn, const guint8 * data,
1143 guint size, GTimeVal * timeout)
1150 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1151 g_return_val_if_fail (data != NULL || size == 0, GST_RTSP_EINVAL);
1152 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
1154 gst_poll_set_controllable (conn->fdset, TRUE);
1155 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, TRUE);
1156 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, FALSE);
1157 /* clear all previous poll results */
1158 gst_poll_fd_ignored (conn->fdset, conn->writefd);
1159 gst_poll_fd_ignored (conn->fdset, conn->readfd);
1161 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1167 res = write_bytes (conn->writefd->fd, data, &offset, size);
1168 if (G_LIKELY (res == GST_RTSP_OK))
1170 if (G_UNLIKELY (res != GST_RTSP_EINTR))
1173 /* not all is written, wait until we can write more */
1175 retval = gst_poll_wait (conn->fdset, to);
1176 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1178 if (G_UNLIKELY (retval == 0))
1181 if (G_UNLIKELY (retval == -1)) {
1193 return GST_RTSP_ETIMEOUT;
1197 return GST_RTSP_ESYS;
1201 return GST_RTSP_EINTR;
1210 message_to_string (GstRTSPConnection * conn, GstRTSPMessage * message)
1212 GString *str = NULL;
1214 str = g_string_new ("");
1216 switch (message->type) {
1217 case GST_RTSP_MESSAGE_REQUEST:
1218 /* create request string, add CSeq */
1219 g_string_append_printf (str, "%s %s RTSP/1.0\r\n"
1221 gst_rtsp_method_as_text (message->type_data.request.method),
1222 message->type_data.request.uri, conn->cseq++);
1223 /* add session id if we have one */
1224 if (conn->session_id[0] != '\0') {
1225 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SESSION,
1228 /* add any authentication headers */
1229 add_auth_header (conn, message);
1231 case GST_RTSP_MESSAGE_RESPONSE:
1232 /* create response string */
1233 g_string_append_printf (str, "RTSP/1.0 %d %s\r\n",
1234 message->type_data.response.code, message->type_data.response.reason);
1236 case GST_RTSP_MESSAGE_DATA:
1238 guint8 data_header[4];
1240 /* prepare data header */
1241 data_header[0] = '$';
1242 data_header[1] = message->type_data.data.channel;
1243 data_header[2] = (message->body_size >> 8) & 0xff;
1244 data_header[3] = message->body_size & 0xff;
1246 /* create string with header and data */
1247 str = g_string_append_len (str, (gchar *) data_header, 4);
1249 g_string_append_len (str, (gchar *) message->body,
1250 message->body_size);
1254 g_string_free (str, TRUE);
1255 g_return_val_if_reached (NULL);
1259 /* append headers and body */
1260 if (message->type != GST_RTSP_MESSAGE_DATA) {
1261 gchar date_string[100];
1263 gen_date_string (date_string, sizeof (date_string));
1265 /* add date header */
1266 gst_rtsp_message_add_header (message, GST_RTSP_HDR_DATE, date_string);
1268 /* append headers */
1269 gst_rtsp_message_append_headers (message, str);
1271 /* append Content-Length and body if needed */
1272 if (message->body != NULL && message->body_size > 0) {
1275 len = g_strdup_printf ("%d", message->body_size);
1276 g_string_append_printf (str, "%s: %s\r\n",
1277 gst_rtsp_header_as_text (GST_RTSP_HDR_CONTENT_LENGTH), len);
1279 /* header ends here */
1280 g_string_append (str, "\r\n");
1282 g_string_append_len (str, (gchar *) message->body,
1283 message->body_size);
1285 /* just end headers */
1286 g_string_append (str, "\r\n");
1294 * gst_rtsp_connection_send:
1295 * @conn: a #GstRTSPConnection
1296 * @message: the message to send
1297 * @timeout: a timeout value or #NULL
1299 * Attempt to send @message to the connected @conn, blocking up to
1300 * the specified @timeout. @timeout can be #NULL, in which case this function
1301 * might block forever.
1303 * This function can be cancelled with gst_rtsp_connection_flush().
1305 * Returns: #GST_RTSP_OK on success.
1308 gst_rtsp_connection_send (GstRTSPConnection * conn, GstRTSPMessage * message,
1311 GString *string = NULL;
1316 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1317 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1319 if (G_UNLIKELY (!(string = message_to_string (conn, message))))
1322 if (conn->tunneled) {
1323 str = g_base64_encode ((const guchar *) string->str, string->len);
1324 g_string_free (string, TRUE);
1329 g_string_free (string, FALSE);
1333 res = gst_rtsp_connection_write (conn, (guint8 *) str, len, timeout);
1341 g_warning ("Wrong message");
1342 return GST_RTSP_EINVAL;
1347 parse_string (gchar * dest, gint size, gchar ** src)
1353 while (g_ascii_isspace (**src))
1356 while (!g_ascii_isspace (**src) && **src != '\0') {
1358 dest[idx++] = **src;
1366 parse_key (gchar * dest, gint size, gchar ** src)
1371 while (**src != ':' && **src != '\0') {
1373 dest[idx++] = **src;
1380 static GstRTSPResult
1381 parse_response_status (guint8 * buffer, GstRTSPMessage * msg)
1384 gchar versionstr[20];
1389 bptr = (gchar *) buffer;
1391 parse_string (versionstr, sizeof (versionstr), &bptr);
1392 parse_string (codestr, sizeof (codestr), &bptr);
1393 code = atoi (codestr);
1395 while (g_ascii_isspace (*bptr))
1398 if (strcmp (versionstr, "RTSP/1.0") == 0)
1399 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
1401 else if (strncmp (versionstr, "RTSP/", 5) == 0) {
1402 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
1404 msg->type_data.response.version = GST_RTSP_VERSION_INVALID;
1412 return GST_RTSP_EPARSE;
1416 static GstRTSPResult
1417 parse_request_line (GstRTSPConnection * conn, guint8 * buffer,
1418 GstRTSPMessage * msg)
1420 GstRTSPResult res = GST_RTSP_OK;
1421 gchar versionstr[20];
1422 gchar methodstr[20];
1425 GstRTSPMethod method;
1426 GstRTSPTunnelState tstate = TUNNEL_STATE_NONE;
1428 bptr = (gchar *) buffer;
1430 parse_string (methodstr, sizeof (methodstr), &bptr);
1431 method = gst_rtsp_find_method (methodstr);
1432 if (method == GST_RTSP_INVALID) {
1433 /* a tunnel request is allowed when we don't have one yet */
1434 if (conn->tstate != TUNNEL_STATE_NONE)
1435 goto invalid_method;
1436 /* we need GET or POST for a valid tunnel request */
1437 if (!strcmp (methodstr, "GET"))
1438 tstate = TUNNEL_STATE_GET;
1439 else if (!strcmp (methodstr, "POST"))
1440 tstate = TUNNEL_STATE_POST;
1442 goto invalid_method;
1445 parse_string (urlstr, sizeof (urlstr), &bptr);
1446 if (G_UNLIKELY (*urlstr == '\0'))
1449 parse_string (versionstr, sizeof (versionstr), &bptr);
1451 if (G_UNLIKELY (*bptr != '\0'))
1452 goto invalid_version;
1454 if (strcmp (versionstr, "RTSP/1.0") == 0) {
1455 res = gst_rtsp_message_init_request (msg, method, urlstr);
1456 } else if (strncmp (versionstr, "RTSP/", 5) == 0) {
1457 res = gst_rtsp_message_init_request (msg, method, urlstr);
1458 msg->type_data.request.version = GST_RTSP_VERSION_INVALID;
1459 } else if (strcmp (versionstr, "HTTP/1.0") == 0) {
1460 /* tunnel request, we need a tunnel method */
1461 if (tstate == TUNNEL_STATE_NONE) {
1462 res = GST_RTSP_EPARSE;
1464 conn->tstate = tstate;
1467 res = GST_RTSP_EPARSE;
1475 GST_ERROR ("invalid method %s", methodstr);
1476 return GST_RTSP_EPARSE;
1480 GST_ERROR ("invalid url %s", urlstr);
1481 return GST_RTSP_EPARSE;
1485 GST_ERROR ("invalid version");
1486 return GST_RTSP_EPARSE;
1490 static GstRTSPResult
1491 parse_key_value (guint8 * buffer, gchar * key, guint keysize, gchar ** value)
1495 bptr = (gchar *) buffer;
1498 parse_key (key, keysize, &bptr);
1499 if (G_UNLIKELY (*bptr != ':'))
1503 while (g_ascii_isspace (*bptr))
1513 return GST_RTSP_EPARSE;
1517 /* parsing lines means reading a Key: Value pair */
1518 static GstRTSPResult
1519 parse_line (GstRTSPConnection * conn, guint8 * buffer, GstRTSPMessage * msg)
1524 GstRTSPHeaderField field;
1526 res = parse_key_value (buffer, key, sizeof (key), &value);
1527 if (G_UNLIKELY (res != GST_RTSP_OK))
1530 if (conn->tstate == TUNNEL_STATE_GET || conn->tstate == TUNNEL_STATE_POST) {
1531 /* save the tunnel session in the connection */
1532 if (!strcmp (key, "x-sessioncookie")) {
1533 strncpy (conn->tunnelid, value, TUNNELID_LEN);
1534 conn->tunnelid[TUNNELID_LEN - 1] = '\0';
1535 conn->tunneled = TRUE;
1538 field = gst_rtsp_find_header_field (key);
1539 if (field != GST_RTSP_HDR_INVALID)
1540 gst_rtsp_message_add_header (msg, field, value);
1553 * GST_RTSP_OK when a complete message was read.
1554 * GST_RTSP_EEOF: when the socket is closed
1555 * GST_RTSP_EINTR: when more data is needed.
1556 * GST_RTSP_..: some other error occured.
1558 static GstRTSPResult
1559 build_next (GstRTSPBuilder * builder, GstRTSPMessage * message,
1560 GstRTSPConnection * conn)
1565 switch (builder->state) {
1567 builder->offset = 0;
1569 read_bytes (conn->readfd->fd, (guint8 *) builder->buffer,
1570 &builder->offset, 1, conn->ctxp);
1571 if (res != GST_RTSP_OK)
1574 /* we have 1 bytes now and we can see if this is a data message or
1576 if (builder->buffer[0] == '$') {
1577 /* data message, prepare for the header */
1578 builder->state = STATE_DATA_HEADER;
1581 builder->state = STATE_READ_LINES;
1584 case STATE_DATA_HEADER:
1587 read_bytes (conn->readfd->fd, (guint8 *) builder->buffer,
1588 &builder->offset, 4, conn->ctxp);
1589 if (res != GST_RTSP_OK)
1592 gst_rtsp_message_init_data (message, builder->buffer[1]);
1594 builder->body_len = (builder->buffer[2] << 8) | builder->buffer[3];
1595 builder->body_data = g_malloc (builder->body_len + 1);
1596 builder->body_data[builder->body_len] = '\0';
1597 builder->offset = 0;
1598 builder->state = STATE_DATA_BODY;
1601 case STATE_DATA_BODY:
1604 read_bytes (conn->readfd->fd, builder->body_data, &builder->offset,
1605 builder->body_len, conn->ctxp);
1606 if (res != GST_RTSP_OK)
1609 /* we have the complete body now, store in the message adjusting the
1610 * length to include the traling '\0' */
1611 gst_rtsp_message_take_body (message,
1612 (guint8 *) builder->body_data, builder->body_len + 1);
1613 builder->body_data = NULL;
1614 builder->body_len = 0;
1616 builder->state = STATE_END;
1619 case STATE_READ_LINES:
1621 res = read_line (conn->readfd->fd, builder->buffer, &builder->offset,
1622 sizeof (builder->buffer), conn->ctxp);
1623 if (res != GST_RTSP_OK)
1626 /* we have a regular response */
1627 if (builder->buffer[0] == '\r') {
1628 builder->buffer[0] = '\0';
1631 if (builder->buffer[0] == '\0') {
1634 /* empty line, end of message header */
1635 /* see if there is a Content-Length header */
1636 if (gst_rtsp_message_get_header (message,
1637 GST_RTSP_HDR_CONTENT_LENGTH, &hdrval, 0) == GST_RTSP_OK) {
1638 /* there is, prepare to read the body */
1639 builder->body_len = atol (hdrval);
1640 builder->body_data = g_malloc (builder->body_len + 1);
1641 builder->body_data[builder->body_len] = '\0';
1642 builder->offset = 0;
1643 builder->state = STATE_DATA_BODY;
1645 builder->state = STATE_END;
1650 /* we have a line */
1651 if (builder->line == 0) {
1652 /* first line, check for response status */
1653 if (memcmp (builder->buffer, "RTSP", 4) == 0) {
1654 res = parse_response_status (builder->buffer, message);
1656 res = parse_request_line (conn, builder->buffer, message);
1658 /* the first line must parse without errors */
1659 if (res != GST_RTSP_OK)
1662 /* else just parse the line, ignore errors */
1663 parse_line (conn, builder->buffer, message);
1666 builder->offset = 0;
1673 if (conn->tstate == TUNNEL_STATE_GET) {
1674 res = GST_RTSP_ETGET;
1676 } else if (conn->tstate == TUNNEL_STATE_POST) {
1677 res = GST_RTSP_ETPOST;
1681 if (message->type == GST_RTSP_MESSAGE_DATA) {
1682 /* data messages don't have headers */
1687 /* save session id in the connection for further use */
1688 if (gst_rtsp_message_get_header (message, GST_RTSP_HDR_SESSION,
1689 &session_id, 0) == GST_RTSP_OK) {
1692 maxlen = sizeof (conn->session_id) - 1;
1693 /* the sessionid can have attributes marked with ;
1694 * Make sure we strip them */
1695 for (i = 0; session_id[i] != '\0'; i++) {
1696 if (session_id[i] == ';') {
1701 } while (g_ascii_isspace (session_id[i]));
1702 if (g_str_has_prefix (&session_id[i], "timeout=")) {
1705 /* if we parsed something valid, configure */
1706 if ((to = atoi (&session_id[i + 8])) > 0)
1713 /* make sure to not overflow */
1714 strncpy (conn->session_id, session_id, maxlen);
1715 conn->session_id[maxlen] = '\0';
1721 res = GST_RTSP_ERROR;
1730 * gst_rtsp_connection_read:
1731 * @conn: a #GstRTSPConnection
1732 * @data: the data to read
1733 * @size: the size of @data
1734 * @timeout: a timeout value or #NULL
1736 * Attempt to read @size bytes into @data from the connected @conn, blocking up to
1737 * the specified @timeout. @timeout can be #NULL, in which case this function
1738 * might block forever.
1740 * This function can be cancelled with gst_rtsp_connection_flush().
1742 * Returns: #GST_RTSP_OK on success.
1745 gst_rtsp_connection_read (GstRTSPConnection * conn, guint8 * data, guint size,
1753 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1754 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
1755 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1757 if (G_UNLIKELY (size == 0))
1762 /* configure timeout if any */
1763 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1765 gst_poll_set_controllable (conn->fdset, TRUE);
1766 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
1767 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
1770 res = read_bytes (conn->readfd->fd, data, &offset, size, conn->ctxp);
1771 if (G_UNLIKELY (res == GST_RTSP_EEOF))
1773 if (G_LIKELY (res == GST_RTSP_OK))
1775 if (G_UNLIKELY (res != GST_RTSP_EINTR))
1779 retval = gst_poll_wait (conn->fdset, to);
1780 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1782 /* check for timeout */
1783 if (G_UNLIKELY (retval == 0))
1784 goto select_timeout;
1786 if (G_UNLIKELY (retval == -1)) {
1792 gst_poll_set_controllable (conn->fdset, FALSE);
1799 return GST_RTSP_ESYS;
1803 return GST_RTSP_ETIMEOUT;
1807 return GST_RTSP_EINTR;
1811 return GST_RTSP_EEOF;
1820 gen_tunnel_reply (GstRTSPConnection * conn, GstRTSPStatusCode code)
1823 gchar date_string[100];
1824 const gchar *status;
1826 gen_date_string (date_string, sizeof (date_string));
1828 status = gst_rtsp_status_as_text (code);
1829 if (status == NULL) {
1830 code = GST_RTSP_STS_INTERNAL_SERVER_ERROR;
1831 status = "Internal Server Error";
1834 str = g_string_new ("");
1837 g_string_append_printf (str, "HTTP/1.0 %d %s\r\n", code, status);
1838 g_string_append_printf (str,
1839 "Server: GStreamer RTSP Server\r\n"
1841 "Connection: close\r\n"
1842 "Cache-Control: no-store\r\n" "Pragma: no-cache\r\n", date_string);
1843 if (code == GST_RTSP_STS_OK) {
1845 g_string_append_printf (str, "x-server-ip-address: %s\r\n", conn->ip);
1846 g_string_append_printf (str,
1847 "Content-Type: application/x-rtsp-tunnelled\r\n");
1849 g_string_append_printf (str, "\r\n");
1854 * gst_rtsp_connection_receive:
1855 * @conn: a #GstRTSPConnection
1856 * @message: the message to read
1857 * @timeout: a timeout value or #NULL
1859 * Attempt to read into @message from the connected @conn, blocking up to
1860 * the specified @timeout. @timeout can be #NULL, in which case this function
1861 * might block forever.
1863 * This function can be cancelled with gst_rtsp_connection_flush().
1865 * Returns: #GST_RTSP_OK on success.
1868 gst_rtsp_connection_receive (GstRTSPConnection * conn, GstRTSPMessage * message,
1872 GstRTSPBuilder builder;
1876 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1877 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1878 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1880 /* configure timeout if any */
1881 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1883 gst_poll_set_controllable (conn->fdset, TRUE);
1884 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
1885 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
1887 memset (&builder, 0, sizeof (GstRTSPBuilder));
1889 res = build_next (&builder, message, conn);
1890 if (G_UNLIKELY (res == GST_RTSP_EEOF))
1892 if (G_LIKELY (res == GST_RTSP_OK))
1894 if (res == GST_RTSP_ETGET) {
1897 /* tunnel GET request, we can reply now */
1898 str = gen_tunnel_reply (conn, GST_RTSP_STS_OK);
1900 gst_rtsp_connection_write (conn, (guint8 *) str->str, str->len,
1902 g_string_free (str, TRUE);
1903 } else if (res == GST_RTSP_ETPOST) {
1904 /* tunnel POST request, return the value, the caller now has to link the
1905 * two connections. */
1907 } else if (G_UNLIKELY (res != GST_RTSP_EINTR))
1911 retval = gst_poll_wait (conn->fdset, to);
1912 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1914 /* check for timeout */
1915 if (G_UNLIKELY (retval == 0))
1916 goto select_timeout;
1918 if (G_UNLIKELY (retval == -1)) {
1924 gst_poll_set_controllable (conn->fdset, FALSE);
1927 /* we have a message here */
1928 build_reset (&builder);
1935 res = GST_RTSP_ESYS;
1940 res = GST_RTSP_ETIMEOUT;
1945 res = GST_RTSP_EINTR;
1950 res = GST_RTSP_EEOF;
1956 build_reset (&builder);
1957 gst_rtsp_message_unset (message);
1963 * gst_rtsp_connection_close:
1964 * @conn: a #GstRTSPConnection
1966 * Close the connected @conn. After this call, the connection is in the same
1967 * state as when it was first created.
1969 * Returns: #GST_RTSP_OK on success.
1972 gst_rtsp_connection_close (GstRTSPConnection * conn)
1974 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1979 REMOVE_POLLFD (conn->fdset, &conn->fd0);
1980 REMOVE_POLLFD (conn->fdset, &conn->fd1);
1981 conn->writefd = NULL;
1982 conn->readfd = NULL;
1983 conn->tunneled = FALSE;
1984 conn->tstate = TUNNEL_STATE_NONE;
1986 g_free (conn->username);
1987 conn->username = NULL;
1988 g_free (conn->passwd);
1989 conn->passwd = NULL;
1990 gst_rtsp_connection_clear_auth_params (conn);
1993 conn->session_id[0] = '\0';
1999 * gst_rtsp_connection_free:
2000 * @conn: a #GstRTSPConnection
2002 * Close and free @conn.
2004 * Returns: #GST_RTSP_OK on success.
2007 gst_rtsp_connection_free (GstRTSPConnection * conn)
2011 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2013 res = gst_rtsp_connection_close (conn);
2014 gst_poll_free (conn->fdset);
2015 g_timer_destroy (conn->timer);
2016 gst_rtsp_url_free (conn->url);
2017 g_free (conn->proxy_host);
2027 * gst_rtsp_connection_poll:
2028 * @conn: a #GstRTSPConnection
2029 * @events: a bitmask of #GstRTSPEvent flags to check
2030 * @revents: location for result flags
2031 * @timeout: a timeout
2033 * Wait up to the specified @timeout for the connection to become available for
2034 * at least one of the operations specified in @events. When the function returns
2035 * with #GST_RTSP_OK, @revents will contain a bitmask of available operations on
2038 * @timeout can be #NULL, in which case this function might block forever.
2040 * This function can be cancelled with gst_rtsp_connection_flush().
2042 * Returns: #GST_RTSP_OK on success.
2047 gst_rtsp_connection_poll (GstRTSPConnection * conn, GstRTSPEvent events,
2048 GstRTSPEvent * revents, GTimeVal * timeout)
2053 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2054 g_return_val_if_fail (events != 0, GST_RTSP_EINVAL);
2055 g_return_val_if_fail (revents != NULL, GST_RTSP_EINVAL);
2056 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2057 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2059 gst_poll_set_controllable (conn->fdset, TRUE);
2061 /* add fd to writer set when asked to */
2062 gst_poll_fd_ctl_write (conn->fdset, conn->writefd,
2063 events & GST_RTSP_EV_WRITE);
2065 /* add fd to reader set when asked to */
2066 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, events & GST_RTSP_EV_READ);
2068 /* configure timeout if any */
2069 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
2072 retval = gst_poll_wait (conn->fdset, to);
2073 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
2075 if (G_UNLIKELY (retval == 0))
2076 goto select_timeout;
2078 if (G_UNLIKELY (retval == -1)) {
2086 if (events & GST_RTSP_EV_READ) {
2087 if (gst_poll_fd_can_read (conn->fdset, conn->readfd))
2088 *revents |= GST_RTSP_EV_READ;
2090 if (events & GST_RTSP_EV_WRITE) {
2091 if (gst_poll_fd_can_write (conn->fdset, conn->writefd))
2092 *revents |= GST_RTSP_EV_WRITE;
2099 return GST_RTSP_ETIMEOUT;
2103 return GST_RTSP_ESYS;
2107 return GST_RTSP_EINTR;
2112 * gst_rtsp_connection_next_timeout:
2113 * @conn: a #GstRTSPConnection
2114 * @timeout: a timeout
2116 * Calculate the next timeout for @conn, storing the result in @timeout.
2118 * Returns: #GST_RTSP_OK.
2121 gst_rtsp_connection_next_timeout (GstRTSPConnection * conn, GTimeVal * timeout)
2127 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2128 g_return_val_if_fail (timeout != NULL, GST_RTSP_EINVAL);
2130 elapsed = g_timer_elapsed (conn->timer, &usec);
2131 if (elapsed >= conn->timeout) {
2135 sec = conn->timeout - elapsed;
2138 timeout->tv_sec = sec;
2139 timeout->tv_usec = usec;
2145 * gst_rtsp_connection_reset_timeout:
2146 * @conn: a #GstRTSPConnection
2148 * Reset the timeout of @conn.
2150 * Returns: #GST_RTSP_OK.
2153 gst_rtsp_connection_reset_timeout (GstRTSPConnection * conn)
2155 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2157 g_timer_start (conn->timer);
2163 * gst_rtsp_connection_flush:
2164 * @conn: a #GstRTSPConnection
2165 * @flush: start or stop the flush
2167 * Start or stop the flushing action on @conn. When flushing, all current
2168 * and future actions on @conn will return #GST_RTSP_EINTR until the connection
2169 * is set to non-flushing mode again.
2171 * Returns: #GST_RTSP_OK.
2174 gst_rtsp_connection_flush (GstRTSPConnection * conn, gboolean flush)
2176 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2178 gst_poll_set_flushing (conn->fdset, flush);
2184 * gst_rtsp_connection_set_proxy:
2185 * @conn: a #GstRTSPConnection
2186 * @host: the proxy host
2187 * @port: the proxy port
2189 * Set the proxy host and port.
2191 * Returns: #GST_RTSP_OK.
2196 gst_rtsp_connection_set_proxy (GstRTSPConnection * conn,
2197 const gchar * host, guint port)
2199 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2201 g_free (conn->proxy_host);
2202 conn->proxy_host = g_strdup (host);
2203 conn->proxy_port = port;
2209 * gst_rtsp_connection_set_auth:
2210 * @conn: a #GstRTSPConnection
2211 * @method: authentication method
2213 * @pass: the password
2215 * Configure @conn for authentication mode @method with @user and @pass as the
2216 * user and password respectively.
2218 * Returns: #GST_RTSP_OK.
2221 gst_rtsp_connection_set_auth (GstRTSPConnection * conn,
2222 GstRTSPAuthMethod method, const gchar * user, const gchar * pass)
2224 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2226 if (method == GST_RTSP_AUTH_DIGEST && ((user == NULL || pass == NULL)
2227 || g_strrstr (user, ":") != NULL))
2228 return GST_RTSP_EINVAL;
2230 /* Make sure the username and passwd are being set for authentication */
2231 if (method == GST_RTSP_AUTH_NONE && (user == NULL || pass == NULL))
2232 return GST_RTSP_EINVAL;
2234 /* ":" chars are not allowed in usernames for basic auth */
2235 if (method == GST_RTSP_AUTH_BASIC && g_strrstr (user, ":") != NULL)
2236 return GST_RTSP_EINVAL;
2238 g_free (conn->username);
2239 g_free (conn->passwd);
2241 conn->auth_method = method;
2242 conn->username = g_strdup (user);
2243 conn->passwd = g_strdup (pass);
2250 * @key: ASCII string to hash
2252 * Hashes @key in a case-insensitive manner.
2254 * Returns: the hash code.
2257 str_case_hash (gconstpointer key)
2259 const char *p = key;
2260 guint h = g_ascii_toupper (*p);
2263 for (p += 1; *p != '\0'; p++)
2264 h = (h << 5) - h + g_ascii_toupper (*p);
2271 * @v1: an ASCII string
2272 * @v2: another ASCII string
2274 * Compares @v1 and @v2 in a case-insensitive manner
2276 * Returns: %TRUE if they are equal (modulo case)
2279 str_case_equal (gconstpointer v1, gconstpointer v2)
2281 const char *string1 = v1;
2282 const char *string2 = v2;
2284 return g_ascii_strcasecmp (string1, string2) == 0;
2288 * gst_rtsp_connection_set_auth_param:
2289 * @conn: a #GstRTSPConnection
2290 * @param: authentication directive
2293 * Setup @conn with authentication directives. This is not necesary for
2294 * methods #GST_RTSP_AUTH_NONE and #GST_RTSP_AUTH_BASIC. For
2295 * #GST_RTSP_AUTH_DIGEST, directives should be taken from the digest challenge
2296 * in the WWW-Authenticate response header and can include realm, domain,
2297 * nonce, opaque, stale, algorithm, qop as per RFC2617.
2302 gst_rtsp_connection_set_auth_param (GstRTSPConnection * conn,
2303 const gchar * param, const gchar * value)
2305 g_return_if_fail (conn != NULL);
2306 g_return_if_fail (param != NULL);
2308 if (conn->auth_params == NULL) {
2310 g_hash_table_new_full (str_case_hash, str_case_equal, g_free, g_free);
2312 g_hash_table_insert (conn->auth_params, g_strdup (param), g_strdup (value));
2316 * gst_rtsp_connection_clear_auth_params:
2317 * @conn: a #GstRTSPConnection
2319 * Clear the list of authentication directives stored in @conn.
2324 gst_rtsp_connection_clear_auth_params (GstRTSPConnection * conn)
2326 g_return_if_fail (conn != NULL);
2328 if (conn->auth_params != NULL) {
2329 g_hash_table_destroy (conn->auth_params);
2330 conn->auth_params = NULL;
2334 static GstRTSPResult
2335 set_qos_dscp (gint fd, guint qos_dscp)
2340 struct sockaddr_in6 sa_in6;
2341 struct sockaddr_storage sa_stor;
2343 socklen_t slen = sizeof (sa);
2350 if (getsockname (fd, &sa.sa, &slen) < 0)
2351 goto no_getsockname;
2353 af = sa.sa.sa_family;
2355 /* if this is an IPv4-mapped address then do IPv4 QoS */
2356 if (af == AF_INET6) {
2357 if (IN6_IS_ADDR_V4MAPPED (&sa.sa_in6.sin6_addr))
2361 /* extract and shift 6 bits of the DSCP */
2362 tos = (qos_dscp & 0x3f) << 2;
2366 if (SETSOCKOPT (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0)
2371 if (SETSOCKOPT (fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof (tos)) < 0)
2385 return GST_RTSP_ESYS;
2390 return GST_RTSP_ERROR;
2395 * gst_rtsp_connection_set_qos_dscp:
2396 * @conn: a #GstRTSPConnection
2397 * @qos_dscp: DSCP value
2399 * Configure @conn to use the specified DSCP value.
2401 * Returns: #GST_RTSP_OK on success.
2406 gst_rtsp_connection_set_qos_dscp (GstRTSPConnection * conn, guint qos_dscp)
2410 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2411 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2412 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2414 res = set_qos_dscp (conn->fd0.fd, qos_dscp);
2415 if (res == GST_RTSP_OK)
2416 res = set_qos_dscp (conn->fd1.fd, qos_dscp);
2423 * gst_rtsp_connection_get_url:
2424 * @conn: a #GstRTSPConnection
2426 * Retrieve the URL of the other end of @conn.
2428 * Returns: The URL. This value remains valid until the
2429 * connection is freed.
2434 gst_rtsp_connection_get_url (const GstRTSPConnection * conn)
2436 g_return_val_if_fail (conn != NULL, NULL);
2442 * gst_rtsp_connection_get_ip:
2443 * @conn: a #GstRTSPConnection
2445 * Retrieve the IP address of the other end of @conn.
2447 * Returns: The IP address as a string. this value remains valid until the
2448 * connection is closed.
2453 gst_rtsp_connection_get_ip (const GstRTSPConnection * conn)
2455 g_return_val_if_fail (conn != NULL, NULL);
2461 * gst_rtsp_connection_set_ip:
2462 * @conn: a #GstRTSPConnection
2463 * @ip: an ip address
2465 * Set the IP address of the server.
2470 gst_rtsp_connection_set_ip (GstRTSPConnection * conn, const gchar * ip)
2472 g_return_if_fail (conn != NULL);
2475 conn->ip = g_strdup (ip);
2479 * gst_rtsp_connection_get_readfd:
2480 * @conn: a #GstRTSPConnection
2482 * Get the file descriptor for reading.
2484 * Returns: the file descriptor used for reading or -1 on error. The file
2485 * descriptor remains valid until the connection is closed.
2490 gst_rtsp_connection_get_readfd (const GstRTSPConnection * conn)
2492 g_return_val_if_fail (conn != NULL, -1);
2493 g_return_val_if_fail (conn->readfd != NULL, -1);
2495 return conn->readfd->fd;
2499 * gst_rtsp_connection_get_writefd:
2500 * @conn: a #GstRTSPConnection
2502 * Get the file descriptor for writing.
2504 * Returns: the file descriptor used for writing or -1 on error. The file
2505 * descriptor remains valid until the connection is closed.
2510 gst_rtsp_connection_get_writefd (const GstRTSPConnection * conn)
2512 g_return_val_if_fail (conn != NULL, -1);
2513 g_return_val_if_fail (conn->writefd != NULL, -1);
2515 return conn->writefd->fd;
2520 * gst_rtsp_connection_set_tunneled:
2521 * @conn: a #GstRTSPConnection
2522 * @tunneled: the new state
2524 * Set the HTTP tunneling state of the connection. This must be configured before
2525 * the @conn is connected.
2530 gst_rtsp_connection_set_tunneled (GstRTSPConnection * conn, gboolean tunneled)
2532 g_return_if_fail (conn != NULL);
2533 g_return_if_fail (conn->readfd == NULL);
2534 g_return_if_fail (conn->writefd == NULL);
2536 conn->tunneled = tunneled;
2540 * gst_rtsp_connection_is_tunneled:
2541 * @conn: a #GstRTSPConnection
2543 * Get the tunneling state of the connection.
2545 * Returns: if @conn is using HTTP tunneling.
2550 gst_rtsp_connection_is_tunneled (const GstRTSPConnection * conn)
2552 g_return_val_if_fail (conn != NULL, FALSE);
2554 return conn->tunneled;
2558 * gst_rtsp_connection_get_tunnelid:
2559 * @conn: a #GstRTSPConnection
2561 * Get the tunnel session id the connection.
2563 * Returns: returns a non-empty string if @conn is being tunneled over HTTP.
2568 gst_rtsp_connection_get_tunnelid (const GstRTSPConnection * conn)
2570 g_return_val_if_fail (conn != NULL, NULL);
2572 if (!conn->tunneled)
2575 return conn->tunnelid;
2579 * gst_rtsp_connection_do_tunnel:
2580 * @conn: a #GstRTSPConnection
2581 * @conn2: a #GstRTSPConnection
2583 * If @conn received the first tunnel connection and @conn2 received
2584 * the second tunnel connection, link the two connections together so that
2585 * @conn manages the tunneled connection.
2587 * After this call, @conn2 cannot be used anymore and must be freed with
2588 * gst_rtsp_connection_free().
2590 * Returns: return GST_RTSP_OK on success.
2595 gst_rtsp_connection_do_tunnel (GstRTSPConnection * conn,
2596 GstRTSPConnection * conn2)
2598 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2599 g_return_val_if_fail (conn2 != NULL, GST_RTSP_EINVAL);
2600 g_return_val_if_fail (conn->tstate == TUNNEL_STATE_GET, GST_RTSP_EINVAL);
2601 g_return_val_if_fail (conn2->tstate == TUNNEL_STATE_POST, GST_RTSP_EINVAL);
2602 g_return_val_if_fail (!memcmp (conn2->tunnelid, conn->tunnelid, TUNNELID_LEN),
2605 /* both connections have fd0 as the read/write socket. start by taking the
2606 * socket from conn2 and set it as the socket in conn */
2607 conn->fd1 = conn2->fd0;
2609 /* clean up some of the state of conn2 */
2610 gst_poll_remove_fd (conn2->fdset, &conn2->fd0);
2612 conn2->readfd = conn2->writefd = NULL;
2614 /* We make fd0 the write socket and fd1 the read socket. */
2615 conn->writefd = &conn->fd0;
2616 conn->readfd = &conn->fd1;
2618 conn->tstate = TUNNEL_STATE_COMPLETE;
2620 /* we need base64 decoding for the readfd */
2621 conn->ctx.state = 0;
2625 conn->ctxp = &conn->ctx;
2630 #define READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
2631 #define WRITE_COND (G_IO_OUT | G_IO_ERR)
2639 static guint queue_response (GstRTSPWatch * watch, GString * str);
2641 /* async functions */
2642 struct _GstRTSPWatch
2646 GstRTSPConnection *conn;
2648 GstRTSPBuilder builder;
2649 GstRTSPMessage message;
2653 gboolean write_added;
2655 /* queued message for transmission */
2657 GAsyncQueue *messages;
2663 GstRTSPWatchFuncs funcs;
2666 GDestroyNotify notify;
2670 gst_rtsp_source_prepare (GSource * source, gint * timeout)
2672 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2674 *timeout = (watch->conn->timeout * 1000);
2680 gst_rtsp_source_check (GSource * source)
2682 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2684 if (watch->readfd.revents & READ_COND)
2687 if (watch->writefd.revents & WRITE_COND)
2694 gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback G_GNUC_UNUSED,
2695 gpointer user_data G_GNUC_UNUSED)
2697 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2700 /* first read as much as we can */
2701 if (watch->readfd.revents & READ_COND) {
2703 res = build_next (&watch->builder, &watch->message, watch->conn);
2704 if (res == GST_RTSP_EINTR)
2706 if (G_UNLIKELY (res == GST_RTSP_EEOF))
2708 if (res == GST_RTSP_ETGET) {
2710 GstRTSPStatusCode code;
2712 if (watch->funcs.tunnel_start)
2713 code = watch->funcs.tunnel_start (watch, watch->user_data);
2715 code = GST_RTSP_STS_OK;
2717 /* queue the response string */
2718 str = gen_tunnel_reply (watch->conn, code);
2719 queue_response (watch, str);
2720 } else if (res == GST_RTSP_ETPOST) {
2721 /* in the callback the connection should be tunneled with the
2723 if (watch->funcs.tunnel_complete)
2724 watch->funcs.tunnel_complete (watch, watch->user_data);
2725 } else if (G_UNLIKELY (res != GST_RTSP_OK))
2728 if (G_LIKELY (res == GST_RTSP_OK)) {
2729 if (watch->funcs.message_received)
2730 watch->funcs.message_received (watch, &watch->message,
2733 gst_rtsp_message_unset (&watch->message);
2735 build_reset (&watch->builder);
2739 if (watch->writefd.revents & WRITE_COND) {
2741 if (watch->write_data == NULL) {
2744 /* get a new message from the queue */
2745 rec = g_async_queue_try_pop (watch->messages);
2749 watch->write_off = 0;
2750 watch->write_len = rec->str->len;
2751 watch->write_data = (guint8 *) g_string_free (rec->str, FALSE);
2752 watch->write_id = rec->id;
2755 g_slice_free (GstRTSPRec, rec);
2758 res = write_bytes (watch->writefd.fd, watch->write_data,
2759 &watch->write_off, watch->write_len);
2760 if (res == GST_RTSP_EINTR)
2762 if (G_UNLIKELY (res != GST_RTSP_OK))
2765 if (watch->funcs.message_sent)
2766 watch->funcs.message_sent (watch, watch->write_id, watch->user_data);
2769 if (g_async_queue_length (watch->messages) == 0 && watch->write_added) {
2770 g_source_remove_poll ((GSource *) watch, &watch->writefd);
2771 watch->write_added = FALSE;
2772 watch->writefd.revents = 0;
2774 g_free (watch->write_data);
2775 watch->write_data = NULL;
2784 if (watch->funcs.closed)
2785 watch->funcs.closed (watch, watch->user_data);
2790 if (watch->funcs.error)
2791 watch->funcs.error (watch, res, watch->user_data);
2797 gst_rtsp_rec_free (gpointer data)
2799 GstRTSPRec *rec = data;
2801 g_string_free (rec->str, TRUE);
2803 g_slice_free (GstRTSPRec, rec);
2807 gst_rtsp_source_finalize (GSource * source)
2809 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2811 build_reset (&watch->builder);
2812 gst_rtsp_message_unset (&watch->message);
2814 g_async_queue_unref (watch->messages);
2815 watch->messages = NULL;
2817 g_free (watch->write_data);
2820 watch->notify (watch->user_data);
2823 static GSourceFuncs gst_rtsp_source_funcs = {
2824 gst_rtsp_source_prepare,
2825 gst_rtsp_source_check,
2826 gst_rtsp_source_dispatch,
2827 gst_rtsp_source_finalize,
2833 * gst_rtsp_watch_new:
2834 * @conn: a #GstRTSPConnection
2835 * @funcs: watch functions
2836 * @user_data: user data to pass to @funcs
2837 * @notify: notify when @user_data is not referenced anymore
2839 * Create a watch object for @conn. The functions provided in @funcs will be
2840 * called with @user_data when activity happened on the watch.
2842 * The new watch is usually created so that it can be attached to a
2843 * maincontext with gst_rtsp_watch_attach().
2845 * @conn must exist for the entire lifetime of the watch.
2847 * Returns: a #GstRTSPWatch that can be used for asynchronous RTSP
2848 * communication. Free with gst_rtsp_watch_unref () after usage.
2853 gst_rtsp_watch_new (GstRTSPConnection * conn,
2854 GstRTSPWatchFuncs * funcs, gpointer user_data, GDestroyNotify notify)
2856 GstRTSPWatch *result;
2858 g_return_val_if_fail (conn != NULL, NULL);
2859 g_return_val_if_fail (funcs != NULL, NULL);
2860 g_return_val_if_fail (conn->readfd != NULL, NULL);
2861 g_return_val_if_fail (conn->writefd != NULL, NULL);
2863 result = (GstRTSPWatch *) g_source_new (&gst_rtsp_source_funcs,
2864 sizeof (GstRTSPWatch));
2866 result->conn = conn;
2867 result->builder.state = STATE_START;
2869 result->messages = g_async_queue_new_full (gst_rtsp_rec_free);
2871 result->readfd.fd = -1;
2872 result->writefd.fd = -1;
2874 gst_rtsp_watch_reset (result);
2876 result->funcs = *funcs;
2877 result->user_data = user_data;
2878 result->notify = notify;
2880 /* only add the read fd, the write fd is only added when we have data
2882 g_source_add_poll ((GSource *) result, &result->readfd);
2888 * gst_rtsp_watch_reset:
2889 * @watch: a #GstRTSPWatch
2891 * Reset @watch, this is usually called after gst_rtsp_connection_do_tunnel()
2892 * when the file descriptors of the connection might have changed.
2897 gst_rtsp_watch_reset (GstRTSPWatch * watch)
2899 if (watch->readfd.fd != -1)
2900 g_source_remove_poll ((GSource *) watch, &watch->readfd);
2901 if (watch->writefd.fd != -1)
2902 g_source_remove_poll ((GSource *) watch, &watch->writefd);
2904 watch->readfd.fd = watch->conn->readfd->fd;
2905 watch->readfd.events = READ_COND;
2906 watch->readfd.revents = 0;
2908 watch->writefd.fd = watch->conn->writefd->fd;
2909 watch->writefd.events = WRITE_COND;
2910 watch->writefd.revents = 0;
2911 watch->write_added = FALSE;
2913 g_source_add_poll ((GSource *) watch, &watch->readfd);
2917 * gst_rtsp_watch_attach:
2918 * @watch: a #GstRTSPWatch
2919 * @context: a GMainContext (if NULL, the default context will be used)
2921 * Adds a #GstRTSPWatch to a context so that it will be executed within that context.
2923 * Returns: the ID (greater than 0) for the watch within the GMainContext.
2928 gst_rtsp_watch_attach (GstRTSPWatch * watch, GMainContext * context)
2930 g_return_val_if_fail (watch != NULL, 0);
2932 return g_source_attach ((GSource *) watch, context);
2936 * gst_rtsp_watch_unref:
2937 * @watch: a #GstRTSPWatch
2939 * Decreases the reference count of @watch by one. If the resulting reference
2940 * count is zero the watch and associated memory will be destroyed.
2945 gst_rtsp_watch_unref (GstRTSPWatch * watch)
2947 g_return_if_fail (watch != NULL);
2949 g_source_unref ((GSource *) watch);
2953 queue_response (GstRTSPWatch * watch, GString * str)
2957 /* make a record with the message as a string and id */
2958 rec = g_slice_new (GstRTSPRec);
2960 rec->id = ++watch->id;
2962 /* add the record to a queue. FIXME we would like to have an upper limit here */
2963 g_async_queue_push (watch->messages, rec);
2965 /* FIXME: does the following need to be made thread-safe? (this might be
2966 * called from a streaming thread, like appsink's render function) */
2967 /* make sure the main context will now also check for writability on the
2969 if (!watch->write_added) {
2970 g_source_add_poll ((GSource *) watch, &watch->writefd);
2971 watch->write_added = TRUE;
2978 * gst_rtsp_watch_queue_message:
2979 * @watch: a #GstRTSPWatch
2980 * @message: a #GstRTSPMessage
2982 * Queue a @message for transmission in @watch. The contents of this
2983 * message will be serialized and transmitted when the connection of the
2984 * @watch becomes writable.
2986 * The return value of this function will be used as the id argument in the
2987 * message_sent callback.
2994 gst_rtsp_watch_queue_message (GstRTSPWatch * watch, GstRTSPMessage * message)
2996 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
2997 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2999 /* make a record with the message as a string and id */
3000 return queue_response (watch, message_to_string (watch->conn, message));