LCOV - code coverage report
Current view: top level - libavformat - sapdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 3 122 2.5 %
Date: 2017-12-16 13:57:32 Functions: 1 4 25.0 %

          Line data    Source code
       1             : /*
       2             :  * Session Announcement Protocol (RFC 2974) demuxer
       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             : #include "libavutil/avassert.h"
      24             : #include "libavutil/avstring.h"
      25             : #include "libavutil/intreadwrite.h"
      26             : #include "network.h"
      27             : #include "os_support.h"
      28             : #include "internal.h"
      29             : #include "avio_internal.h"
      30             : #include "url.h"
      31             : #include "rtpdec.h"
      32             : #if HAVE_POLL_H
      33             : #include <poll.h>
      34             : #endif
      35             : 
      36             : struct SAPState {
      37             :     URLContext *ann_fd;
      38             :     AVFormatContext *sdp_ctx;
      39             :     AVIOContext sdp_pb;
      40             :     uint16_t hash;
      41             :     char *sdp;
      42             :     int eof;
      43             : };
      44             : 
      45        3209 : static int sap_probe(AVProbeData *p)
      46             : {
      47        3209 :     if (av_strstart(p->filename, "sap:", NULL))
      48           0 :         return AVPROBE_SCORE_MAX;
      49        3209 :     return 0;
      50             : }
      51             : 
      52           0 : static int sap_read_close(AVFormatContext *s)
      53             : {
      54           0 :     struct SAPState *sap = s->priv_data;
      55           0 :     if (sap->sdp_ctx)
      56           0 :         avformat_close_input(&sap->sdp_ctx);
      57           0 :     if (sap->ann_fd)
      58           0 :         ffurl_close(sap->ann_fd);
      59           0 :     av_freep(&sap->sdp);
      60           0 :     ff_network_close();
      61           0 :     return 0;
      62             : }
      63             : 
      64           0 : static int sap_read_header(AVFormatContext *s)
      65             : {
      66           0 :     struct SAPState *sap = s->priv_data;
      67             :     char host[1024], path[1024], url[1024];
      68             :     uint8_t recvbuf[RTP_MAX_PACKET_LENGTH];
      69             :     int port;
      70             :     int ret, i;
      71             :     AVInputFormat* infmt;
      72             : 
      73           0 :     if (!ff_network_init())
      74           0 :         return AVERROR(EIO);
      75             : 
      76           0 :     av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port,
      77           0 :                  path, sizeof(path), s->filename);
      78           0 :     if (port < 0)
      79           0 :         port = 9875;
      80             : 
      81           0 :     if (!host[0]) {
      82             :         /* Listen for announcements on sap.mcast.net if no host was specified */
      83           0 :         av_strlcpy(host, "224.2.127.254", sizeof(host));
      84             :     }
      85             : 
      86           0 :     ff_url_join(url, sizeof(url), "udp", NULL, host, port, "?localport=%d",
      87             :                 port);
      88           0 :     ret = ffurl_open_whitelist(&sap->ann_fd, url, AVIO_FLAG_READ,
      89           0 :                                &s->interrupt_callback, NULL,
      90           0 :                                s->protocol_whitelist, s->protocol_blacklist, NULL);
      91           0 :     if (ret)
      92           0 :         goto fail;
      93             : 
      94           0 :     while (1) {
      95             :         int addr_type, auth_len;
      96             :         int pos;
      97             : 
      98           0 :         ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf) - 1);
      99           0 :         if (ret == AVERROR(EAGAIN))
     100           0 :             continue;
     101           0 :         if (ret < 0)
     102           0 :             goto fail;
     103           0 :         recvbuf[ret] = '\0'; /* Null terminate for easier parsing */
     104           0 :         if (ret < 8) {
     105           0 :             av_log(s, AV_LOG_WARNING, "Received too short packet\n");
     106           0 :             continue;
     107             :         }
     108             : 
     109           0 :         if ((recvbuf[0] & 0xe0) != 0x20) {
     110           0 :             av_log(s, AV_LOG_WARNING, "Unsupported SAP version packet "
     111             :                                       "received\n");
     112           0 :             continue;
     113             :         }
     114             : 
     115           0 :         if (recvbuf[0] & 0x04) {
     116           0 :             av_log(s, AV_LOG_WARNING, "Received stream deletion "
     117             :                                       "announcement\n");
     118           0 :             continue;
     119             :         }
     120           0 :         addr_type = recvbuf[0] & 0x10;
     121           0 :         auth_len  = recvbuf[1];
     122           0 :         sap->hash = AV_RB16(&recvbuf[2]);
     123           0 :         pos = 4;
     124           0 :         if (addr_type)
     125           0 :             pos += 16; /* IPv6 */
     126             :         else
     127           0 :             pos += 4; /* IPv4 */
     128           0 :         pos += auth_len * 4;
     129           0 :         if (pos + 4 >= ret) {
     130           0 :             av_log(s, AV_LOG_WARNING, "Received too short packet\n");
     131           0 :             continue;
     132             :         }
     133             : #define MIME "application/sdp"
     134           0 :         if (strcmp(&recvbuf[pos], MIME) == 0) {
     135           0 :             pos += strlen(MIME) + 1;
     136           0 :         } else if (strncmp(&recvbuf[pos], "v=0\r\n", 5) == 0) {
     137             :             // Direct SDP without a mime type
     138             :         } else {
     139           0 :             av_log(s, AV_LOG_WARNING, "Unsupported mime type %s\n",
     140             :                                       &recvbuf[pos]);
     141           0 :             continue;
     142             :         }
     143             : 
     144           0 :         sap->sdp = av_strdup(&recvbuf[pos]);
     145           0 :         break;
     146             :     }
     147             : 
     148           0 :     av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sap->sdp);
     149           0 :     ffio_init_context(&sap->sdp_pb, sap->sdp, strlen(sap->sdp), 0, NULL, NULL,
     150             :                   NULL, NULL);
     151             : 
     152           0 :     infmt = av_find_input_format("sdp");
     153           0 :     if (!infmt)
     154           0 :         goto fail;
     155           0 :     sap->sdp_ctx = avformat_alloc_context();
     156           0 :     if (!sap->sdp_ctx) {
     157           0 :         ret = AVERROR(ENOMEM);
     158           0 :         goto fail;
     159             :     }
     160           0 :     sap->sdp_ctx->max_delay = s->max_delay;
     161           0 :     sap->sdp_ctx->pb        = &sap->sdp_pb;
     162           0 :     sap->sdp_ctx->interrupt_callback = s->interrupt_callback;
     163             : 
     164           0 :     if ((ret = ff_copy_whiteblacklists(sap->sdp_ctx, s)) < 0)
     165           0 :         goto fail;
     166             : 
     167           0 :     ret = avformat_open_input(&sap->sdp_ctx, "temp.sdp", infmt, NULL);
     168           0 :     if (ret < 0)
     169           0 :         goto fail;
     170           0 :     if (sap->sdp_ctx->ctx_flags & AVFMTCTX_NOHEADER)
     171           0 :         s->ctx_flags |= AVFMTCTX_NOHEADER;
     172           0 :     for (i = 0; i < sap->sdp_ctx->nb_streams; i++) {
     173           0 :         AVStream *st = avformat_new_stream(s, NULL);
     174           0 :         if (!st) {
     175           0 :             ret = AVERROR(ENOMEM);
     176           0 :             goto fail;
     177             :         }
     178           0 :         st->id = i;
     179           0 :         avcodec_parameters_copy(st->codecpar, sap->sdp_ctx->streams[i]->codecpar);
     180           0 :         st->time_base = sap->sdp_ctx->streams[i]->time_base;
     181             :     }
     182             : 
     183           0 :     return 0;
     184             : 
     185           0 : fail:
     186           0 :     sap_read_close(s);
     187           0 :     return ret;
     188             : }
     189             : 
     190           0 : static int sap_fetch_packet(AVFormatContext *s, AVPacket *pkt)
     191             : {
     192           0 :     struct SAPState *sap = s->priv_data;
     193           0 :     int fd = ffurl_get_file_handle(sap->ann_fd);
     194             :     int n, ret;
     195           0 :     struct pollfd p = {fd, POLLIN, 0};
     196             :     uint8_t recvbuf[RTP_MAX_PACKET_LENGTH];
     197             : 
     198           0 :     if (sap->eof)
     199           0 :         return AVERROR_EOF;
     200             : 
     201             :     while (1) {
     202           0 :         n = poll(&p, 1, 0);
     203           0 :         if (n <= 0 || !(p.revents & POLLIN))
     204             :             break;
     205           0 :         ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf));
     206           0 :         if (ret >= 8) {
     207           0 :             uint16_t hash = AV_RB16(&recvbuf[2]);
     208             :             /* Should ideally check the source IP address, too */
     209           0 :             if (recvbuf[0] & 0x04 && hash == sap->hash) {
     210             :                 /* Stream deletion */
     211           0 :                 sap->eof = 1;
     212           0 :                 return AVERROR_EOF;
     213             :             }
     214             :         }
     215             :     }
     216           0 :     ret = av_read_frame(sap->sdp_ctx, pkt);
     217           0 :     if (ret < 0)
     218           0 :         return ret;
     219           0 :     if (s->ctx_flags & AVFMTCTX_NOHEADER) {
     220           0 :         while (sap->sdp_ctx->nb_streams > s->nb_streams) {
     221           0 :             int i = s->nb_streams;
     222           0 :             AVStream *st = avformat_new_stream(s, NULL);
     223           0 :             if (!st) {
     224           0 :                 av_packet_unref(pkt);
     225           0 :                 return AVERROR(ENOMEM);
     226             :             }
     227           0 :             st->id = i;
     228           0 :             avcodec_parameters_copy(st->codecpar, sap->sdp_ctx->streams[i]->codecpar);
     229           0 :             st->time_base = sap->sdp_ctx->streams[i]->time_base;
     230             :         }
     231             :     }
     232           0 :     return ret;
     233             : }
     234             : 
     235             : AVInputFormat ff_sap_demuxer = {
     236             :     .name           = "sap",
     237             :     .long_name      = NULL_IF_CONFIG_SMALL("SAP input"),
     238             :     .priv_data_size = sizeof(struct SAPState),
     239             :     .read_probe     = sap_probe,
     240             :     .read_header    = sap_read_header,
     241             :     .read_packet    = sap_fetch_packet,
     242             :     .read_close     = sap_read_close,
     243             :     .flags          = AVFMT_NOFILE,
     244             : };

Generated by: LCOV version 1.13