Ring Daemon
Loading...
Searching...
No Matches
audio_frame_resizer.cpp
Go to the documentation of this file.
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_frame_resizer.h"
19#include "libav_deps.h"
20#include "libav_utils.h"
21#include "logger.h"
22#include <libavutil/frame.h>
23
24extern "C" {
25#include <libavutil/audio_fifo.h>
26}
27
28#include <stdexcept>
29#include <utility>
30
31namespace jami {
32
34 int size,
35 std::function<void(std::shared_ptr<AudioFrame>&&)> cb)
36 : format_(format)
37 , frameSize_(size)
38 , cb_(std::move(cb))
39 , queue_(av_audio_fifo_alloc(format.sampleFormat, static_cast<int>(format.nb_channels), frameSize_))
40{}
41
46
47int
49{
50 return av_audio_fifo_size(queue_);
51}
52
53int
55{
56 return frameSize_;
57}
58
61{
62 return format_;
63}
64
65void
67{
68 if (size)
69 setFrameSize(size);
70 if (format != format_) {
71 if (auto discarded = samples())
72 JAMI_WARN("Discarding %d samples", discarded);
73 av_audio_fifo_free(queue_);
74 format_ = format;
75 queue_ = av_audio_fifo_alloc(format.sampleFormat, static_cast<int>(format.nb_channels), frameSize_);
76 }
77}
78
79void
81{
82 if (frameSize_ != frameSize) {
83 frameSize_ = frameSize;
84 if (cb_)
85 while (auto frame = dequeue())
86 cb_(std::move(frame));
87 }
88}
89
90void
91AudioFrameResizer::enqueue(std::shared_ptr<AudioFrame>&& frame)
92{
93 if (not frame or frame->pointer() == nullptr)
94 return;
95
96 int ret = 0;
97 auto* f = frame->pointer();
98 AudioFormat format(f->sample_rate, f->ch_layout.nb_channels, (AVSampleFormat) f->format);
99 if (format != format_) {
100 JAMI_WARNING("Expected {} but got {}", format_.toString(), format.toString());
101 setFormat(format, frameSize_);
102 }
103
104 auto nb_samples = samples();
105 if (cb_ && nb_samples == 0 && f->nb_samples == frameSize_) {
106 nextOutputPts_ = frame->pointer()->pts + frameSize_;
107 cb_(std::move(frame));
108 return; // return if frame was just passed through
109 }
110
111 // voice activity
112 hasVoice_ = frame->has_voice;
113
114 // queue reallocates itself if need be
115 if ((ret = av_audio_fifo_write(queue_, reinterpret_cast<void**>(f->data), f->nb_samples)) < 0) {
116 JAMI_ERR() << "Audio resizer error: " << libav_utils::getError(ret);
117 throw std::runtime_error("Failed to add audio to frame resizer");
118 }
119
120 if (nextOutputPts_ == 0)
121 nextOutputPts_ = frame->pointer()->pts - nb_samples;
122
123 if (cb_)
124 while (auto frame = dequeue())
125 cb_(std::move(frame));
126}
127
128std::shared_ptr<AudioFrame>
130{
131 if (samples() < frameSize_)
132 return {};
133
134 auto frame = std::make_shared<AudioFrame>(format_, frameSize_);
135 int ret;
136 if ((ret = av_audio_fifo_read(queue_, reinterpret_cast<void**>(frame->pointer()->data), frameSize_)) < 0) {
137 JAMI_ERR() << "Unable to read samples from queue: " << libav_utils::getError(ret);
138 return {};
139 }
140 frame->pointer()->pts = nextOutputPts_;
141 frame->has_voice = hasVoice_;
142 nextOutputPts_ += frameSize_;
143 return frame;
144}
145
146} // namespace jami
void setFrameSize(int frameSize)
void setFormat(const AudioFormat &format, int frameSize)
AudioFrameResizer(const AudioFormat &format, int frameSize, std::function< void(std::shared_ptr< AudioFrame > &&)> cb={})
int frameSize() const
Gets the number of samples per output frame.
int samples() const
Gets the numbers of samples available for reading.
void enqueue(std::shared_ptr< AudioFrame > &&frame)
Write @frame's data to the queue.
std::shared_ptr< AudioFrame > dequeue()
Notifies owner of a new frame.
AudioFormat format() const
Gets the format used by @queue_, input frames must match this format or enqueuing will fail.
#define JAMI_ERR(...)
Definition logger.h:230
#define JAMI_WARN(...)
Definition logger.h:229
#define JAMI_WARNING(formatstr,...)
Definition logger.h:242
std::string getError(int err)
void emitSignal(Args... args)
Definition jami_signal.h:64
Structure to hold sample rate and channel number associated with audio data.
AVSampleFormat sampleFormat
std::string toString() const