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.

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