yading@11: /* yading@11: * RTSP muxer yading@11: * Copyright (c) 2010 Martin Storsjo yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #include "avformat.h" yading@11: yading@11: #if HAVE_POLL_H yading@11: #include yading@11: #endif yading@11: #include "network.h" yading@11: #include "os_support.h" yading@11: #include "rtsp.h" yading@11: #include "internal.h" yading@11: #include "avio_internal.h" yading@11: #include "libavutil/intreadwrite.h" yading@11: #include "libavutil/avstring.h" yading@11: #include "libavutil/time.h" yading@11: #include "url.h" yading@11: yading@11: #define SDP_MAX_SIZE 16384 yading@11: yading@11: static const AVClass rtsp_muxer_class = { yading@11: .class_name = "RTSP muxer", yading@11: .item_name = av_default_item_name, yading@11: .option = ff_rtsp_options, yading@11: .version = LIBAVUTIL_VERSION_INT, yading@11: }; yading@11: yading@11: int ff_rtsp_setup_output_streams(AVFormatContext *s, const char *addr) yading@11: { yading@11: RTSPState *rt = s->priv_data; yading@11: RTSPMessageHeader reply1, *reply = &reply1; yading@11: int i; yading@11: char *sdp; yading@11: AVFormatContext sdp_ctx, *ctx_array[1]; yading@11: yading@11: s->start_time_realtime = av_gettime(); yading@11: yading@11: /* Announce the stream */ yading@11: sdp = av_mallocz(SDP_MAX_SIZE); yading@11: if (sdp == NULL) yading@11: return AVERROR(ENOMEM); yading@11: /* We create the SDP based on the RTSP AVFormatContext where we yading@11: * aren't allowed to change the filename field. (We create the SDP yading@11: * based on the RTSP context since the contexts for the RTP streams yading@11: * don't exist yet.) In order to specify a custom URL with the actual yading@11: * peer IP instead of the originally specified hostname, we create yading@11: * a temporary copy of the AVFormatContext, where the custom URL is set. yading@11: * yading@11: * FIXME: Create the SDP without copying the AVFormatContext. yading@11: * This either requires setting up the RTP stream AVFormatContexts yading@11: * already here (complicating things immensely) or getting a more yading@11: * flexible SDP creation interface. yading@11: */ yading@11: sdp_ctx = *s; yading@11: ff_url_join(sdp_ctx.filename, sizeof(sdp_ctx.filename), yading@11: "rtsp", NULL, addr, -1, NULL); yading@11: ctx_array[0] = &sdp_ctx; yading@11: if (av_sdp_create(ctx_array, 1, sdp, SDP_MAX_SIZE)) { yading@11: av_free(sdp); yading@11: return AVERROR_INVALIDDATA; yading@11: } yading@11: av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sdp); yading@11: ff_rtsp_send_cmd_with_content(s, "ANNOUNCE", rt->control_uri, yading@11: "Content-Type: application/sdp\r\n", yading@11: reply, NULL, sdp, strlen(sdp)); yading@11: av_free(sdp); yading@11: if (reply->status_code != RTSP_STATUS_OK) yading@11: return AVERROR_INVALIDDATA; yading@11: yading@11: /* Set up the RTSPStreams for each AVStream */ yading@11: for (i = 0; i < s->nb_streams; i++) { yading@11: RTSPStream *rtsp_st; yading@11: yading@11: rtsp_st = av_mallocz(sizeof(RTSPStream)); yading@11: if (!rtsp_st) yading@11: return AVERROR(ENOMEM); yading@11: dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st); yading@11: yading@11: rtsp_st->stream_index = i; yading@11: yading@11: av_strlcpy(rtsp_st->control_url, rt->control_uri, sizeof(rtsp_st->control_url)); yading@11: /* Note, this must match the relative uri set in the sdp content */ yading@11: av_strlcatf(rtsp_st->control_url, sizeof(rtsp_st->control_url), yading@11: "/streamid=%d", i); yading@11: } yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: static int rtsp_write_record(AVFormatContext *s) yading@11: { yading@11: RTSPState *rt = s->priv_data; yading@11: RTSPMessageHeader reply1, *reply = &reply1; yading@11: char cmd[1024]; yading@11: yading@11: snprintf(cmd, sizeof(cmd), yading@11: "Range: npt=0.000-\r\n"); yading@11: ff_rtsp_send_cmd(s, "RECORD", rt->control_uri, cmd, reply, NULL); yading@11: if (reply->status_code != RTSP_STATUS_OK) yading@11: return -1; yading@11: rt->state = RTSP_STATE_STREAMING; yading@11: return 0; yading@11: } yading@11: yading@11: static int rtsp_write_header(AVFormatContext *s) yading@11: { yading@11: int ret; yading@11: yading@11: ret = ff_rtsp_connect(s); yading@11: if (ret) yading@11: return ret; yading@11: yading@11: if (rtsp_write_record(s) < 0) { yading@11: ff_rtsp_close_streams(s); yading@11: ff_rtsp_close_connections(s); yading@11: return AVERROR_INVALIDDATA; yading@11: } yading@11: return 0; yading@11: } yading@11: yading@11: static int tcp_write_packet(AVFormatContext *s, RTSPStream *rtsp_st) yading@11: { yading@11: RTSPState *rt = s->priv_data; yading@11: AVFormatContext *rtpctx = rtsp_st->transport_priv; yading@11: uint8_t *buf, *ptr; yading@11: int size; yading@11: uint8_t *interleave_header, *interleaved_packet; yading@11: yading@11: size = avio_close_dyn_buf(rtpctx->pb, &buf); yading@11: ptr = buf; yading@11: while (size > 4) { yading@11: uint32_t packet_len = AV_RB32(ptr); yading@11: int id; yading@11: /* The interleaving header is exactly 4 bytes, which happens to be yading@11: * the same size as the packet length header from yading@11: * ffio_open_dyn_packet_buf. So by writing the interleaving header yading@11: * over these bytes, we get a consecutive interleaved packet yading@11: * that can be written in one call. */ yading@11: interleaved_packet = interleave_header = ptr; yading@11: ptr += 4; yading@11: size -= 4; yading@11: if (packet_len > size || packet_len < 2) yading@11: break; yading@11: if (RTP_PT_IS_RTCP(ptr[1])) yading@11: id = rtsp_st->interleaved_max; /* RTCP */ yading@11: else yading@11: id = rtsp_st->interleaved_min; /* RTP */ yading@11: interleave_header[0] = '$'; yading@11: interleave_header[1] = id; yading@11: AV_WB16(interleave_header + 2, packet_len); yading@11: ffurl_write(rt->rtsp_hd_out, interleaved_packet, 4 + packet_len); yading@11: ptr += packet_len; yading@11: size -= packet_len; yading@11: } yading@11: av_free(buf); yading@11: ffio_open_dyn_packet_buf(&rtpctx->pb, RTSP_TCP_MAX_PACKET_SIZE); yading@11: return 0; yading@11: } yading@11: yading@11: static int rtsp_write_packet(AVFormatContext *s, AVPacket *pkt) yading@11: { yading@11: RTSPState *rt = s->priv_data; yading@11: RTSPStream *rtsp_st; yading@11: int n; yading@11: struct pollfd p = {ffurl_get_file_handle(rt->rtsp_hd), POLLIN, 0}; yading@11: AVFormatContext *rtpctx; yading@11: int ret; yading@11: yading@11: while (1) { yading@11: n = poll(&p, 1, 0); yading@11: if (n <= 0) yading@11: break; yading@11: if (p.revents & POLLIN) { yading@11: RTSPMessageHeader reply; yading@11: yading@11: /* Don't let ff_rtsp_read_reply handle interleaved packets, yading@11: * since it would block and wait for an RTSP reply on the socket yading@11: * (which may not be coming any time soon) if it handles yading@11: * interleaved packets internally. */ yading@11: ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL); yading@11: if (ret < 0) yading@11: return AVERROR(EPIPE); yading@11: if (ret == 1) yading@11: ff_rtsp_skip_packet(s); yading@11: /* XXX: parse message */ yading@11: if (rt->state != RTSP_STATE_STREAMING) yading@11: return AVERROR(EPIPE); yading@11: } yading@11: } yading@11: yading@11: if (pkt->stream_index < 0 || pkt->stream_index >= rt->nb_rtsp_streams) yading@11: return AVERROR_INVALIDDATA; yading@11: rtsp_st = rt->rtsp_streams[pkt->stream_index]; yading@11: rtpctx = rtsp_st->transport_priv; yading@11: yading@11: ret = ff_write_chained(rtpctx, 0, pkt, s); yading@11: /* ff_write_chained does all the RTP packetization. If using TCP as yading@11: * transport, rtpctx->pb is only a dyn_packet_buf that queues up the yading@11: * packets, so we need to send them out on the TCP connection separately. yading@11: */ yading@11: if (!ret && rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) yading@11: ret = tcp_write_packet(s, rtsp_st); yading@11: return ret; yading@11: } yading@11: yading@11: static int rtsp_write_close(AVFormatContext *s) yading@11: { yading@11: RTSPState *rt = s->priv_data; yading@11: yading@11: ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL); yading@11: yading@11: ff_rtsp_close_streams(s); yading@11: ff_rtsp_close_connections(s); yading@11: ff_network_close(); yading@11: return 0; yading@11: } yading@11: yading@11: AVOutputFormat ff_rtsp_muxer = { yading@11: .name = "rtsp", yading@11: .long_name = NULL_IF_CONFIG_SMALL("RTSP output"), yading@11: .priv_data_size = sizeof(RTSPState), yading@11: .audio_codec = AV_CODEC_ID_AAC, yading@11: .video_codec = AV_CODEC_ID_MPEG4, yading@11: .write_header = rtsp_write_header, yading@11: .write_packet = rtsp_write_packet, yading@11: .write_trailer = rtsp_write_close, yading@11: .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER, yading@11: .priv_class = &rtsp_muxer_class, yading@11: };