LCOV - code coverage report
Current view: top level - libavformat - rtspenc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 101 0.0 %
Date: 2018-05-20 11:54:08 Functions: 0 6 0.0 %

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

Generated by: LCOV version 1.13