Ring Daemon 16.0.0
Loading...
Searching...
No Matches
alsalayer.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 "audio/audiolayer.h"
20#include "noncopyable.h"
21
22#include <alsa/asoundlib.h>
23
24#include <memory>
25#include <thread>
26
27#define PCM_DMIX "plug:dmix"
29// Error codes for error handling
30#define ALSA_CAPTURE_DEVICE 0x0001
31#define ALSA_PLAYBACK_DEVICE 0x0010
38namespace jami {
39
40class AlsaThread;
41class RingBuffer;
42class AudioPreference;
43
45typedef std::pair<int, std::string> HwIDPair;
46
47class AlsaLayer : public AudioLayer
48{
49public:
54
58 ~AlsaLayer();
59
66
72 virtual void stopStream(AudioDeviceType stream = AudioDeviceType::ALL);
73
80 std::string buildDeviceTopo(const std::string& plugin, int card);
81
86 virtual std::vector<std::string> getCaptureDeviceList() const;
87 virtual std::vector<std::string> getPlaybackDeviceList() const;
88
100 static bool soundCardIndexExists(int card, AudioDeviceType stream);
101
107 int getAudioDeviceIndex(const std::string& description, AudioDeviceType type) const;
108 std::string getAudioDeviceName(int index, AudioDeviceType type) const;
109
110 void playback();
111 void ringtone();
112 void capture();
113
119 virtual int getIndexCapture() const { return indexIn_; }
120
126 virtual int getIndexPlayback() const { return indexOut_; }
127
133 virtual int getIndexRingtone() const { return indexRing_; }
134
135 void run();
136
137private:
141 std::vector<HwIDPair> getAudioDeviceIndexMap(bool getCapture) const;
142
148 bool openDevice(snd_pcm_t** pcm,
149 const std::string& dev,
150 snd_pcm_stream_t stream,
151 AudioFormat& format);
152
156 int indexIn_;
157
161 int indexOut_;
162
166 int indexRing_;
167
169
174 void closeCaptureStream();
175 void stopCaptureStream();
176 void startCaptureStream();
177 void prepareCaptureStream();
178
179 void closePlaybackStream();
180 void stopPlaybackStream();
181 void startPlaybackStream();
182 void closeRingtoneStream();
183
184 bool alsa_set_params(snd_pcm_t* pcm_handle, AudioFormat& format);
185
186 void startThread();
187 void stopThread();
188
195 void write(const AudioFrame& buffer, snd_pcm_t* handle);
196
204 std::unique_ptr<AudioFrame> read(unsigned frames);
205
206 virtual void updatePreference(AudioPreference& pref, int index, AudioDeviceType type);
207
212 snd_pcm_t* playbackHandle_ {nullptr};
213
218 snd_pcm_t* ringtoneHandle_ {nullptr};
219
224 snd_pcm_t* captureHandle_ {nullptr};
225
229 std::string audioPlugin_;
230
231 bool is_capture_prepared_ {false};
232 bool is_playback_running_ {false};
233 bool is_capture_running_ {false};
234 bool is_playback_open_ {false};
235 bool is_capture_open_ {false};
236
237 std::atomic_bool running_ {false};
238 std::thread audioThread_;
239};
240
241} // namespace jami
Main sound class.
virtual int getIndexPlayback() const
Get the index of the audio card for playback.
Definition alsalayer.h:126
virtual int getIndexCapture() const
Get the index of the audio card for capture.
Definition alsalayer.h:119
std::string getAudioDeviceName(int index, AudioDeviceType type) const
~AlsaLayer()
Destructor.
Definition alsalayer.cpp:47
std::string buildDeviceTopo(const std::string &plugin, int card)
Concatenate two strings.
int getAudioDeviceIndex(const std::string &description, AudioDeviceType type) const
An index is associated with its string description.
virtual std::vector< std::string > getCaptureDeviceList() const
Scan the sound card available on the system.
virtual std::vector< std::string > getPlaybackDeviceList() const
virtual int getIndexRingtone() const
Get the index of the audio card for ringtone (could be differnet from playback)
Definition alsalayer.h:133
void run()
Reimplementation of run()
Definition alsalayer.cpp:62
static bool soundCardIndexExists(int card, AudioDeviceType stream)
Check if the given index corresponds to an existing sound card and supports the specified streaming m...
virtual void startStream(AudioDeviceType stream=AudioDeviceType::ALL)
Start the capture stream and prepare the playback stream.
virtual void stopStream(AudioDeviceType stream=AudioDeviceType::ALL)
Stop the playback and capture streams.
Definition Address.h:25
void emitSignal(Args... args)
Definition ring_signal.h:64
std::pair< int, std::string > HwIDPair
Associate a sound card index to its string description.
Definition alsalayer.h:45
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.