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.

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