| 1 | /**************************************************************************/ | 
|---|
| 2 | /*  audio_driver_pulseaudio.h                                             */ | 
|---|
| 3 | /**************************************************************************/ | 
|---|
| 4 | /*                         This file is part of:                          */ | 
|---|
| 5 | /*                             GODOT ENGINE                               */ | 
|---|
| 6 | /*                        https://godotengine.org                         */ | 
|---|
| 7 | /**************************************************************************/ | 
|---|
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
|---|
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
|---|
| 10 | /*                                                                        */ | 
|---|
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining  */ | 
|---|
| 12 | /* a copy of this software and associated documentation files (the        */ | 
|---|
| 13 | /* "Software"), to deal in the Software without restriction, including    */ | 
|---|
| 14 | /* without limitation the rights to use, copy, modify, merge, publish,    */ | 
|---|
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to     */ | 
|---|
| 16 | /* permit persons to whom the Software is furnished to do so, subject to  */ | 
|---|
| 17 | /* the following conditions:                                              */ | 
|---|
| 18 | /*                                                                        */ | 
|---|
| 19 | /* The above copyright notice and this permission notice shall be         */ | 
|---|
| 20 | /* included in all copies or substantial portions of the Software.        */ | 
|---|
| 21 | /*                                                                        */ | 
|---|
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */ | 
|---|
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */ | 
|---|
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | 
|---|
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */ | 
|---|
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */ | 
|---|
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */ | 
|---|
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */ | 
|---|
| 29 | /**************************************************************************/ | 
|---|
| 30 |  | 
|---|
| 31 | #ifndef AUDIO_DRIVER_PULSEAUDIO_H | 
|---|
| 32 | #define AUDIO_DRIVER_PULSEAUDIO_H | 
|---|
| 33 |  | 
|---|
| 34 | #ifdef PULSEAUDIO_ENABLED | 
|---|
| 35 |  | 
|---|
| 36 | #include "core/os/mutex.h" | 
|---|
| 37 | #include "core/os/thread.h" | 
|---|
| 38 | #include "core/templates/safe_refcount.h" | 
|---|
| 39 | #include "servers/audio_server.h" | 
|---|
| 40 |  | 
|---|
| 41 | #ifdef SOWRAP_ENABLED | 
|---|
| 42 | #include "pulse-so_wrap.h" | 
|---|
| 43 | #else | 
|---|
| 44 | #include <pulse/pulseaudio.h> | 
|---|
| 45 | #endif | 
|---|
| 46 |  | 
|---|
| 47 | class AudioDriverPulseAudio : public AudioDriver { | 
|---|
| 48 | Thread thread; | 
|---|
| 49 | Mutex mutex; | 
|---|
| 50 |  | 
|---|
| 51 | pa_mainloop *pa_ml = nullptr; | 
|---|
| 52 | pa_context *pa_ctx = nullptr; | 
|---|
| 53 | pa_stream *pa_str = nullptr; | 
|---|
| 54 | pa_stream *pa_rec_str = nullptr; | 
|---|
| 55 | pa_channel_map pa_map = {}; | 
|---|
| 56 | pa_channel_map pa_rec_map = {}; | 
|---|
| 57 |  | 
|---|
| 58 | String output_device_name = "Default"; | 
|---|
| 59 | String new_output_device = "Default"; | 
|---|
| 60 | String default_output_device; | 
|---|
| 61 |  | 
|---|
| 62 | String input_device_name; | 
|---|
| 63 | String new_input_device; | 
|---|
| 64 | String default_input_device; | 
|---|
| 65 |  | 
|---|
| 66 | Vector<int32_t> samples_in; | 
|---|
| 67 | Vector<int16_t> samples_out; | 
|---|
| 68 |  | 
|---|
| 69 | unsigned int mix_rate = 0; | 
|---|
| 70 | unsigned int buffer_frames = 0; | 
|---|
| 71 | unsigned int pa_buffer_size = 0; | 
|---|
| 72 | int channels = 0; | 
|---|
| 73 | int pa_ready = 0; | 
|---|
| 74 | int pa_status = 0; | 
|---|
| 75 | PackedStringArray pa_devices; | 
|---|
| 76 | PackedStringArray pa_rec_devices; | 
|---|
| 77 |  | 
|---|
| 78 | SafeFlag active; | 
|---|
| 79 | SafeFlag exit_thread; | 
|---|
| 80 |  | 
|---|
| 81 | float latency = 0; | 
|---|
| 82 |  | 
|---|
| 83 | static void pa_state_cb(pa_context *c, void *userdata); | 
|---|
| 84 | static void pa_sink_info_cb(pa_context *c, const pa_sink_info *l, int eol, void *userdata); | 
|---|
| 85 | static void pa_source_info_cb(pa_context *c, const pa_source_info *l, int eol, void *userdata); | 
|---|
| 86 | static void pa_server_info_cb(pa_context *c, const pa_server_info *i, void *userdata); | 
|---|
| 87 | static void pa_sinklist_cb(pa_context *c, const pa_sink_info *l, int eol, void *userdata); | 
|---|
| 88 | static void pa_sourcelist_cb(pa_context *c, const pa_source_info *l, int eol, void *userdata); | 
|---|
| 89 |  | 
|---|
| 90 | Error init_output_device(); | 
|---|
| 91 | void finish_output_device(); | 
|---|
| 92 |  | 
|---|
| 93 | Error init_input_device(); | 
|---|
| 94 | void finish_input_device(); | 
|---|
| 95 |  | 
|---|
| 96 | Error detect_channels(bool capture = false); | 
|---|
| 97 |  | 
|---|
| 98 | static void thread_func(void *p_udata); | 
|---|
| 99 |  | 
|---|
| 100 | public: | 
|---|
| 101 | virtual const char *get_name() const override { | 
|---|
| 102 | return "PulseAudio"; | 
|---|
| 103 | }; | 
|---|
| 104 |  | 
|---|
| 105 | virtual Error init() override; | 
|---|
| 106 | virtual void start() override; | 
|---|
| 107 | virtual int get_mix_rate() const override; | 
|---|
| 108 | virtual SpeakerMode get_speaker_mode() const override; | 
|---|
| 109 | virtual float get_latency() override; | 
|---|
| 110 |  | 
|---|
| 111 | virtual void lock() override; | 
|---|
| 112 | virtual void unlock() override; | 
|---|
| 113 | virtual void finish() override; | 
|---|
| 114 |  | 
|---|
| 115 | virtual PackedStringArray get_output_device_list() override; | 
|---|
| 116 | virtual String get_output_device() override; | 
|---|
| 117 | virtual void set_output_device(const String &p_name) override; | 
|---|
| 118 |  | 
|---|
| 119 | virtual Error input_start() override; | 
|---|
| 120 | virtual Error input_stop() override; | 
|---|
| 121 |  | 
|---|
| 122 | virtual PackedStringArray get_input_device_list() override; | 
|---|
| 123 | virtual String get_input_device() override; | 
|---|
| 124 | virtual void set_input_device(const String &p_name) override; | 
|---|
| 125 |  | 
|---|
| 126 | AudioDriverPulseAudio(); | 
|---|
| 127 | ~AudioDriverPulseAudio() {} | 
|---|
| 128 | }; | 
|---|
| 129 |  | 
|---|
| 130 | #endif // PULSEAUDIO_ENABLED | 
|---|
| 131 |  | 
|---|
| 132 | #endif // AUDIO_DRIVER_PULSEAUDIO_H | 
|---|
| 133 |  | 
|---|