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.

2234 lines
81KB

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