VirtualBox

source: vbox/trunk/src/libs/curl-7.64.0/lib/rtsp.c@ 94601

Last change on this file since 94601 was 85671, checked in by vboxsync, 4 years ago

Export out internal curl copy to make it a lot simpler to build VBox (OSE) on Windows. bugref:9814

  • Property svn:eol-style set to native
File size: 25.6 KB
Line 
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2018, Daniel Stenberg, <[email protected]>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22
23#include "curl_setup.h"
24
25#ifndef CURL_DISABLE_RTSP
26
27#include "urldata.h"
28#include <curl/curl.h>
29#include "transfer.h"
30#include "sendf.h"
31#include "multiif.h"
32#include "http.h"
33#include "url.h"
34#include "progress.h"
35#include "rtsp.h"
36#include "strcase.h"
37#include "select.h"
38#include "connect.h"
39#include "strdup.h"
40/* The last 3 #include files should be in this order */
41#include "curl_printf.h"
42#include "curl_memory.h"
43#include "memdebug.h"
44
45/*
46 * TODO (general)
47 * -incoming server requests
48 * -server CSeq counter
49 * -digest authentication
50 * -connect through proxy
51 * -pipelining?
52 */
53
54
55#define RTP_PKT_CHANNEL(p) ((int)((unsigned char)((p)[1])))
56
57#define RTP_PKT_LENGTH(p) ((((int)((unsigned char)((p)[2]))) << 8) | \
58 ((int)((unsigned char)((p)[3]))))
59
60/* protocol-specific functions set up to be called by the main engine */
61static CURLcode rtsp_do(struct connectdata *conn, bool *done);
62static CURLcode rtsp_done(struct connectdata *conn, CURLcode, bool premature);
63static CURLcode rtsp_connect(struct connectdata *conn, bool *done);
64static CURLcode rtsp_disconnect(struct connectdata *conn, bool dead);
65
66static int rtsp_getsock_do(struct connectdata *conn,
67 curl_socket_t *socks,
68 int numsocks);
69
70/*
71 * Parse and write out any available RTP data.
72 *
73 * nread: amount of data left after k->str. will be modified if RTP
74 * data is parsed and k->str is moved up
75 * readmore: whether or not the RTP parser needs more data right away
76 */
77static CURLcode rtsp_rtp_readwrite(struct Curl_easy *data,
78 struct connectdata *conn,
79 ssize_t *nread,
80 bool *readmore);
81
82static CURLcode rtsp_setup_connection(struct connectdata *conn);
83
84bool rtsp_connisdead(struct connectdata *check);
85static unsigned int rtsp_conncheck(struct connectdata *check,
86 unsigned int checks_to_perform);
87
88/* this returns the socket to wait for in the DO and DOING state for the multi
89 interface and then we're always _sending_ a request and thus we wait for
90 the single socket to become writable only */
91static int rtsp_getsock_do(struct connectdata *conn,
92 curl_socket_t *socks,
93 int numsocks)
94{
95 /* write mode */
96 (void)numsocks; /* unused, we trust it to be at least 1 */
97 socks[0] = conn->sock[FIRSTSOCKET];
98 return GETSOCK_WRITESOCK(0);
99}
100
101static
102CURLcode rtp_client_write(struct connectdata *conn, char *ptr, size_t len);
103
104
105/*
106 * RTSP handler interface.
107 */
108const struct Curl_handler Curl_handler_rtsp = {
109 "RTSP", /* scheme */
110 rtsp_setup_connection, /* setup_connection */
111 rtsp_do, /* do_it */
112 rtsp_done, /* done */
113 ZERO_NULL, /* do_more */
114 rtsp_connect, /* connect_it */
115 ZERO_NULL, /* connecting */
116 ZERO_NULL, /* doing */
117 ZERO_NULL, /* proto_getsock */
118 rtsp_getsock_do, /* doing_getsock */
119 ZERO_NULL, /* domore_getsock */
120 ZERO_NULL, /* perform_getsock */
121 rtsp_disconnect, /* disconnect */
122 rtsp_rtp_readwrite, /* readwrite */
123 rtsp_conncheck, /* connection_check */
124 PORT_RTSP, /* defport */
125 CURLPROTO_RTSP, /* protocol */
126 PROTOPT_NONE /* flags */
127};
128
129
130static CURLcode rtsp_setup_connection(struct connectdata *conn)
131{
132 struct RTSP *rtsp;
133
134 conn->data->req.protop = rtsp = calloc(1, sizeof(struct RTSP));
135 if(!rtsp)
136 return CURLE_OUT_OF_MEMORY;
137
138 return CURLE_OK;
139}
140
141
142/*
143 * The server may send us RTP data at any point, and RTSPREQ_RECEIVE does not
144 * want to block the application forever while receiving a stream. Therefore,
145 * we cannot assume that an RTSP socket is dead just because it is readable.
146 *
147 * Instead, if it is readable, run Curl_connalive() to peek at the socket
148 * and distinguish between closed and data.
149 */
150bool rtsp_connisdead(struct connectdata *check)
151{
152 int sval;
153 bool ret_val = TRUE;
154
155 sval = SOCKET_READABLE(check->sock[FIRSTSOCKET], 0);
156 if(sval == 0) {
157 /* timeout */
158 ret_val = FALSE;
159 }
160 else if(sval & CURL_CSELECT_ERR) {
161 /* socket is in an error state */
162 ret_val = TRUE;
163 }
164 else if(sval & CURL_CSELECT_IN) {
165 /* readable with no error. could still be closed */
166 ret_val = !Curl_connalive(check);
167 }
168
169 return ret_val;
170}
171
172/*
173 * Function to check on various aspects of a connection.
174 */
175static unsigned int rtsp_conncheck(struct connectdata *check,
176 unsigned int checks_to_perform)
177{
178 unsigned int ret_val = CONNRESULT_NONE;
179
180 if(checks_to_perform & CONNCHECK_ISDEAD) {
181 if(rtsp_connisdead(check))
182 ret_val |= CONNRESULT_DEAD;
183 }
184
185 return ret_val;
186}
187
188
189static CURLcode rtsp_connect(struct connectdata *conn, bool *done)
190{
191 CURLcode httpStatus;
192 struct Curl_easy *data = conn->data;
193
194 httpStatus = Curl_http_connect(conn, done);
195
196 /* Initialize the CSeq if not already done */
197 if(data->state.rtsp_next_client_CSeq == 0)
198 data->state.rtsp_next_client_CSeq = 1;
199 if(data->state.rtsp_next_server_CSeq == 0)
200 data->state.rtsp_next_server_CSeq = 1;
201
202 conn->proto.rtspc.rtp_channel = -1;
203
204 return httpStatus;
205}
206
207static CURLcode rtsp_disconnect(struct connectdata *conn, bool dead)
208{
209 (void) dead;
210 Curl_safefree(conn->proto.rtspc.rtp_buf);
211 return CURLE_OK;
212}
213
214
215static CURLcode rtsp_done(struct connectdata *conn,
216 CURLcode status, bool premature)
217{
218 struct Curl_easy *data = conn->data;
219 struct RTSP *rtsp = data->req.protop;
220 CURLcode httpStatus;
221
222 /* Bypass HTTP empty-reply checks on receive */
223 if(data->set.rtspreq == RTSPREQ_RECEIVE)
224 premature = TRUE;
225
226 httpStatus = Curl_http_done(conn, status, premature);
227
228 if(rtsp) {
229 /* Check the sequence numbers */
230 long CSeq_sent = rtsp->CSeq_sent;
231 long CSeq_recv = rtsp->CSeq_recv;
232 if((data->set.rtspreq != RTSPREQ_RECEIVE) && (CSeq_sent != CSeq_recv)) {
233 failf(data,
234 "The CSeq of this request %ld did not match the response %ld",
235 CSeq_sent, CSeq_recv);
236 return CURLE_RTSP_CSEQ_ERROR;
237 }
238 if(data->set.rtspreq == RTSPREQ_RECEIVE &&
239 (conn->proto.rtspc.rtp_channel == -1)) {
240 infof(data, "Got an RTP Receive with a CSeq of %ld\n", CSeq_recv);
241 /* TODO CPC: Server -> Client logic here */
242 }
243 }
244
245 return httpStatus;
246}
247
248static CURLcode rtsp_do(struct connectdata *conn, bool *done)
249{
250 struct Curl_easy *data = conn->data;
251 CURLcode result = CURLE_OK;
252 Curl_RtspReq rtspreq = data->set.rtspreq;
253 struct RTSP *rtsp = data->req.protop;
254 struct HTTP *http;
255 Curl_send_buffer *req_buffer;
256 curl_off_t postsize = 0; /* for ANNOUNCE and SET_PARAMETER */
257 curl_off_t putsize = 0; /* for ANNOUNCE and SET_PARAMETER */
258
259 const char *p_request = NULL;
260 const char *p_session_id = NULL;
261 const char *p_accept = NULL;
262 const char *p_accept_encoding = NULL;
263 const char *p_range = NULL;
264 const char *p_referrer = NULL;
265 const char *p_stream_uri = NULL;
266 const char *p_transport = NULL;
267 const char *p_uagent = NULL;
268 const char *p_proxyuserpwd = NULL;
269 const char *p_userpwd = NULL;
270
271 *done = TRUE;
272
273 http = &(rtsp->http_wrapper);
274 /* Assert that no one has changed the RTSP struct in an evil way */
275 DEBUGASSERT((void *)http == (void *)rtsp);
276
277 rtsp->CSeq_sent = data->state.rtsp_next_client_CSeq;
278 rtsp->CSeq_recv = 0;
279
280 /* Setup the 'p_request' pointer to the proper p_request string
281 * Since all RTSP requests are included here, there is no need to
282 * support custom requests like HTTP.
283 **/
284 data->set.opt_no_body = TRUE; /* most requests don't contain a body */
285 switch(rtspreq) {
286 default:
287 failf(data, "Got invalid RTSP request");
288 return CURLE_BAD_FUNCTION_ARGUMENT;
289 case RTSPREQ_OPTIONS:
290 p_request = "OPTIONS";
291 break;
292 case RTSPREQ_DESCRIBE:
293 p_request = "DESCRIBE";
294 data->set.opt_no_body = FALSE;
295 break;
296 case RTSPREQ_ANNOUNCE:
297 p_request = "ANNOUNCE";
298 break;
299 case RTSPREQ_SETUP:
300 p_request = "SETUP";
301 break;
302 case RTSPREQ_PLAY:
303 p_request = "PLAY";
304 break;
305 case RTSPREQ_PAUSE:
306 p_request = "PAUSE";
307 break;
308 case RTSPREQ_TEARDOWN:
309 p_request = "TEARDOWN";
310 break;
311 case RTSPREQ_GET_PARAMETER:
312 /* GET_PARAMETER's no_body status is determined later */
313 p_request = "GET_PARAMETER";
314 data->set.opt_no_body = FALSE;
315 break;
316 case RTSPREQ_SET_PARAMETER:
317 p_request = "SET_PARAMETER";
318 break;
319 case RTSPREQ_RECORD:
320 p_request = "RECORD";
321 break;
322 case RTSPREQ_RECEIVE:
323 p_request = "";
324 /* Treat interleaved RTP as body*/
325 data->set.opt_no_body = FALSE;
326 break;
327 case RTSPREQ_LAST:
328 failf(data, "Got invalid RTSP request: RTSPREQ_LAST");
329 return CURLE_BAD_FUNCTION_ARGUMENT;
330 }
331
332 if(rtspreq == RTSPREQ_RECEIVE) {
333 Curl_setup_transfer(conn, FIRSTSOCKET, -1, TRUE,
334 &http->readbytecount, -1, NULL);
335
336 return result;
337 }
338
339 p_session_id = data->set.str[STRING_RTSP_SESSION_ID];
340 if(!p_session_id &&
341 (rtspreq & ~(RTSPREQ_OPTIONS | RTSPREQ_DESCRIBE | RTSPREQ_SETUP))) {
342 failf(data, "Refusing to issue an RTSP request [%s] without a session ID.",
343 p_request);
344 return CURLE_BAD_FUNCTION_ARGUMENT;
345 }
346
347 /* TODO: proxy? */
348
349 /* Stream URI. Default to server '*' if not specified */
350 if(data->set.str[STRING_RTSP_STREAM_URI]) {
351 p_stream_uri = data->set.str[STRING_RTSP_STREAM_URI];
352 }
353 else {
354 p_stream_uri = "*";
355 }
356
357 /* Transport Header for SETUP requests */
358 p_transport = Curl_checkheaders(conn, "Transport");
359 if(rtspreq == RTSPREQ_SETUP && !p_transport) {
360 /* New Transport: setting? */
361 if(data->set.str[STRING_RTSP_TRANSPORT]) {
362 Curl_safefree(conn->allocptr.rtsp_transport);
363
364 conn->allocptr.rtsp_transport =
365 aprintf("Transport: %s\r\n",
366 data->set.str[STRING_RTSP_TRANSPORT]);
367 if(!conn->allocptr.rtsp_transport)
368 return CURLE_OUT_OF_MEMORY;
369 }
370 else {
371 failf(data,
372 "Refusing to issue an RTSP SETUP without a Transport: header.");
373 return CURLE_BAD_FUNCTION_ARGUMENT;
374 }
375
376 p_transport = conn->allocptr.rtsp_transport;
377 }
378
379 /* Accept Headers for DESCRIBE requests */
380 if(rtspreq == RTSPREQ_DESCRIBE) {
381 /* Accept Header */
382 p_accept = Curl_checkheaders(conn, "Accept")?
383 NULL:"Accept: application/sdp\r\n";
384
385 /* Accept-Encoding header */
386 if(!Curl_checkheaders(conn, "Accept-Encoding") &&
387 data->set.str[STRING_ENCODING]) {
388 Curl_safefree(conn->allocptr.accept_encoding);
389 conn->allocptr.accept_encoding =
390 aprintf("Accept-Encoding: %s\r\n", data->set.str[STRING_ENCODING]);
391
392 if(!conn->allocptr.accept_encoding)
393 return CURLE_OUT_OF_MEMORY;
394
395 p_accept_encoding = conn->allocptr.accept_encoding;
396 }
397 }
398
399 /* The User-Agent string might have been allocated in url.c already, because
400 it might have been used in the proxy connect, but if we have got a header
401 with the user-agent string specified, we erase the previously made string
402 here. */
403 if(Curl_checkheaders(conn, "User-Agent") && conn->allocptr.uagent) {
404 Curl_safefree(conn->allocptr.uagent);
405 conn->allocptr.uagent = NULL;
406 }
407 else if(!Curl_checkheaders(conn, "User-Agent") &&
408 data->set.str[STRING_USERAGENT]) {
409 p_uagent = conn->allocptr.uagent;
410 }
411
412 /* setup the authentication headers */
413 result = Curl_http_output_auth(conn, p_request, p_stream_uri, FALSE);
414 if(result)
415 return result;
416
417 p_proxyuserpwd = conn->allocptr.proxyuserpwd;
418 p_userpwd = conn->allocptr.userpwd;
419
420 /* Referrer */
421 Curl_safefree(conn->allocptr.ref);
422 if(data->change.referer && !Curl_checkheaders(conn, "Referer"))
423 conn->allocptr.ref = aprintf("Referer: %s\r\n", data->change.referer);
424 else
425 conn->allocptr.ref = NULL;
426
427 p_referrer = conn->allocptr.ref;
428
429 /*
430 * Range Header
431 * Only applies to PLAY, PAUSE, RECORD
432 *
433 * Go ahead and use the Range stuff supplied for HTTP
434 */
435 if(data->state.use_range &&
436 (rtspreq & (RTSPREQ_PLAY | RTSPREQ_PAUSE | RTSPREQ_RECORD))) {
437
438 /* Check to see if there is a range set in the custom headers */
439 if(!Curl_checkheaders(conn, "Range") && data->state.range) {
440 Curl_safefree(conn->allocptr.rangeline);
441 conn->allocptr.rangeline = aprintf("Range: %s\r\n", data->state.range);
442 p_range = conn->allocptr.rangeline;
443 }
444 }
445
446 /*
447 * Sanity check the custom headers
448 */
449 if(Curl_checkheaders(conn, "CSeq")) {
450 failf(data, "CSeq cannot be set as a custom header.");
451 return CURLE_RTSP_CSEQ_ERROR;
452 }
453 if(Curl_checkheaders(conn, "Session")) {
454 failf(data, "Session ID cannot be set as a custom header.");
455 return CURLE_BAD_FUNCTION_ARGUMENT;
456 }
457
458 /* Initialize a dynamic send buffer */
459 req_buffer = Curl_add_buffer_init();
460
461 if(!req_buffer)
462 return CURLE_OUT_OF_MEMORY;
463
464 result =
465 Curl_add_bufferf(&req_buffer,
466 "%s %s RTSP/1.0\r\n" /* Request Stream-URI RTSP/1.0 */
467 "CSeq: %ld\r\n", /* CSeq */
468 p_request, p_stream_uri, rtsp->CSeq_sent);
469 if(result)
470 return result;
471
472 /*
473 * Rather than do a normal alloc line, keep the session_id unformatted
474 * to make comparison easier
475 */
476 if(p_session_id) {
477 result = Curl_add_bufferf(&req_buffer, "Session: %s\r\n", p_session_id);
478 if(result)
479 return result;
480 }
481
482 /*
483 * Shared HTTP-like options
484 */
485 result = Curl_add_bufferf(&req_buffer,
486 "%s" /* transport */
487 "%s" /* accept */
488 "%s" /* accept-encoding */
489 "%s" /* range */
490 "%s" /* referrer */
491 "%s" /* user-agent */
492 "%s" /* proxyuserpwd */
493 "%s" /* userpwd */
494 ,
495 p_transport ? p_transport : "",
496 p_accept ? p_accept : "",
497 p_accept_encoding ? p_accept_encoding : "",
498 p_range ? p_range : "",
499 p_referrer ? p_referrer : "",
500 p_uagent ? p_uagent : "",
501 p_proxyuserpwd ? p_proxyuserpwd : "",
502 p_userpwd ? p_userpwd : "");
503
504 /*
505 * Free userpwd now --- cannot reuse this for Negotiate and possibly NTLM
506 * with basic and digest, it will be freed anyway by the next request
507 */
508 Curl_safefree(conn->allocptr.userpwd);
509 conn->allocptr.userpwd = NULL;
510
511 if(result)
512 return result;
513
514 if((rtspreq == RTSPREQ_SETUP) || (rtspreq == RTSPREQ_DESCRIBE)) {
515 result = Curl_add_timecondition(data, req_buffer);
516 if(result)
517 return result;
518 }
519
520 result = Curl_add_custom_headers(conn, FALSE, req_buffer);
521 if(result)
522 return result;
523
524 if(rtspreq == RTSPREQ_ANNOUNCE ||
525 rtspreq == RTSPREQ_SET_PARAMETER ||
526 rtspreq == RTSPREQ_GET_PARAMETER) {
527
528 if(data->set.upload) {
529 putsize = data->state.infilesize;
530 data->set.httpreq = HTTPREQ_PUT;
531
532 }
533 else {
534 postsize = (data->state.infilesize != -1)?
535 data->state.infilesize:
536 (data->set.postfields? (curl_off_t)strlen(data->set.postfields):0);
537 data->set.httpreq = HTTPREQ_POST;
538 }
539
540 if(putsize > 0 || postsize > 0) {
541 /* As stated in the http comments, it is probably not wise to
542 * actually set a custom Content-Length in the headers */
543 if(!Curl_checkheaders(conn, "Content-Length")) {
544 result =
545 Curl_add_bufferf(&req_buffer,
546 "Content-Length: %" CURL_FORMAT_CURL_OFF_T"\r\n",
547 (data->set.upload ? putsize : postsize));
548 if(result)
549 return result;
550 }
551
552 if(rtspreq == RTSPREQ_SET_PARAMETER ||
553 rtspreq == RTSPREQ_GET_PARAMETER) {
554 if(!Curl_checkheaders(conn, "Content-Type")) {
555 result = Curl_add_bufferf(&req_buffer,
556 "Content-Type: text/parameters\r\n");
557 if(result)
558 return result;
559 }
560 }
561
562 if(rtspreq == RTSPREQ_ANNOUNCE) {
563 if(!Curl_checkheaders(conn, "Content-Type")) {
564 result = Curl_add_bufferf(&req_buffer,
565 "Content-Type: application/sdp\r\n");
566 if(result)
567 return result;
568 }
569 }
570
571 data->state.expect100header = FALSE; /* RTSP posts are simple/small */
572 }
573 else if(rtspreq == RTSPREQ_GET_PARAMETER) {
574 /* Check for an empty GET_PARAMETER (heartbeat) request */
575 data->set.httpreq = HTTPREQ_HEAD;
576 data->set.opt_no_body = TRUE;
577 }
578 }
579
580 /* RTSP never allows chunked transfer */
581 data->req.forbidchunk = TRUE;
582 /* Finish the request buffer */
583 result = Curl_add_buffer(&req_buffer, "\r\n", 2);
584 if(result)
585 return result;
586
587 if(postsize > 0) {
588 result = Curl_add_buffer(&req_buffer, data->set.postfields,
589 (size_t)postsize);
590 if(result)
591 return result;
592 }
593
594 /* issue the request */
595 result = Curl_add_buffer_send(&req_buffer, conn,
596 &data->info.request_size, 0, FIRSTSOCKET);
597 if(result) {
598 failf(data, "Failed sending RTSP request");
599 return result;
600 }
601
602 Curl_setup_transfer(conn, FIRSTSOCKET, -1, TRUE, &http->readbytecount,
603 putsize?FIRSTSOCKET:-1,
604 putsize?&http->writebytecount:NULL);
605
606 /* Increment the CSeq on success */
607 data->state.rtsp_next_client_CSeq++;
608
609 if(http->writebytecount) {
610 /* if a request-body has been sent off, we make sure this progress is
611 noted properly */
612 Curl_pgrsSetUploadCounter(data, http->writebytecount);
613 if(Curl_pgrsUpdate(conn))
614 result = CURLE_ABORTED_BY_CALLBACK;
615 }
616
617 return result;
618}
619
620
621static CURLcode rtsp_rtp_readwrite(struct Curl_easy *data,
622 struct connectdata *conn,
623 ssize_t *nread,
624 bool *readmore) {
625 struct SingleRequest *k = &data->req;
626 struct rtsp_conn *rtspc = &(conn->proto.rtspc);
627
628 char *rtp; /* moving pointer to rtp data */
629 ssize_t rtp_dataleft; /* how much data left to parse in this round */
630 char *scratch;
631 CURLcode result;
632
633 if(rtspc->rtp_buf) {
634 /* There was some leftover data the last time. Merge buffers */
635 char *newptr = Curl_saferealloc(rtspc->rtp_buf,
636 rtspc->rtp_bufsize + *nread);
637 if(!newptr) {
638 rtspc->rtp_buf = NULL;
639 rtspc->rtp_bufsize = 0;
640 return CURLE_OUT_OF_MEMORY;
641 }
642 rtspc->rtp_buf = newptr;
643 memcpy(rtspc->rtp_buf + rtspc->rtp_bufsize, k->str, *nread);
644 rtspc->rtp_bufsize += *nread;
645 rtp = rtspc->rtp_buf;
646 rtp_dataleft = rtspc->rtp_bufsize;
647 }
648 else {
649 /* Just parse the request buffer directly */
650 rtp = k->str;
651 rtp_dataleft = *nread;
652 }
653
654 while((rtp_dataleft > 0) &&
655 (rtp[0] == '$')) {
656 if(rtp_dataleft > 4) {
657 int rtp_length;
658
659 /* Parse the header */
660 /* The channel identifier immediately follows and is 1 byte */
661 rtspc->rtp_channel = RTP_PKT_CHANNEL(rtp);
662
663 /* The length is two bytes */
664 rtp_length = RTP_PKT_LENGTH(rtp);
665
666 if(rtp_dataleft < rtp_length + 4) {
667 /* Need more - incomplete payload*/
668 *readmore = TRUE;
669 break;
670 }
671 /* We have the full RTP interleaved packet
672 * Write out the header including the leading '$' */
673 DEBUGF(infof(data, "RTP write channel %d rtp_length %d\n",
674 rtspc->rtp_channel, rtp_length));
675 result = rtp_client_write(conn, &rtp[0], rtp_length + 4);
676 if(result) {
677 failf(data, "Got an error writing an RTP packet");
678 *readmore = FALSE;
679 Curl_safefree(rtspc->rtp_buf);
680 rtspc->rtp_buf = NULL;
681 rtspc->rtp_bufsize = 0;
682 return result;
683 }
684
685 /* Move forward in the buffer */
686 rtp_dataleft -= rtp_length + 4;
687 rtp += rtp_length + 4;
688
689 if(data->set.rtspreq == RTSPREQ_RECEIVE) {
690 /* If we are in a passive receive, give control back
691 * to the app as often as we can.
692 */
693 k->keepon &= ~KEEP_RECV;
694 }
695 }
696 else {
697 /* Need more - incomplete header */
698 *readmore = TRUE;
699 break;
700 }
701 }
702
703 if(rtp_dataleft != 0 && rtp[0] == '$') {
704 DEBUGF(infof(data, "RTP Rewinding %zd %s\n", rtp_dataleft,
705 *readmore ? "(READMORE)" : ""));
706
707 /* Store the incomplete RTP packet for a "rewind" */
708 scratch = malloc(rtp_dataleft);
709 if(!scratch) {
710 Curl_safefree(rtspc->rtp_buf);
711 rtspc->rtp_buf = NULL;
712 rtspc->rtp_bufsize = 0;
713 return CURLE_OUT_OF_MEMORY;
714 }
715 memcpy(scratch, rtp, rtp_dataleft);
716 Curl_safefree(rtspc->rtp_buf);
717 rtspc->rtp_buf = scratch;
718 rtspc->rtp_bufsize = rtp_dataleft;
719
720 /* As far as the transfer is concerned, this data is consumed */
721 *nread = 0;
722 return CURLE_OK;
723 }
724 /* Fix up k->str to point just after the last RTP packet */
725 k->str += *nread - rtp_dataleft;
726
727 /* either all of the data has been read or...
728 * rtp now points at the next byte to parse
729 */
730 if(rtp_dataleft > 0)
731 DEBUGASSERT(k->str[0] == rtp[0]);
732
733 DEBUGASSERT(rtp_dataleft <= *nread); /* sanity check */
734
735 *nread = rtp_dataleft;
736
737 /* If we get here, we have finished with the leftover/merge buffer */
738 Curl_safefree(rtspc->rtp_buf);
739 rtspc->rtp_buf = NULL;
740 rtspc->rtp_bufsize = 0;
741
742 return CURLE_OK;
743}
744
745static
746CURLcode rtp_client_write(struct connectdata *conn, char *ptr, size_t len)
747{
748 struct Curl_easy *data = conn->data;
749 size_t wrote;
750 curl_write_callback writeit;
751 void *user_ptr;
752
753 if(len == 0) {
754 failf(data, "Cannot write a 0 size RTP packet.");
755 return CURLE_WRITE_ERROR;
756 }
757
758 /* If the user has configured CURLOPT_INTERLEAVEFUNCTION then use that
759 function and any configured CURLOPT_INTERLEAVEDATA to write out the RTP
760 data. Otherwise, use the CURLOPT_WRITEFUNCTION with the CURLOPT_WRITEDATA
761 pointer to write out the RTP data. */
762 if(data->set.fwrite_rtp) {
763 writeit = data->set.fwrite_rtp;
764 user_ptr = data->set.rtp_out;
765 }
766 else {
767 writeit = data->set.fwrite_func;
768 user_ptr = data->set.out;
769 }
770
771 Curl_set_in_callback(data, true);
772 wrote = writeit(ptr, 1, len, user_ptr);
773 Curl_set_in_callback(data, false);
774
775 if(CURL_WRITEFUNC_PAUSE == wrote) {
776 failf(data, "Cannot pause RTP");
777 return CURLE_WRITE_ERROR;
778 }
779
780 if(wrote != len) {
781 failf(data, "Failed writing RTP data");
782 return CURLE_WRITE_ERROR;
783 }
784
785 return CURLE_OK;
786}
787
788CURLcode Curl_rtsp_parseheader(struct connectdata *conn,
789 char *header)
790{
791 struct Curl_easy *data = conn->data;
792 long CSeq = 0;
793
794 if(checkprefix("CSeq:", header)) {
795 /* Store the received CSeq. Match is verified in rtsp_done */
796 int nc = sscanf(&header[4], ": %ld", &CSeq);
797 if(nc == 1) {
798 struct RTSP *rtsp = data->req.protop;
799 rtsp->CSeq_recv = CSeq; /* mark the request */
800 data->state.rtsp_CSeq_recv = CSeq; /* update the handle */
801 }
802 else {
803 failf(data, "Unable to read the CSeq header: [%s]", header);
804 return CURLE_RTSP_CSEQ_ERROR;
805 }
806 }
807 else if(checkprefix("Session:", header)) {
808 char *start;
809
810 /* Find the first non-space letter */
811 start = header + 8;
812 while(*start && ISSPACE(*start))
813 start++;
814
815 if(!*start) {
816 failf(data, "Got a blank Session ID");
817 }
818 else if(data->set.str[STRING_RTSP_SESSION_ID]) {
819 /* If the Session ID is set, then compare */
820 if(strncmp(start, data->set.str[STRING_RTSP_SESSION_ID],
821 strlen(data->set.str[STRING_RTSP_SESSION_ID])) != 0) {
822 failf(data, "Got RTSP Session ID Line [%s], but wanted ID [%s]",
823 start, data->set.str[STRING_RTSP_SESSION_ID]);
824 return CURLE_RTSP_SESSION_ERROR;
825 }
826 }
827 else {
828 /* If the Session ID is not set, and we find it in a response, then set
829 * it.
830 *
831 * Allow any non whitespace content, up to the field separator or end of
832 * line. RFC 2326 isn't 100% clear on the session ID and for example
833 * gstreamer does url-encoded session ID's not covered by the standard.
834 */
835 char *end = start;
836 while(*end && *end != ';' && !ISSPACE(*end))
837 end++;
838
839 /* Copy the id substring into a new buffer */
840 data->set.str[STRING_RTSP_SESSION_ID] = malloc(end - start + 1);
841 if(data->set.str[STRING_RTSP_SESSION_ID] == NULL)
842 return CURLE_OUT_OF_MEMORY;
843 memcpy(data->set.str[STRING_RTSP_SESSION_ID], start, end - start);
844 (data->set.str[STRING_RTSP_SESSION_ID])[end - start] = '\0';
845 }
846 }
847 return CURLE_OK;
848}
849
850#endif /* CURL_DISABLE_RTSP */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette