Ring Daemon 16.0.0
Loading...
Searching...
No Matches
audio_sender.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 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 "client/videomanager.h"
20#include "libav_deps.h"
21#include "logger.h"
22#include "media_encoder.h"
23#include "media_io_handle.h"
24#include "media_stream.h"
25#include "resampler.h"
26
27#include <memory>
28
29namespace jami {
30
31AudioSender::AudioSender(const std::string& dest,
34 const uint16_t seqVal,
35 const uint16_t mtu)
36 : dest_(dest)
37 , args_(args)
38 , seqVal_(seqVal)
39 , mtu_(mtu)
40{
41 setup(socketPair);
42}
43
45{
46 audioEncoder_.reset();
47 muxContext_.reset();
48}
49
50bool
51AudioSender::setup(SocketPair& socketPair)
52{
53 audioEncoder_.reset(new MediaEncoder);
54 muxContext_.reset(socketPair.createIOContext(mtu_));
55
56 try {
57 /* Encoder setup */
58 JAMI_DBG("audioEncoder_->openOutput %s", dest_.c_str());
59 audioEncoder_->openOutput(dest_, "rtp");
60 audioEncoder_->setOptions(args_);
61 auto codec = std::static_pointer_cast<SystemAudioCodecInfo>(args_.codec);
62 auto ms = MediaStream("audio sender", codec->audioformat);
63 audioEncoder_->setOptions(ms);
64 audioEncoder_->addStream(*args_.codec);
65 audioEncoder_->setInitSeqVal(seqVal_);
66 audioEncoder_->setIOContext(muxContext_->getContext());
67 } catch (const MediaEncoderException& e) {
68 JAMI_ERR("%s", e.what());
69 return false;
70 }
71#ifdef DEBUG_SDP
72 audioEncoder_->print_sdp();
73#endif
74
75 return true;
76}
77
78void
79AudioSender::update(Observable<std::shared_ptr<jami::MediaFrame>>* /*obs*/,
80 const std::shared_ptr<jami::MediaFrame>& framePtr)
81{
82 auto frame = framePtr->pointer();
83 frame->pts = sent_samples;
84 sent_samples += frame->nb_samples;
85
86 // check for change in voice activity, if so, call callback
87 // downcast MediaFrame to AudioFrame
88 bool hasVoice = std::dynamic_pointer_cast<AudioFrame>(framePtr)->has_voice;
89 if (hasVoice != voice_) {
90 voice_ = hasVoice;
91 if (voiceCallback_) {
92 voiceCallback_(voice_);
93 } else {
94 JAMI_ERR("AudioSender no voice callback!");
95 }
96 }
97
98 if (audioEncoder_->encodeAudio(*std::static_pointer_cast<AudioFrame>(framePtr)) < 0)
99 JAMI_ERR("encoding failed");
100}
101
102void
103AudioSender::setVoiceCallback(std::function<void(bool)> cb)
104{
105 if (cb) {
106 voiceCallback_ = std::move(cb);
107 } else {
108 JAMI_ERR("AudioSender attempting to set invalid voice callback");
109 }
110}
111
114{
115 return audioEncoder_->getLastSeqValue();
116}
117
118int
120{
121 // The encoder may be destroy during a bitrate change
122 // when a codec parameter like auto quality change
123 if (!audioEncoder_)
124 return -1; // NOK
125
126 return audioEncoder_->setPacketLoss(pl);
127}
128
129} // namespace jami
AudioSender(const std::string &dest, const MediaDescription &args, SocketPair &socketPair, const uint16_t seqVal, const uint16_t mtu)
void setVoiceCallback(std::function< void(bool)> cb)
int setPacketLoss(uint64_t pl)
uint16_t getLastSeqValue()
void update(Observable< std::shared_ptr< jami::MediaFrame > > *, const std::shared_ptr< jami::MediaFrame > &) override
#define JAMI_ERR(...)
Definition logger.h:218
#define JAMI_DBG(...)
Definition logger.h:216
void emitSignal(Args... args)
Definition ring_signal.h:64
MediaDescription Negotiated RTP media slot.
std::shared_ptr< SystemCodecInfo > codec
RTP.