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 struct _GstRTSPConnection
121 /* URL for the connection */
124 /* connection state */
130 gint cseq; /* sequence number */
131 gchar session_id[512]; /* session id */
132 gint timeout; /* session timeout in seconds */
133 GTimer *timer; /* timeout timer */
136 GstRTSPAuthMethod auth_method;
139 GHashTable *auth_params;
144 inet_aton (const char *c, struct in_addr *paddr)
146 /* note that inet_addr is deprecated on unix because
147 * inet_addr returns -1 (INADDR_NONE) for the valid 255.255.255.255
149 paddr->s_addr = inet_addr (c);
151 if (paddr->s_addr == INADDR_NONE)
168 /* a structure for constructing RTSPMessages */
181 build_reset (GstRTSPBuilder * builder)
183 g_free (builder->body_data);
184 memset (builder, 0, sizeof (builder));
188 * gst_rtsp_connection_create:
189 * @url: a #GstRTSPUrl
190 * @conn: storage for a #GstRTSPConnection
192 * Create a newly allocated #GstRTSPConnection from @url and store it in @conn.
193 * The connection will not yet attempt to connect to @url, use
194 * gst_rtsp_connection_connect().
196 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
199 gst_rtsp_connection_create (GstRTSPUrl * url, GstRTSPConnection ** conn)
201 GstRTSPConnection *newconn;
207 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
210 error = WSAStartup (0x0202, &w);
215 if (w.wVersion != 0x0202)
219 newconn = g_new0 (GstRTSPConnection, 1);
221 if ((newconn->fdset = gst_poll_new (TRUE)) == NULL)
226 newconn->timer = g_timer_new ();
227 newconn->timeout = 60;
229 newconn->auth_method = GST_RTSP_AUTH_NONE;
230 newconn->username = NULL;
231 newconn->passwd = NULL;
232 newconn->auth_params = NULL;
242 g_warning ("Error %d on WSAStartup", error);
243 return GST_RTSP_EWSASTART;
247 g_warning ("Windows sockets are not version 0x202 (current 0x%x)",
250 return GST_RTSP_EWSAVERSION;
259 return GST_RTSP_ESYS;
264 * gst_rtsp_connection_accept:
266 * @conn: storage for a #GstRTSPConnection
268 * Accept a new connection on @sock and create a new #GstRTSPConnection for
269 * handling communication on new socket.
271 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
276 gst_rtsp_connection_accept (gint sock, GstRTSPConnection ** conn)
279 unsigned int address_len;
280 GstRTSPConnection *newconn;
281 struct sockaddr_in address;
284 address_len = sizeof (address);
285 memset (&address, 0, address_len);
287 fd = accept (sock, (struct sockaddr *) &address, &address_len);
291 /* set to non-blocking mode so that we can cancel the communication */
293 fcntl (fd, F_SETFL, O_NONBLOCK);
295 ioctlsocket (fd, FIONBIO, &flags);
296 #endif /* G_OS_WIN32 */
298 /* create a url for the client address */
299 url = g_new0 (GstRTSPUrl, 1);
300 url->host = g_strdup_printf ("%s", inet_ntoa (address.sin_addr));
301 url->port = address.sin_port;
303 /* now create the connection object */
304 gst_rtsp_connection_create (url, &newconn);
306 gst_poll_add_fd (newconn->fdset, &newconn->fd);
315 return GST_RTSP_ESYS;
320 * gst_rtsp_connection_connect:
321 * @conn: a #GstRTSPConnection
322 * @timeout: a #GTimeVal timeout
324 * Attempt to connect to the url of @conn made with
325 * gst_rtsp_connection_create(). If @timeout is #NULL this function can block
326 * forever. If @timeout contains a valid timeout, this function will return
327 * #GST_RTSP_ETIMEOUT after the timeout expired.
329 * This function can be cancelled with gst_rtsp_connection_flush().
331 * Returns: #GST_RTSP_OK when a connection could be made.
334 gst_rtsp_connection_connect (GstRTSPConnection * conn, GTimeVal * timeout)
337 struct sockaddr_in sa_in;
338 struct hostent *hostinfo;
348 unsigned long flags = 1;
349 struct in_addr *addrp;
352 gchar ipbuf[INET_ADDRSTRLEN];
353 #endif /* G_OS_WIN32 */
355 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
356 g_return_val_if_fail (conn->url != NULL, GST_RTSP_EINVAL);
357 g_return_val_if_fail (conn->fd.fd < 0, GST_RTSP_EINVAL);
361 /* first check if it already is an IP address */
362 if (inet_aton (url->host, &addr)) {
365 hostinfo = gethostbyname (url->host);
367 goto not_resolved; /* h_errno set */
369 if (hostinfo->h_addrtype != AF_INET)
370 goto not_ip; /* host not an IP host */
372 addrp = (struct in_addr *) hostinfo->h_addr_list[0];
373 /* this is not threadsafe */
374 ip = inet_ntoa (*addrp);
376 addrs = hostinfo->h_addr_list;
377 ip = inet_ntop (AF_INET, (struct in_addr *) addrs[0], ipbuf,
379 #endif /* G_OS_WIN32 */
382 /* get the port from the url */
383 gst_rtsp_url_get_port (url, &port);
385 memset (&sa_in, 0, sizeof (sa_in));
386 sa_in.sin_family = AF_INET; /* network socket */
387 sa_in.sin_port = htons (port); /* on port */
388 sa_in.sin_addr.s_addr = inet_addr (ip); /* on host ip */
390 fd = socket (AF_INET, SOCK_STREAM, 0);
394 /* set to non-blocking mode so that we can cancel the connect */
396 fcntl (fd, F_SETFL, O_NONBLOCK);
398 ioctlsocket (fd, FIONBIO, &flags);
399 #endif /* G_OS_WIN32 */
401 /* add the socket to our fdset */
403 gst_poll_add_fd (conn->fdset, &conn->fd);
405 /* we are going to connect ASYNC now */
406 ret = connect (fd, (struct sockaddr *) &sa_in, sizeof (sa_in));
409 if (!ERRNO_IS_EINPROGRESS)
412 /* wait for connect to complete up to the specified timeout or until we got
414 gst_poll_fd_ctl_write (conn->fdset, &conn->fd, TRUE);
416 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
419 retval = gst_poll_wait (conn->fdset, to);
420 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
424 else if (retval == -1)
427 /* we can still have an error connecting on windows */
428 if (gst_poll_fd_has_error (conn->fdset, &conn->fd)) {
429 socklen_t len = sizeof (errno);
430 getsockopt (conn->fd.fd, SOL_SOCKET, SO_ERROR, &errno, &len);
434 gst_poll_fd_ignored (conn->fdset, &conn->fd);
437 conn->ip = g_strdup (ip);
443 GST_ERROR ("system error %d (%s)", errno, g_strerror (errno));
444 if (conn->fd.fd >= 0) {
445 GST_DEBUG ("remove fd %d", conn->fd.fd);
446 gst_poll_remove_fd (conn->fdset, &conn->fd);
451 return GST_RTSP_ESYS;
455 GST_ERROR ("could not resolve %s", url->host);
456 return GST_RTSP_ENET;
460 GST_ERROR ("not an IP address");
461 return GST_RTSP_ENOTIP;
465 GST_ERROR ("timeout");
466 if (conn->fd.fd >= 0) {
467 GST_DEBUG ("remove fd %d", conn->fd.fd);
468 gst_poll_remove_fd (conn->fdset, &conn->fd);
473 return GST_RTSP_ETIMEOUT;
478 md5_digest_to_hex_string (unsigned char digest[16], char string[33])
480 static const char hexdigits[] = "0123456789abcdef";
483 for (i = 0; i < 16; i++) {
484 string[i * 2] = hexdigits[(digest[i] >> 4) & 0x0f];
485 string[i * 2 + 1] = hexdigits[digest[i] & 0x0f];
491 auth_digest_compute_hex_urp (const gchar * username,
492 const gchar * realm, const gchar * password, gchar hex_urp[33])
494 struct MD5Context md5_context;
495 unsigned char digest[16];
497 MD5Init (&md5_context);
498 MD5Update (&md5_context, username, strlen (username));
499 MD5Update (&md5_context, ":", 1);
500 MD5Update (&md5_context, realm, strlen (realm));
501 MD5Update (&md5_context, ":", 1);
502 MD5Update (&md5_context, password, strlen (password));
503 MD5Final (digest, &md5_context);
504 md5_digest_to_hex_string (digest, hex_urp);
508 auth_digest_compute_response (const gchar * method,
509 const gchar * uri, const gchar * hex_a1, const gchar * nonce,
513 struct MD5Context md5_context;
514 unsigned char digest[16];
517 MD5Init (&md5_context);
518 MD5Update (&md5_context, method, strlen (method));
519 MD5Update (&md5_context, ":", 1);
520 MD5Update (&md5_context, uri, strlen (uri));
521 MD5Final (digest, &md5_context);
522 md5_digest_to_hex_string (digest, hex_a2);
525 MD5Init (&md5_context);
526 MD5Update (&md5_context, hex_a1, strlen (hex_a1));
527 MD5Update (&md5_context, ":", 1);
528 MD5Update (&md5_context, nonce, strlen (nonce));
529 MD5Update (&md5_context, ":", 1);
531 MD5Update (&md5_context, hex_a2, 32);
532 MD5Final (digest, &md5_context);
533 md5_digest_to_hex_string (digest, response);
537 add_auth_header (GstRTSPConnection * conn, GstRTSPMessage * message)
539 switch (conn->auth_method) {
540 case GST_RTSP_AUTH_BASIC:{
542 g_strdup_printf ("%s:%s", conn->username, conn->passwd);
544 gst_rtsp_base64_encode (user_pass, strlen (user_pass));
545 gchar *auth_string = g_strdup_printf ("Basic %s", user_pass64);
547 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
551 g_free (user_pass64);
554 case GST_RTSP_AUTH_DIGEST:{
555 gchar response[33], hex_urp[33];
556 gchar *auth_string, *auth_string2;
563 /* we need to have some params set */
564 if (conn->auth_params == NULL)
567 /* we need the realm and nonce */
568 realm = (gchar *) g_hash_table_lookup (conn->auth_params, "realm");
569 nonce = (gchar *) g_hash_table_lookup (conn->auth_params, "nonce");
570 if (realm == NULL || nonce == NULL)
573 auth_digest_compute_hex_urp (conn->username, realm, conn->passwd,
576 method = gst_rtsp_method_as_text (message->type_data.request.method);
577 uri = message->type_data.request.uri;
579 /* Assume no qop, algorithm=md5, stale=false */
580 /* For algorithm MD5, a1 = urp. */
581 auth_digest_compute_response (method, uri, hex_urp, nonce, response);
582 auth_string = g_strdup_printf ("Digest username=\"%s\", "
583 "realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"",
584 conn->username, realm, nonce, uri, response);
586 opaque = (gchar *) g_hash_table_lookup (conn->auth_params, "opaque");
588 auth_string2 = g_strdup_printf ("%s, opaque=\"%s\"", auth_string,
590 g_free (auth_string);
591 auth_string = auth_string2;
593 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
604 add_date_header (GstRTSPMessage * message)
607 gchar date_string[100];
614 g_get_current_time (&tv);
615 t = (time_t) tv.tv_sec;
618 strftime (date_string, sizeof (date_string), "%a, %d %b %Y %H:%M:%S GMT",
619 gmtime_r (&t, &tm_));
621 strftime (date_string, sizeof (date_string), "%a, %d %b %Y %H:%M:%S GMT",
625 gst_rtsp_message_add_header (message, GST_RTSP_HDR_DATE, date_string);
629 write_bytes (gint fd, const guint8 * buffer, guint * idx, guint size)
634 return GST_RTSP_ERROR;
641 r = WRITE_SOCKET (fd, &buffer[*idx], left);
643 return GST_RTSP_EINTR;
646 return GST_RTSP_EINTR;
648 return GST_RTSP_ESYS;
658 read_bytes (gint fd, guint8 * buffer, guint * idx, guint size)
663 return GST_RTSP_ERROR;
670 r = READ_SOCKET (fd, &buffer[*idx], left);
672 return GST_RTSP_EEOF;
675 return GST_RTSP_EINTR;
677 return GST_RTSP_ESYS;
687 read_line (gint fd, guint8 * buffer, guint * idx, guint size)
693 r = READ_SOCKET (fd, &c, 1);
695 return GST_RTSP_EEOF;
698 return GST_RTSP_EINTR;
700 return GST_RTSP_ESYS;
702 if (c == '\n') /* end on \n */
704 if (c == '\r') /* ignore \r */
708 buffer[(*idx)++] = c;
717 * gst_rtsp_connection_write:
718 * @conn: a #GstRTSPConnection
719 * @data: the data to write
720 * @size: the size of @data
721 * @timeout: a timeout value or #NULL
723 * Attempt to write @size bytes of @data to the connected @conn, blocking up to
724 * the specified @timeout. @timeout can be #NULL, in which case this function
725 * might block forever.
727 * This function can be cancelled with gst_rtsp_connection_flush().
729 * Returns: #GST_RTSP_OK on success.
732 gst_rtsp_connection_write (GstRTSPConnection * conn, const guint8 * data,
733 guint size, GTimeVal * timeout)
740 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
741 g_return_val_if_fail (data != NULL || size == 0, GST_RTSP_EINVAL);
742 g_return_val_if_fail (conn->fd.fd >= 0, GST_RTSP_EINVAL);
744 gst_poll_set_controllable (conn->fdset, TRUE);
745 gst_poll_fd_ctl_write (conn->fdset, &conn->fd, TRUE);
746 gst_poll_fd_ctl_read (conn->fdset, &conn->fd, FALSE);
747 /* clear all previous poll results */
748 gst_poll_fd_ignored (conn->fdset, &conn->fd);
750 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
756 res = write_bytes (conn->fd.fd, data, &offset, size);
757 if (res == GST_RTSP_OK)
759 if (res != GST_RTSP_EINTR)
762 /* not all is written, wait until we can write more */
764 retval = gst_poll_wait (conn->fdset, to);
765 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
782 return GST_RTSP_ETIMEOUT;
786 return GST_RTSP_ESYS;
790 return GST_RTSP_EINTR;
799 message_to_string (GstRTSPConnection * conn, GstRTSPMessage * message)
803 str = g_string_new ("");
805 switch (message->type) {
806 case GST_RTSP_MESSAGE_REQUEST:
807 /* create request string, add CSeq */
808 g_string_append_printf (str, "%s %s RTSP/1.0\r\n"
810 gst_rtsp_method_as_text (message->type_data.request.method),
811 message->type_data.request.uri, conn->cseq++);
812 /* add session id if we have one */
813 if (conn->session_id[0] != '\0') {
814 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SESSION,
817 /* add any authentication headers */
818 add_auth_header (conn, message);
820 case GST_RTSP_MESSAGE_RESPONSE:
821 /* create response string */
822 g_string_append_printf (str, "RTSP/1.0 %d %s\r\n",
823 message->type_data.response.code, message->type_data.response.reason);
825 case GST_RTSP_MESSAGE_DATA:
827 guint8 data_header[4];
829 /* prepare data header */
830 data_header[0] = '$';
831 data_header[1] = message->type_data.data.channel;
832 data_header[2] = (message->body_size >> 8) & 0xff;
833 data_header[3] = message->body_size & 0xff;
835 /* create string with header and data */
836 str = g_string_append_len (str, (gchar *) data_header, 4);
838 g_string_append_len (str, (gchar *) message->body,
843 g_string_free (str, TRUE);
844 g_return_val_if_reached (NULL);
848 /* append headers and body */
849 if (message->type != GST_RTSP_MESSAGE_DATA) {
850 /* add date header */
851 add_date_header (message);
854 gst_rtsp_message_append_headers (message, str);
856 /* append Content-Length and body if needed */
857 if (message->body != NULL && message->body_size > 0) {
860 len = g_strdup_printf ("%d", message->body_size);
861 g_string_append_printf (str, "%s: %s\r\n",
862 gst_rtsp_header_as_text (GST_RTSP_HDR_CONTENT_LENGTH), len);
864 /* header ends here */
865 g_string_append (str, "\r\n");
867 g_string_append_len (str, (gchar *) message->body,
870 /* just end headers */
871 g_string_append (str, "\r\n");
879 * gst_rtsp_connection_send:
880 * @conn: a #GstRTSPConnection
881 * @message: the message to send
882 * @timeout: a timeout value or #NULL
884 * Attempt to send @message to the connected @conn, blocking up to
885 * the specified @timeout. @timeout can be #NULL, in which case this function
886 * might block forever.
888 * This function can be cancelled with gst_rtsp_connection_flush().
890 * Returns: #GST_RTSP_OK on success.
893 gst_rtsp_connection_send (GstRTSPConnection * conn, GstRTSPMessage * message,
899 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
900 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
902 if (!(str = message_to_string (conn, message)))
907 gst_rtsp_connection_write (conn, (guint8 *) str->str, str->len, timeout);
909 g_string_free (str, TRUE);
915 g_warning ("Wrong message");
916 return GST_RTSP_EINVAL;
921 parse_string (gchar * dest, gint size, gchar ** src)
927 while (g_ascii_isspace (**src))
930 while (!g_ascii_isspace (**src) && **src != '\0') {
940 parse_key (gchar * dest, gint size, gchar ** src)
945 while (**src != ':' && **src != '\0') {
955 parse_response_status (guint8 * buffer, GstRTSPMessage * msg)
958 gchar versionstr[20];
963 bptr = (gchar *) buffer;
965 parse_string (versionstr, sizeof (versionstr), &bptr);
966 parse_string (codestr, sizeof (codestr), &bptr);
967 code = atoi (codestr);
969 while (g_ascii_isspace (*bptr))
972 if (strcmp (versionstr, "RTSP/1.0") == 0)
973 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
975 else if (strncmp (versionstr, "RTSP/", 5) == 0) {
976 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
978 msg->type_data.response.version = GST_RTSP_VERSION_INVALID;
986 return GST_RTSP_EPARSE;
991 parse_request_line (guint8 * buffer, GstRTSPMessage * msg)
993 GstRTSPResult res = GST_RTSP_OK;
994 gchar versionstr[20];
998 GstRTSPMethod method;
1000 bptr = (gchar *) buffer;
1002 parse_string (methodstr, sizeof (methodstr), &bptr);
1003 method = gst_rtsp_find_method (methodstr);
1005 parse_string (urlstr, sizeof (urlstr), &bptr);
1006 if (*urlstr == '\0')
1007 res = GST_RTSP_EPARSE;
1009 parse_string (versionstr, sizeof (versionstr), &bptr);
1012 res = GST_RTSP_EPARSE;
1014 if (strcmp (versionstr, "RTSP/1.0") == 0) {
1015 if (gst_rtsp_message_init_request (msg, method, urlstr) != GST_RTSP_OK)
1016 res = GST_RTSP_EPARSE;
1017 } else if (strncmp (versionstr, "RTSP/", 5) == 0) {
1018 if (gst_rtsp_message_init_request (msg, method, urlstr) != GST_RTSP_OK)
1019 res = GST_RTSP_EPARSE;
1020 msg->type_data.request.version = GST_RTSP_VERSION_INVALID;
1022 gst_rtsp_message_init_request (msg, method, urlstr);
1023 msg->type_data.request.version = GST_RTSP_VERSION_INVALID;
1024 res = GST_RTSP_EPARSE;
1030 /* parsing lines means reading a Key: Value pair */
1031 static GstRTSPResult
1032 parse_line (guint8 * buffer, GstRTSPMessage * msg)
1036 GstRTSPHeaderField field;
1038 bptr = (gchar *) buffer;
1041 parse_key (key, sizeof (key), &bptr);
1047 field = gst_rtsp_find_header_field (key);
1048 if (field != GST_RTSP_HDR_INVALID) {
1049 while (g_ascii_isspace (*bptr))
1051 gst_rtsp_message_add_header (msg, field, bptr);
1058 return GST_RTSP_EPARSE;
1063 * GST_RTSP_OK when a complete message was read.
1064 * GST_RTSP_EEOF: when the socket is closed
1065 * GST_RTSP_EINTR: when more data is needed.
1066 * GST_RTSP_..: some other error occured.
1068 static GstRTSPResult
1069 build_next (GstRTSPBuilder * builder, GstRTSPMessage * message,
1070 GstRTSPConnection * conn)
1075 switch (builder->state) {
1077 builder->offset = 0;
1079 read_bytes (conn->fd.fd, (guint8 *) builder->buffer,
1080 &builder->offset, 1);
1081 if (res != GST_RTSP_OK)
1084 /* we have 1 bytes now and we can see if this is a data message or
1086 if (builder->buffer[0] == '$') {
1087 /* data message, prepare for the header */
1088 builder->state = STATE_DATA_HEADER;
1091 builder->state = STATE_READ_LINES;
1094 case STATE_DATA_HEADER:
1097 read_bytes (conn->fd.fd, (guint8 *) builder->buffer,
1098 &builder->offset, 4);
1099 if (res != GST_RTSP_OK)
1102 gst_rtsp_message_init_data (message, builder->buffer[1]);
1104 builder->body_len = (builder->buffer[2] << 8) | builder->buffer[3];
1105 builder->body_data = g_malloc (builder->body_len + 1);
1106 builder->body_data[builder->body_len] = '\0';
1107 builder->offset = 0;
1108 builder->state = STATE_DATA_BODY;
1111 case STATE_DATA_BODY:
1113 res = read_bytes (conn->fd.fd, builder->body_data, &builder->offset,
1115 if (res != GST_RTSP_OK)
1118 /* we have the complete body now, store in the message adjusting the
1119 * length to include the traling '\0' */
1120 gst_rtsp_message_take_body (message,
1121 (guint8 *) builder->body_data, builder->body_len + 1);
1122 builder->body_data = NULL;
1123 builder->body_len = 0;
1125 builder->state = STATE_END;
1128 case STATE_READ_LINES:
1130 res = read_line (conn->fd.fd, builder->buffer, &builder->offset,
1131 sizeof (builder->buffer));
1132 if (res != GST_RTSP_OK)
1135 /* we have a regular response */
1136 if (builder->buffer[0] == '\r') {
1137 builder->buffer[0] = '\0';
1140 if (builder->buffer[0] == '\0') {
1143 /* empty line, end of message header */
1144 /* see if there is a Content-Length header */
1145 if (gst_rtsp_message_get_header (message,
1146 GST_RTSP_HDR_CONTENT_LENGTH, &hdrval, 0) == GST_RTSP_OK) {
1147 /* there is, prepare to read the body */
1148 builder->body_len = atol (hdrval);
1149 builder->body_data = g_malloc (builder->body_len + 1);
1150 builder->body_data[builder->body_len] = '\0';
1151 builder->offset = 0;
1152 builder->state = STATE_DATA_BODY;
1154 builder->state = STATE_END;
1159 /* we have a line */
1160 if (builder->line == 0) {
1161 /* first line, check for response status */
1162 if (memcmp (builder->buffer, "RTSP", 4) == 0) {
1163 res = parse_response_status (builder->buffer, message);
1165 res = parse_request_line (builder->buffer, message);
1168 /* else just parse the line */
1169 parse_line (builder->buffer, message);
1172 builder->offset = 0;
1179 if (message->type == GST_RTSP_MESSAGE_DATA) {
1180 /* data messages don't have headers */
1185 /* save session id in the connection for further use */
1186 if (gst_rtsp_message_get_header (message, GST_RTSP_HDR_SESSION,
1187 &session_id, 0) == GST_RTSP_OK) {
1190 maxlen = sizeof (conn->session_id) - 1;
1191 /* the sessionid can have attributes marked with ;
1192 * Make sure we strip them */
1193 for (i = 0; session_id[i] != '\0'; i++) {
1194 if (session_id[i] == ';') {
1199 } while (g_ascii_isspace (session_id[i]));
1200 if (g_str_has_prefix (&session_id[i], "timeout=")) {
1203 /* if we parsed something valid, configure */
1204 if ((to = atoi (&session_id[i + 9])) > 0)
1211 /* make sure to not overflow */
1212 strncpy (conn->session_id, session_id, maxlen);
1213 conn->session_id[maxlen] = '\0';
1219 res = GST_RTSP_ERROR;
1228 * gst_rtsp_connection_read:
1229 * @conn: a #GstRTSPConnection
1230 * @data: the data to read
1231 * @size: the size of @data
1232 * @timeout: a timeout value or #NULL
1234 * Attempt to read @size bytes into @data from the connected @conn, blocking up to
1235 * the specified @timeout. @timeout can be #NULL, in which case this function
1236 * might block forever.
1238 * This function can be cancelled with gst_rtsp_connection_flush().
1240 * Returns: #GST_RTSP_OK on success.
1243 gst_rtsp_connection_read (GstRTSPConnection * conn, guint8 * data, guint size,
1251 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1252 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
1253 g_return_val_if_fail (conn->fd.fd >= 0, GST_RTSP_EINVAL);
1260 /* configure timeout if any */
1261 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1263 gst_poll_set_controllable (conn->fdset, TRUE);
1264 gst_poll_fd_ctl_write (conn->fdset, &conn->fd, FALSE);
1265 gst_poll_fd_ctl_read (conn->fdset, &conn->fd, TRUE);
1268 res = read_bytes (conn->fd.fd, data, &offset, size);
1269 if (res == GST_RTSP_EEOF)
1271 if (res == GST_RTSP_OK)
1273 if (res != GST_RTSP_EINTR)
1277 retval = gst_poll_wait (conn->fdset, to);
1278 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1280 /* check for timeout */
1282 goto select_timeout;
1290 gst_poll_set_controllable (conn->fdset, FALSE);
1297 return GST_RTSP_ESYS;
1301 return GST_RTSP_ETIMEOUT;
1305 return GST_RTSP_EINTR;
1309 return GST_RTSP_EEOF;
1319 * gst_rtsp_connection_receive:
1320 * @conn: a #GstRTSPConnection
1321 * @message: the message to read
1322 * @timeout: a timeout value or #NULL
1324 * Attempt to read into @message from the connected @conn, blocking up to
1325 * the specified @timeout. @timeout can be #NULL, in which case this function
1326 * might block forever.
1328 * This function can be cancelled with gst_rtsp_connection_flush().
1330 * Returns: #GST_RTSP_OK on success.
1333 gst_rtsp_connection_receive (GstRTSPConnection * conn, GstRTSPMessage * message,
1337 GstRTSPBuilder builder = { 0 };
1341 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1342 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1344 /* configure timeout if any */
1345 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1347 gst_poll_set_controllable (conn->fdset, TRUE);
1348 gst_poll_fd_ctl_write (conn->fdset, &conn->fd, FALSE);
1349 gst_poll_fd_ctl_read (conn->fdset, &conn->fd, TRUE);
1352 res = build_next (&builder, message, conn);
1353 if (res == GST_RTSP_EEOF)
1355 if (res == GST_RTSP_OK)
1357 if (res != GST_RTSP_EINTR)
1361 retval = gst_poll_wait (conn->fdset, to);
1362 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1364 /* check for timeout */
1366 goto select_timeout;
1374 gst_poll_set_controllable (conn->fdset, FALSE);
1377 /* we have a message here */
1378 build_reset (&builder);
1385 res = GST_RTSP_ESYS;
1390 res = GST_RTSP_ETIMEOUT;
1395 res = GST_RTSP_EINTR;
1400 res = GST_RTSP_EEOF;
1406 build_reset (&builder);
1407 gst_rtsp_message_unset (message);
1413 * gst_rtsp_connection_close:
1414 * @conn: a #GstRTSPConnection
1416 * Close the connected @conn.
1418 * Returns: #GST_RTSP_OK on success.
1421 gst_rtsp_connection_close (GstRTSPConnection * conn)
1425 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1430 if (conn->fd.fd != -1) {
1431 gst_poll_remove_fd (conn->fdset, &conn->fd);
1432 res = CLOSE_SOCKET (conn->fd.fd);
1442 return GST_RTSP_ESYS;
1447 * gst_rtsp_connection_free:
1448 * @conn: a #GstRTSPConnection
1450 * Close and free @conn.
1452 * Returns: #GST_RTSP_OK on success.
1455 gst_rtsp_connection_free (GstRTSPConnection * conn)
1459 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1461 res = gst_rtsp_connection_close (conn);
1462 gst_poll_free (conn->fdset);
1463 g_timer_destroy (conn->timer);
1464 g_free (conn->username);
1465 g_free (conn->passwd);
1466 gst_rtsp_connection_clear_auth_params (conn);
1476 * gst_rtsp_connection_poll:
1477 * @conn: a #GstRTSPConnection
1478 * @events: a bitmask of #GstRTSPEvent flags to check
1479 * @revents: location for result flags
1480 * @timeout: a timeout
1482 * Wait up to the specified @timeout for the connection to become available for
1483 * at least one of the operations specified in @events. When the function returns
1484 * with #GST_RTSP_OK, @revents will contain a bitmask of available operations on
1487 * @timeout can be #NULL, in which case this function might block forever.
1489 * This function can be cancelled with gst_rtsp_connection_flush().
1491 * Returns: #GST_RTSP_OK on success.
1496 gst_rtsp_connection_poll (GstRTSPConnection * conn, GstRTSPEvent events,
1497 GstRTSPEvent * revents, GTimeVal * timeout)
1502 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1503 g_return_val_if_fail (events != 0, GST_RTSP_EINVAL);
1504 g_return_val_if_fail (revents != NULL, GST_RTSP_EINVAL);
1505 g_return_val_if_fail (conn->fd.fd >= 0, GST_RTSP_EINVAL);
1507 gst_poll_set_controllable (conn->fdset, TRUE);
1509 /* add fd to writer set when asked to */
1510 gst_poll_fd_ctl_write (conn->fdset, &conn->fd, events & GST_RTSP_EV_WRITE);
1512 /* add fd to reader set when asked to */
1513 gst_poll_fd_ctl_read (conn->fdset, &conn->fd, events & GST_RTSP_EV_READ);
1515 /* configure timeout if any */
1516 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1519 retval = gst_poll_wait (conn->fdset, to);
1520 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1523 goto select_timeout;
1533 if (events & GST_RTSP_EV_READ) {
1534 if (gst_poll_fd_can_read (conn->fdset, &conn->fd))
1535 *revents |= GST_RTSP_EV_READ;
1537 if (events & GST_RTSP_EV_WRITE) {
1538 if (gst_poll_fd_can_write (conn->fdset, &conn->fd))
1539 *revents |= GST_RTSP_EV_WRITE;
1546 return GST_RTSP_ETIMEOUT;
1550 return GST_RTSP_ESYS;
1554 return GST_RTSP_EINTR;
1559 * gst_rtsp_connection_next_timeout:
1560 * @conn: a #GstRTSPConnection
1561 * @timeout: a timeout
1563 * Calculate the next timeout for @conn, storing the result in @timeout.
1565 * Returns: #GST_RTSP_OK.
1568 gst_rtsp_connection_next_timeout (GstRTSPConnection * conn, GTimeVal * timeout)
1574 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1575 g_return_val_if_fail (timeout != NULL, GST_RTSP_EINVAL);
1577 elapsed = g_timer_elapsed (conn->timer, &usec);
1578 if (elapsed >= conn->timeout) {
1582 sec = conn->timeout - elapsed;
1585 timeout->tv_sec = sec;
1586 timeout->tv_usec = usec;
1592 * gst_rtsp_connection_reset_timeout:
1593 * @conn: a #GstRTSPConnection
1595 * Reset the timeout of @conn.
1597 * Returns: #GST_RTSP_OK.
1600 gst_rtsp_connection_reset_timeout (GstRTSPConnection * conn)
1602 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1604 g_timer_start (conn->timer);
1610 * gst_rtsp_connection_flush:
1611 * @conn: a #GstRTSPConnection
1612 * @flush: start or stop the flush
1614 * Start or stop the flushing action on @conn. When flushing, all current
1615 * and future actions on @conn will return #GST_RTSP_EINTR until the connection
1616 * is set to non-flushing mode again.
1618 * Returns: #GST_RTSP_OK.
1621 gst_rtsp_connection_flush (GstRTSPConnection * conn, gboolean flush)
1623 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1625 gst_poll_set_flushing (conn->fdset, flush);
1631 * gst_rtsp_connection_set_auth:
1632 * @conn: a #GstRTSPConnection
1633 * @method: authentication method
1635 * @pass: the password
1637 * Configure @conn for authentication mode @method with @user and @pass as the
1638 * user and password respectively.
1640 * Returns: #GST_RTSP_OK.
1643 gst_rtsp_connection_set_auth (GstRTSPConnection * conn,
1644 GstRTSPAuthMethod method, const gchar * user, const gchar * pass)
1646 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1648 if (method == GST_RTSP_AUTH_DIGEST && ((user == NULL || pass == NULL)
1649 || g_strrstr (user, ":") != NULL))
1650 return GST_RTSP_EINVAL;
1652 /* Make sure the username and passwd are being set for authentication */
1653 if (method == GST_RTSP_AUTH_NONE && (user == NULL || pass == NULL))
1654 return GST_RTSP_EINVAL;
1656 /* ":" chars are not allowed in usernames for basic auth */
1657 if (method == GST_RTSP_AUTH_BASIC && g_strrstr (user, ":") != NULL)
1658 return GST_RTSP_EINVAL;
1660 g_free (conn->username);
1661 g_free (conn->passwd);
1663 conn->auth_method = method;
1664 conn->username = g_strdup (user);
1665 conn->passwd = g_strdup (pass);
1672 * @key: ASCII string to hash
1674 * Hashes @key in a case-insensitive manner.
1676 * Returns: the hash code.
1679 str_case_hash (gconstpointer key)
1681 const char *p = key;
1682 guint h = g_ascii_toupper (*p);
1685 for (p += 1; *p != '\0'; p++)
1686 h = (h << 5) - h + g_ascii_toupper (*p);
1693 * @v1: an ASCII string
1694 * @v2: another ASCII string
1696 * Compares @v1 and @v2 in a case-insensitive manner
1698 * Returns: %TRUE if they are equal (modulo case)
1701 str_case_equal (gconstpointer v1, gconstpointer v2)
1703 const char *string1 = v1;
1704 const char *string2 = v2;
1706 return g_ascii_strcasecmp (string1, string2) == 0;
1710 * gst_rtsp_connection_set_auth_param:
1711 * @conn: a #GstRTSPConnection
1712 * @param: authentication directive
1715 * Setup @conn with authentication directives. This is not necesary for
1716 * methods #GST_RTSP_AUTH_NONE and #GST_RTSP_AUTH_BASIC. For
1717 * #GST_RTSP_AUTH_DIGEST, directives should be taken from the digest challenge
1718 * in the WWW-Authenticate response header and can include realm, domain,
1719 * nonce, opaque, stale, algorithm, qop as per RFC2617.
1724 gst_rtsp_connection_set_auth_param (GstRTSPConnection * conn,
1725 const gchar * param, const gchar * value)
1727 g_return_if_fail (conn != NULL);
1728 g_return_if_fail (param != NULL);
1730 if (conn->auth_params == NULL) {
1732 g_hash_table_new_full (str_case_hash, str_case_equal, g_free, g_free);
1734 g_hash_table_insert (conn->auth_params, g_strdup (param), g_strdup (value));
1738 * gst_rtsp_connection_clear_auth_params:
1739 * @conn: a #GstRTSPConnection
1741 * Clear the list of authentication directives stored in @conn.
1746 gst_rtsp_connection_clear_auth_params (GstRTSPConnection * conn)
1748 g_return_if_fail (conn != NULL);
1750 if (conn->auth_params != NULL) {
1751 g_hash_table_destroy (conn->auth_params);
1752 conn->auth_params = NULL;
1757 * gst_rtsp_connection_set_qos_dscp:
1758 * @conn: a #GstRTSPConnection
1759 * @qos_dscp: DSCP value
1761 * Configure @conn to use the specified DSCP value.
1763 * Returns: #GST_RTSP_OK on success.
1768 gst_rtsp_connection_set_qos_dscp (GstRTSPConnection * conn, guint qos_dscp)
1773 struct sockaddr_in6 sa_in6;
1774 struct sockaddr_storage sa_stor;
1776 socklen_t slen = sizeof (sa);
1780 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1781 g_return_val_if_fail (conn->fd.fd >= 0, GST_RTSP_EINVAL);
1783 if (getsockname (conn->fd.fd, &sa.sa, &slen) < 0)
1784 goto no_getsockname;
1786 af = sa.sa.sa_family;
1788 /* if this is an IPv4-mapped address then do IPv4 QoS */
1789 if (af == AF_INET6) {
1791 if (IN6_IS_ADDR_V4MAPPED (&sa.sa_in6.sin6_addr))
1795 /* extract and shift 6 bits of the DSCP */
1796 tos = (qos_dscp & 0x3f) << 2;
1800 if (SETSOCKOPT (conn->fd.fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0)
1805 if (SETSOCKOPT (conn->fd.fd, IPPROTO_IPV6, IPV6_TCLASS, &tos,
1820 return GST_RTSP_ESYS;
1825 return GST_RTSP_ERROR;
1830 * gst_rtsp_connection_get_ip:
1831 * @conn: a #GstRTSPConnection
1833 * Retrieve the IP address of the other end of @conn.
1835 * Returns: The IP address as a string. this value remains valid until the
1836 * connection is closed.
1841 gst_rtsp_connection_get_ip (const GstRTSPConnection * conn)
1843 g_return_val_if_fail (conn != NULL, NULL);
1848 #define READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
1849 #define WRITE_COND (G_IO_OUT | G_IO_ERR)
1857 /* async functions */
1858 struct _GstRTSPWatch
1862 GstRTSPConnection *conn;
1864 GstRTSPBuilder builder;
1865 GstRTSPMessage message;
1869 gboolean write_added;
1871 /* queued message for transmission */
1878 GstRTSPWatchFuncs funcs;
1881 GDestroyNotify notify;
1885 gst_rtsp_source_prepare (GSource * source, gint * timeout)
1887 GstRTSPWatch *watch = (GstRTSPWatch *) source;
1889 *timeout = (watch->conn->timeout * 1000);
1895 gst_rtsp_source_check (GSource * source)
1897 GstRTSPWatch *watch = (GstRTSPWatch *) source;
1899 if (watch->readfd.revents & READ_COND)
1902 if (watch->writefd.revents & WRITE_COND)
1909 gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback,
1912 GstRTSPWatch *watch = (GstRTSPWatch *) source;
1915 /* first read as much as we can */
1916 if (watch->readfd.revents & READ_COND) {
1918 res = build_next (&watch->builder, &watch->message, watch->conn);
1919 if (res == GST_RTSP_EINTR)
1921 if (res == GST_RTSP_EEOF)
1923 if (res != GST_RTSP_OK)
1926 if (watch->funcs.message_received)
1927 watch->funcs.message_received (watch, &watch->message,
1930 gst_rtsp_message_unset (&watch->message);
1931 build_reset (&watch->builder);
1935 if (watch->writefd.revents & WRITE_COND) {
1937 if (watch->write_data == NULL) {
1940 if (!watch->messages)
1943 /* no data, get a new message from the queue */
1944 data = watch->messages->data;
1945 watch->messages = g_list_delete_link (watch->messages, watch->messages);
1947 watch->write_off = 0;
1948 watch->write_len = data->str->len;
1949 watch->write_data = (guint8 *) g_string_free (data->str, FALSE);
1950 watch->write_cseq = data->cseq;
1952 g_slice_free (GstRTSPRec, data);
1955 res = write_bytes (watch->writefd.fd, watch->write_data,
1956 &watch->write_off, watch->write_len);
1957 if (res == GST_RTSP_EINTR)
1959 if (res != GST_RTSP_OK)
1962 if (watch->funcs.message_sent)
1963 watch->funcs.message_sent (watch, watch->write_cseq, watch->user_data);
1966 if (watch->messages == NULL && watch->write_added) {
1967 g_source_remove_poll ((GSource *) watch, &watch->writefd);
1968 watch->write_added = FALSE;
1969 watch->writefd.revents = 0;
1971 g_free (watch->write_data);
1972 watch->write_data = NULL;
1981 if (watch->funcs.closed)
1982 watch->funcs.closed (watch, watch->user_data);
1987 if (watch->funcs.error)
1988 watch->funcs.error (watch, res, watch->user_data);
1994 gst_rtsp_source_finalize (GSource * source)
1996 GstRTSPWatch *watch = (GstRTSPWatch *) source;
1999 build_reset (&watch->builder);
2001 for (walk = watch->messages; walk; walk = g_list_next (walk)) {
2002 GstRTSPRec *data = walk->data;
2004 g_string_free (data->str, TRUE);
2005 g_slice_free (GstRTSPRec, data);
2007 g_list_free (watch->messages);
2008 g_free (watch->write_data);
2011 watch->notify (watch->user_data);
2014 static GSourceFuncs gst_rtsp_source_funcs = {
2015 gst_rtsp_source_prepare,
2016 gst_rtsp_source_check,
2017 gst_rtsp_source_dispatch,
2018 gst_rtsp_source_finalize
2022 * gst_rtsp_watch_new:
2023 * @conn: a #GstRTSPConnection
2024 * @funcs: watch functions
2025 * @user_data: user data to pass to @funcs
2027 * Create a watch object for @conn. The functions provided in @funcs will be
2028 * called with @user_data when activity happened on the watch.
2030 * The new watch is usually created so that it can be attached to a
2031 * maincontext with gst_rtsp_watch_attach().
2033 * @conn must exist for the entire lifetime of the watch.
2035 * Returns: a #GstRTSPWatch that can be used for asynchronous RTSP
2036 * communication. Free with gst_rtsp_watch_unref () after usage.
2041 gst_rtsp_watch_new (GstRTSPConnection * conn,
2042 GstRTSPWatchFuncs * funcs, gpointer user_data, GDestroyNotify notify)
2044 GstRTSPWatch *result;
2046 g_return_val_if_fail (conn != NULL, NULL);
2047 g_return_val_if_fail (funcs != NULL, NULL);
2049 result = (GstRTSPWatch *) g_source_new (&gst_rtsp_source_funcs,
2050 sizeof (GstRTSPWatch));
2052 result->conn = conn;
2053 result->builder.state = STATE_START;
2055 result->readfd.fd = conn->fd.fd;
2056 result->readfd.events = READ_COND;
2057 result->readfd.revents = 0;
2059 result->writefd.fd = conn->fd.fd;
2060 result->writefd.events = WRITE_COND;
2061 result->writefd.revents = 0;
2062 result->write_added = FALSE;
2064 result->funcs = *funcs;
2065 result->user_data = user_data;
2066 result->notify = notify;
2068 /* only add the read fd, the write fd is only added when we have data
2070 g_source_add_poll ((GSource *) result, &result->readfd);
2076 * gst_rtsp_watch_attach:
2077 * @watch: a #GstRTSPWatch
2078 * @context: a GMainContext (if NULL, the default context will be used)
2080 * Adds a #GstRTSPWatch to a context so that it will be executed within that context.
2082 * Returns: the ID (greater than 0) for the watch within the GMainContext.
2087 gst_rtsp_watch_attach (GstRTSPWatch * watch, GMainContext * context)
2089 g_return_val_if_fail (watch != NULL, 0);
2091 return g_source_attach ((GSource *) watch, context);
2095 * gst_rtsp_watch_free:
2096 * @watch: a #GstRTSPWatch
2098 * Decreases the reference count of @watch by one. If the resulting reference
2099 * count is zero the watch and associated memory will be destroyed.
2104 gst_rtsp_watch_unref (GstRTSPWatch * watch)
2106 g_return_if_fail (watch != NULL);
2108 g_source_unref ((GSource *) watch);
2112 * gst_rtsp_watch_queue_message:
2113 * @watch: a #GstRTSPWatch
2114 * @message: a #GstRTSPMessage
2116 * Queue a @message for transmission in @watch. The contents of this
2117 * message will be serialized and transmitted when the connection of the
2118 * watch becomes writable.
2120 * The return value of this function will be returned as the cseq argument in
2121 * the message_sent callback.
2123 * Returns: the sequence number of the message or -1 if the cseq could not be
2129 gst_rtsp_watch_queue_message (GstRTSPWatch * watch, GstRTSPMessage * message)
2135 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
2136 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2138 /* get the cseq from the message, when we finish writing this message on the
2139 * socket we will have to pass the cseq to the callback. */
2140 if (gst_rtsp_message_get_header (message, GST_RTSP_HDR_CSEQ, &header,
2141 0) == GST_RTSP_OK) {
2142 cseq = atoi (header);
2147 /* make a record with the message as a string ans cseq */
2148 data = g_slice_new (GstRTSPRec);
2149 data->str = message_to_string (watch->conn, message);
2152 /* add the record to a queue */
2153 watch->messages = g_list_append (watch->messages, data);
2155 /* make sure the main context will now also check for writability on the
2157 if (!watch->write_added) {
2158 g_source_add_poll ((GSource *) watch, &watch->writefd);
2159 watch->write_added = TRUE;