| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920 |
- /**
- * OpenAL cross platform audio library
- * Copyright (C) 2009 by Konstantinos Natsakis <[email protected]>
- * Copyright (C) 2010 by Chris Robinson <[email protected]>
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- * Or go to http://www.gnu.org/copyleft/lgpl.html
- */
- #include "config.h"
- #include <string.h>
- #include "alMain.h"
- #include "alu.h"
- #include "alconfig.h"
- #include "threads.h"
- #include "compat.h"
- #include "backends/base.h"
- #include <pulse/pulseaudio.h>
- #if PA_API_VERSION == 12
- #ifdef HAVE_DYNLOAD
- static void *pa_handle;
- #define MAKE_FUNC(x) static __typeof(x) * p##x
- MAKE_FUNC(pa_context_unref);
- MAKE_FUNC(pa_sample_spec_valid);
- MAKE_FUNC(pa_frame_size);
- MAKE_FUNC(pa_stream_drop);
- MAKE_FUNC(pa_strerror);
- MAKE_FUNC(pa_context_get_state);
- MAKE_FUNC(pa_stream_get_state);
- MAKE_FUNC(pa_threaded_mainloop_signal);
- MAKE_FUNC(pa_stream_peek);
- MAKE_FUNC(pa_threaded_mainloop_wait);
- MAKE_FUNC(pa_threaded_mainloop_unlock);
- MAKE_FUNC(pa_threaded_mainloop_in_thread);
- MAKE_FUNC(pa_context_new);
- MAKE_FUNC(pa_threaded_mainloop_stop);
- MAKE_FUNC(pa_context_disconnect);
- MAKE_FUNC(pa_threaded_mainloop_start);
- MAKE_FUNC(pa_threaded_mainloop_get_api);
- MAKE_FUNC(pa_context_set_state_callback);
- MAKE_FUNC(pa_stream_write);
- MAKE_FUNC(pa_xfree);
- MAKE_FUNC(pa_stream_connect_record);
- MAKE_FUNC(pa_stream_connect_playback);
- MAKE_FUNC(pa_stream_readable_size);
- MAKE_FUNC(pa_stream_writable_size);
- MAKE_FUNC(pa_stream_is_corked);
- MAKE_FUNC(pa_stream_cork);
- MAKE_FUNC(pa_stream_is_suspended);
- MAKE_FUNC(pa_stream_get_device_name);
- MAKE_FUNC(pa_stream_get_latency);
- MAKE_FUNC(pa_path_get_filename);
- MAKE_FUNC(pa_get_binary_name);
- MAKE_FUNC(pa_threaded_mainloop_free);
- MAKE_FUNC(pa_context_errno);
- MAKE_FUNC(pa_xmalloc);
- MAKE_FUNC(pa_stream_unref);
- MAKE_FUNC(pa_threaded_mainloop_accept);
- MAKE_FUNC(pa_stream_set_write_callback);
- MAKE_FUNC(pa_threaded_mainloop_new);
- MAKE_FUNC(pa_context_connect);
- MAKE_FUNC(pa_stream_set_buffer_attr);
- MAKE_FUNC(pa_stream_get_buffer_attr);
- MAKE_FUNC(pa_stream_get_sample_spec);
- MAKE_FUNC(pa_stream_get_time);
- MAKE_FUNC(pa_stream_set_read_callback);
- MAKE_FUNC(pa_stream_set_state_callback);
- MAKE_FUNC(pa_stream_set_moved_callback);
- MAKE_FUNC(pa_stream_set_underflow_callback);
- MAKE_FUNC(pa_stream_new_with_proplist);
- MAKE_FUNC(pa_stream_disconnect);
- MAKE_FUNC(pa_threaded_mainloop_lock);
- MAKE_FUNC(pa_channel_map_init_auto);
- MAKE_FUNC(pa_channel_map_parse);
- MAKE_FUNC(pa_channel_map_snprint);
- MAKE_FUNC(pa_channel_map_equal);
- MAKE_FUNC(pa_context_get_server_info);
- MAKE_FUNC(pa_context_get_sink_info_by_name);
- MAKE_FUNC(pa_context_get_sink_info_list);
- MAKE_FUNC(pa_context_get_source_info_by_name);
- MAKE_FUNC(pa_context_get_source_info_list);
- MAKE_FUNC(pa_operation_get_state);
- MAKE_FUNC(pa_operation_unref);
- MAKE_FUNC(pa_proplist_new);
- MAKE_FUNC(pa_proplist_free);
- MAKE_FUNC(pa_proplist_set);
- MAKE_FUNC(pa_channel_map_superset);
- MAKE_FUNC(pa_stream_set_buffer_attr_callback);
- MAKE_FUNC(pa_stream_begin_write);
- #undef MAKE_FUNC
- #define pa_context_unref ppa_context_unref
- #define pa_sample_spec_valid ppa_sample_spec_valid
- #define pa_frame_size ppa_frame_size
- #define pa_stream_drop ppa_stream_drop
- #define pa_strerror ppa_strerror
- #define pa_context_get_state ppa_context_get_state
- #define pa_stream_get_state ppa_stream_get_state
- #define pa_threaded_mainloop_signal ppa_threaded_mainloop_signal
- #define pa_stream_peek ppa_stream_peek
- #define pa_threaded_mainloop_wait ppa_threaded_mainloop_wait
- #define pa_threaded_mainloop_unlock ppa_threaded_mainloop_unlock
- #define pa_threaded_mainloop_in_thread ppa_threaded_mainloop_in_thread
- #define pa_context_new ppa_context_new
- #define pa_threaded_mainloop_stop ppa_threaded_mainloop_stop
- #define pa_context_disconnect ppa_context_disconnect
- #define pa_threaded_mainloop_start ppa_threaded_mainloop_start
- #define pa_threaded_mainloop_get_api ppa_threaded_mainloop_get_api
- #define pa_context_set_state_callback ppa_context_set_state_callback
- #define pa_stream_write ppa_stream_write
- #define pa_xfree ppa_xfree
- #define pa_stream_connect_record ppa_stream_connect_record
- #define pa_stream_connect_playback ppa_stream_connect_playback
- #define pa_stream_readable_size ppa_stream_readable_size
- #define pa_stream_writable_size ppa_stream_writable_size
- #define pa_stream_is_corked ppa_stream_is_corked
- #define pa_stream_cork ppa_stream_cork
- #define pa_stream_is_suspended ppa_stream_is_suspended
- #define pa_stream_get_device_name ppa_stream_get_device_name
- #define pa_stream_get_latency ppa_stream_get_latency
- #define pa_path_get_filename ppa_path_get_filename
- #define pa_get_binary_name ppa_get_binary_name
- #define pa_threaded_mainloop_free ppa_threaded_mainloop_free
- #define pa_context_errno ppa_context_errno
- #define pa_xmalloc ppa_xmalloc
- #define pa_stream_unref ppa_stream_unref
- #define pa_threaded_mainloop_accept ppa_threaded_mainloop_accept
- #define pa_stream_set_write_callback ppa_stream_set_write_callback
- #define pa_threaded_mainloop_new ppa_threaded_mainloop_new
- #define pa_context_connect ppa_context_connect
- #define pa_stream_set_buffer_attr ppa_stream_set_buffer_attr
- #define pa_stream_get_buffer_attr ppa_stream_get_buffer_attr
- #define pa_stream_get_sample_spec ppa_stream_get_sample_spec
- #define pa_stream_get_time ppa_stream_get_time
- #define pa_stream_set_read_callback ppa_stream_set_read_callback
- #define pa_stream_set_state_callback ppa_stream_set_state_callback
- #define pa_stream_set_moved_callback ppa_stream_set_moved_callback
- #define pa_stream_set_underflow_callback ppa_stream_set_underflow_callback
- #define pa_stream_new_with_proplist ppa_stream_new_with_proplist
- #define pa_stream_disconnect ppa_stream_disconnect
- #define pa_threaded_mainloop_lock ppa_threaded_mainloop_lock
- #define pa_channel_map_init_auto ppa_channel_map_init_auto
- #define pa_channel_map_parse ppa_channel_map_parse
- #define pa_channel_map_snprint ppa_channel_map_snprint
- #define pa_channel_map_equal ppa_channel_map_equal
- #define pa_context_get_server_info ppa_context_get_server_info
- #define pa_context_get_sink_info_by_name ppa_context_get_sink_info_by_name
- #define pa_context_get_sink_info_list ppa_context_get_sink_info_list
- #define pa_context_get_source_info_by_name ppa_context_get_source_info_by_name
- #define pa_context_get_source_info_list ppa_context_get_source_info_list
- #define pa_operation_get_state ppa_operation_get_state
- #define pa_operation_unref ppa_operation_unref
- #define pa_proplist_new ppa_proplist_new
- #define pa_proplist_free ppa_proplist_free
- #define pa_proplist_set ppa_proplist_set
- #define pa_channel_map_superset ppa_channel_map_superset
- #define pa_stream_set_buffer_attr_callback ppa_stream_set_buffer_attr_callback
- #define pa_stream_begin_write ppa_stream_begin_write
- #endif
- static ALCboolean pulse_load(void)
- {
- ALCboolean ret = ALC_TRUE;
- #ifdef HAVE_DYNLOAD
- if(!pa_handle)
- {
- al_string missing_funcs = AL_STRING_INIT_STATIC();
- #ifdef _WIN32
- #define PALIB "libpulse-0.dll"
- #elif defined(__APPLE__) && defined(__MACH__)
- #define PALIB "libpulse.0.dylib"
- #else
- #define PALIB "libpulse.so.0"
- #endif
- pa_handle = LoadLib(PALIB);
- if(!pa_handle)
- {
- WARN("Failed to load %s\n", PALIB);
- return ALC_FALSE;
- }
- #define LOAD_FUNC(x) do { \
- p##x = GetSymbol(pa_handle, #x); \
- if(!(p##x)) { \
- ret = ALC_FALSE; \
- alstr_append_cstr(&missing_funcs, "\n" #x); \
- } \
- } while(0)
- LOAD_FUNC(pa_context_unref);
- LOAD_FUNC(pa_sample_spec_valid);
- LOAD_FUNC(pa_stream_drop);
- LOAD_FUNC(pa_frame_size);
- LOAD_FUNC(pa_strerror);
- LOAD_FUNC(pa_context_get_state);
- LOAD_FUNC(pa_stream_get_state);
- LOAD_FUNC(pa_threaded_mainloop_signal);
- LOAD_FUNC(pa_stream_peek);
- LOAD_FUNC(pa_threaded_mainloop_wait);
- LOAD_FUNC(pa_threaded_mainloop_unlock);
- LOAD_FUNC(pa_threaded_mainloop_in_thread);
- LOAD_FUNC(pa_context_new);
- LOAD_FUNC(pa_threaded_mainloop_stop);
- LOAD_FUNC(pa_context_disconnect);
- LOAD_FUNC(pa_threaded_mainloop_start);
- LOAD_FUNC(pa_threaded_mainloop_get_api);
- LOAD_FUNC(pa_context_set_state_callback);
- LOAD_FUNC(pa_stream_write);
- LOAD_FUNC(pa_xfree);
- LOAD_FUNC(pa_stream_connect_record);
- LOAD_FUNC(pa_stream_connect_playback);
- LOAD_FUNC(pa_stream_readable_size);
- LOAD_FUNC(pa_stream_writable_size);
- LOAD_FUNC(pa_stream_is_corked);
- LOAD_FUNC(pa_stream_cork);
- LOAD_FUNC(pa_stream_is_suspended);
- LOAD_FUNC(pa_stream_get_device_name);
- LOAD_FUNC(pa_stream_get_latency);
- LOAD_FUNC(pa_path_get_filename);
- LOAD_FUNC(pa_get_binary_name);
- LOAD_FUNC(pa_threaded_mainloop_free);
- LOAD_FUNC(pa_context_errno);
- LOAD_FUNC(pa_xmalloc);
- LOAD_FUNC(pa_stream_unref);
- LOAD_FUNC(pa_threaded_mainloop_accept);
- LOAD_FUNC(pa_stream_set_write_callback);
- LOAD_FUNC(pa_threaded_mainloop_new);
- LOAD_FUNC(pa_context_connect);
- LOAD_FUNC(pa_stream_set_buffer_attr);
- LOAD_FUNC(pa_stream_get_buffer_attr);
- LOAD_FUNC(pa_stream_get_sample_spec);
- LOAD_FUNC(pa_stream_get_time);
- LOAD_FUNC(pa_stream_set_read_callback);
- LOAD_FUNC(pa_stream_set_state_callback);
- LOAD_FUNC(pa_stream_set_moved_callback);
- LOAD_FUNC(pa_stream_set_underflow_callback);
- LOAD_FUNC(pa_stream_new_with_proplist);
- LOAD_FUNC(pa_stream_disconnect);
- LOAD_FUNC(pa_threaded_mainloop_lock);
- LOAD_FUNC(pa_channel_map_init_auto);
- LOAD_FUNC(pa_channel_map_parse);
- LOAD_FUNC(pa_channel_map_snprint);
- LOAD_FUNC(pa_channel_map_equal);
- LOAD_FUNC(pa_context_get_server_info);
- LOAD_FUNC(pa_context_get_sink_info_by_name);
- LOAD_FUNC(pa_context_get_sink_info_list);
- LOAD_FUNC(pa_context_get_source_info_by_name);
- LOAD_FUNC(pa_context_get_source_info_list);
- LOAD_FUNC(pa_operation_get_state);
- LOAD_FUNC(pa_operation_unref);
- LOAD_FUNC(pa_proplist_new);
- LOAD_FUNC(pa_proplist_free);
- LOAD_FUNC(pa_proplist_set);
- LOAD_FUNC(pa_channel_map_superset);
- LOAD_FUNC(pa_stream_set_buffer_attr_callback);
- LOAD_FUNC(pa_stream_begin_write);
- #undef LOAD_FUNC
- if(ret == ALC_FALSE)
- {
- WARN("Missing expected functions:%s\n", alstr_get_cstr(missing_funcs));
- CloseLib(pa_handle);
- pa_handle = NULL;
- }
- alstr_reset(&missing_funcs);
- }
- #endif /* HAVE_DYNLOAD */
- return ret;
- }
- /* Global flags and properties */
- static pa_context_flags_t pulse_ctx_flags;
- static pa_proplist *prop_filter;
- /* PulseAudio Event Callbacks */
- static void context_state_callback(pa_context *context, void *pdata)
- {
- pa_threaded_mainloop *loop = pdata;
- pa_context_state_t state;
- state = pa_context_get_state(context);
- if(state == PA_CONTEXT_READY || !PA_CONTEXT_IS_GOOD(state))
- pa_threaded_mainloop_signal(loop, 0);
- }
- static void stream_state_callback(pa_stream *stream, void *pdata)
- {
- pa_threaded_mainloop *loop = pdata;
- pa_stream_state_t state;
- state = pa_stream_get_state(stream);
- if(state == PA_STREAM_READY || !PA_STREAM_IS_GOOD(state))
- pa_threaded_mainloop_signal(loop, 0);
- }
- static void stream_success_callback(pa_stream *UNUSED(stream), int UNUSED(success), void *pdata)
- {
- pa_threaded_mainloop *loop = pdata;
- pa_threaded_mainloop_signal(loop, 0);
- }
- static void wait_for_operation(pa_operation *op, pa_threaded_mainloop *loop)
- {
- if(op)
- {
- while(pa_operation_get_state(op) == PA_OPERATION_RUNNING)
- pa_threaded_mainloop_wait(loop);
- pa_operation_unref(op);
- }
- }
- static pa_context *connect_context(pa_threaded_mainloop *loop, ALboolean silent)
- {
- const char *name = "OpenAL Soft";
- al_string binname = AL_STRING_INIT_STATIC();
- pa_context_state_t state;
- pa_context *context;
- int err;
- GetProcBinary(NULL, &binname);
- if(!alstr_empty(binname))
- name = alstr_get_cstr(binname);
- context = pa_context_new(pa_threaded_mainloop_get_api(loop), name);
- if(!context)
- {
- ERR("pa_context_new() failed\n");
- alstr_reset(&binname);
- return NULL;
- }
- pa_context_set_state_callback(context, context_state_callback, loop);
- if((err=pa_context_connect(context, NULL, pulse_ctx_flags, NULL)) >= 0)
- {
- while((state=pa_context_get_state(context)) != PA_CONTEXT_READY)
- {
- if(!PA_CONTEXT_IS_GOOD(state))
- {
- err = pa_context_errno(context);
- if(err > 0) err = -err;
- break;
- }
- pa_threaded_mainloop_wait(loop);
- }
- }
- pa_context_set_state_callback(context, NULL, NULL);
- if(err < 0)
- {
- if(!silent)
- ERR("Context did not connect: %s\n", pa_strerror(err));
- pa_context_unref(context);
- context = NULL;
- }
- alstr_reset(&binname);
- return context;
- }
- static ALCboolean pulse_open(pa_threaded_mainloop **loop, pa_context **context,
- void(*state_cb)(pa_context*,void*), void *ptr)
- {
- if(!(*loop = pa_threaded_mainloop_new()))
- {
- ERR("pa_threaded_mainloop_new() failed!\n");
- return ALC_FALSE;
- }
- if(pa_threaded_mainloop_start(*loop) < 0)
- {
- ERR("pa_threaded_mainloop_start() failed\n");
- goto error;
- }
- pa_threaded_mainloop_lock(*loop);
- *context = connect_context(*loop, AL_FALSE);
- if(!*context)
- {
- pa_threaded_mainloop_unlock(*loop);
- pa_threaded_mainloop_stop(*loop);
- goto error;
- }
- pa_context_set_state_callback(*context, state_cb, ptr);
- pa_threaded_mainloop_unlock(*loop);
- return ALC_TRUE;
- error:
- pa_threaded_mainloop_free(*loop);
- *loop = NULL;
- return ALC_FALSE;
- }
- static void pulse_close(pa_threaded_mainloop *loop, pa_context *context, pa_stream *stream)
- {
- pa_threaded_mainloop_lock(loop);
- if(stream)
- {
- pa_stream_set_state_callback(stream, NULL, NULL);
- pa_stream_set_moved_callback(stream, NULL, NULL);
- pa_stream_set_write_callback(stream, NULL, NULL);
- pa_stream_set_buffer_attr_callback(stream, NULL, NULL);
- pa_stream_disconnect(stream);
- pa_stream_unref(stream);
- }
- pa_context_disconnect(context);
- pa_context_unref(context);
- pa_threaded_mainloop_unlock(loop);
- pa_threaded_mainloop_stop(loop);
- pa_threaded_mainloop_free(loop);
- }
- typedef struct {
- al_string name;
- al_string device_name;
- } DevMap;
- TYPEDEF_VECTOR(DevMap, vector_DevMap)
- static vector_DevMap PlaybackDevices;
- static vector_DevMap CaptureDevices;
- static void clear_devlist(vector_DevMap *list)
- {
- #define DEINIT_STRS(i) (AL_STRING_DEINIT((i)->name),AL_STRING_DEINIT((i)->device_name))
- VECTOR_FOR_EACH(DevMap, *list, DEINIT_STRS);
- #undef DEINIT_STRS
- VECTOR_RESIZE(*list, 0, 0);
- }
- typedef struct ALCpulsePlayback {
- DERIVE_FROM_TYPE(ALCbackend);
- al_string device_name;
- pa_buffer_attr attr;
- pa_sample_spec spec;
- pa_threaded_mainloop *loop;
- pa_stream *stream;
- pa_context *context;
- ATOMIC(ALenum) killNow;
- althrd_t thread;
- } ALCpulsePlayback;
- static void ALCpulsePlayback_deviceCallback(pa_context *context, const pa_sink_info *info, int eol, void *pdata);
- static void ALCpulsePlayback_probeDevices(void);
- static void ALCpulsePlayback_bufferAttrCallback(pa_stream *stream, void *pdata);
- static void ALCpulsePlayback_contextStateCallback(pa_context *context, void *pdata);
- static void ALCpulsePlayback_streamStateCallback(pa_stream *stream, void *pdata);
- static void ALCpulsePlayback_streamWriteCallback(pa_stream *p, size_t nbytes, void *userdata);
- static void ALCpulsePlayback_sinkInfoCallback(pa_context *context, const pa_sink_info *info, int eol, void *pdata);
- static void ALCpulsePlayback_sinkNameCallback(pa_context *context, const pa_sink_info *info, int eol, void *pdata);
- static void ALCpulsePlayback_streamMovedCallback(pa_stream *stream, void *pdata);
- static pa_stream *ALCpulsePlayback_connectStream(const char *device_name, pa_threaded_mainloop *loop,
- pa_context *context, pa_stream_flags_t flags,
- pa_buffer_attr *attr, pa_sample_spec *spec,
- pa_channel_map *chanmap);
- static int ALCpulsePlayback_mixerProc(void *ptr);
- static void ALCpulsePlayback_Construct(ALCpulsePlayback *self, ALCdevice *device);
- static void ALCpulsePlayback_Destruct(ALCpulsePlayback *self);
- static ALCenum ALCpulsePlayback_open(ALCpulsePlayback *self, const ALCchar *name);
- static ALCboolean ALCpulsePlayback_reset(ALCpulsePlayback *self);
- static ALCboolean ALCpulsePlayback_start(ALCpulsePlayback *self);
- static void ALCpulsePlayback_stop(ALCpulsePlayback *self);
- static DECLARE_FORWARD2(ALCpulsePlayback, ALCbackend, ALCenum, captureSamples, ALCvoid*, ALCuint)
- static DECLARE_FORWARD(ALCpulsePlayback, ALCbackend, ALCuint, availableSamples)
- static ClockLatency ALCpulsePlayback_getClockLatency(ALCpulsePlayback *self);
- static void ALCpulsePlayback_lock(ALCpulsePlayback *self);
- static void ALCpulsePlayback_unlock(ALCpulsePlayback *self);
- DECLARE_DEFAULT_ALLOCATORS(ALCpulsePlayback)
- DEFINE_ALCBACKEND_VTABLE(ALCpulsePlayback);
- static void ALCpulsePlayback_Construct(ALCpulsePlayback *self, ALCdevice *device)
- {
- ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
- SET_VTABLE2(ALCpulsePlayback, ALCbackend, self);
- self->loop = NULL;
- AL_STRING_INIT(self->device_name);
- ATOMIC_INIT(&self->killNow, AL_TRUE);
- }
- static void ALCpulsePlayback_Destruct(ALCpulsePlayback *self)
- {
- if(self->loop)
- {
- pulse_close(self->loop, self->context, self->stream);
- self->loop = NULL;
- self->context = NULL;
- self->stream = NULL;
- }
- AL_STRING_DEINIT(self->device_name);
- ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
- }
- static void ALCpulsePlayback_deviceCallback(pa_context *UNUSED(context), const pa_sink_info *info, int eol, void *pdata)
- {
- pa_threaded_mainloop *loop = pdata;
- const DevMap *iter;
- DevMap entry;
- int count;
- if(eol)
- {
- pa_threaded_mainloop_signal(loop, 0);
- return;
- }
- #define MATCH_INFO_NAME(iter) (alstr_cmp_cstr((iter)->device_name, info->name) == 0)
- VECTOR_FIND_IF(iter, const DevMap, PlaybackDevices, MATCH_INFO_NAME);
- if(iter != VECTOR_END(PlaybackDevices)) return;
- #undef MATCH_INFO_NAME
- AL_STRING_INIT(entry.name);
- AL_STRING_INIT(entry.device_name);
- alstr_copy_cstr(&entry.device_name, info->name);
- count = 0;
- while(1)
- {
- alstr_copy_cstr(&entry.name, info->description);
- if(count != 0)
- {
- char str[64];
- snprintf(str, sizeof(str), " #%d", count+1);
- alstr_append_cstr(&entry.name, str);
- }
- #define MATCH_ENTRY(i) (alstr_cmp(entry.name, (i)->name) == 0)
- VECTOR_FIND_IF(iter, const DevMap, PlaybackDevices, MATCH_ENTRY);
- if(iter == VECTOR_END(PlaybackDevices)) break;
- #undef MATCH_ENTRY
- count++;
- }
- TRACE("Got device \"%s\", \"%s\"\n", alstr_get_cstr(entry.name), alstr_get_cstr(entry.device_name));
- VECTOR_PUSH_BACK(PlaybackDevices, entry);
- }
- static void ALCpulsePlayback_probeDevices(void)
- {
- pa_threaded_mainloop *loop;
- clear_devlist(&PlaybackDevices);
- if((loop=pa_threaded_mainloop_new()) &&
- pa_threaded_mainloop_start(loop) >= 0)
- {
- pa_context *context;
- pa_threaded_mainloop_lock(loop);
- context = connect_context(loop, AL_FALSE);
- if(context)
- {
- pa_operation *o;
- pa_stream_flags_t flags;
- pa_sample_spec spec;
- pa_stream *stream;
- flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
- PA_STREAM_FIX_CHANNELS | PA_STREAM_DONT_MOVE;
- spec.format = PA_SAMPLE_S16NE;
- spec.rate = 44100;
- spec.channels = 2;
- stream = ALCpulsePlayback_connectStream(NULL, loop, context, flags,
- NULL, &spec, NULL);
- if(stream)
- {
- o = pa_context_get_sink_info_by_name(context, pa_stream_get_device_name(stream),
- ALCpulsePlayback_deviceCallback, loop);
- wait_for_operation(o, loop);
- pa_stream_disconnect(stream);
- pa_stream_unref(stream);
- stream = NULL;
- }
- o = pa_context_get_sink_info_list(context, ALCpulsePlayback_deviceCallback, loop);
- wait_for_operation(o, loop);
- pa_context_disconnect(context);
- pa_context_unref(context);
- }
- pa_threaded_mainloop_unlock(loop);
- pa_threaded_mainloop_stop(loop);
- }
- if(loop)
- pa_threaded_mainloop_free(loop);
- }
- static void ALCpulsePlayback_bufferAttrCallback(pa_stream *stream, void *pdata)
- {
- ALCpulsePlayback *self = pdata;
- self->attr = *pa_stream_get_buffer_attr(stream);
- TRACE("minreq=%d, tlength=%d, prebuf=%d\n", self->attr.minreq, self->attr.tlength, self->attr.prebuf);
- /* FIXME: Update the device's UpdateSize (and/or NumUpdates) using the new
- * buffer attributes? Changing UpdateSize will change the ALC_REFRESH
- * property, which probably shouldn't change between device resets. But
- * leaving it alone means ALC_REFRESH will be off.
- */
- }
- static void ALCpulsePlayback_contextStateCallback(pa_context *context, void *pdata)
- {
- ALCpulsePlayback *self = pdata;
- if(pa_context_get_state(context) == PA_CONTEXT_FAILED)
- {
- ERR("Received context failure!\n");
- aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice, "Playback state failure");
- }
- pa_threaded_mainloop_signal(self->loop, 0);
- }
- static void ALCpulsePlayback_streamStateCallback(pa_stream *stream, void *pdata)
- {
- ALCpulsePlayback *self = pdata;
- if(pa_stream_get_state(stream) == PA_STREAM_FAILED)
- {
- ERR("Received stream failure!\n");
- aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice, "Playback stream failure");
- }
- pa_threaded_mainloop_signal(self->loop, 0);
- }
- static void ALCpulsePlayback_streamWriteCallback(pa_stream* UNUSED(p), size_t UNUSED(nbytes), void *pdata)
- {
- ALCpulsePlayback *self = pdata;
- pa_threaded_mainloop_signal(self->loop, 0);
- }
- static void ALCpulsePlayback_sinkInfoCallback(pa_context *UNUSED(context), const pa_sink_info *info, int eol, void *pdata)
- {
- static const struct {
- enum DevFmtChannels chans;
- pa_channel_map map;
- } chanmaps[] = {
- { DevFmtX71, { 8, {
- PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
- PA_CHANNEL_POSITION_FRONT_CENTER, PA_CHANNEL_POSITION_LFE,
- PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
- PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT
- } } },
- { DevFmtX61, { 7, {
- PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
- PA_CHANNEL_POSITION_FRONT_CENTER, PA_CHANNEL_POSITION_LFE,
- PA_CHANNEL_POSITION_REAR_CENTER,
- PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT
- } } },
- { DevFmtX51, { 6, {
- PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
- PA_CHANNEL_POSITION_FRONT_CENTER, PA_CHANNEL_POSITION_LFE,
- PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT
- } } },
- { DevFmtX51Rear, { 6, {
- PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
- PA_CHANNEL_POSITION_FRONT_CENTER, PA_CHANNEL_POSITION_LFE,
- PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT
- } } },
- { DevFmtQuad, { 4, {
- PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
- PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT
- } } },
- { DevFmtStereo, { 2, {
- PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT
- } } },
- { DevFmtMono, { 1, {PA_CHANNEL_POSITION_MONO} } }
- };
- ALCpulsePlayback *self = pdata;
- ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
- size_t i;
- if(eol)
- {
- pa_threaded_mainloop_signal(self->loop, 0);
- return;
- }
- for(i = 0;i < COUNTOF(chanmaps);i++)
- {
- if(pa_channel_map_superset(&info->channel_map, &chanmaps[i].map))
- {
- if(!(device->Flags&DEVICE_CHANNELS_REQUEST))
- device->FmtChans = chanmaps[i].chans;
- break;
- }
- }
- if(i == COUNTOF(chanmaps))
- {
- char chanmap_str[PA_CHANNEL_MAP_SNPRINT_MAX] = "";
- pa_channel_map_snprint(chanmap_str, sizeof(chanmap_str), &info->channel_map);
- WARN("Failed to find format for channel map:\n %s\n", chanmap_str);
- }
- if(info->active_port)
- TRACE("Active port: %s (%s)\n", info->active_port->name, info->active_port->description);
- device->IsHeadphones = (info->active_port &&
- strcmp(info->active_port->name, "analog-output-headphones") == 0 &&
- device->FmtChans == DevFmtStereo);
- }
- static void ALCpulsePlayback_sinkNameCallback(pa_context *UNUSED(context), const pa_sink_info *info, int eol, void *pdata)
- {
- ALCpulsePlayback *self = pdata;
- ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
- if(eol)
- {
- pa_threaded_mainloop_signal(self->loop, 0);
- return;
- }
- alstr_copy_cstr(&device->DeviceName, info->description);
- }
- static void ALCpulsePlayback_streamMovedCallback(pa_stream *stream, void *pdata)
- {
- ALCpulsePlayback *self = pdata;
- alstr_copy_cstr(&self->device_name, pa_stream_get_device_name(stream));
- TRACE("Stream moved to %s\n", alstr_get_cstr(self->device_name));
- }
- static pa_stream *ALCpulsePlayback_connectStream(const char *device_name,
- pa_threaded_mainloop *loop, pa_context *context,
- pa_stream_flags_t flags, pa_buffer_attr *attr, pa_sample_spec *spec,
- pa_channel_map *chanmap)
- {
- pa_stream_state_t state;
- pa_stream *stream;
- if(!device_name)
- {
- device_name = getenv("ALSOFT_PULSE_DEFAULT");
- if(device_name && !device_name[0])
- device_name = NULL;
- }
- stream = pa_stream_new_with_proplist(context, "Playback Stream", spec, chanmap, prop_filter);
- if(!stream)
- {
- ERR("pa_stream_new_with_proplist() failed: %s\n", pa_strerror(pa_context_errno(context)));
- return NULL;
- }
- pa_stream_set_state_callback(stream, stream_state_callback, loop);
- if(pa_stream_connect_playback(stream, device_name, attr, flags, NULL, NULL) < 0)
- {
- ERR("Stream did not connect: %s\n", pa_strerror(pa_context_errno(context)));
- pa_stream_unref(stream);
- return NULL;
- }
- while((state=pa_stream_get_state(stream)) != PA_STREAM_READY)
- {
- if(!PA_STREAM_IS_GOOD(state))
- {
- ERR("Stream did not get ready: %s\n", pa_strerror(pa_context_errno(context)));
- pa_stream_unref(stream);
- return NULL;
- }
- pa_threaded_mainloop_wait(loop);
- }
- pa_stream_set_state_callback(stream, NULL, NULL);
- return stream;
- }
- static int ALCpulsePlayback_mixerProc(void *ptr)
- {
- ALCpulsePlayback *self = ptr;
- ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
- ALuint buffer_size;
- size_t frame_size;
- ssize_t len;
- SetRTPriority();
- althrd_setname(althrd_current(), MIXER_THREAD_NAME);
- pa_threaded_mainloop_lock(self->loop);
- frame_size = pa_frame_size(&self->spec);
- while(!ATOMIC_LOAD(&self->killNow, almemory_order_acquire) &&
- ATOMIC_LOAD(&device->Connected, almemory_order_acquire))
- {
- void *buf;
- int ret;
- len = pa_stream_writable_size(self->stream);
- if(len < 0)
- {
- ERR("Failed to get writable size: %ld", (long)len);
- aluHandleDisconnect(device, "Failed to get writable size: %ld", (long)len);
- break;
- }
- /* Make sure we're going to write at least 2 'periods' (minreqs), in
- * case the server increased it since starting playback. Also round up
- * the number of writable periods if it's not an integer count.
- */
- buffer_size = maxu((self->attr.tlength + self->attr.minreq/2) / self->attr.minreq, 2) *
- self->attr.minreq;
- /* NOTE: This assumes pa_stream_writable_size returns between 0 and
- * tlength, else there will be more latency than intended.
- */
- len = mini(len - (ssize_t)self->attr.tlength, 0) + buffer_size;
- if(len < (int32_t)self->attr.minreq)
- {
- if(pa_stream_is_corked(self->stream))
- {
- pa_operation *o;
- o = pa_stream_cork(self->stream, 0, NULL, NULL);
- if(o) pa_operation_unref(o);
- }
- pa_threaded_mainloop_wait(self->loop);
- continue;
- }
- len -= len%self->attr.minreq;
- len -= len%frame_size;
- buf = pa_xmalloc(len);
- aluMixData(device, buf, len/frame_size);
- ret = pa_stream_write(self->stream, buf, len, pa_xfree, 0, PA_SEEK_RELATIVE);
- if(ret != PA_OK) ERR("Failed to write to stream: %d, %s\n", ret, pa_strerror(ret));
- }
- pa_threaded_mainloop_unlock(self->loop);
- return 0;
- }
- static ALCenum ALCpulsePlayback_open(ALCpulsePlayback *self, const ALCchar *name)
- {
- const_al_string dev_name = AL_STRING_INIT_STATIC();
- const char *pulse_name = NULL;
- pa_stream_flags_t flags;
- pa_sample_spec spec;
- if(name)
- {
- const DevMap *iter;
- if(VECTOR_SIZE(PlaybackDevices) == 0)
- ALCpulsePlayback_probeDevices();
- #define MATCH_NAME(iter) (alstr_cmp_cstr((iter)->name, name) == 0)
- VECTOR_FIND_IF(iter, const DevMap, PlaybackDevices, MATCH_NAME);
- #undef MATCH_NAME
- if(iter == VECTOR_END(PlaybackDevices))
- return ALC_INVALID_VALUE;
- pulse_name = alstr_get_cstr(iter->device_name);
- dev_name = iter->name;
- }
- if(!pulse_open(&self->loop, &self->context, ALCpulsePlayback_contextStateCallback, self))
- return ALC_INVALID_VALUE;
- pa_threaded_mainloop_lock(self->loop);
- flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
- PA_STREAM_FIX_CHANNELS;
- if(!GetConfigValueBool(NULL, "pulse", "allow-moves", 0))
- flags |= PA_STREAM_DONT_MOVE;
- spec.format = PA_SAMPLE_S16NE;
- spec.rate = 44100;
- spec.channels = 2;
- TRACE("Connecting to \"%s\"\n", pulse_name ? pulse_name : "(default)");
- self->stream = ALCpulsePlayback_connectStream(pulse_name, self->loop, self->context,
- flags, NULL, &spec, NULL);
- if(!self->stream)
- {
- pa_threaded_mainloop_unlock(self->loop);
- pulse_close(self->loop, self->context, self->stream);
- self->loop = NULL;
- self->context = NULL;
- return ALC_INVALID_VALUE;
- }
- pa_stream_set_moved_callback(self->stream, ALCpulsePlayback_streamMovedCallback, self);
- alstr_copy_cstr(&self->device_name, pa_stream_get_device_name(self->stream));
- if(alstr_empty(dev_name))
- {
- pa_operation *o = pa_context_get_sink_info_by_name(
- self->context, alstr_get_cstr(self->device_name),
- ALCpulsePlayback_sinkNameCallback, self
- );
- wait_for_operation(o, self->loop);
- }
- else
- {
- ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
- alstr_copy(&device->DeviceName, dev_name);
- }
- pa_threaded_mainloop_unlock(self->loop);
- return ALC_NO_ERROR;
- }
- static ALCboolean ALCpulsePlayback_reset(ALCpulsePlayback *self)
- {
- ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
- pa_stream_flags_t flags = 0;
- const char *mapname = NULL;
- pa_channel_map chanmap;
- pa_operation *o;
- pa_threaded_mainloop_lock(self->loop);
- if(self->stream)
- {
- pa_stream_set_state_callback(self->stream, NULL, NULL);
- pa_stream_set_moved_callback(self->stream, NULL, NULL);
- pa_stream_set_write_callback(self->stream, NULL, NULL);
- pa_stream_set_buffer_attr_callback(self->stream, NULL, NULL);
- pa_stream_disconnect(self->stream);
- pa_stream_unref(self->stream);
- self->stream = NULL;
- }
- o = pa_context_get_sink_info_by_name(self->context, alstr_get_cstr(self->device_name),
- ALCpulsePlayback_sinkInfoCallback, self);
- wait_for_operation(o, self->loop);
- if(GetConfigValueBool(alstr_get_cstr(device->DeviceName), "pulse", "fix-rate", 0) ||
- !(device->Flags&DEVICE_FREQUENCY_REQUEST))
- flags |= PA_STREAM_FIX_RATE;
- flags |= PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
- flags |= PA_STREAM_ADJUST_LATENCY;
- flags |= PA_STREAM_START_CORKED;
- if(!GetConfigValueBool(NULL, "pulse", "allow-moves", 0))
- flags |= PA_STREAM_DONT_MOVE;
- switch(device->FmtType)
- {
- case DevFmtByte:
- device->FmtType = DevFmtUByte;
- /* fall-through */
- case DevFmtUByte:
- self->spec.format = PA_SAMPLE_U8;
- break;
- case DevFmtUShort:
- device->FmtType = DevFmtShort;
- /* fall-through */
- case DevFmtShort:
- self->spec.format = PA_SAMPLE_S16NE;
- break;
- case DevFmtUInt:
- device->FmtType = DevFmtInt;
- /* fall-through */
- case DevFmtInt:
- self->spec.format = PA_SAMPLE_S32NE;
- break;
- case DevFmtFloat:
- self->spec.format = PA_SAMPLE_FLOAT32NE;
- break;
- }
- self->spec.rate = device->Frequency;
- self->spec.channels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
- if(pa_sample_spec_valid(&self->spec) == 0)
- {
- ERR("Invalid sample format\n");
- pa_threaded_mainloop_unlock(self->loop);
- return ALC_FALSE;
- }
- switch(device->FmtChans)
- {
- case DevFmtMono:
- mapname = "mono";
- break;
- case DevFmtAmbi3D:
- device->FmtChans = DevFmtStereo;
- /*fall-through*/
- case DevFmtStereo:
- mapname = "front-left,front-right";
- break;
- case DevFmtQuad:
- mapname = "front-left,front-right,rear-left,rear-right";
- break;
- case DevFmtX51:
- mapname = "front-left,front-right,front-center,lfe,side-left,side-right";
- break;
- case DevFmtX51Rear:
- mapname = "front-left,front-right,front-center,lfe,rear-left,rear-right";
- break;
- case DevFmtX61:
- mapname = "front-left,front-right,front-center,lfe,rear-center,side-left,side-right";
- break;
- case DevFmtX71:
- mapname = "front-left,front-right,front-center,lfe,rear-left,rear-right,side-left,side-right";
- break;
- }
- if(!pa_channel_map_parse(&chanmap, mapname))
- {
- ERR("Failed to build channel map for %s\n", DevFmtChannelsString(device->FmtChans));
- pa_threaded_mainloop_unlock(self->loop);
- return ALC_FALSE;
- }
- SetDefaultWFXChannelOrder(device);
- self->attr.fragsize = -1;
- self->attr.prebuf = 0;
- self->attr.minreq = device->UpdateSize * pa_frame_size(&self->spec);
- self->attr.tlength = self->attr.minreq * maxu(device->NumUpdates, 2);
- self->attr.maxlength = -1;
- self->stream = ALCpulsePlayback_connectStream(alstr_get_cstr(self->device_name),
- self->loop, self->context, flags, &self->attr, &self->spec, &chanmap
- );
- if(!self->stream)
- {
- pa_threaded_mainloop_unlock(self->loop);
- return ALC_FALSE;
- }
- pa_stream_set_state_callback(self->stream, ALCpulsePlayback_streamStateCallback, self);
- pa_stream_set_moved_callback(self->stream, ALCpulsePlayback_streamMovedCallback, self);
- pa_stream_set_write_callback(self->stream, ALCpulsePlayback_streamWriteCallback, self);
- self->spec = *(pa_stream_get_sample_spec(self->stream));
- if(device->Frequency != self->spec.rate)
- {
- /* Server updated our playback rate, so modify the buffer attribs
- * accordingly. */
- device->NumUpdates = (ALuint)clampd(
- (ALdouble)device->NumUpdates/device->Frequency*self->spec.rate + 0.5, 2.0, 16.0
- );
- self->attr.minreq = device->UpdateSize * pa_frame_size(&self->spec);
- self->attr.tlength = self->attr.minreq * device->NumUpdates;
- self->attr.maxlength = -1;
- self->attr.prebuf = 0;
- o = pa_stream_set_buffer_attr(self->stream, &self->attr,
- stream_success_callback, self->loop);
- wait_for_operation(o, self->loop);
- device->Frequency = self->spec.rate;
- }
- pa_stream_set_buffer_attr_callback(self->stream, ALCpulsePlayback_bufferAttrCallback, self);
- ALCpulsePlayback_bufferAttrCallback(self->stream, self);
- device->NumUpdates = (ALuint)clampu64(
- (self->attr.tlength + self->attr.minreq/2) / self->attr.minreq, 2, 16
- );
- device->UpdateSize = self->attr.minreq / pa_frame_size(&self->spec);
- /* HACK: prebuf should be 0 as that's what we set it to. However on some
- * systems it comes back as non-0, so we have to make sure the device will
- * write enough audio to start playback. The lack of manual start control
- * may have unintended consequences, but it's better than not starting at
- * all.
- */
- if(self->attr.prebuf != 0)
- {
- ALuint len = self->attr.prebuf / pa_frame_size(&self->spec);
- if(len <= device->UpdateSize*device->NumUpdates)
- ERR("Non-0 prebuf, %u samples (%u bytes), device has %u samples\n",
- len, self->attr.prebuf, device->UpdateSize*device->NumUpdates);
- else
- {
- ERR("Large prebuf, %u samples (%u bytes), increasing device from %u samples",
- len, self->attr.prebuf, device->UpdateSize*device->NumUpdates);
- device->NumUpdates = (len+device->UpdateSize-1) / device->UpdateSize;
- }
- }
- pa_threaded_mainloop_unlock(self->loop);
- return ALC_TRUE;
- }
- static ALCboolean ALCpulsePlayback_start(ALCpulsePlayback *self)
- {
- ATOMIC_STORE(&self->killNow, AL_FALSE, almemory_order_release);
- if(althrd_create(&self->thread, ALCpulsePlayback_mixerProc, self) != althrd_success)
- return ALC_FALSE;
- return ALC_TRUE;
- }
- static void ALCpulsePlayback_stop(ALCpulsePlayback *self)
- {
- pa_operation *o;
- int res;
- if(!self->stream || ATOMIC_EXCHANGE(&self->killNow, AL_TRUE, almemory_order_acq_rel))
- return;
- /* Signal the main loop in case PulseAudio isn't sending us audio requests
- * (e.g. if the device is suspended). We need to lock the mainloop in case
- * the mixer is between checking the killNow flag but before waiting for
- * the signal.
- */
- pa_threaded_mainloop_lock(self->loop);
- pa_threaded_mainloop_unlock(self->loop);
- pa_threaded_mainloop_signal(self->loop, 0);
- althrd_join(self->thread, &res);
- pa_threaded_mainloop_lock(self->loop);
- o = pa_stream_cork(self->stream, 1, stream_success_callback, self->loop);
- wait_for_operation(o, self->loop);
- pa_threaded_mainloop_unlock(self->loop);
- }
- static ClockLatency ALCpulsePlayback_getClockLatency(ALCpulsePlayback *self)
- {
- ClockLatency ret;
- pa_usec_t latency;
- int neg, err;
- pa_threaded_mainloop_lock(self->loop);
- ret.ClockTime = GetDeviceClockTime(STATIC_CAST(ALCbackend,self)->mDevice);
- err = pa_stream_get_latency(self->stream, &latency, &neg);
- pa_threaded_mainloop_unlock(self->loop);
- if(UNLIKELY(err != 0))
- {
- /* FIXME: if err = -PA_ERR_NODATA, it means we were called too soon
- * after starting the stream and no timing info has been received from
- * the server yet. Should we wait, possibly stalling the app, or give a
- * dummy value? Either way, it shouldn't be 0. */
- if(err != -PA_ERR_NODATA)
- ERR("Failed to get stream latency: 0x%x\n", err);
- latency = 0;
- neg = 0;
- }
- else if(UNLIKELY(neg))
- latency = 0;
- ret.Latency = (ALint64)minu64(latency, U64(0x7fffffffffffffff)/1000) * 1000;
- return ret;
- }
- static void ALCpulsePlayback_lock(ALCpulsePlayback *self)
- {
- pa_threaded_mainloop_lock(self->loop);
- }
- static void ALCpulsePlayback_unlock(ALCpulsePlayback *self)
- {
- pa_threaded_mainloop_unlock(self->loop);
- }
- typedef struct ALCpulseCapture {
- DERIVE_FROM_TYPE(ALCbackend);
- al_string device_name;
- const void *cap_store;
- size_t cap_len;
- size_t cap_remain;
- ALCuint last_readable;
- pa_buffer_attr attr;
- pa_sample_spec spec;
- pa_threaded_mainloop *loop;
- pa_stream *stream;
- pa_context *context;
- } ALCpulseCapture;
- static void ALCpulseCapture_deviceCallback(pa_context *context, const pa_source_info *info, int eol, void *pdata);
- static void ALCpulseCapture_probeDevices(void);
- static void ALCpulseCapture_contextStateCallback(pa_context *context, void *pdata);
- static void ALCpulseCapture_streamStateCallback(pa_stream *stream, void *pdata);
- static void ALCpulseCapture_sourceNameCallback(pa_context *context, const pa_source_info *info, int eol, void *pdata);
- static void ALCpulseCapture_streamMovedCallback(pa_stream *stream, void *pdata);
- static pa_stream *ALCpulseCapture_connectStream(const char *device_name,
- pa_threaded_mainloop *loop, pa_context *context,
- pa_stream_flags_t flags, pa_buffer_attr *attr,
- pa_sample_spec *spec, pa_channel_map *chanmap);
- static void ALCpulseCapture_Construct(ALCpulseCapture *self, ALCdevice *device);
- static void ALCpulseCapture_Destruct(ALCpulseCapture *self);
- static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name);
- static DECLARE_FORWARD(ALCpulseCapture, ALCbackend, ALCboolean, reset)
- static ALCboolean ALCpulseCapture_start(ALCpulseCapture *self);
- static void ALCpulseCapture_stop(ALCpulseCapture *self);
- static ALCenum ALCpulseCapture_captureSamples(ALCpulseCapture *self, ALCvoid *buffer, ALCuint samples);
- static ALCuint ALCpulseCapture_availableSamples(ALCpulseCapture *self);
- static ClockLatency ALCpulseCapture_getClockLatency(ALCpulseCapture *self);
- static void ALCpulseCapture_lock(ALCpulseCapture *self);
- static void ALCpulseCapture_unlock(ALCpulseCapture *self);
- DECLARE_DEFAULT_ALLOCATORS(ALCpulseCapture)
- DEFINE_ALCBACKEND_VTABLE(ALCpulseCapture);
- static void ALCpulseCapture_Construct(ALCpulseCapture *self, ALCdevice *device)
- {
- ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
- SET_VTABLE2(ALCpulseCapture, ALCbackend, self);
- self->loop = NULL;
- AL_STRING_INIT(self->device_name);
- }
- static void ALCpulseCapture_Destruct(ALCpulseCapture *self)
- {
- if(self->loop)
- {
- pulse_close(self->loop, self->context, self->stream);
- self->loop = NULL;
- self->context = NULL;
- self->stream = NULL;
- }
- AL_STRING_DEINIT(self->device_name);
- ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
- }
- static void ALCpulseCapture_deviceCallback(pa_context *UNUSED(context), const pa_source_info *info, int eol, void *pdata)
- {
- pa_threaded_mainloop *loop = pdata;
- const DevMap *iter;
- DevMap entry;
- int count;
- if(eol)
- {
- pa_threaded_mainloop_signal(loop, 0);
- return;
- }
- #define MATCH_INFO_NAME(iter) (alstr_cmp_cstr((iter)->device_name, info->name) == 0)
- VECTOR_FIND_IF(iter, const DevMap, CaptureDevices, MATCH_INFO_NAME);
- if(iter != VECTOR_END(CaptureDevices)) return;
- #undef MATCH_INFO_NAME
- AL_STRING_INIT(entry.name);
- AL_STRING_INIT(entry.device_name);
- alstr_copy_cstr(&entry.device_name, info->name);
- count = 0;
- while(1)
- {
- alstr_copy_cstr(&entry.name, info->description);
- if(count != 0)
- {
- char str[64];
- snprintf(str, sizeof(str), " #%d", count+1);
- alstr_append_cstr(&entry.name, str);
- }
- #define MATCH_ENTRY(i) (alstr_cmp(entry.name, (i)->name) == 0)
- VECTOR_FIND_IF(iter, const DevMap, CaptureDevices, MATCH_ENTRY);
- if(iter == VECTOR_END(CaptureDevices)) break;
- #undef MATCH_ENTRY
- count++;
- }
- TRACE("Got device \"%s\", \"%s\"\n", alstr_get_cstr(entry.name), alstr_get_cstr(entry.device_name));
- VECTOR_PUSH_BACK(CaptureDevices, entry);
- }
- static void ALCpulseCapture_probeDevices(void)
- {
- pa_threaded_mainloop *loop;
- clear_devlist(&CaptureDevices);
- if((loop=pa_threaded_mainloop_new()) &&
- pa_threaded_mainloop_start(loop) >= 0)
- {
- pa_context *context;
- pa_threaded_mainloop_lock(loop);
- context = connect_context(loop, AL_FALSE);
- if(context)
- {
- pa_operation *o;
- pa_stream_flags_t flags;
- pa_sample_spec spec;
- pa_stream *stream;
- flags = PA_STREAM_FIX_FORMAT | PA_STREAM_FIX_RATE |
- PA_STREAM_FIX_CHANNELS | PA_STREAM_DONT_MOVE;
- spec.format = PA_SAMPLE_S16NE;
- spec.rate = 44100;
- spec.channels = 1;
- stream = ALCpulseCapture_connectStream(NULL, loop, context, flags,
- NULL, &spec, NULL);
- if(stream)
- {
- o = pa_context_get_source_info_by_name(context, pa_stream_get_device_name(stream),
- ALCpulseCapture_deviceCallback, loop);
- wait_for_operation(o, loop);
- pa_stream_disconnect(stream);
- pa_stream_unref(stream);
- stream = NULL;
- }
- o = pa_context_get_source_info_list(context, ALCpulseCapture_deviceCallback, loop);
- wait_for_operation(o, loop);
- pa_context_disconnect(context);
- pa_context_unref(context);
- }
- pa_threaded_mainloop_unlock(loop);
- pa_threaded_mainloop_stop(loop);
- }
- if(loop)
- pa_threaded_mainloop_free(loop);
- }
- static void ALCpulseCapture_contextStateCallback(pa_context *context, void *pdata)
- {
- ALCpulseCapture *self = pdata;
- if(pa_context_get_state(context) == PA_CONTEXT_FAILED)
- {
- ERR("Received context failure!\n");
- aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice, "Capture state failure");
- }
- pa_threaded_mainloop_signal(self->loop, 0);
- }
- static void ALCpulseCapture_streamStateCallback(pa_stream *stream, void *pdata)
- {
- ALCpulseCapture *self = pdata;
- if(pa_stream_get_state(stream) == PA_STREAM_FAILED)
- {
- ERR("Received stream failure!\n");
- aluHandleDisconnect(STATIC_CAST(ALCbackend,self)->mDevice, "Capture stream failure");
- }
- pa_threaded_mainloop_signal(self->loop, 0);
- }
- static void ALCpulseCapture_sourceNameCallback(pa_context *UNUSED(context), const pa_source_info *info, int eol, void *pdata)
- {
- ALCpulseCapture *self = pdata;
- ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
- if(eol)
- {
- pa_threaded_mainloop_signal(self->loop, 0);
- return;
- }
- alstr_copy_cstr(&device->DeviceName, info->description);
- }
- static void ALCpulseCapture_streamMovedCallback(pa_stream *stream, void *pdata)
- {
- ALCpulseCapture *self = pdata;
- alstr_copy_cstr(&self->device_name, pa_stream_get_device_name(stream));
- TRACE("Stream moved to %s\n", alstr_get_cstr(self->device_name));
- }
- static pa_stream *ALCpulseCapture_connectStream(const char *device_name,
- pa_threaded_mainloop *loop, pa_context *context,
- pa_stream_flags_t flags, pa_buffer_attr *attr, pa_sample_spec *spec,
- pa_channel_map *chanmap)
- {
- pa_stream_state_t state;
- pa_stream *stream;
- stream = pa_stream_new_with_proplist(context, "Capture Stream", spec, chanmap, prop_filter);
- if(!stream)
- {
- ERR("pa_stream_new_with_proplist() failed: %s\n", pa_strerror(pa_context_errno(context)));
- return NULL;
- }
- pa_stream_set_state_callback(stream, stream_state_callback, loop);
- if(pa_stream_connect_record(stream, device_name, attr, flags) < 0)
- {
- ERR("Stream did not connect: %s\n", pa_strerror(pa_context_errno(context)));
- pa_stream_unref(stream);
- return NULL;
- }
- while((state=pa_stream_get_state(stream)) != PA_STREAM_READY)
- {
- if(!PA_STREAM_IS_GOOD(state))
- {
- ERR("Stream did not get ready: %s\n", pa_strerror(pa_context_errno(context)));
- pa_stream_unref(stream);
- return NULL;
- }
- pa_threaded_mainloop_wait(loop);
- }
- pa_stream_set_state_callback(stream, NULL, NULL);
- return stream;
- }
- static ALCenum ALCpulseCapture_open(ALCpulseCapture *self, const ALCchar *name)
- {
- ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
- const char *pulse_name = NULL;
- pa_stream_flags_t flags = 0;
- const char *mapname = NULL;
- pa_channel_map chanmap;
- ALuint samples;
- if(name)
- {
- const DevMap *iter;
- if(VECTOR_SIZE(CaptureDevices) == 0)
- ALCpulseCapture_probeDevices();
- #define MATCH_NAME(iter) (alstr_cmp_cstr((iter)->name, name) == 0)
- VECTOR_FIND_IF(iter, const DevMap, CaptureDevices, MATCH_NAME);
- #undef MATCH_NAME
- if(iter == VECTOR_END(CaptureDevices))
- return ALC_INVALID_VALUE;
- pulse_name = alstr_get_cstr(iter->device_name);
- alstr_copy(&device->DeviceName, iter->name);
- }
- if(!pulse_open(&self->loop, &self->context, ALCpulseCapture_contextStateCallback, self))
- return ALC_INVALID_VALUE;
- pa_threaded_mainloop_lock(self->loop);
- switch(device->FmtType)
- {
- case DevFmtUByte:
- self->spec.format = PA_SAMPLE_U8;
- break;
- case DevFmtShort:
- self->spec.format = PA_SAMPLE_S16NE;
- break;
- case DevFmtInt:
- self->spec.format = PA_SAMPLE_S32NE;
- break;
- case DevFmtFloat:
- self->spec.format = PA_SAMPLE_FLOAT32NE;
- break;
- case DevFmtByte:
- case DevFmtUShort:
- case DevFmtUInt:
- ERR("%s capture samples not supported\n", DevFmtTypeString(device->FmtType));
- pa_threaded_mainloop_unlock(self->loop);
- goto fail;
- }
- switch(device->FmtChans)
- {
- case DevFmtMono:
- mapname = "mono";
- break;
- case DevFmtStereo:
- mapname = "front-left,front-right";
- break;
- case DevFmtQuad:
- mapname = "front-left,front-right,rear-left,rear-right";
- break;
- case DevFmtX51:
- mapname = "front-left,front-right,front-center,lfe,side-left,side-right";
- break;
- case DevFmtX51Rear:
- mapname = "front-left,front-right,front-center,lfe,rear-left,rear-right";
- break;
- case DevFmtX61:
- mapname = "front-left,front-right,front-center,lfe,rear-center,side-left,side-right";
- break;
- case DevFmtX71:
- mapname = "front-left,front-right,front-center,lfe,rear-left,rear-right,side-left,side-right";
- break;
- case DevFmtAmbi3D:
- ERR("%s capture samples not supported\n", DevFmtChannelsString(device->FmtChans));
- pa_threaded_mainloop_unlock(self->loop);
- goto fail;
- }
- if(!pa_channel_map_parse(&chanmap, mapname))
- {
- ERR("Failed to build channel map for %s\n", DevFmtChannelsString(device->FmtChans));
- pa_threaded_mainloop_unlock(self->loop);
- return ALC_FALSE;
- }
- self->spec.rate = device->Frequency;
- self->spec.channels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
- if(pa_sample_spec_valid(&self->spec) == 0)
- {
- ERR("Invalid sample format\n");
- pa_threaded_mainloop_unlock(self->loop);
- goto fail;
- }
- if(!pa_channel_map_init_auto(&chanmap, self->spec.channels, PA_CHANNEL_MAP_WAVEEX))
- {
- ERR("Couldn't build map for channel count (%d)!\n", self->spec.channels);
- pa_threaded_mainloop_unlock(self->loop);
- goto fail;
- }
- samples = device->UpdateSize * device->NumUpdates;
- samples = maxu(samples, 100 * device->Frequency / 1000);
- self->attr.minreq = -1;
- self->attr.prebuf = -1;
- self->attr.maxlength = samples * pa_frame_size(&self->spec);
- self->attr.tlength = -1;
- self->attr.fragsize = minu(samples, 50*device->Frequency/1000) *
- pa_frame_size(&self->spec);
- flags |= PA_STREAM_START_CORKED|PA_STREAM_ADJUST_LATENCY;
- if(!GetConfigValueBool(NULL, "pulse", "allow-moves", 0))
- flags |= PA_STREAM_DONT_MOVE;
- TRACE("Connecting to \"%s\"\n", pulse_name ? pulse_name : "(default)");
- self->stream = ALCpulseCapture_connectStream(pulse_name,
- self->loop, self->context, flags, &self->attr, &self->spec, &chanmap
- );
- if(!self->stream)
- {
- pa_threaded_mainloop_unlock(self->loop);
- goto fail;
- }
- pa_stream_set_moved_callback(self->stream, ALCpulseCapture_streamMovedCallback, self);
- pa_stream_set_state_callback(self->stream, ALCpulseCapture_streamStateCallback, self);
- alstr_copy_cstr(&self->device_name, pa_stream_get_device_name(self->stream));
- if(alstr_empty(device->DeviceName))
- {
- pa_operation *o = pa_context_get_source_info_by_name(
- self->context, alstr_get_cstr(self->device_name),
- ALCpulseCapture_sourceNameCallback, self
- );
- wait_for_operation(o, self->loop);
- }
- pa_threaded_mainloop_unlock(self->loop);
- return ALC_NO_ERROR;
- fail:
- pulse_close(self->loop, self->context, self->stream);
- self->loop = NULL;
- self->context = NULL;
- self->stream = NULL;
- return ALC_INVALID_VALUE;
- }
- static ALCboolean ALCpulseCapture_start(ALCpulseCapture *self)
- {
- pa_operation *o;
- pa_threaded_mainloop_lock(self->loop);
- o = pa_stream_cork(self->stream, 0, stream_success_callback, self->loop);
- wait_for_operation(o, self->loop);
- pa_threaded_mainloop_unlock(self->loop);
- return ALC_TRUE;
- }
- static void ALCpulseCapture_stop(ALCpulseCapture *self)
- {
- pa_operation *o;
- pa_threaded_mainloop_lock(self->loop);
- o = pa_stream_cork(self->stream, 1, stream_success_callback, self->loop);
- wait_for_operation(o, self->loop);
- pa_threaded_mainloop_unlock(self->loop);
- }
- static ALCenum ALCpulseCapture_captureSamples(ALCpulseCapture *self, ALCvoid *buffer, ALCuint samples)
- {
- ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
- ALCuint todo = samples * pa_frame_size(&self->spec);
- /* Capture is done in fragment-sized chunks, so we loop until we get all
- * that's available */
- self->last_readable -= todo;
- pa_threaded_mainloop_lock(self->loop);
- while(todo > 0)
- {
- size_t rem = todo;
- if(self->cap_len == 0)
- {
- pa_stream_state_t state;
- state = pa_stream_get_state(self->stream);
- if(!PA_STREAM_IS_GOOD(state))
- {
- aluHandleDisconnect(device, "Bad capture state: %u", state);
- break;
- }
- if(pa_stream_peek(self->stream, &self->cap_store, &self->cap_len) < 0)
- {
- ERR("pa_stream_peek() failed: %s\n",
- pa_strerror(pa_context_errno(self->context)));
- aluHandleDisconnect(device, "Failed retrieving capture samples: %s",
- pa_strerror(pa_context_errno(self->context)));
- break;
- }
- self->cap_remain = self->cap_len;
- }
- if(rem > self->cap_remain)
- rem = self->cap_remain;
- memcpy(buffer, self->cap_store, rem);
- buffer = (ALbyte*)buffer + rem;
- todo -= rem;
- self->cap_store = (ALbyte*)self->cap_store + rem;
- self->cap_remain -= rem;
- if(self->cap_remain == 0)
- {
- pa_stream_drop(self->stream);
- self->cap_len = 0;
- }
- }
- pa_threaded_mainloop_unlock(self->loop);
- if(todo > 0)
- memset(buffer, ((device->FmtType==DevFmtUByte) ? 0x80 : 0), todo);
- return ALC_NO_ERROR;
- }
- static ALCuint ALCpulseCapture_availableSamples(ALCpulseCapture *self)
- {
- ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
- size_t readable = self->cap_remain;
- if(ATOMIC_LOAD(&device->Connected, almemory_order_acquire))
- {
- ssize_t got;
- pa_threaded_mainloop_lock(self->loop);
- got = pa_stream_readable_size(self->stream);
- if(got < 0)
- {
- ERR("pa_stream_readable_size() failed: %s\n", pa_strerror(got));
- aluHandleDisconnect(device, "Failed getting readable size: %s", pa_strerror(got));
- }
- else if((size_t)got > self->cap_len)
- readable += got - self->cap_len;
- pa_threaded_mainloop_unlock(self->loop);
- }
- if(self->last_readable < readable)
- self->last_readable = readable;
- return self->last_readable / pa_frame_size(&self->spec);
- }
- static ClockLatency ALCpulseCapture_getClockLatency(ALCpulseCapture *self)
- {
- ClockLatency ret;
- pa_usec_t latency;
- int neg, err;
- pa_threaded_mainloop_lock(self->loop);
- ret.ClockTime = GetDeviceClockTime(STATIC_CAST(ALCbackend,self)->mDevice);
- err = pa_stream_get_latency(self->stream, &latency, &neg);
- pa_threaded_mainloop_unlock(self->loop);
- if(UNLIKELY(err != 0))
- {
- ERR("Failed to get stream latency: 0x%x\n", err);
- latency = 0;
- neg = 0;
- }
- else if(UNLIKELY(neg))
- latency = 0;
- ret.Latency = (ALint64)minu64(latency, U64(0x7fffffffffffffff)/1000) * 1000;
- return ret;
- }
- static void ALCpulseCapture_lock(ALCpulseCapture *self)
- {
- pa_threaded_mainloop_lock(self->loop);
- }
- static void ALCpulseCapture_unlock(ALCpulseCapture *self)
- {
- pa_threaded_mainloop_unlock(self->loop);
- }
- typedef struct ALCpulseBackendFactory {
- DERIVE_FROM_TYPE(ALCbackendFactory);
- } ALCpulseBackendFactory;
- #define ALCPULSEBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCpulseBackendFactory, ALCbackendFactory) } }
- static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory *self);
- static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory *self);
- static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory *self, ALCbackend_Type type);
- static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory *self, enum DevProbe type, al_string *outnames);
- static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
- DEFINE_ALCBACKENDFACTORY_VTABLE(ALCpulseBackendFactory);
- static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory* UNUSED(self))
- {
- ALCboolean ret = ALC_FALSE;
- VECTOR_INIT(PlaybackDevices);
- VECTOR_INIT(CaptureDevices);
- if(pulse_load())
- {
- pa_threaded_mainloop *loop;
- pulse_ctx_flags = 0;
- if(!GetConfigValueBool(NULL, "pulse", "spawn-server", 1))
- pulse_ctx_flags |= PA_CONTEXT_NOAUTOSPAWN;
- if((loop=pa_threaded_mainloop_new()) &&
- pa_threaded_mainloop_start(loop) >= 0)
- {
- pa_context *context;
- pa_threaded_mainloop_lock(loop);
- context = connect_context(loop, AL_TRUE);
- if(context)
- {
- ret = ALC_TRUE;
- /* Some libraries (Phonon, Qt) set some pulseaudio properties
- * through environment variables, which causes all streams in
- * the process to inherit them. This attempts to filter those
- * properties out by setting them to 0-length data. */
- prop_filter = pa_proplist_new();
- pa_proplist_set(prop_filter, PA_PROP_MEDIA_ROLE, NULL, 0);
- pa_proplist_set(prop_filter, "phonon.streamid", NULL, 0);
- pa_context_disconnect(context);
- pa_context_unref(context);
- }
- pa_threaded_mainloop_unlock(loop);
- pa_threaded_mainloop_stop(loop);
- }
- if(loop)
- pa_threaded_mainloop_free(loop);
- }
- return ret;
- }
- static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory* UNUSED(self))
- {
- clear_devlist(&PlaybackDevices);
- VECTOR_DEINIT(PlaybackDevices);
- clear_devlist(&CaptureDevices);
- VECTOR_DEINIT(CaptureDevices);
- if(prop_filter)
- pa_proplist_free(prop_filter);
- prop_filter = NULL;
- /* PulseAudio doesn't like being CloseLib'd sometimes */
- }
- static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory* UNUSED(self), ALCbackend_Type type)
- {
- if(type == ALCbackend_Playback || type == ALCbackend_Capture)
- return ALC_TRUE;
- return ALC_FALSE;
- }
- static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory* UNUSED(self), enum DevProbe type, al_string *outnames)
- {
- switch(type)
- {
- #define APPEND_OUTNAME(e) do { \
- if(!alstr_empty((e)->name)) \
- alstr_append_range(outnames, VECTOR_BEGIN((e)->name), \
- VECTOR_END((e)->name)+1); \
- } while(0)
- case ALL_DEVICE_PROBE:
- ALCpulsePlayback_probeDevices();
- VECTOR_FOR_EACH(const DevMap, PlaybackDevices, APPEND_OUTNAME);
- break;
- case CAPTURE_DEVICE_PROBE:
- ALCpulseCapture_probeDevices();
- VECTOR_FOR_EACH(const DevMap, CaptureDevices, APPEND_OUTNAME);
- break;
- #undef APPEND_OUTNAME
- }
- }
- static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
- {
- if(type == ALCbackend_Playback)
- {
- ALCpulsePlayback *backend;
- NEW_OBJ(backend, ALCpulsePlayback)(device);
- if(!backend) return NULL;
- return STATIC_CAST(ALCbackend, backend);
- }
- if(type == ALCbackend_Capture)
- {
- ALCpulseCapture *backend;
- NEW_OBJ(backend, ALCpulseCapture)(device);
- if(!backend) return NULL;
- return STATIC_CAST(ALCbackend, backend);
- }
- return NULL;
- }
- #else /* PA_API_VERSION == 12 */
- #warning "Unsupported API version, backend will be unavailable!"
- typedef struct ALCpulseBackendFactory {
- DERIVE_FROM_TYPE(ALCbackendFactory);
- } ALCpulseBackendFactory;
- #define ALCPULSEBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCpulseBackendFactory, ALCbackendFactory) } }
- static ALCboolean ALCpulseBackendFactory_init(ALCpulseBackendFactory* UNUSED(self))
- {
- return ALC_FALSE;
- }
- static void ALCpulseBackendFactory_deinit(ALCpulseBackendFactory* UNUSED(self))
- {
- }
- static ALCboolean ALCpulseBackendFactory_querySupport(ALCpulseBackendFactory* UNUSED(self), ALCbackend_Type UNUSED(type))
- {
- return ALC_FALSE;
- }
- static void ALCpulseBackendFactory_probe(ALCpulseBackendFactory* UNUSED(self), enum DevProbe UNUSED(type), al_string* UNUSED(outnames))
- {
- }
- static ALCbackend* ALCpulseBackendFactory_createBackend(ALCpulseBackendFactory* UNUSED(self), ALCdevice* UNUSED(device), ALCbackend_Type UNUSED(type))
- {
- return NULL;
- }
- DEFINE_ALCBACKENDFACTORY_VTABLE(ALCpulseBackendFactory);
- #endif /* PA_API_VERSION == 12 */
- ALCbackendFactory *ALCpulseBackendFactory_getFactory(void)
- {
- static ALCpulseBackendFactory factory = ALCPULSEBACKENDFACTORY_INITIALIZER;
- return STATIC_CAST(ALCbackendFactory, &factory);
- }
|