LCOV - code coverage report
Current view: top level - libavformat - sapenc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 143 0.0 %
Date: 2017-12-12 03:56:30 Functions: 0 3 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Session Announcement Protocol (RFC 2974) 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             : #include "libavutil/parseutils.h"
      24             : #include "libavutil/random_seed.h"
      25             : #include "libavutil/avstring.h"
      26             : #include "libavutil/dict.h"
      27             : #include "libavutil/intreadwrite.h"
      28             : #include "libavutil/time.h"
      29             : #include "internal.h"
      30             : #include "network.h"
      31             : #include "os_support.h"
      32             : #include "rtpenc_chain.h"
      33             : #include "url.h"
      34             : 
      35             : struct SAPState {
      36             :     uint8_t    *ann;
      37             :     int         ann_size;
      38             :     URLContext *ann_fd;
      39             :     int64_t     last_time;
      40             : };
      41             : 
      42           0 : static int sap_write_close(AVFormatContext *s)
      43             : {
      44           0 :     struct SAPState *sap = s->priv_data;
      45             :     int i;
      46             : 
      47           0 :     for (i = 0; i < s->nb_streams; i++) {
      48           0 :         AVFormatContext *rtpctx = s->streams[i]->priv_data;
      49           0 :         if (!rtpctx)
      50           0 :             continue;
      51           0 :         av_write_trailer(rtpctx);
      52           0 :         avio_closep(&rtpctx->pb);
      53           0 :         avformat_free_context(rtpctx);
      54           0 :         s->streams[i]->priv_data = NULL;
      55             :     }
      56             : 
      57           0 :     if (sap->last_time && sap->ann && sap->ann_fd) {
      58           0 :         sap->ann[0] |= 4; /* Session deletion*/
      59           0 :         ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
      60             :     }
      61             : 
      62           0 :     av_freep(&sap->ann);
      63           0 :     if (sap->ann_fd)
      64           0 :         ffurl_close(sap->ann_fd);
      65           0 :     ff_network_close();
      66           0 :     return 0;
      67             : }
      68             : 
      69           0 : static int sap_write_header(AVFormatContext *s)
      70             : {
      71           0 :     struct SAPState *sap = s->priv_data;
      72           0 :     char host[1024], path[1024], url[1024], announce_addr[50] = "";
      73             :     char *option_list;
      74           0 :     int port = 9875, base_port = 5004, i, pos = 0, same_port = 0, ttl = 255;
      75           0 :     AVFormatContext **contexts = NULL;
      76           0 :     int ret = 0;
      77             :     struct sockaddr_storage localaddr;
      78           0 :     socklen_t addrlen = sizeof(localaddr);
      79             :     int udp_fd;
      80           0 :     AVDictionaryEntry* title = av_dict_get(s->metadata, "title", NULL, 0);
      81             : 
      82           0 :     if (!ff_network_init())
      83           0 :         return AVERROR(EIO);
      84             : 
      85             :     /* extract hostname and port */
      86           0 :     av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &base_port,
      87           0 :                  path, sizeof(path), s->filename);
      88           0 :     if (base_port < 0)
      89           0 :         base_port = 5004;
      90             : 
      91             :     /* search for options */
      92           0 :     option_list = strrchr(path, '?');
      93           0 :     if (option_list) {
      94             :         char buf[50];
      95           0 :         if (av_find_info_tag(buf, sizeof(buf), "announce_port", option_list)) {
      96           0 :             port = strtol(buf, NULL, 10);
      97             :         }
      98           0 :         if (av_find_info_tag(buf, sizeof(buf), "same_port", option_list)) {
      99           0 :             same_port = strtol(buf, NULL, 10);
     100             :         }
     101           0 :         if (av_find_info_tag(buf, sizeof(buf), "ttl", option_list)) {
     102           0 :             ttl = strtol(buf, NULL, 10);
     103             :         }
     104           0 :         if (av_find_info_tag(buf, sizeof(buf), "announce_addr", option_list)) {
     105           0 :             av_strlcpy(announce_addr, buf, sizeof(announce_addr));
     106             :         }
     107             :     }
     108             : 
     109           0 :     if (!announce_addr[0]) {
     110           0 :         struct addrinfo hints = { 0 }, *ai = NULL;
     111           0 :         hints.ai_family = AF_UNSPEC;
     112           0 :         if (getaddrinfo(host, NULL, &hints, &ai)) {
     113           0 :             av_log(s, AV_LOG_ERROR, "Unable to resolve %s\n", host);
     114           0 :             ret = AVERROR(EIO);
     115           0 :             goto fail;
     116             :         }
     117           0 :         if (ai->ai_family == AF_INET) {
     118             :             /* Also known as sap.mcast.net */
     119           0 :             av_strlcpy(announce_addr, "224.2.127.254", sizeof(announce_addr));
     120             : #if HAVE_STRUCT_SOCKADDR_IN6
     121           0 :         } else if (ai->ai_family == AF_INET6) {
     122             :             /* With IPv6, you can use the same destination in many different
     123             :              * multicast subnets, to choose how far you want it routed.
     124             :              * This one is intended to be routed globally. */
     125           0 :             av_strlcpy(announce_addr, "ff0e::2:7ffe", sizeof(announce_addr));
     126             : #endif
     127             :         } else {
     128           0 :             freeaddrinfo(ai);
     129           0 :             av_log(s, AV_LOG_ERROR, "Host %s resolved to unsupported "
     130             :                                     "address family\n", host);
     131           0 :             ret = AVERROR(EIO);
     132           0 :             goto fail;
     133             :         }
     134           0 :         freeaddrinfo(ai);
     135             :     }
     136             : 
     137           0 :     contexts = av_mallocz_array(s->nb_streams, sizeof(AVFormatContext*));
     138           0 :     if (!contexts) {
     139           0 :         ret = AVERROR(ENOMEM);
     140           0 :         goto fail;
     141             :     }
     142             : 
     143           0 :     if (s->start_time_realtime == 0  ||  s->start_time_realtime == AV_NOPTS_VALUE)
     144           0 :         s->start_time_realtime = av_gettime();
     145           0 :     for (i = 0; i < s->nb_streams; i++) {
     146             :         URLContext *fd;
     147             : 
     148           0 :         ff_url_join(url, sizeof(url), "rtp", NULL, host, base_port,
     149             :                     "?ttl=%d", ttl);
     150           0 :         if (!same_port)
     151           0 :             base_port += 2;
     152           0 :         ret = ffurl_open_whitelist(&fd, url, AVIO_FLAG_WRITE,
     153           0 :                                    &s->interrupt_callback, NULL,
     154           0 :                                    s->protocol_whitelist, s->protocol_blacklist, NULL);
     155           0 :         if (ret) {
     156           0 :             ret = AVERROR(EIO);
     157           0 :             goto fail;
     158             :         }
     159           0 :         ret = ff_rtp_chain_mux_open(&contexts[i], s, s->streams[i], fd, 0, i);
     160           0 :         if (ret < 0)
     161           0 :             goto fail;
     162           0 :         s->streams[i]->priv_data = contexts[i];
     163           0 :         s->streams[i]->time_base = contexts[i]->streams[0]->time_base;
     164           0 :         av_strlcpy(contexts[i]->filename, url, sizeof(contexts[i]->filename));
     165             :     }
     166             : 
     167           0 :     if (s->nb_streams > 0 && title)
     168           0 :         av_dict_set(&contexts[0]->metadata, "title", title->value, 0);
     169             : 
     170           0 :     ff_url_join(url, sizeof(url), "udp", NULL, announce_addr, port,
     171             :                 "?ttl=%d&connect=1", ttl);
     172           0 :     ret = ffurl_open_whitelist(&sap->ann_fd, url, AVIO_FLAG_WRITE,
     173           0 :                                &s->interrupt_callback, NULL,
     174           0 :                                s->protocol_whitelist, s->protocol_blacklist, NULL);
     175           0 :     if (ret) {
     176           0 :         ret = AVERROR(EIO);
     177           0 :         goto fail;
     178             :     }
     179             : 
     180           0 :     udp_fd = ffurl_get_file_handle(sap->ann_fd);
     181           0 :     if (getsockname(udp_fd, (struct sockaddr*) &localaddr, &addrlen)) {
     182           0 :         ret = AVERROR(EIO);
     183           0 :         goto fail;
     184             :     }
     185           0 :     if (localaddr.ss_family != AF_INET
     186             : #if HAVE_STRUCT_SOCKADDR_IN6
     187           0 :         && localaddr.ss_family != AF_INET6
     188             : #endif
     189             :         ) {
     190           0 :         av_log(s, AV_LOG_ERROR, "Unsupported protocol family\n");
     191           0 :         ret = AVERROR(EIO);
     192           0 :         goto fail;
     193             :     }
     194           0 :     sap->ann_size = 8192;
     195           0 :     sap->ann = av_mallocz(sap->ann_size);
     196           0 :     if (!sap->ann) {
     197           0 :         ret = AVERROR(EIO);
     198           0 :         goto fail;
     199             :     }
     200           0 :     sap->ann[pos] = (1 << 5);
     201             : #if HAVE_STRUCT_SOCKADDR_IN6
     202           0 :     if (localaddr.ss_family == AF_INET6)
     203           0 :         sap->ann[pos] |= 0x10;
     204             : #endif
     205           0 :     pos++;
     206           0 :     sap->ann[pos++] = 0; /* Authentication length */
     207           0 :     AV_WB16(&sap->ann[pos], av_get_random_seed());
     208           0 :     pos += 2;
     209           0 :     if (localaddr.ss_family == AF_INET) {
     210           0 :         memcpy(&sap->ann[pos], &((struct sockaddr_in*)&localaddr)->sin_addr,
     211             :                sizeof(struct in_addr));
     212           0 :         pos += sizeof(struct in_addr);
     213             : #if HAVE_STRUCT_SOCKADDR_IN6
     214             :     } else {
     215           0 :         memcpy(&sap->ann[pos], &((struct sockaddr_in6*)&localaddr)->sin6_addr,
     216             :                sizeof(struct in6_addr));
     217           0 :         pos += sizeof(struct in6_addr);
     218             : #endif
     219             :     }
     220             : 
     221           0 :     av_strlcpy(&sap->ann[pos], "application/sdp", sap->ann_size - pos);
     222           0 :     pos += strlen(&sap->ann[pos]) + 1;
     223             : 
     224           0 :     if (av_sdp_create(contexts, s->nb_streams, &sap->ann[pos],
     225           0 :                       sap->ann_size - pos)) {
     226           0 :         ret = AVERROR_INVALIDDATA;
     227           0 :         goto fail;
     228             :     }
     229           0 :     av_freep(&contexts);
     230           0 :     av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", &sap->ann[pos]);
     231           0 :     pos += strlen(&sap->ann[pos]);
     232           0 :     sap->ann_size = pos;
     233             : 
     234           0 :     if (sap->ann_size > sap->ann_fd->max_packet_size) {
     235           0 :         av_log(s, AV_LOG_ERROR, "Announcement too large to send in one "
     236             :                                 "packet\n");
     237           0 :         goto fail;
     238             :     }
     239             : 
     240           0 :     return 0;
     241             : 
     242           0 : fail:
     243           0 :     av_free(contexts);
     244           0 :     sap_write_close(s);
     245           0 :     return ret;
     246             : }
     247             : 
     248           0 : static int sap_write_packet(AVFormatContext *s, AVPacket *pkt)
     249             : {
     250             :     AVFormatContext *rtpctx;
     251           0 :     struct SAPState *sap = s->priv_data;
     252           0 :     int64_t now = av_gettime_relative();
     253             : 
     254           0 :     if (!sap->last_time || now - sap->last_time > 5000000) {
     255           0 :         int ret = ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
     256             :         /* Don't abort even if we get "Destination unreachable" */
     257           0 :         if (ret < 0 && ret != AVERROR(ECONNREFUSED))
     258           0 :             return ret;
     259           0 :         sap->last_time = now;
     260             :     }
     261           0 :     rtpctx = s->streams[pkt->stream_index]->priv_data;
     262           0 :     return ff_write_chained(rtpctx, 0, pkt, s, 0);
     263             : }
     264             : 
     265             : AVOutputFormat ff_sap_muxer = {
     266             :     .name              = "sap",
     267             :     .long_name         = NULL_IF_CONFIG_SMALL("SAP output"),
     268             :     .priv_data_size    = sizeof(struct SAPState),
     269             :     .audio_codec       = AV_CODEC_ID_AAC,
     270             :     .video_codec       = AV_CODEC_ID_MPEG4,
     271             :     .write_header      = sap_write_header,
     272             :     .write_packet      = sap_write_packet,
     273             :     .write_trailer     = sap_write_close,
     274             :     .flags             = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
     275             : };

Generated by: LCOV version 1.13