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.

2226 lines
81KB

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