pulseaudio.c 64 KB

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