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.

1986 lines
64KB

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