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.

1330 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. /* skip a RTP/TCP interleaved packet */
  511. static void rtsp_skip_packet(AVFormatContext *s)
  512. {
  513. RTSPState *rt = s->priv_data;
  514. int ret, len, len1;
  515. uint8_t buf[1024];
  516. ret = url_read(rt->rtsp_hd, buf, 3);
  517. if (ret != 3)
  518. return;
  519. len = (buf[1] << 8) | buf[2];
  520. #ifdef DEBUG
  521. printf("skipping RTP packet len=%d\n", len);
  522. #endif
  523. /* skip payload */
  524. while (len > 0) {
  525. len1 = len;
  526. if (len1 > sizeof(buf))
  527. len1 = sizeof(buf);
  528. ret = url_read(rt->rtsp_hd, buf, len1);
  529. if (ret != len1)
  530. return;
  531. len -= len1;
  532. }
  533. }
  534. static void rtsp_send_cmd(AVFormatContext *s,
  535. const char *cmd, RTSPHeader *reply,
  536. unsigned char **content_ptr)
  537. {
  538. RTSPState *rt = s->priv_data;
  539. char buf[4096], buf1[1024], *q;
  540. unsigned char ch;
  541. const char *p;
  542. int content_length, line_count;
  543. unsigned char *content = NULL;
  544. memset(reply, 0, sizeof(RTSPHeader));
  545. rt->seq++;
  546. pstrcpy(buf, sizeof(buf), cmd);
  547. snprintf(buf1, sizeof(buf1), "CSeq: %d\r\n", rt->seq);
  548. pstrcat(buf, sizeof(buf), buf1);
  549. if (rt->session_id[0] != '\0' && !strstr(cmd, "\nIf-Match:")) {
  550. snprintf(buf1, sizeof(buf1), "Session: %s\r\n", rt->session_id);
  551. pstrcat(buf, sizeof(buf), buf1);
  552. }
  553. pstrcat(buf, sizeof(buf), "\r\n");
  554. #ifdef DEBUG
  555. printf("Sending:\n%s--\n", buf);
  556. #endif
  557. url_write(rt->rtsp_hd, buf, strlen(buf));
  558. /* parse reply (XXX: use buffers) */
  559. line_count = 0;
  560. rt->last_reply[0] = '\0';
  561. for(;;) {
  562. q = buf;
  563. for(;;) {
  564. if (url_read(rt->rtsp_hd, &ch, 1) != 1)
  565. break;
  566. if (ch == '\n')
  567. break;
  568. if (ch == '$') {
  569. /* XXX: only parse it if first char on line ? */
  570. rtsp_skip_packet(s);
  571. } else if (ch != '\r') {
  572. if ((q - buf) < sizeof(buf) - 1)
  573. *q++ = ch;
  574. }
  575. }
  576. *q = '\0';
  577. #ifdef DEBUG
  578. printf("line='%s'\n", buf);
  579. #endif
  580. /* test if last line */
  581. if (buf[0] == '\0')
  582. break;
  583. p = buf;
  584. if (line_count == 0) {
  585. /* get reply code */
  586. get_word(buf1, sizeof(buf1), &p);
  587. get_word(buf1, sizeof(buf1), &p);
  588. reply->status_code = atoi(buf1);
  589. } else {
  590. rtsp_parse_line(reply, p);
  591. pstrcat(rt->last_reply, sizeof(rt->last_reply), p);
  592. pstrcat(rt->last_reply, sizeof(rt->last_reply), "\n");
  593. }
  594. line_count++;
  595. }
  596. if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0')
  597. pstrcpy(rt->session_id, sizeof(rt->session_id), reply->session_id);
  598. content_length = reply->content_length;
  599. if (content_length > 0) {
  600. /* leave some room for a trailing '\0' (useful for simple parsing) */
  601. content = av_malloc(content_length + 1);
  602. url_read(rt->rtsp_hd, content, content_length);
  603. content[content_length] = '\0';
  604. }
  605. if (content_ptr)
  606. *content_ptr = content;
  607. }
  608. /* useful for modules: set RTSP callback function */
  609. void rtsp_set_callback(FFRTSPCallback *rtsp_cb)
  610. {
  611. ff_rtsp_callback = rtsp_cb;
  612. }
  613. /* close and free RTSP streams */
  614. static void rtsp_close_streams(RTSPState *rt)
  615. {
  616. int i;
  617. RTSPStream *rtsp_st;
  618. for(i=0;i<rt->nb_rtsp_streams;i++) {
  619. rtsp_st = rt->rtsp_streams[i];
  620. if (rtsp_st) {
  621. if (rtsp_st->rtp_ctx)
  622. rtp_parse_close(rtsp_st->rtp_ctx);
  623. if (rtsp_st->rtp_handle)
  624. url_close(rtsp_st->rtp_handle);
  625. }
  626. av_free(rtsp_st);
  627. }
  628. av_free(rt->rtsp_streams);
  629. }
  630. static int rtsp_read_header(AVFormatContext *s,
  631. AVFormatParameters *ap)
  632. {
  633. RTSPState *rt = s->priv_data;
  634. char host[1024], path[1024], tcpname[1024], cmd[2048];
  635. URLContext *rtsp_hd;
  636. int port, i, ret, err;
  637. RTSPHeader reply1, *reply = &reply1;
  638. unsigned char *content = NULL;
  639. RTSPStream *rtsp_st;
  640. int protocol_mask;
  641. AVStream *st;
  642. /* extract hostname and port */
  643. url_split(NULL, 0,
  644. host, sizeof(host), &port, path, sizeof(path), s->filename);
  645. if (port < 0)
  646. port = RTSP_DEFAULT_PORT;
  647. /* open the tcp connexion */
  648. snprintf(tcpname, sizeof(tcpname), "tcp://%s:%d", host, port);
  649. if (url_open(&rtsp_hd, tcpname, URL_RDWR) < 0)
  650. return AVERROR_IO;
  651. rt->rtsp_hd = rtsp_hd;
  652. rt->seq = 0;
  653. /* describe the stream */
  654. snprintf(cmd, sizeof(cmd),
  655. "DESCRIBE %s RTSP/1.0\r\n"
  656. "Accept: application/sdp\r\n",
  657. s->filename);
  658. rtsp_send_cmd(s, cmd, reply, &content);
  659. if (!content) {
  660. err = AVERROR_INVALIDDATA;
  661. goto fail;
  662. }
  663. if (reply->status_code != RTSP_STATUS_OK) {
  664. err = AVERROR_INVALIDDATA;
  665. goto fail;
  666. }
  667. /* now we got the SDP description, we parse it */
  668. ret = sdp_parse(s, (const char *)content);
  669. av_freep(&content);
  670. if (ret < 0) {
  671. err = AVERROR_INVALIDDATA;
  672. goto fail;
  673. }
  674. protocol_mask = rtsp_default_protocols;
  675. /* for each stream, make the setup request */
  676. /* XXX: we assume the same server is used for the control of each
  677. RTSP stream */
  678. for(i=0;i<rt->nb_rtsp_streams;i++) {
  679. char transport[2048];
  680. rtsp_st = rt->rtsp_streams[i];
  681. /* compute available transports */
  682. transport[0] = '\0';
  683. /* RTP/UDP */
  684. if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP)) {
  685. char buf[256];
  686. int j;
  687. /* first try in specified port range */
  688. if (rtsp_rtp_port_min != 0) {
  689. for(j=rtsp_rtp_port_min;j<=rtsp_rtp_port_max;j++) {
  690. snprintf(buf, sizeof(buf), "rtp://?localport=%d", j);
  691. if (url_open(&rtsp_st->rtp_handle, buf, URL_RDONLY) == 0)
  692. goto rtp_opened;
  693. }
  694. }
  695. /* then try on any port */
  696. if (url_open(&rtsp_st->rtp_handle, "rtp://", URL_RDONLY) < 0) {
  697. err = AVERROR_INVALIDDATA;
  698. goto fail;
  699. }
  700. rtp_opened:
  701. port = rtp_get_local_port(rtsp_st->rtp_handle);
  702. if (transport[0] != '\0')
  703. pstrcat(transport, sizeof(transport), ",");
  704. snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
  705. "RTP/AVP/UDP;unicast;client_port=%d-%d",
  706. port, port + 1);
  707. }
  708. /* RTP/TCP */
  709. if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_TCP)) {
  710. if (transport[0] != '\0')
  711. pstrcat(transport, sizeof(transport), ",");
  712. snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
  713. "RTP/AVP/TCP");
  714. }
  715. if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP_MULTICAST)) {
  716. if (transport[0] != '\0')
  717. pstrcat(transport, sizeof(transport), ",");
  718. snprintf(transport + strlen(transport),
  719. sizeof(transport) - strlen(transport) - 1,
  720. "RTP/AVP/UDP;multicast");
  721. }
  722. snprintf(cmd, sizeof(cmd),
  723. "SETUP %s RTSP/1.0\r\n"
  724. "Transport: %s\r\n",
  725. rtsp_st->control_url, transport);
  726. rtsp_send_cmd(s, cmd, reply, NULL);
  727. if (reply->status_code != RTSP_STATUS_OK ||
  728. reply->nb_transports != 1) {
  729. err = AVERROR_INVALIDDATA;
  730. goto fail;
  731. }
  732. /* XXX: same protocol for all streams is required */
  733. if (i > 0) {
  734. if (reply->transports[0].protocol != rt->protocol) {
  735. err = AVERROR_INVALIDDATA;
  736. goto fail;
  737. }
  738. } else {
  739. rt->protocol = reply->transports[0].protocol;
  740. }
  741. /* close RTP connection if not choosen */
  742. if (reply->transports[0].protocol != RTSP_PROTOCOL_RTP_UDP &&
  743. (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP))) {
  744. url_close(rtsp_st->rtp_handle);
  745. rtsp_st->rtp_handle = NULL;
  746. }
  747. switch(reply->transports[0].protocol) {
  748. case RTSP_PROTOCOL_RTP_TCP:
  749. rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
  750. rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
  751. break;
  752. case RTSP_PROTOCOL_RTP_UDP:
  753. {
  754. char url[1024];
  755. /* XXX: also use address if specified */
  756. snprintf(url, sizeof(url), "rtp://%s:%d",
  757. host, reply->transports[0].server_port_min);
  758. if (rtp_set_remote_url(rtsp_st->rtp_handle, url) < 0) {
  759. err = AVERROR_INVALIDDATA;
  760. goto fail;
  761. }
  762. }
  763. break;
  764. case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
  765. {
  766. char url[1024];
  767. int ttl;
  768. ttl = reply->transports[0].ttl;
  769. if (!ttl)
  770. ttl = 16;
  771. snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
  772. host,
  773. reply->transports[0].server_port_min,
  774. ttl);
  775. if (url_open(&rtsp_st->rtp_handle, url, URL_RDONLY) < 0) {
  776. err = AVERROR_INVALIDDATA;
  777. goto fail;
  778. }
  779. }
  780. break;
  781. }
  782. /* open the RTP context */
  783. st = NULL;
  784. if (rtsp_st->stream_index >= 0)
  785. st = s->streams[rtsp_st->stream_index];
  786. if (!st)
  787. s->ctx_flags |= AVFMTCTX_NOHEADER;
  788. rtsp_st->rtp_ctx = rtp_parse_open(s, st, rtsp_st->sdp_payload_type);
  789. if (!rtsp_st->rtp_ctx) {
  790. err = AVERROR_NOMEM;
  791. goto fail;
  792. }
  793. }
  794. /* use callback if available to extend setup */
  795. if (ff_rtsp_callback) {
  796. if (ff_rtsp_callback(RTSP_ACTION_CLIENT_SETUP, rt->session_id,
  797. NULL, 0, rt->last_reply) < 0) {
  798. err = AVERROR_INVALIDDATA;
  799. goto fail;
  800. }
  801. }
  802. rt->state = RTSP_STATE_IDLE;
  803. rt->seek_timestamp = 0; /* default is to start stream at position
  804. zero */
  805. if (ap && ap->initial_pause) {
  806. /* do not start immediately */
  807. } else {
  808. if (rtsp_read_play(s) < 0) {
  809. err = AVERROR_INVALIDDATA;
  810. goto fail;
  811. }
  812. }
  813. return 0;
  814. fail:
  815. rtsp_close_streams(rt);
  816. av_freep(&content);
  817. url_close(rt->rtsp_hd);
  818. return err;
  819. }
  820. static int tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  821. uint8_t *buf, int buf_size)
  822. {
  823. RTSPState *rt = s->priv_data;
  824. int id, len, i, ret;
  825. RTSPStream *rtsp_st;
  826. #ifdef DEBUG_RTP_TCP
  827. printf("tcp_read_packet:\n");
  828. #endif
  829. redo:
  830. for(;;) {
  831. ret = url_read(rt->rtsp_hd, buf, 1);
  832. #ifdef DEBUG_RTP_TCP
  833. printf("ret=%d c=%02x [%c]\n", ret, buf[0], buf[0]);
  834. #endif
  835. if (ret != 1)
  836. return -1;
  837. if (buf[0] == '$')
  838. break;
  839. }
  840. ret = url_read(rt->rtsp_hd, buf, 3);
  841. if (ret != 3)
  842. return -1;
  843. id = buf[0];
  844. len = (buf[1] << 8) | buf[2];
  845. #ifdef DEBUG_RTP_TCP
  846. printf("id=%d len=%d\n", id, len);
  847. #endif
  848. if (len > buf_size || len < 12)
  849. goto redo;
  850. /* get the data */
  851. ret = url_read(rt->rtsp_hd, buf, len);
  852. if (ret != len)
  853. return -1;
  854. /* find the matching stream */
  855. for(i = 0; i < rt->nb_rtsp_streams; i++) {
  856. rtsp_st = rt->rtsp_streams[i];
  857. if (id >= rtsp_st->interleaved_min &&
  858. id <= rtsp_st->interleaved_max)
  859. goto found;
  860. }
  861. goto redo;
  862. found:
  863. *prtsp_st = rtsp_st;
  864. return len;
  865. }
  866. static int udp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  867. uint8_t *buf, int buf_size)
  868. {
  869. RTSPState *rt = s->priv_data;
  870. RTSPStream *rtsp_st;
  871. fd_set rfds;
  872. int fd1, fd2, fd_max, n, i, ret;
  873. struct timeval tv;
  874. for(;;) {
  875. if (url_interrupt_cb())
  876. return -1;
  877. FD_ZERO(&rfds);
  878. fd_max = -1;
  879. for(i = 0; i < rt->nb_rtsp_streams; i++) {
  880. rtsp_st = rt->rtsp_streams[i];
  881. /* currently, we cannot probe RTCP handle because of blocking restrictions */
  882. rtp_get_file_handles(rtsp_st->rtp_handle, &fd1, &fd2);
  883. if (fd1 > fd_max)
  884. fd_max = fd1;
  885. FD_SET(fd1, &rfds);
  886. }
  887. tv.tv_sec = 0;
  888. tv.tv_usec = 100 * 1000;
  889. n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
  890. if (n > 0) {
  891. for(i = 0; i < rt->nb_rtsp_streams; i++) {
  892. rtsp_st = rt->rtsp_streams[i];
  893. rtp_get_file_handles(rtsp_st->rtp_handle, &fd1, &fd2);
  894. if (FD_ISSET(fd1, &rfds)) {
  895. ret = url_read(rtsp_st->rtp_handle, buf, buf_size);
  896. if (ret > 0) {
  897. *prtsp_st = rtsp_st;
  898. return ret;
  899. }
  900. }
  901. }
  902. }
  903. }
  904. }
  905. static int rtsp_read_packet(AVFormatContext *s,
  906. AVPacket *pkt)
  907. {
  908. RTSPState *rt = s->priv_data;
  909. RTSPStream *rtsp_st;
  910. int ret, len;
  911. uint8_t buf[RTP_MAX_PACKET_LENGTH];
  912. /* get next frames from the same RTP packet */
  913. if (rt->cur_rtp) {
  914. ret = rtp_parse_packet(rt->cur_rtp, pkt, NULL, 0);
  915. if (ret == 0) {
  916. rt->cur_rtp = NULL;
  917. return 0;
  918. } else if (ret == 1) {
  919. return 0;
  920. } else {
  921. rt->cur_rtp = NULL;
  922. }
  923. }
  924. /* read next RTP packet */
  925. redo:
  926. switch(rt->protocol) {
  927. default:
  928. case RTSP_PROTOCOL_RTP_TCP:
  929. len = tcp_read_packet(s, &rtsp_st, buf, sizeof(buf));
  930. break;
  931. case RTSP_PROTOCOL_RTP_UDP:
  932. case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
  933. len = udp_read_packet(s, &rtsp_st, buf, sizeof(buf));
  934. break;
  935. }
  936. if (len < 0)
  937. return AVERROR_IO;
  938. ret = rtp_parse_packet(rtsp_st->rtp_ctx, pkt, buf, len);
  939. if (ret < 0)
  940. goto redo;
  941. if (ret == 1) {
  942. /* more packets may follow, so we save the RTP context */
  943. rt->cur_rtp = rtsp_st->rtp_ctx;
  944. }
  945. return 0;
  946. }
  947. static int rtsp_read_play(AVFormatContext *s)
  948. {
  949. RTSPState *rt = s->priv_data;
  950. RTSPHeader reply1, *reply = &reply1;
  951. char cmd[1024];
  952. printf("hello state=%d\n", rt->state);
  953. if (rt->state == RTSP_STATE_PAUSED) {
  954. snprintf(cmd, sizeof(cmd),
  955. "PLAY %s RTSP/1.0\r\n",
  956. s->filename);
  957. } else {
  958. snprintf(cmd, sizeof(cmd),
  959. "PLAY %s RTSP/1.0\r\n"
  960. "Range: npt=%0.3f-\r\n",
  961. s->filename,
  962. (double)rt->seek_timestamp / AV_TIME_BASE);
  963. }
  964. rtsp_send_cmd(s, cmd, reply, NULL);
  965. if (reply->status_code != RTSP_STATUS_OK) {
  966. return -1;
  967. } else {
  968. rt->state = RTSP_STATE_PLAYING;
  969. return 0;
  970. }
  971. }
  972. /* pause the stream */
  973. static int rtsp_read_pause(AVFormatContext *s)
  974. {
  975. RTSPState *rt = s->priv_data;
  976. RTSPHeader reply1, *reply = &reply1;
  977. char cmd[1024];
  978. rt = s->priv_data;
  979. if (rt->state != RTSP_STATE_PLAYING)
  980. return 0;
  981. snprintf(cmd, sizeof(cmd),
  982. "PAUSE %s RTSP/1.0\r\n",
  983. s->filename);
  984. rtsp_send_cmd(s, cmd, reply, NULL);
  985. if (reply->status_code != RTSP_STATUS_OK) {
  986. return -1;
  987. } else {
  988. rt->state = RTSP_STATE_PAUSED;
  989. return 0;
  990. }
  991. }
  992. static int rtsp_read_seek(AVFormatContext *s, int stream_index,
  993. int64_t timestamp)
  994. {
  995. RTSPState *rt = s->priv_data;
  996. rt->seek_timestamp = timestamp;
  997. switch(rt->state) {
  998. default:
  999. case RTSP_STATE_IDLE:
  1000. break;
  1001. case RTSP_STATE_PLAYING:
  1002. if (rtsp_read_play(s) != 0)
  1003. return -1;
  1004. break;
  1005. case RTSP_STATE_PAUSED:
  1006. rt->state = RTSP_STATE_IDLE;
  1007. break;
  1008. }
  1009. return 0;
  1010. }
  1011. static int rtsp_read_close(AVFormatContext *s)
  1012. {
  1013. RTSPState *rt = s->priv_data;
  1014. RTSPHeader reply1, *reply = &reply1;
  1015. char cmd[1024];
  1016. #if 0
  1017. /* NOTE: it is valid to flush the buffer here */
  1018. if (rt->protocol == RTSP_PROTOCOL_RTP_TCP) {
  1019. url_fclose(&rt->rtsp_gb);
  1020. }
  1021. #endif
  1022. snprintf(cmd, sizeof(cmd),
  1023. "TEARDOWN %s RTSP/1.0\r\n",
  1024. s->filename);
  1025. rtsp_send_cmd(s, cmd, reply, NULL);
  1026. if (ff_rtsp_callback) {
  1027. ff_rtsp_callback(RTSP_ACTION_CLIENT_TEARDOWN, rt->session_id,
  1028. NULL, 0, NULL);
  1029. }
  1030. rtsp_close_streams(rt);
  1031. url_close(rt->rtsp_hd);
  1032. return 0;
  1033. }
  1034. AVInputFormat rtsp_demux = {
  1035. "rtsp",
  1036. "RTSP input format",
  1037. sizeof(RTSPState),
  1038. rtsp_probe,
  1039. rtsp_read_header,
  1040. rtsp_read_packet,
  1041. rtsp_read_close,
  1042. rtsp_read_seek,
  1043. .flags = AVFMT_NOFILE,
  1044. .read_play = rtsp_read_play,
  1045. .read_pause = rtsp_read_pause,
  1046. };
  1047. static int sdp_probe(AVProbeData *p1)
  1048. {
  1049. const char *p;
  1050. /* we look for a line beginning "c=IN IP4" */
  1051. p = p1->buf;
  1052. while (*p != '\0') {
  1053. if (strstart(p, "c=IN IP4", NULL))
  1054. return AVPROBE_SCORE_MAX / 2;
  1055. p = strchr(p, '\n');
  1056. if (!p)
  1057. break;
  1058. p++;
  1059. if (*p == '\r')
  1060. p++;
  1061. }
  1062. return 0;
  1063. }
  1064. #define SDP_MAX_SIZE 8192
  1065. static int sdp_read_header(AVFormatContext *s,
  1066. AVFormatParameters *ap)
  1067. {
  1068. RTSPState *rt = s->priv_data;
  1069. RTSPStream *rtsp_st;
  1070. int size, i, err;
  1071. char *content;
  1072. char url[1024];
  1073. AVStream *st;
  1074. /* read the whole sdp file */
  1075. /* XXX: better loading */
  1076. content = av_malloc(SDP_MAX_SIZE);
  1077. size = get_buffer(&s->pb, content, SDP_MAX_SIZE - 1);
  1078. if (size <= 0) {
  1079. av_free(content);
  1080. return AVERROR_INVALIDDATA;
  1081. }
  1082. content[size] ='\0';
  1083. sdp_parse(s, content);
  1084. av_free(content);
  1085. /* open each RTP stream */
  1086. for(i=0;i<rt->nb_rtsp_streams;i++) {
  1087. rtsp_st = rt->rtsp_streams[i];
  1088. snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
  1089. inet_ntoa(rtsp_st->sdp_ip),
  1090. rtsp_st->sdp_port,
  1091. rtsp_st->sdp_ttl);
  1092. if (url_open(&rtsp_st->rtp_handle, url, URL_RDONLY) < 0) {
  1093. err = AVERROR_INVALIDDATA;
  1094. goto fail;
  1095. }
  1096. /* open the RTP context */
  1097. st = NULL;
  1098. if (rtsp_st->stream_index >= 0)
  1099. st = s->streams[rtsp_st->stream_index];
  1100. if (!st)
  1101. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1102. rtsp_st->rtp_ctx = rtp_parse_open(s, st, rtsp_st->sdp_payload_type);
  1103. if (!rtsp_st->rtp_ctx) {
  1104. err = AVERROR_NOMEM;
  1105. goto fail;
  1106. }
  1107. }
  1108. return 0;
  1109. fail:
  1110. rtsp_close_streams(rt);
  1111. return err;
  1112. }
  1113. static int sdp_read_packet(AVFormatContext *s,
  1114. AVPacket *pkt)
  1115. {
  1116. return rtsp_read_packet(s, pkt);
  1117. }
  1118. static int sdp_read_close(AVFormatContext *s)
  1119. {
  1120. RTSPState *rt = s->priv_data;
  1121. rtsp_close_streams(rt);
  1122. return 0;
  1123. }
  1124. static AVInputFormat sdp_demux = {
  1125. "sdp",
  1126. "SDP",
  1127. sizeof(RTSPState),
  1128. sdp_probe,
  1129. sdp_read_header,
  1130. sdp_read_packet,
  1131. sdp_read_close,
  1132. };
  1133. /* dummy redirector format (used directly in av_open_input_file now) */
  1134. static int redir_probe(AVProbeData *pd)
  1135. {
  1136. const char *p;
  1137. p = pd->buf;
  1138. while (redir_isspace(*p))
  1139. p++;
  1140. if (strstart(p, "http://", NULL) ||
  1141. strstart(p, "rtsp://", NULL))
  1142. return AVPROBE_SCORE_MAX;
  1143. return 0;
  1144. }
  1145. /* called from utils.c */
  1146. int redir_open(AVFormatContext **ic_ptr, ByteIOContext *f)
  1147. {
  1148. char buf[4096], *q;
  1149. int c;
  1150. AVFormatContext *ic = NULL;
  1151. /* parse each URL and try to open it */
  1152. c = url_fgetc(f);
  1153. while (c != URL_EOF) {
  1154. /* skip spaces */
  1155. for(;;) {
  1156. if (!redir_isspace(c))
  1157. break;
  1158. c = url_fgetc(f);
  1159. }
  1160. if (c == URL_EOF)
  1161. break;
  1162. /* record url */
  1163. q = buf;
  1164. for(;;) {
  1165. if (c == URL_EOF || redir_isspace(c))
  1166. break;
  1167. if ((q - buf) < sizeof(buf) - 1)
  1168. *q++ = c;
  1169. c = url_fgetc(f);
  1170. }
  1171. *q = '\0';
  1172. //printf("URL='%s'\n", buf);
  1173. /* try to open the media file */
  1174. if (av_open_input_file(&ic, buf, NULL, 0, NULL) == 0)
  1175. break;
  1176. }
  1177. *ic_ptr = ic;
  1178. if (!ic)
  1179. return AVERROR_IO;
  1180. else
  1181. return 0;
  1182. }
  1183. AVInputFormat redir_demux = {
  1184. "redir",
  1185. "Redirector format",
  1186. 0,
  1187. redir_probe,
  1188. NULL,
  1189. NULL,
  1190. NULL,
  1191. };
  1192. int rtsp_init(void)
  1193. {
  1194. av_register_input_format(&rtsp_demux);
  1195. av_register_input_format(&redir_demux);
  1196. av_register_input_format(&sdp_demux);
  1197. return 0;
  1198. }