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.

1430 lines
41KB

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