2 * Copyright (C) <2005,2006> Wim Taymans <wim@fluendo.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 * Unless otherwise indicated, Source Code is licensed under MIT license.
21 * See further explanation attached in License Statement (distributed in the file
24 * Permission is hereby granted, free of charge, to any person obtaining a copy of
25 * this software and associated documentation files (the "Software"), to deal in
26 * the Software without restriction, including without limitation the rights to
27 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
28 * of the Software, and to permit persons to whom the Software is furnished to do
29 * so, subject to the following conditions:
31 * The above copyright notice and this permission notice shall be included in all
32 * copies or substantial portions of the Software.
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
44 * SECTION:gstrtspconnection
45 * @short_description: manage RTSP connections
46 * @see_also: gstrtspurl
50 * This object manages the RTSP connection to the server. It provides function
51 * to receive and send bytes and messages.
55 * Last reviewed on 2007-07-24 (0.10.14)
73 /* we include this here to get the G_OS_* defines */
80 #define EINPROGRESS WSAEINPROGRESS
82 #include <sys/ioctl.h>
84 #include <sys/socket.h>
85 #include <netinet/in.h>
86 #include <arpa/inet.h>
90 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
91 #include <sys/filio.h>
94 #include "gstrtspconnection.h"
95 #include "gstrtspbase64.h"
99 #define READ_SOCKET(fd, buf, len) recv (fd, (char *)buf, len, 0)
100 #define WRITE_SOCKET(fd, buf, len) send (fd, (const char *)buf, len, 0)
101 #define SETSOCKOPT(sock, level, name, val, len) setsockopt (sock, level, name, (const char *)val, len)
102 #define CLOSE_SOCKET(sock) closesocket (sock)
103 #define ERRNO_IS_EAGAIN (WSAGetLastError () == WSAEWOULDBLOCK)
104 #define ERRNO_IS_EINTR (WSAGetLastError () == WSAEINTR)
105 /* According to Microsoft's connect() documentation this one returns
106 * WSAEWOULDBLOCK and not WSAEINPROGRESS. */
107 #define ERRNO_IS_EINPROGRESS (WSAGetLastError () == WSAEWOULDBLOCK)
109 #define READ_SOCKET(fd, buf, len) read (fd, buf, len)
110 #define WRITE_SOCKET(fd, buf, len) write (fd, buf, len)
111 #define SETSOCKOPT(sock, level, name, val, len) setsockopt (sock, level, name, val, len)
112 #define CLOSE_SOCKET(sock) close (sock)
113 #define ERRNO_IS_EAGAIN (errno == EAGAIN)
114 #define ERRNO_IS_EINTR (errno == EINTR)
115 #define ERRNO_IS_EINPROGRESS (errno == EINPROGRESS)
118 #define ADD_POLLFD(fdset, pfd, fd) \
121 gst_poll_add_fd (fdset, &pfd); \
124 #define REMOVE_POLLFD(fdset, pfd) \
126 if (pfd.fd != -1) { \
127 GST_DEBUG ("remove fd %d", pfd.fd); \
128 gst_poll_remove_fd (fdset, &pfd); \
129 CLOSE_SOCKET (pfd.fd); \
134 struct _GstRTSPConnection
137 /* URL for the connection */
140 /* connection state */
151 gint cseq; /* sequence number */
152 gchar session_id[512]; /* session id */
153 gint timeout; /* session timeout in seconds */
154 GTimer *timer; /* timeout timer */
157 GstRTSPAuthMethod auth_method;
160 GHashTable *auth_params;
165 inet_aton (const char *c, struct in_addr *paddr)
167 /* note that inet_addr is deprecated on unix because
168 * inet_addr returns -1 (INADDR_NONE) for the valid 255.255.255.255
170 paddr->s_addr = inet_addr (c);
172 if (paddr->s_addr == INADDR_NONE)
189 /* a structure for constructing RTSPMessages */
202 build_reset (GstRTSPBuilder * builder)
204 g_free (builder->body_data);
205 memset (builder, 0, sizeof (builder));
209 * gst_rtsp_connection_create:
210 * @url: a #GstRTSPUrl
211 * @conn: storage for a #GstRTSPConnection
213 * Create a newly allocated #GstRTSPConnection from @url and store it in @conn.
214 * The connection will not yet attempt to connect to @url, use
215 * gst_rtsp_connection_connect().
217 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
220 gst_rtsp_connection_create (GstRTSPUrl * url, GstRTSPConnection ** conn)
222 GstRTSPConnection *newconn;
228 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
231 error = WSAStartup (0x0202, &w);
236 if (w.wVersion != 0x0202)
240 newconn = g_new0 (GstRTSPConnection, 1);
242 if ((newconn->fdset = gst_poll_new (TRUE)) == NULL)
246 newconn->fd0.fd = -1;
247 newconn->fd1.fd = -1;
248 newconn->timer = g_timer_new ();
249 newconn->timeout = 60;
251 newconn->auth_method = GST_RTSP_AUTH_NONE;
252 newconn->username = NULL;
253 newconn->passwd = NULL;
254 newconn->auth_params = NULL;
264 g_warning ("Error %d on WSAStartup", error);
265 return GST_RTSP_EWSASTART;
269 g_warning ("Windows sockets are not version 0x202 (current 0x%x)",
272 return GST_RTSP_EWSAVERSION;
281 return GST_RTSP_ESYS;
286 * gst_rtsp_connection_accept:
288 * @conn: storage for a #GstRTSPConnection
290 * Accept a new connection on @sock and create a new #GstRTSPConnection for
291 * handling communication on new socket.
293 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
298 gst_rtsp_connection_accept (gint sock, GstRTSPConnection ** conn)
301 unsigned int address_len;
302 GstRTSPConnection *newconn = NULL;
303 struct sockaddr_in address;
309 address_len = sizeof (address);
310 memset (&address, 0, address_len);
313 fd = accept (sock, (struct sockaddr *) &address, &address_len);
315 fd = accept (sock, (struct sockaddr *) &address, (gint *) & address_len);
316 #endif /* G_OS_WIN32 */
320 /* set to non-blocking mode so that we can cancel the communication */
322 fcntl (fd, F_SETFL, O_NONBLOCK);
324 ioctlsocket (fd, FIONBIO, &flags);
325 #endif /* G_OS_WIN32 */
327 /* create a url for the client address */
328 url = g_new0 (GstRTSPUrl, 1);
329 url->host = g_strdup_printf ("%s", inet_ntoa (address.sin_addr));
330 url->port = address.sin_port;
332 /* now create the connection object */
333 gst_rtsp_connection_create (url, &newconn);
334 ADD_POLLFD (newconn->fdset, newconn->fd0, fd);
336 newconn->readfd = &newconn->fd0;
337 newconn->writefd = &newconn->fd0;
346 return GST_RTSP_ESYS;
351 * gst_rtsp_connection_connect:
352 * @conn: a #GstRTSPConnection
353 * @timeout: a #GTimeVal timeout
355 * Attempt to connect to the url of @conn made with
356 * gst_rtsp_connection_create(). If @timeout is #NULL this function can block
357 * forever. If @timeout contains a valid timeout, this function will return
358 * #GST_RTSP_ETIMEOUT after the timeout expired.
360 * This function can be cancelled with gst_rtsp_connection_flush().
362 * Returns: #GST_RTSP_OK when a connection could be made.
365 gst_rtsp_connection_connect (GstRTSPConnection * conn, GTimeVal * timeout)
368 struct sockaddr_in sa_in;
369 struct hostent *hostinfo;
379 unsigned long flags = 1;
380 struct in_addr *addrp;
383 gchar ipbuf[INET_ADDRSTRLEN];
384 #endif /* G_OS_WIN32 */
386 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
387 g_return_val_if_fail (conn->url != NULL, GST_RTSP_EINVAL);
388 g_return_val_if_fail (conn->fd0.fd < 0, GST_RTSP_EINVAL);
392 /* first check if it already is an IP address */
393 if (inet_aton (url->host, &addr)) {
396 hostinfo = gethostbyname (url->host);
398 goto not_resolved; /* h_errno set */
400 if (hostinfo->h_addrtype != AF_INET)
401 goto not_ip; /* host not an IP host */
403 addrp = (struct in_addr *) hostinfo->h_addr_list[0];
404 /* this is not threadsafe */
405 ip = inet_ntoa (*addrp);
407 addrs = hostinfo->h_addr_list;
408 ip = inet_ntop (AF_INET, (struct in_addr *) addrs[0], ipbuf,
410 #endif /* G_OS_WIN32 */
413 /* get the port from the url */
414 gst_rtsp_url_get_port (url, &port);
416 memset (&sa_in, 0, sizeof (sa_in));
417 sa_in.sin_family = AF_INET; /* network socket */
418 sa_in.sin_port = htons (port); /* on port */
419 sa_in.sin_addr.s_addr = inet_addr (ip); /* on host ip */
421 fd = socket (AF_INET, SOCK_STREAM, 0);
425 /* set to non-blocking mode so that we can cancel the connect */
427 fcntl (fd, F_SETFL, O_NONBLOCK);
429 ioctlsocket (fd, FIONBIO, &flags);
430 #endif /* G_OS_WIN32 */
432 /* add the socket to our fdset */
433 ADD_POLLFD (conn->fdset, conn->fd0, fd);
435 conn->readfd = &conn->fd0;
436 conn->writefd = &conn->fd0;
438 /* we are going to connect ASYNC now */
439 ret = connect (fd, (struct sockaddr *) &sa_in, sizeof (sa_in));
442 if (!ERRNO_IS_EINPROGRESS)
445 /* wait for connect to complete up to the specified timeout or until we got
447 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, TRUE);
449 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
452 retval = gst_poll_wait (conn->fdset, to);
453 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
457 else if (retval == -1)
460 /* we can still have an error connecting on windows */
461 if (gst_poll_fd_has_error (conn->fdset, conn->writefd)) {
462 socklen_t len = sizeof (errno);
464 getsockopt (fd, SOL_SOCKET, SO_ERROR, &errno, &len);
466 getsockopt (fd, SOL_SOCKET, SO_ERROR, (char *) &errno, &len);
471 gst_poll_fd_ignored (conn->fdset, conn->writefd);
472 gst_poll_fd_ignored (conn->fdset, conn->readfd);
475 conn->ip = g_strdup (ip);
481 GST_ERROR ("system error %d (%s)", errno, g_strerror (errno));
482 REMOVE_POLLFD (conn->fdset, conn->fd0);
483 REMOVE_POLLFD (conn->fdset, conn->fd1);
484 return GST_RTSP_ESYS;
488 GST_ERROR ("could not resolve %s", url->host);
489 return GST_RTSP_ENET;
493 GST_ERROR ("not an IP address");
494 return GST_RTSP_ENOTIP;
498 GST_ERROR ("timeout");
499 REMOVE_POLLFD (conn->fdset, conn->fd0);
500 REMOVE_POLLFD (conn->fdset, conn->fd1);
501 return GST_RTSP_ETIMEOUT;
506 md5_digest_to_hex_string (unsigned char digest[16], char string[33])
508 static const char hexdigits[] = "0123456789abcdef";
511 for (i = 0; i < 16; i++) {
512 string[i * 2] = hexdigits[(digest[i] >> 4) & 0x0f];
513 string[i * 2 + 1] = hexdigits[digest[i] & 0x0f];
519 auth_digest_compute_hex_urp (const gchar * username,
520 const gchar * realm, const gchar * password, gchar hex_urp[33])
522 struct MD5Context md5_context;
523 unsigned char digest[16];
525 MD5Init (&md5_context);
526 MD5Update (&md5_context, username, strlen (username));
527 MD5Update (&md5_context, ":", 1);
528 MD5Update (&md5_context, realm, strlen (realm));
529 MD5Update (&md5_context, ":", 1);
530 MD5Update (&md5_context, password, strlen (password));
531 MD5Final (digest, &md5_context);
532 md5_digest_to_hex_string (digest, hex_urp);
536 auth_digest_compute_response (const gchar * method,
537 const gchar * uri, const gchar * hex_a1, const gchar * nonce,
541 struct MD5Context md5_context;
542 unsigned char digest[16];
545 MD5Init (&md5_context);
546 MD5Update (&md5_context, method, strlen (method));
547 MD5Update (&md5_context, ":", 1);
548 MD5Update (&md5_context, uri, strlen (uri));
549 MD5Final (digest, &md5_context);
550 md5_digest_to_hex_string (digest, hex_a2);
553 MD5Init (&md5_context);
554 MD5Update (&md5_context, hex_a1, strlen (hex_a1));
555 MD5Update (&md5_context, ":", 1);
556 MD5Update (&md5_context, nonce, strlen (nonce));
557 MD5Update (&md5_context, ":", 1);
559 MD5Update (&md5_context, hex_a2, 32);
560 MD5Final (digest, &md5_context);
561 md5_digest_to_hex_string (digest, response);
565 add_auth_header (GstRTSPConnection * conn, GstRTSPMessage * message)
567 switch (conn->auth_method) {
568 case GST_RTSP_AUTH_BASIC:{
570 g_strdup_printf ("%s:%s", conn->username, conn->passwd);
572 gst_rtsp_base64_encode (user_pass, strlen (user_pass));
573 gchar *auth_string = g_strdup_printf ("Basic %s", user_pass64);
575 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
579 g_free (user_pass64);
582 case GST_RTSP_AUTH_DIGEST:{
583 gchar response[33], hex_urp[33];
584 gchar *auth_string, *auth_string2;
591 /* we need to have some params set */
592 if (conn->auth_params == NULL)
595 /* we need the realm and nonce */
596 realm = (gchar *) g_hash_table_lookup (conn->auth_params, "realm");
597 nonce = (gchar *) g_hash_table_lookup (conn->auth_params, "nonce");
598 if (realm == NULL || nonce == NULL)
601 auth_digest_compute_hex_urp (conn->username, realm, conn->passwd,
604 method = gst_rtsp_method_as_text (message->type_data.request.method);
605 uri = message->type_data.request.uri;
607 /* Assume no qop, algorithm=md5, stale=false */
608 /* For algorithm MD5, a1 = urp. */
609 auth_digest_compute_response (method, uri, hex_urp, nonce, response);
610 auth_string = g_strdup_printf ("Digest username=\"%s\", "
611 "realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"",
612 conn->username, realm, nonce, uri, response);
614 opaque = (gchar *) g_hash_table_lookup (conn->auth_params, "opaque");
616 auth_string2 = g_strdup_printf ("%s, opaque=\"%s\"", auth_string,
618 g_free (auth_string);
619 auth_string = auth_string2;
621 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
632 add_date_header (GstRTSPMessage * message)
635 gchar date_string[100];
642 g_get_current_time (&tv);
643 t = (time_t) tv.tv_sec;
646 strftime (date_string, sizeof (date_string), "%a, %d %b %Y %H:%M:%S GMT",
647 gmtime_r (&t, &tm_));
649 strftime (date_string, sizeof (date_string), "%a, %d %b %Y %H:%M:%S GMT",
653 gst_rtsp_message_add_header (message, GST_RTSP_HDR_DATE, date_string);
657 write_bytes (gint fd, const guint8 * buffer, guint * idx, guint size)
662 return GST_RTSP_ERROR;
669 r = WRITE_SOCKET (fd, &buffer[*idx], left);
671 return GST_RTSP_EINTR;
674 return GST_RTSP_EINTR;
676 return GST_RTSP_ESYS;
686 read_bytes (gint fd, guint8 * buffer, guint * idx, guint size)
691 return GST_RTSP_ERROR;
698 r = READ_SOCKET (fd, &buffer[*idx], left);
700 return GST_RTSP_EEOF;
703 return GST_RTSP_EINTR;
705 return GST_RTSP_ESYS;
715 read_line (gint fd, guint8 * buffer, guint * idx, guint size)
721 r = READ_SOCKET (fd, &c, 1);
723 return GST_RTSP_EEOF;
726 return GST_RTSP_EINTR;
728 return GST_RTSP_ESYS;
730 if (c == '\n') /* end on \n */
732 if (c == '\r') /* ignore \r */
736 buffer[(*idx)++] = c;
745 * gst_rtsp_connection_write:
746 * @conn: a #GstRTSPConnection
747 * @data: the data to write
748 * @size: the size of @data
749 * @timeout: a timeout value or #NULL
751 * Attempt to write @size bytes of @data to the connected @conn, blocking up to
752 * the specified @timeout. @timeout can be #NULL, in which case this function
753 * might block forever.
755 * This function can be cancelled with gst_rtsp_connection_flush().
757 * Returns: #GST_RTSP_OK on success.
760 gst_rtsp_connection_write (GstRTSPConnection * conn, const guint8 * data,
761 guint size, GTimeVal * timeout)
768 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
769 g_return_val_if_fail (data != NULL || size == 0, GST_RTSP_EINVAL);
770 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
772 gst_poll_set_controllable (conn->fdset, TRUE);
773 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, TRUE);
774 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, FALSE);
775 /* clear all previous poll results */
776 gst_poll_fd_ignored (conn->fdset, conn->writefd);
777 gst_poll_fd_ignored (conn->fdset, conn->readfd);
779 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
785 res = write_bytes (conn->writefd->fd, data, &offset, size);
786 if (res == GST_RTSP_OK)
788 if (res != GST_RTSP_EINTR)
791 /* not all is written, wait until we can write more */
793 retval = gst_poll_wait (conn->fdset, to);
794 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
811 return GST_RTSP_ETIMEOUT;
815 return GST_RTSP_ESYS;
819 return GST_RTSP_EINTR;
828 message_to_string (GstRTSPConnection * conn, GstRTSPMessage * message)
832 str = g_string_new ("");
834 switch (message->type) {
835 case GST_RTSP_MESSAGE_REQUEST:
836 /* create request string, add CSeq */
837 g_string_append_printf (str, "%s %s RTSP/1.0\r\n"
839 gst_rtsp_method_as_text (message->type_data.request.method),
840 message->type_data.request.uri, conn->cseq++);
841 /* add session id if we have one */
842 if (conn->session_id[0] != '\0') {
843 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SESSION,
846 /* add any authentication headers */
847 add_auth_header (conn, message);
849 case GST_RTSP_MESSAGE_RESPONSE:
850 /* create response string */
851 g_string_append_printf (str, "RTSP/1.0 %d %s\r\n",
852 message->type_data.response.code, message->type_data.response.reason);
854 case GST_RTSP_MESSAGE_DATA:
856 guint8 data_header[4];
858 /* prepare data header */
859 data_header[0] = '$';
860 data_header[1] = message->type_data.data.channel;
861 data_header[2] = (message->body_size >> 8) & 0xff;
862 data_header[3] = message->body_size & 0xff;
864 /* create string with header and data */
865 str = g_string_append_len (str, (gchar *) data_header, 4);
867 g_string_append_len (str, (gchar *) message->body,
872 g_string_free (str, TRUE);
873 g_return_val_if_reached (NULL);
877 /* append headers and body */
878 if (message->type != GST_RTSP_MESSAGE_DATA) {
879 /* add date header */
880 add_date_header (message);
883 gst_rtsp_message_append_headers (message, str);
885 /* append Content-Length and body if needed */
886 if (message->body != NULL && message->body_size > 0) {
889 len = g_strdup_printf ("%d", message->body_size);
890 g_string_append_printf (str, "%s: %s\r\n",
891 gst_rtsp_header_as_text (GST_RTSP_HDR_CONTENT_LENGTH), len);
893 /* header ends here */
894 g_string_append (str, "\r\n");
896 g_string_append_len (str, (gchar *) message->body,
899 /* just end headers */
900 g_string_append (str, "\r\n");
908 * gst_rtsp_connection_send:
909 * @conn: a #GstRTSPConnection
910 * @message: the message to send
911 * @timeout: a timeout value or #NULL
913 * Attempt to send @message to the connected @conn, blocking up to
914 * the specified @timeout. @timeout can be #NULL, in which case this function
915 * might block forever.
917 * This function can be cancelled with gst_rtsp_connection_flush().
919 * Returns: #GST_RTSP_OK on success.
922 gst_rtsp_connection_send (GstRTSPConnection * conn, GstRTSPMessage * message,
928 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
929 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
931 if (!(str = message_to_string (conn, message)))
936 gst_rtsp_connection_write (conn, (guint8 *) str->str, str->len, timeout);
938 g_string_free (str, TRUE);
944 g_warning ("Wrong message");
945 return GST_RTSP_EINVAL;
950 parse_string (gchar * dest, gint size, gchar ** src)
956 while (g_ascii_isspace (**src))
959 while (!g_ascii_isspace (**src) && **src != '\0') {
969 parse_key (gchar * dest, gint size, gchar ** src)
974 while (**src != ':' && **src != '\0') {
984 parse_response_status (guint8 * buffer, GstRTSPMessage * msg)
987 gchar versionstr[20];
992 bptr = (gchar *) buffer;
994 parse_string (versionstr, sizeof (versionstr), &bptr);
995 parse_string (codestr, sizeof (codestr), &bptr);
996 code = atoi (codestr);
998 while (g_ascii_isspace (*bptr))
1001 if (strcmp (versionstr, "RTSP/1.0") == 0)
1002 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
1004 else if (strncmp (versionstr, "RTSP/", 5) == 0) {
1005 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
1007 msg->type_data.response.version = GST_RTSP_VERSION_INVALID;
1015 return GST_RTSP_EPARSE;
1019 static GstRTSPResult
1020 parse_request_line (guint8 * buffer, GstRTSPMessage * msg)
1022 GstRTSPResult res = GST_RTSP_OK;
1023 gchar versionstr[20];
1024 gchar methodstr[20];
1027 GstRTSPMethod method;
1029 bptr = (gchar *) buffer;
1031 parse_string (methodstr, sizeof (methodstr), &bptr);
1032 method = gst_rtsp_find_method (methodstr);
1034 parse_string (urlstr, sizeof (urlstr), &bptr);
1035 if (*urlstr == '\0')
1036 res = GST_RTSP_EPARSE;
1038 parse_string (versionstr, sizeof (versionstr), &bptr);
1041 res = GST_RTSP_EPARSE;
1043 if (strcmp (versionstr, "RTSP/1.0") == 0) {
1044 if (gst_rtsp_message_init_request (msg, method, urlstr) != GST_RTSP_OK)
1045 res = GST_RTSP_EPARSE;
1046 } else if (strncmp (versionstr, "RTSP/", 5) == 0) {
1047 if (gst_rtsp_message_init_request (msg, method, urlstr) != GST_RTSP_OK)
1048 res = GST_RTSP_EPARSE;
1049 msg->type_data.request.version = GST_RTSP_VERSION_INVALID;
1051 gst_rtsp_message_init_request (msg, method, urlstr);
1052 msg->type_data.request.version = GST_RTSP_VERSION_INVALID;
1053 res = GST_RTSP_EPARSE;
1059 /* parsing lines means reading a Key: Value pair */
1060 static GstRTSPResult
1061 parse_line (guint8 * buffer, GstRTSPMessage * msg)
1065 GstRTSPHeaderField field;
1067 bptr = (gchar *) buffer;
1070 parse_key (key, sizeof (key), &bptr);
1076 field = gst_rtsp_find_header_field (key);
1077 if (field != GST_RTSP_HDR_INVALID) {
1078 while (g_ascii_isspace (*bptr))
1080 gst_rtsp_message_add_header (msg, field, bptr);
1087 return GST_RTSP_EPARSE;
1092 * GST_RTSP_OK when a complete message was read.
1093 * GST_RTSP_EEOF: when the socket is closed
1094 * GST_RTSP_EINTR: when more data is needed.
1095 * GST_RTSP_..: some other error occured.
1097 static GstRTSPResult
1098 build_next (GstRTSPBuilder * builder, GstRTSPMessage * message,
1099 GstRTSPConnection * conn)
1104 switch (builder->state) {
1106 builder->offset = 0;
1108 read_bytes (conn->readfd->fd, (guint8 *) builder->buffer,
1109 &builder->offset, 1);
1110 if (res != GST_RTSP_OK)
1113 /* we have 1 bytes now and we can see if this is a data message or
1115 if (builder->buffer[0] == '$') {
1116 /* data message, prepare for the header */
1117 builder->state = STATE_DATA_HEADER;
1120 builder->state = STATE_READ_LINES;
1123 case STATE_DATA_HEADER:
1126 read_bytes (conn->readfd->fd, (guint8 *) builder->buffer,
1127 &builder->offset, 4);
1128 if (res != GST_RTSP_OK)
1131 gst_rtsp_message_init_data (message, builder->buffer[1]);
1133 builder->body_len = (builder->buffer[2] << 8) | builder->buffer[3];
1134 builder->body_data = g_malloc (builder->body_len + 1);
1135 builder->body_data[builder->body_len] = '\0';
1136 builder->offset = 0;
1137 builder->state = STATE_DATA_BODY;
1140 case STATE_DATA_BODY:
1143 read_bytes (conn->readfd->fd, builder->body_data, &builder->offset,
1145 if (res != GST_RTSP_OK)
1148 /* we have the complete body now, store in the message adjusting the
1149 * length to include the traling '\0' */
1150 gst_rtsp_message_take_body (message,
1151 (guint8 *) builder->body_data, builder->body_len + 1);
1152 builder->body_data = NULL;
1153 builder->body_len = 0;
1155 builder->state = STATE_END;
1158 case STATE_READ_LINES:
1160 res = read_line (conn->readfd->fd, builder->buffer, &builder->offset,
1161 sizeof (builder->buffer));
1162 if (res != GST_RTSP_OK)
1165 /* we have a regular response */
1166 if (builder->buffer[0] == '\r') {
1167 builder->buffer[0] = '\0';
1170 if (builder->buffer[0] == '\0') {
1173 /* empty line, end of message header */
1174 /* see if there is a Content-Length header */
1175 if (gst_rtsp_message_get_header (message,
1176 GST_RTSP_HDR_CONTENT_LENGTH, &hdrval, 0) == GST_RTSP_OK) {
1177 /* there is, prepare to read the body */
1178 builder->body_len = atol (hdrval);
1179 builder->body_data = g_malloc (builder->body_len + 1);
1180 builder->body_data[builder->body_len] = '\0';
1181 builder->offset = 0;
1182 builder->state = STATE_DATA_BODY;
1184 builder->state = STATE_END;
1189 /* we have a line */
1190 if (builder->line == 0) {
1191 /* first line, check for response status */
1192 if (memcmp (builder->buffer, "RTSP", 4) == 0) {
1193 res = parse_response_status (builder->buffer, message);
1195 res = parse_request_line (builder->buffer, message);
1198 /* else just parse the line */
1199 parse_line (builder->buffer, message);
1202 builder->offset = 0;
1209 if (message->type == GST_RTSP_MESSAGE_DATA) {
1210 /* data messages don't have headers */
1215 /* save session id in the connection for further use */
1216 if (gst_rtsp_message_get_header (message, GST_RTSP_HDR_SESSION,
1217 &session_id, 0) == GST_RTSP_OK) {
1220 maxlen = sizeof (conn->session_id) - 1;
1221 /* the sessionid can have attributes marked with ;
1222 * Make sure we strip them */
1223 for (i = 0; session_id[i] != '\0'; i++) {
1224 if (session_id[i] == ';') {
1229 } while (g_ascii_isspace (session_id[i]));
1230 if (g_str_has_prefix (&session_id[i], "timeout=")) {
1233 /* if we parsed something valid, configure */
1234 if ((to = atoi (&session_id[i + 9])) > 0)
1241 /* make sure to not overflow */
1242 strncpy (conn->session_id, session_id, maxlen);
1243 conn->session_id[maxlen] = '\0';
1249 res = GST_RTSP_ERROR;
1258 * gst_rtsp_connection_read:
1259 * @conn: a #GstRTSPConnection
1260 * @data: the data to read
1261 * @size: the size of @data
1262 * @timeout: a timeout value or #NULL
1264 * Attempt to read @size bytes into @data from the connected @conn, blocking up to
1265 * the specified @timeout. @timeout can be #NULL, in which case this function
1266 * might block forever.
1268 * This function can be cancelled with gst_rtsp_connection_flush().
1270 * Returns: #GST_RTSP_OK on success.
1273 gst_rtsp_connection_read (GstRTSPConnection * conn, guint8 * data, guint size,
1281 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1282 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
1283 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1290 /* configure timeout if any */
1291 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1293 gst_poll_set_controllable (conn->fdset, TRUE);
1294 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
1295 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
1298 res = read_bytes (conn->readfd->fd, data, &offset, size);
1299 if (res == GST_RTSP_EEOF)
1301 if (res == GST_RTSP_OK)
1303 if (res != GST_RTSP_EINTR)
1307 retval = gst_poll_wait (conn->fdset, to);
1308 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1310 /* check for timeout */
1312 goto select_timeout;
1320 gst_poll_set_controllable (conn->fdset, FALSE);
1327 return GST_RTSP_ESYS;
1331 return GST_RTSP_ETIMEOUT;
1335 return GST_RTSP_EINTR;
1339 return GST_RTSP_EEOF;
1349 * gst_rtsp_connection_receive:
1350 * @conn: a #GstRTSPConnection
1351 * @message: the message to read
1352 * @timeout: a timeout value or #NULL
1354 * Attempt to read into @message from the connected @conn, blocking up to
1355 * the specified @timeout. @timeout can be #NULL, in which case this function
1356 * might block forever.
1358 * This function can be cancelled with gst_rtsp_connection_flush().
1360 * Returns: #GST_RTSP_OK on success.
1363 gst_rtsp_connection_receive (GstRTSPConnection * conn, GstRTSPMessage * message,
1367 GstRTSPBuilder builder = { 0 };
1371 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1372 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1373 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1375 /* configure timeout if any */
1376 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1378 gst_poll_set_controllable (conn->fdset, TRUE);
1379 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
1380 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
1383 res = build_next (&builder, message, conn);
1384 if (res == GST_RTSP_EEOF)
1386 if (res == GST_RTSP_OK)
1388 if (res != GST_RTSP_EINTR)
1392 retval = gst_poll_wait (conn->fdset, to);
1393 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1395 /* check for timeout */
1397 goto select_timeout;
1405 gst_poll_set_controllable (conn->fdset, FALSE);
1408 /* we have a message here */
1409 build_reset (&builder);
1416 res = GST_RTSP_ESYS;
1421 res = GST_RTSP_ETIMEOUT;
1426 res = GST_RTSP_EINTR;
1431 res = GST_RTSP_EEOF;
1437 build_reset (&builder);
1438 gst_rtsp_message_unset (message);
1444 * gst_rtsp_connection_close:
1445 * @conn: a #GstRTSPConnection
1447 * Close the connected @conn.
1449 * Returns: #GST_RTSP_OK on success.
1452 gst_rtsp_connection_close (GstRTSPConnection * conn)
1454 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1459 REMOVE_POLLFD (conn->fdset, conn->fd0);
1460 REMOVE_POLLFD (conn->fdset, conn->fd1);
1461 conn->writefd = NULL;
1462 conn->readfd = NULL;
1468 * gst_rtsp_connection_free:
1469 * @conn: a #GstRTSPConnection
1471 * Close and free @conn.
1473 * Returns: #GST_RTSP_OK on success.
1476 gst_rtsp_connection_free (GstRTSPConnection * conn)
1480 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1482 res = gst_rtsp_connection_close (conn);
1483 gst_poll_free (conn->fdset);
1484 g_timer_destroy (conn->timer);
1485 g_free (conn->username);
1486 g_free (conn->passwd);
1487 gst_rtsp_connection_clear_auth_params (conn);
1497 * gst_rtsp_connection_poll:
1498 * @conn: a #GstRTSPConnection
1499 * @events: a bitmask of #GstRTSPEvent flags to check
1500 * @revents: location for result flags
1501 * @timeout: a timeout
1503 * Wait up to the specified @timeout for the connection to become available for
1504 * at least one of the operations specified in @events. When the function returns
1505 * with #GST_RTSP_OK, @revents will contain a bitmask of available operations on
1508 * @timeout can be #NULL, in which case this function might block forever.
1510 * This function can be cancelled with gst_rtsp_connection_flush().
1512 * Returns: #GST_RTSP_OK on success.
1517 gst_rtsp_connection_poll (GstRTSPConnection * conn, GstRTSPEvent events,
1518 GstRTSPEvent * revents, GTimeVal * timeout)
1523 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1524 g_return_val_if_fail (events != 0, GST_RTSP_EINVAL);
1525 g_return_val_if_fail (revents != NULL, GST_RTSP_EINVAL);
1526 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1527 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
1529 gst_poll_set_controllable (conn->fdset, TRUE);
1531 /* add fd to writer set when asked to */
1532 gst_poll_fd_ctl_write (conn->fdset, conn->writefd,
1533 events & GST_RTSP_EV_WRITE);
1535 /* add fd to reader set when asked to */
1536 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, events & GST_RTSP_EV_READ);
1538 /* configure timeout if any */
1539 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1542 retval = gst_poll_wait (conn->fdset, to);
1543 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1546 goto select_timeout;
1556 if (events & GST_RTSP_EV_READ) {
1557 if (gst_poll_fd_can_read (conn->fdset, conn->readfd))
1558 *revents |= GST_RTSP_EV_READ;
1560 if (events & GST_RTSP_EV_WRITE) {
1561 if (gst_poll_fd_can_write (conn->fdset, conn->writefd))
1562 *revents |= GST_RTSP_EV_WRITE;
1569 return GST_RTSP_ETIMEOUT;
1573 return GST_RTSP_ESYS;
1577 return GST_RTSP_EINTR;
1582 * gst_rtsp_connection_next_timeout:
1583 * @conn: a #GstRTSPConnection
1584 * @timeout: a timeout
1586 * Calculate the next timeout for @conn, storing the result in @timeout.
1588 * Returns: #GST_RTSP_OK.
1591 gst_rtsp_connection_next_timeout (GstRTSPConnection * conn, GTimeVal * timeout)
1597 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1598 g_return_val_if_fail (timeout != NULL, GST_RTSP_EINVAL);
1600 elapsed = g_timer_elapsed (conn->timer, &usec);
1601 if (elapsed >= conn->timeout) {
1605 sec = conn->timeout - elapsed;
1608 timeout->tv_sec = sec;
1609 timeout->tv_usec = usec;
1615 * gst_rtsp_connection_reset_timeout:
1616 * @conn: a #GstRTSPConnection
1618 * Reset the timeout of @conn.
1620 * Returns: #GST_RTSP_OK.
1623 gst_rtsp_connection_reset_timeout (GstRTSPConnection * conn)
1625 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1627 g_timer_start (conn->timer);
1633 * gst_rtsp_connection_flush:
1634 * @conn: a #GstRTSPConnection
1635 * @flush: start or stop the flush
1637 * Start or stop the flushing action on @conn. When flushing, all current
1638 * and future actions on @conn will return #GST_RTSP_EINTR until the connection
1639 * is set to non-flushing mode again.
1641 * Returns: #GST_RTSP_OK.
1644 gst_rtsp_connection_flush (GstRTSPConnection * conn, gboolean flush)
1646 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1648 gst_poll_set_flushing (conn->fdset, flush);
1654 * gst_rtsp_connection_set_auth:
1655 * @conn: a #GstRTSPConnection
1656 * @method: authentication method
1658 * @pass: the password
1660 * Configure @conn for authentication mode @method with @user and @pass as the
1661 * user and password respectively.
1663 * Returns: #GST_RTSP_OK.
1666 gst_rtsp_connection_set_auth (GstRTSPConnection * conn,
1667 GstRTSPAuthMethod method, const gchar * user, const gchar * pass)
1669 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1671 if (method == GST_RTSP_AUTH_DIGEST && ((user == NULL || pass == NULL)
1672 || g_strrstr (user, ":") != NULL))
1673 return GST_RTSP_EINVAL;
1675 /* Make sure the username and passwd are being set for authentication */
1676 if (method == GST_RTSP_AUTH_NONE && (user == NULL || pass == NULL))
1677 return GST_RTSP_EINVAL;
1679 /* ":" chars are not allowed in usernames for basic auth */
1680 if (method == GST_RTSP_AUTH_BASIC && g_strrstr (user, ":") != NULL)
1681 return GST_RTSP_EINVAL;
1683 g_free (conn->username);
1684 g_free (conn->passwd);
1686 conn->auth_method = method;
1687 conn->username = g_strdup (user);
1688 conn->passwd = g_strdup (pass);
1695 * @key: ASCII string to hash
1697 * Hashes @key in a case-insensitive manner.
1699 * Returns: the hash code.
1702 str_case_hash (gconstpointer key)
1704 const char *p = key;
1705 guint h = g_ascii_toupper (*p);
1708 for (p += 1; *p != '\0'; p++)
1709 h = (h << 5) - h + g_ascii_toupper (*p);
1716 * @v1: an ASCII string
1717 * @v2: another ASCII string
1719 * Compares @v1 and @v2 in a case-insensitive manner
1721 * Returns: %TRUE if they are equal (modulo case)
1724 str_case_equal (gconstpointer v1, gconstpointer v2)
1726 const char *string1 = v1;
1727 const char *string2 = v2;
1729 return g_ascii_strcasecmp (string1, string2) == 0;
1733 * gst_rtsp_connection_set_auth_param:
1734 * @conn: a #GstRTSPConnection
1735 * @param: authentication directive
1738 * Setup @conn with authentication directives. This is not necesary for
1739 * methods #GST_RTSP_AUTH_NONE and #GST_RTSP_AUTH_BASIC. For
1740 * #GST_RTSP_AUTH_DIGEST, directives should be taken from the digest challenge
1741 * in the WWW-Authenticate response header and can include realm, domain,
1742 * nonce, opaque, stale, algorithm, qop as per RFC2617.
1747 gst_rtsp_connection_set_auth_param (GstRTSPConnection * conn,
1748 const gchar * param, const gchar * value)
1750 g_return_if_fail (conn != NULL);
1751 g_return_if_fail (param != NULL);
1753 if (conn->auth_params == NULL) {
1755 g_hash_table_new_full (str_case_hash, str_case_equal, g_free, g_free);
1757 g_hash_table_insert (conn->auth_params, g_strdup (param), g_strdup (value));
1761 * gst_rtsp_connection_clear_auth_params:
1762 * @conn: a #GstRTSPConnection
1764 * Clear the list of authentication directives stored in @conn.
1769 gst_rtsp_connection_clear_auth_params (GstRTSPConnection * conn)
1771 g_return_if_fail (conn != NULL);
1773 if (conn->auth_params != NULL) {
1774 g_hash_table_destroy (conn->auth_params);
1775 conn->auth_params = NULL;
1779 static GstRTSPResult
1780 set_qos_dscp (gint fd, guint qos_dscp)
1785 struct sockaddr_in6 sa_in6;
1786 struct sockaddr_storage sa_stor;
1788 socklen_t slen = sizeof (sa);
1795 if (getsockname (fd, &sa.sa, &slen) < 0)
1796 goto no_getsockname;
1798 af = sa.sa.sa_family;
1800 /* if this is an IPv4-mapped address then do IPv4 QoS */
1801 if (af == AF_INET6) {
1802 if (IN6_IS_ADDR_V4MAPPED (&sa.sa_in6.sin6_addr))
1806 /* extract and shift 6 bits of the DSCP */
1807 tos = (qos_dscp & 0x3f) << 2;
1811 if (SETSOCKOPT (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0)
1816 if (SETSOCKOPT (fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof (tos)) < 0)
1830 return GST_RTSP_ESYS;
1835 return GST_RTSP_ERROR;
1840 * gst_rtsp_connection_set_qos_dscp:
1841 * @conn: a #GstRTSPConnection
1842 * @qos_dscp: DSCP value
1844 * Configure @conn to use the specified DSCP value.
1846 * Returns: #GST_RTSP_OK on success.
1851 gst_rtsp_connection_set_qos_dscp (GstRTSPConnection * conn, guint qos_dscp)
1855 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1856 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1857 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
1859 res = set_qos_dscp (conn->fd0.fd, qos_dscp);
1860 if (res == GST_RTSP_OK)
1861 res = set_qos_dscp (conn->fd1.fd, qos_dscp);
1868 * gst_rtsp_connection_get_url:
1869 * @conn: a #GstRTSPConnection
1871 * Retrieve the URL of the other end of @conn.
1873 * Returns: The URL. This value remains valid until the
1874 * connection is closed.
1879 gst_rtsp_connection_get_url (const GstRTSPConnection * conn)
1881 g_return_val_if_fail (conn != NULL, NULL);
1887 * gst_rtsp_connection_get_ip:
1888 * @conn: a #GstRTSPConnection
1890 * Retrieve the IP address of the other end of @conn.
1892 * Returns: The IP address as a string. this value remains valid until the
1893 * connection is closed.
1898 gst_rtsp_connection_get_ip (const GstRTSPConnection * conn)
1900 g_return_val_if_fail (conn != NULL, NULL);
1905 #define READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
1906 #define WRITE_COND (G_IO_OUT | G_IO_ERR)
1914 /* async functions */
1915 struct _GstRTSPWatch
1919 GstRTSPConnection *conn;
1921 GstRTSPBuilder builder;
1922 GstRTSPMessage message;
1926 gboolean write_added;
1928 /* queued message for transmission */
1935 GstRTSPWatchFuncs funcs;
1938 GDestroyNotify notify;
1942 gst_rtsp_source_prepare (GSource * source, gint * timeout)
1944 GstRTSPWatch *watch = (GstRTSPWatch *) source;
1946 *timeout = (watch->conn->timeout * 1000);
1952 gst_rtsp_source_check (GSource * source)
1954 GstRTSPWatch *watch = (GstRTSPWatch *) source;
1956 if (watch->readfd.revents & READ_COND)
1959 if (watch->writefd.revents & WRITE_COND)
1966 gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback,
1969 GstRTSPWatch *watch = (GstRTSPWatch *) source;
1972 /* first read as much as we can */
1973 if (watch->readfd.revents & READ_COND) {
1975 res = build_next (&watch->builder, &watch->message, watch->conn);
1976 if (res == GST_RTSP_EINTR)
1978 if (res == GST_RTSP_EEOF)
1980 if (res != GST_RTSP_OK)
1983 if (watch->funcs.message_received)
1984 watch->funcs.message_received (watch, &watch->message,
1987 gst_rtsp_message_unset (&watch->message);
1988 build_reset (&watch->builder);
1992 if (watch->writefd.revents & WRITE_COND) {
1994 if (watch->write_data == NULL) {
1997 if (!watch->messages)
2000 /* no data, get a new message from the queue */
2001 data = watch->messages->data;
2002 watch->messages = g_list_delete_link (watch->messages, watch->messages);
2004 watch->write_off = 0;
2005 watch->write_len = data->str->len;
2006 watch->write_data = (guint8 *) g_string_free (data->str, FALSE);
2007 watch->write_cseq = data->cseq;
2009 g_slice_free (GstRTSPRec, data);
2012 res = write_bytes (watch->writefd.fd, watch->write_data,
2013 &watch->write_off, watch->write_len);
2014 if (res == GST_RTSP_EINTR)
2016 if (res != GST_RTSP_OK)
2019 if (watch->funcs.message_sent)
2020 watch->funcs.message_sent (watch, watch->write_cseq, watch->user_data);
2023 if (watch->messages == NULL && watch->write_added) {
2024 g_source_remove_poll ((GSource *) watch, &watch->writefd);
2025 watch->write_added = FALSE;
2026 watch->writefd.revents = 0;
2028 g_free (watch->write_data);
2029 watch->write_data = NULL;
2038 if (watch->funcs.closed)
2039 watch->funcs.closed (watch, watch->user_data);
2044 if (watch->funcs.error)
2045 watch->funcs.error (watch, res, watch->user_data);
2051 gst_rtsp_source_finalize (GSource * source)
2053 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2056 build_reset (&watch->builder);
2058 for (walk = watch->messages; walk; walk = g_list_next (walk)) {
2059 GstRTSPRec *data = walk->data;
2061 g_string_free (data->str, TRUE);
2062 g_slice_free (GstRTSPRec, data);
2064 g_list_free (watch->messages);
2065 g_free (watch->write_data);
2068 watch->notify (watch->user_data);
2071 static GSourceFuncs gst_rtsp_source_funcs = {
2072 gst_rtsp_source_prepare,
2073 gst_rtsp_source_check,
2074 gst_rtsp_source_dispatch,
2075 gst_rtsp_source_finalize
2079 * gst_rtsp_watch_new:
2080 * @conn: a #GstRTSPConnection
2081 * @funcs: watch functions
2082 * @user_data: user data to pass to @funcs
2084 * Create a watch object for @conn. The functions provided in @funcs will be
2085 * called with @user_data when activity happened on the watch.
2087 * The new watch is usually created so that it can be attached to a
2088 * maincontext with gst_rtsp_watch_attach().
2090 * @conn must exist for the entire lifetime of the watch.
2092 * Returns: a #GstRTSPWatch that can be used for asynchronous RTSP
2093 * communication. Free with gst_rtsp_watch_unref () after usage.
2098 gst_rtsp_watch_new (GstRTSPConnection * conn,
2099 GstRTSPWatchFuncs * funcs, gpointer user_data, GDestroyNotify notify)
2101 GstRTSPWatch *result;
2103 g_return_val_if_fail (conn != NULL, NULL);
2104 g_return_val_if_fail (funcs != NULL, NULL);
2105 g_return_val_if_fail (conn->readfd != NULL, NULL);
2106 g_return_val_if_fail (conn->writefd != NULL, NULL);
2108 result = (GstRTSPWatch *) g_source_new (&gst_rtsp_source_funcs,
2109 sizeof (GstRTSPWatch));
2111 result->conn = conn;
2112 result->builder.state = STATE_START;
2114 result->readfd.fd = conn->readfd->fd;
2115 result->readfd.events = READ_COND;
2116 result->readfd.revents = 0;
2118 result->writefd.fd = conn->writefd->fd;
2119 result->writefd.events = WRITE_COND;
2120 result->writefd.revents = 0;
2121 result->write_added = FALSE;
2123 result->funcs = *funcs;
2124 result->user_data = user_data;
2125 result->notify = notify;
2127 /* only add the read fd, the write fd is only added when we have data
2129 g_source_add_poll ((GSource *) result, &result->readfd);
2135 * gst_rtsp_watch_attach:
2136 * @watch: a #GstRTSPWatch
2137 * @context: a GMainContext (if NULL, the default context will be used)
2139 * Adds a #GstRTSPWatch to a context so that it will be executed within that context.
2141 * Returns: the ID (greater than 0) for the watch within the GMainContext.
2146 gst_rtsp_watch_attach (GstRTSPWatch * watch, GMainContext * context)
2148 g_return_val_if_fail (watch != NULL, 0);
2150 return g_source_attach ((GSource *) watch, context);
2154 * gst_rtsp_watch_free:
2155 * @watch: a #GstRTSPWatch
2157 * Decreases the reference count of @watch by one. If the resulting reference
2158 * count is zero the watch and associated memory will be destroyed.
2163 gst_rtsp_watch_unref (GstRTSPWatch * watch)
2165 g_return_if_fail (watch != NULL);
2167 g_source_unref ((GSource *) watch);
2171 * gst_rtsp_watch_queue_message:
2172 * @watch: a #GstRTSPWatch
2173 * @message: a #GstRTSPMessage
2175 * Queue a @message for transmission in @watch. The contents of this
2176 * message will be serialized and transmitted when the connection of the
2177 * watch becomes writable.
2179 * The return value of this function will be returned as the cseq argument in
2180 * the message_sent callback.
2182 * Returns: the sequence number of the message or -1 if the cseq could not be
2188 gst_rtsp_watch_queue_message (GstRTSPWatch * watch, GstRTSPMessage * message)
2194 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
2195 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2197 /* get the cseq from the message, when we finish writing this message on the
2198 * socket we will have to pass the cseq to the callback. */
2199 if (gst_rtsp_message_get_header (message, GST_RTSP_HDR_CSEQ, &header,
2200 0) == GST_RTSP_OK) {
2201 cseq = atoi (header);
2206 /* make a record with the message as a string ans cseq */
2207 data = g_slice_new (GstRTSPRec);
2208 data->str = message_to_string (watch->conn, message);
2211 /* add the record to a queue */
2212 watch->messages = g_list_append (watch->messages, data);
2214 /* make sure the main context will now also check for writability on the
2216 if (!watch->write_added) {
2217 g_source_add_poll ((GSource *) watch, &watch->writefd);
2218 watch->write_added = TRUE;