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.

1862 lines
61KB

  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. /* needed by inet_aton() */
  22. #define _SVID_SOURCE
  23. #include "libavutil/base64.h"
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "avformat.h"
  27. #include <sys/time.h>
  28. #if HAVE_SYS_SELECT_H
  29. #include <sys/select.h>
  30. #endif
  31. #include <strings.h>
  32. #include "network.h"
  33. #include "rtsp.h"
  34. #include "rtpdec.h"
  35. #include "rdt.h"
  36. #include "rtp_asf.h"
  37. #include "rtp_vorbis.h"
  38. //#define DEBUG
  39. //#define DEBUG_RTP_TCP
  40. #if LIBAVFORMAT_VERSION_INT < (53 << 16)
  41. int rtsp_default_protocols = (1 << RTSP_LOWER_TRANSPORT_UDP);
  42. #endif
  43. #define SPACE_CHARS " \t\r\n"
  44. /* we use memchr() instead of strchr() here because strchr() will return
  45. * the terminating '\0' of SPACE_CHARS instead of NULL if c is '\0'. */
  46. #define redir_isspace(c) memchr(SPACE_CHARS, c, 4)
  47. static void skip_spaces(const char **pp)
  48. {
  49. const char *p;
  50. p = *pp;
  51. while (redir_isspace(*p))
  52. p++;
  53. *pp = p;
  54. }
  55. static void get_word_until_chars(char *buf, int buf_size,
  56. const char *sep, const char **pp)
  57. {
  58. const char *p;
  59. char *q;
  60. p = *pp;
  61. skip_spaces(&p);
  62. q = buf;
  63. while (!strchr(sep, *p) && *p != '\0') {
  64. if ((q - buf) < buf_size - 1)
  65. *q++ = *p;
  66. p++;
  67. }
  68. if (buf_size > 0)
  69. *q = '\0';
  70. *pp = p;
  71. }
  72. static void get_word_sep(char *buf, int buf_size, const char *sep,
  73. const char **pp)
  74. {
  75. if (**pp == '/') (*pp)++;
  76. get_word_until_chars(buf, buf_size, sep, pp);
  77. }
  78. static void get_word(char *buf, int buf_size, const char **pp)
  79. {
  80. get_word_until_chars(buf, buf_size, SPACE_CHARS, pp);
  81. }
  82. /* parse the rtpmap description: <codec_name>/<clock_rate>[/<other params>] */
  83. static int sdp_parse_rtpmap(AVFormatContext *s,
  84. AVCodecContext *codec, RTSPStream *rtsp_st,
  85. int payload_type, const char *p)
  86. {
  87. char buf[256];
  88. int i;
  89. AVCodec *c;
  90. const char *c_name;
  91. /* Loop into AVRtpDynamicPayloadTypes[] and AVRtpPayloadTypes[] and
  92. * see if we can handle this kind of payload.
  93. * The space should normally not be there but some Real streams or
  94. * particular servers ("RealServer Version 6.1.3.970", see issue 1658)
  95. * have a trailing space. */
  96. get_word_sep(buf, sizeof(buf), "/ ", &p);
  97. if (payload_type >= RTP_PT_PRIVATE) {
  98. RTPDynamicProtocolHandler *handler;
  99. for (handler = RTPFirstDynamicPayloadHandler;
  100. handler; handler = handler->next) {
  101. if (!strcasecmp(buf, handler->enc_name) &&
  102. codec->codec_type == handler->codec_type) {
  103. codec->codec_id = handler->codec_id;
  104. rtsp_st->dynamic_handler = handler;
  105. if (handler->open)
  106. rtsp_st->dynamic_protocol_context = handler->open();
  107. break;
  108. }
  109. }
  110. } else {
  111. /* We are in a standard case
  112. * (from http://www.iana.org/assignments/rtp-parameters). */
  113. /* search into AVRtpPayloadTypes[] */
  114. codec->codec_id = ff_rtp_codec_id(buf, codec->codec_type);
  115. }
  116. c = avcodec_find_decoder(codec->codec_id);
  117. if (c && c->name)
  118. c_name = c->name;
  119. else
  120. c_name = "(null)";
  121. get_word_sep(buf, sizeof(buf), "/", &p);
  122. i = atoi(buf);
  123. switch (codec->codec_type) {
  124. case CODEC_TYPE_AUDIO:
  125. av_log(s, AV_LOG_DEBUG, "audio codec set to: %s\n", c_name);
  126. codec->sample_rate = RTSP_DEFAULT_AUDIO_SAMPLERATE;
  127. codec->channels = RTSP_DEFAULT_NB_AUDIO_CHANNELS;
  128. if (i > 0) {
  129. codec->sample_rate = i;
  130. get_word_sep(buf, sizeof(buf), "/", &p);
  131. i = atoi(buf);
  132. if (i > 0)
  133. codec->channels = i;
  134. // TODO: there is a bug here; if it is a mono stream, and
  135. // less than 22000Hz, faad upconverts to stereo and twice
  136. // the frequency. No problem, but the sample rate is being
  137. // set here by the sdp line. Patch on its way. (rdm)
  138. }
  139. av_log(s, AV_LOG_DEBUG, "audio samplerate set to: %i\n",
  140. codec->sample_rate);
  141. av_log(s, AV_LOG_DEBUG, "audio channels set to: %i\n",
  142. codec->channels);
  143. break;
  144. case CODEC_TYPE_VIDEO:
  145. av_log(s, AV_LOG_DEBUG, "video codec set to: %s\n", c_name);
  146. break;
  147. default:
  148. break;
  149. }
  150. return 0;
  151. }
  152. /* return the length and optionally the data */
  153. static int hex_to_data(uint8_t *data, const char *p)
  154. {
  155. int c, len, v;
  156. len = 0;
  157. v = 1;
  158. for (;;) {
  159. skip_spaces(&p);
  160. if (*p == '\0')
  161. break;
  162. c = toupper((unsigned char) *p++);
  163. if (c >= '0' && c <= '9')
  164. c = c - '0';
  165. else if (c >= 'A' && c <= 'F')
  166. c = c - 'A' + 10;
  167. else
  168. break;
  169. v = (v << 4) | c;
  170. if (v & 0x100) {
  171. if (data)
  172. data[len] = v;
  173. len++;
  174. v = 1;
  175. }
  176. }
  177. return len;
  178. }
  179. static void sdp_parse_fmtp_config(AVCodecContext * codec, void *ctx,
  180. char *attr, char *value)
  181. {
  182. switch (codec->codec_id) {
  183. case CODEC_ID_MPEG4:
  184. case CODEC_ID_AAC:
  185. if (!strcmp(attr, "config")) {
  186. /* decode the hexa encoded parameter */
  187. int len = hex_to_data(NULL, value);
  188. if (codec->extradata)
  189. av_free(codec->extradata);
  190. codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
  191. if (!codec->extradata)
  192. return;
  193. codec->extradata_size = len;
  194. hex_to_data(codec->extradata, value);
  195. }
  196. break;
  197. case CODEC_ID_VORBIS:
  198. ff_vorbis_parse_fmtp_config(codec, ctx, attr, value);
  199. break;
  200. default:
  201. break;
  202. }
  203. return;
  204. }
  205. typedef struct {
  206. const char *str;
  207. uint16_t type;
  208. uint32_t offset;
  209. } AttrNameMap;
  210. /* All known fmtp parmeters and the corresping RTPAttrTypeEnum */
  211. #define ATTR_NAME_TYPE_INT 0
  212. #define ATTR_NAME_TYPE_STR 1
  213. static const AttrNameMap attr_names[]=
  214. {
  215. { "SizeLength", ATTR_NAME_TYPE_INT,
  216. offsetof(RTPPayloadData, sizelength) },
  217. { "IndexLength", ATTR_NAME_TYPE_INT,
  218. offsetof(RTPPayloadData, indexlength) },
  219. { "IndexDeltaLength", ATTR_NAME_TYPE_INT,
  220. offsetof(RTPPayloadData, indexdeltalength) },
  221. { "profile-level-id", ATTR_NAME_TYPE_INT,
  222. offsetof(RTPPayloadData, profile_level_id) },
  223. { "StreamType", ATTR_NAME_TYPE_INT,
  224. offsetof(RTPPayloadData, streamtype) },
  225. { "mode", ATTR_NAME_TYPE_STR,
  226. offsetof(RTPPayloadData, mode) },
  227. { NULL, -1, -1 },
  228. };
  229. /* parse the attribute line from the fmtp a line of an sdp resonse. This
  230. * is broken out as a function because it is used in rtp_h264.c, which is
  231. * forthcoming. */
  232. int rtsp_next_attr_and_value(const char **p, char *attr, int attr_size,
  233. char *value, int value_size)
  234. {
  235. skip_spaces(p);
  236. if (**p) {
  237. get_word_sep(attr, attr_size, "=", p);
  238. if (**p == '=')
  239. (*p)++;
  240. get_word_sep(value, value_size, ";", p);
  241. if (**p == ';')
  242. (*p)++;
  243. return 1;
  244. }
  245. return 0;
  246. }
  247. /* parse a SDP line and save stream attributes */
  248. static void sdp_parse_fmtp(AVStream *st, const char *p)
  249. {
  250. char attr[256];
  251. /* Vorbis setup headers can be up to 12KB and are sent base64
  252. * encoded, giving a 12KB * (4/3) = 16KB FMTP line. */
  253. char value[16384];
  254. int i;
  255. RTSPStream *rtsp_st = st->priv_data;
  256. AVCodecContext *codec = st->codec;
  257. RTPPayloadData *rtp_payload_data = &rtsp_st->rtp_payload_data;
  258. /* loop on each attribute */
  259. while (rtsp_next_attr_and_value(&p, attr, sizeof(attr),
  260. value, sizeof(value))) {
  261. /* grab the codec extra_data from the config parameter of the fmtp
  262. * line */
  263. sdp_parse_fmtp_config(codec, rtsp_st->dynamic_protocol_context,
  264. attr, value);
  265. /* Looking for a known attribute */
  266. for (i = 0; attr_names[i].str; ++i) {
  267. if (!strcasecmp(attr, attr_names[i].str)) {
  268. if (attr_names[i].type == ATTR_NAME_TYPE_INT) {
  269. *(int *)((char *)rtp_payload_data +
  270. attr_names[i].offset) = atoi(value);
  271. } else if (attr_names[i].type == ATTR_NAME_TYPE_STR)
  272. *(char **)((char *)rtp_payload_data +
  273. attr_names[i].offset) = av_strdup(value);
  274. }
  275. }
  276. }
  277. }
  278. /** Parse a string p in the form of Range:npt=xx-xx, and determine the start
  279. * and end time.
  280. * Used for seeking in the rtp stream.
  281. */
  282. static void rtsp_parse_range_npt(const char *p, int64_t *start, int64_t *end)
  283. {
  284. char buf[256];
  285. skip_spaces(&p);
  286. if (!av_stristart(p, "npt=", &p))
  287. return;
  288. *start = AV_NOPTS_VALUE;
  289. *end = AV_NOPTS_VALUE;
  290. get_word_sep(buf, sizeof(buf), "-", &p);
  291. *start = parse_date(buf, 1);
  292. if (*p == '-') {
  293. p++;
  294. get_word_sep(buf, sizeof(buf), "-", &p);
  295. *end = parse_date(buf, 1);
  296. }
  297. // av_log(NULL, AV_LOG_DEBUG, "Range Start: %lld\n", *start);
  298. // av_log(NULL, AV_LOG_DEBUG, "Range End: %lld\n", *end);
  299. }
  300. typedef struct SDPParseState {
  301. /* SDP only */
  302. struct in_addr default_ip;
  303. int default_ttl;
  304. int skip_media; ///< set if an unknown m= line occurs
  305. } SDPParseState;
  306. static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1,
  307. int letter, const char *buf)
  308. {
  309. RTSPState *rt = s->priv_data;
  310. char buf1[64], st_type[64];
  311. const char *p;
  312. enum CodecType codec_type;
  313. int payload_type, i;
  314. AVStream *st;
  315. RTSPStream *rtsp_st;
  316. struct in_addr sdp_ip;
  317. int ttl;
  318. dprintf(s, "sdp: %c='%s'\n", letter, buf);
  319. p = buf;
  320. if (s1->skip_media && letter != 'm')
  321. return;
  322. switch (letter) {
  323. case 'c':
  324. get_word(buf1, sizeof(buf1), &p);
  325. if (strcmp(buf1, "IN") != 0)
  326. return;
  327. get_word(buf1, sizeof(buf1), &p);
  328. if (strcmp(buf1, "IP4") != 0)
  329. return;
  330. get_word_sep(buf1, sizeof(buf1), "/", &p);
  331. if (inet_aton(buf1, &sdp_ip) == 0)
  332. return;
  333. ttl = 16;
  334. if (*p == '/') {
  335. p++;
  336. get_word_sep(buf1, sizeof(buf1), "/", &p);
  337. ttl = atoi(buf1);
  338. }
  339. if (s->nb_streams == 0) {
  340. s1->default_ip = sdp_ip;
  341. s1->default_ttl = ttl;
  342. } else {
  343. st = s->streams[s->nb_streams - 1];
  344. rtsp_st = st->priv_data;
  345. rtsp_st->sdp_ip = sdp_ip;
  346. rtsp_st->sdp_ttl = ttl;
  347. }
  348. break;
  349. case 's':
  350. av_metadata_set(&s->metadata, "title", p);
  351. break;
  352. case 'i':
  353. if (s->nb_streams == 0) {
  354. av_metadata_set(&s->metadata, "comment", p);
  355. break;
  356. }
  357. break;
  358. case 'm':
  359. /* new stream */
  360. s1->skip_media = 0;
  361. get_word(st_type, sizeof(st_type), &p);
  362. if (!strcmp(st_type, "audio")) {
  363. codec_type = CODEC_TYPE_AUDIO;
  364. } else if (!strcmp(st_type, "video")) {
  365. codec_type = CODEC_TYPE_VIDEO;
  366. } else if (!strcmp(st_type, "application")) {
  367. codec_type = CODEC_TYPE_DATA;
  368. } else {
  369. s1->skip_media = 1;
  370. return;
  371. }
  372. rtsp_st = av_mallocz(sizeof(RTSPStream));
  373. if (!rtsp_st)
  374. return;
  375. rtsp_st->stream_index = -1;
  376. dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
  377. rtsp_st->sdp_ip = s1->default_ip;
  378. rtsp_st->sdp_ttl = s1->default_ttl;
  379. get_word(buf1, sizeof(buf1), &p); /* port */
  380. rtsp_st->sdp_port = atoi(buf1);
  381. get_word(buf1, sizeof(buf1), &p); /* protocol (ignored) */
  382. /* XXX: handle list of formats */
  383. get_word(buf1, sizeof(buf1), &p); /* format list */
  384. rtsp_st->sdp_payload_type = atoi(buf1);
  385. if (!strcmp(ff_rtp_enc_name(rtsp_st->sdp_payload_type), "MP2T")) {
  386. /* no corresponding stream */
  387. } else {
  388. st = av_new_stream(s, 0);
  389. if (!st)
  390. return;
  391. st->priv_data = rtsp_st;
  392. rtsp_st->stream_index = st->index;
  393. st->codec->codec_type = codec_type;
  394. if (rtsp_st->sdp_payload_type < RTP_PT_PRIVATE) {
  395. /* if standard payload type, we can find the codec right now */
  396. ff_rtp_get_codec_info(st->codec, rtsp_st->sdp_payload_type);
  397. }
  398. }
  399. /* put a default control url */
  400. av_strlcpy(rtsp_st->control_url, rt->control_uri,
  401. sizeof(rtsp_st->control_url));
  402. break;
  403. case 'a':
  404. if (av_strstart(p, "control:", &p)) {
  405. if (s->nb_streams == 0) {
  406. if (!strncmp(p, "rtsp://", 7))
  407. av_strlcpy(rt->control_uri, p,
  408. sizeof(rt->control_uri));
  409. } else {
  410. char proto[32];
  411. /* get the control url */
  412. st = s->streams[s->nb_streams - 1];
  413. rtsp_st = st->priv_data;
  414. /* XXX: may need to add full url resolution */
  415. url_split(proto, sizeof(proto), NULL, 0, NULL, 0,
  416. NULL, NULL, 0, p);
  417. if (proto[0] == '\0') {
  418. /* relative control URL */
  419. if (rtsp_st->control_url[strlen(rtsp_st->control_url)-1]!='/')
  420. av_strlcat(rtsp_st->control_url, "/",
  421. sizeof(rtsp_st->control_url));
  422. av_strlcat(rtsp_st->control_url, p,
  423. sizeof(rtsp_st->control_url));
  424. } else
  425. av_strlcpy(rtsp_st->control_url, p,
  426. sizeof(rtsp_st->control_url));
  427. }
  428. } else if (av_strstart(p, "rtpmap:", &p) && s->nb_streams > 0) {
  429. /* NOTE: rtpmap is only supported AFTER the 'm=' tag */
  430. get_word(buf1, sizeof(buf1), &p);
  431. payload_type = atoi(buf1);
  432. st = s->streams[s->nb_streams - 1];
  433. rtsp_st = st->priv_data;
  434. sdp_parse_rtpmap(s, st->codec, rtsp_st, payload_type, p);
  435. } else if (av_strstart(p, "fmtp:", &p)) {
  436. /* NOTE: fmtp is only supported AFTER the 'a=rtpmap:xxx' tag */
  437. get_word(buf1, sizeof(buf1), &p);
  438. payload_type = atoi(buf1);
  439. for (i = 0; i < s->nb_streams; i++) {
  440. st = s->streams[i];
  441. rtsp_st = st->priv_data;
  442. if (rtsp_st->sdp_payload_type == payload_type) {
  443. if (!(rtsp_st->dynamic_handler &&
  444. rtsp_st->dynamic_handler->parse_sdp_a_line &&
  445. rtsp_st->dynamic_handler->parse_sdp_a_line(s,
  446. i, rtsp_st->dynamic_protocol_context, buf)))
  447. sdp_parse_fmtp(st, p);
  448. }
  449. }
  450. } else if (av_strstart(p, "framesize:", &p)) {
  451. // let dynamic protocol handlers have a stab at the line.
  452. get_word(buf1, sizeof(buf1), &p);
  453. payload_type = atoi(buf1);
  454. for (i = 0; i < s->nb_streams; i++) {
  455. st = s->streams[i];
  456. rtsp_st = st->priv_data;
  457. if (rtsp_st->sdp_payload_type == payload_type &&
  458. rtsp_st->dynamic_handler &&
  459. rtsp_st->dynamic_handler->parse_sdp_a_line)
  460. rtsp_st->dynamic_handler->parse_sdp_a_line(s, i,
  461. rtsp_st->dynamic_protocol_context, buf);
  462. }
  463. } else if (av_strstart(p, "range:", &p)) {
  464. int64_t start, end;
  465. // this is so that seeking on a streamed file can work.
  466. rtsp_parse_range_npt(p, &start, &end);
  467. s->start_time = start;
  468. /* AV_NOPTS_VALUE means live broadcast (and can't seek) */
  469. s->duration = (end == AV_NOPTS_VALUE) ?
  470. AV_NOPTS_VALUE : end - start;
  471. } else if (av_strstart(p, "IsRealDataType:integer;",&p)) {
  472. if (atoi(p) == 1)
  473. rt->transport = RTSP_TRANSPORT_RDT;
  474. } else {
  475. if (rt->server_type == RTSP_SERVER_WMS)
  476. ff_wms_parse_sdp_a_line(s, p);
  477. if (s->nb_streams > 0) {
  478. if (rt->server_type == RTSP_SERVER_REAL)
  479. ff_real_parse_sdp_a_line(s, s->nb_streams - 1, p);
  480. rtsp_st = s->streams[s->nb_streams - 1]->priv_data;
  481. if (rtsp_st->dynamic_handler &&
  482. rtsp_st->dynamic_handler->parse_sdp_a_line)
  483. rtsp_st->dynamic_handler->parse_sdp_a_line(s,
  484. s->nb_streams - 1,
  485. rtsp_st->dynamic_protocol_context, buf);
  486. }
  487. }
  488. break;
  489. }
  490. }
  491. static int sdp_parse(AVFormatContext *s, const char *content)
  492. {
  493. const char *p;
  494. int letter;
  495. /* Some SDP lines, particularly for Realmedia or ASF RTSP streams,
  496. * contain long SDP lines containing complete ASF Headers (several
  497. * kB) or arrays of MDPR (RM stream descriptor) headers plus
  498. * "rulebooks" describing their properties. Therefore, the SDP line
  499. * buffer is large.
  500. *
  501. * The Vorbis FMTP line can be up to 16KB - see sdp_parse_fmtp. */
  502. char buf[16384], *q;
  503. SDPParseState sdp_parse_state, *s1 = &sdp_parse_state;
  504. memset(s1, 0, sizeof(SDPParseState));
  505. p = content;
  506. for (;;) {
  507. skip_spaces(&p);
  508. letter = *p;
  509. if (letter == '\0')
  510. break;
  511. p++;
  512. if (*p != '=')
  513. goto next_line;
  514. p++;
  515. /* get the content */
  516. q = buf;
  517. while (*p != '\n' && *p != '\r' && *p != '\0') {
  518. if ((q - buf) < sizeof(buf) - 1)
  519. *q++ = *p;
  520. p++;
  521. }
  522. *q = '\0';
  523. sdp_parse_line(s, s1, letter, buf);
  524. next_line:
  525. while (*p != '\n' && *p != '\0')
  526. p++;
  527. if (*p == '\n')
  528. p++;
  529. }
  530. return 0;
  531. }
  532. /* close and free RTSP streams */
  533. static void rtsp_close_streams(RTSPState *rt)
  534. {
  535. int i;
  536. RTSPStream *rtsp_st;
  537. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  538. rtsp_st = rt->rtsp_streams[i];
  539. if (rtsp_st) {
  540. if (rtsp_st->transport_priv) {
  541. if (rt->transport == RTSP_TRANSPORT_RDT)
  542. ff_rdt_parse_close(rtsp_st->transport_priv);
  543. else
  544. rtp_parse_close(rtsp_st->transport_priv);
  545. }
  546. if (rtsp_st->rtp_handle)
  547. url_close(rtsp_st->rtp_handle);
  548. if (rtsp_st->dynamic_handler && rtsp_st->dynamic_protocol_context)
  549. rtsp_st->dynamic_handler->close(
  550. rtsp_st->dynamic_protocol_context);
  551. }
  552. }
  553. av_free(rt->rtsp_streams);
  554. if (rt->asf_ctx) {
  555. av_close_input_stream (rt->asf_ctx);
  556. rt->asf_ctx = NULL;
  557. }
  558. av_freep(&rt->auth_b64);
  559. }
  560. static int rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st)
  561. {
  562. RTSPState *rt = s->priv_data;
  563. AVStream *st = NULL;
  564. /* open the RTP context */
  565. if (rtsp_st->stream_index >= 0)
  566. st = s->streams[rtsp_st->stream_index];
  567. if (!st)
  568. s->ctx_flags |= AVFMTCTX_NOHEADER;
  569. if (rt->transport == RTSP_TRANSPORT_RDT)
  570. rtsp_st->transport_priv = ff_rdt_parse_open(s, st->index,
  571. rtsp_st->dynamic_protocol_context,
  572. rtsp_st->dynamic_handler);
  573. else
  574. rtsp_st->transport_priv = rtp_parse_open(s, st, rtsp_st->rtp_handle,
  575. rtsp_st->sdp_payload_type,
  576. &rtsp_st->rtp_payload_data);
  577. if (!rtsp_st->transport_priv) {
  578. return AVERROR(ENOMEM);
  579. } else if (rt->transport != RTSP_TRANSPORT_RDT) {
  580. if (rtsp_st->dynamic_handler) {
  581. rtp_parse_set_dynamic_protocol(rtsp_st->transport_priv,
  582. rtsp_st->dynamic_protocol_context,
  583. rtsp_st->dynamic_handler);
  584. }
  585. }
  586. return 0;
  587. }
  588. #if CONFIG_RTSP_DEMUXER
  589. static int rtsp_probe(AVProbeData *p)
  590. {
  591. if (av_strstart(p->filename, "rtsp:", NULL))
  592. return AVPROBE_SCORE_MAX;
  593. return 0;
  594. }
  595. static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
  596. {
  597. const char *p;
  598. int v;
  599. p = *pp;
  600. skip_spaces(&p);
  601. v = strtol(p, (char **)&p, 10);
  602. if (*p == '-') {
  603. p++;
  604. *min_ptr = v;
  605. v = strtol(p, (char **)&p, 10);
  606. *max_ptr = v;
  607. } else {
  608. *min_ptr = v;
  609. *max_ptr = v;
  610. }
  611. *pp = p;
  612. }
  613. /* XXX: only one transport specification is parsed */
  614. static void rtsp_parse_transport(RTSPMessageHeader *reply, const char *p)
  615. {
  616. char transport_protocol[16];
  617. char profile[16];
  618. char lower_transport[16];
  619. char parameter[16];
  620. RTSPTransportField *th;
  621. char buf[256];
  622. reply->nb_transports = 0;
  623. for (;;) {
  624. skip_spaces(&p);
  625. if (*p == '\0')
  626. break;
  627. th = &reply->transports[reply->nb_transports];
  628. get_word_sep(transport_protocol, sizeof(transport_protocol),
  629. "/", &p);
  630. if (!strcasecmp (transport_protocol, "rtp")) {
  631. get_word_sep(profile, sizeof(profile), "/;,", &p);
  632. lower_transport[0] = '\0';
  633. /* rtp/avp/<protocol> */
  634. if (*p == '/') {
  635. get_word_sep(lower_transport, sizeof(lower_transport),
  636. ";,", &p);
  637. }
  638. th->transport = RTSP_TRANSPORT_RTP;
  639. } else if (!strcasecmp (transport_protocol, "x-pn-tng") ||
  640. !strcasecmp (transport_protocol, "x-real-rdt")) {
  641. /* x-pn-tng/<protocol> */
  642. get_word_sep(lower_transport, sizeof(lower_transport), "/;,", &p);
  643. profile[0] = '\0';
  644. th->transport = RTSP_TRANSPORT_RDT;
  645. }
  646. if (!strcasecmp(lower_transport, "TCP"))
  647. th->lower_transport = RTSP_LOWER_TRANSPORT_TCP;
  648. else
  649. th->lower_transport = RTSP_LOWER_TRANSPORT_UDP;
  650. if (*p == ';')
  651. p++;
  652. /* get each parameter */
  653. while (*p != '\0' && *p != ',') {
  654. get_word_sep(parameter, sizeof(parameter), "=;,", &p);
  655. if (!strcmp(parameter, "port")) {
  656. if (*p == '=') {
  657. p++;
  658. rtsp_parse_range(&th->port_min, &th->port_max, &p);
  659. }
  660. } else if (!strcmp(parameter, "client_port")) {
  661. if (*p == '=') {
  662. p++;
  663. rtsp_parse_range(&th->client_port_min,
  664. &th->client_port_max, &p);
  665. }
  666. } else if (!strcmp(parameter, "server_port")) {
  667. if (*p == '=') {
  668. p++;
  669. rtsp_parse_range(&th->server_port_min,
  670. &th->server_port_max, &p);
  671. }
  672. } else if (!strcmp(parameter, "interleaved")) {
  673. if (*p == '=') {
  674. p++;
  675. rtsp_parse_range(&th->interleaved_min,
  676. &th->interleaved_max, &p);
  677. }
  678. } else if (!strcmp(parameter, "multicast")) {
  679. if (th->lower_transport == RTSP_LOWER_TRANSPORT_UDP)
  680. th->lower_transport = RTSP_LOWER_TRANSPORT_UDP_MULTICAST;
  681. } else if (!strcmp(parameter, "ttl")) {
  682. if (*p == '=') {
  683. p++;
  684. th->ttl = strtol(p, (char **)&p, 10);
  685. }
  686. } else if (!strcmp(parameter, "destination")) {
  687. struct in_addr ipaddr;
  688. if (*p == '=') {
  689. p++;
  690. get_word_sep(buf, sizeof(buf), ";,", &p);
  691. if (inet_aton(buf, &ipaddr))
  692. th->destination = ntohl(ipaddr.s_addr);
  693. }
  694. }
  695. while (*p != ';' && *p != '\0' && *p != ',')
  696. p++;
  697. if (*p == ';')
  698. p++;
  699. }
  700. if (*p == ',')
  701. p++;
  702. reply->nb_transports++;
  703. }
  704. }
  705. void rtsp_parse_line(RTSPMessageHeader *reply, const char *buf)
  706. {
  707. const char *p;
  708. /* NOTE: we do case independent match for broken servers */
  709. p = buf;
  710. if (av_stristart(p, "Session:", &p)) {
  711. int t;
  712. get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
  713. if (av_stristart(p, ";timeout=", &p) &&
  714. (t = strtol(p, NULL, 10)) > 0) {
  715. reply->timeout = t;
  716. }
  717. } else if (av_stristart(p, "Content-Length:", &p)) {
  718. reply->content_length = strtol(p, NULL, 10);
  719. } else if (av_stristart(p, "Transport:", &p)) {
  720. rtsp_parse_transport(reply, p);
  721. } else if (av_stristart(p, "CSeq:", &p)) {
  722. reply->seq = strtol(p, NULL, 10);
  723. } else if (av_stristart(p, "Range:", &p)) {
  724. rtsp_parse_range_npt(p, &reply->range_start, &reply->range_end);
  725. } else if (av_stristart(p, "RealChallenge1:", &p)) {
  726. skip_spaces(&p);
  727. av_strlcpy(reply->real_challenge, p, sizeof(reply->real_challenge));
  728. } else if (av_stristart(p, "Server:", &p)) {
  729. skip_spaces(&p);
  730. av_strlcpy(reply->server, p, sizeof(reply->server));
  731. } else if (av_stristart(p, "Notice:", &p) ||
  732. av_stristart(p, "X-Notice:", &p)) {
  733. reply->notice = strtol(p, NULL, 10);
  734. } else if (av_stristart(p, "Location:", &p)) {
  735. skip_spaces(&p);
  736. av_strlcpy(reply->location, p , sizeof(reply->location));
  737. }
  738. }
  739. /* skip a RTP/TCP interleaved packet */
  740. static void rtsp_skip_packet(AVFormatContext *s)
  741. {
  742. RTSPState *rt = s->priv_data;
  743. int ret, len, len1;
  744. uint8_t buf[1024];
  745. ret = url_read_complete(rt->rtsp_hd, buf, 3);
  746. if (ret != 3)
  747. return;
  748. len = AV_RB16(buf + 1);
  749. dprintf(s, "skipping RTP packet len=%d\n", len);
  750. /* skip payload */
  751. while (len > 0) {
  752. len1 = len;
  753. if (len1 > sizeof(buf))
  754. len1 = sizeof(buf);
  755. ret = url_read_complete(rt->rtsp_hd, buf, len1);
  756. if (ret != len1)
  757. return;
  758. len -= len1;
  759. }
  760. }
  761. /**
  762. * Read a RTSP message from the server, or prepare to read data
  763. * packets if we're reading data interleaved over the TCP/RTSP
  764. * connection as well.
  765. *
  766. * @param s RTSP demuxer context
  767. * @param reply pointer where the RTSP message header will be stored
  768. * @param content_ptr pointer where the RTSP message body, if any, will
  769. * be stored (length is in reply)
  770. * @param return_on_interleaved_data whether the function may return if we
  771. * encounter a data marker ('$'), which precedes data
  772. * packets over interleaved TCP/RTSP connections. If this
  773. * is set, this function will return 1 after encountering
  774. * a '$'. If it is not set, the function will skip any
  775. * data packets (if they are encountered), until a reply
  776. * has been fully parsed. If no more data is available
  777. * without parsing a reply, it will return an error.
  778. *
  779. * @returns 1 if a data packets is ready to be received, -1 on error,
  780. * and 0 on success.
  781. */
  782. static int rtsp_read_reply(AVFormatContext *s, RTSPMessageHeader *reply,
  783. unsigned char **content_ptr,
  784. int return_on_interleaved_data)
  785. {
  786. RTSPState *rt = s->priv_data;
  787. char buf[4096], buf1[1024], *q;
  788. unsigned char ch;
  789. const char *p;
  790. int ret, content_length, line_count = 0;
  791. unsigned char *content = NULL;
  792. memset(reply, 0, sizeof(*reply));
  793. /* parse reply (XXX: use buffers) */
  794. rt->last_reply[0] = '\0';
  795. for (;;) {
  796. q = buf;
  797. for (;;) {
  798. ret = url_read_complete(rt->rtsp_hd, &ch, 1);
  799. #ifdef DEBUG_RTP_TCP
  800. dprintf(s, "ret=%d c=%02x [%c]\n", ret, ch, ch);
  801. #endif
  802. if (ret != 1)
  803. return -1;
  804. if (ch == '\n')
  805. break;
  806. if (ch == '$') {
  807. /* XXX: only parse it if first char on line ? */
  808. if (return_on_interleaved_data) {
  809. return 1;
  810. } else
  811. rtsp_skip_packet(s);
  812. } else if (ch != '\r') {
  813. if ((q - buf) < sizeof(buf) - 1)
  814. *q++ = ch;
  815. }
  816. }
  817. *q = '\0';
  818. dprintf(s, "line='%s'\n", buf);
  819. /* test if last line */
  820. if (buf[0] == '\0')
  821. break;
  822. p = buf;
  823. if (line_count == 0) {
  824. /* get reply code */
  825. get_word(buf1, sizeof(buf1), &p);
  826. get_word(buf1, sizeof(buf1), &p);
  827. reply->status_code = atoi(buf1);
  828. } else {
  829. rtsp_parse_line(reply, p);
  830. av_strlcat(rt->last_reply, p, sizeof(rt->last_reply));
  831. av_strlcat(rt->last_reply, "\n", sizeof(rt->last_reply));
  832. }
  833. line_count++;
  834. }
  835. if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0')
  836. av_strlcpy(rt->session_id, reply->session_id, sizeof(rt->session_id));
  837. content_length = reply->content_length;
  838. if (content_length > 0) {
  839. /* leave some room for a trailing '\0' (useful for simple parsing) */
  840. content = av_malloc(content_length + 1);
  841. (void)url_read_complete(rt->rtsp_hd, content, content_length);
  842. content[content_length] = '\0';
  843. }
  844. if (content_ptr)
  845. *content_ptr = content;
  846. else
  847. av_free(content);
  848. /* EOS */
  849. if (reply->notice == 2101 /* End-of-Stream Reached */ ||
  850. reply->notice == 2104 /* Start-of-Stream Reached */ ||
  851. reply->notice == 2306 /* Continuous Feed Terminated */) {
  852. rt->state = RTSP_STATE_IDLE;
  853. } else if (reply->notice >= 4400 && reply->notice < 5500) {
  854. return AVERROR(EIO); /* data or server error */
  855. } else if (reply->notice == 2401 /* Ticket Expired */ ||
  856. (reply->notice >= 5500 && reply->notice < 5600) /* end of term */ )
  857. return AVERROR(EPERM);
  858. return 0;
  859. }
  860. static void rtsp_send_cmd_with_content_async(AVFormatContext *s,
  861. const char *cmd,
  862. const unsigned char *send_content,
  863. int send_content_length)
  864. {
  865. RTSPState *rt = s->priv_data;
  866. char buf[4096], buf1[1024];
  867. rt->seq++;
  868. av_strlcpy(buf, cmd, sizeof(buf));
  869. snprintf(buf1, sizeof(buf1), "CSeq: %d\r\n", rt->seq);
  870. av_strlcat(buf, buf1, sizeof(buf));
  871. if (rt->session_id[0] != '\0' && !strstr(cmd, "\nIf-Match:")) {
  872. snprintf(buf1, sizeof(buf1), "Session: %s\r\n", rt->session_id);
  873. av_strlcat(buf, buf1, sizeof(buf));
  874. }
  875. if (rt->auth_b64)
  876. av_strlcatf(buf, sizeof(buf),
  877. "Authorization: Basic %s\r\n",
  878. rt->auth_b64);
  879. if (send_content_length > 0 && send_content)
  880. av_strlcatf(buf, sizeof(buf), "Content-Length: %d\r\n", send_content_length);
  881. av_strlcat(buf, "\r\n", sizeof(buf));
  882. dprintf(s, "Sending:\n%s--\n", buf);
  883. url_write(rt->rtsp_hd, buf, strlen(buf));
  884. if (send_content_length > 0 && send_content)
  885. url_write(rt->rtsp_hd, send_content, send_content_length);
  886. rt->last_cmd_time = av_gettime();
  887. }
  888. static void rtsp_send_cmd_async(AVFormatContext *s, const char *cmd)
  889. {
  890. rtsp_send_cmd_with_content_async(s, cmd, NULL, 0);
  891. }
  892. static void rtsp_send_cmd(AVFormatContext *s,
  893. const char *cmd, RTSPMessageHeader *reply,
  894. unsigned char **content_ptr)
  895. {
  896. rtsp_send_cmd_async(s, cmd);
  897. rtsp_read_reply(s, reply, content_ptr, 0);
  898. }
  899. static void rtsp_send_cmd_with_content(AVFormatContext *s,
  900. const char *cmd,
  901. RTSPMessageHeader *reply,
  902. unsigned char **content_ptr,
  903. const unsigned char *send_content,
  904. int send_content_length)
  905. {
  906. rtsp_send_cmd_with_content_async(s, cmd, send_content, send_content_length);
  907. rtsp_read_reply(s, reply, content_ptr, 0);
  908. }
  909. /**
  910. * @returns 0 on success, <0 on error, 1 if protocol is unavailable.
  911. */
  912. static int make_setup_request(AVFormatContext *s, const char *host, int port,
  913. int lower_transport, const char *real_challenge)
  914. {
  915. RTSPState *rt = s->priv_data;
  916. int rtx, j, i, err, interleave = 0;
  917. RTSPStream *rtsp_st;
  918. RTSPMessageHeader reply1, *reply = &reply1;
  919. char cmd[2048];
  920. const char *trans_pref;
  921. if (rt->transport == RTSP_TRANSPORT_RDT)
  922. trans_pref = "x-pn-tng";
  923. else
  924. trans_pref = "RTP/AVP";
  925. /* default timeout: 1 minute */
  926. rt->timeout = 60;
  927. /* for each stream, make the setup request */
  928. /* XXX: we assume the same server is used for the control of each
  929. * RTSP stream */
  930. for (j = RTSP_RTP_PORT_MIN, i = 0; i < rt->nb_rtsp_streams; ++i) {
  931. char transport[2048];
  932. /**
  933. * WMS serves all UDP data over a single connection, the RTX, which
  934. * isn't necessarily the first in the SDP but has to be the first
  935. * to be set up, else the second/third SETUP will fail with a 461.
  936. */
  937. if (lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
  938. rt->server_type == RTSP_SERVER_WMS) {
  939. if (i == 0) {
  940. /* rtx first */
  941. for (rtx = 0; rtx < rt->nb_rtsp_streams; rtx++) {
  942. int len = strlen(rt->rtsp_streams[rtx]->control_url);
  943. if (len >= 4 &&
  944. !strcmp(rt->rtsp_streams[rtx]->control_url + len - 4,
  945. "/rtx"))
  946. break;
  947. }
  948. if (rtx == rt->nb_rtsp_streams)
  949. return -1; /* no RTX found */
  950. rtsp_st = rt->rtsp_streams[rtx];
  951. } else
  952. rtsp_st = rt->rtsp_streams[i > rtx ? i : i - 1];
  953. } else
  954. rtsp_st = rt->rtsp_streams[i];
  955. /* RTP/UDP */
  956. if (lower_transport == RTSP_LOWER_TRANSPORT_UDP) {
  957. char buf[256];
  958. if (rt->server_type == RTSP_SERVER_WMS && i > 1) {
  959. port = reply->transports[0].client_port_min;
  960. goto have_port;
  961. }
  962. /* first try in specified port range */
  963. if (RTSP_RTP_PORT_MIN != 0) {
  964. while (j <= RTSP_RTP_PORT_MAX) {
  965. snprintf(buf, sizeof(buf), "rtp://%s?localport=%d",
  966. host, j);
  967. /* we will use two ports per rtp stream (rtp and rtcp) */
  968. j += 2;
  969. if (url_open(&rtsp_st->rtp_handle, buf, URL_RDWR) == 0)
  970. goto rtp_opened;
  971. }
  972. }
  973. #if 0
  974. /* then try on any port */
  975. if (url_open(&rtsp_st->rtp_handle, "rtp://", URL_RDONLY) < 0) {
  976. err = AVERROR_INVALIDDATA;
  977. goto fail;
  978. }
  979. #endif
  980. rtp_opened:
  981. port = rtp_get_local_port(rtsp_st->rtp_handle);
  982. have_port:
  983. snprintf(transport, sizeof(transport) - 1,
  984. "%s/UDP;", trans_pref);
  985. if (rt->server_type != RTSP_SERVER_REAL)
  986. av_strlcat(transport, "unicast;", sizeof(transport));
  987. av_strlcatf(transport, sizeof(transport),
  988. "client_port=%d", port);
  989. if (rt->transport == RTSP_TRANSPORT_RTP &&
  990. !(rt->server_type == RTSP_SERVER_WMS && i > 0))
  991. av_strlcatf(transport, sizeof(transport), "-%d", port + 1);
  992. }
  993. /* RTP/TCP */
  994. else if (lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
  995. /** For WMS streams, the application streams are only used for
  996. * UDP. When trying to set it up for TCP streams, the server
  997. * will return an error. Therefore, we skip those streams. */
  998. if (rt->server_type == RTSP_SERVER_WMS &&
  999. s->streams[rtsp_st->stream_index]->codec->codec_type ==
  1000. CODEC_TYPE_DATA)
  1001. continue;
  1002. snprintf(transport, sizeof(transport) - 1,
  1003. "%s/TCP;", trans_pref);
  1004. if (rt->server_type == RTSP_SERVER_WMS)
  1005. av_strlcat(transport, "unicast;", sizeof(transport));
  1006. av_strlcatf(transport, sizeof(transport),
  1007. "interleaved=%d-%d",
  1008. interleave, interleave + 1);
  1009. interleave += 2;
  1010. }
  1011. else if (lower_transport == RTSP_LOWER_TRANSPORT_UDP_MULTICAST) {
  1012. snprintf(transport, sizeof(transport) - 1,
  1013. "%s/UDP;multicast", trans_pref);
  1014. }
  1015. if (rt->server_type == RTSP_SERVER_REAL ||
  1016. rt->server_type == RTSP_SERVER_WMS)
  1017. av_strlcat(transport, ";mode=play", sizeof(transport));
  1018. snprintf(cmd, sizeof(cmd),
  1019. "SETUP %s RTSP/1.0\r\n"
  1020. "Transport: %s\r\n",
  1021. rtsp_st->control_url, transport);
  1022. if (i == 0 && rt->server_type == RTSP_SERVER_REAL) {
  1023. char real_res[41], real_csum[9];
  1024. ff_rdt_calc_response_and_checksum(real_res, real_csum,
  1025. real_challenge);
  1026. av_strlcatf(cmd, sizeof(cmd),
  1027. "If-Match: %s\r\n"
  1028. "RealChallenge2: %s, sd=%s\r\n",
  1029. rt->session_id, real_res, real_csum);
  1030. }
  1031. rtsp_send_cmd(s, cmd, reply, NULL);
  1032. if (reply->status_code == 461 /* Unsupported protocol */ && i == 0) {
  1033. err = 1;
  1034. goto fail;
  1035. } else if (reply->status_code != RTSP_STATUS_OK ||
  1036. reply->nb_transports != 1) {
  1037. err = AVERROR_INVALIDDATA;
  1038. goto fail;
  1039. }
  1040. /* XXX: same protocol for all streams is required */
  1041. if (i > 0) {
  1042. if (reply->transports[0].lower_transport != rt->lower_transport ||
  1043. reply->transports[0].transport != rt->transport) {
  1044. err = AVERROR_INVALIDDATA;
  1045. goto fail;
  1046. }
  1047. } else {
  1048. rt->lower_transport = reply->transports[0].lower_transport;
  1049. rt->transport = reply->transports[0].transport;
  1050. }
  1051. /* close RTP connection if not choosen */
  1052. if (reply->transports[0].lower_transport != RTSP_LOWER_TRANSPORT_UDP &&
  1053. (lower_transport == RTSP_LOWER_TRANSPORT_UDP)) {
  1054. url_close(rtsp_st->rtp_handle);
  1055. rtsp_st->rtp_handle = NULL;
  1056. }
  1057. switch(reply->transports[0].lower_transport) {
  1058. case RTSP_LOWER_TRANSPORT_TCP:
  1059. rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
  1060. rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
  1061. break;
  1062. case RTSP_LOWER_TRANSPORT_UDP: {
  1063. char url[1024];
  1064. /* XXX: also use address if specified */
  1065. snprintf(url, sizeof(url), "rtp://%s:%d",
  1066. host, reply->transports[0].server_port_min);
  1067. if (!(rt->server_type == RTSP_SERVER_WMS && i > 1) &&
  1068. rtp_set_remote_url(rtsp_st->rtp_handle, url) < 0) {
  1069. err = AVERROR_INVALIDDATA;
  1070. goto fail;
  1071. }
  1072. /* Try to initialize the connection state in a
  1073. * potential NAT router by sending dummy packets.
  1074. * RTP/RTCP dummy packets are used for RDT, too.
  1075. */
  1076. if (!(rt->server_type == RTSP_SERVER_WMS && i > 1))
  1077. rtp_send_punch_packets(rtsp_st->rtp_handle);
  1078. break;
  1079. }
  1080. case RTSP_LOWER_TRANSPORT_UDP_MULTICAST: {
  1081. char url[1024];
  1082. struct in_addr in;
  1083. int port, ttl;
  1084. if (reply->transports[0].destination) {
  1085. in.s_addr = htonl(reply->transports[0].destination);
  1086. port = reply->transports[0].port_min;
  1087. ttl = reply->transports[0].ttl;
  1088. } else {
  1089. in = rtsp_st->sdp_ip;
  1090. port = rtsp_st->sdp_port;
  1091. ttl = rtsp_st->sdp_ttl;
  1092. }
  1093. snprintf(url, sizeof(url), "rtp://%s:%d?ttl=%d",
  1094. inet_ntoa(in), port, ttl);
  1095. if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
  1096. err = AVERROR_INVALIDDATA;
  1097. goto fail;
  1098. }
  1099. break;
  1100. }
  1101. }
  1102. if ((err = rtsp_open_transport_ctx(s, rtsp_st)))
  1103. goto fail;
  1104. }
  1105. if (reply->timeout > 0)
  1106. rt->timeout = reply->timeout;
  1107. if (rt->server_type == RTSP_SERVER_REAL)
  1108. rt->need_subscription = 1;
  1109. return 0;
  1110. fail:
  1111. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1112. if (rt->rtsp_streams[i]->rtp_handle) {
  1113. url_close(rt->rtsp_streams[i]->rtp_handle);
  1114. rt->rtsp_streams[i]->rtp_handle = NULL;
  1115. }
  1116. }
  1117. return err;
  1118. }
  1119. static int rtsp_read_play(AVFormatContext *s)
  1120. {
  1121. RTSPState *rt = s->priv_data;
  1122. RTSPMessageHeader reply1, *reply = &reply1;
  1123. char cmd[1024];
  1124. av_log(s, AV_LOG_DEBUG, "hello state=%d\n", rt->state);
  1125. if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
  1126. if (rt->state == RTSP_STATE_PAUSED) {
  1127. snprintf(cmd, sizeof(cmd),
  1128. "PLAY %s RTSP/1.0\r\n",
  1129. rt->control_uri);
  1130. } else {
  1131. snprintf(cmd, sizeof(cmd),
  1132. "PLAY %s RTSP/1.0\r\n"
  1133. "Range: npt=%0.3f-\r\n",
  1134. rt->control_uri,
  1135. (double)rt->seek_timestamp / AV_TIME_BASE);
  1136. }
  1137. rtsp_send_cmd(s, cmd, reply, NULL);
  1138. if (reply->status_code != RTSP_STATUS_OK) {
  1139. return -1;
  1140. }
  1141. }
  1142. rt->state = RTSP_STATE_STREAMING;
  1143. return 0;
  1144. }
  1145. static int rtsp_read_header(AVFormatContext *s,
  1146. AVFormatParameters *ap)
  1147. {
  1148. RTSPState *rt = s->priv_data;
  1149. char host[1024], path[1024], tcpname[1024], cmd[2048], auth[128];
  1150. char *option_list, *option, *filename;
  1151. URLContext *rtsp_hd;
  1152. int port, ret, err;
  1153. RTSPMessageHeader reply1, *reply = &reply1;
  1154. unsigned char *content = NULL;
  1155. int lower_transport_mask = 0;
  1156. char real_challenge[64];
  1157. redirect:
  1158. /* extract hostname and port */
  1159. url_split(NULL, 0, auth, sizeof(auth),
  1160. host, sizeof(host), &port, path, sizeof(path), s->filename);
  1161. if (*auth) {
  1162. int auth_len = strlen(auth), b64_len = ((auth_len + 2) / 3) * 4 + 1;
  1163. if (!(rt->auth_b64 = av_malloc(b64_len)))
  1164. return AVERROR(ENOMEM);
  1165. if (!av_base64_encode(rt->auth_b64, b64_len, auth, auth_len)) {
  1166. err = AVERROR(EINVAL);
  1167. goto fail;
  1168. }
  1169. }
  1170. if (port < 0)
  1171. port = RTSP_DEFAULT_PORT;
  1172. /* search for options */
  1173. option_list = strchr(path, '?');
  1174. if (option_list) {
  1175. filename = strchr(s->filename, '?');
  1176. while (option_list) {
  1177. /* move the option pointer */
  1178. option = ++option_list;
  1179. option_list = strchr(option_list, '&');
  1180. if (option_list)
  1181. *option_list = 0;
  1182. /* handle the options */
  1183. if (!strcmp(option, "udp")) {
  1184. lower_transport_mask = (1<< RTSP_LOWER_TRANSPORT_UDP);
  1185. } else if (!strcmp(option, "multicast")) {
  1186. lower_transport_mask = (1<< RTSP_LOWER_TRANSPORT_UDP_MULTICAST);
  1187. } else if (!strcmp(option, "tcp")) {
  1188. lower_transport_mask = (1<< RTSP_LOWER_TRANSPORT_TCP);
  1189. } else {
  1190. strcpy(++filename, option);
  1191. filename += strlen(option);
  1192. if (option_list) *filename = '&';
  1193. }
  1194. }
  1195. *filename = 0;
  1196. }
  1197. if (!lower_transport_mask)
  1198. lower_transport_mask = (1 << RTSP_LOWER_TRANSPORT_NB) - 1;
  1199. /* open the tcp connexion */
  1200. snprintf(tcpname, sizeof(tcpname), "tcp://%s:%d", host, port);
  1201. if (url_open(&rtsp_hd, tcpname, URL_RDWR) < 0) {
  1202. err = AVERROR(EIO);
  1203. goto fail;
  1204. }
  1205. rt->rtsp_hd = rtsp_hd;
  1206. rt->seq = 0;
  1207. /* request options supported by the server; this also detects server
  1208. * type */
  1209. av_strlcpy(rt->control_uri, s->filename,
  1210. sizeof(rt->control_uri));
  1211. for (rt->server_type = RTSP_SERVER_RTP;;) {
  1212. snprintf(cmd, sizeof(cmd),
  1213. "OPTIONS %s RTSP/1.0\r\n", s->filename);
  1214. if (rt->server_type == RTSP_SERVER_REAL)
  1215. av_strlcat(cmd,
  1216. /**
  1217. * The following entries are required for proper
  1218. * streaming from a Realmedia server. They are
  1219. * interdependent in some way although we currently
  1220. * don't quite understand how. Values were copied
  1221. * from mplayer SVN r23589.
  1222. * @param CompanyID is a 16-byte ID in base64
  1223. * @param ClientChallenge is a 16-byte ID in hex
  1224. */
  1225. "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7\r\n"
  1226. "PlayerStarttime: [28/03/2003:22:50:23 00:00]\r\n"
  1227. "CompanyID: KnKV4M4I/B2FjJ1TToLycw==\r\n"
  1228. "GUID: 00000000-0000-0000-0000-000000000000\r\n",
  1229. sizeof(cmd));
  1230. rtsp_send_cmd(s, cmd, reply, NULL);
  1231. if (reply->status_code != RTSP_STATUS_OK) {
  1232. err = AVERROR_INVALIDDATA;
  1233. goto fail;
  1234. }
  1235. /* detect server type if not standard-compliant RTP */
  1236. if (rt->server_type != RTSP_SERVER_REAL && reply->real_challenge[0]) {
  1237. rt->server_type = RTSP_SERVER_REAL;
  1238. continue;
  1239. } else if (!strncasecmp(reply->server, "WMServer/", 9)) {
  1240. rt->server_type = RTSP_SERVER_WMS;
  1241. } else if (rt->server_type == RTSP_SERVER_REAL)
  1242. strcpy(real_challenge, reply->real_challenge);
  1243. break;
  1244. }
  1245. /* describe the stream */
  1246. snprintf(cmd, sizeof(cmd),
  1247. "DESCRIBE %s RTSP/1.0\r\n"
  1248. "Accept: application/sdp\r\n",
  1249. s->filename);
  1250. if (rt->server_type == RTSP_SERVER_REAL) {
  1251. /**
  1252. * The Require: attribute is needed for proper streaming from
  1253. * Realmedia servers.
  1254. */
  1255. av_strlcat(cmd,
  1256. "Require: com.real.retain-entity-for-setup\r\n",
  1257. sizeof(cmd));
  1258. }
  1259. rtsp_send_cmd(s, cmd, reply, &content);
  1260. if (!content) {
  1261. err = AVERROR_INVALIDDATA;
  1262. goto fail;
  1263. }
  1264. if (reply->status_code != RTSP_STATUS_OK) {
  1265. err = AVERROR_INVALIDDATA;
  1266. goto fail;
  1267. }
  1268. /* now we got the SDP description, we parse it */
  1269. ret = sdp_parse(s, (const char *)content);
  1270. av_freep(&content);
  1271. if (ret < 0) {
  1272. err = AVERROR_INVALIDDATA;
  1273. goto fail;
  1274. }
  1275. do {
  1276. int lower_transport = ff_log2_tab[lower_transport_mask &
  1277. ~(lower_transport_mask - 1)];
  1278. err = make_setup_request(s, host, port, lower_transport,
  1279. rt->server_type == RTSP_SERVER_REAL ?
  1280. real_challenge : NULL);
  1281. if (err < 0)
  1282. goto fail;
  1283. lower_transport_mask &= ~(1 << lower_transport);
  1284. if (lower_transport_mask == 0 && err == 1) {
  1285. err = AVERROR(FF_NETERROR(EPROTONOSUPPORT));
  1286. goto fail;
  1287. }
  1288. } while (err);
  1289. rt->state = RTSP_STATE_IDLE;
  1290. rt->seek_timestamp = 0; /* default is to start stream at position zero */
  1291. if (ap->initial_pause) {
  1292. /* do not start immediately */
  1293. } else {
  1294. if (rtsp_read_play(s) < 0) {
  1295. err = AVERROR_INVALIDDATA;
  1296. goto fail;
  1297. }
  1298. }
  1299. return 0;
  1300. fail:
  1301. rtsp_close_streams(rt);
  1302. av_freep(&content);
  1303. url_close(rt->rtsp_hd);
  1304. if (reply->status_code >=300 && reply->status_code < 400) {
  1305. av_strlcpy(s->filename, reply->location, sizeof(s->filename));
  1306. av_log(s, AV_LOG_INFO, "Status %d: Redirecting to %s\n",
  1307. reply->status_code,
  1308. s->filename);
  1309. goto redirect;
  1310. }
  1311. return err;
  1312. }
  1313. static int udp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  1314. uint8_t *buf, int buf_size)
  1315. {
  1316. RTSPState *rt = s->priv_data;
  1317. RTSPStream *rtsp_st;
  1318. fd_set rfds;
  1319. int fd, fd_max, n, i, ret, tcp_fd;
  1320. struct timeval tv;
  1321. for (;;) {
  1322. if (url_interrupt_cb())
  1323. return AVERROR(EINTR);
  1324. FD_ZERO(&rfds);
  1325. if (rt->rtsp_hd) {
  1326. tcp_fd = fd_max = url_get_file_handle(rt->rtsp_hd);
  1327. FD_SET(tcp_fd, &rfds);
  1328. } else {
  1329. fd_max = 0;
  1330. tcp_fd = -1;
  1331. }
  1332. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1333. rtsp_st = rt->rtsp_streams[i];
  1334. if (rtsp_st->rtp_handle) {
  1335. /* currently, we cannot probe RTCP handle because of
  1336. * blocking restrictions */
  1337. fd = url_get_file_handle(rtsp_st->rtp_handle);
  1338. if (fd > fd_max)
  1339. fd_max = fd;
  1340. FD_SET(fd, &rfds);
  1341. }
  1342. }
  1343. tv.tv_sec = 0;
  1344. tv.tv_usec = 100 * 1000;
  1345. n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
  1346. if (n > 0) {
  1347. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1348. rtsp_st = rt->rtsp_streams[i];
  1349. if (rtsp_st->rtp_handle) {
  1350. fd = url_get_file_handle(rtsp_st->rtp_handle);
  1351. if (FD_ISSET(fd, &rfds)) {
  1352. ret = url_read(rtsp_st->rtp_handle, buf, buf_size);
  1353. if (ret > 0) {
  1354. *prtsp_st = rtsp_st;
  1355. return ret;
  1356. }
  1357. }
  1358. }
  1359. }
  1360. #if CONFIG_RTSP_DEMUXER
  1361. if (tcp_fd != -1 && FD_ISSET(tcp_fd, &rfds)) {
  1362. RTSPMessageHeader reply;
  1363. rtsp_read_reply(s, &reply, NULL, 0);
  1364. /* XXX: parse message */
  1365. if (rt->state != RTSP_STATE_STREAMING)
  1366. return 0;
  1367. }
  1368. #endif
  1369. }
  1370. }
  1371. }
  1372. static int tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  1373. uint8_t *buf, int buf_size)
  1374. {
  1375. RTSPState *rt = s->priv_data;
  1376. int id, len, i, ret;
  1377. RTSPStream *rtsp_st;
  1378. #ifdef DEBUG_RTP_TCP
  1379. dprintf(s, "tcp_read_packet:\n");
  1380. #endif
  1381. redo:
  1382. for (;;) {
  1383. RTSPMessageHeader reply;
  1384. ret = rtsp_read_reply(s, &reply, NULL, 1);
  1385. if (ret == -1)
  1386. return -1;
  1387. if (ret == 1) /* received '$' */
  1388. break;
  1389. /* XXX: parse message */
  1390. if (rt->state != RTSP_STATE_STREAMING)
  1391. return 0;
  1392. }
  1393. ret = url_read_complete(rt->rtsp_hd, buf, 3);
  1394. if (ret != 3)
  1395. return -1;
  1396. id = buf[0];
  1397. len = AV_RB16(buf + 1);
  1398. #ifdef DEBUG_RTP_TCP
  1399. dprintf(s, "id=%d len=%d\n", id, len);
  1400. #endif
  1401. if (len > buf_size || len < 12)
  1402. goto redo;
  1403. /* get the data */
  1404. ret = url_read_complete(rt->rtsp_hd, buf, len);
  1405. if (ret != len)
  1406. return -1;
  1407. if (rt->transport == RTSP_TRANSPORT_RDT &&
  1408. ff_rdt_parse_header(buf, len, &id, NULL, NULL, NULL, NULL) < 0)
  1409. return -1;
  1410. /* find the matching stream */
  1411. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1412. rtsp_st = rt->rtsp_streams[i];
  1413. if (id >= rtsp_st->interleaved_min &&
  1414. id <= rtsp_st->interleaved_max)
  1415. goto found;
  1416. }
  1417. goto redo;
  1418. found:
  1419. *prtsp_st = rtsp_st;
  1420. return len;
  1421. }
  1422. static int rtsp_fetch_packet(AVFormatContext *s, AVPacket *pkt)
  1423. {
  1424. RTSPState *rt = s->priv_data;
  1425. int ret, len;
  1426. uint8_t buf[10 * RTP_MAX_PACKET_LENGTH];
  1427. RTSPStream *rtsp_st;
  1428. /* get next frames from the same RTP packet */
  1429. if (rt->cur_transport_priv) {
  1430. if (rt->transport == RTSP_TRANSPORT_RDT) {
  1431. ret = ff_rdt_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
  1432. } else
  1433. ret = rtp_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
  1434. if (ret == 0) {
  1435. rt->cur_transport_priv = NULL;
  1436. return 0;
  1437. } else if (ret == 1) {
  1438. return 0;
  1439. } else
  1440. rt->cur_transport_priv = NULL;
  1441. }
  1442. /* read next RTP packet */
  1443. redo:
  1444. switch(rt->lower_transport) {
  1445. default:
  1446. #if CONFIG_RTSP_DEMUXER
  1447. case RTSP_LOWER_TRANSPORT_TCP:
  1448. len = tcp_read_packet(s, &rtsp_st, buf, sizeof(buf));
  1449. break;
  1450. #endif
  1451. case RTSP_LOWER_TRANSPORT_UDP:
  1452. case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
  1453. len = udp_read_packet(s, &rtsp_st, buf, sizeof(buf));
  1454. if (len >=0 && rtsp_st->transport_priv && rt->transport == RTSP_TRANSPORT_RTP)
  1455. rtp_check_and_send_back_rr(rtsp_st->transport_priv, len);
  1456. break;
  1457. }
  1458. if (len < 0)
  1459. return len;
  1460. if (len == 0)
  1461. return AVERROR_EOF;
  1462. if (rt->transport == RTSP_TRANSPORT_RDT) {
  1463. ret = ff_rdt_parse_packet(rtsp_st->transport_priv, pkt, buf, len);
  1464. } else
  1465. ret = rtp_parse_packet(rtsp_st->transport_priv, pkt, buf, len);
  1466. if (ret < 0)
  1467. goto redo;
  1468. if (ret == 1)
  1469. /* more packets may follow, so we save the RTP context */
  1470. rt->cur_transport_priv = rtsp_st->transport_priv;
  1471. return ret;
  1472. }
  1473. static int rtsp_read_packet(AVFormatContext *s, AVPacket *pkt)
  1474. {
  1475. RTSPState *rt = s->priv_data;
  1476. int ret;
  1477. RTSPMessageHeader reply1, *reply = &reply1;
  1478. char cmd[1024];
  1479. if (rt->server_type == RTSP_SERVER_REAL) {
  1480. int i;
  1481. enum AVDiscard cache[MAX_STREAMS];
  1482. for (i = 0; i < s->nb_streams; i++)
  1483. cache[i] = s->streams[i]->discard;
  1484. if (!rt->need_subscription) {
  1485. if (memcmp (cache, rt->real_setup_cache,
  1486. sizeof(enum AVDiscard) * s->nb_streams)) {
  1487. snprintf(cmd, sizeof(cmd),
  1488. "SET_PARAMETER %s RTSP/1.0\r\n"
  1489. "Unsubscribe: %s\r\n",
  1490. rt->control_uri, rt->last_subscription);
  1491. rtsp_send_cmd(s, cmd, reply, NULL);
  1492. if (reply->status_code != RTSP_STATUS_OK)
  1493. return AVERROR_INVALIDDATA;
  1494. rt->need_subscription = 1;
  1495. }
  1496. }
  1497. if (rt->need_subscription) {
  1498. int r, rule_nr, first = 1;
  1499. memcpy(rt->real_setup_cache, cache,
  1500. sizeof(enum AVDiscard) * s->nb_streams);
  1501. rt->last_subscription[0] = 0;
  1502. snprintf(cmd, sizeof(cmd),
  1503. "SET_PARAMETER %s RTSP/1.0\r\n"
  1504. "Subscribe: ",
  1505. rt->control_uri);
  1506. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1507. rule_nr = 0;
  1508. for (r = 0; r < s->nb_streams; r++) {
  1509. if (s->streams[r]->priv_data == rt->rtsp_streams[i]) {
  1510. if (s->streams[r]->discard != AVDISCARD_ALL) {
  1511. if (!first)
  1512. av_strlcat(rt->last_subscription, ",",
  1513. sizeof(rt->last_subscription));
  1514. ff_rdt_subscribe_rule(
  1515. rt->last_subscription,
  1516. sizeof(rt->last_subscription), i, rule_nr);
  1517. first = 0;
  1518. }
  1519. rule_nr++;
  1520. }
  1521. }
  1522. }
  1523. av_strlcatf(cmd, sizeof(cmd), "%s\r\n", rt->last_subscription);
  1524. rtsp_send_cmd(s, cmd, reply, NULL);
  1525. if (reply->status_code != RTSP_STATUS_OK)
  1526. return AVERROR_INVALIDDATA;
  1527. rt->need_subscription = 0;
  1528. if (rt->state == RTSP_STATE_STREAMING)
  1529. rtsp_read_play (s);
  1530. }
  1531. }
  1532. ret = rtsp_fetch_packet(s, pkt);
  1533. if (ret < 0)
  1534. return ret;
  1535. /* send dummy request to keep TCP connection alive */
  1536. if ((rt->server_type == RTSP_SERVER_WMS ||
  1537. rt->server_type == RTSP_SERVER_REAL) &&
  1538. (av_gettime() - rt->last_cmd_time) / 1000000 >= rt->timeout / 2) {
  1539. if (rt->server_type == RTSP_SERVER_WMS) {
  1540. snprintf(cmd, sizeof(cmd) - 1,
  1541. "GET_PARAMETER %s RTSP/1.0\r\n",
  1542. rt->control_uri);
  1543. rtsp_send_cmd_async(s, cmd);
  1544. } else {
  1545. rtsp_send_cmd_async(s, "OPTIONS * RTSP/1.0\r\n");
  1546. }
  1547. }
  1548. return 0;
  1549. }
  1550. /* pause the stream */
  1551. static int rtsp_read_pause(AVFormatContext *s)
  1552. {
  1553. RTSPState *rt = s->priv_data;
  1554. RTSPMessageHeader reply1, *reply = &reply1;
  1555. char cmd[1024];
  1556. rt = s->priv_data;
  1557. if (rt->state != RTSP_STATE_STREAMING)
  1558. return 0;
  1559. else if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
  1560. snprintf(cmd, sizeof(cmd),
  1561. "PAUSE %s RTSP/1.0\r\n",
  1562. rt->control_uri);
  1563. rtsp_send_cmd(s, cmd, reply, NULL);
  1564. if (reply->status_code != RTSP_STATUS_OK) {
  1565. return -1;
  1566. }
  1567. }
  1568. rt->state = RTSP_STATE_PAUSED;
  1569. return 0;
  1570. }
  1571. static int rtsp_read_seek(AVFormatContext *s, int stream_index,
  1572. int64_t timestamp, int flags)
  1573. {
  1574. RTSPState *rt = s->priv_data;
  1575. rt->seek_timestamp = av_rescale_q(timestamp,
  1576. s->streams[stream_index]->time_base,
  1577. AV_TIME_BASE_Q);
  1578. switch(rt->state) {
  1579. default:
  1580. case RTSP_STATE_IDLE:
  1581. break;
  1582. case RTSP_STATE_STREAMING:
  1583. if (rtsp_read_pause(s) != 0)
  1584. return -1;
  1585. rt->state = RTSP_STATE_SEEKING;
  1586. if (rtsp_read_play(s) != 0)
  1587. return -1;
  1588. break;
  1589. case RTSP_STATE_PAUSED:
  1590. rt->state = RTSP_STATE_IDLE;
  1591. break;
  1592. }
  1593. return 0;
  1594. }
  1595. static int rtsp_read_close(AVFormatContext *s)
  1596. {
  1597. RTSPState *rt = s->priv_data;
  1598. char cmd[1024];
  1599. #if 0
  1600. /* NOTE: it is valid to flush the buffer here */
  1601. if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
  1602. url_fclose(&rt->rtsp_gb);
  1603. }
  1604. #endif
  1605. snprintf(cmd, sizeof(cmd),
  1606. "TEARDOWN %s RTSP/1.0\r\n",
  1607. s->filename);
  1608. rtsp_send_cmd_async(s, cmd);
  1609. rtsp_close_streams(rt);
  1610. url_close(rt->rtsp_hd);
  1611. return 0;
  1612. }
  1613. AVInputFormat rtsp_demuxer = {
  1614. "rtsp",
  1615. NULL_IF_CONFIG_SMALL("RTSP input format"),
  1616. sizeof(RTSPState),
  1617. rtsp_probe,
  1618. rtsp_read_header,
  1619. rtsp_read_packet,
  1620. rtsp_read_close,
  1621. rtsp_read_seek,
  1622. .flags = AVFMT_NOFILE,
  1623. .read_play = rtsp_read_play,
  1624. .read_pause = rtsp_read_pause,
  1625. };
  1626. #endif
  1627. static int sdp_probe(AVProbeData *p1)
  1628. {
  1629. const char *p = p1->buf, *p_end = p1->buf + p1->buf_size;
  1630. /* we look for a line beginning "c=IN IP4" */
  1631. while (p < p_end && *p != '\0') {
  1632. if (p + sizeof("c=IN IP4") - 1 < p_end &&
  1633. av_strstart(p, "c=IN IP4", NULL))
  1634. return AVPROBE_SCORE_MAX / 2;
  1635. while (p < p_end - 1 && *p != '\n') p++;
  1636. if (++p >= p_end)
  1637. break;
  1638. if (*p == '\r')
  1639. p++;
  1640. }
  1641. return 0;
  1642. }
  1643. #define SDP_MAX_SIZE 8192
  1644. static int sdp_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1645. {
  1646. RTSPState *rt = s->priv_data;
  1647. RTSPStream *rtsp_st;
  1648. int size, i, err;
  1649. char *content;
  1650. char url[1024];
  1651. /* read the whole sdp file */
  1652. /* XXX: better loading */
  1653. content = av_malloc(SDP_MAX_SIZE);
  1654. size = get_buffer(s->pb, content, SDP_MAX_SIZE - 1);
  1655. if (size <= 0) {
  1656. av_free(content);
  1657. return AVERROR_INVALIDDATA;
  1658. }
  1659. content[size] ='\0';
  1660. sdp_parse(s, content);
  1661. av_free(content);
  1662. /* open each RTP stream */
  1663. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1664. rtsp_st = rt->rtsp_streams[i];
  1665. snprintf(url, sizeof(url), "rtp://%s:%d?localport=%d&ttl=%d",
  1666. inet_ntoa(rtsp_st->sdp_ip),
  1667. rtsp_st->sdp_port,
  1668. rtsp_st->sdp_port,
  1669. rtsp_st->sdp_ttl);
  1670. if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
  1671. err = AVERROR_INVALIDDATA;
  1672. goto fail;
  1673. }
  1674. if ((err = rtsp_open_transport_ctx(s, rtsp_st)))
  1675. goto fail;
  1676. }
  1677. return 0;
  1678. fail:
  1679. rtsp_close_streams(rt);
  1680. return err;
  1681. }
  1682. static int sdp_read_close(AVFormatContext *s)
  1683. {
  1684. RTSPState *rt = s->priv_data;
  1685. rtsp_close_streams(rt);
  1686. return 0;
  1687. }
  1688. AVInputFormat sdp_demuxer = {
  1689. "sdp",
  1690. NULL_IF_CONFIG_SMALL("SDP"),
  1691. sizeof(RTSPState),
  1692. sdp_probe,
  1693. sdp_read_header,
  1694. rtsp_fetch_packet,
  1695. sdp_read_close,
  1696. };