Ring Daemon 16.0.0
Loading...
Searching...
No Matches
audio_frame_resizer.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_frame_resizer.h"
19#include "libav_deps.h"
20#include "logger.h"
21
22extern "C" {
23#include <libavutil/audio_fifo.h>
24}
25
26#include <stdexcept>
27
28namespace jami {
29
31 int size,
32 std::function<void(std::shared_ptr<AudioFrame>&&)> cb)
33 : format_(format)
34 , frameSize_(size)
35 , cb_(cb)
36 , queue_(av_audio_fifo_alloc(format.sampleFormat, format.nb_channels, frameSize_))
37{}
38
43
44int
46{
47 return av_audio_fifo_size(queue_);
48}
49
50int
52{
53 return frameSize_;
54}
55
58{
59 return format_;
60}
61
62void
64{
65 if (size)
66 setFrameSize(size);
67 if (format != format_) {
68 if (auto discarded = samples())
69 JAMI_WARN("Discarding %d samples", discarded);
70 av_audio_fifo_free(queue_);
71 format_ = format;
73 }
74}
75
76void
78{
79 if (frameSize_ != frameSize) {
80 frameSize_ = frameSize;
81 if (cb_)
82 while (auto frame = dequeue())
83 cb_(std::move(frame));
84 }
85}
86
87void
88AudioFrameResizer::enqueue(std::shared_ptr<AudioFrame>&& frame)
89{
90 if (not frame or frame->pointer() == nullptr)
91 return;
92
93 int ret = 0;
94 auto f = frame->pointer();
95 AudioFormat format(f->sample_rate, f->ch_layout.nb_channels, (AVSampleFormat) f->format);
96 if (format != format_) {
97 JAMI_WARNING("Expected {} but got {}", format_.toString(), format.toString());
98 setFormat(format, frameSize_);
99 }
100
101 auto nb_samples = samples();
102 if (cb_ && nb_samples == 0 && f->nb_samples == frameSize_) {
103 nextOutputPts_ = frame->pointer()->pts + frameSize_;
104 cb_(std::move(frame));
105 return; // return if frame was just passed through
106 }
107
108 // voice activity
109 hasVoice_ = frame->has_voice;
110
111 // queue reallocates itself if need be
112 if ((ret = av_audio_fifo_write(queue_, reinterpret_cast<void**>(f->data), f->nb_samples)) < 0) {
113 JAMI_ERR() << "Audio resizer error: " << libav_utils::getError(ret);
114 throw std::runtime_error("Failed to add audio to frame resizer");
115 }
116
117 if (nextOutputPts_ == 0)
118 nextOutputPts_ = frame->pointer()->pts - nb_samples;
119
120 if (cb_)
121 while (auto frame = dequeue())
122 cb_(std::move(frame));
123}
124
125std::shared_ptr<AudioFrame>
127{
128 if (samples() < frameSize_)
129 return {};
130
131 auto frame = std::make_shared<AudioFrame>(format_, frameSize_);
132 int ret;
133 if ((ret = av_audio_fifo_read(queue_,
134 reinterpret_cast<void**>(frame->pointer()->data),
135 frameSize_))
136 < 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:218
#define JAMI_WARN(...)
Definition logger.h:217
#define JAMI_WARNING(formatstr,...)
Definition logger.h:227
std::string getError(int err)
void emitSignal(Args... args)
Definition ring_signal.h:64
Structure to hold sample rate and channel number associated with audio data.
AVSampleFormat sampleFormat
std::string toString() const