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.

2212 lines
80KB

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