3 * Copyright (c) 2002 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <netinet/in.h>
23 #include <sys/socket.h>
24 #include <arpa/inet.h>
28 typedef struct RTSPState {
29 URLContext *rtsp_hd; /* RTSP TCP connexion handle */
30 ByteIOContext rtsp_gb;
31 int seq; /* RTSP command sequence number */
33 enum RTSPProtocol protocol;
34 char last_reply[2048]; /* XXX: allocate ? */
37 typedef struct RTSPStream {
39 int interleaved_min, interleaved_max; /* interleave ids, if TCP transport */
40 char control_url[1024]; /* url for this stream */
43 /* suppress this hack */
44 int rtsp_abort_req = 0;
46 /* XXX: currently, the only way to change the protocols consists in
47 changing this variable */
48 int rtsp_default_protocols = (1 << RTSP_PROTOCOL_RTP_TCP) | (1 << RTSP_PROTOCOL_RTP_UDP) | (1 << RTSP_PROTOCOL_RTP_UDP_MULTICAST);
50 /* if non zero, then set a range for RTP ports */
51 int rtsp_rtp_port_min = 0;
52 int rtsp_rtp_port_max = 0;
54 FFRTSPCallback *ff_rtsp_callback = NULL;
56 static int rtsp_probe(AVProbeData *p)
58 if (strstart(p->filename, "rtsp:", NULL))
59 return AVPROBE_SCORE_MAX;
63 static int redir_isspace(int c)
65 return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
68 static void skip_spaces(const char **pp)
72 while (redir_isspace(*p))
77 static void get_word_sep(char *buf, int buf_size, const char *sep,
86 while (!strchr(sep, *p) && *p != '\0') {
87 if ((q - buf) < buf_size - 1)
96 static void get_word(char *buf, int buf_size, const char **pp)
104 while (!redir_isspace(*p) && *p != '\0') {
105 if ((q - buf) < buf_size - 1)
114 static void sdp_parse_line(AVFormatContext *s,
115 int letter, const char *buf)
117 char buf1[64], st_type[64];
119 int codec_type, payload_type;
124 printf("sdp: %c='%s'\n", letter, buf);
130 pstrcpy(s->title, sizeof(s->title), p);
133 if (s->nb_streams == 0) {
134 pstrcpy(s->comment, sizeof(s->comment), p);
140 get_word(st_type, sizeof(st_type), &p);
141 if (!strcmp(st_type, "audio")) {
142 codec_type = CODEC_TYPE_AUDIO;
143 } else if (!strcmp(st_type, "video")) {
144 codec_type = CODEC_TYPE_VIDEO;
148 get_word(buf1, sizeof(buf1), &p); /* port */
149 get_word(buf1, sizeof(buf1), &p); /* protocol */
150 /* XXX: handle list of formats */
151 get_word(buf1, sizeof(buf1), &p); /* format list */
152 payload_type = atoi(buf1);
154 rtsp_st = av_mallocz(sizeof(RTSPStream));
157 st = av_new_stream(s, s->nb_streams);
160 st->priv_data = rtsp_st;
161 /* put a default control url */
162 pstrcpy(rtsp_st->control_url, sizeof(rtsp_st->control_url), s->filename);
163 st->codec.codec_type = codec_type;
164 rtp_get_codec_info(&st->codec, payload_type);
167 if (strstart(p, "control:", &p) && s->nb_streams > 0) {
169 /* get the control url */
170 st = s->streams[s->nb_streams - 1];
171 rtsp_st = st->priv_data;
173 /* XXX: may need to add full url resolution */
174 url_split(proto, sizeof(proto), NULL, 0, NULL, NULL, 0, p);
175 if (proto[0] == '\0') {
176 /* relative control URL */
177 pstrcat(rtsp_st->control_url, sizeof(rtsp_st->control_url), "/");
178 pstrcat(rtsp_st->control_url, sizeof(rtsp_st->control_url), p);
180 pstrcpy(rtsp_st->control_url, sizeof(rtsp_st->control_url), p);
187 int sdp_parse(AVFormatContext *s, const char *content)
203 /* get the content */
205 while (*p != '\n' && *p != '\0') {
206 if ((q - buf) < sizeof(buf) - 1)
211 sdp_parse_line(s, letter, buf);
213 while (*p != '\n' && *p != '\0')
221 static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
228 v = strtol(p, (char **)&p, 10);
232 v = strtol(p, (char **)&p, 10);
241 /* XXX: only one transport specification is parsed */
242 static void rtsp_parse_transport(RTSPHeader *reply, const char *p)
244 char transport_protocol[16];
246 char lower_transport[16];
248 RTSPTransportField *th;
251 reply->nb_transports = 0;
258 th = &reply->transports[reply->nb_transports];
260 get_word_sep(transport_protocol, sizeof(transport_protocol),
264 get_word_sep(profile, sizeof(profile), "/;,", &p);
265 lower_transport[0] = '\0';
267 get_word_sep(lower_transport, sizeof(lower_transport),
270 if (!strcmp(lower_transport, "TCP"))
271 th->protocol = RTSP_PROTOCOL_RTP_TCP;
273 th->protocol = RTSP_PROTOCOL_RTP_UDP;
277 /* get each parameter */
278 while (*p != '\0' && *p != ',') {
279 get_word_sep(parameter, sizeof(parameter), "=;,", &p);
280 if (!strcmp(parameter, "port")) {
283 rtsp_parse_range(&th->port_min, &th->port_max, &p);
285 } else if (!strcmp(parameter, "client_port")) {
288 rtsp_parse_range(&th->client_port_min,
289 &th->client_port_max, &p);
291 } else if (!strcmp(parameter, "server_port")) {
294 rtsp_parse_range(&th->server_port_min,
295 &th->server_port_max, &p);
297 } else if (!strcmp(parameter, "interleaved")) {
300 rtsp_parse_range(&th->interleaved_min,
301 &th->interleaved_max, &p);
303 } else if (!strcmp(parameter, "multicast")) {
304 if (th->protocol == RTSP_PROTOCOL_RTP_UDP)
305 th->protocol = RTSP_PROTOCOL_RTP_UDP_MULTICAST;
306 } else if (!strcmp(parameter, "ttl")) {
309 th->ttl = strtol(p, (char **)&p, 10);
311 } else if (!strcmp(parameter, "destination")) {
312 struct in_addr ipaddr;
316 get_word_sep(buf, sizeof(buf), ";,", &p);
317 if (inet_aton(buf, &ipaddr))
318 th->destination = ntohl(ipaddr.s_addr);
321 while (*p != ';' && *p != '\0' && *p != ',')
329 reply->nb_transports++;
333 void rtsp_parse_line(RTSPHeader *reply, const char *buf)
337 /* NOTE: we do case independent match for broken servers */
339 if (stristart(p, "Session:", &p)) {
340 get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
341 } else if (stristart(p, "Content-Length:", &p)) {
342 reply->content_length = strtol(p, NULL, 10);
343 } else if (stristart(p, "Transport:", &p)) {
344 rtsp_parse_transport(reply, p);
345 } else if (stristart(p, "CSeq:", &p)) {
346 reply->seq = strtol(p, NULL, 10);
351 static void rtsp_send_cmd(AVFormatContext *s,
352 const char *cmd, RTSPHeader *reply,
353 unsigned char **content_ptr)
355 RTSPState *rt = s->priv_data;
356 char buf[4096], buf1[1024], *q;
359 int content_length, line_count;
360 unsigned char *content = NULL;
362 memset(reply, 0, sizeof(RTSPHeader));
365 pstrcpy(buf, sizeof(buf), cmd);
366 snprintf(buf1, sizeof(buf1), "CSeq: %d\n", rt->seq);
367 pstrcat(buf, sizeof(buf), buf1);
368 if (rt->session_id[0] != '\0' && !strstr(cmd, "\nIf-Match:")) {
369 snprintf(buf1, sizeof(buf1), "Session: %s\n", rt->session_id);
370 pstrcat(buf, sizeof(buf), buf1);
372 pstrcat(buf, sizeof(buf), "\n");
374 printf("Sending:\n%s--\n", buf);
376 url_write(rt->rtsp_hd, buf, strlen(buf));
378 /* parse reply (XXX: use buffers) */
380 rt->last_reply[0] = '\0';
384 if (url_read(rt->rtsp_hd, &ch, 1) == 0)
389 if ((q - buf) < sizeof(buf) - 1)
395 printf("line='%s'\n", buf);
397 /* test if last line */
401 if (line_count == 0) {
403 get_word(buf1, sizeof(buf1), &p);
404 get_word(buf1, sizeof(buf1), &p);
405 reply->status_code = atoi(buf1);
407 rtsp_parse_line(reply, p);
408 pstrcat(rt->last_reply, sizeof(rt->last_reply), p);
409 pstrcat(rt->last_reply, sizeof(rt->last_reply), "\n");
414 if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0')
415 pstrcpy(rt->session_id, sizeof(rt->session_id), reply->session_id);
417 content_length = reply->content_length;
418 if (content_length > 0) {
419 /* leave some room for a trailing '\0' (useful for simple parsing) */
420 content = av_malloc(content_length + 1);
421 url_read(rt->rtsp_hd, content, content_length);
422 content[content_length] = '\0';
425 *content_ptr = content;
428 /* useful for modules: set RTSP callback function */
430 void rtsp_set_callback(FFRTSPCallback *rtsp_cb)
432 ff_rtsp_callback = rtsp_cb;
436 static int rtsp_read_header(AVFormatContext *s,
437 AVFormatParameters *ap)
439 RTSPState *rt = s->priv_data;
440 char host[1024], path[1024], tcpname[1024], cmd[2048];
442 int port, i, ret, err;
443 RTSPHeader reply1, *reply = &reply1;
444 unsigned char *content = NULL;
451 /* extract hostname and port */
453 host, sizeof(host), &port, path, sizeof(path), s->filename);
455 port = RTSP_DEFAULT_PORT;
457 /* open the tcp connexion */
458 snprintf(tcpname, sizeof(tcpname), "tcp://%s:%d", host, port);
459 if (url_open(&rtsp_hd, tcpname, URL_RDWR) < 0)
461 rt->rtsp_hd = rtsp_hd;
464 /* describe the stream */
465 snprintf(cmd, sizeof(cmd),
466 "DESCRIBE %s RTSP/1.0\n"
467 "Accept: application/sdp\n",
469 rtsp_send_cmd(s, cmd, reply, &content);
471 err = AVERROR_INVALIDDATA;
474 if (reply->status_code != RTSP_STATUS_OK) {
475 err = AVERROR_INVALIDDATA;
479 /* now we got the SDP description, we parse it */
480 ret = sdp_parse(s, (const char *)content);
483 err = AVERROR_INVALIDDATA;
487 protocol_mask = rtsp_default_protocols;
489 /* for each stream, make the setup request */
490 /* XXX: we assume the same server is used for the control of each
492 for(i=0;i<s->nb_streams;i++) {
493 char transport[2048];
497 rtsp_st = st->priv_data;
499 /* compute available transports */
503 if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP)) {
507 /* first try in specified port range */
508 if (rtsp_rtp_port_min != 0) {
509 for(j=rtsp_rtp_port_min;j<=rtsp_rtp_port_max;j++) {
510 snprintf(buf, sizeof(buf), "rtp://?localport=%d", j);
511 if (!av_open_input_file(&rtsp_st->ic, buf,
512 &rtp_demux, 0, NULL))
517 /* then try on any port */
518 if (av_open_input_file(&rtsp_st->ic, "rtp://",
519 &rtp_demux, 0, NULL) < 0) {
520 err = AVERROR_INVALIDDATA;
525 port = rtp_get_local_port(url_fileno(&rtsp_st->ic->pb));
526 if (transport[0] != '\0')
527 pstrcat(transport, sizeof(transport), ",");
528 snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
529 "RTP/AVP/UDP;unicast;client_port=%d-%d",
534 if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_TCP)) {
535 if (transport[0] != '\0')
536 pstrcat(transport, sizeof(transport), ",");
537 snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
541 if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP_MULTICAST)) {
542 if (transport[0] != '\0')
543 pstrcat(transport, sizeof(transport), ",");
544 snprintf(transport + strlen(transport),
545 sizeof(transport) - strlen(transport) - 1,
546 "RTP/AVP/UDP;multicast");
549 snprintf(cmd, sizeof(cmd),
550 "SETUP %s RTSP/1.0\n"
552 rtsp_st->control_url, transport);
553 rtsp_send_cmd(s, cmd, reply, NULL);
554 if (reply->status_code != RTSP_STATUS_OK ||
555 reply->nb_transports != 1) {
556 err = AVERROR_INVALIDDATA;
560 /* XXX: same protocol for all streams is required */
562 if (reply->transports[0].protocol != rt->protocol) {
563 err = AVERROR_INVALIDDATA;
567 rt->protocol = reply->transports[0].protocol;
570 /* close RTP connection if not choosen */
571 if (reply->transports[0].protocol != RTSP_PROTOCOL_RTP_UDP &&
572 (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP))) {
573 av_close_input_file(rtsp_st->ic);
577 switch(reply->transports[0].protocol) {
578 case RTSP_PROTOCOL_RTP_TCP:
580 if (av_open_input_file(&rtsp_st->ic, "null", fmt, 0, NULL) < 0) {
581 err = AVERROR_INVALIDDATA;
584 rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
585 rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
588 case RTSP_PROTOCOL_RTP_UDP:
592 /* XXX: also use address if specified */
593 snprintf(url, sizeof(url), "rtp://%s:%d",
594 host, reply->transports[0].server_port_min);
595 if (rtp_set_remote_url(url_fileno(&rtsp_st->ic->pb), url) < 0) {
596 err = AVERROR_INVALIDDATA;
601 case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
607 ttl = reply->transports[0].ttl;
610 snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
612 reply->transports[0].server_port_min,
614 if (av_open_input_file(&rtsp_st->ic, url, fmt, 0, NULL) < 0) {
615 err = AVERROR_INVALIDDATA;
623 /* use callback if available to extend setup */
624 if (ff_rtsp_callback) {
625 if (ff_rtsp_callback(RTSP_ACTION_CLIENT_SETUP, rt->session_id,
626 NULL, 0, rt->last_reply) < 0) {
627 err = AVERROR_INVALIDDATA;
633 snprintf(cmd, sizeof(cmd),
634 "PLAY %s RTSP/1.0\n",
636 rtsp_send_cmd(s, cmd, reply, NULL);
637 if (reply->status_code != RTSP_STATUS_OK) {
638 err = AVERROR_INVALIDDATA;
642 /* open TCP with bufferized input */
643 if (rt->protocol == RTSP_PROTOCOL_RTP_TCP) {
644 if (url_fdopen(&rt->rtsp_gb, rt->rtsp_hd) < 0) {
652 for(i=0;i<s->nb_streams;i++) {
654 rtsp_st = st->priv_data;
657 av_close_input_file(rtsp_st->ic);
662 url_close(rt->rtsp_hd);
666 static int tcp_read_packet(AVFormatContext *s,
669 RTSPState *rt = s->priv_data;
670 ByteIOContext *rtsp_gb = &rt->rtsp_gb;
671 int c, id, len, i, ret;
674 char buf[RTP_MAX_PACKET_LENGTH];
678 c = url_fgetc(rtsp_gb);
684 id = get_byte(rtsp_gb);
685 len = get_be16(rtsp_gb);
686 if (len > RTP_MAX_PACKET_LENGTH || len < 12)
689 get_buffer(rtsp_gb, buf, len);
691 /* find the matching stream */
692 for(i = 0; i < s->nb_streams; i++) {
694 rtsp_st = st->priv_data;
695 if (i >= rtsp_st->interleaved_min &&
696 i <= rtsp_st->interleaved_max)
701 ret = rtp_parse_packet(rtsp_st->ic, pkt, buf, len);
704 pkt->stream_index = i;
708 /* NOTE: output one packet at a time. May need to add a small fifo */
709 static int udp_read_packet(AVFormatContext *s,
716 int fd1, fd2, fd_max, n, i, ret;
717 char buf[RTP_MAX_PACKET_LENGTH];
725 for(i = 0; i < s->nb_streams; i++) {
727 rtsp_st = st->priv_data;
729 /* currently, we cannot probe RTCP handle because of blocking restrictions */
730 rtp_get_file_handles(url_fileno(&ic->pb), &fd1, &fd2);
735 /* XXX: also add proper API to abort */
738 n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
740 for(i = 0; i < s->nb_streams; i++) {
742 rtsp_st = st->priv_data;
744 rtp_get_file_handles(url_fileno(&ic->pb), &fd1, &fd2);
745 if (FD_ISSET(fd1, &rfds)) {
746 ret = url_read(url_fileno(&ic->pb), buf, sizeof(buf));
748 rtp_parse_packet(ic, pkt, buf, ret) == 0) {
749 pkt->stream_index = i;
758 static int rtsp_read_packet(AVFormatContext *s,
761 RTSPState *rt = s->priv_data;
764 switch(rt->protocol) {
766 case RTSP_PROTOCOL_RTP_TCP:
767 ret = tcp_read_packet(s, pkt);
769 case RTSP_PROTOCOL_RTP_UDP:
770 ret = udp_read_packet(s, pkt);
776 static int rtsp_read_close(AVFormatContext *s)
778 RTSPState *rt = s->priv_data;
781 RTSPHeader reply1, *reply = &reply1;
785 /* NOTE: it is valid to flush the buffer here */
786 if (rt->protocol == RTSP_PROTOCOL_RTP_TCP) {
787 url_fclose(&rt->rtsp_gb);
790 snprintf(cmd, sizeof(cmd),
791 "TEARDOWN %s RTSP/1.0\n",
793 rtsp_send_cmd(s, cmd, reply, NULL);
795 if (ff_rtsp_callback) {
796 ff_rtsp_callback(RTSP_ACTION_CLIENT_TEARDOWN, rt->session_id,
800 for(i=0;i<s->nb_streams;i++) {
802 rtsp_st = st->priv_data;
805 av_close_input_file(rtsp_st->ic);
809 url_close(rt->rtsp_hd);
813 static AVInputFormat rtsp_demux = {
824 /* dummy redirector format (used directly in av_open_input_file now) */
825 static int redir_probe(AVProbeData *pd)
829 while (redir_isspace(*p))
831 if (strstart(p, "http://", NULL) ||
832 strstart(p, "rtsp://", NULL))
833 return AVPROBE_SCORE_MAX;
837 /* called from utils.c */
838 int redir_open(AVFormatContext **ic_ptr, ByteIOContext *f)
842 AVFormatContext *ic = NULL;
844 /* parse each URL and try to open it */
846 while (c != URL_EOF) {
849 if (!redir_isspace(c))
858 if (c == URL_EOF || redir_isspace(c))
860 if ((q - buf) < sizeof(buf) - 1)
865 //printf("URL='%s'\n", buf);
866 /* try to open the media file */
867 if (av_open_input_file(&ic, buf, NULL, 0, NULL) == 0)
877 AVInputFormat redir_demux = {
889 av_register_input_format(&rtsp_demux);
890 av_register_input_format(&redir_demux);