pulseaudio.c 61 KB

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