You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1490 lines
44KB

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