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.

1821 lines
63KB

  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/base64.h"
  22. #include "libavutil/avstring.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "libavutil/random_seed.h"
  25. #include "avformat.h"
  26. #include <sys/time.h>
  27. #if HAVE_SYS_SELECT_H
  28. #include <sys/select.h>
  29. #endif
  30. #include <strings.h>
  31. #include "internal.h"
  32. #include "network.h"
  33. #include "os_support.h"
  34. #include "http.h"
  35. #include "rtsp.h"
  36. #include "rtpdec.h"
  37. #include "rdt.h"
  38. #include "rtpdec_formats.h"
  39. #include "rtpenc_chain.h"
  40. //#define DEBUG
  41. //#define DEBUG_RTP_TCP
  42. /* Timeout values for socket select, in ms,
  43. * and read_packet(), in seconds */
  44. #define SELECT_TIMEOUT_MS 100
  45. #define READ_PACKET_TIMEOUT_S 10
  46. #define MAX_TIMEOUTS READ_PACKET_TIMEOUT_S * 1000 / SELECT_TIMEOUT_MS
  47. #define SDP_MAX_SIZE 16384
  48. #define RECVBUF_SIZE 10 * RTP_MAX_PACKET_LENGTH
  49. static void get_word_until_chars(char *buf, int buf_size,
  50. const char *sep, const char **pp)
  51. {
  52. const char *p;
  53. char *q;
  54. p = *pp;
  55. p += strspn(p, SPACE_CHARS);
  56. q = buf;
  57. while (!strchr(sep, *p) && *p != '\0') {
  58. if ((q - buf) < buf_size - 1)
  59. *q++ = *p;
  60. p++;
  61. }
  62. if (buf_size > 0)
  63. *q = '\0';
  64. *pp = p;
  65. }
  66. static void get_word_sep(char *buf, int buf_size, const char *sep,
  67. const char **pp)
  68. {
  69. if (**pp == '/') (*pp)++;
  70. get_word_until_chars(buf, buf_size, sep, pp);
  71. }
  72. static void get_word(char *buf, int buf_size, const char **pp)
  73. {
  74. get_word_until_chars(buf, buf_size, SPACE_CHARS, pp);
  75. }
  76. /** Parse a string p in the form of Range:npt=xx-xx, and determine the start
  77. * and end time.
  78. * Used for seeking in the rtp stream.
  79. */
  80. static void rtsp_parse_range_npt(const char *p, int64_t *start, int64_t *end)
  81. {
  82. char buf[256];
  83. p += strspn(p, SPACE_CHARS);
  84. if (!av_stristart(p, "npt=", &p))
  85. return;
  86. *start = AV_NOPTS_VALUE;
  87. *end = AV_NOPTS_VALUE;
  88. get_word_sep(buf, sizeof(buf), "-", &p);
  89. *start = parse_date(buf, 1);
  90. if (*p == '-') {
  91. p++;
  92. get_word_sep(buf, sizeof(buf), "-", &p);
  93. *end = parse_date(buf, 1);
  94. }
  95. // av_log(NULL, AV_LOG_DEBUG, "Range Start: %lld\n", *start);
  96. // av_log(NULL, AV_LOG_DEBUG, "Range End: %lld\n", *end);
  97. }
  98. static int get_sockaddr(const char *buf, struct sockaddr_storage *sock)
  99. {
  100. struct addrinfo hints, *ai = NULL;
  101. memset(&hints, 0, sizeof(hints));
  102. hints.ai_flags = AI_NUMERICHOST;
  103. if (getaddrinfo(buf, NULL, &hints, &ai))
  104. return -1;
  105. memcpy(sock, ai->ai_addr, FFMIN(sizeof(*sock), ai->ai_addrlen));
  106. freeaddrinfo(ai);
  107. return 0;
  108. }
  109. #if CONFIG_RTPDEC
  110. static void init_rtp_handler(RTPDynamicProtocolHandler *handler,
  111. RTSPStream *rtsp_st, AVCodecContext *codec)
  112. {
  113. if (!handler)
  114. return;
  115. codec->codec_id = handler->codec_id;
  116. rtsp_st->dynamic_handler = handler;
  117. if (handler->open)
  118. rtsp_st->dynamic_protocol_context = handler->open();
  119. }
  120. /* parse the rtpmap description: <codec_name>/<clock_rate>[/<other params>] */
  121. static int sdp_parse_rtpmap(AVFormatContext *s,
  122. AVCodecContext *codec, RTSPStream *rtsp_st,
  123. int payload_type, const char *p)
  124. {
  125. char buf[256];
  126. int i;
  127. AVCodec *c;
  128. const char *c_name;
  129. /* Loop into AVRtpDynamicPayloadTypes[] and AVRtpPayloadTypes[] and
  130. * see if we can handle this kind of payload.
  131. * The space should normally not be there but some Real streams or
  132. * particular servers ("RealServer Version 6.1.3.970", see issue 1658)
  133. * have a trailing space. */
  134. get_word_sep(buf, sizeof(buf), "/ ", &p);
  135. if (payload_type >= RTP_PT_PRIVATE) {
  136. RTPDynamicProtocolHandler *handler =
  137. ff_rtp_handler_find_by_name(buf, codec->codec_type);
  138. init_rtp_handler(handler, rtsp_st, codec);
  139. /* If no dynamic handler was found, check with the list of standard
  140. * allocated types, if such a stream for some reason happens to
  141. * use a private payload type. This isn't handled in rtpdec.c, since
  142. * the format name from the rtpmap line never is passed into rtpdec. */
  143. if (!rtsp_st->dynamic_handler)
  144. codec->codec_id = ff_rtp_codec_id(buf, codec->codec_type);
  145. } else {
  146. /* We are in a standard case
  147. * (from http://www.iana.org/assignments/rtp-parameters). */
  148. /* search into AVRtpPayloadTypes[] */
  149. codec->codec_id = ff_rtp_codec_id(buf, codec->codec_type);
  150. }
  151. c = avcodec_find_decoder(codec->codec_id);
  152. if (c && c->name)
  153. c_name = c->name;
  154. else
  155. c_name = "(null)";
  156. get_word_sep(buf, sizeof(buf), "/", &p);
  157. i = atoi(buf);
  158. switch (codec->codec_type) {
  159. case AVMEDIA_TYPE_AUDIO:
  160. av_log(s, AV_LOG_DEBUG, "audio codec set to: %s\n", c_name);
  161. codec->sample_rate = RTSP_DEFAULT_AUDIO_SAMPLERATE;
  162. codec->channels = RTSP_DEFAULT_NB_AUDIO_CHANNELS;
  163. if (i > 0) {
  164. codec->sample_rate = i;
  165. get_word_sep(buf, sizeof(buf), "/", &p);
  166. i = atoi(buf);
  167. if (i > 0)
  168. codec->channels = i;
  169. // TODO: there is a bug here; if it is a mono stream, and
  170. // less than 22000Hz, faad upconverts to stereo and twice
  171. // the frequency. No problem, but the sample rate is being
  172. // set here by the sdp line. Patch on its way. (rdm)
  173. }
  174. av_log(s, AV_LOG_DEBUG, "audio samplerate set to: %i\n",
  175. codec->sample_rate);
  176. av_log(s, AV_LOG_DEBUG, "audio channels set to: %i\n",
  177. codec->channels);
  178. break;
  179. case AVMEDIA_TYPE_VIDEO:
  180. av_log(s, AV_LOG_DEBUG, "video codec set to: %s\n", c_name);
  181. break;
  182. default:
  183. break;
  184. }
  185. return 0;
  186. }
  187. /* parse the attribute line from the fmtp a line of an sdp response. This
  188. * is broken out as a function because it is used in rtp_h264.c, which is
  189. * forthcoming. */
  190. int ff_rtsp_next_attr_and_value(const char **p, char *attr, int attr_size,
  191. char *value, int value_size)
  192. {
  193. *p += strspn(*p, SPACE_CHARS);
  194. if (**p) {
  195. get_word_sep(attr, attr_size, "=", p);
  196. if (**p == '=')
  197. (*p)++;
  198. get_word_sep(value, value_size, ";", p);
  199. if (**p == ';')
  200. (*p)++;
  201. return 1;
  202. }
  203. return 0;
  204. }
  205. typedef struct SDPParseState {
  206. /* SDP only */
  207. struct sockaddr_storage default_ip;
  208. int default_ttl;
  209. int skip_media; ///< set if an unknown m= line occurs
  210. } SDPParseState;
  211. static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1,
  212. int letter, const char *buf)
  213. {
  214. RTSPState *rt = s->priv_data;
  215. char buf1[64], st_type[64];
  216. const char *p;
  217. enum AVMediaType codec_type;
  218. int payload_type, i;
  219. AVStream *st;
  220. RTSPStream *rtsp_st;
  221. struct sockaddr_storage sdp_ip;
  222. int ttl;
  223. dprintf(s, "sdp: %c='%s'\n", letter, buf);
  224. p = buf;
  225. if (s1->skip_media && letter != 'm')
  226. return;
  227. switch (letter) {
  228. case 'c':
  229. get_word(buf1, sizeof(buf1), &p);
  230. if (strcmp(buf1, "IN") != 0)
  231. return;
  232. get_word(buf1, sizeof(buf1), &p);
  233. if (strcmp(buf1, "IP4") && strcmp(buf1, "IP6"))
  234. return;
  235. get_word_sep(buf1, sizeof(buf1), "/", &p);
  236. if (get_sockaddr(buf1, &sdp_ip))
  237. return;
  238. ttl = 16;
  239. if (*p == '/') {
  240. p++;
  241. get_word_sep(buf1, sizeof(buf1), "/", &p);
  242. ttl = atoi(buf1);
  243. }
  244. if (s->nb_streams == 0) {
  245. s1->default_ip = sdp_ip;
  246. s1->default_ttl = ttl;
  247. } else {
  248. st = s->streams[s->nb_streams - 1];
  249. rtsp_st = st->priv_data;
  250. rtsp_st->sdp_ip = sdp_ip;
  251. rtsp_st->sdp_ttl = ttl;
  252. }
  253. break;
  254. case 's':
  255. av_metadata_set2(&s->metadata, "title", p, 0);
  256. break;
  257. case 'i':
  258. if (s->nb_streams == 0) {
  259. av_metadata_set2(&s->metadata, "comment", p, 0);
  260. break;
  261. }
  262. break;
  263. case 'm':
  264. /* new stream */
  265. s1->skip_media = 0;
  266. get_word(st_type, sizeof(st_type), &p);
  267. if (!strcmp(st_type, "audio")) {
  268. codec_type = AVMEDIA_TYPE_AUDIO;
  269. } else if (!strcmp(st_type, "video")) {
  270. codec_type = AVMEDIA_TYPE_VIDEO;
  271. } else if (!strcmp(st_type, "application")) {
  272. codec_type = AVMEDIA_TYPE_DATA;
  273. } else {
  274. s1->skip_media = 1;
  275. return;
  276. }
  277. rtsp_st = av_mallocz(sizeof(RTSPStream));
  278. if (!rtsp_st)
  279. return;
  280. rtsp_st->stream_index = -1;
  281. dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
  282. rtsp_st->sdp_ip = s1->default_ip;
  283. rtsp_st->sdp_ttl = s1->default_ttl;
  284. get_word(buf1, sizeof(buf1), &p); /* port */
  285. rtsp_st->sdp_port = atoi(buf1);
  286. get_word(buf1, sizeof(buf1), &p); /* protocol (ignored) */
  287. /* XXX: handle list of formats */
  288. get_word(buf1, sizeof(buf1), &p); /* format list */
  289. rtsp_st->sdp_payload_type = atoi(buf1);
  290. if (!strcmp(ff_rtp_enc_name(rtsp_st->sdp_payload_type), "MP2T")) {
  291. /* no corresponding stream */
  292. } else {
  293. st = av_new_stream(s, 0);
  294. if (!st)
  295. return;
  296. st->priv_data = rtsp_st;
  297. rtsp_st->stream_index = st->index;
  298. st->codec->codec_type = codec_type;
  299. if (rtsp_st->sdp_payload_type < RTP_PT_PRIVATE) {
  300. /* if standard payload type, we can find the codec right now */
  301. ff_rtp_get_codec_info(st->codec, rtsp_st->sdp_payload_type);
  302. }
  303. }
  304. /* put a default control url */
  305. av_strlcpy(rtsp_st->control_url, rt->control_uri,
  306. sizeof(rtsp_st->control_url));
  307. break;
  308. case 'a':
  309. if (av_strstart(p, "control:", &p)) {
  310. if (s->nb_streams == 0) {
  311. if (!strncmp(p, "rtsp://", 7))
  312. av_strlcpy(rt->control_uri, p,
  313. sizeof(rt->control_uri));
  314. } else {
  315. char proto[32];
  316. /* get the control url */
  317. st = s->streams[s->nb_streams - 1];
  318. rtsp_st = st->priv_data;
  319. /* XXX: may need to add full url resolution */
  320. av_url_split(proto, sizeof(proto), NULL, 0, NULL, 0,
  321. NULL, NULL, 0, p);
  322. if (proto[0] == '\0') {
  323. /* relative control URL */
  324. if (rtsp_st->control_url[strlen(rtsp_st->control_url)-1]!='/')
  325. av_strlcat(rtsp_st->control_url, "/",
  326. sizeof(rtsp_st->control_url));
  327. av_strlcat(rtsp_st->control_url, p,
  328. sizeof(rtsp_st->control_url));
  329. } else
  330. av_strlcpy(rtsp_st->control_url, p,
  331. sizeof(rtsp_st->control_url));
  332. }
  333. } else if (av_strstart(p, "rtpmap:", &p) && s->nb_streams > 0) {
  334. /* NOTE: rtpmap is only supported AFTER the 'm=' tag */
  335. get_word(buf1, sizeof(buf1), &p);
  336. payload_type = atoi(buf1);
  337. st = s->streams[s->nb_streams - 1];
  338. rtsp_st = st->priv_data;
  339. sdp_parse_rtpmap(s, st->codec, rtsp_st, payload_type, p);
  340. } else if (av_strstart(p, "fmtp:", &p) ||
  341. av_strstart(p, "framesize:", &p)) {
  342. /* NOTE: fmtp is only supported AFTER the 'a=rtpmap:xxx' tag */
  343. // let dynamic protocol handlers have a stab at the line.
  344. get_word(buf1, sizeof(buf1), &p);
  345. payload_type = atoi(buf1);
  346. for (i = 0; i < s->nb_streams; i++) {
  347. st = s->streams[i];
  348. rtsp_st = st->priv_data;
  349. if (rtsp_st->sdp_payload_type == payload_type &&
  350. rtsp_st->dynamic_handler &&
  351. rtsp_st->dynamic_handler->parse_sdp_a_line)
  352. rtsp_st->dynamic_handler->parse_sdp_a_line(s, i,
  353. rtsp_st->dynamic_protocol_context, buf);
  354. }
  355. } else if (av_strstart(p, "range:", &p)) {
  356. int64_t start, end;
  357. // this is so that seeking on a streamed file can work.
  358. rtsp_parse_range_npt(p, &start, &end);
  359. s->start_time = start;
  360. /* AV_NOPTS_VALUE means live broadcast (and can't seek) */
  361. s->duration = (end == AV_NOPTS_VALUE) ?
  362. AV_NOPTS_VALUE : end - start;
  363. } else if (av_strstart(p, "IsRealDataType:integer;",&p)) {
  364. if (atoi(p) == 1)
  365. rt->transport = RTSP_TRANSPORT_RDT;
  366. } else {
  367. if (rt->server_type == RTSP_SERVER_WMS)
  368. ff_wms_parse_sdp_a_line(s, p);
  369. if (s->nb_streams > 0) {
  370. if (rt->server_type == RTSP_SERVER_REAL)
  371. ff_real_parse_sdp_a_line(s, s->nb_streams - 1, p);
  372. rtsp_st = s->streams[s->nb_streams - 1]->priv_data;
  373. if (rtsp_st->dynamic_handler &&
  374. rtsp_st->dynamic_handler->parse_sdp_a_line)
  375. rtsp_st->dynamic_handler->parse_sdp_a_line(s,
  376. s->nb_streams - 1,
  377. rtsp_st->dynamic_protocol_context, buf);
  378. }
  379. }
  380. break;
  381. }
  382. }
  383. int ff_sdp_parse(AVFormatContext *s, const char *content)
  384. {
  385. const char *p;
  386. int letter;
  387. /* Some SDP lines, particularly for Realmedia or ASF RTSP streams,
  388. * contain long SDP lines containing complete ASF Headers (several
  389. * kB) or arrays of MDPR (RM stream descriptor) headers plus
  390. * "rulebooks" describing their properties. Therefore, the SDP line
  391. * buffer is large.
  392. *
  393. * The Vorbis FMTP line can be up to 16KB - see xiph_parse_sdp_line
  394. * in rtpdec_xiph.c. */
  395. char buf[16384], *q;
  396. SDPParseState sdp_parse_state, *s1 = &sdp_parse_state;
  397. memset(s1, 0, sizeof(SDPParseState));
  398. p = content;
  399. for (;;) {
  400. p += strspn(p, SPACE_CHARS);
  401. letter = *p;
  402. if (letter == '\0')
  403. break;
  404. p++;
  405. if (*p != '=')
  406. goto next_line;
  407. p++;
  408. /* get the content */
  409. q = buf;
  410. while (*p != '\n' && *p != '\r' && *p != '\0') {
  411. if ((q - buf) < sizeof(buf) - 1)
  412. *q++ = *p;
  413. p++;
  414. }
  415. *q = '\0';
  416. sdp_parse_line(s, s1, letter, buf);
  417. next_line:
  418. while (*p != '\n' && *p != '\0')
  419. p++;
  420. if (*p == '\n')
  421. p++;
  422. }
  423. return 0;
  424. }
  425. #endif /* CONFIG_RTPDEC */
  426. /* close and free RTSP streams */
  427. void ff_rtsp_close_streams(AVFormatContext *s)
  428. {
  429. RTSPState *rt = s->priv_data;
  430. int i;
  431. RTSPStream *rtsp_st;
  432. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  433. rtsp_st = rt->rtsp_streams[i];
  434. if (rtsp_st) {
  435. if (rtsp_st->transport_priv) {
  436. if (s->oformat) {
  437. AVFormatContext *rtpctx = rtsp_st->transport_priv;
  438. av_write_trailer(rtpctx);
  439. if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
  440. uint8_t *ptr;
  441. url_close_dyn_buf(rtpctx->pb, &ptr);
  442. av_free(ptr);
  443. } else {
  444. url_fclose(rtpctx->pb);
  445. }
  446. av_metadata_free(&rtpctx->streams[0]->metadata);
  447. av_metadata_free(&rtpctx->metadata);
  448. av_free(rtpctx->streams[0]);
  449. av_free(rtpctx);
  450. } else if (rt->transport == RTSP_TRANSPORT_RDT && CONFIG_RTPDEC)
  451. ff_rdt_parse_close(rtsp_st->transport_priv);
  452. else if (CONFIG_RTPDEC)
  453. rtp_parse_close(rtsp_st->transport_priv);
  454. }
  455. if (rtsp_st->rtp_handle)
  456. url_close(rtsp_st->rtp_handle);
  457. if (rtsp_st->dynamic_handler && rtsp_st->dynamic_protocol_context)
  458. rtsp_st->dynamic_handler->close(
  459. rtsp_st->dynamic_protocol_context);
  460. }
  461. }
  462. av_free(rt->rtsp_streams);
  463. if (rt->asf_ctx) {
  464. av_close_input_stream (rt->asf_ctx);
  465. rt->asf_ctx = NULL;
  466. }
  467. av_free(rt->recvbuf);
  468. }
  469. static int rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st)
  470. {
  471. RTSPState *rt = s->priv_data;
  472. AVStream *st = NULL;
  473. /* open the RTP context */
  474. if (rtsp_st->stream_index >= 0)
  475. st = s->streams[rtsp_st->stream_index];
  476. if (!st)
  477. s->ctx_flags |= AVFMTCTX_NOHEADER;
  478. if (s->oformat && CONFIG_RTSP_MUXER) {
  479. rtsp_st->transport_priv = ff_rtp_chain_mux_open(s, st,
  480. rtsp_st->rtp_handle,
  481. RTSP_TCP_MAX_PACKET_SIZE);
  482. /* Ownership of rtp_handle is passed to the rtp mux context */
  483. rtsp_st->rtp_handle = NULL;
  484. } else if (rt->transport == RTSP_TRANSPORT_RDT && CONFIG_RTPDEC)
  485. rtsp_st->transport_priv = ff_rdt_parse_open(s, st->index,
  486. rtsp_st->dynamic_protocol_context,
  487. rtsp_st->dynamic_handler);
  488. else if (CONFIG_RTPDEC)
  489. rtsp_st->transport_priv = rtp_parse_open(s, st, rtsp_st->rtp_handle,
  490. rtsp_st->sdp_payload_type,
  491. (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP || !s->max_delay)
  492. ? 0 : RTP_REORDER_QUEUE_DEFAULT_SIZE);
  493. if (!rtsp_st->transport_priv) {
  494. return AVERROR(ENOMEM);
  495. } else if (rt->transport != RTSP_TRANSPORT_RDT && CONFIG_RTPDEC) {
  496. if (rtsp_st->dynamic_handler) {
  497. rtp_parse_set_dynamic_protocol(rtsp_st->transport_priv,
  498. rtsp_st->dynamic_protocol_context,
  499. rtsp_st->dynamic_handler);
  500. }
  501. }
  502. return 0;
  503. }
  504. #if CONFIG_RTSP_DEMUXER || CONFIG_RTSP_MUXER
  505. static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
  506. {
  507. const char *p;
  508. int v;
  509. p = *pp;
  510. p += strspn(p, SPACE_CHARS);
  511. v = strtol(p, (char **)&p, 10);
  512. if (*p == '-') {
  513. p++;
  514. *min_ptr = v;
  515. v = strtol(p, (char **)&p, 10);
  516. *max_ptr = v;
  517. } else {
  518. *min_ptr = v;
  519. *max_ptr = v;
  520. }
  521. *pp = p;
  522. }
  523. /* XXX: only one transport specification is parsed */
  524. static void rtsp_parse_transport(RTSPMessageHeader *reply, const char *p)
  525. {
  526. char transport_protocol[16];
  527. char profile[16];
  528. char lower_transport[16];
  529. char parameter[16];
  530. RTSPTransportField *th;
  531. char buf[256];
  532. reply->nb_transports = 0;
  533. for (;;) {
  534. p += strspn(p, SPACE_CHARS);
  535. if (*p == '\0')
  536. break;
  537. th = &reply->transports[reply->nb_transports];
  538. get_word_sep(transport_protocol, sizeof(transport_protocol),
  539. "/", &p);
  540. if (!strcasecmp (transport_protocol, "rtp")) {
  541. get_word_sep(profile, sizeof(profile), "/;,", &p);
  542. lower_transport[0] = '\0';
  543. /* rtp/avp/<protocol> */
  544. if (*p == '/') {
  545. get_word_sep(lower_transport, sizeof(lower_transport),
  546. ";,", &p);
  547. }
  548. th->transport = RTSP_TRANSPORT_RTP;
  549. } else if (!strcasecmp (transport_protocol, "x-pn-tng") ||
  550. !strcasecmp (transport_protocol, "x-real-rdt")) {
  551. /* x-pn-tng/<protocol> */
  552. get_word_sep(lower_transport, sizeof(lower_transport), "/;,", &p);
  553. profile[0] = '\0';
  554. th->transport = RTSP_TRANSPORT_RDT;
  555. }
  556. if (!strcasecmp(lower_transport, "TCP"))
  557. th->lower_transport = RTSP_LOWER_TRANSPORT_TCP;
  558. else
  559. th->lower_transport = RTSP_LOWER_TRANSPORT_UDP;
  560. if (*p == ';')
  561. p++;
  562. /* get each parameter */
  563. while (*p != '\0' && *p != ',') {
  564. get_word_sep(parameter, sizeof(parameter), "=;,", &p);
  565. if (!strcmp(parameter, "port")) {
  566. if (*p == '=') {
  567. p++;
  568. rtsp_parse_range(&th->port_min, &th->port_max, &p);
  569. }
  570. } else if (!strcmp(parameter, "client_port")) {
  571. if (*p == '=') {
  572. p++;
  573. rtsp_parse_range(&th->client_port_min,
  574. &th->client_port_max, &p);
  575. }
  576. } else if (!strcmp(parameter, "server_port")) {
  577. if (*p == '=') {
  578. p++;
  579. rtsp_parse_range(&th->server_port_min,
  580. &th->server_port_max, &p);
  581. }
  582. } else if (!strcmp(parameter, "interleaved")) {
  583. if (*p == '=') {
  584. p++;
  585. rtsp_parse_range(&th->interleaved_min,
  586. &th->interleaved_max, &p);
  587. }
  588. } else if (!strcmp(parameter, "multicast")) {
  589. if (th->lower_transport == RTSP_LOWER_TRANSPORT_UDP)
  590. th->lower_transport = RTSP_LOWER_TRANSPORT_UDP_MULTICAST;
  591. } else if (!strcmp(parameter, "ttl")) {
  592. if (*p == '=') {
  593. p++;
  594. th->ttl = strtol(p, (char **)&p, 10);
  595. }
  596. } else if (!strcmp(parameter, "destination")) {
  597. if (*p == '=') {
  598. p++;
  599. get_word_sep(buf, sizeof(buf), ";,", &p);
  600. get_sockaddr(buf, &th->destination);
  601. }
  602. } else if (!strcmp(parameter, "source")) {
  603. if (*p == '=') {
  604. p++;
  605. get_word_sep(buf, sizeof(buf), ";,", &p);
  606. av_strlcpy(th->source, buf, sizeof(th->source));
  607. }
  608. }
  609. while (*p != ';' && *p != '\0' && *p != ',')
  610. p++;
  611. if (*p == ';')
  612. p++;
  613. }
  614. if (*p == ',')
  615. p++;
  616. reply->nb_transports++;
  617. }
  618. }
  619. void ff_rtsp_parse_line(RTSPMessageHeader *reply, const char *buf,
  620. HTTPAuthState *auth_state)
  621. {
  622. const char *p;
  623. /* NOTE: we do case independent match for broken servers */
  624. p = buf;
  625. if (av_stristart(p, "Session:", &p)) {
  626. int t;
  627. get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
  628. if (av_stristart(p, ";timeout=", &p) &&
  629. (t = strtol(p, NULL, 10)) > 0) {
  630. reply->timeout = t;
  631. }
  632. } else if (av_stristart(p, "Content-Length:", &p)) {
  633. reply->content_length = strtol(p, NULL, 10);
  634. } else if (av_stristart(p, "Transport:", &p)) {
  635. rtsp_parse_transport(reply, p);
  636. } else if (av_stristart(p, "CSeq:", &p)) {
  637. reply->seq = strtol(p, NULL, 10);
  638. } else if (av_stristart(p, "Range:", &p)) {
  639. rtsp_parse_range_npt(p, &reply->range_start, &reply->range_end);
  640. } else if (av_stristart(p, "RealChallenge1:", &p)) {
  641. p += strspn(p, SPACE_CHARS);
  642. av_strlcpy(reply->real_challenge, p, sizeof(reply->real_challenge));
  643. } else if (av_stristart(p, "Server:", &p)) {
  644. p += strspn(p, SPACE_CHARS);
  645. av_strlcpy(reply->server, p, sizeof(reply->server));
  646. } else if (av_stristart(p, "Notice:", &p) ||
  647. av_stristart(p, "X-Notice:", &p)) {
  648. reply->notice = strtol(p, NULL, 10);
  649. } else if (av_stristart(p, "Location:", &p)) {
  650. p += strspn(p, SPACE_CHARS);
  651. av_strlcpy(reply->location, p , sizeof(reply->location));
  652. } else if (av_stristart(p, "WWW-Authenticate:", &p) && auth_state) {
  653. p += strspn(p, SPACE_CHARS);
  654. ff_http_auth_handle_header(auth_state, "WWW-Authenticate", p);
  655. } else if (av_stristart(p, "Authentication-Info:", &p) && auth_state) {
  656. p += strspn(p, SPACE_CHARS);
  657. ff_http_auth_handle_header(auth_state, "Authentication-Info", p);
  658. } else if (av_stristart(p, "Content-Base:", &p)) {
  659. p += strspn(p, SPACE_CHARS);
  660. av_strlcpy(reply->content_base, p , sizeof(reply->content_base));
  661. }
  662. }
  663. /* skip a RTP/TCP interleaved packet */
  664. void ff_rtsp_skip_packet(AVFormatContext *s)
  665. {
  666. RTSPState *rt = s->priv_data;
  667. int ret, len, len1;
  668. uint8_t buf[1024];
  669. ret = url_read_complete(rt->rtsp_hd, buf, 3);
  670. if (ret != 3)
  671. return;
  672. len = AV_RB16(buf + 1);
  673. dprintf(s, "skipping RTP packet len=%d\n", len);
  674. /* skip payload */
  675. while (len > 0) {
  676. len1 = len;
  677. if (len1 > sizeof(buf))
  678. len1 = sizeof(buf);
  679. ret = url_read_complete(rt->rtsp_hd, buf, len1);
  680. if (ret != len1)
  681. return;
  682. len -= len1;
  683. }
  684. }
  685. int ff_rtsp_read_reply(AVFormatContext *s, RTSPMessageHeader *reply,
  686. unsigned char **content_ptr,
  687. int return_on_interleaved_data)
  688. {
  689. RTSPState *rt = s->priv_data;
  690. char buf[4096], buf1[1024], *q;
  691. unsigned char ch;
  692. const char *p;
  693. int ret, content_length, line_count = 0;
  694. unsigned char *content = NULL;
  695. memset(reply, 0, sizeof(*reply));
  696. /* parse reply (XXX: use buffers) */
  697. rt->last_reply[0] = '\0';
  698. for (;;) {
  699. q = buf;
  700. for (;;) {
  701. ret = url_read_complete(rt->rtsp_hd, &ch, 1);
  702. #ifdef DEBUG_RTP_TCP
  703. dprintf(s, "ret=%d c=%02x [%c]\n", ret, ch, ch);
  704. #endif
  705. if (ret != 1)
  706. return AVERROR_EOF;
  707. if (ch == '\n')
  708. break;
  709. if (ch == '$') {
  710. /* XXX: only parse it if first char on line ? */
  711. if (return_on_interleaved_data) {
  712. return 1;
  713. } else
  714. ff_rtsp_skip_packet(s);
  715. } else if (ch != '\r') {
  716. if ((q - buf) < sizeof(buf) - 1)
  717. *q++ = ch;
  718. }
  719. }
  720. *q = '\0';
  721. dprintf(s, "line='%s'\n", buf);
  722. /* test if last line */
  723. if (buf[0] == '\0')
  724. break;
  725. p = buf;
  726. if (line_count == 0) {
  727. /* get reply code */
  728. get_word(buf1, sizeof(buf1), &p);
  729. get_word(buf1, sizeof(buf1), &p);
  730. reply->status_code = atoi(buf1);
  731. av_strlcpy(reply->reason, p, sizeof(reply->reason));
  732. } else {
  733. ff_rtsp_parse_line(reply, p, &rt->auth_state);
  734. av_strlcat(rt->last_reply, p, sizeof(rt->last_reply));
  735. av_strlcat(rt->last_reply, "\n", sizeof(rt->last_reply));
  736. }
  737. line_count++;
  738. }
  739. if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0')
  740. av_strlcpy(rt->session_id, reply->session_id, sizeof(rt->session_id));
  741. content_length = reply->content_length;
  742. if (content_length > 0) {
  743. /* leave some room for a trailing '\0' (useful for simple parsing) */
  744. content = av_malloc(content_length + 1);
  745. (void)url_read_complete(rt->rtsp_hd, content, content_length);
  746. content[content_length] = '\0';
  747. }
  748. if (content_ptr)
  749. *content_ptr = content;
  750. else
  751. av_free(content);
  752. if (rt->seq != reply->seq) {
  753. av_log(s, AV_LOG_WARNING, "CSeq %d expected, %d received.\n",
  754. rt->seq, reply->seq);
  755. }
  756. /* EOS */
  757. if (reply->notice == 2101 /* End-of-Stream Reached */ ||
  758. reply->notice == 2104 /* Start-of-Stream Reached */ ||
  759. reply->notice == 2306 /* Continuous Feed Terminated */) {
  760. rt->state = RTSP_STATE_IDLE;
  761. } else if (reply->notice >= 4400 && reply->notice < 5500) {
  762. return AVERROR(EIO); /* data or server error */
  763. } else if (reply->notice == 2401 /* Ticket Expired */ ||
  764. (reply->notice >= 5500 && reply->notice < 5600) /* end of term */ )
  765. return AVERROR(EPERM);
  766. return 0;
  767. }
  768. int ff_rtsp_send_cmd_with_content_async(AVFormatContext *s,
  769. const char *method, const char *url,
  770. const char *headers,
  771. const unsigned char *send_content,
  772. int send_content_length)
  773. {
  774. RTSPState *rt = s->priv_data;
  775. char buf[4096], *out_buf;
  776. char base64buf[AV_BASE64_SIZE(sizeof(buf))];
  777. /* Add in RTSP headers */
  778. out_buf = buf;
  779. rt->seq++;
  780. snprintf(buf, sizeof(buf), "%s %s RTSP/1.0\r\n", method, url);
  781. if (headers)
  782. av_strlcat(buf, headers, sizeof(buf));
  783. av_strlcatf(buf, sizeof(buf), "CSeq: %d\r\n", rt->seq);
  784. if (rt->session_id[0] != '\0' && (!headers ||
  785. !strstr(headers, "\nIf-Match:"))) {
  786. av_strlcatf(buf, sizeof(buf), "Session: %s\r\n", rt->session_id);
  787. }
  788. if (rt->auth[0]) {
  789. char *str = ff_http_auth_create_response(&rt->auth_state,
  790. rt->auth, url, method);
  791. if (str)
  792. av_strlcat(buf, str, sizeof(buf));
  793. av_free(str);
  794. }
  795. if (send_content_length > 0 && send_content)
  796. av_strlcatf(buf, sizeof(buf), "Content-Length: %d\r\n", send_content_length);
  797. av_strlcat(buf, "\r\n", sizeof(buf));
  798. /* base64 encode rtsp if tunneling */
  799. if (rt->control_transport == RTSP_MODE_TUNNEL) {
  800. av_base64_encode(base64buf, sizeof(base64buf), buf, strlen(buf));
  801. out_buf = base64buf;
  802. }
  803. dprintf(s, "Sending:\n%s--\n", buf);
  804. url_write(rt->rtsp_hd_out, out_buf, strlen(out_buf));
  805. if (send_content_length > 0 && send_content) {
  806. if (rt->control_transport == RTSP_MODE_TUNNEL) {
  807. av_log(s, AV_LOG_ERROR, "tunneling of RTSP requests "
  808. "with content data not supported\n");
  809. return AVERROR_PATCHWELCOME;
  810. }
  811. url_write(rt->rtsp_hd_out, send_content, send_content_length);
  812. }
  813. rt->last_cmd_time = av_gettime();
  814. return 0;
  815. }
  816. int ff_rtsp_send_cmd_async(AVFormatContext *s, const char *method,
  817. const char *url, const char *headers)
  818. {
  819. return ff_rtsp_send_cmd_with_content_async(s, method, url, headers, NULL, 0);
  820. }
  821. int ff_rtsp_send_cmd(AVFormatContext *s, const char *method, const char *url,
  822. const char *headers, RTSPMessageHeader *reply,
  823. unsigned char **content_ptr)
  824. {
  825. return ff_rtsp_send_cmd_with_content(s, method, url, headers, reply,
  826. content_ptr, NULL, 0);
  827. }
  828. int ff_rtsp_send_cmd_with_content(AVFormatContext *s,
  829. const char *method, const char *url,
  830. const char *header,
  831. RTSPMessageHeader *reply,
  832. unsigned char **content_ptr,
  833. const unsigned char *send_content,
  834. int send_content_length)
  835. {
  836. RTSPState *rt = s->priv_data;
  837. HTTPAuthType cur_auth_type;
  838. int ret;
  839. retry:
  840. cur_auth_type = rt->auth_state.auth_type;
  841. if ((ret = ff_rtsp_send_cmd_with_content_async(s, method, url, header,
  842. send_content,
  843. send_content_length)))
  844. return ret;
  845. if ((ret = ff_rtsp_read_reply(s, reply, content_ptr, 0) ) < 0)
  846. return ret;
  847. if (reply->status_code == 401 && cur_auth_type == HTTP_AUTH_NONE &&
  848. rt->auth_state.auth_type != HTTP_AUTH_NONE)
  849. goto retry;
  850. if (reply->status_code > 400){
  851. av_log(s, AV_LOG_ERROR, "method %s failed: %d%s\n",
  852. method,
  853. reply->status_code,
  854. reply->reason);
  855. av_log(s, AV_LOG_DEBUG, "%s\n", rt->last_reply);
  856. }
  857. return 0;
  858. }
  859. /**
  860. * @return 0 on success, <0 on error, 1 if protocol is unavailable.
  861. */
  862. static int make_setup_request(AVFormatContext *s, const char *host, int port,
  863. int lower_transport, const char *real_challenge)
  864. {
  865. RTSPState *rt = s->priv_data;
  866. int rtx, j, i, err, interleave = 0;
  867. RTSPStream *rtsp_st;
  868. RTSPMessageHeader reply1, *reply = &reply1;
  869. char cmd[2048];
  870. const char *trans_pref;
  871. if (rt->transport == RTSP_TRANSPORT_RDT)
  872. trans_pref = "x-pn-tng";
  873. else
  874. trans_pref = "RTP/AVP";
  875. /* default timeout: 1 minute */
  876. rt->timeout = 60;
  877. /* for each stream, make the setup request */
  878. /* XXX: we assume the same server is used for the control of each
  879. * RTSP stream */
  880. for (j = RTSP_RTP_PORT_MIN, i = 0; i < rt->nb_rtsp_streams; ++i) {
  881. char transport[2048];
  882. /**
  883. * WMS serves all UDP data over a single connection, the RTX, which
  884. * isn't necessarily the first in the SDP but has to be the first
  885. * to be set up, else the second/third SETUP will fail with a 461.
  886. */
  887. if (lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
  888. rt->server_type == RTSP_SERVER_WMS) {
  889. if (i == 0) {
  890. /* rtx first */
  891. for (rtx = 0; rtx < rt->nb_rtsp_streams; rtx++) {
  892. int len = strlen(rt->rtsp_streams[rtx]->control_url);
  893. if (len >= 4 &&
  894. !strcmp(rt->rtsp_streams[rtx]->control_url + len - 4,
  895. "/rtx"))
  896. break;
  897. }
  898. if (rtx == rt->nb_rtsp_streams)
  899. return -1; /* no RTX found */
  900. rtsp_st = rt->rtsp_streams[rtx];
  901. } else
  902. rtsp_st = rt->rtsp_streams[i > rtx ? i : i - 1];
  903. } else
  904. rtsp_st = rt->rtsp_streams[i];
  905. /* RTP/UDP */
  906. if (lower_transport == RTSP_LOWER_TRANSPORT_UDP) {
  907. char buf[256];
  908. if (rt->server_type == RTSP_SERVER_WMS && i > 1) {
  909. port = reply->transports[0].client_port_min;
  910. goto have_port;
  911. }
  912. /* first try in specified port range */
  913. if (RTSP_RTP_PORT_MIN != 0) {
  914. while (j <= RTSP_RTP_PORT_MAX) {
  915. ff_url_join(buf, sizeof(buf), "rtp", NULL, host, -1,
  916. "?localport=%d", j);
  917. /* we will use two ports per rtp stream (rtp and rtcp) */
  918. j += 2;
  919. if (url_open(&rtsp_st->rtp_handle, buf, URL_RDWR) == 0)
  920. goto rtp_opened;
  921. }
  922. }
  923. #if 0
  924. /* then try on any port */
  925. if (url_open(&rtsp_st->rtp_handle, "rtp://", URL_RDONLY) < 0) {
  926. err = AVERROR_INVALIDDATA;
  927. goto fail;
  928. }
  929. #endif
  930. rtp_opened:
  931. port = rtp_get_local_rtp_port(rtsp_st->rtp_handle);
  932. have_port:
  933. snprintf(transport, sizeof(transport) - 1,
  934. "%s/UDP;", trans_pref);
  935. if (rt->server_type != RTSP_SERVER_REAL)
  936. av_strlcat(transport, "unicast;", sizeof(transport));
  937. av_strlcatf(transport, sizeof(transport),
  938. "client_port=%d", port);
  939. if (rt->transport == RTSP_TRANSPORT_RTP &&
  940. !(rt->server_type == RTSP_SERVER_WMS && i > 0))
  941. av_strlcatf(transport, sizeof(transport), "-%d", port + 1);
  942. }
  943. /* RTP/TCP */
  944. else if (lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
  945. /** For WMS streams, the application streams are only used for
  946. * UDP. When trying to set it up for TCP streams, the server
  947. * will return an error. Therefore, we skip those streams. */
  948. if (rt->server_type == RTSP_SERVER_WMS &&
  949. s->streams[rtsp_st->stream_index]->codec->codec_type ==
  950. AVMEDIA_TYPE_DATA)
  951. continue;
  952. snprintf(transport, sizeof(transport) - 1,
  953. "%s/TCP;", trans_pref);
  954. if (rt->server_type == RTSP_SERVER_WMS)
  955. av_strlcat(transport, "unicast;", sizeof(transport));
  956. av_strlcatf(transport, sizeof(transport),
  957. "interleaved=%d-%d",
  958. interleave, interleave + 1);
  959. interleave += 2;
  960. }
  961. else if (lower_transport == RTSP_LOWER_TRANSPORT_UDP_MULTICAST) {
  962. snprintf(transport, sizeof(transport) - 1,
  963. "%s/UDP;multicast", trans_pref);
  964. }
  965. if (s->oformat) {
  966. av_strlcat(transport, ";mode=receive", sizeof(transport));
  967. } else if (rt->server_type == RTSP_SERVER_REAL ||
  968. rt->server_type == RTSP_SERVER_WMS)
  969. av_strlcat(transport, ";mode=play", sizeof(transport));
  970. snprintf(cmd, sizeof(cmd),
  971. "Transport: %s\r\n",
  972. transport);
  973. if (i == 0 && rt->server_type == RTSP_SERVER_REAL && CONFIG_RTPDEC) {
  974. char real_res[41], real_csum[9];
  975. ff_rdt_calc_response_and_checksum(real_res, real_csum,
  976. real_challenge);
  977. av_strlcatf(cmd, sizeof(cmd),
  978. "If-Match: %s\r\n"
  979. "RealChallenge2: %s, sd=%s\r\n",
  980. rt->session_id, real_res, real_csum);
  981. }
  982. ff_rtsp_send_cmd(s, "SETUP", rtsp_st->control_url, cmd, reply, NULL);
  983. if (reply->status_code == 461 /* Unsupported protocol */ && i == 0) {
  984. err = 1;
  985. goto fail;
  986. } else if (reply->status_code != RTSP_STATUS_OK ||
  987. reply->nb_transports != 1) {
  988. err = AVERROR_INVALIDDATA;
  989. goto fail;
  990. }
  991. /* XXX: same protocol for all streams is required */
  992. if (i > 0) {
  993. if (reply->transports[0].lower_transport != rt->lower_transport ||
  994. reply->transports[0].transport != rt->transport) {
  995. err = AVERROR_INVALIDDATA;
  996. goto fail;
  997. }
  998. } else {
  999. rt->lower_transport = reply->transports[0].lower_transport;
  1000. rt->transport = reply->transports[0].transport;
  1001. }
  1002. /* close RTP connection if not chosen */
  1003. if (reply->transports[0].lower_transport != RTSP_LOWER_TRANSPORT_UDP &&
  1004. (lower_transport == RTSP_LOWER_TRANSPORT_UDP)) {
  1005. url_close(rtsp_st->rtp_handle);
  1006. rtsp_st->rtp_handle = NULL;
  1007. }
  1008. switch(reply->transports[0].lower_transport) {
  1009. case RTSP_LOWER_TRANSPORT_TCP:
  1010. rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
  1011. rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
  1012. break;
  1013. case RTSP_LOWER_TRANSPORT_UDP: {
  1014. char url[1024];
  1015. /* Use source address if specified */
  1016. if (reply->transports[0].source[0]) {
  1017. ff_url_join(url, sizeof(url), "rtp", NULL,
  1018. reply->transports[0].source,
  1019. reply->transports[0].server_port_min, NULL);
  1020. } else {
  1021. ff_url_join(url, sizeof(url), "rtp", NULL, host,
  1022. reply->transports[0].server_port_min, NULL);
  1023. }
  1024. if (!(rt->server_type == RTSP_SERVER_WMS && i > 1) &&
  1025. rtp_set_remote_url(rtsp_st->rtp_handle, url) < 0) {
  1026. err = AVERROR_INVALIDDATA;
  1027. goto fail;
  1028. }
  1029. /* Try to initialize the connection state in a
  1030. * potential NAT router by sending dummy packets.
  1031. * RTP/RTCP dummy packets are used for RDT, too.
  1032. */
  1033. if (!(rt->server_type == RTSP_SERVER_WMS && i > 1) && s->iformat &&
  1034. CONFIG_RTPDEC)
  1035. rtp_send_punch_packets(rtsp_st->rtp_handle);
  1036. break;
  1037. }
  1038. case RTSP_LOWER_TRANSPORT_UDP_MULTICAST: {
  1039. char url[1024], namebuf[50];
  1040. struct sockaddr_storage addr;
  1041. int port, ttl;
  1042. if (reply->transports[0].destination.ss_family) {
  1043. addr = reply->transports[0].destination;
  1044. port = reply->transports[0].port_min;
  1045. ttl = reply->transports[0].ttl;
  1046. } else {
  1047. addr = rtsp_st->sdp_ip;
  1048. port = rtsp_st->sdp_port;
  1049. ttl = rtsp_st->sdp_ttl;
  1050. }
  1051. getnameinfo((struct sockaddr*) &addr, sizeof(addr),
  1052. namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST);
  1053. ff_url_join(url, sizeof(url), "rtp", NULL, namebuf,
  1054. port, "?ttl=%d", ttl);
  1055. if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
  1056. err = AVERROR_INVALIDDATA;
  1057. goto fail;
  1058. }
  1059. break;
  1060. }
  1061. }
  1062. if ((err = rtsp_open_transport_ctx(s, rtsp_st)))
  1063. goto fail;
  1064. }
  1065. if (reply->timeout > 0)
  1066. rt->timeout = reply->timeout;
  1067. if (rt->server_type == RTSP_SERVER_REAL)
  1068. rt->need_subscription = 1;
  1069. return 0;
  1070. fail:
  1071. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1072. if (rt->rtsp_streams[i]->rtp_handle) {
  1073. url_close(rt->rtsp_streams[i]->rtp_handle);
  1074. rt->rtsp_streams[i]->rtp_handle = NULL;
  1075. }
  1076. }
  1077. return err;
  1078. }
  1079. void ff_rtsp_close_connections(AVFormatContext *s)
  1080. {
  1081. RTSPState *rt = s->priv_data;
  1082. if (rt->rtsp_hd_out != rt->rtsp_hd) url_close(rt->rtsp_hd_out);
  1083. url_close(rt->rtsp_hd);
  1084. rt->rtsp_hd = rt->rtsp_hd_out = NULL;
  1085. }
  1086. int ff_rtsp_connect(AVFormatContext *s)
  1087. {
  1088. RTSPState *rt = s->priv_data;
  1089. char host[1024], path[1024], tcpname[1024], cmd[2048], auth[128];
  1090. char *option_list, *option, *filename;
  1091. int port, err, tcp_fd;
  1092. RTSPMessageHeader reply1 = {0}, *reply = &reply1;
  1093. int lower_transport_mask = 0;
  1094. char real_challenge[64];
  1095. struct sockaddr_storage peer;
  1096. socklen_t peer_len = sizeof(peer);
  1097. if (!ff_network_init())
  1098. return AVERROR(EIO);
  1099. redirect:
  1100. rt->control_transport = RTSP_MODE_PLAIN;
  1101. /* extract hostname and port */
  1102. av_url_split(NULL, 0, auth, sizeof(auth),
  1103. host, sizeof(host), &port, path, sizeof(path), s->filename);
  1104. if (*auth) {
  1105. av_strlcpy(rt->auth, auth, sizeof(rt->auth));
  1106. }
  1107. if (port < 0)
  1108. port = RTSP_DEFAULT_PORT;
  1109. /* search for options */
  1110. option_list = strrchr(path, '?');
  1111. if (option_list) {
  1112. /* Strip out the RTSP specific options, write out the rest of
  1113. * the options back into the same string. */
  1114. filename = option_list;
  1115. while (option_list) {
  1116. /* move the option pointer */
  1117. option = ++option_list;
  1118. option_list = strchr(option_list, '&');
  1119. if (option_list)
  1120. *option_list = 0;
  1121. /* handle the options */
  1122. if (!strcmp(option, "udp")) {
  1123. lower_transport_mask |= (1<< RTSP_LOWER_TRANSPORT_UDP);
  1124. } else if (!strcmp(option, "multicast")) {
  1125. lower_transport_mask |= (1<< RTSP_LOWER_TRANSPORT_UDP_MULTICAST);
  1126. } else if (!strcmp(option, "tcp")) {
  1127. lower_transport_mask |= (1<< RTSP_LOWER_TRANSPORT_TCP);
  1128. } else if(!strcmp(option, "http")) {
  1129. lower_transport_mask |= (1<< RTSP_LOWER_TRANSPORT_TCP);
  1130. rt->control_transport = RTSP_MODE_TUNNEL;
  1131. } else {
  1132. /* Write options back into the buffer, using memmove instead
  1133. * of strcpy since the strings may overlap. */
  1134. int len = strlen(option);
  1135. memmove(++filename, option, len);
  1136. filename += len;
  1137. if (option_list) *filename = '&';
  1138. }
  1139. }
  1140. *filename = 0;
  1141. }
  1142. if (!lower_transport_mask)
  1143. lower_transport_mask = (1 << RTSP_LOWER_TRANSPORT_NB) - 1;
  1144. if (s->oformat) {
  1145. /* Only UDP or TCP - UDP multicast isn't supported. */
  1146. lower_transport_mask &= (1 << RTSP_LOWER_TRANSPORT_UDP) |
  1147. (1 << RTSP_LOWER_TRANSPORT_TCP);
  1148. if (!lower_transport_mask || rt->control_transport == RTSP_MODE_TUNNEL) {
  1149. av_log(s, AV_LOG_ERROR, "Unsupported lower transport method, "
  1150. "only UDP and TCP are supported for output.\n");
  1151. err = AVERROR(EINVAL);
  1152. goto fail;
  1153. }
  1154. }
  1155. /* Construct the URI used in request; this is similar to s->filename,
  1156. * but with authentication credentials removed and RTSP specific options
  1157. * stripped out. */
  1158. ff_url_join(rt->control_uri, sizeof(rt->control_uri), "rtsp", NULL,
  1159. host, port, "%s", path);
  1160. if (rt->control_transport == RTSP_MODE_TUNNEL) {
  1161. /* set up initial handshake for tunneling */
  1162. char httpname[1024];
  1163. char sessioncookie[17];
  1164. char headers[1024];
  1165. ff_url_join(httpname, sizeof(httpname), "http", auth, host, port, "%s", path);
  1166. snprintf(sessioncookie, sizeof(sessioncookie), "%08x%08x",
  1167. av_get_random_seed(), av_get_random_seed());
  1168. /* GET requests */
  1169. if (url_alloc(&rt->rtsp_hd, httpname, URL_RDONLY) < 0) {
  1170. err = AVERROR(EIO);
  1171. goto fail;
  1172. }
  1173. /* generate GET headers */
  1174. snprintf(headers, sizeof(headers),
  1175. "x-sessioncookie: %s\r\n"
  1176. "Accept: application/x-rtsp-tunnelled\r\n"
  1177. "Pragma: no-cache\r\n"
  1178. "Cache-Control: no-cache\r\n",
  1179. sessioncookie);
  1180. ff_http_set_headers(rt->rtsp_hd, headers);
  1181. /* complete the connection */
  1182. if (url_connect(rt->rtsp_hd)) {
  1183. err = AVERROR(EIO);
  1184. goto fail;
  1185. }
  1186. /* POST requests */
  1187. if (url_alloc(&rt->rtsp_hd_out, httpname, URL_WRONLY) < 0 ) {
  1188. err = AVERROR(EIO);
  1189. goto fail;
  1190. }
  1191. /* generate POST headers */
  1192. snprintf(headers, sizeof(headers),
  1193. "x-sessioncookie: %s\r\n"
  1194. "Content-Type: application/x-rtsp-tunnelled\r\n"
  1195. "Pragma: no-cache\r\n"
  1196. "Cache-Control: no-cache\r\n"
  1197. "Content-Length: 32767\r\n"
  1198. "Expires: Sun, 9 Jan 1972 00:00:00 GMT\r\n",
  1199. sessioncookie);
  1200. ff_http_set_headers(rt->rtsp_hd_out, headers);
  1201. ff_http_set_chunked_transfer_encoding(rt->rtsp_hd_out, 0);
  1202. /* Initialize the authentication state for the POST session. The HTTP
  1203. * protocol implementation doesn't properly handle multi-pass
  1204. * authentication for POST requests, since it would require one of
  1205. * the following:
  1206. * - implementing Expect: 100-continue, which many HTTP servers
  1207. * don't support anyway, even less the RTSP servers that do HTTP
  1208. * tunneling
  1209. * - sending the whole POST data until getting a 401 reply specifying
  1210. * what authentication method to use, then resending all that data
  1211. * - waiting for potential 401 replies directly after sending the
  1212. * POST header (waiting for some unspecified time)
  1213. * Therefore, we copy the full auth state, which works for both basic
  1214. * and digest. (For digest, we would have to synchronize the nonce
  1215. * count variable between the two sessions, if we'd do more requests
  1216. * with the original session, though.)
  1217. */
  1218. ff_http_init_auth_state(rt->rtsp_hd_out, rt->rtsp_hd);
  1219. /* complete the connection */
  1220. if (url_connect(rt->rtsp_hd_out)) {
  1221. err = AVERROR(EIO);
  1222. goto fail;
  1223. }
  1224. } else {
  1225. /* open the tcp connection */
  1226. ff_url_join(tcpname, sizeof(tcpname), "tcp", NULL, host, port, NULL);
  1227. if (url_open(&rt->rtsp_hd, tcpname, URL_RDWR) < 0) {
  1228. err = AVERROR(EIO);
  1229. goto fail;
  1230. }
  1231. rt->rtsp_hd_out = rt->rtsp_hd;
  1232. }
  1233. rt->seq = 0;
  1234. tcp_fd = url_get_file_handle(rt->rtsp_hd);
  1235. if (!getpeername(tcp_fd, (struct sockaddr*) &peer, &peer_len)) {
  1236. getnameinfo((struct sockaddr*) &peer, peer_len, host, sizeof(host),
  1237. NULL, 0, NI_NUMERICHOST);
  1238. }
  1239. /* request options supported by the server; this also detects server
  1240. * type */
  1241. for (rt->server_type = RTSP_SERVER_RTP;;) {
  1242. cmd[0] = 0;
  1243. if (rt->server_type == RTSP_SERVER_REAL)
  1244. av_strlcat(cmd,
  1245. /**
  1246. * The following entries are required for proper
  1247. * streaming from a Realmedia server. They are
  1248. * interdependent in some way although we currently
  1249. * don't quite understand how. Values were copied
  1250. * from mplayer SVN r23589.
  1251. * @param CompanyID is a 16-byte ID in base64
  1252. * @param ClientChallenge is a 16-byte ID in hex
  1253. */
  1254. "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7\r\n"
  1255. "PlayerStarttime: [28/03/2003:22:50:23 00:00]\r\n"
  1256. "CompanyID: KnKV4M4I/B2FjJ1TToLycw==\r\n"
  1257. "GUID: 00000000-0000-0000-0000-000000000000\r\n",
  1258. sizeof(cmd));
  1259. ff_rtsp_send_cmd(s, "OPTIONS", rt->control_uri, cmd, reply, NULL);
  1260. if (reply->status_code != RTSP_STATUS_OK) {
  1261. err = AVERROR_INVALIDDATA;
  1262. goto fail;
  1263. }
  1264. /* detect server type if not standard-compliant RTP */
  1265. if (rt->server_type != RTSP_SERVER_REAL && reply->real_challenge[0]) {
  1266. rt->server_type = RTSP_SERVER_REAL;
  1267. continue;
  1268. } else if (!strncasecmp(reply->server, "WMServer/", 9)) {
  1269. rt->server_type = RTSP_SERVER_WMS;
  1270. } else if (rt->server_type == RTSP_SERVER_REAL)
  1271. strcpy(real_challenge, reply->real_challenge);
  1272. break;
  1273. }
  1274. if (s->iformat && CONFIG_RTSP_DEMUXER)
  1275. err = ff_rtsp_setup_input_streams(s, reply);
  1276. else if (CONFIG_RTSP_MUXER)
  1277. err = ff_rtsp_setup_output_streams(s, host);
  1278. if (err)
  1279. goto fail;
  1280. do {
  1281. int lower_transport = ff_log2_tab[lower_transport_mask &
  1282. ~(lower_transport_mask - 1)];
  1283. err = make_setup_request(s, host, port, lower_transport,
  1284. rt->server_type == RTSP_SERVER_REAL ?
  1285. real_challenge : NULL);
  1286. if (err < 0)
  1287. goto fail;
  1288. lower_transport_mask &= ~(1 << lower_transport);
  1289. if (lower_transport_mask == 0 && err == 1) {
  1290. err = FF_NETERROR(EPROTONOSUPPORT);
  1291. goto fail;
  1292. }
  1293. } while (err);
  1294. rt->state = RTSP_STATE_IDLE;
  1295. rt->seek_timestamp = 0; /* default is to start stream at position zero */
  1296. return 0;
  1297. fail:
  1298. ff_rtsp_close_streams(s);
  1299. ff_rtsp_close_connections(s);
  1300. if (reply->status_code >=300 && reply->status_code < 400 && s->iformat) {
  1301. av_strlcpy(s->filename, reply->location, sizeof(s->filename));
  1302. av_log(s, AV_LOG_INFO, "Status %d: Redirecting to %s\n",
  1303. reply->status_code,
  1304. s->filename);
  1305. goto redirect;
  1306. }
  1307. ff_network_close();
  1308. return err;
  1309. }
  1310. #endif /* CONFIG_RTSP_DEMUXER || CONFIG_RTSP_MUXER */
  1311. #if CONFIG_RTPDEC
  1312. static int udp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  1313. uint8_t *buf, int buf_size, int64_t wait_end)
  1314. {
  1315. RTSPState *rt = s->priv_data;
  1316. RTSPStream *rtsp_st;
  1317. fd_set rfds;
  1318. int fd, fd_rtcp, fd_max, n, i, ret, tcp_fd, timeout_cnt = 0;
  1319. struct timeval tv;
  1320. for (;;) {
  1321. if (url_interrupt_cb())
  1322. return AVERROR(EINTR);
  1323. if (wait_end && wait_end - av_gettime() < 0)
  1324. return AVERROR(EAGAIN);
  1325. FD_ZERO(&rfds);
  1326. if (rt->rtsp_hd) {
  1327. tcp_fd = fd_max = url_get_file_handle(rt->rtsp_hd);
  1328. FD_SET(tcp_fd, &rfds);
  1329. } else {
  1330. fd_max = 0;
  1331. tcp_fd = -1;
  1332. }
  1333. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1334. rtsp_st = rt->rtsp_streams[i];
  1335. if (rtsp_st->rtp_handle) {
  1336. fd = url_get_file_handle(rtsp_st->rtp_handle);
  1337. fd_rtcp = rtp_get_rtcp_file_handle(rtsp_st->rtp_handle);
  1338. if (FFMAX(fd, fd_rtcp) > fd_max)
  1339. fd_max = FFMAX(fd, fd_rtcp);
  1340. FD_SET(fd, &rfds);
  1341. FD_SET(fd_rtcp, &rfds);
  1342. }
  1343. }
  1344. tv.tv_sec = 0;
  1345. tv.tv_usec = SELECT_TIMEOUT_MS * 1000;
  1346. n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
  1347. if (n > 0) {
  1348. timeout_cnt = 0;
  1349. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1350. rtsp_st = rt->rtsp_streams[i];
  1351. if (rtsp_st->rtp_handle) {
  1352. fd = url_get_file_handle(rtsp_st->rtp_handle);
  1353. fd_rtcp = rtp_get_rtcp_file_handle(rtsp_st->rtp_handle);
  1354. if (FD_ISSET(fd_rtcp, &rfds) || FD_ISSET(fd, &rfds)) {
  1355. ret = url_read(rtsp_st->rtp_handle, buf, buf_size);
  1356. if (ret > 0) {
  1357. *prtsp_st = rtsp_st;
  1358. return ret;
  1359. }
  1360. }
  1361. }
  1362. }
  1363. #if CONFIG_RTSP_DEMUXER
  1364. if (tcp_fd != -1 && FD_ISSET(tcp_fd, &rfds)) {
  1365. RTSPMessageHeader reply;
  1366. ret = ff_rtsp_read_reply(s, &reply, NULL, 0);
  1367. if (ret < 0)
  1368. return ret;
  1369. /* XXX: parse message */
  1370. if (rt->state != RTSP_STATE_STREAMING)
  1371. return 0;
  1372. }
  1373. #endif
  1374. } else if (n == 0 && ++timeout_cnt >= MAX_TIMEOUTS) {
  1375. return FF_NETERROR(ETIMEDOUT);
  1376. } else if (n < 0 && errno != EINTR)
  1377. return AVERROR(errno);
  1378. }
  1379. }
  1380. int ff_rtsp_fetch_packet(AVFormatContext *s, AVPacket *pkt)
  1381. {
  1382. RTSPState *rt = s->priv_data;
  1383. int ret, len;
  1384. RTSPStream *rtsp_st, *first_queue_st = NULL;
  1385. int64_t wait_end = 0;
  1386. if (rt->nb_byes == rt->nb_rtsp_streams)
  1387. return AVERROR_EOF;
  1388. /* get next frames from the same RTP packet */
  1389. if (rt->cur_transport_priv) {
  1390. if (rt->transport == RTSP_TRANSPORT_RDT) {
  1391. ret = ff_rdt_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
  1392. } else
  1393. ret = rtp_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
  1394. if (ret == 0) {
  1395. rt->cur_transport_priv = NULL;
  1396. return 0;
  1397. } else if (ret == 1) {
  1398. return 0;
  1399. } else
  1400. rt->cur_transport_priv = NULL;
  1401. }
  1402. if (rt->transport == RTSP_TRANSPORT_RTP) {
  1403. int i;
  1404. int64_t first_queue_time = 0;
  1405. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1406. RTPDemuxContext *rtpctx = rt->rtsp_streams[i]->transport_priv;
  1407. int64_t queue_time = ff_rtp_queued_packet_time(rtpctx);
  1408. if (queue_time && (queue_time - first_queue_time < 0 ||
  1409. !first_queue_time)) {
  1410. first_queue_time = queue_time;
  1411. first_queue_st = rt->rtsp_streams[i];
  1412. }
  1413. }
  1414. if (first_queue_time)
  1415. wait_end = first_queue_time + s->max_delay;
  1416. }
  1417. /* read next RTP packet */
  1418. redo:
  1419. if (!rt->recvbuf) {
  1420. rt->recvbuf = av_malloc(RECVBUF_SIZE);
  1421. if (!rt->recvbuf)
  1422. return AVERROR(ENOMEM);
  1423. }
  1424. switch(rt->lower_transport) {
  1425. default:
  1426. #if CONFIG_RTSP_DEMUXER
  1427. case RTSP_LOWER_TRANSPORT_TCP:
  1428. len = ff_rtsp_tcp_read_packet(s, &rtsp_st, rt->recvbuf, RECVBUF_SIZE);
  1429. break;
  1430. #endif
  1431. case RTSP_LOWER_TRANSPORT_UDP:
  1432. case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
  1433. len = udp_read_packet(s, &rtsp_st, rt->recvbuf, RECVBUF_SIZE, wait_end);
  1434. if (len >=0 && rtsp_st->transport_priv && rt->transport == RTSP_TRANSPORT_RTP)
  1435. rtp_check_and_send_back_rr(rtsp_st->transport_priv, len);
  1436. break;
  1437. }
  1438. if (len == AVERROR(EAGAIN) && first_queue_st &&
  1439. rt->transport == RTSP_TRANSPORT_RTP) {
  1440. rtsp_st = first_queue_st;
  1441. ret = rtp_parse_packet(rtsp_st->transport_priv, pkt, NULL, 0);
  1442. goto end;
  1443. }
  1444. if (len < 0)
  1445. return len;
  1446. if (len == 0)
  1447. return AVERROR_EOF;
  1448. if (rt->transport == RTSP_TRANSPORT_RDT) {
  1449. ret = ff_rdt_parse_packet(rtsp_st->transport_priv, pkt, &rt->recvbuf, len);
  1450. } else {
  1451. ret = rtp_parse_packet(rtsp_st->transport_priv, pkt, &rt->recvbuf, len);
  1452. if (ret < 0) {
  1453. /* Either bad packet, or a RTCP packet. Check if the
  1454. * first_rtcp_ntp_time field was initialized. */
  1455. RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
  1456. if (rtpctx->first_rtcp_ntp_time != AV_NOPTS_VALUE) {
  1457. /* first_rtcp_ntp_time has been initialized for this stream,
  1458. * copy the same value to all other uninitialized streams,
  1459. * in order to map their timestamp origin to the same ntp time
  1460. * as this one. */
  1461. int i;
  1462. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1463. RTPDemuxContext *rtpctx2 = rt->rtsp_streams[i]->transport_priv;
  1464. if (rtpctx2 &&
  1465. rtpctx2->first_rtcp_ntp_time == AV_NOPTS_VALUE)
  1466. rtpctx2->first_rtcp_ntp_time = rtpctx->first_rtcp_ntp_time;
  1467. }
  1468. }
  1469. if (ret == -RTCP_BYE) {
  1470. rt->nb_byes++;
  1471. av_log(s, AV_LOG_DEBUG, "Received BYE for stream %d (%d/%d)\n",
  1472. rtsp_st->stream_index, rt->nb_byes, rt->nb_rtsp_streams);
  1473. if (rt->nb_byes == rt->nb_rtsp_streams)
  1474. return AVERROR_EOF;
  1475. }
  1476. }
  1477. }
  1478. end:
  1479. if (ret < 0)
  1480. goto redo;
  1481. if (ret == 1)
  1482. /* more packets may follow, so we save the RTP context */
  1483. rt->cur_transport_priv = rtsp_st->transport_priv;
  1484. return ret;
  1485. }
  1486. #endif /* CONFIG_RTPDEC */
  1487. #if CONFIG_SDP_DEMUXER
  1488. static int sdp_probe(AVProbeData *p1)
  1489. {
  1490. const char *p = p1->buf, *p_end = p1->buf + p1->buf_size;
  1491. /* we look for a line beginning "c=IN IP" */
  1492. while (p < p_end && *p != '\0') {
  1493. if (p + sizeof("c=IN IP") - 1 < p_end &&
  1494. av_strstart(p, "c=IN IP", NULL))
  1495. return AVPROBE_SCORE_MAX / 2;
  1496. while (p < p_end - 1 && *p != '\n') p++;
  1497. if (++p >= p_end)
  1498. break;
  1499. if (*p == '\r')
  1500. p++;
  1501. }
  1502. return 0;
  1503. }
  1504. static int sdp_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1505. {
  1506. RTSPState *rt = s->priv_data;
  1507. RTSPStream *rtsp_st;
  1508. int size, i, err;
  1509. char *content;
  1510. char url[1024];
  1511. if (!ff_network_init())
  1512. return AVERROR(EIO);
  1513. /* read the whole sdp file */
  1514. /* XXX: better loading */
  1515. content = av_malloc(SDP_MAX_SIZE);
  1516. size = get_buffer(s->pb, content, SDP_MAX_SIZE - 1);
  1517. if (size <= 0) {
  1518. av_free(content);
  1519. return AVERROR_INVALIDDATA;
  1520. }
  1521. content[size] ='\0';
  1522. ff_sdp_parse(s, content);
  1523. av_free(content);
  1524. /* open each RTP stream */
  1525. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1526. char namebuf[50];
  1527. rtsp_st = rt->rtsp_streams[i];
  1528. getnameinfo((struct sockaddr*) &rtsp_st->sdp_ip, sizeof(rtsp_st->sdp_ip),
  1529. namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST);
  1530. ff_url_join(url, sizeof(url), "rtp", NULL,
  1531. namebuf, rtsp_st->sdp_port,
  1532. "?localport=%d&ttl=%d", rtsp_st->sdp_port,
  1533. rtsp_st->sdp_ttl);
  1534. if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
  1535. err = AVERROR_INVALIDDATA;
  1536. goto fail;
  1537. }
  1538. if ((err = rtsp_open_transport_ctx(s, rtsp_st)))
  1539. goto fail;
  1540. }
  1541. return 0;
  1542. fail:
  1543. ff_rtsp_close_streams(s);
  1544. ff_network_close();
  1545. return err;
  1546. }
  1547. static int sdp_read_close(AVFormatContext *s)
  1548. {
  1549. ff_rtsp_close_streams(s);
  1550. ff_network_close();
  1551. return 0;
  1552. }
  1553. AVInputFormat sdp_demuxer = {
  1554. "sdp",
  1555. NULL_IF_CONFIG_SMALL("SDP"),
  1556. sizeof(RTSPState),
  1557. sdp_probe,
  1558. sdp_read_header,
  1559. ff_rtsp_fetch_packet,
  1560. sdp_read_close,
  1561. };
  1562. #endif /* CONFIG_SDP_DEMUXER */
  1563. #if CONFIG_RTP_DEMUXER
  1564. static int rtp_probe(AVProbeData *p)
  1565. {
  1566. if (av_strstart(p->filename, "rtp:", NULL))
  1567. return AVPROBE_SCORE_MAX;
  1568. return 0;
  1569. }
  1570. static int rtp_read_header(AVFormatContext *s,
  1571. AVFormatParameters *ap)
  1572. {
  1573. uint8_t recvbuf[1500];
  1574. char host[500], sdp[500];
  1575. int ret, port;
  1576. URLContext* in = NULL;
  1577. int payload_type;
  1578. AVCodecContext codec;
  1579. struct sockaddr_storage addr;
  1580. ByteIOContext pb;
  1581. socklen_t addrlen = sizeof(addr);
  1582. if (!ff_network_init())
  1583. return AVERROR(EIO);
  1584. ret = url_open(&in, s->filename, URL_RDONLY);
  1585. if (ret)
  1586. goto fail;
  1587. while (1) {
  1588. ret = url_read(in, recvbuf, sizeof(recvbuf));
  1589. if (ret == AVERROR(EAGAIN))
  1590. continue;
  1591. if (ret < 0)
  1592. goto fail;
  1593. if (ret < 12) {
  1594. av_log(s, AV_LOG_WARNING, "Received too short packet\n");
  1595. continue;
  1596. }
  1597. if ((recvbuf[0] & 0xc0) != 0x80) {
  1598. av_log(s, AV_LOG_WARNING, "Unsupported RTP version packet "
  1599. "received\n");
  1600. continue;
  1601. }
  1602. payload_type = recvbuf[1] & 0x7f;
  1603. break;
  1604. }
  1605. getsockname(url_get_file_handle(in), (struct sockaddr*) &addr, &addrlen);
  1606. url_close(in);
  1607. in = NULL;
  1608. memset(&codec, 0, sizeof(codec));
  1609. if (ff_rtp_get_codec_info(&codec, payload_type)) {
  1610. av_log(s, AV_LOG_ERROR, "Unable to receive RTP payload type %d "
  1611. "without an SDP file describing it\n",
  1612. payload_type);
  1613. goto fail;
  1614. }
  1615. if (codec.codec_type != AVMEDIA_TYPE_DATA) {
  1616. av_log(s, AV_LOG_WARNING, "Guessing on RTP content - if not received "
  1617. "properly you need an SDP file "
  1618. "describing it\n");
  1619. }
  1620. av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port,
  1621. NULL, 0, s->filename);
  1622. snprintf(sdp, sizeof(sdp),
  1623. "v=0\r\nc=IN IP%d %s\r\nm=%s %d RTP/AVP %d\r\n",
  1624. addr.ss_family == AF_INET ? 4 : 6, host,
  1625. codec.codec_type == AVMEDIA_TYPE_DATA ? "application" :
  1626. codec.codec_type == AVMEDIA_TYPE_VIDEO ? "video" : "audio",
  1627. port, payload_type);
  1628. av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sdp);
  1629. init_put_byte(&pb, sdp, strlen(sdp), 0, NULL, NULL, NULL, NULL);
  1630. s->pb = &pb;
  1631. /* sdp_read_header initializes this again */
  1632. ff_network_close();
  1633. ret = sdp_read_header(s, ap);
  1634. s->pb = NULL;
  1635. return ret;
  1636. fail:
  1637. if (in)
  1638. url_close(in);
  1639. ff_network_close();
  1640. return ret;
  1641. }
  1642. AVInputFormat rtp_demuxer = {
  1643. "rtp",
  1644. NULL_IF_CONFIG_SMALL("RTP input format"),
  1645. sizeof(RTSPState),
  1646. rtp_probe,
  1647. rtp_read_header,
  1648. ff_rtsp_fetch_packet,
  1649. sdp_read_close,
  1650. .flags = AVFMT_NOFILE,
  1651. };
  1652. #endif /* CONFIG_RTP_DEMUXER */