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.

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