2
0

audio_driver_pulseaudio.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. uint32_t default_device_msec = OS::get_singleton()->get_ticks_msec();
  261. while (!ad->exit_thread) {
  262. size_t read_bytes = 0;
  263. size_t written_bytes = 0;
  264. if (avail_bytes == 0) {
  265. ad->lock();
  266. ad->start_counting_ticks();
  267. if (!ad->active) {
  268. for (unsigned int i = 0; i < ad->pa_buffer_size; i++) {
  269. ad->samples_out.write[i] = 0;
  270. }
  271. } else {
  272. ad->audio_server_process(ad->buffer_frames, ad->samples_in.ptrw());
  273. if (ad->channels == ad->pa_map.channels) {
  274. for (unsigned int i = 0; i < ad->pa_buffer_size; i++) {
  275. ad->samples_out.write[i] = ad->samples_in[i] >> 16;
  276. }
  277. } else {
  278. // Uneven amount of channels
  279. unsigned int in_idx = 0;
  280. unsigned int out_idx = 0;
  281. for (unsigned int i = 0; i < ad->buffer_frames; i++) {
  282. for (int j = 0; j < ad->pa_map.channels - 1; j++) {
  283. ad->samples_out.write[out_idx++] = ad->samples_in[in_idx++] >> 16;
  284. }
  285. uint32_t l = ad->samples_in[in_idx++] >> 16;
  286. uint32_t r = ad->samples_in[in_idx++] >> 16;
  287. ad->samples_out.write[out_idx++] = (l + r) / 2;
  288. }
  289. }
  290. }
  291. avail_bytes = ad->pa_buffer_size * sizeof(int16_t);
  292. write_ofs = 0;
  293. ad->stop_counting_ticks();
  294. ad->unlock();
  295. }
  296. ad->lock();
  297. ad->start_counting_ticks();
  298. int ret;
  299. do {
  300. ret = pa_mainloop_iterate(ad->pa_ml, 0, NULL);
  301. } while (ret > 0);
  302. if (avail_bytes > 0 && pa_stream_get_state(ad->pa_str) == PA_STREAM_READY) {
  303. size_t bytes = pa_stream_writable_size(ad->pa_str);
  304. if (bytes > 0) {
  305. size_t bytes_to_write = MIN(bytes, avail_bytes);
  306. const void *ptr = ad->samples_out.ptr();
  307. ret = pa_stream_write(ad->pa_str, (char *)ptr + write_ofs, bytes_to_write, NULL, 0LL, PA_SEEK_RELATIVE);
  308. if (ret != 0) {
  309. ERR_PRINTS("PulseAudio: pa_stream_write error: " + String(pa_strerror(ret)));
  310. } else {
  311. avail_bytes -= bytes_to_write;
  312. write_ofs += bytes_to_write;
  313. written_bytes += bytes_to_write;
  314. }
  315. }
  316. }
  317. // User selected a new device, finish the current one so we'll init the new device
  318. if (ad->device_name != ad->new_device) {
  319. ad->device_name = ad->new_device;
  320. ad->finish_device();
  321. Error err = ad->init_device();
  322. if (err != OK) {
  323. ERR_PRINT("PulseAudio: init_device error");
  324. ad->device_name = "Default";
  325. ad->new_device = "Default";
  326. err = ad->init_device();
  327. if (err != OK) {
  328. ad->active = false;
  329. ad->exit_thread = true;
  330. break;
  331. }
  332. }
  333. avail_bytes = 0;
  334. write_ofs = 0;
  335. }
  336. // If we're using the default device check that the current device is still the default
  337. if (ad->device_name == "Default") {
  338. uint32_t msec = OS::get_singleton()->get_ticks_msec();
  339. if (msec > (default_device_msec + 1000)) {
  340. String old_default_device = ad->default_device;
  341. default_device_msec = msec;
  342. ad->pa_status = 0;
  343. pa_operation *pa_op = pa_context_get_server_info(ad->pa_ctx, &AudioDriverPulseAudio::pa_server_info_cb, (void *)ad);
  344. if (pa_op) {
  345. while (ad->pa_status == 0) {
  346. ret = pa_mainloop_iterate(ad->pa_ml, 1, NULL);
  347. if (ret < 0) {
  348. ERR_PRINT("pa_mainloop_iterate error");
  349. }
  350. }
  351. pa_operation_unref(pa_op);
  352. } else {
  353. ERR_PRINT("pa_context_get_server_info error");
  354. }
  355. if (old_default_device != ad->default_device) {
  356. ad->finish_device();
  357. Error err = ad->init_device();
  358. if (err != OK) {
  359. ERR_PRINT("PulseAudio: init_device error");
  360. ad->active = false;
  361. ad->exit_thread = true;
  362. break;
  363. }
  364. avail_bytes = 0;
  365. write_ofs = 0;
  366. }
  367. }
  368. }
  369. if (ad->pa_rec_str && pa_stream_get_state(ad->pa_rec_str) == PA_STREAM_READY) {
  370. size_t bytes = pa_stream_readable_size(ad->pa_rec_str);
  371. if (bytes > 0) {
  372. const void *ptr = NULL;
  373. size_t maxbytes = ad->input_buffer.size() * sizeof(int16_t);
  374. bytes = MIN(bytes, maxbytes);
  375. ret = pa_stream_peek(ad->pa_rec_str, &ptr, &bytes);
  376. if (ret != 0) {
  377. ERR_PRINT("pa_stream_peek error");
  378. } else {
  379. int16_t *srcptr = (int16_t *)ptr;
  380. for (size_t i = bytes >> 1; i > 0; i--) {
  381. int32_t sample = int32_t(*srcptr++) << 16;
  382. ad->input_buffer_write(sample);
  383. if (ad->pa_rec_map.channels == 1) {
  384. // In case input device is single channel convert it to Stereo
  385. ad->input_buffer_write(sample);
  386. }
  387. }
  388. read_bytes += bytes;
  389. ret = pa_stream_drop(ad->pa_rec_str);
  390. if (ret != 0) {
  391. ERR_PRINT("pa_stream_drop error");
  392. }
  393. }
  394. }
  395. // User selected a new device, finish the current one so we'll init the new device
  396. if (ad->capture_device_name != ad->capture_new_device) {
  397. ad->capture_device_name = ad->capture_new_device;
  398. ad->capture_finish_device();
  399. Error err = ad->capture_init_device();
  400. if (err != OK) {
  401. ERR_PRINT("PulseAudio: capture_init_device error");
  402. ad->capture_device_name = "Default";
  403. ad->capture_new_device = "Default";
  404. err = ad->capture_init_device();
  405. if (err != OK) {
  406. ad->active = false;
  407. ad->exit_thread = true;
  408. break;
  409. }
  410. }
  411. }
  412. }
  413. ad->stop_counting_ticks();
  414. ad->unlock();
  415. // Let the thread rest a while if we haven't read or write anything
  416. if (written_bytes == 0 && read_bytes == 0) {
  417. OS::get_singleton()->delay_usec(1000);
  418. }
  419. }
  420. ad->thread_exited = true;
  421. }
  422. void AudioDriverPulseAudio::start() {
  423. active = true;
  424. }
  425. int AudioDriverPulseAudio::get_mix_rate() const {
  426. return mix_rate;
  427. }
  428. AudioDriver::SpeakerMode AudioDriverPulseAudio::get_speaker_mode() const {
  429. return get_speaker_mode_by_total_channels(channels);
  430. }
  431. void AudioDriverPulseAudio::pa_sinklist_cb(pa_context *c, const pa_sink_info *l, int eol, void *userdata) {
  432. AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)userdata;
  433. // If eol is set to a positive number, you're at the end of the list
  434. if (eol > 0) {
  435. return;
  436. }
  437. ad->pa_devices.push_back(l->name);
  438. ad->pa_status++;
  439. }
  440. Array AudioDriverPulseAudio::get_device_list() {
  441. pa_devices.clear();
  442. pa_devices.push_back("Default");
  443. if (pa_ctx == NULL) {
  444. return pa_devices;
  445. }
  446. lock();
  447. // Get the device list
  448. pa_status = 0;
  449. pa_operation *pa_op = pa_context_get_sink_info_list(pa_ctx, pa_sinklist_cb, (void *)this);
  450. if (pa_op) {
  451. while (pa_status == 0) {
  452. int ret = pa_mainloop_iterate(pa_ml, 1, NULL);
  453. if (ret < 0) {
  454. ERR_PRINT("pa_mainloop_iterate error");
  455. }
  456. }
  457. pa_operation_unref(pa_op);
  458. } else {
  459. ERR_PRINT("pa_context_get_server_info error");
  460. }
  461. unlock();
  462. return pa_devices;
  463. }
  464. String AudioDriverPulseAudio::get_device() {
  465. return device_name;
  466. }
  467. void AudioDriverPulseAudio::set_device(String device) {
  468. lock();
  469. new_device = device;
  470. unlock();
  471. }
  472. void AudioDriverPulseAudio::lock() {
  473. if (!thread || !mutex)
  474. return;
  475. mutex->lock();
  476. }
  477. void AudioDriverPulseAudio::unlock() {
  478. if (!thread || !mutex)
  479. return;
  480. mutex->unlock();
  481. }
  482. void AudioDriverPulseAudio::finish_device() {
  483. if (pa_str) {
  484. pa_stream_disconnect(pa_str);
  485. pa_stream_unref(pa_str);
  486. pa_str = NULL;
  487. }
  488. }
  489. void AudioDriverPulseAudio::finish() {
  490. if (!thread)
  491. return;
  492. exit_thread = true;
  493. Thread::wait_to_finish(thread);
  494. finish_device();
  495. if (pa_ctx) {
  496. pa_context_disconnect(pa_ctx);
  497. pa_context_unref(pa_ctx);
  498. pa_ctx = NULL;
  499. }
  500. if (pa_ml) {
  501. pa_mainloop_free(pa_ml);
  502. pa_ml = NULL;
  503. }
  504. memdelete(thread);
  505. if (mutex) {
  506. memdelete(mutex);
  507. mutex = NULL;
  508. }
  509. thread = NULL;
  510. }
  511. Error AudioDriverPulseAudio::capture_init_device() {
  512. // If there is a specified device check that it is really present
  513. if (capture_device_name != "Default") {
  514. Array list = capture_get_device_list();
  515. if (list.find(capture_device_name) == -1) {
  516. capture_device_name = "Default";
  517. capture_new_device = "Default";
  518. }
  519. }
  520. detect_channels(true);
  521. switch (pa_rec_map.channels) {
  522. case 1: // Mono
  523. case 2: // Stereo
  524. break;
  525. default:
  526. WARN_PRINTS("PulseAudio: Unsupported number of input channels: " + itos(pa_rec_map.channels));
  527. pa_channel_map_init_stereo(&pa_rec_map);
  528. break;
  529. }
  530. pa_sample_spec spec;
  531. spec.format = PA_SAMPLE_S16LE;
  532. spec.channels = pa_rec_map.channels;
  533. spec.rate = mix_rate;
  534. int input_latency = 30;
  535. int input_buffer_frames = closest_power_of_2(input_latency * mix_rate / 1000);
  536. int input_buffer_size = input_buffer_frames * spec.channels;
  537. pa_buffer_attr attr;
  538. attr.fragsize = input_buffer_size * sizeof(int16_t);
  539. pa_rec_str = pa_stream_new(pa_ctx, "Record", &spec, &pa_rec_map);
  540. if (pa_rec_str == NULL) {
  541. ERR_PRINTS("PulseAudio: pa_stream_new error: " + String(pa_strerror(pa_context_errno(pa_ctx))));
  542. ERR_FAIL_V(ERR_CANT_OPEN);
  543. }
  544. const char *dev = capture_device_name == "Default" ? NULL : capture_device_name.utf8().get_data();
  545. pa_stream_flags flags = pa_stream_flags(PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE);
  546. int error_code = pa_stream_connect_record(pa_rec_str, dev, &attr, flags);
  547. if (error_code < 0) {
  548. ERR_PRINTS("PulseAudio: pa_stream_connect_record error: " + String(pa_strerror(error_code)));
  549. ERR_FAIL_V(ERR_CANT_OPEN);
  550. }
  551. input_buffer_init(input_buffer_frames);
  552. print_verbose("PulseAudio: detected " + itos(pa_rec_map.channels) + " input channels");
  553. print_verbose("PulseAudio: input buffer frames: " + itos(input_buffer_frames) + " calculated latency: " + itos(input_buffer_frames * 1000 / mix_rate) + "ms");
  554. return OK;
  555. }
  556. void AudioDriverPulseAudio::capture_finish_device() {
  557. if (pa_rec_str) {
  558. int ret = pa_stream_disconnect(pa_rec_str);
  559. if (ret != 0) {
  560. ERR_PRINTS("PulseAudio: pa_stream_disconnect error: " + String(pa_strerror(ret)));
  561. }
  562. pa_stream_unref(pa_rec_str);
  563. pa_rec_str = NULL;
  564. }
  565. }
  566. Error AudioDriverPulseAudio::capture_start() {
  567. lock();
  568. Error err = capture_init_device();
  569. unlock();
  570. return err;
  571. }
  572. Error AudioDriverPulseAudio::capture_stop() {
  573. lock();
  574. capture_finish_device();
  575. unlock();
  576. return OK;
  577. }
  578. void AudioDriverPulseAudio::capture_set_device(const String &p_name) {
  579. lock();
  580. capture_new_device = p_name;
  581. unlock();
  582. }
  583. void AudioDriverPulseAudio::pa_sourcelist_cb(pa_context *c, const pa_source_info *l, int eol, void *userdata) {
  584. AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)userdata;
  585. // If eol is set to a positive number, you're at the end of the list
  586. if (eol > 0) {
  587. return;
  588. }
  589. if (l->monitor_of_sink == PA_INVALID_INDEX) {
  590. ad->pa_rec_devices.push_back(l->name);
  591. }
  592. ad->pa_status++;
  593. }
  594. Array AudioDriverPulseAudio::capture_get_device_list() {
  595. pa_rec_devices.clear();
  596. pa_rec_devices.push_back("Default");
  597. if (pa_ctx == NULL) {
  598. return pa_rec_devices;
  599. }
  600. lock();
  601. // Get the device list
  602. pa_status = 0;
  603. pa_operation *pa_op = pa_context_get_source_info_list(pa_ctx, pa_sourcelist_cb, (void *)this);
  604. if (pa_op) {
  605. while (pa_status == 0) {
  606. int ret = pa_mainloop_iterate(pa_ml, 1, NULL);
  607. if (ret < 0) {
  608. ERR_PRINT("pa_mainloop_iterate error");
  609. }
  610. }
  611. pa_operation_unref(pa_op);
  612. } else {
  613. ERR_PRINT("pa_context_get_server_info error");
  614. }
  615. unlock();
  616. return pa_rec_devices;
  617. }
  618. String AudioDriverPulseAudio::capture_get_device() {
  619. lock();
  620. String name = capture_device_name;
  621. unlock();
  622. return name;
  623. }
  624. AudioDriverPulseAudio::AudioDriverPulseAudio() :
  625. thread(NULL),
  626. mutex(NULL),
  627. pa_ml(NULL),
  628. pa_ctx(NULL),
  629. pa_str(NULL),
  630. pa_rec_str(NULL),
  631. device_name("Default"),
  632. new_device("Default"),
  633. default_device(""),
  634. mix_rate(0),
  635. buffer_frames(0),
  636. pa_buffer_size(0),
  637. channels(0),
  638. pa_ready(0),
  639. pa_status(0),
  640. active(false),
  641. thread_exited(false),
  642. exit_thread(false),
  643. latency(0) {
  644. samples_in.clear();
  645. samples_out.clear();
  646. }
  647. AudioDriverPulseAudio::~AudioDriverPulseAudio() {
  648. }
  649. #endif