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.

2244 lines
82KB

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