1 /*************************************************************************/
2 /*  audio_driver_pulseaudio.h                                            */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
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 "servers/audio_server.h"
39 
40 #include <pulse/pulseaudio.h>
41 
42 class AudioDriverPulseAudio : public AudioDriver {
43 
44 	Thread *thread;
45 	Mutex *mutex;
46 
47 	pa_mainloop *pa_ml;
48 	pa_context *pa_ctx;
49 	pa_stream *pa_str;
50 	pa_stream *pa_rec_str;
51 	pa_channel_map pa_map;
52 	pa_channel_map pa_rec_map;
53 
54 	String device_name;
55 	String new_device;
56 	String default_device;
57 
58 	String capture_device_name;
59 	String capture_new_device;
60 	String capture_default_device;
61 
62 	Vector<int32_t> samples_in;
63 	Vector<int16_t> samples_out;
64 
65 	unsigned int mix_rate;
66 	unsigned int buffer_frames;
67 	unsigned int pa_buffer_size;
68 	int channels;
69 	int pa_ready;
70 	int pa_status;
71 	Array pa_devices;
72 	Array pa_rec_devices;
73 
74 	bool active;
75 	bool thread_exited;
76 	mutable bool exit_thread;
77 
78 	float latency;
79 
80 	static void pa_state_cb(pa_context *c, void *userdata);
81 	static void pa_sink_info_cb(pa_context *c, const pa_sink_info *l, int eol, void *userdata);
82 	static void pa_source_info_cb(pa_context *c, const pa_source_info *l, int eol, void *userdata);
83 	static void pa_server_info_cb(pa_context *c, const pa_server_info *i, void *userdata);
84 	static void pa_sinklist_cb(pa_context *c, const pa_sink_info *l, int eol, void *userdata);
85 	static void pa_sourcelist_cb(pa_context *c, const pa_source_info *l, int eol, void *userdata);
86 
87 	Error init_device();
88 	void finish_device();
89 
90 	Error capture_init_device();
91 	void capture_finish_device();
92 
93 	void detect_channels(bool capture = false);
94 
95 	static void thread_func(void *p_udata);
96 
97 public:
get_name()98 	const char *get_name() const {
99 		return "PulseAudio";
100 	};
101 
102 	virtual Error init();
103 	virtual void start();
104 	virtual int get_mix_rate() const;
105 	virtual SpeakerMode get_speaker_mode() const;
106 
107 	virtual Array get_device_list();
108 	virtual String get_device();
109 	virtual void set_device(String device);
110 
111 	virtual Array capture_get_device_list();
112 	virtual void capture_set_device(const String &p_name);
113 	virtual String capture_get_device();
114 
115 	virtual void lock();
116 	virtual void unlock();
117 	virtual void finish();
118 
119 	virtual float get_latency();
120 
121 	virtual Error capture_start();
122 	virtual Error capture_stop();
123 
124 	AudioDriverPulseAudio();
125 	~AudioDriverPulseAudio();
126 };
127 
128 #endif // PULSEAUDIO_ENABLED
129 
130 #endif // AUDIO_DRIVER_PULSEAUDIO_H
131