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.

1424 lines
41KB

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