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.

1492 lines
44KB

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