LCOV - code coverage report
Current view: top level - src/media/audio - audio_sender.cpp (source / functions) Coverage Total Hit
Test: jami-coverage-filtered.info Lines: 56.6 % 53 30
Test Date: 2026-06-13 09:18:46 Functions: 38.9 % 18 7

            Line data    Source code
       1              : /*
       2              :  *  Copyright (C) 2004-2026 Savoir-faire Linux Inc.
       3              :  *
       4              :  *  This program is free software: you can redistribute it and/or modify
       5              :  *  it under the terms of the GNU General Public License as published by
       6              :  *  the Free Software Foundation, either version 3 of the License, or
       7              :  *  (at your option) any later version.
       8              :  *
       9              :  *  This program is distributed in the hope that it will be useful,
      10              :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      11              :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      12              :  *  GNU General Public License for more details.
      13              :  *
      14              :  *  You should have received a copy of the GNU General Public License
      15              :  *  along with this program. If not, see <https://www.gnu.org/licenses/>.
      16              :  */
      17              : 
      18              : #include "audio_sender.h"
      19              : #include "libav_deps.h"
      20              : #include "logger.h"
      21              : #include "media_encoder.h"
      22              : #include "media_io_handle.h"
      23              : #include "media_stream.h"
      24              : #include "resampler.h"
      25              : 
      26              : #include <memory>
      27              : 
      28              : namespace jami {
      29              : 
      30          176 : AudioSender::AudioSender(const std::string& dest,
      31              :                          const MediaDescription& args,
      32              :                          SocketPair& socketPair,
      33              :                          const uint16_t seqVal,
      34          176 :                          const uint16_t mtu)
      35          176 :     : dest_(dest)
      36          176 :     , args_(args)
      37          176 :     , seqVal_(seqVal)
      38          352 :     , mtu_(mtu)
      39              : {
      40          176 :     setup(socketPair);
      41          176 : }
      42              : 
      43          352 : AudioSender::~AudioSender()
      44              : {
      45          176 :     audioEncoder_.reset();
      46          176 :     muxContext_.reset();
      47          352 : }
      48              : 
      49              : bool
      50          176 : AudioSender::setup(SocketPair& socketPair)
      51              : {
      52          176 :     audioEncoder_.reset(new MediaEncoder);
      53          176 :     muxContext_.reset(socketPair.createIOContext(mtu_));
      54              : 
      55              :     try {
      56              :         /* Encoder setup */
      57          704 :         JAMI_LOG("audioEncoder_->openOutput {}", dest_);
      58          352 :         audioEncoder_->openOutput(dest_, "rtp");
      59          176 :         audioEncoder_->setOptions(args_);
      60          176 :         auto codec = std::static_pointer_cast<SystemAudioCodecInfo>(args_.codec);
      61          352 :         auto ms = MediaStream("audio sender", codec->audioformat);
      62          176 :         audioEncoder_->setOptions(ms);
      63          176 :         audioEncoder_->addStream(*args_.codec);
      64          176 :         audioEncoder_->setInitSeqVal(seqVal_);
      65          176 :         audioEncoder_->setIOContext(muxContext_->getContext());
      66          176 :     } catch (const MediaEncoderException& e) {
      67            0 :         JAMI_ERROR("{}", e.what());
      68            0 :         return false;
      69            0 :     }
      70              : #ifdef DEBUG_SDP
      71              :     audioEncoder_->print_sdp();
      72              : #endif
      73              : 
      74          176 :     return true;
      75              : }
      76              : 
      77              : void
      78            0 : AudioSender::update(Observable<std::shared_ptr<jami::MediaFrame>>* /*obs*/,
      79              :                     const std::shared_ptr<jami::MediaFrame>& framePtr)
      80              : {
      81            0 :     auto* frame = framePtr->pointer();
      82            0 :     frame->pts = static_cast<int64_t>(sent_samples);
      83            0 :     sent_samples += frame->nb_samples;
      84              : 
      85              :     // check for change in voice activity, if so, call callback
      86              :     // downcast MediaFrame to AudioFrame
      87            0 :     bool hasVoice = std::dynamic_pointer_cast<AudioFrame>(framePtr)->has_voice;
      88            0 :     if (hasVoice != voice_) {
      89            0 :         voice_ = hasVoice;
      90            0 :         if (voiceCallback_) {
      91            0 :             voiceCallback_(voice_);
      92              :         } else {
      93            0 :             JAMI_ERROR("AudioSender no voice callback!");
      94              :         }
      95              :     }
      96              : 
      97            0 :     if (audioEncoder_->encodeAudio(*std::static_pointer_cast<AudioFrame>(framePtr)) < 0)
      98            0 :         JAMI_ERROR("encoding failed");
      99            0 : }
     100              : 
     101              : void
     102          197 : AudioSender::setVoiceCallback(std::function<void(bool)> cb)
     103              : {
     104          197 :     if (cb) {
     105          197 :         voiceCallback_ = std::move(cb);
     106              :     } else {
     107            0 :         JAMI_ERROR("AudioSender attempting to set invalid voice callback");
     108              :     }
     109          197 : }
     110              : 
     111              : uint16_t
     112            0 : AudioSender::getLastSeqValue()
     113              : {
     114            0 :     return audioEncoder_->getLastSeqValue();
     115              : }
     116              : 
     117              : int
     118            0 : AudioSender::setPacketLoss(uint64_t pl)
     119              : {
     120              :     // The encoder may be destroy during a bitrate change
     121              :     // when a codec parameter like auto quality change
     122            0 :     if (!audioEncoder_)
     123            0 :         return -1; // NOK
     124              : 
     125            0 :     return audioEncoder_->setPacketLoss(pl);
     126              : }
     127              : 
     128              : } // namespace jami
        

Generated by: LCOV version 2.0-1