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.

1344 lines
37KB

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