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 166 : AudioSender::AudioSender(const std::string& dest, 31 : const MediaDescription& args, 32 : SocketPair& socketPair, 33 : const uint16_t seqVal, 34 166 : const uint16_t mtu) 35 166 : : dest_(dest) 36 166 : , args_(args) 37 166 : , seqVal_(seqVal) 38 332 : , mtu_(mtu) 39 : { 40 166 : setup(socketPair); 41 166 : } 42 : 43 332 : AudioSender::~AudioSender() 44 : { 45 166 : audioEncoder_.reset(); 46 166 : muxContext_.reset(); 47 332 : } 48 : 49 : bool 50 166 : AudioSender::setup(SocketPair& socketPair) 51 : { 52 166 : audioEncoder_.reset(new MediaEncoder); 53 166 : muxContext_.reset(socketPair.createIOContext(mtu_)); 54 : 55 : try { 56 : /* Encoder setup */ 57 166 : JAMI_DBG("audioEncoder_->openOutput %s", dest_.c_str()); 58 166 : audioEncoder_->openOutput(dest_, "rtp"); 59 166 : audioEncoder_->setOptions(args_); 60 166 : auto codec = std::static_pointer_cast<SystemAudioCodecInfo>(args_.codec); 61 332 : auto ms = MediaStream("audio sender", codec->audioformat); 62 166 : audioEncoder_->setOptions(ms); 63 166 : audioEncoder_->addStream(*args_.codec); 64 166 : audioEncoder_->setInitSeqVal(seqVal_); 65 166 : audioEncoder_->setIOContext(muxContext_->getContext()); 66 166 : } catch (const MediaEncoderException& e) { 67 0 : JAMI_ERR("%s", e.what()); 68 0 : return false; 69 0 : } 70 : #ifdef DEBUG_SDP 71 : audioEncoder_->print_sdp(); 72 : #endif 73 : 74 166 : 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_ERR("AudioSender no voice callback!"); 94 : } 95 : } 96 : 97 0 : if (audioEncoder_->encodeAudio(*std::static_pointer_cast<AudioFrame>(framePtr)) < 0) 98 0 : JAMI_ERR("encoding failed"); 99 0 : } 100 : 101 : void 102 186 : AudioSender::setVoiceCallback(std::function<void(bool)> cb) 103 : { 104 186 : if (cb) { 105 186 : voiceCallback_ = std::move(cb); 106 : } else { 107 0 : JAMI_ERR("AudioSender attempting to set invalid voice callback"); 108 : } 109 186 : } 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