Mercurial > libavformat.hg
annotate rtspenc.c @ 6110:d3cfb2687491 libavformat
Reindent
author | mstorsjo |
---|---|
date | Tue, 08 Jun 2010 11:58:22 +0000 |
parents | e6d6b3826eb2 |
children | fcbafbe31721 |
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" | |
6025
fd7fc7d79630
Take ff_write_chained in use in the mov rtp hinter and in the rtsp muxer
mstorsjo
parents:
5888
diff
changeset
|
30 #include "internal.h" |
5864
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
31 #include <libavutil/intreadwrite.h> |
5695 | 32 |
33 static int rtsp_write_record(AVFormatContext *s) | |
34 { | |
35 RTSPState *rt = s->priv_data; | |
36 RTSPMessageHeader reply1, *reply = &reply1; | |
37 char cmd[1024]; | |
38 | |
39 snprintf(cmd, sizeof(cmd), | |
40 "Range: npt=%0.3f-\r\n", | |
41 (double) 0); | |
5888
4ddbc14bc768
Add separate method/url parameters to the rtsp_send_cmd functions
mstorsjo
parents:
5864
diff
changeset
|
42 ff_rtsp_send_cmd(s, "RECORD", rt->control_uri, cmd, reply, NULL); |
5695 | 43 if (reply->status_code != RTSP_STATUS_OK) |
44 return -1; | |
45 rt->state = RTSP_STATE_STREAMING; | |
46 return 0; | |
47 } | |
48 | |
49 static int rtsp_write_header(AVFormatContext *s) | |
50 { | |
51 int ret; | |
52 | |
5697 | 53 ret = ff_rtsp_connect(s); |
5695 | 54 if (ret) |
55 return ret; | |
56 | |
57 if (rtsp_write_record(s) < 0) { | |
5697 | 58 ff_rtsp_close_streams(s); |
6093
e688babd14df
RTSP: Add a second URLContext for outgoing messages
mstorsjo
parents:
6081
diff
changeset
|
59 ff_rtsp_close_connections(s); |
5695 | 60 return AVERROR_INVALIDDATA; |
61 } | |
62 return 0; | |
63 } | |
64 | |
5864
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
65 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
|
66 { |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
67 RTSPState *rt = s->priv_data; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
68 AVFormatContext *rtpctx = rtsp_st->transport_priv; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
69 uint8_t *buf, *ptr; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
70 int size; |
6081
d21ccbea3294
rtspenc: Write the interleaving header in the same buffer as the packet data
mstorsjo
parents:
6026
diff
changeset
|
71 uint8_t *interleave_header, *interleaved_packet; |
5864
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
72 |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
73 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
|
74 ptr = buf; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
75 while (size > 4) { |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
76 uint32_t packet_len = AV_RB32(ptr); |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
77 int id; |
6081
d21ccbea3294
rtspenc: Write the interleaving header in the same buffer as the packet data
mstorsjo
parents:
6026
diff
changeset
|
78 /* The interleaving header is exactly 4 bytes, which happens to be |
d21ccbea3294
rtspenc: Write the interleaving header in the same buffer as the packet data
mstorsjo
parents:
6026
diff
changeset
|
79 * the same size as the packet length header from |
d21ccbea3294
rtspenc: Write the interleaving header in the same buffer as the packet data
mstorsjo
parents:
6026
diff
changeset
|
80 * url_open_dyn_packet_buf. So by writing the interleaving header |
d21ccbea3294
rtspenc: Write the interleaving header in the same buffer as the packet data
mstorsjo
parents:
6026
diff
changeset
|
81 * over these bytes, we get a consecutive interleaved packet |
d21ccbea3294
rtspenc: Write the interleaving header in the same buffer as the packet data
mstorsjo
parents:
6026
diff
changeset
|
82 * that can be written in one call. */ |
d21ccbea3294
rtspenc: Write the interleaving header in the same buffer as the packet data
mstorsjo
parents:
6026
diff
changeset
|
83 interleaved_packet = interleave_header = ptr; |
5864
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
84 ptr += 4; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
85 size -= 4; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
86 if (packet_len > size || packet_len < 2) |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
87 break; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
88 if (ptr[1] >= 200 && ptr[1] <= 204) |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
89 id = rtsp_st->interleaved_max; /* RTCP */ |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
90 else |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
91 id = rtsp_st->interleaved_min; /* RTP */ |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
92 interleave_header[0] = '$'; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
93 interleave_header[1] = id; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
94 AV_WB16(interleave_header + 2, packet_len); |
6081
d21ccbea3294
rtspenc: Write the interleaving header in the same buffer as the packet data
mstorsjo
parents:
6026
diff
changeset
|
95 url_write(rt->rtsp_hd, interleaved_packet, 4 + packet_len); |
5864
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
96 ptr += packet_len; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
97 size -= packet_len; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
98 } |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
99 av_free(buf); |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
100 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
|
101 return 0; |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
102 } |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
103 |
5695 | 104 static int rtsp_write_packet(AVFormatContext *s, AVPacket *pkt) |
105 { | |
106 RTSPState *rt = s->priv_data; | |
107 RTSPStream *rtsp_st; | |
108 fd_set rfds; | |
109 int n, tcp_fd; | |
110 struct timeval tv; | |
111 AVFormatContext *rtpctx; | |
5846
6821f3887d0e
Don't let ff_rtsp_read_reply skip interleaved RTP/TCP packets in rtsp_write_packet.
mstorsjo
parents:
5800
diff
changeset
|
112 int ret; |
5695 | 113 |
114 tcp_fd = url_get_file_handle(rt->rtsp_hd); | |
115 | |
5846
6821f3887d0e
Don't let ff_rtsp_read_reply skip interleaved RTP/TCP packets in rtsp_write_packet.
mstorsjo
parents:
5800
diff
changeset
|
116 while (1) { |
6821f3887d0e
Don't let ff_rtsp_read_reply skip interleaved RTP/TCP packets in rtsp_write_packet.
mstorsjo
parents:
5800
diff
changeset
|
117 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
|
118 FD_SET(tcp_fd, &rfds); |
5847 | 119 tv.tv_sec = 0; |
120 tv.tv_usec = 0; | |
121 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
|
122 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
|
123 break; |
5695 | 124 if (FD_ISSET(tcp_fd, &rfds)) { |
125 RTSPMessageHeader reply; | |
126 | |
5846
6821f3887d0e
Don't let ff_rtsp_read_reply skip interleaved RTP/TCP packets in rtsp_write_packet.
mstorsjo
parents:
5800
diff
changeset
|
127 /* 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
|
128 * 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
|
129 * (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
|
130 * 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
|
131 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
|
132 if (ret < 0) |
5695 | 133 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
|
134 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
|
135 ff_rtsp_skip_packet(s); |
5695 | 136 /* XXX: parse message */ |
137 if (rt->state != RTSP_STATE_STREAMING) | |
138 return AVERROR(EPIPE); | |
139 } | |
140 } | |
141 | |
142 if (pkt->stream_index < 0 || pkt->stream_index >= rt->nb_rtsp_streams) | |
143 return AVERROR_INVALIDDATA; | |
144 rtsp_st = rt->rtsp_streams[pkt->stream_index]; | |
145 rtpctx = rtsp_st->transport_priv; | |
146 | |
6025
fd7fc7d79630
Take ff_write_chained in use in the mov rtp hinter and in the rtsp muxer
mstorsjo
parents:
5888
diff
changeset
|
147 ret = ff_write_chained(rtpctx, 0, pkt, s); |
fd7fc7d79630
Take ff_write_chained in use in the mov rtp hinter and in the rtsp muxer
mstorsjo
parents:
5888
diff
changeset
|
148 /* ff_write_chained does all the RTP packetization. If using TCP as |
5864
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
149 * 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
|
150 * 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
|
151 */ |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
152 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
|
153 ret = tcp_write_packet(s, rtsp_st); |
0ff0826b6c3d
Add support for TCP as lower transport in the RTSP muxer
mstorsjo
parents:
5847
diff
changeset
|
154 return ret; |
5695 | 155 } |
156 | |
157 static int rtsp_write_close(AVFormatContext *s) | |
158 { | |
159 RTSPState *rt = s->priv_data; | |
160 | |
5888
4ddbc14bc768
Add separate method/url parameters to the rtsp_send_cmd functions
mstorsjo
parents:
5864
diff
changeset
|
161 ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL); |
5695 | 162 |
5697 | 163 ff_rtsp_close_streams(s); |
6093
e688babd14df
RTSP: Add a second URLContext for outgoing messages
mstorsjo
parents:
6081
diff
changeset
|
164 ff_rtsp_close_connections(s); |
5756
7c7fe75728dd
Use ff_url_join for assembling URLs, instead of snprintf
mstorsjo
parents:
5721
diff
changeset
|
165 ff_network_close(); |
5695 | 166 return 0; |
167 } | |
168 | |
169 AVOutputFormat rtsp_muxer = { | |
170 "rtsp", | |
171 NULL_IF_CONFIG_SMALL("RTSP output format"), | |
172 NULL, | |
173 NULL, | |
174 sizeof(RTSPState), | |
6026
785444452e93
Change the default codecs for the rtsp muxer to mpeg4 and aac
mstorsjo
parents:
6025
diff
changeset
|
175 CODEC_ID_AAC, |
785444452e93
Change the default codecs for the rtsp muxer to mpeg4 and aac
mstorsjo
parents:
6025
diff
changeset
|
176 CODEC_ID_MPEG4, |
5695 | 177 rtsp_write_header, |
178 rtsp_write_packet, | |
179 rtsp_write_close, | |
180 .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER, | |
181 }; | |
182 |