Mercurial > libavformat.hg
annotate rtspenc.c @ 5881:fd67c6b9f915 libavformat
Fix warnings in NUT demuxer:
libavformat/nutdec.c: In function ¡Æread_seek¡Ç:
libavformat/nutdec.c:862: warning: passing argument 4 of ¡Æav_tree_find¡Ç from incompatible pointer type
./libavutil/tree.h:44: note: expected ¡Ævoid **¡Ç but argument is of type ¡Æstruct Syncpoint **¡Ç
libavformat/nutdec.c:871: warning: passing argument 4 of ¡Æav_tree_find¡Ç from incompatible pointer type
./libavutil/tree.h:44: note: expected ¡Ævoid **¡Ç but argument is of type ¡Æstruct Syncpoint **¡Ç
author | vitor |
---|---|
date | Wed, 24 Mar 2010 22:44:06 +0000 |
parents | 0ff0826b6c3d |
children | 4ddbc14bc768 |
rev | line source |
---|---|
5695 | 1 /* |
2 * RTSP muxer | |
3 * Copyright (c) 2010 Martin Storsjo | |
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 | |
22 #include "avformat.h" | |
23 | |
24 #include <sys/time.h> | |
25 #if HAVE_SYS_SELECT_H | |
26 #include <sys/select.h> | |
27 #endif | |
28 #include "network.h" | |
29 #include "rtsp.h" | |
5864
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
30 #include <libavutil/intreadwrite.h> |
5695 | 31 |
32 static int rtsp_write_record(AVFormatContext *s) | |
33 { | |
34 RTSPState *rt = s->priv_data; | |
35 RTSPMessageHeader reply1, *reply = &reply1; | |
36 char cmd[1024]; | |
37 | |
38 snprintf(cmd, sizeof(cmd), | |
39 "RECORD %s RTSP/1.0\r\n" | |
40 "Range: npt=%0.3f-\r\n", | |
5800
4410bccf6a8a
Use rt->control_uri consequently instead of s->filename in all RTSP commands
mstorsjo
parents:
5756
diff
changeset
|
41 rt->control_uri, |
5695 | 42 (double) 0); |
5697 | 43 ff_rtsp_send_cmd(s, cmd, reply, NULL); |
5695 | 44 if (reply->status_code != RTSP_STATUS_OK) |
45 return -1; | |
46 rt->state = RTSP_STATE_STREAMING; | |
47 return 0; | |
48 } | |
49 | |
50 static int rtsp_write_header(AVFormatContext *s) | |
51 { | |
52 RTSPState *rt = s->priv_data; | |
53 int ret; | |
54 | |
5697 | 55 ret = ff_rtsp_connect(s); |
5695 | 56 if (ret) |
57 return ret; | |
58 | |
59 if (rtsp_write_record(s) < 0) { | |
5697 | 60 ff_rtsp_close_streams(s); |
5695 | 61 url_close(rt->rtsp_hd); |
62 return AVERROR_INVALIDDATA; | |
63 } | |
64 return 0; | |
65 } | |
66 | |
5864
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
67 static int tcp_write_packet(AVFormatContext *s, RTSPStream *rtsp_st) |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
68 { |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
69 RTSPState *rt = s->priv_data; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
70 AVFormatContext *rtpctx = rtsp_st->transport_priv; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
71 uint8_t *buf, *ptr; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
72 int size; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
73 uint8_t interleave_header[4]; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
74 |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
75 size = url_close_dyn_buf(rtpctx->pb, &buf); |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
76 ptr = buf; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
77 while (size > 4) { |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
78 uint32_t packet_len = AV_RB32(ptr); |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
79 int id; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
80 ptr += 4; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
81 size -= 4; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
82 if (packet_len > size || packet_len < 2) |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
83 break; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
84 if (ptr[1] >= 200 && ptr[1] <= 204) |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
85 id = rtsp_st->interleaved_max; /* RTCP */ |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
86 else |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
87 id = rtsp_st->interleaved_min; /* RTP */ |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
88 interleave_header[0] = '$'; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
89 interleave_header[1] = id; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
90 AV_WB16(interleave_header + 2, packet_len); |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
91 url_write(rt->rtsp_hd, interleave_header, 4); |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
92 url_write(rt->rtsp_hd, ptr, packet_len); |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
93 ptr += packet_len; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
94 size -= packet_len; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
95 } |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
96 av_free(buf); |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
97 url_open_dyn_packet_buf(&rtpctx->pb, RTSP_TCP_MAX_PACKET_SIZE); |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
98 return 0; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
99 } |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
100 |
5695 | 101 static int rtsp_write_packet(AVFormatContext *s, AVPacket *pkt) |
102 { | |
103 RTSPState *rt = s->priv_data; | |
104 RTSPStream *rtsp_st; | |
105 fd_set rfds; | |
106 int n, tcp_fd; | |
107 struct timeval tv; | |
108 AVFormatContext *rtpctx; | |
5721
82b0dc4f052a
RTSP muxer: Use a local copy of the AVPacket for sending to the chained muxer
mstorsjo
parents:
5697
diff
changeset
|
109 AVPacket local_pkt; |
5846
6821f3887d0e
Don't let ff_rtsp_read_reply skip interleaved RTP/TCP packets in rtsp_write_packet.
mstorsjo
parents:
5800
diff
changeset
|
110 int ret; |
5695 | 111 |
112 tcp_fd = url_get_file_handle(rt->rtsp_hd); | |
113 | |
5846
6821f3887d0e
Don't let ff_rtsp_read_reply skip interleaved RTP/TCP packets in rtsp_write_packet.
mstorsjo
parents:
5800
diff
changeset
|
114 while (1) { |
6821f3887d0e
Don't let ff_rtsp_read_reply skip interleaved RTP/TCP packets in rtsp_write_packet.
mstorsjo
parents:
5800
diff
changeset
|
115 FD_ZERO(&rfds); |
6821f3887d0e
Don't let ff_rtsp_read_reply skip interleaved RTP/TCP packets in rtsp_write_packet.
mstorsjo
parents:
5800
diff
changeset
|
116 FD_SET(tcp_fd, &rfds); |
5847 | 117 tv.tv_sec = 0; |
118 tv.tv_usec = 0; | |
119 n = select(tcp_fd + 1, &rfds, NULL, NULL, &tv); | |
5846
6821f3887d0e
Don't let ff_rtsp_read_reply skip interleaved RTP/TCP packets in rtsp_write_packet.
mstorsjo
parents:
5800
diff
changeset
|
120 if (n <= 0) |
6821f3887d0e
Don't let ff_rtsp_read_reply skip interleaved RTP/TCP packets in rtsp_write_packet.
mstorsjo
parents:
5800
diff
changeset
|
121 break; |
5695 | 122 if (FD_ISSET(tcp_fd, &rfds)) { |
123 RTSPMessageHeader reply; | |
124 | |
5846
6821f3887d0e
Don't let ff_rtsp_read_reply skip interleaved RTP/TCP packets in rtsp_write_packet.
mstorsjo
parents:
5800
diff
changeset
|
125 /* Don't let ff_rtsp_read_reply handle interleaved packets, |
6821f3887d0e
Don't let ff_rtsp_read_reply skip interleaved RTP/TCP packets in rtsp_write_packet.
mstorsjo
parents:
5800
diff
changeset
|
126 * since it would block and wait for an RTSP reply on the socket |
6821f3887d0e
Don't let ff_rtsp_read_reply skip interleaved RTP/TCP packets in rtsp_write_packet.
mstorsjo
parents:
5800
diff
changeset
|
127 * (which may not be coming any time soon) if it handles |
6821f3887d0e
Don't let ff_rtsp_read_reply skip interleaved RTP/TCP packets in rtsp_write_packet.
mstorsjo
parents:
5800
diff
changeset
|
128 * interleaved packets internally. */ |
6821f3887d0e
Don't let ff_rtsp_read_reply skip interleaved RTP/TCP packets in rtsp_write_packet.
mstorsjo
parents:
5800
diff
changeset
|
129 ret = ff_rtsp_read_reply(s, &reply, NULL, 1); |
6821f3887d0e
Don't let ff_rtsp_read_reply skip interleaved RTP/TCP packets in rtsp_write_packet.
mstorsjo
parents:
5800
diff
changeset
|
130 if (ret < 0) |
5695 | 131 return AVERROR(EPIPE); |
5846
6821f3887d0e
Don't let ff_rtsp_read_reply skip interleaved RTP/TCP packets in rtsp_write_packet.
mstorsjo
parents:
5800
diff
changeset
|
132 if (ret == 1) |
6821f3887d0e
Don't let ff_rtsp_read_reply skip interleaved RTP/TCP packets in rtsp_write_packet.
mstorsjo
parents:
5800
diff
changeset
|
133 ff_rtsp_skip_packet(s); |
5695 | 134 /* XXX: parse message */ |
135 if (rt->state != RTSP_STATE_STREAMING) | |
136 return AVERROR(EPIPE); | |
137 } | |
138 } | |
139 | |
140 if (pkt->stream_index < 0 || pkt->stream_index >= rt->nb_rtsp_streams) | |
141 return AVERROR_INVALIDDATA; | |
142 rtsp_st = rt->rtsp_streams[pkt->stream_index]; | |
143 rtpctx = rtsp_st->transport_priv; | |
144 | |
5721
82b0dc4f052a
RTSP muxer: Use a local copy of the AVPacket for sending to the chained muxer
mstorsjo
parents:
5697
diff
changeset
|
145 /* Use a local packet for writing to the chained muxer, otherwise |
82b0dc4f052a
RTSP muxer: Use a local copy of the AVPacket for sending to the chained muxer
mstorsjo
parents:
5697
diff
changeset
|
146 * the internal stream_index = 0 becomes visible to the muxer user. */ |
82b0dc4f052a
RTSP muxer: Use a local copy of the AVPacket for sending to the chained muxer
mstorsjo
parents:
5697
diff
changeset
|
147 local_pkt = *pkt; |
82b0dc4f052a
RTSP muxer: Use a local copy of the AVPacket for sending to the chained muxer
mstorsjo
parents:
5697
diff
changeset
|
148 local_pkt.stream_index = 0; |
5864
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
149 ret = av_write_frame(rtpctx, &local_pkt); |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
150 /* av_write_frame does all the RTP packetization. If using TCP as |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
151 * transport, rtpctx->pb is only a dyn_packet_buf that queues up the |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
152 * packets, so we need to send them out on the TCP connection separately. |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
153 */ |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
154 if (!ret && rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
155 ret = tcp_write_packet(s, rtsp_st); |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
156 return ret; |
5695 | 157 } |
158 | |
159 static int rtsp_write_close(AVFormatContext *s) | |
160 { | |
161 RTSPState *rt = s->priv_data; | |
162 char cmd[1024]; | |
163 | |
164 snprintf(cmd, sizeof(cmd), | |
165 "TEARDOWN %s RTSP/1.0\r\n", | |
5800
4410bccf6a8a
Use rt->control_uri consequently instead of s->filename in all RTSP commands
mstorsjo
parents:
5756
diff
changeset
|
166 rt->control_uri); |
5697 | 167 ff_rtsp_send_cmd_async(s, cmd); |
5695 | 168 |
5697 | 169 ff_rtsp_close_streams(s); |
5695 | 170 url_close(rt->rtsp_hd); |
5756
7c7fe75728dd
Use ff_url_join for assembling URLs, instead of snprintf
mstorsjo
parents:
5721
diff
changeset
|
171 ff_network_close(); |
5695 | 172 return 0; |
173 } | |
174 | |
175 AVOutputFormat rtsp_muxer = { | |
176 "rtsp", | |
177 NULL_IF_CONFIG_SMALL("RTSP output format"), | |
178 NULL, | |
179 NULL, | |
180 sizeof(RTSPState), | |
181 CODEC_ID_PCM_MULAW, | |
182 CODEC_ID_NONE, | |
183 rtsp_write_header, | |
184 rtsp_write_packet, | |
185 rtsp_write_close, | |
186 .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER, | |
187 }; | |
188 |