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.

2222 lines
80KB

  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 "libavutil/avassert.h"
  22. #include "libavutil/base64.h"
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/intreadwrite.h"
  25. #include "libavutil/mathematics.h"
  26. #include "libavutil/parseutils.h"
  27. #include "libavutil/random_seed.h"
  28. #include "libavutil/dict.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/time.h"
  31. #include "avformat.h"
  32. #include "avio_internal.h"
  33. #if HAVE_POLL_H
  34. #include <poll.h>
  35. #endif
  36. #include "internal.h"
  37. #include "network.h"
  38. #include "os_support.h"
  39. #include "http.h"
  40. #include "rtsp.h"
  41. #include "rtpdec.h"
  42. #include "rdt.h"
  43. #include "rtpdec_formats.h"
  44. #include "rtpenc_chain.h"
  45. #include "url.h"
  46. #include "rtpenc.h"
  47. #include "mpegts.h"
  48. //#define DEBUG
  49. /* Timeout values for socket poll, in ms,
  50. * and read_packet(), in seconds */
  51. #define POLL_TIMEOUT_MS 100
  52. #define READ_PACKET_TIMEOUT_S 10
  53. #define MAX_TIMEOUTS READ_PACKET_TIMEOUT_S * 1000 / POLL_TIMEOUT_MS
  54. #define SDP_MAX_SIZE 16384
  55. #define RECVBUF_SIZE 10 * RTP_MAX_PACKET_LENGTH
  56. #define DEFAULT_REORDERING_DELAY 100000
  57. #define OFFSET(x) offsetof(RTSPState, x)
  58. #define DEC AV_OPT_FLAG_DECODING_PARAM
  59. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  60. #define RTSP_FLAG_OPTS(name, longname) \
  61. { name, longname, OFFSET(rtsp_flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, DEC, "rtsp_flags" }, \
  62. { "filter_src", "Only receive packets from the negotiated peer IP", 0, AV_OPT_TYPE_CONST, {.i64 = RTSP_FLAG_FILTER_SRC}, 0, 0, DEC, "rtsp_flags" }, \
  63. { "listen", "Wait for incoming connections", 0, AV_OPT_TYPE_CONST, {.i64 = RTSP_FLAG_LISTEN}, 0, 0, DEC, "rtsp_flags" }
  64. #define RTSP_MEDIATYPE_OPTS(name, longname) \
  65. { name, longname, OFFSET(media_type_mask), AV_OPT_TYPE_FLAGS, { .i64 = (1 << (AVMEDIA_TYPE_DATA+1)) - 1 }, INT_MIN, INT_MAX, DEC, "allowed_media_types" }, \
  66. { "video", "Video", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << AVMEDIA_TYPE_VIDEO}, 0, 0, DEC, "allowed_media_types" }, \
  67. { "audio", "Audio", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << AVMEDIA_TYPE_AUDIO}, 0, 0, DEC, "allowed_media_types" }, \
  68. { "data", "Data", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << AVMEDIA_TYPE_DATA}, 0, 0, DEC, "allowed_media_types" }
  69. #define RTSP_REORDERING_OPTS() \
  70. { "reorder_queue_size", "Number of packets to buffer for handling of reordered packets", OFFSET(reordering_queue_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, DEC }
  71. const AVOption ff_rtsp_options[] = {
  72. { "initial_pause", "Don't start playing the stream immediately", OFFSET(initial_pause), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC },
  73. FF_RTP_FLAG_OPTS(RTSPState, rtp_muxer_flags),
  74. { "rtsp_transport", "RTSP transport protocols", OFFSET(lower_transport_mask), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, DEC|ENC, "rtsp_transport" }, \
  75. { "udp", "UDP", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << RTSP_LOWER_TRANSPORT_UDP}, 0, 0, DEC|ENC, "rtsp_transport" }, \
  76. { "tcp", "TCP", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << RTSP_LOWER_TRANSPORT_TCP}, 0, 0, DEC|ENC, "rtsp_transport" }, \
  77. { "udp_multicast", "UDP multicast", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << RTSP_LOWER_TRANSPORT_UDP_MULTICAST}, 0, 0, DEC, "rtsp_transport" },
  78. { "http", "HTTP tunneling", 0, AV_OPT_TYPE_CONST, {.i64 = (1 << RTSP_LOWER_TRANSPORT_HTTP)}, 0, 0, DEC, "rtsp_transport" },
  79. RTSP_FLAG_OPTS("rtsp_flags", "RTSP flags"),
  80. RTSP_MEDIATYPE_OPTS("allowed_media_types", "Media types to accept from the server"),
  81. { "min_port", "Minimum local UDP port", OFFSET(rtp_port_min), AV_OPT_TYPE_INT, {.i64 = RTSP_RTP_PORT_MIN}, 0, 65535, DEC|ENC },
  82. { "max_port", "Maximum local UDP port", OFFSET(rtp_port_max), AV_OPT_TYPE_INT, {.i64 = RTSP_RTP_PORT_MAX}, 0, 65535, DEC|ENC },
  83. { "timeout", "Maximum timeout (in seconds) to wait for incoming connections. -1 is infinite. Implies flag listen", OFFSET(initial_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, INT_MIN, INT_MAX, DEC },
  84. RTSP_REORDERING_OPTS(),
  85. { NULL },
  86. };
  87. static const AVOption sdp_options[] = {
  88. RTSP_FLAG_OPTS("sdp_flags", "SDP flags"),
  89. { "custom_io", "Use custom IO", 0, AV_OPT_TYPE_CONST, {.i64 = RTSP_FLAG_CUSTOM_IO}, 0, 0, DEC, "rtsp_flags" },
  90. RTSP_MEDIATYPE_OPTS("allowed_media_types", "Media types to accept from the server"),
  91. RTSP_REORDERING_OPTS(),
  92. { NULL },
  93. };
  94. static const AVOption rtp_options[] = {
  95. RTSP_FLAG_OPTS("rtp_flags", "RTP flags"),
  96. RTSP_REORDERING_OPTS(),
  97. { NULL },
  98. };
  99. static void get_word_until_chars(char *buf, int buf_size,
  100. const char *sep, const char **pp)
  101. {
  102. const char *p;
  103. char *q;
  104. p = *pp;
  105. p += strspn(p, SPACE_CHARS);
  106. q = buf;
  107. while (!strchr(sep, *p) && *p != '\0') {
  108. if ((q - buf) < buf_size - 1)
  109. *q++ = *p;
  110. p++;
  111. }
  112. if (buf_size > 0)
  113. *q = '\0';
  114. *pp = p;
  115. }
  116. static void get_word_sep(char *buf, int buf_size, const char *sep,
  117. const char **pp)
  118. {
  119. if (**pp == '/') (*pp)++;
  120. get_word_until_chars(buf, buf_size, sep, pp);
  121. }
  122. static void get_word(char *buf, int buf_size, const char **pp)
  123. {
  124. get_word_until_chars(buf, buf_size, SPACE_CHARS, pp);
  125. }
  126. /** Parse a string p in the form of Range:npt=xx-xx, and determine the start
  127. * and end time.
  128. * Used for seeking in the rtp stream.
  129. */
  130. static void rtsp_parse_range_npt(const char *p, int64_t *start, int64_t *end)
  131. {
  132. char buf[256];
  133. p += strspn(p, SPACE_CHARS);
  134. if (!av_stristart(p, "npt=", &p))
  135. return;
  136. *start = AV_NOPTS_VALUE;
  137. *end = AV_NOPTS_VALUE;
  138. get_word_sep(buf, sizeof(buf), "-", &p);
  139. av_parse_time(start, buf, 1);
  140. if (*p == '-') {
  141. p++;
  142. get_word_sep(buf, sizeof(buf), "-", &p);
  143. av_parse_time(end, buf, 1);
  144. }
  145. }
  146. static int get_sockaddr(const char *buf, struct sockaddr_storage *sock)
  147. {
  148. struct addrinfo hints = { 0 }, *ai = NULL;
  149. hints.ai_flags = AI_NUMERICHOST;
  150. if (getaddrinfo(buf, NULL, &hints, &ai))
  151. return -1;
  152. memcpy(sock, ai->ai_addr, FFMIN(sizeof(*sock), ai->ai_addrlen));
  153. freeaddrinfo(ai);
  154. return 0;
  155. }
  156. #if CONFIG_RTPDEC
  157. static void init_rtp_handler(RTPDynamicProtocolHandler *handler,
  158. RTSPStream *rtsp_st, AVCodecContext *codec)
  159. {
  160. if (!handler)
  161. return;
  162. codec->codec_id = handler->codec_id;
  163. rtsp_st->dynamic_handler = handler;
  164. if (handler->alloc) {
  165. rtsp_st->dynamic_protocol_context = handler->alloc();
  166. if (!rtsp_st->dynamic_protocol_context)
  167. rtsp_st->dynamic_handler = NULL;
  168. }
  169. }
  170. /* parse the rtpmap description: <codec_name>/<clock_rate>[/<other params>] */
  171. static int sdp_parse_rtpmap(AVFormatContext *s,
  172. AVStream *st, RTSPStream *rtsp_st,
  173. int payload_type, const char *p)
  174. {
  175. AVCodecContext *codec = st->codec;
  176. char buf[256];
  177. int i;
  178. AVCodec *c;
  179. const char *c_name;
  180. /* See if we can handle this kind of payload.
  181. * The space should normally not be there but some Real streams or
  182. * particular servers ("RealServer Version 6.1.3.970", see issue 1658)
  183. * have a trailing space. */
  184. get_word_sep(buf, sizeof(buf), "/ ", &p);
  185. if (payload_type < RTP_PT_PRIVATE) {
  186. /* We are in a standard case
  187. * (from http://www.iana.org/assignments/rtp-parameters). */
  188. codec->codec_id = ff_rtp_codec_id(buf, codec->codec_type);
  189. }
  190. if (codec->codec_id == AV_CODEC_ID_NONE) {
  191. RTPDynamicProtocolHandler *handler =
  192. ff_rtp_handler_find_by_name(buf, codec->codec_type);
  193. init_rtp_handler(handler, rtsp_st, codec);
  194. /* If no dynamic handler was found, check with the list of standard
  195. * allocated types, if such a stream for some reason happens to
  196. * use a private payload type. This isn't handled in rtpdec.c, since
  197. * the format name from the rtpmap line never is passed into rtpdec. */
  198. if (!rtsp_st->dynamic_handler)
  199. codec->codec_id = ff_rtp_codec_id(buf, codec->codec_type);
  200. }
  201. c = avcodec_find_decoder(codec->codec_id);
  202. if (c && c->name)
  203. c_name = c->name;
  204. else
  205. c_name = "(null)";
  206. get_word_sep(buf, sizeof(buf), "/", &p);
  207. i = atoi(buf);
  208. switch (codec->codec_type) {
  209. case AVMEDIA_TYPE_AUDIO:
  210. av_log(s, AV_LOG_DEBUG, "audio codec set to: %s\n", c_name);
  211. codec->sample_rate = RTSP_DEFAULT_AUDIO_SAMPLERATE;
  212. codec->channels = RTSP_DEFAULT_NB_AUDIO_CHANNELS;
  213. if (i > 0) {
  214. codec->sample_rate = i;
  215. avpriv_set_pts_info(st, 32, 1, codec->sample_rate);
  216. get_word_sep(buf, sizeof(buf), "/", &p);
  217. i = atoi(buf);
  218. if (i > 0)
  219. codec->channels = i;
  220. }
  221. av_log(s, AV_LOG_DEBUG, "audio samplerate set to: %i\n",
  222. codec->sample_rate);
  223. av_log(s, AV_LOG_DEBUG, "audio channels set to: %i\n",
  224. codec->channels);
  225. break;
  226. case AVMEDIA_TYPE_VIDEO:
  227. av_log(s, AV_LOG_DEBUG, "video codec set to: %s\n", c_name);
  228. if (i > 0)
  229. avpriv_set_pts_info(st, 32, 1, i);
  230. break;
  231. default:
  232. break;
  233. }
  234. if (rtsp_st->dynamic_handler && rtsp_st->dynamic_handler->init)
  235. rtsp_st->dynamic_handler->init(s, st->index,
  236. rtsp_st->dynamic_protocol_context);
  237. return 0;
  238. }
  239. /* parse the attribute line from the fmtp a line of an sdp response. This
  240. * is broken out as a function because it is used in rtp_h264.c, which is
  241. * forthcoming. */
  242. int ff_rtsp_next_attr_and_value(const char **p, char *attr, int attr_size,
  243. char *value, int value_size)
  244. {
  245. *p += strspn(*p, SPACE_CHARS);
  246. if (**p) {
  247. get_word_sep(attr, attr_size, "=", p);
  248. if (**p == '=')
  249. (*p)++;
  250. get_word_sep(value, value_size, ";", p);
  251. if (**p == ';')
  252. (*p)++;
  253. return 1;
  254. }
  255. return 0;
  256. }
  257. typedef struct SDPParseState {
  258. /* SDP only */
  259. struct sockaddr_storage default_ip;
  260. int default_ttl;
  261. int skip_media; ///< set if an unknown m= line occurs
  262. } SDPParseState;
  263. static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1,
  264. int letter, const char *buf)
  265. {
  266. RTSPState *rt = s->priv_data;
  267. char buf1[64], st_type[64];
  268. const char *p;
  269. enum AVMediaType codec_type;
  270. int payload_type, i;
  271. AVStream *st;
  272. RTSPStream *rtsp_st;
  273. struct sockaddr_storage sdp_ip;
  274. int ttl;
  275. av_dlog(s, "sdp: %c='%s'\n", letter, buf);
  276. p = buf;
  277. if (s1->skip_media && letter != 'm')
  278. return;
  279. switch (letter) {
  280. case 'c':
  281. get_word(buf1, sizeof(buf1), &p);
  282. if (strcmp(buf1, "IN") != 0)
  283. return;
  284. get_word(buf1, sizeof(buf1), &p);
  285. if (strcmp(buf1, "IP4") && strcmp(buf1, "IP6"))
  286. return;
  287. get_word_sep(buf1, sizeof(buf1), "/", &p);
  288. if (get_sockaddr(buf1, &sdp_ip))
  289. return;
  290. ttl = 16;
  291. if (*p == '/') {
  292. p++;
  293. get_word_sep(buf1, sizeof(buf1), "/", &p);
  294. ttl = atoi(buf1);
  295. }
  296. if (s->nb_streams == 0) {
  297. s1->default_ip = sdp_ip;
  298. s1->default_ttl = ttl;
  299. } else {
  300. rtsp_st = rt->rtsp_streams[rt->nb_rtsp_streams - 1];
  301. rtsp_st->sdp_ip = sdp_ip;
  302. rtsp_st->sdp_ttl = ttl;
  303. }
  304. break;
  305. case 's':
  306. av_dict_set(&s->metadata, "title", p, 0);
  307. break;
  308. case 'i':
  309. if (s->nb_streams == 0) {
  310. av_dict_set(&s->metadata, "comment", p, 0);
  311. break;
  312. }
  313. break;
  314. case 'm':
  315. /* new stream */
  316. s1->skip_media = 0;
  317. codec_type = AVMEDIA_TYPE_UNKNOWN;
  318. get_word(st_type, sizeof(st_type), &p);
  319. if (!strcmp(st_type, "audio")) {
  320. codec_type = AVMEDIA_TYPE_AUDIO;
  321. } else if (!strcmp(st_type, "video")) {
  322. codec_type = AVMEDIA_TYPE_VIDEO;
  323. } else if (!strcmp(st_type, "application")) {
  324. codec_type = AVMEDIA_TYPE_DATA;
  325. }
  326. if (codec_type == AVMEDIA_TYPE_UNKNOWN || !(rt->media_type_mask & (1 << codec_type))) {
  327. s1->skip_media = 1;
  328. return;
  329. }
  330. rtsp_st = av_mallocz(sizeof(RTSPStream));
  331. if (!rtsp_st)
  332. return;
  333. rtsp_st->stream_index = -1;
  334. dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
  335. rtsp_st->sdp_ip = s1->default_ip;
  336. rtsp_st->sdp_ttl = s1->default_ttl;
  337. get_word(buf1, sizeof(buf1), &p); /* port */
  338. rtsp_st->sdp_port = atoi(buf1);
  339. get_word(buf1, sizeof(buf1), &p); /* protocol */
  340. if (!strcmp(buf1, "udp"))
  341. rt->transport = RTSP_TRANSPORT_RAW;
  342. else if (strstr(buf1, "/AVPF") || strstr(buf1, "/SAVPF"))
  343. rtsp_st->feedback = 1;
  344. /* XXX: handle list of formats */
  345. get_word(buf1, sizeof(buf1), &p); /* format list */
  346. rtsp_st->sdp_payload_type = atoi(buf1);
  347. if (!strcmp(ff_rtp_enc_name(rtsp_st->sdp_payload_type), "MP2T")) {
  348. /* no corresponding stream */
  349. if (rt->transport == RTSP_TRANSPORT_RAW && !rt->ts && CONFIG_RTPDEC)
  350. rt->ts = ff_mpegts_parse_open(s);
  351. } else if (rt->server_type == RTSP_SERVER_WMS &&
  352. codec_type == AVMEDIA_TYPE_DATA) {
  353. /* RTX stream, a stream that carries all the other actual
  354. * audio/video streams. Don't expose this to the callers. */
  355. } else {
  356. st = avformat_new_stream(s, NULL);
  357. if (!st)
  358. return;
  359. st->id = rt->nb_rtsp_streams - 1;
  360. rtsp_st->stream_index = st->index;
  361. st->codec->codec_type = codec_type;
  362. if (rtsp_st->sdp_payload_type < RTP_PT_PRIVATE) {
  363. RTPDynamicProtocolHandler *handler;
  364. /* if standard payload type, we can find the codec right now */
  365. ff_rtp_get_codec_info(st->codec, rtsp_st->sdp_payload_type);
  366. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
  367. st->codec->sample_rate > 0)
  368. avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
  369. /* Even static payload types may need a custom depacketizer */
  370. handler = ff_rtp_handler_find_by_id(
  371. rtsp_st->sdp_payload_type, st->codec->codec_type);
  372. init_rtp_handler(handler, rtsp_st, st->codec);
  373. if (handler && handler->init)
  374. handler->init(s, st->index,
  375. rtsp_st->dynamic_protocol_context);
  376. }
  377. }
  378. /* put a default control url */
  379. av_strlcpy(rtsp_st->control_url, rt->control_uri,
  380. sizeof(rtsp_st->control_url));
  381. break;
  382. case 'a':
  383. if (av_strstart(p, "control:", &p)) {
  384. if (s->nb_streams == 0) {
  385. if (!strncmp(p, "rtsp://", 7))
  386. av_strlcpy(rt->control_uri, p,
  387. sizeof(rt->control_uri));
  388. } else {
  389. char proto[32];
  390. /* get the control url */
  391. rtsp_st = rt->rtsp_streams[rt->nb_rtsp_streams - 1];
  392. /* XXX: may need to add full url resolution */
  393. av_url_split(proto, sizeof(proto), NULL, 0, NULL, 0,
  394. NULL, NULL, 0, p);
  395. if (proto[0] == '\0') {
  396. /* relative control URL */
  397. if (rtsp_st->control_url[strlen(rtsp_st->control_url)-1]!='/')
  398. av_strlcat(rtsp_st->control_url, "/",
  399. sizeof(rtsp_st->control_url));
  400. av_strlcat(rtsp_st->control_url, p,
  401. sizeof(rtsp_st->control_url));
  402. } else
  403. av_strlcpy(rtsp_st->control_url, p,
  404. sizeof(rtsp_st->control_url));
  405. }
  406. } else if (av_strstart(p, "rtpmap:", &p) && s->nb_streams > 0) {
  407. /* NOTE: rtpmap is only supported AFTER the 'm=' tag */
  408. get_word(buf1, sizeof(buf1), &p);
  409. payload_type = atoi(buf1);
  410. rtsp_st = rt->rtsp_streams[rt->nb_rtsp_streams - 1];
  411. if (rtsp_st->stream_index >= 0) {
  412. st = s->streams[rtsp_st->stream_index];
  413. sdp_parse_rtpmap(s, st, rtsp_st, payload_type, p);
  414. }
  415. } else if (av_strstart(p, "fmtp:", &p) ||
  416. av_strstart(p, "framesize:", &p)) {
  417. /* NOTE: fmtp is only supported AFTER the 'a=rtpmap:xxx' tag */
  418. // let dynamic protocol handlers have a stab at the line.
  419. get_word(buf1, sizeof(buf1), &p);
  420. payload_type = atoi(buf1);
  421. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  422. rtsp_st = rt->rtsp_streams[i];
  423. if (rtsp_st->sdp_payload_type == payload_type &&
  424. rtsp_st->dynamic_handler &&
  425. rtsp_st->dynamic_handler->parse_sdp_a_line)
  426. rtsp_st->dynamic_handler->parse_sdp_a_line(s, i,
  427. rtsp_st->dynamic_protocol_context, buf);
  428. }
  429. } else if (av_strstart(p, "range:", &p)) {
  430. int64_t start, end;
  431. // this is so that seeking on a streamed file can work.
  432. rtsp_parse_range_npt(p, &start, &end);
  433. s->start_time = start;
  434. /* AV_NOPTS_VALUE means live broadcast (and can't seek) */
  435. s->duration = (end == AV_NOPTS_VALUE) ?
  436. AV_NOPTS_VALUE : end - start;
  437. } else if (av_strstart(p, "IsRealDataType:integer;",&p)) {
  438. if (atoi(p) == 1)
  439. rt->transport = RTSP_TRANSPORT_RDT;
  440. } else if (av_strstart(p, "SampleRate:integer;", &p) &&
  441. s->nb_streams > 0) {
  442. st = s->streams[s->nb_streams - 1];
  443. st->codec->sample_rate = atoi(p);
  444. } else {
  445. if (rt->server_type == RTSP_SERVER_WMS)
  446. ff_wms_parse_sdp_a_line(s, p);
  447. if (s->nb_streams > 0) {
  448. rtsp_st = rt->rtsp_streams[rt->nb_rtsp_streams - 1];
  449. if (rt->server_type == RTSP_SERVER_REAL)
  450. ff_real_parse_sdp_a_line(s, rtsp_st->stream_index, p);
  451. if (rtsp_st->dynamic_handler &&
  452. rtsp_st->dynamic_handler->parse_sdp_a_line)
  453. rtsp_st->dynamic_handler->parse_sdp_a_line(s,
  454. rtsp_st->stream_index,
  455. rtsp_st->dynamic_protocol_context, buf);
  456. }
  457. }
  458. break;
  459. }
  460. }
  461. int ff_sdp_parse(AVFormatContext *s, const char *content)
  462. {
  463. RTSPState *rt = s->priv_data;
  464. const char *p;
  465. int letter;
  466. /* Some SDP lines, particularly for Realmedia or ASF RTSP streams,
  467. * contain long SDP lines containing complete ASF Headers (several
  468. * kB) or arrays of MDPR (RM stream descriptor) headers plus
  469. * "rulebooks" describing their properties. Therefore, the SDP line
  470. * buffer is large.
  471. *
  472. * The Vorbis FMTP line can be up to 16KB - see xiph_parse_sdp_line
  473. * in rtpdec_xiph.c. */
  474. char buf[16384], *q;
  475. SDPParseState sdp_parse_state = { { 0 } }, *s1 = &sdp_parse_state;
  476. p = content;
  477. for (;;) {
  478. p += strspn(p, SPACE_CHARS);
  479. letter = *p;
  480. if (letter == '\0')
  481. break;
  482. p++;
  483. if (*p != '=')
  484. goto next_line;
  485. p++;
  486. /* get the content */
  487. q = buf;
  488. while (*p != '\n' && *p != '\r' && *p != '\0') {
  489. if ((q - buf) < sizeof(buf) - 1)
  490. *q++ = *p;
  491. p++;
  492. }
  493. *q = '\0';
  494. sdp_parse_line(s, s1, letter, buf);
  495. next_line:
  496. while (*p != '\n' && *p != '\0')
  497. p++;
  498. if (*p == '\n')
  499. p++;
  500. }
  501. rt->p = av_malloc(sizeof(struct pollfd)*2*(rt->nb_rtsp_streams+1));
  502. if (!rt->p) return AVERROR(ENOMEM);
  503. return 0;
  504. }
  505. #endif /* CONFIG_RTPDEC */
  506. void ff_rtsp_undo_setup(AVFormatContext *s)
  507. {
  508. RTSPState *rt = s->priv_data;
  509. int i;
  510. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  511. RTSPStream *rtsp_st = rt->rtsp_streams[i];
  512. if (!rtsp_st)
  513. continue;
  514. if (rtsp_st->transport_priv) {
  515. if (s->oformat) {
  516. AVFormatContext *rtpctx = rtsp_st->transport_priv;
  517. av_write_trailer(rtpctx);
  518. if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
  519. uint8_t *ptr;
  520. avio_close_dyn_buf(rtpctx->pb, &ptr);
  521. av_free(ptr);
  522. } else {
  523. avio_close(rtpctx->pb);
  524. }
  525. avformat_free_context(rtpctx);
  526. } else if (rt->transport == RTSP_TRANSPORT_RDT && CONFIG_RTPDEC)
  527. ff_rdt_parse_close(rtsp_st->transport_priv);
  528. else if (rt->transport == RTSP_TRANSPORT_RTP && CONFIG_RTPDEC)
  529. ff_rtp_parse_close(rtsp_st->transport_priv);
  530. }
  531. rtsp_st->transport_priv = NULL;
  532. if (rtsp_st->rtp_handle)
  533. ffurl_close(rtsp_st->rtp_handle);
  534. rtsp_st->rtp_handle = NULL;
  535. }
  536. }
  537. /* close and free RTSP streams */
  538. void ff_rtsp_close_streams(AVFormatContext *s)
  539. {
  540. RTSPState *rt = s->priv_data;
  541. int i;
  542. RTSPStream *rtsp_st;
  543. ff_rtsp_undo_setup(s);
  544. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  545. rtsp_st = rt->rtsp_streams[i];
  546. if (rtsp_st) {
  547. if (rtsp_st->dynamic_handler && rtsp_st->dynamic_protocol_context)
  548. rtsp_st->dynamic_handler->free(
  549. rtsp_st->dynamic_protocol_context);
  550. av_free(rtsp_st);
  551. }
  552. }
  553. av_free(rt->rtsp_streams);
  554. if (rt->asf_ctx) {
  555. avformat_close_input(&rt->asf_ctx);
  556. }
  557. if (rt->ts && CONFIG_RTPDEC)
  558. ff_mpegts_parse_close(rt->ts);
  559. av_free(rt->p);
  560. av_free(rt->recvbuf);
  561. }
  562. int ff_rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st)
  563. {
  564. RTSPState *rt = s->priv_data;
  565. AVStream *st = NULL;
  566. int reordering_queue_size = rt->reordering_queue_size;
  567. if (reordering_queue_size < 0) {
  568. if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP || !s->max_delay)
  569. reordering_queue_size = 0;
  570. else
  571. reordering_queue_size = RTP_REORDER_QUEUE_DEFAULT_SIZE;
  572. }
  573. /* open the RTP context */
  574. if (rtsp_st->stream_index >= 0)
  575. st = s->streams[rtsp_st->stream_index];
  576. if (!st)
  577. s->ctx_flags |= AVFMTCTX_NOHEADER;
  578. if (s->oformat && CONFIG_RTSP_MUXER) {
  579. int ret = ff_rtp_chain_mux_open((AVFormatContext **)&rtsp_st->transport_priv, s, st,
  580. rtsp_st->rtp_handle,
  581. RTSP_TCP_MAX_PACKET_SIZE,
  582. rtsp_st->stream_index);
  583. /* Ownership of rtp_handle is passed to the rtp mux context */
  584. rtsp_st->rtp_handle = NULL;
  585. if (ret < 0)
  586. return ret;
  587. } else if (rt->transport == RTSP_TRANSPORT_RAW) {
  588. return 0; // Don't need to open any parser here
  589. } else if (rt->transport == RTSP_TRANSPORT_RDT && CONFIG_RTPDEC)
  590. rtsp_st->transport_priv = ff_rdt_parse_open(s, st->index,
  591. rtsp_st->dynamic_protocol_context,
  592. rtsp_st->dynamic_handler);
  593. else if (CONFIG_RTPDEC)
  594. rtsp_st->transport_priv = ff_rtp_parse_open(s, st,
  595. rtsp_st->sdp_payload_type,
  596. reordering_queue_size);
  597. if (!rtsp_st->transport_priv) {
  598. return AVERROR(ENOMEM);
  599. } else if (rt->transport == RTSP_TRANSPORT_RTP && CONFIG_RTPDEC) {
  600. if (rtsp_st->dynamic_handler) {
  601. ff_rtp_parse_set_dynamic_protocol(rtsp_st->transport_priv,
  602. rtsp_st->dynamic_protocol_context,
  603. rtsp_st->dynamic_handler);
  604. }
  605. }
  606. return 0;
  607. }
  608. #if CONFIG_RTSP_DEMUXER || CONFIG_RTSP_MUXER
  609. static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
  610. {
  611. const char *q;
  612. char *p;
  613. int v;
  614. q = *pp;
  615. q += strspn(q, SPACE_CHARS);
  616. v = strtol(q, &p, 10);
  617. if (*p == '-') {
  618. p++;
  619. *min_ptr = v;
  620. v = strtol(p, &p, 10);
  621. *max_ptr = v;
  622. } else {
  623. *min_ptr = v;
  624. *max_ptr = v;
  625. }
  626. *pp = p;
  627. }
  628. /* XXX: only one transport specification is parsed */
  629. static void rtsp_parse_transport(RTSPMessageHeader *reply, const char *p)
  630. {
  631. char transport_protocol[16];
  632. char profile[16];
  633. char lower_transport[16];
  634. char parameter[16];
  635. RTSPTransportField *th;
  636. char buf[256];
  637. reply->nb_transports = 0;
  638. for (;;) {
  639. p += strspn(p, SPACE_CHARS);
  640. if (*p == '\0')
  641. break;
  642. th = &reply->transports[reply->nb_transports];
  643. get_word_sep(transport_protocol, sizeof(transport_protocol),
  644. "/", &p);
  645. if (!av_strcasecmp (transport_protocol, "rtp")) {
  646. get_word_sep(profile, sizeof(profile), "/;,", &p);
  647. lower_transport[0] = '\0';
  648. /* rtp/avp/<protocol> */
  649. if (*p == '/') {
  650. get_word_sep(lower_transport, sizeof(lower_transport),
  651. ";,", &p);
  652. }
  653. th->transport = RTSP_TRANSPORT_RTP;
  654. } else if (!av_strcasecmp (transport_protocol, "x-pn-tng") ||
  655. !av_strcasecmp (transport_protocol, "x-real-rdt")) {
  656. /* x-pn-tng/<protocol> */
  657. get_word_sep(lower_transport, sizeof(lower_transport), "/;,", &p);
  658. profile[0] = '\0';
  659. th->transport = RTSP_TRANSPORT_RDT;
  660. } else if (!av_strcasecmp(transport_protocol, "raw")) {
  661. get_word_sep(profile, sizeof(profile), "/;,", &p);
  662. lower_transport[0] = '\0';
  663. /* raw/raw/<protocol> */
  664. if (*p == '/') {
  665. get_word_sep(lower_transport, sizeof(lower_transport),
  666. ";,", &p);
  667. }
  668. th->transport = RTSP_TRANSPORT_RAW;
  669. }
  670. if (!av_strcasecmp(lower_transport, "TCP"))
  671. th->lower_transport = RTSP_LOWER_TRANSPORT_TCP;
  672. else
  673. th->lower_transport = RTSP_LOWER_TRANSPORT_UDP;
  674. if (*p == ';')
  675. p++;
  676. /* get each parameter */
  677. while (*p != '\0' && *p != ',') {
  678. get_word_sep(parameter, sizeof(parameter), "=;,", &p);
  679. if (!strcmp(parameter, "port")) {
  680. if (*p == '=') {
  681. p++;
  682. rtsp_parse_range(&th->port_min, &th->port_max, &p);
  683. }
  684. } else if (!strcmp(parameter, "client_port")) {
  685. if (*p == '=') {
  686. p++;
  687. rtsp_parse_range(&th->client_port_min,
  688. &th->client_port_max, &p);
  689. }
  690. } else if (!strcmp(parameter, "server_port")) {
  691. if (*p == '=') {
  692. p++;
  693. rtsp_parse_range(&th->server_port_min,
  694. &th->server_port_max, &p);
  695. }
  696. } else if (!strcmp(parameter, "interleaved")) {
  697. if (*p == '=') {
  698. p++;
  699. rtsp_parse_range(&th->interleaved_min,
  700. &th->interleaved_max, &p);
  701. }
  702. } else if (!strcmp(parameter, "multicast")) {
  703. if (th->lower_transport == RTSP_LOWER_TRANSPORT_UDP)
  704. th->lower_transport = RTSP_LOWER_TRANSPORT_UDP_MULTICAST;
  705. } else if (!strcmp(parameter, "ttl")) {
  706. if (*p == '=') {
  707. char *end;
  708. p++;
  709. th->ttl = strtol(p, &end, 10);
  710. p = end;
  711. }
  712. } else if (!strcmp(parameter, "destination")) {
  713. if (*p == '=') {
  714. p++;
  715. get_word_sep(buf, sizeof(buf), ";,", &p);
  716. get_sockaddr(buf, &th->destination);
  717. }
  718. } else if (!strcmp(parameter, "source")) {
  719. if (*p == '=') {
  720. p++;
  721. get_word_sep(buf, sizeof(buf), ";,", &p);
  722. av_strlcpy(th->source, buf, sizeof(th->source));
  723. }
  724. } else if (!strcmp(parameter, "mode")) {
  725. if (*p == '=') {
  726. p++;
  727. get_word_sep(buf, sizeof(buf), ";, ", &p);
  728. if (!strcmp(buf, "record") ||
  729. !strcmp(buf, "receive"))
  730. th->mode_record = 1;
  731. }
  732. }
  733. while (*p != ';' && *p != '\0' && *p != ',')
  734. p++;
  735. if (*p == ';')
  736. p++;
  737. }
  738. if (*p == ',')
  739. p++;
  740. reply->nb_transports++;
  741. }
  742. }
  743. static void handle_rtp_info(RTSPState *rt, const char *url,
  744. uint32_t seq, uint32_t rtptime)
  745. {
  746. int i;
  747. if (!rtptime || !url[0])
  748. return;
  749. if (rt->transport != RTSP_TRANSPORT_RTP)
  750. return;
  751. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  752. RTSPStream *rtsp_st = rt->rtsp_streams[i];
  753. RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
  754. if (!rtpctx)
  755. continue;
  756. if (!strcmp(rtsp_st->control_url, url)) {
  757. rtpctx->base_timestamp = rtptime;
  758. break;
  759. }
  760. }
  761. }
  762. static void rtsp_parse_rtp_info(RTSPState *rt, const char *p)
  763. {
  764. int read = 0;
  765. char key[20], value[1024], url[1024] = "";
  766. uint32_t seq = 0, rtptime = 0;
  767. for (;;) {
  768. p += strspn(p, SPACE_CHARS);
  769. if (!*p)
  770. break;
  771. get_word_sep(key, sizeof(key), "=", &p);
  772. if (*p != '=')
  773. break;
  774. p++;
  775. get_word_sep(value, sizeof(value), ";, ", &p);
  776. read++;
  777. if (!strcmp(key, "url"))
  778. av_strlcpy(url, value, sizeof(url));
  779. else if (!strcmp(key, "seq"))
  780. seq = strtoul(value, NULL, 10);
  781. else if (!strcmp(key, "rtptime"))
  782. rtptime = strtoul(value, NULL, 10);
  783. if (*p == ',') {
  784. handle_rtp_info(rt, url, seq, rtptime);
  785. url[0] = '\0';
  786. seq = rtptime = 0;
  787. read = 0;
  788. }
  789. if (*p)
  790. p++;
  791. }
  792. if (read > 0)
  793. handle_rtp_info(rt, url, seq, rtptime);
  794. }
  795. void ff_rtsp_parse_line(RTSPMessageHeader *reply, const char *buf,
  796. RTSPState *rt, const char *method)
  797. {
  798. const char *p;
  799. /* NOTE: we do case independent match for broken servers */
  800. p = buf;
  801. if (av_stristart(p, "Session:", &p)) {
  802. int t;
  803. get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
  804. if (av_stristart(p, ";timeout=", &p) &&
  805. (t = strtol(p, NULL, 10)) > 0) {
  806. reply->timeout = t;
  807. }
  808. } else if (av_stristart(p, "Content-Length:", &p)) {
  809. reply->content_length = strtol(p, NULL, 10);
  810. } else if (av_stristart(p, "Transport:", &p)) {
  811. rtsp_parse_transport(reply, p);
  812. } else if (av_stristart(p, "CSeq:", &p)) {
  813. reply->seq = strtol(p, NULL, 10);
  814. } else if (av_stristart(p, "Range:", &p)) {
  815. rtsp_parse_range_npt(p, &reply->range_start, &reply->range_end);
  816. } else if (av_stristart(p, "RealChallenge1:", &p)) {
  817. p += strspn(p, SPACE_CHARS);
  818. av_strlcpy(reply->real_challenge, p, sizeof(reply->real_challenge));
  819. } else if (av_stristart(p, "Server:", &p)) {
  820. p += strspn(p, SPACE_CHARS);
  821. av_strlcpy(reply->server, p, sizeof(reply->server));
  822. } else if (av_stristart(p, "Notice:", &p) ||
  823. av_stristart(p, "X-Notice:", &p)) {
  824. reply->notice = strtol(p, NULL, 10);
  825. } else if (av_stristart(p, "Location:", &p)) {
  826. p += strspn(p, SPACE_CHARS);
  827. av_strlcpy(reply->location, p , sizeof(reply->location));
  828. } else if (av_stristart(p, "WWW-Authenticate:", &p) && rt) {
  829. p += strspn(p, SPACE_CHARS);
  830. ff_http_auth_handle_header(&rt->auth_state, "WWW-Authenticate", p);
  831. } else if (av_stristart(p, "Authentication-Info:", &p) && rt) {
  832. p += strspn(p, SPACE_CHARS);
  833. ff_http_auth_handle_header(&rt->auth_state, "Authentication-Info", p);
  834. } else if (av_stristart(p, "Content-Base:", &p) && rt) {
  835. p += strspn(p, SPACE_CHARS);
  836. if (method && !strcmp(method, "DESCRIBE"))
  837. av_strlcpy(rt->control_uri, p , sizeof(rt->control_uri));
  838. } else if (av_stristart(p, "RTP-Info:", &p) && rt) {
  839. p += strspn(p, SPACE_CHARS);
  840. if (method && !strcmp(method, "PLAY"))
  841. rtsp_parse_rtp_info(rt, p);
  842. } else if (av_stristart(p, "Public:", &p) && rt) {
  843. if (strstr(p, "GET_PARAMETER") &&
  844. method && !strcmp(method, "OPTIONS"))
  845. rt->get_parameter_supported = 1;
  846. } else if (av_stristart(p, "x-Accept-Dynamic-Rate:", &p) && rt) {
  847. p += strspn(p, SPACE_CHARS);
  848. rt->accept_dynamic_rate = atoi(p);
  849. } else if (av_stristart(p, "Content-Type:", &p)) {
  850. p += strspn(p, SPACE_CHARS);
  851. av_strlcpy(reply->content_type, p, sizeof(reply->content_type));
  852. }
  853. }
  854. /* skip a RTP/TCP interleaved packet */
  855. void ff_rtsp_skip_packet(AVFormatContext *s)
  856. {
  857. RTSPState *rt = s->priv_data;
  858. int ret, len, len1;
  859. uint8_t buf[1024];
  860. ret = ffurl_read_complete(rt->rtsp_hd, buf, 3);
  861. if (ret != 3)
  862. return;
  863. len = AV_RB16(buf + 1);
  864. av_dlog(s, "skipping RTP packet len=%d\n", len);
  865. /* skip payload */
  866. while (len > 0) {
  867. len1 = len;
  868. if (len1 > sizeof(buf))
  869. len1 = sizeof(buf);
  870. ret = ffurl_read_complete(rt->rtsp_hd, buf, len1);
  871. if (ret != len1)
  872. return;
  873. len -= len1;
  874. }
  875. }
  876. int ff_rtsp_read_reply(AVFormatContext *s, RTSPMessageHeader *reply,
  877. unsigned char **content_ptr,
  878. int return_on_interleaved_data, const char *method)
  879. {
  880. RTSPState *rt = s->priv_data;
  881. char buf[4096], buf1[1024], *q;
  882. unsigned char ch;
  883. const char *p;
  884. int ret, content_length, line_count = 0, request = 0;
  885. unsigned char *content = NULL;
  886. start:
  887. line_count = 0;
  888. request = 0;
  889. content = NULL;
  890. memset(reply, 0, sizeof(*reply));
  891. /* parse reply (XXX: use buffers) */
  892. rt->last_reply[0] = '\0';
  893. for (;;) {
  894. q = buf;
  895. for (;;) {
  896. ret = ffurl_read_complete(rt->rtsp_hd, &ch, 1);
  897. av_dlog(s, "ret=%d c=%02x [%c]\n", ret, ch, ch);
  898. if (ret != 1)
  899. return AVERROR_EOF;
  900. if (ch == '\n')
  901. break;
  902. if (ch == '$') {
  903. /* XXX: only parse it if first char on line ? */
  904. if (return_on_interleaved_data) {
  905. return 1;
  906. } else
  907. ff_rtsp_skip_packet(s);
  908. } else if (ch != '\r') {
  909. if ((q - buf) < sizeof(buf) - 1)
  910. *q++ = ch;
  911. }
  912. }
  913. *q = '\0';
  914. av_dlog(s, "line='%s'\n", buf);
  915. /* test if last line */
  916. if (buf[0] == '\0')
  917. break;
  918. p = buf;
  919. if (line_count == 0) {
  920. /* get reply code */
  921. get_word(buf1, sizeof(buf1), &p);
  922. if (!strncmp(buf1, "RTSP/", 5)) {
  923. get_word(buf1, sizeof(buf1), &p);
  924. reply->status_code = atoi(buf1);
  925. av_strlcpy(reply->reason, p, sizeof(reply->reason));
  926. } else {
  927. av_strlcpy(reply->reason, buf1, sizeof(reply->reason)); // method
  928. get_word(buf1, sizeof(buf1), &p); // object
  929. request = 1;
  930. }
  931. } else {
  932. ff_rtsp_parse_line(reply, p, rt, method);
  933. av_strlcat(rt->last_reply, p, sizeof(rt->last_reply));
  934. av_strlcat(rt->last_reply, "\n", sizeof(rt->last_reply));
  935. }
  936. line_count++;
  937. }
  938. if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0' && !request)
  939. av_strlcpy(rt->session_id, reply->session_id, sizeof(rt->session_id));
  940. content_length = reply->content_length;
  941. if (content_length > 0) {
  942. /* leave some room for a trailing '\0' (useful for simple parsing) */
  943. content = av_malloc(content_length + 1);
  944. ffurl_read_complete(rt->rtsp_hd, content, content_length);
  945. content[content_length] = '\0';
  946. }
  947. if (content_ptr)
  948. *content_ptr = content;
  949. else
  950. av_free(content);
  951. if (request) {
  952. char buf[1024];
  953. char base64buf[AV_BASE64_SIZE(sizeof(buf))];
  954. const char* ptr = buf;
  955. if (!strcmp(reply->reason, "OPTIONS")) {
  956. snprintf(buf, sizeof(buf), "RTSP/1.0 200 OK\r\n");
  957. if (reply->seq)
  958. av_strlcatf(buf, sizeof(buf), "CSeq: %d\r\n", reply->seq);
  959. if (reply->session_id[0])
  960. av_strlcatf(buf, sizeof(buf), "Session: %s\r\n",
  961. reply->session_id);
  962. } else {
  963. snprintf(buf, sizeof(buf), "RTSP/1.0 501 Not Implemented\r\n");
  964. }
  965. av_strlcat(buf, "\r\n", sizeof(buf));
  966. if (rt->control_transport == RTSP_MODE_TUNNEL) {
  967. av_base64_encode(base64buf, sizeof(base64buf), buf, strlen(buf));
  968. ptr = base64buf;
  969. }
  970. ffurl_write(rt->rtsp_hd_out, ptr, strlen(ptr));
  971. rt->last_cmd_time = av_gettime();
  972. /* Even if the request from the server had data, it is not the data
  973. * that the caller wants or expects. The memory could also be leaked
  974. * if the actual following reply has content data. */
  975. if (content_ptr)
  976. av_freep(content_ptr);
  977. /* If method is set, this is called from ff_rtsp_send_cmd,
  978. * where a reply to exactly this request is awaited. For
  979. * callers from within packet receiving, we just want to
  980. * return to the caller and go back to receiving packets. */
  981. if (method)
  982. goto start;
  983. return 0;
  984. }
  985. if (rt->seq != reply->seq) {
  986. av_log(s, AV_LOG_WARNING, "CSeq %d expected, %d received.\n",
  987. rt->seq, reply->seq);
  988. }
  989. /* EOS */
  990. if (reply->notice == 2101 /* End-of-Stream Reached */ ||
  991. reply->notice == 2104 /* Start-of-Stream Reached */ ||
  992. reply->notice == 2306 /* Continuous Feed Terminated */) {
  993. rt->state = RTSP_STATE_IDLE;
  994. } else if (reply->notice >= 4400 && reply->notice < 5500) {
  995. return AVERROR(EIO); /* data or server error */
  996. } else if (reply->notice == 2401 /* Ticket Expired */ ||
  997. (reply->notice >= 5500 && reply->notice < 5600) /* end of term */ )
  998. return AVERROR(EPERM);
  999. return 0;
  1000. }
  1001. /**
  1002. * Send a command to the RTSP server without waiting for the reply.
  1003. *
  1004. * @param s RTSP (de)muxer context
  1005. * @param method the method for the request
  1006. * @param url the target url for the request
  1007. * @param headers extra header lines to include in the request
  1008. * @param send_content if non-null, the data to send as request body content
  1009. * @param send_content_length the length of the send_content data, or 0 if
  1010. * send_content is null
  1011. *
  1012. * @return zero if success, nonzero otherwise
  1013. */
  1014. static int ff_rtsp_send_cmd_with_content_async(AVFormatContext *s,
  1015. const char *method, const char *url,
  1016. const char *headers,
  1017. const unsigned char *send_content,
  1018. int send_content_length)
  1019. {
  1020. RTSPState *rt = s->priv_data;
  1021. char buf[4096], *out_buf;
  1022. char base64buf[AV_BASE64_SIZE(sizeof(buf))];
  1023. /* Add in RTSP headers */
  1024. out_buf = buf;
  1025. rt->seq++;
  1026. snprintf(buf, sizeof(buf), "%s %s RTSP/1.0\r\n", method, url);
  1027. if (headers)
  1028. av_strlcat(buf, headers, sizeof(buf));
  1029. av_strlcatf(buf, sizeof(buf), "CSeq: %d\r\n", rt->seq);
  1030. if (rt->session_id[0] != '\0' && (!headers ||
  1031. !strstr(headers, "\nIf-Match:"))) {
  1032. av_strlcatf(buf, sizeof(buf), "Session: %s\r\n", rt->session_id);
  1033. }
  1034. if (rt->auth[0]) {
  1035. char *str = ff_http_auth_create_response(&rt->auth_state,
  1036. rt->auth, url, method);
  1037. if (str)
  1038. av_strlcat(buf, str, sizeof(buf));
  1039. av_free(str);
  1040. }
  1041. if (send_content_length > 0 && send_content)
  1042. av_strlcatf(buf, sizeof(buf), "Content-Length: %d\r\n", send_content_length);
  1043. av_strlcat(buf, "\r\n", sizeof(buf));
  1044. /* base64 encode rtsp if tunneling */
  1045. if (rt->control_transport == RTSP_MODE_TUNNEL) {
  1046. av_base64_encode(base64buf, sizeof(base64buf), buf, strlen(buf));
  1047. out_buf = base64buf;
  1048. }
  1049. av_dlog(s, "Sending:\n%s--\n", buf);
  1050. ffurl_write(rt->rtsp_hd_out, out_buf, strlen(out_buf));
  1051. if (send_content_length > 0 && send_content) {
  1052. if (rt->control_transport == RTSP_MODE_TUNNEL) {
  1053. av_log(s, AV_LOG_ERROR, "tunneling of RTSP requests "
  1054. "with content data not supported\n");
  1055. return AVERROR_PATCHWELCOME;
  1056. }
  1057. ffurl_write(rt->rtsp_hd_out, send_content, send_content_length);
  1058. }
  1059. rt->last_cmd_time = av_gettime();
  1060. return 0;
  1061. }
  1062. int ff_rtsp_send_cmd_async(AVFormatContext *s, const char *method,
  1063. const char *url, const char *headers)
  1064. {
  1065. return ff_rtsp_send_cmd_with_content_async(s, method, url, headers, NULL, 0);
  1066. }
  1067. int ff_rtsp_send_cmd(AVFormatContext *s, const char *method, const char *url,
  1068. const char *headers, RTSPMessageHeader *reply,
  1069. unsigned char **content_ptr)
  1070. {
  1071. return ff_rtsp_send_cmd_with_content(s, method, url, headers, reply,
  1072. content_ptr, NULL, 0);
  1073. }
  1074. int ff_rtsp_send_cmd_with_content(AVFormatContext *s,
  1075. const char *method, const char *url,
  1076. const char *header,
  1077. RTSPMessageHeader *reply,
  1078. unsigned char **content_ptr,
  1079. const unsigned char *send_content,
  1080. int send_content_length)
  1081. {
  1082. RTSPState *rt = s->priv_data;
  1083. HTTPAuthType cur_auth_type;
  1084. int ret, attempts = 0;
  1085. retry:
  1086. cur_auth_type = rt->auth_state.auth_type;
  1087. if ((ret = ff_rtsp_send_cmd_with_content_async(s, method, url, header,
  1088. send_content,
  1089. send_content_length)))
  1090. return ret;
  1091. if ((ret = ff_rtsp_read_reply(s, reply, content_ptr, 0, method) ) < 0)
  1092. return ret;
  1093. attempts++;
  1094. if (reply->status_code == 401 &&
  1095. (cur_auth_type == HTTP_AUTH_NONE || rt->auth_state.stale) &&
  1096. rt->auth_state.auth_type != HTTP_AUTH_NONE && attempts < 2)
  1097. goto retry;
  1098. if (reply->status_code > 400){
  1099. av_log(s, AV_LOG_ERROR, "method %s failed: %d%s\n",
  1100. method,
  1101. reply->status_code,
  1102. reply->reason);
  1103. av_log(s, AV_LOG_DEBUG, "%s\n", rt->last_reply);
  1104. }
  1105. return 0;
  1106. }
  1107. int ff_rtsp_make_setup_request(AVFormatContext *s, const char *host, int port,
  1108. int lower_transport, const char *real_challenge)
  1109. {
  1110. RTSPState *rt = s->priv_data;
  1111. int rtx = 0, j, i, err, interleave = 0, port_off;
  1112. RTSPStream *rtsp_st;
  1113. RTSPMessageHeader reply1, *reply = &reply1;
  1114. char cmd[2048];
  1115. const char *trans_pref;
  1116. if (rt->transport == RTSP_TRANSPORT_RDT)
  1117. trans_pref = "x-pn-tng";
  1118. else if (rt->transport == RTSP_TRANSPORT_RAW)
  1119. trans_pref = "RAW/RAW";
  1120. else
  1121. trans_pref = "RTP/AVP";
  1122. /* default timeout: 1 minute */
  1123. rt->timeout = 60;
  1124. /* Choose a random starting offset within the first half of the
  1125. * port range, to allow for a number of ports to try even if the offset
  1126. * happens to be at the end of the random range. */
  1127. port_off = av_get_random_seed() % ((rt->rtp_port_max - rt->rtp_port_min)/2);
  1128. /* even random offset */
  1129. port_off -= port_off & 0x01;
  1130. for (j = rt->rtp_port_min + port_off, i = 0; i < rt->nb_rtsp_streams; ++i) {
  1131. char transport[2048];
  1132. /*
  1133. * WMS serves all UDP data over a single connection, the RTX, which
  1134. * isn't necessarily the first in the SDP but has to be the first
  1135. * to be set up, else the second/third SETUP will fail with a 461.
  1136. */
  1137. if (lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
  1138. rt->server_type == RTSP_SERVER_WMS) {
  1139. if (i == 0) {
  1140. /* rtx first */
  1141. for (rtx = 0; rtx < rt->nb_rtsp_streams; rtx++) {
  1142. int len = strlen(rt->rtsp_streams[rtx]->control_url);
  1143. if (len >= 4 &&
  1144. !strcmp(rt->rtsp_streams[rtx]->control_url + len - 4,
  1145. "/rtx"))
  1146. break;
  1147. }
  1148. if (rtx == rt->nb_rtsp_streams)
  1149. return -1; /* no RTX found */
  1150. rtsp_st = rt->rtsp_streams[rtx];
  1151. } else
  1152. rtsp_st = rt->rtsp_streams[i > rtx ? i : i - 1];
  1153. } else
  1154. rtsp_st = rt->rtsp_streams[i];
  1155. /* RTP/UDP */
  1156. if (lower_transport == RTSP_LOWER_TRANSPORT_UDP) {
  1157. char buf[256];
  1158. if (rt->server_type == RTSP_SERVER_WMS && i > 1) {
  1159. port = reply->transports[0].client_port_min;
  1160. goto have_port;
  1161. }
  1162. /* first try in specified port range */
  1163. while (j <= rt->rtp_port_max) {
  1164. ff_url_join(buf, sizeof(buf), "rtp", NULL, host, -1,
  1165. "?localport=%d", j);
  1166. /* we will use two ports per rtp stream (rtp and rtcp) */
  1167. j += 2;
  1168. if (!ffurl_open(&rtsp_st->rtp_handle, buf, AVIO_FLAG_READ_WRITE,
  1169. &s->interrupt_callback, NULL))
  1170. goto rtp_opened;
  1171. }
  1172. av_log(s, AV_LOG_ERROR, "Unable to open an input RTP port\n");
  1173. err = AVERROR(EIO);
  1174. goto fail;
  1175. rtp_opened:
  1176. port = ff_rtp_get_local_rtp_port(rtsp_st->rtp_handle);
  1177. have_port:
  1178. snprintf(transport, sizeof(transport) - 1,
  1179. "%s/UDP;", trans_pref);
  1180. if (rt->server_type != RTSP_SERVER_REAL)
  1181. av_strlcat(transport, "unicast;", sizeof(transport));
  1182. av_strlcatf(transport, sizeof(transport),
  1183. "client_port=%d", port);
  1184. if (rt->transport == RTSP_TRANSPORT_RTP &&
  1185. !(rt->server_type == RTSP_SERVER_WMS && i > 0))
  1186. av_strlcatf(transport, sizeof(transport), "-%d", port + 1);
  1187. }
  1188. /* RTP/TCP */
  1189. else if (lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
  1190. /* For WMS streams, the application streams are only used for
  1191. * UDP. When trying to set it up for TCP streams, the server
  1192. * will return an error. Therefore, we skip those streams. */
  1193. if (rt->server_type == RTSP_SERVER_WMS &&
  1194. (rtsp_st->stream_index < 0 ||
  1195. s->streams[rtsp_st->stream_index]->codec->codec_type ==
  1196. AVMEDIA_TYPE_DATA))
  1197. continue;
  1198. snprintf(transport, sizeof(transport) - 1,
  1199. "%s/TCP;", trans_pref);
  1200. if (rt->transport != RTSP_TRANSPORT_RDT)
  1201. av_strlcat(transport, "unicast;", sizeof(transport));
  1202. av_strlcatf(transport, sizeof(transport),
  1203. "interleaved=%d-%d",
  1204. interleave, interleave + 1);
  1205. interleave += 2;
  1206. }
  1207. else if (lower_transport == RTSP_LOWER_TRANSPORT_UDP_MULTICAST) {
  1208. snprintf(transport, sizeof(transport) - 1,
  1209. "%s/UDP;multicast", trans_pref);
  1210. }
  1211. if (s->oformat) {
  1212. av_strlcat(transport, ";mode=record", sizeof(transport));
  1213. } else if (rt->server_type == RTSP_SERVER_REAL ||
  1214. rt->server_type == RTSP_SERVER_WMS)
  1215. av_strlcat(transport, ";mode=play", sizeof(transport));
  1216. snprintf(cmd, sizeof(cmd),
  1217. "Transport: %s\r\n",
  1218. transport);
  1219. if (rt->accept_dynamic_rate)
  1220. av_strlcat(cmd, "x-Dynamic-Rate: 0\r\n", sizeof(cmd));
  1221. if (i == 0 && rt->server_type == RTSP_SERVER_REAL && CONFIG_RTPDEC) {
  1222. char real_res[41], real_csum[9];
  1223. ff_rdt_calc_response_and_checksum(real_res, real_csum,
  1224. real_challenge);
  1225. av_strlcatf(cmd, sizeof(cmd),
  1226. "If-Match: %s\r\n"
  1227. "RealChallenge2: %s, sd=%s\r\n",
  1228. rt->session_id, real_res, real_csum);
  1229. }
  1230. ff_rtsp_send_cmd(s, "SETUP", rtsp_st->control_url, cmd, reply, NULL);
  1231. if (reply->status_code == 461 /* Unsupported protocol */ && i == 0) {
  1232. err = 1;
  1233. goto fail;
  1234. } else if (reply->status_code != RTSP_STATUS_OK ||
  1235. reply->nb_transports != 1) {
  1236. err = AVERROR_INVALIDDATA;
  1237. goto fail;
  1238. }
  1239. /* XXX: same protocol for all streams is required */
  1240. if (i > 0) {
  1241. if (reply->transports[0].lower_transport != rt->lower_transport ||
  1242. reply->transports[0].transport != rt->transport) {
  1243. err = AVERROR_INVALIDDATA;
  1244. goto fail;
  1245. }
  1246. } else {
  1247. rt->lower_transport = reply->transports[0].lower_transport;
  1248. rt->transport = reply->transports[0].transport;
  1249. }
  1250. /* Fail if the server responded with another lower transport mode
  1251. * than what we requested. */
  1252. if (reply->transports[0].lower_transport != lower_transport) {
  1253. av_log(s, AV_LOG_ERROR, "Nonmatching transport in server reply\n");
  1254. err = AVERROR_INVALIDDATA;
  1255. goto fail;
  1256. }
  1257. switch(reply->transports[0].lower_transport) {
  1258. case RTSP_LOWER_TRANSPORT_TCP:
  1259. rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
  1260. rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
  1261. break;
  1262. case RTSP_LOWER_TRANSPORT_UDP: {
  1263. char url[1024], options[30] = "";
  1264. if (rt->rtsp_flags & RTSP_FLAG_FILTER_SRC)
  1265. av_strlcpy(options, "?connect=1", sizeof(options));
  1266. /* Use source address if specified */
  1267. if (reply->transports[0].source[0]) {
  1268. ff_url_join(url, sizeof(url), "rtp", NULL,
  1269. reply->transports[0].source,
  1270. reply->transports[0].server_port_min, "%s", options);
  1271. } else {
  1272. ff_url_join(url, sizeof(url), "rtp", NULL, host,
  1273. reply->transports[0].server_port_min, "%s", options);
  1274. }
  1275. if (!(rt->server_type == RTSP_SERVER_WMS && i > 1) &&
  1276. ff_rtp_set_remote_url(rtsp_st->rtp_handle, url) < 0) {
  1277. err = AVERROR_INVALIDDATA;
  1278. goto fail;
  1279. }
  1280. /* Try to initialize the connection state in a
  1281. * potential NAT router by sending dummy packets.
  1282. * RTP/RTCP dummy packets are used for RDT, too.
  1283. */
  1284. if (!(rt->server_type == RTSP_SERVER_WMS && i > 1) && s->iformat &&
  1285. CONFIG_RTPDEC)
  1286. ff_rtp_send_punch_packets(rtsp_st->rtp_handle);
  1287. break;
  1288. }
  1289. case RTSP_LOWER_TRANSPORT_UDP_MULTICAST: {
  1290. char url[1024], namebuf[50], optbuf[20] = "";
  1291. struct sockaddr_storage addr;
  1292. int port, ttl;
  1293. if (reply->transports[0].destination.ss_family) {
  1294. addr = reply->transports[0].destination;
  1295. port = reply->transports[0].port_min;
  1296. ttl = reply->transports[0].ttl;
  1297. } else {
  1298. addr = rtsp_st->sdp_ip;
  1299. port = rtsp_st->sdp_port;
  1300. ttl = rtsp_st->sdp_ttl;
  1301. }
  1302. if (ttl > 0)
  1303. snprintf(optbuf, sizeof(optbuf), "?ttl=%d", ttl);
  1304. getnameinfo((struct sockaddr*) &addr, sizeof(addr),
  1305. namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST);
  1306. ff_url_join(url, sizeof(url), "rtp", NULL, namebuf,
  1307. port, "%s", optbuf);
  1308. if (ffurl_open(&rtsp_st->rtp_handle, url, AVIO_FLAG_READ_WRITE,
  1309. &s->interrupt_callback, NULL) < 0) {
  1310. err = AVERROR_INVALIDDATA;
  1311. goto fail;
  1312. }
  1313. break;
  1314. }
  1315. }
  1316. if ((err = ff_rtsp_open_transport_ctx(s, rtsp_st)))
  1317. goto fail;
  1318. }
  1319. if (rt->nb_rtsp_streams && reply->timeout > 0)
  1320. rt->timeout = reply->timeout;
  1321. if (rt->server_type == RTSP_SERVER_REAL)
  1322. rt->need_subscription = 1;
  1323. return 0;
  1324. fail:
  1325. ff_rtsp_undo_setup(s);
  1326. return err;
  1327. }
  1328. void ff_rtsp_close_connections(AVFormatContext *s)
  1329. {
  1330. RTSPState *rt = s->priv_data;
  1331. if (rt->rtsp_hd_out != rt->rtsp_hd) ffurl_close(rt->rtsp_hd_out);
  1332. ffurl_close(rt->rtsp_hd);
  1333. rt->rtsp_hd = rt->rtsp_hd_out = NULL;
  1334. }
  1335. int ff_rtsp_connect(AVFormatContext *s)
  1336. {
  1337. RTSPState *rt = s->priv_data;
  1338. char host[1024], path[1024], tcpname[1024], cmd[2048], auth[128];
  1339. int port, err, tcp_fd;
  1340. RTSPMessageHeader reply1 = {0}, *reply = &reply1;
  1341. int lower_transport_mask = 0;
  1342. char real_challenge[64] = "";
  1343. struct sockaddr_storage peer;
  1344. socklen_t peer_len = sizeof(peer);
  1345. if (rt->rtp_port_max < rt->rtp_port_min) {
  1346. av_log(s, AV_LOG_ERROR, "Invalid UDP port range, max port %d less "
  1347. "than min port %d\n", rt->rtp_port_max,
  1348. rt->rtp_port_min);
  1349. return AVERROR(EINVAL);
  1350. }
  1351. if (!ff_network_init())
  1352. return AVERROR(EIO);
  1353. if (s->max_delay < 0) /* Not set by the caller */
  1354. s->max_delay = s->iformat ? DEFAULT_REORDERING_DELAY : 0;
  1355. rt->control_transport = RTSP_MODE_PLAIN;
  1356. if (rt->lower_transport_mask & (1 << RTSP_LOWER_TRANSPORT_HTTP)) {
  1357. rt->lower_transport_mask = 1 << RTSP_LOWER_TRANSPORT_TCP;
  1358. rt->control_transport = RTSP_MODE_TUNNEL;
  1359. }
  1360. /* Only pass through valid flags from here */
  1361. rt->lower_transport_mask &= (1 << RTSP_LOWER_TRANSPORT_NB) - 1;
  1362. redirect:
  1363. lower_transport_mask = rt->lower_transport_mask;
  1364. /* extract hostname and port */
  1365. av_url_split(NULL, 0, auth, sizeof(auth),
  1366. host, sizeof(host), &port, path, sizeof(path), s->filename);
  1367. if (*auth) {
  1368. av_strlcpy(rt->auth, auth, sizeof(rt->auth));
  1369. }
  1370. if (port < 0)
  1371. port = RTSP_DEFAULT_PORT;
  1372. if (!lower_transport_mask)
  1373. lower_transport_mask = (1 << RTSP_LOWER_TRANSPORT_NB) - 1;
  1374. if (s->oformat) {
  1375. /* Only UDP or TCP - UDP multicast isn't supported. */
  1376. lower_transport_mask &= (1 << RTSP_LOWER_TRANSPORT_UDP) |
  1377. (1 << RTSP_LOWER_TRANSPORT_TCP);
  1378. if (!lower_transport_mask || rt->control_transport == RTSP_MODE_TUNNEL) {
  1379. av_log(s, AV_LOG_ERROR, "Unsupported lower transport method, "
  1380. "only UDP and TCP are supported for output.\n");
  1381. err = AVERROR(EINVAL);
  1382. goto fail;
  1383. }
  1384. }
  1385. /* Construct the URI used in request; this is similar to s->filename,
  1386. * but with authentication credentials removed and RTSP specific options
  1387. * stripped out. */
  1388. ff_url_join(rt->control_uri, sizeof(rt->control_uri), "rtsp", NULL,
  1389. host, port, "%s", path);
  1390. if (rt->control_transport == RTSP_MODE_TUNNEL) {
  1391. /* set up initial handshake for tunneling */
  1392. char httpname[1024];
  1393. char sessioncookie[17];
  1394. char headers[1024];
  1395. ff_url_join(httpname, sizeof(httpname), "http", auth, host, port, "%s", path);
  1396. snprintf(sessioncookie, sizeof(sessioncookie), "%08x%08x",
  1397. av_get_random_seed(), av_get_random_seed());
  1398. /* GET requests */
  1399. if (ffurl_alloc(&rt->rtsp_hd, httpname, AVIO_FLAG_READ,
  1400. &s->interrupt_callback) < 0) {
  1401. err = AVERROR(EIO);
  1402. goto fail;
  1403. }
  1404. /* generate GET headers */
  1405. snprintf(headers, sizeof(headers),
  1406. "x-sessioncookie: %s\r\n"
  1407. "Accept: application/x-rtsp-tunnelled\r\n"
  1408. "Pragma: no-cache\r\n"
  1409. "Cache-Control: no-cache\r\n",
  1410. sessioncookie);
  1411. av_opt_set(rt->rtsp_hd->priv_data, "headers", headers, 0);
  1412. /* complete the connection */
  1413. if (ffurl_connect(rt->rtsp_hd, NULL)) {
  1414. err = AVERROR(EIO);
  1415. goto fail;
  1416. }
  1417. /* POST requests */
  1418. if (ffurl_alloc(&rt->rtsp_hd_out, httpname, AVIO_FLAG_WRITE,
  1419. &s->interrupt_callback) < 0 ) {
  1420. err = AVERROR(EIO);
  1421. goto fail;
  1422. }
  1423. /* generate POST headers */
  1424. snprintf(headers, sizeof(headers),
  1425. "x-sessioncookie: %s\r\n"
  1426. "Content-Type: application/x-rtsp-tunnelled\r\n"
  1427. "Pragma: no-cache\r\n"
  1428. "Cache-Control: no-cache\r\n"
  1429. "Content-Length: 32767\r\n"
  1430. "Expires: Sun, 9 Jan 1972 00:00:00 GMT\r\n",
  1431. sessioncookie);
  1432. av_opt_set(rt->rtsp_hd_out->priv_data, "headers", headers, 0);
  1433. av_opt_set(rt->rtsp_hd_out->priv_data, "chunked_post", "0", 0);
  1434. /* Initialize the authentication state for the POST session. The HTTP
  1435. * protocol implementation doesn't properly handle multi-pass
  1436. * authentication for POST requests, since it would require one of
  1437. * the following:
  1438. * - implementing Expect: 100-continue, which many HTTP servers
  1439. * don't support anyway, even less the RTSP servers that do HTTP
  1440. * tunneling
  1441. * - sending the whole POST data until getting a 401 reply specifying
  1442. * what authentication method to use, then resending all that data
  1443. * - waiting for potential 401 replies directly after sending the
  1444. * POST header (waiting for some unspecified time)
  1445. * Therefore, we copy the full auth state, which works for both basic
  1446. * and digest. (For digest, we would have to synchronize the nonce
  1447. * count variable between the two sessions, if we'd do more requests
  1448. * with the original session, though.)
  1449. */
  1450. ff_http_init_auth_state(rt->rtsp_hd_out, rt->rtsp_hd);
  1451. /* complete the connection */
  1452. if (ffurl_connect(rt->rtsp_hd_out, NULL)) {
  1453. err = AVERROR(EIO);
  1454. goto fail;
  1455. }
  1456. } else {
  1457. /* open the tcp connection */
  1458. ff_url_join(tcpname, sizeof(tcpname), "tcp", NULL, host, port, NULL);
  1459. if (ffurl_open(&rt->rtsp_hd, tcpname, AVIO_FLAG_READ_WRITE,
  1460. &s->interrupt_callback, NULL) < 0) {
  1461. err = AVERROR(EIO);
  1462. goto fail;
  1463. }
  1464. rt->rtsp_hd_out = rt->rtsp_hd;
  1465. }
  1466. rt->seq = 0;
  1467. tcp_fd = ffurl_get_file_handle(rt->rtsp_hd);
  1468. if (!getpeername(tcp_fd, (struct sockaddr*) &peer, &peer_len)) {
  1469. getnameinfo((struct sockaddr*) &peer, peer_len, host, sizeof(host),
  1470. NULL, 0, NI_NUMERICHOST);
  1471. }
  1472. /* request options supported by the server; this also detects server
  1473. * type */
  1474. for (rt->server_type = RTSP_SERVER_RTP;;) {
  1475. cmd[0] = 0;
  1476. if (rt->server_type == RTSP_SERVER_REAL)
  1477. av_strlcat(cmd,
  1478. /*
  1479. * The following entries are required for proper
  1480. * streaming from a Realmedia server. They are
  1481. * interdependent in some way although we currently
  1482. * don't quite understand how. Values were copied
  1483. * from mplayer SVN r23589.
  1484. * ClientChallenge is a 16-byte ID in hex
  1485. * CompanyID is a 16-byte ID in base64
  1486. */
  1487. "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7\r\n"
  1488. "PlayerStarttime: [28/03/2003:22:50:23 00:00]\r\n"
  1489. "CompanyID: KnKV4M4I/B2FjJ1TToLycw==\r\n"
  1490. "GUID: 00000000-0000-0000-0000-000000000000\r\n",
  1491. sizeof(cmd));
  1492. ff_rtsp_send_cmd(s, "OPTIONS", rt->control_uri, cmd, reply, NULL);
  1493. if (reply->status_code != RTSP_STATUS_OK) {
  1494. err = AVERROR_INVALIDDATA;
  1495. goto fail;
  1496. }
  1497. /* detect server type if not standard-compliant RTP */
  1498. if (rt->server_type != RTSP_SERVER_REAL && reply->real_challenge[0]) {
  1499. rt->server_type = RTSP_SERVER_REAL;
  1500. continue;
  1501. } else if (!av_strncasecmp(reply->server, "WMServer/", 9)) {
  1502. rt->server_type = RTSP_SERVER_WMS;
  1503. } else if (rt->server_type == RTSP_SERVER_REAL)
  1504. strcpy(real_challenge, reply->real_challenge);
  1505. break;
  1506. }
  1507. if (s->iformat && CONFIG_RTSP_DEMUXER)
  1508. err = ff_rtsp_setup_input_streams(s, reply);
  1509. else if (CONFIG_RTSP_MUXER)
  1510. err = ff_rtsp_setup_output_streams(s, host);
  1511. if (err)
  1512. goto fail;
  1513. do {
  1514. int lower_transport = ff_log2_tab[lower_transport_mask &
  1515. ~(lower_transport_mask - 1)];
  1516. err = ff_rtsp_make_setup_request(s, host, port, lower_transport,
  1517. rt->server_type == RTSP_SERVER_REAL ?
  1518. real_challenge : NULL);
  1519. if (err < 0)
  1520. goto fail;
  1521. lower_transport_mask &= ~(1 << lower_transport);
  1522. if (lower_transport_mask == 0 && err == 1) {
  1523. err = AVERROR(EPROTONOSUPPORT);
  1524. goto fail;
  1525. }
  1526. } while (err);
  1527. rt->lower_transport_mask = lower_transport_mask;
  1528. av_strlcpy(rt->real_challenge, real_challenge, sizeof(rt->real_challenge));
  1529. rt->state = RTSP_STATE_IDLE;
  1530. rt->seek_timestamp = 0; /* default is to start stream at position zero */
  1531. return 0;
  1532. fail:
  1533. ff_rtsp_close_streams(s);
  1534. ff_rtsp_close_connections(s);
  1535. if (reply->status_code >=300 && reply->status_code < 400 && s->iformat) {
  1536. av_strlcpy(s->filename, reply->location, sizeof(s->filename));
  1537. av_log(s, AV_LOG_INFO, "Status %d: Redirecting to %s\n",
  1538. reply->status_code,
  1539. s->filename);
  1540. goto redirect;
  1541. }
  1542. ff_network_close();
  1543. return err;
  1544. }
  1545. #endif /* CONFIG_RTSP_DEMUXER || CONFIG_RTSP_MUXER */
  1546. #if CONFIG_RTPDEC
  1547. static int udp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  1548. uint8_t *buf, int buf_size, int64_t wait_end)
  1549. {
  1550. RTSPState *rt = s->priv_data;
  1551. RTSPStream *rtsp_st;
  1552. int n, i, ret, tcp_fd, timeout_cnt = 0;
  1553. int max_p = 0;
  1554. struct pollfd *p = rt->p;
  1555. int *fds = NULL, fdsnum, fdsidx;
  1556. for (;;) {
  1557. if (ff_check_interrupt(&s->interrupt_callback))
  1558. return AVERROR_EXIT;
  1559. if (wait_end && wait_end - av_gettime() < 0)
  1560. return AVERROR(EAGAIN);
  1561. max_p = 0;
  1562. if (rt->rtsp_hd) {
  1563. tcp_fd = ffurl_get_file_handle(rt->rtsp_hd);
  1564. p[max_p].fd = tcp_fd;
  1565. p[max_p++].events = POLLIN;
  1566. } else {
  1567. tcp_fd = -1;
  1568. }
  1569. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1570. rtsp_st = rt->rtsp_streams[i];
  1571. if (rtsp_st->rtp_handle) {
  1572. if (ret = ffurl_get_multi_file_handle(rtsp_st->rtp_handle,
  1573. &fds, &fdsnum)) {
  1574. av_log(s, AV_LOG_ERROR, "Unable to recover rtp ports\n");
  1575. return ret;
  1576. }
  1577. if (fdsnum != 2) {
  1578. av_log(s, AV_LOG_ERROR,
  1579. "Number of fds %d not supported\n", fdsnum);
  1580. return AVERROR_INVALIDDATA;
  1581. }
  1582. for (fdsidx = 0; fdsidx < fdsnum; fdsidx++) {
  1583. p[max_p].fd = fds[fdsidx];
  1584. p[max_p++].events = POLLIN;
  1585. }
  1586. av_free(fds);
  1587. }
  1588. }
  1589. n = poll(p, max_p, POLL_TIMEOUT_MS);
  1590. if (n > 0) {
  1591. int j = 1 - (tcp_fd == -1);
  1592. timeout_cnt = 0;
  1593. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1594. rtsp_st = rt->rtsp_streams[i];
  1595. if (rtsp_st->rtp_handle) {
  1596. if (p[j].revents & POLLIN || p[j+1].revents & POLLIN) {
  1597. ret = ffurl_read(rtsp_st->rtp_handle, buf, buf_size);
  1598. if (ret > 0) {
  1599. *prtsp_st = rtsp_st;
  1600. return ret;
  1601. }
  1602. }
  1603. j+=2;
  1604. }
  1605. }
  1606. #if CONFIG_RTSP_DEMUXER
  1607. if (tcp_fd != -1 && p[0].revents & POLLIN) {
  1608. if (rt->rtsp_flags & RTSP_FLAG_LISTEN) {
  1609. if (rt->state == RTSP_STATE_STREAMING) {
  1610. if (!ff_rtsp_parse_streaming_commands(s))
  1611. return AVERROR_EOF;
  1612. else
  1613. av_log(s, AV_LOG_WARNING,
  1614. "Unable to answer to TEARDOWN\n");
  1615. } else
  1616. return 0;
  1617. } else {
  1618. RTSPMessageHeader reply;
  1619. ret = ff_rtsp_read_reply(s, &reply, NULL, 0, NULL);
  1620. if (ret < 0)
  1621. return ret;
  1622. /* XXX: parse message */
  1623. if (rt->state != RTSP_STATE_STREAMING)
  1624. return 0;
  1625. }
  1626. }
  1627. #endif
  1628. } else if (n == 0 && ++timeout_cnt >= MAX_TIMEOUTS) {
  1629. return AVERROR(ETIMEDOUT);
  1630. } else if (n < 0 && errno != EINTR)
  1631. return AVERROR(errno);
  1632. }
  1633. }
  1634. static int pick_stream(AVFormatContext *s, RTSPStream **rtsp_st,
  1635. const uint8_t *buf, int len)
  1636. {
  1637. RTSPState *rt = s->priv_data;
  1638. int i;
  1639. if (len < 0)
  1640. return len;
  1641. if (rt->nb_rtsp_streams == 1) {
  1642. *rtsp_st = rt->rtsp_streams[0];
  1643. return len;
  1644. }
  1645. if (len >= 8 && rt->transport == RTSP_TRANSPORT_RTP) {
  1646. if (RTP_PT_IS_RTCP(rt->recvbuf[1])) {
  1647. int no_ssrc = 0;
  1648. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1649. RTPDemuxContext *rtpctx = rt->rtsp_streams[i]->transport_priv;
  1650. if (!rtpctx)
  1651. continue;
  1652. if (rtpctx->ssrc == AV_RB32(&buf[4])) {
  1653. *rtsp_st = rt->rtsp_streams[i];
  1654. return len;
  1655. }
  1656. if (!rtpctx->ssrc)
  1657. no_ssrc = 1;
  1658. }
  1659. if (no_ssrc) {
  1660. av_log(s, AV_LOG_WARNING,
  1661. "Unable to pick stream for packet - SSRC not known for "
  1662. "all streams\n");
  1663. return AVERROR(EAGAIN);
  1664. }
  1665. } else {
  1666. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1667. if ((buf[1] & 0x7f) == rt->rtsp_streams[i]->sdp_payload_type) {
  1668. *rtsp_st = rt->rtsp_streams[i];
  1669. return len;
  1670. }
  1671. }
  1672. }
  1673. }
  1674. av_log(s, AV_LOG_WARNING, "Unable to pick stream for packet\n");
  1675. return AVERROR(EAGAIN);
  1676. }
  1677. int ff_rtsp_fetch_packet(AVFormatContext *s, AVPacket *pkt)
  1678. {
  1679. RTSPState *rt = s->priv_data;
  1680. int ret, len;
  1681. RTSPStream *rtsp_st, *first_queue_st = NULL;
  1682. int64_t wait_end = 0;
  1683. if (rt->nb_byes == rt->nb_rtsp_streams)
  1684. return AVERROR_EOF;
  1685. /* get next frames from the same RTP packet */
  1686. if (rt->cur_transport_priv) {
  1687. if (rt->transport == RTSP_TRANSPORT_RDT) {
  1688. ret = ff_rdt_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
  1689. } else if (rt->transport == RTSP_TRANSPORT_RTP) {
  1690. ret = ff_rtp_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
  1691. } else if (rt->ts && CONFIG_RTPDEC) {
  1692. ret = ff_mpegts_parse_packet(rt->ts, pkt, rt->recvbuf + rt->recvbuf_pos, rt->recvbuf_len - rt->recvbuf_pos);
  1693. if (ret >= 0) {
  1694. rt->recvbuf_pos += ret;
  1695. ret = rt->recvbuf_pos < rt->recvbuf_len;
  1696. }
  1697. } else
  1698. ret = -1;
  1699. if (ret == 0) {
  1700. rt->cur_transport_priv = NULL;
  1701. return 0;
  1702. } else if (ret == 1) {
  1703. return 0;
  1704. } else
  1705. rt->cur_transport_priv = NULL;
  1706. }
  1707. redo:
  1708. if (rt->transport == RTSP_TRANSPORT_RTP) {
  1709. int i;
  1710. int64_t first_queue_time = 0;
  1711. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1712. RTPDemuxContext *rtpctx = rt->rtsp_streams[i]->transport_priv;
  1713. int64_t queue_time;
  1714. if (!rtpctx)
  1715. continue;
  1716. queue_time = ff_rtp_queued_packet_time(rtpctx);
  1717. if (queue_time && (queue_time - first_queue_time < 0 ||
  1718. !first_queue_time)) {
  1719. first_queue_time = queue_time;
  1720. first_queue_st = rt->rtsp_streams[i];
  1721. }
  1722. }
  1723. if (first_queue_time) {
  1724. wait_end = first_queue_time + s->max_delay;
  1725. } else {
  1726. wait_end = 0;
  1727. first_queue_st = NULL;
  1728. }
  1729. }
  1730. /* read next RTP packet */
  1731. if (!rt->recvbuf) {
  1732. rt->recvbuf = av_malloc(RECVBUF_SIZE);
  1733. if (!rt->recvbuf)
  1734. return AVERROR(ENOMEM);
  1735. }
  1736. switch(rt->lower_transport) {
  1737. default:
  1738. #if CONFIG_RTSP_DEMUXER
  1739. case RTSP_LOWER_TRANSPORT_TCP:
  1740. len = ff_rtsp_tcp_read_packet(s, &rtsp_st, rt->recvbuf, RECVBUF_SIZE);
  1741. break;
  1742. #endif
  1743. case RTSP_LOWER_TRANSPORT_UDP:
  1744. case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
  1745. len = udp_read_packet(s, &rtsp_st, rt->recvbuf, RECVBUF_SIZE, wait_end);
  1746. if (len > 0 && rtsp_st->transport_priv && rt->transport == RTSP_TRANSPORT_RTP)
  1747. ff_rtp_check_and_send_back_rr(rtsp_st->transport_priv, rtsp_st->rtp_handle, NULL, len);
  1748. break;
  1749. case RTSP_LOWER_TRANSPORT_CUSTOM:
  1750. if (first_queue_st && rt->transport == RTSP_TRANSPORT_RTP &&
  1751. wait_end && wait_end < av_gettime())
  1752. len = AVERROR(EAGAIN);
  1753. else
  1754. len = ffio_read_partial(s->pb, rt->recvbuf, RECVBUF_SIZE);
  1755. len = pick_stream(s, &rtsp_st, rt->recvbuf, len);
  1756. if (len > 0 && rtsp_st->transport_priv && rt->transport == RTSP_TRANSPORT_RTP)
  1757. ff_rtp_check_and_send_back_rr(rtsp_st->transport_priv, NULL, s->pb, len);
  1758. break;
  1759. }
  1760. if (len == AVERROR(EAGAIN) && first_queue_st &&
  1761. rt->transport == RTSP_TRANSPORT_RTP) {
  1762. rtsp_st = first_queue_st;
  1763. ret = ff_rtp_parse_packet(rtsp_st->transport_priv, pkt, NULL, 0);
  1764. goto end;
  1765. }
  1766. if (len < 0)
  1767. return len;
  1768. if (len == 0)
  1769. return AVERROR_EOF;
  1770. if (rt->transport == RTSP_TRANSPORT_RDT) {
  1771. ret = ff_rdt_parse_packet(rtsp_st->transport_priv, pkt, &rt->recvbuf, len);
  1772. } else if (rt->transport == RTSP_TRANSPORT_RTP) {
  1773. ret = ff_rtp_parse_packet(rtsp_st->transport_priv, pkt, &rt->recvbuf, len);
  1774. if (rtsp_st->feedback) {
  1775. AVIOContext *pb = NULL;
  1776. if (rt->lower_transport == RTSP_LOWER_TRANSPORT_CUSTOM)
  1777. pb = s->pb;
  1778. ff_rtp_send_rtcp_feedback(rtsp_st->transport_priv, rtsp_st->rtp_handle, pb);
  1779. }
  1780. if (ret < 0) {
  1781. /* Either bad packet, or a RTCP packet. Check if the
  1782. * first_rtcp_ntp_time field was initialized. */
  1783. RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
  1784. if (rtpctx->first_rtcp_ntp_time != AV_NOPTS_VALUE) {
  1785. /* first_rtcp_ntp_time has been initialized for this stream,
  1786. * copy the same value to all other uninitialized streams,
  1787. * in order to map their timestamp origin to the same ntp time
  1788. * as this one. */
  1789. int i;
  1790. AVStream *st = NULL;
  1791. if (rtsp_st->stream_index >= 0)
  1792. st = s->streams[rtsp_st->stream_index];
  1793. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1794. RTPDemuxContext *rtpctx2 = rt->rtsp_streams[i]->transport_priv;
  1795. AVStream *st2 = NULL;
  1796. if (rt->rtsp_streams[i]->stream_index >= 0)
  1797. st2 = s->streams[rt->rtsp_streams[i]->stream_index];
  1798. if (rtpctx2 && st && st2 &&
  1799. rtpctx2->first_rtcp_ntp_time == AV_NOPTS_VALUE) {
  1800. rtpctx2->first_rtcp_ntp_time = rtpctx->first_rtcp_ntp_time;
  1801. rtpctx2->rtcp_ts_offset = av_rescale_q(
  1802. rtpctx->rtcp_ts_offset, st->time_base,
  1803. st2->time_base);
  1804. }
  1805. }
  1806. }
  1807. if (ret == -RTCP_BYE) {
  1808. rt->nb_byes++;
  1809. av_log(s, AV_LOG_DEBUG, "Received BYE for stream %d (%d/%d)\n",
  1810. rtsp_st->stream_index, rt->nb_byes, rt->nb_rtsp_streams);
  1811. if (rt->nb_byes == rt->nb_rtsp_streams)
  1812. return AVERROR_EOF;
  1813. }
  1814. }
  1815. } else if (rt->ts && CONFIG_RTPDEC) {
  1816. ret = ff_mpegts_parse_packet(rt->ts, pkt, rt->recvbuf, len);
  1817. if (ret >= 0) {
  1818. if (ret < len) {
  1819. rt->recvbuf_len = len;
  1820. rt->recvbuf_pos = ret;
  1821. rt->cur_transport_priv = rt->ts;
  1822. return 1;
  1823. } else {
  1824. ret = 0;
  1825. }
  1826. }
  1827. } else {
  1828. return AVERROR_INVALIDDATA;
  1829. }
  1830. end:
  1831. if (ret < 0)
  1832. goto redo;
  1833. if (ret == 1)
  1834. /* more packets may follow, so we save the RTP context */
  1835. rt->cur_transport_priv = rtsp_st->transport_priv;
  1836. return ret;
  1837. }
  1838. #endif /* CONFIG_RTPDEC */
  1839. #if CONFIG_SDP_DEMUXER
  1840. static int sdp_probe(AVProbeData *p1)
  1841. {
  1842. const char *p = p1->buf, *p_end = p1->buf + p1->buf_size;
  1843. /* we look for a line beginning "c=IN IP" */
  1844. while (p < p_end && *p != '\0') {
  1845. if (p + sizeof("c=IN IP") - 1 < p_end &&
  1846. av_strstart(p, "c=IN IP", NULL))
  1847. return AVPROBE_SCORE_MAX / 2;
  1848. while (p < p_end - 1 && *p != '\n') p++;
  1849. if (++p >= p_end)
  1850. break;
  1851. if (*p == '\r')
  1852. p++;
  1853. }
  1854. return 0;
  1855. }
  1856. static int sdp_read_header(AVFormatContext *s)
  1857. {
  1858. RTSPState *rt = s->priv_data;
  1859. RTSPStream *rtsp_st;
  1860. int size, i, err;
  1861. char *content;
  1862. char url[1024];
  1863. if (!ff_network_init())
  1864. return AVERROR(EIO);
  1865. if (s->max_delay < 0) /* Not set by the caller */
  1866. s->max_delay = DEFAULT_REORDERING_DELAY;
  1867. if (rt->rtsp_flags & RTSP_FLAG_CUSTOM_IO)
  1868. rt->lower_transport = RTSP_LOWER_TRANSPORT_CUSTOM;
  1869. /* read the whole sdp file */
  1870. /* XXX: better loading */
  1871. content = av_malloc(SDP_MAX_SIZE);
  1872. size = avio_read(s->pb, content, SDP_MAX_SIZE - 1);
  1873. if (size <= 0) {
  1874. av_free(content);
  1875. return AVERROR_INVALIDDATA;
  1876. }
  1877. content[size] ='\0';
  1878. err = ff_sdp_parse(s, content);
  1879. av_free(content);
  1880. if (err) goto fail;
  1881. /* open each RTP stream */
  1882. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1883. char namebuf[50];
  1884. rtsp_st = rt->rtsp_streams[i];
  1885. if (!(rt->rtsp_flags & RTSP_FLAG_CUSTOM_IO)) {
  1886. getnameinfo((struct sockaddr*) &rtsp_st->sdp_ip, sizeof(rtsp_st->sdp_ip),
  1887. namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST);
  1888. ff_url_join(url, sizeof(url), "rtp", NULL,
  1889. namebuf, rtsp_st->sdp_port,
  1890. "?localport=%d&ttl=%d&connect=%d", rtsp_st->sdp_port,
  1891. rtsp_st->sdp_ttl,
  1892. rt->rtsp_flags & RTSP_FLAG_FILTER_SRC ? 1 : 0);
  1893. if (ffurl_open(&rtsp_st->rtp_handle, url, AVIO_FLAG_READ_WRITE,
  1894. &s->interrupt_callback, NULL) < 0) {
  1895. err = AVERROR_INVALIDDATA;
  1896. goto fail;
  1897. }
  1898. }
  1899. if ((err = ff_rtsp_open_transport_ctx(s, rtsp_st)))
  1900. goto fail;
  1901. }
  1902. return 0;
  1903. fail:
  1904. ff_rtsp_close_streams(s);
  1905. ff_network_close();
  1906. return err;
  1907. }
  1908. static int sdp_read_close(AVFormatContext *s)
  1909. {
  1910. ff_rtsp_close_streams(s);
  1911. ff_network_close();
  1912. return 0;
  1913. }
  1914. static const AVClass sdp_demuxer_class = {
  1915. .class_name = "SDP demuxer",
  1916. .item_name = av_default_item_name,
  1917. .option = sdp_options,
  1918. .version = LIBAVUTIL_VERSION_INT,
  1919. };
  1920. AVInputFormat ff_sdp_demuxer = {
  1921. .name = "sdp",
  1922. .long_name = NULL_IF_CONFIG_SMALL("SDP"),
  1923. .priv_data_size = sizeof(RTSPState),
  1924. .read_probe = sdp_probe,
  1925. .read_header = sdp_read_header,
  1926. .read_packet = ff_rtsp_fetch_packet,
  1927. .read_close = sdp_read_close,
  1928. .priv_class = &sdp_demuxer_class,
  1929. };
  1930. #endif /* CONFIG_SDP_DEMUXER */
  1931. #if CONFIG_RTP_DEMUXER
  1932. static int rtp_probe(AVProbeData *p)
  1933. {
  1934. if (av_strstart(p->filename, "rtp:", NULL))
  1935. return AVPROBE_SCORE_MAX;
  1936. return 0;
  1937. }
  1938. static int rtp_read_header(AVFormatContext *s)
  1939. {
  1940. uint8_t recvbuf[1500];
  1941. char host[500], sdp[500];
  1942. int ret, port;
  1943. URLContext* in = NULL;
  1944. int payload_type;
  1945. AVCodecContext codec = { 0 };
  1946. struct sockaddr_storage addr;
  1947. AVIOContext pb;
  1948. socklen_t addrlen = sizeof(addr);
  1949. RTSPState *rt = s->priv_data;
  1950. if (!ff_network_init())
  1951. return AVERROR(EIO);
  1952. ret = ffurl_open(&in, s->filename, AVIO_FLAG_READ,
  1953. &s->interrupt_callback, NULL);
  1954. if (ret)
  1955. goto fail;
  1956. while (1) {
  1957. ret = ffurl_read(in, recvbuf, sizeof(recvbuf));
  1958. if (ret == AVERROR(EAGAIN))
  1959. continue;
  1960. if (ret < 0)
  1961. goto fail;
  1962. if (ret < 12) {
  1963. av_log(s, AV_LOG_WARNING, "Received too short packet\n");
  1964. continue;
  1965. }
  1966. if ((recvbuf[0] & 0xc0) != 0x80) {
  1967. av_log(s, AV_LOG_WARNING, "Unsupported RTP version packet "
  1968. "received\n");
  1969. continue;
  1970. }
  1971. if (RTP_PT_IS_RTCP(recvbuf[1]))
  1972. continue;
  1973. payload_type = recvbuf[1] & 0x7f;
  1974. break;
  1975. }
  1976. getsockname(ffurl_get_file_handle(in), (struct sockaddr*) &addr, &addrlen);
  1977. ffurl_close(in);
  1978. in = NULL;
  1979. if (ff_rtp_get_codec_info(&codec, payload_type)) {
  1980. av_log(s, AV_LOG_ERROR, "Unable to receive RTP payload type %d "
  1981. "without an SDP file describing it\n",
  1982. payload_type);
  1983. goto fail;
  1984. }
  1985. if (codec.codec_type != AVMEDIA_TYPE_DATA) {
  1986. av_log(s, AV_LOG_WARNING, "Guessing on RTP content - if not received "
  1987. "properly you need an SDP file "
  1988. "describing it\n");
  1989. }
  1990. av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port,
  1991. NULL, 0, s->filename);
  1992. snprintf(sdp, sizeof(sdp),
  1993. "v=0\r\nc=IN IP%d %s\r\nm=%s %d RTP/AVP %d\r\n",
  1994. addr.ss_family == AF_INET ? 4 : 6, host,
  1995. codec.codec_type == AVMEDIA_TYPE_DATA ? "application" :
  1996. codec.codec_type == AVMEDIA_TYPE_VIDEO ? "video" : "audio",
  1997. port, payload_type);
  1998. av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sdp);
  1999. ffio_init_context(&pb, sdp, strlen(sdp), 0, NULL, NULL, NULL, NULL);
  2000. s->pb = &pb;
  2001. /* sdp_read_header initializes this again */
  2002. ff_network_close();
  2003. rt->media_type_mask = (1 << (AVMEDIA_TYPE_DATA+1)) - 1;
  2004. ret = sdp_read_header(s);
  2005. s->pb = NULL;
  2006. return ret;
  2007. fail:
  2008. if (in)
  2009. ffurl_close(in);
  2010. ff_network_close();
  2011. return ret;
  2012. }
  2013. static const AVClass rtp_demuxer_class = {
  2014. .class_name = "RTP demuxer",
  2015. .item_name = av_default_item_name,
  2016. .option = rtp_options,
  2017. .version = LIBAVUTIL_VERSION_INT,
  2018. };
  2019. AVInputFormat ff_rtp_demuxer = {
  2020. .name = "rtp",
  2021. .long_name = NULL_IF_CONFIG_SMALL("RTP input"),
  2022. .priv_data_size = sizeof(RTSPState),
  2023. .read_probe = rtp_probe,
  2024. .read_header = rtp_read_header,
  2025. .read_packet = ff_rtsp_fetch_packet,
  2026. .read_close = sdp_read_close,
  2027. .flags = AVFMT_NOFILE,
  2028. .priv_class = &rtp_demuxer_class,
  2029. };
  2030. #endif /* CONFIG_RTP_DEMUXER */