Statistics
| Branch: | Revision:

ffmpeg / libavformat / rtspenc.c @ 403ee835

History | View | Annotate | Download (7.58 KB)

1
/*
2
 * RTSP muxer
3
 * Copyright (c) 2010 Martin Storsjo
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav 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
 * Libav 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 Libav; 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_POLL_H
26
#include <poll.h>
27
#endif
28
#include "network.h"
29
#include "os_support.h"
30
#include "rtsp.h"
31
#include "internal.h"
32
#include "avio_internal.h"
33
#include "libavutil/intreadwrite.h"
34
#include "libavutil/avstring.h"
35

    
36
#define SDP_MAX_SIZE 16384
37

    
38
int ff_rtsp_setup_output_streams(AVFormatContext *s, const char *addr)
39
{
40
    RTSPState *rt = s->priv_data;
41
    RTSPMessageHeader reply1, *reply = &reply1;
42
    int i;
43
    char *sdp;
44
    AVFormatContext sdp_ctx, *ctx_array[1];
45

    
46
    s->start_time_realtime = av_gettime();
47

    
48
    /* Announce the stream */
49
    sdp = av_mallocz(SDP_MAX_SIZE);
50
    if (sdp == NULL)
51
        return AVERROR(ENOMEM);
52
    /* We create the SDP based on the RTSP AVFormatContext where we
53
     * aren't allowed to change the filename field. (We create the SDP
54
     * based on the RTSP context since the contexts for the RTP streams
55
     * don't exist yet.) In order to specify a custom URL with the actual
56
     * peer IP instead of the originally specified hostname, we create
57
     * a temporary copy of the AVFormatContext, where the custom URL is set.
58
     *
59
     * FIXME: Create the SDP without copying the AVFormatContext.
60
     * This either requires setting up the RTP stream AVFormatContexts
61
     * already here (complicating things immensely) or getting a more
62
     * flexible SDP creation interface.
63
     */
64
    sdp_ctx = *s;
65
    ff_url_join(sdp_ctx.filename, sizeof(sdp_ctx.filename),
66
                "rtsp", NULL, addr, -1, NULL);
67
    ctx_array[0] = &sdp_ctx;
68
    if (avf_sdp_create(ctx_array, 1, sdp, SDP_MAX_SIZE)) {
69
        av_free(sdp);
70
        return AVERROR_INVALIDDATA;
71
    }
72
    av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sdp);
73
    ff_rtsp_send_cmd_with_content(s, "ANNOUNCE", rt->control_uri,
74
                                  "Content-Type: application/sdp\r\n",
75
                                  reply, NULL, sdp, strlen(sdp));
76
    av_free(sdp);
77
    if (reply->status_code != RTSP_STATUS_OK)
78
        return AVERROR_INVALIDDATA;
79

    
80
    /* Set up the RTSPStreams for each AVStream */
81
    for (i = 0; i < s->nb_streams; i++) {
82
        RTSPStream *rtsp_st;
83

    
84
        rtsp_st = av_mallocz(sizeof(RTSPStream));
85
        if (!rtsp_st)
86
            return AVERROR(ENOMEM);
87
        dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
88

    
89
        rtsp_st->stream_index = i;
90

    
91
        av_strlcpy(rtsp_st->control_url, rt->control_uri, sizeof(rtsp_st->control_url));
92
        /* Note, this must match the relative uri set in the sdp content */
93
        av_strlcatf(rtsp_st->control_url, sizeof(rtsp_st->control_url),
94
                    "/streamid=%d", i);
95
    }
96

    
97
    return 0;
