http: Set http_code to 200 when doing a POST
[libav.git] / libavformat / rtspenc.c
CommitLineData
6f5a3d0a
MS
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"
648e41b3 30#include "internal.h"
b7dc88fc 31#include <libavutil/intreadwrite.h>
6f5a3d0a
MS
32
33static 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),
6f5a3d0a 40 "Range: npt=%0.3f-\r\n",
6f5a3d0a 41 (double) 0);
b17d11c6 42 ff_rtsp_send_cmd(s, "RECORD", rt->control_uri, cmd, reply, NULL);
6f5a3d0a
MS
43 if (reply->status_code != RTSP_STATUS_OK)
44 return -1;
45 rt->state = RTSP_STATE_STREAMING;
46 return 0;
47}
48
49static int rtsp_write_header(AVFormatContext *s)
50{
51 RTSPState *rt = s->priv_data;
52 int ret;
53
3307e6ea 54 ret = ff_rtsp_connect(s);
6f5a3d0a
MS
55 if (ret)
56 return ret;
57
58 if (rtsp_write_record(s) < 0) {
3307e6ea 59 ff_rtsp_close_streams(s);
6f5a3d0a
MS
60 url_close(rt->rtsp_hd);
61 return AVERROR_INVALIDDATA;
62 }
63 return 0;
64}
65
b7dc88fc
MS
66static int tcp_write_packet(AVFormatContext *s, RTSPStream *rtsp_st)
67{
68 RTSPState *rt = s->priv_data;
69 AVFormatContext *rtpctx = rtsp_st->transport_priv;
70 uint8_t *buf, *ptr;
71 int size;
72 uint8_t interleave_header[4];
73
74 size = url_close_dyn_buf(rtpctx->pb, &buf);
75 ptr = buf;
76 while (size > 4) {
77 uint32_t packet_len = AV_RB32(ptr);
78 int id;
79 ptr += 4;
80 size -= 4;
81 if (packet_len > size || packet_len < 2)
82 break;
83 if (ptr[1] >= 200 && ptr[1] <= 204)
84 id = rtsp_st->interleaved_max; /* RTCP */
85 else
86 id = rtsp_st->interleaved_min; /* RTP */
87 interleave_header[0] = '$';
88 interleave_header[1] = id;
89 AV_WB16(interleave_header + 2, packet_len);
90 url_write(rt->rtsp_hd, interleave_header, 4);
91 url_write(rt->rtsp_hd, ptr, packet_len);
92 ptr += packet_len;
93 size -= packet_len;
94 }
95 av_free(buf);
96 url_open_dyn_packet_buf(&rtpctx->pb, RTSP_TCP_MAX_PACKET_SIZE);
97 return 0;
98}
99
6f5a3d0a
MS
100static int rtsp_write_packet(AVFormatContext *s, AVPacket *pkt)
101{
102 RTSPState *rt = s->priv_data;
103 RTSPStream *rtsp_st;
104 fd_set rfds;
105 int n, tcp_fd;
106 struct timeval tv;
107 AVFormatContext *rtpctx;
ad2ae6db 108 int ret;
6f5a3d0a 109
6f5a3d0a 110 tcp_fd = url_get_file_handle(rt->rtsp_hd);
6f5a3d0a 111
ad2ae6db
MS
112 while (1) {
113 FD_ZERO(&rfds);
114 FD_SET(tcp_fd, &rfds);
db128802
MS
115 tv.tv_sec = 0;
116 tv.tv_usec = 0;
117 n = select(tcp_fd + 1, &rfds, NULL, NULL, &tv);
ad2ae6db
MS
118 if (n <= 0)
119 break;
6f5a3d0a
MS
120 if (FD_ISSET(tcp_fd, &rfds)) {
121 RTSPMessageHeader reply;
122
ad2ae6db
MS
123 /* Don't let ff_rtsp_read_reply handle interleaved packets,
124 * since it would block and wait for an RTSP reply on the socket
125 * (which may not be coming any time soon) if it handles
126 * interleaved packets internally. */
127 ret = ff_rtsp_read_reply(s, &reply, NULL, 1);
128 if (ret < 0)
6f5a3d0a 129 return AVERROR(EPIPE);
ad2ae6db
MS
130 if (ret == 1)
131 ff_rtsp_skip_packet(s);
6f5a3d0a
MS
132 /* XXX: parse message */
133 if (rt->state != RTSP_STATE_STREAMING)
134 return AVERROR(EPIPE);
135 }
136 }
137
138 if (pkt->stream_index < 0 || pkt->stream_index >= rt->nb_rtsp_streams)
139 return AVERROR_INVALIDDATA;
140 rtsp_st = rt->rtsp_streams[pkt->stream_index];
141 rtpctx = rtsp_st->transport_priv;
142
648e41b3
MS
143 ret = ff_write_chained(rtpctx, 0, pkt, s);
144 /* ff_write_chained does all the RTP packetization. If using TCP as
b7dc88fc
MS
145 * transport, rtpctx->pb is only a dyn_packet_buf that queues up the
146 * packets, so we need to send them out on the TCP connection separately.
147 */
148 if (!ret && rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP)
149 ret = tcp_write_packet(s, rtsp_st);
150 return ret;
6f5a3d0a
MS
151}
152
153static int rtsp_write_close(AVFormatContext *s)
154{
155 RTSPState *rt = s->priv_data;
6f5a3d0a 156
b17d11c6 157 ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
6f5a3d0a 158
3307e6ea 159 ff_rtsp_close_streams(s);
6f5a3d0a 160 url_close(rt->rtsp_hd);
57b5555c 161 ff_network_close();
6f5a3d0a
MS
162 return 0;
163}
164
165AVOutputFormat rtsp_muxer = {
166 "rtsp",
167 NULL_IF_CONFIG_SMALL("RTSP output format"),
168 NULL,
169 NULL,
170 sizeof(RTSPState),
38d4a217
MS
171 CODEC_ID_AAC,
172 CODEC_ID_MPEG4,
6f5a3d0a
MS
173 rtsp_write_header,
174 rtsp_write_packet,
175 rtsp_write_close,
176 .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
177};
178