pulseaudio.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 2009 by Konstantinos Natsakis <[email protected]>
  4. * Copyright (C) 2010 by Chris Robinson <[email protected]>
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public
  16. * License along with this library; if not, write to the
  17. * Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. * Or go to http://www.gnu.org/copyleft/lgpl.html
  20. */
  21. #include "config.h"
  22. #include <string.h>
  23. #include "alMain.h"
  24. #include "alu.h"
  25. #include "threads.h"
  26. #include "compat.h"
  27. #include "backends/base.h"
  28. #include <pulse/pulseaudio.h>
  29. #if PA_API_VERSION == 12
  30. #ifdef HAVE_DYNLOAD
  31. static void *pa_handle;
  32. #define MAKE_FUNC(x) static __typeof(x) * p##x
  33. MAKE_FUNC(pa_context_unref);
  34. MAKE_FUNC(pa_sample_spec_valid);
  35. MAKE_FUNC(pa_frame_size);
  36. MAKE_FUNC(pa_stream_drop);
  37. MAKE_FUNC(pa_strerror);
  38. MAKE_FUNC(pa_context_get_state);
  39. MAKE_FUNC(pa_stream_get_state);
  40. MAKE_FUNC(pa_threaded_mainloop_signal);
  41. MAKE_FUNC(pa_stream_peek);
  42. MAKE_FUNC(pa_threaded_mainloop_wait);
  43. MAKE_FUNC(pa_threaded_mainloop_unlock);
  44. MAKE_FUNC(pa_threaded_mainloop_in_thread);
  45. MAKE_FUNC(pa_context_new);
  46. MAKE_FUNC(pa_threaded_mainloop_stop);
  47. MAKE_FUNC(pa_context_disconnect);
  48. MAKE_FUNC(pa_threaded_mainloop_start);
  49. MAKE_FUNC(pa_threaded_mainloop_get_api);
  50. MAKE_FUNC(pa_context_set_state_callback);
  51. MAKE_FUNC(pa_stream_write);
  52. MAKE_FUNC(pa_xfree);
  53. MAKE_FUNC(pa_stream_connect_record);
  54. MAKE_FUNC(pa_stream_connect_playback);
  55. MAKE_FUNC(pa_stream_readable_size);
  56. MAKE_FUNC(pa_stream_writable_size);
  57. MAKE_FUNC(pa_stream_is_corked);
  58. MAKE_FUNC(pa_stream_cork);
  59. MAKE_FUNC(pa_stream_is_suspended);
  60. MAKE_FUNC(pa_stream_get_device_name);
  61. MAKE_FUNC(pa_stream_get_latency);
  62. MAKE_FUNC(pa_path_get_filename);
  63. MAKE_FUNC(pa_get_binary_name);
  64. MAKE_FUNC(pa_threaded_mainloop_free);
  65. MAKE_FUNC(pa_context_errno);
  66. MAKE_FUNC(pa_xmalloc);
  67. MAKE_FUNC(pa_stream_unref);
  68. MAKE_FUNC(pa_threaded_mainloop_accept);
  69. MAKE_FUNC(pa_stream_set_write_callback);
  70. MAKE_FUNC(pa_threaded_mainloop_new);
  71. MAKE_FUNC(pa_context_connect);
  72. MAKE_FUNC(pa_stream_set_buffer_attr);
  73. MAKE_FUNC(pa_stream_get_buffer_attr);
  74. MAKE_FUNC(pa_stream_get_sample_spec);
  75. MAKE_FUNC(pa_stream_get_time);
  76. MAKE_FUNC(pa_stream_set_read_callback);
  77. MAKE_FUNC(pa_stream_set_state_callback);
  78. MAKE_FUNC(pa_stream_set_moved_callback);
  79. MAKE_FUNC(pa_stream_set_underflow_callback);
  80. MAKE_FUNC(pa_stream_new_with_proplist);
  81. MAKE_FUNC(pa_stream_disconnect);
  82. MAKE_FUNC(pa_threaded_mainloop_lock);
  83. MAKE_FUNC(pa_channel_map_init_auto);
  84. MAKE_FUNC(pa_channel_map_parse);
  85. MAKE_FUNC(pa_channel_map_snprint);
  86. MAKE_FUNC(pa_channel_map_equal);
  87. MAKE_FUNC(pa_context_get_server_info);
  88. MAKE_FUNC(pa_context_get_sink_info_by_name);
  89. MAKE_FUNC(pa_context_get_sink_info_list);
  90. MAKE_FUNC(pa_context_get_source_info_by_name);
  91. MAKE_FUNC(pa_context_get_source_info_list);
  92. MAKE_FUNC(pa_operation_get_state);
  93. MAKE_FUNC(pa_operation_unref);
  94. MAKE_FUNC(pa_proplist_new);
  95. MAKE_FUNC(pa_proplist_free);
  96. MAKE_FUNC(pa_proplist_set);
  97. MAKE_FUNC(pa_channel_map_superset);
  98. MAKE_FUNC(pa_stream_set_buffer_attr_callback);
  99. MAKE_FUNC(pa_stream_begin_write);
  100. #undef MAKE_FUNC
  101. #define pa_context_unref ppa_context_unref
  102. #define pa_sample_spec_valid ppa_sample_spec_valid
  103. #define pa_frame_size ppa_frame_size
  104. #define pa_stream_drop ppa_stream_drop
  105. #define pa_strerror ppa_strerror
  106. #define pa_context_get_state ppa_context_get_state
  107. #define pa_stream_get_state ppa_stream_get_state
  108. #define pa_threaded_mainloop_signal ppa_threaded_mainloop_signal
  109. #define pa_stream_peek ppa_stream_peek
  110. #define pa_threaded_mainloop_wait ppa_threaded_mainloop_wait
  111. #define pa_threaded_mainloop_unlock ppa_threaded_mainloop_unlock
  112. #define pa_threaded_mainloop_in_thread ppa_threaded_mainloop_in_thread
  113. #define pa_context_new ppa_context_new
  114. #define pa_threaded_mainloop_stop ppa_threaded_mainloop_stop
  115. #define pa_context_disconnect ppa_context_disconnect
  116. #define pa_threaded_mainloop_start ppa_threaded_mainloop_start
  117. #define pa_threaded_mainloop_get_api ppa_threaded_mainloop_get_api
  118. #define pa_context_set_state_callback ppa_context_set_state_callback
  119. #define pa_stream_write ppa_stream_write
  120. #define pa_xfree ppa_xfree
  121. #define pa_stream_connect_record ppa_stream_connect_record
  122. #define pa_stream_connect_playback ppa_stream_connect_playback
  123. #define pa_stream_readable_size ppa_stream_readable_size
  124. #define pa_stream_writable_size ppa_stream_writable_size
  125. #define pa_stream_is_corked ppa_stream_is_corked
  126. #define pa_stream_cork ppa_stream_cork
  127. #define pa_stream_is_suspended ppa_stream_is_suspended
  128. #define pa_stream_get_device_name ppa_stream_get_device_name
  129. #define pa_stream_get_latency ppa_stream_get_latency
  130. #define pa_path_get_filename ppa_path_get_filename
  131. #define pa_get_binary_name ppa_get_binary_name
  132. #define pa_threaded_mainloop_free ppa_threaded_mainloop_free
  133. #define pa_context_errno ppa_context_errno
  134. #define pa_xmalloc ppa_xmalloc
  135. #define pa_stream_unref ppa_stream_unref
  136. #define pa_threaded_mainloop_accept ppa_threaded_mainloop_accept
  137. #define pa_stream_set_write_callback ppa_stream_set_write_callback
  138. #define pa_threaded_mainloop_new ppa_threaded_mainloop_new
  139. #define pa_context_connect ppa_context_connect
  140. #define pa_stream_set_buffer_attr ppa_stream_set_buffer_attr
  141. #define pa_stream_get_buffer_attr ppa_stream_get_buffer_attr
  142. #define pa_stream_get_sample_spec ppa_stream_get_sample_spec
  143. #define pa_stream_get_time ppa_stream_get_time
  144. #define pa_stream_set_read_callback ppa_stream_set_read_callback
  145. #define pa_stream_set_state_callback ppa_stream_set_state_callback
  146. #define pa_stream_set_moved_callback ppa_stream_set_moved_callback
  147. #define pa_stream_set_underflow_callback ppa_stream_set_underflow_callback
  148. #define pa_stream_new_with_proplist ppa_stream_new_with_proplist
  149. #define pa_stream_disconnect ppa_stream_disconnect
  150. #define pa_threaded_mainloop_lock ppa_threaded_mainloop_lock
  151. #define pa_channel_map_init_auto ppa_channel_map_init_auto
  152. #define pa_channel_map_parse ppa_channel_map_parse
  153. #define pa_channel_map_snprint ppa_channel_map_snprint
  154. #define pa_channel_map_equal ppa_channel_map_equal
  155. #define pa_context_get_server_info ppa_context_get_server_info
  156. #define pa_context_get_sink_info_by_name ppa_context_get_sink_info_by_name
  157. #define pa_context_get_sink_info_list ppa_context_get_sink_info_list
  158. #define pa_context_get_source_info_by_name ppa_context_get_source_info_by_name
  159. #define pa_context_get_source_info_list ppa_context_get_source_info_list
  160. #define pa_operation_get_state ppa_operation_get_state
  161. #define pa_operation_unref ppa_operation_unref
  162. #define pa_proplist_new ppa_proplist_new
  163. #define pa_proplist_free ppa_proplist_free
  164. #define pa_proplist_set ppa_proplist_set
  165. #define pa_channel_map_superset ppa_channel_map_superset
  166. #define pa_stream_set_buffer_attr_callback ppa_stream_set_buffer_attr_callback
  167. #define pa_stream_begin_write ppa_stream_begin_write
  168. #endif
  169. static ALCboolean pulse_load(void)
  170. {
  171. ALCboolean ret = ALC_TRUE;
  172. #ifdef HAVE_DYNLOAD
  173. if(!pa_handle)
  174. {
  175. #ifdef _WIN32
  176. #define PALIB "libpulse-0.dll"
  177. #elif defined(__APPLE__) && defined(__MACH__)
  178. #define PALIB "libpulse.0.dylib"
  179. #else
  180. #define PALIB "libpulse.so.0"
  181. #endif
  182. pa_handle = LoadLib(PALIB);
  183. if(!pa_handle)
  184. return ALC_FALSE;
  185. #define LOAD_FUNC(x) do { \
  186. p##x = GetSymbol(pa_handle, #x); \
  187. if(!(p##x)) { \
  188. ret = ALC_FALSE; \
  189. } \
  190. } while(0)
  191. LOAD_FUNC(pa_context_unref);
  192. LOAD_FUNC(pa_sample_spec_valid);
  193. LOAD_FUNC(pa_stream_drop);
  194. LOAD_FUNC(pa_frame_size);
  195. LOAD_FUNC(pa_strerror);
  196. LOAD_FUNC(pa_context_get_state);
  197. LOAD_FUNC(pa_stream_get_state);
  198. LOAD_FUNC(pa_threaded_mainloop_signal);
  199. LOAD_FUNC(pa_stream_peek);
  200. LOAD_FUNC(pa_threaded_mainloop_wait);
  201. LOAD_FUNC(pa_threaded_mainloop_unlock);
  202. LOAD_FUNC(pa_threaded_mainloop_in_thread);
  203. LOAD_FUNC(pa_context_new);
  204. LOAD_FUNC(pa_threaded_mainloop_stop);
  205. LOAD_FUNC(pa_context_disconnect);
  206. LOAD_FUNC(pa_threaded_mainloop_start);
  207. LOAD_FUNC(pa_threaded_mainloop_get_api);
  208. LOAD_FUNC(pa_context_set_state_callback);
  209. LOAD_FUNC(pa_stream_write);
  210. LOAD_FUNC(pa_xfree);
  211. LOAD_FUNC(pa_stream_connect_record);
  212. LOAD_FUNC(pa_stream_connect_playback);
  213. LOAD_FUNC(pa_stream_readable_size);
  214. LOAD_FUNC(pa_stream_writable_size);
  215. LOAD_FUNC(pa_stream_is_corked);
  216. LOAD_FUNC(pa_stream_cork);
  217. LOAD_FUNC(pa_stream_is_suspended);
  218. LOAD_FUNC(pa_stream_get_device_name);
  219. LOAD_FUNC(pa_stream_get_latency);
  220. LOAD_FUNC(pa_path_get_filename);
  221. LOAD_FUNC(pa_get_binary_name);
  222. LOAD_FUNC(pa_threaded_mainloop_free);
  223. LOAD_FUNC(pa_context_errno);
  224. LOAD_FUNC(pa_xmalloc);
  225. LOAD_FUNC(pa_stream_unref);
  226. LOAD_FUNC(pa_threaded_mainloop_accept);
  227. LOAD_FUNC(pa_stream_set_write_callback);
  228. LOAD_FUNC(pa_threaded_mainloop_new);
  229. LOAD_FUNC(pa_context_connect);
  230. LOAD_FUNC(pa_stream_set_buffer_attr);
  231. LOAD_FUNC(pa_stream_get_buffer_attr);
  232. LOAD_FUNC(pa_stream_get_sample_spec);
  233. LOAD_FUNC(pa_stream_get_time);
  234. LOAD_FUNC(pa_stream_set_read_callback);
  235. LOAD_FUNC(pa_stream_set_state_callback);
  236. LOAD_FUNC(pa_stream_set_moved_callback);
  237. LOAD_FUNC(pa_stream_set_underflow_callback);
  238. LOAD_FUNC(pa_stream_new_with_proplist);
  239. LOAD_FUNC(pa_stream_disconnect);
  240. LOAD_FUNC(pa_threaded_mainloop_lock);
  241. LOAD_FUNC(pa_channel_map_init_auto);
  242. LOAD_FUNC(pa_channel_map_parse);
  243. LOAD_FUNC(pa_channel_map_snprint);
  244. LOAD_FUNC(pa_channel_map_equal);
  245. LOAD_FUNC(pa_context_get_server_info);
  246. LOAD_FUNC(pa_context_get_sink_info_by_name);
  247. LOAD_FUNC(pa_context_get_sink_info_list);
  248. LOAD_FUNC(pa_context_get_source_info_by_name);
  249. LOAD_FUNC(pa_context_get_source_info_list);
  250. LOAD_FUNC(pa_operation_get_state);
  251. LOAD_FUNC(pa_operation_unref);
  252. LOAD_FUNC(pa_proplist_new);
  253. LOAD_FUNC(pa_proplist_free);
  254. LOAD_FUNC(pa_proplist_set);
  255. LOAD_FUNC(pa_channel_map_superset);
  256. LOAD_FUNC(pa_stream_set_buffer_attr_callback);
  257. LOAD_FUNC(pa_stream_begin_write);
  258. #undef LOAD_FUNC
  259. if(ret == ALC_FALSE)
  260. {
  261. CloseLib(pa_handle);
  262. pa_handle = NULL;
  263. }
  264. }
  265. #endif /* HAVE_DYNLOAD */
  266. return ret;
  267. }
  268. /* Global flags and properties */
  269. static pa_context_flags_t pulse_ctx_flags;
  270. static pa_proplist *prop_filter;
  271. /* PulseAudio Event Callbacks */
  272. static void context_state_callback(pa_context *context, void *pdata)
  273. {
  274. pa_threaded_mainloop *loop = pdata;
  275. pa_context_state_t state;
  276. state = pa_context_get_state(context);
  277. if(state == PA_CONTEXT_READY || !PA_CONTEXT_IS_GOOD(state))
  278. pa_threaded_mainloop_signal(loop, 0);
  279. }
  280. static void stream_state_callback(pa_stream *stream, void *pdata)
  281. {
  282. pa_threaded_mainloop *loop = pdata;
  283. pa_stream_state_t state;
  284. state = pa_stream_get_state(stream);
  285. if(state == PA_STREAM_READY || !PA_STREAM_IS_GOOD(state))
  286. pa_threaded_mainloop_signal(loop, 0);
  287. }
  288. static void stream_success_callback(pa_stream *UNUSED(stream), int UNUSED(success), void *pdata)
  289. {
  290. pa_threaded_mainloop *loop = pdata;
  291. pa_threaded_mainloop_signal(loop, 0);
  292. }
  293. static void wait_for_operation(pa_operation *op, pa_threaded_mainloop *loop)
  294. {
  295. if(op)
  296. {
  297. while(pa_operation_get_state(op) == PA_OPERATION_RUNNING)
  298. pa_threaded_mainloop_wait(loop);
  299. pa_operation_unref(op);
  300. }
  301. }
  302. static pa_context *connect_context(pa_threaded_mainloop *loop, ALboolean silent)
  303. {
  304. const char *name = "OpenAL Soft";
  305. char path_name[PATH_MAX];
  306. pa_context_state_t state;
  307. pa_context *context;
  308. int err;
  309. if(pa_get_binary_name(path_name, sizeof(path_name)))
  310. name = pa_path_get_filename(path_name);
  311. context = pa_context_new(pa_threaded_mainloop_get_api(loop), name);
  312. if(!context)
  313. {
  314. ERR("pa_context_new() failed\n");
  315. return NULL;
  316. }
  317. pa_context_set_state_callback(context, context_state_callback, loop);
  318. if((err=pa_context_connect(context, NULL, pulse_ctx_flags, NULL)) >= 0)
  319. {
  320. while((state=pa_context_get_state(context)) != PA_CONTEXT_READY)
  321. {
  322. if(!PA_CONTEXT_IS_GOOD(state))
  323. {
  324. err = pa_context_errno(context);
  325. if(err > 0) err = -err;
  326. break;
  327. }
  328. pa_threaded_mainloop_wait(loop);
  329. }
  330. }
  331. pa_context_set_state_callback(context, NULL, NULL);
  332. if(err < 0)
  333. {
  334. if(!silent)
  335. ERR("Context did not connect: %s\n", pa_strerror(err));
  336. pa_context_unref(context);
  337. return NULL;
  338. }
  339. return context;
  340. }
  341. static ALCboolean pulse_open(pa_threaded_mainloop **loop, pa_context **context,
  342. void(*state_cb)(pa_context*,void*), void *ptr)
  343. {
  344. if(!(*loop = pa_threaded_mainloop_new()))
  345. {
  346. ERR("pa_threaded_mainloop_new() failed!\n");
  347. return ALC_FALSE;
  348. }
  349. if(pa_threaded_mainloop_start(*loop) < 0)
  350. {
  351. ERR("pa_threaded_mainloop_start() failed\n");
  352. goto error;
  353. }
  354. pa_threaded_mainloop_lock(*loop);
  355. *context = connect_context(*loop, AL_FALSE);
  356. if(!*context)
  357. {
  358. pa_threaded_mainloop_unlock(*loop);
  359. pa_threaded_mainloop_stop(*loop);
  360. goto error;
  361. }
  362. pa_context_set_state_callback(*context, state_cb, ptr);
  363. pa_threaded_mainloop_unlock(*loop);
  364. return ALC_TRUE;
  365. error:
  366. pa_threaded_mainloop_free(*loop);
  367. *loop = NULL;
  368. return ALC_FALSE;
  369. }
  370. static void pulse_close(pa_threaded_mainloop *loop, pa_context *context, pa_stream *stream)
  371. {
  372. pa_threaded_mainloop_lock(loop);
  373. if(stream)
  374. {
  375. pa_stream_set_state_callback(stream, NULL, NULL);
  376. pa_stream_set_moved_callback(stream, NULL, NULL);
  377. pa_stream_set_write_callback(stream, NULL, NULL);
  378. pa_stream_set_buffer_attr_callback(stream, NULL, NULL);
  379. pa_stream_disconnect(stream);
  380. pa_stream_unref(stream);
  381. }
  382. pa_context_disconnect(context);
  383. pa_context_unref(context);
  384. pa_threaded_mainloop_unlock(loop);
  385. pa_threaded_mainloop_stop(loop);
  386. pa_threaded_mainloop_free(loop);
  387. }
  388. typedef struct {
  389. al_string name;
  390. al_string device_name;
  391. } DevMap;
  392. TYPEDEF_VECTOR(DevMap, vector_DevMap)
  393. static vector_DevMap PlaybackDevices;
  394. static vector_DevMap CaptureDevices;
  395. static void clear_devlist(vector_DevMap *list)
  396. {
  397. #define DEINIT_STRS(i) (AL_STRING_DEINIT((i)->name),AL_STRING_DEINIT((i)->device_name))
  398. VECTOR_FOR_EACH(DevMap, *list, DEINIT_STRS);
  399. #undef DEINIT_STRS
  400. VECTOR_RESIZE(*list, 0);
  401. }
  402. typedef struct ALCpulsePlayback {
  403. DERIVE_FROM_TYPE(ALCbackend);
  404. al_string device_name;
  405. pa_buffer_attr attr;
  406. pa_sample_spec spec;
  407. pa_threaded_mainloop *loop;
  408. pa_stream *stream;
  409. pa_context *context;
  410. volatile ALboolean killNow;
  411. althrd_t thread;
  412. } ALCpulsePlayback;
  413. static void ALCpulsePlayback_deviceCallback(pa_context *context, const pa_sink_info *info, int eol, void *pdata);
  414. static void ALCpulsePlayback_probeDevices(void);
  415. static void ALCpulsePlayback_bufferAttrCallback(pa_stream *stream, void *pdata);
  416. static void ALCpulsePlayback_contextStateCallback(pa_context *context, void *pdata);
  417. static void ALCpulsePlayback_streamStateCallback(pa_stream *stream, void *pdata);
  418. static void ALCpulsePlayback_streamWriteCallback(pa_stream *p, size_t nbytes, void *userdata);
  419. static void ALCpulsePlayback_sinkInfoCallback(pa_context *context, const pa_sink_info *info, int eol, void *pdata);
  420. static void ALCpulsePlayback_sinkNameCallback(pa_context *context, const pa_sink_info *info, int eol, void *pdata);
  421. static void ALCpulsePlayback_streamMovedCallback(pa_stream *stream, void *pdata);
  422. static pa_stream *ALCpulsePlayback_connectStream(const char *device_name, pa_threaded_mainloop *loop,
  423. pa_context *context, pa_stream_flags_t flags,
  424. pa_buffer_attr *attr, pa_sample_spec *spec,
  425. pa_channel_map *chanmap);
  426. static int ALCpulsePlayback_mixerProc(void *ptr);
  427. static void ALCpulsePlayback_Construct(ALCpulsePlayback *self, ALCdevice *device);
  428. static void ALCpulsePlayback_Destruct(ALCpulsePlayback *self);
  429. static ALCenum ALCpulsePlayback_open(ALCpulsePlayback *self, const ALCchar *name);
  430. static void ALCpulsePlayback_close(ALCpulsePlayback *self);
  431. static ALCboolean ALCpulsePlayback_reset(ALCpulsePlayback *self);
  432. static ALCboolean ALCpulsePlayback_start(ALCpulsePlayback *self);
  433. static void ALCpulsePlayback_stop(ALCpulsePlayback *self);
  434. static DECLARE_FORWARD2(ALCpulsePlayback, ALCbackend, ALCenum, captureSamples, ALCvoid*, ALCuint)
  435. static DECLARE_FORWARD(ALCpulsePlayback, ALCbackend, ALCuint, availableSamples)
  436. static ALint64 ALCpulsePlayback_getLatency(ALCpulsePlayback *self);
  437. static void ALCpulsePlayback_lock(ALCpulsePlayback *self);
  438. static void ALCpulsePlayback_unlock(ALCpulsePlayback *self);
  439. DECLARE_DEFAULT_ALLOCATORS(ALCpulsePlayback)
  440. DEFINE_ALCBACKEND_VTABLE(ALCpulsePlayback);
  441. static void ALCpulsePlayback_Construct(ALCpulsePlayback *self, ALCdevice *device)
  442. {
  443. ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
  444. SET_VTABLE2(ALCpulsePlayback, ALCbackend, self);
  445. AL_STRING_INIT(self->device_name);
  446. }
  447. static void ALCpulsePlayback_Destruct(ALCpulsePlayback *self)
  448. {
  449. AL_STRING_DEINIT(self->device_name);
  450. ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
  451. }
  452. static void ALCpulsePlayback_deviceCallback(pa_context *UNUSED(context), const pa_sink_info *info, int eol, void *pdata)
  453. {
  454. pa_threaded_mainloop *loop = pdata;
  455. const DevMap *iter;
  456. DevMap entry;
  457. int count;
  458. if(eol)
  459. {
  460. pa_threaded_mainloop_signal(loop, 0);
  461. return;
  462. }
  463. #define MATCH_INFO_NAME(iter) (al_string_cmp_cstr((iter)->device_name, info->name) == 0)
  464. VECTOR_FIND_IF(iter, const DevMap, PlaybackDevices, MATCH_INFO_NAME);
  465. if(iter != VECTOR_ITER_END(PlaybackDevices)) return;
  466. #undef MATCH_INFO_NAME
  467. AL_STRING_INIT(entry.name);
  468. AL_STRING_INIT(entry.device_name);
  469. al_string_copy_cstr(&entry.device_name, info->name);
  470. count = 0;
  471. while(1)
  472. {
  473. al_string_copy_cstr(&entry.name, info->description);
  474. if(count != 0)
  475. {
  476. char str[64];
  477. snprintf(str, sizeof(str), " #%d", count+1);
  478. al_string_append_cstr(&entry.name, str);
  479. }
  480. #define MATCH_ENTRY(i) (al_string_cmp(entry.name, (i)->name) == 0)
  481. VECTOR_FIND_IF(iter, const DevMap, PlaybackDevices, MATCH_ENTRY);
  482. if(iter == VECTOR_ITER_END(PlaybackDevices)) break;
  483. #undef MATCH_ENTRY
  484. count++;
  485. }
  486. TRACE("Got device \"%s\", \"%s\"\n", al_string_get_cstr(entry.name), al_string_get_cstr(entry.device_name));
  487. VECTOR_PUSH_BACK(PlaybackDevices, entry);
  488. }
  489. static void ALCpulsePlayback_probeDevices(void)
  490. {
  491. pa_threaded_mainloop *loop;
  492. clear_devlist(&PlaybackDevices);
  493. if((loop=pa_threaded_mainloop_new()) &&
  494. pa_threaded_mainloop_start(loop) >= 0)
  495. {
  496. pa_context *context;
  497. pa_threaded_mainloop_lock(loop);
  498. context = connect_context(loop, AL_FALSE);
  499. if(context)
  500. {
  501. pa_operation *o;
  502. pa_stream_flags_t flags;
  503. pa_sample_spec spec;
  504. pa_stream *stream;
  505. flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
  506. PA_STREAM_FIX_CHANNELS | PA_STREAM_DONT_MOVE;
  507. spec.format = PA_SAMPLE_S16NE;
  508. spec.rate = 44100;
  509. spec.channels = 2;
  510. stream = ALCpulsePlayback_connectStream(NULL, loop, context, flags,
  511. NULL, &spec, NULL);
  512. if(stream)
  513. {
  514. o = pa_context_get_sink_info_by_name(context, pa_stream_get_device_name(stream),
  515. ALCpulsePlayback_deviceCallback, loop);
  516. wait_for_operation(o, loop);
  517. pa_stream_disconnect(stream);
  518. pa_stream_unref(stream);
  519. stream = NULL;
  520. }
  521. o = pa_context_get_sink_info_list(context, ALCpulsePlayback_deviceCallback, loop);
  522. wait_for_operation(o, loop);
  523. pa_context_disconnect(context);
  524. pa_context_unref(context);
  525. }
  526. pa_threaded_mainloop_unlock(loop);
  527. pa_threaded_mainloop_stop(loop);
  528. }
  529. if(loop)
  530. pa_threaded_mainloop_free(loop);
  531. }
  532. static void ALCpulsePlayback_bufferAttrCallback(pa_stream *stream, void *pdata)
  533. {
  534. ALCpulsePlayback *self = pdata;
  535. self->attr = *pa_stream_get_buffer_attr(stream);
  536. TRACE("minreq=%d, tlength=%d, prebuf=%d\n", self->attr.minreq, self->attr.tlength, self->attr.prebuf);
  537. }
  538. static void ALCpulsePlayback_contextStateCallback(pa_context *context, void *pdata)
  539. {
  540. ALCpulsePlayback *self = pdata;
  541. if(pa_context_get_state(context) == PA_CONTEXT_FAILED)
  542. {
  543. ERR("Received context failure!\n");
  544. aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
  545. }
  546. pa_threaded_mainloop_signal(self->loop, 0);
  547. }
  548. static void ALCpulsePlayback_streamStateCallback(pa_stream *stream, void *pdata)
  549. {
  550. ALCpulsePlayback *self = pdata;
  551. if(pa_stream_get_state(stream) == PA_STREAM_FAILED)
  552. {
  553. ERR("Received stream failure!\n");
  554. aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
  555. }
  556. pa_threaded_mainloop_signal(self->loop, 0);
  557. }
  558. static void ALCpulsePlayback_streamWriteCallback(pa_stream* UNUSED(p), size_t UNUSED(nbytes), void *pdata)
  559. {
  560. ALCpulsePlayback *self = pdata;
  561. pa_threaded_mainloop_signal(self->loop, 0);
  562. }
  563. static void ALCpulsePlayback_sinkInfoCallback(pa_context *UNUSED(context), const pa_sink_info *info, int eol, void *pdata)
  564. {
  565. static const struct {
  566. enum DevFmtChannels chans;
  567. pa_channel_map map;
  568. } chanmaps[] = {
  569. { DevFmtX71, { 8, {
  570. PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
  571. PA_CHANNEL_POSITION_FRONT_CENTER, PA_CHANNEL_POSITION_LFE,
  572. PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
  573. PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT
  574. } } },
  575. { DevFmtX61, { 7, {
  576. PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
  577. PA_CHANNEL_POSITION_FRONT_CENTER, PA_CHANNEL_POSITION_LFE,
  578. PA_CHANNEL_POSITION_REAR_CENTER,
  579. PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT
  580. } } },
  581. { DevFmtX51, { 6, {
  582. PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
  583. PA_CHANNEL_POSITION_FRONT_CENTER, PA_CHANNEL_POSITION_LFE,
  584. PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT
  585. } } },
  586. { DevFmtX51Rear, { 6, {
  587. PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
  588. PA_CHANNEL_POSITION_FRONT_CENTER, PA_CHANNEL_POSITION_LFE,
  589. PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT
  590. } } },
  591. { DevFmtQuad, { 4, {
  592. PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
  593. PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT
  594. } } },
  595. { DevFmtStereo, { 2, {
  596. PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT
  597. } } },
  598. { DevFmtMono, { 1, {PA_CHANNEL_POSITION_MONO} } }
  599. };
  600. ALCpulsePlayback *self = pdata;
  601. ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
  602. size_t i;
  603. if(eol)
  604. {
  605. pa_threaded_mainloop_signal(self->loop, 0);
  606. return;
  607. }
  608. for(i = 0;i < COUNTOF(chanmaps);i++)
  609. {
  610. if(pa_channel_map_superset(&info->channel_map, &chanmaps[i].map))
  611. {
  612. if(!(device->Flags&DEVICE_CHANNELS_REQUEST))
  613. device->FmtChans = chanmaps[i].chans;
  614. break;
  615. }
  616. }
  617. if(i == COUNTOF(chanmaps))
  618. {
  619. char chanmap_str[PA_CHANNEL_MAP_SNPRINT_MAX] = "";
  620. pa_channel_map_snprint(chanmap_str, sizeof(chanmap_str), &info->channel_map);
  621. WARN("Failed to find format for channel map:\n %s\n", chanmap_str);
  622. }
  623. if(info->active_port)
  624. TRACE("Active port: %s (%s)\n", info->active_port->name, info->active_port->description);
  625. device->IsHeadphones = (info->active_port &&
  626. strcmp(info->active_port->name, "analog-output-headphones") == 0 &&
  627. device->FmtChans == DevFmtStereo);
  628. }
  629. static void ALCpulsePlayback_sinkNameCallback(pa_context *UNUSED(context), const pa_sink_info *info, int eol, void *pdata)
  630. {
  631. ALCpulsePlayback *self = pdata;
  632. ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
  633. if(eol)
  634. {
  635. pa_threaded_mainloop_signal(self->loop, 0);
  636. return;
  637. }
  638. al_string_copy_cstr(&device->DeviceName, info->description);
  639. }
  640. static void ALCpulsePlayback_streamMovedCallback(pa_stream *stream, void *pdata)
  641. {
  642. ALCpulsePlayback *self = pdata;
  643. al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(stream));
  644. TRACE("Stream moved to %s\n", al_string_get_cstr(self->device_name));
  645. }
  646. static pa_stream *ALCpulsePlayback_connectStream(const char *device_name,
  647. pa_threaded_mainloop *loop, pa_context *context,
  648. pa_stream_flags_t flags, pa_buffer_attr *attr, pa_sample_spec *spec,
  649. pa_channel_map *chanmap)
  650. {
  651. pa_stream_state_t state;
  652. pa_stream *stream;
  653. stream = pa_stream_new_with_proplist(context, "Playback Stream", spec, chanmap, prop_filter);
  654. if(!stream)
  655. {
  656. ERR("pa_stream_new_with_proplist() failed: %s\n", pa_strerror(pa_context_errno(context)));
  657. return NULL;
  658. }
  659. pa_stream_set_state_callback(stream, stream_state_callback, loop);
  660. if(pa_stream_connect_playback(stream, device_name, attr, flags, NULL, NULL) < 0)
  661. {
  662. ERR("Stream did not connect: %s\n", pa_strerror(pa_context_errno(context)));
  663. pa_stream_unref(stream);
  664. return NULL;
  665. }
  666. while((state=pa_stream_get_state(stream)) != PA_STREAM_READY)
  667. {
  668. if(!PA_STREAM_IS_GOOD(state))
  669. {
  670. ERR("Stream did not get ready: %s\n", pa_strerror(pa_context_errno(context)));
  671. pa_stream_unref(stream);
  672. return NULL;
  673. }
  674. pa_threaded_mainloop_wait(loop);
  675. }
  676. pa_stream_set_state_callback(stream, NULL, NULL);
  677. return stream;
  678. }
  679. static int ALCpulsePlayback_mixerProc(void *ptr)
  680. {
  681. ALCpulsePlayback *self = ptr;
  682. ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
  683. ALuint buffer_size;
  684. ALint update_size;
  685. size_t frame_size;
  686. ssize_t len;
  687. SetRTPriority();
  688. althrd_setname(althrd_current(), MIXER_THREAD_NAME);
  689. pa_threaded_mainloop_lock(self->loop);
  690. frame_size = pa_frame_size(&self->spec);
  691. update_size = device->UpdateSize * frame_size;
  692. /* Sanitize buffer metrics, in case we actually have less than what we
  693. * asked for. */
  694. buffer_size = minu(update_size*device->NumUpdates, self->attr.tlength);
  695. update_size = minu(update_size, buffer_size/2);
  696. do {
  697. len = pa_stream_writable_size(self->stream) - self->attr.tlength +
  698. buffer_size;
  699. if(len < update_size)
  700. {
  701. if(pa_stream_is_corked(self->stream) == 1)
  702. {
  703. pa_operation *o;
  704. o = pa_stream_cork(self->stream, 0, NULL, NULL);
  705. if(o) pa_operation_unref(o);
  706. }
  707. pa_threaded_mainloop_wait(self->loop);
  708. continue;
  709. }
  710. len -= len%update_size;
  711. while(len > 0)
  712. {
  713. size_t newlen = len;
  714. void *buf;
  715. pa_free_cb_t free_func = NULL;
  716. if(pa_stream_begin_write(self->stream, &buf, &newlen) < 0)
  717. {
  718. buf = pa_xmalloc(newlen);
  719. free_func = pa_xfree;
  720. }
  721. aluMixData(device, buf, newlen/frame_size);
  722. pa_stream_write(self->stream, buf, newlen, free_func, 0, PA_SEEK_RELATIVE);
  723. len -= newlen;
  724. }
  725. } while(!self->killNow && device->Connected);
  726. pa_threaded_mainloop_unlock(self->loop);
  727. return 0;
  728. }
  729. static ALCenum ALCpulsePlayback_open(ALCpulsePlayback *self, const ALCchar *name)
  730. {
  731. const_al_string dev_name = AL_STRING_INIT_STATIC();
  732. const char *pulse_name = NULL;
  733. pa_stream_flags_t flags;
  734. pa_sample_spec spec;
  735. if(name)
  736. {
  737. const DevMap *iter;
  738. if(VECTOR_SIZE(PlaybackDevices) == 0)
  739. ALCpulsePlayback_probeDevices();
  740. #define MATCH_NAME(iter) (al_string_cmp_cstr((iter)->name, name) == 0)
  741. VECTOR_FIND_IF(iter, const DevMap, PlaybackDevices, MATCH_NAME);
  742. #undef MATCH_NAME
  743. if(iter == VECTOR_ITER_END(PlaybackDevices))
  744. return ALC_INVALID_VALUE;
  745. pulse_name = al_string_get_cstr(iter->device_name);
  746. dev_name = iter->name;
  747. }
  748. if(!pulse_open(&self->loop, &self->context, ALCpulsePlayback_contextStateCallback, self))
  749. return ALC_INVALID_VALUE;
  750. pa_threaded_mainloop_lock(self->loop);
  751. flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
  752. PA_STREAM_FIX_CHANNELS;
  753. if(!GetConfigValueBool(NULL, "pulse", "allow-moves", 0))
  754. flags |= PA_STREAM_DONT_MOVE;
  755. spec.format = PA_SAMPLE_S16NE;
  756. spec.rate = 44100;
  757. spec.channels = 2;
  758. TRACE("Connecting to \"%s\"\n", pulse_name ? pulse_name : "(default)");
  759. self->stream = ALCpulsePlayback_connectStream(pulse_name, self->loop, self->context,
  760. flags, NULL, &spec, NULL);
  761. if(!self->stream)
  762. {
  763. pa_threaded_mainloop_unlock(self->loop);
  764. pulse_close(self->loop, self->context, self->stream);
  765. self->loop = NULL;
  766. self->context = NULL;
  767. return ALC_INVALID_VALUE;
  768. }
  769. pa_stream_set_moved_callback(self->stream, ALCpulsePlayback_streamMovedCallback, self);
  770. al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(self->stream));
  771. if(al_string_empty(dev_name))
  772. {
  773. pa_operation *o = pa_context_get_sink_info_by_name(
  774. self->context, al_string_get_cstr(self->device_name),
  775. ALCpulsePlayback_sinkNameCallback, self
  776. );
  777. wait_for_operation(o, self->loop);
  778. }
  779. else
  780. {
  781. ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
  782. al_string_copy(&device->DeviceName, dev_name);
  783. }
  784. pa_threaded_mainloop_unlock(self->loop);
  785. return ALC_NO_ERROR;
  786. }
  787. static void ALCpulsePlayback_close(ALCpulsePlayback *self)
  788. {
  789. pulse_close(self->loop, self->context, self->stream);
  790. self->loop = NULL;
  791. self->context = NULL;
  792. self->stream = NULL;
  793. al_string_clear(&self->device_name);
  794. }
  795. static ALCboolean ALCpulsePlayback_reset(ALCpulsePlayback *self)
  796. {
  797. ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
  798. pa_stream_flags_t flags = 0;
  799. const char *mapname = NULL;
  800. pa_channel_map chanmap;
  801. pa_operation *o;
  802. ALuint len;
  803. pa_threaded_mainloop_lock(self->loop);
  804. if(self->stream)
  805. {
  806. pa_stream_set_state_callback(self->stream, NULL, NULL);
  807. pa_stream_set_moved_callback(self->stream, NULL, NULL);
  808. pa_stream_set_write_callback(self->stream, NULL, NULL);
  809. pa_stream_set_buffer_attr_callback(self->stream, NULL, NULL);
  810. pa_stream_disconnect(self->stream);
  811. pa_stream_unref(self->stream);
  812. self->stream = NULL;
  813. }
  814. o = pa_context_get_sink_info_by_name(self->context, al_string_get_cstr(self->device_name),
  815. ALCpulsePlayback_sinkInfoCallback, self);
  816. wait_for_operation(o, self->loop);
  817. if(GetConfigValueBool(al_string_get_cstr(device->DeviceName), "pulse", "fix-rate", 0) ||
  818. !(device->Flags&DEVICE_FREQUENCY_REQUEST))
  819. flags |= PA_STREAM_FIX_RATE;
  820. flags |= PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
  821. flags |= PA_STREAM_ADJUST_LATENCY;
  822. flags |= PA_STREAM_START_CORKED;
  823. if(!GetConfigValueBool(NULL, "pulse", "allow-moves", 0))
  824. flags |= PA_STREAM_DONT_MOVE;
  825. switch(device->FmtType)
  826. {
  827. case DevFmtByte:
  828. device->FmtType = DevFmtUByte;
  829. /* fall-through */
  830. case DevFmtUByte:
  831. self->spec.format = PA_SAMPLE_U8;
  832. break;
  833. case DevFmtUShort:
  834. device->FmtType = DevFmtShort;
  835. /* fall-through */
  836. case DevFmtShort:
  837. self->spec.format = PA_SAMPLE_S16NE;
  838. break;
  839. case DevFmtUInt:
  840. device->FmtType = DevFmtInt;
  841. /* fall-through */
  842. case DevFmtInt:
  843. self->spec.format = PA_SAMPLE_S32NE;
  844. break;
  845. case DevFmtFloat:
  846. self->spec.format = PA_SAMPLE_FLOAT32NE;
  847. break;
  848. }
  849. self->spec.rate = device->Frequency;
  850. self->spec.channels = ChannelsFromDevFmt(device->FmtChans);
  851. if(pa_sample_spec_valid(&self->spec) == 0)
  852. {
  853. ERR("Invalid sample format\n");
  854. pa_threaded_mainloop_unlock(self->loop);
  855. return ALC_FALSE;
  856. }
  857. switch(device->FmtChans)
  858. {
  859. case DevFmtMono:
  860. mapname = "mono";
  861. break;
  862. case DevFmtBFormat3D:
  863. device->FmtChans = DevFmtStereo;
  864. /*fall-through*/
  865. case DevFmtStereo:
  866. mapname = "front-left,front-right";
  867. break;
  868. case DevFmtQuad:
  869. mapname = "front-left,front-right,rear-left,rear-right";
  870. break;
  871. case DevFmtX51:
  872. mapname = "front-left,front-right,front-center,lfe,side-left,side-right";
  873. break;
  874. case DevFmtX51Rear:
  875. mapname = "front-left,front-right,front-center,lfe,rear-left,rear-right";
  876. break;
  877. case DevFmtX61:
  878. mapname = "front-left,front-right,front-center,lfe,rear-center,side-left,side-right";
  879. break;
  880. case DevFmtX71:
  881. mapname = "front-left,front-right,front-center,lfe,rear-left,rear-right,side-left,side-right";
  882. break;
  883. }
  884. if(!pa_channel_map_parse(&chanmap, mapname))
  885. {
  886. ERR("Failed to build channel map for %s\n", DevFmtChannelsString(device->FmtChans));
  887. pa_threaded_mainloop_unlock(self->loop);
  888. return ALC_FALSE;
  889. }
  890. SetDefaultWFXChannelOrder(device);
  891. self->attr.fragsize = -1;
  892. self->attr.prebuf = 0;
  893. self->attr.minreq = device->UpdateSize * pa_frame_size(&self->spec);
  894. self->attr.tlength = self->attr.minreq * maxu(device->NumUpdates, 2);
  895. self->attr.maxlength = -1;
  896. self->stream = ALCpulsePlayback_connectStream(al_string_get_cstr(self->device_name),
  897. self->loop, self->context, flags,
  898. &self->attr, &self->spec, &chanmap);
  899. if(!self->stream)
  900. {
  901. pa_threaded_mainloop_unlock(self->loop);
  902. return ALC_FALSE;
  903. }
  904. pa_stream_set_state_callback(self->stream, ALCpulsePlayback_streamStateCallback, self);
  905. pa_stream_set_moved_callback(self->stream, ALCpulsePlayback_streamMovedCallback, self);
  906. pa_stream_set_write_callback(self->stream, ALCpulsePlayback_streamWriteCallback, self);
  907. self->spec = *(pa_stream_get_sample_spec(self->stream));
  908. if(device->Frequency != self->spec.rate)
  909. {
  910. /* Server updated our playback rate, so modify the buffer attribs
  911. * accordingly. */
  912. device->NumUpdates = (ALuint)((ALdouble)device->NumUpdates / device->Frequency *
  913. self->spec.rate + 0.5);
  914. self->attr.minreq = device->UpdateSize * pa_frame_size(&self->spec);
  915. self->attr.tlength = self->attr.minreq * clampu(device->NumUpdates, 2, 16);
  916. self->attr.maxlength = -1;
  917. self->attr.prebuf = 0;
  918. o = pa_stream_set_buffer_attr(self->stream, &self->attr,
  919. stream_success_callback, self->loop);
  920. wait_for_operation(o, self->loop);
  921. device->Frequency = self->spec.rate;
  922. }
  923. pa_stream_set_buffer_attr_callback(self->stream, ALCpulsePlayback_bufferAttrCallback, self);
  924. ALCpulsePlayback_bufferAttrCallback(self->stream, self);
  925. len = self->attr.minreq / pa_frame_size(&self->spec);
  926. device->NumUpdates = (ALuint)((ALdouble)device->NumUpdates/len*device->UpdateSize + 0.5);
  927. device->NumUpdates = clampu(device->NumUpdates, 2, 16);
  928. device->UpdateSize = len;
  929. pa_threaded_mainloop_unlock(self->loop);
  930. return ALC_TRUE;
  931. }
  932. static ALCboolean ALCpulsePlayback_start(ALCpulsePlayback *self)
  933. {
  934. self->killNow = AL_FALSE;
  935. if(althrd_create(&self->thread, ALCpulsePlayback_mixerProc, self) != althrd_success)
  936. return ALC_FALSE;
  937. return ALC_TRUE;
  938. }
  939. static void ALCpulsePlayback_stop(ALCpulsePlayback *self)
  940. {
  941. pa_operation *o;
  942. int res;
  943. if(!self->stream || self->killNow)
  944. return;
  945. self->killNow = AL_TRUE;
  946. /* Signal the main loop in case PulseAudio isn't sending us audio requests
  947. * (e.g. if the device is suspended). We need to lock the mainloop in case
  948. * the mixer is between checking the killNow flag but before waiting for
  949. * the signal.
  950. */
  951. pa_threaded_mainloop_lock(self->loop);
  952. pa_threaded_mainloop_unlock(self->loop);
  953. pa_threaded_mainloop_signal(self->loop, 0);
  954. althrd_join(self->thread, &res);
  955. pa_threaded_mainloop_lock(self->loop);
  956. o = pa_stream_cork(self->stream, 1, stream_success_callback, self->loop);
  957. wait_for_operation(o, self->loop);
  958. pa_threaded_mainloop_unlock(self->loop);
  959. }
  960. static ALint64 ALCpulsePlayback_getLatency(ALCpulsePlayback *self)
  961. {
  962. pa_usec_t latency = 0;
  963. int neg, err;
  964. if((err=pa_stream_get_latency(self->stream, &latency, &neg)) != 0)
  965. {
  966. /* FIXME: if err = -PA_ERR_NODATA, it means we were called too soon
  967. * after starting the stream and no timing info has been received from
  968. * the server yet. Should we wait, possibly stalling the app, or give a
  969. * dummy value? Either way, it shouldn't be 0. */
  970. if(err != -PA_ERR_NODATA)
  971. ERR("Failed to get stream latency: 0x%x\n", err);
  972. return 0;
  973. }
  974. if(neg) latency = 0;
  975. return (ALint64)minu64(latency, U64(0x7fffffffffffffff)/1000) * 1000;
  976. }
  977. static void ALCpulsePlayback_lock(ALCpulsePlayback *self)
  978. {
  979. pa_threaded_mainloop_lock(self->loop);
  980. }
  981. static void ALCpulsePlayback_unlock(ALCpulsePlayback *self)
  982. {
  983. pa_threaded_mainloop_unlock(self->loop);
  984. }
  985. typedef struct ALCpulseCapture {
  986. DERIVE_FROM_TYPE(ALCbackend);
  987. al_string device_name;
  988. const void *cap_store;
  989. size_t cap_len;
  990. size_t cap_remain;
  991. ALCuint last_readable;
  992. pa_buffer_attr attr;
  993. pa_sample_spec spec;
  994. pa_threaded_mainloop *loop;
  995. pa_stream *stream;
  996. pa_context *context;
  997. } ALCpulseCapture;
  998. static void ALCpulseCapture_deviceCallback(pa_context *context, const pa_source_info *info, int eol, void *pdata);
  999. static void ALCpulseCapture_probeDevices(void);
  1000. static void ALCpulseCapture_contextStateCallback(pa_context *context, void *pdata);
  1001. static void ALCpulseCapture_streamStateCallback(pa_stream *stream, void *pdata);
  1002. static void ALCpulseCapture_sourceNameCallback(pa_context *context, const pa_source_info *info, int eol, void *pdata);
  1003. static void ALCpulseCapture_streamMovedCallback(pa_stream *stream, void *pdata);
  1004. static pa_stream *ALCpulseCapture_connectStream(const char *device_name,
  1005. pa_threaded_mainloop *loop, pa_context *context,
  1006. pa_stream_flags_t flags, pa_buffer_attr *attr,
  1007. pa_sample_spec *spec, pa_channel_map *chanmap);
  1008. static void ALCpulseCapture_Construct(ALCpulseCapture *self, ALCdevice *device);
  1009. static void ALCpulseCapture_Destruct(ALCpulseCapture *self);
  1010. static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name);
  1011. static void ALCpulseCapture_close(ALCpulseCapture *self);
  1012. static DECLARE_FORWARD(ALCpulseCapture, ALCbackend, ALCboolean, reset)
  1013. static ALCboolean ALCpulseCapture_start(ALCpulseCapture *self);
  1014. static void ALCpulseCapture_stop(ALCpulseCapture *self);
  1015. static ALCenum ALCpulseCapture_captureSamples(ALCpulseCapture *self, ALCvoid *buffer, ALCuint samples);
  1016. static ALCuint ALCpulseCapture_availableSamples(ALCpulseCapture *self);
  1017. static ALint64 ALCpulseCapture_getLatency(ALCpulseCapture *self);
  1018. static void ALCpulseCapture_lock(ALCpulseCapture *self);
  1019. static void ALCpulseCapture_unlock(ALCpulseCapture *self);
  1020. DECLARE_DEFAULT_ALLOCATORS(ALCpulseCapture)
  1021. DEFINE_ALCBACKEND_VTABLE(ALCpulseCapture);
  1022. static void ALCpulseCapture_Construct(ALCpulseCapture *self, ALCdevice *device)
  1023. {
  1024. ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
  1025. SET_VTABLE2(ALCpulseCapture, ALCbackend, self);
  1026. AL_STRING_INIT(self->device_name);
  1027. }
  1028. static void ALCpulseCapture_Destruct(ALCpulseCapture *self)
  1029. {
  1030. AL_STRING_DEINIT(self->device_name);
  1031. ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
  1032. }
  1033. static void ALCpulseCapture_deviceCallback(pa_context *UNUSED(context), const pa_source_info *info, int eol, void *pdata)
  1034. {
  1035. pa_threaded_mainloop *loop = pdata;
  1036. const DevMap *iter;
  1037. DevMap entry;
  1038. int count;
  1039. if(eol)
  1040. {
  1041. pa_threaded_mainloop_signal(loop, 0);
  1042. return;
  1043. }
  1044. #define MATCH_INFO_NAME(iter) (al_string_cmp_cstr((iter)->device_name, info->name) == 0)
  1045. VECTOR_FIND_IF(iter, const DevMap, CaptureDevices, MATCH_INFO_NAME);
  1046. if(iter != VECTOR_ITER_END(CaptureDevices)) return;
  1047. #undef MATCH_INFO_NAME
  1048. AL_STRING_INIT(entry.name);
  1049. AL_STRING_INIT(entry.device_name);
  1050. al_string_copy_cstr(&entry.device_name, info->name);
  1051. count = 0;
  1052. while(1)
  1053. {
  1054. al_string_copy_cstr(&entry.name, info->description);
  1055. if(count != 0)
  1056. {
  1057. char str[64];
  1058. snprintf(str, sizeof(str), " #%d", count+1);
  1059. al_string_append_cstr(&entry.name, str);
  1060. }
  1061. #define MATCH_ENTRY(i) (al_string_cmp(entry.name, (i)->name) == 0)
  1062. VECTOR_FIND_IF(iter, const DevMap, CaptureDevices, MATCH_ENTRY);
  1063. if(iter == VECTOR_ITER_END(CaptureDevices)) break;
  1064. #undef MATCH_ENTRY
  1065. count++;
  1066. }
  1067. TRACE("Got device \"%s\", \"%s\"\n", al_string_get_cstr(entry.name), al_string_get_cstr(entry.device_name));
  1068. VECTOR_PUSH_BACK(CaptureDevices, entry);
  1069. }
  1070. static void ALCpulseCapture_probeDevices(void)
  1071. {
  1072. pa_threaded_mainloop *loop;
  1073. clear_devlist(&CaptureDevices);
  1074. if((loop=pa_threaded_mainloop_new()) &&
  1075. pa_threaded_mainloop_start(loop) >= 0)
  1076. {
  1077. pa_context *context;
  1078. pa_threaded_mainloop_lock(loop);
  1079. context = connect_context(loop, AL_FALSE);
  1080. if(context)
  1081. {
  1082. pa_operation *o;
  1083. pa_stream_flags_t flags;
  1084. pa_sample_spec spec;
  1085. pa_stream *stream;
  1086. flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
  1087. PA_STREAM_FIX_CHANNELS | PA_STREAM_DONT_MOVE;
  1088. spec.format = PA_SAMPLE_S16NE;
  1089. spec.rate = 44100;
  1090. spec.channels = 1;
  1091. stream = ALCpulseCapture_connectStream(NULL, loop, context, flags,
  1092. NULL, &spec, NULL);
  1093. if(stream)
  1094. {
  1095. o = pa_context_get_source_info_by_name(context, pa_stream_get_device_name(stream),
  1096. ALCpulseCapture_deviceCallback, loop);
  1097. wait_for_operation(o, loop);
  1098. pa_stream_disconnect(stream);
  1099. pa_stream_unref(stream);
  1100. stream = NULL;
  1101. }
  1102. o = pa_context_get_source_info_list(context, ALCpulseCapture_deviceCallback, loop);
  1103. wait_for_operation(o, loop);
  1104. pa_context_disconnect(context);
  1105. pa_context_unref(context);
  1106. }
  1107. pa_threaded_mainloop_unlock(loop);
  1108. pa_threaded_mainloop_stop(loop);
  1109. }
  1110. if(loop)
  1111. pa_threaded_mainloop_free(loop);
  1112. }
  1113. static void ALCpulseCapture_contextStateCallback(pa_context *context, void *pdata)
  1114. {
  1115. ALCpulseCapture *self = pdata;
  1116. if(pa_context_get_state(context) == PA_CONTEXT_FAILED)
  1117. {
  1118. ERR("Received context failure!\n");
  1119. aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
  1120. }
  1121. pa_threaded_mainloop_signal(self->loop, 0);
  1122. }
  1123. static void ALCpulseCapture_streamStateCallback(pa_stream *stream, void *pdata)
  1124. {
  1125. ALCpulseCapture *self = pdata;
  1126. if(pa_stream_get_state(stream) == PA_STREAM_FAILED)
  1127. {
  1128. ERR("Received stream failure!\n");
  1129. aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice);
  1130. }
  1131. pa_threaded_mainloop_signal(self->loop, 0);
  1132. }
  1133. static void ALCpulseCapture_sourceNameCallback(pa_context *UNUSED(context), const pa_source_info *info, int eol, void *pdata)
  1134. {
  1135. ALCpulseCapture *self = pdata;
  1136. ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
  1137. if(eol)
  1138. {
  1139. pa_threaded_mainloop_signal(self->loop, 0);
  1140. return;
  1141. }
  1142. al_string_copy_cstr(&device->DeviceName, info->description);
  1143. }
  1144. static void ALCpulseCapture_streamMovedCallback(pa_stream *stream, void *pdata)
  1145. {
  1146. ALCpulseCapture *self = pdata;
  1147. al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(stream));
  1148. TRACE("Stream moved to %s\n", al_string_get_cstr(self->device_name));
  1149. }
  1150. static pa_stream *ALCpulseCapture_connectStream(const char *device_name,
  1151. pa_threaded_mainloop *loop, pa_context *context,
  1152. pa_stream_flags_t flags, pa_buffer_attr *attr, pa_sample_spec *spec,
  1153. pa_channel_map *chanmap)
  1154. {
  1155. pa_stream_state_t state;
  1156. pa_stream *stream;
  1157. stream = pa_stream_new_with_proplist(context, "Capture Stream", spec, chanmap, prop_filter);
  1158. if(!stream)
  1159. {
  1160. ERR("pa_stream_new_with_proplist() failed: %s\n", pa_strerror(pa_context_errno(context)));
  1161. return NULL;
  1162. }
  1163. pa_stream_set_state_callback(stream, stream_state_callback, loop);
  1164. if(pa_stream_connect_record(stream, device_name, attr, flags) < 0)
  1165. {
  1166. ERR("Stream did not connect: %s\n", pa_strerror(pa_context_errno(context)));
  1167. pa_stream_unref(stream);
  1168. return NULL;
  1169. }
  1170. while((state=pa_stream_get_state(stream)) != PA_STREAM_READY)
  1171. {
  1172. if(!PA_STREAM_IS_GOOD(state))
  1173. {
  1174. ERR("Stream did not get ready: %s\n", pa_strerror(pa_context_errno(context)));
  1175. pa_stream_unref(stream);
  1176. return NULL;
  1177. }
  1178. pa_threaded_mainloop_wait(loop);
  1179. }
  1180. pa_stream_set_state_callback(stream, NULL, NULL);
  1181. return stream;
  1182. }
  1183. static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name)
  1184. {
  1185. ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
  1186. const char *pulse_name = NULL;
  1187. pa_stream_flags_t flags = 0;
  1188. pa_channel_map chanmap;
  1189. ALuint samples;
  1190. if(name)
  1191. {
  1192. const DevMap *iter;
  1193. if(VECTOR_SIZE(CaptureDevices) == 0)
  1194. ALCpulseCapture_probeDevices();
  1195. #define MATCH_NAME(iter) (al_string_cmp_cstr((iter)->name, name) == 0)
  1196. VECTOR_FIND_IF(iter, const DevMap, CaptureDevices, MATCH_NAME);
  1197. #undef MATCH_NAME
  1198. if(iter == VECTOR_ITER_END(CaptureDevices))
  1199. return ALC_INVALID_VALUE;
  1200. pulse_name = al_string_get_cstr(iter->device_name);
  1201. al_string_copy(&device->DeviceName, iter->name);
  1202. }
  1203. if(!pulse_open(&self->loop, &self->context, ALCpulseCapture_contextStateCallback, self))
  1204. return ALC_INVALID_VALUE;
  1205. pa_threaded_mainloop_lock(self->loop);
  1206. self->spec.rate = device->Frequency;
  1207. self->spec.channels = ChannelsFromDevFmt(device->FmtChans);
  1208. switch(device->FmtType)
  1209. {
  1210. case DevFmtUByte:
  1211. self->spec.format = PA_SAMPLE_U8;
  1212. break;
  1213. case DevFmtShort:
  1214. self->spec.format = PA_SAMPLE_S16NE;
  1215. break;
  1216. case DevFmtInt:
  1217. self->spec.format = PA_SAMPLE_S32NE;
  1218. break;
  1219. case DevFmtFloat:
  1220. self->spec.format = PA_SAMPLE_FLOAT32NE;
  1221. break;
  1222. case DevFmtByte:
  1223. case DevFmtUShort:
  1224. case DevFmtUInt:
  1225. ERR("%s capture samples not supported\n", DevFmtTypeString(device->FmtType));
  1226. pa_threaded_mainloop_unlock(self->loop);
  1227. goto fail;
  1228. }
  1229. if(pa_sample_spec_valid(&self->spec) == 0)
  1230. {
  1231. ERR("Invalid sample format\n");
  1232. pa_threaded_mainloop_unlock(self->loop);
  1233. goto fail;
  1234. }
  1235. if(!pa_channel_map_init_auto(&chanmap, self->spec.channels, PA_CHANNEL_MAP_WAVEEX))
  1236. {
  1237. ERR("Couldn't build map for channel count (%d)!\n", self->spec.channels);
  1238. pa_threaded_mainloop_unlock(self->loop);
  1239. goto fail;
  1240. }
  1241. samples = device->UpdateSize * device->NumUpdates;
  1242. samples = maxu(samples, 100 * device->Frequency / 1000);
  1243. self->attr.minreq = -1;
  1244. self->attr.prebuf = -1;
  1245. self->attr.maxlength = samples * pa_frame_size(&self->spec);
  1246. self->attr.tlength = -1;
  1247. self->attr.fragsize = minu(samples, 50*device->Frequency/1000) *
  1248. pa_frame_size(&self->spec);
  1249. flags |= PA_STREAM_START_CORKED|PA_STREAM_ADJUST_LATENCY;
  1250. if(!GetConfigValueBool(NULL, "pulse", "allow-moves", 0))
  1251. flags |= PA_STREAM_DONT_MOVE;
  1252. TRACE("Connecting to \"%s\"\n", pulse_name ? pulse_name : "(default)");
  1253. self->stream = ALCpulseCapture_connectStream(pulse_name, self->loop, self->context,
  1254. flags, &self->attr, &self->spec,
  1255. &chanmap);
  1256. if(!self->stream)
  1257. {
  1258. pa_threaded_mainloop_unlock(self->loop);
  1259. goto fail;
  1260. }
  1261. pa_stream_set_moved_callback(self->stream, ALCpulseCapture_streamMovedCallback, self);
  1262. pa_stream_set_state_callback(self->stream, ALCpulseCapture_streamStateCallback, self);
  1263. al_string_copy_cstr(&self->device_name, pa_stream_get_device_name(self->stream));
  1264. if(al_string_empty(device->DeviceName))
  1265. {
  1266. pa_operation *o = pa_context_get_source_info_by_name(
  1267. self->context, al_string_get_cstr(self->device_name),
  1268. ALCpulseCapture_sourceNameCallback, self
  1269. );
  1270. wait_for_operation(o, self->loop);
  1271. }
  1272. pa_threaded_mainloop_unlock(self->loop);
  1273. return ALC_NO_ERROR;
  1274. fail:
  1275. pulse_close(self->loop, self->context, self->stream);
  1276. self->loop = NULL;
  1277. self->context = NULL;
  1278. self->stream = NULL;
  1279. return ALC_INVALID_VALUE;
  1280. }
  1281. static void ALCpulseCapture_close(ALCpulseCapture *self)
  1282. {
  1283. pulse_close(self->loop, self->context, self->stream);
  1284. self->loop = NULL;
  1285. self->context = NULL;
  1286. self->stream = NULL;
  1287. al_string_clear(&self->device_name);
  1288. }
  1289. static ALCboolean ALCpulseCapture_start(ALCpulseCapture *self)
  1290. {
  1291. pa_operation *o;
  1292. o = pa_stream_cork(self->stream, 0, stream_success_callback, self->loop);
  1293. wait_for_operation(o, self->loop);
  1294. return ALC_TRUE;
  1295. }
  1296. static void ALCpulseCapture_stop(ALCpulseCapture *self)
  1297. {
  1298. pa_operation *o;
  1299. o = pa_stream_cork(self->stream, 1, stream_success_callback, self->loop);
  1300. wait_for_operation(o, self->loop);
  1301. }
  1302. static ALCenum ALCpulseCapture_captureSamples(ALCpulseCapture *self, ALCvoid *buffer, ALCuint samples)
  1303. {
  1304. ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
  1305. ALCuint todo = samples * pa_frame_size(&self->spec);
  1306. /* Capture is done in fragment-sized chunks, so we loop until we get all
  1307. * that's available */
  1308. self->last_readable -= todo;
  1309. while(todo > 0)
  1310. {
  1311. size_t rem = todo;
  1312. if(self->cap_len == 0)
  1313. {
  1314. pa_stream_state_t state;
  1315. state = pa_stream_get_state(self->stream);
  1316. if(!PA_STREAM_IS_GOOD(state))
  1317. {
  1318. aluHandleDisconnect(device);
  1319. break;
  1320. }
  1321. if(pa_stream_peek(self->stream, &self->cap_store, &self->cap_len) < 0)
  1322. {
  1323. ERR("pa_stream_peek() failed: %s\n",
  1324. pa_strerror(pa_context_errno(self->context)));
  1325. aluHandleDisconnect(device);
  1326. break;
  1327. }
  1328. self->cap_remain = self->cap_len;
  1329. }
  1330. if(rem > self->cap_remain)
  1331. rem = self->cap_remain;
  1332. memcpy(buffer, self->cap_store, rem);
  1333. buffer = (ALbyte*)buffer + rem;
  1334. todo -= rem;
  1335. self->cap_store = (ALbyte*)self->cap_store + rem;
  1336. self->cap_remain -= rem;
  1337. if(self->cap_remain == 0)
  1338. {
  1339. pa_stream_drop(self->stream);
  1340. self->cap_len = 0;
  1341. }
  1342. }
  1343. if(todo > 0)
  1344. memset(buffer, ((device->FmtType==DevFmtUByte) ? 0x80 : 0), todo);
  1345. return ALC_NO_ERROR;
  1346. }
  1347. static ALCuint ALCpulseCapture_availableSamples(ALCpulseCapture *self)
  1348. {
  1349. ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
  1350. size_t readable = self->cap_remain;
  1351. if(device->Connected)
  1352. {
  1353. ssize_t got = pa_stream_readable_size(self->stream);
  1354. if(got < 0)
  1355. {
  1356. ERR("pa_stream_readable_size() failed: %s\n", pa_strerror(got));
  1357. aluHandleDisconnect(device);
  1358. }
  1359. else if((size_t)got > self->cap_len)
  1360. readable += got - self->cap_len;
  1361. }
  1362. if(self->last_readable < readable)
  1363. self->last_readable = readable;
  1364. return self->last_readable / pa_frame_size(&self->spec);
  1365. }
  1366. static ALint64 ALCpulseCapture_getLatency(ALCpulseCapture *self)
  1367. {
  1368. pa_usec_t latency = 0;
  1369. int neg;
  1370. if(pa_stream_get_latency(self->stream, &latency, &neg) != 0)
  1371. {
  1372. ERR("Failed to get stream latency!\n");
  1373. return 0;
  1374. }
  1375. if(neg) latency = 0;
  1376. return (ALint64)minu64(latency, U64(0x7fffffffffffffff)/1000) * 1000;
  1377. }
  1378. static void ALCpulseCapture_lock(ALCpulseCapture *self)
  1379. {
  1380. pa_threaded_mainloop_lock(self->loop);
  1381. }
  1382. static void ALCpulseCapture_unlock(ALCpulseCapture *self)
  1383. {
  1384. pa_threaded_mainloop_unlock(self->loop);
  1385. }
  1386. typedef struct ALCpulseBackendFactory {
  1387. DERIVE_FROM_TYPE(ALCbackendFactory);
  1388. } ALCpulseBackendFactory;
  1389. #define ALCPULSEBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCpulseBackendFactory, ALCbackendFactory) } }
  1390. static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory *self);
  1391. static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory *self);
  1392. static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory *self, ALCbackend_Type type);
  1393. static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory *self, enum DevProbe type);
  1394. static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
  1395. DEFINE_ALCBACKENDFACTORY_VTABLE(ALCpulseBackendFactory);
  1396. static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory* UNUSED(self))
  1397. {
  1398. ALCboolean ret = ALC_FALSE;
  1399. VECTOR_INIT(PlaybackDevices);
  1400. VECTOR_INIT(CaptureDevices);
  1401. if(pulse_load())
  1402. {
  1403. pa_threaded_mainloop *loop;
  1404. pulse_ctx_flags = 0;
  1405. if(!GetConfigValueBool(NULL, "pulse", "spawn-server", 1))
  1406. pulse_ctx_flags |= PA_CONTEXT_NOAUTOSPAWN;
  1407. if((loop=pa_threaded_mainloop_new()) &&
  1408. pa_threaded_mainloop_start(loop) >= 0)
  1409. {
  1410. pa_context *context;
  1411. pa_threaded_mainloop_lock(loop);
  1412. context = connect_context(loop, AL_TRUE);
  1413. if(context)
  1414. {
  1415. ret = ALC_TRUE;
  1416. /* Some libraries (Phonon, Qt) set some pulseaudio properties
  1417. * through environment variables, which causes all streams in
  1418. * the process to inherit them. This attempts to filter those
  1419. * properties out by setting them to 0-length data. */
  1420. prop_filter = pa_proplist_new();
  1421. pa_proplist_set(prop_filter, PA_PROP_MEDIA_ROLE, NULL, 0);
  1422. pa_proplist_set(prop_filter, "phonon.streamid", NULL, 0);
  1423. pa_context_disconnect(context);
  1424. pa_context_unref(context);
  1425. }
  1426. pa_threaded_mainloop_unlock(loop);
  1427. pa_threaded_mainloop_stop(loop);
  1428. }
  1429. if(loop)
  1430. pa_threaded_mainloop_free(loop);
  1431. }
  1432. return ret;
  1433. }
  1434. static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory* UNUSED(self))
  1435. {
  1436. clear_devlist(&PlaybackDevices);
  1437. VECTOR_DEINIT(PlaybackDevices);
  1438. clear_devlist(&CaptureDevices);
  1439. VECTOR_DEINIT(CaptureDevices);
  1440. if(prop_filter)
  1441. pa_proplist_free(prop_filter);
  1442. prop_filter = NULL;
  1443. /* PulseAudio doesn't like being CloseLib'd sometimes */
  1444. }
  1445. static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory* UNUSED(self), ALCbackend_Type type)
  1446. {
  1447. if(type == ALCbackend_Playback || type == ALCbackend_Capture)
  1448. return ALC_TRUE;
  1449. return ALC_FALSE;
  1450. }
  1451. static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory* UNUSED(self), enum DevProbe type)
  1452. {
  1453. switch(type)
  1454. {
  1455. case ALL_DEVICE_PROBE:
  1456. ALCpulsePlayback_probeDevices();
  1457. #define APPEND_ALL_DEVICES_LIST(e) AppendAllDevicesList(al_string_get_cstr((e)->name))
  1458. VECTOR_FOR_EACH(const DevMap, PlaybackDevices, APPEND_ALL_DEVICES_LIST);
  1459. #undef APPEND_ALL_DEVICES_LIST
  1460. break;
  1461. case CAPTURE_DEVICE_PROBE:
  1462. ALCpulseCapture_probeDevices();
  1463. #define APPEND_CAPTURE_DEVICE_LIST(e) AppendCaptureDeviceList(al_string_get_cstr((e)->name))
  1464. VECTOR_FOR_EACH(const DevMap, CaptureDevices, APPEND_CAPTURE_DEVICE_LIST);
  1465. #undef APPEND_CAPTURE_DEVICE_LIST
  1466. break;
  1467. }
  1468. }
  1469. static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
  1470. {
  1471. if(type == ALCbackend_Playback)
  1472. {
  1473. ALCpulsePlayback *backend;
  1474. NEW_OBJ(backend, ALCpulsePlayback)(device);
  1475. if(!backend) return NULL;
  1476. return STATIC_CAST(ALCbackend, backend);
  1477. }
  1478. if(type == ALCbackend_Capture)
  1479. {
  1480. ALCpulseCapture *backend;
  1481. NEW_OBJ(backend, ALCpulseCapture)(device);
  1482. if(!backend) return NULL;
  1483. return STATIC_CAST(ALCbackend, backend);
  1484. }
  1485. return NULL;
  1486. }
  1487. #else /* PA_API_VERSION == 12 */
  1488. #warning "Unsupported API version, backend will be unavailable!"
  1489. typedef struct ALCpulseBackendFactory {
  1490. DERIVE_FROM_TYPE(ALCbackendFactory);
  1491. } ALCpulseBackendFactory;
  1492. #define ALCPULSEBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCpulseBackendFactory, ALCbackendFactory) } }
  1493. static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory* UNUSED(self))
  1494. {
  1495. return ALC_FALSE;
  1496. }
  1497. static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory* UNUSED(self))
  1498. {
  1499. }
  1500. static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory* UNUSED(self), ALCbackend_Type UNUSED(type))
  1501. {
  1502. return ALC_FALSE;
  1503. }
  1504. static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory* UNUSED(self), enum DevProbe UNUSED(type))
  1505. {
  1506. }
  1507. static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory* UNUSED(self), ALCdevice* UNUSED(device), ALCbackend_Type UNUSED(type))
  1508. {
  1509. return NULL;
  1510. }
  1511. DEFINE_ALCBACKENDFACTORY_VTABLE(ALCpulseBackendFactory);
  1512. #endif /* PA_API_VERSION == 12 */
  1513. ALCbackendFactory *ALCpulseBackendFactory_getFactory(void)
  1514. {
  1515. static ALCpulseBackendFactory factory = ALCPULSEBACKENDFACTORY_INITIALIZER;
  1516. return STATIC_CAST(ALCbackendFactory, &factory);
  1517. }