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.

1455 lines
42KB

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