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)
120 inet_aton (const char *c, struct in_addr *paddr)
122 /* note that inet_addr is deprecated on unix because
123 * inet_addr returns -1 (INADDR_NONE) for the valid 255.255.255.255
125 paddr->s_addr = inet_addr (c);
127 if (paddr->s_addr == INADDR_NONE)
144 /* a structure for constructing RTSPMessages */
157 build_reset (GstRTSPBuilder * builder)
159 g_free (builder->body_data);
160 memset (builder, 0, sizeof (builder));
164 * gst_rtsp_connection_create:
165 * @url: a #GstRTSPUrl
166 * @conn: a #GstRTSPConnection
168 * Create a newly allocated #GstRTSPConnection from @url and store it in @conn.
169 * The connection will not yet attempt to connect to @url, use
170 * gst_rtsp_connection_connect().
172 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
175 gst_rtsp_connection_create (GstRTSPUrl * url, GstRTSPConnection ** conn)
177 GstRTSPConnection *newconn;
179 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
181 newconn = g_new0 (GstRTSPConnection, 1);
183 if ((newconn->fdset = gst_poll_new (TRUE)) == NULL)
188 newconn->timer = g_timer_new ();
189 newconn->timeout = 60;
191 newconn->auth_method = GST_RTSP_AUTH_NONE;
192 newconn->username = NULL;
193 newconn->passwd = NULL;
194 newconn->auth_params = NULL;
204 return GST_RTSP_ESYS;
209 * gst_rtsp_connection_connect:
210 * @conn: a #GstRTSPConnection
211 * @timeout: a #GTimeVal timeout
213 * Attempt to connect to the url of @conn made with
214 * gst_rtsp_connection_create(). If @timeout is #NULL this function can block
215 * forever. If @timeout contains a valid timeout, this function will return
216 * #GST_RTSP_ETIMEOUT after the timeout expired.
218 * This function can be cancelled with gst_rtsp_connection_flush().
220 * Returns: #GST_RTSP_OK when a connection could be made.
223 gst_rtsp_connection_connect (GstRTSPConnection * conn, GTimeVal * timeout)
226 struct sockaddr_in sa_in;
227 struct hostent *hostinfo;
237 unsigned long flags = 1;
238 struct in_addr *addrp;
241 gchar ipbuf[INET_ADDRSTRLEN];
242 #endif /* G_OS_WIN32 */
244 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
245 g_return_val_if_fail (conn->url != NULL, GST_RTSP_EINVAL);
246 g_return_val_if_fail (conn->fd.fd < 0, GST_RTSP_EINVAL);
250 /* first check if it already is an IP address */
251 if (inet_aton (url->host, &addr)) {
254 hostinfo = gethostbyname (url->host);
256 goto not_resolved; /* h_errno set */
258 if (hostinfo->h_addrtype != AF_INET)
259 goto not_ip; /* host not an IP host */
261 addrp = (struct in_addr *) hostinfo->h_addr_list[0];
262 /* this is not threadsafe */
263 ip = inet_ntoa (*addrp);
265 addrs = hostinfo->h_addr_list;
266 ip = inet_ntop (AF_INET, (struct in_addr *) addrs[0], ipbuf,
268 #endif /* G_OS_WIN32 */
271 /* get the port from the url */
272 gst_rtsp_url_get_port (url, &port);
274 memset (&sa_in, 0, sizeof (sa_in));
275 sa_in.sin_family = AF_INET; /* network socket */
276 sa_in.sin_port = htons (port); /* on port */
277 sa_in.sin_addr.s_addr = inet_addr (ip); /* on host ip */
279 fd = socket (AF_INET, SOCK_STREAM, 0);
283 /* set to non-blocking mode so that we can cancel the connect */
285 fcntl (fd, F_SETFL, O_NONBLOCK);
287 ioctlsocket (fd, FIONBIO, &flags);
288 #endif /* G_OS_WIN32 */
290 /* add the socket to our fdset */
292 gst_poll_add_fd (conn->fdset, &conn->fd);
294 /* we are going to connect ASYNC now */
295 ret = connect (fd, (struct sockaddr *) &sa_in, sizeof (sa_in));
298 if (!ERRNO_IS_EINPROGRESS)
301 /* wait for connect to complete up to the specified timeout or until we got
303 gst_poll_fd_ctl_write (conn->fdset, &conn->fd, TRUE);
305 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
308 retval = gst_poll_wait (conn->fdset, to);
309 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
313 else if (retval == -1)
316 /* we can still have an error connecting on windows */
317 if (gst_poll_fd_has_error (conn->fdset, &conn->fd)) {
318 socklen_t len = sizeof (errno);
319 getsockopt (conn->fd.fd, SOL_SOCKET, SO_ERROR, &errno, &len);
323 gst_poll_fd_ignored (conn->fdset, &conn->fd);
326 conn->ip = g_strdup (ip);
332 GST_ERROR ("system error %d (%s)", errno, g_strerror (errno));
333 if (conn->fd.fd >= 0) {
334 GST_DEBUG ("remove fd %d", conn->fd.fd);
335 gst_poll_remove_fd (conn->fdset, &conn->fd);
340 return GST_RTSP_ESYS;
344 GST_ERROR ("could not resolve %s", url->host);
345 return GST_RTSP_ENET;
349 GST_ERROR ("not an IP address");
350 return GST_RTSP_ENOTIP;
354 GST_ERROR ("timeout");
355 if (conn->fd.fd >= 0) {
356 GST_DEBUG ("remove fd %d", conn->fd.fd);
357 gst_poll_remove_fd (conn->fdset, &conn->fd);
362 return GST_RTSP_ETIMEOUT;
367 md5_digest_to_hex_string (unsigned char digest[16], char string[33])
369 static const char hexdigits[] = "0123456789abcdef";
372 for (i = 0; i < 16; i++) {
373 string[i * 2] = hexdigits[(digest[i] >> 4) & 0x0f];
374 string[i * 2 + 1] = hexdigits[digest[i] & 0x0f];
380 auth_digest_compute_hex_urp (const gchar * username,
381 const gchar * realm, const gchar * password, gchar hex_urp[33])
383 struct MD5Context md5_context;
384 unsigned char digest[16];
386 MD5Init (&md5_context);
387 MD5Update (&md5_context, username, strlen (username));
388 MD5Update (&md5_context, ":", 1);
389 MD5Update (&md5_context, realm, strlen (realm));
390 MD5Update (&md5_context, ":", 1);
391 MD5Update (&md5_context, password, strlen (password));
392 MD5Final (digest, &md5_context);
393 md5_digest_to_hex_string (digest, hex_urp);
397 auth_digest_compute_response (const gchar * method,
398 const gchar * uri, const gchar * hex_a1, const gchar * nonce,
402 struct MD5Context md5_context;
403 unsigned char digest[16];
406 MD5Init (&md5_context);
407 MD5Update (&md5_context, method, strlen (method));
408 MD5Update (&md5_context, ":", 1);
409 MD5Update (&md5_context, uri, strlen (uri));
410 MD5Final (digest, &md5_context);
411 md5_digest_to_hex_string (digest, hex_a2);
414 MD5Init (&md5_context);
415 MD5Update (&md5_context, hex_a1, strlen (hex_a1));
416 MD5Update (&md5_context, ":", 1);
417 MD5Update (&md5_context, nonce, strlen (nonce));
418 MD5Update (&md5_context, ":", 1);
420 MD5Update (&md5_context, hex_a2, 32);
421 MD5Final (digest, &md5_context);
422 md5_digest_to_hex_string (digest, response);
426 add_auth_header (GstRTSPConnection * conn, GstRTSPMessage * message)
428 switch (conn->auth_method) {
429 case GST_RTSP_AUTH_BASIC:{
431 g_strdup_printf ("%s:%s", conn->username, conn->passwd);
433 gst_rtsp_base64_encode (user_pass, strlen (user_pass));
434 gchar *auth_string = g_strdup_printf ("Basic %s", user_pass64);
436 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
440 g_free (user_pass64);
443 case GST_RTSP_AUTH_DIGEST:{
444 gchar response[33], hex_urp[33];
445 gchar *auth_string, *auth_string2;
452 /* we need to have some params set */
453 if (conn->auth_params == NULL)
456 /* we need the realm and nonce */
457 realm = (gchar *) g_hash_table_lookup (conn->auth_params, "realm");
458 nonce = (gchar *) g_hash_table_lookup (conn->auth_params, "nonce");
459 if (realm == NULL || nonce == NULL)
462 auth_digest_compute_hex_urp (conn->username, realm, conn->passwd,
465 method = gst_rtsp_method_as_text (message->type_data.request.method);
466 uri = message->type_data.request.uri;
468 /* Assume no qop, algorithm=md5, stale=false */
469 /* For algorithm MD5, a1 = urp. */
470 auth_digest_compute_response (method, uri, hex_urp, nonce, response);
471 auth_string = g_strdup_printf ("Digest username=\"%s\", "
472 "realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"",
473 conn->username, realm, nonce, uri, response);
475 opaque = (gchar *) g_hash_table_lookup (conn->auth_params, "opaque");
477 auth_string2 = g_strdup_printf ("%s, opaque=\"%s\"", auth_string,
479 g_free (auth_string);
480 auth_string = auth_string2;
482 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
493 add_date_header (GstRTSPMessage * message)
496 gchar date_string[100];
503 g_get_current_time (&tv);
504 t = (time_t) tv.tv_sec;
507 strftime (date_string, sizeof (date_string), "%a, %d %b %Y %H:%M:%S GMT",
508 gmtime_r (&t, &tm_));
510 strftime (date_string, sizeof (date_string), "%a, %d %b %Y %H:%M:%S GMT",
514 gst_rtsp_message_add_header (message, GST_RTSP_HDR_DATE, date_string);
518 write_bytes (gint fd, const guint8 * buffer, guint * idx, guint size)
523 return GST_RTSP_ERROR;
530 r = WRITE_SOCKET (fd, &buffer[*idx], left);
532 return GST_RTSP_EINTR;
535 return GST_RTSP_EINTR;
537 return GST_RTSP_ESYS;
547 read_bytes (gint fd, guint8 * buffer, guint * idx, guint size)
552 return GST_RTSP_ERROR;
559 r = READ_SOCKET (fd, &buffer[*idx], left);
561 return GST_RTSP_EEOF;
564 return GST_RTSP_EINTR;
566 return GST_RTSP_ESYS;
576 read_line (gint fd, guint8 * buffer, guint * idx, guint size)
582 r = READ_SOCKET (fd, &c, 1);
584 return GST_RTSP_EEOF;
587 return GST_RTSP_EINTR;
589 return GST_RTSP_ESYS;
591 if (c == '\n') /* end on \n */
593 if (c == '\r') /* ignore \r */
597 buffer[(*idx)++] = c;
606 * gst_rtsp_connection_write:
607 * @conn: a #GstRTSPConnection
608 * @data: the data to write
609 * @size: the size of @data
610 * @timeout: a timeout value or #NULL
612 * Attempt to write @size bytes of @data to the connected @conn, blocking up to
613 * the specified @timeout. @timeout can be #NULL, in which case this function
614 * might block forever.
616 * This function can be cancelled with gst_rtsp_connection_flush().
618 * Returns: #GST_RTSP_OK on success.
621 gst_rtsp_connection_write (GstRTSPConnection * conn, const guint8 * data,
622 guint size, GTimeVal * timeout)
629 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
630 g_return_val_if_fail (data != NULL || size == 0, GST_RTSP_EINVAL);
631 g_return_val_if_fail (conn->fd.fd >= 0, GST_RTSP_EINVAL);
633 gst_poll_set_controllable (conn->fdset, TRUE);
634 gst_poll_fd_ctl_write (conn->fdset, &conn->fd, TRUE);
635 gst_poll_fd_ctl_read (conn->fdset, &conn->fd, FALSE);
636 /* clear all previous poll results */
637 gst_poll_fd_ignored (conn->fdset, &conn->fd);
639 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
645 res = write_bytes (conn->fd.fd, data, &offset, size);
646 if (res == GST_RTSP_OK)
648 if (res != GST_RTSP_EINTR)
651 /* not all is written, wait until we can write more */
653 retval = gst_poll_wait (conn->fdset, to);
654 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
671 return GST_RTSP_ETIMEOUT;
675 return GST_RTSP_ESYS;
679 return GST_RTSP_EINTR;
688 message_to_string (GstRTSPConnection * conn, GstRTSPMessage * message)
692 str = g_string_new ("");
694 switch (message->type) {
695 case GST_RTSP_MESSAGE_REQUEST:
696 /* create request string, add CSeq */
697 g_string_append_printf (str, "%s %s RTSP/1.0\r\n"
699 gst_rtsp_method_as_text (message->type_data.request.method),
700 message->type_data.request.uri, conn->cseq++);
701 /* add session id if we have one */
702 if (conn->session_id[0] != '\0') {
703 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SESSION,
706 /* add any authentication headers */
707 add_auth_header (conn, message);
709 case GST_RTSP_MESSAGE_RESPONSE:
710 /* create response string */
711 g_string_append_printf (str, "RTSP/1.0 %d %s\r\n",
712 message->type_data.response.code, message->type_data.response.reason);
714 case GST_RTSP_MESSAGE_DATA:
716 guint8 data_header[4];
718 /* prepare data header */
719 data_header[0] = '$';
720 data_header[1] = message->type_data.data.channel;
721 data_header[2] = (message->body_size >> 8) & 0xff;
722 data_header[3] = message->body_size & 0xff;
724 /* create string with header and data */
725 str = g_string_append_len (str, (gchar *) data_header, 4);
727 g_string_append_len (str, (gchar *) message->body,
732 g_string_free (str, TRUE);
733 g_return_val_if_reached (NULL);
737 /* append headers and body */
738 if (message->type != GST_RTSP_MESSAGE_DATA) {
739 /* add date header */
740 add_date_header (message);
743 gst_rtsp_message_append_headers (message, str);
745 /* append Content-Length and body if needed */
746 if (message->body != NULL && message->body_size > 0) {
749 len = g_strdup_printf ("%d", message->body_size);
750 g_string_append_printf (str, "%s: %s\r\n",
751 gst_rtsp_header_as_text (GST_RTSP_HDR_CONTENT_LENGTH), len);
753 /* header ends here */
754 g_string_append (str, "\r\n");
756 g_string_append_len (str, (gchar *) message->body,
759 /* just end headers */
760 g_string_append (str, "\r\n");
768 * gst_rtsp_connection_send:
769 * @conn: a #GstRTSPConnection
770 * @message: the message to send
771 * @timeout: a timeout value or #NULL
773 * Attempt to send @message to the connected @conn, blocking up to
774 * the specified @timeout. @timeout can be #NULL, in which case this function
775 * might block forever.
777 * This function can be cancelled with gst_rtsp_connection_flush().
779 * Returns: #GST_RTSP_OK on success.
782 gst_rtsp_connection_send (GstRTSPConnection * conn, GstRTSPMessage * message,
793 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
794 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
797 error = WSAStartup (0x0202, &w);
802 if (w.wVersion != 0x0202)
806 if (!(str = message_to_string (conn, message)))
811 gst_rtsp_connection_write (conn, (guint8 *) str->str, str->len, timeout);
813 g_string_free (str, TRUE);
819 g_warning ("Wrong message");
820 return GST_RTSP_EINVAL;
825 g_warning ("Error %d on WSAStartup", error);
826 return GST_RTSP_EWSASTART;
830 g_warning ("Windows sockets are not version 0x202 (current 0x%x)",
833 return GST_RTSP_EWSAVERSION;
839 parse_string (gchar * dest, gint size, gchar ** src)
845 while (g_ascii_isspace (**src))
848 while (!g_ascii_isspace (**src) && **src != '\0') {
858 parse_key (gchar * dest, gint size, gchar ** src)
863 while (**src != ':' && **src != '\0') {
873 parse_response_status (guint8 * buffer, GstRTSPMessage * msg)
876 gchar versionstr[20];
881 bptr = (gchar *) buffer;
883 parse_string (versionstr, sizeof (versionstr), &bptr);
884 parse_string (codestr, sizeof (codestr), &bptr);
885 code = atoi (codestr);
887 while (g_ascii_isspace (*bptr))
890 if (strcmp (versionstr, "RTSP/1.0") == 0)
891 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
893 else if (strncmp (versionstr, "RTSP/", 5) == 0) {
894 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
896 msg->type_data.response.version = GST_RTSP_VERSION_INVALID;
904 return GST_RTSP_EPARSE;
909 parse_request_line (guint8 * buffer, GstRTSPMessage * msg)
911 GstRTSPResult res = GST_RTSP_OK;
912 gchar versionstr[20];
916 GstRTSPMethod method;
918 bptr = (gchar *) buffer;
920 parse_string (methodstr, sizeof (methodstr), &bptr);
921 method = gst_rtsp_find_method (methodstr);
923 parse_string (urlstr, sizeof (urlstr), &bptr);
925 res = GST_RTSP_EPARSE;
927 parse_string (versionstr, sizeof (versionstr), &bptr);
930 res = GST_RTSP_EPARSE;
932 if (strcmp (versionstr, "RTSP/1.0") == 0) {
933 if (gst_rtsp_message_init_request (msg, method, urlstr) != GST_RTSP_OK)
934 res = GST_RTSP_EPARSE;
935 } else if (strncmp (versionstr, "RTSP/", 5) == 0) {
936 if (gst_rtsp_message_init_request (msg, method, urlstr) != GST_RTSP_OK)
937 res = GST_RTSP_EPARSE;
938 msg->type_data.request.version = GST_RTSP_VERSION_INVALID;
940 gst_rtsp_message_init_request (msg, method, urlstr);
941 msg->type_data.request.version = GST_RTSP_VERSION_INVALID;
942 res = GST_RTSP_EPARSE;
948 /* parsing lines means reading a Key: Value pair */
950 parse_line (guint8 * buffer, GstRTSPMessage * msg)
954 GstRTSPHeaderField field;
956 bptr = (gchar *) buffer;
959 parse_key (key, sizeof (key), &bptr);
965 field = gst_rtsp_find_header_field (key);
966 if (field != GST_RTSP_HDR_INVALID) {
967 while (g_ascii_isspace (*bptr))
969 gst_rtsp_message_add_header (msg, field, bptr);
976 return GST_RTSP_EPARSE;
981 * GST_RTSP_OK when a complete message was read.
982 * GST_RTSP_EEOF: when the socket is closed
983 * GST_RTSP_EINTR: when more data is needed.
984 * GST_RTSP_..: some other error occured.
987 build_next (GstRTSPBuilder * builder, GstRTSPMessage * message,
988 GstRTSPConnection * conn)
993 switch (builder->state) {
997 read_bytes (conn->fd.fd, (guint8 *) builder->buffer,
998 &builder->offset, 1);
999 if (res != GST_RTSP_OK)
1002 /* we have 1 bytes now and we can see if this is a data message or
1004 if (builder->buffer[0] == '$') {
1005 /* data message, prepare for the header */
1006 builder->state = STATE_DATA_HEADER;
1009 builder->state = STATE_READ_LINES;
1012 case STATE_DATA_HEADER:
1015 read_bytes (conn->fd.fd, (guint8 *) builder->buffer,
1016 &builder->offset, 4);
1017 if (res != GST_RTSP_OK)
1020 gst_rtsp_message_init_data (message, builder->buffer[1]);
1022 builder->body_len = (builder->buffer[2] << 8) | builder->buffer[3];
1023 builder->body_data = g_malloc (builder->body_len + 1);
1024 builder->body_data[builder->body_len] = '\0';
1025 builder->offset = 0;
1026 builder->state = STATE_DATA_BODY;
1029 case STATE_DATA_BODY:
1031 res = read_bytes (conn->fd.fd, builder->body_data, &builder->offset,
1033 if (res != GST_RTSP_OK)
1036 /* we have the complete body now */
1037 gst_rtsp_message_take_body (message,
1038 (guint8 *) builder->body_data, builder->body_len);
1039 builder->body_data = NULL;
1040 builder->body_len = 0;
1042 builder->state = STATE_END;
1045 case STATE_READ_LINES:
1047 res = read_line (conn->fd.fd, builder->buffer, &builder->offset,
1048 sizeof (builder->buffer));
1049 if (res != GST_RTSP_OK)
1052 /* we have a regular response */
1053 if (builder->buffer[0] == '\r') {
1054 builder->buffer[0] = '\0';
1057 if (builder->buffer[0] == '\0') {
1060 /* empty line, end of message header */
1061 /* see if there is a Content-Length header */
1062 if (gst_rtsp_message_get_header (message,
1063 GST_RTSP_HDR_CONTENT_LENGTH, &hdrval, 0) == GST_RTSP_OK) {
1064 /* there is, prepare to read the body */
1065 builder->body_len = atol (hdrval);
1066 builder->body_data = g_malloc (builder->body_len + 1);
1067 builder->body_data[builder->body_len] = '\0';
1068 builder->offset = 0;
1069 builder->state = STATE_DATA_BODY;
1071 builder->state = STATE_END;
1076 /* we have a line */
1077 if (builder->line == 0) {
1078 /* first line, check for response status */
1079 if (memcmp (builder->buffer, "RTSP", 4) == 0) {
1080 res = parse_response_status (builder->buffer, message);
1082 res = parse_request_line (builder->buffer, message);
1085 /* else just parse the line */
1086 parse_line (builder->buffer, message);
1089 builder->offset = 0;
1096 /* save session id in the connection for further use */
1097 if (gst_rtsp_message_get_header (message, GST_RTSP_HDR_SESSION,
1098 &session_id, 0) == GST_RTSP_OK) {
1101 maxlen = sizeof (conn->session_id) - 1;
1102 /* the sessionid can have attributes marked with ;
1103 * Make sure we strip them */
1104 for (i = 0; session_id[i] != '\0'; i++) {
1105 if (session_id[i] == ';') {
1110 } while (g_ascii_isspace (session_id[i]));
1111 if (g_str_has_prefix (&session_id[i], "timeout=")) {
1114 /* if we parsed something valid, configure */
1115 if ((to = atoi (&session_id[i + 9])) > 0)
1122 /* make sure to not overflow */
1123 strncpy (conn->session_id, session_id, maxlen);
1124 conn->session_id[maxlen] = '\0';
1130 res = GST_RTSP_ERROR;
1139 * gst_rtsp_connection_read:
1140 * @conn: a #GstRTSPConnection
1141 * @data: the data to read
1142 * @size: the size of @data
1143 * @timeout: a timeout value or #NULL
1145 * Attempt to read @size bytes into @data from the connected @conn, blocking up to
1146 * the specified @timeout. @timeout can be #NULL, in which case this function
1147 * might block forever.
1149 * This function can be cancelled with gst_rtsp_connection_flush().
1151 * Returns: #GST_RTSP_OK on success.
1154 gst_rtsp_connection_read (GstRTSPConnection * conn, guint8 * data, guint size,
1162 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1163 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
1164 g_return_val_if_fail (conn->fd.fd >= 0, GST_RTSP_EINVAL);
1171 /* configure timeout if any */
1172 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1174 gst_poll_set_controllable (conn->fdset, TRUE);
1175 gst_poll_fd_ctl_write (conn->fdset, &conn->fd, FALSE);
1176 gst_poll_fd_ctl_read (conn->fdset, &conn->fd, TRUE);
1179 res = read_bytes (conn->fd.fd, data, &offset, size);
1180 if (res == GST_RTSP_EEOF)
1182 if (res == GST_RTSP_OK)
1184 if (res != GST_RTSP_EINTR)
1188 retval = gst_poll_wait (conn->fdset, to);
1189 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1191 /* check for timeout */
1193 goto select_timeout;
1201 gst_poll_set_controllable (conn->fdset, FALSE);
1208 return GST_RTSP_ESYS;
1212 return GST_RTSP_ETIMEOUT;
1216 return GST_RTSP_EINTR;
1220 return GST_RTSP_EEOF;
1230 * gst_rtsp_connection_receive:
1231 * @conn: a #GstRTSPConnection
1232 * @message: the message to read
1233 * @timeout: a timeout value or #NULL
1235 * Attempt to read into @message from the connected @conn, blocking up to
1236 * the specified @timeout. @timeout can be #NULL, in which case this function
1237 * might block forever.
1239 * This function can be cancelled with gst_rtsp_connection_flush().
1241 * Returns: #GST_RTSP_OK on success.
1244 gst_rtsp_connection_receive (GstRTSPConnection * conn, GstRTSPMessage * message,
1248 GstRTSPBuilder builder = { 0 };
1252 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1253 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1255 /* configure timeout if any */
1256 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1258 gst_poll_set_controllable (conn->fdset, TRUE);
1259 gst_poll_fd_ctl_write (conn->fdset, &conn->fd, FALSE);
1260 gst_poll_fd_ctl_read (conn->fdset, &conn->fd, TRUE);
1263 res = build_next (&builder, message, conn);
1264 if (res == GST_RTSP_EEOF)
1266 if (res == GST_RTSP_OK)
1268 if (res != GST_RTSP_EINTR)
1272 retval = gst_poll_wait (conn->fdset, to);
1273 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1275 /* check for timeout */
1277 goto select_timeout;
1285 gst_poll_set_controllable (conn->fdset, FALSE);
1288 /* we have a message here */
1289 build_reset (&builder);
1296 res = GST_RTSP_ESYS;
1301 res = GST_RTSP_ETIMEOUT;
1306 res = GST_RTSP_EINTR;
1311 res = GST_RTSP_EEOF;
1317 build_reset (&builder);
1318 gst_rtsp_message_unset (message);
1324 * gst_rtsp_connection_close:
1325 * @conn: a #GstRTSPConnection
1327 * Close the connected @conn.
1329 * Returns: #GST_RTSP_OK on success.
1332 gst_rtsp_connection_close (GstRTSPConnection * conn)
1336 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1341 if (conn->fd.fd != -1) {
1342 gst_poll_remove_fd (conn->fdset, &conn->fd);
1343 res = CLOSE_SOCKET (conn->fd.fd);
1356 return GST_RTSP_ESYS;
1361 * gst_rtsp_connection_free:
1362 * @conn: a #GstRTSPConnection
1364 * Close and free @conn.
1366 * Returns: #GST_RTSP_OK on success.
1369 gst_rtsp_connection_free (GstRTSPConnection * conn)
1373 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1375 res = gst_rtsp_connection_close (conn);
1376 gst_poll_free (conn->fdset);
1380 g_timer_destroy (conn->timer);
1381 g_free (conn->username);
1382 g_free (conn->passwd);
1383 gst_rtsp_connection_clear_auth_params (conn);
1390 * gst_rtsp_connection_poll:
1391 * @conn: a #GstRTSPConnection
1392 * @events: a bitmask of #GstRTSPEvent flags to check
1393 * @revents: location for result flags
1394 * @timeout: a timeout
1396 * Wait up to the specified @timeout for the connection to become available for
1397 * at least one of the operations specified in @events. When the function returns
1398 * with #GST_RTSP_OK, @revents will contain a bitmask of available operations on
1401 * @timeout can be #NULL, in which case this function might block forever.
1403 * This function can be cancelled with gst_rtsp_connection_flush().
1405 * Returns: #GST_RTSP_OK on success.
1410 gst_rtsp_connection_poll (GstRTSPConnection * conn, GstRTSPEvent events,
1411 GstRTSPEvent * revents, GTimeVal * timeout)
1416 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1417 g_return_val_if_fail (events != 0, GST_RTSP_EINVAL);
1418 g_return_val_if_fail (revents != NULL, GST_RTSP_EINVAL);
1419 g_return_val_if_fail (conn->fd.fd >= 0, GST_RTSP_EINVAL);
1421 gst_poll_set_controllable (conn->fdset, TRUE);
1423 /* add fd to writer set when asked to */
1424 gst_poll_fd_ctl_write (conn->fdset, &conn->fd, events & GST_RTSP_EV_WRITE);
1426 /* add fd to reader set when asked to */
1427 gst_poll_fd_ctl_read (conn->fdset, &conn->fd, events & GST_RTSP_EV_READ);
1429 /* configure timeout if any */
1430 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1433 retval = gst_poll_wait (conn->fdset, to);
1434 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1437 goto select_timeout;
1447 if (events & GST_RTSP_EV_READ) {
1448 if (gst_poll_fd_can_read (conn->fdset, &conn->fd))
1449 *revents |= GST_RTSP_EV_READ;
1451 if (events & GST_RTSP_EV_WRITE) {
1452 if (gst_poll_fd_can_write (conn->fdset, &conn->fd))
1453 *revents |= GST_RTSP_EV_WRITE;
1460 return GST_RTSP_ETIMEOUT;
1464 return GST_RTSP_ESYS;
1468 return GST_RTSP_EINTR;
1473 * gst_rtsp_connection_next_timeout:
1474 * @conn: a #GstRTSPConnection
1475 * @timeout: a timeout
1477 * Calculate the next timeout for @conn, storing the result in @timeout.
1479 * Returns: #GST_RTSP_OK.
1482 gst_rtsp_connection_next_timeout (GstRTSPConnection * conn, GTimeVal * timeout)
1488 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1489 g_return_val_if_fail (timeout != NULL, GST_RTSP_EINVAL);
1491 elapsed = g_timer_elapsed (conn->timer, &usec);
1492 if (elapsed >= conn->timeout) {
1496 sec = conn->timeout - elapsed;
1499 timeout->tv_sec = sec;
1500 timeout->tv_usec = usec;
1506 * gst_rtsp_connection_reset_timeout:
1507 * @conn: a #GstRTSPConnection
1509 * Reset the timeout of @conn.
1511 * Returns: #GST_RTSP_OK.
1514 gst_rtsp_connection_reset_timeout (GstRTSPConnection * conn)
1516 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1518 g_timer_start (conn->timer);
1524 * gst_rtsp_connection_flush:
1525 * @conn: a #GstRTSPConnection
1526 * @flush: start or stop the flush
1528 * Start or stop the flushing action on @conn. When flushing, all current
1529 * and future actions on @conn will return #GST_RTSP_EINTR until the connection
1530 * is set to non-flushing mode again.
1532 * Returns: #GST_RTSP_OK.
1535 gst_rtsp_connection_flush (GstRTSPConnection * conn, gboolean flush)
1537 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1539 gst_poll_set_flushing (conn->fdset, flush);
1545 * gst_rtsp_connection_set_auth:
1546 * @conn: a #GstRTSPConnection
1547 * @method: authentication method
1549 * @pass: the password
1551 * Configure @conn for authentication mode @method with @user and @pass as the
1552 * user and password respectively.
1554 * Returns: #GST_RTSP_OK.
1557 gst_rtsp_connection_set_auth (GstRTSPConnection * conn,
1558 GstRTSPAuthMethod method, const gchar * user, const gchar * pass)
1560 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1562 if (method == GST_RTSP_AUTH_DIGEST && ((user == NULL || pass == NULL)
1563 || g_strrstr (user, ":") != NULL))
1564 return GST_RTSP_EINVAL;
1566 /* Make sure the username and passwd are being set for authentication */
1567 if (method == GST_RTSP_AUTH_NONE && (user == NULL || pass == NULL))
1568 return GST_RTSP_EINVAL;
1570 /* ":" chars are not allowed in usernames for basic auth */
1571 if (method == GST_RTSP_AUTH_BASIC && g_strrstr (user, ":") != NULL)
1572 return GST_RTSP_EINVAL;
1574 g_free (conn->username);
1575 g_free (conn->passwd);
1577 conn->auth_method = method;
1578 conn->username = g_strdup (user);
1579 conn->passwd = g_strdup (pass);
1586 * @key: ASCII string to hash
1588 * Hashes @key in a case-insensitive manner.
1590 * Returns: the hash code.
1593 str_case_hash (gconstpointer key)
1595 const char *p = key;
1596 guint h = g_ascii_toupper (*p);
1599 for (p += 1; *p != '\0'; p++)
1600 h = (h << 5) - h + g_ascii_toupper (*p);
1607 * @v1: an ASCII string
1608 * @v2: another ASCII string
1610 * Compares @v1 and @v2 in a case-insensitive manner
1612 * Returns: %TRUE if they are equal (modulo case)
1615 str_case_equal (gconstpointer v1, gconstpointer v2)
1617 const char *string1 = v1;
1618 const char *string2 = v2;
1620 return g_ascii_strcasecmp (string1, string2) == 0;
1624 * gst_rtsp_connection_set_auth_param:
1625 * @conn: a #GstRTSPConnection
1626 * @param: authentication directive
1629 * Setup @conn with authentication directives. This is not necesary for
1630 * methods #GST_RTSP_AUTH_NONE and #GST_RTSP_AUTH_BASIC. For
1631 * #GST_RTSP_AUTH_DIGEST, directives should be taken from the digest challenge
1632 * in the WWW-Authenticate response header and can include realm, domain,
1633 * nonce, opaque, stale, algorithm, qop as per RFC2617.
1638 gst_rtsp_connection_set_auth_param (GstRTSPConnection * conn,
1639 const gchar * param, const gchar * value)
1641 g_return_if_fail (conn != NULL);
1642 g_return_if_fail (param != NULL);
1644 if (conn->auth_params == NULL) {
1646 g_hash_table_new_full (str_case_hash, str_case_equal, g_free, g_free);
1648 g_hash_table_insert (conn->auth_params, g_strdup (param), g_strdup (value));
1652 * gst_rtsp_connection_clear_auth_params:
1653 * @conn: a #GstRTSPConnection
1655 * Clear the list of authentication directives stored in @conn.
1660 gst_rtsp_connection_clear_auth_params (GstRTSPConnection * conn)
1662 g_return_if_fail (conn != NULL);
1664 if (conn->auth_params != NULL) {
1665 g_hash_table_destroy (conn->auth_params);
1666 conn->auth_params = NULL;
1671 * gst_rtsp_connection_set_qos_dscp:
1672 * @conn: a #GstRTSPConnection
1673 * @qos_dscp: DSCP value
1675 * Configure @conn to use the specified DSCP value.
1677 * Returns: #GST_RTSP_OK on success.
1682 gst_rtsp_connection_set_qos_dscp (GstRTSPConnection * conn, guint qos_dscp)
1684 struct sockaddr_storage sa_s;
1685 socklen_t sa_sl = sizeof (sa_s);
1689 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1690 g_return_val_if_fail (conn->fd.fd >= 0, GST_RTSP_EINVAL);
1692 if (getsockname (conn->fd.fd, (struct sockaddr *) &sa_s, &sa_sl) < 0)
1693 goto no_getsockname;
1695 af = sa_s.ss_family;
1697 /* if this is an IPv4-mapped address then do IPv4 QoS */
1698 if (af == AF_INET6) {
1699 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *) &sa_s;
1701 if (IN6_IS_ADDR_V4MAPPED (&saddr6->sin6_addr))
1705 /* extract and shift 6 bits of the DSCP */
1706 tos = (qos_dscp & 0x3f) << 2;
1710 if (SETSOCKOPT (conn->fd.fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0)
1715 if (SETSOCKOPT (conn->fd.fd, IPPROTO_IPV6, IPV6_TCLASS, &tos,
1730 return GST_RTSP_ESYS;
1735 return GST_RTSP_ERROR;
1740 * gst_rtsp_connection_get_ip:
1741 * @conn: a #GstRTSPConnection
1743 * Retrieve the IP address of the other end of @conn.
1745 * Returns: The IP address as a string. this value remains valid until the
1746 * connection is closed.
1751 gst_rtsp_connection_get_ip (const GstRTSPConnection * conn)
1753 g_return_val_if_fail (conn != NULL, NULL);
1758 #define READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
1759 #define WRITE_COND (G_IO_OUT | G_IO_ERR)
1767 /* async functions */
1768 struct _GstRTSPChannel
1772 GstRTSPConnection *conn;
1774 GstRTSPBuilder builder;
1775 GstRTSPMessage message;
1779 gboolean write_added;
1781 /* queued message for transmission */
1788 GstRTSPChannelFuncs funcs;
1791 GDestroyNotify notify;
1795 gst_rtsp_source_prepare (GSource * source, gint * timeout)
1797 GstRTSPChannel *channel = (GstRTSPChannel *) source;
1799 *timeout = (channel->conn->timeout * 1000);
1805 gst_rtsp_source_check (GSource * source)
1807 GstRTSPChannel *channel = (GstRTSPChannel *) source;
1809 if (channel->readfd.revents & READ_COND)
1812 if (channel->writefd.revents & WRITE_COND)
1819 gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback,
1822 GstRTSPChannel *channel = (GstRTSPChannel *) source;
1825 /* first read as much as we can */
1826 if (channel->readfd.revents & READ_COND) {
1828 res = build_next (&channel->builder, &channel->message, channel->conn);
1829 if (res == GST_RTSP_EINTR)
1831 if (res == GST_RTSP_EEOF)
1833 if (res != GST_RTSP_OK)
1836 if (channel->funcs.message_received)
1837 channel->funcs.message_received (channel, &channel->message,
1838 channel->user_data);
1840 gst_rtsp_message_unset (&channel->message);
1841 build_reset (&channel->builder);
1845 if (channel->writefd.revents & WRITE_COND) {
1847 if (channel->write_data == NULL) {
1850 if (!channel->messages)
1853 /* no data, get a new message from the queue */
1854 data = channel->messages->data;
1856 g_list_delete_link (channel->messages, channel->messages);
1858 channel->write_off = 0;
1859 channel->write_len = data->str->len;
1860 channel->write_data = (guint8 *) g_string_free (data->str, FALSE);
1861 channel->write_cseq = data->cseq;
1863 g_slice_free (GstRTSPRec, data);
1866 res = write_bytes (channel->writefd.fd, channel->write_data,
1867 &channel->write_off, channel->write_len);
1868 if (res == GST_RTSP_EINTR)
1870 if (res != GST_RTSP_OK)
1873 if (channel->funcs.message_sent)
1874 channel->funcs.message_sent (channel, channel->write_cseq,
1875 channel->user_data);
1878 if (channel->messages == NULL && channel->write_added) {
1879 g_source_remove_poll ((GSource *) channel, &channel->writefd);
1880 channel->write_added = FALSE;
1881 channel->writefd.revents = 0;
1883 g_free (channel->write_data);
1884 channel->write_data = NULL;
1893 if (channel->funcs.closed)
1894 channel->funcs.closed (channel, channel->user_data);
1899 if (channel->funcs.error)
1900 channel->funcs.error (channel, res, channel->user_data);
1906 gst_rtsp_source_finalize (GSource * source)
1908 GstRTSPChannel *channel = (GstRTSPChannel *) source;
1911 build_reset (&channel->builder);
1913 for (walk = channel->messages; walk; walk = g_list_next (walk)) {
1914 GstRTSPRec *data = walk->data;
1916 g_string_free (data->str, TRUE);
1917 g_slice_free (GstRTSPRec, data);
1919 g_list_free (channel->messages);
1920 g_free (channel->write_data);
1922 if (channel->notify)
1923 channel->notify (channel->user_data);
1926 static GSourceFuncs gst_rtsp_source_funcs = {
1927 gst_rtsp_source_prepare,
1928 gst_rtsp_source_check,
1929 gst_rtsp_source_dispatch,
1930 gst_rtsp_source_finalize
1934 * gst_rtsp_channel_new:
1935 * @conn: a #GstRTSPConnection
1936 * @funcs: channel functions
1937 * @user_data: user data to pass to @funcs
1939 * Create a channel object for @conn. The functions provided in @funcs will be
1940 * called with @user_data when activity happened on the channel.
1942 * The new channel is usually created so that it can be attached to a
1943 * maincontext with gst_rtsp_channel_attach().
1945 * @conn must exist for the entire lifetime of the channel.
1947 * Returns: a #GstRTSPChannel that can be used for asynchronous RTSP
1948 * communication. Free with gst_rtsp_channel_unref () after usage.
1953 gst_rtsp_channel_new (GstRTSPConnection * conn,
1954 GstRTSPChannelFuncs * funcs, gpointer user_data, GDestroyNotify notify)
1956 GstRTSPChannel *result;
1958 g_return_val_if_fail (conn != NULL, NULL);
1959 g_return_val_if_fail (funcs != NULL, NULL);
1961 result = (GstRTSPChannel *) g_source_new (&gst_rtsp_source_funcs,
1962 sizeof (GstRTSPChannel));
1964 result->conn = conn;
1965 result->builder.state = STATE_START;
1967 result->readfd.fd = conn->fd.fd;
1968 result->readfd.events = READ_COND;
1969 result->readfd.revents = 0;
1971 result->writefd.fd = conn->fd.fd;
1972 result->writefd.events = WRITE_COND;
1973 result->writefd.revents = 0;
1974 result->write_added = FALSE;
1976 result->funcs = *funcs;
1977 result->user_data = user_data;
1978 result->notify = notify;
1980 /* only add the read fd, the write fd is only added when we have data
1982 g_source_add_poll ((GSource *) result, &result->readfd);
1988 * gst_rtsp_channel_attach:
1989 * @channel: a #GstRTSPChannel
1990 * @context: a GMainContext (if NULL, the default context will be used)
1992 * Adds a #GstRTSPChannel to a context so that it will be executed within that context.
1994 * Returns: the ID (greater than 0) for the channel within the GMainContext.
1999 gst_rtsp_channel_attach (GstRTSPChannel * channel, GMainContext * context)
2001 g_return_val_if_fail (channel != NULL, 0);
2003 return g_source_attach ((GSource *) channel, context);
2007 * gst_rtsp_channel_free:
2008 * @channel: a #GstRTSPChannel
2010 * Decreases the reference count of @channel by one. If the resulting reference
2011 * count is zero the channel and associated memory will be destroyed.
2016 gst_rtsp_channel_unref (GstRTSPChannel * channel)
2018 g_return_if_fail (channel != NULL);
2020 g_source_unref ((GSource *) channel);
2024 * gst_rtsp_channel_queue_message:
2025 * @channel: a #GstRTSPChannel
2026 * @message: a #GstRTSPMessage
2028 * Queue a @message for transmission in @channel. The contents of this
2029 * message will be serialized and transmitted when the connection of the
2030 * channel becomes writable.
2032 * The return value of this function will be returned as the cseq argument in
2033 * the message_sent callback.
2035 * Returns: the sequence number of the message or -1 if the cseq could not be
2041 gst_rtsp_channel_queue_message (GstRTSPChannel * channel,
2042 GstRTSPMessage * message)
2048 g_return_val_if_fail (channel != NULL, GST_RTSP_EINVAL);
2049 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2051 /* get the cseq from the message, when we finish writing this message on the
2052 * socket we will have to pass the cseq to the callback. */
2053 if (gst_rtsp_message_get_header (message, GST_RTSP_HDR_CSEQ, &header,
2054 0) == GST_RTSP_OK) {
2055 cseq = atoi (header);
2060 /* make a record with the message as a string ans cseq */
2061 data = g_slice_new (GstRTSPRec);
2062 data->str = message_to_string (channel->conn, message);
2065 /* add the record to a queue */
2066 channel->messages = g_list_append (channel->messages, data);
2068 /* make sure the main context will now also check for writability on the
2070 if (!channel->write_added) {
2071 g_source_add_poll ((GSource *) channel, &channel->writefd);
2072 channel->write_added = TRUE;