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.

2238 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 if (av_strstart(p, "crypto:", &p) && s->nb_streams > 0) {
  444. // RFC 4568
  445. rtsp_st = rt->rtsp_streams[rt->nb_rtsp_streams - 1];
  446. get_word(buf1, sizeof(buf1), &p); // ignore tag
  447. get_word(rtsp_st->crypto_suite, sizeof(rtsp_st->crypto_suite), &p);
  448. p += strspn(p, SPACE_CHARS);
  449. if (av_strstart(p, "inline:", &p))
  450. get_word(rtsp_st->crypto_params, sizeof(rtsp_st->crypto_params), &p);
  451. } else {
  452. if (rt->server_type == RTSP_SERVER_WMS)
  453. ff_wms_parse_sdp_a_line(s, p);
  454. if (s->nb_streams > 0) {
  455. rtsp_st = rt->rtsp_streams[rt->nb_rtsp_streams - 1];
  456. if (rt->server_type == RTSP_SERVER_REAL)
  457. ff_real_parse_sdp_a_line(s, rtsp_st->stream_index, p);
  458. if (rtsp_st->dynamic_handler &&
  459. rtsp_st->dynamic_handler->parse_sdp_a_line)
  460. rtsp_st->dynamic_handler->parse_sdp_a_line(s,
  461. rtsp_st->stream_index,
  462. rtsp_st->dynamic_protocol_context, buf);
  463. }
  464. }
  465. break;
  466. }
  467. }
  468. int ff_sdp_parse(AVFormatContext *s, const char *content)
  469. {
  470. RTSPState *rt = s->priv_data;
  471. const char *p;
  472. int letter;
  473. /* Some SDP lines, particularly for Realmedia or ASF RTSP streams,
  474. * contain long SDP lines containing complete ASF Headers (several
  475. * kB) or arrays of MDPR (RM stream descriptor) headers plus
  476. * "rulebooks" describing their properties. Therefore, the SDP line
  477. * buffer is large.
  478. *
  479. * The Vorbis FMTP line can be up to 16KB - see xiph_parse_sdp_line
  480. * in rtpdec_xiph.c. */
  481. char buf[16384], *q;
  482. SDPParseState sdp_parse_state = { { 0 } }, *s1 = &sdp_parse_state;
  483. p = content;
  484. for (;;) {
  485. p += strspn(p, SPACE_CHARS);
  486. letter = *p;
  487. if (letter == '\0')
  488. break;
  489. p++;
  490. if (*p != '=')
  491. goto next_line;
  492. p++;
  493. /* get the content */
  494. q = buf;
  495. while (*p != '\n' && *p != '\r' && *p != '\0') {
  496. if ((q - buf) < sizeof(buf) - 1)
  497. *q++ = *p;
  498. p++;
  499. }
  500. *q = '\0';
  501. sdp_parse_line(s, s1, letter, buf);
  502. next_line:
  503. while (*p != '\n' && *p != '\0')
  504. p++;
  505. if (*p == '\n')
  506. p++;
  507. }
  508. rt->p = av_malloc(sizeof(struct pollfd)*2*(rt->nb_rtsp_streams+1));
  509. if (!rt->p) return AVERROR(ENOMEM);
  510. return 0;
  511. }
  512. #endif /* CONFIG_RTPDEC */
  513. void ff_rtsp_undo_setup(AVFormatContext *s)
  514. {
  515. RTSPState *rt = s->priv_data;
  516. int i;
  517. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  518. RTSPStream *rtsp_st = rt->rtsp_streams[i];
  519. if (!rtsp_st)
  520. continue;
  521. if (rtsp_st->transport_priv) {
  522. if (s->oformat) {
  523. AVFormatContext *rtpctx = rtsp_st->transport_priv;
  524. av_write_trailer(rtpctx);
  525. if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
  526. uint8_t *ptr;
  527. avio_close_dyn_buf(rtpctx->pb, &ptr);
  528. av_free(ptr);
  529. } else {
  530. avio_close(rtpctx->pb);
  531. }
  532. avformat_free_context(rtpctx);
  533. } else if (rt->transport == RTSP_TRANSPORT_RDT && CONFIG_RTPDEC)
  534. ff_rdt_parse_close(rtsp_st->transport_priv);
  535. else if (rt->transport == RTSP_TRANSPORT_RTP && CONFIG_RTPDEC)
  536. ff_rtp_parse_close(rtsp_st->transport_priv);
  537. }
  538. rtsp_st->transport_priv = NULL;
  539. if (rtsp_st->rtp_handle)
  540. ffurl_close(rtsp_st->rtp_handle);
  541. rtsp_st->rtp_handle = NULL;
  542. }
  543. }
  544. /* close and free RTSP streams */
  545. void ff_rtsp_close_streams(AVFormatContext *s)
  546. {
  547. RTSPState *rt = s->priv_data;
  548. int i;
  549. RTSPStream *rtsp_st;
  550. ff_rtsp_undo_setup(s);
  551. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  552. rtsp_st = rt->rtsp_streams[i];
  553. if (rtsp_st) {
  554. if (rtsp_st->dynamic_handler && rtsp_st->dynamic_protocol_context)
  555. rtsp_st->dynamic_handler->free(
  556. rtsp_st->dynamic_protocol_context);
  557. av_free(rtsp_st);
  558. }
  559. }
  560. av_free(rt->rtsp_streams);
  561. if (rt->asf_ctx) {
  562. avformat_close_input(&rt->asf_ctx);
  563. }
  564. if (rt->ts && CONFIG_RTPDEC)
  565. ff_mpegts_parse_close(rt->ts);
  566. av_free(rt->p);
  567. av_free(rt->recvbuf);
  568. }
  569. int ff_rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st)
  570. {
  571. RTSPState *rt = s->priv_data;
  572. AVStream *st = NULL;
  573. int reordering_queue_size = rt->reordering_queue_size;
  574. if (reordering_queue_size < 0) {
  575. if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP || !s->max_delay)
  576. reordering_queue_size = 0;
  577. else
  578. reordering_queue_size = RTP_REORDER_QUEUE_DEFAULT_SIZE;
  579. }
  580. /* open the RTP context */
  581. if (rtsp_st->stream_index >= 0)
  582. st = s->streams[rtsp_st->stream_index];
  583. if (!st)
  584. s->ctx_flags |= AVFMTCTX_NOHEADER;
  585. if (s->oformat && CONFIG_RTSP_MUXER) {
  586. int ret = ff_rtp_chain_mux_open(&rtsp_st->transport_priv, s, st,
  587. rtsp_st->rtp_handle,
  588. RTSP_TCP_MAX_PACKET_SIZE,
  589. rtsp_st->stream_index);
  590. /* Ownership of rtp_handle is passed to the rtp mux context */
  591. rtsp_st->rtp_handle = NULL;
  592. if (ret < 0)
  593. return ret;
  594. } else if (rt->transport == RTSP_TRANSPORT_RAW) {
  595. return 0; // Don't need to open any parser here
  596. } else if (rt->transport == RTSP_TRANSPORT_RDT && CONFIG_RTPDEC)
  597. rtsp_st->transport_priv = ff_rdt_parse_open(s, st->index,
  598. rtsp_st->dynamic_protocol_context,
  599. rtsp_st->dynamic_handler);
  600. else if (CONFIG_RTPDEC)
  601. rtsp_st->transport_priv = ff_rtp_parse_open(s, st,
  602. rtsp_st->sdp_payload_type,
  603. reordering_queue_size);
  604. if (!rtsp_st->transport_priv) {
  605. return AVERROR(ENOMEM);
  606. } else if (rt->transport == RTSP_TRANSPORT_RTP && CONFIG_RTPDEC) {
  607. if (rtsp_st->dynamic_handler) {
  608. ff_rtp_parse_set_dynamic_protocol(rtsp_st->transport_priv,
  609. rtsp_st->dynamic_protocol_context,
  610. rtsp_st->dynamic_handler);
  611. }
  612. if (rtsp_st->crypto_suite[0])
  613. ff_rtp_parse_set_crypto(rtsp_st->transport_priv,
  614. rtsp_st->crypto_suite,
  615. rtsp_st->crypto_params);
  616. }
  617. return 0;
  618. }
  619. #if CONFIG_RTSP_DEMUXER || CONFIG_RTSP_MUXER
  620. static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
  621. {
  622. const char *q;
  623. char *p;
  624. int v;
  625. q = *pp;
  626. q += strspn(q, SPACE_CHARS);
  627. v = strtol(q, &p, 10);
  628. if (*p == '-') {
  629. p++;
  630. *min_ptr = v;
  631. v = strtol(p, &p, 10);
  632. *max_ptr = v;
  633. } else {
  634. *min_ptr = v;
  635. *max_ptr = v;
  636. }
  637. *pp = p;
  638. }
  639. /* XXX: only one transport specification is parsed */
  640. static void rtsp_parse_transport(RTSPMessageHeader *reply, const char *p)
  641. {
  642. char transport_protocol[16];
  643. char profile[16];
  644. char lower_transport[16];
  645. char parameter[16];
  646. RTSPTransportField *th;
  647. char buf[256];
  648. reply->nb_transports = 0;
  649. for (;;) {
  650. p += strspn(p, SPACE_CHARS);
  651. if (*p == '\0')
  652. break;
  653. th = &reply->transports[reply->nb_transports];
  654. get_word_sep(transport_protocol, sizeof(transport_protocol),
  655. "/", &p);
  656. if (!av_strcasecmp (transport_protocol, "rtp")) {
  657. get_word_sep(profile, sizeof(profile), "/;,", &p);
  658. lower_transport[0] = '\0';
  659. /* rtp/avp/<protocol> */
  660. if (*p == '/') {
  661. get_word_sep(lower_transport, sizeof(lower_transport),
  662. ";,", &p);
  663. }
  664. th->transport = RTSP_TRANSPORT_RTP;
  665. } else if (!av_strcasecmp (transport_protocol, "x-pn-tng") ||
  666. !av_strcasecmp (transport_protocol, "x-real-rdt")) {
  667. /* x-pn-tng/<protocol> */
  668. get_word_sep(lower_transport, sizeof(lower_transport), "/;,", &p);
  669. profile[0] = '\0';
  670. th->transport = RTSP_TRANSPORT_RDT;
  671. } else if (!av_strcasecmp(transport_protocol, "raw")) {
  672. get_word_sep(profile, sizeof(profile), "/;,", &p);
  673. lower_transport[0] = '\0';
  674. /* raw/raw/<protocol> */
  675. if (*p == '/') {
  676. get_word_sep(lower_transport, sizeof(lower_transport),
  677. ";,", &p);
  678. }
  679. th->transport = RTSP_TRANSPORT_RAW;
  680. }
  681. if (!av_strcasecmp(lower_transport, "TCP"))
  682. th->lower_transport = RTSP_LOWER_TRANSPORT_TCP;
  683. else
  684. th->lower_transport = RTSP_LOWER_TRANSPORT_UDP;
  685. if (*p == ';')
  686. p++;
  687. /* get each parameter */
  688. while (*p != '\0' && *p != ',') {
  689. get_word_sep(parameter, sizeof(parameter), "=;,", &p);
  690. if (!strcmp(parameter, "port")) {
  691. if (*p == '=') {
  692. p++;
  693. rtsp_parse_range(&th->port_min, &th->port_max, &p);
  694. }
  695. } else if (!strcmp(parameter, "client_port")) {
  696. if (*p == '=') {
  697. p++;
  698. rtsp_parse_range(&th->client_port_min,
  699. &th->client_port_max, &p);
  700. }
  701. } else if (!strcmp(parameter, "server_port")) {
  702. if (*p == '=') {
  703. p++;
  704. rtsp_parse_range(&th->server_port_min,
  705. &th->server_port_max, &p);
  706. }
  707. } else if (!strcmp(parameter, "interleaved")) {
  708. if (*p == '=') {
  709. p++;
  710. rtsp_parse_range(&th->interleaved_min,
  711. &th->interleaved_max, &p);
  712. }
  713. } else if (!strcmp(parameter, "multicast")) {
  714. if (th->lower_transport == RTSP_LOWER_TRANSPORT_UDP)
  715. th->lower_transport = RTSP_LOWER_TRANSPORT_UDP_MULTICAST;
  716. } else if (!strcmp(parameter, "ttl")) {
  717. if (*p == '=') {
  718. char *end;
  719. p++;
  720. th->ttl = strtol(p, &end, 10);
  721. p = end;
  722. }
  723. } else if (!strcmp(parameter, "destination")) {
  724. if (*p == '=') {
  725. p++;
  726. get_word_sep(buf, sizeof(buf), ";,", &p);
  727. get_sockaddr(buf, &th->destination);
  728. }
  729. } else if (!strcmp(parameter, "source")) {
  730. if (*p == '=') {
  731. p++;
  732. get_word_sep(buf, sizeof(buf), ";,", &p);
  733. av_strlcpy(th->source, buf, sizeof(th->source));
  734. }
  735. } else if (!strcmp(parameter, "mode")) {
  736. if (*p == '=') {
  737. p++;
  738. get_word_sep(buf, sizeof(buf), ";, ", &p);
  739. if (!strcmp(buf, "record") ||
  740. !strcmp(buf, "receive"))
  741. th->mode_record = 1;
  742. }
  743. }
  744. while (*p != ';' && *p != '\0' && *p != ',')
  745. p++;
  746. if (*p == ';')
  747. p++;
  748. }
  749. if (*p == ',')
  750. p++;
  751. reply->nb_transports++;
  752. }
  753. }
  754. static void handle_rtp_info(RTSPState *rt, const char *url,
  755. uint32_t seq, uint32_t rtptime)
  756. {
  757. int i;
  758. if (!rtptime || !url[0])
  759. return;
  760. if (rt->transport != RTSP_TRANSPORT_RTP)
  761. return;
  762. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  763. RTSPStream *rtsp_st = rt->rtsp_streams[i];
  764. RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
  765. if (!rtpctx)
  766. continue;
  767. if (!strcmp(rtsp_st->control_url, url)) {
  768. rtpctx->base_timestamp = rtptime;
  769. break;
  770. }
  771. }
  772. }
  773. static void rtsp_parse_rtp_info(RTSPState *rt, const char *p)
  774. {
  775. int read = 0;
  776. char key[20], value[1024], url[1024] = "";
  777. uint32_t seq = 0, rtptime = 0;
  778. for (;;) {
  779. p += strspn(p, SPACE_CHARS);
  780. if (!*p)
  781. break;
  782. get_word_sep(key, sizeof(key), "=", &p);
  783. if (*p != '=')
  784. break;
  785. p++;
  786. get_word_sep(value, sizeof(value), ";, ", &p);
  787. read++;
  788. if (!strcmp(key, "url"))
  789. av_strlcpy(url, value, sizeof(url));
  790. else if (!strcmp(key, "seq"))
  791. seq = strtoul(value, NULL, 10);
  792. else if (!strcmp(key, "rtptime"))
  793. rtptime = strtoul(value, NULL, 10);
  794. if (*p == ',') {
  795. handle_rtp_info(rt, url, seq, rtptime);
  796. url[0] = '\0';
  797. seq = rtptime = 0;
  798. read = 0;
  799. }
  800. if (*p)
  801. p++;
  802. }
  803. if (read > 0)
  804. handle_rtp_info(rt, url, seq, rtptime);
  805. }
  806. void ff_rtsp_parse_line(RTSPMessageHeader *reply, const char *buf,
  807. RTSPState *rt, const char *method)
  808. {
  809. const char *p;
  810. /* NOTE: we do case independent match for broken servers */
  811. p = buf;
  812. if (av_stristart(p, "Session:", &p)) {
  813. int t;
  814. get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
  815. if (av_stristart(p, ";timeout=", &p) &&
  816. (t = strtol(p, NULL, 10)) > 0) {
  817. reply->timeout = t;
  818. }
  819. } else if (av_stristart(p, "Content-Length:", &p)) {
  820. reply->content_length = strtol(p, NULL, 10);
  821. } else if (av_stristart(p, "Transport:", &p)) {
  822. rtsp_parse_transport(reply, p);
  823. } else if (av_stristart(p, "CSeq:", &p)) {
  824. reply->seq = strtol(p, NULL, 10);
  825. } else if (av_stristart(p, "Range:", &p)) {
  826. rtsp_parse_range_npt(p, &reply->range_start, &reply->range_end);
  827. } else if (av_stristart(p, "RealChallenge1:", &p)) {
  828. p += strspn(p, SPACE_CHARS);
  829. av_strlcpy(reply->real_challenge, p, sizeof(reply->real_challenge));
  830. } else if (av_stristart(p, "Server:", &p)) {
  831. p += strspn(p, SPACE_CHARS);
  832. av_strlcpy(reply->server, p, sizeof(reply->server));
  833. } else if (av_stristart(p, "Notice:", &p) ||
  834. av_stristart(p, "X-Notice:", &p)) {
  835. reply->notice = strtol(p, NULL, 10);
  836. } else if (av_stristart(p, "Location:", &p)) {
  837. p += strspn(p, SPACE_CHARS);
  838. av_strlcpy(reply->location, p , sizeof(reply->location));
  839. } else if (av_stristart(p, "WWW-Authenticate:", &p) && rt) {
  840. p += strspn(p, SPACE_CHARS);
  841. ff_http_auth_handle_header(&rt->auth_state, "WWW-Authenticate", p);
  842. } else if (av_stristart(p, "Authentication-Info:", &p) && rt) {
  843. p += strspn(p, SPACE_CHARS);
  844. ff_http_auth_handle_header(&rt->auth_state, "Authentication-Info", p);
  845. } else if (av_stristart(p, "Content-Base:", &p) && rt) {
  846. p += strspn(p, SPACE_CHARS);
  847. if (method && !strcmp(method, "DESCRIBE"))
  848. av_strlcpy(rt->control_uri, p , sizeof(rt->control_uri));
  849. } else if (av_stristart(p, "RTP-Info:", &p) && rt) {
  850. p += strspn(p, SPACE_CHARS);
  851. if (method && !strcmp(method, "PLAY"))
  852. rtsp_parse_rtp_info(rt, p);
  853. } else if (av_stristart(p, "Public:", &p) && rt) {
  854. if (strstr(p, "GET_PARAMETER") &&
  855. method && !strcmp(method, "OPTIONS"))
  856. rt->get_parameter_supported = 1;
  857. } else if (av_stristart(p, "x-Accept-Dynamic-Rate:", &p) && rt) {
  858. p += strspn(p, SPACE_CHARS);
  859. rt->accept_dynamic_rate = atoi(p);
  860. } else if (av_stristart(p, "Content-Type:", &p)) {
  861. p += strspn(p, SPACE_CHARS);
  862. av_strlcpy(reply->content_type, p, sizeof(reply->content_type));
  863. }
  864. }
  865. /* skip a RTP/TCP interleaved packet */
  866. void ff_rtsp_skip_packet(AVFormatContext *s)
  867. {
  868. RTSPState *rt = s->priv_data;
  869. int ret, len, len1;
  870. uint8_t buf[1024];
  871. ret = ffurl_read_complete(rt->rtsp_hd, buf, 3);
  872. if (ret != 3)
  873. return;
  874. len = AV_RB16(buf + 1);
  875. av_dlog(s, "skipping RTP packet len=%d\n", len);
  876. /* skip payload */
  877. while (len > 0) {
  878. len1 = len;
  879. if (len1 > sizeof(buf))
  880. len1 = sizeof(buf);
  881. ret = ffurl_read_complete(rt->rtsp_hd, buf, len1);
  882. if (ret != len1)
  883. return;
  884. len -= len1;
  885. }
  886. }
  887. int ff_rtsp_read_reply(AVFormatContext *s, RTSPMessageHeader *reply,
  888. unsigned char **content_ptr,
  889. int return_on_interleaved_data, const char *method)
  890. {
  891. RTSPState *rt = s->priv_data;
  892. char buf[4096], buf1[1024], *q;
  893. unsigned char ch;
  894. const char *p;
  895. int ret, content_length, line_count = 0, request = 0;
  896. unsigned char *content = NULL;
  897. start:
  898. line_count = 0;
  899. request = 0;
  900. content = NULL;
  901. memset(reply, 0, sizeof(*reply));
  902. /* parse reply (XXX: use buffers) */
  903. rt->last_reply[0] = '\0';
  904. for (;;) {
  905. q = buf;
  906. for (;;) {
  907. ret = ffurl_read_complete(rt->rtsp_hd, &ch, 1);
  908. av_dlog(s, "ret=%d c=%02x [%c]\n", ret, ch, ch);
  909. if (ret != 1)
  910. return AVERROR_EOF;
  911. if (ch == '\n')
  912. break;
  913. if (ch == '$') {
  914. /* XXX: only parse it if first char on line ? */
  915. if (return_on_interleaved_data) {
  916. return 1;
  917. } else
  918. ff_rtsp_skip_packet(s);
  919. } else if (ch != '\r') {
  920. if ((q - buf) < sizeof(buf) - 1)
  921. *q++ = ch;
  922. }
  923. }
  924. *q = '\0';
  925. av_dlog(s, "line='%s'\n", buf);
  926. /* test if last line */
  927. if (buf[0] == '\0')
  928. break;
  929. p = buf;
  930. if (line_count == 0) {
  931. /* get reply code */
  932. get_word(buf1, sizeof(buf1), &p);
  933. if (!strncmp(buf1, "RTSP/", 5)) {
  934. get_word(buf1, sizeof(buf1), &p);
  935. reply->status_code = atoi(buf1);
  936. av_strlcpy(reply->reason, p, sizeof(reply->reason));
  937. } else {
  938. av_strlcpy(reply->reason, buf1, sizeof(reply->reason)); // method
  939. get_word(buf1, sizeof(buf1), &p); // object
  940. request = 1;
  941. }
  942. } else {
  943. ff_rtsp_parse_line(reply, p, rt, method);
  944. av_strlcat(rt->last_reply, p, sizeof(rt->last_reply));
  945. av_strlcat(rt->last_reply, "\n", sizeof(rt->last_reply));
  946. }
  947. line_count++;
  948. }
  949. if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0' && !request)
  950. av_strlcpy(rt->session_id, reply->session_id, sizeof(rt->session_id));
  951. content_length = reply->content_length;
  952. if (content_length > 0) {
  953. /* leave some room for a trailing '\0' (useful for simple parsing) */
  954. content = av_malloc(content_length + 1);
  955. ffurl_read_complete(rt->rtsp_hd, content, content_length);
  956. content[content_length] = '\0';
  957. }
  958. if (content_ptr)
  959. *content_ptr = content;
  960. else
  961. av_free(content);
  962. if (request) {
  963. char buf[1024];
  964. char base64buf[AV_BASE64_SIZE(sizeof(buf))];
  965. const char* ptr = buf;
  966. if (!strcmp(reply->reason, "OPTIONS")) {
  967. snprintf(buf, sizeof(buf), "RTSP/1.0 200 OK\r\n");
  968. if (reply->seq)
  969. av_strlcatf(buf, sizeof(buf), "CSeq: %d\r\n", reply->seq);
  970. if (reply->session_id[0])
  971. av_strlcatf(buf, sizeof(buf), "Session: %s\r\n",
  972. reply->session_id);
  973. } else {
  974. snprintf(buf, sizeof(buf), "RTSP/1.0 501 Not Implemented\r\n");
  975. }
  976. av_strlcat(buf, "\r\n", sizeof(buf));
  977. if (rt->control_transport == RTSP_MODE_TUNNEL) {
  978. av_base64_encode(base64buf, sizeof(base64buf), buf, strlen(buf));
  979. ptr = base64buf;
  980. }
  981. ffurl_write(rt->rtsp_hd_out, ptr, strlen(ptr));
  982. rt->last_cmd_time = av_gettime();
  983. /* Even if the request from the server had data, it is not the data
  984. * that the caller wants or expects. The memory could also be leaked
  985. * if the actual following reply has content data. */
  986. if (content_ptr)
  987. av_freep(content_ptr);
  988. /* If method is set, this is called from ff_rtsp_send_cmd,
  989. * where a reply to exactly this request is awaited. For
  990. * callers from within packet receiving, we just want to
  991. * return to the caller and go back to receiving packets. */
  992. if (method)
  993. goto start;
  994. return 0;
  995. }
  996. if (rt->seq != reply->seq) {
  997. av_log(s, AV_LOG_WARNING, "CSeq %d expected, %d received.\n",
  998. rt->seq, reply->seq);
  999. }
  1000. /* EOS */
  1001. if (reply->notice == 2101 /* End-of-Stream Reached */ ||
  1002. reply->notice == 2104 /* Start-of-Stream Reached */ ||
  1003. reply->notice == 2306 /* Continuous Feed Terminated */) {
  1004. rt->state = RTSP_STATE_IDLE;
  1005. } else if (reply->notice >= 4400 && reply->notice < 5500) {
  1006. return AVERROR(EIO); /* data or server error */
  1007. } else if (reply->notice == 2401 /* Ticket Expired */ ||
  1008. (reply->notice >= 5500 && reply->notice < 5600) /* end of term */ )
  1009. return AVERROR(EPERM);
  1010. return 0;
  1011. }
  1012. /**
  1013. * Send a command to the RTSP server without waiting for the reply.
  1014. *
  1015. * @param s RTSP (de)muxer context
  1016. * @param method the method for the request
  1017. * @param url the target url for the request
  1018. * @param headers extra header lines to include in the request
  1019. * @param send_content if non-null, the data to send as request body content
  1020. * @param send_content_length the length of the send_content data, or 0 if
  1021. * send_content is null
  1022. *
  1023. * @return zero if success, nonzero otherwise
  1024. */
  1025. static int ff_rtsp_send_cmd_with_content_async(AVFormatContext *s,
  1026. const char *method, const char *url,
  1027. const char *headers,
  1028. const unsigned char *send_content,
  1029. int send_content_length)
  1030. {
  1031. RTSPState *rt = s->priv_data;
  1032. char buf[4096], *out_buf;
  1033. char base64buf[AV_BASE64_SIZE(sizeof(buf))];
  1034. /* Add in RTSP headers */
  1035. out_buf = buf;
  1036. rt->seq++;
  1037. snprintf(buf, sizeof(buf), "%s %s RTSP/1.0\r\n", method, url);
  1038. if (headers)
  1039. av_strlcat(buf, headers, sizeof(buf));
  1040. av_strlcatf(buf, sizeof(buf), "CSeq: %d\r\n", rt->seq);
  1041. if (rt->session_id[0] != '\0' && (!headers ||
  1042. !strstr(headers, "\nIf-Match:"))) {
  1043. av_strlcatf(buf, sizeof(buf), "Session: %s\r\n", rt->session_id);
  1044. }
  1045. if (rt->auth[0]) {
  1046. char *str = ff_http_auth_create_response(&rt->auth_state,
  1047. rt->auth, url, method);
  1048. if (str)
  1049. av_strlcat(buf, str, sizeof(buf));
  1050. av_free(str);
  1051. }
  1052. if (send_content_length > 0 && send_content)
  1053. av_strlcatf(buf, sizeof(buf), "Content-Length: %d\r\n", send_content_length);
  1054. av_strlcat(buf, "\r\n", sizeof(buf));
  1055. /* base64 encode rtsp if tunneling */
  1056. if (rt->control_transport == RTSP_MODE_TUNNEL) {
  1057. av_base64_encode(base64buf, sizeof(base64buf), buf, strlen(buf));
  1058. out_buf = base64buf;
  1059. }
  1060. av_dlog(s, "Sending:\n%s--\n", buf);
  1061. ffurl_write(rt->rtsp_hd_out, out_buf, strlen(out_buf));
  1062. if (send_content_length > 0 && send_content) {
  1063. if (rt->control_transport == RTSP_MODE_TUNNEL) {
  1064. av_log(s, AV_LOG_ERROR, "tunneling of RTSP requests "
  1065. "with content data not supported\n");
  1066. return AVERROR_PATCHWELCOME;
  1067. }
  1068. ffurl_write(rt->rtsp_hd_out, send_content, send_content_length);
  1069. }
  1070. rt->last_cmd_time = av_gettime();
  1071. return 0;
  1072. }
  1073. int ff_rtsp_send_cmd_async(AVFormatContext *s, const char *method,
  1074. const char *url, const char *headers)
  1075. {
  1076. return ff_rtsp_send_cmd_with_content_async(s, method, url, headers, NULL, 0);
  1077. }
  1078. int ff_rtsp_send_cmd(AVFormatContext *s, const char *method, const char *url,
  1079. const char *headers, RTSPMessageHeader *reply,
  1080. unsigned char **content_ptr)
  1081. {
  1082. return ff_rtsp_send_cmd_with_content(s, method, url, headers, reply,
  1083. content_ptr, NULL, 0);
  1084. }
  1085. int ff_rtsp_send_cmd_with_content(AVFormatContext *s,
  1086. const char *method, const char *url,
  1087. const char *header,
  1088. RTSPMessageHeader *reply,
  1089. unsigned char **content_ptr,
  1090. const unsigned char *send_content,
  1091. int send_content_length)
  1092. {
  1093. RTSPState *rt = s->priv_data;
  1094. HTTPAuthType cur_auth_type;
  1095. int ret, attempts = 0;
  1096. retry:
  1097. cur_auth_type = rt->auth_state.auth_type;
  1098. if ((ret = ff_rtsp_send_cmd_with_content_async(s, method, url, header,
  1099. send_content,
  1100. send_content_length)))
  1101. return ret;
  1102. if ((ret = ff_rtsp_read_reply(s, reply, content_ptr, 0, method) ) < 0)
  1103. return ret;
  1104. attempts++;
  1105. if (reply->status_code == 401 &&
  1106. (cur_auth_type == HTTP_AUTH_NONE || rt->auth_state.stale) &&
  1107. rt->auth_state.auth_type != HTTP_AUTH_NONE && attempts < 2)
  1108. goto retry;
  1109. if (reply->status_code > 400){
  1110. av_log(s, AV_LOG_ERROR, "method %s failed: %d%s\n",
  1111. method,
  1112. reply->status_code,
  1113. reply->reason);
  1114. av_log(s, AV_LOG_DEBUG, "%s\n", rt->last_reply);
  1115. }
  1116. return 0;
  1117. }
  1118. int ff_rtsp_make_setup_request(AVFormatContext *s, const char *host, int port,
  1119. int lower_transport, const char *real_challenge)
  1120. {
  1121. RTSPState *rt = s->priv_data;
  1122. int rtx = 0, j, i, err, interleave = 0, port_off;
  1123. RTSPStream *rtsp_st;
  1124. RTSPMessageHeader reply1, *reply = &reply1;
  1125. char cmd[2048];
  1126. const char *trans_pref;
  1127. if (rt->transport == RTSP_TRANSPORT_RDT)
  1128. trans_pref = "x-pn-tng";
  1129. else if (rt->transport == RTSP_TRANSPORT_RAW)
  1130. trans_pref = "RAW/RAW";
  1131. else
  1132. trans_pref = "RTP/AVP";
  1133. /* default timeout: 1 minute */
  1134. rt->timeout = 60;
  1135. /* for each stream, make the setup request */
  1136. /* XXX: we assume the same server is used for the control of each
  1137. * RTSP stream */
  1138. /* Choose a random starting offset within the first half of the
  1139. * port range, to allow for a number of ports to try even if the offset
  1140. * happens to be at the end of the random range. */
  1141. port_off = av_get_random_seed() % ((rt->rtp_port_max - rt->rtp_port_min)/2);
  1142. /* even random offset */
  1143. port_off -= port_off & 0x01;
  1144. for (j = rt->rtp_port_min + port_off, i = 0; i < rt->nb_rtsp_streams; ++i) {
  1145. char transport[2048];
  1146. /*
  1147. * WMS serves all UDP data over a single connection, the RTX, which
  1148. * isn't necessarily the first in the SDP but has to be the first
  1149. * to be set up, else the second/third SETUP will fail with a 461.
  1150. */
  1151. if (lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
  1152. rt->server_type == RTSP_SERVER_WMS) {
  1153. if (i == 0) {
  1154. /* rtx first */
  1155. for (rtx = 0; rtx < rt->nb_rtsp_streams; rtx++) {
  1156. int len = strlen(rt->rtsp_streams[rtx]->control_url);
  1157. if (len >= 4 &&
  1158. !strcmp(rt->rtsp_streams[rtx]->control_url + len - 4,
  1159. "/rtx"))
  1160. break;
  1161. }
  1162. if (rtx == rt->nb_rtsp_streams)
  1163. return -1; /* no RTX found */
  1164. rtsp_st = rt->rtsp_streams[rtx];
  1165. } else
  1166. rtsp_st = rt->rtsp_streams[i > rtx ? i : i - 1];
  1167. } else
  1168. rtsp_st = rt->rtsp_streams[i];
  1169. /* RTP/UDP */
  1170. if (lower_transport == RTSP_LOWER_TRANSPORT_UDP) {
  1171. char buf[256];
  1172. if (rt->server_type == RTSP_SERVER_WMS && i > 1) {
  1173. port = reply->transports[0].client_port_min;
  1174. goto have_port;
  1175. }
  1176. /* first try in specified port range */
  1177. while (j <= rt->rtp_port_max) {
  1178. ff_url_join(buf, sizeof(buf), "rtp", NULL, host, -1,
  1179. "?localport=%d", j);
  1180. /* we will use two ports per rtp stream (rtp and rtcp) */
  1181. j += 2;
  1182. if (!ffurl_open(&rtsp_st->rtp_handle, buf, AVIO_FLAG_READ_WRITE,
  1183. &s->interrupt_callback, NULL))
  1184. goto rtp_opened;
  1185. }
  1186. av_log(s, AV_LOG_ERROR, "Unable to open an input RTP port\n");
  1187. err = AVERROR(EIO);
  1188. goto fail;
  1189. rtp_opened:
  1190. port = ff_rtp_get_local_rtp_port(rtsp_st->rtp_handle);
  1191. have_port:
  1192. snprintf(transport, sizeof(transport) - 1,
  1193. "%s/UDP;", trans_pref);
  1194. if (rt->server_type != RTSP_SERVER_REAL)
  1195. av_strlcat(transport, "unicast;", sizeof(transport));
  1196. av_strlcatf(transport, sizeof(transport),
  1197. "client_port=%d", port);
  1198. if (rt->transport == RTSP_TRANSPORT_RTP &&
  1199. !(rt->server_type == RTSP_SERVER_WMS && i > 0))
  1200. av_strlcatf(transport, sizeof(transport), "-%d", port + 1);
  1201. }
  1202. /* RTP/TCP */
  1203. else if (lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
  1204. /* For WMS streams, the application streams are only used for
  1205. * UDP. When trying to set it up for TCP streams, the server
  1206. * will return an error. Therefore, we skip those streams. */
  1207. if (rt->server_type == RTSP_SERVER_WMS &&
  1208. (rtsp_st->stream_index < 0 ||
  1209. s->streams[rtsp_st->stream_index]->codec->codec_type ==
  1210. AVMEDIA_TYPE_DATA))
  1211. continue;
  1212. snprintf(transport, sizeof(transport) - 1,
  1213. "%s/TCP;", trans_pref);
  1214. if (rt->transport != RTSP_TRANSPORT_RDT)
  1215. av_strlcat(transport, "unicast;", sizeof(transport));
  1216. av_strlcatf(transport, sizeof(transport),
  1217. "interleaved=%d-%d",
  1218. interleave, interleave + 1);
  1219. interleave += 2;
  1220. }
  1221. else if (lower_transport == RTSP_LOWER_TRANSPORT_UDP_MULTICAST) {
  1222. snprintf(transport, sizeof(transport) - 1,
  1223. "%s/UDP;multicast", trans_pref);
  1224. }
  1225. if (s->oformat) {
  1226. av_strlcat(transport, ";mode=record", sizeof(transport));
  1227. } else if (rt->server_type == RTSP_SERVER_REAL ||
  1228. rt->server_type == RTSP_SERVER_WMS)
  1229. av_strlcat(transport, ";mode=play", sizeof(transport));
  1230. snprintf(cmd, sizeof(cmd),
  1231. "Transport: %s\r\n",
  1232. transport);
  1233. if (rt->accept_dynamic_rate)
  1234. av_strlcat(cmd, "x-Dynamic-Rate: 0\r\n", sizeof(cmd));
  1235. if (i == 0 && rt->server_type == RTSP_SERVER_REAL && CONFIG_RTPDEC) {
  1236. char real_res[41], real_csum[9];
  1237. ff_rdt_calc_response_and_checksum(real_res, real_csum,
  1238. real_challenge);
  1239. av_strlcatf(cmd, sizeof(cmd),
  1240. "If-Match: %s\r\n"
  1241. "RealChallenge2: %s, sd=%s\r\n",
  1242. rt->session_id, real_res, real_csum);
  1243. }
  1244. ff_rtsp_send_cmd(s, "SETUP", rtsp_st->control_url, cmd, reply, NULL);
  1245. if (reply->status_code == 461 /* Unsupported protocol */ && i == 0) {
  1246. err = 1;
  1247. goto fail;
  1248. } else if (reply->status_code != RTSP_STATUS_OK ||
  1249. reply->nb_transports != 1) {
  1250. err = AVERROR_INVALIDDATA;
  1251. goto fail;
  1252. }
  1253. /* XXX: same protocol for all streams is required */
  1254. if (i > 0) {
  1255. if (reply->transports[0].lower_transport != rt->lower_transport ||
  1256. reply->transports[0].transport != rt->transport) {
  1257. err = AVERROR_INVALIDDATA;
  1258. goto fail;
  1259. }
  1260. } else {
  1261. rt->lower_transport = reply->transports[0].lower_transport;
  1262. rt->transport = reply->transports[0].transport;
  1263. }
  1264. /* Fail if the server responded with another lower transport mode
  1265. * than what we requested. */
  1266. if (reply->transports[0].lower_transport != lower_transport) {
  1267. av_log(s, AV_LOG_ERROR, "Nonmatching transport in server reply\n");
  1268. err = AVERROR_INVALIDDATA;
  1269. goto fail;
  1270. }
  1271. switch(reply->transports[0].lower_transport) {
  1272. case RTSP_LOWER_TRANSPORT_TCP:
  1273. rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
  1274. rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
  1275. break;
  1276. case RTSP_LOWER_TRANSPORT_UDP: {
  1277. char url[1024], options[30] = "";
  1278. if (rt->rtsp_flags & RTSP_FLAG_FILTER_SRC)
  1279. av_strlcpy(options, "?connect=1", sizeof(options));
  1280. /* Use source address if specified */
  1281. if (reply->transports[0].source[0]) {
  1282. ff_url_join(url, sizeof(url), "rtp", NULL,
  1283. reply->transports[0].source,
  1284. reply->transports[0].server_port_min, "%s", options);
  1285. } else {
  1286. ff_url_join(url, sizeof(url), "rtp", NULL, host,
  1287. reply->transports[0].server_port_min, "%s", options);
  1288. }
  1289. if (!(rt->server_type == RTSP_SERVER_WMS && i > 1) &&
  1290. ff_rtp_set_remote_url(rtsp_st->rtp_handle, url) < 0) {
  1291. err = AVERROR_INVALIDDATA;
  1292. goto fail;
  1293. }
  1294. /* Try to initialize the connection state in a
  1295. * potential NAT router by sending dummy packets.
  1296. * RTP/RTCP dummy packets are used for RDT, too.
  1297. */
  1298. if (!(rt->server_type == RTSP_SERVER_WMS && i > 1) && s->iformat &&
  1299. CONFIG_RTPDEC)
  1300. ff_rtp_send_punch_packets(rtsp_st->rtp_handle);
  1301. break;
  1302. }
  1303. case RTSP_LOWER_TRANSPORT_UDP_MULTICAST: {
  1304. char url[1024], namebuf[50], optbuf[20] = "";
  1305. struct sockaddr_storage addr;
  1306. int port, ttl;
  1307. if (reply->transports[0].destination.ss_family) {
  1308. addr = reply->transports[0].destination;
  1309. port = reply->transports[0].port_min;
  1310. ttl = reply->transports[0].ttl;
  1311. } else {
  1312. addr = rtsp_st->sdp_ip;
  1313. port = rtsp_st->sdp_port;
  1314. ttl = rtsp_st->sdp_ttl;
  1315. }
  1316. if (ttl > 0)
  1317. snprintf(optbuf, sizeof(optbuf), "?ttl=%d", ttl);
  1318. getnameinfo((struct sockaddr*) &addr, sizeof(addr),
  1319. namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST);
  1320. ff_url_join(url, sizeof(url), "rtp", NULL, namebuf,
  1321. port, "%s", optbuf);
  1322. if (ffurl_open(&rtsp_st->rtp_handle, url, AVIO_FLAG_READ_WRITE,
  1323. &s->interrupt_callback, NULL) < 0) {
  1324. err = AVERROR_INVALIDDATA;
  1325. goto fail;
  1326. }
  1327. break;
  1328. }
  1329. }
  1330. if ((err = ff_rtsp_open_transport_ctx(s, rtsp_st)))
  1331. goto fail;
  1332. }
  1333. if (rt->nb_rtsp_streams && reply->timeout > 0)
  1334. rt->timeout = reply->timeout;
  1335. if (rt->server_type == RTSP_SERVER_REAL)
  1336. rt->need_subscription = 1;
  1337. return 0;
  1338. fail:
  1339. ff_rtsp_undo_setup(s);
  1340. return err;
  1341. }
  1342. void ff_rtsp_close_connections(AVFormatContext *s)
  1343. {
  1344. RTSPState *rt = s->priv_data;
  1345. if (rt->rtsp_hd_out != rt->rtsp_hd) ffurl_close(rt->rtsp_hd_out);
  1346. ffurl_close(rt->rtsp_hd);
  1347. rt->rtsp_hd = rt->rtsp_hd_out = NULL;
  1348. }
  1349. int ff_rtsp_connect(AVFormatContext *s)
  1350. {
  1351. RTSPState *rt = s->priv_data;
  1352. char host[1024], path[1024], tcpname[1024], cmd[2048], auth[128];
  1353. int port, err, tcp_fd;
  1354. RTSPMessageHeader reply1 = {0}, *reply = &reply1;
  1355. int lower_transport_mask = 0;
  1356. char real_challenge[64] = "";
  1357. struct sockaddr_storage peer;
  1358. socklen_t peer_len = sizeof(peer);
  1359. if (rt->rtp_port_max < rt->rtp_port_min) {
  1360. av_log(s, AV_LOG_ERROR, "Invalid UDP port range, max port %d less "
  1361. "than min port %d\n", rt->rtp_port_max,
  1362. rt->rtp_port_min);
  1363. return AVERROR(EINVAL);
  1364. }
  1365. if (!ff_network_init())
  1366. return AVERROR(EIO);
  1367. if (s->max_delay < 0) /* Not set by the caller */
  1368. s->max_delay = s->iformat ? DEFAULT_REORDERING_DELAY : 0;
  1369. rt->control_transport = RTSP_MODE_PLAIN;
  1370. if (rt->lower_transport_mask & (1 << RTSP_LOWER_TRANSPORT_HTTP)) {
  1371. rt->lower_transport_mask = 1 << RTSP_LOWER_TRANSPORT_TCP;
  1372. rt->control_transport = RTSP_MODE_TUNNEL;
  1373. }
  1374. /* Only pass through valid flags from here */
  1375. rt->lower_transport_mask &= (1 << RTSP_LOWER_TRANSPORT_NB) - 1;
  1376. redirect:
  1377. lower_transport_mask = rt->lower_transport_mask;
  1378. /* extract hostname and port */
  1379. av_url_split(NULL, 0, auth, sizeof(auth),
  1380. host, sizeof(host), &port, path, sizeof(path), s->filename);
  1381. if (*auth) {
  1382. av_strlcpy(rt->auth, auth, sizeof(rt->auth));
  1383. }
  1384. if (port < 0)
  1385. port = RTSP_DEFAULT_PORT;
  1386. if (!lower_transport_mask)
  1387. lower_transport_mask = (1 << RTSP_LOWER_TRANSPORT_NB) - 1;
  1388. if (s->oformat) {
  1389. /* Only UDP or TCP - UDP multicast isn't supported. */
  1390. lower_transport_mask &= (1 << RTSP_LOWER_TRANSPORT_UDP) |
  1391. (1 << RTSP_LOWER_TRANSPORT_TCP);
  1392. if (!lower_transport_mask || rt->control_transport == RTSP_MODE_TUNNEL) {
  1393. av_log(s, AV_LOG_ERROR, "Unsupported lower transport method, "
  1394. "only UDP and TCP are supported for output.\n");
  1395. err = AVERROR(EINVAL);
  1396. goto fail;
  1397. }
  1398. }
  1399. /* Construct the URI used in request; this is similar to s->filename,
  1400. * but with authentication credentials removed and RTSP specific options
  1401. * stripped out. */
  1402. ff_url_join(rt->control_uri, sizeof(rt->control_uri), "rtsp", NULL,
  1403. host, port, "%s", path);
  1404. if (rt->control_transport == RTSP_MODE_TUNNEL) {
  1405. /* set up initial handshake for tunneling */
  1406. char httpname[1024];
  1407. char sessioncookie[17];
  1408. char headers[1024];
  1409. ff_url_join(httpname, sizeof(httpname), "http", auth, host, port, "%s", path);
  1410. snprintf(sessioncookie, sizeof(sessioncookie), "%08x%08x",
  1411. av_get_random_seed(), av_get_random_seed());
  1412. /* GET requests */
  1413. if (ffurl_alloc(&rt->rtsp_hd, httpname, AVIO_FLAG_READ,
  1414. &s->interrupt_callback) < 0) {
  1415. err = AVERROR(EIO);
  1416. goto fail;
  1417. }
  1418. /* generate GET headers */
  1419. snprintf(headers, sizeof(headers),
  1420. "x-sessioncookie: %s\r\n"
  1421. "Accept: application/x-rtsp-tunnelled\r\n"
  1422. "Pragma: no-cache\r\n"
  1423. "Cache-Control: no-cache\r\n",
  1424. sessioncookie);
  1425. av_opt_set(rt->rtsp_hd->priv_data, "headers", headers, 0);
  1426. /* complete the connection */
  1427. if (ffurl_connect(rt->rtsp_hd, NULL)) {
  1428. err = AVERROR(EIO);
  1429. goto fail;
  1430. }
  1431. /* POST requests */
  1432. if (ffurl_alloc(&rt->rtsp_hd_out, httpname, AVIO_FLAG_WRITE,
  1433. &s->interrupt_callback) < 0 ) {
  1434. err = AVERROR(EIO);
  1435. goto fail;
  1436. }
  1437. /* generate POST headers */
  1438. snprintf(headers, sizeof(headers),
  1439. "x-sessioncookie: %s\r\n"
  1440. "Content-Type: application/x-rtsp-tunnelled\r\n"
  1441. "Pragma: no-cache\r\n"
  1442. "Cache-Control: no-cache\r\n"
  1443. "Content-Length: 32767\r\n"
  1444. "Expires: Sun, 9 Jan 1972 00:00:00 GMT\r\n",
  1445. sessioncookie);
  1446. av_opt_set(rt->rtsp_hd_out->priv_data, "headers", headers, 0);
  1447. av_opt_set(rt->rtsp_hd_out->priv_data, "chunked_post", "0", 0);
  1448. /* Initialize the authentication state for the POST session. The HTTP
  1449. * protocol implementation doesn't properly handle multi-pass
  1450. * authentication for POST requests, since it would require one of
  1451. * the following:
  1452. * - implementing Expect: 100-continue, which many HTTP servers
  1453. * don't support anyway, even less the RTSP servers that do HTTP
  1454. * tunneling
  1455. * - sending the whole POST data until getting a 401 reply specifying
  1456. * what authentication method to use, then resending all that data
  1457. * - waiting for potential 401 replies directly after sending the
  1458. * POST header (waiting for some unspecified time)
  1459. * Therefore, we copy the full auth state, which works for both basic
  1460. * and digest. (For digest, we would have to synchronize the nonce
  1461. * count variable between the two sessions, if we'd do more requests
  1462. * with the original session, though.)
  1463. */
  1464. ff_http_init_auth_state(rt->rtsp_hd_out, rt->rtsp_hd);
  1465. /* complete the connection */
  1466. if (ffurl_connect(rt->rtsp_hd_out, NULL)) {
  1467. err = AVERROR(EIO);
  1468. goto fail;
  1469. }
  1470. } else {
  1471. /* open the tcp connection */
  1472. ff_url_join(tcpname, sizeof(tcpname), "tcp", NULL, host, port, NULL);
  1473. if (ffurl_open(&rt->rtsp_hd, tcpname, AVIO_FLAG_READ_WRITE,
  1474. &s->interrupt_callback, NULL) < 0) {
  1475. err = AVERROR(EIO);
  1476. goto fail;
  1477. }
  1478. rt->rtsp_hd_out = rt->rtsp_hd;
  1479. }
  1480. rt->seq = 0;
  1481. tcp_fd = ffurl_get_file_handle(rt->rtsp_hd);
  1482. if (!getpeername(tcp_fd, (struct sockaddr*) &peer, &peer_len)) {
  1483. getnameinfo((struct sockaddr*) &peer, peer_len, host, sizeof(host),
  1484. NULL, 0, NI_NUMERICHOST);
  1485. }
  1486. /* request options supported by the server; this also detects server
  1487. * type */
  1488. for (rt->server_type = RTSP_SERVER_RTP;;) {
  1489. cmd[0] = 0;
  1490. if (rt->server_type == RTSP_SERVER_REAL)
  1491. av_strlcat(cmd,
  1492. /*
  1493. * The following entries are required for proper
  1494. * streaming from a Realmedia server. They are
  1495. * interdependent in some way although we currently
  1496. * don't quite understand how. Values were copied
  1497. * from mplayer SVN r23589.
  1498. * ClientChallenge is a 16-byte ID in hex
  1499. * CompanyID is a 16-byte ID in base64
  1500. */
  1501. "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7\r\n"
  1502. "PlayerStarttime: [28/03/2003:22:50:23 00:00]\r\n"
  1503. "CompanyID: KnKV4M4I/B2FjJ1TToLycw==\r\n"
  1504. "GUID: 00000000-0000-0000-0000-000000000000\r\n",
  1505. sizeof(cmd));
  1506. ff_rtsp_send_cmd(s, "OPTIONS", rt->control_uri, cmd, reply, NULL);
  1507. if (reply->status_code != RTSP_STATUS_OK) {
  1508. err = AVERROR_INVALIDDATA;
  1509. goto fail;
  1510. }
  1511. /* detect server type if not standard-compliant RTP */
  1512. if (rt->server_type != RTSP_SERVER_REAL && reply->real_challenge[0]) {
  1513. rt->server_type = RTSP_SERVER_REAL;
  1514. continue;
  1515. } else if (!av_strncasecmp(reply->server, "WMServer/", 9)) {
  1516. rt->server_type = RTSP_SERVER_WMS;
  1517. } else if (rt->server_type == RTSP_SERVER_REAL)
  1518. strcpy(real_challenge, reply->real_challenge);
  1519. break;
  1520. }
  1521. if (s->iformat && CONFIG_RTSP_DEMUXER)
  1522. err = ff_rtsp_setup_input_streams(s, reply);
  1523. else if (CONFIG_RTSP_MUXER)
  1524. err = ff_rtsp_setup_output_streams(s, host);
  1525. if (err)
  1526. goto fail;
  1527. do {
  1528. int lower_transport = ff_log2_tab[lower_transport_mask &
  1529. ~(lower_transport_mask - 1)];
  1530. err = ff_rtsp_make_setup_request(s, host, port, lower_transport,
  1531. rt->server_type == RTSP_SERVER_REAL ?
  1532. real_challenge : NULL);
  1533. if (err < 0)
  1534. goto fail;
  1535. lower_transport_mask &= ~(1 << lower_transport);
  1536. if (lower_transport_mask == 0 && err == 1) {
  1537. err = AVERROR(EPROTONOSUPPORT);
  1538. goto fail;
  1539. }
  1540. } while (err);
  1541. rt->lower_transport_mask = lower_transport_mask;
  1542. av_strlcpy(rt->real_challenge, real_challenge, sizeof(rt->real_challenge));
  1543. rt->state = RTSP_STATE_IDLE;
  1544. rt->seek_timestamp = 0; /* default is to start stream at position zero */
  1545. return 0;
  1546. fail:
  1547. ff_rtsp_close_streams(s);
  1548. ff_rtsp_close_connections(s);
  1549. if (reply->status_code >=300 && reply->status_code < 400 && s->iformat) {
  1550. av_strlcpy(s->filename, reply->location, sizeof(s->filename));
  1551. av_log(s, AV_LOG_INFO, "Status %d: Redirecting to %s\n",
  1552. reply->status_code,
  1553. s->filename);
  1554. goto redirect;
  1555. }
  1556. ff_network_close();
  1557. return err;
  1558. }
  1559. #endif /* CONFIG_RTSP_DEMUXER || CONFIG_RTSP_MUXER */
  1560. #if CONFIG_RTPDEC
  1561. static int udp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  1562. uint8_t *buf, int buf_size, int64_t wait_end)
  1563. {
  1564. RTSPState *rt = s->priv_data;
  1565. RTSPStream *rtsp_st;
  1566. int n, i, ret, tcp_fd, timeout_cnt = 0;
  1567. int max_p = 0;
  1568. struct pollfd *p = rt->p;
  1569. int *fds = NULL, fdsnum, fdsidx;
  1570. for (;;) {
  1571. if (ff_check_interrupt(&s->interrupt_callback))
  1572. return AVERROR_EXIT;
  1573. if (wait_end && wait_end - av_gettime() < 0)
  1574. return AVERROR(EAGAIN);
  1575. max_p = 0;
  1576. if (rt->rtsp_hd) {
  1577. tcp_fd = ffurl_get_file_handle(rt->rtsp_hd);
  1578. p[max_p].fd = tcp_fd;
  1579. p[max_p++].events = POLLIN;
  1580. } else {
  1581. tcp_fd = -1;
  1582. }
  1583. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1584. rtsp_st = rt->rtsp_streams[i];
  1585. if (rtsp_st->rtp_handle) {
  1586. if (ret = ffurl_get_multi_file_handle(rtsp_st->rtp_handle,
  1587. &fds, &fdsnum)) {
  1588. av_log(s, AV_LOG_ERROR, "Unable to recover rtp ports\n");
  1589. return ret;
  1590. }
  1591. if (fdsnum != 2) {
  1592. av_log(s, AV_LOG_ERROR,
  1593. "Number of fds %d not supported\n", fdsnum);
  1594. return AVERROR_INVALIDDATA;
  1595. }
  1596. for (fdsidx = 0; fdsidx < fdsnum; fdsidx++) {
  1597. p[max_p].fd = fds[fdsidx];
  1598. p[max_p++].events = POLLIN;
  1599. }
  1600. av_free(fds);
  1601. }
  1602. }
  1603. n = poll(p, max_p, POLL_TIMEOUT_MS);
  1604. if (n > 0) {
  1605. int j = 1 - (tcp_fd == -1);
  1606. timeout_cnt = 0;
  1607. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1608. rtsp_st = rt->rtsp_streams[i];
  1609. if (rtsp_st->rtp_handle) {
  1610. if (p[j].revents & POLLIN || p[j+1].revents & POLLIN) {
  1611. ret = ffurl_read(rtsp_st->rtp_handle, buf, buf_size);
  1612. if (ret > 0) {
  1613. *prtsp_st = rtsp_st;
  1614. return ret;
  1615. }
  1616. }
  1617. j+=2;
  1618. }
  1619. }
  1620. #if CONFIG_RTSP_DEMUXER
  1621. if (tcp_fd != -1 && p[0].revents & POLLIN) {
  1622. if (rt->rtsp_flags & RTSP_FLAG_LISTEN) {
  1623. if (rt->state == RTSP_STATE_STREAMING) {
  1624. if (!ff_rtsp_parse_streaming_commands(s))
  1625. return AVERROR_EOF;
  1626. else
  1627. av_log(s, AV_LOG_WARNING,
  1628. "Unable to answer to TEARDOWN\n");
  1629. } else
  1630. return 0;
  1631. } else {
  1632. RTSPMessageHeader reply;
  1633. ret = ff_rtsp_read_reply(s, &reply, NULL, 0, NULL);
  1634. if (ret < 0)
  1635. return ret;
  1636. /* XXX: parse message */
  1637. if (rt->state != RTSP_STATE_STREAMING)
  1638. return 0;
  1639. }
  1640. }
  1641. #endif
  1642. } else if (n == 0 && ++timeout_cnt >= MAX_TIMEOUTS) {
  1643. return AVERROR(ETIMEDOUT);
  1644. } else if (n < 0 && errno != EINTR)
  1645. return AVERROR(errno);
  1646. }
  1647. }
  1648. static int pick_stream(AVFormatContext *s, RTSPStream **rtsp_st,
  1649. const uint8_t *buf, int len)
  1650. {
  1651. RTSPState *rt = s->priv_data;
  1652. int i;
  1653. if (len < 0)
  1654. return len;
  1655. if (rt->nb_rtsp_streams == 1) {
  1656. *rtsp_st = rt->rtsp_streams[0];
  1657. return len;
  1658. }
  1659. if (len >= 8 && rt->transport == RTSP_TRANSPORT_RTP) {
  1660. if (RTP_PT_IS_RTCP(rt->recvbuf[1])) {
  1661. int no_ssrc = 0;
  1662. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1663. RTPDemuxContext *rtpctx = rt->rtsp_streams[i]->transport_priv;
  1664. if (!rtpctx)
  1665. continue;
  1666. if (rtpctx->ssrc == AV_RB32(&buf[4])) {
  1667. *rtsp_st = rt->rtsp_streams[i];
  1668. return len;
  1669. }
  1670. if (!rtpctx->ssrc)
  1671. no_ssrc = 1;
  1672. }
  1673. if (no_ssrc) {
  1674. av_log(s, AV_LOG_WARNING,
  1675. "Unable to pick stream for packet - SSRC not known for "
  1676. "all streams\n");
  1677. return AVERROR(EAGAIN);
  1678. }
  1679. } else {
  1680. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1681. if ((buf[1] & 0x7f) == rt->rtsp_streams[i]->sdp_payload_type) {
  1682. *rtsp_st = rt->rtsp_streams[i];
  1683. return len;
  1684. }
  1685. }
  1686. }
  1687. }
  1688. av_log(s, AV_LOG_WARNING, "Unable to pick stream for packet\n");
  1689. return AVERROR(EAGAIN);
  1690. }
  1691. int ff_rtsp_fetch_packet(AVFormatContext *s, AVPacket *pkt)
  1692. {
  1693. RTSPState *rt = s->priv_data;
  1694. int ret, len;
  1695. RTSPStream *rtsp_st, *first_queue_st = NULL;
  1696. int64_t wait_end = 0;
  1697. if (rt->nb_byes == rt->nb_rtsp_streams)
  1698. return AVERROR_EOF;
  1699. /* get next frames from the same RTP packet */
  1700. if (rt->cur_transport_priv) {
  1701. if (rt->transport == RTSP_TRANSPORT_RDT) {
  1702. ret = ff_rdt_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
  1703. } else if (rt->transport == RTSP_TRANSPORT_RTP) {
  1704. ret = ff_rtp_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
  1705. } else if (rt->ts && CONFIG_RTPDEC) {
  1706. ret = ff_mpegts_parse_packet(rt->ts, pkt, rt->recvbuf + rt->recvbuf_pos, rt->recvbuf_len - rt->recvbuf_pos);
  1707. if (ret >= 0) {
  1708. rt->recvbuf_pos += ret;
  1709. ret = rt->recvbuf_pos < rt->recvbuf_len;
  1710. }
  1711. } else
  1712. ret = -1;
  1713. if (ret == 0) {
  1714. rt->cur_transport_priv = NULL;
  1715. return 0;
  1716. } else if (ret == 1) {
  1717. return 0;
  1718. } else
  1719. rt->cur_transport_priv = NULL;
  1720. }
  1721. redo:
  1722. if (rt->transport == RTSP_TRANSPORT_RTP) {
  1723. int i;
  1724. int64_t first_queue_time = 0;
  1725. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1726. RTPDemuxContext *rtpctx = rt->rtsp_streams[i]->transport_priv;
  1727. int64_t queue_time;
  1728. if (!rtpctx)
  1729. continue;
  1730. queue_time = ff_rtp_queued_packet_time(rtpctx);
  1731. if (queue_time && (queue_time - first_queue_time < 0 ||
  1732. !first_queue_time)) {
  1733. first_queue_time = queue_time;
  1734. first_queue_st = rt->rtsp_streams[i];
  1735. }
  1736. }
  1737. if (first_queue_time) {
  1738. wait_end = first_queue_time + s->max_delay;
  1739. } else {
  1740. wait_end = 0;
  1741. first_queue_st = NULL;
  1742. }
  1743. }
  1744. /* read next RTP packet */
  1745. if (!rt->recvbuf) {
  1746. rt->recvbuf = av_malloc(RECVBUF_SIZE);
  1747. if (!rt->recvbuf)
  1748. return AVERROR(ENOMEM);
  1749. }
  1750. switch(rt->lower_transport) {
  1751. default:
  1752. #if CONFIG_RTSP_DEMUXER
  1753. case RTSP_LOWER_TRANSPORT_TCP:
  1754. len = ff_rtsp_tcp_read_packet(s, &rtsp_st, rt->recvbuf, RECVBUF_SIZE);
  1755. break;
  1756. #endif
  1757. case RTSP_LOWER_TRANSPORT_UDP:
  1758. case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
  1759. len = udp_read_packet(s, &rtsp_st, rt->recvbuf, RECVBUF_SIZE, wait_end);
  1760. if (len > 0 && rtsp_st->transport_priv && rt->transport == RTSP_TRANSPORT_RTP)
  1761. ff_rtp_check_and_send_back_rr(rtsp_st->transport_priv, rtsp_st->rtp_handle, NULL, len);
  1762. break;
  1763. case RTSP_LOWER_TRANSPORT_CUSTOM:
  1764. if (first_queue_st && rt->transport == RTSP_TRANSPORT_RTP &&
  1765. wait_end && wait_end < av_gettime())
  1766. len = AVERROR(EAGAIN);
  1767. else
  1768. len = ffio_read_partial(s->pb, rt->recvbuf, RECVBUF_SIZE);
  1769. len = pick_stream(s, &rtsp_st, rt->recvbuf, len);
  1770. if (len > 0 && rtsp_st->transport_priv && rt->transport == RTSP_TRANSPORT_RTP)
  1771. ff_rtp_check_and_send_back_rr(rtsp_st->transport_priv, NULL, s->pb, len);
  1772. break;
  1773. }
  1774. if (len == AVERROR(EAGAIN) && first_queue_st &&
  1775. rt->transport == RTSP_TRANSPORT_RTP) {
  1776. rtsp_st = first_queue_st;
  1777. ret = ff_rtp_parse_packet(rtsp_st->transport_priv, pkt, NULL, 0);
  1778. goto end;
  1779. }
  1780. if (len < 0)
  1781. return len;
  1782. if (len == 0)
  1783. return AVERROR_EOF;
  1784. if (rt->transport == RTSP_TRANSPORT_RDT) {
  1785. ret = ff_rdt_parse_packet(rtsp_st->transport_priv, pkt, &rt->recvbuf, len);
  1786. } else if (rt->transport == RTSP_TRANSPORT_RTP) {
  1787. ret = ff_rtp_parse_packet(rtsp_st->transport_priv, pkt, &rt->recvbuf, len);
  1788. if (rtsp_st->feedback) {
  1789. AVIOContext *pb = NULL;
  1790. if (rt->lower_transport == RTSP_LOWER_TRANSPORT_CUSTOM)
  1791. pb = s->pb;
  1792. ff_rtp_send_rtcp_feedback(rtsp_st->transport_priv, rtsp_st->rtp_handle, pb);
  1793. }
  1794. if (ret < 0) {
  1795. /* Either bad packet, or a RTCP packet. Check if the
  1796. * first_rtcp_ntp_time field was initialized. */
  1797. RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
  1798. if (rtpctx->first_rtcp_ntp_time != AV_NOPTS_VALUE) {
  1799. /* first_rtcp_ntp_time has been initialized for this stream,
  1800. * copy the same value to all other uninitialized streams,
  1801. * in order to map their timestamp origin to the same ntp time
  1802. * as this one. */
  1803. int i;
  1804. AVStream *st = NULL;
  1805. if (rtsp_st->stream_index >= 0)
  1806. st = s->streams[rtsp_st->stream_index];
  1807. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1808. RTPDemuxContext *rtpctx2 = rt->rtsp_streams[i]->transport_priv;
  1809. AVStream *st2 = NULL;
  1810. if (rt->rtsp_streams[i]->stream_index >= 0)
  1811. st2 = s->streams[rt->rtsp_streams[i]->stream_index];
  1812. if (rtpctx2 && st && st2 &&
  1813. rtpctx2->first_rtcp_ntp_time == AV_NOPTS_VALUE) {
  1814. rtpctx2->first_rtcp_ntp_time = rtpctx->first_rtcp_ntp_time;
  1815. rtpctx2->rtcp_ts_offset = av_rescale_q(
  1816. rtpctx->rtcp_ts_offset, st->time_base,
  1817. st2->time_base);
  1818. }
  1819. }
  1820. }
  1821. if (ret == -RTCP_BYE) {
  1822. rt->nb_byes++;
  1823. av_log(s, AV_LOG_DEBUG, "Received BYE for stream %d (%d/%d)\n",
  1824. rtsp_st->stream_index, rt->nb_byes, rt->nb_rtsp_streams);
  1825. if (rt->nb_byes == rt->nb_rtsp_streams)
  1826. return AVERROR_EOF;
  1827. }
  1828. }
  1829. } else if (rt->ts && CONFIG_RTPDEC) {
  1830. ret = ff_mpegts_parse_packet(rt->ts, pkt, rt->recvbuf, len);
  1831. if (ret >= 0) {
  1832. if (ret < len) {
  1833. rt->recvbuf_len = len;
  1834. rt->recvbuf_pos = ret;
  1835. rt->cur_transport_priv = rt->ts;
  1836. return 1;
  1837. } else {
  1838. ret = 0;
  1839. }
  1840. }
  1841. } else {
  1842. return AVERROR_INVALIDDATA;
  1843. }
  1844. end:
  1845. if (ret < 0)
  1846. goto redo;
  1847. if (ret == 1)
  1848. /* more packets may follow, so we save the RTP context */
  1849. rt->cur_transport_priv = rtsp_st->transport_priv;
  1850. return ret;
  1851. }
  1852. #endif /* CONFIG_RTPDEC */
  1853. #if CONFIG_SDP_DEMUXER
  1854. static int sdp_probe(AVProbeData *p1)
  1855. {
  1856. const char *p = p1->buf, *p_end = p1->buf + p1->buf_size;
  1857. /* we look for a line beginning "c=IN IP" */
  1858. while (p < p_end && *p != '\0') {
  1859. if (p + sizeof("c=IN IP") - 1 < p_end &&
  1860. av_strstart(p, "c=IN IP", NULL))
  1861. return AVPROBE_SCORE_MAX / 2;
  1862. while (p < p_end - 1 && *p != '\n') p++;
  1863. if (++p >= p_end)
  1864. break;
  1865. if (*p == '\r')
  1866. p++;
  1867. }
  1868. return 0;
  1869. }
  1870. static int sdp_read_header(AVFormatContext *s)
  1871. {
  1872. RTSPState *rt = s->priv_data;
  1873. RTSPStream *rtsp_st;
  1874. int size, i, err;
  1875. char *content;
  1876. char url[1024];
  1877. if (!ff_network_init())
  1878. return AVERROR(EIO);
  1879. if (s->max_delay < 0) /* Not set by the caller */
  1880. s->max_delay = DEFAULT_REORDERING_DELAY;
  1881. if (rt->rtsp_flags & RTSP_FLAG_CUSTOM_IO)
  1882. rt->lower_transport = RTSP_LOWER_TRANSPORT_CUSTOM;
  1883. /* read the whole sdp file */
  1884. /* XXX: better loading */
  1885. content = av_malloc(SDP_MAX_SIZE);
  1886. size = avio_read(s->pb, content, SDP_MAX_SIZE - 1);
  1887. if (size <= 0) {
  1888. av_free(content);
  1889. return AVERROR_INVALIDDATA;
  1890. }
  1891. content[size] ='\0';
  1892. err = ff_sdp_parse(s, content);
  1893. av_free(content);
  1894. if (err) goto fail;
  1895. /* open each RTP stream */
  1896. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1897. char namebuf[50];
  1898. rtsp_st = rt->rtsp_streams[i];
  1899. if (!(rt->rtsp_flags & RTSP_FLAG_CUSTOM_IO)) {
  1900. getnameinfo((struct sockaddr*) &rtsp_st->sdp_ip, sizeof(rtsp_st->sdp_ip),
  1901. namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST);
  1902. ff_url_join(url, sizeof(url), "rtp", NULL,
  1903. namebuf, rtsp_st->sdp_port,
  1904. "?localport=%d&ttl=%d&connect=%d", rtsp_st->sdp_port,
  1905. rtsp_st->sdp_ttl,
  1906. rt->rtsp_flags & RTSP_FLAG_FILTER_SRC ? 1 : 0);
  1907. if (ffurl_open(&rtsp_st->rtp_handle, url, AVIO_FLAG_READ_WRITE,
  1908. &s->interrupt_callback, NULL) < 0) {
  1909. err = AVERROR_INVALIDDATA;
  1910. goto fail;
  1911. }
  1912. }
  1913. if ((err = ff_rtsp_open_transport_ctx(s, rtsp_st)))
  1914. goto fail;
  1915. }
  1916. return 0;
  1917. fail:
  1918. ff_rtsp_close_streams(s);
  1919. ff_network_close();
  1920. return err;
  1921. }
  1922. static int sdp_read_close(AVFormatContext *s)
  1923. {
  1924. ff_rtsp_close_streams(s);
  1925. ff_network_close();
  1926. return 0;
  1927. }
  1928. static const AVClass sdp_demuxer_class = {
  1929. .class_name = "SDP demuxer",
  1930. .item_name = av_default_item_name,
  1931. .option = sdp_options,
  1932. .version = LIBAVUTIL_VERSION_INT,
  1933. };
  1934. AVInputFormat ff_sdp_demuxer = {
  1935. .name = "sdp",
  1936. .long_name = NULL_IF_CONFIG_SMALL("SDP"),
  1937. .priv_data_size = sizeof(RTSPState),
  1938. .read_probe = sdp_probe,
  1939. .read_header = sdp_read_header,
  1940. .read_packet = ff_rtsp_fetch_packet,
  1941. .read_close = sdp_read_close,
  1942. .priv_class = &sdp_demuxer_class,
  1943. };
  1944. #endif /* CONFIG_SDP_DEMUXER */
  1945. #if CONFIG_RTP_DEMUXER
  1946. static int rtp_probe(AVProbeData *p)
  1947. {
  1948. if (av_strstart(p->filename, "rtp:", NULL))
  1949. return AVPROBE_SCORE_MAX;
  1950. return 0;
  1951. }
  1952. static int rtp_read_header(AVFormatContext *s)
  1953. {
  1954. uint8_t recvbuf[1500];
  1955. char host[500], sdp[500];
  1956. int ret, port;
  1957. URLContext* in = NULL;
  1958. int payload_type;
  1959. AVCodecContext codec = { 0 };
  1960. struct sockaddr_storage addr;
  1961. AVIOContext pb;
  1962. socklen_t addrlen = sizeof(addr);
  1963. RTSPState *rt = s->priv_data;
  1964. if (!ff_network_init())
  1965. return AVERROR(EIO);
  1966. ret = ffurl_open(&in, s->filename, AVIO_FLAG_READ,
  1967. &s->interrupt_callback, NULL);
  1968. if (ret)
  1969. goto fail;
  1970. while (1) {
  1971. ret = ffurl_read(in, recvbuf, sizeof(recvbuf));
  1972. if (ret == AVERROR(EAGAIN))
  1973. continue;
  1974. if (ret < 0)
  1975. goto fail;
  1976. if (ret < 12) {
  1977. av_log(s, AV_LOG_WARNING, "Received too short packet\n");
  1978. continue;
  1979. }
  1980. if ((recvbuf[0] & 0xc0) != 0x80) {
  1981. av_log(s, AV_LOG_WARNING, "Unsupported RTP version packet "
  1982. "received\n");
  1983. continue;
  1984. }
  1985. if (RTP_PT_IS_RTCP(recvbuf[1]))
  1986. continue;
  1987. payload_type = recvbuf[1] & 0x7f;
  1988. break;
  1989. }
  1990. getsockname(ffurl_get_file_handle(in), (struct sockaddr*) &addr, &addrlen);
  1991. ffurl_close(in);
  1992. in = NULL;
  1993. if (ff_rtp_get_codec_info(&codec, payload_type)) {
  1994. av_log(s, AV_LOG_ERROR, "Unable to receive RTP payload type %d "
  1995. "without an SDP file describing it\n",
  1996. payload_type);
  1997. goto fail;
  1998. }
  1999. if (codec.codec_type != AVMEDIA_TYPE_DATA) {
  2000. av_log(s, AV_LOG_WARNING, "Guessing on RTP content - if not received "
  2001. "properly you need an SDP file "
  2002. "describing it\n");
  2003. }
  2004. av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port,
  2005. NULL, 0, s->filename);
  2006. snprintf(sdp, sizeof(sdp),
  2007. "v=0\r\nc=IN IP%d %s\r\nm=%s %d RTP/AVP %d\r\n",
  2008. addr.ss_family == AF_INET ? 4 : 6, host,
  2009. codec.codec_type == AVMEDIA_TYPE_DATA ? "application" :
  2010. codec.codec_type == AVMEDIA_TYPE_VIDEO ? "video" : "audio",
  2011. port, payload_type);
  2012. av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sdp);
  2013. ffio_init_context(&pb, sdp, strlen(sdp), 0, NULL, NULL, NULL, NULL);
  2014. s->pb = &pb;
  2015. /* sdp_read_header initializes this again */
  2016. ff_network_close();
  2017. rt->media_type_mask = (1 << (AVMEDIA_TYPE_DATA+1)) - 1;
  2018. ret = sdp_read_header(s);
  2019. s->pb = NULL;
  2020. return ret;
  2021. fail:
  2022. if (in)
  2023. ffurl_close(in);
  2024. ff_network_close();
  2025. return ret;
  2026. }
  2027. static const AVClass rtp_demuxer_class = {
  2028. .class_name = "RTP demuxer",
  2029. .item_name = av_default_item_name,
  2030. .option = rtp_options,
  2031. .version = LIBAVUTIL_VERSION_INT,
  2032. };
  2033. AVInputFormat ff_rtp_demuxer = {
  2034. .name = "rtp",
  2035. .long_name = NULL_IF_CONFIG_SMALL("RTP input"),
  2036. .priv_data_size = sizeof(RTSPState),
  2037. .read_probe = rtp_probe,
  2038. .read_header = rtp_read_header,
  2039. .read_packet = ff_rtsp_fetch_packet,
  2040. .read_close = sdp_read_close,
  2041. .flags = AVFMT_NOFILE,
  2042. .priv_class = &rtp_demuxer_class,
  2043. };
  2044. #endif /* CONFIG_RTP_DEMUXER */