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.

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