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.

1650 lines
50KB

  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/avstring.h"
  24. #include "avformat.h"
  25. #include <sys/time.h>
  26. #ifdef HAVE_SYS_SELECT_H
  27. #include <sys/select.h>
  28. #endif
  29. #include <strings.h>
  30. #include "network.h"
  31. #include "rtsp.h"
  32. #include "rtp_internal.h"
  33. #include "rdt.h"
  34. //#define DEBUG
  35. //#define DEBUG_RTP_TCP
  36. enum RTSPClientState {
  37. RTSP_STATE_IDLE,
  38. RTSP_STATE_PLAYING,
  39. RTSP_STATE_PAUSED,
  40. };
  41. enum RTSPServerType {
  42. RTSP_SERVER_RTP, /*< Standard-compliant RTP-server */
  43. RTSP_SERVER_REAL, /*< Realmedia-style server */
  44. RTSP_SERVER_LAST
  45. };
  46. typedef struct RTSPState {
  47. URLContext *rtsp_hd; /* RTSP TCP connexion handle */
  48. int nb_rtsp_streams;
  49. struct RTSPStream **rtsp_streams;
  50. enum RTSPClientState state;
  51. int64_t seek_timestamp;
  52. /* XXX: currently we use unbuffered input */
  53. // ByteIOContext rtsp_gb;
  54. int seq; /* RTSP command sequence number */
  55. char session_id[512];
  56. enum RTSPProtocol protocol;
  57. enum RTSPServerType server_type;
  58. char last_reply[2048]; /* XXX: allocate ? */
  59. RTPDemuxContext *cur_rtp;
  60. int need_subscription;
  61. } RTSPState;
  62. typedef struct RTSPStream {
  63. URLContext *rtp_handle; /* RTP stream handle */
  64. RTPDemuxContext *rtp_ctx; /* RTP parse context */
  65. int stream_index; /* corresponding stream index, if any. -1 if none (MPEG2TS case) */
  66. int interleaved_min, interleaved_max; /* interleave ids, if TCP transport */
  67. char control_url[1024]; /* url for this stream (from SDP) */
  68. int sdp_port; /* port (from SDP content - not used in RTSP) */
  69. struct in_addr sdp_ip; /* IP address (from SDP content - not used in RTSP) */
  70. int sdp_ttl; /* IP TTL (from SDP content - not used in RTSP) */
  71. int sdp_payload_type; /* payload type - only used in SDP */
  72. rtp_payload_data_t rtp_payload_data; /* rtp payload parsing infos from SDP */
  73. RTPDynamicProtocolHandler *dynamic_handler; ///< Only valid if it's a dynamic protocol. (This is the handler structure)
  74. void *dynamic_protocol_context; ///< Only valid if it's a dynamic protocol. (This is any private data associated with the dynamic protocol)
  75. } RTSPStream;
  76. static int rtsp_read_play(AVFormatContext *s);
  77. /* XXX: currently, the only way to change the protocols consists in
  78. changing this variable */
  79. #if LIBAVFORMAT_VERSION_INT < (53 << 16)
  80. int rtsp_default_protocols = (1 << RTSP_PROTOCOL_RTP_UDP);
  81. #endif
  82. static int rtsp_probe(AVProbeData *p)
  83. {
  84. if (av_strstart(p->filename, "rtsp:", NULL))
  85. return AVPROBE_SCORE_MAX;
  86. return 0;
  87. }
  88. static int redir_isspace(int c)
  89. {
  90. return c == ' ' || c == '\t' || c == '\n' || c == '\r';
  91. }
  92. static void skip_spaces(const char **pp)
  93. {
  94. const char *p;
  95. p = *pp;
  96. while (redir_isspace(*p))
  97. p++;
  98. *pp = p;
  99. }
  100. static void get_word_sep(char *buf, int buf_size, const char *sep,
  101. const char **pp)
  102. {
  103. const char *p;
  104. char *q;
  105. p = *pp;
  106. if (*p == '/')
  107. p++;
  108. skip_spaces(&p);
  109. q = buf;
  110. while (!strchr(sep, *p) && *p != '\0') {
  111. if ((q - buf) < buf_size - 1)
  112. *q++ = *p;
  113. p++;
  114. }
  115. if (buf_size > 0)
  116. *q = '\0';
  117. *pp = p;
  118. }
  119. static void get_word(char *buf, int buf_size, const char **pp)
  120. {
  121. const char *p;
  122. char *q;
  123. p = *pp;
  124. skip_spaces(&p);
  125. q = buf;
  126. while (!redir_isspace(*p) && *p != '\0') {
  127. if ((q - buf) < buf_size - 1)
  128. *q++ = *p;
  129. p++;
  130. }
  131. if (buf_size > 0)
  132. *q = '\0';
  133. *pp = p;
  134. }
  135. /* parse the rtpmap description: <codec_name>/<clock_rate>[/<other
  136. params>] */
  137. static int sdp_parse_rtpmap(AVCodecContext *codec, RTSPStream *rtsp_st, int payload_type, const char *p)
  138. {
  139. char buf[256];
  140. int i;
  141. AVCodec *c;
  142. const char *c_name;
  143. /* Loop into AVRtpDynamicPayloadTypes[] and AVRtpPayloadTypes[] and
  144. see if we can handle this kind of payload */
  145. get_word_sep(buf, sizeof(buf), "/", &p);
  146. if (payload_type >= RTP_PT_PRIVATE) {
  147. RTPDynamicProtocolHandler *handler= RTPFirstDynamicPayloadHandler;
  148. while(handler) {
  149. if (!strcmp(buf, handler->enc_name) && (codec->codec_type == handler->codec_type)) {
  150. codec->codec_id = handler->codec_id;
  151. rtsp_st->dynamic_handler= handler;
  152. if(handler->open) {
  153. rtsp_st->dynamic_protocol_context= handler->open();
  154. }
  155. break;
  156. }
  157. handler= handler->next;
  158. }
  159. } else {
  160. /* We are in a standard case ( from http://www.iana.org/assignments/rtp-parameters) */
  161. /* search into AVRtpPayloadTypes[] */
  162. codec->codec_id = ff_rtp_codec_id(buf, codec->codec_type);
  163. }
  164. c = avcodec_find_decoder(codec->codec_id);
  165. if (c && c->name)
  166. c_name = c->name;
  167. else
  168. c_name = (char *)NULL;
  169. if (c_name) {
  170. get_word_sep(buf, sizeof(buf), "/", &p);
  171. i = atoi(buf);
  172. switch (codec->codec_type) {
  173. case CODEC_TYPE_AUDIO:
  174. av_log(codec, AV_LOG_DEBUG, " audio codec set to : %s\n", c_name);
  175. codec->sample_rate = RTSP_DEFAULT_AUDIO_SAMPLERATE;
  176. codec->channels = RTSP_DEFAULT_NB_AUDIO_CHANNELS;
  177. if (i > 0) {
  178. codec->sample_rate = i;
  179. get_word_sep(buf, sizeof(buf), "/", &p);
  180. i = atoi(buf);
  181. if (i > 0)
  182. codec->channels = i;
  183. // TODO: there is a bug here; if it is a mono stream, and less than 22000Hz, faad upconverts to stereo and twice the
  184. // frequency. No problem, but the sample rate is being set here by the sdp line. Upcoming patch forthcoming. (rdm)
  185. }
  186. av_log(codec, AV_LOG_DEBUG, " audio samplerate set to : %i\n", codec->sample_rate);
  187. av_log(codec, AV_LOG_DEBUG, " audio channels set to : %i\n", codec->channels);
  188. break;
  189. case CODEC_TYPE_VIDEO:
  190. av_log(codec, AV_LOG_DEBUG, " video codec set to : %s\n", c_name);
  191. break;
  192. default:
  193. break;
  194. }
  195. return 0;
  196. }
  197. return -1;
  198. }
  199. /* return the length and optionnaly the data */
  200. static int hex_to_data(uint8_t *data, const char *p)
  201. {
  202. int c, len, v;
  203. len = 0;
  204. v = 1;
  205. for(;;) {
  206. skip_spaces(&p);
  207. if (p == '\0')
  208. break;
  209. c = toupper((unsigned char)*p++);
  210. if (c >= '0' && c <= '9')
  211. c = c - '0';
  212. else if (c >= 'A' && c <= 'F')
  213. c = c - 'A' + 10;
  214. else
  215. break;
  216. v = (v << 4) | c;
  217. if (v & 0x100) {
  218. if (data)
  219. data[len] = v;
  220. len++;
  221. v = 1;
  222. }
  223. }
  224. return len;
  225. }
  226. static void sdp_parse_fmtp_config(AVCodecContext *codec, char *attr, char *value)
  227. {
  228. switch (codec->codec_id) {
  229. case CODEC_ID_MPEG4:
  230. case CODEC_ID_AAC:
  231. if (!strcmp(attr, "config")) {
  232. /* decode the hexa encoded parameter */
  233. int len = hex_to_data(NULL, value);
  234. codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
  235. if (!codec->extradata)
  236. return;
  237. codec->extradata_size = len;
  238. hex_to_data(codec->extradata, value);
  239. }
  240. break;
  241. default:
  242. break;
  243. }
  244. return;
  245. }
  246. typedef struct attrname_map
  247. {
  248. const char *str;
  249. uint16_t type;
  250. uint32_t offset;
  251. } attrname_map_t;
  252. /* All known fmtp parmeters and the corresping RTPAttrTypeEnum */
  253. #define ATTR_NAME_TYPE_INT 0
  254. #define ATTR_NAME_TYPE_STR 1
  255. static attrname_map_t attr_names[]=
  256. {
  257. {"SizeLength", ATTR_NAME_TYPE_INT, offsetof(rtp_payload_data_t, sizelength)},
  258. {"IndexLength", ATTR_NAME_TYPE_INT, offsetof(rtp_payload_data_t, indexlength)},
  259. {"IndexDeltaLength", ATTR_NAME_TYPE_INT, offsetof(rtp_payload_data_t, indexdeltalength)},
  260. {"profile-level-id", ATTR_NAME_TYPE_INT, offsetof(rtp_payload_data_t, profile_level_id)},
  261. {"StreamType", ATTR_NAME_TYPE_INT, offsetof(rtp_payload_data_t, streamtype)},
  262. {"mode", ATTR_NAME_TYPE_STR, offsetof(rtp_payload_data_t, mode)},
  263. {NULL, -1, -1},
  264. };
  265. /** parse the attribute line from the fmtp a line of an sdp resonse. This is broken out as a function
  266. * because it is used in rtp_h264.c, which is forthcoming.
  267. */
  268. int rtsp_next_attr_and_value(const char **p, char *attr, int attr_size, char *value, int value_size)
  269. {
  270. skip_spaces(p);
  271. if(**p)
  272. {
  273. get_word_sep(attr, attr_size, "=", p);
  274. if (**p == '=')
  275. (*p)++;
  276. get_word_sep(value, value_size, ";", p);
  277. if (**p == ';')
  278. (*p)++;
  279. return 1;
  280. }
  281. return 0;
  282. }
  283. /* parse a SDP line and save stream attributes */
  284. static void sdp_parse_fmtp(AVStream *st, const char *p)
  285. {
  286. char attr[256];
  287. char value[4096];
  288. int i;
  289. RTSPStream *rtsp_st = st->priv_data;
  290. AVCodecContext *codec = st->codec;
  291. rtp_payload_data_t *rtp_payload_data = &rtsp_st->rtp_payload_data;
  292. /* loop on each attribute */
  293. while(rtsp_next_attr_and_value(&p, attr, sizeof(attr), value, sizeof(value)))
  294. {
  295. /* grab the codec extra_data from the config parameter of the fmtp line */
  296. sdp_parse_fmtp_config(codec, attr, value);
  297. /* Looking for a known attribute */
  298. for (i = 0; attr_names[i].str; ++i) {
  299. if (!strcasecmp(attr, attr_names[i].str)) {
  300. if (attr_names[i].type == ATTR_NAME_TYPE_INT)
  301. *(int *)((char *)rtp_payload_data + attr_names[i].offset) = atoi(value);
  302. else if (attr_names[i].type == ATTR_NAME_TYPE_STR)
  303. *(char **)((char *)rtp_payload_data + attr_names[i].offset) = av_strdup(value);
  304. }
  305. }
  306. }
  307. }
  308. /** Parse a string \p in the form of Range:npt=xx-xx, and determine the start
  309. * and end time.
  310. * Used for seeking in the rtp stream.
  311. */
  312. static void rtsp_parse_range_npt(const char *p, int64_t *start, int64_t *end)
  313. {
  314. char buf[256];
  315. skip_spaces(&p);
  316. if (!av_stristart(p, "npt=", &p))
  317. return;
  318. *start = AV_NOPTS_VALUE;
  319. *end = AV_NOPTS_VALUE;
  320. get_word_sep(buf, sizeof(buf), "-", &p);
  321. *start = parse_date(buf, 1);
  322. if (*p == '-') {
  323. p++;
  324. get_word_sep(buf, sizeof(buf), "-", &p);
  325. *end = parse_date(buf, 1);
  326. }
  327. // av_log(NULL, AV_LOG_DEBUG, "Range Start: %lld\n", *start);
  328. // av_log(NULL, AV_LOG_DEBUG, "Range End: %lld\n", *end);
  329. }
  330. typedef struct SDPParseState {
  331. /* SDP only */
  332. struct in_addr default_ip;
  333. int default_ttl;
  334. } SDPParseState;
  335. static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1,
  336. int letter, const char *buf)
  337. {
  338. RTSPState *rt = s->priv_data;
  339. char buf1[64], st_type[64];
  340. const char *p;
  341. int codec_type, payload_type, i;
  342. AVStream *st;
  343. RTSPStream *rtsp_st;
  344. struct in_addr sdp_ip;
  345. int ttl;
  346. #ifdef DEBUG
  347. printf("sdp: %c='%s'\n", letter, buf);
  348. #endif
  349. p = buf;
  350. switch(letter) {
  351. case 'c':
  352. get_word(buf1, sizeof(buf1), &p);
  353. if (strcmp(buf1, "IN") != 0)
  354. return;
  355. get_word(buf1, sizeof(buf1), &p);
  356. if (strcmp(buf1, "IP4") != 0)
  357. return;
  358. get_word_sep(buf1, sizeof(buf1), "/", &p);
  359. if (inet_aton(buf1, &sdp_ip) == 0)
  360. return;
  361. ttl = 16;
  362. if (*p == '/') {
  363. p++;
  364. get_word_sep(buf1, sizeof(buf1), "/", &p);
  365. ttl = atoi(buf1);
  366. }
  367. if (s->nb_streams == 0) {
  368. s1->default_ip = sdp_ip;
  369. s1->default_ttl = ttl;
  370. } else {
  371. st = s->streams[s->nb_streams - 1];
  372. rtsp_st = st->priv_data;
  373. rtsp_st->sdp_ip = sdp_ip;
  374. rtsp_st->sdp_ttl = ttl;
  375. }
  376. break;
  377. case 's':
  378. av_strlcpy(s->title, p, sizeof(s->title));
  379. break;
  380. case 'i':
  381. if (s->nb_streams == 0) {
  382. av_strlcpy(s->comment, p, sizeof(s->comment));
  383. break;
  384. }
  385. break;
  386. case 'm':
  387. /* new stream */
  388. get_word(st_type, sizeof(st_type), &p);
  389. if (!strcmp(st_type, "audio")) {
  390. codec_type = CODEC_TYPE_AUDIO;
  391. } else if (!strcmp(st_type, "video")) {
  392. codec_type = CODEC_TYPE_VIDEO;
  393. } else {
  394. return;
  395. }
  396. rtsp_st = av_mallocz(sizeof(RTSPStream));
  397. if (!rtsp_st)
  398. return;
  399. rtsp_st->stream_index = -1;
  400. dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
  401. rtsp_st->sdp_ip = s1->default_ip;
  402. rtsp_st->sdp_ttl = s1->default_ttl;
  403. get_word(buf1, sizeof(buf1), &p); /* port */
  404. rtsp_st->sdp_port = atoi(buf1);
  405. get_word(buf1, sizeof(buf1), &p); /* protocol (ignored) */
  406. /* XXX: handle list of formats */
  407. get_word(buf1, sizeof(buf1), &p); /* format list */
  408. rtsp_st->sdp_payload_type = atoi(buf1);
  409. if (!strcmp(ff_rtp_enc_name(rtsp_st->sdp_payload_type), "MP2T")) {
  410. /* no corresponding stream */
  411. } else {
  412. st = av_new_stream(s, 0);
  413. if (!st)
  414. return;
  415. st->priv_data = rtsp_st;
  416. rtsp_st->stream_index = st->index;
  417. st->codec->codec_type = codec_type;
  418. if (rtsp_st->sdp_payload_type < RTP_PT_PRIVATE) {
  419. /* if standard payload type, we can find the codec right now */
  420. rtp_get_codec_info(st->codec, rtsp_st->sdp_payload_type);
  421. }
  422. }
  423. /* put a default control url */
  424. av_strlcpy(rtsp_st->control_url, s->filename, sizeof(rtsp_st->control_url));
  425. break;
  426. case 'a':
  427. if (av_strstart(p, "control:", &p) && s->nb_streams > 0) {
  428. char proto[32];
  429. /* get the control url */
  430. st = s->streams[s->nb_streams - 1];
  431. rtsp_st = st->priv_data;
  432. /* XXX: may need to add full url resolution */
  433. url_split(proto, sizeof(proto), NULL, 0, NULL, 0, NULL, NULL, 0, p);
  434. if (proto[0] == '\0') {
  435. /* relative control URL */
  436. av_strlcat(rtsp_st->control_url, "/", sizeof(rtsp_st->control_url));
  437. av_strlcat(rtsp_st->control_url, p, sizeof(rtsp_st->control_url));
  438. } else {
  439. av_strlcpy(rtsp_st->control_url, p, sizeof(rtsp_st->control_url));
  440. }
  441. } else if (av_strstart(p, "rtpmap:", &p)) {
  442. /* NOTE: rtpmap is only supported AFTER the 'm=' tag */
  443. get_word(buf1, sizeof(buf1), &p);
  444. payload_type = atoi(buf1);
  445. for(i = 0; i < s->nb_streams;i++) {
  446. st = s->streams[i];
  447. rtsp_st = st->priv_data;
  448. if (rtsp_st->sdp_payload_type == payload_type) {
  449. sdp_parse_rtpmap(st->codec, rtsp_st, payload_type, p);
  450. }
  451. }
  452. } else if (av_strstart(p, "fmtp:", &p)) {
  453. /* NOTE: fmtp is only supported AFTER the 'a=rtpmap:xxx' tag */
  454. get_word(buf1, sizeof(buf1), &p);
  455. payload_type = atoi(buf1);
  456. for(i = 0; i < s->nb_streams;i++) {
  457. st = s->streams[i];
  458. rtsp_st = st->priv_data;
  459. if (rtsp_st->sdp_payload_type == payload_type) {
  460. if(rtsp_st->dynamic_handler && rtsp_st->dynamic_handler->parse_sdp_a_line) {
  461. if(!rtsp_st->dynamic_handler->parse_sdp_a_line(st, rtsp_st->dynamic_protocol_context, buf)) {
  462. sdp_parse_fmtp(st, p);
  463. }
  464. } else {
  465. sdp_parse_fmtp(st, p);
  466. }
  467. }
  468. }
  469. } else if(av_strstart(p, "framesize:", &p)) {
  470. // let dynamic protocol handlers have a stab at the line.
  471. get_word(buf1, sizeof(buf1), &p);
  472. payload_type = atoi(buf1);
  473. for(i = 0; i < s->nb_streams;i++) {
  474. st = s->streams[i];
  475. rtsp_st = st->priv_data;
  476. if (rtsp_st->sdp_payload_type == payload_type) {
  477. if(rtsp_st->dynamic_handler && rtsp_st->dynamic_handler->parse_sdp_a_line) {
  478. rtsp_st->dynamic_handler->parse_sdp_a_line(st, rtsp_st->dynamic_protocol_context, buf);
  479. }
  480. }
  481. }
  482. } else if(av_strstart(p, "range:", &p)) {
  483. int64_t start, end;
  484. // this is so that seeking on a streamed file can work.
  485. rtsp_parse_range_npt(p, &start, &end);
  486. s->start_time= start;
  487. s->duration= (end==AV_NOPTS_VALUE)?AV_NOPTS_VALUE:end-start; // AV_NOPTS_VALUE means live broadcast (and can't seek)
  488. } else if (s->nb_streams > 0) {
  489. rtsp_st = s->streams[s->nb_streams - 1]->priv_data;
  490. if (rtsp_st->dynamic_handler &&
  491. rtsp_st->dynamic_handler->parse_sdp_a_line)
  492. rtsp_st->dynamic_handler->parse_sdp_a_line(s->streams[s->nb_streams - 1],
  493. rtsp_st->dynamic_protocol_context, buf);
  494. }
  495. break;
  496. }
  497. }
  498. static int sdp_parse(AVFormatContext *s, const char *content)
  499. {
  500. const char *p;
  501. int letter;
  502. char buf[2048], *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. static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
  533. {
  534. const char *p;
  535. int v;
  536. p = *pp;
  537. skip_spaces(&p);
  538. v = strtol(p, (char **)&p, 10);
  539. if (*p == '-') {
  540. p++;
  541. *min_ptr = v;
  542. v = strtol(p, (char **)&p, 10);
  543. *max_ptr = v;
  544. } else {
  545. *min_ptr = v;
  546. *max_ptr = v;
  547. }
  548. *pp = p;
  549. }
  550. /* XXX: only one transport specification is parsed */
  551. static void rtsp_parse_transport(RTSPHeader *reply, const char *p)
  552. {
  553. char transport_protocol[16];
  554. char profile[16];
  555. char lower_transport[16];
  556. char parameter[16];
  557. RTSPTransportField *th;
  558. char buf[256];
  559. reply->nb_transports = 0;
  560. for(;;) {
  561. skip_spaces(&p);
  562. if (*p == '\0')
  563. break;
  564. th = &reply->transports[reply->nb_transports];
  565. get_word_sep(transport_protocol, sizeof(transport_protocol),
  566. "/", &p);
  567. if (*p == '/')
  568. p++;
  569. if (!strcasecmp (transport_protocol, "rtp")) {
  570. get_word_sep(profile, sizeof(profile), "/;,", &p);
  571. lower_transport[0] = '\0';
  572. /* rtp/avp/<protocol> */
  573. if (*p == '/') {
  574. p++;
  575. get_word_sep(lower_transport, sizeof(lower_transport),
  576. ";,", &p);
  577. }
  578. } else if (!strcasecmp (transport_protocol, "x-pn-tng")) {
  579. /* x-pn-tng/<protocol> */
  580. get_word_sep(lower_transport, sizeof(lower_transport), "/;,", &p);
  581. profile[0] = '\0';
  582. }
  583. if (!strcasecmp(lower_transport, "TCP"))
  584. th->protocol = RTSP_PROTOCOL_RTP_TCP;
  585. else
  586. th->protocol = RTSP_PROTOCOL_RTP_UDP;
  587. if (*p == ';')
  588. p++;
  589. /* get each parameter */
  590. while (*p != '\0' && *p != ',') {
  591. get_word_sep(parameter, sizeof(parameter), "=;,", &p);
  592. if (!strcmp(parameter, "port")) {
  593. if (*p == '=') {
  594. p++;
  595. rtsp_parse_range(&th->port_min, &th->port_max, &p);
  596. }
  597. } else if (!strcmp(parameter, "client_port")) {
  598. if (*p == '=') {
  599. p++;
  600. rtsp_parse_range(&th->client_port_min,
  601. &th->client_port_max, &p);
  602. }
  603. } else if (!strcmp(parameter, "server_port")) {
  604. if (*p == '=') {
  605. p++;
  606. rtsp_parse_range(&th->server_port_min,
  607. &th->server_port_max, &p);
  608. }
  609. } else if (!strcmp(parameter, "interleaved")) {
  610. if (*p == '=') {
  611. p++;
  612. rtsp_parse_range(&th->interleaved_min,
  613. &th->interleaved_max, &p);
  614. }
  615. } else if (!strcmp(parameter, "multicast")) {
  616. if (th->protocol == RTSP_PROTOCOL_RTP_UDP)
  617. th->protocol = RTSP_PROTOCOL_RTP_UDP_MULTICAST;
  618. } else if (!strcmp(parameter, "ttl")) {
  619. if (*p == '=') {
  620. p++;
  621. th->ttl = strtol(p, (char **)&p, 10);
  622. }
  623. } else if (!strcmp(parameter, "destination")) {
  624. struct in_addr ipaddr;
  625. if (*p == '=') {
  626. p++;
  627. get_word_sep(buf, sizeof(buf), ";,", &p);
  628. if (inet_aton(buf, &ipaddr))
  629. th->destination = ntohl(ipaddr.s_addr);
  630. }
  631. }
  632. while (*p != ';' && *p != '\0' && *p != ',')
  633. p++;
  634. if (*p == ';')
  635. p++;
  636. }
  637. if (*p == ',')
  638. p++;
  639. reply->nb_transports++;
  640. }
  641. }
  642. void rtsp_parse_line(RTSPHeader *reply, const char *buf)
  643. {
  644. const char *p;
  645. /* NOTE: we do case independent match for broken servers */
  646. p = buf;
  647. if (av_stristart(p, "Session:", &p)) {
  648. get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
  649. } else if (av_stristart(p, "Content-Length:", &p)) {
  650. reply->content_length = strtol(p, NULL, 10);
  651. } else if (av_stristart(p, "Transport:", &p)) {
  652. rtsp_parse_transport(reply, p);
  653. } else if (av_stristart(p, "CSeq:", &p)) {
  654. reply->seq = strtol(p, NULL, 10);
  655. } else if (av_stristart(p, "Range:", &p)) {
  656. rtsp_parse_range_npt(p, &reply->range_start, &reply->range_end);
  657. } else if (av_stristart(p, "RealChallenge1:", &p)) {
  658. skip_spaces(&p);
  659. av_strlcpy(reply->real_challenge, p, sizeof(reply->real_challenge));
  660. }
  661. }
  662. static int url_readbuf(URLContext *h, unsigned char *buf, int size)
  663. {
  664. int ret, len;
  665. len = 0;
  666. while (len < size) {
  667. ret = url_read(h, buf+len, size-len);
  668. if (ret < 1)
  669. return ret;
  670. len += ret;
  671. }
  672. return len;
  673. }
  674. /* skip a RTP/TCP interleaved packet */
  675. static void rtsp_skip_packet(AVFormatContext *s)
  676. {
  677. RTSPState *rt = s->priv_data;
  678. int ret, len, len1;
  679. uint8_t buf[1024];
  680. ret = url_readbuf(rt->rtsp_hd, buf, 3);
  681. if (ret != 3)
  682. return;
  683. len = AV_RB16(buf + 1);
  684. #ifdef DEBUG
  685. printf("skipping RTP packet len=%d\n", len);
  686. #endif
  687. /* skip payload */
  688. while (len > 0) {
  689. len1 = len;
  690. if (len1 > sizeof(buf))
  691. len1 = sizeof(buf);
  692. ret = url_readbuf(rt->rtsp_hd, buf, len1);
  693. if (ret != len1)
  694. return;
  695. len -= len1;
  696. }
  697. }
  698. static void rtsp_send_cmd(AVFormatContext *s,
  699. const char *cmd, RTSPHeader *reply,
  700. unsigned char **content_ptr)
  701. {
  702. RTSPState *rt = s->priv_data;
  703. char buf[4096], buf1[1024], *q;
  704. unsigned char ch;
  705. const char *p;
  706. int content_length, line_count;
  707. unsigned char *content = NULL;
  708. memset(reply, 0, sizeof(RTSPHeader));
  709. rt->seq++;
  710. av_strlcpy(buf, cmd, sizeof(buf));
  711. snprintf(buf1, sizeof(buf1), "CSeq: %d\r\n", rt->seq);
  712. av_strlcat(buf, buf1, sizeof(buf));
  713. if (rt->session_id[0] != '\0' && !strstr(cmd, "\nIf-Match:")) {
  714. snprintf(buf1, sizeof(buf1), "Session: %s\r\n", rt->session_id);
  715. av_strlcat(buf, buf1, sizeof(buf));
  716. }
  717. av_strlcat(buf, "\r\n", sizeof(buf));
  718. #ifdef DEBUG
  719. printf("Sending:\n%s--\n", buf);
  720. #endif
  721. url_write(rt->rtsp_hd, buf, strlen(buf));
  722. /* parse reply (XXX: use buffers) */
  723. line_count = 0;
  724. rt->last_reply[0] = '\0';
  725. for(;;) {
  726. q = buf;
  727. for(;;) {
  728. if (url_readbuf(rt->rtsp_hd, &ch, 1) != 1)
  729. break;
  730. if (ch == '\n')
  731. break;
  732. if (ch == '$') {
  733. /* XXX: only parse it if first char on line ? */
  734. rtsp_skip_packet(s);
  735. } else if (ch != '\r') {
  736. if ((q - buf) < sizeof(buf) - 1)
  737. *q++ = ch;
  738. }
  739. }
  740. *q = '\0';
  741. #ifdef DEBUG
  742. printf("line='%s'\n", buf);
  743. #endif
  744. /* test if last line */
  745. if (buf[0] == '\0')
  746. break;
  747. p = buf;
  748. if (line_count == 0) {
  749. /* get reply code */
  750. get_word(buf1, sizeof(buf1), &p);
  751. get_word(buf1, sizeof(buf1), &p);
  752. reply->status_code = atoi(buf1);
  753. } else {
  754. rtsp_parse_line(reply, p);
  755. av_strlcat(rt->last_reply, p, sizeof(rt->last_reply));
  756. av_strlcat(rt->last_reply, "\n", sizeof(rt->last_reply));
  757. }
  758. line_count++;
  759. }
  760. if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0')
  761. av_strlcpy(rt->session_id, reply->session_id, sizeof(rt->session_id));
  762. content_length = reply->content_length;
  763. if (content_length > 0) {
  764. /* leave some room for a trailing '\0' (useful for simple parsing) */
  765. content = av_malloc(content_length + 1);
  766. (void)url_readbuf(rt->rtsp_hd, content, content_length);
  767. content[content_length] = '\0';
  768. }
  769. if (content_ptr)
  770. *content_ptr = content;
  771. else
  772. av_free(content);
  773. }
  774. /* close and free RTSP streams */
  775. static void rtsp_close_streams(RTSPState *rt)
  776. {
  777. int i;
  778. RTSPStream *rtsp_st;
  779. for(i=0;i<rt->nb_rtsp_streams;i++) {
  780. rtsp_st = rt->rtsp_streams[i];
  781. if (rtsp_st) {
  782. if (rtsp_st->rtp_ctx)
  783. rtp_parse_close(rtsp_st->rtp_ctx);
  784. if (rtsp_st->rtp_handle)
  785. url_close(rtsp_st->rtp_handle);
  786. if (rtsp_st->dynamic_handler && rtsp_st->dynamic_protocol_context)
  787. rtsp_st->dynamic_handler->close(rtsp_st->dynamic_protocol_context);
  788. }
  789. }
  790. av_free(rt->rtsp_streams);
  791. }
  792. static int
  793. rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st)
  794. {
  795. AVStream *st = NULL;
  796. /* open the RTP context */
  797. if (rtsp_st->stream_index >= 0)
  798. st = s->streams[rtsp_st->stream_index];
  799. if (!st)
  800. s->ctx_flags |= AVFMTCTX_NOHEADER;
  801. rtsp_st->rtp_ctx = rtp_parse_open(s, st, rtsp_st->rtp_handle, rtsp_st->sdp_payload_type, &rtsp_st->rtp_payload_data);
  802. if (!rtsp_st->rtp_ctx) {
  803. return AVERROR(ENOMEM);
  804. } else {
  805. if(rtsp_st->dynamic_handler) {
  806. rtsp_st->rtp_ctx->dynamic_protocol_context= rtsp_st->dynamic_protocol_context;
  807. rtsp_st->rtp_ctx->parse_packet= rtsp_st->dynamic_handler->parse_packet;
  808. }
  809. }
  810. return 0;
  811. }
  812. /**
  813. * @returns 0 on success, <0 on error, 1 if protocol is unavailable.
  814. */
  815. static int
  816. make_setup_request (AVFormatContext *s, const char *host, int port,
  817. int protocol, const char *real_challenge)
  818. {
  819. RTSPState *rt = s->priv_data;
  820. int j, i, err;
  821. RTSPStream *rtsp_st;
  822. RTSPHeader reply1, *reply = &reply1;
  823. char cmd[2048];
  824. const char *trans_pref;
  825. if (rt->server_type == RTSP_SERVER_REAL)
  826. trans_pref = "x-pn-tng";
  827. else
  828. trans_pref = "RTP/AVP";
  829. /* for each stream, make the setup request */
  830. /* XXX: we assume the same server is used for the control of each
  831. RTSP stream */
  832. for(j = RTSP_RTP_PORT_MIN, i = 0; i < rt->nb_rtsp_streams; ++i) {
  833. char transport[2048];
  834. rtsp_st = rt->rtsp_streams[i];
  835. /* RTP/UDP */
  836. if (protocol == RTSP_PROTOCOL_RTP_UDP) {
  837. char buf[256];
  838. /* first try in specified port range */
  839. if (RTSP_RTP_PORT_MIN != 0) {
  840. while(j <= RTSP_RTP_PORT_MAX) {
  841. snprintf(buf, sizeof(buf), "rtp://%s?localport=%d", host, j);
  842. j += 2; /* we will use two port by rtp stream (rtp and rtcp) */
  843. if (url_open(&rtsp_st->rtp_handle, buf, URL_RDWR) == 0) {
  844. goto rtp_opened;
  845. }
  846. }
  847. }
  848. /* then try on any port
  849. ** if (url_open(&rtsp_st->rtp_handle, "rtp://", URL_RDONLY) < 0) {
  850. ** err = AVERROR_INVALIDDATA;
  851. ** goto fail;
  852. ** }
  853. */
  854. rtp_opened:
  855. port = rtp_get_local_port(rtsp_st->rtp_handle);
  856. snprintf(transport, sizeof(transport) - 1,
  857. "%s/UDP;unicast;client_port=%d",
  858. trans_pref, port);
  859. if (rt->server_type == RTSP_SERVER_RTP)
  860. av_strlcatf(transport, sizeof(transport), "-%d", port + 1);
  861. }
  862. /* RTP/TCP */
  863. else if (protocol == RTSP_PROTOCOL_RTP_TCP) {
  864. snprintf(transport, sizeof(transport) - 1,
  865. "%s/TCP", trans_pref);
  866. }
  867. else if (protocol == RTSP_PROTOCOL_RTP_UDP_MULTICAST) {
  868. snprintf(transport, sizeof(transport) - 1,
  869. "%s/UDP;multicast", trans_pref);
  870. }
  871. if (rt->server_type == RTSP_SERVER_REAL)
  872. av_strlcat(transport, ";mode=play", sizeof(transport));
  873. snprintf(cmd, sizeof(cmd),
  874. "SETUP %s RTSP/1.0\r\n"
  875. "Transport: %s\r\n",
  876. rtsp_st->control_url, transport);
  877. if (i == 0 && rt->server_type == RTSP_SERVER_REAL) {
  878. char real_res[41], real_csum[9];
  879. ff_rdt_calc_response_and_checksum(real_res, real_csum,
  880. real_challenge);
  881. av_strlcatf(cmd, sizeof(cmd),
  882. "If-Match: %s\r\n"
  883. "RealChallenge2: %s, sd=%s\r\n",
  884. rt->session_id, real_res, real_csum);
  885. }
  886. rtsp_send_cmd(s, cmd, reply, NULL);
  887. if (reply->status_code == 461 /* Unsupported protocol */ && i == 0) {
  888. err = 1;
  889. goto fail;
  890. } else if (reply->status_code != RTSP_STATUS_OK ||
  891. reply->nb_transports != 1) {
  892. err = AVERROR_INVALIDDATA;
  893. goto fail;
  894. }
  895. /* XXX: same protocol for all streams is required */
  896. if (i > 0) {
  897. if (reply->transports[0].protocol != rt->protocol) {
  898. err = AVERROR_INVALIDDATA;
  899. goto fail;
  900. }
  901. } else {
  902. rt->protocol = reply->transports[0].protocol;
  903. }
  904. /* close RTP connection if not choosen */
  905. if (reply->transports[0].protocol != RTSP_PROTOCOL_RTP_UDP &&
  906. (protocol == RTSP_PROTOCOL_RTP_UDP)) {
  907. url_close(rtsp_st->rtp_handle);
  908. rtsp_st->rtp_handle = NULL;
  909. }
  910. switch(reply->transports[0].protocol) {
  911. case RTSP_PROTOCOL_RTP_TCP:
  912. rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
  913. rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
  914. break;
  915. case RTSP_PROTOCOL_RTP_UDP:
  916. {
  917. char url[1024];
  918. /* XXX: also use address if specified */
  919. snprintf(url, sizeof(url), "rtp://%s:%d",
  920. host, reply->transports[0].server_port_min);
  921. if (rtp_set_remote_url(rtsp_st->rtp_handle, url) < 0) {
  922. err = AVERROR_INVALIDDATA;
  923. goto fail;
  924. }
  925. }
  926. break;
  927. case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
  928. {
  929. char url[1024];
  930. struct in_addr in;
  931. in.s_addr = htonl(reply->transports[0].destination);
  932. snprintf(url, sizeof(url), "rtp://%s:%d?ttl=%d",
  933. inet_ntoa(in),
  934. reply->transports[0].port_min,
  935. reply->transports[0].ttl);
  936. if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
  937. err = AVERROR_INVALIDDATA;
  938. goto fail;
  939. }
  940. }
  941. break;
  942. }
  943. if ((err = rtsp_open_transport_ctx(s, rtsp_st)))
  944. goto fail;
  945. }
  946. if (rt->server_type == RTSP_SERVER_REAL)
  947. rt->need_subscription = 1;
  948. return 0;
  949. fail:
  950. for (i=0; i<rt->nb_rtsp_streams; i++) {
  951. if (rt->rtsp_streams[i]->rtp_handle) {
  952. url_close(rt->rtsp_streams[i]->rtp_handle);
  953. rt->rtsp_streams[i]->rtp_handle = NULL;
  954. }
  955. }
  956. return err;
  957. }
  958. static int rtsp_read_header(AVFormatContext *s,
  959. AVFormatParameters *ap)
  960. {
  961. RTSPState *rt = s->priv_data;
  962. char host[1024], path[1024], tcpname[1024], cmd[2048], *option_list, *option;
  963. URLContext *rtsp_hd;
  964. int port, ret, err;
  965. RTSPHeader reply1, *reply = &reply1;
  966. unsigned char *content = NULL;
  967. int protocol_mask = 0;
  968. char real_challenge[64];
  969. /* extract hostname and port */
  970. url_split(NULL, 0, NULL, 0,
  971. host, sizeof(host), &port, path, sizeof(path), s->filename);
  972. if (port < 0)
  973. port = RTSP_DEFAULT_PORT;
  974. /* search for options */
  975. option_list = strchr(path, '?');
  976. if (option_list) {
  977. /* remove the options from the path */
  978. *option_list++ = 0;
  979. while(option_list) {
  980. /* move the option pointer */
  981. option = option_list;
  982. option_list = strchr(option_list, '&');
  983. if (option_list)
  984. *(option_list++) = 0;
  985. /* handle the options */
  986. if (strcmp(option, "udp") == 0)
  987. protocol_mask = (1<< RTSP_PROTOCOL_RTP_UDP);
  988. else if (strcmp(option, "multicast") == 0)
  989. protocol_mask = (1<< RTSP_PROTOCOL_RTP_UDP_MULTICAST);
  990. else if (strcmp(option, "tcp") == 0)
  991. protocol_mask = (1<< RTSP_PROTOCOL_RTP_TCP);
  992. }
  993. }
  994. if (!protocol_mask)
  995. protocol_mask = (1 << RTSP_PROTOCOL_RTP_LAST) - 1;
  996. /* open the tcp connexion */
  997. snprintf(tcpname, sizeof(tcpname), "tcp://%s:%d", host, port);
  998. if (url_open(&rtsp_hd, tcpname, URL_RDWR) < 0)
  999. return AVERROR(EIO);
  1000. rt->rtsp_hd = rtsp_hd;
  1001. rt->seq = 0;
  1002. /* request options supported by the server; this also detects server type */
  1003. for (rt->server_type = RTSP_SERVER_RTP;;) {
  1004. snprintf(cmd, sizeof(cmd),
  1005. "OPTIONS %s RTSP/1.0\r\n", s->filename);
  1006. if (rt->server_type == RTSP_SERVER_REAL)
  1007. av_strlcat(cmd,
  1008. /**
  1009. * The following entries are required for proper
  1010. * streaming from a Realmedia server. They are
  1011. * interdependent in some way although we currently
  1012. * don't quite understand how. Values were copied
  1013. * from mplayer SVN r23589.
  1014. * @param CompanyID is a 16-byte ID in base64
  1015. * @param ClientChallenge is a 16-byte ID in hex
  1016. */
  1017. "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7\r\n"
  1018. "PlayerStarttime: [28/03/2003:22:50:23 00:00]\r\n"
  1019. "CompanyID: KnKV4M4I/B2FjJ1TToLycw==\r\n"
  1020. "GUID: 00000000-0000-0000-0000-000000000000\r\n",
  1021. sizeof(cmd));
  1022. rtsp_send_cmd(s, cmd, reply, NULL);
  1023. if (reply->status_code != RTSP_STATUS_OK) {
  1024. err = AVERROR_INVALIDDATA;
  1025. goto fail;
  1026. }
  1027. /* detect server type if not standard-compliant RTP */
  1028. if (rt->server_type != RTSP_SERVER_REAL && reply->real_challenge[0]) {
  1029. rt->server_type = RTSP_SERVER_REAL;
  1030. continue;
  1031. } else if (rt->server_type == RTSP_SERVER_REAL) {
  1032. strcpy(real_challenge, reply->real_challenge);
  1033. }
  1034. break;
  1035. }
  1036. /* describe the stream */
  1037. snprintf(cmd, sizeof(cmd),
  1038. "DESCRIBE %s RTSP/1.0\r\n"
  1039. "Accept: application/sdp\r\n",
  1040. s->filename);
  1041. if (rt->server_type == RTSP_SERVER_REAL) {
  1042. /**
  1043. * The Require: attribute is needed for proper streaming from
  1044. * Realmedia servers.
  1045. */
  1046. av_strlcat(cmd,
  1047. "Require: com.real.retain-entity-for-setup\r\n",
  1048. sizeof(cmd));
  1049. }
  1050. rtsp_send_cmd(s, cmd, reply, &content);
  1051. if (!content) {
  1052. err = AVERROR_INVALIDDATA;
  1053. goto fail;
  1054. }
  1055. if (reply->status_code != RTSP_STATUS_OK) {
  1056. err = AVERROR_INVALIDDATA;
  1057. goto fail;
  1058. }
  1059. /* now we got the SDP description, we parse it */
  1060. ret = sdp_parse(s, (const char *)content);
  1061. av_freep(&content);
  1062. if (ret < 0) {
  1063. err = AVERROR_INVALIDDATA;
  1064. goto fail;
  1065. }
  1066. do {
  1067. int protocol = ff_log2_tab[protocol_mask & ~(protocol_mask - 1)];
  1068. err = make_setup_request(s, host, port, protocol,
  1069. rt->server_type == RTSP_SERVER_REAL ?
  1070. real_challenge : NULL);
  1071. if (err < 0)
  1072. goto fail;
  1073. protocol_mask &= ~(1 << protocol);
  1074. if (protocol_mask == 0 && err == 1) {
  1075. err = AVERROR(FF_NETERROR(EPROTONOSUPPORT));
  1076. goto fail;
  1077. }
  1078. } while (err);
  1079. rt->state = RTSP_STATE_IDLE;
  1080. rt->seek_timestamp = 0; /* default is to start stream at position
  1081. zero */
  1082. if (ap->initial_pause) {
  1083. /* do not start immediately */
  1084. } else {
  1085. if (rtsp_read_play(s) < 0) {
  1086. err = AVERROR_INVALIDDATA;
  1087. goto fail;
  1088. }
  1089. }
  1090. return 0;
  1091. fail:
  1092. rtsp_close_streams(rt);
  1093. av_freep(&content);
  1094. url_close(rt->rtsp_hd);
  1095. return err;
  1096. }
  1097. static int tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  1098. uint8_t *buf, int buf_size)
  1099. {
  1100. RTSPState *rt = s->priv_data;
  1101. int id, len, i, ret;
  1102. RTSPStream *rtsp_st;
  1103. #ifdef DEBUG_RTP_TCP
  1104. printf("tcp_read_packet:\n");
  1105. #endif
  1106. redo:
  1107. for(;;) {
  1108. ret = url_readbuf(rt->rtsp_hd, buf, 1);
  1109. #ifdef DEBUG_RTP_TCP
  1110. printf("ret=%d c=%02x [%c]\n", ret, buf[0], buf[0]);
  1111. #endif
  1112. if (ret != 1)
  1113. return -1;
  1114. if (buf[0] == '$')
  1115. break;
  1116. }
  1117. ret = url_readbuf(rt->rtsp_hd, buf, 3);
  1118. if (ret != 3)
  1119. return -1;
  1120. id = buf[0];
  1121. len = AV_RB16(buf + 1);
  1122. #ifdef DEBUG_RTP_TCP
  1123. printf("id=%d len=%d\n", id, len);
  1124. #endif
  1125. if (len > buf_size || len < 12)
  1126. goto redo;
  1127. /* get the data */
  1128. ret = url_readbuf(rt->rtsp_hd, buf, len);
  1129. if (ret != len)
  1130. return -1;
  1131. /* find the matching stream */
  1132. for(i = 0; i < rt->nb_rtsp_streams; i++) {
  1133. rtsp_st = rt->rtsp_streams[i];
  1134. if (id >= rtsp_st->interleaved_min &&
  1135. id <= rtsp_st->interleaved_max)
  1136. goto found;
  1137. }
  1138. goto redo;
  1139. found:
  1140. *prtsp_st = rtsp_st;
  1141. return len;
  1142. }
  1143. static int udp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  1144. uint8_t *buf, int buf_size)
  1145. {
  1146. RTSPState *rt = s->priv_data;
  1147. RTSPStream *rtsp_st;
  1148. fd_set rfds;
  1149. int fd1, fd2, fd_max, n, i, ret;
  1150. struct timeval tv;
  1151. for(;;) {
  1152. if (url_interrupt_cb())
  1153. return AVERROR(EINTR);
  1154. FD_ZERO(&rfds);
  1155. fd_max = -1;
  1156. for(i = 0; i < rt->nb_rtsp_streams; i++) {
  1157. rtsp_st = rt->rtsp_streams[i];
  1158. /* currently, we cannot probe RTCP handle because of blocking restrictions */
  1159. rtp_get_file_handles(rtsp_st->rtp_handle, &fd1, &fd2);
  1160. if (fd1 > fd_max)
  1161. fd_max = fd1;
  1162. FD_SET(fd1, &rfds);
  1163. }
  1164. tv.tv_sec = 0;
  1165. tv.tv_usec = 100 * 1000;
  1166. n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
  1167. if (n > 0) {
  1168. for(i = 0; i < rt->nb_rtsp_streams; i++) {
  1169. rtsp_st = rt->rtsp_streams[i];
  1170. rtp_get_file_handles(rtsp_st->rtp_handle, &fd1, &fd2);
  1171. if (FD_ISSET(fd1, &rfds)) {
  1172. ret = url_read(rtsp_st->rtp_handle, buf, buf_size);
  1173. if (ret > 0) {
  1174. *prtsp_st = rtsp_st;
  1175. return ret;
  1176. }
  1177. }
  1178. }
  1179. }
  1180. }
  1181. }
  1182. static int rtsp_read_packet(AVFormatContext *s,
  1183. AVPacket *pkt)
  1184. {
  1185. RTSPState *rt = s->priv_data;
  1186. RTSPStream *rtsp_st;
  1187. int ret, len;
  1188. uint8_t buf[RTP_MAX_PACKET_LENGTH];
  1189. if (rt->server_type == RTSP_SERVER_REAL && rt->need_subscription) {
  1190. int i;
  1191. RTSPHeader reply1, *reply = &reply1;
  1192. char cmd[1024];
  1193. snprintf(cmd, sizeof(cmd),
  1194. "SET_PARAMETER %s RTSP/1.0\r\n"
  1195. "Subscribe: ",
  1196. s->filename);
  1197. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1198. if (i != 0) av_strlcat(cmd, ",", sizeof(cmd));
  1199. ff_rdt_subscribe_rule(
  1200. rt->rtsp_streams[i]->rtp_ctx,
  1201. cmd, sizeof(cmd), i, 0);
  1202. }
  1203. av_strlcat(cmd, "\r\n", sizeof(cmd));
  1204. rtsp_send_cmd(s, cmd, reply, NULL);
  1205. if (reply->status_code != RTSP_STATUS_OK)
  1206. return AVERROR_INVALIDDATA;
  1207. rt->need_subscription = 0;
  1208. if (rt->state == RTSP_STATE_PLAYING)
  1209. rtsp_read_play (s);
  1210. }
  1211. /* get next frames from the same RTP packet */
  1212. if (rt->cur_rtp) {
  1213. if (rt->server_type == RTSP_SERVER_REAL)
  1214. ret = ff_rdt_parse_packet(rt->cur_rtp, pkt, NULL, 0);
  1215. else
  1216. ret = rtp_parse_packet(rt->cur_rtp, pkt, NULL, 0);
  1217. if (ret == 0) {
  1218. rt->cur_rtp = NULL;
  1219. return 0;
  1220. } else if (ret == 1) {
  1221. return 0;
  1222. } else {
  1223. rt->cur_rtp = NULL;
  1224. }
  1225. }
  1226. /* read next RTP packet */
  1227. redo:
  1228. switch(rt->protocol) {
  1229. default:
  1230. case RTSP_PROTOCOL_RTP_TCP:
  1231. len = tcp_read_packet(s, &rtsp_st, buf, sizeof(buf));
  1232. break;
  1233. case RTSP_PROTOCOL_RTP_UDP:
  1234. case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
  1235. len = udp_read_packet(s, &rtsp_st, buf, sizeof(buf));
  1236. if (len >=0 && rtsp_st->rtp_ctx)
  1237. rtp_check_and_send_back_rr(rtsp_st->rtp_ctx, len);
  1238. break;
  1239. }
  1240. if (len < 0)
  1241. return len;
  1242. if (rt->server_type == RTSP_SERVER_REAL)
  1243. ret = ff_rdt_parse_packet(rtsp_st->rtp_ctx, pkt, buf, len);
  1244. else
  1245. ret = rtp_parse_packet(rtsp_st->rtp_ctx, pkt, buf, len);
  1246. if (ret < 0)
  1247. goto redo;
  1248. if (ret == 1) {
  1249. /* more packets may follow, so we save the RTP context */
  1250. rt->cur_rtp = rtsp_st->rtp_ctx;
  1251. }
  1252. return 0;
  1253. }
  1254. static int rtsp_read_play(AVFormatContext *s)
  1255. {
  1256. RTSPState *rt = s->priv_data;
  1257. RTSPHeader reply1, *reply = &reply1;
  1258. char cmd[1024];
  1259. av_log(s, AV_LOG_DEBUG, "hello state=%d\n", rt->state);
  1260. if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
  1261. if (rt->state == RTSP_STATE_PAUSED) {
  1262. snprintf(cmd, sizeof(cmd),
  1263. "PLAY %s RTSP/1.0\r\n",
  1264. s->filename);
  1265. } else {
  1266. snprintf(cmd, sizeof(cmd),
  1267. "PLAY %s RTSP/1.0\r\n"
  1268. "Range: npt=%0.3f-\r\n",
  1269. s->filename,
  1270. (double)rt->seek_timestamp / AV_TIME_BASE);
  1271. }
  1272. rtsp_send_cmd(s, cmd, reply, NULL);
  1273. if (reply->status_code != RTSP_STATUS_OK) {
  1274. return -1;
  1275. }
  1276. }
  1277. rt->state = RTSP_STATE_PLAYING;
  1278. return 0;
  1279. }
  1280. /* pause the stream */
  1281. static int rtsp_read_pause(AVFormatContext *s)
  1282. {
  1283. RTSPState *rt = s->priv_data;
  1284. RTSPHeader reply1, *reply = &reply1;
  1285. char cmd[1024];
  1286. rt = s->priv_data;
  1287. if (rt->state != RTSP_STATE_PLAYING)
  1288. return 0;
  1289. else if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
  1290. snprintf(cmd, sizeof(cmd),
  1291. "PAUSE %s RTSP/1.0\r\n",
  1292. s->filename);
  1293. rtsp_send_cmd(s, cmd, reply, NULL);
  1294. if (reply->status_code != RTSP_STATUS_OK) {
  1295. return -1;
  1296. }
  1297. }
  1298. rt->state = RTSP_STATE_PAUSED;
  1299. return 0;
  1300. }
  1301. static int rtsp_read_seek(AVFormatContext *s, int stream_index,
  1302. int64_t timestamp, int flags)
  1303. {
  1304. RTSPState *rt = s->priv_data;
  1305. rt->seek_timestamp = av_rescale_q(timestamp, s->streams[stream_index]->time_base, AV_TIME_BASE_Q);
  1306. switch(rt->state) {
  1307. default:
  1308. case RTSP_STATE_IDLE:
  1309. break;
  1310. case RTSP_STATE_PLAYING:
  1311. if (rtsp_read_play(s) != 0)
  1312. return -1;
  1313. break;
  1314. case RTSP_STATE_PAUSED:
  1315. rt->state = RTSP_STATE_IDLE;
  1316. break;
  1317. }
  1318. return 0;
  1319. }
  1320. static int rtsp_read_close(AVFormatContext *s)
  1321. {
  1322. RTSPState *rt = s->priv_data;
  1323. RTSPHeader reply1, *reply = &reply1;
  1324. char cmd[1024];
  1325. #if 0
  1326. /* NOTE: it is valid to flush the buffer here */
  1327. if (rt->protocol == RTSP_PROTOCOL_RTP_TCP) {
  1328. url_fclose(&rt->rtsp_gb);
  1329. }
  1330. #endif
  1331. snprintf(cmd, sizeof(cmd),
  1332. "TEARDOWN %s RTSP/1.0\r\n",
  1333. s->filename);
  1334. rtsp_send_cmd(s, cmd, reply, NULL);
  1335. rtsp_close_streams(rt);
  1336. url_close(rt->rtsp_hd);
  1337. return 0;
  1338. }
  1339. #ifdef CONFIG_RTSP_DEMUXER
  1340. AVInputFormat rtsp_demuxer = {
  1341. "rtsp",
  1342. NULL_IF_CONFIG_SMALL("RTSP input format"),
  1343. sizeof(RTSPState),
  1344. rtsp_probe,
  1345. rtsp_read_header,
  1346. rtsp_read_packet,
  1347. rtsp_read_close,
  1348. rtsp_read_seek,
  1349. .flags = AVFMT_NOFILE,
  1350. .read_play = rtsp_read_play,
  1351. .read_pause = rtsp_read_pause,
  1352. };
  1353. #endif
  1354. static int sdp_probe(AVProbeData *p1)
  1355. {
  1356. const char *p = p1->buf, *p_end = p1->buf + p1->buf_size;
  1357. /* we look for a line beginning "c=IN IP4" */
  1358. while (p < p_end && *p != '\0') {
  1359. if (p + sizeof("c=IN IP4") - 1 < p_end && av_strstart(p, "c=IN IP4", NULL))
  1360. return AVPROBE_SCORE_MAX / 2;
  1361. while(p < p_end - 1 && *p != '\n') p++;
  1362. if (++p >= p_end)
  1363. break;
  1364. if (*p == '\r')
  1365. p++;
  1366. }
  1367. return 0;
  1368. }
  1369. #define SDP_MAX_SIZE 8192
  1370. static int sdp_read_header(AVFormatContext *s,
  1371. AVFormatParameters *ap)
  1372. {
  1373. RTSPState *rt = s->priv_data;
  1374. RTSPStream *rtsp_st;
  1375. int size, i, err;
  1376. char *content;
  1377. char url[1024];
  1378. /* read the whole sdp file */
  1379. /* XXX: better loading */
  1380. content = av_malloc(SDP_MAX_SIZE);
  1381. size = get_buffer(s->pb, content, SDP_MAX_SIZE - 1);
  1382. if (size <= 0) {
  1383. av_free(content);
  1384. return AVERROR_INVALIDDATA;
  1385. }
  1386. content[size] ='\0';
  1387. sdp_parse(s, content);
  1388. av_free(content);
  1389. /* open each RTP stream */
  1390. for(i=0;i<rt->nb_rtsp_streams;i++) {
  1391. rtsp_st = rt->rtsp_streams[i];
  1392. snprintf(url, sizeof(url), "rtp://%s:%d?localport=%d&ttl=%d",
  1393. inet_ntoa(rtsp_st->sdp_ip),
  1394. rtsp_st->sdp_port,
  1395. rtsp_st->sdp_port,
  1396. rtsp_st->sdp_ttl);
  1397. if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
  1398. err = AVERROR_INVALIDDATA;
  1399. goto fail;
  1400. }
  1401. if ((err = rtsp_open_transport_ctx(s, rtsp_st)))
  1402. goto fail;
  1403. }
  1404. return 0;
  1405. fail:
  1406. rtsp_close_streams(rt);
  1407. return err;
  1408. }
  1409. static int sdp_read_packet(AVFormatContext *s,
  1410. AVPacket *pkt)
  1411. {
  1412. return rtsp_read_packet(s, pkt);
  1413. }
  1414. static int sdp_read_close(AVFormatContext *s)
  1415. {
  1416. RTSPState *rt = s->priv_data;
  1417. rtsp_close_streams(rt);
  1418. return 0;
  1419. }
  1420. #ifdef CONFIG_SDP_DEMUXER
  1421. AVInputFormat sdp_demuxer = {
  1422. "sdp",
  1423. NULL_IF_CONFIG_SMALL("SDP"),
  1424. sizeof(RTSPState),
  1425. sdp_probe,
  1426. sdp_read_header,
  1427. sdp_read_packet,
  1428. sdp_read_close,
  1429. };
  1430. #endif
  1431. #ifdef CONFIG_REDIR_DEMUXER
  1432. /* dummy redirector format (used directly in av_open_input_file now) */
  1433. static int redir_probe(AVProbeData *pd)
  1434. {
  1435. const char *p;
  1436. p = pd->buf;
  1437. while (redir_isspace(*p))
  1438. p++;
  1439. if (av_strstart(p, "http://", NULL) ||
  1440. av_strstart(p, "rtsp://", NULL))
  1441. return AVPROBE_SCORE_MAX;
  1442. return 0;
  1443. }
  1444. static int redir_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1445. {
  1446. char buf[4096], *q;
  1447. int c;
  1448. AVFormatContext *ic = NULL;
  1449. ByteIOContext *f = s->pb;
  1450. /* parse each URL and try to open it */
  1451. c = url_fgetc(f);
  1452. while (c != URL_EOF) {
  1453. /* skip spaces */
  1454. for(;;) {
  1455. if (!redir_isspace(c))
  1456. break;
  1457. c = url_fgetc(f);
  1458. }
  1459. if (c == URL_EOF)
  1460. break;
  1461. /* record url */
  1462. q = buf;
  1463. for(;;) {
  1464. if (c == URL_EOF || redir_isspace(c))
  1465. break;
  1466. if ((q - buf) < sizeof(buf) - 1)
  1467. *q++ = c;
  1468. c = url_fgetc(f);
  1469. }
  1470. *q = '\0';
  1471. //printf("URL='%s'\n", buf);
  1472. /* try to open the media file */
  1473. if (av_open_input_file(&ic, buf, NULL, 0, NULL) == 0)
  1474. break;
  1475. }
  1476. if (!ic)
  1477. return AVERROR(EIO);
  1478. *s = *ic;
  1479. url_fclose(f);
  1480. return 0;
  1481. }
  1482. AVInputFormat redir_demuxer = {
  1483. "redir",
  1484. NULL_IF_CONFIG_SMALL("Redirector format"),
  1485. 0,
  1486. redir_probe,
  1487. redir_read_header,
  1488. NULL,
  1489. NULL,
  1490. };
  1491. #endif