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.

1164 lines
32KB

  1. /*
  2. * RTSP/SDP 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 <ctype.h>
  24. #ifndef __BEOS__
  25. # include <arpa/inet.h>
  26. #else
  27. # include "barpainet.h"
  28. #endif
  29. //#define DEBUG
  30. typedef struct RTSPState {
  31. URLContext *rtsp_hd; /* RTSP TCP connexion handle */
  32. ByteIOContext rtsp_gb;
  33. int seq; /* RTSP command sequence number */
  34. char session_id[512];
  35. enum RTSPProtocol protocol;
  36. char last_reply[2048]; /* XXX: allocate ? */
  37. } RTSPState;
  38. typedef struct RTSPStream {
  39. AVFormatContext *ic;
  40. int interleaved_min, interleaved_max; /* interleave ids, if TCP transport */
  41. char control_url[1024]; /* url for this stream (from SDP) */
  42. int sdp_port; /* port (from SDP content - not used in RTSP) */
  43. struct in_addr sdp_ip; /* IP address (from SDP content - not used in RTSP) */
  44. int sdp_ttl; /* IP TTL (from SDP content - not used in RTSP) */
  45. int sdp_payload_type; /* payload type - only used in SDP */
  46. } RTSPStream;
  47. /* suppress this hack */
  48. int rtsp_abort_req = 0;
  49. /* XXX: currently, the only way to change the protocols consists in
  50. changing this variable */
  51. int rtsp_default_protocols = (1 << RTSP_PROTOCOL_RTP_TCP) | (1 << RTSP_PROTOCOL_RTP_UDP) | (1 << RTSP_PROTOCOL_RTP_UDP_MULTICAST);
  52. /* if non zero, then set a range for RTP ports */
  53. int rtsp_rtp_port_min = 0;
  54. int rtsp_rtp_port_max = 0;
  55. FFRTSPCallback *ff_rtsp_callback = NULL;
  56. static int rtsp_probe(AVProbeData *p)
  57. {
  58. if (strstart(p->filename, "rtsp:", NULL))
  59. return AVPROBE_SCORE_MAX;
  60. return 0;
  61. }
  62. static int redir_isspace(int c)
  63. {
  64. return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
  65. }
  66. static void skip_spaces(const char **pp)
  67. {
  68. const char *p;
  69. p = *pp;
  70. while (redir_isspace(*p))
  71. p++;
  72. *pp = p;
  73. }
  74. static void get_word_sep(char *buf, int buf_size, const char *sep,
  75. const char **pp)
  76. {
  77. const char *p;
  78. char *q;
  79. p = *pp;
  80. skip_spaces(&p);
  81. q = buf;
  82. while (!strchr(sep, *p) && *p != '\0') {
  83. if ((q - buf) < buf_size - 1)
  84. *q++ = *p;
  85. p++;
  86. }
  87. if (buf_size > 0)
  88. *q = '\0';
  89. *pp = p;
  90. }
  91. static void get_word(char *buf, int buf_size, const char **pp)
  92. {
  93. const char *p;
  94. char *q;
  95. p = *pp;
  96. skip_spaces(&p);
  97. q = buf;
  98. while (!redir_isspace(*p) && *p != '\0') {
  99. if ((q - buf) < buf_size - 1)
  100. *q++ = *p;
  101. p++;
  102. }
  103. if (buf_size > 0)
  104. *q = '\0';
  105. *pp = p;
  106. }
  107. /* parse the rtpmap description: <codec_name>/<clock_rate>[/<other
  108. params>] */
  109. static int sdp_parse_rtpmap(AVCodecContext *codec, const char *p)
  110. {
  111. char buf[256];
  112. /* codec name */
  113. get_word_sep(buf, sizeof(buf), "/", &p);
  114. if (!strcmp(buf, "MP4V-ES")) {
  115. codec->codec_id = CODEC_ID_MPEG4;
  116. return 0;
  117. } else {
  118. return -1;
  119. }
  120. }
  121. /* return the length and optionnaly the data */
  122. static int hex_to_data(uint8_t *data, const char *p)
  123. {
  124. int c, len, v;
  125. len = 0;
  126. v = 1;
  127. for(;;) {
  128. skip_spaces(&p);
  129. if (p == '\0')
  130. break;
  131. c = toupper((unsigned char)*p++);
  132. if (c >= '0' && c <= '9')
  133. c = c - '0';
  134. else if (c >= 'A' && c <= 'F')
  135. c = c - 'A' + 10;
  136. else
  137. break;
  138. v = (v << 4) | c;
  139. if (v & 0x100) {
  140. if (data)
  141. data[len] = v;
  142. len++;
  143. v = 1;
  144. }
  145. }
  146. return len;
  147. }
  148. static void sdp_parse_fmtp(AVCodecContext *codec, const char *p)
  149. {
  150. char attr[256];
  151. char value[4096];
  152. int len;
  153. /* loop on each attribute */
  154. for(;;) {
  155. skip_spaces(&p);
  156. if (*p == '\0')
  157. break;
  158. get_word_sep(attr, sizeof(attr), "=", &p);
  159. if (*p == '=')
  160. p++;
  161. get_word_sep(value, sizeof(value), ";", &p);
  162. if (*p == ';')
  163. p++;
  164. /* handle MPEG4 video */
  165. switch(codec->codec_id) {
  166. case CODEC_ID_MPEG4:
  167. if (!strcmp(attr, "config")) {
  168. /* decode the hexa encoded parameter */
  169. len = hex_to_data(NULL, value);
  170. codec->extradata = av_mallocz(len);
  171. if (!codec->extradata)
  172. goto fail;
  173. codec->extradata_size = len;
  174. hex_to_data(codec->extradata, value);
  175. }
  176. break;
  177. default:
  178. /* ignore data for other codecs */
  179. break;
  180. }
  181. fail: ;
  182. // printf("'%s' = '%s'\n", attr, value);
  183. }
  184. }
  185. typedef struct SDPParseState {
  186. /* SDP only */
  187. struct in_addr default_ip;
  188. int default_ttl;
  189. } SDPParseState;
  190. static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1,
  191. int letter, const char *buf)
  192. {
  193. char buf1[64], st_type[64];
  194. const char *p;
  195. int codec_type, payload_type, i;
  196. AVStream *st;
  197. RTSPStream *rtsp_st;
  198. struct in_addr sdp_ip;
  199. int ttl;
  200. #ifdef DEBUG
  201. printf("sdp: %c='%s'\n", letter, buf);
  202. #endif
  203. p = buf;
  204. switch(letter) {
  205. case 'c':
  206. get_word(buf1, sizeof(buf1), &p);
  207. if (strcmp(buf1, "IN") != 0)
  208. return;
  209. get_word(buf1, sizeof(buf1), &p);
  210. if (strcmp(buf1, "IP4") != 0)
  211. return;
  212. get_word_sep(buf1, sizeof(buf1), "/", &p);
  213. if (inet_aton(buf1, &sdp_ip) == 0)
  214. return;
  215. ttl = 16;
  216. if (*p == '/') {
  217. p++;
  218. get_word_sep(buf1, sizeof(buf1), "/", &p);
  219. ttl = atoi(buf1);
  220. }
  221. if (s->nb_streams == 0) {
  222. s1->default_ip = sdp_ip;
  223. s1->default_ttl = ttl;
  224. } else {
  225. st = s->streams[s->nb_streams - 1];
  226. rtsp_st = st->priv_data;
  227. rtsp_st->sdp_ip = sdp_ip;
  228. rtsp_st->sdp_ttl = ttl;
  229. }
  230. break;
  231. case 's':
  232. pstrcpy(s->title, sizeof(s->title), p);
  233. break;
  234. case 'i':
  235. if (s->nb_streams == 0) {
  236. pstrcpy(s->comment, sizeof(s->comment), p);
  237. break;
  238. }
  239. break;
  240. case 'm':
  241. /* new stream */
  242. get_word(st_type, sizeof(st_type), &p);
  243. if (!strcmp(st_type, "audio")) {
  244. codec_type = CODEC_TYPE_AUDIO;
  245. } else if (!strcmp(st_type, "video")) {
  246. codec_type = CODEC_TYPE_VIDEO;
  247. } else {
  248. return;
  249. }
  250. rtsp_st = av_mallocz(sizeof(RTSPStream));
  251. if (!rtsp_st)
  252. return;
  253. st = av_new_stream(s, s->nb_streams);
  254. if (!st)
  255. return;
  256. st->priv_data = rtsp_st;
  257. rtsp_st->sdp_ip = s1->default_ip;
  258. rtsp_st->sdp_ttl = s1->default_ttl;
  259. st->codec.codec_type = codec_type;
  260. get_word(buf1, sizeof(buf1), &p); /* port */
  261. rtsp_st->sdp_port = atoi(buf1);
  262. get_word(buf1, sizeof(buf1), &p); /* protocol (ignored) */
  263. /* XXX: handle list of formats */
  264. get_word(buf1, sizeof(buf1), &p); /* format list */
  265. rtsp_st->sdp_payload_type = atoi(buf1);
  266. if (rtsp_st->sdp_payload_type < 96) {
  267. /* if standard payload type, we can find the codec right now */
  268. rtp_get_codec_info(&st->codec, rtsp_st->sdp_payload_type);
  269. }
  270. /* put a default control url */
  271. pstrcpy(rtsp_st->control_url, sizeof(rtsp_st->control_url), s->filename);
  272. break;
  273. case 'a':
  274. if (strstart(p, "control:", &p) && s->nb_streams > 0) {
  275. char proto[32];
  276. /* get the control url */
  277. st = s->streams[s->nb_streams - 1];
  278. rtsp_st = st->priv_data;
  279. /* XXX: may need to add full url resolution */
  280. url_split(proto, sizeof(proto), NULL, 0, NULL, NULL, 0, p);
  281. if (proto[0] == '\0') {
  282. /* relative control URL */
  283. pstrcat(rtsp_st->control_url, sizeof(rtsp_st->control_url), "/");
  284. pstrcat(rtsp_st->control_url, sizeof(rtsp_st->control_url), p);
  285. } else {
  286. pstrcpy(rtsp_st->control_url, sizeof(rtsp_st->control_url), p);
  287. }
  288. } else if (strstart(p, "rtpmap:", &p)) {
  289. /* NOTE: rtpmap is only supported AFTER the 'm=' tag */
  290. get_word(buf1, sizeof(buf1), &p);
  291. payload_type = atoi(buf1);
  292. for(i = 0; i < s->nb_streams;i++) {
  293. st = s->streams[i];
  294. rtsp_st = st->priv_data;
  295. if (rtsp_st->sdp_payload_type == payload_type) {
  296. sdp_parse_rtpmap(&st->codec, p);
  297. }
  298. }
  299. } else if (strstart(p, "fmtp:", &p)) {
  300. /* NOTE: fmtp is only supported AFTER the 'a=rtpmap:xxx' tag */
  301. get_word(buf1, sizeof(buf1), &p);
  302. payload_type = atoi(buf1);
  303. for(i = 0; i < s->nb_streams;i++) {
  304. st = s->streams[i];
  305. rtsp_st = st->priv_data;
  306. if (rtsp_st->sdp_payload_type == payload_type) {
  307. sdp_parse_fmtp(&st->codec, p);
  308. }
  309. }
  310. }
  311. break;
  312. }
  313. }
  314. int sdp_parse(AVFormatContext *s, const char *content)
  315. {
  316. const char *p;
  317. int letter;
  318. char buf[1024], *q;
  319. SDPParseState sdp_parse_state, *s1 = &sdp_parse_state;
  320. memset(s1, 0, sizeof(SDPParseState));
  321. p = content;
  322. for(;;) {
  323. skip_spaces(&p);
  324. letter = *p;
  325. if (letter == '\0')
  326. break;
  327. p++;
  328. if (*p != '=')
  329. goto next_line;
  330. p++;
  331. /* get the content */
  332. q = buf;
  333. while (*p != '\n' && *p != '\0') {
  334. if ((q - buf) < sizeof(buf) - 1)
  335. *q++ = *p;
  336. p++;
  337. }
  338. *q = '\0';
  339. sdp_parse_line(s, s1, letter, buf);
  340. next_line:
  341. while (*p != '\n' && *p != '\0')
  342. p++;
  343. if (*p == '\n')
  344. p++;
  345. }
  346. return 0;
  347. }
  348. static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
  349. {
  350. const char *p;
  351. int v;
  352. p = *pp;
  353. skip_spaces(&p);
  354. v = strtol(p, (char **)&p, 10);
  355. if (*p == '-') {
  356. p++;
  357. *min_ptr = v;
  358. v = strtol(p, (char **)&p, 10);
  359. *max_ptr = v;
  360. } else {
  361. *min_ptr = v;
  362. *max_ptr = v;
  363. }
  364. *pp = p;
  365. }
  366. /* XXX: only one transport specification is parsed */
  367. static void rtsp_parse_transport(RTSPHeader *reply, const char *p)
  368. {
  369. char transport_protocol[16];
  370. char profile[16];
  371. char lower_transport[16];
  372. char parameter[16];
  373. RTSPTransportField *th;
  374. char buf[256];
  375. reply->nb_transports = 0;
  376. for(;;) {
  377. skip_spaces(&p);
  378. if (*p == '\0')
  379. break;
  380. th = &reply->transports[reply->nb_transports];
  381. get_word_sep(transport_protocol, sizeof(transport_protocol),
  382. "/", &p);
  383. if (*p == '/')
  384. p++;
  385. get_word_sep(profile, sizeof(profile), "/;,", &p);
  386. lower_transport[0] = '\0';
  387. if (*p == '/') {
  388. get_word_sep(lower_transport, sizeof(lower_transport),
  389. ";,", &p);
  390. }
  391. if (!strcmp(lower_transport, "TCP"))
  392. th->protocol = RTSP_PROTOCOL_RTP_TCP;
  393. else
  394. th->protocol = RTSP_PROTOCOL_RTP_UDP;
  395. if (*p == ';')
  396. p++;
  397. /* get each parameter */
  398. while (*p != '\0' && *p != ',') {
  399. get_word_sep(parameter, sizeof(parameter), "=;,", &p);
  400. if (!strcmp(parameter, "port")) {
  401. if (*p == '=') {
  402. p++;
  403. rtsp_parse_range(&th->port_min, &th->port_max, &p);
  404. }
  405. } else if (!strcmp(parameter, "client_port")) {
  406. if (*p == '=') {
  407. p++;
  408. rtsp_parse_range(&th->client_port_min,
  409. &th->client_port_max, &p);
  410. }
  411. } else if (!strcmp(parameter, "server_port")) {
  412. if (*p == '=') {
  413. p++;
  414. rtsp_parse_range(&th->server_port_min,
  415. &th->server_port_max, &p);
  416. }
  417. } else if (!strcmp(parameter, "interleaved")) {
  418. if (*p == '=') {
  419. p++;
  420. rtsp_parse_range(&th->interleaved_min,
  421. &th->interleaved_max, &p);
  422. }
  423. } else if (!strcmp(parameter, "multicast")) {
  424. if (th->protocol == RTSP_PROTOCOL_RTP_UDP)
  425. th->protocol = RTSP_PROTOCOL_RTP_UDP_MULTICAST;
  426. } else if (!strcmp(parameter, "ttl")) {
  427. if (*p == '=') {
  428. p++;
  429. th->ttl = strtol(p, (char **)&p, 10);
  430. }
  431. } else if (!strcmp(parameter, "destination")) {
  432. struct in_addr ipaddr;
  433. if (*p == '=') {
  434. p++;
  435. get_word_sep(buf, sizeof(buf), ";,", &p);
  436. if (inet_aton(buf, &ipaddr))
  437. th->destination = ntohl(ipaddr.s_addr);
  438. }
  439. }
  440. while (*p != ';' && *p != '\0' && *p != ',')
  441. p++;
  442. if (*p == ';')
  443. p++;
  444. }
  445. if (*p == ',')
  446. p++;
  447. reply->nb_transports++;
  448. }
  449. }
  450. void rtsp_parse_line(RTSPHeader *reply, const char *buf)
  451. {
  452. const char *p;
  453. /* NOTE: we do case independent match for broken servers */
  454. p = buf;
  455. if (stristart(p, "Session:", &p)) {
  456. get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
  457. } else if (stristart(p, "Content-Length:", &p)) {
  458. reply->content_length = strtol(p, NULL, 10);
  459. } else if (stristart(p, "Transport:", &p)) {
  460. rtsp_parse_transport(reply, p);
  461. } else if (stristart(p, "CSeq:", &p)) {
  462. reply->seq = strtol(p, NULL, 10);
  463. }
  464. }
  465. static void rtsp_send_cmd(AVFormatContext *s,
  466. const char *cmd, RTSPHeader *reply,
  467. unsigned char **content_ptr)
  468. {
  469. RTSPState *rt = s->priv_data;
  470. char buf[4096], buf1[1024], *q;
  471. unsigned char ch;
  472. const char *p;
  473. int content_length, line_count;
  474. unsigned char *content = NULL;
  475. memset(reply, 0, sizeof(RTSPHeader));
  476. rt->seq++;
  477. pstrcpy(buf, sizeof(buf), cmd);
  478. snprintf(buf1, sizeof(buf1), "CSeq: %d\n", rt->seq);
  479. pstrcat(buf, sizeof(buf), buf1);
  480. if (rt->session_id[0] != '\0' && !strstr(cmd, "\nIf-Match:")) {
  481. snprintf(buf1, sizeof(buf1), "Session: %s\n", rt->session_id);
  482. pstrcat(buf, sizeof(buf), buf1);
  483. }
  484. pstrcat(buf, sizeof(buf), "\n");
  485. #ifdef DEBUG
  486. printf("Sending:\n%s--\n", buf);
  487. #endif
  488. url_write(rt->rtsp_hd, buf, strlen(buf));
  489. /* parse reply (XXX: use buffers) */
  490. line_count = 0;
  491. rt->last_reply[0] = '\0';
  492. for(;;) {
  493. q = buf;
  494. for(;;) {
  495. if (url_read(rt->rtsp_hd, &ch, 1) == 0)
  496. break;
  497. if (ch == '\n')
  498. break;
  499. if (ch != '\r') {
  500. if ((q - buf) < sizeof(buf) - 1)
  501. *q++ = ch;
  502. }
  503. }
  504. *q = '\0';
  505. #ifdef DEBUG
  506. printf("line='%s'\n", buf);
  507. #endif
  508. /* test if last line */
  509. if (buf[0] == '\0')
  510. break;
  511. p = buf;
  512. if (line_count == 0) {
  513. /* get reply code */
  514. get_word(buf1, sizeof(buf1), &p);
  515. get_word(buf1, sizeof(buf1), &p);
  516. reply->status_code = atoi(buf1);
  517. } else {
  518. rtsp_parse_line(reply, p);
  519. pstrcat(rt->last_reply, sizeof(rt->last_reply), p);
  520. pstrcat(rt->last_reply, sizeof(rt->last_reply), "\n");
  521. }
  522. line_count++;
  523. }
  524. if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0')
  525. pstrcpy(rt->session_id, sizeof(rt->session_id), reply->session_id);
  526. content_length = reply->content_length;
  527. if (content_length > 0) {
  528. /* leave some room for a trailing '\0' (useful for simple parsing) */
  529. content = av_malloc(content_length + 1);
  530. url_read(rt->rtsp_hd, content, content_length);
  531. content[content_length] = '\0';
  532. }
  533. if (content_ptr)
  534. *content_ptr = content;
  535. }
  536. /* useful for modules: set RTSP callback function */
  537. void rtsp_set_callback(FFRTSPCallback *rtsp_cb)
  538. {
  539. ff_rtsp_callback = rtsp_cb;
  540. }
  541. static int rtsp_read_header(AVFormatContext *s,
  542. AVFormatParameters *ap)
  543. {
  544. RTSPState *rt = s->priv_data;
  545. char host[1024], path[1024], tcpname[1024], cmd[2048];
  546. URLContext *rtsp_hd;
  547. int port, i, ret, err;
  548. RTSPHeader reply1, *reply = &reply1;
  549. unsigned char *content = NULL;
  550. AVStream *st;
  551. RTSPStream *rtsp_st;
  552. int protocol_mask;
  553. rtsp_abort_req = 0;
  554. /* extract hostname and port */
  555. url_split(NULL, 0,
  556. host, sizeof(host), &port, path, sizeof(path), s->filename);
  557. if (port < 0)
  558. port = RTSP_DEFAULT_PORT;
  559. /* open the tcp connexion */
  560. snprintf(tcpname, sizeof(tcpname), "tcp://%s:%d", host, port);
  561. if (url_open(&rtsp_hd, tcpname, URL_RDWR) < 0)
  562. return AVERROR_IO;
  563. rt->rtsp_hd = rtsp_hd;
  564. rt->seq = 0;
  565. /* describe the stream */
  566. snprintf(cmd, sizeof(cmd),
  567. "DESCRIBE %s RTSP/1.0\n"
  568. "Accept: application/sdp\n",
  569. s->filename);
  570. rtsp_send_cmd(s, cmd, reply, &content);
  571. if (!content) {
  572. err = AVERROR_INVALIDDATA;
  573. goto fail;
  574. }
  575. if (reply->status_code != RTSP_STATUS_OK) {
  576. err = AVERROR_INVALIDDATA;
  577. goto fail;
  578. }
  579. /* now we got the SDP description, we parse it */
  580. ret = sdp_parse(s, (const char *)content);
  581. av_freep(&content);
  582. if (ret < 0) {
  583. err = AVERROR_INVALIDDATA;
  584. goto fail;
  585. }
  586. protocol_mask = rtsp_default_protocols;
  587. /* for each stream, make the setup request */
  588. /* XXX: we assume the same server is used for the control of each
  589. RTSP stream */
  590. for(i=0;i<s->nb_streams;i++) {
  591. char transport[2048];
  592. AVInputFormat *fmt;
  593. st = s->streams[i];
  594. rtsp_st = st->priv_data;
  595. /* compute available transports */
  596. transport[0] = '\0';
  597. /* RTP/UDP */
  598. if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP)) {
  599. char buf[256];
  600. int j;
  601. /* first try in specified port range */
  602. if (rtsp_rtp_port_min != 0) {
  603. for(j=rtsp_rtp_port_min;j<=rtsp_rtp_port_max;j++) {
  604. snprintf(buf, sizeof(buf), "rtp://?localport=%d", j);
  605. if (!av_open_input_file(&rtsp_st->ic, buf,
  606. &rtp_demux, 0, NULL))
  607. goto rtp_opened;
  608. }
  609. }
  610. /* then try on any port */
  611. if (av_open_input_file(&rtsp_st->ic, "rtp://",
  612. &rtp_demux, 0, NULL) < 0) {
  613. err = AVERROR_INVALIDDATA;
  614. goto fail;
  615. }
  616. rtp_opened:
  617. port = rtp_get_local_port(url_fileno(&rtsp_st->ic->pb));
  618. if (transport[0] != '\0')
  619. pstrcat(transport, sizeof(transport), ",");
  620. snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
  621. "RTP/AVP/UDP;unicast;client_port=%d-%d",
  622. port, port + 1);
  623. }
  624. /* RTP/TCP */
  625. if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_TCP)) {
  626. if (transport[0] != '\0')
  627. pstrcat(transport, sizeof(transport), ",");
  628. snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
  629. "RTP/AVP/TCP");
  630. }
  631. if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP_MULTICAST)) {
  632. if (transport[0] != '\0')
  633. pstrcat(transport, sizeof(transport), ",");
  634. snprintf(transport + strlen(transport),
  635. sizeof(transport) - strlen(transport) - 1,
  636. "RTP/AVP/UDP;multicast");
  637. }
  638. snprintf(cmd, sizeof(cmd),
  639. "SETUP %s RTSP/1.0\n"
  640. "Transport: %s\n",
  641. rtsp_st->control_url, transport);
  642. rtsp_send_cmd(s, cmd, reply, NULL);
  643. if (reply->status_code != RTSP_STATUS_OK ||
  644. reply->nb_transports != 1) {
  645. err = AVERROR_INVALIDDATA;
  646. goto fail;
  647. }
  648. /* XXX: same protocol for all streams is required */
  649. if (i > 0) {
  650. if (reply->transports[0].protocol != rt->protocol) {
  651. err = AVERROR_INVALIDDATA;
  652. goto fail;
  653. }
  654. } else {
  655. rt->protocol = reply->transports[0].protocol;
  656. }
  657. /* close RTP connection if not choosen */
  658. if (reply->transports[0].protocol != RTSP_PROTOCOL_RTP_UDP &&
  659. (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP))) {
  660. av_close_input_file(rtsp_st->ic);
  661. rtsp_st->ic = NULL;
  662. }
  663. switch(reply->transports[0].protocol) {
  664. case RTSP_PROTOCOL_RTP_TCP:
  665. fmt = &rtp_demux;
  666. if (av_open_input_file(&rtsp_st->ic, "null", fmt, 0, NULL) < 0) {
  667. err = AVERROR_INVALIDDATA;
  668. goto fail;
  669. }
  670. rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
  671. rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
  672. break;
  673. case RTSP_PROTOCOL_RTP_UDP:
  674. {
  675. char url[1024];
  676. /* XXX: also use address if specified */
  677. snprintf(url, sizeof(url), "rtp://%s:%d",
  678. host, reply->transports[0].server_port_min);
  679. if (rtp_set_remote_url(url_fileno(&rtsp_st->ic->pb), url) < 0) {
  680. err = AVERROR_INVALIDDATA;
  681. goto fail;
  682. }
  683. }
  684. break;
  685. case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
  686. {
  687. char url[1024];
  688. int ttl;
  689. fmt = &rtp_demux;
  690. ttl = reply->transports[0].ttl;
  691. if (!ttl)
  692. ttl = 16;
  693. snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
  694. host,
  695. reply->transports[0].server_port_min,
  696. ttl);
  697. if (av_open_input_file(&rtsp_st->ic, url, fmt, 0, NULL) < 0) {
  698. err = AVERROR_INVALIDDATA;
  699. goto fail;
  700. }
  701. }
  702. break;
  703. }
  704. }
  705. /* use callback if available to extend setup */
  706. if (ff_rtsp_callback) {
  707. if (ff_rtsp_callback(RTSP_ACTION_CLIENT_SETUP, rt->session_id,
  708. NULL, 0, rt->last_reply) < 0) {
  709. err = AVERROR_INVALIDDATA;
  710. goto fail;
  711. }
  712. }
  713. /* start playing */
  714. snprintf(cmd, sizeof(cmd),
  715. "PLAY %s RTSP/1.0\n",
  716. s->filename);
  717. rtsp_send_cmd(s, cmd, reply, NULL);
  718. if (reply->status_code != RTSP_STATUS_OK) {
  719. err = AVERROR_INVALIDDATA;
  720. goto fail;
  721. }
  722. /* open TCP with bufferized input */
  723. if (rt->protocol == RTSP_PROTOCOL_RTP_TCP) {
  724. if (url_fdopen(&rt->rtsp_gb, rt->rtsp_hd) < 0) {
  725. err = AVERROR_NOMEM;
  726. goto fail;
  727. }
  728. }
  729. return 0;
  730. fail:
  731. for(i=0;i<s->nb_streams;i++) {
  732. st = s->streams[i];
  733. rtsp_st = st->priv_data;
  734. if (rtsp_st) {
  735. if (rtsp_st->ic)
  736. av_close_input_file(rtsp_st->ic);
  737. }
  738. av_free(rtsp_st);
  739. }
  740. av_freep(&content);
  741. url_close(rt->rtsp_hd);
  742. return err;
  743. }
  744. static int tcp_read_packet(AVFormatContext *s,
  745. AVPacket *pkt)
  746. {
  747. RTSPState *rt = s->priv_data;
  748. ByteIOContext *rtsp_gb = &rt->rtsp_gb;
  749. int c, id, len, i, ret;
  750. AVStream *st;
  751. RTSPStream *rtsp_st;
  752. char buf[RTP_MAX_PACKET_LENGTH];
  753. redo:
  754. for(;;) {
  755. c = url_fgetc(rtsp_gb);
  756. if (c == URL_EOF)
  757. return AVERROR_IO;
  758. if (c == '$')
  759. break;
  760. }
  761. id = get_byte(rtsp_gb);
  762. len = get_be16(rtsp_gb);
  763. if (len > RTP_MAX_PACKET_LENGTH || len < 12)
  764. goto redo;
  765. /* get the data */
  766. get_buffer(rtsp_gb, buf, len);
  767. /* find the matching stream */
  768. for(i = 0; i < s->nb_streams; i++) {
  769. st = s->streams[i];
  770. rtsp_st = st->priv_data;
  771. if (i >= rtsp_st->interleaved_min &&
  772. i <= rtsp_st->interleaved_max)
  773. goto found;
  774. }
  775. goto redo;
  776. found:
  777. ret = rtp_parse_packet(rtsp_st->ic, pkt, buf, len);
  778. if (ret < 0)
  779. goto redo;
  780. pkt->stream_index = i;
  781. return ret;
  782. }
  783. /* NOTE: output one packet at a time. May need to add a small fifo */
  784. static int udp_read_packet(AVFormatContext *s,
  785. AVPacket *pkt)
  786. {
  787. AVFormatContext *ic;
  788. AVStream *st;
  789. RTSPStream *rtsp_st;
  790. fd_set rfds;
  791. int fd1, fd2, fd_max, n, i, ret;
  792. char buf[RTP_MAX_PACKET_LENGTH];
  793. struct timeval tv;
  794. for(;;) {
  795. if (rtsp_abort_req)
  796. return -EIO;
  797. FD_ZERO(&rfds);
  798. fd_max = -1;
  799. for(i = 0; i < s->nb_streams; i++) {
  800. st = s->streams[i];
  801. rtsp_st = st->priv_data;
  802. ic = rtsp_st->ic;
  803. /* currently, we cannot probe RTCP handle because of blocking restrictions */
  804. rtp_get_file_handles(url_fileno(&ic->pb), &fd1, &fd2);
  805. if (fd1 > fd_max)
  806. fd_max = fd1;
  807. FD_SET(fd1, &rfds);
  808. }
  809. /* XXX: also add proper API to abort */
  810. tv.tv_sec = 0;
  811. tv.tv_usec = 500000;
  812. n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
  813. if (n > 0) {
  814. for(i = 0; i < s->nb_streams; i++) {
  815. st = s->streams[i];
  816. rtsp_st = st->priv_data;
  817. ic = rtsp_st->ic;
  818. rtp_get_file_handles(url_fileno(&ic->pb), &fd1, &fd2);
  819. if (FD_ISSET(fd1, &rfds)) {
  820. ret = url_read(url_fileno(&ic->pb), buf, sizeof(buf));
  821. if (ret >= 0 &&
  822. rtp_parse_packet(ic, pkt, buf, ret) == 0) {
  823. pkt->stream_index = i;
  824. return ret;
  825. }
  826. }
  827. }
  828. }
  829. }
  830. }
  831. static int rtsp_read_packet(AVFormatContext *s,
  832. AVPacket *pkt)
  833. {
  834. RTSPState *rt = s->priv_data;
  835. int ret;
  836. switch(rt->protocol) {
  837. default:
  838. case RTSP_PROTOCOL_RTP_TCP:
  839. ret = tcp_read_packet(s, pkt);
  840. break;
  841. case RTSP_PROTOCOL_RTP_UDP:
  842. ret = udp_read_packet(s, pkt);
  843. break;
  844. }
  845. return ret;
  846. }
  847. static int rtsp_read_close(AVFormatContext *s)
  848. {
  849. RTSPState *rt = s->priv_data;
  850. AVStream *st;
  851. RTSPStream *rtsp_st;
  852. RTSPHeader reply1, *reply = &reply1;
  853. int i;
  854. char cmd[1024];
  855. /* NOTE: it is valid to flush the buffer here */
  856. if (rt->protocol == RTSP_PROTOCOL_RTP_TCP) {
  857. url_fclose(&rt->rtsp_gb);
  858. }
  859. snprintf(cmd, sizeof(cmd),
  860. "TEARDOWN %s RTSP/1.0\n",
  861. s->filename);
  862. rtsp_send_cmd(s, cmd, reply, NULL);
  863. if (ff_rtsp_callback) {
  864. ff_rtsp_callback(RTSP_ACTION_CLIENT_TEARDOWN, rt->session_id,
  865. NULL, 0, NULL);
  866. }
  867. for(i=0;i<s->nb_streams;i++) {
  868. st = s->streams[i];
  869. rtsp_st = st->priv_data;
  870. if (rtsp_st) {
  871. if (rtsp_st->ic)
  872. av_close_input_file(rtsp_st->ic);
  873. }
  874. av_free(rtsp_st);
  875. }
  876. url_close(rt->rtsp_hd);
  877. return 0;
  878. }
  879. static AVInputFormat rtsp_demux = {
  880. "rtsp",
  881. "RTSP input format",
  882. sizeof(RTSPState),
  883. rtsp_probe,
  884. rtsp_read_header,
  885. rtsp_read_packet,
  886. rtsp_read_close,
  887. .flags = AVFMT_NOFILE,
  888. };
  889. static int sdp_probe(AVProbeData *p1)
  890. {
  891. const char *p;
  892. /* we look for a line beginning "c=IN IP4" */
  893. p = p1->buf;
  894. while (*p != '\0') {
  895. if (strstart(p, "c=IN IP4", NULL))
  896. return AVPROBE_SCORE_MAX / 2;
  897. p = strchr(p, '\n');
  898. if (!p)
  899. break;
  900. p++;
  901. if (*p == '\r')
  902. p++;
  903. }
  904. return 0;
  905. }
  906. #define SDP_MAX_SIZE 8192
  907. static int sdp_read_header(AVFormatContext *s,
  908. AVFormatParameters *ap)
  909. {
  910. AVStream *st;
  911. RTSPStream *rtsp_st;
  912. int size, i, err;
  913. char *content;
  914. char url[1024];
  915. /* read the whole sdp file */
  916. /* XXX: better loading */
  917. content = av_malloc(SDP_MAX_SIZE);
  918. size = get_buffer(&s->pb, content, SDP_MAX_SIZE - 1);
  919. if (size <= 0) {
  920. av_free(content);
  921. return AVERROR_INVALIDDATA;
  922. }
  923. content[size] ='\0';
  924. sdp_parse(s, content);
  925. av_free(content);
  926. /* open each RTP stream */
  927. for(i=0;i<s->nb_streams;i++) {
  928. st = s->streams[i];
  929. rtsp_st = st->priv_data;
  930. snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
  931. inet_ntoa(rtsp_st->sdp_ip),
  932. rtsp_st->sdp_port,
  933. rtsp_st->sdp_ttl);
  934. if (av_open_input_file(&rtsp_st->ic, url, &rtp_demux, 0, NULL) < 0) {
  935. err = AVERROR_INVALIDDATA;
  936. goto fail;
  937. }
  938. }
  939. return 0;
  940. fail:
  941. for(i=0;i<s->nb_streams;i++) {
  942. st = s->streams[i];
  943. rtsp_st = st->priv_data;
  944. if (rtsp_st) {
  945. if (rtsp_st->ic)
  946. av_close_input_file(rtsp_st->ic);
  947. }
  948. av_free(rtsp_st);
  949. }
  950. return err;
  951. }
  952. static int sdp_read_packet(AVFormatContext *s,
  953. AVPacket *pkt)
  954. {
  955. return udp_read_packet(s, pkt);
  956. }
  957. static int sdp_read_close(AVFormatContext *s)
  958. {
  959. AVStream *st;
  960. RTSPStream *rtsp_st;
  961. int i;
  962. for(i=0;i<s->nb_streams;i++) {
  963. st = s->streams[i];
  964. rtsp_st = st->priv_data;
  965. if (rtsp_st) {
  966. if (rtsp_st->ic)
  967. av_close_input_file(rtsp_st->ic);
  968. }
  969. av_free(rtsp_st);
  970. }
  971. return 0;
  972. }
  973. static AVInputFormat sdp_demux = {
  974. "sdp",
  975. "SDP",
  976. sizeof(RTSPState),
  977. sdp_probe,
  978. sdp_read_header,
  979. sdp_read_packet,
  980. sdp_read_close,
  981. };
  982. /* dummy redirector format (used directly in av_open_input_file now) */
  983. static int redir_probe(AVProbeData *pd)
  984. {
  985. const char *p;
  986. p = pd->buf;
  987. while (redir_isspace(*p))
  988. p++;
  989. if (strstart(p, "http://", NULL) ||
  990. strstart(p, "rtsp://", NULL))
  991. return AVPROBE_SCORE_MAX;
  992. return 0;
  993. }
  994. /* called from utils.c */
  995. int redir_open(AVFormatContext **ic_ptr, ByteIOContext *f)
  996. {
  997. char buf[4096], *q;
  998. int c;
  999. AVFormatContext *ic = NULL;
  1000. /* parse each URL and try to open it */
  1001. c = url_fgetc(f);
  1002. while (c != URL_EOF) {
  1003. /* skip spaces */
  1004. for(;;) {
  1005. if (!redir_isspace(c))
  1006. break;
  1007. c = url_fgetc(f);
  1008. }
  1009. if (c == URL_EOF)
  1010. break;
  1011. /* record url */
  1012. q = buf;
  1013. for(;;) {
  1014. if (c == URL_EOF || redir_isspace(c))
  1015. break;
  1016. if ((q - buf) < sizeof(buf) - 1)
  1017. *q++ = c;
  1018. c = url_fgetc(f);
  1019. }
  1020. *q = '\0';
  1021. //printf("URL='%s'\n", buf);
  1022. /* try to open the media file */
  1023. if (av_open_input_file(&ic, buf, NULL, 0, NULL) == 0)
  1024. break;
  1025. }
  1026. *ic_ptr = ic;
  1027. if (!ic)
  1028. return AVERROR_IO;
  1029. else
  1030. return 0;
  1031. }
  1032. AVInputFormat redir_demux = {
  1033. "redir",
  1034. "Redirector format",
  1035. 0,
  1036. redir_probe,
  1037. NULL,
  1038. NULL,
  1039. NULL,
  1040. };
  1041. int rtsp_init(void)
  1042. {
  1043. av_register_input_format(&rtsp_demux);
  1044. av_register_input_format(&redir_demux);
  1045. av_register_input_format(&sdp_demux);
  1046. return 0;
  1047. }