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.

1276 lines
35KB

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