Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc. 3 : * 4 : * Author: Tristan Matthews <tristan.matthews@savoirfairelinux.com> 5 : * Author: Philippe Gorley <philippe.gorley@savoirfairelinux.com> 6 : * 7 : * This program is free software; you can redistribute it and/or modify 8 : * it under the terms of the GNU General Public License as published by 9 : * the Free Software Foundation; either version 3 of the License, or 10 : * (at your option) any later version. 11 : * 12 : * This program 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 15 : * GNU General Public License for more details. 16 : * 17 : * You should have received a copy of the GNU General Public License 18 : * along with this program; if not, write to the Free Software 19 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 20 : */ 21 : 22 : #include "audio_sender.h" 23 : #include "client/videomanager.h" 24 : #include "libav_deps.h" 25 : #include "logger.h" 26 : #include "media_encoder.h" 27 : #include "media_io_handle.h" 28 : #include "media_stream.h" 29 : #include "resampler.h" 30 : 31 : #include <memory> 32 : 33 : namespace jami { 34 : 35 187 : AudioSender::AudioSender(const std::string& dest, 36 : const MediaDescription& args, 37 : SocketPair& socketPair, 38 : const uint16_t seqVal, 39 187 : const uint16_t mtu) 40 187 : : dest_(dest) 41 187 : , args_(args) 42 187 : , seqVal_(seqVal) 43 374 : , mtu_(mtu) 44 : { 45 187 : setup(socketPair); 46 187 : } 47 : 48 374 : AudioSender::~AudioSender() 49 : { 50 187 : audioEncoder_.reset(); 51 187 : muxContext_.reset(); 52 374 : } 53 : 54 : bool 55 187 : AudioSender::setup(SocketPair& socketPair) 56 : { 57 187 : audioEncoder_.reset(new MediaEncoder); 58 187 : muxContext_.reset(socketPair.createIOContext(mtu_)); 59 : 60 : try { 61 : /* Encoder setup */ 62 187 : JAMI_DBG("audioEncoder_->openOutput %s", dest_.c_str()); 63 187 : audioEncoder_->openOutput(dest_, "rtp"); 64 187 : audioEncoder_->setOptions(args_); 65 187 : auto codec = std::static_pointer_cast<SystemAudioCodecInfo>(args_.codec); 66 374 : auto ms = MediaStream("audio sender", codec->audioformat); 67 187 : audioEncoder_->setOptions(ms); 68 187 : audioEncoder_->addStream(*args_.codec); 69 187 : audioEncoder_->setInitSeqVal(seqVal_); 70 187 : audioEncoder_->setIOContext(muxContext_->getContext()); 71 187 : } catch (const MediaEncoderException& e) { 72 0 : JAMI_ERR("%s", e.what()); 73 0 : return false; 74 0 : } 75 : #ifdef DEBUG_SDP 76 : audioEncoder_->print_sdp(); 77 : #endif 78 : 79 187 : return true; 80 : } 81 : 82 : void 83 0 : AudioSender::update(Observable<std::shared_ptr<jami::MediaFrame>>* /*obs*/, 84 : const std::shared_ptr<jami::MediaFrame>& framePtr) 85 : { 86 0 : auto frame = framePtr->pointer(); 87 0 : frame->pts = sent_samples; 88 0 : sent_samples += frame->nb_samples; 89 : 90 : // check for change in voice activity, if so, call callback 91 : // downcast MediaFrame to AudioFrame 92 0 : bool hasVoice = std::dynamic_pointer_cast<AudioFrame>(framePtr)->has_voice; 93 0 : if (hasVoice != voice_) { 94 0 : voice_ = hasVoice; 95 0 : if (voiceCallback_) { 96 0 : voiceCallback_(voice_); 97 : } else { 98 0 : JAMI_ERR("AudioSender no voice callback!"); 99 : } 100 : } 101 : 102 0 : if (audioEncoder_->encodeAudio(*std::static_pointer_cast<AudioFrame>(framePtr)) < 0) 103 0 : JAMI_ERR("encoding failed"); 104 0 : } 105 : 106 : void 107 206 : AudioSender::setVoiceCallback(std::function<void(bool)> cb) 108 : { 109 206 : if (cb) { 110 206 : voiceCallback_ = std::move(cb); 111 : } else { 112 0 : JAMI_ERR("AudioSender trying to set invalid voice callback"); 113 : } 114 206 : } 115 : 116 : uint16_t 117 0 : AudioSender::getLastSeqValue() 118 : { 119 0 : return audioEncoder_->getLastSeqValue(); 120 : } 121 : 122 : int 123 0 : AudioSender::setPacketLoss(uint64_t pl) 124 : { 125 : // The encoder may be destroy during a bitrate change 126 : // when a codec parameter like auto quality change 127 0 : if (!audioEncoder_) 128 0 : return -1; // NOK 129 : 130 0 : return audioEncoder_->setPacketLoss(pl); 131 : } 132 : 133 : } // namespace jami