98
}
99

    
100
static int rtsp_write_record(AVFormatContext *s)
101
{
102
    RTSPState *rt = s->priv_data;
103
    RTSPMessageHeader reply1, *reply = &reply1;
104
    char cmd[1024];
105

    
106
    snprintf(cmd, sizeof(cmd),
107
             "Range: npt=0.000-\r\n");
108
    ff_rtsp_send_cmd(s, "RECORD", rt->control_uri, cmd, reply, NULL);
109
    if (reply->status_code != RTSP_STATUS_OK)
110
        return -1;
111
    rt->state = RTSP_STATE_STREAMING;
112
    return 0;
113
}
114

    
115
static int rtsp_write_header(AVFormatContext *s)
116
{
117
    int ret;
118

    
119
    ret = ff_rtsp_connect(s);
120
    if (ret)
121
        return ret;
122

    
123
    if (rtsp_write_record(s) < 0) {
124
        ff_rtsp_close_streams(s);
125
        ff_rtsp_close_connections(s);
126
        return AVERROR_INVALIDDATA;
127
    }
128
    return 0;
129
}
130

    
131
static int tcp_write_packet(AVFormatContext *s, RTSPStream *rtsp_st)
132
{
133
    RTSPState *rt = s->priv_data;
134
    AVFormatContext *rtpctx = rtsp_st->transport_priv;
135
    uint8_t *buf, *ptr;
136
    int size;
137
    uint8_t *interleave_header, *interleaved_packet;
138

    
139
    size = avio_close_dyn_buf(rtpctx->pb, &buf);
140
    ptr = buf;
141
    while (size > 4) {
142
        uint32_t packet_len = AV_RB32(ptr);
143
        int id;
144
        /* The interleaving header is exactly 4 bytes, which happens to be
145
         * the same size as the packet length header from
146
         * ffio_open_dyn_packet_buf. So by writing the interleaving header
147
         * over these bytes, we get a consecutive interleaved packet
148
         * that can be written in one call. */
149
        interleaved_packet = interleave_header = ptr;
150
        ptr += 4;
151
        size -= 4;
152
        if (packet_len > size || packet_len < 2)
153
            break;
154
        if (ptr[1] >= RTCP_SR && ptr[1] <= RTCP_APP)
155
            id = rtsp_st->interleaved_max; /* RTCP */
156
        else
157
            id = rtsp_st->interleaved_min; /* RTP */
158
        interleave_header[0] = '$';
159
        interleave_header[1] = id;
160
        AV_WB16(interleave_header + 2, packet_len);
161
        url_write(rt->rtsp_hd_out, interleaved_packet, 4 + packet_len);
162
        ptr += packet_len;
163
        size -= packet_len;
164
    }
165
    av_free(buf);
166
    ffio_open_dyn_packet_buf(&rtpctx->pb, RTSP_TCP_MAX_PACKET_SIZE);
167
    return 0;
168
}
169

    
170
static int rtsp_write_packet(AVFormatContext *s, AVPacket *pkt)
171
{
172
    RTSPState *rt = s->priv_data;
173
    RTSPStream *rtsp_st;
174
    int n;
175
    struct pollfd p = {url_get_file_handle(rt->rtsp_hd), POLLIN, 0};
176
    AVFormatContext *rtpctx;
177
    int ret;
178

    
179
    while (1) {
180
        n = poll(&p, 1, 0);
181
        if (n <= 0)
182
            break;
183
        if (p.revents & POLLIN) {
184
            RTSPMessageHeader reply;
185

    
186
            /* Don't let ff_rtsp_read_reply handle interleaved packets,
187
             * since it would block and wait for an RTSP reply on the socket
188
             * (which may not be coming any time soon) if it handles
189
             * interleaved packets internally. */
190
            ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
191
            if (ret < 0)
192
                return AVERROR(EPIPE);
193
            if (ret == 1)
194
                ff_rtsp_skip_packet(s);
195
            /* XXX: parse message */
196
            if (rt->state != RTSP_STATE_STREAMING)
197
                return AVERROR(EPIPE);
198
        }
199
    }
200

    
201
    if (pkt->stream_index < 0 || pkt->stream_index >= rt->nb_rtsp_streams)
202
        return AVERROR_INVALIDDATA;
203
    rtsp_st = rt->rtsp_streams[pkt->stream_index];
204
    rtpctx = rtsp_st->transport_priv;
205

    
206
    ret = ff_write_chained(rtpctx, 0, pkt, s);
207
    /* ff_write_chained does all the RTP packetization. If using TCP as
208
     * transport, rtpctx->pb is only a dyn_packet_buf that queues up the
209
     * packets, so we need to send them out on the TCP connection separately.
210
     */
211
    if (!ret && rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP)
212
        ret = tcp_write_packet(s, rtsp_st);
213
    return ret;
214
}
215

    
216
static int rtsp_write_close(AVFormatContext *s)
217
{
218
    RTSPState *rt = s->priv_data;
219

    
220
    ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
221

    
222
    ff_rtsp_close_streams(s);
223
    ff_rtsp_close_connections(s);
224
    ff_network_close();
225
    return 0;
226
}
227

    
228
AVOutputFormat ff_rtsp_muxer = {
229
    "rtsp",
230
    NULL_IF_CONFIG_SMALL("RTSP output format"),
231
    NULL,
232
    NULL,
233
    sizeof(RTSPState),
234
    CODEC_ID_AAC,
235
    CODEC_ID_MPEG4,
236
    rtsp_write_header,
237
    rtsp_write_packet,
238
    rtsp_write_close,
239
    .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
240
};
241