Ring Daemon
Loading...
Searching...
No Matches
aaudiolayer.h
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#pragma once
18
19#include "audio/audiolayer.h"
20
21#include <aaudio/AAudio.h>
22#include <jni.h>
23
24#include <thread>
25#include <mutex>
26#include <atomic>
27#include <condition_variable>
28#include <vector>
29
30namespace jami {
31
32class AAudioLayer : public AudioLayer {
33public:
36
37 // Must be called from JNI_OnLoad to enable the Java AudioTrack fallback
38 // for the ringtone stream on Android API < 28.
39 static void setJavaVM(JavaVM* vm);
40
41 void startStream(AudioDeviceType stream) override;
42 void stopStream(AudioDeviceType stream = AudioDeviceType::ALL) override;
43
44 std::vector<std::string> getCaptureDeviceList() const override;
45 std::vector<std::string> getPlaybackDeviceList() const override;
46
47 int getAudioDeviceIndex(const std::string& name, AudioDeviceType type) const override;
48 std::string getAudioDeviceName(int index, AudioDeviceType type) const override;
49
50 int getIndexCapture() const override { return 0; }
51 int getIndexPlayback() const override { return 0; }
52 int getIndexRingtone() const override { return 0; }
53
54 void updatePreference(AudioPreference& pref, int index, AudioDeviceType type) override;
55
56private:
57 struct AAudioStreamDeleter {
58 void operator()(AAudioStream* stream) const {
59 if (stream) {
60 AAudioStream_close(stream);
61 }
62 }
63 };
64 using AAudioStreamPtr = std::unique_ptr<AAudioStream, AAudioStreamDeleter>;
65
66 AAudioStreamPtr buildStream(AudioDeviceType type);
67
68 // AAudio specific members
69 AAudioStreamPtr playStream_ {nullptr};
70 AAudioStreamPtr ringStream_ {nullptr};
71 AAudioStreamPtr recStream_ {nullptr};
72 int previousUnderrunCount_ {0};
73
74 static aaudio_data_callback_result_t dataCallback(
75 AAudioStream *stream,
76 void *userData,
77 void *audioData,
79
80 static void errorCallback(
81 AAudioStream *stream,
82 void *userData,
83 aaudio_result_t error);
84
85 void stopPlayback();
86 void stopCapture();
87
88 void loop();
89 void startStreamLocked(AudioDeviceType stream);
90 void stopStreamLocked(AudioDeviceType stream);
91
92 std::thread loopThread_;
93 std::condition_variable loopCv_;
94 std::set<AudioDeviceType> streamsToRestart_;
95 bool isRunning_ {false};
96
97 bool startJavaRingStream();
98 void stopJavaRingStream();
99 void javaRingLoop();
100
101 jobject javaRingTrack_ {nullptr}; // GlobalRef to android.media.AudioTrack
102 jfloatArray javaRingBuffer_ {nullptr}; // GlobalRef, reused each write
103 std::atomic<bool> javaRingRunning_ {false};
104 std::thread javaRingThread_;
105};
106
107}
Main sound class.
void updatePreference(AudioPreference &pref, int index, AudioDeviceType type) override
void startStream(AudioDeviceType stream) override
Start the capture stream and prepare the playback stream.
static void setJavaVM(JavaVM *vm)
int getIndexRingtone() const override
Definition aaudiolayer.h:52
std::string getAudioDeviceName(int index, AudioDeviceType type) const override
int getAudioDeviceIndex(const std::string &name, AudioDeviceType type) const override
std::vector< std::string > getPlaybackDeviceList() const override
int getIndexCapture() const override
Definition aaudiolayer.h:50
int getIndexPlayback() const override
Definition aaudiolayer.h:51
std::vector< std::string > getCaptureDeviceList() const override
void stopStream(AudioDeviceType stream=AudioDeviceType::ALL) override
Stop the playback and capture streams.
void emitSignal(Args... args)
Definition jami_signal.h:64
AudioDeviceType
Definition audiolayer.h:57