VirtualBox

source: vbox/trunk/src/libs/curl-7.87.0/lib/rtsp.c@ 98334

Last change on this file since 98334 was 98326, checked in by vboxsync, 2 years ago

curl-7.87.0: Applied and adjusted our curl changes to 7.83.1. bugref:10356

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