Ring Daemon 16.0.0
Loading...
Searching...
No Matches
audiostream.h
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#pragma once
18
19#include "noncopyable.h"
20#include "pulselayer.h"
21
22#include <pulse/pulseaudio.h>
23#include <string>
24
25namespace jami {
26
27inline AVSampleFormat
29switch (format)
30{
33 return AV_SAMPLE_FMT_S16;
36 return AV_SAMPLE_FMT_FLT;
39 return AV_SAMPLE_FMT_S32;
40default:
41 return AV_SAMPLE_FMT_S16;
42}
43}
44
47switch (format)
48{
50 return PA_SAMPLE_S16LE;
54 return PA_SAMPLE_S32LE;
55default:
56 return PA_SAMPLE_S16LE;
57}
58}
59
61{
62public:
63 using OnReady = std::function<void()>;
64 using OnData = std::function<void(size_t)>;
65
79 const char*,
81 unsigned,
83 const PaDeviceInfos&,
84 bool,
85 OnReady onReady,
87
89
90 void start();
91 void stop();
92
97 pa_stream* stream() { return audiostream_; }
98
99 const pa_sample_spec* sampleSpec() const { return pa_stream_get_sample_spec(audiostream_); }
100
101 inline size_t sampleSize() const { return pa_sample_size(sampleSpec()); }
102 inline size_t frameSize() const { return pa_frame_size(sampleSpec()); }
103
104 inline uint8_t channels() const { return sampleSpec()->channels; }
105
106 inline AudioFormat format() const
107 {
108 auto s = sampleSpec();
109 return AudioFormat(s->rate, s->channels, sampleFormatFromPulse(s->format));
110 }
111
112 inline std::string getDeviceName() const
113 {
114 auto res = pa_stream_get_device_name(audiostream_);
115 if (res == reinterpret_cast<decltype(res)>(-PA_ERR_NOTSUPPORTED) or !res)
116 return {};
117 return res;
118 }
119
120 bool isReady();
121
122 void setEchoCancelCb(std::function<void(bool)>&& cb) { echoCancelCb = cb; }
123
124private:
126
127 OnReady onReady_;
128 OnData onData_;
129
133 void stateChanged(pa_stream* s);
134 void moved(pa_stream* s);
135 void opEnded(pa_operation* s);
136
140 pa_stream* audiostream_;
141
145 pa_threaded_mainloop* mainloop_;
146
150 AudioDeviceType audioType_;
151
155 std::function<void(bool)> echoCancelCb;
156
157 std::mutex mutex_;
158 std::condition_variable cond_;
159 std::set<pa_operation*> ongoing_ops;
160};
161
162} // namespace jami
std::function< void()> OnReady
Definition audiostream.h:63
std::string getDeviceName() const
void setEchoCancelCb(std::function< void(bool)> &&cb)
std::function< void(size_t)> OnData
Definition audiostream.h:64
AudioFormat format() const
pa_stream * stream()
Accessor: Get the pulseaudio stream object.
Definition audiostream.h:97
size_t frameSize() const
uint8_t channels() const
const pa_sample_spec * sampleSpec() const
Definition audiostream.h:99
size_t sampleSize() const
AVSampleFormat sampleFormatFromPulse(pa_sample_format_t format)
Definition audiostream.h:28
void emitSignal(Args... args)
Definition ring_signal.h:64
pa_sample_format_t pulseSampleFormatFromAv(AVSampleFormat format)
Definition audiostream.h:46
AudioDeviceType
Definition audiolayer.h:58
Simple macro to hide class' copy constructor and assignment operator.
#define NON_COPYABLE(ClassName)
Definition noncopyable.h:30
Structure to hold sample rate and channel number associated with audio data.
Convenience structure to hold PulseAudio device propreties such as supported channel number etc.
Definition pulselayer.h:41