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.

1490 lines
44KB

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