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.

1468 lines
43KB

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