rtsp.c 24 KB

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