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.

1490 lines
44KB

  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. get_word_sep(profile, sizeof(profile), "/;,", &p);
  549. lower_transport[0] = '\0';
  550. if (*p == '/') {
  551. p++;
  552. get_word_sep(lower_transport, sizeof(lower_transport),
  553. ";,", &p);
  554. }
  555. if (!strcasecmp(lower_transport, "TCP"))
  556. th->protocol = RTSP_PROTOCOL_RTP_TCP;
  557. else
  558. th->protocol = RTSP_PROTOCOL_RTP_UDP;
  559. if (*p == ';')
  560. p++;
  561. /* get each parameter */
  562. while (*p != '\0' && *p != ',') {
  563. get_word_sep(parameter, sizeof(parameter), "=;,", &p);
  564. if (!strcmp(parameter, "port")) {
  565. if (*p == '=') {
  566. p++;
  567. rtsp_parse_range(&th->port_min, &th->port_max, &p);
  568. }
  569. } else if (!strcmp(parameter, "client_port")) {
  570. if (*p == '=') {
  571. p++;
  572. rtsp_parse_range(&th->client_port_min,
  573. &th->client_port_max, &p);
  574. }
  575. } else if (!strcmp(parameter, "server_port")) {
  576. if (*p == '=') {
  577. p++;
  578. rtsp_parse_range(&th->server_port_min,
  579. &th->server_port_max, &p);
  580. }
  581. } else if (!strcmp(parameter, "interleaved")) {
  582. if (*p == '=') {
  583. p++;
  584. rtsp_parse_range(&th->interleaved_min,
  585. &th->interleaved_max, &p);
  586. }
  587. } else if (!strcmp(parameter, "multicast")) {
  588. if (th->protocol == RTSP_PROTOCOL_RTP_UDP)
  589. th->protocol = RTSP_PROTOCOL_RTP_UDP_MULTICAST;
  590. } else if (!strcmp(parameter, "ttl")) {
  591. if (*p == '=') {
  592. p++;
  593. th->ttl = strtol(p, (char **)&p, 10);
  594. }
  595. } else if (!strcmp(parameter, "destination")) {
  596. struct in_addr ipaddr;
  597. if (*p == '=') {
  598. p++;
  599. get_word_sep(buf, sizeof(buf), ";,", &p);
  600. if (inet_aton(buf, &ipaddr))
  601. th->destination = ntohl(ipaddr.s_addr);
  602. }
  603. }
  604. while (*p != ';' && *p != '\0' && *p != ',')
  605. p++;
  606. if (*p == ';')
  607. p++;
  608. }
  609. if (*p == ',')
  610. p++;
  611. reply->nb_transports++;
  612. }
  613. }
  614. void rtsp_parse_line(RTSPHeader *reply, const char *buf)
  615. {
  616. const char *p;
  617. /* NOTE: we do case independent match for broken servers */
  618. p = buf;
  619. if (av_stristart(p, "Session:", &p)) {
  620. get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
  621. } else if (av_stristart(p, "Content-Length:", &p)) {
  622. reply->content_length = strtol(p, NULL, 10);
  623. } else if (av_stristart(p, "Transport:", &p)) {
  624. rtsp_parse_transport(reply, p);
  625. } else if (av_stristart(p, "CSeq:", &p)) {
  626. reply->seq = strtol(p, NULL, 10);
  627. } else if (av_stristart(p, "Range:", &p)) {
  628. rtsp_parse_range_npt(p, &reply->range_start, &reply->range_end);
  629. }
  630. }
  631. static int url_readbuf(URLContext *h, unsigned char *buf, int size)
  632. {
  633. int ret, len;
  634. len = 0;
  635. while (len < size) {
  636. ret = url_read(h, buf+len, size-len);
  637. if (ret < 1)
  638. return ret;
  639. len += ret;
  640. }
  641. return len;
  642. }
  643. /* skip a RTP/TCP interleaved packet */
  644. static void rtsp_skip_packet(AVFormatContext *s)
  645. {
  646. RTSPState *rt = s->priv_data;
  647. int ret, len, len1;
  648. uint8_t buf[1024];
  649. ret = url_readbuf(rt->rtsp_hd, buf, 3);
  650. if (ret != 3)
  651. return;
  652. len = AV_RB16(buf + 1);
  653. #ifdef DEBUG
  654. printf("skipping RTP packet len=%d\n", len);
  655. #endif
  656. /* skip payload */
  657. while (len > 0) {
  658. len1 = len;
  659. if (len1 > sizeof(buf))
  660. len1 = sizeof(buf);
  661. ret = url_readbuf(rt->rtsp_hd, buf, len1);
  662. if (ret != len1)
  663. return;
  664. len -= len1;
  665. }
  666. }
  667. static void rtsp_send_cmd(AVFormatContext *s,
  668. const char *cmd, RTSPHeader *reply,
  669. unsigned char **content_ptr)
  670. {
  671. RTSPState *rt = s->priv_data;
  672. char buf[4096], buf1[1024], *q;
  673. unsigned char ch;
  674. const char *p;
  675. int content_length, line_count;
  676. unsigned char *content = NULL;
  677. memset(reply, 0, sizeof(RTSPHeader));
  678. rt->seq++;
  679. av_strlcpy(buf, cmd, sizeof(buf));
  680. snprintf(buf1, sizeof(buf1), "CSeq: %d\r\n", rt->seq);
  681. av_strlcat(buf, buf1, sizeof(buf));
  682. if (rt->session_id[0] != '\0' && !strstr(cmd, "\nIf-Match:")) {
  683. snprintf(buf1, sizeof(buf1), "Session: %s\r\n", rt->session_id);
  684. av_strlcat(buf, buf1, sizeof(buf));
  685. }
  686. av_strlcat(buf, "\r\n", sizeof(buf));
  687. #ifdef DEBUG
  688. printf("Sending:\n%s--\n", buf);
  689. #endif
  690. url_write(rt->rtsp_hd, buf, strlen(buf));
  691. /* parse reply (XXX: use buffers) */
  692. line_count = 0;
  693. rt->last_reply[0] = '\0';
  694. for(;;) {
  695. q = buf;
  696. for(;;) {
  697. if (url_readbuf(rt->rtsp_hd, &ch, 1) != 1)
  698. break;
  699. if (ch == '\n')
  700. break;
  701. if (ch == '$') {
  702. /* XXX: only parse it if first char on line ? */
  703. rtsp_skip_packet(s);
  704. } else if (ch != '\r') {
  705. if ((q - buf) < sizeof(buf) - 1)
  706. *q++ = ch;
  707. }
  708. }
  709. *q = '\0';
  710. #ifdef DEBUG
  711. printf("line='%s'\n", buf);
  712. #endif
  713. /* test if last line */
  714. if (buf[0] == '\0')
  715. break;
  716. p = buf;
  717. if (line_count == 0) {
  718. /* get reply code */
  719. get_word(buf1, sizeof(buf1), &p);
  720. get_word(buf1, sizeof(buf1), &p);
  721. reply->status_code = atoi(buf1);
  722. } else {
  723. rtsp_parse_line(reply, p);
  724. av_strlcat(rt->last_reply, p, sizeof(rt->last_reply));
  725. av_strlcat(rt->last_reply, "\n", sizeof(rt->last_reply));
  726. }
  727. line_count++;
  728. }
  729. if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0')
  730. av_strlcpy(rt->session_id, reply->session_id, sizeof(rt->session_id));
  731. content_length = reply->content_length;
  732. if (content_length > 0) {
  733. /* leave some room for a trailing '\0' (useful for simple parsing) */
  734. content = av_malloc(content_length + 1);
  735. (void)url_readbuf(rt->rtsp_hd, content, content_length);
  736. content[content_length] = '\0';
  737. }
  738. if (content_ptr)
  739. *content_ptr = content;
  740. else
  741. av_free(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], *option_list, *option;
  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 = 0;
  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. /* search for options */
  780. option_list = strchr(path, '?');
  781. if (option_list) {
  782. /* remove the options from the path */
  783. *option_list++ = 0;
  784. while(option_list) {
  785. /* move the option pointer */
  786. option = option_list;
  787. option_list = strchr(option_list, '&');
  788. if (option_list)
  789. *(option_list++) = 0;
  790. /* handle the options */
  791. if (strcmp(option, "udp") == 0)
  792. protocol_mask = (1<< RTSP_PROTOCOL_RTP_UDP);
  793. else if (strcmp(option, "multicast") == 0)
  794. protocol_mask = (1<< RTSP_PROTOCOL_RTP_UDP_MULTICAST);
  795. else if (strcmp(option, "tcp") == 0)
  796. protocol_mask = (1<< RTSP_PROTOCOL_RTP_TCP);
  797. }
  798. }
  799. if (!protocol_mask)
  800. protocol_mask = rtsp_default_protocols;
  801. /* open the tcp connexion */
  802. snprintf(tcpname, sizeof(tcpname), "tcp://%s:%d", host, port);
  803. if (url_open(&rtsp_hd, tcpname, URL_RDWR) < 0)
  804. return AVERROR(EIO);
  805. rt->rtsp_hd = rtsp_hd;
  806. rt->seq = 0;
  807. /* describe the stream */
  808. snprintf(cmd, sizeof(cmd),
  809. "DESCRIBE %s RTSP/1.0\r\n"
  810. "Accept: application/sdp\r\n",
  811. s->filename);
  812. rtsp_send_cmd(s, cmd, reply, &content);
  813. if (!content) {
  814. err = AVERROR_INVALIDDATA;
  815. goto fail;
  816. }
  817. if (reply->status_code != RTSP_STATUS_OK) {
  818. err = AVERROR_INVALIDDATA;
  819. goto fail;
  820. }
  821. /* now we got the SDP description, we parse it */
  822. ret = sdp_parse(s, (const char *)content);
  823. av_freep(&content);
  824. if (ret < 0) {
  825. err = AVERROR_INVALIDDATA;
  826. goto fail;
  827. }
  828. /* for each stream, make the setup request */
  829. /* XXX: we assume the same server is used for the control of each
  830. RTSP stream */
  831. for(j = RTSP_RTP_PORT_MIN, i = 0; i < rt->nb_rtsp_streams; ++i) {
  832. char transport[2048];
  833. rtsp_st = rt->rtsp_streams[i];
  834. /* compute available transports */
  835. transport[0] = '\0';
  836. /* RTP/UDP */
  837. if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP)) {
  838. char buf[256];
  839. /* first try in specified port range */
  840. if (RTSP_RTP_PORT_MIN != 0) {
  841. while(j <= RTSP_RTP_PORT_MAX) {
  842. snprintf(buf, sizeof(buf), "rtp://%s?localport=%d", host, j);
  843. j += 2; /* we will use two port by rtp stream (rtp and rtcp) */
  844. if (url_open(&rtsp_st->rtp_handle, buf, URL_RDWR) == 0) {
  845. goto rtp_opened;
  846. }
  847. }
  848. }
  849. /* then try on any port
  850. ** if (url_open(&rtsp_st->rtp_handle, "rtp://", URL_RDONLY) < 0) {
  851. ** err = AVERROR_INVALIDDATA;
  852. ** goto fail;
  853. ** }
  854. */
  855. rtp_opened:
  856. port = rtp_get_local_port(rtsp_st->rtp_handle);
  857. if (transport[0] != '\0')
  858. av_strlcat(transport, ",", sizeof(transport));
  859. snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
  860. "RTP/AVP/UDP;unicast;client_port=%d-%d",
  861. port, port + 1);
  862. }
  863. /* RTP/TCP */
  864. else if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_TCP)) {
  865. if (transport[0] != '\0')
  866. av_strlcat(transport, ",", sizeof(transport));
  867. snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
  868. "RTP/AVP/TCP");
  869. }
  870. else if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP_MULTICAST)) {
  871. if (transport[0] != '\0')
  872. av_strlcat(transport, ",", sizeof(transport));
  873. snprintf(transport + strlen(transport),
  874. sizeof(transport) - strlen(transport) - 1,
  875. "RTP/AVP/UDP;multicast");
  876. }
  877. snprintf(cmd, sizeof(cmd),
  878. "SETUP %s RTSP/1.0\r\n"
  879. "Transport: %s\r\n",
  880. rtsp_st->control_url, transport);
  881. rtsp_send_cmd(s, cmd, reply, NULL);
  882. if (reply->status_code != RTSP_STATUS_OK ||
  883. reply->nb_transports != 1) {
  884. err = AVERROR_INVALIDDATA;
  885. goto fail;
  886. }
  887. /* XXX: same protocol for all streams is required */
  888. if (i > 0) {
  889. if (reply->transports[0].protocol != rt->protocol) {
  890. err = AVERROR_INVALIDDATA;
  891. goto fail;
  892. }
  893. } else {
  894. rt->protocol = reply->transports[0].protocol;
  895. }
  896. /* close RTP connection if not choosen */
  897. if (reply->transports[0].protocol != RTSP_PROTOCOL_RTP_UDP &&
  898. (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP))) {
  899. url_close(rtsp_st->rtp_handle);
  900. rtsp_st->rtp_handle = NULL;
  901. }
  902. switch(reply->transports[0].protocol) {
  903. case RTSP_PROTOCOL_RTP_TCP:
  904. rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
  905. rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
  906. break;
  907. case RTSP_PROTOCOL_RTP_UDP:
  908. {
  909. char url[1024];
  910. /* XXX: also use address if specified */
  911. snprintf(url, sizeof(url), "rtp://%s:%d",
  912. host, reply->transports[0].server_port_min);
  913. if (rtp_set_remote_url(rtsp_st->rtp_handle, url) < 0) {
  914. err = AVERROR_INVALIDDATA;
  915. goto fail;
  916. }
  917. }
  918. break;
  919. case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
  920. {
  921. char url[1024];
  922. struct in_addr in;
  923. in.s_addr = htonl(reply->transports[0].destination);
  924. snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
  925. inet_ntoa(in),
  926. reply->transports[0].port_min,
  927. reply->transports[0].ttl);
  928. if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
  929. err = AVERROR_INVALIDDATA;
  930. goto fail;
  931. }
  932. }
  933. break;
  934. }
  935. /* open the RTP context */
  936. st = NULL;
  937. if (rtsp_st->stream_index >= 0)
  938. st = s->streams[rtsp_st->stream_index];
  939. if (!st)
  940. s->ctx_flags |= AVFMTCTX_NOHEADER;
  941. rtsp_st->rtp_ctx = rtp_parse_open(s, st, rtsp_st->rtp_handle, rtsp_st->sdp_payload_type, &rtsp_st->rtp_payload_data);
  942. if (!rtsp_st->rtp_ctx) {
  943. err = AVERROR(ENOMEM);
  944. goto fail;
  945. } else {
  946. if(rtsp_st->dynamic_handler) {
  947. rtsp_st->rtp_ctx->dynamic_protocol_context= rtsp_st->dynamic_protocol_context;
  948. rtsp_st->rtp_ctx->parse_packet= rtsp_st->dynamic_handler->parse_packet;
  949. }
  950. }
  951. }
  952. rt->state = RTSP_STATE_IDLE;
  953. rt->seek_timestamp = 0; /* default is to start stream at position
  954. zero */
  955. if (ap->initial_pause) {
  956. /* do not start immediately */
  957. } else {
  958. if (rtsp_read_play(s) < 0) {
  959. err = AVERROR_INVALIDDATA;
  960. goto fail;
  961. }
  962. }
  963. return 0;
  964. fail:
  965. rtsp_close_streams(rt);
  966. av_freep(&content);
  967. url_close(rt->rtsp_hd);
  968. return err;
  969. }
  970. static int tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  971. uint8_t *buf, int buf_size)
  972. {
  973. RTSPState *rt = s->priv_data;
  974. int id, len, i, ret;
  975. RTSPStream *rtsp_st;
  976. #ifdef DEBUG_RTP_TCP
  977. printf("tcp_read_packet:\n");
  978. #endif
  979. redo:
  980. for(;;) {
  981. ret = url_readbuf(rt->rtsp_hd, buf, 1);
  982. #ifdef DEBUG_RTP_TCP
  983. printf("ret=%d c=%02x [%c]\n", ret, buf[0], buf[0]);
  984. #endif
  985. if (ret != 1)
  986. return -1;
  987. if (buf[0] == '$')
  988. break;
  989. }
  990. ret = url_readbuf(rt->rtsp_hd, buf, 3);
  991. if (ret != 3)
  992. return -1;
  993. id = buf[0];
  994. len = AV_RB16(buf + 1);
  995. #ifdef DEBUG_RTP_TCP
  996. printf("id=%d len=%d\n", id, len);
  997. #endif
  998. if (len > buf_size || len < 12)
  999. goto redo;
  1000. /* get the data */
  1001. ret = url_readbuf(rt->rtsp_hd, buf, len);
  1002. if (ret != len)
  1003. return -1;
  1004. /* find the matching stream */
  1005. for(i = 0; i < rt->nb_rtsp_streams; i++) {
  1006. rtsp_st = rt->rtsp_streams[i];
  1007. if (id >= rtsp_st->interleaved_min &&
  1008. id <= rtsp_st->interleaved_max)
  1009. goto found;
  1010. }
  1011. goto redo;
  1012. found:
  1013. *prtsp_st = rtsp_st;
  1014. return len;
  1015. }
  1016. static int udp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  1017. uint8_t *buf, int buf_size)
  1018. {
  1019. RTSPState *rt = s->priv_data;
  1020. RTSPStream *rtsp_st;
  1021. fd_set rfds;
  1022. int fd1, fd2, fd_max, n, i, ret;
  1023. struct timeval tv;
  1024. for(;;) {
  1025. if (url_interrupt_cb())
  1026. return AVERROR(EINTR);
  1027. FD_ZERO(&rfds);
  1028. fd_max = -1;
  1029. for(i = 0; i < rt->nb_rtsp_streams; i++) {
  1030. rtsp_st = rt->rtsp_streams[i];
  1031. /* currently, we cannot probe RTCP handle because of blocking restrictions */
  1032. rtp_get_file_handles(rtsp_st->rtp_handle, &fd1, &fd2);
  1033. if (fd1 > fd_max)
  1034. fd_max = fd1;
  1035. FD_SET(fd1, &rfds);
  1036. }
  1037. tv.tv_sec = 0;
  1038. tv.tv_usec = 100 * 1000;
  1039. n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
  1040. if (n > 0) {
  1041. for(i = 0; i < rt->nb_rtsp_streams; i++) {
  1042. rtsp_st = rt->rtsp_streams[i];
  1043. rtp_get_file_handles(rtsp_st->rtp_handle, &fd1, &fd2);
  1044. if (FD_ISSET(fd1, &rfds)) {
  1045. ret = url_read(rtsp_st->rtp_handle, buf, buf_size);
  1046. if (ret > 0) {
  1047. *prtsp_st = rtsp_st;
  1048. return ret;
  1049. }
  1050. }
  1051. }
  1052. }
  1053. }
  1054. }
  1055. static int rtsp_read_packet(AVFormatContext *s,
  1056. AVPacket *pkt)
  1057. {
  1058. RTSPState *rt = s->priv_data;
  1059. RTSPStream *rtsp_st;
  1060. int ret, len;
  1061. uint8_t buf[RTP_MAX_PACKET_LENGTH];
  1062. /* get next frames from the same RTP packet */
  1063. if (rt->cur_rtp) {
  1064. ret = rtp_parse_packet(rt->cur_rtp, pkt, NULL, 0);
  1065. if (ret == 0) {
  1066. rt->cur_rtp = NULL;
  1067. return 0;
  1068. } else if (ret == 1) {
  1069. return 0;
  1070. } else {
  1071. rt->cur_rtp = NULL;
  1072. }
  1073. }
  1074. /* read next RTP packet */
  1075. redo:
  1076. switch(rt->protocol) {
  1077. default:
  1078. case RTSP_PROTOCOL_RTP_TCP:
  1079. len = tcp_read_packet(s, &rtsp_st, buf, sizeof(buf));
  1080. break;
  1081. case RTSP_PROTOCOL_RTP_UDP:
  1082. case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
  1083. len = udp_read_packet(s, &rtsp_st, buf, sizeof(buf));
  1084. if (len >=0 && rtsp_st->rtp_ctx)
  1085. rtp_check_and_send_back_rr(rtsp_st->rtp_ctx, len);
  1086. break;
  1087. }
  1088. if (len < 0)
  1089. return len;
  1090. ret = rtp_parse_packet(rtsp_st->rtp_ctx, pkt, buf, len);
  1091. if (ret < 0)
  1092. goto redo;
  1093. if (ret == 1) {
  1094. /* more packets may follow, so we save the RTP context */
  1095. rt->cur_rtp = rtsp_st->rtp_ctx;
  1096. }
  1097. return 0;
  1098. }
  1099. static int rtsp_read_play(AVFormatContext *s)
  1100. {
  1101. RTSPState *rt = s->priv_data;
  1102. RTSPHeader reply1, *reply = &reply1;
  1103. char cmd[1024];
  1104. av_log(s, AV_LOG_DEBUG, "hello state=%d\n", rt->state);
  1105. if (rt->state == RTSP_STATE_PAUSED) {
  1106. snprintf(cmd, sizeof(cmd),
  1107. "PLAY %s RTSP/1.0\r\n",
  1108. s->filename);
  1109. } else {
  1110. snprintf(cmd, sizeof(cmd),
  1111. "PLAY %s RTSP/1.0\r\n"
  1112. "Range: npt=%0.3f-\r\n",
  1113. s->filename,
  1114. (double)rt->seek_timestamp / AV_TIME_BASE);
  1115. }
  1116. rtsp_send_cmd(s, cmd, reply, NULL);
  1117. if (reply->status_code != RTSP_STATUS_OK) {
  1118. return -1;
  1119. } else {
  1120. rt->state = RTSP_STATE_PLAYING;
  1121. return 0;
  1122. }
  1123. }
  1124. /* pause the stream */
  1125. static int rtsp_read_pause(AVFormatContext *s)
  1126. {
  1127. RTSPState *rt = s->priv_data;
  1128. RTSPHeader reply1, *reply = &reply1;
  1129. char cmd[1024];
  1130. rt = s->priv_data;
  1131. if (rt->state != RTSP_STATE_PLAYING)
  1132. return 0;
  1133. snprintf(cmd, sizeof(cmd),
  1134. "PAUSE %s RTSP/1.0\r\n",
  1135. s->filename);
  1136. rtsp_send_cmd(s, cmd, reply, NULL);
  1137. if (reply->status_code != RTSP_STATUS_OK) {
  1138. return -1;
  1139. } else {
  1140. rt->state = RTSP_STATE_PAUSED;
  1141. return 0;
  1142. }
  1143. }
  1144. static int rtsp_read_seek(AVFormatContext *s, int stream_index,
  1145. int64_t timestamp, int flags)
  1146. {
  1147. RTSPState *rt = s->priv_data;
  1148. rt->seek_timestamp = av_rescale_q(timestamp, s->streams[stream_index]->time_base, AV_TIME_BASE_Q);
  1149. switch(rt->state) {
  1150. default:
  1151. case RTSP_STATE_IDLE:
  1152. break;
  1153. case RTSP_STATE_PLAYING:
  1154. if (rtsp_read_play(s) != 0)
  1155. return -1;
  1156. break;
  1157. case RTSP_STATE_PAUSED:
  1158. rt->state = RTSP_STATE_IDLE;
  1159. break;
  1160. }
  1161. return 0;
  1162. }
  1163. static int rtsp_read_close(AVFormatContext *s)
  1164. {
  1165. RTSPState *rt = s->priv_data;
  1166. RTSPHeader reply1, *reply = &reply1;
  1167. char cmd[1024];
  1168. #if 0
  1169. /* NOTE: it is valid to flush the buffer here */
  1170. if (rt->protocol == RTSP_PROTOCOL_RTP_TCP) {
  1171. url_fclose(&rt->rtsp_gb);
  1172. }
  1173. #endif
  1174. snprintf(cmd, sizeof(cmd),
  1175. "TEARDOWN %s RTSP/1.0\r\n",
  1176. s->filename);
  1177. rtsp_send_cmd(s, cmd, reply, NULL);
  1178. rtsp_close_streams(rt);
  1179. url_close(rt->rtsp_hd);
  1180. return 0;
  1181. }
  1182. #ifdef CONFIG_RTSP_DEMUXER
  1183. AVInputFormat rtsp_demuxer = {
  1184. "rtsp",
  1185. "RTSP input format",
  1186. sizeof(RTSPState),
  1187. rtsp_probe,
  1188. rtsp_read_header,
  1189. rtsp_read_packet,
  1190. rtsp_read_close,
  1191. rtsp_read_seek,
  1192. .flags = AVFMT_NOFILE,
  1193. .read_play = rtsp_read_play,
  1194. .read_pause = rtsp_read_pause,
  1195. };
  1196. #endif
  1197. static int sdp_probe(AVProbeData *p1)
  1198. {
  1199. const char *p = p1->buf, *p_end = p1->buf + p1->buf_size;
  1200. /* we look for a line beginning "c=IN IP4" */
  1201. while (p < p_end && *p != '\0') {
  1202. if (p + sizeof("c=IN IP4") - 1 < p_end && av_strstart(p, "c=IN IP4", NULL))
  1203. return AVPROBE_SCORE_MAX / 2;
  1204. while(p < p_end - 1 && *p != '\n') p++;
  1205. if (++p >= p_end)
  1206. break;
  1207. if (*p == '\r')
  1208. p++;
  1209. }
  1210. return 0;
  1211. }
  1212. #define SDP_MAX_SIZE 8192
  1213. static int sdp_read_header(AVFormatContext *s,
  1214. AVFormatParameters *ap)
  1215. {
  1216. RTSPState *rt = s->priv_data;
  1217. RTSPStream *rtsp_st;
  1218. int size, i, err;
  1219. char *content;
  1220. char url[1024];
  1221. AVStream *st;
  1222. /* read the whole sdp file */
  1223. /* XXX: better loading */
  1224. content = av_malloc(SDP_MAX_SIZE);
  1225. size = get_buffer(s->pb, content, SDP_MAX_SIZE - 1);
  1226. if (size <= 0) {
  1227. av_free(content);
  1228. return AVERROR_INVALIDDATA;
  1229. }
  1230. content[size] ='\0';
  1231. sdp_parse(s, content);
  1232. av_free(content);
  1233. /* open each RTP stream */
  1234. for(i=0;i<rt->nb_rtsp_streams;i++) {
  1235. rtsp_st = rt->rtsp_streams[i];
  1236. snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
  1237. inet_ntoa(rtsp_st->sdp_ip),
  1238. rtsp_st->sdp_port,
  1239. rtsp_st->sdp_ttl);
  1240. if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
  1241. err = AVERROR_INVALIDDATA;
  1242. goto fail;
  1243. }
  1244. /* open the RTP context */
  1245. st = NULL;
  1246. if (rtsp_st->stream_index >= 0)
  1247. st = s->streams[rtsp_st->stream_index];
  1248. if (!st)
  1249. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1250. rtsp_st->rtp_ctx = rtp_parse_open(s, st, rtsp_st->rtp_handle, rtsp_st->sdp_payload_type, &rtsp_st->rtp_payload_data);
  1251. if (!rtsp_st->rtp_ctx) {
  1252. err = AVERROR(ENOMEM);
  1253. goto fail;
  1254. } else {
  1255. if(rtsp_st->dynamic_handler) {
  1256. rtsp_st->rtp_ctx->dynamic_protocol_context= rtsp_st->dynamic_protocol_context;
  1257. rtsp_st->rtp_ctx->parse_packet= rtsp_st->dynamic_handler->parse_packet;
  1258. }
  1259. }
  1260. }
  1261. return 0;
  1262. fail:
  1263. rtsp_close_streams(rt);
  1264. return err;
  1265. }
  1266. static int sdp_read_packet(AVFormatContext *s,
  1267. AVPacket *pkt)
  1268. {
  1269. return rtsp_read_packet(s, pkt);
  1270. }
  1271. static int sdp_read_close(AVFormatContext *s)
  1272. {
  1273. RTSPState *rt = s->priv_data;
  1274. rtsp_close_streams(rt);
  1275. return 0;
  1276. }
  1277. #ifdef CONFIG_SDP_DEMUXER
  1278. AVInputFormat sdp_demuxer = {
  1279. "sdp",
  1280. "SDP",
  1281. sizeof(RTSPState),
  1282. sdp_probe,
  1283. sdp_read_header,
  1284. sdp_read_packet,
  1285. sdp_read_close,
  1286. };
  1287. #endif
  1288. #ifdef CONFIG_REDIR_DEMUXER
  1289. /* dummy redirector format (used directly in av_open_input_file now) */
  1290. static int redir_probe(AVProbeData *pd)
  1291. {
  1292. const char *p;
  1293. p = pd->buf;
  1294. while (redir_isspace(*p))
  1295. p++;
  1296. if (av_strstart(p, "http://", NULL) ||
  1297. av_strstart(p, "rtsp://", NULL))
  1298. return AVPROBE_SCORE_MAX;
  1299. return 0;
  1300. }
  1301. static int redir_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1302. {
  1303. char buf[4096], *q;
  1304. int c;
  1305. AVFormatContext *ic = NULL;
  1306. ByteIOContext *f = s->pb;
  1307. /* parse each URL and try to open it */
  1308. c = url_fgetc(f);
  1309. while (c != URL_EOF) {
  1310. /* skip spaces */
  1311. for(;;) {
  1312. if (!redir_isspace(c))
  1313. break;
  1314. c = url_fgetc(f);
  1315. }
  1316. if (c == URL_EOF)
  1317. break;
  1318. /* record url */
  1319. q = buf;
  1320. for(;;) {
  1321. if (c == URL_EOF || redir_isspace(c))
  1322. break;
  1323. if ((q - buf) < sizeof(buf) - 1)
  1324. *q++ = c;
  1325. c = url_fgetc(f);
  1326. }
  1327. *q = '\0';
  1328. //printf("URL='%s'\n", buf);
  1329. /* try to open the media file */
  1330. if (av_open_input_file(&ic, buf, NULL, 0, NULL) == 0)
  1331. break;
  1332. }
  1333. if (!ic)
  1334. return AVERROR(EIO);
  1335. *s = *ic;
  1336. url_fclose(f);
  1337. return 0;
  1338. }
  1339. AVInputFormat redir_demuxer = {
  1340. "redir",
  1341. "Redirector format",
  1342. 0,
  1343. redir_probe,
  1344. redir_read_header,
  1345. NULL,
  1346. NULL,
  1347. };
  1348. #endif