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.

1503 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. // TODO (Replace with rtsp_next_attr_and_value)
  287. /* loop on each attribute */
  288. for(;;) {
  289. skip_spaces(&p);
  290. if (*p == '\0')
  291. break;
  292. get_word_sep(attr, sizeof(attr), "=", &p);
  293. if (*p == '=')
  294. p++;
  295. get_word_sep(value, sizeof(value), ";", &p);
  296. if (*p == ';')
  297. p++;
  298. /* grab the codec extra_data from the config parameter of the fmtp line */
  299. sdp_parse_fmtp_config(codec, attr, value);
  300. /* Looking for a known attribute */
  301. for (i = 0; attr_names[i].str; ++i) {
  302. if (!strcasecmp(attr, attr_names[i].str)) {
  303. if (attr_names[i].type == ATTR_NAME_TYPE_INT)
  304. *(int *)((char *)rtp_payload_data + attr_names[i].offset) = atoi(value);
  305. else if (attr_names[i].type == ATTR_NAME_TYPE_STR)
  306. *(char **)((char *)rtp_payload_data + attr_names[i].offset) = av_strdup(value);
  307. }
  308. }
  309. }
  310. }
  311. /** Parse a string \p in the form of Range:npt=xx-xx, and determine the start
  312. * and end time.
  313. * Used for seeking in the rtp stream.
  314. */
  315. static void rtsp_parse_range_npt(const char *p, int64_t *start, int64_t *end)
  316. {
  317. char buf[256];
  318. skip_spaces(&p);
  319. if (!stristart(p, "npt=", &p))
  320. return;
  321. *start = AV_NOPTS_VALUE;
  322. *end = AV_NOPTS_VALUE;
  323. get_word_sep(buf, sizeof(buf), "-", &p);
  324. *start = parse_date(buf, 1);
  325. if (*p == '-') {
  326. p++;
  327. get_word_sep(buf, sizeof(buf), "-", &p);
  328. *end = parse_date(buf, 1);
  329. }
  330. // av_log(NULL, AV_LOG_DEBUG, "Range Start: %lld\n", *start);
  331. // av_log(NULL, AV_LOG_DEBUG, "Range End: %lld\n", *end);
  332. }
  333. typedef struct SDPParseState {
  334. /* SDP only */
  335. struct in_addr default_ip;
  336. int default_ttl;
  337. } SDPParseState;
  338. static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1,
  339. int letter, const char *buf)
  340. {
  341. RTSPState *rt = s->priv_data;
  342. char buf1[64], st_type[64];
  343. const char *p;
  344. int codec_type, payload_type, i;
  345. AVStream *st;
  346. RTSPStream *rtsp_st;
  347. struct in_addr sdp_ip;
  348. int ttl;
  349. #ifdef DEBUG
  350. printf("sdp: %c='%s'\n", letter, buf);
  351. #endif
  352. p = buf;
  353. switch(letter) {
  354. case 'c':
  355. get_word(buf1, sizeof(buf1), &p);
  356. if (strcmp(buf1, "IN") != 0)
  357. return;
  358. get_word(buf1, sizeof(buf1), &p);
  359. if (strcmp(buf1, "IP4") != 0)
  360. return;
  361. get_word_sep(buf1, sizeof(buf1), "/", &p);
  362. if (inet_aton(buf1, &sdp_ip) == 0)
  363. return;
  364. ttl = 16;
  365. if (*p == '/') {
  366. p++;
  367. get_word_sep(buf1, sizeof(buf1), "/", &p);
  368. ttl = atoi(buf1);
  369. }
  370. if (s->nb_streams == 0) {
  371. s1->default_ip = sdp_ip;
  372. s1->default_ttl = ttl;
  373. } else {
  374. st = s->streams[s->nb_streams - 1];
  375. rtsp_st = st->priv_data;
  376. rtsp_st->sdp_ip = sdp_ip;
  377. rtsp_st->sdp_ttl = ttl;
  378. }
  379. break;
  380. case 's':
  381. pstrcpy(s->title, sizeof(s->title), p);
  382. break;
  383. case 'i':
  384. if (s->nb_streams == 0) {
  385. pstrcpy(s->comment, sizeof(s->comment), p);
  386. break;
  387. }
  388. break;
  389. case 'm':
  390. /* new stream */
  391. get_word(st_type, sizeof(st_type), &p);
  392. if (!strcmp(st_type, "audio")) {
  393. codec_type = CODEC_TYPE_AUDIO;
  394. } else if (!strcmp(st_type, "video")) {
  395. codec_type = CODEC_TYPE_VIDEO;
  396. } else {
  397. return;
  398. }
  399. rtsp_st = av_mallocz(sizeof(RTSPStream));
  400. if (!rtsp_st)
  401. return;
  402. rtsp_st->stream_index = -1;
  403. dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
  404. rtsp_st->sdp_ip = s1->default_ip;
  405. rtsp_st->sdp_ttl = s1->default_ttl;
  406. get_word(buf1, sizeof(buf1), &p); /* port */
  407. rtsp_st->sdp_port = atoi(buf1);
  408. get_word(buf1, sizeof(buf1), &p); /* protocol (ignored) */
  409. /* XXX: handle list of formats */
  410. get_word(buf1, sizeof(buf1), &p); /* format list */
  411. rtsp_st->sdp_payload_type = atoi(buf1);
  412. if (!strcmp(AVRtpPayloadTypes[rtsp_st->sdp_payload_type].enc_name, "MP2T")) {
  413. /* no corresponding stream */
  414. } else {
  415. st = av_new_stream(s, 0);
  416. if (!st)
  417. return;
  418. st->priv_data = rtsp_st;
  419. rtsp_st->stream_index = st->index;
  420. st->codec->codec_type = codec_type;
  421. if (rtsp_st->sdp_payload_type < RTP_PT_PRIVATE) {
  422. /* if standard payload type, we can find the codec right now */
  423. rtp_get_codec_info(st->codec, rtsp_st->sdp_payload_type);
  424. }
  425. }
  426. /* put a default control url */
  427. pstrcpy(rtsp_st->control_url, sizeof(rtsp_st->control_url), s->filename);
  428. break;
  429. case 'a':
  430. if (strstart(p, "control:", &p) && s->nb_streams > 0) {
  431. char proto[32];
  432. /* get the control url */
  433. st = s->streams[s->nb_streams - 1];
  434. rtsp_st = st->priv_data;
  435. /* XXX: may need to add full url resolution */
  436. url_split(proto, sizeof(proto), NULL, 0, NULL, 0, NULL, NULL, 0, p);
  437. if (proto[0] == '\0') {
  438. /* relative control URL */
  439. pstrcat(rtsp_st->control_url, sizeof(rtsp_st->control_url), "/");
  440. pstrcat(rtsp_st->control_url, sizeof(rtsp_st->control_url), p);
  441. } else {
  442. pstrcpy(rtsp_st->control_url, sizeof(rtsp_st->control_url), p);
  443. }
  444. } else if (strstart(p, "rtpmap:", &p)) {
  445. /* NOTE: rtpmap is only supported AFTER the 'm=' tag */
  446. get_word(buf1, sizeof(buf1), &p);
  447. payload_type = atoi(buf1);
  448. for(i = 0; i < s->nb_streams;i++) {
  449. st = s->streams[i];
  450. rtsp_st = st->priv_data;
  451. if (rtsp_st->sdp_payload_type == payload_type) {
  452. sdp_parse_rtpmap(st->codec, rtsp_st, payload_type, p);
  453. }
  454. }
  455. } else if (strstart(p, "fmtp:", &p)) {
  456. /* NOTE: fmtp is only supported AFTER the 'a=rtpmap:xxx' tag */
  457. get_word(buf1, sizeof(buf1), &p);
  458. payload_type = atoi(buf1);
  459. for(i = 0; i < s->nb_streams;i++) {
  460. st = s->streams[i];
  461. rtsp_st = st->priv_data;
  462. if (rtsp_st->sdp_payload_type == payload_type) {
  463. if(rtsp_st->dynamic_handler && rtsp_st->dynamic_handler->parse_sdp_a_line) {
  464. if(!rtsp_st->dynamic_handler->parse_sdp_a_line(st, rtsp_st->dynamic_protocol_context, buf)) {
  465. sdp_parse_fmtp(st, p);
  466. }
  467. } else {
  468. sdp_parse_fmtp(st, p);
  469. }
  470. }
  471. }
  472. } else if(strstart(p, "framesize:", &p)) {
  473. // let dynamic protocol handlers have a stab at the line.
  474. get_word(buf1, sizeof(buf1), &p);
  475. payload_type = atoi(buf1);
  476. for(i = 0; i < s->nb_streams;i++) {
  477. st = s->streams[i];
  478. rtsp_st = st->priv_data;
  479. if (rtsp_st->sdp_payload_type == payload_type) {
  480. if(rtsp_st->dynamic_handler && rtsp_st->dynamic_handler->parse_sdp_a_line) {
  481. rtsp_st->dynamic_handler->parse_sdp_a_line(st, rtsp_st->dynamic_protocol_context, buf);
  482. }
  483. }
  484. }
  485. } else if(strstart(p, "range:", &p)) {
  486. int64_t start, end;
  487. // this is so that seeking on a streamed file can work.
  488. rtsp_parse_range_npt(p, &start, &end);
  489. s->start_time= start;
  490. s->duration= (end==AV_NOPTS_VALUE)?AV_NOPTS_VALUE:end-start; // AV_NOPTS_VALUE means live broadcast (and can't seek)
  491. }
  492. break;
  493. }
  494. }
  495. static int sdp_parse(AVFormatContext *s, const char *content)
  496. {
  497. const char *p;
  498. int letter;
  499. char buf[1024], *q;
  500. SDPParseState sdp_parse_state, *s1 = &sdp_parse_state;
  501. memset(s1, 0, sizeof(SDPParseState));
  502. p = content;
  503. for(;;) {
  504. skip_spaces(&p);
  505. letter = *p;
  506. if (letter == '\0')
  507. break;
  508. p++;
  509. if (*p != '=')
  510. goto next_line;
  511. p++;
  512. /* get the content */
  513. q = buf;
  514. while (*p != '\n' && *p != '\r' && *p != '\0') {
  515. if ((q - buf) < sizeof(buf) - 1)
  516. *q++ = *p;
  517. p++;
  518. }
  519. *q = '\0';
  520. sdp_parse_line(s, s1, letter, buf);
  521. next_line:
  522. while (*p != '\n' && *p != '\0')
  523. p++;
  524. if (*p == '\n')
  525. p++;
  526. }
  527. return 0;
  528. }
  529. static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
  530. {
  531. const char *p;
  532. int v;
  533. p = *pp;
  534. skip_spaces(&p);
  535. v = strtol(p, (char **)&p, 10);
  536. if (*p == '-') {
  537. p++;
  538. *min_ptr = v;
  539. v = strtol(p, (char **)&p, 10);
  540. *max_ptr = v;
  541. } else {
  542. *min_ptr = v;
  543. *max_ptr = v;
  544. }
  545. *pp = p;
  546. }
  547. /* XXX: only one transport specification is parsed */
  548. static void rtsp_parse_transport(RTSPHeader *reply, const char *p)
  549. {
  550. char transport_protocol[16];
  551. char profile[16];
  552. char lower_transport[16];
  553. char parameter[16];
  554. RTSPTransportField *th;
  555. char buf[256];
  556. reply->nb_transports = 0;
  557. for(;;) {
  558. skip_spaces(&p);
  559. if (*p == '\0')
  560. break;
  561. th = &reply->transports[reply->nb_transports];
  562. get_word_sep(transport_protocol, sizeof(transport_protocol),
  563. "/", &p);
  564. if (*p == '/')
  565. p++;
  566. get_word_sep(profile, sizeof(profile), "/;,", &p);
  567. lower_transport[0] = '\0';
  568. if (*p == '/') {
  569. p++;
  570. get_word_sep(lower_transport, sizeof(lower_transport),
  571. ";,", &p);
  572. }
  573. if (!strcasecmp(lower_transport, "TCP"))
  574. th->protocol = RTSP_PROTOCOL_RTP_TCP;
  575. else
  576. th->protocol = RTSP_PROTOCOL_RTP_UDP;
  577. if (*p == ';')
  578. p++;
  579. /* get each parameter */
  580. while (*p != '\0' && *p != ',') {
  581. get_word_sep(parameter, sizeof(parameter), "=;,", &p);
  582. if (!strcmp(parameter, "port")) {
  583. if (*p == '=') {
  584. p++;
  585. rtsp_parse_range(&th->port_min, &th->port_max, &p);
  586. }
  587. } else if (!strcmp(parameter, "client_port")) {
  588. if (*p == '=') {
  589. p++;
  590. rtsp_parse_range(&th->client_port_min,
  591. &th->client_port_max, &p);
  592. }
  593. } else if (!strcmp(parameter, "server_port")) {
  594. if (*p == '=') {
  595. p++;
  596. rtsp_parse_range(&th->server_port_min,
  597. &th->server_port_max, &p);
  598. }
  599. } else if (!strcmp(parameter, "interleaved")) {
  600. if (*p == '=') {
  601. p++;
  602. rtsp_parse_range(&th->interleaved_min,
  603. &th->interleaved_max, &p);
  604. }
  605. } else if (!strcmp(parameter, "multicast")) {
  606. if (th->protocol == RTSP_PROTOCOL_RTP_UDP)
  607. th->protocol = RTSP_PROTOCOL_RTP_UDP_MULTICAST;
  608. } else if (!strcmp(parameter, "ttl")) {
  609. if (*p == '=') {
  610. p++;
  611. th->ttl = strtol(p, (char **)&p, 10);
  612. }
  613. } else if (!strcmp(parameter, "destination")) {
  614. struct in_addr ipaddr;
  615. if (*p == '=') {
  616. p++;
  617. get_word_sep(buf, sizeof(buf), ";,", &p);
  618. if (inet_aton(buf, &ipaddr))
  619. th->destination = ntohl(ipaddr.s_addr);
  620. }
  621. }
  622. while (*p != ';' && *p != '\0' && *p != ',')
  623. p++;
  624. if (*p == ';')
  625. p++;
  626. }
  627. if (*p == ',')
  628. p++;
  629. reply->nb_transports++;
  630. }
  631. }
  632. void rtsp_parse_line(RTSPHeader *reply, const char *buf)
  633. {
  634. const char *p;
  635. /* NOTE: we do case independent match for broken servers */
  636. p = buf;
  637. if (stristart(p, "Session:", &p)) {
  638. get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
  639. } else if (stristart(p, "Content-Length:", &p)) {
  640. reply->content_length = strtol(p, NULL, 10);
  641. } else if (stristart(p, "Transport:", &p)) {
  642. rtsp_parse_transport(reply, p);
  643. } else if (stristart(p, "CSeq:", &p)) {
  644. reply->seq = strtol(p, NULL, 10);
  645. } else if (stristart(p, "Range:", &p)) {
  646. rtsp_parse_range_npt(p, &reply->range_start, &reply->range_end);
  647. }
  648. }
  649. static int url_readbuf(URLContext *h, unsigned char *buf, int size)
  650. {
  651. int ret, len;
  652. len = 0;
  653. while (len < size) {
  654. ret = url_read(h, buf+len, size-len);
  655. if (ret < 1)
  656. return ret;
  657. len += ret;
  658. }
  659. return len;
  660. }
  661. /* skip a RTP/TCP interleaved packet */
  662. static void rtsp_skip_packet(AVFormatContext *s)
  663. {
  664. RTSPState *rt = s->priv_data;
  665. int ret, len, len1;
  666. uint8_t buf[1024];
  667. ret = url_readbuf(rt->rtsp_hd, buf, 3);
  668. if (ret != 3)
  669. return;
  670. len = (buf[1] << 8) | buf[2];
  671. #ifdef DEBUG
  672. printf("skipping RTP packet len=%d\n", len);
  673. #endif
  674. /* skip payload */
  675. while (len > 0) {
  676. len1 = len;
  677. if (len1 > sizeof(buf))
  678. len1 = sizeof(buf);
  679. ret = url_readbuf(rt->rtsp_hd, buf, len1);
  680. if (ret != len1)
  681. return;
  682. len -= len1;
  683. }
  684. }
  685. static void rtsp_send_cmd(AVFormatContext *s,
  686. const char *cmd, RTSPHeader *reply,
  687. unsigned char **content_ptr)
  688. {
  689. RTSPState *rt = s->priv_data;
  690. char buf[4096], buf1[1024], *q;
  691. unsigned char ch;
  692. const char *p;
  693. int content_length, line_count;
  694. unsigned char *content = NULL;
  695. memset(reply, 0, sizeof(RTSPHeader));
  696. rt->seq++;
  697. pstrcpy(buf, sizeof(buf), cmd);
  698. snprintf(buf1, sizeof(buf1), "CSeq: %d\r\n", rt->seq);
  699. pstrcat(buf, sizeof(buf), buf1);
  700. if (rt->session_id[0] != '\0' && !strstr(cmd, "\nIf-Match:")) {
  701. snprintf(buf1, sizeof(buf1), "Session: %s\r\n", rt->session_id);
  702. pstrcat(buf, sizeof(buf), buf1);
  703. }
  704. pstrcat(buf, sizeof(buf), "\r\n");
  705. #ifdef DEBUG
  706. printf("Sending:\n%s--\n", buf);
  707. #endif
  708. url_write(rt->rtsp_hd, buf, strlen(buf));
  709. /* parse reply (XXX: use buffers) */
  710. line_count = 0;
  711. rt->last_reply[0] = '\0';
  712. for(;;) {
  713. q = buf;
  714. for(;;) {
  715. if (url_readbuf(rt->rtsp_hd, &ch, 1) != 1)
  716. break;
  717. if (ch == '\n')
  718. break;
  719. if (ch == '$') {
  720. /* XXX: only parse it if first char on line ? */
  721. rtsp_skip_packet(s);
  722. } else if (ch != '\r') {
  723. if ((q - buf) < sizeof(buf) - 1)
  724. *q++ = ch;
  725. }
  726. }
  727. *q = '\0';
  728. #ifdef DEBUG
  729. printf("line='%s'\n", buf);
  730. #endif
  731. /* test if last line */
  732. if (buf[0] == '\0')
  733. break;
  734. p = buf;
  735. if (line_count == 0) {
  736. /* get reply code */
  737. get_word(buf1, sizeof(buf1), &p);
  738. get_word(buf1, sizeof(buf1), &p);
  739. reply->status_code = atoi(buf1);
  740. } else {
  741. rtsp_parse_line(reply, p);
  742. pstrcat(rt->last_reply, sizeof(rt->last_reply), p);
  743. pstrcat(rt->last_reply, sizeof(rt->last_reply), "\n");
  744. }
  745. line_count++;
  746. }
  747. if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0')
  748. pstrcpy(rt->session_id, sizeof(rt->session_id), reply->session_id);
  749. content_length = reply->content_length;
  750. if (content_length > 0) {
  751. /* leave some room for a trailing '\0' (useful for simple parsing) */
  752. content = av_malloc(content_length + 1);
  753. (void)url_readbuf(rt->rtsp_hd, content, content_length);
  754. content[content_length] = '\0';
  755. }
  756. if (content_ptr)
  757. *content_ptr = content;
  758. }
  759. /* useful for modules: set RTSP callback function */
  760. void rtsp_set_callback(FFRTSPCallback *rtsp_cb)
  761. {
  762. ff_rtsp_callback = rtsp_cb;
  763. }
  764. /* close and free RTSP streams */
  765. static void rtsp_close_streams(RTSPState *rt)
  766. {
  767. int i;
  768. RTSPStream *rtsp_st;
  769. for(i=0;i<rt->nb_rtsp_streams;i++) {
  770. rtsp_st = rt->rtsp_streams[i];
  771. if (rtsp_st) {
  772. if (rtsp_st->rtp_ctx)
  773. rtp_parse_close(rtsp_st->rtp_ctx);
  774. if (rtsp_st->rtp_handle)
  775. url_close(rtsp_st->rtp_handle);
  776. if (rtsp_st->dynamic_handler && rtsp_st->dynamic_protocol_context)
  777. rtsp_st->dynamic_handler->close(rtsp_st->dynamic_protocol_context);
  778. }
  779. av_free(rtsp_st);
  780. }
  781. av_free(rt->rtsp_streams);
  782. }
  783. static int rtsp_read_header(AVFormatContext *s,
  784. AVFormatParameters *ap)
  785. {
  786. RTSPState *rt = s->priv_data;
  787. char host[1024], path[1024], tcpname[1024], cmd[2048];
  788. URLContext *rtsp_hd;
  789. int port, i, j, ret, err;
  790. RTSPHeader reply1, *reply = &reply1;
  791. unsigned char *content = NULL;
  792. RTSPStream *rtsp_st;
  793. int protocol_mask;
  794. AVStream *st;
  795. /* extract hostname and port */
  796. url_split(NULL, 0, NULL, 0,
  797. host, sizeof(host), &port, path, sizeof(path), s->filename);
  798. if (port < 0)
  799. port = RTSP_DEFAULT_PORT;
  800. /* open the tcp connexion */
  801. snprintf(tcpname, sizeof(tcpname), "tcp://%s:%d", host, port);
  802. if (url_open(&rtsp_hd, tcpname, URL_RDWR) < 0)
  803. return AVERROR_IO;
  804. rt->rtsp_hd = rtsp_hd;
  805. rt->seq = 0;
  806. /* describe the stream */
  807. snprintf(cmd, sizeof(cmd),
  808. "DESCRIBE %s RTSP/1.0\r\n"
  809. "Accept: application/sdp\r\n",
  810. s->filename);
  811. rtsp_send_cmd(s, cmd, reply, &content);
  812. if (!content) {
  813. err = AVERROR_INVALIDDATA;
  814. goto fail;
  815. }
  816. if (reply->status_code != RTSP_STATUS_OK) {
  817. err = AVERROR_INVALIDDATA;
  818. goto fail;
  819. }
  820. /* now we got the SDP description, we parse it */
  821. ret = sdp_parse(s, (const char *)content);
  822. av_freep(&content);
  823. if (ret < 0) {
  824. err = AVERROR_INVALIDDATA;
  825. goto fail;
  826. }
  827. protocol_mask = rtsp_default_protocols;
  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://?localport=%d", j);
  843. if (url_open(&rtsp_st->rtp_handle, buf, URL_RDWR) == 0) {
  844. j += 2; /* we will use two port by rtp stream (rtp and rtcp) */
  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. pstrcat(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. pstrcat(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. pstrcat(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. int ttl;
  923. ttl = reply->transports[0].ttl;
  924. if (!ttl)
  925. ttl = 16;
  926. snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
  927. host,
  928. reply->transports[0].server_port_min,
  929. ttl);
  930. if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
  931. err = AVERROR_INVALIDDATA;
  932. goto fail;
  933. }
  934. }
  935. break;
  936. }
  937. /* open the RTP context */
  938. st = NULL;
  939. if (rtsp_st->stream_index >= 0)
  940. st = s->streams[rtsp_st->stream_index];
  941. if (!st)
  942. s->ctx_flags |= AVFMTCTX_NOHEADER;
  943. rtsp_st->rtp_ctx = rtp_parse_open(s, st, rtsp_st->rtp_handle, rtsp_st->sdp_payload_type, &rtsp_st->rtp_payload_data);
  944. if (!rtsp_st->rtp_ctx) {
  945. err = AVERROR_NOMEM;
  946. goto fail;
  947. } else {
  948. if(rtsp_st->dynamic_handler) {
  949. rtsp_st->rtp_ctx->dynamic_protocol_context= rtsp_st->dynamic_protocol_context;
  950. rtsp_st->rtp_ctx->parse_packet= rtsp_st->dynamic_handler->parse_packet;
  951. }
  952. }
  953. }
  954. /* use callback if available to extend setup */
  955. if (ff_rtsp_callback) {
  956. if (ff_rtsp_callback(RTSP_ACTION_CLIENT_SETUP, rt->session_id,
  957. NULL, 0, rt->last_reply) < 0) {
  958. err = AVERROR_INVALIDDATA;
  959. goto fail;
  960. }
  961. }
  962. rt->state = RTSP_STATE_IDLE;
  963. rt->seek_timestamp = 0; /* default is to start stream at position
  964. zero */
  965. if (ap->initial_pause) {
  966. /* do not start immediately */
  967. } else {
  968. if (rtsp_read_play(s) < 0) {
  969. err = AVERROR_INVALIDDATA;
  970. goto fail;
  971. }
  972. }
  973. return 0;
  974. fail:
  975. rtsp_close_streams(rt);
  976. av_freep(&content);
  977. url_close(rt->rtsp_hd);
  978. return err;
  979. }
  980. static int tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  981. uint8_t *buf, int buf_size)
  982. {
  983. RTSPState *rt = s->priv_data;
  984. int id, len, i, ret;
  985. RTSPStream *rtsp_st;
  986. #ifdef DEBUG_RTP_TCP
  987. printf("tcp_read_packet:\n");
  988. #endif
  989. redo:
  990. for(;;) {
  991. ret = url_readbuf(rt->rtsp_hd, buf, 1);
  992. #ifdef DEBUG_RTP_TCP
  993. printf("ret=%d c=%02x [%c]\n", ret, buf[0], buf[0]);
  994. #endif
  995. if (ret != 1)
  996. return -1;
  997. if (buf[0] == '$')
  998. break;
  999. }
  1000. ret = url_readbuf(rt->rtsp_hd, buf, 3);
  1001. if (ret != 3)
  1002. return -1;
  1003. id = buf[0];
  1004. len = (buf[1] << 8) | buf[2];
  1005. #ifdef DEBUG_RTP_TCP
  1006. printf("id=%d len=%d\n", id, len);
  1007. #endif
  1008. if (len > buf_size || len < 12)
  1009. goto redo;
  1010. /* get the data */
  1011. ret = url_readbuf(rt->rtsp_hd, buf, len);
  1012. if (ret != len)
  1013. return -1;
  1014. /* find the matching stream */
  1015. for(i = 0; i < rt->nb_rtsp_streams; i++) {
  1016. rtsp_st = rt->rtsp_streams[i];
  1017. if (id >= rtsp_st->interleaved_min &&
  1018. id <= rtsp_st->interleaved_max)
  1019. goto found;
  1020. }
  1021. goto redo;
  1022. found:
  1023. *prtsp_st = rtsp_st;
  1024. return len;
  1025. }
  1026. static int udp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  1027. uint8_t *buf, int buf_size)
  1028. {
  1029. RTSPState *rt = s->priv_data;
  1030. RTSPStream *rtsp_st;
  1031. fd_set rfds;
  1032. int fd1, fd2, fd_max, n, i, ret;
  1033. struct timeval tv;
  1034. for(;;) {
  1035. if (url_interrupt_cb())
  1036. return -1;
  1037. FD_ZERO(&rfds);
  1038. fd_max = -1;
  1039. for(i = 0; i < rt->nb_rtsp_streams; i++) {
  1040. rtsp_st = rt->rtsp_streams[i];
  1041. /* currently, we cannot probe RTCP handle because of blocking restrictions */
  1042. rtp_get_file_handles(rtsp_st->rtp_handle, &fd1, &fd2);
  1043. if (fd1 > fd_max)
  1044. fd_max = fd1;
  1045. FD_SET(fd1, &rfds);
  1046. }
  1047. tv.tv_sec = 0;
  1048. tv.tv_usec = 100 * 1000;
  1049. n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
  1050. if (n > 0) {
  1051. for(i = 0; i < rt->nb_rtsp_streams; i++) {
  1052. rtsp_st = rt->rtsp_streams[i];
  1053. rtp_get_file_handles(rtsp_st->rtp_handle, &fd1, &fd2);
  1054. if (FD_ISSET(fd1, &rfds)) {
  1055. ret = url_read(rtsp_st->rtp_handle, buf, buf_size);
  1056. if (ret > 0) {
  1057. *prtsp_st = rtsp_st;
  1058. return ret;
  1059. }
  1060. }
  1061. }
  1062. }
  1063. }
  1064. }
  1065. static int rtsp_read_packet(AVFormatContext *s,
  1066. AVPacket *pkt)
  1067. {
  1068. RTSPState *rt = s->priv_data;
  1069. RTSPStream *rtsp_st;
  1070. int ret, len;
  1071. uint8_t buf[RTP_MAX_PACKET_LENGTH];
  1072. /* get next frames from the same RTP packet */
  1073. if (rt->cur_rtp) {
  1074. ret = rtp_parse_packet(rt->cur_rtp, pkt, NULL, 0);
  1075. if (ret == 0) {
  1076. rt->cur_rtp = NULL;
  1077. return 0;
  1078. } else if (ret == 1) {
  1079. return 0;
  1080. } else {
  1081. rt->cur_rtp = NULL;
  1082. }
  1083. }
  1084. /* read next RTP packet */
  1085. redo:
  1086. switch(rt->protocol) {
  1087. default:
  1088. case RTSP_PROTOCOL_RTP_TCP:
  1089. len = tcp_read_packet(s, &rtsp_st, buf, sizeof(buf));
  1090. break;
  1091. case RTSP_PROTOCOL_RTP_UDP:
  1092. case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
  1093. len = udp_read_packet(s, &rtsp_st, buf, sizeof(buf));
  1094. if (rtsp_st->rtp_ctx)
  1095. rtp_check_and_send_back_rr(rtsp_st->rtp_ctx, len);
  1096. break;
  1097. }
  1098. if (len < 0)
  1099. return AVERROR_IO;
  1100. ret = rtp_parse_packet(rtsp_st->rtp_ctx, pkt, buf, len);
  1101. if (ret < 0)
  1102. goto redo;
  1103. if (ret == 1) {
  1104. /* more packets may follow, so we save the RTP context */
  1105. rt->cur_rtp = rtsp_st->rtp_ctx;
  1106. }
  1107. return 0;
  1108. }
  1109. static int rtsp_read_play(AVFormatContext *s)
  1110. {
  1111. RTSPState *rt = s->priv_data;
  1112. RTSPHeader reply1, *reply = &reply1;
  1113. char cmd[1024];
  1114. av_log(s, AV_LOG_DEBUG, "hello state=%d\n", rt->state);
  1115. if (rt->state == RTSP_STATE_PAUSED) {
  1116. snprintf(cmd, sizeof(cmd),
  1117. "PLAY %s RTSP/1.0\r\n",
  1118. s->filename);
  1119. } else {
  1120. snprintf(cmd, sizeof(cmd),
  1121. "PLAY %s RTSP/1.0\r\n"
  1122. "Range: npt=%0.3f-\r\n",
  1123. s->filename,
  1124. (double)rt->seek_timestamp / AV_TIME_BASE);
  1125. }
  1126. rtsp_send_cmd(s, cmd, reply, NULL);
  1127. if (reply->status_code != RTSP_STATUS_OK) {
  1128. return -1;
  1129. } else {
  1130. rt->state = RTSP_STATE_PLAYING;
  1131. return 0;
  1132. }
  1133. }
  1134. /* pause the stream */
  1135. static int rtsp_read_pause(AVFormatContext *s)
  1136. {
  1137. RTSPState *rt = s->priv_data;
  1138. RTSPHeader reply1, *reply = &reply1;
  1139. char cmd[1024];
  1140. rt = s->priv_data;
  1141. if (rt->state != RTSP_STATE_PLAYING)
  1142. return 0;
  1143. snprintf(cmd, sizeof(cmd),
  1144. "PAUSE %s RTSP/1.0\r\n",
  1145. s->filename);
  1146. rtsp_send_cmd(s, cmd, reply, NULL);
  1147. if (reply->status_code != RTSP_STATUS_OK) {
  1148. return -1;
  1149. } else {
  1150. rt->state = RTSP_STATE_PAUSED;
  1151. return 0;
  1152. }
  1153. }
  1154. static int rtsp_read_seek(AVFormatContext *s, int stream_index,
  1155. int64_t timestamp, int flags)
  1156. {
  1157. RTSPState *rt = s->priv_data;
  1158. rt->seek_timestamp = timestamp;
  1159. switch(rt->state) {
  1160. default:
  1161. case RTSP_STATE_IDLE:
  1162. break;
  1163. case RTSP_STATE_PLAYING:
  1164. if (rtsp_read_play(s) != 0)
  1165. return -1;
  1166. break;
  1167. case RTSP_STATE_PAUSED:
  1168. rt->state = RTSP_STATE_IDLE;
  1169. break;
  1170. }
  1171. return 0;
  1172. }
  1173. static int rtsp_read_close(AVFormatContext *s)
  1174. {
  1175. RTSPState *rt = s->priv_data;
  1176. RTSPHeader reply1, *reply = &reply1;
  1177. char cmd[1024];
  1178. #if 0
  1179. /* NOTE: it is valid to flush the buffer here */
  1180. if (rt->protocol == RTSP_PROTOCOL_RTP_TCP) {
  1181. url_fclose(&rt->rtsp_gb);
  1182. }
  1183. #endif
  1184. snprintf(cmd, sizeof(cmd),
  1185. "TEARDOWN %s RTSP/1.0\r\n",
  1186. s->filename);
  1187. rtsp_send_cmd(s, cmd, reply, NULL);
  1188. if (ff_rtsp_callback) {
  1189. ff_rtsp_callback(RTSP_ACTION_CLIENT_TEARDOWN, rt->session_id,
  1190. NULL, 0, NULL);
  1191. }
  1192. rtsp_close_streams(rt);
  1193. url_close(rt->rtsp_hd);
  1194. return 0;
  1195. }
  1196. AVInputFormat rtsp_demuxer = {
  1197. "rtsp",
  1198. "RTSP input format",
  1199. sizeof(RTSPState),
  1200. rtsp_probe,
  1201. rtsp_read_header,
  1202. rtsp_read_packet,
  1203. rtsp_read_close,
  1204. rtsp_read_seek,
  1205. .flags = AVFMT_NOFILE,
  1206. .read_play = rtsp_read_play,
  1207. .read_pause = rtsp_read_pause,
  1208. };
  1209. static int sdp_probe(AVProbeData *p1)
  1210. {
  1211. const char *p = p1->buf, *p_end = p1->buf + p1->buf_size;
  1212. /* we look for a line beginning "c=IN IP4" */
  1213. while (p < p_end && *p != '\0') {
  1214. if (p + sizeof("c=IN IP4") - 1 < p_end && strstart(p, "c=IN IP4", NULL))
  1215. return AVPROBE_SCORE_MAX / 2;
  1216. while(p < p_end - 1 && *p != '\n') p++;
  1217. if (++p >= p_end)
  1218. break;
  1219. if (*p == '\r')
  1220. p++;
  1221. }
  1222. return 0;
  1223. }
  1224. #define SDP_MAX_SIZE 8192
  1225. static int sdp_read_header(AVFormatContext *s,
  1226. AVFormatParameters *ap)
  1227. {
  1228. RTSPState *rt = s->priv_data;
  1229. RTSPStream *rtsp_st;
  1230. int size, i, err;
  1231. char *content;
  1232. char url[1024];
  1233. AVStream *st;
  1234. /* read the whole sdp file */
  1235. /* XXX: better loading */
  1236. content = av_malloc(SDP_MAX_SIZE);
  1237. size = get_buffer(&s->pb, content, SDP_MAX_SIZE - 1);
  1238. if (size <= 0) {
  1239. av_free(content);
  1240. return AVERROR_INVALIDDATA;
  1241. }
  1242. content[size] ='\0';
  1243. sdp_parse(s, content);
  1244. av_free(content);
  1245. /* open each RTP stream */
  1246. for(i=0;i<rt->nb_rtsp_streams;i++) {
  1247. rtsp_st = rt->rtsp_streams[i];
  1248. snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
  1249. inet_ntoa(rtsp_st->sdp_ip),
  1250. rtsp_st->sdp_port,
  1251. rtsp_st->sdp_ttl);
  1252. if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
  1253. err = AVERROR_INVALIDDATA;
  1254. goto fail;
  1255. }
  1256. /* open the RTP context */
  1257. st = NULL;
  1258. if (rtsp_st->stream_index >= 0)
  1259. st = s->streams[rtsp_st->stream_index];
  1260. if (!st)
  1261. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1262. rtsp_st->rtp_ctx = rtp_parse_open(s, st, rtsp_st->rtp_handle, rtsp_st->sdp_payload_type, &rtsp_st->rtp_payload_data);
  1263. if (!rtsp_st->rtp_ctx) {
  1264. err = AVERROR_NOMEM;
  1265. goto fail;
  1266. } else {
  1267. if(rtsp_st->dynamic_handler) {
  1268. rtsp_st->rtp_ctx->dynamic_protocol_context= rtsp_st->dynamic_protocol_context;
  1269. rtsp_st->rtp_ctx->parse_packet= rtsp_st->dynamic_handler->parse_packet;
  1270. }
  1271. }
  1272. }
  1273. return 0;
  1274. fail:
  1275. rtsp_close_streams(rt);
  1276. return err;
  1277. }
  1278. static int sdp_read_packet(AVFormatContext *s,
  1279. AVPacket *pkt)
  1280. {
  1281. return rtsp_read_packet(s, pkt);
  1282. }
  1283. static int sdp_read_close(AVFormatContext *s)
  1284. {
  1285. RTSPState *rt = s->priv_data;
  1286. rtsp_close_streams(rt);
  1287. return 0;
  1288. }
  1289. #ifdef CONFIG_SDP_DEMUXER
  1290. AVInputFormat sdp_demuxer = {
  1291. "sdp",
  1292. "SDP",
  1293. sizeof(RTSPState),
  1294. sdp_probe,
  1295. sdp_read_header,
  1296. sdp_read_packet,
  1297. sdp_read_close,
  1298. };
  1299. #endif
  1300. /* dummy redirector format (used directly in av_open_input_file now) */
  1301. static int redir_probe(AVProbeData *pd)
  1302. {
  1303. const char *p;
  1304. p = pd->buf;
  1305. while (redir_isspace(*p))
  1306. p++;
  1307. if (strstart(p, "http://", NULL) ||
  1308. strstart(p, "rtsp://", NULL))
  1309. return AVPROBE_SCORE_MAX;
  1310. return 0;
  1311. }
  1312. /* called from utils.c */
  1313. int redir_open(AVFormatContext **ic_ptr, ByteIOContext *f)
  1314. {
  1315. char buf[4096], *q;
  1316. int c;
  1317. AVFormatContext *ic = NULL;
  1318. /* parse each URL and try to open it */
  1319. c = url_fgetc(f);
  1320. while (c != URL_EOF) {
  1321. /* skip spaces */
  1322. for(;;) {
  1323. if (!redir_isspace(c))
  1324. break;
  1325. c = url_fgetc(f);
  1326. }
  1327. if (c == URL_EOF)
  1328. break;
  1329. /* record url */
  1330. q = buf;
  1331. for(;;) {
  1332. if (c == URL_EOF || redir_isspace(c))
  1333. break;
  1334. if ((q - buf) < sizeof(buf) - 1)
  1335. *q++ = c;
  1336. c = url_fgetc(f);
  1337. }
  1338. *q = '\0';
  1339. //printf("URL='%s'\n", buf);
  1340. /* try to open the media file */
  1341. if (av_open_input_file(&ic, buf, NULL, 0, NULL) == 0)
  1342. break;
  1343. }
  1344. *ic_ptr = ic;
  1345. if (!ic)
  1346. return AVERROR_IO;
  1347. else
  1348. return 0;
  1349. }
  1350. AVInputFormat redir_demuxer = {
  1351. "redir",
  1352. "Redirector format",
  1353. 0,
  1354. redir_probe,
  1355. NULL,
  1356. NULL,
  1357. NULL,
  1358. };