annotate ffmpeg/libavformat/rtspenc.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * RTSP muxer
yading@11 3 * Copyright (c) 2010 Martin Storsjo
yading@11 4 *
yading@11 5 * This file is part of FFmpeg.
yading@11 6 *
yading@11 7 * FFmpeg is free software; you can redistribute it and/or
yading@11 8 * modify it under the terms of the GNU Lesser General Public
yading@11 9 * License as published by the Free Software Foundation; either
yading@11 10 * version 2.1 of the License, or (at your option) any later version.
yading@11 11 *
yading@11 12 * FFmpeg is distributed in the hope that it will be useful,
yading@11 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 15 * Lesser General Public License for more details.
yading@11 16 *
yading@11 17 * You should have received a copy of the GNU Lesser General Public
yading@11 18 * License along with FFmpeg; if not, write to the Free Software
yading@11 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 20 */
yading@11 21
yading@11 22 #include "avformat.h"
yading@11 23
yading@11 24 #if HAVE_POLL_H
yading@11 25 #include <poll.h>
yading@11 26 #endif
yading@11 27 #include "network.h"
yading@11 28 #include "os_support.h"
yading@11 29 #include "rtsp.h"
yading@11 30 #include "internal.h"
yading@11 31 #include "avio_internal.h"
yading@11 32 #include "libavutil/intreadwrite.h"
yading@11 33 #include "libavutil/avstring.h"
yading@11 34 #include "libavutil/time.h"
yading@11 35 #include "url.h"
yading@11 36
yading@11 37 #define SDP_MAX_SIZE 16384
yading@11 38
yading@11 39 static const AVClass rtsp_muxer_class = {
yading@11 40 .class_name = "RTSP muxer",
yading@11 41 .item_name = av_default_item_name,
yading@11 42 .option = ff_rtsp_options,
yading@11 43 .version = LIBAVUTIL_VERSION_INT,
yading@11 44 };
yading@11 45
yading@11 46 int ff_rtsp_setup_output_streams(AVFormatContext *s, const char *addr)
yading@11 47 {
yading@11 48 RTSPState *rt = s->priv_data;
yading@11 49 RTSPMessageHeader reply1, *reply = &reply1;
yading@11 50 int i;
yading@11 51 char *sdp;
yading@11 52 AVFormatContext sdp_ctx, *ctx_array[1];
yading@11 53
yading@11 54 s->start_time_realtime = av_gettime();
yading@11 55
yading@11 56 /* Announce the stream */
yading@11 57 sdp = av_mallocz(SDP_MAX_SIZE);
yading@11 58 if (sdp == NULL)
yading@11 59 return AVERROR(ENOMEM);
yading@11 60 /* We create the SDP based on the RTSP AVFormatContext where we
yading@11 61 * aren't allowed to change the filename field. (We create the SDP
yading@11 62 * based on the RTSP context since the contexts for the RTP streams
yading@11 63 * don't exist yet.) In order to specify a custom URL with the actual
yading@11 64 * peer IP instead of the originally specified hostname, we create
yading@11 65 * a temporary copy of the AVFormatContext, where the custom URL is set.
yading@11 66 *
yading@11 67 * FIXME: Create the SDP without copying the AVFormatContext.
yading@11 68 * This either requires setting up the RTP stream AVFormatContexts
yading@11 69 * already here (complicating things immensely) or getting a more
yading@11 70 * flexible SDP creation interface.
yading@11 71 */
yading@11 72 sdp_ctx = *s;
yading@11 73 ff_url_join(sdp_ctx.filename, sizeof(sdp_ctx.filename),
yading@11 74 "rtsp", NULL, addr, -1, NULL);
yading@11 75 ctx_array[0] = &sdp_ctx;
yading@11 76 if (av_sdp_create(ctx_array, 1, sdp, SDP_MAX_SIZE)) {
yading@11 77 av_free(sdp);
yading@11 78 return AVERROR_INVALIDDATA;
yading@11 79 }
yading@11 80 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sdp);
yading@11 81 ff_rtsp_send_cmd_with_content(s, "ANNOUNCE", rt->control_uri,
yading@11 82 "Content-Type: application/sdp\r\n",
yading@11 83 reply, NULL, sdp, strlen(sdp));
yading@11 84 av_free(sdp);
yading@11 85 if (reply->status_code != RTSP_STATUS_OK)
yading@11 86 return AVERROR_INVALIDDATA;
yading@11 87
yading@11 88 /* Set up the RTSPStreams for each AVStream */
yading@11 89 for (i = 0; i < s->nb_streams; i++) {
yading@11 90 RTSPStream *rtsp_st;
yading@11 91
yading@11 92 rtsp_st = av_mallocz(sizeof(RTSPStream));
yading@11 93 if (!rtsp_st)
yading@11 94 return AVERROR(ENOMEM);
yading@11 95 dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
yading@11 96
yading@11 97 rtsp_st->stream_index = i;
yading@11 98
yading@11 99 av_strlcpy(rtsp_st->control_url, rt->control_uri, sizeof(rtsp_st->control_url));
yading@11 100 /* Note, this must match the relative uri set in the sdp content */
yading@11 101 av_strlcatf(rtsp_st->control_url, sizeof(rtsp_st->control_url),
yading@11 102 "/streamid=%d", i);
yading@11 103 }
yading@11 104
yading@11 105 return 0;
yading@11 106 }
yading@11 107
yading@11 108 static int rtsp_write_record(AVFormatContext *s)
yading@11 109 {
yading@11 110 RTSPState *rt = s->priv_data;
yading@11 111 RTSPMessageHeader reply1, *reply = &reply1;
yading@11 112 char cmd[1024];
yading@11 113
yading@11 114 snprintf(cmd, sizeof(cmd),
yading@11 115 "Range: npt=0.000-\r\n");
yading@11 116 ff_rtsp_send_cmd(s, "RECORD", rt->control_uri, cmd, reply, NULL);
yading@11 117 if (reply->status_code != RTSP_STATUS_OK)
yading@11 118 return -1;
yading@11 119 rt->state = RTSP_STATE_STREAMING;
yading@11 120 return 0;
yading@11 121 }
yading@11 122
yading@11 123 static int rtsp_write_header(AVFormatContext *s)
yading@11 124 {
yading@11 125 int ret;
yading@11 126
yading@11 127 ret = ff_rtsp_connect(s);
yading@11 128 if (ret)
yading@11 129 return ret;
yading@11 130
yading@11 131 if (rtsp_write_record(s) < 0) {
yading@11 132 ff_rtsp_close_streams(s);
yading@11 133 ff_rtsp_close_connections(s);
yading@11 134 return AVERROR_INVALIDDATA;
yading@11 135 }
yading@11 136 return 0;
yading@11 137 }
yading@11 138
yading@11 139 static int tcp_write_packet(AVFormatContext *s, RTSPStream *rtsp_st)
yading@11 140 {
yading@11 141 RTSPState *rt = s->priv_data;
yading@11 142 AVFormatContext *rtpctx = rtsp_st->transport_priv;
yading@11 143 uint8_t *buf, *ptr;
yading@11 144 int size;
yading@11 145 uint8_t *interleave_header, *interleaved_packet;
yading@11 146
yading@11 147 size = avio_close_dyn_buf(rtpctx->pb, &buf);
yading@11 148 ptr = buf;
yading@11 149 while (size > 4) {
yading@11 150 uint32_t packet_len = AV_RB32(ptr);
yading@11 151 int id;
yading@11 152 /* The interleaving header is exactly 4 bytes, which happens to be
yading@11 153 * the same size as the packet length header from
yading@11 154 * ffio_open_dyn_packet_buf. So by writing the interleaving header
yading@11 155 * over these bytes, we get a consecutive interleaved packet
yading@11 156 * that can be written in one call. */
yading@11 157 interleaved_packet = interleave_header = ptr;
yading@11 158 ptr += 4;
yading@11 159 size -= 4;
yading@11 160 if (packet_len > size || packet_len < 2)
yading@11 161 break;
yading@11 162 if (RTP_PT_IS_RTCP(ptr[1]))
yading@11 163 id = rtsp_st->interleaved_max; /* RTCP */
yading@11 164 else
yading@11 165 id = rtsp_st->interleaved_min; /* RTP */
yading@11 166 interleave_header[0] = '$';
yading@11 167 interleave_header[1] = id;
yading@11 168 AV_WB16(interleave_header + 2, packet_len);
yading@11 169 ffurl_write(rt->rtsp_hd_out, interleaved_packet, 4 + packet_len);
yading@11 170 ptr += packet_len;
yading@11 171 size -= packet_len;
yading@11 172 }
yading@11 173 av_free(buf);
yading@11 174 ffio_open_dyn_packet_buf(&rtpctx->pb, RTSP_TCP_MAX_PACKET_SIZE);
yading@11 175 return 0;
yading@11 176 }
yading@11 177
yading@11 178 static int rtsp_write_packet(AVFormatContext *s, AVPacket *pkt)
yading@11 179 {
yading@11 180 RTSPState *rt = s->priv_data;
yading@11 181 RTSPStream *rtsp_st;
yading@11 182 int n;
yading@11 183 struct pollfd p = {ffurl_get_file_handle(rt->rtsp_hd), POLLIN, 0};
yading@11 184 AVFormatContext *rtpctx;
yading@11 185 int ret;
yading@11 186
yading@11 187 while (1) {
yading@11 188 n = poll(&p, 1, 0);
yading@11 189 if (n <= 0)
yading@11 190 break;
yading@11 191 if (p.revents & POLLIN) {
yading@11 192 RTSPMessageHeader reply;
yading@11 193
yading@11 194 /* Don't let ff_rtsp_read_reply handle interleaved packets,
yading@11 195 * since it would block and wait for an RTSP reply on the socket
yading@11 196 * (which may not be coming any time soon) if it handles
yading@11 197 * interleaved packets internally. */
yading@11 198 ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
yading@11 199 if (ret < 0)
yading@11 200 return AVERROR(EPIPE);
yading@11 201 if (ret == 1)
yading@11 202 ff_rtsp_skip_packet(s);
yading@11 203 /* XXX: parse message */
yading@11 204 if (rt->state != RTSP_STATE_STREAMING)
yading@11 205 return AVERROR(EPIPE);
yading@11 206 }
yading@11 207 }
yading@11 208
yading@11 209 if (pkt->stream_index < 0 || pkt->stream_index >= rt->nb_rtsp_streams)
yading@11 210 return AVERROR_INVALIDDATA;
yading@11 211 rtsp_st = rt->rtsp_streams[pkt->stream_index];
yading@11 212 rtpctx = rtsp_st->transport_priv;
yading@11 213
yading@11 214 ret = ff_write_chained(rtpctx, 0, pkt, s);
yading@11 215 /* ff_write_chained does all the RTP packetization. If using TCP as
yading@11 216 * transport, rtpctx->pb is only a dyn_packet_buf that queues up the
yading@11 217 * packets, so we need to send them out on the TCP connection separately.
yading@11 218 */
yading@11 219 if (!ret && rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP)
yading@11 220 ret = tcp_write_packet(s, rtsp_st);
yading@11 221 return ret;
yading@11 222 }
yading@11 223
yading@11 224 static int rtsp_write_close(AVFormatContext *s)
yading@11 225 {
yading@11 226 RTSPState *rt = s->priv_data;
yading@11 227
yading@11 228 ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
yading@11 229
yading@11 230 ff_rtsp_close_streams(s);
yading@11 231 ff_rtsp_close_connections(s);
yading@11 232 ff_network_close();
yading@11 233 return 0;
yading@11 234 }
yading@11 235
yading@11 236 AVOutputFormat ff_rtsp_muxer = {
yading@11 237 .name = "rtsp",
yading@11 238 .long_name = NULL_IF_CONFIG_SMALL("RTSP output"),
yading@11 239 .priv_data_size = sizeof(RTSPState),
yading@11 240 .audio_codec = AV_CODEC_ID_AAC,
yading@11 241 .video_codec = AV_CODEC_ID_MPEG4,
yading@11 242 .write_header = rtsp_write_header,
yading@11 243 .write_packet = rtsp_write_packet,
yading@11 244 .write_trailer = rtsp_write_close,
yading@11 245 .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
yading@11 246 .priv_class = &rtsp_muxer_class,
yading@11 247 };