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.

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