• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/rtspenc.c

Go to the documentation of this file.
00001 /*
00002  * RTSP muxer
00003  * Copyright (c) 2010 Martin Storsjo
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "avformat.h"
00023 
00024 #include <sys/time.h>
00025 #if HAVE_POLL_H
00026 #include <poll.h>
00027 #endif
00028 #include "network.h"
00029 #include "os_support.h"
00030 #include "rtsp.h"
00031 #include "internal.h"
00032 #include "avio_internal.h"
00033 #include "libavutil/intreadwrite.h"
00034 #include "libavutil/avstring.h"
00035 #include "url.h"
00036 
00037 #define SDP_MAX_SIZE 16384
00038 
00039 static const AVClass rtsp_muxer_class = {
00040     .class_name = "RTSP muxer",
00041     .item_name  = av_default_item_name,
00042     .option     = ff_rtsp_options,
00043     .version    = LIBAVUTIL_VERSION_INT,
00044 };
00045 
00046 int ff_rtsp_setup_output_streams(AVFormatContext *s, const char *addr)
00047 {
00048     RTSPState *rt = s->priv_data;
00049     RTSPMessageHeader reply1, *reply = &reply1;
00050     int i;
00051     char *sdp;
00052     AVFormatContext sdp_ctx, *ctx_array[1];
00053 
00054     s->start_time_realtime = av_gettime();
00055 
00056     /* Announce the stream */
00057     sdp = av_mallocz(SDP_MAX_SIZE);
00058     if (sdp == NULL)
00059         return AVERROR(ENOMEM);
00060     /* We create the SDP based on the RTSP AVFormatContext where we
00061      * aren't allowed to change the filename field. (We create the SDP
00062      * based on the RTSP context since the contexts for the RTP streams
00063      * don't exist yet.) In order to specify a custom URL with the actual
00064      * peer IP instead of the originally specified hostname, we create
00065      * a temporary copy of the AVFormatContext, where the custom URL is set.
00066      *
00067      * FIXME: Create the SDP without copying the AVFormatContext.
00068      * This either requires setting up the RTP stream AVFormatContexts
00069      * already here (complicating things immensely) or getting a more
00070      * flexible SDP creation interface.
00071      */
00072     sdp_ctx = *s;
00073     ff_url_join(sdp_ctx.filename, sizeof(sdp_ctx.filename),
00074                 "rtsp", NULL, addr, -1, NULL);
00075     ctx_array[0] = &sdp_ctx;
00076     if (av_sdp_create(ctx_array, 1, sdp, SDP_MAX_SIZE)) {
00077         av_free(sdp);
00078         return AVERROR_INVALIDDATA;
00079     }
00080     av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sdp);
00081     ff_rtsp_send_cmd_with_content(s, "ANNOUNCE", rt->control_uri,
00082                                   "Content-Type: application/sdp\r\n",
00083                                   reply, NULL, sdp, strlen(sdp));
00084     av_free(sdp);
00085     if (reply->status_code != RTSP_STATUS_OK)
00086         return AVERROR_INVALIDDATA;
00087 
00088     /* Set up the RTSPStreams for each AVStream */
00089     for (i = 0; i < s->nb_streams; i++) {
00090         RTSPStream *rtsp_st;
00091 
00092         rtsp_st = av_mallocz(sizeof(RTSPStream));
00093         if (!rtsp_st)
00094             return AVERROR(ENOMEM);
00095         dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
00096 
00097         rtsp_st->stream_index = i;
00098 
00099         av_strlcpy(rtsp_st->control_url, rt->control_uri, sizeof(rtsp_st->control_url));
00100         /* Note, this must match the relative uri set in the sdp content */
00101         av_strlcatf(rtsp_st->control_url, sizeof(rtsp_st->control_url),
00102                     "/streamid=%d", i);
00103     }
00104 
00105     return 0;
00106 }
00107 
00108 static int rtsp_write_record(AVFormatContext *s)
00109 {
00110     RTSPState *rt = s->priv_data;
00111     RTSPMessageHeader reply1, *reply = &reply1;
00112     char cmd[1024];
00113 
00114     snprintf(cmd, sizeof(cmd),
00115              "Range: npt=0.000-\r\n");
00116     ff_rtsp_send_cmd(s, "RECORD", rt->control_uri, cmd, reply, NULL);
00117     if (reply->status_code != RTSP_STATUS_OK)
00118         return -1;
00119     rt->state = RTSP_STATE_STREAMING;
00120     return 0;
00121 }
00122 
00123 static int rtsp_write_header(AVFormatContext *s)
00124 {
00125     int ret;
00126 
00127     ret = ff_rtsp_connect(s);
00128     if (ret)
00129         return ret;
00130 
00131     if (rtsp_write_record(s) < 0) {
00132         ff_rtsp_close_streams(s);
00133         ff_rtsp_close_connections(s);
00134         return AVERROR_INVALIDDATA;
00135     }
00136     return 0;
00137 }
00138 
00139 static int tcp_write_packet(AVFormatContext *s, RTSPStream *rtsp_st)
00140 {
00141     RTSPState *rt = s->priv_data;
00142     AVFormatContext *rtpctx = rtsp_st->transport_priv;
00143     uint8_t *buf, *ptr;
00144     int size;
00145     uint8_t *interleave_header, *interleaved_packet;
00146 
00147     size = avio_close_dyn_buf(rtpctx->pb, &buf);
00148     ptr = buf;
00149     while (size > 4) {
00150         uint32_t packet_len = AV_RB32(ptr);
00151         int id;
00152         /* The interleaving header is exactly 4 bytes, which happens to be
00153          * the same size as the packet length header from
00154          * ffio_open_dyn_packet_buf. So by writing the interleaving header
00155          * over these bytes, we get a consecutive interleaved packet
00156          * that can be written in one call. */
00157         interleaved_packet = interleave_header = ptr;
00158         ptr += 4;
00159         size -= 4;
00160         if (packet_len > size || packet_len < 2)
00161             break;
00162         if (ptr[1] >= RTCP_SR && ptr[1] <= RTCP_APP)
00163             id = rtsp_st->interleaved_max; /* RTCP */
00164         else
00165             id = rtsp_st->interleaved_min; /* RTP */
00166         interleave_header[0] = '$';
00167         interleave_header[1] = id;
00168         AV_WB16(interleave_header + 2, packet_len);
00169         ffurl_write(rt->rtsp_hd_out, interleaved_packet, 4 + packet_len);
00170         ptr += packet_len;
00171         size -= packet_len;
00172     }
00173     av_free(buf);
00174     ffio_open_dyn_packet_buf(&rtpctx->pb, RTSP_TCP_MAX_PACKET_SIZE);
00175     return 0;
00176 }
00177 
00178 static int rtsp_write_packet(AVFormatContext *s, AVPacket *pkt)
00179 {
00180     RTSPState *rt = s->priv_data;
00181     RTSPStream *rtsp_st;
00182     int n;
00183     struct pollfd p = {ffurl_get_file_handle(rt->rtsp_hd), POLLIN, 0};
00184     AVFormatContext *rtpctx;
00185     int ret;
00186 
00187     while (1) {
00188         n = poll(&p, 1, 0);
00189         if (n <= 0)
00190             break;
00191         if (p.revents & POLLIN) {
00192             RTSPMessageHeader reply;
00193 
00194             /* Don't let ff_rtsp_read_reply handle interleaved packets,
00195              * since it would block and wait for an RTSP reply on the socket
00196              * (which may not be coming any time soon) if it handles
00197              * interleaved packets internally. */
00198             ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
00199             if (ret < 0)
00200                 return AVERROR(EPIPE);
00201             if (ret == 1)
00202                 ff_rtsp_skip_packet(s);
00203             /* XXX: parse message */
00204             if (rt->state != RTSP_STATE_STREAMING)
00205                 return AVERROR(EPIPE);
00206         }
00207     }
00208 
00209     if (pkt->stream_index < 0 || pkt->stream_index >= rt->nb_rtsp_streams)
00210         return AVERROR_INVALIDDATA;
00211     rtsp_st = rt->rtsp_streams[pkt->stream_index];
00212     rtpctx = rtsp_st->transport_priv;
00213 
00214     ret = ff_write_chained(rtpctx, 0, pkt, s);
00215     /* ff_write_chained does all the RTP packetization. If using TCP as
00216      * transport, rtpctx->pb is only a dyn_packet_buf that queues up the
00217      * packets, so we need to send them out on the TCP connection separately.
00218      */
00219     if (!ret && rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP)
00220         ret = tcp_write_packet(s, rtsp_st);
00221     return ret;
00222 }
00223 
00224 static int rtsp_write_close(AVFormatContext *s)
00225 {
00226     RTSPState *rt = s->priv_data;
00227 
00228     ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
00229 
00230     ff_rtsp_close_streams(s);
00231     ff_rtsp_close_connections(s);
00232     ff_network_close();
00233     return 0;
00234 }
00235 
00236 AVOutputFormat ff_rtsp_muxer = {
00237     .name              = "rtsp",
00238     .long_name         = NULL_IF_CONFIG_SMALL("RTSP output format"),
00239     .priv_data_size    = sizeof(RTSPState),
00240     .audio_codec       = CODEC_ID_AAC,
00241     .video_codec       = CODEC_ID_MPEG4,
00242     .write_header      = rtsp_write_header,
00243     .write_packet      = rtsp_write_packet,
00244     .write_trailer     = rtsp_write_close,
00245     .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
00246     .priv_class = &rtsp_muxer_class,
00247 };
00248 
Generated on Thu Jan 24 2013 17:08:56 for Libav by doxygen 1.7.1