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)
72 /* we include this here to get the G_OS_* defines */
77 /* ws2_32.dll has getaddrinfo and freeaddrinfo on Windows XP and later.
78 * minwg32 headers check WINVER before allowing the use of these */
84 #define EINPROGRESS WSAEINPROGRESS
86 #include <sys/ioctl.h>
88 #include <sys/socket.h>
90 #include <netinet/in.h>
93 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
94 #include <sys/filio.h>
97 #include "gstrtspconnection.h"
98 #include "gstrtspbase64.h"
103 struct sockaddr_in sa_in;
104 struct sockaddr_in6 sa_in6;
105 struct sockaddr_storage sa_stor;
112 guchar out[3]; /* the size must be evenly divisible by 3 */
117 static GstRTSPResult read_line (gint fd, guint8 * buffer, guint * idx,
118 guint size, DecodeCtx * ctxp);
119 static GstRTSPResult parse_key_value (guint8 * buffer, gchar * key,
120 guint keysize, gchar ** value);
121 static void parse_string (gchar * dest, gint size, gchar ** src);
124 #define READ_SOCKET(fd, buf, len) recv (fd, (char *)buf, len, 0)
125 #define WRITE_SOCKET(fd, buf, len) send (fd, (const char *)buf, len, 0)
126 #define SETSOCKOPT(sock, level, name, val, len) setsockopt (sock, level, name, (const char *)val, len)
127 #define CLOSE_SOCKET(sock) closesocket (sock)
128 #define ERRNO_IS_EAGAIN (WSAGetLastError () == WSAEWOULDBLOCK)
129 #define ERRNO_IS_EINTR (WSAGetLastError () == WSAEINTR)
130 /* According to Microsoft's connect() documentation this one returns
131 * WSAEWOULDBLOCK and not WSAEINPROGRESS. */
132 #define ERRNO_IS_EINPROGRESS (WSAGetLastError () == WSAEWOULDBLOCK)
134 #define READ_SOCKET(fd, buf, len) read (fd, buf, len)
135 #define WRITE_SOCKET(fd, buf, len) write (fd, buf, len)
136 #define SETSOCKOPT(sock, level, name, val, len) setsockopt (sock, level, name, val, len)
137 #define CLOSE_SOCKET(sock) close (sock)
138 #define ERRNO_IS_EAGAIN (errno == EAGAIN)
139 #define ERRNO_IS_EINTR (errno == EINTR)
140 #define ERRNO_IS_EINPROGRESS (errno == EINPROGRESS)
143 #define ADD_POLLFD(fdset, pfd, fd) \
146 gst_poll_add_fd (fdset, pfd); \
149 #define REMOVE_POLLFD(fdset, pfd) \
151 if ((pfd)->fd != -1) { \
152 GST_DEBUG ("remove fd %d", (pfd)->fd); \
153 gst_poll_remove_fd (fdset, pfd); \
154 CLOSE_SOCKET ((pfd)->fd); \
164 TUNNEL_STATE_COMPLETE
165 } GstRTSPTunnelState;
167 #define TUNNELID_LEN 24
169 struct _GstRTSPConnection
172 /* URL for the connection */
175 /* connection state */
182 gchar tunnelid[TUNNELID_LEN];
184 GstRTSPTunnelState tstate;
190 gint cseq; /* sequence number */
191 gchar session_id[512]; /* session id */
192 gint timeout; /* session timeout in seconds */
193 GTimer *timer; /* timeout timer */
196 GstRTSPAuthMethod auth_method;
199 GHashTable *auth_params;
218 /* a structure for constructing RTSPMessages */
231 build_reset (GstRTSPBuilder * builder)
233 g_free (builder->body_data);
234 memset (builder, 0, sizeof (GstRTSPBuilder));
238 * gst_rtsp_connection_create:
239 * @url: a #GstRTSPUrl
240 * @conn: storage for a #GstRTSPConnection
242 * Create a newly allocated #GstRTSPConnection from @url and store it in @conn.
243 * The connection will not yet attempt to connect to @url, use
244 * gst_rtsp_connection_connect().
246 * A copy of @url will be made.
248 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
251 gst_rtsp_connection_create (const GstRTSPUrl * url, GstRTSPConnection ** conn)
253 GstRTSPConnection *newconn;
259 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
262 error = WSAStartup (0x0202, &w);
267 if (w.wVersion != 0x0202)
271 newconn = g_new0 (GstRTSPConnection, 1);
273 if ((newconn->fdset = gst_poll_new (TRUE)) == NULL)
276 newconn->url = gst_rtsp_url_copy (url);
277 newconn->fd0.fd = -1;
278 newconn->fd1.fd = -1;
279 newconn->timer = g_timer_new ();
280 newconn->timeout = 60;
283 newconn->auth_method = GST_RTSP_AUTH_NONE;
284 newconn->username = NULL;
285 newconn->passwd = NULL;
286 newconn->auth_params = NULL;
296 g_warning ("Error %d on WSAStartup", error);
297 return GST_RTSP_EWSASTART;
301 g_warning ("Windows sockets are not version 0x202 (current 0x%x)",
304 return GST_RTSP_EWSAVERSION;
313 return GST_RTSP_ESYS;
318 * gst_rtsp_connection_accept:
320 * @conn: storage for a #GstRTSPConnection
322 * Accept a new connection on @sock and create a new #GstRTSPConnection for
323 * handling communication on new socket.
325 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
330 gst_rtsp_connection_accept (gint sock, GstRTSPConnection ** conn)
333 GstRTSPConnection *newconn = NULL;
334 union gst_sockaddr sa;
335 socklen_t slen = sizeof (sa);
336 gchar ip[INET6_ADDRSTRLEN];
342 memset (&sa, 0, slen);
345 fd = accept (sock, &sa.sa, &slen);
347 fd = accept (sock, &sa.sa, (gint *) & slen);
348 #endif /* G_OS_WIN32 */
352 if (getnameinfo (&sa.sa, slen, ip, sizeof (ip), NULL, 0, NI_NUMERICHOST) != 0)
353 goto getnameinfo_failed;
354 if (sa.sa.sa_family != AF_INET && sa.sa.sa_family != AF_INET6)
357 /* set to non-blocking mode so that we can cancel the communication */
359 fcntl (fd, F_SETFL, O_NONBLOCK);
361 ioctlsocket (fd, FIONBIO, &flags);
362 #endif /* G_OS_WIN32 */
364 /* create a url for the client address */
365 url = g_new0 (GstRTSPUrl, 1);
366 url->host = g_strdup (ip);
367 if (sa.sa.sa_family == AF_INET)
368 url->port = sa.sa_in.sin_port;
370 url->port = sa.sa_in6.sin6_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;
395 return GST_RTSP_ERROR;
400 do_resolve (const gchar * host)
402 static gchar ip[INET6_ADDRSTRLEN];
403 struct addrinfo *aires;
407 aierr = getaddrinfo (host, NULL, NULL, &aires);
411 for (ai = aires; ai; ai = ai->ai_next) {
412 if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) {
419 aierr = getnameinfo (ai->ai_addr, ai->ai_addrlen, ip, sizeof (ip), NULL, 0,
420 NI_NUMERICHOST | NI_NUMERICSERV);
424 freeaddrinfo (aires);
426 return g_strdup (ip);
431 GST_ERROR ("no addrinfo found for %s: %s", host, gai_strerror (aierr));
436 GST_ERROR ("no family found for %s", host);
437 freeaddrinfo (aires);
442 GST_ERROR ("no address found for %s: %s", host, gai_strerror (aierr));
443 freeaddrinfo (aires);
449 do_connect (const gchar * ip, guint16 port, GstPollFD * fdout,
450 GstPoll * fdset, GTimeVal * timeout)
453 struct addrinfo hints;
454 struct addrinfo *aires;
457 gchar service[NI_MAXSERV];
460 unsigned long flags = 1;
461 #endif /* G_OS_WIN32 */
465 memset (&hints, 0, sizeof hints);
466 hints.ai_flags = AI_NUMERICHOST;
467 hints.ai_family = AF_UNSPEC;
468 hints.ai_socktype = SOCK_STREAM;
469 g_snprintf (service, sizeof (service) - 1, "%hu", port);
470 service[sizeof (service) - 1] = '\0';
472 aierr = getaddrinfo (ip, service, &hints, &aires);
476 for (ai = aires; ai; ai = ai->ai_next) {
477 if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) {
484 fd = socket (ai->ai_family, SOCK_STREAM, 0);
488 /* set to non-blocking mode so that we can cancel the connect */
490 fcntl (fd, F_SETFL, O_NONBLOCK);
492 ioctlsocket (fd, FIONBIO, &flags);
493 #endif /* G_OS_WIN32 */
495 /* add the socket to our fdset */
496 ADD_POLLFD (fdset, fdout, fd);
498 /* we are going to connect ASYNC now */
499 ret = connect (fd, ai->ai_addr, ai->ai_addrlen);
502 if (!ERRNO_IS_EINPROGRESS)
505 /* wait for connect to complete up to the specified timeout or until we got
507 gst_poll_fd_ctl_write (fdset, fdout, TRUE);
509 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
512 retval = gst_poll_wait (fdset, to);
513 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
517 else if (retval == -1)
520 /* we can still have an error connecting on windows */
521 if (gst_poll_fd_has_error (fdset, fdout)) {
522 socklen_t len = sizeof (errno);
524 getsockopt (fd, SOL_SOCKET, SO_ERROR, &errno, &len);
526 getsockopt (fd, SOL_SOCKET, SO_ERROR, (char *) &errno, &len);
531 gst_poll_fd_ignored (fdset, fdout);
534 freeaddrinfo (aires);
541 GST_ERROR ("no addrinfo found for %s: %s", ip, gai_strerror (aierr));
542 return GST_RTSP_ERROR;
546 GST_ERROR ("no family found for %s", ip);
547 freeaddrinfo (aires);
548 return GST_RTSP_ERROR;
552 GST_ERROR ("no socket %d (%s)", errno, g_strerror (errno));
553 freeaddrinfo (aires);
554 return GST_RTSP_ESYS;
558 GST_ERROR ("system error %d (%s)", errno, g_strerror (errno));
559 REMOVE_POLLFD (fdset, fdout);
560 freeaddrinfo (aires);
561 return GST_RTSP_ESYS;
565 GST_ERROR ("timeout");
566 REMOVE_POLLFD (fdset, fdout);
567 freeaddrinfo (aires);
568 return GST_RTSP_ETIMEOUT;
573 setup_tunneling (GstRTSPConnection * conn, GTimeVal * timeout)
581 gchar *ip, *url_port_str;
582 guint16 port, url_port;
583 gchar codestr[4], *resultstr;
588 /* create a random sessionid */
589 for (i = 0; i < TUNNELID_LEN; i++)
590 conn->tunnelid[i] = g_random_int_range ('a', 'z');
591 conn->tunnelid[TUNNELID_LEN - 1] = '\0';
594 /* get the port from the url */
595 gst_rtsp_url_get_port (url, &url_port);
597 if (conn->proxy_host) {
598 hostparam = g_strdup_printf ("Host: %s:%d\r\n", url->host, url_port);
599 url_port_str = g_strdup_printf (":%d", url_port);
600 ip = conn->proxy_host;
601 port = conn->proxy_port;
610 str = g_strdup_printf ("GET %s%s%s%s%s%s HTTP/1.0\r\n"
612 "x-sessioncookie: %s\r\n"
613 "Accept: application/x-rtsp-tunnelled\r\n"
614 "Pragma: no-cache\r\n"
615 "Cache-Control: no-cache\r\n" "\r\n",
616 conn->proxy_host ? "http://" : "",
617 conn->proxy_host ? url->host : "",
618 conn->proxy_host ? url_port_str : "",
619 url->abspath, url->query ? "?" : "", url->query ? url->query : "",
620 hostparam ? hostparam : "", conn->tunnelid);
622 /* we start by writing to this fd */
623 conn->writefd = &conn->fd0;
625 res = gst_rtsp_connection_write (conn, (guint8 *) str, strlen (str), timeout);
627 if (res != GST_RTSP_OK)
630 gst_poll_fd_ctl_write (conn->fdset, &conn->fd0, FALSE);
631 gst_poll_fd_ctl_read (conn->fdset, &conn->fd0, TRUE);
633 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
641 res = read_line (conn->fd0.fd, buffer, &idx, sizeof (buffer), NULL);
642 if (res == GST_RTSP_EEOF)
644 if (res == GST_RTSP_OK)
646 if (res != GST_RTSP_EINTR)
650 retval = gst_poll_wait (conn->fdset, to);
651 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
653 /* check for timeout */
665 /* check for last line */
666 if (buffer[0] == '\r')
668 if (buffer[0] == '\0')
672 /* first line, parse response */
673 gchar versionstr[20];
676 bptr = (gchar *) buffer;
678 parse_string (versionstr, sizeof (versionstr), &bptr);
679 parse_string (codestr, sizeof (codestr), &bptr);
680 code = atoi (codestr);
682 while (g_ascii_isspace (*bptr))
687 if (code != GST_RTSP_STS_OK)
693 /* other lines, parse key/value */
694 res = parse_key_value (buffer, key, sizeof (key), &value);
695 if (res == GST_RTSP_OK) {
696 /* we got a new ip address */
697 if (g_ascii_strcasecmp (key, "x-server-ip-address") == 0) {
698 if (conn->proxy_host) {
699 /* if we use a proxy we need to change the destination url */
701 url->host = g_strdup (value);
703 g_free (url_port_str);
705 g_strdup_printf ("Host: %s:%d\r\n", url->host, url_port);
706 url_port_str = g_strdup_printf (":%d", url_port);
708 /* and resolve the new ip address */
709 if (!(ip = do_resolve (conn->ip)))
720 /* connect to the host/port */
721 res = do_connect (ip, port, &conn->fd1, conn->fdset, timeout);
722 if (res != GST_RTSP_OK)
725 /* this is now our writing socket */
726 conn->writefd = &conn->fd1;
729 str = g_strdup_printf ("POST %s%s%s%s%s%s HTTP/1.0\r\n"
731 "x-sessioncookie: %s\r\n"
732 "Content-Type: application/x-rtsp-tunnelled\r\n"
733 "Pragma: no-cache\r\n"
734 "Cache-Control: no-cache\r\n"
735 "Content-Length: 32767\r\n"
736 "Expires: Sun, 9 Jan 1972 00:00:00 GMT\r\n"
738 conn->proxy_host ? "http://" : "",
739 conn->proxy_host ? url->host : "",
740 conn->proxy_host ? url_port_str : "",
741 url->abspath, url->query ? "?" : "", url->query ? url->query : "",
742 hostparam ? hostparam : "", conn->tunnelid);
744 res = gst_rtsp_connection_write (conn, (guint8 *) str, strlen (str), timeout);
746 if (res != GST_RTSP_OK)
751 g_free (url_port_str);
758 GST_ERROR ("write failed (%d)", res);
772 res = GST_RTSP_ETIMEOUT;
782 res = GST_RTSP_EINTR;
787 GST_ERROR ("got failure response %d %s", code, resultstr);
788 res = GST_RTSP_ERROR;
793 GST_ERROR ("could not resolve %s", conn->ip);
799 GST_ERROR ("failed to connect");
805 * gst_rtsp_connection_connect:
806 * @conn: a #GstRTSPConnection
807 * @timeout: a #GTimeVal timeout
809 * Attempt to connect to the url of @conn made with
810 * gst_rtsp_connection_create(). If @timeout is #NULL this function can block
811 * forever. If @timeout contains a valid timeout, this function will return
812 * #GST_RTSP_ETIMEOUT after the timeout expired.
814 * This function can be cancelled with gst_rtsp_connection_flush().
816 * Returns: #GST_RTSP_OK when a connection could be made.
819 gst_rtsp_connection_connect (GstRTSPConnection * conn, GTimeVal * timeout)
826 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
827 g_return_val_if_fail (conn->url != NULL, GST_RTSP_EINVAL);
828 g_return_val_if_fail (conn->fd0.fd < 0, GST_RTSP_EINVAL);
832 if (conn->proxy_host && conn->tunneled) {
833 if (!(ip = do_resolve (conn->proxy_host))) {
834 GST_ERROR ("could not resolve %s", conn->proxy_host);
837 port = conn->proxy_port;
838 g_free (conn->proxy_host);
839 conn->proxy_host = ip;
841 if (!(ip = do_resolve (url->host))) {
842 GST_ERROR ("could not resolve %s", url->host);
845 /* get the port from the url */
846 gst_rtsp_url_get_port (url, &port);
852 /* connect to the host/port */
853 res = do_connect (ip, port, &conn->fd0, conn->fdset, timeout);
854 if (res != GST_RTSP_OK)
857 /* this is our read URL */
858 conn->readfd = &conn->fd0;
860 if (conn->tunneled) {
861 res = setup_tunneling (conn, timeout);
862 if (res != GST_RTSP_OK)
863 goto tunneling_failed;
865 conn->writefd = &conn->fd0;
872 return GST_RTSP_ENET;
876 GST_ERROR ("failed to connect");
881 GST_ERROR ("failed to setup tunneling");
887 auth_digest_compute_hex_urp (const gchar * username,
888 const gchar * realm, const gchar * password, gchar hex_urp[33])
890 GChecksum *md5_context = g_checksum_new (G_CHECKSUM_MD5);
891 const gchar *digest_string;
893 g_checksum_update (md5_context, (const guchar *) username, strlen (username));
894 g_checksum_update (md5_context, (const guchar *) ":", 1);
895 g_checksum_update (md5_context, (const guchar *) realm, strlen (realm));
896 g_checksum_update (md5_context, (const guchar *) ":", 1);
897 g_checksum_update (md5_context, (const guchar *) password, strlen (password));
898 digest_string = g_checksum_get_string (md5_context);
900 memset (hex_urp, 0, 33);
901 memcpy (hex_urp, digest_string, strlen (digest_string));
903 g_checksum_free (md5_context);
907 auth_digest_compute_response (const gchar * method,
908 const gchar * uri, const gchar * hex_a1, const gchar * nonce,
911 char hex_a2[33] = { 0, };
912 GChecksum *md5_context = g_checksum_new (G_CHECKSUM_MD5);
913 const gchar *digest_string;
916 g_checksum_update (md5_context, (const guchar *) method, strlen (method));
917 g_checksum_update (md5_context, (const guchar *) ":", 1);
918 g_checksum_update (md5_context, (const guchar *) uri, strlen (uri));
919 digest_string = g_checksum_get_string (md5_context);
920 memcpy (hex_a2, digest_string, strlen (digest_string));
923 #if GLIB_CHECK_VERSION (2, 18, 0)
924 g_checksum_reset (md5_context);
926 g_checksum_free (md5_context);
927 md5_context = g_checksum_new (G_CHECKSUM_MD5);
929 g_checksum_update (md5_context, (const guchar *) hex_a1, strlen (hex_a1));
930 g_checksum_update (md5_context, (const guchar *) ":", 1);
931 g_checksum_update (md5_context, (const guchar *) nonce, strlen (nonce));
932 g_checksum_update (md5_context, (const guchar *) ":", 1);
934 g_checksum_update (md5_context, (const guchar *) hex_a2, 32);
935 digest_string = g_checksum_get_string (md5_context);
936 memset (response, 0, 33);
937 memcpy (response, digest_string, strlen (digest_string));
939 g_checksum_free (md5_context);
943 add_auth_header (GstRTSPConnection * conn, GstRTSPMessage * message)
945 switch (conn->auth_method) {
946 case GST_RTSP_AUTH_BASIC:{
951 user_pass = g_strdup_printf ("%s:%s", conn->username, conn->passwd);
952 user_pass64 = g_base64_encode ((guchar *) user_pass, strlen (user_pass));
953 auth_string = g_strdup_printf ("Basic %s", user_pass64);
955 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
959 g_free (user_pass64);
962 case GST_RTSP_AUTH_DIGEST:{
963 gchar response[33], hex_urp[33];
964 gchar *auth_string, *auth_string2;
971 /* we need to have some params set */
972 if (conn->auth_params == NULL)
975 /* we need the realm and nonce */
976 realm = (gchar *) g_hash_table_lookup (conn->auth_params, "realm");
977 nonce = (gchar *) g_hash_table_lookup (conn->auth_params, "nonce");
978 if (realm == NULL || nonce == NULL)
981 auth_digest_compute_hex_urp (conn->username, realm, conn->passwd,
984 method = gst_rtsp_method_as_text (message->type_data.request.method);
985 uri = message->type_data.request.uri;
987 /* Assume no qop, algorithm=md5, stale=false */
988 /* For algorithm MD5, a1 = urp. */
989 auth_digest_compute_response (method, uri, hex_urp, nonce, response);
990 auth_string = g_strdup_printf ("Digest username=\"%s\", "
991 "realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"",
992 conn->username, realm, nonce, uri, response);
994 opaque = (gchar *) g_hash_table_lookup (conn->auth_params, "opaque");
996 auth_string2 = g_strdup_printf ("%s, opaque=\"%s\"", auth_string,
998 g_free (auth_string);
999 auth_string = auth_string2;
1001 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
1012 gen_date_string (gchar * date_string, guint len)
1016 #ifdef HAVE_GMTIME_R
1020 g_get_current_time (&tv);
1021 t = (time_t) tv.tv_sec;
1023 #ifdef HAVE_GMTIME_R
1024 strftime (date_string, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime_r (&t, &tm_));
1026 strftime (date_string, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime (&t));
1030 static GstRTSPResult
1031 write_bytes (gint fd, const guint8 * buffer, guint * idx, guint size)
1035 if (G_UNLIKELY (*idx > size))
1036 return GST_RTSP_ERROR;
1043 r = WRITE_SOCKET (fd, &buffer[*idx], left);
1044 if (G_UNLIKELY (r == 0)) {
1045 return GST_RTSP_EINTR;
1046 } else if (G_UNLIKELY (r < 0)) {
1047 if (ERRNO_IS_EAGAIN)
1048 return GST_RTSP_EINTR;
1049 if (!ERRNO_IS_EINTR)
1050 return GST_RTSP_ESYS;
1060 fill_bytes (gint fd, guint8 * buffer, guint size, DecodeCtx * ctx)
1066 guint8 in[sizeof (ctx->out) * 4 / 3];
1069 while (size > 0 && ctx->cout < ctx->coutl) {
1070 /* we have some leftover bytes */
1071 *buffer++ = ctx->out[ctx->cout++];
1076 /* got what we needed? */
1080 /* try to read more bytes */
1081 r = READ_SOCKET (fd, in, sizeof (in));
1090 g_base64_decode_step ((gchar *) in, r, ctx->out, &ctx->state,
1094 out = READ_SOCKET (fd, buffer, size);
1100 static GstRTSPResult
1101 read_bytes (gint fd, guint8 * buffer, guint * idx, guint size, DecodeCtx * ctx)
1105 if (G_UNLIKELY (*idx > size))
1106 return GST_RTSP_ERROR;
1113 r = fill_bytes (fd, &buffer[*idx], left, ctx);
1114 if (G_UNLIKELY (r == 0)) {
1115 return GST_RTSP_EEOF;
1116 } else if (G_UNLIKELY (r < 0)) {
1117 if (ERRNO_IS_EAGAIN)
1118 return GST_RTSP_EINTR;
1119 if (!ERRNO_IS_EINTR)
1120 return GST_RTSP_ESYS;
1129 static GstRTSPResult
1130 read_line (gint fd, guint8 * buffer, guint * idx, guint size, DecodeCtx * ctx)
1136 r = fill_bytes (fd, &c, 1, ctx);
1137 if (G_UNLIKELY (r == 0)) {
1138 return GST_RTSP_EEOF;
1139 } else if (G_UNLIKELY (r < 0)) {
1140 if (ERRNO_IS_EAGAIN)
1141 return GST_RTSP_EINTR;
1142 if (!ERRNO_IS_EINTR)
1143 return GST_RTSP_ESYS;
1145 if (c == '\n') /* end on \n */
1147 if (c == '\r') /* ignore \r */
1150 if (G_LIKELY (*idx < size - 1))
1151 buffer[(*idx)++] = c;
1154 buffer[*idx] = '\0';
1160 * gst_rtsp_connection_write:
1161 * @conn: a #GstRTSPConnection
1162 * @data: the data to write
1163 * @size: the size of @data
1164 * @timeout: a timeout value or #NULL
1166 * Attempt to write @size bytes of @data to the connected @conn, blocking up to
1167 * the specified @timeout. @timeout can be #NULL, in which case this function
1168 * might block forever.
1170 * This function can be cancelled with gst_rtsp_connection_flush().
1172 * Returns: #GST_RTSP_OK on success.
1175 gst_rtsp_connection_write (GstRTSPConnection * conn, const guint8 * data,
1176 guint size, GTimeVal * timeout)
1183 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1184 g_return_val_if_fail (data != NULL || size == 0, GST_RTSP_EINVAL);
1185 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
1187 gst_poll_set_controllable (conn->fdset, TRUE);
1188 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, TRUE);
1189 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, FALSE);
1190 /* clear all previous poll results */
1191 gst_poll_fd_ignored (conn->fdset, conn->writefd);
1192 gst_poll_fd_ignored (conn->fdset, conn->readfd);
1194 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1200 res = write_bytes (conn->writefd->fd, data, &offset, size);
1201 if (G_LIKELY (res == GST_RTSP_OK))
1203 if (G_UNLIKELY (res != GST_RTSP_EINTR))
1206 /* not all is written, wait until we can write more */
1208 retval = gst_poll_wait (conn->fdset, to);
1209 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1211 if (G_UNLIKELY (retval == 0))
1214 if (G_UNLIKELY (retval == -1)) {
1226 return GST_RTSP_ETIMEOUT;
1230 return GST_RTSP_ESYS;
1234 return GST_RTSP_EINTR;
1243 message_to_string (GstRTSPConnection * conn, GstRTSPMessage * message)
1245 GString *str = NULL;
1247 str = g_string_new ("");
1249 switch (message->type) {
1250 case GST_RTSP_MESSAGE_REQUEST:
1251 /* create request string, add CSeq */
1252 g_string_append_printf (str, "%s %s RTSP/1.0\r\n"
1254 gst_rtsp_method_as_text (message->type_data.request.method),
1255 message->type_data.request.uri, conn->cseq++);
1256 /* add session id if we have one */
1257 if (conn->session_id[0] != '\0') {
1258 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
1259 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SESSION,
1262 /* add any authentication headers */
1263 add_auth_header (conn, message);
1265 case GST_RTSP_MESSAGE_RESPONSE:
1266 /* create response string */
1267 g_string_append_printf (str, "RTSP/1.0 %d %s\r\n",
1268 message->type_data.response.code, message->type_data.response.reason);
1270 case GST_RTSP_MESSAGE_DATA:
1272 guint8 data_header[4];
1274 /* prepare data header */
1275 data_header[0] = '$';
1276 data_header[1] = message->type_data.data.channel;
1277 data_header[2] = (message->body_size >> 8) & 0xff;
1278 data_header[3] = message->body_size & 0xff;
1280 /* create string with header and data */
1281 str = g_string_append_len (str, (gchar *) data_header, 4);
1283 g_string_append_len (str, (gchar *) message->body,
1284 message->body_size);
1288 g_string_free (str, TRUE);
1289 g_return_val_if_reached (NULL);
1293 /* append headers and body */
1294 if (message->type != GST_RTSP_MESSAGE_DATA) {
1295 gchar date_string[100];
1297 gen_date_string (date_string, sizeof (date_string));
1299 /* add date header */
1300 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_DATE, -1);
1301 gst_rtsp_message_add_header (message, GST_RTSP_HDR_DATE, date_string);
1303 /* append headers */
1304 gst_rtsp_message_append_headers (message, str);
1306 /* append Content-Length and body if needed */
1307 if (message->body != NULL && message->body_size > 0) {
1310 len = g_strdup_printf ("%d", message->body_size);
1311 g_string_append_printf (str, "%s: %s\r\n",
1312 gst_rtsp_header_as_text (GST_RTSP_HDR_CONTENT_LENGTH), len);
1314 /* header ends here */
1315 g_string_append (str, "\r\n");
1317 g_string_append_len (str, (gchar *) message->body,
1318 message->body_size);
1320 /* just end headers */
1321 g_string_append (str, "\r\n");
1329 * gst_rtsp_connection_send:
1330 * @conn: a #GstRTSPConnection
1331 * @message: the message to send
1332 * @timeout: a timeout value or #NULL
1334 * Attempt to send @message to the connected @conn, blocking up to
1335 * the specified @timeout. @timeout can be #NULL, in which case this function
1336 * might block forever.
1338 * This function can be cancelled with gst_rtsp_connection_flush().
1340 * Returns: #GST_RTSP_OK on success.
1343 gst_rtsp_connection_send (GstRTSPConnection * conn, GstRTSPMessage * message,
1346 GString *string = NULL;
1351 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1352 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1354 if (G_UNLIKELY (!(string = message_to_string (conn, message))))
1357 if (conn->tunneled) {
1358 str = g_base64_encode ((const guchar *) string->str, string->len);
1359 g_string_free (string, TRUE);
1364 g_string_free (string, FALSE);
1368 res = gst_rtsp_connection_write (conn, (guint8 *) str, len, timeout);
1376 g_warning ("Wrong message");
1377 return GST_RTSP_EINVAL;
1382 parse_string (gchar * dest, gint size, gchar ** src)
1388 while (g_ascii_isspace (**src))
1391 while (!g_ascii_isspace (**src) && **src != '\0') {
1393 dest[idx++] = **src;
1401 parse_key (gchar * dest, gint size, gchar ** src)
1406 while (**src != ':' && **src != '\0') {
1408 dest[idx++] = **src;
1415 static GstRTSPResult
1416 parse_response_status (guint8 * buffer, GstRTSPMessage * msg)
1419 gchar versionstr[20];
1424 bptr = (gchar *) buffer;
1426 parse_string (versionstr, sizeof (versionstr), &bptr);
1427 parse_string (codestr, sizeof (codestr), &bptr);
1428 code = atoi (codestr);
1430 while (g_ascii_isspace (*bptr))
1433 if (strcmp (versionstr, "RTSP/1.0") == 0)
1434 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
1436 else if (strncmp (versionstr, "RTSP/", 5) == 0) {
1437 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
1439 msg->type_data.response.version = GST_RTSP_VERSION_INVALID;
1447 return GST_RTSP_EPARSE;
1451 static GstRTSPResult
1452 parse_request_line (GstRTSPConnection * conn, guint8 * buffer,
1453 GstRTSPMessage * msg)
1455 GstRTSPResult res = GST_RTSP_OK;
1456 gchar versionstr[20];
1457 gchar methodstr[20];
1460 GstRTSPMethod method;
1461 GstRTSPTunnelState tstate = TUNNEL_STATE_NONE;
1463 bptr = (gchar *) buffer;
1465 parse_string (methodstr, sizeof (methodstr), &bptr);
1466 method = gst_rtsp_find_method (methodstr);
1467 if (method == GST_RTSP_INVALID) {
1468 /* a tunnel request is allowed when we don't have one yet */
1469 if (conn->tstate != TUNNEL_STATE_NONE)
1470 goto invalid_method;
1471 /* we need GET or POST for a valid tunnel request */
1472 if (!strcmp (methodstr, "GET"))
1473 tstate = TUNNEL_STATE_GET;
1474 else if (!strcmp (methodstr, "POST"))
1475 tstate = TUNNEL_STATE_POST;
1477 goto invalid_method;
1480 parse_string (urlstr, sizeof (urlstr), &bptr);
1481 if (G_UNLIKELY (*urlstr == '\0'))
1484 parse_string (versionstr, sizeof (versionstr), &bptr);
1486 if (G_UNLIKELY (*bptr != '\0'))
1487 goto invalid_version;
1489 if (strcmp (versionstr, "RTSP/1.0") == 0) {
1490 res = gst_rtsp_message_init_request (msg, method, urlstr);
1491 } else if (strncmp (versionstr, "RTSP/", 5) == 0) {
1492 res = gst_rtsp_message_init_request (msg, method, urlstr);
1493 msg->type_data.request.version = GST_RTSP_VERSION_INVALID;
1494 } else if (strcmp (versionstr, "HTTP/1.0") == 0) {
1495 /* tunnel request, we need a tunnel method */
1496 if (tstate == TUNNEL_STATE_NONE) {
1497 res = GST_RTSP_EPARSE;
1499 conn->tstate = tstate;
1502 res = GST_RTSP_EPARSE;
1510 GST_ERROR ("invalid method %s", methodstr);
1511 return GST_RTSP_EPARSE;
1515 GST_ERROR ("invalid url %s", urlstr);
1516 return GST_RTSP_EPARSE;
1520 GST_ERROR ("invalid version");
1521 return GST_RTSP_EPARSE;
1525 static GstRTSPResult
1526 parse_key_value (guint8 * buffer, gchar * key, guint keysize, gchar ** value)
1530 bptr = (gchar *) buffer;
1533 parse_key (key, keysize, &bptr);
1534 if (G_UNLIKELY (*bptr != ':'))
1538 while (g_ascii_isspace (*bptr))
1548 return GST_RTSP_EPARSE;
1552 /* parsing lines means reading a Key: Value pair */
1553 static GstRTSPResult
1554 parse_line (GstRTSPConnection * conn, guint8 * buffer, GstRTSPMessage * msg)
1559 GstRTSPHeaderField field;
1561 res = parse_key_value (buffer, key, sizeof (key), &value);
1562 if (G_UNLIKELY (res != GST_RTSP_OK))
1565 if (conn->tstate == TUNNEL_STATE_GET || conn->tstate == TUNNEL_STATE_POST) {
1566 /* save the tunnel session in the connection */
1567 if (!strcmp (key, "x-sessioncookie")) {
1568 strncpy (conn->tunnelid, value, TUNNELID_LEN);
1569 conn->tunnelid[TUNNELID_LEN - 1] = '\0';
1570 conn->tunneled = TRUE;
1573 field = gst_rtsp_find_header_field (key);
1574 if (field != GST_RTSP_HDR_INVALID)
1575 gst_rtsp_message_add_header (msg, field, value);
1588 * GST_RTSP_OK when a complete message was read.
1589 * GST_RTSP_EEOF: when the socket is closed
1590 * GST_RTSP_EINTR: when more data is needed.
1591 * GST_RTSP_..: some other error occured.
1593 static GstRTSPResult
1594 build_next (GstRTSPBuilder * builder, GstRTSPMessage * message,
1595 GstRTSPConnection * conn)
1600 switch (builder->state) {
1602 builder->offset = 0;
1604 read_bytes (conn->readfd->fd, (guint8 *) builder->buffer,
1605 &builder->offset, 1, conn->ctxp);
1606 if (res != GST_RTSP_OK)
1609 /* we have 1 bytes now and we can see if this is a data message or
1611 if (builder->buffer[0] == '$') {
1612 /* data message, prepare for the header */
1613 builder->state = STATE_DATA_HEADER;
1616 builder->state = STATE_READ_LINES;
1619 case STATE_DATA_HEADER:
1622 read_bytes (conn->readfd->fd, (guint8 *) builder->buffer,
1623 &builder->offset, 4, conn->ctxp);
1624 if (res != GST_RTSP_OK)
1627 gst_rtsp_message_init_data (message, builder->buffer[1]);
1629 builder->body_len = (builder->buffer[2] << 8) | builder->buffer[3];
1630 builder->body_data = g_malloc (builder->body_len + 1);
1631 builder->body_data[builder->body_len] = '\0';
1632 builder->offset = 0;
1633 builder->state = STATE_DATA_BODY;
1636 case STATE_DATA_BODY:
1639 read_bytes (conn->readfd->fd, builder->body_data, &builder->offset,
1640 builder->body_len, conn->ctxp);
1641 if (res != GST_RTSP_OK)
1644 /* we have the complete body now, store in the message adjusting the
1645 * length to include the traling '\0' */
1646 gst_rtsp_message_take_body (message,
1647 (guint8 *) builder->body_data, builder->body_len + 1);
1648 builder->body_data = NULL;
1649 builder->body_len = 0;
1651 builder->state = STATE_END;
1654 case STATE_READ_LINES:
1656 res = read_line (conn->readfd->fd, builder->buffer, &builder->offset,
1657 sizeof (builder->buffer), conn->ctxp);
1658 if (res != GST_RTSP_OK)
1661 /* we have a regular response */
1662 if (builder->buffer[0] == '\r') {
1663 builder->buffer[0] = '\0';
1666 if (builder->buffer[0] == '\0') {
1669 /* empty line, end of message header */
1670 /* see if there is a Content-Length header */
1671 if (gst_rtsp_message_get_header (message,
1672 GST_RTSP_HDR_CONTENT_LENGTH, &hdrval, 0) == GST_RTSP_OK) {
1673 /* there is, prepare to read the body */
1674 builder->body_len = atol (hdrval);
1675 builder->body_data = g_malloc (builder->body_len + 1);
1676 builder->body_data[builder->body_len] = '\0';
1677 builder->offset = 0;
1678 builder->state = STATE_DATA_BODY;
1680 builder->state = STATE_END;
1685 /* we have a line */
1686 if (builder->line == 0) {
1687 /* first line, check for response status */
1688 if (memcmp (builder->buffer, "RTSP", 4) == 0) {
1689 res = parse_response_status (builder->buffer, message);
1691 res = parse_request_line (conn, builder->buffer, message);
1693 /* the first line must parse without errors */
1694 if (res != GST_RTSP_OK)
1697 /* else just parse the line, ignore errors */
1698 parse_line (conn, builder->buffer, message);
1701 builder->offset = 0;
1708 if (conn->tstate == TUNNEL_STATE_GET) {
1709 res = GST_RTSP_ETGET;
1711 } else if (conn->tstate == TUNNEL_STATE_POST) {
1712 res = GST_RTSP_ETPOST;
1716 if (message->type == GST_RTSP_MESSAGE_DATA) {
1717 /* data messages don't have headers */
1722 /* save session id in the connection for further use */
1723 if (message->type == GST_RTSP_MESSAGE_RESPONSE &&
1724 gst_rtsp_message_get_header (message, GST_RTSP_HDR_SESSION,
1725 &session_id, 0) == GST_RTSP_OK) {
1728 maxlen = sizeof (conn->session_id) - 1;
1729 /* the sessionid can have attributes marked with ;
1730 * Make sure we strip them */
1731 for (i = 0; session_id[i] != '\0'; i++) {
1732 if (session_id[i] == ';') {
1737 } while (g_ascii_isspace (session_id[i]));
1738 if (g_str_has_prefix (&session_id[i], "timeout=")) {
1741 /* if we parsed something valid, configure */
1742 if ((to = atoi (&session_id[i + 8])) > 0)
1749 /* make sure to not overflow */
1750 strncpy (conn->session_id, session_id, maxlen);
1751 conn->session_id[maxlen] = '\0';
1757 res = GST_RTSP_ERROR;
1766 * gst_rtsp_connection_read:
1767 * @conn: a #GstRTSPConnection
1768 * @data: the data to read
1769 * @size: the size of @data
1770 * @timeout: a timeout value or #NULL
1772 * Attempt to read @size bytes into @data from the connected @conn, blocking up to
1773 * the specified @timeout. @timeout can be #NULL, in which case this function
1774 * might block forever.
1776 * This function can be cancelled with gst_rtsp_connection_flush().
1778 * Returns: #GST_RTSP_OK on success.
1781 gst_rtsp_connection_read (GstRTSPConnection * conn, guint8 * data, guint size,
1789 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1790 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
1791 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1793 if (G_UNLIKELY (size == 0))
1798 /* configure timeout if any */
1799 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1801 gst_poll_set_controllable (conn->fdset, TRUE);
1802 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
1803 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
1806 res = read_bytes (conn->readfd->fd, data, &offset, size, conn->ctxp);
1807 if (G_UNLIKELY (res == GST_RTSP_EEOF))
1809 if (G_LIKELY (res == GST_RTSP_OK))
1811 if (G_UNLIKELY (res != GST_RTSP_EINTR))
1815 retval = gst_poll_wait (conn->fdset, to);
1816 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1818 /* check for timeout */
1819 if (G_UNLIKELY (retval == 0))
1820 goto select_timeout;
1822 if (G_UNLIKELY (retval == -1)) {
1828 gst_poll_set_controllable (conn->fdset, FALSE);
1835 return GST_RTSP_ESYS;
1839 return GST_RTSP_ETIMEOUT;
1843 return GST_RTSP_EINTR;
1847 return GST_RTSP_EEOF;
1856 gen_tunnel_reply (GstRTSPConnection * conn, GstRTSPStatusCode code)
1859 gchar date_string[100];
1860 const gchar *status;
1862 gen_date_string (date_string, sizeof (date_string));
1864 status = gst_rtsp_status_as_text (code);
1865 if (status == NULL) {
1866 code = GST_RTSP_STS_INTERNAL_SERVER_ERROR;
1867 status = "Internal Server Error";
1870 str = g_string_new ("");
1873 g_string_append_printf (str, "HTTP/1.0 %d %s\r\n", code, status);
1874 g_string_append_printf (str,
1875 "Server: GStreamer RTSP Server\r\n"
1877 "Connection: close\r\n"
1878 "Cache-Control: no-store\r\n" "Pragma: no-cache\r\n", date_string);
1879 if (code == GST_RTSP_STS_OK) {
1881 g_string_append_printf (str, "x-server-ip-address: %s\r\n", conn->ip);
1882 g_string_append_printf (str,
1883 "Content-Type: application/x-rtsp-tunnelled\r\n");
1885 g_string_append_printf (str, "\r\n");
1890 * gst_rtsp_connection_receive:
1891 * @conn: a #GstRTSPConnection
1892 * @message: the message to read
1893 * @timeout: a timeout value or #NULL
1895 * Attempt to read into @message from the connected @conn, blocking up to
1896 * the specified @timeout. @timeout can be #NULL, in which case this function
1897 * might block forever.
1899 * This function can be cancelled with gst_rtsp_connection_flush().
1901 * Returns: #GST_RTSP_OK on success.
1904 gst_rtsp_connection_receive (GstRTSPConnection * conn, GstRTSPMessage * message,
1908 GstRTSPBuilder builder;
1912 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1913 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1914 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1916 /* configure timeout if any */
1917 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1919 gst_poll_set_controllable (conn->fdset, TRUE);
1920 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
1921 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
1923 memset (&builder, 0, sizeof (GstRTSPBuilder));
1925 res = build_next (&builder, message, conn);
1926 if (G_UNLIKELY (res == GST_RTSP_EEOF))
1928 if (G_LIKELY (res == GST_RTSP_OK))
1930 if (res == GST_RTSP_ETGET) {
1933 /* tunnel GET request, we can reply now */
1934 str = gen_tunnel_reply (conn, GST_RTSP_STS_OK);
1936 gst_rtsp_connection_write (conn, (guint8 *) str->str, str->len,
1938 g_string_free (str, TRUE);
1939 } else if (res == GST_RTSP_ETPOST) {
1940 /* tunnel POST request, return the value, the caller now has to link the
1941 * two connections. */
1943 } else if (G_UNLIKELY (res != GST_RTSP_EINTR))
1947 retval = gst_poll_wait (conn->fdset, to);
1948 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1950 /* check for timeout */
1951 if (G_UNLIKELY (retval == 0))
1952 goto select_timeout;
1954 if (G_UNLIKELY (retval == -1)) {
1960 gst_poll_set_controllable (conn->fdset, FALSE);
1963 /* we have a message here */
1964 build_reset (&builder);
1971 res = GST_RTSP_ESYS;
1976 res = GST_RTSP_ETIMEOUT;
1981 res = GST_RTSP_EINTR;
1986 res = GST_RTSP_EEOF;
1992 build_reset (&builder);
1993 gst_rtsp_message_unset (message);
1999 * gst_rtsp_connection_close:
2000 * @conn: a #GstRTSPConnection
2002 * Close the connected @conn. After this call, the connection is in the same
2003 * state as when it was first created.
2005 * Returns: #GST_RTSP_OK on success.
2008 gst_rtsp_connection_close (GstRTSPConnection * conn)
2010 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2015 REMOVE_POLLFD (conn->fdset, &conn->fd0);
2016 REMOVE_POLLFD (conn->fdset, &conn->fd1);
2017 conn->writefd = NULL;
2018 conn->readfd = NULL;
2019 conn->tunneled = FALSE;
2020 conn->tstate = TUNNEL_STATE_NONE;
2022 g_free (conn->username);
2023 conn->username = NULL;
2024 g_free (conn->passwd);
2025 conn->passwd = NULL;
2026 gst_rtsp_connection_clear_auth_params (conn);
2029 conn->session_id[0] = '\0';
2035 * gst_rtsp_connection_free:
2036 * @conn: a #GstRTSPConnection
2038 * Close and free @conn.
2040 * Returns: #GST_RTSP_OK on success.
2043 gst_rtsp_connection_free (GstRTSPConnection * conn)
2047 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2049 res = gst_rtsp_connection_close (conn);
2050 gst_poll_free (conn->fdset);
2051 g_timer_destroy (conn->timer);
2052 gst_rtsp_url_free (conn->url);
2053 g_free (conn->proxy_host);
2063 * gst_rtsp_connection_poll:
2064 * @conn: a #GstRTSPConnection
2065 * @events: a bitmask of #GstRTSPEvent flags to check
2066 * @revents: location for result flags
2067 * @timeout: a timeout
2069 * Wait up to the specified @timeout for the connection to become available for
2070 * at least one of the operations specified in @events. When the function returns
2071 * with #GST_RTSP_OK, @revents will contain a bitmask of available operations on
2074 * @timeout can be #NULL, in which case this function might block forever.
2076 * This function can be cancelled with gst_rtsp_connection_flush().
2078 * Returns: #GST_RTSP_OK on success.
2083 gst_rtsp_connection_poll (GstRTSPConnection * conn, GstRTSPEvent events,
2084 GstRTSPEvent * revents, GTimeVal * timeout)
2089 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2090 g_return_val_if_fail (events != 0, GST_RTSP_EINVAL);
2091 g_return_val_if_fail (revents != NULL, GST_RTSP_EINVAL);
2092 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2093 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2095 gst_poll_set_controllable (conn->fdset, TRUE);
2097 /* add fd to writer set when asked to */
2098 gst_poll_fd_ctl_write (conn->fdset, conn->writefd,
2099 events & GST_RTSP_EV_WRITE);
2101 /* add fd to reader set when asked to */
2102 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, events & GST_RTSP_EV_READ);
2104 /* configure timeout if any */
2105 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
2108 retval = gst_poll_wait (conn->fdset, to);
2109 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
2111 if (G_UNLIKELY (retval == 0))
2112 goto select_timeout;
2114 if (G_UNLIKELY (retval == -1)) {
2122 if (events & GST_RTSP_EV_READ) {
2123 if (gst_poll_fd_can_read (conn->fdset, conn->readfd))
2124 *revents |= GST_RTSP_EV_READ;
2126 if (events & GST_RTSP_EV_WRITE) {
2127 if (gst_poll_fd_can_write (conn->fdset, conn->writefd))
2128 *revents |= GST_RTSP_EV_WRITE;
2135 return GST_RTSP_ETIMEOUT;
2139 return GST_RTSP_ESYS;
2143 return GST_RTSP_EINTR;
2148 * gst_rtsp_connection_next_timeout:
2149 * @conn: a #GstRTSPConnection
2150 * @timeout: a timeout
2152 * Calculate the next timeout for @conn, storing the result in @timeout.
2154 * Returns: #GST_RTSP_OK.
2157 gst_rtsp_connection_next_timeout (GstRTSPConnection * conn, GTimeVal * timeout)
2163 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2164 g_return_val_if_fail (timeout != NULL, GST_RTSP_EINVAL);
2166 elapsed = g_timer_elapsed (conn->timer, &usec);
2167 if (elapsed >= conn->timeout) {
2171 sec = conn->timeout - elapsed;
2174 timeout->tv_sec = sec;
2175 timeout->tv_usec = usec;
2181 * gst_rtsp_connection_reset_timeout:
2182 * @conn: a #GstRTSPConnection
2184 * Reset the timeout of @conn.
2186 * Returns: #GST_RTSP_OK.
2189 gst_rtsp_connection_reset_timeout (GstRTSPConnection * conn)
2191 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2193 g_timer_start (conn->timer);
2199 * gst_rtsp_connection_flush:
2200 * @conn: a #GstRTSPConnection
2201 * @flush: start or stop the flush
2203 * Start or stop the flushing action on @conn. When flushing, all current
2204 * and future actions on @conn will return #GST_RTSP_EINTR until the connection
2205 * is set to non-flushing mode again.
2207 * Returns: #GST_RTSP_OK.
2210 gst_rtsp_connection_flush (GstRTSPConnection * conn, gboolean flush)
2212 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2214 gst_poll_set_flushing (conn->fdset, flush);
2220 * gst_rtsp_connection_set_proxy:
2221 * @conn: a #GstRTSPConnection
2222 * @host: the proxy host
2223 * @port: the proxy port
2225 * Set the proxy host and port.
2227 * Returns: #GST_RTSP_OK.
2232 gst_rtsp_connection_set_proxy (GstRTSPConnection * conn,
2233 const gchar * host, guint port)
2235 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2237 g_free (conn->proxy_host);
2238 conn->proxy_host = g_strdup (host);
2239 conn->proxy_port = port;
2245 * gst_rtsp_connection_set_auth:
2246 * @conn: a #GstRTSPConnection
2247 * @method: authentication method
2249 * @pass: the password
2251 * Configure @conn for authentication mode @method with @user and @pass as the
2252 * user and password respectively.
2254 * Returns: #GST_RTSP_OK.
2257 gst_rtsp_connection_set_auth (GstRTSPConnection * conn,
2258 GstRTSPAuthMethod method, const gchar * user, const gchar * pass)
2260 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2262 if (method == GST_RTSP_AUTH_DIGEST && ((user == NULL || pass == NULL)
2263 || g_strrstr (user, ":") != NULL))
2264 return GST_RTSP_EINVAL;
2266 /* Make sure the username and passwd are being set for authentication */
2267 if (method == GST_RTSP_AUTH_NONE && (user == NULL || pass == NULL))
2268 return GST_RTSP_EINVAL;
2270 /* ":" chars are not allowed in usernames for basic auth */
2271 if (method == GST_RTSP_AUTH_BASIC && g_strrstr (user, ":") != NULL)
2272 return GST_RTSP_EINVAL;
2274 g_free (conn->username);
2275 g_free (conn->passwd);
2277 conn->auth_method = method;
2278 conn->username = g_strdup (user);
2279 conn->passwd = g_strdup (pass);
2286 * @key: ASCII string to hash
2288 * Hashes @key in a case-insensitive manner.
2290 * Returns: the hash code.
2293 str_case_hash (gconstpointer key)
2295 const char *p = key;
2296 guint h = g_ascii_toupper (*p);
2299 for (p += 1; *p != '\0'; p++)
2300 h = (h << 5) - h + g_ascii_toupper (*p);
2307 * @v1: an ASCII string
2308 * @v2: another ASCII string
2310 * Compares @v1 and @v2 in a case-insensitive manner
2312 * Returns: %TRUE if they are equal (modulo case)
2315 str_case_equal (gconstpointer v1, gconstpointer v2)
2317 const char *string1 = v1;
2318 const char *string2 = v2;
2320 return g_ascii_strcasecmp (string1, string2) == 0;
2324 * gst_rtsp_connection_set_auth_param:
2325 * @conn: a #GstRTSPConnection
2326 * @param: authentication directive
2329 * Setup @conn with authentication directives. This is not necesary for
2330 * methods #GST_RTSP_AUTH_NONE and #GST_RTSP_AUTH_BASIC. For
2331 * #GST_RTSP_AUTH_DIGEST, directives should be taken from the digest challenge
2332 * in the WWW-Authenticate response header and can include realm, domain,
2333 * nonce, opaque, stale, algorithm, qop as per RFC2617.
2338 gst_rtsp_connection_set_auth_param (GstRTSPConnection * conn,
2339 const gchar * param, const gchar * value)
2341 g_return_if_fail (conn != NULL);
2342 g_return_if_fail (param != NULL);
2344 if (conn->auth_params == NULL) {
2346 g_hash_table_new_full (str_case_hash, str_case_equal, g_free, g_free);
2348 g_hash_table_insert (conn->auth_params, g_strdup (param), g_strdup (value));
2352 * gst_rtsp_connection_clear_auth_params:
2353 * @conn: a #GstRTSPConnection
2355 * Clear the list of authentication directives stored in @conn.
2360 gst_rtsp_connection_clear_auth_params (GstRTSPConnection * conn)
2362 g_return_if_fail (conn != NULL);
2364 if (conn->auth_params != NULL) {
2365 g_hash_table_destroy (conn->auth_params);
2366 conn->auth_params = NULL;
2370 static GstRTSPResult
2371 set_qos_dscp (gint fd, guint qos_dscp)
2373 union gst_sockaddr sa;
2374 socklen_t slen = sizeof (sa);
2381 if (getsockname (fd, &sa.sa, &slen) < 0)
2382 goto no_getsockname;
2384 af = sa.sa.sa_family;
2386 /* if this is an IPv4-mapped address then do IPv4 QoS */
2387 if (af == AF_INET6) {
2388 if (IN6_IS_ADDR_V4MAPPED (&sa.sa_in6.sin6_addr))
2392 /* extract and shift 6 bits of the DSCP */
2393 tos = (qos_dscp & 0x3f) << 2;
2397 if (SETSOCKOPT (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0)
2402 if (SETSOCKOPT (fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof (tos)) < 0)
2416 return GST_RTSP_ESYS;
2421 return GST_RTSP_ERROR;
2426 * gst_rtsp_connection_set_qos_dscp:
2427 * @conn: a #GstRTSPConnection
2428 * @qos_dscp: DSCP value
2430 * Configure @conn to use the specified DSCP value.
2432 * Returns: #GST_RTSP_OK on success.
2437 gst_rtsp_connection_set_qos_dscp (GstRTSPConnection * conn, guint qos_dscp)
2441 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2442 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2443 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2445 res = set_qos_dscp (conn->fd0.fd, qos_dscp);
2446 if (res == GST_RTSP_OK)
2447 res = set_qos_dscp (conn->fd1.fd, qos_dscp);
2454 * gst_rtsp_connection_get_url:
2455 * @conn: a #GstRTSPConnection
2457 * Retrieve the URL of the other end of @conn.
2459 * Returns: The URL. This value remains valid until the
2460 * connection is freed.
2465 gst_rtsp_connection_get_url (const GstRTSPConnection * conn)
2467 g_return_val_if_fail (conn != NULL, NULL);
2473 * gst_rtsp_connection_get_ip:
2474 * @conn: a #GstRTSPConnection
2476 * Retrieve the IP address of the other end of @conn.
2478 * Returns: The IP address as a string. this value remains valid until the
2479 * connection is closed.
2484 gst_rtsp_connection_get_ip (const GstRTSPConnection * conn)
2486 g_return_val_if_fail (conn != NULL, NULL);
2492 * gst_rtsp_connection_set_ip:
2493 * @conn: a #GstRTSPConnection
2494 * @ip: an ip address
2496 * Set the IP address of the server.
2501 gst_rtsp_connection_set_ip (GstRTSPConnection * conn, const gchar * ip)
2503 g_return_if_fail (conn != NULL);
2506 conn->ip = g_strdup (ip);
2510 * gst_rtsp_connection_get_readfd:
2511 * @conn: a #GstRTSPConnection
2513 * Get the file descriptor for reading.
2515 * Returns: the file descriptor used for reading or -1 on error. The file
2516 * descriptor remains valid until the connection is closed.
2521 gst_rtsp_connection_get_readfd (const GstRTSPConnection * conn)
2523 g_return_val_if_fail (conn != NULL, -1);
2524 g_return_val_if_fail (conn->readfd != NULL, -1);
2526 return conn->readfd->fd;
2530 * gst_rtsp_connection_get_writefd:
2531 * @conn: a #GstRTSPConnection
2533 * Get the file descriptor for writing.
2535 * Returns: the file descriptor used for writing or -1 on error. The file
2536 * descriptor remains valid until the connection is closed.
2541 gst_rtsp_connection_get_writefd (const GstRTSPConnection * conn)
2543 g_return_val_if_fail (conn != NULL, -1);
2544 g_return_val_if_fail (conn->writefd != NULL, -1);
2546 return conn->writefd->fd;
2551 * gst_rtsp_connection_set_tunneled:
2552 * @conn: a #GstRTSPConnection
2553 * @tunneled: the new state
2555 * Set the HTTP tunneling state of the connection. This must be configured before
2556 * the @conn is connected.
2561 gst_rtsp_connection_set_tunneled (GstRTSPConnection * conn, gboolean tunneled)
2563 g_return_if_fail (conn != NULL);
2564 g_return_if_fail (conn->readfd == NULL);
2565 g_return_if_fail (conn->writefd == NULL);
2567 conn->tunneled = tunneled;
2571 * gst_rtsp_connection_is_tunneled:
2572 * @conn: a #GstRTSPConnection
2574 * Get the tunneling state of the connection.
2576 * Returns: if @conn is using HTTP tunneling.
2581 gst_rtsp_connection_is_tunneled (const GstRTSPConnection * conn)
2583 g_return_val_if_fail (conn != NULL, FALSE);
2585 return conn->tunneled;
2589 * gst_rtsp_connection_get_tunnelid:
2590 * @conn: a #GstRTSPConnection
2592 * Get the tunnel session id the connection.
2594 * Returns: returns a non-empty string if @conn is being tunneled over HTTP.
2599 gst_rtsp_connection_get_tunnelid (const GstRTSPConnection * conn)
2601 g_return_val_if_fail (conn != NULL, NULL);
2603 if (!conn->tunneled)
2606 return conn->tunnelid;
2610 * gst_rtsp_connection_do_tunnel:
2611 * @conn: a #GstRTSPConnection
2612 * @conn2: a #GstRTSPConnection
2614 * If @conn received the first tunnel connection and @conn2 received
2615 * the second tunnel connection, link the two connections together so that
2616 * @conn manages the tunneled connection.
2618 * After this call, @conn2 cannot be used anymore and must be freed with
2619 * gst_rtsp_connection_free().
2621 * Returns: return GST_RTSP_OK on success.
2626 gst_rtsp_connection_do_tunnel (GstRTSPConnection * conn,
2627 GstRTSPConnection * conn2)
2629 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2630 g_return_val_if_fail (conn2 != NULL, GST_RTSP_EINVAL);
2631 g_return_val_if_fail (conn->tstate == TUNNEL_STATE_GET, GST_RTSP_EINVAL);
2632 g_return_val_if_fail (conn2->tstate == TUNNEL_STATE_POST, GST_RTSP_EINVAL);
2633 g_return_val_if_fail (!memcmp (conn2->tunnelid, conn->tunnelid, TUNNELID_LEN),
2636 /* both connections have fd0 as the read/write socket. start by taking the
2637 * socket from conn2 and set it as the socket in conn */
2638 conn->fd1 = conn2->fd0;
2640 /* clean up some of the state of conn2 */
2641 gst_poll_remove_fd (conn2->fdset, &conn2->fd0);
2643 conn2->readfd = conn2->writefd = NULL;
2645 /* We make fd0 the write socket and fd1 the read socket. */
2646 conn->writefd = &conn->fd0;
2647 conn->readfd = &conn->fd1;
2649 conn->tstate = TUNNEL_STATE_COMPLETE;
2651 /* we need base64 decoding for the readfd */
2652 conn->ctx.state = 0;
2655 conn->ctx.coutl = 0;
2656 conn->ctxp = &conn->ctx;
2661 #define READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
2662 #define WRITE_COND (G_IO_OUT | G_IO_ERR)
2671 /* async functions */
2672 struct _GstRTSPWatch
2676 GstRTSPConnection *conn;
2678 GstRTSPBuilder builder;
2679 GstRTSPMessage message;
2683 gboolean write_added;
2685 /* queued message for transmission */
2687 GAsyncQueue *messages;
2693 GstRTSPWatchFuncs funcs;
2696 GDestroyNotify notify;
2700 gst_rtsp_source_prepare (GSource * source, gint * timeout)
2702 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2704 *timeout = (watch->conn->timeout * 1000);
2710 gst_rtsp_source_check (GSource * source)
2712 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2714 if (watch->readfd.revents & READ_COND)
2717 if (watch->writefd.revents & WRITE_COND)
2724 gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback G_GNUC_UNUSED,
2725 gpointer user_data G_GNUC_UNUSED)
2727 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2730 /* first read as much as we can */
2731 if (watch->readfd.revents & READ_COND) {
2733 res = build_next (&watch->builder, &watch->message, watch->conn);
2734 if (res == GST_RTSP_EINTR)
2736 if (G_UNLIKELY (res == GST_RTSP_EEOF))
2738 if (res == GST_RTSP_ETGET) {
2740 GstRTSPStatusCode code;
2743 if (watch->funcs.tunnel_start)
2744 code = watch->funcs.tunnel_start (watch, watch->user_data);
2746 code = GST_RTSP_STS_OK;
2748 /* queue the response string */
2749 str = gen_tunnel_reply (watch->conn, code);
2751 gst_rtsp_watch_queue_data (watch, (guint8 *) g_string_free (str, FALSE),
2753 } else if (res == GST_RTSP_ETPOST) {
2754 /* in the callback the connection should be tunneled with the
2756 if (watch->funcs.tunnel_complete)
2757 watch->funcs.tunnel_complete (watch, watch->user_data);
2758 } else if (G_UNLIKELY (res != GST_RTSP_OK))
2761 if (G_LIKELY (res == GST_RTSP_OK)) {
2762 if (watch->funcs.message_received)
2763 watch->funcs.message_received (watch, &watch->message,
2766 gst_rtsp_message_unset (&watch->message);
2768 build_reset (&watch->builder);
2772 if (watch->writefd.revents & WRITE_COND) {
2774 if (watch->write_data == NULL) {
2777 /* get a new message from the queue */
2778 rec = g_async_queue_try_pop (watch->messages);
2782 watch->write_off = 0;
2783 watch->write_data = rec->data;
2784 watch->write_size = rec->size;
2785 watch->write_id = rec->id;
2787 g_slice_free (GstRTSPRec, rec);
2790 res = write_bytes (watch->writefd.fd, watch->write_data,
2791 &watch->write_off, watch->write_size);
2792 if (res == GST_RTSP_EINTR)
2794 if (G_UNLIKELY (res != GST_RTSP_OK))
2797 if (watch->funcs.message_sent)
2798 watch->funcs.message_sent (watch, watch->write_id, watch->user_data);
2801 if (g_async_queue_length (watch->messages) == 0 && watch->write_added) {
2802 g_source_remove_poll ((GSource *) watch, &watch->writefd);
2803 watch->write_added = FALSE;
2804 watch->writefd.revents = 0;
2806 g_free (watch->write_data);
2807 watch->write_data = NULL;
2816 if (watch->funcs.closed)
2817 watch->funcs.closed (watch, watch->user_data);
2822 if (watch->funcs.error)
2823 watch->funcs.error (watch, res, watch->user_data);
2829 gst_rtsp_rec_free (gpointer data)
2831 GstRTSPRec *rec = data;
2834 g_slice_free (GstRTSPRec, rec);
2838 gst_rtsp_source_finalize (GSource * source)
2840 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2842 build_reset (&watch->builder);
2843 gst_rtsp_message_unset (&watch->message);
2845 g_async_queue_unref (watch->messages);
2846 watch->messages = NULL;
2848 g_free (watch->write_data);
2851 watch->notify (watch->user_data);
2854 static GSourceFuncs gst_rtsp_source_funcs = {
2855 gst_rtsp_source_prepare,
2856 gst_rtsp_source_check,
2857 gst_rtsp_source_dispatch,
2858 gst_rtsp_source_finalize,
2864 * gst_rtsp_watch_new:
2865 * @conn: a #GstRTSPConnection
2866 * @funcs: watch functions
2867 * @user_data: user data to pass to @funcs
2868 * @notify: notify when @user_data is not referenced anymore
2870 * Create a watch object for @conn. The functions provided in @funcs will be
2871 * called with @user_data when activity happened on the watch.
2873 * The new watch is usually created so that it can be attached to a
2874 * maincontext with gst_rtsp_watch_attach().
2876 * @conn must exist for the entire lifetime of the watch.
2878 * Returns: a #GstRTSPWatch that can be used for asynchronous RTSP
2879 * communication. Free with gst_rtsp_watch_unref () after usage.
2884 gst_rtsp_watch_new (GstRTSPConnection * conn,
2885 GstRTSPWatchFuncs * funcs, gpointer user_data, GDestroyNotify notify)
2887 GstRTSPWatch *result;
2889 g_return_val_if_fail (conn != NULL, NULL);
2890 g_return_val_if_fail (funcs != NULL, NULL);
2891 g_return_val_if_fail (conn->readfd != NULL, NULL);
2892 g_return_val_if_fail (conn->writefd != NULL, NULL);
2894 result = (GstRTSPWatch *) g_source_new (&gst_rtsp_source_funcs,
2895 sizeof (GstRTSPWatch));
2897 result->conn = conn;
2898 result->builder.state = STATE_START;
2900 result->messages = g_async_queue_new_full (gst_rtsp_rec_free);
2902 result->readfd.fd = -1;
2903 result->writefd.fd = -1;
2905 gst_rtsp_watch_reset (result);
2907 result->funcs = *funcs;
2908 result->user_data = user_data;
2909 result->notify = notify;
2911 /* only add the read fd, the write fd is only added when we have data
2913 g_source_add_poll ((GSource *) result, &result->readfd);
2919 * gst_rtsp_watch_reset:
2920 * @watch: a #GstRTSPWatch
2922 * Reset @watch, this is usually called after gst_rtsp_connection_do_tunnel()
2923 * when the file descriptors of the connection might have changed.
2928 gst_rtsp_watch_reset (GstRTSPWatch * watch)
2930 if (watch->readfd.fd != -1)
2931 g_source_remove_poll ((GSource *) watch, &watch->readfd);
2932 if (watch->writefd.fd != -1)
2933 g_source_remove_poll ((GSource *) watch, &watch->writefd);
2935 watch->readfd.fd = watch->conn->readfd->fd;
2936 watch->readfd.events = READ_COND;
2937 watch->readfd.revents = 0;
2939 watch->writefd.fd = watch->conn->writefd->fd;
2940 watch->writefd.events = WRITE_COND;
2941 watch->writefd.revents = 0;
2942 watch->write_added = FALSE;
2944 g_source_add_poll ((GSource *) watch, &watch->readfd);
2948 * gst_rtsp_watch_attach:
2949 * @watch: a #GstRTSPWatch
2950 * @context: a GMainContext (if NULL, the default context will be used)
2952 * Adds a #GstRTSPWatch to a context so that it will be executed within that context.
2954 * Returns: the ID (greater than 0) for the watch within the GMainContext.
2959 gst_rtsp_watch_attach (GstRTSPWatch * watch, GMainContext * context)
2961 g_return_val_if_fail (watch != NULL, 0);
2963 return g_source_attach ((GSource *) watch, context);
2967 * gst_rtsp_watch_unref:
2968 * @watch: a #GstRTSPWatch
2970 * Decreases the reference count of @watch by one. If the resulting reference
2971 * count is zero the watch and associated memory will be destroyed.
2976 gst_rtsp_watch_unref (GstRTSPWatch * watch)
2978 g_return_if_fail (watch != NULL);
2980 g_source_unref ((GSource *) watch);
2984 * gst_rtsp_watch_queue_data:
2985 * @watch: a #GstRTSPWatch
2986 * @data: the data to queue
2987 * @size: the size of @data
2989 * Queue @data for transmission in @watch. It will be transmitted when the
2990 * connection of the @watch becomes writable.
2992 * This function will take ownership of @data and g_free() it after use.
2994 * The return value of this function will be used as the id argument in the
2995 * message_sent callback.
3002 gst_rtsp_watch_queue_data (GstRTSPWatch * watch, const guint8 * data,
3007 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3008 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
3009 g_return_val_if_fail (size != 0, GST_RTSP_EINVAL);
3011 /* make a record with the data and id */
3012 rec = g_slice_new (GstRTSPRec);
3013 rec->data = (guint8 *) data;
3016 /* make sure rec->id is never 0 */
3017 rec->id = ++watch->id;
3018 } while (G_UNLIKELY (rec->id == 0));
3020 /* add the record to a queue. FIXME we would like to have an upper limit here */
3021 g_async_queue_push (watch->messages, rec);
3023 /* FIXME: does the following need to be made thread-safe? (this might be
3024 * called from a streaming thread, like appsink's render function) */
3025 /* make sure the main context will now also check for writability on the
3027 if (!watch->write_added) {
3028 g_source_add_poll ((GSource *) watch, &watch->writefd);
3029 watch->write_added = TRUE;
3036 * gst_rtsp_watch_queue_message:
3037 * @watch: a #GstRTSPWatch
3038 * @message: a #GstRTSPMessage
3040 * Queue a @message for transmission in @watch. The contents of this
3041 * message will be serialized and transmitted when the connection of the
3042 * @watch becomes writable.
3044 * The return value of this function will be used as the id argument in the
3045 * message_sent callback.
3052 gst_rtsp_watch_queue_message (GstRTSPWatch * watch, GstRTSPMessage * message)
3057 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3058 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3060 /* make a record with the message as a string and id */
3061 str = message_to_string (watch->conn, message);
3063 return gst_rtsp_watch_queue_data (watch,
3064 (guint8 *) g_string_free (str, FALSE), size);