pulseaudio.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919
  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);
  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)
  1556. {
  1557. switch(type)
  1558. {
  1559. case ALL_DEVICE_PROBE:
  1560. ALCpulsePlayback_probeDevices();
  1561. #define APPEND_ALL_DEVICES_LIST(e) AppendAllDevicesList(alstr_get_cstr((e)->name))
  1562. VECTOR_FOR_EACH(const DevMap, PlaybackDevices, APPEND_ALL_DEVICES_LIST);
  1563. #undef APPEND_ALL_DEVICES_LIST
  1564. break;
  1565. case CAPTURE_DEVICE_PROBE:
  1566. ALCpulseCapture_probeDevices();
  1567. #define APPEND_CAPTURE_DEVICE_LIST(e) AppendCaptureDeviceList(alstr_get_cstr((e)->name))
  1568. VECTOR_FOR_EACH(const DevMap, CaptureDevices, APPEND_CAPTURE_DEVICE_LIST);
  1569. #undef APPEND_CAPTURE_DEVICE_LIST
  1570. break;
  1571. }
  1572. }
  1573. static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
  1574. {
  1575. if(type == ALCbackend_Playback)
  1576. {
  1577. ALCpulsePlayback *backend;
  1578. NEW_OBJ(backend, ALCpulsePlayback)(device);
  1579. if(!backend) return NULL;
  1580. return STATIC_CAST(ALCbackend, backend);
  1581. }
  1582. if(type == ALCbackend_Capture)
  1583. {
  1584. ALCpulseCapture *backend;
  1585. NEW_OBJ(backend, ALCpulseCapture)(device);
  1586. if(!backend) return NULL;
  1587. return STATIC_CAST(ALCbackend, backend);
  1588. }
  1589. return NULL;
  1590. }
  1591. #else /* PA_API_VERSION == 12 */
  1592. #warning "Unsupported API version, backend will be unavailable!"
  1593. typedef struct ALCpulseBackendFactory {
  1594. DERIVE_FROM_TYPE(ALCbackendFactory);
  1595. } ALCpulseBackendFactory;
  1596. #define ALCPULSEBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCpulseBackendFactory, ALCbackendFactory) } }
  1597. static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory* UNUSED(self))
  1598. {
  1599. return ALC_FALSE;
  1600. }
  1601. static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory* UNUSED(self))
  1602. {
  1603. }
  1604. static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory* UNUSED(self), ALCbackend_Type UNUSED(type))
  1605. {
  1606. return ALC_FALSE;
  1607. }
  1608. static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory* UNUSED(self), enum DevProbe UNUSED(type))
  1609. {
  1610. }
  1611. static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory* UNUSED(self), ALCdevice* UNUSED(device), ALCbackend_Type UNUSED(type))
  1612. {
  1613. return NULL;
  1614. }
  1615. DEFINE_ALCBACKENDFACTORY_VTABLE(ALCpulseBackendFactory);
  1616. #endif /* PA_API_VERSION == 12 */
  1617. ALCbackendFactory *ALCpulseBackendFactory_getFactory(void)
  1618. {
  1619. static ALCpulseBackendFactory factory = ALCPULSEBACKENDFACTORY_INITIALIZER;
  1620. return STATIC_CAST(ALCbackendFactory, &factory);
  1621. }