You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

893 lines
25KB

  1. /*
  2. * RTSP client
  3. * Copyright (c) 2002 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #include <sys/time.h>
  21. #include <netinet/in.h>
  22. #include <sys/socket.h>
  23. #include <arpa/inet.h>
  24. //#define DEBUG
  25. typedef struct RTSPState {
  26. URLContext *rtsp_hd; /* RTSP TCP connexion handle */
  27. ByteIOContext rtsp_gb;
  28. int seq; /* RTSP command sequence number */
  29. char session_id[512];
  30. enum RTSPProtocol protocol;
  31. char last_reply[2048]; /* XXX: allocate ? */
  32. } RTSPState;
  33. typedef struct RTSPStream {
  34. AVFormatContext *ic;
  35. int interleaved_min, interleaved_max; /* interleave ids, if TCP transport */
  36. char control_url[1024]; /* url for this stream */
  37. } RTSPStream;
  38. /* suppress this hack */
  39. int rtsp_abort_req = 0;
  40. /* XXX: currently, the only way to change the protocols consists in
  41. changing this variable */
  42. int rtsp_default_protocols = (1 << RTSP_PROTOCOL_RTP_TCP) | (1 << RTSP_PROTOCOL_RTP_UDP) | (1 << RTSP_PROTOCOL_RTP_UDP_MULTICAST);
  43. /* if non zero, then set a range for RTP ports */
  44. int rtsp_rtp_port_min = 0;
  45. int rtsp_rtp_port_max = 0;
  46. FFRTSPCallback *ff_rtsp_callback = NULL;
  47. static int rtsp_probe(AVProbeData *p)
  48. {
  49. if (strstart(p->filename, "rtsp:", NULL))
  50. return AVPROBE_SCORE_MAX;
  51. return 0;
  52. }
  53. static int redir_isspace(int c)
  54. {
  55. return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
  56. }
  57. static void skip_spaces(const char **pp)
  58. {
  59. const char *p;
  60. p = *pp;
  61. while (redir_isspace(*p))
  62. p++;
  63. *pp = p;
  64. }
  65. static void get_word_sep(char *buf, int buf_size, const char *sep,
  66. const char **pp)
  67. {
  68. const char *p;
  69. char *q;
  70. p = *pp;
  71. skip_spaces(&p);
  72. q = buf;
  73. while (!strchr(sep, *p) && *p != '\0') {
  74. if ((q - buf) < buf_size - 1)
  75. *q++ = *p;
  76. p++;
  77. }
  78. if (buf_size > 0)
  79. *q = '\0';
  80. *pp = p;
  81. }
  82. static void get_word(char *buf, int buf_size, const char **pp)
  83. {
  84. const char *p;
  85. char *q;
  86. p = *pp;
  87. skip_spaces(&p);
  88. q = buf;
  89. while (!redir_isspace(*p) && *p != '\0') {
  90. if ((q - buf) < buf_size - 1)
  91. *q++ = *p;
  92. p++;
  93. }
  94. if (buf_size > 0)
  95. *q = '\0';
  96. *pp = p;
  97. }
  98. static void sdp_parse_line(AVFormatContext *s,
  99. int letter, const char *buf)
  100. {
  101. char buf1[64], st_type[64];
  102. const char *p;
  103. int codec_type, payload_type;
  104. AVStream *st;
  105. RTSPStream *rtsp_st;
  106. #ifdef DEBUG
  107. printf("sdp: %c='%s'\n", letter, buf);
  108. #endif
  109. p = buf;
  110. switch(letter) {
  111. case 's':
  112. pstrcpy(s->title, sizeof(s->title), p);
  113. break;
  114. case 'i':
  115. if (s->nb_streams == 0) {
  116. pstrcpy(s->comment, sizeof(s->comment), p);
  117. break;
  118. }
  119. break;
  120. case 'm':
  121. /* new stream */
  122. get_word(st_type, sizeof(st_type), &p);
  123. if (!strcmp(st_type, "audio")) {
  124. codec_type = CODEC_TYPE_AUDIO;
  125. } else if (!strcmp(st_type, "video")) {
  126. codec_type = CODEC_TYPE_VIDEO;
  127. } else {
  128. return;
  129. }
  130. get_word(buf1, sizeof(buf1), &p); /* port */
  131. get_word(buf1, sizeof(buf1), &p); /* protocol */
  132. /* XXX: handle list of formats */
  133. get_word(buf1, sizeof(buf1), &p); /* format list */
  134. payload_type = atoi(buf1);
  135. rtsp_st = av_mallocz(sizeof(RTSPStream));
  136. if (!rtsp_st)
  137. return;
  138. st = av_new_stream(s, s->nb_streams);
  139. if (!st)
  140. return;
  141. st->priv_data = rtsp_st;
  142. /* put a default control url */
  143. pstrcpy(rtsp_st->control_url, sizeof(rtsp_st->control_url), s->filename);
  144. st->codec.codec_type = codec_type;
  145. rtp_get_codec_info(&st->codec, payload_type);
  146. break;
  147. case 'a':
  148. if (strstart(p, "control:", &p) && s->nb_streams > 0) {
  149. char proto[32];
  150. /* get the control url */
  151. st = s->streams[s->nb_streams - 1];
  152. rtsp_st = st->priv_data;
  153. /* XXX: may need to add full url resolution */
  154. url_split(proto, sizeof(proto), NULL, 0, NULL, NULL, 0, p);
  155. if (proto[0] == '\0') {
  156. /* relative control URL */
  157. pstrcat(rtsp_st->control_url, sizeof(rtsp_st->control_url), "/");
  158. pstrcat(rtsp_st->control_url, sizeof(rtsp_st->control_url), p);
  159. } else {
  160. pstrcpy(rtsp_st->control_url, sizeof(rtsp_st->control_url), p);
  161. }
  162. }
  163. break;
  164. }
  165. }
  166. int sdp_parse(AVFormatContext *s, const char *content)
  167. {
  168. const char *p;
  169. int letter;
  170. char buf[1024], *q;
  171. p = content;
  172. for(;;) {
  173. skip_spaces(&p);
  174. letter = *p;
  175. if (letter == '\0')
  176. break;
  177. p++;
  178. if (*p != '=')
  179. goto next_line;
  180. p++;
  181. /* get the content */
  182. q = buf;
  183. while (*p != '\n' && *p != '\0') {
  184. if ((q - buf) < sizeof(buf) - 1)
  185. *q++ = *p;
  186. p++;
  187. }
  188. *q = '\0';
  189. sdp_parse_line(s, letter, buf);
  190. next_line:
  191. while (*p != '\n' && *p != '\0')
  192. p++;
  193. if (*p == '\n')
  194. p++;
  195. }
  196. return 0;
  197. }
  198. static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
  199. {
  200. const char *p;
  201. int v;
  202. p = *pp;
  203. skip_spaces(&p);
  204. v = strtol(p, (char **)&p, 10);
  205. if (*p == '-') {
  206. p++;
  207. *min_ptr = v;
  208. v = strtol(p, (char **)&p, 10);
  209. *max_ptr = v;
  210. } else {
  211. *min_ptr = v;
  212. *max_ptr = v;
  213. }
  214. *pp = p;
  215. }
  216. /* XXX: only one transport specification is parsed */
  217. static void rtsp_parse_transport(RTSPHeader *reply, const char *p)
  218. {
  219. char transport_protocol[16];
  220. char profile[16];
  221. char lower_transport[16];
  222. char parameter[16];
  223. RTSPTransportField *th;
  224. char buf[256];
  225. reply->nb_transports = 0;
  226. for(;;) {
  227. skip_spaces(&p);
  228. if (*p == '\0')
  229. break;
  230. th = &reply->transports[reply->nb_transports];
  231. get_word_sep(transport_protocol, sizeof(transport_protocol),
  232. "/", &p);
  233. if (*p == '/')
  234. p++;
  235. get_word_sep(profile, sizeof(profile), "/;,", &p);
  236. lower_transport[0] = '\0';
  237. if (*p == '/') {
  238. get_word_sep(lower_transport, sizeof(lower_transport),
  239. ";,", &p);
  240. }
  241. if (!strcmp(lower_transport, "TCP"))
  242. th->protocol = RTSP_PROTOCOL_RTP_TCP;
  243. else
  244. th->protocol = RTSP_PROTOCOL_RTP_UDP;
  245. if (*p == ';')
  246. p++;
  247. /* get each parameter */
  248. while (*p != '\0' && *p != ',') {
  249. get_word_sep(parameter, sizeof(parameter), "=;,", &p);
  250. if (!strcmp(parameter, "port")) {
  251. if (*p == '=') {
  252. p++;
  253. rtsp_parse_range(&th->port_min, &th->port_max, &p);
  254. }
  255. } else if (!strcmp(parameter, "client_port")) {
  256. if (*p == '=') {
  257. p++;
  258. rtsp_parse_range(&th->client_port_min,
  259. &th->client_port_max, &p);
  260. }
  261. } else if (!strcmp(parameter, "server_port")) {
  262. if (*p == '=') {
  263. p++;
  264. rtsp_parse_range(&th->server_port_min,
  265. &th->server_port_max, &p);
  266. }
  267. } else if (!strcmp(parameter, "interleaved")) {
  268. if (*p == '=') {
  269. p++;
  270. rtsp_parse_range(&th->interleaved_min,
  271. &th->interleaved_max, &p);
  272. }
  273. } else if (!strcmp(parameter, "multicast")) {
  274. if (th->protocol == RTSP_PROTOCOL_RTP_UDP)
  275. th->protocol = RTSP_PROTOCOL_RTP_UDP_MULTICAST;
  276. } else if (!strcmp(parameter, "ttl")) {
  277. if (*p == '=') {
  278. p++;
  279. th->ttl = strtol(p, (char **)&p, 10);
  280. }
  281. } else if (!strcmp(parameter, "destination")) {
  282. struct in_addr ipaddr;
  283. if (*p == '=') {
  284. p++;
  285. get_word_sep(buf, sizeof(buf), ";,", &p);
  286. if (inet_aton(buf, &ipaddr))
  287. th->destination = ntohl(ipaddr.s_addr);
  288. }
  289. }
  290. while (*p != ';' && *p != '\0' && *p != ',')
  291. p++;
  292. if (*p == ';')
  293. p++;
  294. }
  295. if (*p == ',')
  296. p++;
  297. reply->nb_transports++;
  298. }
  299. }
  300. void rtsp_parse_line(RTSPHeader *reply, const char *buf)
  301. {
  302. const char *p;
  303. /* NOTE: we do case independent match for broken servers */
  304. p = buf;
  305. if (stristart(p, "Session:", &p)) {
  306. get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
  307. } else if (stristart(p, "Content-Length:", &p)) {
  308. reply->content_length = strtol(p, NULL, 10);
  309. } else if (stristart(p, "Transport:", &p)) {
  310. rtsp_parse_transport(reply, p);
  311. } else if (stristart(p, "CSeq:", &p)) {
  312. reply->seq = strtol(p, NULL, 10);
  313. }
  314. }
  315. static void rtsp_send_cmd(AVFormatContext *s,
  316. const char *cmd, RTSPHeader *reply,
  317. unsigned char **content_ptr)
  318. {
  319. RTSPState *rt = s->priv_data;
  320. char buf[4096], buf1[1024], *q;
  321. unsigned char ch;
  322. const char *p;
  323. int content_length, line_count;
  324. unsigned char *content = NULL;
  325. memset(reply, 0, sizeof(RTSPHeader));
  326. rt->seq++;
  327. pstrcpy(buf, sizeof(buf), cmd);
  328. snprintf(buf1, sizeof(buf1), "CSeq: %d\n", rt->seq);
  329. pstrcat(buf, sizeof(buf), buf1);
  330. if (rt->session_id[0] != '\0' && !strstr(cmd, "\nIf-Match:")) {
  331. snprintf(buf1, sizeof(buf1), "Session: %s\n", rt->session_id);
  332. pstrcat(buf, sizeof(buf), buf1);
  333. }
  334. pstrcat(buf, sizeof(buf), "\n");
  335. #ifdef DEBUG
  336. printf("Sending:\n%s--\n", buf);
  337. #endif
  338. url_write(rt->rtsp_hd, buf, strlen(buf));
  339. /* parse reply (XXX: use buffers) */
  340. line_count = 0;
  341. rt->last_reply[0] = '\0';
  342. for(;;) {
  343. q = buf;
  344. for(;;) {
  345. if (url_read(rt->rtsp_hd, &ch, 1) == 0)
  346. break;
  347. if (ch == '\n')
  348. break;
  349. if (ch != '\r') {
  350. if ((q - buf) < sizeof(buf) - 1)
  351. *q++ = ch;
  352. }
  353. }
  354. *q = '\0';
  355. #ifdef DEBUG
  356. printf("line='%s'\n", buf);
  357. #endif
  358. /* test if last line */
  359. if (buf[0] == '\0')
  360. break;
  361. p = buf;
  362. if (line_count == 0) {
  363. /* get reply code */
  364. get_word(buf1, sizeof(buf1), &p);
  365. get_word(buf1, sizeof(buf1), &p);
  366. reply->status_code = atoi(buf1);
  367. } else {
  368. rtsp_parse_line(reply, p);
  369. pstrcat(rt->last_reply, sizeof(rt->last_reply), p);
  370. pstrcat(rt->last_reply, sizeof(rt->last_reply), "\n");
  371. }
  372. line_count++;
  373. }
  374. if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0')
  375. pstrcpy(rt->session_id, sizeof(rt->session_id), reply->session_id);
  376. content_length = reply->content_length;
  377. if (content_length > 0) {
  378. /* leave some room for a trailing '\0' (useful for simple parsing) */
  379. content = av_malloc(content_length + 1);
  380. url_read(rt->rtsp_hd, content, content_length);
  381. content[content_length] = '\0';
  382. }
  383. if (content_ptr)
  384. *content_ptr = content;
  385. }
  386. /* useful for modules: set RTSP callback function */
  387. void rtsp_set_callback(FFRTSPCallback *rtsp_cb)
  388. {
  389. ff_rtsp_callback = rtsp_cb;
  390. }
  391. static int rtsp_read_header(AVFormatContext *s,
  392. AVFormatParameters *ap)
  393. {
  394. RTSPState *rt = s->priv_data;
  395. char host[1024], path[1024], tcpname[1024], cmd[2048];
  396. URLContext *rtsp_hd;
  397. int port, i, ret, err;
  398. RTSPHeader reply1, *reply = &reply1;
  399. unsigned char *content = NULL;
  400. AVStream *st;
  401. RTSPStream *rtsp_st;
  402. int protocol_mask;
  403. rtsp_abort_req = 0;
  404. /* extract hostname and port */
  405. url_split(NULL, 0,
  406. host, sizeof(host), &port, path, sizeof(path), s->filename);
  407. if (port < 0)
  408. port = RTSP_DEFAULT_PORT;
  409. /* open the tcp connexion */
  410. snprintf(tcpname, sizeof(tcpname), "tcp://%s:%d", host, port);
  411. if (url_open(&rtsp_hd, tcpname, URL_RDWR) < 0)
  412. return AVERROR_IO;
  413. rt->rtsp_hd = rtsp_hd;
  414. rt->seq = 0;
  415. /* describe the stream */
  416. snprintf(cmd, sizeof(cmd),
  417. "DESCRIBE %s RTSP/1.0\n"
  418. "Accept: application/sdp\n",
  419. s->filename);
  420. rtsp_send_cmd(s, cmd, reply, &content);
  421. if (!content) {
  422. err = AVERROR_INVALIDDATA;
  423. goto fail;
  424. }
  425. if (reply->status_code != RTSP_STATUS_OK) {
  426. err = AVERROR_INVALIDDATA;
  427. goto fail;
  428. }
  429. /* now we got the SDP description, we parse it */
  430. ret = sdp_parse(s, (const char *)content);
  431. av_freep(&content);
  432. if (ret < 0) {
  433. err = AVERROR_INVALIDDATA;
  434. goto fail;
  435. }
  436. protocol_mask = rtsp_default_protocols;
  437. /* for each stream, make the setup request */
  438. /* XXX: we assume the same server is used for the control of each
  439. RTSP stream */
  440. for(i=0;i<s->nb_streams;i++) {
  441. char transport[2048];
  442. AVInputFormat *fmt;
  443. st = s->streams[i];
  444. rtsp_st = st->priv_data;
  445. /* compute available transports */
  446. transport[0] = '\0';
  447. /* RTP/UDP */
  448. if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP)) {
  449. char buf[256];
  450. int j;
  451. /* first try in specified port range */
  452. if (rtsp_rtp_port_min != 0) {
  453. for(j=rtsp_rtp_port_min;j<=rtsp_rtp_port_max;j++) {
  454. snprintf(buf, sizeof(buf), "rtp://?localport=%d", j);
  455. if (!av_open_input_file(&rtsp_st->ic, buf,
  456. &rtp_demux, 0, NULL))
  457. goto rtp_opened;
  458. }
  459. }
  460. /* then try on any port */
  461. if (av_open_input_file(&rtsp_st->ic, "rtp://",
  462. &rtp_demux, 0, NULL) < 0) {
  463. err = AVERROR_INVALIDDATA;
  464. goto fail;
  465. }
  466. rtp_opened:
  467. port = rtp_get_local_port(url_fileno(&rtsp_st->ic->pb));
  468. if (transport[0] != '\0')
  469. pstrcat(transport, sizeof(transport), ",");
  470. snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
  471. "RTP/AVP/UDP;unicast;client_port=%d-%d",
  472. port, port + 1);
  473. }
  474. /* RTP/TCP */
  475. if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_TCP)) {
  476. if (transport[0] != '\0')
  477. pstrcat(transport, sizeof(transport), ",");
  478. snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
  479. "RTP/AVP/TCP");
  480. }
  481. if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP_MULTICAST)) {
  482. if (transport[0] != '\0')
  483. pstrcat(transport, sizeof(transport), ",");
  484. snprintf(transport + strlen(transport),
  485. sizeof(transport) - strlen(transport) - 1,
  486. "RTP/AVP/UDP;multicast");
  487. }
  488. snprintf(cmd, sizeof(cmd),
  489. "SETUP %s RTSP/1.0\n"
  490. "Transport: %s\n",
  491. rtsp_st->control_url, transport);
  492. rtsp_send_cmd(s, cmd, reply, NULL);
  493. if (reply->status_code != RTSP_STATUS_OK ||
  494. reply->nb_transports != 1) {
  495. err = AVERROR_INVALIDDATA;
  496. goto fail;
  497. }
  498. /* XXX: same protocol for all streams is required */
  499. if (i > 0) {
  500. if (reply->transports[0].protocol != rt->protocol) {
  501. err = AVERROR_INVALIDDATA;
  502. goto fail;
  503. }
  504. } else {
  505. rt->protocol = reply->transports[0].protocol;
  506. }
  507. /* close RTP connection if not choosen */
  508. if (reply->transports[0].protocol != RTSP_PROTOCOL_RTP_UDP &&
  509. (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP))) {
  510. av_close_input_file(rtsp_st->ic);
  511. rtsp_st->ic = NULL;
  512. }
  513. switch(reply->transports[0].protocol) {
  514. case RTSP_PROTOCOL_RTP_TCP:
  515. fmt = &rtp_demux;
  516. if (av_open_input_file(&rtsp_st->ic, "null", fmt, 0, NULL) < 0) {
  517. err = AVERROR_INVALIDDATA;
  518. goto fail;
  519. }
  520. rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
  521. rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
  522. break;
  523. case RTSP_PROTOCOL_RTP_UDP:
  524. {
  525. char url[1024];
  526. /* XXX: also use address if specified */
  527. snprintf(url, sizeof(url), "rtp://%s:%d",
  528. host, reply->transports[0].server_port_min);
  529. if (rtp_set_remote_url(url_fileno(&rtsp_st->ic->pb), url) < 0) {
  530. err = AVERROR_INVALIDDATA;
  531. goto fail;
  532. }
  533. }
  534. break;
  535. case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
  536. {
  537. char url[1024];
  538. int ttl;
  539. fmt = &rtp_demux;
  540. ttl = reply->transports[0].ttl;
  541. if (!ttl)
  542. ttl = 16;
  543. snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
  544. host,
  545. reply->transports[0].server_port_min,
  546. ttl);
  547. if (av_open_input_file(&rtsp_st->ic, url, fmt, 0, NULL) < 0) {
  548. err = AVERROR_INVALIDDATA;
  549. goto fail;
  550. }
  551. }
  552. break;
  553. }
  554. }
  555. /* use callback if available to extend setup */
  556. if (ff_rtsp_callback) {
  557. if (ff_rtsp_callback(RTSP_ACTION_CLIENT_SETUP, rt->session_id,
  558. NULL, 0, rt->last_reply) < 0) {
  559. err = AVERROR_INVALIDDATA;
  560. goto fail;
  561. }
  562. }
  563. /* start playing */
  564. snprintf(cmd, sizeof(cmd),
  565. "PLAY %s RTSP/1.0\n",
  566. s->filename);
  567. rtsp_send_cmd(s, cmd, reply, NULL);
  568. if (reply->status_code != RTSP_STATUS_OK) {
  569. err = AVERROR_INVALIDDATA;
  570. goto fail;
  571. }
  572. /* open TCP with bufferized input */
  573. if (rt->protocol == RTSP_PROTOCOL_RTP_TCP) {
  574. if (url_fdopen(&rt->rtsp_gb, rt->rtsp_hd) < 0) {
  575. err = AVERROR_NOMEM;
  576. goto fail;
  577. }
  578. }
  579. return 0;
  580. fail:
  581. for(i=0;i<s->nb_streams;i++) {
  582. st = s->streams[i];
  583. rtsp_st = st->priv_data;
  584. if (rtsp_st) {
  585. if (rtsp_st->ic)
  586. av_close_input_file(rtsp_st->ic);
  587. }
  588. av_free(rtsp_st);
  589. }
  590. av_freep(&content);
  591. url_close(rt->rtsp_hd);
  592. return err;
  593. }
  594. static int tcp_read_packet(AVFormatContext *s,
  595. AVPacket *pkt)
  596. {
  597. RTSPState *rt = s->priv_data;
  598. ByteIOContext *rtsp_gb = &rt->rtsp_gb;
  599. int c, id, len, i, ret;
  600. AVStream *st;
  601. RTSPStream *rtsp_st;
  602. char buf[RTP_MAX_PACKET_LENGTH];
  603. redo:
  604. for(;;) {
  605. c = url_fgetc(rtsp_gb);
  606. if (c == URL_EOF)
  607. return AVERROR_IO;
  608. if (c == '$')
  609. break;
  610. }
  611. id = get_byte(rtsp_gb);
  612. len = get_be16(rtsp_gb);
  613. if (len > RTP_MAX_PACKET_LENGTH || len < 12)
  614. goto redo;
  615. /* get the data */
  616. get_buffer(rtsp_gb, buf, len);
  617. /* find the matching stream */
  618. for(i = 0; i < s->nb_streams; i++) {
  619. st = s->streams[i];
  620. rtsp_st = st->priv_data;
  621. if (i >= rtsp_st->interleaved_min &&
  622. i <= rtsp_st->interleaved_max)
  623. goto found;
  624. }
  625. goto redo;
  626. found:
  627. ret = rtp_parse_packet(rtsp_st->ic, pkt, buf, len);
  628. if (ret < 0)
  629. goto redo;
  630. pkt->stream_index = i;
  631. return ret;
  632. }
  633. /* NOTE: output one packet at a time. May need to add a small fifo */
  634. static int udp_read_packet(AVFormatContext *s,
  635. AVPacket *pkt)
  636. {
  637. AVFormatContext *ic;
  638. AVStream *st;
  639. RTSPStream *rtsp_st;
  640. fd_set rfds;
  641. int fd1, fd2, fd_max, n, i, ret;
  642. char buf[RTP_MAX_PACKET_LENGTH];
  643. struct timeval tv;
  644. for(;;) {
  645. if (rtsp_abort_req)
  646. return -EIO;
  647. FD_ZERO(&rfds);
  648. fd_max = -1;
  649. for(i = 0; i < s->nb_streams; i++) {
  650. st = s->streams[i];
  651. rtsp_st = st->priv_data;
  652. ic = rtsp_st->ic;
  653. /* currently, we cannot probe RTCP handle because of blocking restrictions */
  654. rtp_get_file_handles(url_fileno(&ic->pb), &fd1, &fd2);
  655. if (fd1 > fd_max)
  656. fd_max = fd1;
  657. FD_SET(fd1, &rfds);
  658. }
  659. /* XXX: also add proper API to abort */
  660. tv.tv_sec = 0;
  661. tv.tv_usec = 500000;
  662. n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
  663. if (n > 0) {
  664. for(i = 0; i < s->nb_streams; i++) {
  665. st = s->streams[i];
  666. rtsp_st = st->priv_data;
  667. ic = rtsp_st->ic;
  668. rtp_get_file_handles(url_fileno(&ic->pb), &fd1, &fd2);
  669. if (FD_ISSET(fd1, &rfds)) {
  670. ret = url_read(url_fileno(&ic->pb), buf, sizeof(buf));
  671. if (ret >= 0 &&
  672. rtp_parse_packet(ic, pkt, buf, ret) == 0) {
  673. pkt->stream_index = i;
  674. return ret;
  675. }
  676. }
  677. }
  678. }
  679. }
  680. }
  681. static int rtsp_read_packet(AVFormatContext *s,
  682. AVPacket *pkt)
  683. {
  684. RTSPState *rt = s->priv_data;
  685. int ret;
  686. switch(rt->protocol) {
  687. default:
  688. case RTSP_PROTOCOL_RTP_TCP:
  689. ret = tcp_read_packet(s, pkt);
  690. break;
  691. case RTSP_PROTOCOL_RTP_UDP:
  692. ret = udp_read_packet(s, pkt);
  693. break;
  694. }
  695. return ret;
  696. }
  697. static int rtsp_read_close(AVFormatContext *s)
  698. {
  699. RTSPState *rt = s->priv_data;
  700. AVStream *st;
  701. RTSPStream *rtsp_st;
  702. RTSPHeader reply1, *reply = &reply1;
  703. int i;
  704. char cmd[1024];
  705. /* NOTE: it is valid to flush the buffer here */
  706. if (rt->protocol == RTSP_PROTOCOL_RTP_TCP) {
  707. url_fclose(&rt->rtsp_gb);
  708. }
  709. snprintf(cmd, sizeof(cmd),
  710. "TEARDOWN %s RTSP/1.0\n",
  711. s->filename);
  712. rtsp_send_cmd(s, cmd, reply, NULL);
  713. if (ff_rtsp_callback) {
  714. ff_rtsp_callback(RTSP_ACTION_CLIENT_TEARDOWN, rt->session_id,
  715. NULL, 0, NULL);
  716. }
  717. for(i=0;i<s->nb_streams;i++) {
  718. st = s->streams[i];
  719. rtsp_st = st->priv_data;
  720. if (rtsp_st) {
  721. if (rtsp_st->ic)
  722. av_close_input_file(rtsp_st->ic);
  723. }
  724. av_free(rtsp_st);
  725. }
  726. url_close(rt->rtsp_hd);
  727. return 0;
  728. }
  729. static AVInputFormat rtsp_demux = {
  730. "rtsp",
  731. "RTSP input format",
  732. sizeof(RTSPState),
  733. rtsp_probe,
  734. rtsp_read_header,
  735. rtsp_read_packet,
  736. rtsp_read_close,
  737. flags: AVFMT_NOFILE,
  738. };
  739. /* dummy redirector format (used directly in av_open_input_file now) */
  740. static int redir_probe(AVProbeData *pd)
  741. {
  742. const char *p;
  743. p = pd->buf;
  744. while (redir_isspace(*p))
  745. p++;
  746. if (strstart(p, "http://", NULL) ||
  747. strstart(p, "rtsp://", NULL))
  748. return AVPROBE_SCORE_MAX;
  749. return 0;
  750. }
  751. /* called from utils.c */
  752. int redir_open(AVFormatContext **ic_ptr, ByteIOContext *f)
  753. {
  754. char buf[4096], *q;
  755. int c;
  756. AVFormatContext *ic = NULL;
  757. /* parse each URL and try to open it */
  758. c = url_fgetc(f);
  759. while (c != URL_EOF) {
  760. /* skip spaces */
  761. for(;;) {
  762. if (!redir_isspace(c))
  763. break;
  764. c = url_fgetc(f);
  765. }
  766. if (c == URL_EOF)
  767. break;
  768. /* record url */
  769. q = buf;
  770. for(;;) {
  771. if (c == URL_EOF || redir_isspace(c))
  772. break;
  773. if ((q - buf) < sizeof(buf) - 1)
  774. *q++ = c;
  775. c = url_fgetc(f);
  776. }
  777. *q = '\0';
  778. //printf("URL='%s'\n", buf);
  779. /* try to open the media file */
  780. if (av_open_input_file(&ic, buf, NULL, 0, NULL) == 0)
  781. break;
  782. }
  783. *ic_ptr = ic;
  784. if (!ic)
  785. return AVERROR_IO;
  786. else
  787. return 0;
  788. }
  789. AVInputFormat redir_demux = {
  790. "redir",
  791. "Redirector format",
  792. 0,
  793. redir_probe,
  794. NULL,
  795. NULL,
  796. NULL,
  797. };
  798. int rtsp_init(void)
  799. {
  800. av_register_input_format(&rtsp_demux);
  801. av_register_input_format(&redir_demux);
  802. return 0;
  803. }