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.

1497 lines
45KB

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