audio_driver_pulseaudio.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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_map.map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
  166. pa_map.map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
  167. pa_map.map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
  168. pa_map.map[3] = PA_CHANNEL_POSITION_LFE;
  169. pa_map.map[4] = PA_CHANNEL_POSITION_REAR_LEFT;
  170. pa_map.map[5] = PA_CHANNEL_POSITION_REAR_RIGHT;
  171. pa_map.map[6] = PA_CHANNEL_POSITION_SIDE_LEFT;
  172. pa_map.map[7] = PA_CHANNEL_POSITION_SIDE_RIGHT;
  173. pa_str = pa_stream_new(pa_ctx, "Sound", &spec, &pa_map);
  174. if (pa_str == NULL) {
  175. ERR_PRINTS("PulseAudio: pa_stream_new error: " + String(pa_strerror(pa_context_errno(pa_ctx))));
  176. ERR_FAIL_V(ERR_CANT_OPEN);
  177. }
  178. pa_buffer_attr attr;
  179. // set to appropriate buffer length (in bytes) from global settings
  180. // Note: PulseAudio defaults to 4 fragments, which means that the actual
  181. // latency is tlength / fragments. It seems that the PulseAudio has no way
  182. // to get the fragments number so we're hardcoding this to the default of 4
  183. const int fragments = 4;
  184. attr.tlength = pa_buffer_size * sizeof(int16_t) * fragments;
  185. // set them to be automatically chosen
  186. attr.prebuf = (uint32_t)-1;
  187. attr.maxlength = (uint32_t)-1;
  188. attr.minreq = (uint32_t)-1;
  189. const char *dev = device_name == "Default" ? NULL : device_name.utf8().get_data();
  190. pa_stream_flags flags = pa_stream_flags(PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE);
  191. int error_code = pa_stream_connect_playback(pa_str, dev, &attr, flags, NULL, NULL);
  192. ERR_FAIL_COND_V(error_code < 0, ERR_CANT_OPEN);
  193. samples_in.resize(buffer_frames * channels);
  194. samples_out.resize(pa_buffer_size);
  195. // Reset audio input to keep synchronisation.
  196. input_position = 0;
  197. input_size = 0;
  198. return OK;
  199. }
  200. Error AudioDriverPulseAudio::init() {
  201. active = false;
  202. thread_exited = false;
  203. exit_thread = false;
  204. mix_rate = GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE);
  205. pa_ml = pa_mainloop_new();
  206. ERR_FAIL_COND_V(pa_ml == NULL, ERR_CANT_OPEN);
  207. pa_ctx = pa_context_new(pa_mainloop_get_api(pa_ml), "Godot");
  208. ERR_FAIL_COND_V(pa_ctx == NULL, ERR_CANT_OPEN);
  209. pa_ready = 0;
  210. pa_context_set_state_callback(pa_ctx, pa_state_cb, (void *)this);
  211. int ret = pa_context_connect(pa_ctx, NULL, PA_CONTEXT_NOFLAGS, NULL);
  212. if (ret < 0) {
  213. if (pa_ctx) {
  214. pa_context_unref(pa_ctx);
  215. pa_ctx = NULL;
  216. }
  217. if (pa_ml) {
  218. pa_mainloop_free(pa_ml);
  219. pa_ml = NULL;
  220. }
  221. return ERR_CANT_OPEN;
  222. }
  223. while (pa_ready == 0) {
  224. pa_mainloop_iterate(pa_ml, 1, NULL);
  225. }
  226. if (pa_ready < 0) {
  227. if (pa_ctx) {
  228. pa_context_disconnect(pa_ctx);
  229. pa_context_unref(pa_ctx);
  230. pa_ctx = NULL;
  231. }
  232. if (pa_ml) {
  233. pa_mainloop_free(pa_ml);
  234. pa_ml = NULL;
  235. }
  236. return ERR_CANT_OPEN;
  237. }
  238. Error err = init_device();
  239. if (err == OK) {
  240. mutex = Mutex::create();
  241. thread = Thread::create(AudioDriverPulseAudio::thread_func, this);
  242. }
  243. return OK;
  244. }
  245. float AudioDriverPulseAudio::get_latency() {
  246. if (latency == 0) { //only do this once since it's approximate anyway
  247. lock();
  248. pa_usec_t palat = 0;
  249. if (pa_stream_get_state(pa_str) == PA_STREAM_READY) {
  250. int negative = 0;
  251. if (pa_stream_get_latency(pa_str, &palat, &negative) >= 0) {
  252. if (negative) {
  253. palat = 0;
  254. }
  255. }
  256. }
  257. if (palat > 0) {
  258. latency = double(palat) / 1000000.0;
  259. }
  260. unlock();
  261. }
  262. return latency;
  263. }
  264. void AudioDriverPulseAudio::thread_func(void *p_udata) {
  265. AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)p_udata;
  266. unsigned int write_ofs = 0;
  267. size_t avail_bytes = 0;
  268. uint32_t default_device_msec = OS::get_singleton()->get_ticks_msec();
  269. while (!ad->exit_thread) {
  270. size_t read_bytes = 0;
  271. size_t written_bytes = 0;
  272. if (avail_bytes == 0) {
  273. ad->lock();
  274. ad->start_counting_ticks();
  275. if (!ad->active) {
  276. for (unsigned int i = 0; i < ad->pa_buffer_size; i++) {
  277. ad->samples_out.write[i] = 0;
  278. }
  279. } else {
  280. ad->audio_server_process(ad->buffer_frames, ad->samples_in.ptrw());
  281. if (ad->channels == ad->pa_map.channels) {
  282. for (unsigned int i = 0; i < ad->pa_buffer_size; i++) {
  283. ad->samples_out.write[i] = ad->samples_in[i] >> 16;
  284. }
  285. } else {
  286. // Uneven amount of channels
  287. unsigned int in_idx = 0;
  288. unsigned int out_idx = 0;
  289. for (unsigned int i = 0; i < ad->buffer_frames; i++) {
  290. for (int j = 0; j < ad->pa_map.channels - 1; j++) {
  291. ad->samples_out.write[out_idx++] = ad->samples_in[in_idx++] >> 16;
  292. }
  293. uint32_t l = ad->samples_in[in_idx++] >> 16;
  294. uint32_t r = ad->samples_in[in_idx++] >> 16;
  295. ad->samples_out.write[out_idx++] = (l + r) / 2;
  296. }
  297. }
  298. }
  299. avail_bytes = ad->pa_buffer_size * sizeof(int16_t);
  300. write_ofs = 0;
  301. ad->stop_counting_ticks();
  302. ad->unlock();
  303. }
  304. ad->lock();
  305. ad->start_counting_ticks();
  306. int ret;
  307. do {
  308. ret = pa_mainloop_iterate(ad->pa_ml, 0, NULL);
  309. } while (ret > 0);
  310. if (avail_bytes > 0 && pa_stream_get_state(ad->pa_str) == PA_STREAM_READY) {
  311. size_t bytes = pa_stream_writable_size(ad->pa_str);
  312. if (bytes > 0) {
  313. size_t bytes_to_write = MIN(bytes, avail_bytes);
  314. const void *ptr = ad->samples_out.ptr();
  315. ret = pa_stream_write(ad->pa_str, (char *)ptr + write_ofs, bytes_to_write, NULL, 0LL, PA_SEEK_RELATIVE);
  316. if (ret != 0) {
  317. ERR_PRINTS("PulseAudio: pa_stream_write error: " + String(pa_strerror(ret)));
  318. } else {
  319. avail_bytes -= bytes_to_write;
  320. write_ofs += bytes_to_write;
  321. written_bytes += bytes_to_write;
  322. }
  323. }
  324. }
  325. // User selected a new device, finish the current one so we'll init the new device
  326. if (ad->device_name != ad->new_device) {
  327. ad->device_name = ad->new_device;
  328. ad->finish_device();
  329. Error err = ad->init_device();
  330. if (err != OK) {
  331. ERR_PRINT("PulseAudio: init_device error");
  332. ad->device_name = "Default";
  333. ad->new_device = "Default";
  334. err = ad->init_device();
  335. if (err != OK) {
  336. ad->active = false;
  337. ad->exit_thread = true;
  338. break;
  339. }
  340. }
  341. avail_bytes = 0;
  342. write_ofs = 0;
  343. }
  344. // If we're using the default device check that the current device is still the default
  345. if (ad->device_name == "Default") {
  346. uint32_t msec = OS::get_singleton()->get_ticks_msec();
  347. if (msec > (default_device_msec + 1000)) {
  348. String old_default_device = ad->default_device;
  349. default_device_msec = msec;
  350. ad->pa_status = 0;
  351. pa_operation *pa_op = pa_context_get_server_info(ad->pa_ctx, &AudioDriverPulseAudio::pa_server_info_cb, (void *)ad);
  352. if (pa_op) {
  353. while (ad->pa_status == 0) {
  354. ret = pa_mainloop_iterate(ad->pa_ml, 1, NULL);
  355. if (ret < 0) {
  356. ERR_PRINT("pa_mainloop_iterate error");
  357. }
  358. }
  359. pa_operation_unref(pa_op);
  360. } else {
  361. ERR_PRINT("pa_context_get_server_info error");
  362. }
  363. if (old_default_device != ad->default_device) {
  364. ad->finish_device();
  365. Error err = ad->init_device();
  366. if (err != OK) {
  367. ERR_PRINT("PulseAudio: init_device error");
  368. ad->active = false;
  369. ad->exit_thread = true;
  370. break;
  371. }
  372. avail_bytes = 0;
  373. write_ofs = 0;
  374. }
  375. }
  376. }
  377. if (ad->pa_rec_str && pa_stream_get_state(ad->pa_rec_str) == PA_STREAM_READY) {
  378. size_t bytes = pa_stream_readable_size(ad->pa_rec_str);
  379. if (bytes > 0) {
  380. const void *ptr = NULL;
  381. size_t maxbytes = ad->input_buffer.size() * sizeof(int16_t);
  382. bytes = MIN(bytes, maxbytes);
  383. ret = pa_stream_peek(ad->pa_rec_str, &ptr, &bytes);
  384. if (ret != 0) {
  385. ERR_PRINT("pa_stream_peek error");
  386. } else {
  387. int16_t *srcptr = (int16_t *)ptr;
  388. for (size_t i = bytes >> 1; i > 0; i--) {
  389. int32_t sample = int32_t(*srcptr++) << 16;
  390. ad->input_buffer_write(sample);
  391. if (ad->pa_rec_map.channels == 1) {
  392. // In case input device is single channel convert it to Stereo
  393. ad->input_buffer_write(sample);
  394. }
  395. }
  396. read_bytes += bytes;
  397. ret = pa_stream_drop(ad->pa_rec_str);
  398. if (ret != 0) {
  399. ERR_PRINT("pa_stream_drop error");
  400. }
  401. }
  402. }
  403. // User selected a new device, finish the current one so we'll init the new device
  404. if (ad->capture_device_name != ad->capture_new_device) {
  405. ad->capture_device_name = ad->capture_new_device;
  406. ad->capture_finish_device();
  407. Error err = ad->capture_init_device();
  408. if (err != OK) {
  409. ERR_PRINT("PulseAudio: capture_init_device error");
  410. ad->capture_device_name = "Default";
  411. ad->capture_new_device = "Default";
  412. err = ad->capture_init_device();
  413. if (err != OK) {
  414. ad->active = false;
  415. ad->exit_thread = true;
  416. break;
  417. }
  418. }
  419. }
  420. }
  421. ad->stop_counting_ticks();
  422. ad->unlock();
  423. // Let the thread rest a while if we haven't read or write anything
  424. if (written_bytes == 0 && read_bytes == 0) {
  425. OS::get_singleton()->delay_usec(1000);
  426. }
  427. }
  428. ad->thread_exited = true;
  429. }
  430. void AudioDriverPulseAudio::start() {
  431. active = true;
  432. }
  433. int AudioDriverPulseAudio::get_mix_rate() const {
  434. return mix_rate;
  435. }
  436. AudioDriver::SpeakerMode AudioDriverPulseAudio::get_speaker_mode() const {
  437. return get_speaker_mode_by_total_channels(channels);
  438. }
  439. void AudioDriverPulseAudio::pa_sinklist_cb(pa_context *c, const pa_sink_info *l, int eol, void *userdata) {
  440. AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)userdata;
  441. // If eol is set to a positive number, you're at the end of the list
  442. if (eol > 0) {
  443. return;
  444. }
  445. ad->pa_devices.push_back(l->name);
  446. ad->pa_status++;
  447. }
  448. Array AudioDriverPulseAudio::get_device_list() {
  449. pa_devices.clear();
  450. pa_devices.push_back("Default");
  451. if (pa_ctx == NULL) {
  452. return pa_devices;
  453. }
  454. lock();
  455. // Get the device list
  456. pa_status = 0;
  457. pa_operation *pa_op = pa_context_get_sink_info_list(pa_ctx, pa_sinklist_cb, (void *)this);
  458. if (pa_op) {
  459. while (pa_status == 0) {
  460. int ret = pa_mainloop_iterate(pa_ml, 1, NULL);
  461. if (ret < 0) {
  462. ERR_PRINT("pa_mainloop_iterate error");
  463. }
  464. }
  465. pa_operation_unref(pa_op);
  466. } else {
  467. ERR_PRINT("pa_context_get_server_info error");
  468. }
  469. unlock();
  470. return pa_devices;
  471. }
  472. String AudioDriverPulseAudio::get_device() {
  473. return device_name;
  474. }
  475. void AudioDriverPulseAudio::set_device(String device) {
  476. lock();
  477. new_device = device;
  478. unlock();
  479. }
  480. void AudioDriverPulseAudio::lock() {
  481. if (!thread || !mutex)
  482. return;
  483. mutex->lock();
  484. }
  485. void AudioDriverPulseAudio::unlock() {
  486. if (!thread || !mutex)
  487. return;
  488. mutex->unlock();
  489. }
  490. void AudioDriverPulseAudio::finish_device() {
  491. if (pa_str) {
  492. pa_stream_disconnect(pa_str);
  493. pa_stream_unref(pa_str);
  494. pa_str = NULL;
  495. }
  496. }
  497. void AudioDriverPulseAudio::finish() {
  498. if (!thread)
  499. return;
  500. exit_thread = true;
  501. Thread::wait_to_finish(thread);
  502. finish_device();
  503. if (pa_ctx) {
  504. pa_context_disconnect(pa_ctx);
  505. pa_context_unref(pa_ctx);
  506. pa_ctx = NULL;
  507. }
  508. if (pa_ml) {
  509. pa_mainloop_free(pa_ml);
  510. pa_ml = NULL;
  511. }
  512. memdelete(thread);
  513. if (mutex) {
  514. memdelete(mutex);
  515. mutex = NULL;
  516. }
  517. thread = NULL;
  518. }
  519. Error AudioDriverPulseAudio::capture_init_device() {
  520. // If there is a specified device check that it is really present
  521. if (capture_device_name != "Default") {
  522. Array list = capture_get_device_list();
  523. if (list.find(capture_device_name) == -1) {
  524. capture_device_name = "Default";
  525. capture_new_device = "Default";
  526. }
  527. }
  528. detect_channels(true);
  529. switch (pa_rec_map.channels) {
  530. case 1: // Mono
  531. case 2: // Stereo
  532. break;
  533. default:
  534. WARN_PRINTS("PulseAudio: Unsupported number of input channels: " + itos(pa_rec_map.channels));
  535. pa_channel_map_init_stereo(&pa_rec_map);
  536. break;
  537. }
  538. pa_sample_spec spec;
  539. spec.format = PA_SAMPLE_S16LE;
  540. spec.channels = pa_rec_map.channels;
  541. spec.rate = mix_rate;
  542. int input_latency = 30;
  543. int input_buffer_frames = closest_power_of_2(input_latency * mix_rate / 1000);
  544. int input_buffer_size = input_buffer_frames * spec.channels;
  545. pa_buffer_attr attr;
  546. attr.fragsize = input_buffer_size * sizeof(int16_t);
  547. pa_rec_str = pa_stream_new(pa_ctx, "Record", &spec, &pa_rec_map);
  548. if (pa_rec_str == NULL) {
  549. ERR_PRINTS("PulseAudio: pa_stream_new error: " + String(pa_strerror(pa_context_errno(pa_ctx))));
  550. ERR_FAIL_V(ERR_CANT_OPEN);
  551. }
  552. const char *dev = capture_device_name == "Default" ? NULL : capture_device_name.utf8().get_data();
  553. pa_stream_flags flags = pa_stream_flags(PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE);
  554. int error_code = pa_stream_connect_record(pa_rec_str, dev, &attr, flags);
  555. if (error_code < 0) {
  556. ERR_PRINTS("PulseAudio: pa_stream_connect_record error: " + String(pa_strerror(error_code)));
  557. ERR_FAIL_V(ERR_CANT_OPEN);
  558. }
  559. input_buffer_init(input_buffer_frames);
  560. print_verbose("PulseAudio: detected " + itos(pa_rec_map.channels) + " input channels");
  561. print_verbose("PulseAudio: input buffer frames: " + itos(input_buffer_frames) + " calculated latency: " + itos(input_buffer_frames * 1000 / mix_rate) + "ms");
  562. return OK;
  563. }
  564. void AudioDriverPulseAudio::capture_finish_device() {
  565. if (pa_rec_str) {
  566. int ret = pa_stream_disconnect(pa_rec_str);
  567. if (ret != 0) {
  568. ERR_PRINTS("PulseAudio: pa_stream_disconnect error: " + String(pa_strerror(ret)));
  569. }
  570. pa_stream_unref(pa_rec_str);
  571. pa_rec_str = NULL;
  572. }
  573. }
  574. Error AudioDriverPulseAudio::capture_start() {
  575. lock();
  576. Error err = capture_init_device();
  577. unlock();
  578. return err;
  579. }
  580. Error AudioDriverPulseAudio::capture_stop() {
  581. lock();
  582. capture_finish_device();
  583. unlock();
  584. return OK;
  585. }
  586. void AudioDriverPulseAudio::capture_set_device(const String &p_name) {
  587. lock();
  588. capture_new_device = p_name;
  589. unlock();
  590. }
  591. void AudioDriverPulseAudio::pa_sourcelist_cb(pa_context *c, const pa_source_info *l, int eol, void *userdata) {
  592. AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)userdata;
  593. // If eol is set to a positive number, you're at the end of the list
  594. if (eol > 0) {
  595. return;
  596. }
  597. if (l->monitor_of_sink == PA_INVALID_INDEX) {
  598. ad->pa_rec_devices.push_back(l->name);
  599. }
  600. ad->pa_status++;
  601. }
  602. Array AudioDriverPulseAudio::capture_get_device_list() {
  603. pa_rec_devices.clear();
  604. pa_rec_devices.push_back("Default");
  605. if (pa_ctx == NULL) {
  606. return pa_rec_devices;
  607. }
  608. lock();
  609. // Get the device list
  610. pa_status = 0;
  611. pa_operation *pa_op = pa_context_get_source_info_list(pa_ctx, pa_sourcelist_cb, (void *)this);
  612. if (pa_op) {
  613. while (pa_status == 0) {
  614. int ret = pa_mainloop_iterate(pa_ml, 1, NULL);
  615. if (ret < 0) {
  616. ERR_PRINT("pa_mainloop_iterate error");
  617. }
  618. }
  619. pa_operation_unref(pa_op);
  620. } else {
  621. ERR_PRINT("pa_context_get_server_info error");
  622. }
  623. unlock();
  624. return pa_rec_devices;
  625. }
  626. String AudioDriverPulseAudio::capture_get_device() {
  627. lock();
  628. String name = capture_device_name;
  629. unlock();
  630. return name;
  631. }
  632. AudioDriverPulseAudio::AudioDriverPulseAudio() :
  633. thread(NULL),
  634. mutex(NULL),
  635. pa_ml(NULL),
  636. pa_ctx(NULL),
  637. pa_str(NULL),
  638. pa_rec_str(NULL),
  639. device_name("Default"),
  640. new_device("Default"),
  641. default_device(""),
  642. mix_rate(0),
  643. buffer_frames(0),
  644. pa_buffer_size(0),
  645. channels(0),
  646. pa_ready(0),
  647. pa_status(0),
  648. active(false),
  649. thread_exited(false),
  650. exit_thread(false),
  651. latency(0) {
  652. samples_in.clear();
  653. samples_out.clear();
  654. }
  655. AudioDriverPulseAudio::~AudioDriverPulseAudio() {
  656. }
  657. #endif