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 (GstRTSPConnection * conn, guint8 * buffer,
118 guint * idx, guint size);
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;
189 gchar *initial_buffer;
190 gsize initial_buffer_offset;
193 gint cseq; /* sequence number */
194 gchar session_id[512]; /* session id */
195 gint timeout; /* session timeout in seconds */
196 GTimer *timer; /* timeout timer */
199 GstRTSPAuthMethod auth_method;
202 GHashTable *auth_params;
221 /* a structure for constructing RTSPMessages */
234 build_reset (GstRTSPBuilder * builder)
236 g_free (builder->body_data);
237 memset (builder, 0, sizeof (GstRTSPBuilder));
241 * gst_rtsp_connection_create:
242 * @url: a #GstRTSPUrl
243 * @conn: storage for a #GstRTSPConnection
245 * Create a newly allocated #GstRTSPConnection from @url and store it in @conn.
246 * The connection will not yet attempt to connect to @url, use
247 * gst_rtsp_connection_connect().
249 * A copy of @url will be made.
251 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
254 gst_rtsp_connection_create (const GstRTSPUrl * url, GstRTSPConnection ** conn)
256 GstRTSPConnection *newconn;
262 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
265 error = WSAStartup (0x0202, &w);
270 if (w.wVersion != 0x0202)
274 newconn = g_new0 (GstRTSPConnection, 1);
276 if ((newconn->fdset = gst_poll_new (TRUE)) == NULL)
279 newconn->url = gst_rtsp_url_copy (url);
280 newconn->fd0.fd = -1;
281 newconn->fd1.fd = -1;
282 newconn->timer = g_timer_new ();
283 newconn->timeout = 60;
286 newconn->auth_method = GST_RTSP_AUTH_NONE;
287 newconn->username = NULL;
288 newconn->passwd = NULL;
289 newconn->auth_params = NULL;
299 g_warning ("Error %d on WSAStartup", error);
300 return GST_RTSP_EWSASTART;
304 g_warning ("Windows sockets are not version 0x202 (current 0x%x)",
307 return GST_RTSP_EWSAVERSION;
316 return GST_RTSP_ESYS;
321 * gst_rtsp_connection_create_from_fd:
322 * @fd: a file descriptor
323 * @ip: the IP address of the other end
324 * @port: the port used by the other end
325 * @initial_buffer: data already read from @fd
326 * @conn: storage for a #GstRTSPConnection
328 * Create a new #GstRTSPConnection for handling communication on the existing
329 * file descriptor @fd. The @initial_buffer contains any data already read from
330 * @fd which should be used before starting to read new data.
332 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
337 gst_rtsp_connection_create_from_fd (gint fd, const gchar * ip, guint16 port,
338 const gchar * initial_buffer, GstRTSPConnection ** conn)
340 GstRTSPConnection *newconn = NULL;
347 g_return_val_if_fail (fd >= 0, GST_RTSP_EINVAL);
348 g_return_val_if_fail (ip != NULL, GST_RTSP_EINVAL);
349 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
351 /* set to non-blocking mode so that we can cancel the communication */
353 fcntl (fd, F_SETFL, O_NONBLOCK);
355 ioctlsocket (fd, FIONBIO, &flags);
356 #endif /* G_OS_WIN32 */
358 /* create a url for the client address */
359 url = g_new0 (GstRTSPUrl, 1);
360 url->host = g_strdup (ip);
363 /* now create the connection object */
364 GST_RTSP_CHECK (gst_rtsp_connection_create (url, &newconn), newconn_failed);
365 gst_rtsp_url_free (url);
367 ADD_POLLFD (newconn->fdset, &newconn->fd0, fd);
369 /* both read and write initially */
370 newconn->readfd = &newconn->fd0;
371 newconn->writefd = &newconn->fd0;
373 newconn->ip = g_strdup (ip);
375 newconn->initial_buffer = g_strdup (initial_buffer);
384 gst_rtsp_url_free (url);
390 * gst_rtsp_connection_accept:
392 * @conn: storage for a #GstRTSPConnection
394 * Accept a new connection on @sock and create a new #GstRTSPConnection for
395 * handling communication on new socket.
397 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
402 gst_rtsp_connection_accept (gint sock, GstRTSPConnection ** conn)
405 union gst_sockaddr sa;
406 socklen_t slen = sizeof (sa);
407 gchar ip[INET6_ADDRSTRLEN];
410 g_return_val_if_fail (sock >= 0, GST_RTSP_EINVAL);
411 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
413 memset (&sa, 0, slen);
416 fd = accept (sock, &sa.sa, &slen);
418 fd = accept (sock, &sa.sa, (gint *) & slen);
419 #endif /* G_OS_WIN32 */
423 if (getnameinfo (&sa.sa, slen, ip, sizeof (ip), NULL, 0, NI_NUMERICHOST) != 0)
424 goto getnameinfo_failed;
426 if (sa.sa.sa_family == AF_INET)
427 port = sa.sa_in.sin_port;
428 else if (sa.sa.sa_family == AF_INET6)
429 port = sa.sa_in6.sin6_port;
433 return gst_rtsp_connection_create_from_fd (fd, ip, port, NULL, conn);
438 return GST_RTSP_ESYS;
444 return GST_RTSP_ERROR;
449 do_resolve (const gchar * host)
451 static gchar ip[INET6_ADDRSTRLEN];
452 struct addrinfo *aires;
456 aierr = getaddrinfo (host, NULL, NULL, &aires);
460 for (ai = aires; ai; ai = ai->ai_next) {
461 if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) {
468 aierr = getnameinfo (ai->ai_addr, ai->ai_addrlen, ip, sizeof (ip), NULL, 0,
469 NI_NUMERICHOST | NI_NUMERICSERV);
473 freeaddrinfo (aires);
475 return g_strdup (ip);
480 GST_ERROR ("no addrinfo found for %s: %s", host, gai_strerror (aierr));
485 GST_ERROR ("no family found for %s", host);
486 freeaddrinfo (aires);
491 GST_ERROR ("no address found for %s: %s", host, gai_strerror (aierr));
492 freeaddrinfo (aires);
498 do_connect (const gchar * ip, guint16 port, GstPollFD * fdout,
499 GstPoll * fdset, GTimeVal * timeout)
502 struct addrinfo hints;
503 struct addrinfo *aires;
506 gchar service[NI_MAXSERV];
509 unsigned long flags = 1;
510 #endif /* G_OS_WIN32 */
514 memset (&hints, 0, sizeof hints);
515 hints.ai_flags = AI_NUMERICHOST;
516 hints.ai_family = AF_UNSPEC;
517 hints.ai_socktype = SOCK_STREAM;
518 g_snprintf (service, sizeof (service) - 1, "%hu", port);
519 service[sizeof (service) - 1] = '\0';
521 aierr = getaddrinfo (ip, service, &hints, &aires);
525 for (ai = aires; ai; ai = ai->ai_next) {
526 if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) {
533 fd = socket (ai->ai_family, SOCK_STREAM, 0);
537 /* set to non-blocking mode so that we can cancel the connect */
539 fcntl (fd, F_SETFL, O_NONBLOCK);
541 ioctlsocket (fd, FIONBIO, &flags);
542 #endif /* G_OS_WIN32 */
544 /* add the socket to our fdset */
545 ADD_POLLFD (fdset, fdout, fd);
547 /* we are going to connect ASYNC now */
548 ret = connect (fd, ai->ai_addr, ai->ai_addrlen);
551 if (!ERRNO_IS_EINPROGRESS)
554 /* wait for connect to complete up to the specified timeout or until we got
556 gst_poll_fd_ctl_write (fdset, fdout, TRUE);
558 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
561 retval = gst_poll_wait (fdset, to);
562 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
566 else if (retval == -1)
569 /* we can still have an error connecting on windows */
570 if (gst_poll_fd_has_error (fdset, fdout)) {
571 socklen_t len = sizeof (errno);
573 getsockopt (fd, SOL_SOCKET, SO_ERROR, &errno, &len);
575 getsockopt (fd, SOL_SOCKET, SO_ERROR, (char *) &errno, &len);
580 gst_poll_fd_ignored (fdset, fdout);
583 freeaddrinfo (aires);
590 GST_ERROR ("no addrinfo found for %s: %s", ip, gai_strerror (aierr));
591 return GST_RTSP_ERROR;
595 GST_ERROR ("no family found for %s", ip);
596 freeaddrinfo (aires);
597 return GST_RTSP_ERROR;
601 GST_ERROR ("no socket %d (%s)", errno, g_strerror (errno));
602 freeaddrinfo (aires);
603 return GST_RTSP_ESYS;
607 GST_ERROR ("system error %d (%s)", errno, g_strerror (errno));
608 REMOVE_POLLFD (fdset, fdout);
609 freeaddrinfo (aires);
610 return GST_RTSP_ESYS;
614 GST_ERROR ("timeout");
615 REMOVE_POLLFD (fdset, fdout);
616 freeaddrinfo (aires);
617 return GST_RTSP_ETIMEOUT;
622 setup_tunneling (GstRTSPConnection * conn, GTimeVal * timeout)
630 gchar *ip, *url_port_str;
631 guint16 port, url_port;
632 gchar codestr[4], *resultstr;
637 /* create a random sessionid */
638 for (i = 0; i < TUNNELID_LEN; i++)
639 conn->tunnelid[i] = g_random_int_range ('a', 'z');
640 conn->tunnelid[TUNNELID_LEN - 1] = '\0';
643 /* get the port from the url */
644 gst_rtsp_url_get_port (url, &url_port);
646 if (conn->proxy_host) {
647 hostparam = g_strdup_printf ("Host: %s:%d\r\n", url->host, url_port);
648 url_port_str = g_strdup_printf (":%d", url_port);
649 ip = conn->proxy_host;
650 port = conn->proxy_port;
659 str = g_strdup_printf ("GET %s%s%s%s%s%s HTTP/1.0\r\n"
661 "x-sessioncookie: %s\r\n"
662 "Accept: application/x-rtsp-tunnelled\r\n"
663 "Pragma: no-cache\r\n"
664 "Cache-Control: no-cache\r\n" "\r\n",
665 conn->proxy_host ? "http://" : "",
666 conn->proxy_host ? url->host : "",
667 conn->proxy_host ? url_port_str : "",
668 url->abspath, url->query ? "?" : "", url->query ? url->query : "",
669 hostparam ? hostparam : "", conn->tunnelid);
671 /* we start by writing to this fd */
672 conn->writefd = &conn->fd0;
674 res = gst_rtsp_connection_write (conn, (guint8 *) str, strlen (str), timeout);
676 if (res != GST_RTSP_OK)
679 gst_poll_fd_ctl_write (conn->fdset, &conn->fd0, FALSE);
680 gst_poll_fd_ctl_read (conn->fdset, &conn->fd0, TRUE);
682 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
690 res = read_line (conn, buffer, &idx, sizeof (buffer));
691 if (res == GST_RTSP_EEOF)
693 if (res == GST_RTSP_OK)
695 if (res != GST_RTSP_EINTR)
699 retval = gst_poll_wait (conn->fdset, to);
700 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
702 /* check for timeout */
714 /* check for last line */
715 if (buffer[0] == '\r')
717 if (buffer[0] == '\0')
721 /* first line, parse response */
722 gchar versionstr[20];
725 bptr = (gchar *) buffer;
727 parse_string (versionstr, sizeof (versionstr), &bptr);
728 parse_string (codestr, sizeof (codestr), &bptr);
729 code = atoi (codestr);
731 while (g_ascii_isspace (*bptr))
736 if (code != GST_RTSP_STS_OK)
742 /* other lines, parse key/value */
743 res = parse_key_value (buffer, key, sizeof (key), &value);
744 if (res == GST_RTSP_OK) {
745 /* we got a new ip address */
746 if (g_ascii_strcasecmp (key, "x-server-ip-address") == 0) {
747 if (conn->proxy_host) {
748 /* if we use a proxy we need to change the destination url */
750 url->host = g_strdup (value);
752 g_free (url_port_str);
754 g_strdup_printf ("Host: %s:%d\r\n", url->host, url_port);
755 url_port_str = g_strdup_printf (":%d", url_port);
757 /* and resolve the new ip address */
758 if (!(ip = do_resolve (conn->ip)))
769 /* connect to the host/port */
770 res = do_connect (ip, port, &conn->fd1, conn->fdset, timeout);
771 if (res != GST_RTSP_OK)
774 /* this is now our writing socket */
775 conn->writefd = &conn->fd1;
778 str = g_strdup_printf ("POST %s%s%s%s%s%s HTTP/1.0\r\n"
780 "x-sessioncookie: %s\r\n"
781 "Content-Type: application/x-rtsp-tunnelled\r\n"
782 "Pragma: no-cache\r\n"
783 "Cache-Control: no-cache\r\n"
784 "Content-Length: 32767\r\n"
785 "Expires: Sun, 9 Jan 1972 00:00:00 GMT\r\n"
787 conn->proxy_host ? "http://" : "",
788 conn->proxy_host ? url->host : "",
789 conn->proxy_host ? url_port_str : "",
790 url->abspath, url->query ? "?" : "", url->query ? url->query : "",
791 hostparam ? hostparam : "", conn->tunnelid);
793 res = gst_rtsp_connection_write (conn, (guint8 *) str, strlen (str), timeout);
795 if (res != GST_RTSP_OK)
800 g_free (url_port_str);
807 GST_ERROR ("write failed (%d)", res);
821 res = GST_RTSP_ETIMEOUT;
831 res = GST_RTSP_EINTR;
836 GST_ERROR ("got failure response %d %s", code, resultstr);
837 res = GST_RTSP_ERROR;
842 GST_ERROR ("could not resolve %s", conn->ip);
848 GST_ERROR ("failed to connect");
854 * gst_rtsp_connection_connect:
855 * @conn: a #GstRTSPConnection
856 * @timeout: a #GTimeVal timeout
858 * Attempt to connect to the url of @conn made with
859 * gst_rtsp_connection_create(). If @timeout is #NULL this function can block
860 * forever. If @timeout contains a valid timeout, this function will return
861 * #GST_RTSP_ETIMEOUT after the timeout expired.
863 * This function can be cancelled with gst_rtsp_connection_flush().
865 * Returns: #GST_RTSP_OK when a connection could be made.
868 gst_rtsp_connection_connect (GstRTSPConnection * conn, GTimeVal * timeout)
875 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
876 g_return_val_if_fail (conn->url != NULL, GST_RTSP_EINVAL);
877 g_return_val_if_fail (conn->fd0.fd < 0, GST_RTSP_EINVAL);
881 if (conn->proxy_host && conn->tunneled) {
882 if (!(ip = do_resolve (conn->proxy_host))) {
883 GST_ERROR ("could not resolve %s", conn->proxy_host);
886 port = conn->proxy_port;
887 g_free (conn->proxy_host);
888 conn->proxy_host = ip;
890 if (!(ip = do_resolve (url->host))) {
891 GST_ERROR ("could not resolve %s", url->host);
894 /* get the port from the url */
895 gst_rtsp_url_get_port (url, &port);
901 /* connect to the host/port */
902 res = do_connect (ip, port, &conn->fd0, conn->fdset, timeout);
903 if (res != GST_RTSP_OK)
906 /* this is our read URL */
907 conn->readfd = &conn->fd0;
909 if (conn->tunneled) {
910 res = setup_tunneling (conn, timeout);
911 if (res != GST_RTSP_OK)
912 goto tunneling_failed;
914 conn->writefd = &conn->fd0;
921 return GST_RTSP_ENET;
925 GST_ERROR ("failed to connect");
930 GST_ERROR ("failed to setup tunneling");
936 auth_digest_compute_hex_urp (const gchar * username,
937 const gchar * realm, const gchar * password, gchar hex_urp[33])
939 GChecksum *md5_context = g_checksum_new (G_CHECKSUM_MD5);
940 const gchar *digest_string;
942 g_checksum_update (md5_context, (const guchar *) username, strlen (username));
943 g_checksum_update (md5_context, (const guchar *) ":", 1);
944 g_checksum_update (md5_context, (const guchar *) realm, strlen (realm));
945 g_checksum_update (md5_context, (const guchar *) ":", 1);
946 g_checksum_update (md5_context, (const guchar *) password, strlen (password));
947 digest_string = g_checksum_get_string (md5_context);
949 memset (hex_urp, 0, 33);
950 memcpy (hex_urp, digest_string, strlen (digest_string));
952 g_checksum_free (md5_context);
956 auth_digest_compute_response (const gchar * method,
957 const gchar * uri, const gchar * hex_a1, const gchar * nonce,
960 char hex_a2[33] = { 0, };
961 GChecksum *md5_context = g_checksum_new (G_CHECKSUM_MD5);
962 const gchar *digest_string;
965 g_checksum_update (md5_context, (const guchar *) method, strlen (method));
966 g_checksum_update (md5_context, (const guchar *) ":", 1);
967 g_checksum_update (md5_context, (const guchar *) uri, strlen (uri));
968 digest_string = g_checksum_get_string (md5_context);
969 memcpy (hex_a2, digest_string, strlen (digest_string));
972 #if GLIB_CHECK_VERSION (2, 18, 0)
973 g_checksum_reset (md5_context);
975 g_checksum_free (md5_context);
976 md5_context = g_checksum_new (G_CHECKSUM_MD5);
978 g_checksum_update (md5_context, (const guchar *) hex_a1, strlen (hex_a1));
979 g_checksum_update (md5_context, (const guchar *) ":", 1);
980 g_checksum_update (md5_context, (const guchar *) nonce, strlen (nonce));
981 g_checksum_update (md5_context, (const guchar *) ":", 1);
983 g_checksum_update (md5_context, (const guchar *) hex_a2, 32);
984 digest_string = g_checksum_get_string (md5_context);
985 memset (response, 0, 33);
986 memcpy (response, digest_string, strlen (digest_string));
988 g_checksum_free (md5_context);
992 add_auth_header (GstRTSPConnection * conn, GstRTSPMessage * message)
994 switch (conn->auth_method) {
995 case GST_RTSP_AUTH_BASIC:{
1000 user_pass = g_strdup_printf ("%s:%s", conn->username, conn->passwd);
1001 user_pass64 = g_base64_encode ((guchar *) user_pass, strlen (user_pass));
1002 auth_string = g_strdup_printf ("Basic %s", user_pass64);
1004 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
1008 g_free (user_pass64);
1011 case GST_RTSP_AUTH_DIGEST:{
1012 gchar response[33], hex_urp[33];
1013 gchar *auth_string, *auth_string2;
1018 const gchar *method;
1020 /* we need to have some params set */
1021 if (conn->auth_params == NULL)
1024 /* we need the realm and nonce */
1025 realm = (gchar *) g_hash_table_lookup (conn->auth_params, "realm");
1026 nonce = (gchar *) g_hash_table_lookup (conn->auth_params, "nonce");
1027 if (realm == NULL || nonce == NULL)
1030 auth_digest_compute_hex_urp (conn->username, realm, conn->passwd,
1033 method = gst_rtsp_method_as_text (message->type_data.request.method);
1034 uri = message->type_data.request.uri;
1036 /* Assume no qop, algorithm=md5, stale=false */
1037 /* For algorithm MD5, a1 = urp. */
1038 auth_digest_compute_response (method, uri, hex_urp, nonce, response);
1039 auth_string = g_strdup_printf ("Digest username=\"%s\", "
1040 "realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"",
1041 conn->username, realm, nonce, uri, response);
1043 opaque = (gchar *) g_hash_table_lookup (conn->auth_params, "opaque");
1045 auth_string2 = g_strdup_printf ("%s, opaque=\"%s\"", auth_string,
1047 g_free (auth_string);
1048 auth_string = auth_string2;
1050 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
1061 gen_date_string (gchar * date_string, guint len)
1065 #ifdef HAVE_GMTIME_R
1069 g_get_current_time (&tv);
1070 t = (time_t) tv.tv_sec;
1072 #ifdef HAVE_GMTIME_R
1073 strftime (date_string, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime_r (&t, &tm_));
1075 strftime (date_string, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime (&t));
1079 static GstRTSPResult
1080 write_bytes (gint fd, const guint8 * buffer, guint * idx, guint size)
1084 if (G_UNLIKELY (*idx > size))
1085 return GST_RTSP_ERROR;
1092 r = WRITE_SOCKET (fd, &buffer[*idx], left);
1093 if (G_UNLIKELY (r == 0)) {
1094 return GST_RTSP_EINTR;
1095 } else if (G_UNLIKELY (r < 0)) {
1096 if (ERRNO_IS_EAGAIN)
1097 return GST_RTSP_EINTR;
1098 if (!ERRNO_IS_EINTR)
1099 return GST_RTSP_ESYS;
1109 fill_raw_bytes (GstRTSPConnection * conn, guint8 * buffer, guint size)
1113 if (G_UNLIKELY (conn->initial_buffer != NULL)) {
1114 gsize left = strlen (&conn->initial_buffer[conn->initial_buffer_offset]);
1116 out = MIN (left, size);
1117 memcpy (buffer, &conn->initial_buffer[conn->initial_buffer_offset], out);
1119 if (left == (gsize) out) {
1120 g_free (conn->initial_buffer);
1121 conn->initial_buffer = NULL;
1122 conn->initial_buffer_offset = 0;
1124 conn->initial_buffer_offset += out;
1127 if (G_LIKELY (size > (guint) out)) {
1130 r = READ_SOCKET (conn->readfd->fd, &buffer[out], size - out);
1142 fill_bytes (GstRTSPConnection * conn, guint8 * buffer, guint size)
1144 DecodeCtx *ctx = conn->ctxp;
1149 guint8 in[sizeof (ctx->out) * 4 / 3];
1152 while (size > 0 && ctx->cout < ctx->coutl) {
1153 /* we have some leftover bytes */
1154 *buffer++ = ctx->out[ctx->cout++];
1159 /* got what we needed? */
1163 /* try to read more bytes */
1164 r = fill_raw_bytes (conn, in, sizeof (in));
1173 g_base64_decode_step ((gchar *) in, r, ctx->out, &ctx->state,
1177 out = fill_raw_bytes (conn, buffer, size);
1183 static GstRTSPResult
1184 read_bytes (GstRTSPConnection * conn, guint8 * buffer, guint * idx, guint size)
1188 if (G_UNLIKELY (*idx > size))
1189 return GST_RTSP_ERROR;
1196 r = fill_bytes (conn, &buffer[*idx], left);
1197 if (G_UNLIKELY (r == 0)) {
1198 return GST_RTSP_EEOF;
1199 } else if (G_UNLIKELY (r < 0)) {
1200 if (ERRNO_IS_EAGAIN)
1201 return GST_RTSP_EINTR;
1202 if (!ERRNO_IS_EINTR)
1203 return GST_RTSP_ESYS;
1212 static GstRTSPResult
1213 read_line (GstRTSPConnection * conn, guint8 * buffer, guint * idx, guint size)
1219 r = fill_bytes (conn, &c, 1);
1220 if (G_UNLIKELY (r == 0)) {
1221 return GST_RTSP_EEOF;
1222 } else if (G_UNLIKELY (r < 0)) {
1223 if (ERRNO_IS_EAGAIN)
1224 return GST_RTSP_EINTR;
1225 if (!ERRNO_IS_EINTR)
1226 return GST_RTSP_ESYS;
1228 if (c == '\n') /* end on \n */
1230 if (c == '\r') /* ignore \r */
1233 if (G_LIKELY (*idx < size - 1))
1234 buffer[(*idx)++] = c;
1237 buffer[*idx] = '\0';
1243 * gst_rtsp_connection_write:
1244 * @conn: a #GstRTSPConnection
1245 * @data: the data to write
1246 * @size: the size of @data
1247 * @timeout: a timeout value or #NULL
1249 * Attempt to write @size bytes of @data to the connected @conn, blocking up to
1250 * the specified @timeout. @timeout can be #NULL, in which case this function
1251 * might block forever.
1253 * This function can be cancelled with gst_rtsp_connection_flush().
1255 * Returns: #GST_RTSP_OK on success.
1258 gst_rtsp_connection_write (GstRTSPConnection * conn, const guint8 * data,
1259 guint size, GTimeVal * timeout)
1266 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1267 g_return_val_if_fail (data != NULL || size == 0, GST_RTSP_EINVAL);
1268 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
1270 gst_poll_set_controllable (conn->fdset, TRUE);
1271 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, TRUE);
1272 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, FALSE);
1273 /* clear all previous poll results */
1274 gst_poll_fd_ignored (conn->fdset, conn->writefd);
1275 gst_poll_fd_ignored (conn->fdset, conn->readfd);
1277 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1283 res = write_bytes (conn->writefd->fd, data, &offset, size);
1284 if (G_LIKELY (res == GST_RTSP_OK))
1286 if (G_UNLIKELY (res != GST_RTSP_EINTR))
1289 /* not all is written, wait until we can write more */
1291 retval = gst_poll_wait (conn->fdset, to);
1292 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1294 if (G_UNLIKELY (retval == 0))
1297 if (G_UNLIKELY (retval == -1)) {
1309 return GST_RTSP_ETIMEOUT;
1313 return GST_RTSP_ESYS;
1317 return GST_RTSP_EINTR;
1326 message_to_string (GstRTSPConnection * conn, GstRTSPMessage * message)
1328 GString *str = NULL;
1330 str = g_string_new ("");
1332 switch (message->type) {
1333 case GST_RTSP_MESSAGE_REQUEST:
1334 /* create request string, add CSeq */
1335 g_string_append_printf (str, "%s %s RTSP/1.0\r\n"
1337 gst_rtsp_method_as_text (message->type_data.request.method),
1338 message->type_data.request.uri, conn->cseq++);
1339 /* add session id if we have one */
1340 if (conn->session_id[0] != '\0') {
1341 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
1342 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SESSION,
1345 /* add any authentication headers */
1346 add_auth_header (conn, message);
1348 case GST_RTSP_MESSAGE_RESPONSE:
1349 /* create response string */
1350 g_string_append_printf (str, "RTSP/1.0 %d %s\r\n",
1351 message->type_data.response.code, message->type_data.response.reason);
1353 case GST_RTSP_MESSAGE_DATA:
1355 guint8 data_header[4];
1357 /* prepare data header */
1358 data_header[0] = '$';
1359 data_header[1] = message->type_data.data.channel;
1360 data_header[2] = (message->body_size >> 8) & 0xff;
1361 data_header[3] = message->body_size & 0xff;
1363 /* create string with header and data */
1364 str = g_string_append_len (str, (gchar *) data_header, 4);
1366 g_string_append_len (str, (gchar *) message->body,
1367 message->body_size);
1371 g_string_free (str, TRUE);
1372 g_return_val_if_reached (NULL);
1376 /* append headers and body */
1377 if (message->type != GST_RTSP_MESSAGE_DATA) {
1378 gchar date_string[100];
1380 gen_date_string (date_string, sizeof (date_string));
1382 /* add date header */
1383 gst_rtsp_message_remove_header (message, GST_RTSP_HDR_DATE, -1);
1384 gst_rtsp_message_add_header (message, GST_RTSP_HDR_DATE, date_string);
1386 /* append headers */
1387 gst_rtsp_message_append_headers (message, str);
1389 /* append Content-Length and body if needed */
1390 if (message->body != NULL && message->body_size > 0) {
1393 len = g_strdup_printf ("%d", message->body_size);
1394 g_string_append_printf (str, "%s: %s\r\n",
1395 gst_rtsp_header_as_text (GST_RTSP_HDR_CONTENT_LENGTH), len);
1397 /* header ends here */
1398 g_string_append (str, "\r\n");
1400 g_string_append_len (str, (gchar *) message->body,
1401 message->body_size);
1403 /* just end headers */
1404 g_string_append (str, "\r\n");
1412 * gst_rtsp_connection_send:
1413 * @conn: a #GstRTSPConnection
1414 * @message: the message to send
1415 * @timeout: a timeout value or #NULL
1417 * Attempt to send @message to the connected @conn, blocking up to
1418 * the specified @timeout. @timeout can be #NULL, in which case this function
1419 * might block forever.
1421 * This function can be cancelled with gst_rtsp_connection_flush().
1423 * Returns: #GST_RTSP_OK on success.
1426 gst_rtsp_connection_send (GstRTSPConnection * conn, GstRTSPMessage * message,
1429 GString *string = NULL;
1434 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1435 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1437 if (G_UNLIKELY (!(string = message_to_string (conn, message))))
1440 if (conn->tunneled) {
1441 str = g_base64_encode ((const guchar *) string->str, string->len);
1442 g_string_free (string, TRUE);
1447 g_string_free (string, FALSE);
1451 res = gst_rtsp_connection_write (conn, (guint8 *) str, len, timeout);
1459 g_warning ("Wrong message");
1460 return GST_RTSP_EINVAL;
1465 parse_string (gchar * dest, gint size, gchar ** src)
1471 while (g_ascii_isspace (**src))
1474 while (!g_ascii_isspace (**src) && **src != '\0') {
1476 dest[idx++] = **src;
1484 parse_key (gchar * dest, gint size, gchar ** src)
1489 while (**src != ':' && **src != '\0') {
1491 dest[idx++] = **src;
1498 static GstRTSPResult
1499 parse_response_status (guint8 * buffer, GstRTSPMessage * msg)
1502 gchar versionstr[20];
1507 bptr = (gchar *) buffer;
1509 parse_string (versionstr, sizeof (versionstr), &bptr);
1510 parse_string (codestr, sizeof (codestr), &bptr);
1511 code = atoi (codestr);
1513 while (g_ascii_isspace (*bptr))
1516 if (strcmp (versionstr, "RTSP/1.0") == 0)
1517 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
1519 else if (strncmp (versionstr, "RTSP/", 5) == 0) {
1520 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
1522 msg->type_data.response.version = GST_RTSP_VERSION_INVALID;
1530 return GST_RTSP_EPARSE;
1534 static GstRTSPResult
1535 parse_request_line (GstRTSPConnection * conn, guint8 * buffer,
1536 GstRTSPMessage * msg)
1538 GstRTSPResult res = GST_RTSP_OK;
1539 gchar versionstr[20];
1540 gchar methodstr[20];
1543 GstRTSPMethod method;
1544 GstRTSPTunnelState tstate = TUNNEL_STATE_NONE;
1546 bptr = (gchar *) buffer;
1548 parse_string (methodstr, sizeof (methodstr), &bptr);
1549 method = gst_rtsp_find_method (methodstr);
1550 if (method == GST_RTSP_INVALID) {
1551 /* a tunnel request is allowed when we don't have one yet */
1552 if (conn->tstate != TUNNEL_STATE_NONE)
1553 goto invalid_method;
1554 /* we need GET or POST for a valid tunnel request */
1555 if (!strcmp (methodstr, "GET"))
1556 tstate = TUNNEL_STATE_GET;
1557 else if (!strcmp (methodstr, "POST"))
1558 tstate = TUNNEL_STATE_POST;
1560 goto invalid_method;
1563 parse_string (urlstr, sizeof (urlstr), &bptr);
1564 if (G_UNLIKELY (*urlstr == '\0'))
1567 parse_string (versionstr, sizeof (versionstr), &bptr);
1569 if (G_UNLIKELY (*bptr != '\0'))
1570 goto invalid_version;
1572 if (strcmp (versionstr, "RTSP/1.0") == 0) {
1573 res = gst_rtsp_message_init_request (msg, method, urlstr);
1574 } else if (strncmp (versionstr, "RTSP/", 5) == 0) {
1575 res = gst_rtsp_message_init_request (msg, method, urlstr);
1576 msg->type_data.request.version = GST_RTSP_VERSION_INVALID;
1577 } else if (strcmp (versionstr, "HTTP/1.0") == 0) {
1578 /* tunnel request, we need a tunnel method */
1579 if (tstate == TUNNEL_STATE_NONE) {
1580 res = GST_RTSP_EPARSE;
1582 conn->tstate = tstate;
1585 res = GST_RTSP_EPARSE;
1593 GST_ERROR ("invalid method %s", methodstr);
1594 return GST_RTSP_EPARSE;
1598 GST_ERROR ("invalid url %s", urlstr);
1599 return GST_RTSP_EPARSE;
1603 GST_ERROR ("invalid version");
1604 return GST_RTSP_EPARSE;
1608 static GstRTSPResult
1609 parse_key_value (guint8 * buffer, gchar * key, guint keysize, gchar ** value)
1613 bptr = (gchar *) buffer;
1616 parse_key (key, keysize, &bptr);
1617 if (G_UNLIKELY (*bptr != ':'))
1621 while (g_ascii_isspace (*bptr))
1631 return GST_RTSP_EPARSE;
1635 /* parsing lines means reading a Key: Value pair */
1636 static GstRTSPResult
1637 parse_line (GstRTSPConnection * conn, guint8 * buffer, GstRTSPMessage * msg)
1642 GstRTSPHeaderField field;
1644 res = parse_key_value (buffer, key, sizeof (key), &value);
1645 if (G_UNLIKELY (res != GST_RTSP_OK))
1648 if (conn->tstate == TUNNEL_STATE_GET || conn->tstate == TUNNEL_STATE_POST) {
1649 /* save the tunnel session in the connection */
1650 if (!strcmp (key, "x-sessioncookie")) {
1651 strncpy (conn->tunnelid, value, TUNNELID_LEN);
1652 conn->tunnelid[TUNNELID_LEN - 1] = '\0';
1653 conn->tunneled = TRUE;
1656 field = gst_rtsp_find_header_field (key);
1657 if (field != GST_RTSP_HDR_INVALID)
1658 gst_rtsp_message_add_header (msg, field, value);
1671 * GST_RTSP_OK when a complete message was read.
1672 * GST_RTSP_EEOF: when the socket is closed
1673 * GST_RTSP_EINTR: when more data is needed.
1674 * GST_RTSP_..: some other error occured.
1676 static GstRTSPResult
1677 build_next (GstRTSPBuilder * builder, GstRTSPMessage * message,
1678 GstRTSPConnection * conn)
1683 switch (builder->state) {
1685 builder->offset = 0;
1687 read_bytes (conn, (guint8 *) builder->buffer, &builder->offset, 1);
1688 if (res != GST_RTSP_OK)
1691 /* we have 1 bytes now and we can see if this is a data message or
1693 if (builder->buffer[0] == '$') {
1694 /* data message, prepare for the header */
1695 builder->state = STATE_DATA_HEADER;
1698 builder->state = STATE_READ_LINES;
1701 case STATE_DATA_HEADER:
1704 read_bytes (conn, (guint8 *) builder->buffer, &builder->offset, 4);
1705 if (res != GST_RTSP_OK)
1708 gst_rtsp_message_init_data (message, builder->buffer[1]);
1710 builder->body_len = (builder->buffer[2] << 8) | builder->buffer[3];
1711 builder->body_data = g_malloc (builder->body_len + 1);
1712 builder->body_data[builder->body_len] = '\0';
1713 builder->offset = 0;
1714 builder->state = STATE_DATA_BODY;
1717 case STATE_DATA_BODY:
1720 read_bytes (conn, builder->body_data, &builder->offset,
1722 if (res != GST_RTSP_OK)
1725 /* we have the complete body now, store in the message adjusting the
1726 * length to include the traling '\0' */
1727 gst_rtsp_message_take_body (message,
1728 (guint8 *) builder->body_data, builder->body_len + 1);
1729 builder->body_data = NULL;
1730 builder->body_len = 0;
1732 builder->state = STATE_END;
1735 case STATE_READ_LINES:
1737 res = read_line (conn, builder->buffer, &builder->offset,
1738 sizeof (builder->buffer));
1739 if (res != GST_RTSP_OK)
1742 /* we have a regular response */
1743 if (builder->buffer[0] == '\r') {
1744 builder->buffer[0] = '\0';
1747 if (builder->buffer[0] == '\0') {
1750 /* empty line, end of message header */
1751 /* see if there is a Content-Length header */
1752 if (gst_rtsp_message_get_header (message,
1753 GST_RTSP_HDR_CONTENT_LENGTH, &hdrval, 0) == GST_RTSP_OK) {
1754 /* there is, prepare to read the body */
1755 builder->body_len = atol (hdrval);
1756 builder->body_data = g_malloc (builder->body_len + 1);
1757 builder->body_data[builder->body_len] = '\0';
1758 builder->offset = 0;
1759 builder->state = STATE_DATA_BODY;
1761 builder->state = STATE_END;
1766 /* we have a line */
1767 if (builder->line == 0) {
1768 /* first line, check for response status */
1769 if (memcmp (builder->buffer, "RTSP", 4) == 0) {
1770 res = parse_response_status (builder->buffer, message);
1772 res = parse_request_line (conn, builder->buffer, message);
1774 /* the first line must parse without errors */
1775 if (res != GST_RTSP_OK)
1778 /* else just parse the line, ignore errors */
1779 parse_line (conn, builder->buffer, message);
1782 builder->offset = 0;
1789 if (conn->tstate == TUNNEL_STATE_GET) {
1790 res = GST_RTSP_ETGET;
1792 } else if (conn->tstate == TUNNEL_STATE_POST) {
1793 res = GST_RTSP_ETPOST;
1797 if (message->type == GST_RTSP_MESSAGE_DATA) {
1798 /* data messages don't have headers */
1803 /* save session id in the connection for further use */
1804 if (message->type == GST_RTSP_MESSAGE_RESPONSE &&
1805 gst_rtsp_message_get_header (message, GST_RTSP_HDR_SESSION,
1806 &session_id, 0) == GST_RTSP_OK) {
1809 maxlen = sizeof (conn->session_id) - 1;
1810 /* the sessionid can have attributes marked with ;
1811 * Make sure we strip them */
1812 for (i = 0; session_id[i] != '\0'; i++) {
1813 if (session_id[i] == ';') {
1818 } while (g_ascii_isspace (session_id[i]));
1819 if (g_str_has_prefix (&session_id[i], "timeout=")) {
1822 /* if we parsed something valid, configure */
1823 if ((to = atoi (&session_id[i + 8])) > 0)
1830 /* make sure to not overflow */
1831 strncpy (conn->session_id, session_id, maxlen);
1832 conn->session_id[maxlen] = '\0';
1838 res = GST_RTSP_ERROR;
1847 * gst_rtsp_connection_read:
1848 * @conn: a #GstRTSPConnection
1849 * @data: the data to read
1850 * @size: the size of @data
1851 * @timeout: a timeout value or #NULL
1853 * Attempt to read @size bytes into @data from the connected @conn, blocking up to
1854 * the specified @timeout. @timeout can be #NULL, in which case this function
1855 * might block forever.
1857 * This function can be cancelled with gst_rtsp_connection_flush().
1859 * Returns: #GST_RTSP_OK on success.
1862 gst_rtsp_connection_read (GstRTSPConnection * conn, guint8 * data, guint size,
1870 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1871 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
1872 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1874 if (G_UNLIKELY (size == 0))
1879 /* configure timeout if any */
1880 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1882 gst_poll_set_controllable (conn->fdset, TRUE);
1883 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
1884 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
1887 res = read_bytes (conn, data, &offset, size);
1888 if (G_UNLIKELY (res == GST_RTSP_EEOF))
1890 if (G_LIKELY (res == GST_RTSP_OK))
1892 if (G_UNLIKELY (res != GST_RTSP_EINTR))
1896 retval = gst_poll_wait (conn->fdset, to);
1897 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1899 /* check for timeout */
1900 if (G_UNLIKELY (retval == 0))
1901 goto select_timeout;
1903 if (G_UNLIKELY (retval == -1)) {
1909 gst_poll_set_controllable (conn->fdset, FALSE);
1916 return GST_RTSP_ESYS;
1920 return GST_RTSP_ETIMEOUT;
1924 return GST_RTSP_EINTR;
1928 return GST_RTSP_EEOF;
1937 gen_tunnel_reply (GstRTSPConnection * conn, GstRTSPStatusCode code)
1940 gchar date_string[100];
1941 const gchar *status;
1943 gen_date_string (date_string, sizeof (date_string));
1945 status = gst_rtsp_status_as_text (code);
1946 if (status == NULL) {
1947 code = GST_RTSP_STS_INTERNAL_SERVER_ERROR;
1948 status = "Internal Server Error";
1951 str = g_string_new ("");
1954 g_string_append_printf (str, "HTTP/1.0 %d %s\r\n", code, status);
1955 g_string_append_printf (str,
1956 "Server: GStreamer RTSP Server\r\n"
1958 "Connection: close\r\n"
1959 "Cache-Control: no-store\r\n" "Pragma: no-cache\r\n", date_string);
1960 if (code == GST_RTSP_STS_OK) {
1962 g_string_append_printf (str, "x-server-ip-address: %s\r\n", conn->ip);
1963 g_string_append_printf (str,
1964 "Content-Type: application/x-rtsp-tunnelled\r\n");
1966 g_string_append_printf (str, "\r\n");
1971 * gst_rtsp_connection_receive:
1972 * @conn: a #GstRTSPConnection
1973 * @message: the message to read
1974 * @timeout: a timeout value or #NULL
1976 * Attempt to read into @message from the connected @conn, blocking up to
1977 * the specified @timeout. @timeout can be #NULL, in which case this function
1978 * might block forever.
1980 * This function can be cancelled with gst_rtsp_connection_flush().
1982 * Returns: #GST_RTSP_OK on success.
1985 gst_rtsp_connection_receive (GstRTSPConnection * conn, GstRTSPMessage * message,
1989 GstRTSPBuilder builder;
1993 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1994 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1995 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1997 /* configure timeout if any */
1998 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
2000 gst_poll_set_controllable (conn->fdset, TRUE);
2001 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
2002 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
2004 memset (&builder, 0, sizeof (GstRTSPBuilder));
2006 res = build_next (&builder, message, conn);
2007 if (G_UNLIKELY (res == GST_RTSP_EEOF))
2009 if (G_LIKELY (res == GST_RTSP_OK))
2011 if (res == GST_RTSP_ETGET) {
2014 /* tunnel GET request, we can reply now */
2015 str = gen_tunnel_reply (conn, GST_RTSP_STS_OK);
2017 gst_rtsp_connection_write (conn, (guint8 *) str->str, str->len,
2019 g_string_free (str, TRUE);
2020 } else if (res == GST_RTSP_ETPOST) {
2021 /* tunnel POST request, return the value, the caller now has to link the
2022 * two connections. */
2024 } else if (G_UNLIKELY (res != GST_RTSP_EINTR))
2028 retval = gst_poll_wait (conn->fdset, to);
2029 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
2031 /* check for timeout */
2032 if (G_UNLIKELY (retval == 0))
2033 goto select_timeout;
2035 if (G_UNLIKELY (retval == -1)) {
2041 gst_poll_set_controllable (conn->fdset, FALSE);
2044 /* we have a message here */
2045 build_reset (&builder);
2052 res = GST_RTSP_ESYS;
2057 res = GST_RTSP_ETIMEOUT;
2062 res = GST_RTSP_EINTR;
2067 res = GST_RTSP_EEOF;
2073 build_reset (&builder);
2074 gst_rtsp_message_unset (message);
2080 * gst_rtsp_connection_close:
2081 * @conn: a #GstRTSPConnection
2083 * Close the connected @conn. After this call, the connection is in the same
2084 * state as when it was first created.
2086 * Returns: #GST_RTSP_OK on success.
2089 gst_rtsp_connection_close (GstRTSPConnection * conn)
2091 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2096 g_free (conn->initial_buffer);
2097 conn->initial_buffer = NULL;
2098 conn->initial_buffer_offset = 0;
2100 REMOVE_POLLFD (conn->fdset, &conn->fd0);
2101 REMOVE_POLLFD (conn->fdset, &conn->fd1);
2102 conn->writefd = NULL;
2103 conn->readfd = NULL;
2104 conn->tunneled = FALSE;
2105 conn->tstate = TUNNEL_STATE_NONE;
2107 g_free (conn->username);
2108 conn->username = NULL;
2109 g_free (conn->passwd);
2110 conn->passwd = NULL;
2111 gst_rtsp_connection_clear_auth_params (conn);
2114 conn->session_id[0] = '\0';
2120 * gst_rtsp_connection_free:
2121 * @conn: a #GstRTSPConnection
2123 * Close and free @conn.
2125 * Returns: #GST_RTSP_OK on success.
2128 gst_rtsp_connection_free (GstRTSPConnection * conn)
2132 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2134 res = gst_rtsp_connection_close (conn);
2135 gst_poll_free (conn->fdset);
2136 g_timer_destroy (conn->timer);
2137 gst_rtsp_url_free (conn->url);
2138 g_free (conn->proxy_host);
2148 * gst_rtsp_connection_poll:
2149 * @conn: a #GstRTSPConnection
2150 * @events: a bitmask of #GstRTSPEvent flags to check
2151 * @revents: location for result flags
2152 * @timeout: a timeout
2154 * Wait up to the specified @timeout for the connection to become available for
2155 * at least one of the operations specified in @events. When the function returns
2156 * with #GST_RTSP_OK, @revents will contain a bitmask of available operations on
2159 * @timeout can be #NULL, in which case this function might block forever.
2161 * This function can be cancelled with gst_rtsp_connection_flush().
2163 * Returns: #GST_RTSP_OK on success.
2168 gst_rtsp_connection_poll (GstRTSPConnection * conn, GstRTSPEvent events,
2169 GstRTSPEvent * revents, GTimeVal * timeout)
2174 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2175 g_return_val_if_fail (events != 0, GST_RTSP_EINVAL);
2176 g_return_val_if_fail (revents != NULL, GST_RTSP_EINVAL);
2177 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2178 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2180 gst_poll_set_controllable (conn->fdset, TRUE);
2182 /* add fd to writer set when asked to */
2183 gst_poll_fd_ctl_write (conn->fdset, conn->writefd,
2184 events & GST_RTSP_EV_WRITE);
2186 /* add fd to reader set when asked to */
2187 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, events & GST_RTSP_EV_READ);
2189 /* configure timeout if any */
2190 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
2193 retval = gst_poll_wait (conn->fdset, to);
2194 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
2196 if (G_UNLIKELY (retval == 0))
2197 goto select_timeout;
2199 if (G_UNLIKELY (retval == -1)) {
2207 if (events & GST_RTSP_EV_READ) {
2208 if (gst_poll_fd_can_read (conn->fdset, conn->readfd))
2209 *revents |= GST_RTSP_EV_READ;
2211 if (events & GST_RTSP_EV_WRITE) {
2212 if (gst_poll_fd_can_write (conn->fdset, conn->writefd))
2213 *revents |= GST_RTSP_EV_WRITE;
2220 return GST_RTSP_ETIMEOUT;
2224 return GST_RTSP_ESYS;
2228 return GST_RTSP_EINTR;
2233 * gst_rtsp_connection_next_timeout:
2234 * @conn: a #GstRTSPConnection
2235 * @timeout: a timeout
2237 * Calculate the next timeout for @conn, storing the result in @timeout.
2239 * Returns: #GST_RTSP_OK.
2242 gst_rtsp_connection_next_timeout (GstRTSPConnection * conn, GTimeVal * timeout)
2248 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2249 g_return_val_if_fail (timeout != NULL, GST_RTSP_EINVAL);
2251 elapsed = g_timer_elapsed (conn->timer, &usec);
2252 if (elapsed >= conn->timeout) {
2256 sec = conn->timeout - elapsed;
2259 timeout->tv_sec = sec;
2260 timeout->tv_usec = usec;
2266 * gst_rtsp_connection_reset_timeout:
2267 * @conn: a #GstRTSPConnection
2269 * Reset the timeout of @conn.
2271 * Returns: #GST_RTSP_OK.
2274 gst_rtsp_connection_reset_timeout (GstRTSPConnection * conn)
2276 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2278 g_timer_start (conn->timer);
2284 * gst_rtsp_connection_flush:
2285 * @conn: a #GstRTSPConnection
2286 * @flush: start or stop the flush
2288 * Start or stop the flushing action on @conn. When flushing, all current
2289 * and future actions on @conn will return #GST_RTSP_EINTR until the connection
2290 * is set to non-flushing mode again.
2292 * Returns: #GST_RTSP_OK.
2295 gst_rtsp_connection_flush (GstRTSPConnection * conn, gboolean flush)
2297 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2299 gst_poll_set_flushing (conn->fdset, flush);
2305 * gst_rtsp_connection_set_proxy:
2306 * @conn: a #GstRTSPConnection
2307 * @host: the proxy host
2308 * @port: the proxy port
2310 * Set the proxy host and port.
2312 * Returns: #GST_RTSP_OK.
2317 gst_rtsp_connection_set_proxy (GstRTSPConnection * conn,
2318 const gchar * host, guint port)
2320 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2322 g_free (conn->proxy_host);
2323 conn->proxy_host = g_strdup (host);
2324 conn->proxy_port = port;
2330 * gst_rtsp_connection_set_auth:
2331 * @conn: a #GstRTSPConnection
2332 * @method: authentication method
2334 * @pass: the password
2336 * Configure @conn for authentication mode @method with @user and @pass as the
2337 * user and password respectively.
2339 * Returns: #GST_RTSP_OK.
2342 gst_rtsp_connection_set_auth (GstRTSPConnection * conn,
2343 GstRTSPAuthMethod method, const gchar * user, const gchar * pass)
2345 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2347 if (method == GST_RTSP_AUTH_DIGEST && ((user == NULL || pass == NULL)
2348 || g_strrstr (user, ":") != NULL))
2349 return GST_RTSP_EINVAL;
2351 /* Make sure the username and passwd are being set for authentication */
2352 if (method == GST_RTSP_AUTH_NONE && (user == NULL || pass == NULL))
2353 return GST_RTSP_EINVAL;
2355 /* ":" chars are not allowed in usernames for basic auth */
2356 if (method == GST_RTSP_AUTH_BASIC && g_strrstr (user, ":") != NULL)
2357 return GST_RTSP_EINVAL;
2359 g_free (conn->username);
2360 g_free (conn->passwd);
2362 conn->auth_method = method;
2363 conn->username = g_strdup (user);
2364 conn->passwd = g_strdup (pass);
2371 * @key: ASCII string to hash
2373 * Hashes @key in a case-insensitive manner.
2375 * Returns: the hash code.
2378 str_case_hash (gconstpointer key)
2380 const char *p = key;
2381 guint h = g_ascii_toupper (*p);
2384 for (p += 1; *p != '\0'; p++)
2385 h = (h << 5) - h + g_ascii_toupper (*p);
2392 * @v1: an ASCII string
2393 * @v2: another ASCII string
2395 * Compares @v1 and @v2 in a case-insensitive manner
2397 * Returns: %TRUE if they are equal (modulo case)
2400 str_case_equal (gconstpointer v1, gconstpointer v2)
2402 const char *string1 = v1;
2403 const char *string2 = v2;
2405 return g_ascii_strcasecmp (string1, string2) == 0;
2409 * gst_rtsp_connection_set_auth_param:
2410 * @conn: a #GstRTSPConnection
2411 * @param: authentication directive
2414 * Setup @conn with authentication directives. This is not necesary for
2415 * methods #GST_RTSP_AUTH_NONE and #GST_RTSP_AUTH_BASIC. For
2416 * #GST_RTSP_AUTH_DIGEST, directives should be taken from the digest challenge
2417 * in the WWW-Authenticate response header and can include realm, domain,
2418 * nonce, opaque, stale, algorithm, qop as per RFC2617.
2423 gst_rtsp_connection_set_auth_param (GstRTSPConnection * conn,
2424 const gchar * param, const gchar * value)
2426 g_return_if_fail (conn != NULL);
2427 g_return_if_fail (param != NULL);
2429 if (conn->auth_params == NULL) {
2431 g_hash_table_new_full (str_case_hash, str_case_equal, g_free, g_free);
2433 g_hash_table_insert (conn->auth_params, g_strdup (param), g_strdup (value));
2437 * gst_rtsp_connection_clear_auth_params:
2438 * @conn: a #GstRTSPConnection
2440 * Clear the list of authentication directives stored in @conn.
2445 gst_rtsp_connection_clear_auth_params (GstRTSPConnection * conn)
2447 g_return_if_fail (conn != NULL);
2449 if (conn->auth_params != NULL) {
2450 g_hash_table_destroy (conn->auth_params);
2451 conn->auth_params = NULL;
2455 static GstRTSPResult
2456 set_qos_dscp (gint fd, guint qos_dscp)
2458 union gst_sockaddr sa;
2459 socklen_t slen = sizeof (sa);
2466 if (getsockname (fd, &sa.sa, &slen) < 0)
2467 goto no_getsockname;
2469 af = sa.sa.sa_family;
2471 /* if this is an IPv4-mapped address then do IPv4 QoS */
2472 if (af == AF_INET6) {
2473 if (IN6_IS_ADDR_V4MAPPED (&sa.sa_in6.sin6_addr))
2477 /* extract and shift 6 bits of the DSCP */
2478 tos = (qos_dscp & 0x3f) << 2;
2482 if (SETSOCKOPT (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0)
2487 if (SETSOCKOPT (fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof (tos)) < 0)
2501 return GST_RTSP_ESYS;
2506 return GST_RTSP_ERROR;
2511 * gst_rtsp_connection_set_qos_dscp:
2512 * @conn: a #GstRTSPConnection
2513 * @qos_dscp: DSCP value
2515 * Configure @conn to use the specified DSCP value.
2517 * Returns: #GST_RTSP_OK on success.
2522 gst_rtsp_connection_set_qos_dscp (GstRTSPConnection * conn, guint qos_dscp)
2526 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2527 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2528 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2530 res = set_qos_dscp (conn->fd0.fd, qos_dscp);
2531 if (res == GST_RTSP_OK)
2532 res = set_qos_dscp (conn->fd1.fd, qos_dscp);
2539 * gst_rtsp_connection_get_url:
2540 * @conn: a #GstRTSPConnection
2542 * Retrieve the URL of the other end of @conn.
2544 * Returns: The URL. This value remains valid until the
2545 * connection is freed.
2550 gst_rtsp_connection_get_url (const GstRTSPConnection * conn)
2552 g_return_val_if_fail (conn != NULL, NULL);
2558 * gst_rtsp_connection_get_ip:
2559 * @conn: a #GstRTSPConnection
2561 * Retrieve the IP address of the other end of @conn.
2563 * Returns: The IP address as a string. this value remains valid until the
2564 * connection is closed.
2569 gst_rtsp_connection_get_ip (const GstRTSPConnection * conn)
2571 g_return_val_if_fail (conn != NULL, NULL);
2577 * gst_rtsp_connection_set_ip:
2578 * @conn: a #GstRTSPConnection
2579 * @ip: an ip address
2581 * Set the IP address of the server.
2586 gst_rtsp_connection_set_ip (GstRTSPConnection * conn, const gchar * ip)
2588 g_return_if_fail (conn != NULL);
2591 conn->ip = g_strdup (ip);
2595 * gst_rtsp_connection_get_readfd:
2596 * @conn: a #GstRTSPConnection
2598 * Get the file descriptor for reading.
2600 * Returns: the file descriptor used for reading or -1 on error. The file
2601 * descriptor remains valid until the connection is closed.
2606 gst_rtsp_connection_get_readfd (const GstRTSPConnection * conn)
2608 g_return_val_if_fail (conn != NULL, -1);
2609 g_return_val_if_fail (conn->readfd != NULL, -1);
2611 return conn->readfd->fd;
2615 * gst_rtsp_connection_get_writefd:
2616 * @conn: a #GstRTSPConnection
2618 * Get the file descriptor for writing.
2620 * Returns: the file descriptor used for writing or -1 on error. The file
2621 * descriptor remains valid until the connection is closed.
2626 gst_rtsp_connection_get_writefd (const GstRTSPConnection * conn)
2628 g_return_val_if_fail (conn != NULL, -1);
2629 g_return_val_if_fail (conn->writefd != NULL, -1);
2631 return conn->writefd->fd;
2636 * gst_rtsp_connection_set_tunneled:
2637 * @conn: a #GstRTSPConnection
2638 * @tunneled: the new state
2640 * Set the HTTP tunneling state of the connection. This must be configured before
2641 * the @conn is connected.
2646 gst_rtsp_connection_set_tunneled (GstRTSPConnection * conn, gboolean tunneled)
2648 g_return_if_fail (conn != NULL);
2649 g_return_if_fail (conn->readfd == NULL);
2650 g_return_if_fail (conn->writefd == NULL);
2652 conn->tunneled = tunneled;
2656 * gst_rtsp_connection_is_tunneled:
2657 * @conn: a #GstRTSPConnection
2659 * Get the tunneling state of the connection.
2661 * Returns: if @conn is using HTTP tunneling.
2666 gst_rtsp_connection_is_tunneled (const GstRTSPConnection * conn)
2668 g_return_val_if_fail (conn != NULL, FALSE);
2670 return conn->tunneled;
2674 * gst_rtsp_connection_get_tunnelid:
2675 * @conn: a #GstRTSPConnection
2677 * Get the tunnel session id the connection.
2679 * Returns: returns a non-empty string if @conn is being tunneled over HTTP.
2684 gst_rtsp_connection_get_tunnelid (const GstRTSPConnection * conn)
2686 g_return_val_if_fail (conn != NULL, NULL);
2688 if (!conn->tunneled)
2691 return conn->tunnelid;
2695 * gst_rtsp_connection_do_tunnel:
2696 * @conn: a #GstRTSPConnection
2697 * @conn2: a #GstRTSPConnection
2699 * If @conn received the first tunnel connection and @conn2 received
2700 * the second tunnel connection, link the two connections together so that
2701 * @conn manages the tunneled connection.
2703 * After this call, @conn2 cannot be used anymore and must be freed with
2704 * gst_rtsp_connection_free().
2706 * Returns: return GST_RTSP_OK on success.
2711 gst_rtsp_connection_do_tunnel (GstRTSPConnection * conn,
2712 GstRTSPConnection * conn2)
2714 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2715 g_return_val_if_fail (conn2 != NULL, GST_RTSP_EINVAL);
2716 g_return_val_if_fail (conn->tstate == TUNNEL_STATE_GET, GST_RTSP_EINVAL);
2717 g_return_val_if_fail (conn2->tstate == TUNNEL_STATE_POST, GST_RTSP_EINVAL);
2718 g_return_val_if_fail (!memcmp (conn2->tunnelid, conn->tunnelid, TUNNELID_LEN),
2721 /* both connections have fd0 as the read/write socket. start by taking the
2722 * socket from conn2 and set it as the socket in conn */
2723 conn->fd1 = conn2->fd0;
2725 /* clean up some of the state of conn2 */
2726 gst_poll_remove_fd (conn2->fdset, &conn2->fd0);
2728 conn2->readfd = conn2->writefd = NULL;
2730 /* We make fd0 the write socket and fd1 the read socket. */
2731 conn->writefd = &conn->fd0;
2732 conn->readfd = &conn->fd1;
2734 conn->tstate = TUNNEL_STATE_COMPLETE;
2736 /* we need base64 decoding for the readfd */
2737 conn->ctx.state = 0;
2740 conn->ctx.coutl = 0;
2741 conn->ctxp = &conn->ctx;
2746 #define READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
2747 #define WRITE_COND (G_IO_OUT | G_IO_ERR)
2756 /* async functions */
2757 struct _GstRTSPWatch
2761 GstRTSPConnection *conn;
2763 GstRTSPBuilder builder;
2764 GstRTSPMessage message;
2768 gboolean write_added;
2770 /* queued message for transmission */
2772 GAsyncQueue *messages;
2778 GstRTSPWatchFuncs funcs;
2781 GDestroyNotify notify;
2785 gst_rtsp_source_prepare (GSource * source, gint * timeout)
2787 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2789 if (watch->conn->initial_buffer != NULL)
2792 *timeout = (watch->conn->timeout * 1000);
2798 gst_rtsp_source_check (GSource * source)
2800 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2802 if (watch->readfd.revents & READ_COND)
2805 if (watch->writefd.revents & WRITE_COND)
2812 gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback G_GNUC_UNUSED,
2813 gpointer user_data G_GNUC_UNUSED)
2815 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2818 /* first read as much as we can */
2819 if (watch->readfd.revents & READ_COND || watch->conn->initial_buffer != NULL) {
2821 res = build_next (&watch->builder, &watch->message, watch->conn);
2822 if (res == GST_RTSP_EINTR)
2824 if (G_UNLIKELY (res == GST_RTSP_EEOF))
2826 if (res == GST_RTSP_ETGET) {
2828 GstRTSPStatusCode code;
2831 if (watch->funcs.tunnel_start)
2832 code = watch->funcs.tunnel_start (watch, watch->user_data);
2834 code = GST_RTSP_STS_OK;
2836 /* queue the response string */
2837 str = gen_tunnel_reply (watch->conn, code);
2839 gst_rtsp_watch_queue_data (watch, (guint8 *) g_string_free (str, FALSE),
2841 } else if (res == GST_RTSP_ETPOST) {
2842 /* in the callback the connection should be tunneled with the
2844 if (watch->funcs.tunnel_complete)
2845 watch->funcs.tunnel_complete (watch, watch->user_data);
2846 } else if (G_UNLIKELY (res != GST_RTSP_OK))
2849 if (G_LIKELY (res == GST_RTSP_OK)) {
2850 if (watch->funcs.message_received)
2851 watch->funcs.message_received (watch, &watch->message,
2854 gst_rtsp_message_unset (&watch->message);
2856 build_reset (&watch->builder);
2860 if (watch->writefd.revents & WRITE_COND) {
2862 if (watch->write_data == NULL) {
2865 /* get a new message from the queue */
2866 rec = g_async_queue_try_pop (watch->messages);
2870 watch->write_off = 0;
2871 watch->write_data = rec->data;
2872 watch->write_size = rec->size;
2873 watch->write_id = rec->id;
2875 g_slice_free (GstRTSPRec, rec);
2878 res = write_bytes (watch->writefd.fd, watch->write_data,
2879 &watch->write_off, watch->write_size);
2880 if (res == GST_RTSP_EINTR)
2882 if (G_UNLIKELY (res != GST_RTSP_OK))
2885 if (watch->funcs.message_sent)
2886 watch->funcs.message_sent (watch, watch->write_id, watch->user_data);
2889 if (g_async_queue_length (watch->messages) == 0 && watch->write_added) {
2890 g_source_remove_poll ((GSource *) watch, &watch->writefd);
2891 watch->write_added = FALSE;
2892 watch->writefd.revents = 0;
2894 g_free (watch->write_data);
2895 watch->write_data = NULL;
2904 if (watch->funcs.closed)
2905 watch->funcs.closed (watch, watch->user_data);
2910 if (watch->funcs.error)
2911 watch->funcs.error (watch, res, watch->user_data);
2917 gst_rtsp_rec_free (gpointer data)
2919 GstRTSPRec *rec = data;
2922 g_slice_free (GstRTSPRec, rec);
2926 gst_rtsp_source_finalize (GSource * source)
2928 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2930 build_reset (&watch->builder);
2931 gst_rtsp_message_unset (&watch->message);
2933 g_async_queue_unref (watch->messages);
2934 watch->messages = NULL;
2936 g_free (watch->write_data);
2939 watch->notify (watch->user_data);
2942 static GSourceFuncs gst_rtsp_source_funcs = {
2943 gst_rtsp_source_prepare,
2944 gst_rtsp_source_check,
2945 gst_rtsp_source_dispatch,
2946 gst_rtsp_source_finalize,
2952 * gst_rtsp_watch_new:
2953 * @conn: a #GstRTSPConnection
2954 * @funcs: watch functions
2955 * @user_data: user data to pass to @funcs
2956 * @notify: notify when @user_data is not referenced anymore
2958 * Create a watch object for @conn. The functions provided in @funcs will be
2959 * called with @user_data when activity happened on the watch.
2961 * The new watch is usually created so that it can be attached to a
2962 * maincontext with gst_rtsp_watch_attach().
2964 * @conn must exist for the entire lifetime of the watch.
2966 * Returns: a #GstRTSPWatch that can be used for asynchronous RTSP
2967 * communication. Free with gst_rtsp_watch_unref () after usage.
2972 gst_rtsp_watch_new (GstRTSPConnection * conn,
2973 GstRTSPWatchFuncs * funcs, gpointer user_data, GDestroyNotify notify)
2975 GstRTSPWatch *result;
2977 g_return_val_if_fail (conn != NULL, NULL);
2978 g_return_val_if_fail (funcs != NULL, NULL);
2979 g_return_val_if_fail (conn->readfd != NULL, NULL);
2980 g_return_val_if_fail (conn->writefd != NULL, NULL);
2982 result = (GstRTSPWatch *) g_source_new (&gst_rtsp_source_funcs,
2983 sizeof (GstRTSPWatch));
2985 result->conn = conn;
2986 result->builder.state = STATE_START;
2988 result->messages = g_async_queue_new_full (gst_rtsp_rec_free);
2990 result->readfd.fd = -1;
2991 result->writefd.fd = -1;
2993 gst_rtsp_watch_reset (result);
2995 result->funcs = *funcs;
2996 result->user_data = user_data;
2997 result->notify = notify;
2999 /* only add the read fd, the write fd is only added when we have data
3001 g_source_add_poll ((GSource *) result, &result->readfd);
3007 * gst_rtsp_watch_reset:
3008 * @watch: a #GstRTSPWatch
3010 * Reset @watch, this is usually called after gst_rtsp_connection_do_tunnel()
3011 * when the file descriptors of the connection might have changed.
3016 gst_rtsp_watch_reset (GstRTSPWatch * watch)
3018 if (watch->readfd.fd != -1)
3019 g_source_remove_poll ((GSource *) watch, &watch->readfd);
3020 if (watch->writefd.fd != -1)
3021 g_source_remove_poll ((GSource *) watch, &watch->writefd);
3023 watch->readfd.fd = watch->conn->readfd->fd;
3024 watch->readfd.events = READ_COND;
3025 watch->readfd.revents = 0;
3027 watch->writefd.fd = watch->conn->writefd->fd;
3028 watch->writefd.events = WRITE_COND;
3029 watch->writefd.revents = 0;
3030 watch->write_added = FALSE;
3032 g_source_add_poll ((GSource *) watch, &watch->readfd);
3036 * gst_rtsp_watch_attach:
3037 * @watch: a #GstRTSPWatch
3038 * @context: a GMainContext (if NULL, the default context will be used)
3040 * Adds a #GstRTSPWatch to a context so that it will be executed within that context.
3042 * Returns: the ID (greater than 0) for the watch within the GMainContext.
3047 gst_rtsp_watch_attach (GstRTSPWatch * watch, GMainContext * context)
3049 g_return_val_if_fail (watch != NULL, 0);
3051 return g_source_attach ((GSource *) watch, context);
3055 * gst_rtsp_watch_unref:
3056 * @watch: a #GstRTSPWatch
3058 * Decreases the reference count of @watch by one. If the resulting reference
3059 * count is zero the watch and associated memory will be destroyed.
3064 gst_rtsp_watch_unref (GstRTSPWatch * watch)
3066 g_return_if_fail (watch != NULL);
3068 g_source_unref ((GSource *) watch);
3072 * gst_rtsp_watch_queue_data:
3073 * @watch: a #GstRTSPWatch
3074 * @data: the data to queue
3075 * @size: the size of @data
3077 * Queue @data for transmission in @watch. It will be transmitted when the
3078 * connection of the @watch becomes writable.
3080 * This function will take ownership of @data and g_free() it after use.
3082 * The return value of this function will be used as the id argument in the
3083 * message_sent callback.
3090 gst_rtsp_watch_queue_data (GstRTSPWatch * watch, const guint8 * data,
3095 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3096 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
3097 g_return_val_if_fail (size != 0, GST_RTSP_EINVAL);
3099 /* make a record with the data and id */
3100 rec = g_slice_new (GstRTSPRec);
3101 rec->data = (guint8 *) data;
3104 /* make sure rec->id is never 0 */
3105 rec->id = ++watch->id;
3106 } while (G_UNLIKELY (rec->id == 0));
3108 /* add the record to a queue. FIXME we would like to have an upper limit here */
3109 g_async_queue_push (watch->messages, rec);
3111 /* FIXME: does the following need to be made thread-safe? (this might be
3112 * called from a streaming thread, like appsink's render function) */
3113 /* make sure the main context will now also check for writability on the
3115 if (!watch->write_added) {
3116 g_source_add_poll ((GSource *) watch, &watch->writefd);
3117 watch->write_added = TRUE;
3124 * gst_rtsp_watch_queue_message:
3125 * @watch: a #GstRTSPWatch
3126 * @message: a #GstRTSPMessage
3128 * Queue a @message for transmission in @watch. The contents of this
3129 * message will be serialized and transmitted when the connection of the
3130 * @watch becomes writable.
3132 * The return value of this function will be used as the id argument in the
3133 * message_sent callback.
3140 gst_rtsp_watch_queue_message (GstRTSPWatch * watch, GstRTSPMessage * message)
3145 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3146 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3148 /* make a record with the message as a string and id */
3149 str = message_to_string (watch->conn, message);
3151 return gst_rtsp_watch_queue_data (watch,
3152 (guint8 *) g_string_free (str, FALSE), size);