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.

2246 lines
82KB

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