audio_driver_pulseaudio.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /*************************************************************************/
  2. /* audio_driver_pulseaudio.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. #include "audio_driver_pulseaudio.h"
  31. #ifdef PULSEAUDIO_ENABLED
  32. #include "core/os/os.h"
  33. #include "core/project_settings.h"
  34. void AudioDriverPulseAudio::pa_state_cb(pa_context *c, void *userdata) {
  35. AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)userdata;
  36. switch (pa_context_get_state(c)) {
  37. case PA_CONTEXT_TERMINATED:
  38. case PA_CONTEXT_FAILED:
  39. ad->pa_ready = -1;
  40. break;
  41. case PA_CONTEXT_READY:
  42. ad->pa_ready = 1;
  43. break;
  44. default:
  45. // TODO: Check if we want to handle some of the other
  46. // PA context states like PA_CONTEXT_UNCONNECTED.
  47. break;
  48. }
  49. }
  50. void AudioDriverPulseAudio::pa_sink_info_cb(pa_context *c, const pa_sink_info *l, int eol, void *userdata) {
  51. AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)userdata;
  52. // If eol is set to a positive number, you're at the end of the list
  53. if (eol > 0) {
  54. return;
  55. }
  56. ad->pa_map = l->channel_map;
  57. ad->pa_status++;
  58. }
  59. void AudioDriverPulseAudio::pa_source_info_cb(pa_context *c, const pa_source_info *l, int eol, void *userdata) {
  60. AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)userdata;
  61. // If eol is set to a positive number, you're at the end of the list
  62. if (eol > 0) {
  63. return;
  64. }
  65. ad->pa_rec_map = l->channel_map;
  66. ad->pa_status++;
  67. }
  68. void AudioDriverPulseAudio::pa_server_info_cb(pa_context *c, const pa_server_info *i, void *userdata) {
  69. AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)userdata;
  70. ad->capture_default_device = i->default_source_name;
  71. ad->default_device = i->default_sink_name;
  72. ad->pa_status++;
  73. }
  74. void AudioDriverPulseAudio::detect_channels(bool capture) {
  75. pa_channel_map_init_stereo(capture ? &pa_rec_map : &pa_map);
  76. String device = capture ? capture_device_name : device_name;
  77. if (device == "Default") {
  78. // Get the default output device name
  79. pa_status = 0;
  80. pa_operation *pa_op = pa_context_get_server_info(pa_ctx, &AudioDriverPulseAudio::pa_server_info_cb, (void *)this);
  81. if (pa_op) {
  82. while (pa_status == 0) {
  83. int ret = pa_mainloop_iterate(pa_ml, 1, NULL);
  84. if (ret < 0) {
  85. ERR_PRINT("pa_mainloop_iterate error");
  86. }
  87. }
  88. pa_operation_unref(pa_op);
  89. } else {
  90. ERR_PRINT("pa_context_get_server_info error");
  91. }
  92. }
  93. char dev[1024];
  94. if (device == "Default") {
  95. strcpy(dev, capture ? capture_default_device.utf8().get_data() : default_device.utf8().get_data());
  96. } else {
  97. strcpy(dev, device.utf8().get_data());
  98. }
  99. // Now using the device name get the amount of channels
  100. pa_status = 0;
  101. pa_operation *pa_op;
  102. if (capture) {
  103. pa_op = pa_context_get_source_info_by_name(pa_ctx, dev, &AudioDriverPulseAudio::pa_source_info_cb, (void *)this);
  104. } else {
  105. pa_op = pa_context_get_sink_info_by_name(pa_ctx, dev, &AudioDriverPulseAudio::pa_sink_info_cb, (void *)this);
  106. }
  107. if (pa_op) {
  108. while (pa_status == 0) {
  109. int ret = pa_mainloop_iterate(pa_ml, 1, NULL);
  110. if (ret < 0) {
  111. ERR_PRINT("pa_mainloop_iterate error");
  112. }
  113. }
  114. pa_operation_unref(pa_op);
  115. } else {
  116. if (capture) {
  117. ERR_PRINT("pa_context_get_source_info_by_name error");
  118. } else {
  119. ERR_PRINT("pa_context_get_sink_info_by_name error");
  120. }
  121. }
  122. }
  123. Error AudioDriverPulseAudio::init_device() {
  124. // If there is a specified device check that it is really present
  125. if (device_name != "Default") {
  126. Array list = get_device_list();
  127. if (list.find(device_name) == -1) {
  128. device_name = "Default";
  129. new_device = "Default";
  130. }
  131. }
  132. // Detect the amount of channels PulseAudio is using
  133. // Note: If using an even amount of channels (2, 4, etc) channels and pa_map.channels will be equal,
  134. // if not then pa_map.channels will have the real amount of channels PulseAudio is using and channels
  135. // will have the amount of channels Godot is using (in this case it's pa_map.channels + 1)
  136. detect_channels();
  137. switch (pa_map.channels) {
  138. case 1: // Mono
  139. case 3: // Surround 2.1
  140. case 5: // Surround 5.0
  141. case 7: // Surround 7.0
  142. channels = pa_map.channels + 1;
  143. break;
  144. case 2: // Stereo
  145. case 4: // Surround 4.0
  146. case 6: // Surround 5.1
  147. case 8: // Surround 7.1
  148. channels = pa_map.channels;
  149. break;
  150. default:
  151. WARN_PRINTS("PulseAudio: Unsupported number of channels: " + itos(pa_map.channels));
  152. pa_channel_map_init_stereo(&pa_map);
  153. channels = 2;
  154. break;
  155. }
  156. int latency = GLOBAL_DEF_RST("audio/output_latency", DEFAULT_OUTPUT_LATENCY);
  157. buffer_frames = closest_power_of_2(latency * mix_rate / 1000);
  158. pa_buffer_size = buffer_frames * pa_map.channels;
  159. print_verbose("PulseAudio: detected " + itos(pa_map.channels) + " channels");
  160. print_verbose("PulseAudio: audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms");
  161. pa_sample_spec spec;
  162. spec.format = PA_SAMPLE_S16LE;
  163. spec.channels = pa_map.channels;
  164. spec.rate = mix_rate;
  165. pa_str = pa_stream_new(pa_ctx, "Sound", &spec, &pa_map);
  166. if (pa_str == NULL) {
  167. ERR_PRINTS("PulseAudio: pa_stream_new error: " + String(pa_strerror(pa_context_errno(pa_ctx))));
  168. ERR_FAIL_V(ERR_CANT_OPEN);
  169. }
  170. pa_buffer_attr attr;
  171. // set to appropriate buffer length (in bytes) from global settings
  172. // Note: PulseAudio defaults to 4 fragments, which means that the actual
  173. // latency is tlength / fragments. It seems that the PulseAudio has no way
  174. // to get the fragments number so we're hardcoding this to the default of 4
  175. const int fragments = 4;
  176. attr.tlength = pa_buffer_size * sizeof(int16_t) * fragments;
  177. // set them to be automatically chosen
  178. attr.prebuf = (uint32_t)-1;
  179. attr.maxlength = (uint32_t)-1;
  180. attr.minreq = (uint32_t)-1;
  181. const char *dev = device_name == "Default" ? NULL : device_name.utf8().get_data();
  182. pa_stream_flags flags = pa_stream_flags(PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE);
  183. int error_code = pa_stream_connect_playback(pa_str, dev, &attr, flags, NULL, NULL);
  184. ERR_FAIL_COND_V(error_code < 0, ERR_CANT_OPEN);
  185. samples_in.resize(buffer_frames * channels);
  186. samples_out.resize(pa_buffer_size);
  187. // Reset audio input to keep synchronisation.
  188. input_position = 0;
  189. input_size = 0;
  190. return OK;
  191. }
  192. Error AudioDriverPulseAudio::init() {
  193. active = false;
  194. thread_exited = false;
  195. exit_thread = false;
  196. mix_rate = GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE);
  197. pa_ml = pa_mainloop_new();
  198. ERR_FAIL_COND_V(pa_ml == NULL, ERR_CANT_OPEN);
  199. pa_ctx = pa_context_new(pa_mainloop_get_api(pa_ml), "Godot");
  200. ERR_FAIL_COND_V(pa_ctx == NULL, ERR_CANT_OPEN);
  201. pa_ready = 0;
  202. pa_context_set_state_callback(pa_ctx, pa_state_cb, (void *)this);
  203. int ret = pa_context_connect(pa_ctx, NULL, PA_CONTEXT_NOFLAGS, NULL);
  204. if (ret < 0) {
  205. if (pa_ctx) {
  206. pa_context_unref(pa_ctx);
  207. pa_ctx = NULL;
  208. }
  209. if (pa_ml) {
  210. pa_mainloop_free(pa_ml);
  211. pa_ml = NULL;
  212. }
  213. return ERR_CANT_OPEN;
  214. }
  215. while (pa_ready == 0) {
  216. pa_mainloop_iterate(pa_ml, 1, NULL);
  217. }
  218. if (pa_ready < 0) {
  219. if (pa_ctx) {
  220. pa_context_disconnect(pa_ctx);
  221. pa_context_unref(pa_ctx);
  222. pa_ctx = NULL;
  223. }
  224. if (pa_ml) {
  225. pa_mainloop_free(pa_ml);
  226. pa_ml = NULL;
  227. }
  228. return ERR_CANT_OPEN;
  229. }
  230. Error err = init_device();
  231. if (err == OK) {
  232. mutex = Mutex::create();
  233. thread = Thread::create(AudioDriverPulseAudio::thread_func, this);
  234. }
  235. return OK;
  236. }
  237. float AudioDriverPulseAudio::get_latency() {
  238. if (latency == 0) { //only do this once since it's approximate anyway
  239. lock();
  240. pa_usec_t palat = 0;
  241. if (pa_stream_get_state(pa_str) == PA_STREAM_READY) {
  242. int negative = 0;
  243. if (pa_stream_get_latency(pa_str, &palat, &negative) >= 0) {
  244. if (negative) {
  245. palat = 0;
  246. }
  247. }
  248. }
  249. if (palat > 0) {
  250. latency = double(palat) / 1000000.0;
  251. }
  252. unlock();
  253. }
  254. return latency;
  255. }
  256. void AudioDriverPulseAudio::thread_func(void *p_udata) {
  257. AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)p_udata;
  258. unsigned int write_ofs = 0;
  259. size_t avail_bytes = 0;
  260. while (!ad->exit_thread) {
  261. size_t read_bytes = 0;
  262. size_t written_bytes = 0;
  263. if (avail_bytes == 0) {
  264. ad->lock();
  265. ad->start_counting_ticks();
  266. if (!ad->active) {
  267. for (unsigned int i = 0; i < ad->pa_buffer_size; i++) {
  268. ad->samples_out.write[i] = 0;
  269. }
  270. } else {
  271. ad->audio_server_process(ad->buffer_frames, ad->samples_in.ptrw());
  272. if (ad->channels == ad->pa_map.channels) {
  273. for (unsigned int i = 0; i < ad->pa_buffer_size; i++) {
  274. ad->samples_out.write[i] = ad->samples_in[i] >> 16;
  275. }
  276. } else {
  277. // Uneven amount of channels
  278. unsigned int in_idx = 0;
  279. unsigned int out_idx = 0;
  280. for (unsigned int i = 0; i < ad->buffer_frames; i++) {
  281. for (int j = 0; j < ad->pa_map.channels - 1; j++) {
  282. ad->samples_out.write[out_idx++] = ad->samples_in[in_idx++] >> 16;
  283. }
  284. uint32_t l = ad->samples_in[in_idx++] >> 16;
  285. uint32_t r = ad->samples_in[in_idx++] >> 16;
  286. ad->samples_out.write[out_idx++] = (l + r) / 2;
  287. }
  288. }
  289. }
  290. avail_bytes = ad->pa_buffer_size * sizeof(int16_t);
  291. write_ofs = 0;
  292. ad->stop_counting_ticks();
  293. ad->unlock();
  294. }
  295. ad->lock();
  296. ad->start_counting_ticks();
  297. int ret;
  298. do {
  299. ret = pa_mainloop_iterate(ad->pa_ml, 0, NULL);
  300. } while (ret > 0);
  301. if (avail_bytes > 0 && pa_stream_get_state(ad->pa_str) == PA_STREAM_READY) {
  302. size_t bytes = pa_stream_writable_size(ad->pa_str);
  303. if (bytes > 0) {
  304. size_t bytes_to_write = MIN(bytes, avail_bytes);
  305. const void *ptr = ad->samples_out.ptr();
  306. ret = pa_stream_write(ad->pa_str, (char *)ptr + write_ofs, bytes_to_write, NULL, 0LL, PA_SEEK_RELATIVE);
  307. if (ret != 0) {
  308. ERR_PRINTS("PulseAudio: pa_stream_write error: " + String(pa_strerror(ret)));
  309. } else {
  310. avail_bytes -= bytes_to_write;
  311. write_ofs += bytes_to_write;
  312. written_bytes += bytes_to_write;
  313. }
  314. }
  315. }
  316. // User selected a new device, finish the current one so we'll init the new device
  317. if (ad->device_name != ad->new_device) {
  318. ad->device_name = ad->new_device;
  319. ad->finish_device();
  320. Error err = ad->init_device();
  321. if (err != OK) {
  322. ERR_PRINT("PulseAudio: init_device error");
  323. ad->device_name = "Default";
  324. ad->new_device = "Default";
  325. err = ad->init_device();
  326. if (err != OK) {
  327. ad->active = false;
  328. ad->exit_thread = true;
  329. break;
  330. }
  331. }
  332. avail_bytes = 0;
  333. write_ofs = 0;
  334. }
  335. if (ad->pa_rec_str && pa_stream_get_state(ad->pa_rec_str) == PA_STREAM_READY) {
  336. size_t bytes = pa_stream_readable_size(ad->pa_rec_str);
  337. if (bytes > 0) {
  338. const void *ptr = NULL;
  339. size_t maxbytes = ad->input_buffer.size() * sizeof(int16_t);
  340. bytes = MIN(bytes, maxbytes);
  341. ret = pa_stream_peek(ad->pa_rec_str, &ptr, &bytes);
  342. if (ret != 0) {
  343. ERR_PRINT("pa_stream_peek error");
  344. } else {
  345. int16_t *srcptr = (int16_t *)ptr;
  346. for (size_t i = bytes >> 1; i > 0; i--) {
  347. int32_t sample = int32_t(*srcptr++) << 16;
  348. ad->input_buffer_write(sample);
  349. if (ad->pa_rec_map.channels == 1) {
  350. // In case input device is single channel convert it to Stereo
  351. ad->input_buffer_write(sample);
  352. }
  353. }
  354. read_bytes += bytes;
  355. ret = pa_stream_drop(ad->pa_rec_str);
  356. if (ret != 0) {
  357. ERR_PRINT("pa_stream_drop error");
  358. }
  359. }
  360. }
  361. // User selected a new device, finish the current one so we'll init the new device
  362. if (ad->capture_device_name != ad->capture_new_device) {
  363. ad->capture_device_name = ad->capture_new_device;
  364. ad->capture_finish_device();
  365. Error err = ad->capture_init_device();
  366. if (err != OK) {
  367. ERR_PRINT("PulseAudio: capture_init_device error");
  368. ad->capture_device_name = "Default";
  369. ad->capture_new_device = "Default";
  370. err = ad->capture_init_device();
  371. if (err != OK) {
  372. ad->active = false;
  373. ad->exit_thread = true;
  374. break;
  375. }
  376. }
  377. }
  378. }
  379. ad->stop_counting_ticks();
  380. ad->unlock();
  381. // Let the thread rest a while if we haven't read or write anything
  382. if (written_bytes == 0 && read_bytes == 0) {
  383. OS::get_singleton()->delay_usec(1000);
  384. }
  385. }
  386. ad->thread_exited = true;
  387. }
  388. void AudioDriverPulseAudio::start() {
  389. active = true;
  390. }
  391. int AudioDriverPulseAudio::get_mix_rate() const {
  392. return mix_rate;
  393. }
  394. AudioDriver::SpeakerMode AudioDriverPulseAudio::get_speaker_mode() const {
  395. return get_speaker_mode_by_total_channels(channels);
  396. }
  397. void AudioDriverPulseAudio::pa_sinklist_cb(pa_context *c, const pa_sink_info *l, int eol, void *userdata) {
  398. AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)userdata;
  399. // If eol is set to a positive number, you're at the end of the list
  400. if (eol > 0) {
  401. return;
  402. }
  403. ad->pa_devices.push_back(l->name);
  404. ad->pa_status++;
  405. }
  406. Array AudioDriverPulseAudio::get_device_list() {
  407. pa_devices.clear();
  408. pa_devices.push_back("Default");
  409. if (pa_ctx == NULL) {
  410. return pa_devices;
  411. }
  412. lock();
  413. // Get the device list
  414. pa_status = 0;
  415. pa_operation *pa_op = pa_context_get_sink_info_list(pa_ctx, pa_sinklist_cb, (void *)this);
  416. if (pa_op) {
  417. while (pa_status == 0) {
  418. int ret = pa_mainloop_iterate(pa_ml, 1, NULL);
  419. if (ret < 0) {
  420. ERR_PRINT("pa_mainloop_iterate error");
  421. }
  422. }
  423. pa_operation_unref(pa_op);
  424. } else {
  425. ERR_PRINT("pa_context_get_server_info error");
  426. }
  427. unlock();
  428. return pa_devices;
  429. }
  430. String AudioDriverPulseAudio::get_device() {
  431. return device_name;
  432. }
  433. void AudioDriverPulseAudio::set_device(String device) {
  434. lock();
  435. new_device = device;
  436. unlock();
  437. }
  438. void AudioDriverPulseAudio::lock() {
  439. if (!thread || !mutex)
  440. return;
  441. mutex->lock();
  442. }
  443. void AudioDriverPulseAudio::unlock() {
  444. if (!thread || !mutex)
  445. return;
  446. mutex->unlock();
  447. }
  448. void AudioDriverPulseAudio::finish_device() {
  449. if (pa_str) {
  450. pa_stream_disconnect(pa_str);
  451. pa_stream_unref(pa_str);
  452. pa_str = NULL;
  453. }
  454. }
  455. void AudioDriverPulseAudio::finish() {
  456. if (!thread)
  457. return;
  458. exit_thread = true;
  459. Thread::wait_to_finish(thread);
  460. finish_device();
  461. if (pa_ctx) {
  462. pa_context_disconnect(pa_ctx);
  463. pa_context_unref(pa_ctx);
  464. pa_ctx = NULL;
  465. }
  466. if (pa_ml) {
  467. pa_mainloop_free(pa_ml);
  468. pa_ml = NULL;
  469. }
  470. memdelete(thread);
  471. if (mutex) {
  472. memdelete(mutex);
  473. mutex = NULL;
  474. }
  475. thread = NULL;
  476. }
  477. Error AudioDriverPulseAudio::capture_init_device() {
  478. // If there is a specified device check that it is really present
  479. if (capture_device_name != "Default") {
  480. Array list = capture_get_device_list();
  481. if (list.find(capture_device_name) == -1) {
  482. capture_device_name = "Default";
  483. capture_new_device = "Default";
  484. }
  485. }
  486. detect_channels(true);
  487. switch (pa_rec_map.channels) {
  488. case 1: // Mono
  489. case 2: // Stereo
  490. break;
  491. default:
  492. WARN_PRINTS("PulseAudio: Unsupported number of input channels: " + itos(pa_rec_map.channels));
  493. pa_channel_map_init_stereo(&pa_rec_map);
  494. break;
  495. }
  496. pa_sample_spec spec;
  497. spec.format = PA_SAMPLE_S16LE;
  498. spec.channels = pa_rec_map.channels;
  499. spec.rate = mix_rate;
  500. int input_latency = 30;
  501. int input_buffer_frames = closest_power_of_2(input_latency * mix_rate / 1000);
  502. int input_buffer_size = input_buffer_frames * spec.channels;
  503. pa_buffer_attr attr;
  504. attr.fragsize = input_buffer_size * sizeof(int16_t);
  505. pa_rec_str = pa_stream_new(pa_ctx, "Record", &spec, &pa_rec_map);
  506. if (pa_rec_str == NULL) {
  507. ERR_PRINTS("PulseAudio: pa_stream_new error: " + String(pa_strerror(pa_context_errno(pa_ctx))));
  508. ERR_FAIL_V(ERR_CANT_OPEN);
  509. }
  510. const char *dev = capture_device_name == "Default" ? NULL : capture_device_name.utf8().get_data();
  511. pa_stream_flags flags = pa_stream_flags(PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE);
  512. int error_code = pa_stream_connect_record(pa_rec_str, dev, &attr, flags);
  513. if (error_code < 0) {
  514. ERR_PRINTS("PulseAudio: pa_stream_connect_record error: " + String(pa_strerror(error_code)));
  515. ERR_FAIL_V(ERR_CANT_OPEN);
  516. }
  517. input_buffer_init(input_buffer_frames);
  518. print_verbose("PulseAudio: detected " + itos(pa_rec_map.channels) + " input channels");
  519. print_verbose("PulseAudio: input buffer frames: " + itos(input_buffer_frames) + " calculated latency: " + itos(input_buffer_frames * 1000 / mix_rate) + "ms");
  520. return OK;
  521. }
  522. void AudioDriverPulseAudio::capture_finish_device() {
  523. if (pa_rec_str) {
  524. int ret = pa_stream_disconnect(pa_rec_str);
  525. if (ret != 0) {
  526. ERR_PRINTS("PulseAudio: pa_stream_disconnect error: " + String(pa_strerror(ret)));
  527. }
  528. pa_stream_unref(pa_rec_str);
  529. pa_rec_str = NULL;
  530. }
  531. }
  532. Error AudioDriverPulseAudio::capture_start() {
  533. lock();
  534. Error err = capture_init_device();
  535. unlock();
  536. return err;
  537. }
  538. Error AudioDriverPulseAudio::capture_stop() {
  539. lock();
  540. capture_finish_device();
  541. unlock();
  542. return OK;
  543. }
  544. void AudioDriverPulseAudio::capture_set_device(const String &p_name) {
  545. lock();
  546. capture_new_device = p_name;
  547. unlock();
  548. }
  549. void AudioDriverPulseAudio::pa_sourcelist_cb(pa_context *c, const pa_source_info *l, int eol, void *userdata) {
  550. AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)userdata;
  551. // If eol is set to a positive number, you're at the end of the list
  552. if (eol > 0) {
  553. return;
  554. }
  555. if (l->monitor_of_sink == PA_INVALID_INDEX) {
  556. ad->pa_rec_devices.push_back(l->name);
  557. }
  558. ad->pa_status++;
  559. }
  560. Array AudioDriverPulseAudio::capture_get_device_list() {
  561. pa_rec_devices.clear();
  562. pa_rec_devices.push_back("Default");
  563. if (pa_ctx == NULL) {
  564. return pa_rec_devices;
  565. }
  566. lock();
  567. // Get the device list
  568. pa_status = 0;
  569. pa_operation *pa_op = pa_context_get_source_info_list(pa_ctx, pa_sourcelist_cb, (void *)this);
  570. if (pa_op) {
  571. while (pa_status == 0) {
  572. int ret = pa_mainloop_iterate(pa_ml, 1, NULL);
  573. if (ret < 0) {
  574. ERR_PRINT("pa_mainloop_iterate error");
  575. }
  576. }
  577. pa_operation_unref(pa_op);
  578. } else {
  579. ERR_PRINT("pa_context_get_server_info error");
  580. }
  581. unlock();
  582. return pa_rec_devices;
  583. }
  584. String AudioDriverPulseAudio::capture_get_device() {
  585. lock();
  586. String name = capture_device_name;
  587. unlock();
  588. return name;
  589. }
  590. AudioDriverPulseAudio::AudioDriverPulseAudio() :
  591. thread(NULL),
  592. mutex(NULL),
  593. pa_ml(NULL),
  594. pa_ctx(NULL),
  595. pa_str(NULL),
  596. pa_rec_str(NULL),
  597. device_name("Default"),
  598. new_device("Default"),
  599. default_device(""),
  600. mix_rate(0),
  601. buffer_frames(0),
  602. pa_buffer_size(0),
  603. channels(0),
  604. pa_ready(0),
  605. pa_status(0),
  606. active(false),
  607. thread_exited(false),
  608. exit_thread(false),
  609. latency(0) {
  610. samples_in.clear();
  611. samples_out.clear();
  612. }
  613. AudioDriverPulseAudio::~AudioDriverPulseAudio() {
  614. }
  615. #endif