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.

1543 lines
46KB

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