summaryrefslogtreecommitdiff
path: root/drivers/pulseaudio/audio_driver_pulseaudio.cpp
blob: e4c8641a3b801429d084dc8425cc90c6575eb389 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*************************************************************************/
/*  audio_driver_pulseaudio.cpp                                          */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md)    */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/
#include "audio_driver_pulseaudio.h"

#ifdef PULSEAUDIO_ENABLED

#include <pulse/pulseaudio.h>

#include "os/os.h"
#include "project_settings.h"

void pa_state_cb(pa_context *c, void *userdata) {
	pa_context_state_t state;
	int *pa_ready = (int *)userdata;

	state = pa_context_get_state(c);
	switch (state) {
		case PA_CONTEXT_FAILED:
		case PA_CONTEXT_TERMINATED:
			*pa_ready = 2;
			break;

		case PA_CONTEXT_READY:
			*pa_ready = 1;
			break;
	}
}

void sink_info_cb(pa_context *c, const pa_sink_info *l, int eol, void *userdata) {
	unsigned int *channels = (unsigned int *)userdata;

	// If eol is set to a positive number, you're at the end of the list
	if (eol > 0) {
		return;
	}

	*channels = l->channel_map.channels;
}

void server_info_cb(pa_context *c, const pa_server_info *i, void *userdata) {
	char *default_output = (char *)userdata;

	strncpy(default_output, i->default_sink_name, 1024);
}

static unsigned int detect_channels() {

	pa_mainloop *pa_ml;
	pa_mainloop_api *pa_mlapi;
	pa_operation *pa_op;
	pa_context *pa_ctx;

	int state = 0;
	int pa_ready = 0;

	char default_output[1024];
	unsigned int channels = 2;

	pa_ml = pa_mainloop_new();
	pa_mlapi = pa_mainloop_get_api(pa_ml);
	pa_ctx = pa_context_new(pa_mlapi, "Godot");

	int ret = pa_context_connect(pa_ctx, NULL, PA_CONTEXT_NOFLAGS, NULL);
	if (ret < 0) {
		pa_context_unref(pa_ctx);
		pa_mainloop_free(pa_ml);

		return 2;
	}

	pa_context_set_state_callback(pa_ctx, pa_state_cb, &pa_ready);

	// Wait until the pa server is ready
	while (pa_ready == 0) {
		pa_mainloop_iterate(pa_ml, 1, NULL);
	}

	// Check if there was an error connecting to the pa server
	if (pa_ready == 2) {
		pa_context_disconnect(pa_ctx);
		pa_context_unref(pa_ctx);
		pa_mainloop_free(pa_ml);

		return 2;
	}

	// Get the default output device name
	pa_op = pa_context_get_server_info(pa_ctx, &server_info_cb, (void *)default_output);
	if (pa_op) {
		while (pa_operation_get_state(pa_op) == PA_OPERATION_RUNNING) {
			ret = pa_mainloop_iterate(pa_ml, 1, NULL);
			if (ret < 0) {
				ERR_PRINT("pa_mainloop_iterate error");
			}
		}

		pa_operation_unref(pa_op);

		// Now using the device name get the amount of channels
		pa_op = pa_context_get_sink_info_by_name(pa_ctx, default_output, &sink_info_cb, (void *)&channels);
		if (pa_op) {
			while (pa_operation_get_state(pa_op) == PA_OPERATION_RUNNING) {
				ret = pa_mainloop_iterate(pa_ml, 1, NULL);
				if (ret < 0) {
					ERR_PRINT("pa_mainloop_iterate error");
				}
			}

			pa_operation_unref(pa_op);
		} else {
			ERR_PRINT("pa_context_get_sink_info_by_name error");
		}
	} else {
		ERR_PRINT("pa_context_get_server_info error");
	}

	pa_context_disconnect(pa_ctx);
	pa_context_unref(pa_ctx);
	pa_mainloop_free(pa_ml);

	return channels;
}

Error AudioDriverPulseAudio::init() {

	active = false;
	thread_exited = false;
	exit_thread = false;

	mix_rate = GLOBAL_DEF("audio/mix_rate", DEFAULT_MIX_RATE);
	channels = detect_channels();

	switch (channels) {
		case 2: // Stereo
		case 4: // Surround 3.1
		case 6: // Surround 5.1
		case 8: // Surround 7.1
			break;

		default:
			ERR_PRINTS("PulseAudio: Unsupported number of channels: " + itos(channels));
			ERR_FAIL_V(ERR_CANT_OPEN);
			break;
	}

	pa_sample_spec spec;
	spec.format = PA_SAMPLE_S16LE;
	spec.channels = channels;
	spec.rate = mix_rate;

	int latency = GLOBAL_DEF("audio/output_latency", DEFAULT_OUTPUT_LATENCY);
	buffer_frames = closest_power_of_2(latency * mix_rate / 1000);
	buffer_size = buffer_frames * channels;

	if (OS::get_singleton()->is_stdout_verbose()) {
		print_line("PulseAudio: detected " + itos(channels) + " channels");
		print_line("PulseAudio: audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms");
	}

	pa_buffer_attr attr;
	// set to appropriate buffer length (in bytes) from global settings
	attr.tlength = buffer_size * sizeof(int16_t);
	// set them to be automatically chosen
	attr.prebuf = (uint32_t)-1;
	attr.maxlength = (uint32_t)-1;
	attr.minreq = (uint32_t)-1;

	int error_code;
	pulse = pa_simple_new(NULL, // default server
			"Godot", // application name
			PA_STREAM_PLAYBACK,
			NULL, // default device
			"Sound", // stream description
			&spec,
			NULL, // use default channel map
			&attr, // use buffering attributes from above
			&error_code);

	if (pulse == NULL) {
		fprintf(stderr, "PulseAudio ERR: %s\n", pa_strerror(error_code));
		ERR_FAIL_COND_V(pulse == NULL, ERR_CANT_OPEN);
	}

	samples_in.resize(buffer_size);
	samples_out.resize(buffer_size);

	mutex = Mutex::create();
	thread = Thread::create(AudioDriverPulseAudio::thread_func, this);

	return OK;
}

float AudioDriverPulseAudio::get_latency() {

	if (latency == 0) { //only do this once since it's approximate anyway
		int error_code;
		pa_usec_t palat = pa_simple_get_latency(pulse, &error_code);
		latency = double(palat) / 1000000.0;
	}

	return latency;
}

void AudioDriverPulseAudio::thread_func(void *p_udata) {

	AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)p_udata;

	while (!ad->exit_thread) {
		if (!ad->active) {
			for (unsigned int i = 0; i < ad->buffer_size; i++) {
				ad->samples_out[i] = 0;
			}

		} else {
			ad->lock();

			ad->audio_server_process(ad->buffer_frames, ad->samples_in.ptr());

			ad->unlock();

			for (unsigned int i = 0; i < ad->buffer_size; i++) {
				ad->samples_out[i] = ad->samples_in[i] >> 16;
			}
		}

		// pa_simple_write always consumes the entire buffer

		int error_code;
		int byte_size = ad->buffer_size * sizeof(int16_t);
		if (pa_simple_write(ad->pulse, ad->samples_out.ptr(), byte_size, &error_code) < 0) {
			// can't recover here
			fprintf(stderr, "PulseAudio failed and can't recover: %s\n", pa_strerror(error_code));
			ad->active = false;
			ad->exit_thread = true;
			break;
		}
	}

	ad->thread_exited = true;
}

void AudioDriverPulseAudio::start() {

	active = true;
}

int AudioDriverPulseAudio::get_mix_rate() const {

	return mix_rate;
}

AudioDriver::SpeakerMode AudioDriverPulseAudio::get_speaker_mode() const {

	return get_speaker_mode_by_total_channels(channels);
}

void AudioDriverPulseAudio::lock() {

	if (!thread || !mutex)
		return;
	mutex->lock();
}

void AudioDriverPulseAudio::unlock() {

	if (!thread || !mutex)
		return;
	mutex->unlock();
}

void AudioDriverPulseAudio::finish() {

	if (!thread)
		return;

	exit_thread = true;
	Thread::wait_to_finish(thread);

	if (pulse) {
		pa_simple_free(pulse);
		pulse = NULL;
	}

	memdelete(thread);
	if (mutex) {
		memdelete(mutex);
		mutex = NULL;
	}

	thread = NULL;
}

AudioDriverPulseAudio::AudioDriverPulseAudio() {

	mutex = NULL;
	thread = NULL;
	pulse = NULL;

	samples_in.clear();
	samples_out.clear();

	mix_rate = 0;
	buffer_size = 0;
	channels = 0;

	active = false;
	thread_exited = false;
	exit_thread = false;

	latency = 0;
	buffer_frames = 0;
	buffer_size = 0;
	channels = 0;
}

AudioDriverPulseAudio::~AudioDriverPulseAudio() {
}

#endif